New submission from samwyse:

I'm once again frustrated by a third-party module that only accepts filenames, 
not already-opened file-like objects.  This prevents me from passing in 
StringIO objects or any of the standard file streams.  Currently, *open()* 
function accepts strings or (in Python 3.X) integers.  I propose that *open()* 
accept "file-like" objects, either returning them unchanged, or by returning a 
wrapper object.  While there are many different types of file-like objects, 
they all have one characteristic in common: the presence of a .close() method.

A non-wrapped version of open() could be as simple as this:

  try:
    file = original_open(name, mode, buffering)
  except TypeError:
    if hasattr(name, 'close'):
        return name
    raise

Returning a wrapper object would be slightly more complicated, but would allow 
the wrapped object to remain open even after the wrapper is closed.

----------
components: IO
messages: 171908
nosy: samwyse
priority: normal
severity: normal
status: open
title: Allow *open* to accept file-like objects
type: enhancement
versions: Python 2.7, Python 3.5

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue16122>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to