[issue30264] [Windows] test_sax: Warning -- files was modified by test_sax

STINNER Victor report at bugs.python.org
Fri May 5 05:45:01 EDT 2017


STINNER Victor added the comment:

> This changes the behavior if pass a file object to the parser. When the parser failed it was possible to use the passed file object (for example call tell()). Now it is closed.

If the high-level *function* parse() is called with a filename, the caller doesn't have access to the file object nor the parser.

If you use directly the parser class and pass an open file object, in that case, yes, my change closes the file.

If you want to keep the old behaviour for that case, we can change the code to only close the source if source is not a string (not isinstance(source, str)) in the ExpatParser.parse() method. Since the caller owns the file object, (s)he is responsible to close it.

Note: on parser success, the source is always closed, even if the user pass an already open file object.

I'm not convince that using the file object on parser error and using directly the ExpatParser class is a common use case. I expect that the XML parser reads more data than it needs (read ahead for speed), so I don't think that file.tell() will you exactly the file position where the XML parser failed.

----------

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue30264>
_______________________________________


More information about the Python-bugs-list mailing list