22import platform
33import sys , ctypes , ctypes .util
44from cffi import FFI , CDefError , FFIError , VerificationMissing
5- from testing .support import *
65
76SIZE_OF_INT = ctypes .sizeof (ctypes .c_int )
87SIZE_OF_LONG = ctypes .sizeof (ctypes .c_long )
@@ -311,7 +310,7 @@ def test_char(self):
311310 assert bool (ffi .cast ("char" , 1 ))
312311 assert bool (ffi .cast ("char" , 255 ))
313312 pytest .raises (TypeError , ffi .new , "char*" , 32 )
314- pytest .raises (TypeError , ffi .new , "char*" , u + "x" )
313+ pytest .raises (TypeError , ffi .new , "char*" , "x" )
315314 pytest .raises (TypeError , ffi .new , "char*" , b"foo" )
316315 #
317316 p = ffi .new ("char[]" , [b'a' , b'b' , b'\x9c ' ])
@@ -342,13 +341,13 @@ def check_wchar_t(self, ffi):
342341 def test_wchar_t (self ):
343342 ffi = FFI (backend = self .Backend ())
344343 self .check_wchar_t (ffi )
345- assert ffi .new ("wchar_t*" , u + 'x' )[0 ] == u + 'x'
346- assert ffi .new ("wchar_t*" , u + '\u1234 ' )[0 ] == u + '\u1234 '
344+ assert ffi .new ("wchar_t*" , 'x' )[0 ] == 'x'
345+ assert ffi .new ("wchar_t*" , '\u1234 ' )[0 ] == '\u1234 '
347346 if SIZE_OF_WCHAR > 2 :
348- assert ffi .new ("wchar_t*" , u + '\U00012345 ' )[0 ] == u + '\U00012345 '
347+ assert ffi .new ("wchar_t*" , '\U00012345 ' )[0 ] == '\U00012345 '
349348 else :
350- pytest .raises (TypeError , ffi .new , "wchar_t*" , u + '\U00012345 ' )
351- assert ffi .new ("wchar_t*" )[0 ] == u + '\x00 '
349+ pytest .raises (TypeError , ffi .new , "wchar_t*" , '\U00012345 ' )
350+ assert ffi .new ("wchar_t*" )[0 ] == '\x00 '
352351 assert int (ffi .cast ("wchar_t" , 300 )) == 300
353352 assert not bool (ffi .cast ("wchar_t" , 0 ))
354353 assert bool (ffi .cast ("wchar_t" , 1 ))
@@ -358,40 +357,40 @@ def test_wchar_t(self):
358357 pytest .raises (TypeError , ffi .new , "wchar_t*" , 32 )
359358 pytest .raises (TypeError , ffi .new , "wchar_t*" , "foo" )
360359 #
361- p = ffi .new ("wchar_t[]" , [u + 'a' , u + 'b' , u + '\u1234 ' ])
360+ p = ffi .new ("wchar_t[]" , ['a' , 'b' , '\u1234 ' ])
362361 assert len (p ) == 3
363- assert p [0 ] == u + 'a'
364- assert p [1 ] == u + 'b' and type (p [1 ]) is unicode
365- assert p [2 ] == u + '\u1234 '
366- p [0 ] = u + 'x'
367- assert p [0 ] == u + 'x' and type (p [0 ]) is unicode
368- p [1 ] = u + '\u1357 '
369- assert p [1 ] == u + '\u1357 '
370- p = ffi .new ("wchar_t[]" , u + "abcd" )
362+ assert p [0 ] == 'a'
363+ assert p [1 ] == 'b' and type (p [1 ]) is str
364+ assert p [2 ] == '\u1234 '
365+ p [0 ] = 'x'
366+ assert p [0 ] == 'x' and type (p [0 ]) is str
367+ p [1 ] = '\u1357 '
368+ assert p [1 ] == '\u1357 '
369+ p = ffi .new ("wchar_t[]" , "abcd" )
371370 assert len (p ) == 5
372- assert p [4 ] == u + '\x00 '
373- p = ffi .new ("wchar_t[]" , u + "a\u1234 b" )
371+ assert p [4 ] == '\x00 '
372+ p = ffi .new ("wchar_t[]" , "a\u1234 b" )
374373 assert len (p ) == 4
375- assert p [1 ] == u + '\u1234 '
374+ assert p [1 ] == '\u1234 '
376375 #
377- p = ffi .new ("wchar_t[]" , u + '\U00023456 ' )
376+ p = ffi .new ("wchar_t[]" , '\U00023456 ' )
378377 if SIZE_OF_WCHAR == 2 :
379378 assert len (p ) == 3
380- assert p [0 ] == u + '\ud84d '
381- assert p [1 ] == u + '\udc56 '
382- assert p [2 ] == u + '\x00 '
379+ assert p [0 ] == '\ud84d '
380+ assert p [1 ] == '\udc56 '
381+ assert p [2 ] == '\x00 '
383382 else :
384383 assert len (p ) == 2
385- assert p [0 ] == u + '\U00023456 '
386- assert p [1 ] == u + '\x00 '
384+ assert p [0 ] == '\U00023456 '
385+ assert p [1 ] == '\x00 '
387386 #
388- p = ffi .new ("wchar_t[4]" , u + "ab" )
387+ p = ffi .new ("wchar_t[4]" , "ab" )
389388 assert len (p ) == 4
390- assert [p [i ] for i in range (4 )] == [u + 'a' , u + 'b' , u + '\x00 ' , u + '\x00 ' ]
391- p = ffi .new ("wchar_t[2]" , u + "ab" )
389+ assert [p [i ] for i in range (4 )] == ['a' , 'b' , '\x00 ' , '\x00 ' ]
390+ p = ffi .new ("wchar_t[2]" , "ab" )
392391 assert len (p ) == 2
393- assert [p [i ] for i in range (2 )] == [u + 'a' , u + 'b' ]
394- pytest .raises (IndexError , ffi .new , "wchar_t[2]" , u + "abc" )
392+ assert [p [i ] for i in range (2 )] == ['a' , 'b' ]
393+ pytest .raises (IndexError , ffi .new , "wchar_t[2]" , "abc" )
395394
396395 def test_none_as_null_doesnt_work (self ):
397396 ffi = FFI (backend = self .Backend ())
@@ -596,15 +595,15 @@ def test_string_from_char_pointer(self):
596595 assert str (x ) == repr (x )
597596 assert ffi .string (x ) == b"x"
598597 assert ffi .string (ffi .new ("char*" , b"\x00 " )) == b""
599- pytest .raises (TypeError , ffi .new , "char*" , unicode ( "foo" ) )
598+ pytest .raises (TypeError , ffi .new , "char*" , "foo" )
600599
601- def test_unicode_from_wchar_pointer (self ):
600+ def test_string_from_wchar_pointer (self ):
602601 ffi = FFI (backend = self .Backend ())
603602 self .check_wchar_t (ffi )
604- x = ffi .new ("wchar_t*" , u + "x" )
605- assert unicode (x ) == unicode (repr (x ))
606- assert ffi .string (x ) == u + "x"
607- assert ffi .string (ffi .new ("wchar_t*" , u + "\x00 " )) == u + ""
603+ x = ffi .new ("wchar_t*" , "x" )
604+ assert str (x ) == str (repr (x ))
605+ assert ffi .string (x ) == "x"
606+ assert ffi .string (ffi .new ("wchar_t*" , "\x00 " )) == ""
608607
609608 def test_string_from_char_array (self ):
610609 ffi = FFI (backend = self .Backend ())
@@ -627,38 +626,38 @@ def test_string_from_char_array(self):
627626 def test_string_from_wchar_array (self ):
628627 ffi = FFI (backend = self .Backend ())
629628 self .check_wchar_t (ffi )
630- assert ffi .string (ffi .cast ("wchar_t" , "x" )) == u + "x"
631- assert ffi .string (ffi .cast ("wchar_t" , u + "x" )) == u + "x"
629+ assert ffi .string (ffi .cast ("wchar_t" , "x" )) == "x"
630+ assert ffi .string (ffi .cast ("wchar_t" , "x" )) == "x"
632631 x = ffi .cast ("wchar_t" , "x" )
633632 assert str (x ) == repr (x )
634- assert ffi .string (x ) == u + "x"
633+ assert ffi .string (x ) == "x"
635634 #
636- p = ffi .new ("wchar_t[]" , u + "hello." )
637- p [5 ] = u + '!'
638- assert ffi .string (p ) == u + "hello!"
639- p [6 ] = u + '\u04d2 '
640- assert ffi .string (p ) == u + "hello!\u04d2 "
641- p [3 ] = u + '\x00 '
642- assert ffi .string (p ) == u + "hel"
643- assert ffi .string (p , 123 ) == u + "hel"
635+ p = ffi .new ("wchar_t[]" , "hello." )
636+ p [5 ] = '!'
637+ assert ffi .string (p ) == "hello!"
638+ p [6 ] = '\u04d2 '
639+ assert ffi .string (p ) == "hello!\u04d2 "
640+ p [3 ] = '\x00 '
641+ assert ffi .string (p ) == "hel"
642+ assert ffi .string (p , 123 ) == "hel"
644643 with pytest .raises (IndexError ):
645- p [7 ] = u + 'X'
644+ p [7 ] = 'X'
646645 #
647- a = ffi .new ("wchar_t[]" , u + "hello\x00 world" )
646+ a = ffi .new ("wchar_t[]" , "hello\x00 world" )
648647 assert len (a ) == 12
649648 p = ffi .cast ("wchar_t *" , a )
650- assert ffi .string (p ) == u + 'hello'
651- assert ffi .string (p , 123 ) == u + 'hello'
652- assert ffi .string (p , 5 ) == u + 'hello'
653- assert ffi .string (p , 2 ) == u + 'he'
649+ assert ffi .string (p ) == 'hello'
650+ assert ffi .string (p , 123 ) == 'hello'
651+ assert ffi .string (p , 5 ) == 'hello'
652+ assert ffi .string (p , 2 ) == 'he'
654653
655654 def test_fetch_const_char_p_field (self ):
656655 # 'const' is ignored so far
657656 ffi = FFI (backend = self .Backend ())
658657 ffi .cdef ("struct foo { const char *name; };" )
659658 t = ffi .new ("const char[]" , b"testing" )
660659 s = ffi .new ("struct foo*" , [t ])
661- assert type (s .name ) not in (bytes , str , unicode )
660+ assert type (s .name ) not in (bytes , str )
662661 assert ffi .string (s .name ) == b"testing"
663662 with pytest .raises (TypeError ):
664663 s .name = None
@@ -670,10 +669,10 @@ def test_fetch_const_wchar_p_field(self):
670669 ffi = FFI (backend = self .Backend ())
671670 self .check_wchar_t (ffi )
672671 ffi .cdef ("struct foo { const wchar_t *name; };" )
673- t = ffi .new ("const wchar_t[]" , u + "testing" )
672+ t = ffi .new ("const wchar_t[]" , "testing" )
674673 s = ffi .new ("struct foo*" , [t ])
675- assert type (s .name ) not in (bytes , str , unicode )
676- assert ffi .string (s .name ) == u + "testing"
674+ assert type (s .name ) not in (bytes , str )
675+ assert ffi .string (s .name ) == "testing"
677676 s .name = ffi .NULL
678677 assert s .name == ffi .NULL
679678
@@ -793,7 +792,7 @@ def test_char_cast(self):
793792 def test_wchar_cast (self ):
794793 ffi = FFI (backend = self .Backend ())
795794 self .check_wchar_t (ffi )
796- p = ffi .cast ("int" , ffi .cast ("wchar_t" , u + '\u1234 ' ))
795+ p = ffi .cast ("int" , ffi .cast ("wchar_t" , '\u1234 ' ))
797796 assert int (p ) == 0x1234
798797 p = ffi .cast ("long long" , ffi .cast ("wchar_t" , - 1 ))
799798 if SIZE_OF_WCHAR == 2 : # 2 bytes, unsigned
@@ -803,7 +802,7 @@ def test_wchar_cast(self):
803802 assert int (p ) == - 1
804803 else : # in general, it can be either signed or not
805804 assert int (p ) in [- 1 , 0xffffffff ] # e.g. on arm, both cases occur
806- p = ffi .cast ("int" , u + '\u1234 ' )
805+ p = ffi .cast ("int" , '\u1234 ' )
807806 assert int (p ) == 0x1234
808807
809808 def test_cast_array_to_charp (self ):
@@ -1250,7 +1249,7 @@ def test_ffi_buffer_with_file(self):
12501249 pytest .skip (str (e ))
12511250 f .write (ffi .buffer (a , 1000 * ffi .sizeof ("int" )))
12521251 f .seek (0 )
1253- assert f .read () == arraytostring ( array .array ('i' , range (1000 )))
1252+ assert f .read () == array .array ('i' , range (1000 )). tobytes ( )
12541253 f .seek (0 )
12551254 b = ffi .new ("int[]" , 1005 )
12561255 f .readinto (ffi .buffer (b , 1000 * ffi .sizeof ("int" )))
@@ -1269,7 +1268,7 @@ def test_ffi_buffer_with_io(self):
12691268 pytest .skip (str (e ))
12701269 f .write (ffi .buffer (a , 1000 * ffi .sizeof ("int" )))
12711270 f .seek (0 )
1272- assert f .read () == arraytostring ( array .array ('i' , range (1000 )))
1271+ assert f .read () == array .array ('i' , range (1000 )). tobytes ( )
12731272 f .seek (0 )
12741273 b = ffi .new ("int[]" , 1005 )
12751274 f .readinto (ffi .buffer (b , 1000 * ffi .sizeof ("int" )))
0 commit comments