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

pofile.py: Added new exception called PoFileError and thrown if flagged #532

Merged
merged 1 commit into from
Jan 16, 2018

Conversation

Bedrock02
Copy link

This new exception is thrown when the po parser finds an invalid pofile.
This helps handle invalid po files that are parsed. Invalid po files may cause
other possible errors such as a UnicodeEncodeError.

Closes #531

@Bedrock02 Bedrock02 force-pushed the invalid-po-issue-531 branch 3 times, most recently from 7f54e8e to 4ac2700 Compare September 26, 2017 17:53
@codecov-io
Copy link

codecov-io commented Sep 26, 2017

Codecov Report

Merging #532 into master will decrease coverage by 1.28%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #532      +/-   ##
==========================================
- Coverage   90.15%   88.86%   -1.29%     
==========================================
  Files          24       24              
  Lines        4033     4033              
==========================================
- Hits         3636     3584      -52     
- Misses        397      449      +52
Impacted Files Coverage Δ
babel/messages/pofile.py 96.5% <100%> (-0.25%) ⬇️
babel/_compat.py 53.48% <0%> (-46.52%) ⬇️
babel/localtime/_unix.py 11.53% <0%> (-11.75%) ⬇️
babel/localtime/_win32.py 59.25% <0%> (-3.71%) ⬇️
babel/localtime/__init__.py 62.5% <0%> (-2.5%) ⬇️
babel/messages/catalog.py 92.23% <0%> (-2.39%) ⬇️
babel/util.py 90.78% <0%> (-1.32%) ⬇️
babel/messages/extract.py 94.18% <0%> (-1.1%) ⬇️
babel/localedata.py 94.82% <0%> (-0.63%) ⬇️
babel/support.py 82.28% <0%> (-0.4%) ⬇️
... and 2 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 9e1ec18...9ba950c. Read the comment docs.

@@ -18,6 +18,8 @@
from babel.util import wraptext
from babel._compat import text_type

__all__ = ['PoFileError']
Copy link
Member

Choose a reason for hiding this comment

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

Remove this, please. We haven't had an __all__ stanza in this file, so adding one that just exports a single name will break from babel.messages.pofile import * imports in client code.

Copy link
Author

Choose a reason for hiding this comment

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

👍

@@ -73,6 +75,10 @@ def denormalize(string):
return unescape(string)


class PoFileError(Exception):
"""Exception thrown by PoParser when an invalid po file is encountered."""
Copy link
Member

Choose a reason for hiding this comment

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

It would be nice if this was a more full-fledged exception, to make it easier to use programmatically by client code:

class PoFileError(Exception):
  def __init__(self, message, catalog, line, lineno):
    super(PoFileError, self).__init__('{message} on {lineno}'.format(message=message, lineno=lineno))
    self.catalog = catalog
    self.line = line
    self.lineno = lineno

and naturally the raise below would be

raise PoFileError(msg, self.catalog, line, lineno)

This way client code can introspect the error without having to try to parse the message.

What do you think?

Copy link
Author

Choose a reason for hiding this comment

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

Sounds like a good idea, will implement soon

@@ -276,11 +283,13 @@ def parse(self, fileobj):
self._add_message()

def _invalid_pofile(self, line, lineno, msg):
if self.abort_invalid:
raise PoFileError("Invalid POFile encountered line_number={}, msg={}".format(lineno, msg))
Copy link
Member

Choose a reason for hiding this comment

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

See the discussion above about a better exception :)

Copy link
Author

Choose a reason for hiding this comment

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

👍

msgid ""
"Thank you very much for your time.\n"
"If you have any questions regarding this survey, please contact Tim "
"at andrea.raynorhentschel@dfo.ca."
Copy link
Member

Choose a reason for hiding this comment

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

Please make absolutely sure there aren't any emails or names belonging to real people in the test data. (Consider this duplicated for all the names and emails here.)

Copy link
Author

Choose a reason for hiding this comment

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

👍

@akx akx self-assigned this Sep 29, 2017
@Bedrock02
Copy link
Author

@akx thanks for the quick feedback. I applied the suggested changes in a separate commit. After changes come to a final state I could squash to one commit.

@akx
Copy link
Member

akx commented Jan 15, 2018

@Bedrock02 This looks good now :) Mind rebasing on top of current master and squashing the two commits while you're at it?

@Bedrock02 Bedrock02 force-pushed the invalid-po-issue-531 branch 2 times, most recently from fe4a31c to 0abde9f Compare January 15, 2018 16:55
@Bedrock02
Copy link
Author

@akx branch has been rebased on top of the current master and I squashed the two commits. Let me know if there is anything else that I should do.

@akx
Copy link
Member

akx commented Jan 16, 2018

@Bedrock02 Can you rebase once more on top of the current master so we get rid of the CI failure (as since #546 was just merged we don't support Python 2.6 anymore)? Thank you!

This new exception is thrown when the po parser finds an invalid pofile.
This helps handle invalid po files that are parsed. Invalid po files may cause
other possible errors such as a UnicodeEncodeError.

Closes python-babel#531
@Bedrock02
Copy link
Author

@akx 👍

@akx akx merged commit a220164 into python-babel:master Jan 16, 2018
@akx
Copy link
Member

akx commented Jan 16, 2018

Thank you :)

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.

3 participants