| 1 |
''' |
| 2 |
Test cases for pyclbr.py |
| 3 |
Nick Mathewson |
| 4 |
''' |
| 5 |
|
| 6 |
import importlib.machinery |
| 7 |
import sys |
| 8 |
from contextlib import contextmanager |
| 9 |
from textwrap import dedent |
| 10 |
from types import FunctionType, MethodType, BuiltinFunctionType |
| 11 |
import pyclbr |
| 12 |
from unittest import TestCase, main as unittest_main |
| 13 |
from test.test_importlib import util as test_importlib_util |
| 14 |
import warnings |
| 15 |
|
| 16 |
|
| 17 |
StaticMethodType = type(staticmethod(lambda: None)) |
| 18 |
ClassMethodType = type(classmethod(lambda c: None)) |
| 19 |
|
| 20 |
# Here we test the python class browser code. |
| 21 |
# |
| 22 |
# The main function in this suite, 'testModule', compares the output |
| 23 |
# of pyclbr with the introspected members of a module. Because pyclbr |
| 24 |
# is imperfect (as designed), testModule is called with a set of |
| 25 |
# members to ignore. |
| 26 |
|
| 27 |
|
| 28 |
@contextmanager |
| 29 |
def temporary_main_spec(): |
| 30 |
""" |
| 31 |
A context manager that temporarily sets the `__spec__` attribute |
| 32 |
of the `__main__` module if it's missing. |
| 33 |
""" |
| 34 |
main_mod = sys.modules.get("__main__") |
| 35 |
if main_mod is None: |
| 36 |
yield # Do nothing if __main__ is not present |
| 37 |
return |
| 38 |
|
| 39 |
original_spec = getattr(main_mod, "__spec__", None) |
| 40 |
if original_spec is None: |
| 41 |
main_mod.__spec__ = importlib.machinery.ModuleSpec( |
| 42 |
name="__main__", loader=None, origin="built-in" |
| 43 |
) |
| 44 |
try: |
| 45 |
yield |
| 46 |
finally: |
| 47 |
main_mod.__spec__ = original_spec |
| 48 |
|
| 49 |
|
| 50 |
class PyclbrTest(TestCase): |
| 51 |
|
| 52 |
def assertListEq(self, l1, l2, ignore): |
| 53 |
''' succeed iff {l1} - {ignore} == {l2} - {ignore} ''' |
| 54 |
missing = (set(l1) ^ set(l2)) - set(ignore) |
| 55 |
if missing: |
| 56 |
print("l1=%r\nl2=%r\nignore=%r" % (l1, l2, ignore), file=sys.stderr) |
| 57 |
self.fail("%r missing" % missing.pop()) |
| 58 |
|
| 59 |
def assertHaskey(self, obj, key, ignore): |
| 60 |
''' succeed iff key in obj or key in ignore. ''' |
| 61 |
if key in ignore: return |
| 62 |
if key not in obj: |
| 63 |
print("***",key, file=sys.stderr) |
| 64 |
self.assertIn(key, obj) |
| 65 |
|
| 66 |
def assertEqualsOrIgnored(self, a, b, ignore): |
| 67 |
''' succeed iff a == b or a in ignore or b in ignore ''' |
| 68 |
if a not in ignore and b not in ignore: |
| 69 |
self.assertEqual(a, b) |
| 70 |
|
| 71 |
def checkModule(self, moduleName, module=None, ignore=()): |
| 72 |
''' succeed iff pyclbr.readmodule_ex(modulename) corresponds |
| 73 |
to the actual module object, module. Any identifiers in |
| 74 |
ignore are ignored. If no module is provided, the appropriate |
| 75 |
module is loaded with __import__.''' |
| 76 |
|
| 77 |
ignore = set(ignore) | set(['object']) |
| 78 |
|
| 79 |
if module is None: |
| 80 |
# Import it. |
| 81 |
# ('<silly>' is to work around an API silliness in __import__) |
| 82 |
module = __import__(moduleName, globals(), {}, ['<silly>']) |
| 83 |
|
| 84 |
dict = pyclbr.readmodule_ex(moduleName) |
| 85 |
|
| 86 |
def ismethod(oclass, obj, name): |
| 87 |
classdict = oclass.__dict__ |
| 88 |
if isinstance(obj, MethodType): |
| 89 |
# could be a classmethod |
| 90 |
if (not isinstance(classdict[name], ClassMethodType) or |
| 91 |
obj.__self__ is not oclass): |
| 92 |
return False |
| 93 |
elif not isinstance(obj, FunctionType): |
| 94 |
return False |
| 95 |
|
| 96 |
objname = obj.__name__ |
| 97 |
if objname.startswith("__") and not objname.endswith("__"): |
| 98 |
if stripped_typename := oclass.__name__.lstrip('_'): |
| 99 |
objname = f"_{stripped_typename}{objname}" |
| 100 |
return objname == name |
| 101 |
|
| 102 |
# Make sure the toplevel functions and classes are the same. |
| 103 |
for name, value in dict.items(): |
| 104 |
if name in ignore: |
| 105 |
continue |
| 106 |
self.assertHasAttr(module, name) |
| 107 |
py_item = getattr(module, name) |
| 108 |
if isinstance(value, pyclbr.Function): |
| 109 |
self.assertIsInstance(py_item, (FunctionType, BuiltinFunctionType)) |
| 110 |
if py_item.__module__ != moduleName: |
| 111 |
continue # skip functions that came from somewhere else |
| 112 |
self.assertEqual(py_item.__module__, value.module) |
| 113 |
else: |
| 114 |
self.assertIsInstance(py_item, type) |
| 115 |
if py_item.__module__ != moduleName: |
| 116 |
continue # skip classes that came from somewhere else |
| 117 |
|
| 118 |
real_bases = [base.__name__ for base in py_item.__bases__] |
| 119 |
pyclbr_bases = [ getattr(base, 'name', base) |
| 120 |
for base in value.super ] |
| 121 |
|
| 122 |
try: |
| 123 |
self.assertListEq(real_bases, pyclbr_bases, ignore) |
| 124 |
except: |
| 125 |
print("class=%s" % py_item, file=sys.stderr) |
| 126 |
raise |
| 127 |
|
| 128 |
actualMethods = [] |
| 129 |
for m in py_item.__dict__.keys(): |
| 130 |
if m == "__annotate__": |
| 131 |
continue |
| 132 |
if ismethod(py_item, getattr(py_item, m), m): |
| 133 |
actualMethods.append(m) |
| 134 |
|
| 135 |
if stripped_typename := name.lstrip('_'): |
| 136 |
foundMethods = [] |
| 137 |
for m in value.methods.keys(): |
| 138 |
if m.startswith('__') and not m.endswith('__'): |
| 139 |
foundMethods.append(f"_{stripped_typename}{m}") |
| 140 |
else: |
| 141 |
foundMethods.append(m) |
| 142 |
else: |
| 143 |
foundMethods = list(value.methods.keys()) |
| 144 |
|
| 145 |
try: |
| 146 |
self.assertListEq(foundMethods, actualMethods, ignore) |
| 147 |
self.assertEqual(py_item.__module__, value.module) |
| 148 |
|
| 149 |
self.assertEqualsOrIgnored(py_item.__name__, value.name, |
| 150 |
ignore) |
| 151 |
# can't check file or lineno |
| 152 |
except: |
| 153 |
print("class=%s" % py_item, file=sys.stderr) |
| 154 |
raise |
| 155 |
|
| 156 |
# Now check for missing stuff. |
| 157 |
def defined_in(item, module): |
| 158 |
if isinstance(item, type): |
| 159 |
return item.__module__ == module.__name__ |
| 160 |
if isinstance(item, FunctionType): |
| 161 |
return item.__globals__ is module.__dict__ |
| 162 |
return False |
| 163 |
for name in dir(module): |
| 164 |
item = getattr(module, name) |
| 165 |
if isinstance(item, (type, FunctionType)): |
| 166 |
if defined_in(item, module): |
| 167 |
self.assertHaskey(dict, name, ignore) |
| 168 |
|
| 169 |
def test_easy(self): |
| 170 |
self.checkModule('pyclbr') |
| 171 |
# XXX: Metaclasses are not supported |
| 172 |
# self.checkModule('ast') |
| 173 |
with temporary_main_spec(): |
| 174 |
self.checkModule('doctest', ignore=("TestResults", "_SpoofOut", |
| 175 |
"DocTestCase", '_DocTestSuite')) |
| 176 |
self.checkModule('difflib', ignore=("Match",)) |
| 177 |
|
| 178 |
def test_cases(self): |
| 179 |
# see test.pyclbr_input for the rationale behind the ignored symbols |
| 180 |
self.checkModule('test.pyclbr_input', ignore=['om', 'f']) |
| 181 |
|
| 182 |
def test_nested(self): |
| 183 |
mb = pyclbr |
| 184 |
# Set arguments for descriptor creation and _creat_tree call. |
| 185 |
m, p, f, t, i = 'test', '', 'test.py', {}, None |
| 186 |
source = dedent("""\ |
| 187 |
def f0(): |
| 188 |
def f1(a,b,c): |
| 189 |
def f2(a=1, b=2, c=3): pass |
| 190 |
return f1(a,b,d) |
| 191 |
class c1: pass |
| 192 |
class C0: |
| 193 |
"Test class." |
| 194 |
def F1(): |
| 195 |
"Method." |
| 196 |
return 'return' |
| 197 |
class C1(): |
| 198 |
class C2: |
| 199 |
"Class nested within nested class." |
| 200 |
def F3(): return 1+1 |
| 201 |
|
| 202 |
""") |
| 203 |
actual = mb._create_tree(m, p, f, source, t, i) |
| 204 |
|
| 205 |
# Create descriptors, linked together, and expected dict. |
| 206 |
f0 = mb.Function(m, 'f0', f, 1, end_lineno=5) |
| 207 |
f1 = mb._nest_function(f0, 'f1', 2, 4) |
| 208 |
f2 = mb._nest_function(f1, 'f2', 3, 3) |
| 209 |
c1 = mb._nest_class(f0, 'c1', 5, 5) |
| 210 |
C0 = mb.Class(m, 'C0', None, f, 6, end_lineno=14) |
| 211 |
F1 = mb._nest_function(C0, 'F1', 8, 10) |
| 212 |
C1 = mb._nest_class(C0, 'C1', 11, 14) |
| 213 |
C2 = mb._nest_class(C1, 'C2', 12, 14) |
| 214 |
F3 = mb._nest_function(C2, 'F3', 14, 14) |
| 215 |
expected = {'f0':f0, 'C0':C0} |
| 216 |
|
| 217 |
def compare(parent1, children1, parent2, children2): |
| 218 |
"""Return equality of tree pairs. |
| 219 |
|
| 220 |
Each parent,children pair define a tree. The parents are |
| 221 |
assumed equal. Comparing the children dictionaries as such |
| 222 |
does not work due to comparison by identity and double |
| 223 |
linkage. We separate comparing string and number attributes |
| 224 |
from comparing the children of input children. |
| 225 |
""" |
| 226 |
self.assertEqual(children1.keys(), children2.keys()) |
| 227 |
for ob in children1.values(): |
| 228 |
self.assertIs(ob.parent, parent1) |
| 229 |
for ob in children2.values(): |
| 230 |
self.assertIs(ob.parent, parent2) |
| 231 |
for key in children1.keys(): |
| 232 |
o1, o2 = children1[key], children2[key] |
| 233 |
t1 = type(o1), o1.name, o1.file, o1.module, o1.lineno, o1.end_lineno |
| 234 |
t2 = type(o2), o2.name, o2.file, o2.module, o2.lineno, o2.end_lineno |
| 235 |
self.assertEqual(t1, t2) |
| 236 |
if type(o1) is mb.Class: |
| 237 |
self.assertEqual(o1.methods, o2.methods) |
| 238 |
# Skip superclasses for now as not part of example |
| 239 |
compare(o1, o1.children, o2, o2.children) |
| 240 |
|
| 241 |
compare(None, actual, None, expected) |
| 242 |
|
| 243 |
def test_others(self): |
| 244 |
cm = self.checkModule |
| 245 |
|
| 246 |
# These were once some of the longest modules. |
| 247 |
cm('random', ignore=('Random',)) # from _random import Random as CoreGenerator |
| 248 |
cm('pickle', ignore=('partial', 'PickleBuffer')) |
| 249 |
with warnings.catch_warnings(): |
| 250 |
warnings.simplefilter('ignore', DeprecationWarning) |
| 251 |
cm('sre_parse', ignore=('dump', 'groups', 'pos')) # from sre_constants import *; property |
| 252 |
with temporary_main_spec(): |
| 253 |
cm( |
| 254 |
'pdb', |
| 255 |
# pyclbr does not handle elegantly `typing` or properties |
| 256 |
ignore=('Union', '_ModuleTarget', '_ScriptTarget', '_ZipTarget', 'curframe_locals', |
| 257 |
'_InteractState', 'rlcompleter'), |
| 258 |
) |
| 259 |
cm('pydoc', ignore=('input', 'output',)) # properties |
| 260 |
|
| 261 |
# Tests for modules inside packages |
| 262 |
cm('email.parser') |
| 263 |
cm('test.test_pyclbr') |
| 264 |
|
| 265 |
|
| 266 |
class ReadmoduleTests(TestCase): |
| 267 |
|
| 268 |
def setUp(self): |
| 269 |
self._modules = pyclbr._modules.copy() |
| 270 |
|
| 271 |
def tearDown(self): |
| 272 |
pyclbr._modules = self._modules |
| 273 |
|
| 274 |
|
| 275 |
def test_dotted_name_not_a_package(self): |
| 276 |
# test ImportError is raised when the first part of a dotted name is |
| 277 |
# not a package. |
| 278 |
# |
| 279 |
# Issue #14798. |
| 280 |
self.assertRaises(ImportError, pyclbr.readmodule_ex, 'asyncio.foo') |
| 281 |
|
| 282 |
def test_module_has_no_spec(self): |
| 283 |
module_name = "doesnotexist" |
| 284 |
assert module_name not in pyclbr._modules |
| 285 |
with test_importlib_util.uncache(module_name): |
| 286 |
with self.assertRaises(ModuleNotFoundError): |
| 287 |
pyclbr.readmodule_ex(module_name) |
| 288 |
|
| 289 |
|
| 290 |
if __name__ == "__main__": |
| 291 |
unittest_main() |