Visitar URL original
Fix pep2rss to specify output encoding by Harmon758 · Pull Request #634 · python/peps · GitHub
The Wayback Machine - https://web.archive.org/web/20210202075941/https://github.com/python/peps/pull/634
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 pep2rss to specify output encoding #634

Merged
merged 5 commits into from Jul 10, 2018

Conversation

@Harmon758
Copy link
Contributor

@Harmon758 Harmon758 commented Apr 27, 2018

Fixes #633

pep2rss.py Outdated
@@ -69,5 +69,5 @@ def pep_creation_dt(full_path):
lastBuildDate = datetime.datetime.now(),
items = items)

with open(RSS_PATH, 'w') as fp:
with open(RSS_PATH, 'w', encoding="utf-8") as fp:

This comment has been minimized.

@zware

zware Apr 27, 2018
Member

This looks dangerous; if it's run on Python 2 (which is quite possible; the shebang is /usr/bin/env python), this will fail. We should do from io import open at the top.

This comment has been minimized.

@Harmon758

Harmon758 Apr 27, 2018
Author Contributor

Ah, I overlooked this.
I've gone ahead and changed it to codecs.open for consistency and because rss.to_xml() returns str and not unicode (which would have to be converted to not raise a TypeError in Python 2 when using fp.write with io.open).
This does cause line endings to output as '\n' instead of the platform-specific representation, but since line endings are supposed to be normalized to '\n' when processing XML anyway, I don't think this is of significance.

@Harmon758
Copy link
Contributor Author

@Harmon758 Harmon758 commented Apr 27, 2018 •

Perhaps this is a separate issue, but I've noticed that the RSS feed generated specifies "iso-8859-1" as the encoding in the XML declaration.
This is resulting in ٠being displayed as �, with the second byte being malformed.

However, changing

fp.write(rss.to_xml())

to

fp.write(rss.to_xml(encoding="utf-8"))

results in

Traceback (most recent call last):
  File "pep2rss.py", line 75, in <module>
    fp.write(rss.to_xml(encoding="utf-8"))
  File "C:\Python27\lib\codecs.py", line 706, in write
    return self.writer.write(data)
  File "C:\Python27\lib\codecs.py", line 369, in write
    data, consumed = self.encode(object, self.errors)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc5 in position 1537: ordinal not in range(128)

in Python 2.

This can be fixed by manually converting to unicode like so:

fp.write(unicode(rss.to_xml(encoding="utf-8"), "utf-8"))

but I'm not sure of the best way to make this compatible with Python 3, beyond manually checking sys.version_info[0], or whether this seemingly redundant method is the best way to go about this.

@brettcannon
Copy link
Member

@brettcannon brettcannon commented Apr 27, 2018

Does it even need to support Python 2?

@zware
Copy link
Member

@zware zware commented Apr 27, 2018

Changing the shebang would be simpler :)

@holdenweb
Copy link
Member

@holdenweb holdenweb commented Apr 28, 2018

@brettcannon
Copy link
Member

@brettcannon brettcannon commented Apr 30, 2018

@holdenweb who is being disappointed from not generating the RSS feed for the PEPs using Python 2?

I'm with @zware and the shebang should simply be changed and we should just rely on Python 3.

@holdenweb
Copy link
Member

@holdenweb holdenweb commented Apr 30, 2018

@gvanrossum
Copy link
Member

@gvanrossum gvanrossum commented Jul 7, 2018

This seems stalled -- who is waiting for whom here?

@brettcannon
Copy link
Member

@brettcannon brettcannon commented Jul 9, 2018

@gvanrossum I think @zware and I are waiting for the PR to be updated to update the shebang and use Python 3 instead of trying to support Python 2.

@Harmon758
Copy link
Contributor Author

@Harmon758 Harmon758 commented Jul 10, 2018

Ah, ok, since there don't seem to be any objections, I'll go ahead and drop Python 2 support.

@zware
zware approved these changes Jul 10, 2018
@zware zware merged commit abab110 into python:master Jul 10, 2018
1 check passed
1 check passed
continuous-integration/travis-ci/pr The Travis CI build passed
Details
@zware
Copy link
Member

@zware zware commented Jul 10, 2018

Thanks, @Harmon758!

@Harmon758 Harmon758 deleted the Harmon758:pep2rss-encoding-fix branch Jul 10, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

6 participants