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
018b77c1
Commit
018b77c1
authored
Aug 26, 2004
by
Dave Cole
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Patch #1014930. Expose current parse location to XMLParser.
parent
2a6fe71c
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
76 additions
and
0 deletions
+76
-0
Doc/lib/libpyexpat.tex
Doc/lib/libpyexpat.tex
+20
-0
Lib/test/test_pyexpat.py
Lib/test/test_pyexpat.py
+39
-0
Misc/NEWS
Misc/NEWS
+3
-0
Modules/pyexpat.c
Modules/pyexpat.c
+14
-0
No files found.
Doc/lib/libpyexpat.tex
View file @
018b77c1
...
...
@@ -257,6 +257,26 @@ Column number at which an error occurred.
Line number at which an error occurred.
\end{memberdesc}
The following attributes contain values relating to the current parse
location in an
\class
{
xmlparser
}
object. During a callback reporting
a parse event they indicate the location of the first of the sequence
of characters that generated the event. When called outside of a
callback, the position indicated will be just past the last parse
event (regardless of whether there was an associated callback).
\versionadded
{
2.4
}
\begin{memberdesc}
[xmlparser]
{
CurrentByteIndex
}
Current byte index in the parser input.
\end{memberdesc}
\begin{memberdesc}
[xmlparser]
{
CurrentColumnNumber
}
Current column number in the parser input.
\end{memberdesc}
\begin{memberdesc}
[xmlparser]
{
CurrentLineNumber
}
Current line number in the parser input.
\end{memberdesc}
Here is the list of handlers that can be set. To set a handler on an
\class
{
xmlparser
}
object
\var
{
o
}
, use
\code
{
\var
{
o
}
.
\var
{
handlername
}
=
\var
{
func
}}
.
\var
{
handlername
}
must
...
...
Lib/test/test_pyexpat.py
View file @
018b77c1
...
...
@@ -326,3 +326,42 @@ except RuntimeError, e:
print
"Expected RuntimeError for element 'a'; found %r"
%
e
.
args
[
0
]
else
:
print
"Expected RuntimeError for 'a'"
# Test Current* members:
class
PositionTest
:
def
__init__
(
self
,
expected_list
,
parser
):
self
.
parser
=
parser
self
.
parser
.
StartElementHandler
=
self
.
StartElementHandler
self
.
parser
.
EndElementHandler
=
self
.
EndElementHandler
self
.
expected_list
=
expected_list
self
.
upto
=
0
def
StartElementHandler
(
self
,
name
,
attrs
):
self
.
check_pos
(
's'
)
def
EndElementHandler
(
self
,
name
):
self
.
check_pos
(
'e'
)
def
check_pos
(
self
,
event
):
pos
=
(
event
,
self
.
parser
.
CurrentByteIndex
,
self
.
parser
.
CurrentLineNumber
,
self
.
parser
.
CurrentColumnNumber
)
require
(
self
.
upto
<
len
(
self
.
expected_list
),
'too many parser events'
)
expected
=
self
.
expected_list
[
self
.
upto
]
require
(
pos
==
expected
,
'expected position %s, got %s'
%
(
expected
,
pos
))
self
.
upto
+=
1
parser
=
expat
.
ParserCreate
()
handler
=
PositionTest
([(
's'
,
0
,
1
,
0
),
(
's'
,
5
,
2
,
1
),
(
's'
,
11
,
3
,
2
),
(
'e'
,
15
,
3
,
6
),
(
'e'
,
17
,
4
,
1
),
(
'e'
,
22
,
5
,
0
)],
parser
)
parser
.
Parse
(
'''<a>
<b>
<c/>
</b>
</a>'''
,
1
)
Misc/NEWS
View file @
018b77c1
...
...
@@ -54,6 +54,9 @@ Extension modules
-
Added
socket
.
socketpair
().
-
Added
CurrentByteIndex
,
CurrentColumnNumber
,
CurrentLineNumber
members
to
xml
.
parsers
.
expat
.
XMLParser
object
.
Library
-------
...
...
Modules/pyexpat.c
View file @
018b77c1
...
...
@@ -1455,6 +1455,17 @@ xmlparse_getattr(xmlparseobject *self, char *name)
return
PyInt_FromLong
((
long
)
XML_GetErrorByteIndex
(
self
->
itself
));
}
if
(
name
[
0
]
==
'C'
)
{
if
(
strcmp
(
name
,
"CurrentLineNumber"
)
==
0
)
return
PyInt_FromLong
((
long
)
XML_GetCurrentLineNumber
(
self
->
itself
));
if
(
strcmp
(
name
,
"CurrentColumnNumber"
)
==
0
)
return
PyInt_FromLong
((
long
)
XML_GetCurrentColumnNumber
(
self
->
itself
));
if
(
strcmp
(
name
,
"CurrentByteIndex"
)
==
0
)
return
PyInt_FromLong
((
long
)
XML_GetCurrentByteIndex
(
self
->
itself
));
}
if
(
name
[
0
]
==
'b'
)
{
if
(
strcmp
(
name
,
"buffer_size"
)
==
0
)
return
PyInt_FromLong
((
long
)
self
->
buffer_size
);
...
...
@@ -1503,6 +1514,9 @@ xmlparse_getattr(xmlparseobject *self, char *name)
APPEND
(
rc
,
"ErrorLineNumber"
);
APPEND
(
rc
,
"ErrorColumnNumber"
);
APPEND
(
rc
,
"ErrorByteIndex"
);
APPEND
(
rc
,
"CurrentLineNumber"
);
APPEND
(
rc
,
"CurrentColumnNumber"
);
APPEND
(
rc
,
"CurrentByteIndex"
);
APPEND
(
rc
,
"buffer_size"
);
APPEND
(
rc
,
"buffer_text"
);
APPEND
(
rc
,
"buffer_used"
);
...
...
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