annotate Lib/test/test_inspect.py @ 79675:c0a2294de787 2.7

Issue #16199: Backport 2to3 typo fix from 3.2.
author Chris Jerdonek <chris.jerdonek@gmail.com>
date Thu, 11 Oct 2012 16:15:46 -0700
parents f824744557ba
children bc73223e4c10
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
60080
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
1 import re
34151
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
2 import sys
39226
28cdd43e0569 Patch #1520294: Support for getset and member descriptors in types.py,
Barry Warsaw <barry@python.org>
parents: 39121
diff changeset
3 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
4 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
5 import inspect
62369
017e60ae605f #8720: fix inspect regression by teaching getsourcefile about linecache.
R. David Murray <rdmurray@bitdance.com>
parents: 60483
diff changeset
6 import linecache
39226
28cdd43e0569 Patch #1520294: Support for getset and member descriptors in types.py,
Barry Warsaw <barry@python.org>
parents: 39121
diff changeset
7 import datetime
60080
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
8 from UserList import UserList
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
9 from UserDict import UserDict
16790
67bad9f20b29 inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff changeset
10
59783
c8c840162804 #7092 - Silence more py3k deprecation warnings, using test_support.check_py3k_warnings() helper.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59045
diff changeset
11 from test.test_support import run_unittest, check_py3k_warnings
16790
67bad9f20b29 inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff changeset
12
59783
c8c840162804 #7092 - Silence more py3k deprecation warnings, using test_support.check_py3k_warnings() helper.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59045
diff changeset
13 with check_py3k_warnings(
63572
51102abdec50 Merged revisions 79165 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 63556
diff changeset
14 ("tuple parameter unpacking has been removed", SyntaxWarning),
51102abdec50 Merged revisions 79165 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 63556
diff changeset
15 quiet=True):
51102abdec50 Merged revisions 79165 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 63556
diff changeset
16 from test import inspect_fodder as mod
51102abdec50 Merged revisions 79165 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 63556
diff changeset
17 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
18
54041
347e59ea1a71 Issue #4050: inspect.findsource/getsource now raise an IOError if the 'source'
R. David Murray <rdmurray@bitdance.com>
parents: 50834
diff changeset
19 # 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
20 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
21
16790
67bad9f20b29 inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff changeset
22 # 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
23 # 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
24 # isbuiltin, isroutine, isgenerator, isgeneratorfunction, getmembers,
1bc923a107d9 Issue #1916. Added isgenerator() and isgeneratorfunction() to
Facundo Batista <facundobatista@gmail.com>
parents: 44034
diff changeset
25 # getdoc, getfile, getmodule, getsourcefile, getcomments, getsource,
1bc923a107d9 Issue #1916. Added isgenerator() and isgeneratorfunction() to
Facundo Batista <facundobatista@gmail.com>
parents: 44034
diff changeset
26 # getclasstree, getargspec, getargvalues, formatargspec, formatargvalues,
1bc923a107d9 Issue #1916. Added isgenerator() and isgeneratorfunction() to
Facundo Batista <facundobatista@gmail.com>
parents: 44034
diff changeset
27 # 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
28
50162
4264cf00a75c Merged revisions 67750-67751 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 47506
diff changeset
29 # 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
30 # 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
31
34151
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
32 modfile = mod.__file__
38704
ba6eebf7c719 Make use of new str.startswith/endswith semantics.
Georg Brandl <georg@python.org>
parents: 37235
diff changeset
33 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
34 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
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 import __builtin__
16790
67bad9f20b29 inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff changeset
37
67bad9f20b29 inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff changeset
38 try:
63572
51102abdec50 Merged revisions 79165 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 63556
diff changeset
39 1 // 0
16790
67bad9f20b29 inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff changeset
40 except:
67bad9f20b29 inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff changeset
41 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
42
34151
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
43 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
44
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
45 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
46 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
47 inspect.isframe, inspect.isfunction, inspect.ismethod,
45386
1bc923a107d9 Issue #1916. Added isgenerator() and isgeneratorfunction() to
Facundo Batista <facundobatista@gmail.com>
parents: 44034
diff changeset
48 inspect.ismodule, inspect.istraceback,
1bc923a107d9 Issue #1916. Added isgenerator() and isgeneratorfunction() to
Facundo Batista <facundobatista@gmail.com>
parents: 44034
diff changeset
49 inspect.isgenerator, inspect.isgeneratorfunction])
34358
b1a84233d855 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 34152
diff changeset
50
34151
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
51 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
52 obj = eval(exp)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 54389
diff changeset
53 self.assertTrue(predicate(obj), '%s(%s)' % (predicate.__name__, exp))
34358
b1a84233d855 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 34152
diff changeset
54
34151
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
55 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
56 if predicate == inspect.isgeneratorfunction and\
1bc923a107d9 Issue #1916. Added isgenerator() and isgeneratorfunction() to
Facundo Batista <facundobatista@gmail.com>
parents: 44034
diff changeset
57 other == inspect.isfunction:
1bc923a107d9 Issue #1916. Added isgenerator() and isgeneratorfunction() to
Facundo Batista <facundobatista@gmail.com>
parents: 44034
diff changeset
58 continue
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 54389
diff changeset
59 self.assertFalse(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
60
45386
1bc923a107d9 Issue #1916. Added isgenerator() and isgeneratorfunction() to
Facundo Batista <facundobatista@gmail.com>
parents: 44034
diff changeset
61 def generator_function_example(self):
1bc923a107d9 Issue #1916. Added isgenerator() and isgeneratorfunction() to
Facundo Batista <facundobatista@gmail.com>
parents: 44034
diff changeset
62 for i in xrange(2):
1bc923a107d9 Issue #1916. Added isgenerator() and isgeneratorfunction() to
Facundo Batista <facundobatista@gmail.com>
parents: 44034
diff changeset
63 yield i
1bc923a107d9 Issue #1916. Added isgenerator() and isgeneratorfunction() to
Facundo Batista <facundobatista@gmail.com>
parents: 44034
diff changeset
64
34151
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
65 class TestPredicates(IsTestBase):
45637
65ef608dd66c 15 -> 16
Christian Heimes <christian@cheimes.de>
parents: 45386
diff changeset
66 def test_sixteen(self):
39238
4a6ade9ce780 Ensure the actual number matches the expected count
Neal Norwitz <nnorwitz@gmail.com>
parents: 39226
diff changeset
67 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
68 # 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
69 # which claims there are 16 such functions
45637
65ef608dd66c 15 -> 16
Christian Heimes <christian@cheimes.de>
parents: 45386
diff changeset
70 expected = 16
39238
4a6ade9ce780 Ensure the actual number matches the expected count
Neal Norwitz <nnorwitz@gmail.com>
parents: 39226
diff changeset
71 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
72 self.assertEqual(count, expected, err_msg)
34358
b1a84233d855 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 34152
diff changeset
73
45386
1bc923a107d9 Issue #1916. Added isgenerator() and isgeneratorfunction() to
Facundo Batista <facundobatista@gmail.com>
parents: 44034
diff changeset
74
34151
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
75 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
76 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
77 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
78 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
79 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
80 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
81 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
82 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
83 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
84 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
85 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
86 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
87 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
88 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
89 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
90 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
91 '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
92 else:
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 54389
diff changeset
93 self.assertFalse(inspect.isgetsetdescriptor(type(tb.tb_frame).f_locals))
39226
28cdd43e0569 Patch #1520294: Support for getset and member descriptors in types.py,
Barry Warsaw <barry@python.org>
parents: 39121
diff changeset
94 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
95 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
96 else:
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 54389
diff changeset
97 self.assertFalse(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
98
34151
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
99 def test_isroutine(self):
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 54389
diff changeset
100 self.assertTrue(inspect.isroutine(mod.spam))
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 54389
diff changeset
101 self.assertTrue(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
102
50834
35bf8f7a8edc fix inspect.isclass() on instances with a custom __getattr__ #1225107
Benjamin Peterson <benjamin@python.org>
parents: 50784
diff changeset
103 def test_isclass(self):
35bf8f7a8edc fix inspect.isclass() on instances with a custom __getattr__ #1225107
Benjamin Peterson <benjamin@python.org>
parents: 50784
diff changeset
104 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
105 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
106
35bf8f7a8edc fix inspect.isclass() on instances with a custom __getattr__ #1225107
Benjamin Peterson <benjamin@python.org>
parents: 50784
diff changeset
107 class newstyle(object): pass
35bf8f7a8edc fix inspect.isclass() on instances with a custom __getattr__ #1225107
Benjamin Peterson <benjamin@python.org>
parents: 50784
diff changeset
108 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
109
35bf8f7a8edc fix inspect.isclass() on instances with a custom __getattr__ #1225107
Benjamin Peterson <benjamin@python.org>
parents: 50784
diff changeset
110 class CustomGetattr(object):
35bf8f7a8edc fix inspect.isclass() on instances with a custom __getattr__ #1225107
Benjamin Peterson <benjamin@python.org>
parents: 50784
diff changeset
111 def __getattr__(self, attr):
35bf8f7a8edc fix inspect.isclass() on instances with a custom __getattr__ #1225107
Benjamin Peterson <benjamin@python.org>
parents: 50784
diff changeset
112 return None
35bf8f7a8edc fix inspect.isclass() on instances with a custom __getattr__ #1225107
Benjamin Peterson <benjamin@python.org>
parents: 50784
diff changeset
113 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
114
50784
65c1938fb9ff #1162154: inspect.getmembers() now skips attributes that raise AttributeError,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 50162
diff changeset
115 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
116 class C(object):
65c1938fb9ff #1162154: inspect.getmembers() now skips attributes that raise AttributeError,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 50162
diff changeset
117 __slots__ = ("a", "b")
65c1938fb9ff #1162154: inspect.getmembers() now skips attributes that raise AttributeError,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 50162
diff changeset
118
65c1938fb9ff #1162154: inspect.getmembers() now skips attributes that raise AttributeError,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 50162
diff changeset
119 x = C()
65c1938fb9ff #1162154: inspect.getmembers() now skips attributes that raise AttributeError,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 50162
diff changeset
120 x.a = 42
65c1938fb9ff #1162154: inspect.getmembers() now skips attributes that raise AttributeError,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 50162
diff changeset
121 members = dict(inspect.getmembers(x))
58460
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58198
diff changeset
122 self.assertIn('a', members)
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58198
diff changeset
123 self.assertNotIn('b', members)
50784
65c1938fb9ff #1162154: inspect.getmembers() now skips attributes that raise AttributeError,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 50162
diff changeset
124
56538
983eefbf6649 make inspect.isabstract() always return a boolean; add a test for it, too #7069
Benjamin Peterson <benjamin@python.org>
parents: 55002
diff changeset
125 def test_isabstract(self):
983eefbf6649 make inspect.isabstract() always return a boolean; add a test for it, too #7069
Benjamin Peterson <benjamin@python.org>
parents: 55002
diff changeset
126 from abc import ABCMeta, abstractmethod
983eefbf6649 make inspect.isabstract() always return a boolean; add a test for it, too #7069
Benjamin Peterson <benjamin@python.org>
parents: 55002
diff changeset
127
983eefbf6649 make inspect.isabstract() always return a boolean; add a test for it, too #7069
Benjamin Peterson <benjamin@python.org>
parents: 55002
diff changeset
128 class AbstractClassExample(object):
983eefbf6649 make inspect.isabstract() always return a boolean; add a test for it, too #7069
Benjamin Peterson <benjamin@python.org>
parents: 55002
diff changeset
129 __metaclass__ = ABCMeta
983eefbf6649 make inspect.isabstract() always return a boolean; add a test for it, too #7069
Benjamin Peterson <benjamin@python.org>
parents: 55002
diff changeset
130
983eefbf6649 make inspect.isabstract() always return a boolean; add a test for it, too #7069
Benjamin Peterson <benjamin@python.org>
parents: 55002
diff changeset
131 @abstractmethod
983eefbf6649 make inspect.isabstract() always return a boolean; add a test for it, too #7069
Benjamin Peterson <benjamin@python.org>
parents: 55002
diff changeset
132 def foo(self):
983eefbf6649 make inspect.isabstract() always return a boolean; add a test for it, too #7069
Benjamin Peterson <benjamin@python.org>
parents: 55002
diff changeset
133 pass
983eefbf6649 make inspect.isabstract() always return a boolean; add a test for it, too #7069
Benjamin Peterson <benjamin@python.org>
parents: 55002
diff changeset
134
983eefbf6649 make inspect.isabstract() always return a boolean; add a test for it, too #7069
Benjamin Peterson <benjamin@python.org>
parents: 55002
diff changeset
135 class ClassExample(AbstractClassExample):
983eefbf6649 make inspect.isabstract() always return a boolean; add a test for it, too #7069
Benjamin Peterson <benjamin@python.org>
parents: 55002
diff changeset
136 def foo(self):
983eefbf6649 make inspect.isabstract() always return a boolean; add a test for it, too #7069
Benjamin Peterson <benjamin@python.org>
parents: 55002
diff changeset
137 pass
983eefbf6649 make inspect.isabstract() always return a boolean; add a test for it, too #7069
Benjamin Peterson <benjamin@python.org>
parents: 55002
diff changeset
138
983eefbf6649 make inspect.isabstract() always return a boolean; add a test for it, too #7069
Benjamin Peterson <benjamin@python.org>
parents: 55002
diff changeset
139 a = ClassExample()
983eefbf6649 make inspect.isabstract() always return a boolean; add a test for it, too #7069
Benjamin Peterson <benjamin@python.org>
parents: 55002
diff changeset
140
983eefbf6649 make inspect.isabstract() always return a boolean; add a test for it, too #7069
Benjamin Peterson <benjamin@python.org>
parents: 55002
diff changeset
141 # Test general behaviour.
983eefbf6649 make inspect.isabstract() always return a boolean; add a test for it, too #7069
Benjamin Peterson <benjamin@python.org>
parents: 55002
diff changeset
142 self.assertTrue(inspect.isabstract(AbstractClassExample))
983eefbf6649 make inspect.isabstract() always return a boolean; add a test for it, too #7069
Benjamin Peterson <benjamin@python.org>
parents: 55002
diff changeset
143 self.assertFalse(inspect.isabstract(ClassExample))
983eefbf6649 make inspect.isabstract() always return a boolean; add a test for it, too #7069
Benjamin Peterson <benjamin@python.org>
parents: 55002
diff changeset
144 self.assertFalse(inspect.isabstract(a))
983eefbf6649 make inspect.isabstract() always return a boolean; add a test for it, too #7069
Benjamin Peterson <benjamin@python.org>
parents: 55002
diff changeset
145 self.assertFalse(inspect.isabstract(int))
983eefbf6649 make inspect.isabstract() always return a boolean; add a test for it, too #7069
Benjamin Peterson <benjamin@python.org>
parents: 55002
diff changeset
146 self.assertFalse(inspect.isabstract(5))
983eefbf6649 make inspect.isabstract() always return a boolean; add a test for it, too #7069
Benjamin Peterson <benjamin@python.org>
parents: 55002
diff changeset
147
16790
67bad9f20b29 inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff changeset
148
34151
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
149 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
150 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
151 unittest.TestCase.__init__(self, *args, **kwargs)
34358
b1a84233d855 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 34152
diff changeset
152
34151
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
153 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
154
34151
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
155 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
156 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
157 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
158
34151
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
159 def test_stack(self):
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 54389
diff changeset
160 self.assertTrue(len(mod.st) >= 5)
34151
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(mod.st[0][1:],
34358
b1a84233d855 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 34152
diff changeset
162 (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
163 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
164 (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
165 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
166 (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
167 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
168 (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
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 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
171 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
172 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
173 [' 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
174 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
175 [' 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
176 self.assertEqual(git.tr[2][1:], (modfile, 18, 'eggs',
63556
dba99828d91c Merged revisions 77942,79023 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 50162
diff changeset
177 [' q = y // 0\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
178
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 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
180 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
181 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
182 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
183 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
184 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
185 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
186 '(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
187
34151
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
188 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
189 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
190 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
191 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
192 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
193 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
194 '(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
195
34151
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
196 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
197 # 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
198 fodderFile = None
34358
b1a84233d855 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 34152
diff changeset
199
34151
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
200 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
201 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
202
54389
70fb848870ac explicitly close files
Philip Jenvey <pjenvey@underboss.org>
parents: 54065
diff changeset
203 with open(inspect.getsourcefile(self.fodderFile)) as fp:
70fb848870ac explicitly close files
Philip Jenvey <pjenvey@underboss.org>
parents: 54065
diff changeset
204 self.source = fp.read()
33172
1f9522238c86 Patch #1006219: let inspect.getsource show '@' decorators and add tests for
Johannes Gijsbers <jlg@dds.nl>
parents: 33137
diff changeset
205
34151
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
206 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
207 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
208 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
209
34151
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
210 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
211 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
212 self.sourcerange(top, bottom))
34358
b1a84233d855 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 34152
diff changeset
213
34151
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
214 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
215 fodderFile = mod
34358
b1a84233d855 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 34152
diff changeset
216
34151
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
217 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
218 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
219 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
220 [('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
221 ('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
222 ('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
223 ('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
224 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
225 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
226 [(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
227 (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
228 [(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
229 [(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
230 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
231 ]
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
232 ]
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
233 ])
34358
b1a84233d855 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 34152
diff changeset
234
34151
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
235 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
236 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
237 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
238 ('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
239
59045
a4c45f8e70aa Issue 6292: for the moment at least, the test suite passes if run
R. David Murray <rdmurray@bitdance.com>
parents: 58817
diff changeset
240 @unittest.skipIf(sys.flags.optimize >= 2,
a4c45f8e70aa Issue 6292: for the moment at least, the test suite passes if run
R. David Murray <rdmurray@bitdance.com>
parents: 58817
diff changeset
241 "Docstrings are omitted with -O2 and above")
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_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
243 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
244 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
245 '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
246 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
247 '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
248
47506
ebf6f639e117 Factor out docstring dedenting from inspect.getdoc() into inspect.cleandoc()
Georg Brandl <georg@python.org>
parents: 45640
diff changeset
249 def test_cleandoc(self):
ebf6f639e117 Factor out docstring dedenting from inspect.getdoc() into inspect.cleandoc()
Georg Brandl <georg@python.org>
parents: 45640
diff changeset
250 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
251 'An\nindented\ndocstring.')
ebf6f639e117 Factor out docstring dedenting from inspect.getdoc() into inspect.cleandoc()
Georg Brandl <georg@python.org>
parents: 45640
diff changeset
252
34151
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
253 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
254 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
255 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
256
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
257 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
258 # 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
259 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
260 # 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
261 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
262 # 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
263 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
264 # 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
265 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
266 # 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
267 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
268 # 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
269 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
270
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
271 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
272 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
273 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
274
34151
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
275 def test_getsourcefile(self):
34358
b1a84233d855 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 34152
diff changeset
276 self.assertEqual(inspect.getsourcefile(mod.spam), modfile)
b1a84233d855 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 34152
diff changeset
277 self.assertEqual(inspect.getsourcefile(git.abuse), modfile)
62369
017e60ae605f #8720: fix inspect regression by teaching getsourcefile about linecache.
R. David Murray <rdmurray@bitdance.com>
parents: 60483
diff changeset
278 fn = "_non_existing_filename_used_for_sourcefile_test.py"
017e60ae605f #8720: fix inspect regression by teaching getsourcefile about linecache.
R. David Murray <rdmurray@bitdance.com>
parents: 60483
diff changeset
279 co = compile("None", fn, "exec")
017e60ae605f #8720: fix inspect regression by teaching getsourcefile about linecache.
R. David Murray <rdmurray@bitdance.com>
parents: 60483
diff changeset
280 self.assertEqual(inspect.getsourcefile(co), None)
017e60ae605f #8720: fix inspect regression by teaching getsourcefile about linecache.
R. David Murray <rdmurray@bitdance.com>
parents: 60483
diff changeset
281 linecache.cache[co.co_filename] = (1, None, "None", co.co_filename)
017e60ae605f #8720: fix inspect regression by teaching getsourcefile about linecache.
R. David Murray <rdmurray@bitdance.com>
parents: 60483
diff changeset
282 self.assertEqual(inspect.getsourcefile(co), fn)
34151
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
283
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
284 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
285 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
286
39053
b978afe7cab6 Fix SF#1516184 and add a test to prevent regression.
Phillip J. Eby <pje@telecommunity.com>
parents: 38704
diff changeset
287 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
288 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
289 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
290 m = sys.modules[name] = ModuleType(name)
39057
c361915df291 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 39053
diff changeset
291 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
292 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
293 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
294 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
295 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
296 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
297
70798
527c40add91d allow "fake" filenames in findsource (closes #9284)
Benjamin Peterson <benjamin@python.org>
parents: 69032
diff changeset
298 def test_proceed_with_fake_filename(self):
527c40add91d allow "fake" filenames in findsource (closes #9284)
Benjamin Peterson <benjamin@python.org>
parents: 69032
diff changeset
299 '''doctest monkeypatches linecache to enable inspection'''
527c40add91d allow "fake" filenames in findsource (closes #9284)
Benjamin Peterson <benjamin@python.org>
parents: 69032
diff changeset
300 fn, source = '<test>', 'def x(): pass\n'
527c40add91d allow "fake" filenames in findsource (closes #9284)
Benjamin Peterson <benjamin@python.org>
parents: 69032
diff changeset
301 getlines = linecache.getlines
527c40add91d allow "fake" filenames in findsource (closes #9284)
Benjamin Peterson <benjamin@python.org>
parents: 69032
diff changeset
302 def monkey(filename, module_globals=None):
527c40add91d allow "fake" filenames in findsource (closes #9284)
Benjamin Peterson <benjamin@python.org>
parents: 69032
diff changeset
303 if filename == fn:
527c40add91d allow "fake" filenames in findsource (closes #9284)
Benjamin Peterson <benjamin@python.org>
parents: 69032
diff changeset
304 return source.splitlines(True)
527c40add91d allow "fake" filenames in findsource (closes #9284)
Benjamin Peterson <benjamin@python.org>
parents: 69032
diff changeset
305 else:
527c40add91d allow "fake" filenames in findsource (closes #9284)
Benjamin Peterson <benjamin@python.org>
parents: 69032
diff changeset
306 return getlines(filename, module_globals)
527c40add91d allow "fake" filenames in findsource (closes #9284)
Benjamin Peterson <benjamin@python.org>
parents: 69032
diff changeset
307 linecache.getlines = monkey
527c40add91d allow "fake" filenames in findsource (closes #9284)
Benjamin Peterson <benjamin@python.org>
parents: 69032
diff changeset
308 try:
527c40add91d allow "fake" filenames in findsource (closes #9284)
Benjamin Peterson <benjamin@python.org>
parents: 69032
diff changeset
309 ns = {}
527c40add91d allow "fake" filenames in findsource (closes #9284)
Benjamin Peterson <benjamin@python.org>
parents: 69032
diff changeset
310 exec compile(source, fn, 'single') in ns
527c40add91d allow "fake" filenames in findsource (closes #9284)
Benjamin Peterson <benjamin@python.org>
parents: 69032
diff changeset
311 inspect.getsource(ns["x"])
527c40add91d allow "fake" filenames in findsource (closes #9284)
Benjamin Peterson <benjamin@python.org>
parents: 69032
diff changeset
312 finally:
527c40add91d allow "fake" filenames in findsource (closes #9284)
Benjamin Peterson <benjamin@python.org>
parents: 69032
diff changeset
313 linecache.getlines = getlines
527c40add91d allow "fake" filenames in findsource (closes #9284)
Benjamin Peterson <benjamin@python.org>
parents: 69032
diff changeset
314
34151
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
315 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
316 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
317
34151
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
318 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
319 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
320
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
321 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
322 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
323
34152
bbdaacc44f6f Patch #1011890: fix inspect.getsource breaking with line-continuation &
Johannes Gijsbers <jlg@dds.nl>
parents: 34151
diff changeset
324 class TestOneliners(GetSourceBase):
bbdaacc44f6f Patch #1011890: fix inspect.getsource breaking with line-continuation &
Johannes Gijsbers <jlg@dds.nl>
parents: 34151
diff changeset
325 fodderFile = mod2
bbdaacc44f6f Patch #1011890: fix inspect.getsource breaking with line-continuation &
Johannes Gijsbers <jlg@dds.nl>
parents: 34151
diff changeset
326 def test_oneline_lambda(self):
bbdaacc44f6f Patch #1011890: fix inspect.getsource breaking with line-continuation &
Johannes Gijsbers <jlg@dds.nl>
parents: 34151
diff changeset
327 # 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
328 self.assertSourceEqual(mod2.oll, 25, 25)
34358
b1a84233d855 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 34152
diff changeset
329
34152
bbdaacc44f6f Patch #1011890: fix inspect.getsource breaking with line-continuation &
Johannes Gijsbers <jlg@dds.nl>
parents: 34151
diff changeset
330 def test_threeline_lambda(self):
bbdaacc44f6f Patch #1011890: fix inspect.getsource breaking with line-continuation &
Johannes Gijsbers <jlg@dds.nl>
parents: 34151
diff changeset
331 # 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
332 # where the second and third lines are _not_ indented.
34358
b1a84233d855 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 34152
diff changeset
333 self.assertSourceEqual(mod2.tll, 28, 30)
b1a84233d855 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 34152
diff changeset
334
34152
bbdaacc44f6f Patch #1011890: fix inspect.getsource breaking with line-continuation &
Johannes Gijsbers <jlg@dds.nl>
parents: 34151
diff changeset
335 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
336 # 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
337 # 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
338 self.assertSourceEqual(mod2.tlli, 33, 34)
34358
b1a84233d855 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 34152
diff changeset
339
34152
bbdaacc44f6f Patch #1011890: fix inspect.getsource breaking with line-continuation &
Johannes Gijsbers <jlg@dds.nl>
parents: 34151
diff changeset
340 def test_onelinefunc(self):
bbdaacc44f6f Patch #1011890: fix inspect.getsource breaking with line-continuation &
Johannes Gijsbers <jlg@dds.nl>
parents: 34151
diff changeset
341 # 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
342 self.assertSourceEqual(mod2.onelinefunc, 37, 37)
34358
b1a84233d855 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 34152
diff changeset
343
34152
bbdaacc44f6f Patch #1011890: fix inspect.getsource breaking with line-continuation &
Johannes Gijsbers <jlg@dds.nl>
parents: 34151
diff changeset
344 def test_manyargs(self):
bbdaacc44f6f Patch #1011890: fix inspect.getsource breaking with line-continuation &
Johannes Gijsbers <jlg@dds.nl>
parents: 34151
diff changeset
345 # 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
346 # 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
347 # 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
348 self.assertSourceEqual(mod2.manyargs, 40, 41)
34358
b1a84233d855 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 34152
diff changeset
349
34152
bbdaacc44f6f Patch #1011890: fix inspect.getsource breaking with line-continuation &
Johannes Gijsbers <jlg@dds.nl>
parents: 34151
diff changeset
350 def test_twolinefunc(self):
bbdaacc44f6f Patch #1011890: fix inspect.getsource breaking with line-continuation &
Johannes Gijsbers <jlg@dds.nl>
parents: 34151
diff changeset
351 # 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
352 # 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
353 # 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
354 self.assertSourceEqual(mod2.twolinefunc, 44, 45)
34358
b1a84233d855 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 34152
diff changeset
355
34152
bbdaacc44f6f Patch #1011890: fix inspect.getsource breaking with line-continuation &
Johannes Gijsbers <jlg@dds.nl>
parents: 34151
diff changeset
356 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
357 # 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
358 # defined in a list, indented.
bbdaacc44f6f Patch #1011890: fix inspect.getsource breaking with line-continuation &
Johannes Gijsbers <jlg@dds.nl>
parents: 34151
diff changeset
359 self.assertSourceEqual(mod2.a[1], 49, 49)
34358
b1a84233d855 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 34152
diff changeset
360
34152
bbdaacc44f6f Patch #1011890: fix inspect.getsource breaking with line-continuation &
Johannes Gijsbers <jlg@dds.nl>
parents: 34151
diff changeset
361 def test_anonymous(self):
bbdaacc44f6f Patch #1011890: fix inspect.getsource breaking with line-continuation &
Johannes Gijsbers <jlg@dds.nl>
parents: 34151
diff changeset
362 # 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
363 # as argument to another function.
bbdaacc44f6f Patch #1011890: fix inspect.getsource breaking with line-continuation &
Johannes Gijsbers <jlg@dds.nl>
parents: 34151
diff changeset
364 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
365
34740
3bb759498767 Patch #1159931/bug #1143895: inspect.getsource failed when functions,
Johannes Gijsbers <jlg@dds.nl>
parents: 34358
diff changeset
366 class TestBuggyCases(GetSourceBase):
3bb759498767 Patch #1159931/bug #1143895: inspect.getsource failed when functions,
Johannes Gijsbers <jlg@dds.nl>
parents: 34358
diff changeset
367 fodderFile = mod2
3bb759498767 Patch #1159931/bug #1143895: inspect.getsource failed when functions,
Johannes Gijsbers <jlg@dds.nl>
parents: 34358
diff changeset
368
3bb759498767 Patch #1159931/bug #1143895: inspect.getsource failed when functions,
Johannes Gijsbers <jlg@dds.nl>
parents: 34358
diff changeset
369 def test_with_comment(self):
3bb759498767 Patch #1159931/bug #1143895: inspect.getsource failed when functions,
Johannes Gijsbers <jlg@dds.nl>
parents: 34358
diff changeset
370 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
371
3bb759498767 Patch #1159931/bug #1143895: inspect.getsource failed when functions,
Johannes Gijsbers <jlg@dds.nl>
parents: 34358
diff changeset
372 def test_multiline_sig(self):
3bb759498767 Patch #1159931/bug #1143895: inspect.getsource failed when functions,
Johannes Gijsbers <jlg@dds.nl>
parents: 34358
diff changeset
373 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
374
35640
1e7b755194a4 some more fixes and tests for inspect.getsource(), triggered by crashes
Armin Rigo <arigo@tunes.org>
parents: 34740
diff changeset
375 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
376 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
377
1e7b755194a4 some more fixes and tests for inspect.getsource(), triggered by crashes
Armin Rigo <arigo@tunes.org>
parents: 34740
diff changeset
378 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
379 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
380
1e7b755194a4 some more fixes and tests for inspect.getsource(), triggered by crashes
Armin Rigo <arigo@tunes.org>
parents: 34740
diff changeset
381 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
382 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
383
1e7b755194a4 some more fixes and tests for inspect.getsource(), triggered by crashes
Armin Rigo <arigo@tunes.org>
parents: 34740
diff changeset
384 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
385 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
386
39495
14ec9cfd9760 Make tabnanny recognize IndentationErrors raised by tokenize.
Georg Brandl <georg@python.org>
parents: 39238
diff changeset
387 def test_method_in_dynamic_class(self):
14ec9cfd9760 Make tabnanny recognize IndentationErrors raised by tokenize.
Georg Brandl <georg@python.org>
parents: 39238
diff changeset
388 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
389
54065
e962129665d8 Fix test failure on Windows, and add skip check if even unicodedata
R. David Murray <rdmurray@bitdance.com>
parents: 54041
diff changeset
390 @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
391 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
392 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
393 "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
394 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
395 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
396 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
397
62369
017e60ae605f #8720: fix inspect regression by teaching getsourcefile about linecache.
R. David Murray <rdmurray@bitdance.com>
parents: 60483
diff changeset
398 def test_findsource_code_in_linecache(self):
017e60ae605f #8720: fix inspect regression by teaching getsourcefile about linecache.
R. David Murray <rdmurray@bitdance.com>
parents: 60483
diff changeset
399 lines = ["x=1"]
017e60ae605f #8720: fix inspect regression by teaching getsourcefile about linecache.
R. David Murray <rdmurray@bitdance.com>
parents: 60483
diff changeset
400 co = compile(lines[0], "_dynamically_created_file", "exec")
017e60ae605f #8720: fix inspect regression by teaching getsourcefile about linecache.
R. David Murray <rdmurray@bitdance.com>
parents: 60483
diff changeset
401 self.assertRaises(IOError, inspect.findsource, co)
017e60ae605f #8720: fix inspect regression by teaching getsourcefile about linecache.
R. David Murray <rdmurray@bitdance.com>
parents: 60483
diff changeset
402 self.assertRaises(IOError, inspect.getsource, co)
017e60ae605f #8720: fix inspect regression by teaching getsourcefile about linecache.
R. David Murray <rdmurray@bitdance.com>
parents: 60483
diff changeset
403 linecache.cache[co.co_filename] = (1, None, lines, co.co_filename)
66348
614c84670c63 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62369
diff changeset
404 self.assertEqual(inspect.findsource(co), (lines,0))
614c84670c63 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62369
diff changeset
405 self.assertEqual(inspect.getsource(co), lines[0])
62369
017e60ae605f #8720: fix inspect regression by teaching getsourcefile about linecache.
R. David Murray <rdmurray@bitdance.com>
parents: 60483
diff changeset
406
74106
13f56cd8dec1 Issue #1785: Fix inspect and pydoc with misbehaving descriptors.
Antoine Pitrou <solipsis@pitrou.net>
parents: 70798
diff changeset
407
13f56cd8dec1 Issue #1785: Fix inspect and pydoc with misbehaving descriptors.
Antoine Pitrou <solipsis@pitrou.net>
parents: 70798
diff changeset
408 class _BrokenDataDescriptor(object):
13f56cd8dec1 Issue #1785: Fix inspect and pydoc with misbehaving descriptors.
Antoine Pitrou <solipsis@pitrou.net>
parents: 70798
diff changeset
409 """
13f56cd8dec1 Issue #1785: Fix inspect and pydoc with misbehaving descriptors.
Antoine Pitrou <solipsis@pitrou.net>
parents: 70798
diff changeset
410 A broken data descriptor. See bug #1785.
13f56cd8dec1 Issue #1785: Fix inspect and pydoc with misbehaving descriptors.
Antoine Pitrou <solipsis@pitrou.net>
parents: 70798
diff changeset
411 """
13f56cd8dec1 Issue #1785: Fix inspect and pydoc with misbehaving descriptors.
Antoine Pitrou <solipsis@pitrou.net>
parents: 70798
diff changeset
412 def __get__(*args):
13f56cd8dec1 Issue #1785: Fix inspect and pydoc with misbehaving descriptors.
Antoine Pitrou <solipsis@pitrou.net>
parents: 70798
diff changeset
413 raise AssertionError("should not __get__ data descriptors")
13f56cd8dec1 Issue #1785: Fix inspect and pydoc with misbehaving descriptors.
Antoine Pitrou <solipsis@pitrou.net>
parents: 70798
diff changeset
414
13f56cd8dec1 Issue #1785: Fix inspect and pydoc with misbehaving descriptors.
Antoine Pitrou <solipsis@pitrou.net>
parents: 70798
diff changeset
415 def __set__(*args):
13f56cd8dec1 Issue #1785: Fix inspect and pydoc with misbehaving descriptors.
Antoine Pitrou <solipsis@pitrou.net>
parents: 70798
diff changeset
416 raise RuntimeError
13f56cd8dec1 Issue #1785: Fix inspect and pydoc with misbehaving descriptors.
Antoine Pitrou <solipsis@pitrou.net>
parents: 70798
diff changeset
417
13f56cd8dec1 Issue #1785: Fix inspect and pydoc with misbehaving descriptors.
Antoine Pitrou <solipsis@pitrou.net>
parents: 70798
diff changeset
418 def __getattr__(*args):
13f56cd8dec1 Issue #1785: Fix inspect and pydoc with misbehaving descriptors.
Antoine Pitrou <solipsis@pitrou.net>
parents: 70798
diff changeset
419 raise AssertionError("should not __getattr__ data descriptors")
13f56cd8dec1 Issue #1785: Fix inspect and pydoc with misbehaving descriptors.
Antoine Pitrou <solipsis@pitrou.net>
parents: 70798
diff changeset
420
13f56cd8dec1 Issue #1785: Fix inspect and pydoc with misbehaving descriptors.
Antoine Pitrou <solipsis@pitrou.net>
parents: 70798
diff changeset
421
13f56cd8dec1 Issue #1785: Fix inspect and pydoc with misbehaving descriptors.
Antoine Pitrou <solipsis@pitrou.net>
parents: 70798
diff changeset
422 class _BrokenMethodDescriptor(object):
13f56cd8dec1 Issue #1785: Fix inspect and pydoc with misbehaving descriptors.
Antoine Pitrou <solipsis@pitrou.net>
parents: 70798
diff changeset
423 """
13f56cd8dec1 Issue #1785: Fix inspect and pydoc with misbehaving descriptors.
Antoine Pitrou <solipsis@pitrou.net>
parents: 70798
diff changeset
424 A broken method descriptor. See bug #1785.
13f56cd8dec1 Issue #1785: Fix inspect and pydoc with misbehaving descriptors.
Antoine Pitrou <solipsis@pitrou.net>
parents: 70798
diff changeset
425 """
13f56cd8dec1 Issue #1785: Fix inspect and pydoc with misbehaving descriptors.
Antoine Pitrou <solipsis@pitrou.net>
parents: 70798
diff changeset
426 def __get__(*args):
13f56cd8dec1 Issue #1785: Fix inspect and pydoc with misbehaving descriptors.
Antoine Pitrou <solipsis@pitrou.net>
parents: 70798
diff changeset
427 raise AssertionError("should not __get__ method descriptors")
13f56cd8dec1 Issue #1785: Fix inspect and pydoc with misbehaving descriptors.
Antoine Pitrou <solipsis@pitrou.net>
parents: 70798
diff changeset
428
13f56cd8dec1 Issue #1785: Fix inspect and pydoc with misbehaving descriptors.
Antoine Pitrou <solipsis@pitrou.net>
parents: 70798
diff changeset
429 def __getattr__(*args):
13f56cd8dec1 Issue #1785: Fix inspect and pydoc with misbehaving descriptors.
Antoine Pitrou <solipsis@pitrou.net>
parents: 70798
diff changeset
430 raise AssertionError("should not __getattr__ method descriptors")
13f56cd8dec1 Issue #1785: Fix inspect and pydoc with misbehaving descriptors.
Antoine Pitrou <solipsis@pitrou.net>
parents: 70798
diff changeset
431
13f56cd8dec1 Issue #1785: Fix inspect and pydoc with misbehaving descriptors.
Antoine Pitrou <solipsis@pitrou.net>
parents: 70798
diff changeset
432
34151
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
433 # 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
434 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
435 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
436
74106
13f56cd8dec1 Issue #1785: Fix inspect and pydoc with misbehaving descriptors.
Antoine Pitrou <solipsis@pitrou.net>
parents: 70798
diff changeset
437
34358
b1a84233d855 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 34152
diff changeset
438 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
439 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
440 # 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
441 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
442 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
443 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
444 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
445
34151
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
446 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
447 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
448 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
449
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
450 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
451 # 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
452 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
453 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
454 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
455 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
456
34151
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
457 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
458 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
459 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
460
34151
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
461 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
462 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
463 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
464 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
465 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
466 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
467 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
468 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
469 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
470 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
471 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
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 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
474 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
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 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
477 ['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
478 '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
479 '(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
480
34151
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
481 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
482 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
483 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
484 pass
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.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
486
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
487 def test_getargspec_sublistofone(self):
59783
c8c840162804 #7092 - Silence more py3k deprecation warnings, using test_support.check_py3k_warnings() helper.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59045
diff changeset
488 with check_py3k_warnings(
63572
51102abdec50 Merged revisions 79165 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 63556
diff changeset
489 ("tuple parameter unpacking has been removed", SyntaxWarning),
51102abdec50 Merged revisions 79165 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 63556
diff changeset
490 ("parenthesized argument names are invalid", SyntaxWarning)):
51102abdec50 Merged revisions 79165 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 63556
diff changeset
491 exec 'def sublistOfOne((foo,)): return 1'
51102abdec50 Merged revisions 79165 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 63556
diff changeset
492 self.assertArgSpecEquals(sublistOfOne, [['foo']])
34358
b1a84233d855 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 34152
diff changeset
493
63572
51102abdec50 Merged revisions 79165 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 63556
diff changeset
494 exec 'def fakeSublistOfOne((foo)): return 1'
51102abdec50 Merged revisions 79165 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 63556
diff changeset
495 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
496
60483
97b588ee62f0 Refactor a couple inspect module tests to remove duplicate code
Jean-Paul Calderone <exarkun@divmod.com>
parents: 60105
diff changeset
497
97b588ee62f0 Refactor a couple inspect module tests to remove duplicate code
Jean-Paul Calderone <exarkun@divmod.com>
parents: 60105
diff changeset
498 def _classify_test(self, newstyle):
97b588ee62f0 Refactor a couple inspect module tests to remove duplicate code
Jean-Paul Calderone <exarkun@divmod.com>
parents: 60105
diff changeset
499 """Helper for testing that classify_class_attrs finds a bunch of
97b588ee62f0 Refactor a couple inspect module tests to remove duplicate code
Jean-Paul Calderone <exarkun@divmod.com>
parents: 60105
diff changeset
500 different kinds of attributes on a given class.
97b588ee62f0 Refactor a couple inspect module tests to remove duplicate code
Jean-Paul Calderone <exarkun@divmod.com>
parents: 60105
diff changeset
501 """
97b588ee62f0 Refactor a couple inspect module tests to remove duplicate code
Jean-Paul Calderone <exarkun@divmod.com>
parents: 60105
diff changeset
502 if newstyle:
97b588ee62f0 Refactor a couple inspect module tests to remove duplicate code
Jean-Paul Calderone <exarkun@divmod.com>
parents: 60105
diff changeset
503 base = object
97b588ee62f0 Refactor a couple inspect module tests to remove duplicate code
Jean-Paul Calderone <exarkun@divmod.com>
parents: 60105
diff changeset
504 else:
97b588ee62f0 Refactor a couple inspect module tests to remove duplicate code
Jean-Paul Calderone <exarkun@divmod.com>
parents: 60105
diff changeset
505 class base:
97b588ee62f0 Refactor a couple inspect module tests to remove duplicate code
Jean-Paul Calderone <exarkun@divmod.com>
parents: 60105
diff changeset
506 pass
97b588ee62f0 Refactor a couple inspect module tests to remove duplicate code
Jean-Paul Calderone <exarkun@divmod.com>
parents: 60105
diff changeset
507
97b588ee62f0 Refactor a couple inspect module tests to remove duplicate code
Jean-Paul Calderone <exarkun@divmod.com>
parents: 60105
diff changeset
508 class A(base):
34151
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
509 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
510 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
511
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
512 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
513 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
514
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
515 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
516 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
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 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
519
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
520 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
521
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
522 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
523
74106
13f56cd8dec1 Issue #1785: Fix inspect and pydoc with misbehaving descriptors.
Antoine Pitrou <solipsis@pitrou.net>
parents: 70798
diff changeset
524 dd = _BrokenDataDescriptor()
13f56cd8dec1 Issue #1785: Fix inspect and pydoc with misbehaving descriptors.
Antoine Pitrou <solipsis@pitrou.net>
parents: 70798
diff changeset
525 md = _BrokenMethodDescriptor()
13f56cd8dec1 Issue #1785: Fix inspect and pydoc with misbehaving descriptors.
Antoine Pitrou <solipsis@pitrou.net>
parents: 70798
diff changeset
526
34151
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
527 attrs = attrs_wo_objs(A)
58460
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58198
diff changeset
528 self.assertIn(('s', 'static method', A), attrs, 'missing static method')
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58198
diff changeset
529 self.assertIn(('c', 'class method', A), attrs, 'missing class method')
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58198
diff changeset
530 self.assertIn(('p', 'property', A), attrs, 'missing property')
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58198
diff changeset
531 self.assertIn(('m', 'method', A), attrs, 'missing plain method')
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58198
diff changeset
532 self.assertIn(('m1', 'method', A), attrs, 'missing plain method')
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58198
diff changeset
533 self.assertIn(('datablob', 'data', A), attrs, 'missing data')
74106
13f56cd8dec1 Issue #1785: Fix inspect and pydoc with misbehaving descriptors.
Antoine Pitrou <solipsis@pitrou.net>
parents: 70798
diff changeset
534 self.assertIn(('md', 'method', A), attrs, 'missing method descriptor')
13f56cd8dec1 Issue #1785: Fix inspect and pydoc with misbehaving descriptors.
Antoine Pitrou <solipsis@pitrou.net>
parents: 70798
diff changeset
535 self.assertIn(('dd', 'data', A), attrs, 'missing data descriptor')
34151
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
536
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
537 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
538 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
539
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
540 attrs = attrs_wo_objs(B)
58460
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58198
diff changeset
541 self.assertIn(('s', 'static method', A), attrs, 'missing static method')
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58198
diff changeset
542 self.assertIn(('c', 'class method', A), attrs, 'missing class method')
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58198
diff changeset
543 self.assertIn(('p', 'property', A), attrs, 'missing property')
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58198
diff changeset
544 self.assertIn(('m', 'method', B), attrs, 'missing plain method')
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58198
diff changeset
545 self.assertIn(('m1', 'method', A), attrs, 'missing plain method')
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58198
diff changeset
546 self.assertIn(('datablob', 'data', A), attrs, 'missing data')
74106
13f56cd8dec1 Issue #1785: Fix inspect and pydoc with misbehaving descriptors.
Antoine Pitrou <solipsis@pitrou.net>
parents: 70798
diff changeset
547 self.assertIn(('md', 'method', A), attrs, 'missing method descriptor')
13f56cd8dec1 Issue #1785: Fix inspect and pydoc with misbehaving descriptors.
Antoine Pitrou <solipsis@pitrou.net>
parents: 70798
diff changeset
548 self.assertIn(('dd', 'data', A), attrs, 'missing data descriptor')
20040
59882e7aab89 New function classify_class_attrs(). As a number of SF bug reports
Tim Peters <tim.peters@gmail.com>
parents: 20036
diff changeset
549
59882e7aab89 New function classify_class_attrs(). As a number of SF bug reports
Tim Peters <tim.peters@gmail.com>
parents: 20036
diff changeset
550
34151
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
551 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
552 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
553 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
554
34151
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
555 attrs = attrs_wo_objs(C)
58460
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58198
diff changeset
556 self.assertIn(('s', 'static method', A), attrs, 'missing static method')
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58198
diff changeset
557 self.assertIn(('c', 'method', C), attrs, 'missing plain method')
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58198
diff changeset
558 self.assertIn(('p', 'property', A), attrs, 'missing property')
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58198
diff changeset
559 self.assertIn(('m', 'method', C), attrs, 'missing plain method')
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58198
diff changeset
560 self.assertIn(('m1', 'method', A), attrs, 'missing plain method')
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58198
diff changeset
561 self.assertIn(('datablob', 'data', A), attrs, 'missing data')
74106
13f56cd8dec1 Issue #1785: Fix inspect and pydoc with misbehaving descriptors.
Antoine Pitrou <solipsis@pitrou.net>
parents: 70798
diff changeset
562 self.assertIn(('md', 'method', A), attrs, 'missing method descriptor')
13f56cd8dec1 Issue #1785: Fix inspect and pydoc with misbehaving descriptors.
Antoine Pitrou <solipsis@pitrou.net>
parents: 70798
diff changeset
563 self.assertIn(('dd', 'data', A), attrs, 'missing data descriptor')
20040
59882e7aab89 New function classify_class_attrs(). As a number of SF bug reports
Tim Peters <tim.peters@gmail.com>
parents: 20036
diff changeset
564
34151
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
565 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
566 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
567
34151
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
568 attrs = attrs_wo_objs(D)
58460
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58198
diff changeset
569 self.assertIn(('s', 'static method', A), attrs, 'missing static method')
60483
97b588ee62f0 Refactor a couple inspect module tests to remove duplicate code
Jean-Paul Calderone <exarkun@divmod.com>
parents: 60105
diff changeset
570 if newstyle:
97b588ee62f0 Refactor a couple inspect module tests to remove duplicate code
Jean-Paul Calderone <exarkun@divmod.com>
parents: 60105
diff changeset
571 self.assertIn(('c', 'method', C), attrs, 'missing plain method')
97b588ee62f0 Refactor a couple inspect module tests to remove duplicate code
Jean-Paul Calderone <exarkun@divmod.com>
parents: 60105
diff changeset
572 else:
97b588ee62f0 Refactor a couple inspect module tests to remove duplicate code
Jean-Paul Calderone <exarkun@divmod.com>
parents: 60105
diff changeset
573 self.assertIn(('c', 'class method', A), attrs, 'missing class method')
58460
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58198
diff changeset
574 self.assertIn(('p', 'property', A), attrs, 'missing property')
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58198
diff changeset
575 self.assertIn(('m', 'method', B), attrs, 'missing plain method')
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58198
diff changeset
576 self.assertIn(('m1', 'method', D), attrs, 'missing plain method')
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58198
diff changeset
577 self.assertIn(('datablob', 'data', A), attrs, 'missing data')
74106
13f56cd8dec1 Issue #1785: Fix inspect and pydoc with misbehaving descriptors.
Antoine Pitrou <solipsis@pitrou.net>
parents: 70798
diff changeset
578 self.assertIn(('md', 'method', A), attrs, 'missing method descriptor')
13f56cd8dec1 Issue #1785: Fix inspect and pydoc with misbehaving descriptors.
Antoine Pitrou <solipsis@pitrou.net>
parents: 70798
diff changeset
579 self.assertIn(('dd', 'data', A), attrs, 'missing data descriptor')
20040
59882e7aab89 New function classify_class_attrs(). As a number of SF bug reports
Tim Peters <tim.peters@gmail.com>
parents: 20036
diff changeset
580
59882e7aab89 New function classify_class_attrs(). As a number of SF bug reports
Tim Peters <tim.peters@gmail.com>
parents: 20036
diff changeset
581
60483
97b588ee62f0 Refactor a couple inspect module tests to remove duplicate code
Jean-Paul Calderone <exarkun@divmod.com>
parents: 60105
diff changeset
582 def test_classify_oldstyle(self):
97b588ee62f0 Refactor a couple inspect module tests to remove duplicate code
Jean-Paul Calderone <exarkun@divmod.com>
parents: 60105
diff changeset
583 """classify_class_attrs finds static methods, class methods,
97b588ee62f0 Refactor a couple inspect module tests to remove duplicate code
Jean-Paul Calderone <exarkun@divmod.com>
parents: 60105
diff changeset
584 properties, normal methods, and data attributes on an old-style
97b588ee62f0 Refactor a couple inspect module tests to remove duplicate code
Jean-Paul Calderone <exarkun@divmod.com>
parents: 60105
diff changeset
585 class.
97b588ee62f0 Refactor a couple inspect module tests to remove duplicate code
Jean-Paul Calderone <exarkun@divmod.com>
parents: 60105
diff changeset
586 """
97b588ee62f0 Refactor a couple inspect module tests to remove duplicate code
Jean-Paul Calderone <exarkun@divmod.com>
parents: 60105
diff changeset
587 self._classify_test(False)
20040
59882e7aab89 New function classify_class_attrs(). As a number of SF bug reports
Tim Peters <tim.peters@gmail.com>
parents: 20036
diff changeset
588
59882e7aab89 New function classify_class_attrs(). As a number of SF bug reports
Tim Peters <tim.peters@gmail.com>
parents: 20036
diff changeset
589
60483
97b588ee62f0 Refactor a couple inspect module tests to remove duplicate code
Jean-Paul Calderone <exarkun@divmod.com>
parents: 60105
diff changeset
590 def test_classify_newstyle(self):
97b588ee62f0 Refactor a couple inspect module tests to remove duplicate code
Jean-Paul Calderone <exarkun@divmod.com>
parents: 60105
diff changeset
591 """Just like test_classify_oldstyle, but for a new-style class.
97b588ee62f0 Refactor a couple inspect module tests to remove duplicate code
Jean-Paul Calderone <exarkun@divmod.com>
parents: 60105
diff changeset
592 """
97b588ee62f0 Refactor a couple inspect module tests to remove duplicate code
Jean-Paul Calderone <exarkun@divmod.com>
parents: 60105
diff changeset
593 self._classify_test(True)
20040
59882e7aab89 New function classify_class_attrs(). As a number of SF bug reports
Tim Peters <tim.peters@gmail.com>
parents: 20036
diff changeset
594
74106
13f56cd8dec1 Issue #1785: Fix inspect and pydoc with misbehaving descriptors.
Antoine Pitrou <solipsis@pitrou.net>
parents: 70798
diff changeset
595 def test_classify_builtin_types(self):
13f56cd8dec1 Issue #1785: Fix inspect and pydoc with misbehaving descriptors.
Antoine Pitrou <solipsis@pitrou.net>
parents: 70798
diff changeset
596 # Simple sanity check that all built-in types can have their
13f56cd8dec1 Issue #1785: Fix inspect and pydoc with misbehaving descriptors.
Antoine Pitrou <solipsis@pitrou.net>
parents: 70798
diff changeset
597 # attributes classified.
13f56cd8dec1 Issue #1785: Fix inspect and pydoc with misbehaving descriptors.
Antoine Pitrou <solipsis@pitrou.net>
parents: 70798
diff changeset
598 for name in dir(__builtin__):
13f56cd8dec1 Issue #1785: Fix inspect and pydoc with misbehaving descriptors.
Antoine Pitrou <solipsis@pitrou.net>
parents: 70798
diff changeset
599 builtin = getattr(__builtin__, name)
13f56cd8dec1 Issue #1785: Fix inspect and pydoc with misbehaving descriptors.
Antoine Pitrou <solipsis@pitrou.net>
parents: 70798
diff changeset
600 if isinstance(builtin, type):
13f56cd8dec1 Issue #1785: Fix inspect and pydoc with misbehaving descriptors.
Antoine Pitrou <solipsis@pitrou.net>
parents: 70798
diff changeset
601 inspect.classify_class_attrs(builtin)
13f56cd8dec1 Issue #1785: Fix inspect and pydoc with misbehaving descriptors.
Antoine Pitrou <solipsis@pitrou.net>
parents: 70798
diff changeset
602
74501
f824744557ba Revert part of 13f56cd8dec1 (issue #1785) to avoid breaking getmembers() with unbound methods.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74106
diff changeset
603 def test_getmembers_method(self):
74106
13f56cd8dec1 Issue #1785: Fix inspect and pydoc with misbehaving descriptors.
Antoine Pitrou <solipsis@pitrou.net>
parents: 70798
diff changeset
604 # Old-style classes
74501
f824744557ba Revert part of 13f56cd8dec1 (issue #1785) to avoid breaking getmembers() with unbound methods.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74106
diff changeset
605 class B:
f824744557ba Revert part of 13f56cd8dec1 (issue #1785) to avoid breaking getmembers() with unbound methods.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74106
diff changeset
606 def f(self):
f824744557ba Revert part of 13f56cd8dec1 (issue #1785) to avoid breaking getmembers() with unbound methods.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74106
diff changeset
607 pass
74106
13f56cd8dec1 Issue #1785: Fix inspect and pydoc with misbehaving descriptors.
Antoine Pitrou <solipsis@pitrou.net>
parents: 70798
diff changeset
608
74501
f824744557ba Revert part of 13f56cd8dec1 (issue #1785) to avoid breaking getmembers() with unbound methods.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74106
diff changeset
609 self.assertIn(('f', B.f), inspect.getmembers(B))
f824744557ba Revert part of 13f56cd8dec1 (issue #1785) to avoid breaking getmembers() with unbound methods.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74106
diff changeset
610 # contrary to spec, ismethod() is also True for unbound methods
f824744557ba Revert part of 13f56cd8dec1 (issue #1785) to avoid breaking getmembers() with unbound methods.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74106
diff changeset
611 # (see #1785)
f824744557ba Revert part of 13f56cd8dec1 (issue #1785) to avoid breaking getmembers() with unbound methods.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74106
diff changeset
612 self.assertIn(('f', B.f), inspect.getmembers(B, inspect.ismethod))
f824744557ba Revert part of 13f56cd8dec1 (issue #1785) to avoid breaking getmembers() with unbound methods.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74106
diff changeset
613 b = B()
f824744557ba Revert part of 13f56cd8dec1 (issue #1785) to avoid breaking getmembers() with unbound methods.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74106
diff changeset
614 self.assertIn(('f', b.f), inspect.getmembers(b))
f824744557ba Revert part of 13f56cd8dec1 (issue #1785) to avoid breaking getmembers() with unbound methods.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74106
diff changeset
615 self.assertIn(('f', b.f), inspect.getmembers(b, inspect.ismethod))
74106
13f56cd8dec1 Issue #1785: Fix inspect and pydoc with misbehaving descriptors.
Antoine Pitrou <solipsis@pitrou.net>
parents: 70798
diff changeset
616
13f56cd8dec1 Issue #1785: Fix inspect and pydoc with misbehaving descriptors.
Antoine Pitrou <solipsis@pitrou.net>
parents: 70798
diff changeset
617 # New-style classes
74501
f824744557ba Revert part of 13f56cd8dec1 (issue #1785) to avoid breaking getmembers() with unbound methods.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74106
diff changeset
618 class B(object):
f824744557ba Revert part of 13f56cd8dec1 (issue #1785) to avoid breaking getmembers() with unbound methods.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74106
diff changeset
619 def f(self):
74106
13f56cd8dec1 Issue #1785: Fix inspect and pydoc with misbehaving descriptors.
Antoine Pitrou <solipsis@pitrou.net>
parents: 70798
diff changeset
620 pass
13f56cd8dec1 Issue #1785: Fix inspect and pydoc with misbehaving descriptors.
Antoine Pitrou <solipsis@pitrou.net>
parents: 70798
diff changeset
621
74501
f824744557ba Revert part of 13f56cd8dec1 (issue #1785) to avoid breaking getmembers() with unbound methods.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74106
diff changeset
622 self.assertIn(('f', B.f), inspect.getmembers(B))
f824744557ba Revert part of 13f56cd8dec1 (issue #1785) to avoid breaking getmembers() with unbound methods.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74106
diff changeset
623 self.assertIn(('f', B.f), inspect.getmembers(B, inspect.ismethod))
f824744557ba Revert part of 13f56cd8dec1 (issue #1785) to avoid breaking getmembers() with unbound methods.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74106
diff changeset
624 b = B()
f824744557ba Revert part of 13f56cd8dec1 (issue #1785) to avoid breaking getmembers() with unbound methods.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74106
diff changeset
625 self.assertIn(('f', b.f), inspect.getmembers(b))
f824744557ba Revert part of 13f56cd8dec1 (issue #1785) to avoid breaking getmembers() with unbound methods.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74106
diff changeset
626 self.assertIn(('f', b.f), inspect.getmembers(b, inspect.ismethod))
20040
59882e7aab89 New function classify_class_attrs(). As a number of SF bug reports
Tim Peters <tim.peters@gmail.com>
parents: 20036
diff changeset
627
59882e7aab89 New function classify_class_attrs(). As a number of SF bug reports
Tim Peters <tim.peters@gmail.com>
parents: 20036
diff changeset
628
60080
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
629 class TestGetcallargsFunctions(unittest.TestCase):
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
630
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
631 # tuple parameters are named '.1', '.2', etc.
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
632 is_tuplename = re.compile(r'^\.\d+$').match
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
633
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
634 def assertEqualCallArgs(self, func, call_params_string, locs=None):
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
635 locs = dict(locs or {}, func=func)
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
636 r1 = eval('func(%s)' % call_params_string, None, locs)
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
637 r2 = eval('inspect.getcallargs(func, %s)' % call_params_string, None,
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
638 locs)
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
639 self.assertEqual(r1, r2)
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
640
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
641 def assertEqualException(self, func, call_param_string, locs=None):
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
642 locs = dict(locs or {}, func=func)
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
643 try:
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
644 eval('func(%s)' % call_param_string, None, locs)
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
645 except Exception, ex1:
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
646 pass
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
647 else:
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
648 self.fail('Exception not raised')
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
649 try:
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
650 eval('inspect.getcallargs(func, %s)' % call_param_string, None,
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
651 locs)
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
652 except Exception, ex2:
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
653 pass
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
654 else:
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
655 self.fail('Exception not raised')
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
656 self.assertIs(type(ex1), type(ex2))
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
657 self.assertEqual(str(ex1), str(ex2))
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
658
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
659 def makeCallable(self, signature):
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
660 """Create a function that returns its locals(), excluding the
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
661 autogenerated '.1', '.2', etc. tuple param names (if any)."""
60105
99e523132aea Silence a py3k warning.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 60080
diff changeset
662 with check_py3k_warnings(
99e523132aea Silence a py3k warning.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 60080
diff changeset
663 ("tuple parameter unpacking has been removed", SyntaxWarning),
99e523132aea Silence a py3k warning.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 60080
diff changeset
664 quiet=True):
99e523132aea Silence a py3k warning.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 60080
diff changeset
665 code = ("lambda %s: dict(i for i in locals().items() "
99e523132aea Silence a py3k warning.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 60080
diff changeset
666 "if not is_tuplename(i[0]))")
99e523132aea Silence a py3k warning.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 60080
diff changeset
667 return eval(code % signature, {'is_tuplename' : self.is_tuplename})
20040
59882e7aab89 New function classify_class_attrs(). As a number of SF bug reports
Tim Peters <tim.peters@gmail.com>
parents: 20036
diff changeset
668
60080
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
669 def test_plain(self):
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
670 f = self.makeCallable('a, b=1')
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
671 self.assertEqualCallArgs(f, '2')
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
672 self.assertEqualCallArgs(f, '2, 3')
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
673 self.assertEqualCallArgs(f, 'a=2')
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
674 self.assertEqualCallArgs(f, 'b=3, a=2')
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
675 self.assertEqualCallArgs(f, '2, b=3')
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
676 # expand *iterable / **mapping
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
677 self.assertEqualCallArgs(f, '*(2,)')
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
678 self.assertEqualCallArgs(f, '*[2]')
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
679 self.assertEqualCallArgs(f, '*(2, 3)')
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
680 self.assertEqualCallArgs(f, '*[2, 3]')
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
681 self.assertEqualCallArgs(f, '**{"a":2}')
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
682 self.assertEqualCallArgs(f, 'b=3, **{"a":2}')
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
683 self.assertEqualCallArgs(f, '2, **{"b":3}')
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
684 self.assertEqualCallArgs(f, '**{"b":3, "a":2}')
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
685 # expand UserList / UserDict
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
686 self.assertEqualCallArgs(f, '*UserList([2])')
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
687 self.assertEqualCallArgs(f, '*UserList([2, 3])')
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
688 self.assertEqualCallArgs(f, '**UserDict(a=2)')
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
689 self.assertEqualCallArgs(f, '2, **UserDict(b=3)')
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
690 self.assertEqualCallArgs(f, 'b=2, **UserDict(a=3)')
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
691 # unicode keyword args
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
692 self.assertEqualCallArgs(f, '**{u"a":2}')
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
693 self.assertEqualCallArgs(f, 'b=3, **{u"a":2}')
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
694 self.assertEqualCallArgs(f, '2, **{u"b":3}')
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
695 self.assertEqualCallArgs(f, '**{u"b":3, u"a":2}')
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
696
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
697 def test_varargs(self):
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
698 f = self.makeCallable('a, b=1, *c')
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
699 self.assertEqualCallArgs(f, '2')
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
700 self.assertEqualCallArgs(f, '2, 3')
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
701 self.assertEqualCallArgs(f, '2, 3, 4')
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
702 self.assertEqualCallArgs(f, '*(2,3,4)')
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
703 self.assertEqualCallArgs(f, '2, *[3,4]')
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
704 self.assertEqualCallArgs(f, '2, 3, *UserList([4])')
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
705
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
706 def test_varkw(self):
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
707 f = self.makeCallable('a, b=1, **c')
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
708 self.assertEqualCallArgs(f, 'a=2')
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
709 self.assertEqualCallArgs(f, '2, b=3, c=4')
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
710 self.assertEqualCallArgs(f, 'b=3, a=2, c=4')
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
711 self.assertEqualCallArgs(f, 'c=4, **{"a":2, "b":3}')
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
712 self.assertEqualCallArgs(f, '2, c=4, **{"b":3}')
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
713 self.assertEqualCallArgs(f, 'b=2, **{"a":3, "c":4}')
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
714 self.assertEqualCallArgs(f, '**UserDict(a=2, b=3, c=4)')
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
715 self.assertEqualCallArgs(f, '2, c=4, **UserDict(b=3)')
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
716 self.assertEqualCallArgs(f, 'b=2, **UserDict(a=3, c=4)')
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
717 # unicode keyword args
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
718 self.assertEqualCallArgs(f, 'c=4, **{u"a":2, u"b":3}')
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
719 self.assertEqualCallArgs(f, '2, c=4, **{u"b":3}')
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
720 self.assertEqualCallArgs(f, 'b=2, **{u"a":3, u"c":4}')
20040
59882e7aab89 New function classify_class_attrs(). As a number of SF bug reports
Tim Peters <tim.peters@gmail.com>
parents: 20036
diff changeset
721
69032
b19d76d9d2a7 Correct handling of functions with only kwarg args in getcallargs (closes #11256)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
722 def test_varkw_only(self):
b19d76d9d2a7 Correct handling of functions with only kwarg args in getcallargs (closes #11256)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
723 # issue11256:
b19d76d9d2a7 Correct handling of functions with only kwarg args in getcallargs (closes #11256)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
724 f = self.makeCallable('**c')
b19d76d9d2a7 Correct handling of functions with only kwarg args in getcallargs (closes #11256)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
725 self.assertEqualCallArgs(f, '')
b19d76d9d2a7 Correct handling of functions with only kwarg args in getcallargs (closes #11256)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
726 self.assertEqualCallArgs(f, 'a=1')
b19d76d9d2a7 Correct handling of functions with only kwarg args in getcallargs (closes #11256)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
727 self.assertEqualCallArgs(f, 'a=1, b=2')
b19d76d9d2a7 Correct handling of functions with only kwarg args in getcallargs (closes #11256)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
728 self.assertEqualCallArgs(f, 'c=3, **{"a": 1, "b": 2}')
b19d76d9d2a7 Correct handling of functions with only kwarg args in getcallargs (closes #11256)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
729 self.assertEqualCallArgs(f, '**UserDict(a=1, b=2)')
b19d76d9d2a7 Correct handling of functions with only kwarg args in getcallargs (closes #11256)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
730 self.assertEqualCallArgs(f, 'c=3, **UserDict(a=1, b=2)')
b19d76d9d2a7 Correct handling of functions with only kwarg args in getcallargs (closes #11256)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
731
60080
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
732 def test_tupleargs(self):
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
733 f = self.makeCallable('(b,c), (d,(e,f))=(0,[1,2])')
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
734 self.assertEqualCallArgs(f, '(2,3)')
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
735 self.assertEqualCallArgs(f, '[2,3]')
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
736 self.assertEqualCallArgs(f, 'UserList([2,3])')
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
737 self.assertEqualCallArgs(f, '(2,3), (4,(5,6))')
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
738 self.assertEqualCallArgs(f, '(2,3), (4,[5,6])')
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
739 self.assertEqualCallArgs(f, '(2,3), [4,UserList([5,6])]')
20040
59882e7aab89 New function classify_class_attrs(). As a number of SF bug reports
Tim Peters <tim.peters@gmail.com>
parents: 20036
diff changeset
740
60080
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
741 def test_multiple_features(self):
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
742 f = self.makeCallable('a, b=2, (c,(d,e))=(3,[4,5]), *f, **g')
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
743 self.assertEqualCallArgs(f, '2, 3, (4,[5,6]), 7')
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
744 self.assertEqualCallArgs(f, '2, 3, *[(4,[5,6]), 7], x=8')
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
745 self.assertEqualCallArgs(f, '2, 3, x=8, *[(4,[5,6]), 7]')
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
746 self.assertEqualCallArgs(f, '2, x=8, *[3, (4,[5,6]), 7], y=9')
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
747 self.assertEqualCallArgs(f, 'x=8, *[2, 3, (4,[5,6])], y=9')
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
748 self.assertEqualCallArgs(f, 'x=8, *UserList([2, 3, (4,[5,6])]), '
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
749 '**{"y":9, "z":10}')
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
750 self.assertEqualCallArgs(f, '2, x=8, *UserList([3, (4,[5,6])]), '
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
751 '**UserDict(y=9, z=10)')
29618
58251e0a8b78 Add a trivial test of getargspec() with a method.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 28951
diff changeset
752
60080
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
753 def test_errors(self):
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
754 f0 = self.makeCallable('')
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
755 f1 = self.makeCallable('a, b')
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
756 f2 = self.makeCallable('a, b=1')
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
757 # f0 takes no arguments
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
758 self.assertEqualException(f0, '1')
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
759 self.assertEqualException(f0, 'x=1')
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
760 self.assertEqualException(f0, '1,x=1')
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
761 # f1 takes exactly 2 arguments
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
762 self.assertEqualException(f1, '')
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
763 self.assertEqualException(f1, '1')
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
764 self.assertEqualException(f1, 'a=2')
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
765 self.assertEqualException(f1, 'b=3')
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
766 # f2 takes at least 1 argument
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
767 self.assertEqualException(f2, '')
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
768 self.assertEqualException(f2, 'b=3')
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
769 for f in f1, f2:
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
770 # f1/f2 takes exactly/at most 2 arguments
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
771 self.assertEqualException(f, '2, 3, 4')
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
772 self.assertEqualException(f, '1, 2, 3, a=1')
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
773 self.assertEqualException(f, '2, 3, 4, c=5')
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
774 self.assertEqualException(f, '2, 3, 4, a=1, c=5')
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
775 # f got an unexpected keyword argument
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
776 self.assertEqualException(f, 'c=2')
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
777 self.assertEqualException(f, '2, c=3')
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
778 self.assertEqualException(f, '2, 3, c=4')
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
779 self.assertEqualException(f, '2, c=4, b=3')
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
780 self.assertEqualException(f, '**{u"\u03c0\u03b9": 4}')
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
781 # f got multiple values for keyword argument
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
782 self.assertEqualException(f, '1, a=2')
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
783 self.assertEqualException(f, '1, **{"a":2}')
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
784 self.assertEqualException(f, '1, 2, b=3')
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
785 # XXX: Python inconsistency
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
786 # - for functions and bound methods: unexpected keyword 'c'
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
787 # - for unbound methods: multiple values for keyword 'a'
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
788 #self.assertEqualException(f, '1, c=3, a=2')
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
789 f = self.makeCallable('(a,b)=(0,1)')
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
790 self.assertEqualException(f, '1')
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
791 self.assertEqualException(f, '[1]')
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
792 self.assertEqualException(f, '(1,2,3)')
69032
b19d76d9d2a7 Correct handling of functions with only kwarg args in getcallargs (closes #11256)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
793 # issue11256:
b19d76d9d2a7 Correct handling of functions with only kwarg args in getcallargs (closes #11256)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
794 f3 = self.makeCallable('**c')
b19d76d9d2a7 Correct handling of functions with only kwarg args in getcallargs (closes #11256)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
795 self.assertEqualException(f3, '1, 2')
b19d76d9d2a7 Correct handling of functions with only kwarg args in getcallargs (closes #11256)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
796 self.assertEqualException(f3, '1, 2, a=1, b=2')
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
797
60080
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
798 class TestGetcallargsMethods(TestGetcallargsFunctions):
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
799
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
800 def setUp(self):
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
801 class Foo(object):
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
802 pass
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
803 self.cls = Foo
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
804 self.inst = Foo()
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
805
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
806 def makeCallable(self, signature):
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
807 assert 'self' not in signature
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
808 mk = super(TestGetcallargsMethods, self).makeCallable
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
809 self.cls.method = mk('self, ' + signature)
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
810 return self.inst.method
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
811
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
812 class TestGetcallargsUnboundMethods(TestGetcallargsMethods):
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
813
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
814 def makeCallable(self, signature):
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
815 super(TestGetcallargsUnboundMethods, self).makeCallable(signature)
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
816 return self.cls.method
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
817
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
818 def assertEqualCallArgs(self, func, call_params_string, locs=None):
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
819 return super(TestGetcallargsUnboundMethods, self).assertEqualCallArgs(
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
820 *self._getAssertEqualParams(func, call_params_string, locs))
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
821
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
822 def assertEqualException(self, func, call_params_string, locs=None):
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
823 return super(TestGetcallargsUnboundMethods, self).assertEqualException(
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
824 *self._getAssertEqualParams(func, call_params_string, locs))
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
825
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
826 def _getAssertEqualParams(self, func, call_params_string, locs=None):
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
827 assert 'inst' not in call_params_string
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
828 locs = dict(locs or {}, inst=self.inst)
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
829 return (func, 'inst,' + call_params_string, locs)
33137
ad268a8408f4 - Bug #891637, patch #1005466: fix inspect.getargs() crash on def foo((bar)).
Matthias Klose <doko@ubuntu.com>
parents: 32253
diff changeset
830
34151
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
831 def test_main():
60080
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
832 run_unittest(
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
833 TestDecorators, TestRetrievingSourceCode, TestOneliners, TestBuggyCases,
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
834 TestInterpreterStack, TestClassesAndFunctions, TestPredicates,
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
835 TestGetcallargsFunctions, TestGetcallargsMethods,
a14a205ab1fb add inspect.getcallargs, which binds function arguments like a normal call #3135
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
836 TestGetcallargsUnboundMethods)
34151
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
837
de4701fc176f Patch #736962: port test_inspect to unittest. As part of this, move out
Johannes Gijsbers <jlg@dds.nl>
parents: 33172
diff changeset
838 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
839 test_main()