Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 4 commits into from Jan 7, 2016
Merged

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

merged 4 commits into from Jan 7, 2016

Conversation

cgohlke
Copy link
Contributor

@cgohlke 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.

@jenshnielsen
Copy link
Member

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.

@tacaswell
Copy link
Member

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

@tacaswell tacaswell added this to the Critical bugfix release (1.5.1) milestone Jan 3, 2016
@jenshnielsen
Copy link
Member

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

@QuLogic
Copy link
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, .

@mdboom
Copy link
Member

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.

@jenshnielsen
Copy link
Member

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

@tacaswell
Copy link
Member

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
tacaswell added a commit that referenced this pull request Jan 7, 2016
Fix svg examples in python 3
tacaswell added a commit that referenced this pull request Jan 7, 2016
Fix svg examples in python 3
@tacaswell
Copy link
Member

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
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants