Commit a5981724 authored by Nicolas Delaby's avatar Nicolas Delaby

* rename file variable to file_object

* use expatreader to parse xml trees for better performances 
parent 022e2bc0
...@@ -11,8 +11,8 @@ Patched by Nicolas Delaby nicolas@nexedi.com to support namespaces ...@@ -11,8 +11,8 @@ Patched by Nicolas Delaby nicolas@nexedi.com to support namespaces
# dumps(value), loads(string) # dumps(value), loads(string)
from types import ClassType from types import ClassType
import sys import sys
from xml.sax import make_parser
from xml.sax.saxutils import escape, unescape from xml.sax.saxutils import escape, unescape
import lxml.sax
from lxml.sax import ElementTreeContentHandler from lxml.sax import ElementTreeContentHandler
from lxml import etree from lxml import etree
from lxml.builder import ElementMaker from lxml.builder import ElementMaker
...@@ -293,31 +293,31 @@ class Unmarshaller(ElementTreeContentHandler): ...@@ -293,31 +293,31 @@ class Unmarshaller(ElementTreeContentHandler):
self.kw = {} self.kw = {}
self.accumulating_chars = 0 self.accumulating_chars = 0
def load(self, file): def load(self, file_object):
"Unmarshal one value, reading it from a file-like object" "Unmarshal one value, reading it from a file-like object"
# Instantiate a new object; unmarshalling isn't thread-safe # Instantiate a new object; unmarshalling isn't thread-safe
# because it modifies attributes on the object. # because it modifies attributes on the object.
m = self.__class__() m = self.__class__()
return m._load(file) return m._load(file_object)
def loads(self, string): def loads(self, string):
"Unmarshal one value from a string" "Unmarshal one value from a string"
# Instantiate a new object; unmarshalling isn't thread-safe # Instantiate a new object; unmarshalling isn't thread-safe
# because it modifies attributes on the object. # because it modifies attributes on the object.
m = self.__class__() m = self.__class__()
file = StringIO(string) file_object = StringIO(string)
return m._load(file) return m._load(file_object)
# Basic unmarshalling routine; it creates a SAX XML parser, # Basic unmarshalling routine; it creates a SAX XML parser,
# registers self as the SAX handler, parses it, and returns # registers self as the SAX handler, parses it, and returns
# the only thing on the data stack. # the only thing on the data stack.
def _load(self, file): def _load(self, file_object):
"Read one value from the open file" "Read one value from the open file"
lxml.sax.saxify(lxml.etree.parse(file), self) parser = make_parser()
#p = saxexts.make_parser() parser.setFeature('http://xml.org/sax/features/namespaces', True)
#p.setDocumentHandler(self) parser.setContentHandler(self)
#p.parseFile(file) parser.parse(file_object)
assert len(self.data_stack) == 1 assert len(self.data_stack) == 1
# leave the instance in a steady state # leave the instance in a steady state
result = self.data_stack[0] result = self.data_stack[0]
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment