@@ -161,6 +161,24 @@ def u(v, encoding='utf-8', encodingErrors='strict'):
161161 def is_string (v ):
162162 return isinstance (v , basestring )
163163
164+ if sys .version_info [0 :2 ] >= (3 , 6 ):
165+ def pathlike_obj (path ):
166+ if isinstance (path , os .PathLike ):
167+ return os .fsdecode (path )
168+ else :
169+ return path
170+ else :
171+ def pathlike_obj (path ):
172+ if is_string (path ):
173+ return path
174+ elif hasattr (path , "__fspath__" ):
175+ return path .__fspath__ ()
176+ else :
177+ try :
178+ return str (path )
179+ except :
180+ return path
181+
164182
165183# Begin
166184
@@ -930,14 +948,14 @@ def __init__(self, *args, **kwargs):
930948 self .encodingErrors = kwargs .pop ('encodingErrors' , 'strict' )
931949 # See if a shapefile name was passed as the first argument
932950 if len (args ) > 0 :
933- if is_string (args [0 ]):
934- path = args [ 0 ]
935-
951+ path = pathlike_obj (args [0 ])
952+ if is_string ( path ):
953+
936954 if '.zip' in path :
937955 # Shapefile is inside a zipfile
938956 if path .count ('.zip' ) > 1 :
939957 # Multiple nested zipfiles
940- raise ShapefileException ('Reading from multiple nested zipfiles is not supported: %s' % args [ 0 ] )
958+ raise ShapefileException ('Reading from multiple nested zipfiles is not supported: %s' % path )
941959 # Split into zipfile and shapefile paths
942960 if path .endswith ('.zip' ):
943961 zpath = path
@@ -1708,8 +1726,9 @@ def __init__(self, target=None, shapeType=None, autoBalance=False, **kwargs):
17081726 self .shapeType = shapeType
17091727 self .shp = self .shx = self .dbf = None
17101728 if target :
1729+ target = pathlike_obj (target )
17111730 if not is_string (target ):
1712- raise Exception ('The target filepath {} must be of type str/unicode, not {}.' .format (repr (target ), type (target )) )
1731+ raise Exception ('The target filepath {} must be of type str/unicode or path-like , not {}.' .format (repr (target ), type (target )) )
17131732 self .shp = self .__getFileObj (os .path .splitext (target )[0 ] + '.shp' )
17141733 self .shx = self .__getFileObj (os .path .splitext (target )[0 ] + '.shx' )
17151734 self .dbf = self .__getFileObj (os .path .splitext (target )[0 ] + '.dbf' )
0 commit comments