@@ -295,9 +295,14 @@ def _get_handle(path_or_buf, mode, encoding=None, compression=None,
295295
296296 Returns
297297 -------
298- A file like object.
298+ f : file-like
299+ A file-like object
300+ new_handle : file-like or None
301+ A file-like object that was openned in this function. Or None if none
302+ were openned.
299303 """
300304
305+ new_handle = None
301306 f = path_or_buf
302307 is_path = isinstance (path_or_buf , compat .string_types )
303308
@@ -358,7 +363,8 @@ def _get_handle(path_or_buf, mode, encoding=None, compression=None,
358363 from io import TextIOWrapper
359364 f = TextIOWrapper (f , encoding = encoding )
360365
361- return f
366+ new_handle = f
367+ return f , new_handle
362368
363369 elif is_path :
364370 if compat .PY2 :
@@ -370,11 +376,13 @@ def _get_handle(path_or_buf, mode, encoding=None, compression=None,
370376 else :
371377 # Python 3 and no explicit encoding
372378 f = open (path_or_buf , mode , errors = 'replace' )
379+ new_handle = f
373380
374381 # in Python 3, convert BytesIO or fileobjects passed with an encoding
375382 if compat .PY3 and isinstance (path_or_buf , compat .BytesIO ):
376383 from io import TextIOWrapper
377384 f = TextIOWrapper (f , encoding = encoding )
385+ new_handle = f
378386
379387 if memory_map and hasattr (f , 'fileno' ):
380388 try :
@@ -388,7 +396,7 @@ def _get_handle(path_or_buf, mode, encoding=None, compression=None,
388396 # leave the file handler as is then
389397 pass
390398
391- return f
399+ return f , new_handle
392400
393401
394402class MMapWrapper (BaseIterator ):
0 commit comments