]> granicus.if.org Git - python/commitdiff
For Issue #16076: make sure that pickling of Element objects is tested, and do
authorEli Bendersky <eliben@gmail.com>
Sun, 30 Dec 2012 14:17:49 +0000 (06:17 -0800)
committerEli Bendersky <eliben@gmail.com>
Sun, 30 Dec 2012 14:17:49 +0000 (06:17 -0800)
it properly to avoid problems with test-run-order dependencies.

Lib/test/test_xml_etree.py

index 9cebc3cd7fad7e796f7075ff2e978b4dca0fbe99..ce6bdc6d8b5ad724939d16d0e354a3942c4f4815 100644 (file)
@@ -16,6 +16,7 @@
 
 import html
 import io
+import pickle
 import sys
 import unittest
 import weakref
@@ -1768,6 +1769,20 @@ class BasicElementTest(unittest.TestCase):
         self.assertEqual(flag, True)
         self.assertEqual(wref(), None)
 
+    def test_pickle(self):
+        # For now this test only works for the Python version of ET,
+        # so set sys.modules accordingly because pickle uses __import__
+        # to load the __module__ of the class.
+        if pyET:
+            sys.modules['xml.etree.ElementTree'] = pyET
+        else:
+            raise unittest.SkipTest('only for the Python version')
+        e1 = ET.Element('foo', bar=42)
+        s = pickle.dumps(e1)
+        e2 = pickle.loads(s)
+        self.assertEqual(e2.tag, 'foo')
+        self.assertEqual(e2.attrib['bar'], 42)
+
 
 class ElementTreeTest(unittest.TestCase):
     def test_istype(self):