question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

S3OpenRead doesn't work with TextIOWrapper

See original GitHub issue

Currently only binary-mode read is supported, which would be OK if the stream you returned worked with TextIOWrapper – but it doesn’t, at least not S3OpenRead. TextIOWrapper expects readable, writable, seekable, closed etc methods. If you make S3OpenRead inherit (or quack like) IOBase that should fix it.

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:3
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

3reactions
mjwillsoncommented, Oct 5, 2016

Hey, sorry I’m unlikely to get around to this as it stands – we’ve worked around it like so, although this won’t work for everyone:

            # smart_open's S3OpenRead doesn't support TextIOWrapper
            # and smart_open doesn't support text-mode open. So
            # we return a poor-man's line-by-line TextIOWrapper:
            return (str(line, "utf-8") for line in text_stream)

To be honest smart_open feels like quite a leaky abstraction due to patchy / partial support for various features, meaning you need extra special-cased dispatch and wrapping code after parsing the URL yourself. E.g. gzip only working for local files, some other common compression formats not supported, text mode not working for S3, https issues. Maybe some of those are fixed now, but if I was to revisit I’d be tempted to just manually wrap the streams from the underlying libraries myself.

If you wanted to address that in a deeper way, I’d suggest making sure any stream implementations here respect the stream interfaces from the standard library ( https://docs.python.org/3/library/io.html ), which would make it easier to write general and composable code that builds on them. Perhaps some of that would be easier if you give up on Python 2. Then try and implement features like compression and text mode support in a general way where possible via wrapping streams, and make the dispatch mechanisms (for protocols, compression formats etc) extensible.

0reactions
menshikh-ivcommented, Dec 13, 2017

great @mpenkov, fixed by #131

Read more comments on GitHub >

github_iconTop Results From Across the Web

Problem with TextIOWrapper - doesn't read entire file
I'm trying to read the output file of a simulation and grap some data (elapsed CPU time). I've tried multiple ways of doing...
Read more >
io — Core tools for working with streams
This is a helper function for callables that use open() or TextIOWrapper and have an encoding=None parameter. This function returns encoding if it...
Read more >
How to read from a text file
To work with a text file in Python, you can use the built-in open function, ... TextIOWrapper object, but we don't talk about...
Read more >
python textiowrapper - You.com | The Search Engine You ...
zipfile.open opens the zipped file in binary mode, which doesn't strip out carriage returns (i.e. '\r'), and neither did the defaults for TextIOWrapper...
Read more >
25576 (HttpResponse can't be wrapped by io.TextIOWrapper
TextIOWrapper ; add required IOBase methods ... In Python3, the stdlib CSV writer [1] is expected to write to a text file, not...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found