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

Open configuration file with utf-8 encoding on Windows and Python 2 #2219

Merged
merged 4 commits into from
Mar 10, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions spyderlib/userconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@
from spyderlib.py3compat import configparser as cp
from spyderlib.py3compat import PY2, is_text_string, to_text_string

if PY2:
import codecs

#==============================================================================
# Auxiliary classes
Expand Down Expand Up @@ -113,7 +115,7 @@ def _save(self):
def _write_file(fname):
if PY2:
# Python 2
with open(fname, 'w') as configfile:
with codecs.open(fname, 'w', encoding='utf-8') as configfile:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this, it really seems the right approach here :-)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to this StackOverflow answer, it's better to use io.open, instead of codecs.open because we don't support Python 2.5 anymore.

Could you change this and the call below to io.open please?

self._write(configfile)
else:
# Python 3
Expand Down Expand Up @@ -251,7 +253,13 @@ def load_from_ini(self):
try:
if PY2:
# Python 2
self.read(self.filename())
fname = self.filename()
if osp.isfile(fname):
try:
with codecs.open(fname, encoding='utf-8') as configfile:
self.readfp(configfile)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you decide to use readfp instead of read here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

read() expects filename and does file open itself, readfp() expects open file handle

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I thought that was the case. So codecs.open returns the file handle?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, exactly

except IOError:
print("Failed reading file", fname)
else:
# Python 3
self.read(self.filename(), encoding='utf-8')
Expand Down