[issue21476] Inconsitent behaviour between BytesParser.parse and Parser.parse

2014-06-08 Thread Vajrasky Kok

Vajrasky Kok added the comment:

Here is the patch based on R. David Murray's nitpick.

--
Added file: 
http://bugs.python.org/file35537/bytes_parser_dont_close_file_v5.patch

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue21476
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue21476] Inconsitent behaviour between BytesParser.parse and Parser.parse

2014-06-05 Thread R. David Murray

R. David Murray added the comment:

I believe there are msg_NN files that have defects.  I'd rather use one of 
those in the exception test.

--

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue21476
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue21476] Inconsitent behaviour between BytesParser.parse and Parser.parse

2014-06-01 Thread Vajrasky Kok

Vajrasky Kok added the comment:

Ok, here is the updated patch based on R. David Murray's help. Thanks!

--
Added file: 
http://bugs.python.org/file35424/bytes_parser_dont_close_file_v4.patch

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue21476
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue21476] Inconsitent behaviour between BytesParser.parse and Parser.parse

2014-05-31 Thread Serhiy Storchaka

Serhiy Storchaka added the comment:

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.

--

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue21476
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue21476] Inconsitent behaviour between BytesParser.parse and Parser.parse

2014-05-31 Thread Vajrasky Kok

Vajrasky Kok added the comment:

Serhiy, here is the latest patch incorporating your request.

--
Added file: 
http://bugs.python.org/file35420/bytes_parser_dont_close_file_v3.patch

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue21476
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue21476] Inconsitent behaviour between BytesParser.parse and Parser.parse

2014-05-31 Thread Serhiy Storchaka

Serhiy Storchaka added the comment:

Sorry, I meant to test parser with invalid message, so that parse() raises an 
exception, and file shouldn't be closed after this.

--

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue21476
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue21476] Inconsitent behaviour between BytesParser.parse and Parser.parse

2014-05-31 Thread Vajrasky Kok

Vajrasky Kok added the comment:

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'].

--

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue21476
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue21476] Inconsitent behaviour between BytesParser.parse and Parser.parse

2014-05-31 Thread R. David Murray

R. David Murray added the comment:

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.

--

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue21476
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue21476] Inconsitent behaviour between BytesParser.parse and Parser.parse

2014-05-31 Thread Serhiy Storchaka

Serhiy Storchaka added the comment:

If the parser itself doesn't raise exceptions, we should test with input stream 
raising an exception.

--

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue21476
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue21476] Inconsitent behaviour between BytesParser.parse and Parser.parse

2014-05-30 Thread R. David Murray

R. David Murray added the comment:

I think the code should be using 'detach' after passing the fp to parser.parse, 
instead of using 'with'.

--
versions: +Python 3.5 -Python 3.3

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue21476
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue21476] Inconsitent behaviour between BytesParser.parse and Parser.parse

2014-05-30 Thread Vajrasky Kok

Vajrasky Kok added the comment:

Here is the simple patch based on David Murray's thought.

--
keywords: +patch
nosy: +vajrasky
Added file: http://bugs.python.org/file35413/bytes_parser_dont_close_file.patch

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue21476
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue21476] Inconsitent behaviour between BytesParser.parse and Parser.parse

2014-05-30 Thread Serhiy Storchaka

Serhiy Storchaka added the comment:

Use try/finally to detach even if an exception is raised during parsing.

--
nosy: +serhiy.storchaka

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue21476
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue21476] Inconsitent behaviour between BytesParser.parse and Parser.parse

2014-05-30 Thread Vajrasky Kok

Vajrasky Kok added the comment:

Thank you, Serhiy, for the review! Here is the updated patch.

--
Added file: 
http://bugs.python.org/file35418/bytes_parser_dont_close_file_v2.patch

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue21476
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue21476] Inconsitent behaviour between BytesParser.parse and Parser.parse

2014-05-11 Thread Łukasz Kucharski

New submission from Łukasz Kucharski:

The behaviour of the method for both classes seem to be a little different. 
Executing `Parser.parse(fp)` does not close the file pointer passed while 
Executing `BytesParser.parse` does.

I think this caused by fact that `BytesParser.parse` implementation is using 
`with` statement. Writing this

fp = TextIOWrapper(file_pointer, encoding='ascii', errors='surrogateescape')
with fp:
  return self.parser.parse(fp, headersonly)
file_pointer.seek(0)

The original `file_pointer` gets closed and the call to `seek` fails. 

I am not sure whether such behaviour is intended, and whether, the `with` 
behaves badly, or the `TextIOWrapper`, or the `BytesParser`, thus I am unable 
to suggest or provide a patch. But I think the behaviour should be consistent 
and/or documented.

I attached a file that depicts the issue. The problem originated from SO:
http://stackoverflow.com/questions/23599457/how-to-parse-an-email-in-python-without-closing-the-file
I think it's a minor issue, but it did cause a code to fail with no apparent 
reason.

--
components: email
files: mail_test.py
messages: 218309
nosy: barry, r.david.murray, Łukasz.Kucharski
priority: normal
severity: normal
status: open
title: Inconsitent behaviour between BytesParser.parse and Parser.parse
type: behavior
versions: Python 3.3, Python 3.4
Added file: http://bugs.python.org/file35220/mail_test.py

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue21476
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com