Fix ImportError: No module named 'StringIO' on Python 3 #5788

Merged
merged 4 commits into from Jan 7, 2016

Conversation

Projects
None yet
5 participants
Contributor

cgohlke commented Jan 3, 2016

Several examples use the StringIO module, which fails to import on Python 3.
This PR uses io.BytesIO instead. Tested with Python 2.7 and 3.5 on Windows.
Please review. Not sure if it would be better to use io.StringIO or six.StringIO instead.

cgohlke added some commits Jan 3, 2016

@cgohlke cgohlke Fix ImportError: No module named 'StringIO' on Python 3 02c1c3a
@cgohlke cgohlke Fix ImportError: No module named 'StringIO' on Python 3 e951bd6
@cgohlke cgohlke Fix ImportError: No module named 'StringIO' on Python 3 4c3bf8e
@cgohlke cgohlke Fix ImportError: No module named 'StringIO' on Python 3
6461fd9
Owner

jenshnielsen commented Jan 3, 2016

Thanks, I don't think it matters much but I would prefer the svg examples to use io.StringIO since svg is fundamentally a text format.

Owner

tacaswell commented Jan 3, 2016

On the other hand, it buffer is being used to stand in for an open file handle which should be bytes?

Owner

jenshnielsen commented Jan 3, 2016

The file is opened in w mode i.e. write text mode in print_svg If I understand https://docs.python.org/3/library/io.html#text-i-o and https://docs.python.org/3/library/io.html#binary-i-o correctly then io.StringIO is what replicates this behaviour and is.BytesIO replicates wb and rw mode

Member

QuLogic commented Jan 4, 2016

The SVG backend will wrap any file-like objects opened in binary mode with a UTF-8 text encoder, so it's not actually necessary to pick one over the other based on the strict difference between io.BytesIO and io.StringIO, .

Owner

mdboom commented Jan 4, 2016

The SVG backend will wrap any file-like objects opened in binary mode with a UTF-8 text encoder, so it's not actually necessary to pick one over the other based on the strict difference between io.BytesIO and io.StringIO.

Just wanted to add a "ditto" here. One of the reasons it's designed this way is so that users can pass the same bytes-writing file object to any of the backends and have it work (since many of the output formats are binary). The inverse wouldn't be true, i.e. one couldn't write PNG to a text-writing file object.

Owner

jenshnielsen commented Jan 4, 2016

Yes it's all bikeshedding and the only question is if one thing is more clear than another in the SVG examples?

Owner

tacaswell commented Jan 7, 2016

I have changed them over to io.StringIO in a local commit and will merge locally, push and backport.

Also, those interactive demos are super cool!

@tacaswell tacaswell merged commit 6461fd9 into matplotlib:master Jan 7, 2016

3 checks passed

continuous-integration/appveyor/pr AppVeyor build succeeded
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details
coverage/coveralls Coverage remained the same at 68.607%
Details

@tacaswell tacaswell added a commit that referenced this pull request Jan 7, 2016

@tacaswell tacaswell Merge pull request #5788 from cgohlke/patch-1
Fix svg examples in python 3
cf38ed7

tacaswell removed the needs_review label Jan 7, 2016

@tacaswell tacaswell added a commit that referenced this pull request Jan 7, 2016

@tacaswell tacaswell Merge pull request #5788 from cgohlke/patch-1
Fix svg examples in python 3
9fe5abe
Owner

tacaswell commented Jan 7, 2016

Merged to master via cf38ed7

backported as 9fe5abe

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment