This commit is contained in:
ton
2024-10-07 10:13:40 +07:00
parent aa1631742f
commit 3a7d696db6
9729 changed files with 1832837 additions and 161742 deletions

View File

@@ -79,8 +79,8 @@ class LimitedRecursiveIncludeError(FatalIncludeError):
# @param parse Parse mode. Either "xml" or "text".
# @param encoding Optional text encoding (UTF-8 by default for "text").
# @return The expanded resource. If the parse mode is "xml", this
# is an ElementTree instance. If the parse mode is "text", this
# is a Unicode string. If the loader fails, it can return None
# is an Element instance. If the parse mode is "text", this
# is a string. If the loader fails, it can return None
# or raise an OSError exception.
# @throws OSError If the loader fails to load the resource.
@@ -98,7 +98,7 @@ def default_loader(href, parse, encoding=None):
##
# Expand XInclude directives.
#
# @param elem Root element.
# @param elem Root Element or any ElementTree of a tree to be expanded
# @param loader Optional resource loader. If omitted, it defaults
# to {@link default_loader}. If given, it should be a callable
# that implements the same interface as <b>default_loader</b>.
@@ -106,12 +106,13 @@ def default_loader(href, parse, encoding=None):
# relative include file references.
# @param max_depth The maximum number of recursive inclusions.
# Limited to reduce the risk of malicious content explosion.
# Pass a negative value to disable the limitation.
# Pass None to disable the limitation.
# @throws LimitedRecursiveIncludeError If the {@link max_depth} was exceeded.
# @throws FatalIncludeError If the function fails to include a given
# resource, or if the tree contains malformed XInclude elements.
# @throws IOError If the function fails to load a given resource.
# @returns the node or its replacement if it was an XInclude node
# @throws OSError If the function fails to load a given resource.
# @throws ValueError If negative {@link max_depth} is passed.
# @returns None. Modifies tree pointed by {@link elem}
def include(elem, loader=None, base_url=None,
max_depth=DEFAULT_MAX_INCLUSION_DEPTH):

View File

@@ -99,6 +99,7 @@ import io
import collections
import collections.abc
import contextlib
import weakref
from . import ElementPath
@@ -200,7 +201,7 @@ class Element:
def __bool__(self):
warnings.warn(
"Testing an element's truth value will raise an exception in "
"Testing an element's truth value will always return True in "
"future versions. "
"Use specific 'len(elem)' or 'elem is not None' test instead.",
DeprecationWarning, stacklevel=2
@@ -1223,13 +1224,14 @@ def iterparse(source, events=None, parser=None):
# parser argument of iterparse is removed, this can be killed.
pullparser = XMLPullParser(events=events, _parser=parser)
def iterator(source):
if not hasattr(source, "read"):
source = open(source, "rb")
close_source = True
else:
close_source = False
def iterator(source):
try:
if not hasattr(source, "read"):
source = open(source, "rb")
close_source = True
yield None
while True:
yield from pullparser.read_events()
# load event buffer
@@ -1239,18 +1241,23 @@ def iterparse(source, events=None, parser=None):
pullparser.feed(data)
root = pullparser._close_and_return_root()
yield from pullparser.read_events()
it.root = root
it = wr()
if it is not None:
it.root = root
finally:
if close_source:
source.close()
class IterParseIterator(collections.abc.Iterator):
__next__ = iterator(source).__next__
def __del__(self):
if close_source:
source.close()
it = IterParseIterator()
it.root = None
del iterator, IterParseIterator
next(it)
wr = weakref.ref(it)
return it
@@ -1306,6 +1313,11 @@ class XMLPullParser:
else:
yield event
def flush(self):
if self._parser is None:
raise ValueError("flush() called after end of stream")
self._parser.flush()
def XML(text, parser=None):
"""Parse XML document from string constant.
@@ -1712,6 +1724,15 @@ class XMLParser:
del self.parser, self._parser
del self.target, self._target
def flush(self):
was_enabled = self.parser.GetReparseDeferralEnabled()
try:
self.parser.SetReparseDeferralEnabled(False)
self.parser.Parse(b"", False)
except self._error as v:
self._raiseerror(v)
finally:
self.parser.SetReparseDeferralEnabled(was_enabled)
# --------------------------------------------------------------------
# C14N 2.0

View File

@@ -214,6 +214,20 @@ class ExpatParser(xmlreader.IncrementalParser, xmlreader.Locator):
# FIXME: when to invoke error()?
self._err_handler.fatalError(exc)
def flush(self):
if self._parser is None:
return
was_enabled = self._parser.GetReparseDeferralEnabled()
try:
self._parser.SetReparseDeferralEnabled(False)
self._parser.Parse(b"", False)
except expat.error as e:
exc = SAXParseException(expat.ErrorString(e.code), e, self)
self._err_handler.fatalError(exc)
finally:
self._parser.SetReparseDeferralEnabled(was_enabled)
def _close_source(self):
source = self._source
try: