Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 14aa280

Browse files
committed
Use __slots__ throughout instead of __dict__, to reduce the memory usage.
1 parent 32ac92c commit 14aa280

2 files changed

Lines changed: 114 additions & 141 deletions

File tree

Lib/xml/dom/expatbuilder.py

Lines changed: 13 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -283,27 +283,23 @@ def character_data_handler_cdata(self, data):
283283
elif childNodes and childNodes[-1].nodeType == TEXT_NODE:
284284
node = childNodes[-1]
285285
value = node.data + data
286-
d = node.__dict__
287-
d['data'] = d['nodeValue'] = value
286+
node.data = value
288287
return
289288
else:
290289
node = minidom.Text()
291-
d = node.__dict__
292-
d['data'] = d['nodeValue'] = data
293-
d['ownerDocument'] = self.document
290+
node.data = data
291+
node.ownerDocument = self.document
294292
_append_child(self.curNode, node)
295293

296294
def character_data_handler(self, data):
297295
childNodes = self.curNode.childNodes
298296
if childNodes and childNodes[-1].nodeType == TEXT_NODE:
299297
node = childNodes[-1]
300-
d = node.__dict__
301-
d['data'] = d['nodeValue'] = node.data + data
298+
node.data = node.data + data
302299
return
303300
node = minidom.Text()
304-
d = node.__dict__
305-
d['data'] = d['nodeValue'] = node.data + data
306-
d['ownerDocument'] = self.document
301+
node.data = node.data + data
302+
node.ownerDocument = self.document
307303
_append_child(self.curNode, node)
308304

309305
def entity_decl_handler(self, entityName, is_parameter_entity, value,
@@ -363,11 +359,8 @@ def start_element_handler(self, name, attributes):
363359
a = minidom.Attr(attributes[i], EMPTY_NAMESPACE,
364360
None, EMPTY_PREFIX)
365361
value = attributes[i+1]
366-
d = a.childNodes[0].__dict__
367-
d['data'] = d['nodeValue'] = value
368-
d = a.__dict__
369-
d['value'] = d['nodeValue'] = value
370-
d['ownerDocument'] = self.document
362+
a.value = value
363+
a.ownerDocument = self.document
371364
_set_attribute_node(node, a)
372365

373366
if node is not self.document.documentElement:
@@ -761,11 +754,8 @@ def start_element_handler(self, name, attributes):
761754
else:
762755
a = minidom.Attr("xmlns", XMLNS_NAMESPACE,
763756
"xmlns", EMPTY_PREFIX)
764-
d = a.childNodes[0].__dict__
765-
d['data'] = d['nodeValue'] = uri
766-
d = a.__dict__
767-
d['value'] = d['nodeValue'] = uri
768-
d['ownerDocument'] = self.document
757+
a.value = uri
758+
a.ownerDocuemnt = self.document
769759
_set_attribute_node(node, a)
770760
del self._ns_ordered_prefixes[:]
771761

@@ -785,12 +775,9 @@ def start_element_handler(self, name, attributes):
785775
aname, EMPTY_PREFIX)
786776
_attrs[aname] = a
787777
_attrsNS[(EMPTY_NAMESPACE, aname)] = a
788-
d = a.childNodes[0].__dict__
789-
d['data'] = d['nodeValue'] = value
790-
d = a.__dict__
791-
d['ownerDocument'] = self.document
792-
d['value'] = d['nodeValue'] = value
793-
d['ownerElement'] = node
778+
a.ownerDocument = self.document
779+
a.value = value
780+
a.ownerElement = node
794781

795782
if __debug__:
796783
# This only adds some asserts to the original

0 commit comments

Comments
 (0)