-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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: | ||
self._write(configfile) | ||
else: | ||
# Python 3 | ||
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why did you decide to use There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, I thought that was the case. So There was a problem hiding this comment. Choose a reason for hiding this commentThe 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') | ||
|
There was a problem hiding this comment.
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 :-)
There was a problem hiding this comment.
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 ofcodecs.open
because we don't support Python 2.5 anymore.Could you change this and the call below to
io.open
please?