Skip to content

Commit b722ece

Browse files
committed
Update test_pyclbr.py from 3.14.3
1 parent f7d8a55 commit b722ece

1 file changed

Lines changed: 43 additions & 16 deletions

File tree

Lib/test/test_pyclbr.py

Lines changed: 43 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,17 @@
33
Nick Mathewson
44
'''
55

6+
import importlib.machinery
67
import sys
8+
from contextlib import contextmanager
79
from textwrap import dedent
810
from types import FunctionType, MethodType, BuiltinFunctionType
911
import pyclbr
1012
from unittest import TestCase, main as unittest_main
1113
from test.test_importlib import util as test_importlib_util
1214
import warnings
13-
from test.support.testcase import ExtraAssertions
1415

15-
import unittest # TODO: RUSTPYTHON
16+
import unittest # XXX: RUSTPYTHON; importing to be able to skip tests
1617

1718

1819
StaticMethodType = type(staticmethod(lambda: None))
@@ -25,7 +26,30 @@
2526
# is imperfect (as designed), testModule is called with a set of
2627
# members to ignore.
2728

28-
class PyclbrTest(TestCase, ExtraAssertions):
29+
30+
@contextmanager
31+
def temporary_main_spec():
32+
"""
33+
A context manager that temporarily sets the `__spec__` attribute
34+
of the `__main__` module if it's missing.
35+
"""
36+
main_mod = sys.modules.get("__main__")
37+
if main_mod is None:
38+
yield # Do nothing if __main__ is not present
39+
return
40+
41+
original_spec = getattr(main_mod, "__spec__", None)
42+
if original_spec is None:
43+
main_mod.__spec__ = importlib.machinery.ModuleSpec(
44+
name="__main__", loader=None, origin="built-in"
45+
)
46+
try:
47+
yield
48+
finally:
49+
main_mod.__spec__ = original_spec
50+
51+
52+
class PyclbrTest(TestCase):
2953

3054
def assertListEq(self, l1, l2, ignore):
3155
''' succeed iff {l1} - {ignore} == {l2} - {ignore} '''
@@ -81,7 +105,7 @@ def ismethod(oclass, obj, name):
81105
for name, value in dict.items():
82106
if name in ignore:
83107
continue
84-
self.assertHasAttr(module, name, ignore)
108+
self.assertHasAttr(module, name)
85109
py_item = getattr(module, name)
86110
if isinstance(value, pyclbr.Function):
87111
self.assertIsInstance(py_item, (FunctionType, BuiltinFunctionType))
@@ -105,6 +129,8 @@ def ismethod(oclass, obj, name):
105129

106130
actualMethods = []
107131
for m in py_item.__dict__.keys():
132+
if m == "__annotate__":
133+
continue
108134
if ismethod(py_item, getattr(py_item, m), m):
109135
actualMethods.append(m)
110136

@@ -146,12 +172,12 @@ def test_easy(self):
146172
self.checkModule('pyclbr')
147173
# XXX: Metaclasses are not supported
148174
# self.checkModule('ast')
149-
self.checkModule('doctest', ignore=("TestResults", "_SpoofOut",
150-
"DocTestCase", '_DocTestSuite'))
175+
with temporary_main_spec():
176+
self.checkModule('doctest', ignore=("TestResults", "_SpoofOut",
177+
"DocTestCase", '_DocTestSuite'))
151178
self.checkModule('difflib', ignore=("Match",))
152179

153-
# TODO: RUSTPYTHON
154-
@unittest.expectedFailure
180+
@unittest.expectedFailure # TODO: RUSTPYTHON
155181
def test_cases(self):
156182
# see test.pyclbr_input for the rationale behind the ignored symbols
157183
self.checkModule('test.pyclbr_input', ignore=['om', 'f'])
@@ -217,8 +243,7 @@ def compare(parent1, children1, parent2, children2):
217243

218244
compare(None, actual, None, expected)
219245

220-
# TODO: RUSTPYTHON
221-
@unittest.expectedFailure
246+
@unittest.expectedFailure # TODO: RUSTPYTHON
222247
def test_others(self):
223248
cm = self.checkModule
224249

@@ -228,12 +253,14 @@ def test_others(self):
228253
with warnings.catch_warnings():
229254
warnings.simplefilter('ignore', DeprecationWarning)
230255
cm('sre_parse', ignore=('dump', 'groups', 'pos')) # from sre_constants import *; property
231-
cm(
232-
'pdb',
233-
# pyclbr does not handle elegantly `typing` or properties
234-
ignore=('Union', '_ModuleTarget', '_ScriptTarget', '_ZipTarget'),
235-
)
236-
cm('pydoc', ignore=('input', 'output',)) # properties
256+
with temporary_main_spec():
257+
cm(
258+
'pdb',
259+
# pyclbr does not handle elegantly `typing` or properties
260+
ignore=('Union', '_ModuleTarget', '_ScriptTarget', '_ZipTarget', 'curframe_locals',
261+
'_InteractState', 'rlcompleter'),
262+
)
263+
cm('pydoc', ignore=('input', 'output',)) # properties
237264

238265
# Tests for modules inside packages
239266
cm('email.parser')

0 commit comments

Comments
 (0)