Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
5fabaff4
Commit
5fabaff4
authored
Mar 16, 2012
by
Eli Bendersky
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Closes Issue #14246: _elementtree parser will now handle io.StringIO
parent
944aaab1
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
36 additions
and
1 deletion
+36
-1
Lib/test/test_xml_etree.py
Lib/test/test_xml_etree.py
+14
-0
Modules/_elementtree.c
Modules/_elementtree.c
+22
-1
No files found.
Lib/test/test_xml_etree.py
View file @
5fabaff4
...
...
@@ -16,6 +16,7 @@
import
sys
import
html
import
io
import
unittest
from
test
import
support
...
...
@@ -2026,6 +2027,18 @@ class ElementSlicingTest(unittest.TestCase):
del
e
[::
2
]
self
.
assertEqual
(
self
.
_subelem_tags
(
e
),
[
'a1'
])
class
StringIOTest
(
unittest
.
TestCase
):
def
test_read_from_stringio
(
self
):
tree
=
ET
.
ElementTree
()
stream
=
io
.
StringIO
()
stream
.
write
(
'''<?xml version="1.0"?><site></site>'''
)
stream
.
seek
(
0
)
tree
.
parse
(
stream
)
self
.
assertEqual
(
tree
.
getroot
().
tag
,
'site'
)
# --------------------------------------------------------------------
...
...
@@ -2077,6 +2090,7 @@ def test_main(module=pyET):
test_classes
=
[
ElementSlicingTest
,
StringIOTest
,
ElementTreeTest
,
TreeBuilderTest
]
if
module
is
pyET
:
...
...
Modules/_elementtree.c
View file @
5fabaff4
...
...
@@ -2682,6 +2682,7 @@ xmlparser_parse(XMLParserObject* self, PyObject* args)
PyObject
*
reader
;
PyObject
*
buffer
;
PyObject
*
temp
;
PyObject
*
res
;
PyObject
*
fileobj
;
...
...
@@ -2703,7 +2704,27 @@ xmlparser_parse(XMLParserObject* self, PyObject* args)
return
NULL
;
}
if
(
!
PyBytes_CheckExact
(
buffer
)
||
PyBytes_GET_SIZE
(
buffer
)
==
0
)
{
if
(
PyUnicode_CheckExact
(
buffer
))
{
/* A unicode object is encoded into bytes using UTF-8 */
if
(
PyUnicode_GET_SIZE
(
buffer
)
==
0
)
{
Py_DECREF
(
buffer
);
break
;
}
temp
=
PyUnicode_AsEncodedString
(
buffer
,
"utf-8"
,
"surrogatepass"
);
if
(
!
temp
)
{
/* Propagate exception from PyUnicode_AsEncodedString */
Py_DECREF
(
buffer
);
Py_DECREF
(
reader
);
return
NULL
;
}
/* Here we no longer need the original buffer since it contains
* unicode. Make it point to the encoded bytes object.
*/
Py_DECREF
(
buffer
);
buffer
=
temp
;
}
else
if
(
!
PyBytes_CheckExact
(
buffer
)
||
PyBytes_GET_SIZE
(
buffer
)
==
0
)
{
Py_DECREF
(
buffer
);
break
;
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment