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