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__ :
@@ -108,7 +101,7 @@ def embedding_api(self, csource, packed=False, pack=None):
108101
109102 def _cdef (self , csource , override = False , ** options ):
110103 if not isinstance (csource , str ): # unicode, on Python 2
111- if not isinstance (csource , basestring ):
104+ if not isinstance (csource , str ):
112105 raise TypeError ("cdef() argument must be a string" )
113106 csource = csource .encode ('ascii' )
114107 with self ._lock :
@@ -131,7 +124,7 @@ def dlopen(self, name, flags=0):
131124 linked to a particular library, just like C headers; in the
132125 library we only look for the actual (untyped) symbols.
133126 """
134- if not (isinstance (name , basestring ) or
127+ if not (isinstance (name , str ) or
135128 name is None or
136129 isinstance (name , self .CData )):
137130 raise TypeError ("dlopen(name): name must be a file name, None, "
@@ -186,7 +179,7 @@ def typeof(self, cdecl):
186179 corresponding <ctype> object.
187180 It can also be used on 'cdata' instance to get its C type.
188181 """
189- if isinstance (cdecl , basestring ):
182+ if isinstance (cdecl , str ):
190183 return self ._typeof (cdecl )
191184 if isinstance (cdecl , self .CData ):
192185 return self ._backend .typeof (cdecl )
@@ -204,7 +197,7 @@ def sizeof(self, cdecl):
204197 """Return the size in bytes of the argument. It can be a
205198 string naming a C type, or a 'cdata' instance.
206199 """
207- if isinstance (cdecl , basestring ):
200+ if isinstance (cdecl , str ):
208201 BType = self ._typeof (cdecl )
209202 return self ._backend .sizeof (BType )
210203 else :
@@ -214,7 +207,7 @@ def alignof(self, cdecl):
214207 """Return the natural alignment size in bytes of the C type
215208 given as a string.
216209 """
217- if isinstance (cdecl , basestring ):
210+ if isinstance (cdecl , str ):
218211 cdecl = self ._typeof (cdecl )
219212 return self ._backend .alignof (cdecl )
220213
@@ -225,7 +218,7 @@ def offsetof(self, cdecl, *fields_or_indexes):
225218 You can also give numeric values which correspond to array
226219 items, in case of an array type.
227220 """
228- if isinstance (cdecl , basestring ):
221+ if isinstance (cdecl , str ):
229222 cdecl = self ._typeof (cdecl )
230223 return self ._typeoffsetof (cdecl , * fields_or_indexes )[1 ]
231224
@@ -252,7 +245,7 @@ def new(self, cdecl, init=None):
252245 about that when copying the pointer to the memory somewhere
253246 else, e.g. into another structure.
254247 """
255- if isinstance (cdecl , basestring ):
248+ if isinstance (cdecl , str ):
256249 cdecl = self ._typeof (cdecl )
257250 return self ._backend .newp (cdecl , init )
258251
@@ -275,7 +268,7 @@ def new_allocator(self, alloc=None, free=None,
275268 allocator = compiled_ffi .new_allocator (alloc , free ,
276269 should_clear_after_alloc )
277270 def allocate (cdecl , init = None ):
278- if isinstance (cdecl , basestring ):
271+ if isinstance (cdecl , str ):
279272 cdecl = self ._typeof (cdecl )
280273 return allocator (cdecl , init )
281274 return allocate
@@ -285,7 +278,7 @@ def cast(self, cdecl, source):
285278 type initialized with the given 'source'. The source is
286279 casted between integers or pointers of any type.
287280 """
288- if isinstance (cdecl , basestring ):
281+ if isinstance (cdecl , str ):
289282 cdecl = self ._typeof (cdecl )
290283 return self ._backend .cast (cdecl , source )
291284
@@ -350,7 +343,7 @@ def from_buffer(self, cdecl, python_buffer=_unspecified,
350343 """
351344 if python_buffer is _unspecified :
352345 cdecl , python_buffer = self .BCharA , cdecl
353- elif isinstance (cdecl , basestring ):
346+ elif isinstance (cdecl , str ):
354347 cdecl = self ._typeof (cdecl )
355348 return self ._backend .from_buffer (cdecl , python_buffer ,
356349 require_writable )
@@ -385,7 +378,7 @@ def callback_decorator_wrap(python_callable):
385378 "is not callable" )
386379 return self ._backend .callback (cdecl , python_callable ,
387380 error , onerror )
388- if isinstance (cdecl , basestring ):
381+ if isinstance (cdecl , str ):
389382 cdecl = self ._typeof (cdecl , consider_function_as_funcptr = True )
390383 if python_callable is None :
391384 return callback_decorator_wrap # decorator mode
@@ -398,7 +391,7 @@ def getctype(self, cdecl, replace_with=''):
398391 extra text to append (or insert for more complicated C types), like
399392 a variable name, or '*' to get actually the C type 'pointer-to-cdecl'.
400393 """
401- if isinstance (cdecl , basestring ):
394+ if isinstance (cdecl , str ):
402395 cdecl = self ._typeof (cdecl )
403396 replace_with = replace_with .strip ()
404397 if (replace_with .startswith ('*' )
@@ -605,10 +598,7 @@ def ensure(key, value):
605598 if hasattr (sys , 'gettotalrefcount' ):
606599 template += '_d'
607600 else :
608- try :
609- import sysconfig
610- except ImportError : # 2.6
611- from cffi ._shimmed_dist_utils import sysconfig
601+ import sysconfig
612602 template = "python%d.%d"
613603 if sysconfig .get_config_var ('DEBUG_EXT' ):
614604 template += sysconfig .get_config_var ('DEBUG_EXT' )
@@ -625,7 +615,7 @@ def set_source(self, module_name, source, source_extension='.c', **kwds):
625615 if hasattr (self , '_assigned_source' ):
626616 raise ValueError ("set_source() cannot be called several times "
627617 "per ffi object" )
628- if not isinstance (module_name , basestring ):
618+ if not isinstance (module_name , str ):
629619 raise TypeError ("'module_name' must be a string" )
630620 if os .sep in module_name or (os .altsep and os .altsep in module_name ):
631621 raise ValueError ("'module_name' must not contain '/': use a dotted "
@@ -791,7 +781,7 @@ def list_types(self):
791781
792782def _load_backend_lib (backend , name , flags ):
793783 import os
794- if not isinstance (name , basestring ):
784+ if not isinstance (name , str ):
795785 if sys .platform != "win32" or name is not None :
796786 return backend .load_library (name , flags )
797787 name = "c" # Windows: load_library(None) fails, but this works
@@ -805,8 +795,8 @@ def _load_backend_lib(backend, name, flags):
805795 import ctypes .util
806796 path = ctypes .util .find_library (name )
807797 if path is None :
808- if name == "c" and sys .platform == "win32" and sys . version_info >= ( 3 ,) :
809- raise OSError ("dlopen(None) cannot work on Windows for Python 3 "
798+ if name == "c" and sys .platform == "win32" :
799+ raise OSError ("dlopen(None) cannot work on Windows "
810800 "(see http://bugs.python.org/issue23606)" )
811801 msg = ("ctypes.util.find_library() did not manage "
812802 f"to locate a library called { name !r} " )
@@ -927,7 +917,7 @@ def __cffi_close__(self):
927917 backendlib .close_lib ()
928918 self .__dict__ .clear ()
929919 #
930- if isinstance (libname , basestring ):
920+ if isinstance (libname , str ):
931921 try :
932922 if not isinstance (libname , str ): # unicode, on Python 2
933923 libname = libname .encode ('utf-8' )
0 commit comments