-
-
Notifications
You must be signed in to change notification settings - Fork 31.7k
Inconsistent behaviour between BytesParser.parse and Parser.parse #65675
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
Comments
The behaviour of the method for both classes seem to be a little different. Executing I think this caused by fact that fp = TextIOWrapper(file_pointer, encoding='ascii', errors='surrogateescape')
with fp:
return self.parser.parse(fp, headersonly)
file_pointer.seek(0) The original I am not sure whether such behaviour is intended, and whether, the I attached a file that depicts the issue. The problem originated from SO: |
I think the code should be using 'detach' after passing the fp to parser.parse, instead of using 'with'. |
Here is the simple patch based on David Murray's thought. |
Use try/finally to detach even if an exception is raised during parsing. |
Thank you, Serhiy, for the review! Here is the updated patch. |
Could you please add a test with parse() raising an exception? Yet one nitpick. Instead of fp = openfile('msg_02.txt', 'rb')
self.addCleanup(fp.close)
... you can write with openfile('msg_02.txt', 'rb') as fp:
... as in other tests. |
Serhiy, here is the latest patch incorporating your request. |
Sorry, I meant to test parser with invalid message, so that parse() raises an exception, and file shouldn't be closed after this. |
The Parse class does not throw exception if given invalid message: Assume /tmp/a.txt contains garbage, such as: "&&&&&" With this code: with open("/tmp/a.txt", "r") as fp:
msg = email.parser.Parser().parse(fp) # does not throw exception
print(msg) # => &&&&&
msg['from'] # => None It is just you can not get useful information, such as msg['to']. |
Right, part of the parser contract is to not throw exceptions. Traditionally, a bug could result in an exception, but not an invalid message. However, using the new email policies, it is possible to *request* that it raise exceptions instead of registering defects. See policy.raise_on_defect. |
If the parser itself doesn't raise exceptions, we should test with input stream raising an exception. |
Ok, here is the updated patch based on R. David Murray's help. Thanks! |
I believe there are msg_NN files that have defects. I'd rather use one of those in the exception test. |
Here is the patch based on R. David Murray's nitpick. |
New changeset 0a16756dfcc0 by R David Murray in branch '3.4': New changeset a3ee325fd489 by R David Murray in branch 'default': |
Thanks, Vajrasky. And to the reviewers as well. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: