annotate Lib/test/test_inspect.py @ 54294:dba767bc3155 legacy-trunk

remove mention of old ctypes version
author Benjamin Peterson <benjamin@python.org>
date Sat, 23 May 2009 20:59:09 +0000
parents e962129665d8
children 70fb848870ac
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
34151
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
1 import sys
39226
28cdd43e0569 Patch #1520294: Support for getset and member descriptors in types.py,
Barry Warsaw <barry@python.org>
parents: 39121
diff changeset
2 import types
34151
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
3 import unittest
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
4 import inspect
39226
28cdd43e0569 Patch #1520294: Support for getset and member descriptors in types.py,
Barry Warsaw <barry@python.org>
parents: 39121
diff changeset
5 import datetime
16790
67bad9f20b29 inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff changeset
6
34151
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
7 from test.test_support import TESTFN, run_unittest
16790
67bad9f20b29 inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff changeset
8
34151
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
9 from test import inspect_fodder as mod
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
10 from test import inspect_fodder2 as mod2
16790
67bad9f20b29 inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff changeset
11
54041
347e59ea1a71 Issue #4050: inspect.findsource/getsource now raise an IOError if the 'source'
R. David Murray <rdmurray@bitdance.com>
parents: 50834
diff changeset
12 # C module for test_findsource_binary
54065
e962129665d8 Fix test failure on Windows, and add skip check if even unicodedata
R. David Murray <rdmurray@bitdance.com>
parents: 54041
diff changeset
13 import unicodedata
54041
347e59ea1a71 Issue #4050: inspect.findsource/getsource now raise an IOError if the 'source'
R. David Murray <rdmurray@bitdance.com>
parents: 50834
diff changeset
14
16790
67bad9f20b29 inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff changeset
15 # Functions tested in this suite:
67bad9f20b29 inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff changeset
16 # ismodule, isclass, ismethod, isfunction, istraceback, isframe, iscode,
45386
1bc923a107d9 Issue #1916. Added isgenerator() and isgeneratorfunction() to
Facundo Batista <facundobatista@gmail.com>
parents: 44034
diff changeset
17 # isbuiltin, isroutine, isgenerator, isgeneratorfunction, getmembers,
1bc923a107d9 Issue #1916. Added isgenerator() and isgeneratorfunction() to
Facundo Batista <facundobatista@gmail.com>
parents: 44034
diff changeset
18 # getdoc, getfile, getmodule, getsourcefile, getcomments, getsource,
1bc923a107d9 Issue #1916. Added isgenerator() and isgeneratorfunction() to
Facundo Batista <facundobatista@gmail.com>
parents: 44034
diff changeset
19 # getclasstree, getargspec, getargvalues, formatargspec, formatargvalues,
1bc923a107d9 Issue #1916. Added isgenerator() and isgeneratorfunction() to
Facundo Batista <facundobatista@gmail.com>
parents: 44034
diff changeset
20 # currentframe, stack, trace, isdatadescriptor
16790
67bad9f20b29 inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff changeset
21
50162
4264cf00a75c Merged revisions 67750-67751 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 47506
diff changeset
22 # NOTE: There are some additional tests relating to interaction with
4264cf00a75c Merged revisions 67750-67751 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 47506
diff changeset
23 # zipimport in the test_zipimport_support test module.
4264cf00a75c Merged revisions 67750-67751 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 47506
diff changeset
24
34151
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
25 modfile = mod.__file__
38704
ba6eebf7c719 Make use of new str.startswith/endswith semantics.
Georg Brandl <georg@python.org>
parents: 37235
diff changeset
26 if modfile.endswith(('c', 'o')):
34151
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
27 modfile = modfile[:-1]
16790
67bad9f20b29 inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff changeset
28
34151
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
29 import __builtin__
16790
67bad9f20b29 inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff changeset
30
67bad9f20b29 inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff changeset
31 try:
67bad9f20b29 inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff changeset
32 1/0
67bad9f20b29 inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff changeset
33 except:
67bad9f20b29 inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff changeset
34 tb = sys.exc_traceback
67bad9f20b29 inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff changeset
35
34151
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
36 git = mod.StupidGit()
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
37
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
38 class IsTestBase(unittest.TestCase):
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
39 predicates = set([inspect.isbuiltin, inspect.isclass, inspect.iscode,
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
40 inspect.isframe, inspect.isfunction, inspect.ismethod,
45386
1bc923a107d9 Issue #1916. Added isgenerator() and isgeneratorfunction() to
Facundo Batista <facundobatista@gmail.com>
parents: 44034
diff changeset
41 inspect.ismodule, inspect.istraceback,
1bc923a107d9 Issue #1916. Added isgenerator() and isgeneratorfunction() to
Facundo Batista <facundobatista@gmail.com>
parents: 44034
diff changeset
42 inspect.isgenerator, inspect.isgeneratorfunction])
34358
b1a84233d855 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 34152
diff changeset
43
34151
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
44 def istest(self, predicate, exp):
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
45 obj = eval(exp)
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
46 self.failUnless(predicate(obj), '%s(%s)' % (predicate.__name__, exp))
34358
b1a84233d855 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 34152
diff changeset
47
34151
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
48 for other in self.predicates - set([predicate]):
45386
1bc923a107d9 Issue #1916. Added isgenerator() and isgeneratorfunction() to
Facundo Batista <facundobatista@gmail.com>
parents: 44034
diff changeset
49 if predicate == inspect.isgeneratorfunction and\
1bc923a107d9 Issue #1916. Added isgenerator() and isgeneratorfunction() to
Facundo Batista <facundobatista@gmail.com>
parents: 44034
diff changeset
50 other == inspect.isfunction:
1bc923a107d9 Issue #1916. Added isgenerator() and isgeneratorfunction() to
Facundo Batista <facundobatista@gmail.com>
parents: 44034
diff changeset
51 continue
34151
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
52 self.failIf(other(obj), 'not %s(%s)' % (other.__name__, exp))
16790
67bad9f20b29 inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff changeset
53
45386
1bc923a107d9 Issue #1916. Added isgenerator() and isgeneratorfunction() to
Facundo Batista <facundobatista@gmail.com>
parents: 44034
diff changeset
54 def generator_function_example(self):
1bc923a107d9 Issue #1916. Added isgenerator() and isgeneratorfunction() to
Facundo Batista <facundobatista@gmail.com>
parents: 44034
diff changeset
55 for i in xrange(2):
1bc923a107d9 Issue #1916. Added isgenerator() and isgeneratorfunction() to
Facundo Batista <facundobatista@gmail.com>
parents: 44034
diff changeset
56 yield i
1bc923a107d9 Issue #1916. Added isgenerator() and isgeneratorfunction() to
Facundo Batista <facundobatista@gmail.com>
parents: 44034
diff changeset
57
34151
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
58 class TestPredicates(IsTestBase):
45637
65ef608dd66c 15 -> 16
Christian Heimes <christian@cheimes.de>
parents: 45386
diff changeset
59 def test_sixteen(self):
39238
4a6ade9ce780 Ensure the actual number matches the expected count
Neal Norwitz <nnorwitz@gmail.com>
parents: 39226
diff changeset
60 count = len(filter(lambda x:x.startswith('is'), dir(inspect)))
45386
1bc923a107d9 Issue #1916. Added isgenerator() and isgeneratorfunction() to
Facundo Batista <facundobatista@gmail.com>
parents: 44034
diff changeset
61 # This test is here for remember you to update Doc/library/inspect.rst
45640
a154f1982592 15 -> 16, the 2nd
Georg Brandl <georg@python.org>
parents: 45637
diff changeset
62 # which claims there are 16 such functions
45637
65ef608dd66c 15 -> 16
Christian Heimes <christian@cheimes.de>
parents: 45386
diff changeset
63 expected = 16
39238
4a6ade9ce780 Ensure the actual number matches the expected count
Neal Norwitz <nnorwitz@gmail.com>
parents: 39226
diff changeset
64 err_msg = "There are %d (not %d) is* functions" % (count, expected)
4a6ade9ce780 Ensure the actual number matches the expected count
Neal Norwitz <nnorwitz@gmail.com>
parents: 39226
diff changeset
65 self.assertEqual(count, expected, err_msg)
34358
b1a84233d855 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 34152
diff changeset
66
45386
1bc923a107d9 Issue #1916. Added isgenerator() and isgeneratorfunction() to
Facundo Batista <facundobatista@gmail.com>
parents: 44034
diff changeset
67
34151
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
68 def test_excluding_predicates(self):
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
69 self.istest(inspect.isbuiltin, 'sys.exit')
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
70 self.istest(inspect.isbuiltin, '[].append')
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
71 self.istest(inspect.iscode, 'mod.spam.func_code')
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
72 self.istest(inspect.isframe, 'tb.tb_frame')
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
73 self.istest(inspect.isfunction, 'mod.spam')
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
74 self.istest(inspect.ismethod, 'mod.StupidGit.abuse')
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
75 self.istest(inspect.ismethod, 'git.argue')
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
76 self.istest(inspect.ismodule, 'mod')
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
77 self.istest(inspect.istraceback, 'tb')
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
78 self.istest(inspect.isdatadescriptor, '__builtin__.file.closed')
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
79 self.istest(inspect.isdatadescriptor, '__builtin__.file.softspace')
45386
1bc923a107d9 Issue #1916. Added isgenerator() and isgeneratorfunction() to
Facundo Batista <facundobatista@gmail.com>
parents: 44034
diff changeset
80 self.istest(inspect.isgenerator, '(x for x in xrange(2))')
1bc923a107d9 Issue #1916. Added isgenerator() and isgeneratorfunction() to
Facundo Batista <facundobatista@gmail.com>
parents: 44034
diff changeset
81 self.istest(inspect.isgeneratorfunction, 'generator_function_example')
39226
28cdd43e0569 Patch #1520294: Support for getset and member descriptors in types.py,
Barry Warsaw <barry@python.org>
parents: 39121
diff changeset
82 if hasattr(types, 'GetSetDescriptorType'):
28cdd43e0569 Patch #1520294: Support for getset and member descriptors in types.py,
Barry Warsaw <barry@python.org>
parents: 39121
diff changeset
83 self.istest(inspect.isgetsetdescriptor,
28cdd43e0569 Patch #1520294: Support for getset and member descriptors in types.py,
Barry Warsaw <barry@python.org>
parents: 39121
diff changeset
84 'type(tb.tb_frame).f_locals')
28cdd43e0569 Patch #1520294: Support for getset and member descriptors in types.py,
Barry Warsaw <barry@python.org>
parents: 39121
diff changeset
85 else:
28cdd43e0569 Patch #1520294: Support for getset and member descriptors in types.py,
Barry Warsaw <barry@python.org>
parents: 39121
diff changeset
86 self.failIf(inspect.isgetsetdescriptor(type(tb.tb_frame).f_locals))
28cdd43e0569 Patch #1520294: Support for getset and member descriptors in types.py,
Barry Warsaw <barry@python.org>
parents: 39121
diff changeset
87 if hasattr(types, 'MemberDescriptorType'):
28cdd43e0569 Patch #1520294: Support for getset and member descriptors in types.py,
Barry Warsaw <barry@python.org>
parents: 39121
diff changeset
88 self.istest(inspect.ismemberdescriptor, 'datetime.timedelta.days')
28cdd43e0569 Patch #1520294: Support for getset and member descriptors in types.py,
Barry Warsaw <barry@python.org>
parents: 39121
diff changeset
89 else:
28cdd43e0569 Patch #1520294: Support for getset and member descriptors in types.py,
Barry Warsaw <barry@python.org>
parents: 39121
diff changeset
90 self.failIf(inspect.ismemberdescriptor(datetime.timedelta.days))
16790
67bad9f20b29 inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff changeset
91
34151
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
92 def test_isroutine(self):
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
93 self.assert_(inspect.isroutine(mod.spam))
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
94 self.assert_(inspect.isroutine([].count))
16790
67bad9f20b29 inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff changeset
95
50834
35bf8f7a8edc fix inspect.isclass() on instances with a custom __getattr__ #1225107
Benjamin Peterson <benjamin@python.org>
parents: 50784
diff changeset
96 def test_isclass(self):
35bf8f7a8edc fix inspect.isclass() on instances with a custom __getattr__ #1225107
Benjamin Peterson <benjamin@python.org>
parents: 50784
diff changeset
97 self.istest(inspect.isclass, 'mod.StupidGit')
35bf8f7a8edc fix inspect.isclass() on instances with a custom __getattr__ #1225107
Benjamin Peterson <benjamin@python.org>
parents: 50784
diff changeset
98 self.assertTrue(inspect.isclass(list))
35bf8f7a8edc fix inspect.isclass() on instances with a custom __getattr__ #1225107
Benjamin Peterson <benjamin@python.org>
parents: 50784
diff changeset
99
35bf8f7a8edc fix inspect.isclass() on instances with a custom __getattr__ #1225107
Benjamin Peterson <benjamin@python.org>
parents: 50784
diff changeset
100 class newstyle(object): pass
35bf8f7a8edc fix inspect.isclass() on instances with a custom __getattr__ #1225107
Benjamin Peterson <benjamin@python.org>
parents: 50784
diff changeset
101 self.assertTrue(inspect.isclass(newstyle))
35bf8f7a8edc fix inspect.isclass() on instances with a custom __getattr__ #1225107
Benjamin Peterson <benjamin@python.org>
parents: 50784
diff changeset
102
35bf8f7a8edc fix inspect.isclass() on instances with a custom __getattr__ #1225107
Benjamin Peterson <benjamin@python.org>
parents: 50784
diff changeset
103 class CustomGetattr(object):
35bf8f7a8edc fix inspect.isclass() on instances with a custom __getattr__ #1225107
Benjamin Peterson <benjamin@python.org>
parents: 50784
diff changeset
104 def __getattr__(self, attr):
35bf8f7a8edc fix inspect.isclass() on instances with a custom __getattr__ #1225107
Benjamin Peterson <benjamin@python.org>
parents: 50784
diff changeset
105 return None
35bf8f7a8edc fix inspect.isclass() on instances with a custom __getattr__ #1225107
Benjamin Peterson <benjamin@python.org>
parents: 50784
diff changeset
106 self.assertFalse(inspect.isclass(CustomGetattr()))
35bf8f7a8edc fix inspect.isclass() on instances with a custom __getattr__ #1225107
Benjamin Peterson <benjamin@python.org>
parents: 50784
diff changeset
107
50784
65c1938fb9ff #1162154: inspect.getmembers() now skips attributes that raise AttributeError,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 50162
diff changeset
108 def test_get_slot_members(self):
65c1938fb9ff #1162154: inspect.getmembers() now skips attributes that raise AttributeError,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 50162
diff changeset
109 class C(object):
65c1938fb9ff #1162154: inspect.getmembers() now skips attributes that raise AttributeError,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 50162
diff changeset
110 __slots__ = ("a", "b")
65c1938fb9ff #1162154: inspect.getmembers() now skips attributes that raise AttributeError,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 50162
diff changeset
111
65c1938fb9ff #1162154: inspect.getmembers() now skips attributes that raise AttributeError,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 50162
diff changeset
112 x = C()
65c1938fb9ff #1162154: inspect.getmembers() now skips attributes that raise AttributeError,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 50162
diff changeset
113 x.a = 42
65c1938fb9ff #1162154: inspect.getmembers() now skips attributes that raise AttributeError,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 50162
diff changeset
114 members = dict(inspect.getmembers(x))
65c1938fb9ff #1162154: inspect.getmembers() now skips attributes that raise AttributeError,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 50162
diff changeset
115 self.assert_('a' in members)
65c1938fb9ff #1162154: inspect.getmembers() now skips attributes that raise AttributeError,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 50162
diff changeset
116 self.assert_('b' not in members)
65c1938fb9ff #1162154: inspect.getmembers() now skips attributes that raise AttributeError,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 50162
diff changeset
117
65c1938fb9ff #1162154: inspect.getmembers() now skips attributes that raise AttributeError,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 50162
diff changeset
118
34151
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
119 class TestInterpreterStack(IsTestBase):
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
120 def __init__(self, *args, **kwargs):
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
121 unittest.TestCase.__init__(self, *args, **kwargs)
34358
b1a84233d855 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 34152
diff changeset
122
34151
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
123 git.abuse(7, 8, 9)
16790
67bad9f20b29 inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff changeset
124
34151
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
125 def test_abuse_done(self):
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
126 self.istest(inspect.istraceback, 'git.ex[2]')
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
127 self.istest(inspect.isframe, 'mod.fr')
16790
67bad9f20b29 inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff changeset
128
34151
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
129 def test_stack(self):
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
130 self.assert_(len(mod.st) >= 5)
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
131 self.assertEqual(mod.st[0][1:],
34358
b1a84233d855 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 34152
diff changeset
132 (modfile, 16, 'eggs', [' st = inspect.stack()\n'], 0))
34151
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
133 self.assertEqual(mod.st[1][1:],
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
134 (modfile, 9, 'spam', [' eggs(b + d, c + f)\n'], 0))
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
135 self.assertEqual(mod.st[2][1:],
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
136 (modfile, 43, 'argue', [' spam(a, b, c)\n'], 0))
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
137 self.assertEqual(mod.st[3][1:],
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
138 (modfile, 39, 'abuse', [' self.argue(a, b, c)\n'], 0))
16790
67bad9f20b29 inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff changeset
139
34151
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
140 def test_trace(self):
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
141 self.assertEqual(len(git.tr), 3)
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
142 self.assertEqual(git.tr[0][1:], (modfile, 43, 'argue',
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
143 [' spam(a, b, c)\n'], 0))
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
144 self.assertEqual(git.tr[1][1:], (modfile, 9, 'spam',
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
145 [' eggs(b + d, c + f)\n'], 0))
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
146 self.assertEqual(git.tr[2][1:], (modfile, 18, 'eggs',
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
147 [' q = y / 0\n'], 0))
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
148
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
149 def test_frame(self):
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
150 args, varargs, varkw, locals = inspect.getargvalues(mod.fr)
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
151 self.assertEqual(args, ['x', 'y'])
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
152 self.assertEqual(varargs, None)
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
153 self.assertEqual(varkw, None)
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
154 self.assertEqual(locals, {'x': 11, 'p': 11, 'y': 14})
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
155 self.assertEqual(inspect.formatargvalues(args, varargs, varkw, locals),
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
156 '(x=11, y=14)')
16790
67bad9f20b29 inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff changeset
157
34151
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
158 def test_previous_frame(self):
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
159 args, varargs, varkw, locals = inspect.getargvalues(mod.fr.f_back)
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
160 self.assertEqual(args, ['a', 'b', 'c', 'd', ['e', ['f']]])
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
161 self.assertEqual(varargs, 'g')
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
162 self.assertEqual(varkw, 'h')
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
163 self.assertEqual(inspect.formatargvalues(args, varargs, varkw, locals),
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
164 '(a=7, b=8, c=9, d=3, (e=4, (f=5,)), *g=(), **h={})')
16790
67bad9f20b29 inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff changeset
165
34151
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
166 class GetSourceBase(unittest.TestCase):
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
167 # Subclasses must override.
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
168 fodderFile = None
34358
b1a84233d855 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 34152
diff changeset
169
34151
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
170 def __init__(self, *args, **kwargs):
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
171 unittest.TestCase.__init__(self, *args, **kwargs)
33172
1f9522238c86 Patch #1006219: let inspect.getsource show '@' decorators and add tests for
Johannes Gijsbers <jlg@dds.nl>
parents: 33137
diff changeset
172
34151
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
173 self.source = file(inspect.getsourcefile(self.fodderFile)).read()
33172
1f9522238c86 Patch #1006219: let inspect.getsource show '@' decorators and add tests for
Johannes Gijsbers <jlg@dds.nl>
parents: 33137
diff changeset
174
34151
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
175 def sourcerange(self, top, bottom):
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
176 lines = self.source.split("\n")
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
177 return "\n".join(lines[top-1:bottom]) + "\n"
33172
1f9522238c86 Patch #1006219: let inspect.getsource show '@' decorators and add tests for
Johannes Gijsbers <jlg@dds.nl>
parents: 33137
diff changeset
178
34151
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
179 def assertSourceEqual(self, obj, top, bottom):
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
180 self.assertEqual(inspect.getsource(obj),
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
181 self.sourcerange(top, bottom))
34358
b1a84233d855 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 34152
diff changeset
182
34151
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
183 class TestRetrievingSourceCode(GetSourceBase):
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
184 fodderFile = mod
34358
b1a84233d855 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 34152
diff changeset
185
34151
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
186 def test_getclasses(self):
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
187 classes = inspect.getmembers(mod, inspect.isclass)
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
188 self.assertEqual(classes,
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
189 [('FesteringGob', mod.FesteringGob),
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
190 ('MalodorousPervert', mod.MalodorousPervert),
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
191 ('ParrotDroppings', mod.ParrotDroppings),
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
192 ('StupidGit', mod.StupidGit)])
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
193 tree = inspect.getclasstree([cls[1] for cls in classes], 1)
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
194 self.assertEqual(tree,
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
195 [(mod.ParrotDroppings, ()),
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
196 (mod.StupidGit, ()),
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
197 [(mod.MalodorousPervert, (mod.StupidGit,)),
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
198 [(mod.FesteringGob, (mod.MalodorousPervert,
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
199 mod.ParrotDroppings))
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
200 ]
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
201 ]
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
202 ])
34358
b1a84233d855 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 34152
diff changeset
203
34151
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
204 def test_getfunctions(self):
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
205 functions = inspect.getmembers(mod, inspect.isfunction)
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
206 self.assertEqual(functions, [('eggs', mod.eggs),
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
207 ('spam', mod.spam)])
33172
1f9522238c86 Patch #1006219: let inspect.getsource show '@' decorators and add tests for
Johannes Gijsbers <jlg@dds.nl>
parents: 33137
diff changeset
208
34151
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
209 def test_getdoc(self):
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
210 self.assertEqual(inspect.getdoc(mod), 'A module docstring.')
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
211 self.assertEqual(inspect.getdoc(mod.StupidGit),
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
212 'A longer,\n\nindented\n\ndocstring.')
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
213 self.assertEqual(inspect.getdoc(git.abuse),
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
214 'Another\n\ndocstring\n\ncontaining\n\ntabs')
20036
ab62d147ba90 Add a function to compute a class's method resolution order. This is
Tim Peters <tim.peters@gmail.com>
parents: 17175
diff changeset
215
47506
ebf6f639e117 Factor out docstring dedenting from inspect.getdoc() into inspect.cleandoc()
Georg Brandl <georg@python.org>
parents: 45640
diff changeset
216 def test_cleandoc(self):
ebf6f639e117 Factor out docstring dedenting from inspect.getdoc() into inspect.cleandoc()
Georg Brandl <georg@python.org>
parents: 45640
diff changeset
217 self.assertEqual(inspect.cleandoc('An\n indented\n docstring.'),
ebf6f639e117 Factor out docstring dedenting from inspect.getdoc() into inspect.cleandoc()
Georg Brandl <georg@python.org>
parents: 45640
diff changeset
218 'An\nindented\ndocstring.')
ebf6f639e117 Factor out docstring dedenting from inspect.getdoc() into inspect.cleandoc()
Georg Brandl <georg@python.org>
parents: 45640
diff changeset
219
34151
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
220 def test_getcomments(self):
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
221 self.assertEqual(inspect.getcomments(mod), '# line 1\n')
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
222 self.assertEqual(inspect.getcomments(mod.StupidGit), '# line 20\n')
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
223
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
224 def test_getmodule(self):
39796
38ba9524783a Fix the speed regression in inspect.py by adding another cache to speed up getmodule(). Patch #1553314
Nick Coghlan <ncoghlan@gmail.com>
parents: 39495
diff changeset
225 # Check actual module
38ba9524783a Fix the speed regression in inspect.py by adding another cache to speed up getmodule(). Patch #1553314
Nick Coghlan <ncoghlan@gmail.com>
parents: 39495
diff changeset
226 self.assertEqual(inspect.getmodule(mod), mod)
38ba9524783a Fix the speed regression in inspect.py by adding another cache to speed up getmodule(). Patch #1553314
Nick Coghlan <ncoghlan@gmail.com>
parents: 39495
diff changeset
227 # Check class (uses __module__ attribute)
34151
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
228 self.assertEqual(inspect.getmodule(mod.StupidGit), mod)
39796
38ba9524783a Fix the speed regression in inspect.py by adding another cache to speed up getmodule(). Patch #1553314
Nick Coghlan <ncoghlan@gmail.com>
parents: 39495
diff changeset
229 # Check a method (no __module__ attribute, falls back to filename)
38ba9524783a Fix the speed regression in inspect.py by adding another cache to speed up getmodule(). Patch #1553314
Nick Coghlan <ncoghlan@gmail.com>
parents: 39495
diff changeset
230 self.assertEqual(inspect.getmodule(mod.StupidGit.abuse), mod)
38ba9524783a Fix the speed regression in inspect.py by adding another cache to speed up getmodule(). Patch #1553314
Nick Coghlan <ncoghlan@gmail.com>
parents: 39495
diff changeset
231 # Do it again (check the caching isn't broken)
38ba9524783a Fix the speed regression in inspect.py by adding another cache to speed up getmodule(). Patch #1553314
Nick Coghlan <ncoghlan@gmail.com>
parents: 39495
diff changeset
232 self.assertEqual(inspect.getmodule(mod.StupidGit.abuse), mod)
38ba9524783a Fix the speed regression in inspect.py by adding another cache to speed up getmodule(). Patch #1553314
Nick Coghlan <ncoghlan@gmail.com>
parents: 39495
diff changeset
233 # Check a builtin
38ba9524783a Fix the speed regression in inspect.py by adding another cache to speed up getmodule(). Patch #1553314
Nick Coghlan <ncoghlan@gmail.com>
parents: 39495
diff changeset
234 self.assertEqual(inspect.getmodule(str), sys.modules["__builtin__"])
38ba9524783a Fix the speed regression in inspect.py by adding another cache to speed up getmodule(). Patch #1553314
Nick Coghlan <ncoghlan@gmail.com>
parents: 39495
diff changeset
235 # Check filename override
38ba9524783a Fix the speed regression in inspect.py by adding another cache to speed up getmodule(). Patch #1553314
Nick Coghlan <ncoghlan@gmail.com>
parents: 39495
diff changeset
236 self.assertEqual(inspect.getmodule(None, modfile), mod)
34151
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
237
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
238 def test_getsource(self):
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
239 self.assertSourceEqual(git.abuse, 29, 39)
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
240 self.assertSourceEqual(mod.StupidGit, 21, 46)
20036
ab62d147ba90 Add a function to compute a class's method resolution order. This is
Tim Peters <tim.peters@gmail.com>
parents: 17175
diff changeset
241
34151
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
242 def test_getsourcefile(self):
34358
b1a84233d855 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 34152
diff changeset
243 self.assertEqual(inspect.getsourcefile(mod.spam), modfile)
b1a84233d855 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 34152
diff changeset
244 self.assertEqual(inspect.getsourcefile(git.abuse), modfile)
34151
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
245
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
246 def test_getfile(self):
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
247 self.assertEqual(inspect.getfile(mod.StupidGit), mod.__file__)
20036
ab62d147ba90 Add a function to compute a class's method resolution order. This is
Tim Peters <tim.peters@gmail.com>
parents: 17175
diff changeset
248
39053
b978afe7cab6 Fix SF#1516184 and add a test to prevent regression.
Phillip J. Eby <pje@telecommunity.com>
parents: 38704
diff changeset
249 def test_getmodule_recursion(self):
44034
5d1049d4c3c6 Replaced import of the 'new' module with 'types' module and added a deprecation warning to the 'new' module.
Christian Heimes <christian@cheimes.de>
parents: 39796
diff changeset
250 from types import ModuleType
39053
b978afe7cab6 Fix SF#1516184 and add a test to prevent regression.
Phillip J. Eby <pje@telecommunity.com>
parents: 38704
diff changeset
251 name = '__inspect_dummy'
44034
5d1049d4c3c6 Replaced import of the 'new' module with 'types' module and added a deprecation warning to the 'new' module.
Christian Heimes <christian@cheimes.de>
parents: 39796
diff changeset
252 m = sys.modules[name] = ModuleType(name)
39057
c361915df291 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 39053
diff changeset
253 m.__file__ = "<string>" # hopefully not a real filename...
39053
b978afe7cab6 Fix SF#1516184 and add a test to prevent regression.
Phillip J. Eby <pje@telecommunity.com>
parents: 38704
diff changeset
254 m.__loader__ = "dummy" # pretend the filename is understood by a loader
b978afe7cab6 Fix SF#1516184 and add a test to prevent regression.
Phillip J. Eby <pje@telecommunity.com>
parents: 38704
diff changeset
255 exec "def x(): pass" in m.__dict__
b978afe7cab6 Fix SF#1516184 and add a test to prevent regression.
Phillip J. Eby <pje@telecommunity.com>
parents: 38704
diff changeset
256 self.assertEqual(inspect.getsourcefile(m.x.func_code), '<string>')
b978afe7cab6 Fix SF#1516184 and add a test to prevent regression.
Phillip J. Eby <pje@telecommunity.com>
parents: 38704
diff changeset
257 del sys.modules[name]
39121
5c903f650719 Fix SF#1516184 (again) and add a test to prevent regression.
Phillip J. Eby <pje@telecommunity.com>
parents: 39057
diff changeset
258 inspect.getmodule(compile('a=10','','single'))
39053
b978afe7cab6 Fix SF#1516184 and add a test to prevent regression.
Phillip J. Eby <pje@telecommunity.com>
parents: 38704
diff changeset
259
34151
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
260 class TestDecorators(GetSourceBase):
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
261 fodderFile = mod2
20040
59882e7aab89 New function classify_class_attrs(). As a number of SF bug reports
Tim Peters <tim.peters@gmail.com>
parents: 20036
diff changeset
262
34151
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
263 def test_wrapped_decorator(self):
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
264 self.assertSourceEqual(mod2.wrapped, 14, 17)
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
265
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
266 def test_replacing_decorator(self):
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
267 self.assertSourceEqual(mod2.gone, 9, 10)
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
268
34152
bbdaacc44f6f Patch #1011890: fix inspect.getsource breaking with line-continuation &
Johannes Gijsbers <jlg@dds.nl>
parents: 34151
diff changeset
269 class TestOneliners(GetSourceBase):
bbdaacc44f6f Patch #1011890: fix inspect.getsource breaking with line-continuation &
Johannes Gijsbers <jlg@dds.nl>
parents: 34151
diff changeset
270 fodderFile = mod2
bbdaacc44f6f Patch #1011890: fix inspect.getsource breaking with line-continuation &
Johannes Gijsbers <jlg@dds.nl>
parents: 34151
diff changeset
271 def test_oneline_lambda(self):
bbdaacc44f6f Patch #1011890: fix inspect.getsource breaking with line-continuation &
Johannes Gijsbers <jlg@dds.nl>
parents: 34151
diff changeset
272 # Test inspect.getsource with a one-line lambda function.
bbdaacc44f6f Patch #1011890: fix inspect.getsource breaking with line-continuation &
Johannes Gijsbers <jlg@dds.nl>
parents: 34151
diff changeset
273 self.assertSourceEqual(mod2.oll, 25, 25)
34358
b1a84233d855 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 34152
diff changeset
274
34152
bbdaacc44f6f Patch #1011890: fix inspect.getsource breaking with line-continuation &
Johannes Gijsbers <jlg@dds.nl>
parents: 34151
diff changeset
275 def test_threeline_lambda(self):
bbdaacc44f6f Patch #1011890: fix inspect.getsource breaking with line-continuation &
Johannes Gijsbers <jlg@dds.nl>
parents: 34151
diff changeset
276 # Test inspect.getsource with a three-line lambda function,
bbdaacc44f6f Patch #1011890: fix inspect.getsource breaking with line-continuation &
Johannes Gijsbers <jlg@dds.nl>
parents: 34151
diff changeset
277 # where the second and third lines are _not_ indented.
34358
b1a84233d855 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 34152
diff changeset
278 self.assertSourceEqual(mod2.tll, 28, 30)
b1a84233d855 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 34152
diff changeset
279
34152
bbdaacc44f6f Patch #1011890: fix inspect.getsource breaking with line-continuation &
Johannes Gijsbers <jlg@dds.nl>
parents: 34151
diff changeset
280 def test_twoline_indented_lambda(self):
bbdaacc44f6f Patch #1011890: fix inspect.getsource breaking with line-continuation &
Johannes Gijsbers <jlg@dds.nl>
parents: 34151
diff changeset
281 # Test inspect.getsource with a two-line lambda function,
bbdaacc44f6f Patch #1011890: fix inspect.getsource breaking with line-continuation &
Johannes Gijsbers <jlg@dds.nl>
parents: 34151
diff changeset
282 # where the second line _is_ indented.
bbdaacc44f6f Patch #1011890: fix inspect.getsource breaking with line-continuation &
Johannes Gijsbers <jlg@dds.nl>
parents: 34151
diff changeset
283 self.assertSourceEqual(mod2.tlli, 33, 34)
34358
b1a84233d855 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 34152
diff changeset
284
34152
bbdaacc44f6f Patch #1011890: fix inspect.getsource breaking with line-continuation &
Johannes Gijsbers <jlg@dds.nl>
parents: 34151
diff changeset
285 def test_onelinefunc(self):
bbdaacc44f6f Patch #1011890: fix inspect.getsource breaking with line-continuation &
Johannes Gijsbers <jlg@dds.nl>
parents: 34151
diff changeset
286 # Test inspect.getsource with a regular one-line function.
bbdaacc44f6f Patch #1011890: fix inspect.getsource breaking with line-continuation &
Johannes Gijsbers <jlg@dds.nl>
parents: 34151
diff changeset
287 self.assertSourceEqual(mod2.onelinefunc, 37, 37)
34358
b1a84233d855 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 34152
diff changeset
288
34152
bbdaacc44f6f Patch #1011890: fix inspect.getsource breaking with line-continuation &
Johannes Gijsbers <jlg@dds.nl>
parents: 34151
diff changeset
289 def test_manyargs(self):
bbdaacc44f6f Patch #1011890: fix inspect.getsource breaking with line-continuation &
Johannes Gijsbers <jlg@dds.nl>
parents: 34151
diff changeset
290 # Test inspect.getsource with a regular function where
bbdaacc44f6f Patch #1011890: fix inspect.getsource breaking with line-continuation &
Johannes Gijsbers <jlg@dds.nl>
parents: 34151
diff changeset
291 # the arguments are on two lines and _not_ indented and
bbdaacc44f6f Patch #1011890: fix inspect.getsource breaking with line-continuation &
Johannes Gijsbers <jlg@dds.nl>
parents: 34151
diff changeset
292 # the body on the second line with the last arguments.
bbdaacc44f6f Patch #1011890: fix inspect.getsource breaking with line-continuation &
Johannes Gijsbers <jlg@dds.nl>
parents: 34151
diff changeset
293 self.assertSourceEqual(mod2.manyargs, 40, 41)
34358
b1a84233d855 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 34152
diff changeset
294
34152
bbdaacc44f6f Patch #1011890: fix inspect.getsource breaking with line-continuation &
Johannes Gijsbers <jlg@dds.nl>
parents: 34151
diff changeset
295 def test_twolinefunc(self):
bbdaacc44f6f Patch #1011890: fix inspect.getsource breaking with line-continuation &
Johannes Gijsbers <jlg@dds.nl>
parents: 34151
diff changeset
296 # Test inspect.getsource with a regular function where
bbdaacc44f6f Patch #1011890: fix inspect.getsource breaking with line-continuation &
Johannes Gijsbers <jlg@dds.nl>
parents: 34151
diff changeset
297 # the body is on two lines, following the argument list and
bbdaacc44f6f Patch #1011890: fix inspect.getsource breaking with line-continuation &
Johannes Gijsbers <jlg@dds.nl>
parents: 34151
diff changeset
298 # continued on the next line by a \\.
bbdaacc44f6f Patch #1011890: fix inspect.getsource breaking with line-continuation &
Johannes Gijsbers <jlg@dds.nl>
parents: 34151
diff changeset
299 self.assertSourceEqual(mod2.twolinefunc, 44, 45)
34358
b1a84233d855 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 34152
diff changeset
300
34152
bbdaacc44f6f Patch #1011890: fix inspect.getsource breaking with line-continuation &
Johannes Gijsbers <jlg@dds.nl>
parents: 34151
diff changeset
301 def test_lambda_in_list(self):
bbdaacc44f6f Patch #1011890: fix inspect.getsource breaking with line-continuation &
Johannes Gijsbers <jlg@dds.nl>
parents: 34151
diff changeset
302 # Test inspect.getsource with a one-line lambda function
bbdaacc44f6f Patch #1011890: fix inspect.getsource breaking with line-continuation &
Johannes Gijsbers <jlg@dds.nl>
parents: 34151
diff changeset
303 # defined in a list, indented.
bbdaacc44f6f Patch #1011890: fix inspect.getsource breaking with line-continuation &
Johannes Gijsbers <jlg@dds.nl>
parents: 34151
diff changeset
304 self.assertSourceEqual(mod2.a[1], 49, 49)
34358
b1a84233d855 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 34152
diff changeset
305
34152
bbdaacc44f6f Patch #1011890: fix inspect.getsource breaking with line-continuation &
Johannes Gijsbers <jlg@dds.nl>
parents: 34151
diff changeset
306 def test_anonymous(self):
bbdaacc44f6f Patch #1011890: fix inspect.getsource breaking with line-continuation &
Johannes Gijsbers <jlg@dds.nl>
parents: 34151
diff changeset
307 # Test inspect.getsource with a lambda function defined
bbdaacc44f6f Patch #1011890: fix inspect.getsource breaking with line-continuation &
Johannes Gijsbers <jlg@dds.nl>
parents: 34151
diff changeset
308 # as argument to another function.
bbdaacc44f6f Patch #1011890: fix inspect.getsource breaking with line-continuation &
Johannes Gijsbers <jlg@dds.nl>
parents: 34151
diff changeset
309 self.assertSourceEqual(mod2.anonymous, 55, 55)
bbdaacc44f6f Patch #1011890: fix inspect.getsource breaking with line-continuation &
Johannes Gijsbers <jlg@dds.nl>
parents: 34151
diff changeset
310
34740
3bb759498767 Patch #1159931/bug #1143895: inspect.getsource failed when functions,
Johannes Gijsbers <jlg@dds.nl>
parents: 34358
diff changeset
311 class TestBuggyCases(GetSourceBase):
3bb759498767 Patch #1159931/bug #1143895: inspect.getsource failed when functions,
Johannes Gijsbers <jlg@dds.nl>
parents: 34358
diff changeset
312 fodderFile = mod2
3bb759498767 Patch #1159931/bug #1143895: inspect.getsource failed when functions,
Johannes Gijsbers <jlg@dds.nl>
parents: 34358
diff changeset
313
3bb759498767 Patch #1159931/bug #1143895: inspect.getsource failed when functions,
Johannes Gijsbers <jlg@dds.nl>
parents: 34358
diff changeset
314 def test_with_comment(self):
3bb759498767 Patch #1159931/bug #1143895: inspect.getsource failed when functions,
Johannes Gijsbers <jlg@dds.nl>
parents: 34358
diff changeset
315 self.assertSourceEqual(mod2.with_comment, 58, 59)
3bb759498767 Patch #1159931/bug #1143895: inspect.getsource failed when functions,
Johannes Gijsbers <jlg@dds.nl>
parents: 34358
diff changeset
316
3bb759498767 Patch #1159931/bug #1143895: inspect.getsource failed when functions,
Johannes Gijsbers <jlg@dds.nl>
parents: 34358
diff changeset
317 def test_multiline_sig(self):
3bb759498767 Patch #1159931/bug #1143895: inspect.getsource failed when functions,
Johannes Gijsbers <jlg@dds.nl>
parents: 34358
diff changeset
318 self.assertSourceEqual(mod2.multiline_sig[0], 63, 64)
3bb759498767 Patch #1159931/bug #1143895: inspect.getsource failed when functions,
Johannes Gijsbers <jlg@dds.nl>
parents: 34358
diff changeset
319
35640
1e7b755194a4 some more fixes and tests for inspect.getsource(), triggered by crashes
Armin Rigo <arigo@tunes.org>
parents: 34740
diff changeset
320 def test_nested_class(self):
1e7b755194a4 some more fixes and tests for inspect.getsource(), triggered by crashes
Armin Rigo <arigo@tunes.org>
parents: 34740
diff changeset
321 self.assertSourceEqual(mod2.func69().func71, 71, 72)
1e7b755194a4 some more fixes and tests for inspect.getsource(), triggered by crashes
Armin Rigo <arigo@tunes.org>
parents: 34740
diff changeset
322
1e7b755194a4 some more fixes and tests for inspect.getsource(), triggered by crashes
Armin Rigo <arigo@tunes.org>
parents: 34740
diff changeset
323 def test_one_liner_followed_by_non_name(self):
1e7b755194a4 some more fixes and tests for inspect.getsource(), triggered by crashes
Armin Rigo <arigo@tunes.org>
parents: 34740
diff changeset
324 self.assertSourceEqual(mod2.func77, 77, 77)
1e7b755194a4 some more fixes and tests for inspect.getsource(), triggered by crashes
Armin Rigo <arigo@tunes.org>
parents: 34740
diff changeset
325
1e7b755194a4 some more fixes and tests for inspect.getsource(), triggered by crashes
Armin Rigo <arigo@tunes.org>
parents: 34740
diff changeset
326 def test_one_liner_dedent_non_name(self):
1e7b755194a4 some more fixes and tests for inspect.getsource(), triggered by crashes
Armin Rigo <arigo@tunes.org>
parents: 34740
diff changeset
327 self.assertSourceEqual(mod2.cls82.func83, 83, 83)
1e7b755194a4 some more fixes and tests for inspect.getsource(), triggered by crashes
Armin Rigo <arigo@tunes.org>
parents: 34740
diff changeset
328
1e7b755194a4 some more fixes and tests for inspect.getsource(), triggered by crashes
Armin Rigo <arigo@tunes.org>
parents: 34740
diff changeset
329 def test_with_comment_instead_of_docstring(self):
1e7b755194a4 some more fixes and tests for inspect.getsource(), triggered by crashes
Armin Rigo <arigo@tunes.org>
parents: 34740
diff changeset
330 self.assertSourceEqual(mod2.func88, 88, 90)
1e7b755194a4 some more fixes and tests for inspect.getsource(), triggered by crashes
Armin Rigo <arigo@tunes.org>
parents: 34740
diff changeset
331
39495
14ec9cfd9760 Make tabnanny recognize IndentationErrors raised by tokenize.
Georg Brandl <georg@python.org>
parents: 39238
diff changeset
332 def test_method_in_dynamic_class(self):
14ec9cfd9760 Make tabnanny recognize IndentationErrors raised by tokenize.
Georg Brandl <georg@python.org>
parents: 39238
diff changeset
333 self.assertSourceEqual(mod2.method_in_dynamic_class, 95, 97)
14ec9cfd9760 Make tabnanny recognize IndentationErrors raised by tokenize.
Georg Brandl <georg@python.org>
parents: 39238
diff changeset
334
54065
e962129665d8 Fix test failure on Windows, and add skip check if even unicodedata
R. David Murray <rdmurray@bitdance.com>
parents: 54041
diff changeset
335 @unittest.skipIf(
e962129665d8 Fix test failure on Windows, and add skip check if even unicodedata
R. David Murray <rdmurray@bitdance.com>
parents: 54041
diff changeset
336 not hasattr(unicodedata, '__file__') or
e962129665d8 Fix test failure on Windows, and add skip check if even unicodedata
R. David Murray <rdmurray@bitdance.com>
parents: 54041
diff changeset
337 unicodedata.__file__[-4:] in (".pyc", ".pyo"),
e962129665d8 Fix test failure on Windows, and add skip check if even unicodedata
R. David Murray <rdmurray@bitdance.com>
parents: 54041
diff changeset
338 "unicodedata is not an external binary module")
54041
347e59ea1a71 Issue #4050: inspect.findsource/getsource now raise an IOError if the 'source'
R. David Murray <rdmurray@bitdance.com>
parents: 50834
diff changeset
339 def test_findsource_binary(self):
54065
e962129665d8 Fix test failure on Windows, and add skip check if even unicodedata
R. David Murray <rdmurray@bitdance.com>
parents: 54041
diff changeset
340 self.assertRaises(IOError, inspect.getsource, unicodedata)
e962129665d8 Fix test failure on Windows, and add skip check if even unicodedata
R. David Murray <rdmurray@bitdance.com>
parents: 54041
diff changeset
341 self.assertRaises(IOError, inspect.findsource, unicodedata)
54041
347e59ea1a71 Issue #4050: inspect.findsource/getsource now raise an IOError if the 'source'
R. David Murray <rdmurray@bitdance.com>
parents: 50834
diff changeset
342
34151
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
343 # Helper for testing classify_class_attrs.
20040
59882e7aab89 New function classify_class_attrs(). As a number of SF bug reports
Tim Peters <tim.peters@gmail.com>
parents: 20036
diff changeset
344 def attrs_wo_objs(cls):
59882e7aab89 New function classify_class_attrs(). As a number of SF bug reports
Tim Peters <tim.peters@gmail.com>
parents: 20036
diff changeset
345 return [t[:3] for t in inspect.classify_class_attrs(cls)]
59882e7aab89 New function classify_class_attrs(). As a number of SF bug reports
Tim Peters <tim.peters@gmail.com>
parents: 20036
diff changeset
346
34358
b1a84233d855 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 34152
diff changeset
347 class TestClassesAndFunctions(unittest.TestCase):
34151
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
348 def test_classic_mro(self):
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
349 # Test classic-class method resolution order.
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
350 class A: pass
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
351 class B(A): pass
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
352 class C(A): pass
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
353 class D(B, C): pass
20040
59882e7aab89 New function classify_class_attrs(). As a number of SF bug reports
Tim Peters <tim.peters@gmail.com>
parents: 20036
diff changeset
354
34151
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
355 expected = (D, B, A, C)
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
356 got = inspect.getmro(D)
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
357 self.assertEqual(expected, got)
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
358
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
359 def test_newstyle_mro(self):
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
360 # The same w/ new-class MRO.
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
361 class A(object): pass
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
362 class B(A): pass
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
363 class C(A): pass
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
364 class D(B, C): pass
20040
59882e7aab89 New function classify_class_attrs(). As a number of SF bug reports
Tim Peters <tim.peters@gmail.com>
parents: 20036
diff changeset
365
34151
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
366 expected = (D, B, C, A, object)
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
367 got = inspect.getmro(D)
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
368 self.assertEqual(expected, got)
20040
59882e7aab89 New function classify_class_attrs(). As a number of SF bug reports
Tim Peters <tim.peters@gmail.com>
parents: 20036
diff changeset
369
34151
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
370 def assertArgSpecEquals(self, routine, args_e, varargs_e = None,
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
371 varkw_e = None, defaults_e = None,
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
372 formatted = None):
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
373 args, varargs, varkw, defaults = inspect.getargspec(routine)
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
374 self.assertEqual(args, args_e)
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
375 self.assertEqual(varargs, varargs_e)
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
376 self.assertEqual(varkw, varkw_e)
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
377 self.assertEqual(defaults, defaults_e)
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
378 if formatted is not None:
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
379 self.assertEqual(inspect.formatargspec(args, varargs, varkw, defaults),
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
380 formatted)
20040
59882e7aab89 New function classify_class_attrs(). As a number of SF bug reports
Tim Peters <tim.peters@gmail.com>
parents: 20036
diff changeset
381
34151
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
382 def test_getargspec(self):
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
383 self.assertArgSpecEquals(mod.eggs, ['x', 'y'], formatted = '(x, y)')
20040
59882e7aab89 New function classify_class_attrs(). As a number of SF bug reports
Tim Peters <tim.peters@gmail.com>
parents: 20036
diff changeset
384
34151
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
385 self.assertArgSpecEquals(mod.spam,
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
386 ['a', 'b', 'c', 'd', ['e', ['f']]],
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
387 'g', 'h', (3, (4, (5,))),
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
388 '(a, b, c, d=3, (e, (f,))=(4, (5,)), *g, **h)')
20040
59882e7aab89 New function classify_class_attrs(). As a number of SF bug reports
Tim Peters <tim.peters@gmail.com>
parents: 20036
diff changeset
389
34151
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
390 def test_getargspec_method(self):
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
391 class A(object):
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
392 def m(self):
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
393 pass
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
394 self.assertArgSpecEquals(A.m, ['self'])
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
395
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
396 def test_getargspec_sublistofone(self):
37235
9d8b33aff60c Fix SF bug #1458903 with AST compiler.
Neal Norwitz <nnorwitz@gmail.com>
parents: 35640
diff changeset
397 def sublistOfOne((foo,)): return 1
9d8b33aff60c Fix SF bug #1458903 with AST compiler.
Neal Norwitz <nnorwitz@gmail.com>
parents: 35640
diff changeset
398 self.assertArgSpecEquals(sublistOfOne, [['foo']])
34358
b1a84233d855 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 34152
diff changeset
399
37235
9d8b33aff60c Fix SF bug #1458903 with AST compiler.
Neal Norwitz <nnorwitz@gmail.com>
parents: 35640
diff changeset
400 def fakeSublistOfOne((foo)): return 1
9d8b33aff60c Fix SF bug #1458903 with AST compiler.
Neal Norwitz <nnorwitz@gmail.com>
parents: 35640
diff changeset
401 self.assertArgSpecEquals(fakeSublistOfOne, ['foo'])
34151
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
402
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
403 def test_classify_oldstyle(self):
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
404 class A:
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
405 def s(): pass
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
406 s = staticmethod(s)
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
407
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
408 def c(cls): pass
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
409 c = classmethod(c)
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
410
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
411 def getp(self): pass
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
412 p = property(getp)
20040
59882e7aab89 New function classify_class_attrs(). As a number of SF bug reports
Tim Peters <tim.peters@gmail.com>
parents: 20036
diff changeset
413
34151
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
414 def m(self): pass
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
415
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
416 def m1(self): pass
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
417
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
418 datablob = '1'
20040
59882e7aab89 New function classify_class_attrs(). As a number of SF bug reports
Tim Peters <tim.peters@gmail.com>
parents: 20036
diff changeset
419
34151
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
420 attrs = attrs_wo_objs(A)
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
421 self.assert_(('s', 'static method', A) in attrs, 'missing static method')
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
422 self.assert_(('c', 'class method', A) in attrs, 'missing class method')
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
423 self.assert_(('p', 'property', A) in attrs, 'missing property')
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
424 self.assert_(('m', 'method', A) in attrs, 'missing plain method')
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
425 self.assert_(('m1', 'method', A) in attrs, 'missing plain method')
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
426 self.assert_(('datablob', 'data', A) in attrs, 'missing data')
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
427
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
428 class B(A):
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
429 def m(self): pass
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
430
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
431 attrs = attrs_wo_objs(B)
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
432 self.assert_(('s', 'static method', A) in attrs, 'missing static method')
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
433 self.assert_(('c', 'class method', A) in attrs, 'missing class method')
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
434 self.assert_(('p', 'property', A) in attrs, 'missing property')
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
435 self.assert_(('m', 'method', B) in attrs, 'missing plain method')
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
436 self.assert_(('m1', 'method', A) in attrs, 'missing plain method')
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
437 self.assert_(('datablob', 'data', A) in attrs, 'missing data')
20040
59882e7aab89 New function classify_class_attrs(). As a number of SF bug reports
Tim Peters <tim.peters@gmail.com>
parents: 20036
diff changeset
438
59882e7aab89 New function classify_class_attrs(). As a number of SF bug reports
Tim Peters <tim.peters@gmail.com>
parents: 20036
diff changeset
439
34151
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
440 class C(A):
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
441 def m(self): pass
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
442 def c(self): pass
20040
59882e7aab89 New function classify_class_attrs(). As a number of SF bug reports
Tim Peters <tim.peters@gmail.com>
parents: 20036
diff changeset
443
34151
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
444 attrs = attrs_wo_objs(C)
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
445 self.assert_(('s', 'static method', A) in attrs, 'missing static method')
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
446 self.assert_(('c', 'method', C) in attrs, 'missing plain method')
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
447 self.assert_(('p', 'property', A) in attrs, 'missing property')
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
448 self.assert_(('m', 'method', C) in attrs, 'missing plain method')
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
449 self.assert_(('m1', 'method', A) in attrs, 'missing plain method')
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
450 self.assert_(('datablob', 'data', A) in attrs, 'missing data')
20040
59882e7aab89 New function classify_class_attrs(). As a number of SF bug reports
Tim Peters <tim.peters@gmail.com>
parents: 20036
diff changeset
451
34151
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
452 class D(B, C):
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
453 def m1(self): pass
20040
59882e7aab89 New function classify_class_attrs(). As a number of SF bug reports
Tim Peters <tim.peters@gmail.com>
parents: 20036
diff changeset
454
34151
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
455 attrs = attrs_wo_objs(D)
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
456 self.assert_(('s', 'static method', A) in attrs, 'missing static method')
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
457 self.assert_(('c', 'class method', A) in attrs, 'missing class method')
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
458 self.assert_(('p', 'property', A) in attrs, 'missing property')
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
459 self.assert_(('m', 'method', B) in attrs, 'missing plain method')
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
460 self.assert_(('m1', 'method', D) in attrs, 'missing plain method')
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
461 self.assert_(('datablob', 'data', A) in attrs, 'missing data')
20040
59882e7aab89 New function classify_class_attrs(). As a number of SF bug reports
Tim Peters <tim.peters@gmail.com>
parents: 20036
diff changeset
462
34151
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
463 # Repeat all that, but w/ new-style classes.
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
464 def test_classify_newstyle(self):
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
465 class A(object):
20040
59882e7aab89 New function classify_class_attrs(). As a number of SF bug reports
Tim Peters <tim.peters@gmail.com>
parents: 20036
diff changeset
466
34151
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
467 def s(): pass
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
468 s = staticmethod(s)
20040
59882e7aab89 New function classify_class_attrs(). As a number of SF bug reports
Tim Peters <tim.peters@gmail.com>
parents: 20036
diff changeset
469
34151
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
470 def c(cls): pass
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
471 c = classmethod(c)
20040
59882e7aab89 New function classify_class_attrs(). As a number of SF bug reports
Tim Peters <tim.peters@gmail.com>
parents: 20036
diff changeset
472
34151
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
473 def getp(self): pass
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
474 p = property(getp)
20040
59882e7aab89 New function classify_class_attrs(). As a number of SF bug reports
Tim Peters <tim.peters@gmail.com>
parents: 20036
diff changeset
475
34151
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
476 def m(self): pass
20040
59882e7aab89 New function classify_class_attrs(). As a number of SF bug reports
Tim Peters <tim.peters@gmail.com>
parents: 20036
diff changeset
477
34151
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
478 def m1(self): pass
20040
59882e7aab89 New function classify_class_attrs(). As a number of SF bug reports
Tim Peters <tim.peters@gmail.com>
parents: 20036
diff changeset
479
34151
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
480 datablob = '1'
20040
59882e7aab89 New function classify_class_attrs(). As a number of SF bug reports
Tim Peters <tim.peters@gmail.com>
parents: 20036
diff changeset
481
34151
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
482 attrs = attrs_wo_objs(A)
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
483 self.assert_(('s', 'static method', A) in attrs, 'missing static method')
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
484 self.assert_(('c', 'class method', A) in attrs, 'missing class method')
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
485 self.assert_(('p', 'property', A) in attrs, 'missing property')
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
486 self.assert_(('m', 'method', A) in attrs, 'missing plain method')
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
487 self.assert_(('m1', 'method', A) in attrs, 'missing plain method')
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
488 self.assert_(('datablob', 'data', A) in attrs, 'missing data')
20040
59882e7aab89 New function classify_class_attrs(). As a number of SF bug reports
Tim Peters <tim.peters@gmail.com>
parents: 20036
diff changeset
489
34151
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
490 class B(A):
20040
59882e7aab89 New function classify_class_attrs(). As a number of SF bug reports
Tim Peters <tim.peters@gmail.com>
parents: 20036
diff changeset
491
34151
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
492 def m(self): pass
20040
59882e7aab89 New function classify_class_attrs(). As a number of SF bug reports
Tim Peters <tim.peters@gmail.com>
parents: 20036
diff changeset
493
34151
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
494 attrs = attrs_wo_objs(B)
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
495 self.assert_(('s', 'static method', A) in attrs, 'missing static method')
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
496 self.assert_(('c', 'class method', A) in attrs, 'missing class method')
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
497 self.assert_(('p', 'property', A) in attrs, 'missing property')
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
498 self.assert_(('m', 'method', B) in attrs, 'missing plain method')
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
499 self.assert_(('m1', 'method', A) in attrs, 'missing plain method')
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
500 self.assert_(('datablob', 'data', A) in attrs, 'missing data')
20040
59882e7aab89 New function classify_class_attrs(). As a number of SF bug reports
Tim Peters <tim.peters@gmail.com>
parents: 20036
diff changeset
501
59882e7aab89 New function classify_class_attrs(). As a number of SF bug reports
Tim Peters <tim.peters@gmail.com>
parents: 20036
diff changeset
502
34151
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
503 class C(A):
20040
59882e7aab89 New function classify_class_attrs(). As a number of SF bug reports
Tim Peters <tim.peters@gmail.com>
parents: 20036
diff changeset
504
34151
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
505 def m(self): pass
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
506 def c(self): pass
20040
59882e7aab89 New function classify_class_attrs(). As a number of SF bug reports
Tim Peters <tim.peters@gmail.com>
parents: 20036
diff changeset
507
34151
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
508 attrs = attrs_wo_objs(C)
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
509 self.assert_(('s', 'static method', A) in attrs, 'missing static method')
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
510 self.assert_(('c', 'method', C) in attrs, 'missing plain method')
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
511 self.assert_(('p', 'property', A) in attrs, 'missing property')
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
512 self.assert_(('m', 'method', C) in attrs, 'missing plain method')
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
513 self.assert_(('m1', 'method', A) in attrs, 'missing plain method')
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
514 self.assert_(('datablob', 'data', A) in attrs, 'missing data')
20040
59882e7aab89 New function classify_class_attrs(). As a number of SF bug reports
Tim Peters <tim.peters@gmail.com>
parents: 20036
diff changeset
515
34151
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
516 class D(B, C):
29618
58251e0a8b78 Add a trivial test of getargspec() with a method.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 28951
diff changeset
517
34151
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
518 def m1(self): pass
30826
fac8c63c7838 Patch #830858: Correct the number of is-functions. Backported to 2.3 and 2.2.
Martin v. Löwis <martin@v.loewis.de>
parents: 29618
diff changeset
519
34151
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
520 attrs = attrs_wo_objs(D)
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
521 self.assert_(('s', 'static method', A) in attrs, 'missing static method')
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
522 self.assert_(('c', 'method', C) in attrs, 'missing plain method')
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
523 self.assert_(('p', 'property', A) in attrs, 'missing property')
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
524 self.assert_(('m', 'method', B) in attrs, 'missing plain method')
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
525 self.assert_(('m1', 'method', D) in attrs, 'missing plain method')
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
526 self.assert_(('datablob', 'data', A) in attrs, 'missing data')
33137
ad268a8408f4 - Bug #891637, patch #1005466: fix inspect.getargs() crash on def foo((bar)).
Matthias Klose <doko@ubuntu.com>
parents: 32253
diff changeset
527
34151
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
528 def test_main():
34152
bbdaacc44f6f Patch #1011890: fix inspect.getsource breaking with line-continuation &
Johannes Gijsbers <jlg@dds.nl>
parents: 34151
diff changeset
529 run_unittest(TestDecorators, TestRetrievingSourceCode, TestOneliners,
34740
3bb759498767 Patch #1159931/bug #1143895: inspect.getsource failed when functions,
Johannes Gijsbers <jlg@dds.nl>
parents: 34358
diff changeset
530 TestBuggyCases,
34151
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
531 TestInterpreterStack, TestClassesAndFunctions, TestPredicates)
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
532
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
533 if __name__ == "__main__":
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
534 test_main()