What interface is a ‘Popen.stdout’ presenting?

Ben Finney ben+python at benfinney.id.au
Wed Dec 23 23:40:06 EST 2015


eryk sun <eryksun at gmail.com> writes:

> On Wed, Dec 23, 2015 at 7:36 PM, Ben Finney <ben+python at benfinney.id.au> wrote:
> > So how do I get from a Python 2 ‘file’ object, to whatever
> > ‘io.TextIOWrapper’ wants?
>
> I would dup the file descriptor and close the original file. Then open
> the file descriptor using io.open

Thanks. Okay, now I reveal (I apologise for not making this clear
initially) that I need the *same* code to work with pseudo files created
by the unit test suite: files with no underlying file handle.

That is, I need the same code to accept:

* A Python 2 ‘file’ instance as emitted by many stdlib functions.
* A Python 3 ‘io.*’ file object as emitted by many stdlib functions.
* A pseudo-file (e.g. ‘io.BytesIO’) test double.

With any of those, I need the code to wrap a stream already open in
“binary” mode, and give me a wrapper (e.g. ‘io.TextIOWrapper’) to read
and/or write text on that stream.

-- 
 \     “It is hard to believe that a man is telling the truth when you |
  `\      know that you would lie if you were in his place.” —Henry L. |
_o__)                                                          Mencken |
Ben Finney




More information about the Python-list mailing list