@@ -273,23 +273,23 @@ def file_path_to_url(path):
273
273
return urljoin ('file:' , pathname2url (path ))
274
274
275
275
276
- def _get_handle (source , mode , encoding = None , compression = None ,
276
+ def _get_handle (path_or_buf , mode , encoding = None , compression = None ,
277
277
memory_map = False ):
278
278
"""
279
279
Get file handle for given path/buffer and mode.
280
280
"""
281
281
282
- f = source
283
- is_path = isinstance (source , compat .string_types )
282
+ f = path_or_buf
283
+ is_path = isinstance (path_or_buf , compat .string_types )
284
284
285
285
# in Python 3, convert BytesIO or fileobjects passed with an encoding
286
- if compat .PY3 and isinstance (source , compat .BytesIO ):
286
+ if compat .PY3 and isinstance (path_or_buf , compat .BytesIO ):
287
287
from io import TextIOWrapper
288
- return TextIOWrapper (source , encoding = encoding )
288
+ return TextIOWrapper (path_or_buf , encoding = encoding )
289
289
290
290
elif compression :
291
291
compression = compression .lower ()
292
-
292
+
293
293
if compat .PY2 and not is_path and encoding :
294
294
msg = 'compression with encoding is not yet supported in Python 2'
295
295
raise ValueError (msg )
@@ -298,38 +298,38 @@ def _get_handle(source, mode, encoding=None, compression=None,
298
298
if compression == 'gzip' :
299
299
import gzip
300
300
if is_path :
301
- f = gzip .open (source , mode )
301
+ f = gzip .open (path_or_buf , mode )
302
302
else :
303
- f = gzip .GzipFile (fileobj = source )
303
+ f = gzip .GzipFile (fileobj = path_or_buf )
304
304
305
305
# BZ Compression
306
306
elif compression == 'bz2' :
307
307
import bz2
308
308
if is_path :
309
- f = bz2 .BZ2File (source , mode )
309
+ f = bz2 .BZ2File (path_or_buf , mode )
310
310
elif compat .PY2 :
311
311
# Python 2's bz2 module can't take file objects, so have to
312
312
# run through decompress manually
313
- f = StringIO (bz2 .decompress (source .read ()))
313
+ f = StringIO (bz2 .decompress (path_or_buf .read ()))
314
314
else :
315
- f = bz2 .BZ2File (source )
315
+ f = bz2 .BZ2File (path_or_buf )
316
316
317
317
# ZIP Compression
318
318
elif compression == 'zip' :
319
319
import zipfile
320
- zip_file = zipfile .ZipFile (source )
320
+ zip_file = zipfile .ZipFile (path_or_buf )
321
321
try :
322
322
name , = zip_file .namelist ()
323
323
except ValueError :
324
- msg = 'Zip file must contain exactly one file {}' . format ( source )
325
- raise ValueError ( msg )
326
- f = zip_file .open (zip_names . pop () )
324
+ raise ValueError ( 'Zip file must contain exactly one file {}'
325
+ . format ( path_or_buf ) )
326
+ f = zip_file .open (name )
327
327
328
328
# XZ Compression
329
329
elif compression == 'xz' :
330
330
lzma = compat .import_lzma ()
331
- f = lzma .LZMAFile (source , mode )
332
-
331
+ f = lzma .LZMAFile (path_or_buf , mode )
332
+
333
333
# Unrecognized Compression
334
334
else :
335
335
msg = 'Unrecognized compression: {}' .format (compression )
@@ -345,13 +345,13 @@ def _get_handle(source, mode, encoding=None, compression=None,
345
345
elif is_path :
346
346
if compat .PY2 :
347
347
# Python 2
348
- f = open (source , mode )
348
+ f = open (path_or_buf , mode )
349
349
elif encoding :
350
350
# Python 3 and encoding
351
- f = open (source , mode , encoding = encoding )
351
+ f = open (path_or_buf , mode , encoding = encoding )
352
352
else :
353
353
# Python 3 and no explicit encoding
354
- f = open (source , mode , errors = 'replace' )
354
+ f = open (path_or_buf , mode , errors = 'replace' )
355
355
356
356
if memory_map and hasattr (f , 'fileno' ):
357
357
try :
0 commit comments