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

PR: Add encoding handling to stderr files in the IPython Console #4369

Merged
merged 4 commits into from
Apr 19, 2017

Conversation

dalthviz
Copy link
Member

Fixes #4191

@@ -32,6 +32,7 @@
from spyder.config.gui import get_font, get_shortcut
from spyder.utils import icon_manager as ima
from spyder.utils import sourcecode
from spyder.utils.encoding import getfilesystemencoding
Copy link
Member

Choose a reason for hiding this comment

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

Let's use get_coding from the same module instead

Copy link
Member

Choose a reason for hiding this comment

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

Sorry, this is not correct. Let me think better about it.

try:
stderr = codecs.open(self.stderr_file, 'r',
encoding='utf-8').read()
except(UnicodeDecodeError):
Copy link
Member

Choose a reason for hiding this comment

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

There's no need to use parentheses here.

try:
encoding = get_coding(open(self.stderr_file, 'rb').read())
stderr = codecs.open(self.stderr_file, 'r',
encoding=encoding).read()
Copy link
Member

Choose a reason for hiding this comment

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

Let's change this to

stderr_text = open(self.stderr_file, 'rb').read()
encoding = get_coding(stderr_text)
stderr = to_text_string(stderr_text, encoding)

encoding='cp437').read()
except UnicodeDecodeError:
raise EncodingError("Could not read the stderr "
"file of the kernel while restarting.")
Copy link
Member

@ccordoba12 ccordoba12 Apr 19, 2017

Choose a reason for hiding this comment

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

and remove this whole block, i.e

        except UnicodeDecodeError:
                try:
                    stderr = codecs.open(self.stderr_file, 'r',
                                         encoding='cp437').read()
                except UnicodeDecodeError:
                    raise EncodingError("Could not read the stderr "
                                        "file of the kernel while restarting.")

and change it to

         except:
            stderr = None

The thing is cp437 is another particular encoding, and we should have detected it in the previous block. If we're not able to read self.stderr_file, then we need to skip this.

@goanpeca
Copy link
Member

goanpeca commented Apr 19, 2017

Could you add Tests :-p @dalthviz

# Encoding Exception
#-----------------------------------------------------------------------------
class EncodingError(Exception):
"""Exception when stderr file can't be opened."""
Copy link
Member

Choose a reason for hiding this comment

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

This is unnecessary now, please remove it.

stderr_text = open(self.stderr_file, 'rb').read()
encoding = get_coding(stderr_text)
stderr = to_text_string(stderr_text, encoding)
except UnicodeDecodeError:
Copy link
Member

@ccordoba12 ccordoba12 Apr 19, 2017

Choose a reason for hiding this comment

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

Please change this to only except. We want to catch all errors at this point.

try:
stderr_text = open(self.stderr_file, 'rb').read()
encoding = get_coding(stderr_text)
stderr = to_text_string(stderr_text, encoding)
Copy link
Member

Choose a reason for hiding this comment

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

Let's move these three lines to a new method called _read_stderr, so we can test it independently of this one.

stderr = codecs.open(self.stderr_file, 'r',
encoding='utf-8').read()
except UnicodeDecodeError:
# This handling is needed since the stderr file could be encoded
Copy link
Member

Choose a reason for hiding this comment

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

Remove handling here.

except UnicodeDecodeError:
# This handling is needed since the stderr file could be encoded
# in something different to utf-8. In case of fail at least
# we raise an informative exception.
Copy link
Member

Choose a reason for hiding this comment

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

Remove In case of fail ... an informative exception.

@ccordoba12
Copy link
Member

ccordoba12 commented Apr 19, 2017

Yes, that's what's missing. The test needs to go in plugins/tests/test_ipythonconsole.py and it needs to be like this

  1. Start a console
  2. Write some message in the cp437 encoding to its stderr_file.
  3. Use _read_stderr to get those contents.
  4. Assert that the result of _read_stderr is the same content written to stderr_file.

@ccordoba12
Copy link
Member

Great work @dalthviz!! I think this is ready, so nothing else to do for me than merge ;-)

@ccordoba12 ccordoba12 changed the title PR: Add encoding handling to stderr file in the IPython Console PR: Add encoding handling to stderr files in the IPython Console Apr 19, 2017
@ccordoba12 ccordoba12 merged commit 1820d0a into spyder-ide:3.1.x Apr 19, 2017
ccordoba12 added a commit that referenced this pull request Apr 19, 2017
ccordoba12 added a commit that referenced this pull request Apr 19, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants