test_pyclbr.py 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. '''
  2. Test cases for pyclbr.py
  3. Nick Mathewson
  4. '''
  5. from test.test_support import run_unittest, import_module
  6. import sys
  7. from types import ClassType, FunctionType, MethodType, BuiltinFunctionType
  8. import pyclbr
  9. from unittest import TestCase
  10. StaticMethodType = type(staticmethod(lambda: None))
  11. ClassMethodType = type(classmethod(lambda c: None))
  12. # Silence Py3k warning
  13. import_module('commands', deprecated=True)
  14. # This next line triggers an error on old versions of pyclbr.
  15. from commands import getstatus
  16. # Here we test the python class browser code.
  17. #
  18. # The main function in this suite, 'testModule', compares the output
  19. # of pyclbr with the introspected members of a module. Because pyclbr
  20. # is imperfect (as designed), testModule is called with a set of
  21. # members to ignore.
  22. class PyclbrTest(TestCase):
  23. def assertListEq(self, l1, l2, ignore):
  24. ''' succeed iff {l1} - {ignore} == {l2} - {ignore} '''
  25. missing = (set(l1) ^ set(l2)) - set(ignore)
  26. if missing:
  27. print >>sys.stderr, "l1=%r\nl2=%r\nignore=%r" % (l1, l2, ignore)
  28. self.fail("%r missing" % missing.pop())
  29. def assertHasattr(self, obj, attr, ignore):
  30. ''' succeed iff hasattr(obj,attr) or attr in ignore. '''
  31. if attr in ignore: return
  32. if not hasattr(obj, attr): print "???", attr
  33. self.assertTrue(hasattr(obj, attr),
  34. 'expected hasattr(%r, %r)' % (obj, attr))
  35. def assertHaskey(self, obj, key, ignore):
  36. ''' succeed iff key in obj or key in ignore. '''
  37. if key in ignore: return
  38. if key not in obj:
  39. print >>sys.stderr, "***", key
  40. self.assertIn(key, obj)
  41. def assertEqualsOrIgnored(self, a, b, ignore):
  42. ''' succeed iff a == b or a in ignore or b in ignore '''
  43. if a not in ignore and b not in ignore:
  44. self.assertEqual(a, b)
  45. def checkModule(self, moduleName, module=None, ignore=()):
  46. ''' succeed iff pyclbr.readmodule_ex(modulename) corresponds
  47. to the actual module object, module. Any identifiers in
  48. ignore are ignored. If no module is provided, the appropriate
  49. module is loaded with __import__.'''
  50. if module is None:
  51. # Import it.
  52. # ('<silly>' is to work around an API silliness in __import__)
  53. module = __import__(moduleName, globals(), {}, ['<silly>'])
  54. dict = pyclbr.readmodule_ex(moduleName)
  55. def ismethod(oclass, obj, name):
  56. classdict = oclass.__dict__
  57. if isinstance(obj, FunctionType):
  58. if not isinstance(classdict[name], StaticMethodType):
  59. return False
  60. else:
  61. if not isinstance(obj, MethodType):
  62. return False
  63. if obj.im_self is not None:
  64. if (not isinstance(classdict[name], ClassMethodType) or
  65. obj.im_self is not oclass):
  66. return False
  67. else:
  68. if not isinstance(classdict[name], FunctionType):
  69. return False
  70. objname = obj.__name__
  71. if objname.startswith("__") and not objname.endswith("__"):
  72. objname = "_%s%s" % (obj.im_class.__name__, objname)
  73. return objname == name
  74. # Make sure the toplevel functions and classes are the same.
  75. for name, value in dict.items():
  76. if name in ignore:
  77. continue
  78. self.assertHasattr(module, name, ignore)
  79. py_item = getattr(module, name)
  80. if isinstance(value, pyclbr.Function):
  81. self.assertIsInstance(py_item, (FunctionType, BuiltinFunctionType))
  82. if py_item.__module__ != moduleName:
  83. continue # skip functions that came from somewhere else
  84. self.assertEqual(py_item.__module__, value.module)
  85. else:
  86. self.assertIsInstance(py_item, (ClassType, type))
  87. if py_item.__module__ != moduleName:
  88. continue # skip classes that came from somewhere else
  89. real_bases = [base.__name__ for base in py_item.__bases__]
  90. pyclbr_bases = [ getattr(base, 'name', base)
  91. for base in value.super ]
  92. try:
  93. self.assertListEq(real_bases, pyclbr_bases, ignore)
  94. except:
  95. print >>sys.stderr, "class=%s" % py_item
  96. raise
  97. actualMethods = []
  98. for m in py_item.__dict__.keys():
  99. if ismethod(py_item, getattr(py_item, m), m):
  100. actualMethods.append(m)
  101. foundMethods = []
  102. for m in value.methods.keys():
  103. if m[:2] == '__' and m[-2:] != '__':
  104. foundMethods.append('_'+name+m)
  105. else:
  106. foundMethods.append(m)
  107. try:
  108. self.assertListEq(foundMethods, actualMethods, ignore)
  109. self.assertEqual(py_item.__module__, value.module)
  110. self.assertEqualsOrIgnored(py_item.__name__, value.name,
  111. ignore)
  112. # can't check file or lineno
  113. except:
  114. print >>sys.stderr, "class=%s" % py_item
  115. raise
  116. # Now check for missing stuff.
  117. def defined_in(item, module):
  118. if isinstance(item, ClassType):
  119. return item.__module__ == module.__name__
  120. if isinstance(item, FunctionType):
  121. return item.func_globals is module.__dict__
  122. return False
  123. for name in dir(module):
  124. item = getattr(module, name)
  125. if isinstance(item, (ClassType, FunctionType)):
  126. if defined_in(item, module):
  127. self.assertHaskey(dict, name, ignore)
  128. def test_easy(self):
  129. self.checkModule('pyclbr')
  130. self.checkModule('doctest', ignore=("DocTestCase",))
  131. # Silence Py3k warning
  132. rfc822 = import_module('rfc822', deprecated=True)
  133. self.checkModule('rfc822', rfc822)
  134. self.checkModule('difflib')
  135. def test_decorators(self):
  136. # XXX: See comment in pyclbr_input.py for a test that would fail
  137. # if it were not commented out.
  138. #
  139. self.checkModule('test.pyclbr_input')
  140. def test_others(self):
  141. cm = self.checkModule
  142. # These were once about the 10 longest modules
  143. cm('random', ignore=('Random',)) # from _random import Random as CoreGenerator
  144. cm('cgi', ignore=('log',)) # set with = in module
  145. cm('urllib', ignore=('_CFNumberToInt32',
  146. '_CStringFromCFString',
  147. '_CFSetup',
  148. 'getproxies_registry',
  149. 'proxy_bypass_registry',
  150. 'proxy_bypass_macosx_sysconf',
  151. 'open_https',
  152. 'getproxies_macosx_sysconf',
  153. 'getproxies_internetconfig',)) # not on all platforms
  154. cm('pickle')
  155. cm('aifc', ignore=('openfp',)) # set with = in module
  156. cm('Cookie')
  157. cm('sre_parse', ignore=('dump', 'groups')) # from sre_constants import *; property
  158. cm('pdb')
  159. cm('pydoc')
  160. # Tests for modules inside packages
  161. cm('email.parser')
  162. cm('test.test_pyclbr')
  163. def test_issue_14798(self):
  164. # test ImportError is raised when the first part of a dotted name is
  165. # not a package
  166. self.assertRaises(ImportError, pyclbr.readmodule_ex, 'asyncore.foo')
  167. def test_main():
  168. run_unittest(PyclbrTest)
  169. if __name__ == "__main__":
  170. test_main()