@@ -161,6 +161,24 @@ def u(v, encoding='utf-8', encodingErrors='strict'):
161
161
def is_string (v ):
162
162
return isinstance (v , basestring )
163
163
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
+
164
182
165
183
# Begin
166
184
@@ -930,14 +948,14 @@ def __init__(self, *args, **kwargs):
930
948
self .encodingErrors = kwargs .pop ('encodingErrors' , 'strict' )
931
949
# See if a shapefile name was passed as the first argument
932
950
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
+
936
954
if '.zip' in path :
937
955
# Shapefile is inside a zipfile
938
956
if path .count ('.zip' ) > 1 :
939
957
# 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 )
941
959
# Split into zipfile and shapefile paths
942
960
if path .endswith ('.zip' ):
943
961
zpath = path
@@ -1708,8 +1726,9 @@ def __init__(self, target=None, shapeType=None, autoBalance=False, **kwargs):
1708
1726
self .shapeType = shapeType
1709
1727
self .shp = self .shx = self .dbf = None
1710
1728
if target :
1729
+ target = pathlike_obj (target )
1711
1730
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 )) )
1713
1732
self .shp = self .__getFileObj (os .path .splitext (target )[0 ] + '.shp' )
1714
1733
self .shx = self .__getFileObj (os .path .splitext (target )[0 ] + '.shx' )
1715
1734
self .dbf = self .__getFileObj (os .path .splitext (target )[0 ] + '.dbf' )
0 commit comments