33from .error import CDefError
44from . import model
55
6- try :
7- basestring
8- except NameError :
9- # Python 3.x
10- basestring = str
11-
126_unspecified = object ()
137
148
@@ -36,8 +30,7 @@ def __init__(self, backend=None):
3630 select a non-default backend, mostly for tests.
3731 """
3832 if backend is None :
39- # You need PyPy (>= 2.0 beta), or a CPython (>= 2.6) with
40- # _cffi_backend.so compiled.
33+ # You need PyPy or a CPython with _cffi_backend.so compiled.
4134 import _cffi_backend as backend
4235 from . import __version__
4336 if backend .__version__ != __version__ :
@@ -111,7 +104,7 @@ def embedding_api(self, csource, packed=False, pack=None):
111104
112105 def _cdef (self , csource , override = False , ** options ):
113106 if not isinstance (csource , str ): # unicode, on Python 2
114- if not isinstance (csource , basestring ):
107+ if not isinstance (csource , str ):
115108 raise TypeError ("cdef() argument must be a string" )
116109 csource = csource .encode ('ascii' )
117110 with self ._lock :
@@ -134,7 +127,7 @@ def dlopen(self, name, flags=0):
134127 linked to a particular library, just like C headers; in the
135128 library we only look for the actual (untyped) symbols.
136129 """
137- if not (isinstance (name , basestring ) or
130+ if not (isinstance (name , str ) or
138131 name is None or
139132 isinstance (name , self .CData )):
140133 raise TypeError ("dlopen(name): name must be a file name, None, "
@@ -189,7 +182,7 @@ def typeof(self, cdecl):
189182 corresponding <ctype> object.
190183 It can also be used on 'cdata' instance to get its C type.
191184 """
192- if isinstance (cdecl , basestring ):
185+ if isinstance (cdecl , str ):
193186 return self ._typeof (cdecl )
194187 if isinstance (cdecl , self .CData ):
195188 return self ._backend .typeof (cdecl )
@@ -207,7 +200,7 @@ def sizeof(self, cdecl):
207200 """Return the size in bytes of the argument. It can be a
208201 string naming a C type, or a 'cdata' instance.
209202 """
210- if isinstance (cdecl , basestring ):
203+ if isinstance (cdecl , str ):
211204 BType = self ._typeof (cdecl )
212205 return self ._backend .sizeof (BType )
213206 else :
@@ -217,7 +210,7 @@ def alignof(self, cdecl):
217210 """Return the natural alignment size in bytes of the C type
218211 given as a string.
219212 """
220- if isinstance (cdecl , basestring ):
213+ if isinstance (cdecl , str ):
221214 cdecl = self ._typeof (cdecl )
222215 return self ._backend .alignof (cdecl )
223216
@@ -228,7 +221,7 @@ def offsetof(self, cdecl, *fields_or_indexes):
228221 You can also give numeric values which correspond to array
229222 items, in case of an array type.
230223 """
231- if isinstance (cdecl , basestring ):
224+ if isinstance (cdecl , str ):
232225 cdecl = self ._typeof (cdecl )
233226 return self ._typeoffsetof (cdecl , * fields_or_indexes )[1 ]
234227
@@ -255,7 +248,7 @@ def new(self, cdecl, init=None):
255248 about that when copying the pointer to the memory somewhere
256249 else, e.g. into another structure.
257250 """
258- if isinstance (cdecl , basestring ):
251+ if isinstance (cdecl , str ):
259252 cdecl = self ._typeof (cdecl )
260253 return self ._backend .newp (cdecl , init )
261254
@@ -278,7 +271,7 @@ def new_allocator(self, alloc=None, free=None,
278271 allocator = compiled_ffi .new_allocator (alloc , free ,
279272 should_clear_after_alloc )
280273 def allocate (cdecl , init = None ):
281- if isinstance (cdecl , basestring ):
274+ if isinstance (cdecl , str ):
282275 cdecl = self ._typeof (cdecl )
283276 return allocator (cdecl , init )
284277 return allocate
@@ -288,7 +281,7 @@ def cast(self, cdecl, source):
288281 type initialized with the given 'source'. The source is
289282 casted between integers or pointers of any type.
290283 """
291- if isinstance (cdecl , basestring ):
284+ if isinstance (cdecl , str ):
292285 cdecl = self ._typeof (cdecl )
293286 return self ._backend .cast (cdecl , source )
294287
@@ -353,7 +346,7 @@ def from_buffer(self, cdecl, python_buffer=_unspecified,
353346 """
354347 if python_buffer is _unspecified :
355348 cdecl , python_buffer = self .BCharA , cdecl
356- elif isinstance (cdecl , basestring ):
349+ elif isinstance (cdecl , str ):
357350 cdecl = self ._typeof (cdecl )
358351 return self ._backend .from_buffer (cdecl , python_buffer ,
359352 require_writable )
@@ -388,7 +381,7 @@ def callback_decorator_wrap(python_callable):
388381 "is not callable" )
389382 return self ._backend .callback (cdecl , python_callable ,
390383 error , onerror )
391- if isinstance (cdecl , basestring ):
384+ if isinstance (cdecl , str ):
392385 cdecl = self ._typeof (cdecl , consider_function_as_funcptr = True )
393386 if python_callable is None :
394387 return callback_decorator_wrap # decorator mode
@@ -401,7 +394,7 @@ def getctype(self, cdecl, replace_with=''):
401394 extra text to append (or insert for more complicated C types), like
402395 a variable name, or '*' to get actually the C type 'pointer-to-cdecl'.
403396 """
404- if isinstance (cdecl , basestring ):
397+ if isinstance (cdecl , str ):
405398 cdecl = self ._typeof (cdecl )
406399 replace_with = replace_with .strip ()
407400 if (replace_with .startswith ('*' )
@@ -609,10 +602,7 @@ def ensure(key, value):
609602 if hasattr (sys , 'gettotalrefcount' ):
610603 template += '_d'
611604 else :
612- try :
613- import sysconfig
614- except ImportError : # 2.6
615- from cffi ._shimmed_dist_utils import sysconfig
605+ import sysconfig
616606 template = "python%d.%d"
617607 if sysconfig .get_config_var ('DEBUG_EXT' ):
618608 template += sysconfig .get_config_var ('DEBUG_EXT' )
@@ -629,7 +619,7 @@ def set_source(self, module_name, source, source_extension='.c', **kwds):
629619 if hasattr (self , '_assigned_source' ):
630620 raise ValueError ("set_source() cannot be called several times "
631621 "per ffi object" )
632- if not isinstance (module_name , basestring ):
622+ if not isinstance (module_name , str ):
633623 raise TypeError ("'module_name' must be a string" )
634624 if os .sep in module_name or (os .altsep and os .altsep in module_name ):
635625 raise ValueError ("'module_name' must not contain '/': use a dotted "
@@ -795,7 +785,7 @@ def list_types(self):
795785
796786def _load_backend_lib (backend , name , flags ):
797787 import os
798- if not isinstance (name , basestring ):
788+ if not isinstance (name , str ):
799789 if sys .platform != "win32" or name is not None :
800790 return backend .load_library (name , flags )
801791 name = "c" # Windows: load_library(None) fails, but this works
@@ -809,8 +799,8 @@ def _load_backend_lib(backend, name, flags):
809799 import ctypes .util
810800 path = ctypes .util .find_library (name )
811801 if path is None :
812- if name == "c" and sys .platform == "win32" and sys . version_info >= ( 3 ,) :
813- raise OSError ("dlopen(None) cannot work on Windows for Python 3 "
802+ if name == "c" and sys .platform == "win32" :
803+ raise OSError ("dlopen(None) cannot work on Windows "
814804 "(see http://bugs.python.org/issue23606)" )
815805 msg = ("ctypes.util.find_library() did not manage "
816806 "to locate a library called %r" % (name ,))
@@ -931,7 +921,7 @@ def __cffi_close__(self):
931921 backendlib .close_lib ()
932922 self .__dict__ .clear ()
933923 #
934- if isinstance (libname , basestring ):
924+ if isinstance (libname , str ):
935925 try :
936926 if not isinstance (libname , str ): # unicode, on Python 2
937927 libname = libname .encode ('utf-8' )
0 commit comments