classification
Title: xml.sax.saxutils.prepare_input_source ignores character stream in InputSource
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.1, Python 3.2, Python 2.7
process
Status: closed Resolution: out of date
Dependencies: 17089 Superseder:
Assigned To: serhiy.storchaka Nosy List: serhiy.storchaka, terry.reedy, ygale
Priority: normal Keywords:

Created on 2007-11-21 14:03 by ygale, last changed 2015-04-02 18:09 by serhiy.storchaka. This issue is now closed.

Files
File name Uploaded Description Edit
test_prepare_input_source.py ygale, 2008-02-24 14:06 Almost full set of tests for prepare_input_source()
Messages (7)
msg57737 - (view) Author: Yitz Gale (ygale) Date: 2007-11-21 14:03
In the documentation for xml.sax.xmlreader.InputSource objects
(section 8.12.4 of the Library Reference) we find that
users of InputSource objects should use the following
sequence to get their input data:

1. If the InputSource has a character stream, use that.
2. Otherwise, if the InputSource has a byte stream, use that.
3. Otherwise, open a URI connection to the system ID.

prepare_input_source() skips step 1.

This is a one-line fix in Lib/xml/sax/saxutils.py:
-    if source.getByteStream() is None:
+    if source.getCharacterStream is None and source.getByteStream() is 
None:
msg57749 - (view) Author: Yitz Gale (ygale) Date: 2007-11-22 09:38
Oops, obvious typo, sorry:

-    if source.getByteStream() is None:
+    if source.getCharacterStream() is None and source.getByteStream() is 
None:
msg62808 - (view) Author: A.M. Kuchling (akuchling) * (Python committer) Date: 2008-02-23 20:37
Could you please provide a simple little test case for the bug?  I'd
like to add a test when I commit the change, but you can probably boil
the problem down into a test faster than I can.
msg62902 - (view) Author: Yitz Gale (ygale) Date: 2008-02-24 14:06
Sure. Here is a simple test case:

  def testUseCharacterStream(self):
    '''If the source is an InputSource with a character stream, use 
it.'''
    src = xml.sax.xmlreader.InputSource(temp_file_name)
    src.setCharacterStream(StringIO.StringIO(u"foo"))
    prep = xml.sax.saxutils.prepare_input_source(src)
    self.failIf(prep.getCharacterStream() is None, "ignored character 
stream")

If "temp_file_name" is omitted, you'll get an
AttributeError, and if you put it in but the
file doesn't exist, you'll get an IOError.

I'm attaching an almost full set of tests.
It omits the case of a URL. You can easily
put that in if you have a handy function that
converts a file path to a file URL, with all
the fidgety stuff you need for Windows. (Does that
already exist somewhere?)

Unfortunately, I now see that the problem
is a bit deeper than this. There are two more
related bugs that need to be fixed before
this really works.

See #2174 and #2175.
msg107425 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2010-06-09 22:02
Are this and the other issues still problems in 2.7 (rc out now) and 3.1?
msg116791 - (view) Author: Mark Lawrence (BreamoreBoy) * Date: 2010-09-18 15:06
There's a one line patch in msg57749 and some unit tests are attached so would a committer take a look please.  Also note that #2174 and #2175 are related.
msg239938 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2015-04-02 18:09
Fixed in issue2175.
History
Date User Action Args
2015-04-02 18:09:58serhiy.storchakasetstatus: open -> closed
resolution: out of date
messages: + msg239938

stage: patch review -> resolved
2014-12-31 16:21:04akuchlingsetnosy: - akuchling
2014-02-03 18:41:31BreamoreBoysetnosy: - BreamoreBoy
2013-01-31 10:03:46serhiy.storchakasetdependencies: + Expat parser parses strings only when XML encoding is UTF-8
2013-01-16 18:25:58serhiy.storchakasetassignee: serhiy.storchaka

nosy: + serhiy.storchaka
2010-11-12 21:04:04akuchlingsetassignee: akuchling -> (no value)
2010-09-18 15:06:35BreamoreBoysetnosy: + BreamoreBoy
messages: + msg116791

type: behavior
stage: patch review
2010-06-09 22:02:11terry.reedysetnosy: + terry.reedy

messages: + msg107425
versions: + Python 3.1, Python 2.7, Python 3.2, - Python 2.5
2008-03-20 02:40:53jafosetpriority: normal
assignee: akuchling
2008-02-24 14:06:49ygalesetfiles: + test_prepare_input_source.py
messages: + msg62902
2008-02-23 20:37:53akuchlingsetnosy: + akuchling
messages: + msg62808
2007-11-22 09:38:37ygalesetmessages: + msg57749
2007-11-21 14:03:15ygalecreate