Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 1 | import sys |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2 | import types |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 3 | import unittest |
| 4 | import inspect |
R. David Murray | 19d8cc5 | 2010-06-17 02:06:12 +0000 | [diff] [blame] | 5 | import linecache |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6 | import datetime |
Guido van Rossum | 813b0e5 | 2007-05-21 18:11:34 +0000 | [diff] [blame] | 7 | import collections |
Christian Heimes | a3538eb | 2007-11-06 11:44:48 +0000 | [diff] [blame] | 8 | from os.path import normcase |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 9 | |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 10 | from test.support import TESTFN, run_unittest |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 11 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 12 | from test import inspect_fodder as mod |
| 13 | from test import inspect_fodder2 as mod2 |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 14 | |
R. David Murray | 74b8924 | 2009-05-13 17:33:03 +0000 | [diff] [blame] | 15 | # C module for test_findsource_binary |
R. David Murray | b565577 | 2009-05-14 16:17:50 +0000 | [diff] [blame] | 16 | import unicodedata |
R. David Murray | 74b8924 | 2009-05-13 17:33:03 +0000 | [diff] [blame] | 17 | |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 18 | # Functions tested in this suite: |
| 19 | # ismodule, isclass, ismethod, isfunction, istraceback, isframe, iscode, |
Christian Heimes | 7131fd9 | 2008-02-19 14:21:46 +0000 | [diff] [blame] | 20 | # isbuiltin, isroutine, isgenerator, isgeneratorfunction, getmembers, |
| 21 | # getdoc, getfile, getmodule, getsourcefile, getcomments, getsource, |
| 22 | # getclasstree, getargspec, getargvalues, formatargspec, formatargvalues, |
| 23 | # currentframe, stack, trace, isdatadescriptor |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 24 | |
Nick Coghlan | f088e5e | 2008-12-14 11:50:48 +0000 | [diff] [blame] | 25 | # NOTE: There are some additional tests relating to interaction with |
| 26 | # zipimport in the test_zipimport_support test module. |
| 27 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 28 | modfile = mod.__file__ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 29 | if modfile.endswith(('c', 'o')): |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 30 | modfile = modfile[:-1] |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 31 | |
Christian Heimes | a3538eb | 2007-11-06 11:44:48 +0000 | [diff] [blame] | 32 | # Normalize file names: on Windows, the case of file names of compiled |
| 33 | # modules depends on the path used to start the python executable. |
| 34 | modfile = normcase(modfile) |
| 35 | |
| 36 | def revise(filename, *args): |
| 37 | return (normcase(filename),) + args |
| 38 | |
Georg Brandl | 1a3284e | 2007-12-02 09:40:06 +0000 | [diff] [blame] | 39 | import builtins |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 40 | |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 41 | try: |
| 42 | 1/0 |
| 43 | except: |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 44 | tb = sys.exc_info()[2] |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 45 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 46 | git = mod.StupidGit() |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 47 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 48 | class IsTestBase(unittest.TestCase): |
| 49 | predicates = set([inspect.isbuiltin, inspect.isclass, inspect.iscode, |
| 50 | inspect.isframe, inspect.isfunction, inspect.ismethod, |
Christian Heimes | 7131fd9 | 2008-02-19 14:21:46 +0000 | [diff] [blame] | 51 | inspect.ismodule, inspect.istraceback, |
| 52 | inspect.isgenerator, inspect.isgeneratorfunction]) |
Tim Peters | 5a9fb3c | 2005-01-07 16:01:32 +0000 | [diff] [blame] | 53 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 54 | def istest(self, predicate, exp): |
| 55 | obj = eval(exp) |
Georg Brandl | ab91fde | 2009-08-13 08:51:18 +0000 | [diff] [blame] | 56 | self.assertTrue(predicate(obj), '%s(%s)' % (predicate.__name__, exp)) |
Tim Peters | 5a9fb3c | 2005-01-07 16:01:32 +0000 | [diff] [blame] | 57 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 58 | for other in self.predicates - set([predicate]): |
Christian Heimes | 7131fd9 | 2008-02-19 14:21:46 +0000 | [diff] [blame] | 59 | if predicate == inspect.isgeneratorfunction and\ |
| 60 | other == inspect.isfunction: |
| 61 | continue |
Georg Brandl | ab91fde | 2009-08-13 08:51:18 +0000 | [diff] [blame] | 62 | self.assertFalse(other(obj), 'not %s(%s)' % (other.__name__, exp)) |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 63 | |
Christian Heimes | 7131fd9 | 2008-02-19 14:21:46 +0000 | [diff] [blame] | 64 | def generator_function_example(self): |
| 65 | for i in range(2): |
| 66 | yield i |
| 67 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 68 | class TestPredicates(IsTestBase): |
Christian Heimes | 227c800 | 2008-03-03 20:34:40 +0000 | [diff] [blame] | 69 | def test_sixteen(self): |
Guido van Rossum | c1f779c | 2007-07-03 08:25:58 +0000 | [diff] [blame] | 70 | count = len([x for x in dir(inspect) if x.startswith('is')]) |
Christian Heimes | 7131fd9 | 2008-02-19 14:21:46 +0000 | [diff] [blame] | 71 | # This test is here for remember you to update Doc/library/inspect.rst |
Christian Heimes | 7864476 | 2008-03-04 23:39:23 +0000 | [diff] [blame] | 72 | # which claims there are 16 such functions |
Christian Heimes | 227c800 | 2008-03-03 20:34:40 +0000 | [diff] [blame] | 73 | expected = 16 |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 74 | err_msg = "There are %d (not %d) is* functions" % (count, expected) |
| 75 | self.assertEqual(count, expected, err_msg) |
Tim Peters | 5a9fb3c | 2005-01-07 16:01:32 +0000 | [diff] [blame] | 76 | |
Christian Heimes | 7131fd9 | 2008-02-19 14:21:46 +0000 | [diff] [blame] | 77 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 78 | def test_excluding_predicates(self): |
| 79 | self.istest(inspect.isbuiltin, 'sys.exit') |
| 80 | self.istest(inspect.isbuiltin, '[].append') |
Neal Norwitz | 221085d | 2007-02-25 20:55:47 +0000 | [diff] [blame] | 81 | self.istest(inspect.iscode, 'mod.spam.__code__') |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 82 | self.istest(inspect.isframe, 'tb.tb_frame') |
| 83 | self.istest(inspect.isfunction, 'mod.spam') |
Christian Heimes | 4a22b5d | 2007-11-25 09:39:14 +0000 | [diff] [blame] | 84 | self.istest(inspect.isfunction, 'mod.StupidGit.abuse') |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 85 | self.istest(inspect.ismethod, 'git.argue') |
| 86 | self.istest(inspect.ismodule, 'mod') |
| 87 | self.istest(inspect.istraceback, 'tb') |
Guido van Rossum | 813b0e5 | 2007-05-21 18:11:34 +0000 | [diff] [blame] | 88 | self.istest(inspect.isdatadescriptor, 'collections.defaultdict.default_factory') |
Christian Heimes | 7131fd9 | 2008-02-19 14:21:46 +0000 | [diff] [blame] | 89 | self.istest(inspect.isgenerator, '(x for x in range(2))') |
| 90 | self.istest(inspect.isgeneratorfunction, 'generator_function_example') |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 91 | if hasattr(types, 'GetSetDescriptorType'): |
| 92 | self.istest(inspect.isgetsetdescriptor, |
| 93 | 'type(tb.tb_frame).f_locals') |
| 94 | else: |
Georg Brandl | ab91fde | 2009-08-13 08:51:18 +0000 | [diff] [blame] | 95 | self.assertFalse(inspect.isgetsetdescriptor(type(tb.tb_frame).f_locals)) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 96 | if hasattr(types, 'MemberDescriptorType'): |
| 97 | self.istest(inspect.ismemberdescriptor, 'datetime.timedelta.days') |
| 98 | else: |
Georg Brandl | ab91fde | 2009-08-13 08:51:18 +0000 | [diff] [blame] | 99 | self.assertFalse(inspect.ismemberdescriptor(datetime.timedelta.days)) |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 100 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 101 | def test_isroutine(self): |
Georg Brandl | ab91fde | 2009-08-13 08:51:18 +0000 | [diff] [blame] | 102 | self.assertTrue(inspect.isroutine(mod.spam)) |
| 103 | self.assertTrue(inspect.isroutine([].count)) |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 104 | |
Benjamin Peterson | c465600 | 2009-01-17 22:41:18 +0000 | [diff] [blame] | 105 | def test_isclass(self): |
| 106 | self.istest(inspect.isclass, 'mod.StupidGit') |
| 107 | self.assertTrue(inspect.isclass(list)) |
| 108 | |
| 109 | class CustomGetattr(object): |
| 110 | def __getattr__(self, attr): |
| 111 | return None |
| 112 | self.assertFalse(inspect.isclass(CustomGetattr())) |
| 113 | |
Benjamin Peterson | 058e31e | 2009-01-16 03:54:08 +0000 | [diff] [blame] | 114 | def test_get_slot_members(self): |
| 115 | class C(object): |
| 116 | __slots__ = ("a", "b") |
| 117 | |
| 118 | x = C() |
| 119 | x.a = 42 |
| 120 | members = dict(inspect.getmembers(x)) |
Georg Brandl | ab91fde | 2009-08-13 08:51:18 +0000 | [diff] [blame] | 121 | self.assertTrue('a' in members) |
| 122 | self.assertTrue('b' not in members) |
Benjamin Peterson | 058e31e | 2009-01-16 03:54:08 +0000 | [diff] [blame] | 123 | |
Benjamin Peterson | 8f6713f | 2009-11-13 02:29:35 +0000 | [diff] [blame] | 124 | def test_isabstract(self): |
| 125 | from abc import ABCMeta, abstractmethod |
| 126 | |
| 127 | class AbstractClassExample(metaclass=ABCMeta): |
| 128 | |
| 129 | @abstractmethod |
| 130 | def foo(self): |
| 131 | pass |
| 132 | |
| 133 | class ClassExample(AbstractClassExample): |
| 134 | def foo(self): |
| 135 | pass |
| 136 | |
| 137 | a = ClassExample() |
| 138 | |
| 139 | # Test general behaviour. |
| 140 | self.assertTrue(inspect.isabstract(AbstractClassExample)) |
| 141 | self.assertFalse(inspect.isabstract(ClassExample)) |
| 142 | self.assertFalse(inspect.isabstract(a)) |
| 143 | self.assertFalse(inspect.isabstract(int)) |
| 144 | self.assertFalse(inspect.isabstract(5)) |
| 145 | |
Benjamin Peterson | 058e31e | 2009-01-16 03:54:08 +0000 | [diff] [blame] | 146 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 147 | class TestInterpreterStack(IsTestBase): |
| 148 | def __init__(self, *args, **kwargs): |
| 149 | unittest.TestCase.__init__(self, *args, **kwargs) |
Tim Peters | 5a9fb3c | 2005-01-07 16:01:32 +0000 | [diff] [blame] | 150 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 151 | git.abuse(7, 8, 9) |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 152 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 153 | def test_abuse_done(self): |
| 154 | self.istest(inspect.istraceback, 'git.ex[2]') |
| 155 | self.istest(inspect.isframe, 'mod.fr') |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 156 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 157 | def test_stack(self): |
Georg Brandl | ab91fde | 2009-08-13 08:51:18 +0000 | [diff] [blame] | 158 | self.assertTrue(len(mod.st) >= 5) |
Christian Heimes | a3538eb | 2007-11-06 11:44:48 +0000 | [diff] [blame] | 159 | self.assertEqual(revise(*mod.st[0][1:]), |
Tim Peters | 5a9fb3c | 2005-01-07 16:01:32 +0000 | [diff] [blame] | 160 | (modfile, 16, 'eggs', [' st = inspect.stack()\n'], 0)) |
Christian Heimes | a3538eb | 2007-11-06 11:44:48 +0000 | [diff] [blame] | 161 | self.assertEqual(revise(*mod.st[1][1:]), |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 162 | (modfile, 9, 'spam', [' eggs(b + d, c + f)\n'], 0)) |
Christian Heimes | a3538eb | 2007-11-06 11:44:48 +0000 | [diff] [blame] | 163 | self.assertEqual(revise(*mod.st[2][1:]), |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 164 | (modfile, 43, 'argue', [' spam(a, b, c)\n'], 0)) |
Christian Heimes | a3538eb | 2007-11-06 11:44:48 +0000 | [diff] [blame] | 165 | self.assertEqual(revise(*mod.st[3][1:]), |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 166 | (modfile, 39, 'abuse', [' self.argue(a, b, c)\n'], 0)) |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 167 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 168 | def test_trace(self): |
| 169 | self.assertEqual(len(git.tr), 3) |
Christian Heimes | a3538eb | 2007-11-06 11:44:48 +0000 | [diff] [blame] | 170 | self.assertEqual(revise(*git.tr[0][1:]), |
| 171 | (modfile, 43, 'argue', [' spam(a, b, c)\n'], 0)) |
| 172 | self.assertEqual(revise(*git.tr[1][1:]), |
| 173 | (modfile, 9, 'spam', [' eggs(b + d, c + f)\n'], 0)) |
| 174 | self.assertEqual(revise(*git.tr[2][1:]), |
| 175 | (modfile, 18, 'eggs', [' q = y / 0\n'], 0)) |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 176 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 177 | def test_frame(self): |
| 178 | args, varargs, varkw, locals = inspect.getargvalues(mod.fr) |
| 179 | self.assertEqual(args, ['x', 'y']) |
| 180 | self.assertEqual(varargs, None) |
| 181 | self.assertEqual(varkw, None) |
| 182 | self.assertEqual(locals, {'x': 11, 'p': 11, 'y': 14}) |
| 183 | self.assertEqual(inspect.formatargvalues(args, varargs, varkw, locals), |
| 184 | '(x=11, y=14)') |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 185 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 186 | def test_previous_frame(self): |
| 187 | args, varargs, varkw, locals = inspect.getargvalues(mod.fr.f_back) |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 188 | self.assertEqual(args, ['a', 'b', 'c', 'd', 'e', 'f']) |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 189 | self.assertEqual(varargs, 'g') |
| 190 | self.assertEqual(varkw, 'h') |
| 191 | self.assertEqual(inspect.formatargvalues(args, varargs, varkw, locals), |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 192 | '(a=7, b=8, c=9, d=3, e=4, f=5, *g=(), **h={})') |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 193 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 194 | class GetSourceBase(unittest.TestCase): |
| 195 | # Subclasses must override. |
| 196 | fodderFile = None |
Tim Peters | 5a9fb3c | 2005-01-07 16:01:32 +0000 | [diff] [blame] | 197 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 198 | def __init__(self, *args, **kwargs): |
| 199 | unittest.TestCase.__init__(self, *args, **kwargs) |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 200 | |
Philip Jenvey | a27c5bd | 2009-05-28 06:09:08 +0000 | [diff] [blame] | 201 | with open(inspect.getsourcefile(self.fodderFile)) as fp: |
| 202 | self.source = fp.read() |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 203 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 204 | def sourcerange(self, top, bottom): |
| 205 | lines = self.source.split("\n") |
| 206 | return "\n".join(lines[top-1:bottom]) + "\n" |
Tim Peters | e0b2d7a | 2001-09-22 06:10:55 +0000 | [diff] [blame] | 207 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 208 | def assertSourceEqual(self, obj, top, bottom): |
| 209 | self.assertEqual(inspect.getsource(obj), |
| 210 | self.sourcerange(top, bottom)) |
Tim Peters | 5a9fb3c | 2005-01-07 16:01:32 +0000 | [diff] [blame] | 211 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 212 | class TestRetrievingSourceCode(GetSourceBase): |
| 213 | fodderFile = mod |
Tim Peters | 5a9fb3c | 2005-01-07 16:01:32 +0000 | [diff] [blame] | 214 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 215 | def test_getclasses(self): |
| 216 | classes = inspect.getmembers(mod, inspect.isclass) |
| 217 | self.assertEqual(classes, |
| 218 | [('FesteringGob', mod.FesteringGob), |
| 219 | ('MalodorousPervert', mod.MalodorousPervert), |
| 220 | ('ParrotDroppings', mod.ParrotDroppings), |
| 221 | ('StupidGit', mod.StupidGit)]) |
| 222 | tree = inspect.getclasstree([cls[1] for cls in classes], 1) |
| 223 | self.assertEqual(tree, |
Thomas Wouters | 725af87 | 2006-04-15 09:13:19 +0000 | [diff] [blame] | 224 | [(object, ()), |
| 225 | [(mod.ParrotDroppings, (object,)), |
| 226 | (mod.StupidGit, (object,)), |
| 227 | [(mod.MalodorousPervert, (mod.StupidGit,)), |
| 228 | [(mod.FesteringGob, (mod.MalodorousPervert, |
| 229 | mod.ParrotDroppings)) |
| 230 | ] |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 231 | ] |
| 232 | ] |
| 233 | ]) |
Tim Peters | 5a9fb3c | 2005-01-07 16:01:32 +0000 | [diff] [blame] | 234 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 235 | def test_getfunctions(self): |
| 236 | functions = inspect.getmembers(mod, inspect.isfunction) |
| 237 | self.assertEqual(functions, [('eggs', mod.eggs), |
| 238 | ('spam', mod.spam)]) |
Johannes Gijsbers | c473c99 | 2004-08-18 12:40:31 +0000 | [diff] [blame] | 239 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 240 | def test_getdoc(self): |
| 241 | self.assertEqual(inspect.getdoc(mod), 'A module docstring.') |
| 242 | self.assertEqual(inspect.getdoc(mod.StupidGit), |
| 243 | 'A longer,\n\nindented\n\ndocstring.') |
| 244 | self.assertEqual(inspect.getdoc(git.abuse), |
| 245 | 'Another\n\ndocstring\n\ncontaining\n\ntabs') |
Johannes Gijsbers | c473c99 | 2004-08-18 12:40:31 +0000 | [diff] [blame] | 246 | |
Georg Brandl | 0c77a82 | 2008-06-10 16:37:50 +0000 | [diff] [blame] | 247 | def test_cleandoc(self): |
| 248 | self.assertEqual(inspect.cleandoc('An\n indented\n docstring.'), |
| 249 | 'An\nindented\ndocstring.') |
| 250 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 251 | def test_getcomments(self): |
| 252 | self.assertEqual(inspect.getcomments(mod), '# line 1\n') |
| 253 | self.assertEqual(inspect.getcomments(mod.StupidGit), '# line 20\n') |
Johannes Gijsbers | c473c99 | 2004-08-18 12:40:31 +0000 | [diff] [blame] | 254 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 255 | def test_getmodule(self): |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 256 | # Check actual module |
| 257 | self.assertEqual(inspect.getmodule(mod), mod) |
| 258 | # Check class (uses __module__ attribute) |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 259 | self.assertEqual(inspect.getmodule(mod.StupidGit), mod) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 260 | # Check a method (no __module__ attribute, falls back to filename) |
| 261 | self.assertEqual(inspect.getmodule(mod.StupidGit.abuse), mod) |
| 262 | # Do it again (check the caching isn't broken) |
| 263 | self.assertEqual(inspect.getmodule(mod.StupidGit.abuse), mod) |
| 264 | # Check a builtin |
Georg Brandl | 1a3284e | 2007-12-02 09:40:06 +0000 | [diff] [blame] | 265 | self.assertEqual(inspect.getmodule(str), sys.modules["builtins"]) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 266 | # Check filename override |
| 267 | self.assertEqual(inspect.getmodule(None, modfile), mod) |
Johannes Gijsbers | c473c99 | 2004-08-18 12:40:31 +0000 | [diff] [blame] | 268 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 269 | def test_getsource(self): |
| 270 | self.assertSourceEqual(git.abuse, 29, 39) |
| 271 | self.assertSourceEqual(mod.StupidGit, 21, 46) |
Johannes Gijsbers | c473c99 | 2004-08-18 12:40:31 +0000 | [diff] [blame] | 272 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 273 | def test_getsourcefile(self): |
Christian Heimes | a3538eb | 2007-11-06 11:44:48 +0000 | [diff] [blame] | 274 | self.assertEqual(normcase(inspect.getsourcefile(mod.spam)), modfile) |
| 275 | self.assertEqual(normcase(inspect.getsourcefile(git.abuse)), modfile) |
R. David Murray | 19d8cc5 | 2010-06-17 02:06:12 +0000 | [diff] [blame] | 276 | fn = "_non_existing_filename_used_for_sourcefile_test.py" |
| 277 | co = compile("None", fn, "exec") |
R. David Murray | 0217eb8 | 2010-06-17 13:27:40 +0000 | [diff] [blame] | 278 | self.assertEqual(inspect.getsourcefile(co), None) |
R. David Murray | 19d8cc5 | 2010-06-17 02:06:12 +0000 | [diff] [blame] | 279 | linecache.cache[co.co_filename] = (1, None, "None", co.co_filename) |
| 280 | self.assertEqual(normcase(inspect.getsourcefile(co)), fn) |
Johannes Gijsbers | c473c99 | 2004-08-18 12:40:31 +0000 | [diff] [blame] | 281 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 282 | def test_getfile(self): |
| 283 | self.assertEqual(inspect.getfile(mod.StupidGit), mod.__file__) |
Johannes Gijsbers | c473c99 | 2004-08-18 12:40:31 +0000 | [diff] [blame] | 284 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 285 | def test_getmodule_recursion(self): |
Christian Heimes | 45f9af3 | 2007-11-27 21:50:00 +0000 | [diff] [blame] | 286 | from types import ModuleType |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 287 | name = '__inspect_dummy' |
Christian Heimes | 45f9af3 | 2007-11-27 21:50:00 +0000 | [diff] [blame] | 288 | m = sys.modules[name] = ModuleType(name) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 289 | m.__file__ = "<string>" # hopefully not a real filename... |
| 290 | m.__loader__ = "dummy" # pretend the filename is understood by a loader |
Georg Brandl | 7cae87c | 2006-09-06 06:51:57 +0000 | [diff] [blame] | 291 | exec("def x(): pass", m.__dict__) |
Neal Norwitz | 221085d | 2007-02-25 20:55:47 +0000 | [diff] [blame] | 292 | self.assertEqual(inspect.getsourcefile(m.x.__code__), '<string>') |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 293 | del sys.modules[name] |
| 294 | inspect.getmodule(compile('a=10','','single')) |
| 295 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 296 | class TestDecorators(GetSourceBase): |
| 297 | fodderFile = mod2 |
Johannes Gijsbers | c473c99 | 2004-08-18 12:40:31 +0000 | [diff] [blame] | 298 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 299 | def test_wrapped_decorator(self): |
Christian Heimes | 09aaa88 | 2008-02-23 15:01:06 +0000 | [diff] [blame] | 300 | self.assertSourceEqual(mod2.wrapped, 14, 17) |
Johannes Gijsbers | c473c99 | 2004-08-18 12:40:31 +0000 | [diff] [blame] | 301 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 302 | def test_replacing_decorator(self): |
| 303 | self.assertSourceEqual(mod2.gone, 9, 10) |
Tim Peters | e0b2d7a | 2001-09-22 06:10:55 +0000 | [diff] [blame] | 304 | |
Johannes Gijsbers | 1542f34 | 2004-12-12 16:46:28 +0000 | [diff] [blame] | 305 | class TestOneliners(GetSourceBase): |
| 306 | fodderFile = mod2 |
| 307 | def test_oneline_lambda(self): |
| 308 | # Test inspect.getsource with a one-line lambda function. |
| 309 | self.assertSourceEqual(mod2.oll, 25, 25) |
Tim Peters | 5a9fb3c | 2005-01-07 16:01:32 +0000 | [diff] [blame] | 310 | |
Johannes Gijsbers | 1542f34 | 2004-12-12 16:46:28 +0000 | [diff] [blame] | 311 | def test_threeline_lambda(self): |
| 312 | # Test inspect.getsource with a three-line lambda function, |
| 313 | # where the second and third lines are _not_ indented. |
Tim Peters | 5a9fb3c | 2005-01-07 16:01:32 +0000 | [diff] [blame] | 314 | self.assertSourceEqual(mod2.tll, 28, 30) |
| 315 | |
Johannes Gijsbers | 1542f34 | 2004-12-12 16:46:28 +0000 | [diff] [blame] | 316 | def test_twoline_indented_lambda(self): |
| 317 | # Test inspect.getsource with a two-line lambda function, |
| 318 | # where the second line _is_ indented. |
| 319 | self.assertSourceEqual(mod2.tlli, 33, 34) |
Tim Peters | 5a9fb3c | 2005-01-07 16:01:32 +0000 | [diff] [blame] | 320 | |
Johannes Gijsbers | 1542f34 | 2004-12-12 16:46:28 +0000 | [diff] [blame] | 321 | def test_onelinefunc(self): |
| 322 | # Test inspect.getsource with a regular one-line function. |
| 323 | self.assertSourceEqual(mod2.onelinefunc, 37, 37) |
Tim Peters | 5a9fb3c | 2005-01-07 16:01:32 +0000 | [diff] [blame] | 324 | |
Johannes Gijsbers | 1542f34 | 2004-12-12 16:46:28 +0000 | [diff] [blame] | 325 | def test_manyargs(self): |
| 326 | # Test inspect.getsource with a regular function where |
| 327 | # the arguments are on two lines and _not_ indented and |
| 328 | # the body on the second line with the last arguments. |
| 329 | self.assertSourceEqual(mod2.manyargs, 40, 41) |
Tim Peters | 5a9fb3c | 2005-01-07 16:01:32 +0000 | [diff] [blame] | 330 | |
Johannes Gijsbers | 1542f34 | 2004-12-12 16:46:28 +0000 | [diff] [blame] | 331 | def test_twolinefunc(self): |
| 332 | # Test inspect.getsource with a regular function where |
| 333 | # the body is on two lines, following the argument list and |
| 334 | # continued on the next line by a \\. |
| 335 | self.assertSourceEqual(mod2.twolinefunc, 44, 45) |
Tim Peters | 5a9fb3c | 2005-01-07 16:01:32 +0000 | [diff] [blame] | 336 | |
Johannes Gijsbers | 1542f34 | 2004-12-12 16:46:28 +0000 | [diff] [blame] | 337 | def test_lambda_in_list(self): |
| 338 | # Test inspect.getsource with a one-line lambda function |
| 339 | # defined in a list, indented. |
| 340 | self.assertSourceEqual(mod2.a[1], 49, 49) |
Tim Peters | 5a9fb3c | 2005-01-07 16:01:32 +0000 | [diff] [blame] | 341 | |
Johannes Gijsbers | 1542f34 | 2004-12-12 16:46:28 +0000 | [diff] [blame] | 342 | def test_anonymous(self): |
| 343 | # Test inspect.getsource with a lambda function defined |
| 344 | # as argument to another function. |
| 345 | self.assertSourceEqual(mod2.anonymous, 55, 55) |
| 346 | |
Johannes Gijsbers | a5855d5 | 2005-03-12 16:37:11 +0000 | [diff] [blame] | 347 | class TestBuggyCases(GetSourceBase): |
| 348 | fodderFile = mod2 |
| 349 | |
| 350 | def test_with_comment(self): |
| 351 | self.assertSourceEqual(mod2.with_comment, 58, 59) |
| 352 | |
| 353 | def test_multiline_sig(self): |
| 354 | self.assertSourceEqual(mod2.multiline_sig[0], 63, 64) |
| 355 | |
Armin Rigo | dd5c023 | 2005-09-25 11:45:45 +0000 | [diff] [blame] | 356 | def test_nested_class(self): |
| 357 | self.assertSourceEqual(mod2.func69().func71, 71, 72) |
| 358 | |
| 359 | def test_one_liner_followed_by_non_name(self): |
| 360 | self.assertSourceEqual(mod2.func77, 77, 77) |
| 361 | |
| 362 | def test_one_liner_dedent_non_name(self): |
| 363 | self.assertSourceEqual(mod2.cls82.func83, 83, 83) |
| 364 | |
| 365 | def test_with_comment_instead_of_docstring(self): |
| 366 | self.assertSourceEqual(mod2.func88, 88, 90) |
| 367 | |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 368 | def test_method_in_dynamic_class(self): |
| 369 | self.assertSourceEqual(mod2.method_in_dynamic_class, 95, 97) |
| 370 | |
R. David Murray | b565577 | 2009-05-14 16:17:50 +0000 | [diff] [blame] | 371 | @unittest.skipIf( |
| 372 | not hasattr(unicodedata, '__file__') or |
| 373 | unicodedata.__file__[-4:] in (".pyc", ".pyo"), |
| 374 | "unicodedata is not an external binary module") |
R. David Murray | 74b8924 | 2009-05-13 17:33:03 +0000 | [diff] [blame] | 375 | def test_findsource_binary(self): |
R. David Murray | b565577 | 2009-05-14 16:17:50 +0000 | [diff] [blame] | 376 | self.assertRaises(IOError, inspect.getsource, unicodedata) |
| 377 | self.assertRaises(IOError, inspect.findsource, unicodedata) |
R. David Murray | 74b8924 | 2009-05-13 17:33:03 +0000 | [diff] [blame] | 378 | |
R. David Murray | 19d8cc5 | 2010-06-17 02:06:12 +0000 | [diff] [blame] | 379 | def test_findsource_code_in_linecache(self): |
| 380 | lines = ["x=1"] |
| 381 | co = compile(lines[0], "_dynamically_created_file", "exec") |
| 382 | self.assertRaises(IOError, inspect.findsource, co) |
| 383 | self.assertRaises(IOError, inspect.getsource, co) |
| 384 | linecache.cache[co.co_filename] = (1, None, lines, co.co_filename) |
Ezio Melotti | 19f2aeb | 2010-11-21 01:30:29 +0000 | [diff] [blame] | 385 | self.assertEqual(inspect.findsource(co), (lines,0)) |
| 386 | self.assertEqual(inspect.getsource(co), lines[0]) |
R. David Murray | 19d8cc5 | 2010-06-17 02:06:12 +0000 | [diff] [blame] | 387 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 388 | # Helper for testing classify_class_attrs. |
Tim Peters | 13b49d3 | 2001-09-23 02:00:29 +0000 | [diff] [blame] | 389 | def attrs_wo_objs(cls): |
| 390 | return [t[:3] for t in inspect.classify_class_attrs(cls)] |
| 391 | |
Tim Peters | 5a9fb3c | 2005-01-07 16:01:32 +0000 | [diff] [blame] | 392 | class TestClassesAndFunctions(unittest.TestCase): |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 393 | def test_newstyle_mro(self): |
| 394 | # The same w/ new-class MRO. |
| 395 | class A(object): pass |
| 396 | class B(A): pass |
| 397 | class C(A): pass |
| 398 | class D(B, C): pass |
Tim Peters | 13b49d3 | 2001-09-23 02:00:29 +0000 | [diff] [blame] | 399 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 400 | expected = (D, B, C, A, object) |
| 401 | got = inspect.getmro(D) |
| 402 | self.assertEqual(expected, got) |
Tim Peters | 13b49d3 | 2001-09-23 02:00:29 +0000 | [diff] [blame] | 403 | |
Christian Heimes | 3795b53 | 2007-11-08 13:48:53 +0000 | [diff] [blame] | 404 | def assertArgSpecEquals(self, routine, args_e, varargs_e=None, |
| 405 | varkw_e=None, defaults_e=None, formatted=None): |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 406 | args, varargs, varkw, defaults = inspect.getargspec(routine) |
| 407 | self.assertEqual(args, args_e) |
| 408 | self.assertEqual(varargs, varargs_e) |
| 409 | self.assertEqual(varkw, varkw_e) |
| 410 | self.assertEqual(defaults, defaults_e) |
| 411 | if formatted is not None: |
| 412 | self.assertEqual(inspect.formatargspec(args, varargs, varkw, defaults), |
| 413 | formatted) |
Tim Peters | 13b49d3 | 2001-09-23 02:00:29 +0000 | [diff] [blame] | 414 | |
Christian Heimes | 3795b53 | 2007-11-08 13:48:53 +0000 | [diff] [blame] | 415 | def assertFullArgSpecEquals(self, routine, args_e, varargs_e=None, |
| 416 | varkw_e=None, defaults_e=None, |
| 417 | kwonlyargs_e=[], kwonlydefaults_e=None, |
| 418 | ann_e={}, formatted=None): |
| 419 | args, varargs, varkw, defaults, kwonlyargs, kwonlydefaults, ann = \ |
| 420 | inspect.getfullargspec(routine) |
| 421 | self.assertEqual(args, args_e) |
| 422 | self.assertEqual(varargs, varargs_e) |
| 423 | self.assertEqual(varkw, varkw_e) |
| 424 | self.assertEqual(defaults, defaults_e) |
| 425 | self.assertEqual(kwonlyargs, kwonlyargs_e) |
| 426 | self.assertEqual(kwonlydefaults, kwonlydefaults_e) |
| 427 | self.assertEqual(ann, ann_e) |
| 428 | if formatted is not None: |
| 429 | self.assertEqual(inspect.formatargspec(args, varargs, varkw, defaults, |
| 430 | kwonlyargs, kwonlydefaults, ann), |
| 431 | formatted) |
| 432 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 433 | def test_getargspec(self): |
Christian Heimes | 3795b53 | 2007-11-08 13:48:53 +0000 | [diff] [blame] | 434 | self.assertArgSpecEquals(mod.eggs, ['x', 'y'], formatted='(x, y)') |
Tim Peters | 13b49d3 | 2001-09-23 02:00:29 +0000 | [diff] [blame] | 435 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 436 | self.assertArgSpecEquals(mod.spam, |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 437 | ['a', 'b', 'c', 'd', 'e', 'f'], |
| 438 | 'g', 'h', (3, 4, 5), |
| 439 | '(a, b, c, d=3, e=4, f=5, *g, **h)') |
Tim Peters | 13b49d3 | 2001-09-23 02:00:29 +0000 | [diff] [blame] | 440 | |
Christian Heimes | 3795b53 | 2007-11-08 13:48:53 +0000 | [diff] [blame] | 441 | self.assertRaises(ValueError, self.assertArgSpecEquals, |
| 442 | mod2.keyworded, []) |
| 443 | |
| 444 | self.assertRaises(ValueError, self.assertArgSpecEquals, |
| 445 | mod2.annotated, []) |
Benjamin Peterson | 9953a8d | 2009-01-17 04:15:01 +0000 | [diff] [blame] | 446 | self.assertRaises(ValueError, self.assertArgSpecEquals, |
| 447 | mod2.keyword_only_arg, []) |
| 448 | |
Christian Heimes | 3795b53 | 2007-11-08 13:48:53 +0000 | [diff] [blame] | 449 | |
| 450 | def test_getfullargspec(self): |
| 451 | self.assertFullArgSpecEquals(mod2.keyworded, [], varargs_e='arg1', |
| 452 | kwonlyargs_e=['arg2'], |
| 453 | kwonlydefaults_e={'arg2':1}, |
| 454 | formatted='(*arg1, arg2=1)') |
| 455 | |
| 456 | self.assertFullArgSpecEquals(mod2.annotated, ['arg1'], |
Christian Heimes | c9543e4 | 2007-11-28 08:28:28 +0000 | [diff] [blame] | 457 | ann_e={'arg1' : list}, |
Christian Heimes | 3795b53 | 2007-11-08 13:48:53 +0000 | [diff] [blame] | 458 | formatted='(arg1: list)') |
Benjamin Peterson | 9953a8d | 2009-01-17 04:15:01 +0000 | [diff] [blame] | 459 | self.assertFullArgSpecEquals(mod2.keyword_only_arg, [], |
| 460 | kwonlyargs_e=['arg'], |
| 461 | formatted='(*, arg)') |
| 462 | |
Christian Heimes | 3795b53 | 2007-11-08 13:48:53 +0000 | [diff] [blame] | 463 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 464 | def test_getargspec_method(self): |
| 465 | class A(object): |
| 466 | def m(self): |
| 467 | pass |
| 468 | self.assertArgSpecEquals(A.m, ['self']) |
Tim Peters | 13b49d3 | 2001-09-23 02:00:29 +0000 | [diff] [blame] | 469 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 470 | def test_classify_newstyle(self): |
| 471 | class A(object): |
Tim Peters | 13b49d3 | 2001-09-23 02:00:29 +0000 | [diff] [blame] | 472 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 473 | def s(): pass |
| 474 | s = staticmethod(s) |
Tim Peters | 13b49d3 | 2001-09-23 02:00:29 +0000 | [diff] [blame] | 475 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 476 | def c(cls): pass |
| 477 | c = classmethod(c) |
Tim Peters | 13b49d3 | 2001-09-23 02:00:29 +0000 | [diff] [blame] | 478 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 479 | def getp(self): pass |
| 480 | p = property(getp) |
Tim Peters | 13b49d3 | 2001-09-23 02:00:29 +0000 | [diff] [blame] | 481 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 482 | def m(self): pass |
Tim Peters | 13b49d3 | 2001-09-23 02:00:29 +0000 | [diff] [blame] | 483 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 484 | def m1(self): pass |
Tim Peters | 13b49d3 | 2001-09-23 02:00:29 +0000 | [diff] [blame] | 485 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 486 | datablob = '1' |
Tim Peters | 13b49d3 | 2001-09-23 02:00:29 +0000 | [diff] [blame] | 487 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 488 | attrs = attrs_wo_objs(A) |
Georg Brandl | ab91fde | 2009-08-13 08:51:18 +0000 | [diff] [blame] | 489 | self.assertTrue(('s', 'static method', A) in attrs, 'missing static method') |
| 490 | self.assertTrue(('c', 'class method', A) in attrs, 'missing class method') |
| 491 | self.assertTrue(('p', 'property', A) in attrs, 'missing property') |
| 492 | self.assertTrue(('m', 'method', A) in attrs, |
Christian Heimes | 4a22b5d | 2007-11-25 09:39:14 +0000 | [diff] [blame] | 493 | 'missing plain method: %r' % attrs) |
Georg Brandl | ab91fde | 2009-08-13 08:51:18 +0000 | [diff] [blame] | 494 | self.assertTrue(('m1', 'method', A) in attrs, 'missing plain method') |
| 495 | self.assertTrue(('datablob', 'data', A) in attrs, 'missing data') |
Tim Peters | 13b49d3 | 2001-09-23 02:00:29 +0000 | [diff] [blame] | 496 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 497 | class B(A): |
Tim Peters | 13b49d3 | 2001-09-23 02:00:29 +0000 | [diff] [blame] | 498 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 499 | def m(self): pass |
Tim Peters | 13b49d3 | 2001-09-23 02:00:29 +0000 | [diff] [blame] | 500 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 501 | attrs = attrs_wo_objs(B) |
Georg Brandl | ab91fde | 2009-08-13 08:51:18 +0000 | [diff] [blame] | 502 | self.assertTrue(('s', 'static method', A) in attrs, 'missing static method') |
| 503 | self.assertTrue(('c', 'class method', A) in attrs, 'missing class method') |
| 504 | self.assertTrue(('p', 'property', A) in attrs, 'missing property') |
| 505 | self.assertTrue(('m', 'method', B) in attrs, 'missing plain method') |
| 506 | self.assertTrue(('m1', 'method', A) in attrs, 'missing plain method') |
| 507 | self.assertTrue(('datablob', 'data', A) in attrs, 'missing data') |
Tim Peters | 13b49d3 | 2001-09-23 02:00:29 +0000 | [diff] [blame] | 508 | |
| 509 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 510 | class C(A): |
Tim Peters | 13b49d3 | 2001-09-23 02:00:29 +0000 | [diff] [blame] | 511 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 512 | def m(self): pass |
| 513 | def c(self): pass |
Tim Peters | 13b49d3 | 2001-09-23 02:00:29 +0000 | [diff] [blame] | 514 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 515 | attrs = attrs_wo_objs(C) |
Georg Brandl | ab91fde | 2009-08-13 08:51:18 +0000 | [diff] [blame] | 516 | self.assertTrue(('s', 'static method', A) in attrs, 'missing static method') |
| 517 | self.assertTrue(('c', 'method', C) in attrs, 'missing plain method') |
| 518 | self.assertTrue(('p', 'property', A) in attrs, 'missing property') |
| 519 | self.assertTrue(('m', 'method', C) in attrs, 'missing plain method') |
| 520 | self.assertTrue(('m1', 'method', A) in attrs, 'missing plain method') |
| 521 | self.assertTrue(('datablob', 'data', A) in attrs, 'missing data') |
Tim Peters | 13b49d3 | 2001-09-23 02:00:29 +0000 | [diff] [blame] | 522 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 523 | class D(B, C): |
Tim Peters | 13b49d3 | 2001-09-23 02:00:29 +0000 | [diff] [blame] | 524 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 525 | def m1(self): pass |
Tim Peters | 13b49d3 | 2001-09-23 02:00:29 +0000 | [diff] [blame] | 526 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 527 | attrs = attrs_wo_objs(D) |
Georg Brandl | ab91fde | 2009-08-13 08:51:18 +0000 | [diff] [blame] | 528 | self.assertTrue(('s', 'static method', A) in attrs, 'missing static method') |
| 529 | self.assertTrue(('c', 'method', C) in attrs, 'missing plain method') |
| 530 | self.assertTrue(('p', 'property', A) in attrs, 'missing property') |
| 531 | self.assertTrue(('m', 'method', B) in attrs, 'missing plain method') |
| 532 | self.assertTrue(('m1', 'method', D) in attrs, 'missing plain method') |
| 533 | self.assertTrue(('datablob', 'data', A) in attrs, 'missing data') |
Jeremy Hylton | c4bf5ed | 2003-06-27 18:43:12 +0000 | [diff] [blame] | 534 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 535 | def test_main(): |
Johannes Gijsbers | 1542f34 | 2004-12-12 16:46:28 +0000 | [diff] [blame] | 536 | run_unittest(TestDecorators, TestRetrievingSourceCode, TestOneliners, |
Johannes Gijsbers | a5855d5 | 2005-03-12 16:37:11 +0000 | [diff] [blame] | 537 | TestBuggyCases, |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 538 | TestInterpreterStack, TestClassesAndFunctions, TestPredicates) |
Martin v. Löwis | 893ffa4 | 2003-10-31 15:35:53 +0000 | [diff] [blame] | 539 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 540 | if __name__ == "__main__": |
| 541 | test_main() |