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

bpo-30264: ExpatParser closes the source on error #1451

Merged
merged 1 commit into from
May 5, 2017
Merged

bpo-30264: ExpatParser closes the source on error #1451

merged 1 commit into from
May 5, 2017

Conversation

vstinner
Copy link
Member

@vstinner vstinner commented May 4, 2017

ExpatParser.parse() of xml.sax.xmlreader now always closes the
source: close the file object or the urllib object if source is a
string (not an open file-like object). The change fixes a
ResourceWarning on parsing error.

Add test_parse_close_source() unit test.

@mention-bot
Copy link

@Haypo, thanks for your PR! By analyzing the history of the files in this pull request, we identified @bitdancer, @serhiy-storchaka and @loewis to be potential reviewers.

@brettcannon brettcannon added the type-bug An unexpected behavior, bug, or error label May 4, 2017
@vstinner vstinner changed the title bpo-30264: xml.sax.parse() now closes the source bpo-30264: ExpatParser closes the source on error May 5, 2017
ExpatParser.parse() of xml.sax.xmlreader now always closes the
source: close the file object or the urllib object if source is a
string (not an open file-like object). The change fixes a
ResourceWarning on parsing error.

Add test_parse_close_source() unit test.
@codecov
Copy link

codecov bot commented May 5, 2017

Codecov Report

Merging #1451 into master will increase coverage by <.01%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1451      +/-   ##
==========================================
+ Coverage   83.44%   83.44%   +<.01%     
==========================================
  Files        1368     1368              
  Lines      346460   346477      +17     
==========================================
+ Hits       289099   289130      +31     
+ Misses      57361    57347      -14
Impacted Files Coverage Δ
Lib/test/test_sax.py 97.66% <100%> (+0.02%) ⬆️
Lib/xml/sax/expatreader.py 65.55% <100%> (+0.82%) ⬆️
Lib/wsgiref/handlers.py 88.41% <0%> (-0.86%) ⬇️
Lib/pydoc.py 62% <0%> (+0.06%) ⬆️
Lib/threading.py 82.17% <0%> (+0.17%) ⬆️
Lib/test/test_buffer.py 96.71% <0%> (+0.17%) ⬆️
Lib/test/test_random.py 98.65% <0%> (+0.19%) ⬆️
Lib/test/lock_tests.py 86.66% <0%> (+0.28%) ⬆️
Lib/heapq.py 97.32% <0%> (+2.29%) ⬆️

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 76a3e51...ed9b102. Read the comment docs.

@vstinner vstinner merged commit ef9c0e7 into python:master May 5, 2017
@vstinner vstinner deleted the sax_close branch May 5, 2017 07:46
vstinner added a commit that referenced this pull request May 5, 2017
ExpatParser.parse() of xml.sax.xmlreader now always closes the
source: close the file object or the urllib object if source is a
string (not an open file-like object). The change fixes a
ResourceWarning on parsing error.

Add test_parse_close_source() unit test.
(cherry picked from commit ef9c0e7)
vstinner added a commit that referenced this pull request May 5, 2017
ExpatParser.parse() of xml.sax.xmlreader now always closes the
source: close the file object or the urllib object if source is a
string (not an open file-like object). The change fixes a
ResourceWarning on parsing error.

Add test_parse_close_source() unit test.
(cherry picked from commit ef9c0e7)
@gibfahn
Copy link

gibfahn commented Jun 1, 2018

Sorry if this is the wrong place to post this.

We hit some issues with this change (which could be due to my own misunderstandings, let me know if that's the case!)

My questions are:

  1. Isn't this a breaking change? It certainly breaks code we're using in production.
  2. Why is the sax parser closing file descriptors that it didn't open? Cleaning up your own resources I understand, but cleaning up a fd that was passed to you seems odd.
  3. What do you do if you need access to the file descriptor after parsing it (because you parse it in place)?

For file descriptors that point to files on disk we can work around it by reopening the file, but for something like a StringIO buffer (see simplified example below) I'm not aware of any way to get around the problem.

import xml.sax
import StringIO

# Some StringIO buffer.
source = StringIO.StringIO(b'<_/>')

# Do some parsing.
xml.sax.parse(source, xml.sax.handler.ContentHandler())

# Try to do some other parsing (fails).
xml.sax.parse(source, xml.sax.handler.ContentHandler())

EDIT: This should probably have been on #1476

@serhiy-storchaka
Copy link
Member

The issue on the Python bug tracker is better place: https://bugs.python.org/issue30264.

GitHub is only for discussing technical details of a concrete PR. This PR was merged a year ago.

@gibfahn
Copy link

gibfahn commented Jun 1, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants