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

Skip to content

Commit e26fa1b

Browse files
committed
Add some testing to verify which module was imported in ET tests.
This is useful when mucking with import_fresh_module to either force or block importing of the _elementtree accelerator. These tests in place provide an immediate indication whether the accelerator was actually imported and overrode the classes it should have.
1 parent 3a36756 commit e26fa1b

2 files changed

Lines changed: 15 additions & 2 deletions

File tree

Lib/test/test_xml_etree.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import operator
1111
import pickle
1212
import sys
13+
import types
1314
import unittest
1415
import weakref
1516

@@ -2398,8 +2399,11 @@ def setUp(self):
23982399

23992400
# Test that the C accelerator was not imported for pyET
24002401
def test_correct_import_pyET(self):
2401-
self.assertEqual(pyET.Element.__module__, 'xml.etree.ElementTree')
2402-
self.assertEqual(pyET.SubElement.__module__, 'xml.etree.ElementTree')
2402+
# The type of methods defined in Python code is types.FunctionType,
2403+
# while the type of methods defined inside _elementtree is
2404+
# <class 'wrapper_descriptor'>
2405+
self.assertIsInstance(pyET.Element.__init__, types.FunctionType)
2406+
self.assertIsInstance(pyET.XMLParser.__init__, types.FunctionType)
24032407

24042408
# --------------------------------------------------------------------
24052409

Lib/test/test_xml_etree_c.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import sys, struct
33
from test import support
44
from test.support import import_fresh_module
5+
import types
56
import unittest
67

78
cET = import_fresh_module('xml.etree.ElementTree',
@@ -33,14 +34,22 @@ def test_alias_working(self):
3334

3435

3536
@unittest.skipUnless(cET, 'requires _elementtree')
37+
@support.cpython_only
3638
class TestAcceleratorImported(unittest.TestCase):
3739
# Test that the C accelerator was imported, as expected
3840
def test_correct_import_cET(self):
41+
# SubElement is a function so it retains _elementtree as its module.
3942
self.assertEqual(cET.SubElement.__module__, '_elementtree')
4043

4144
def test_correct_import_cET_alias(self):
4245
self.assertEqual(cET_alias.SubElement.__module__, '_elementtree')
4346

47+
def test_parser_comes_from_C(self):
48+
# The type of methods defined in Python code is types.FunctionType,
49+
# while the type of methods defined inside _elementtree is
50+
# <class 'wrapper_descriptor'>
51+
self.assertNotIsInstance(cET.Element.__init__, types.FunctionType)
52+
4453

4554
@unittest.skipUnless(cET, 'requires _elementtree')
4655
@support.cpython_only

0 commit comments

Comments
 (0)