##// END OF EJS Templates
Fix failure due to .pyc/.py mismatch detected by test_pr.
Fernando Perez -
Show More
@@ -1,277 +1,285 b''
1 """Tests for the object inspection functionality.
1 """Tests for the object inspection functionality.
2 """
2 """
3 #-----------------------------------------------------------------------------
3 #-----------------------------------------------------------------------------
4 # Copyright (C) 2010-2011 The IPython Development Team.
4 # Copyright (C) 2010-2011 The IPython Development Team.
5 #
5 #
6 # Distributed under the terms of the BSD License.
6 # Distributed under the terms of the BSD License.
7 #
7 #
8 # The full license is in the file COPYING.txt, distributed with this software.
8 # The full license is in the file COPYING.txt, distributed with this software.
9 #-----------------------------------------------------------------------------
9 #-----------------------------------------------------------------------------
10
10
11 #-----------------------------------------------------------------------------
11 #-----------------------------------------------------------------------------
12 # Imports
12 # Imports
13 #-----------------------------------------------------------------------------
13 #-----------------------------------------------------------------------------
14 from __future__ import print_function
14 from __future__ import print_function
15
15
16 # Stdlib imports
16 # Stdlib imports
17 import os
17 import os
18 import re
18
19
19 # Third-party imports
20 # Third-party imports
20 import nose.tools as nt
21 import nose.tools as nt
21
22
22 # Our own imports
23 # Our own imports
23 from .. import oinspect
24 from .. import oinspect
24 from IPython.core.magic import (Magics, magics_class, line_magic,
25 from IPython.core.magic import (Magics, magics_class, line_magic,
25 cell_magic, line_cell_magic,
26 cell_magic, line_cell_magic,
26 register_line_magic, register_cell_magic,
27 register_line_magic, register_cell_magic,
27 register_line_cell_magic)
28 register_line_cell_magic)
28 from IPython.external.decorator import decorator
29 from IPython.external.decorator import decorator
29 from IPython.utils import py3compat
30 from IPython.utils import py3compat
30
31
31
32
32 #-----------------------------------------------------------------------------
33 #-----------------------------------------------------------------------------
33 # Globals and constants
34 # Globals and constants
34 #-----------------------------------------------------------------------------
35 #-----------------------------------------------------------------------------
35
36
36 inspector = oinspect.Inspector()
37 inspector = oinspect.Inspector()
37 ip = get_ipython()
38 ip = get_ipython()
38
39
39 #-----------------------------------------------------------------------------
40 #-----------------------------------------------------------------------------
40 # Local utilities
41 # Local utilities
41 #-----------------------------------------------------------------------------
42 #-----------------------------------------------------------------------------
42
43
43 # WARNING: since this test checks the line number where a function is
44 # WARNING: since this test checks the line number where a function is
44 # defined, if any code is inserted above, the following line will need to be
45 # defined, if any code is inserted above, the following line will need to be
45 # updated. Do NOT insert any whitespace between the next line and the function
46 # updated. Do NOT insert any whitespace between the next line and the function
46 # definition below.
47 # definition below.
47 THIS_LINE_NUMBER = 47 # Put here the actual number of this line
48 THIS_LINE_NUMBER = 48 # Put here the actual number of this line
48 def test_find_source_lines():
49 def test_find_source_lines():
49 nt.assert_equal(oinspect.find_source_lines(test_find_source_lines),
50 nt.assert_equal(oinspect.find_source_lines(test_find_source_lines),
50 THIS_LINE_NUMBER+1)
51 THIS_LINE_NUMBER+1)
51
52
52
53
54 # A couple of utilities to ensure these tests work the same from a source or a
55 # binary install
56 def pyfile(fname):
57 return re.sub('.py[co]$', '.py', fname)
58
59
60 def match_pyfiles(f1, f2):
61 nt.assert_equal(pyfile(f1), pyfile(f2))
62
63
53 def test_find_file():
64 def test_find_file():
54 nt.assert_equal(oinspect.find_file(test_find_file),
65 match_pyfiles(oinspect.find_file(test_find_file), os.path.abspath(__file__))
55 os.path.abspath(__file__))
56
66
57
67
58 def test_find_file_decorated1():
68 def test_find_file_decorated1():
59
69
60 @decorator
70 @decorator
61 def noop1(f):
71 def noop1(f):
62 def wrapper():
72 def wrapper():
63 return f(*a, **kw)
73 return f(*a, **kw)
64 return wrapper
74 return wrapper
65
75
66 @noop1
76 @noop1
67 def f(x):
77 def f(x):
68 "My docstring"
78 "My docstring"
69
79
70 nt.assert_equal(oinspect.find_file(f),
80 match_pyfiles(oinspect.find_file(f), os.path.abspath(__file__))
71 os.path.abspath(__file__))
72 nt.assert_equal(f.__doc__, "My docstring")
81 nt.assert_equal(f.__doc__, "My docstring")
73
82
74
83
75 def test_find_file_decorated2():
84 def test_find_file_decorated2():
76
85
77 @decorator
86 @decorator
78 def noop2(f, *a, **kw):
87 def noop2(f, *a, **kw):
79 return f(*a, **kw)
88 return f(*a, **kw)
80
89
81 @noop2
90 @noop2
82 def f(x):
91 def f(x):
83 "My docstring 2"
92 "My docstring 2"
84
93
85 nt.assert_equal(oinspect.find_file(f),
94 match_pyfiles(oinspect.find_file(f), os.path.abspath(__file__))
86 os.path.abspath(__file__))
87 nt.assert_equal(f.__doc__, "My docstring 2")
95 nt.assert_equal(f.__doc__, "My docstring 2")
88
96
89
97
90 def test_find_file_magic():
98 def test_find_file_magic():
91 run = ip.find_line_magic('run')
99 run = ip.find_line_magic('run')
92 nt.assert_not_equal(oinspect.find_file(run), None)
100 nt.assert_not_equal(oinspect.find_file(run), None)
93
101
94
102
95 # A few generic objects we can then inspect in the tests below
103 # A few generic objects we can then inspect in the tests below
96
104
97 class Call(object):
105 class Call(object):
98 """This is the class docstring."""
106 """This is the class docstring."""
99
107
100 def __init__(self, x, y=1):
108 def __init__(self, x, y=1):
101 """This is the constructor docstring."""
109 """This is the constructor docstring."""
102
110
103 def __call__(self, *a, **kw):
111 def __call__(self, *a, **kw):
104 """This is the call docstring."""
112 """This is the call docstring."""
105
113
106 def method(self, x, z=2):
114 def method(self, x, z=2):
107 """Some method's docstring"""
115 """Some method's docstring"""
108
116
109
117
110 class OldStyle:
118 class OldStyle:
111 """An old-style class for testing."""
119 """An old-style class for testing."""
112 pass
120 pass
113
121
114
122
115 def f(x, y=2, *a, **kw):
123 def f(x, y=2, *a, **kw):
116 """A simple function."""
124 """A simple function."""
117
125
118
126
119 def g(y, z=3, *a, **kw):
127 def g(y, z=3, *a, **kw):
120 pass # no docstring
128 pass # no docstring
121
129
122
130
123 @register_line_magic
131 @register_line_magic
124 def lmagic(line):
132 def lmagic(line):
125 "A line magic"
133 "A line magic"
126
134
127
135
128 @register_cell_magic
136 @register_cell_magic
129 def cmagic(line, cell):
137 def cmagic(line, cell):
130 "A cell magic"
138 "A cell magic"
131
139
132
140
133 @register_line_cell_magic
141 @register_line_cell_magic
134 def lcmagic(line, cell=None):
142 def lcmagic(line, cell=None):
135 "A line/cell magic"
143 "A line/cell magic"
136
144
137
145
138 @magics_class
146 @magics_class
139 class SimpleMagics(Magics):
147 class SimpleMagics(Magics):
140 @line_magic
148 @line_magic
141 def Clmagic(self, cline):
149 def Clmagic(self, cline):
142 "A class-based line magic"
150 "A class-based line magic"
143
151
144 @cell_magic
152 @cell_magic
145 def Ccmagic(self, cline, ccell):
153 def Ccmagic(self, cline, ccell):
146 "A class-based cell magic"
154 "A class-based cell magic"
147
155
148 @line_cell_magic
156 @line_cell_magic
149 def Clcmagic(self, cline, ccell=None):
157 def Clcmagic(self, cline, ccell=None):
150 "A class-based line/cell magic"
158 "A class-based line/cell magic"
151
159
152
160
153 def check_calltip(obj, name, call, docstring):
161 def check_calltip(obj, name, call, docstring):
154 """Generic check pattern all calltip tests will use"""
162 """Generic check pattern all calltip tests will use"""
155 info = inspector.info(obj, name)
163 info = inspector.info(obj, name)
156 call_line, ds = oinspect.call_tip(info)
164 call_line, ds = oinspect.call_tip(info)
157 nt.assert_equal(call_line, call)
165 nt.assert_equal(call_line, call)
158 nt.assert_equal(ds, docstring)
166 nt.assert_equal(ds, docstring)
159
167
160 #-----------------------------------------------------------------------------
168 #-----------------------------------------------------------------------------
161 # Tests
169 # Tests
162 #-----------------------------------------------------------------------------
170 #-----------------------------------------------------------------------------
163
171
164 def test_calltip_class():
172 def test_calltip_class():
165 check_calltip(Call, 'Call', 'Call(x, y=1)', Call.__init__.__doc__)
173 check_calltip(Call, 'Call', 'Call(x, y=1)', Call.__init__.__doc__)
166
174
167
175
168 def test_calltip_instance():
176 def test_calltip_instance():
169 c = Call(1)
177 c = Call(1)
170 check_calltip(c, 'c', 'c(*a, **kw)', c.__call__.__doc__)
178 check_calltip(c, 'c', 'c(*a, **kw)', c.__call__.__doc__)
171
179
172
180
173 def test_calltip_method():
181 def test_calltip_method():
174 c = Call(1)
182 c = Call(1)
175 check_calltip(c.method, 'c.method', 'c.method(x, z=2)', c.method.__doc__)
183 check_calltip(c.method, 'c.method', 'c.method(x, z=2)', c.method.__doc__)
176
184
177
185
178 def test_calltip_function():
186 def test_calltip_function():
179 check_calltip(f, 'f', 'f(x, y=2, *a, **kw)', f.__doc__)
187 check_calltip(f, 'f', 'f(x, y=2, *a, **kw)', f.__doc__)
180
188
181
189
182 def test_calltip_function2():
190 def test_calltip_function2():
183 check_calltip(g, 'g', 'g(y, z=3, *a, **kw)', '<no docstring>')
191 check_calltip(g, 'g', 'g(y, z=3, *a, **kw)', '<no docstring>')
184
192
185
193
186 def test_calltip_builtin():
194 def test_calltip_builtin():
187 check_calltip(sum, 'sum', None, sum.__doc__)
195 check_calltip(sum, 'sum', None, sum.__doc__)
188
196
189
197
190 def test_calltip_line_magic():
198 def test_calltip_line_magic():
191 check_calltip(lmagic, 'lmagic', 'lmagic(line)', "A line magic")
199 check_calltip(lmagic, 'lmagic', 'lmagic(line)', "A line magic")
192
200
193
201
194 def test_calltip_cell_magic():
202 def test_calltip_cell_magic():
195 check_calltip(cmagic, 'cmagic', 'cmagic(line, cell)', "A cell magic")
203 check_calltip(cmagic, 'cmagic', 'cmagic(line, cell)', "A cell magic")
196
204
197
205
198 def test_calltip_line_magic():
206 def test_calltip_line_magic():
199 check_calltip(lcmagic, 'lcmagic', 'lcmagic(line, cell=None)',
207 check_calltip(lcmagic, 'lcmagic', 'lcmagic(line, cell=None)',
200 "A line/cell magic")
208 "A line/cell magic")
201
209
202
210
203 def test_class_magics():
211 def test_class_magics():
204 cm = SimpleMagics(ip)
212 cm = SimpleMagics(ip)
205 ip.register_magics(cm)
213 ip.register_magics(cm)
206 check_calltip(cm.Clmagic, 'Clmagic', 'Clmagic(cline)',
214 check_calltip(cm.Clmagic, 'Clmagic', 'Clmagic(cline)',
207 "A class-based line magic")
215 "A class-based line magic")
208 check_calltip(cm.Ccmagic, 'Ccmagic', 'Ccmagic(cline, ccell)',
216 check_calltip(cm.Ccmagic, 'Ccmagic', 'Ccmagic(cline, ccell)',
209 "A class-based cell magic")
217 "A class-based cell magic")
210 check_calltip(cm.Clcmagic, 'Clcmagic', 'Clcmagic(cline, ccell=None)',
218 check_calltip(cm.Clcmagic, 'Clcmagic', 'Clcmagic(cline, ccell=None)',
211 "A class-based line/cell magic")
219 "A class-based line/cell magic")
212
220
213
221
214 def test_info():
222 def test_info():
215 "Check that Inspector.info fills out various fields as expected."
223 "Check that Inspector.info fills out various fields as expected."
216 i = inspector.info(Call, oname='Call')
224 i = inspector.info(Call, oname='Call')
217 nt.assert_equal(i['type_name'], 'type')
225 nt.assert_equal(i['type_name'], 'type')
218 expted_class = str(type(type)) # <class 'type'> (Python 3) or <type 'type'>
226 expted_class = str(type(type)) # <class 'type'> (Python 3) or <type 'type'>
219 nt.assert_equal(i['base_class'], expted_class)
227 nt.assert_equal(i['base_class'], expted_class)
220 nt.assert_equal(i['string_form'], "<class 'IPython.core.tests.test_oinspect.Call'>")
228 nt.assert_equal(i['string_form'], "<class 'IPython.core.tests.test_oinspect.Call'>")
221 fname = __file__
229 fname = __file__
222 if fname.endswith(".pyc"):
230 if fname.endswith(".pyc"):
223 fname = fname[:-1]
231 fname = fname[:-1]
224 # case-insensitive comparison needed on some filesystems
232 # case-insensitive comparison needed on some filesystems
225 # e.g. Windows:
233 # e.g. Windows:
226 nt.assert_equal(i['file'].lower(), fname.lower())
234 nt.assert_equal(i['file'].lower(), fname.lower())
227 nt.assert_equal(i['definition'], 'Call(self, *a, **kw)\n')
235 nt.assert_equal(i['definition'], 'Call(self, *a, **kw)\n')
228 nt.assert_equal(i['docstring'], Call.__doc__)
236 nt.assert_equal(i['docstring'], Call.__doc__)
229 nt.assert_equal(i['source'], None)
237 nt.assert_equal(i['source'], None)
230 nt.assert_true(i['isclass'])
238 nt.assert_true(i['isclass'])
231 nt.assert_equal(i['init_definition'], "Call(self, x, y=1)\n")
239 nt.assert_equal(i['init_definition'], "Call(self, x, y=1)\n")
232 nt.assert_equal(i['init_docstring'], Call.__init__.__doc__)
240 nt.assert_equal(i['init_docstring'], Call.__init__.__doc__)
233
241
234 i = inspector.info(Call, detail_level=1)
242 i = inspector.info(Call, detail_level=1)
235 nt.assert_not_equal(i['source'], None)
243 nt.assert_not_equal(i['source'], None)
236 nt.assert_equal(i['docstring'], None)
244 nt.assert_equal(i['docstring'], None)
237
245
238 c = Call(1)
246 c = Call(1)
239 c.__doc__ = "Modified instance docstring"
247 c.__doc__ = "Modified instance docstring"
240 i = inspector.info(c)
248 i = inspector.info(c)
241 nt.assert_equal(i['type_name'], 'Call')
249 nt.assert_equal(i['type_name'], 'Call')
242 nt.assert_equal(i['docstring'], "Modified instance docstring")
250 nt.assert_equal(i['docstring'], "Modified instance docstring")
243 nt.assert_equal(i['class_docstring'], Call.__doc__)
251 nt.assert_equal(i['class_docstring'], Call.__doc__)
244 nt.assert_equal(i['init_docstring'], Call.__init__.__doc__)
252 nt.assert_equal(i['init_docstring'], Call.__init__.__doc__)
245 nt.assert_equal(i['call_docstring'], c.__call__.__doc__)
253 nt.assert_equal(i['call_docstring'], c.__call__.__doc__)
246
254
247 # Test old-style classes, which for example may not have an __init__ method.
255 # Test old-style classes, which for example may not have an __init__ method.
248 if not py3compat.PY3:
256 if not py3compat.PY3:
249 i = inspector.info(OldStyle)
257 i = inspector.info(OldStyle)
250 nt.assert_equal(i['type_name'], 'classobj')
258 nt.assert_equal(i['type_name'], 'classobj')
251
259
252 i = inspector.info(OldStyle())
260 i = inspector.info(OldStyle())
253 nt.assert_equal(i['type_name'], 'instance')
261 nt.assert_equal(i['type_name'], 'instance')
254 nt.assert_equal(i['docstring'], OldStyle.__doc__)
262 nt.assert_equal(i['docstring'], OldStyle.__doc__)
255
263
256 def test_getdoc():
264 def test_getdoc():
257 class A(object):
265 class A(object):
258 """standard docstring"""
266 """standard docstring"""
259 pass
267 pass
260
268
261 class B(object):
269 class B(object):
262 """standard docstring"""
270 """standard docstring"""
263 def getdoc(self):
271 def getdoc(self):
264 return "custom docstring"
272 return "custom docstring"
265
273
266 class C(object):
274 class C(object):
267 """standard docstring"""
275 """standard docstring"""
268 def getdoc(self):
276 def getdoc(self):
269 return None
277 return None
270
278
271 a = A()
279 a = A()
272 b = B()
280 b = B()
273 c = C()
281 c = C()
274
282
275 nt.assert_equal(oinspect.getdoc(a), "standard docstring")
283 nt.assert_equal(oinspect.getdoc(a), "standard docstring")
276 nt.assert_equal(oinspect.getdoc(b), "custom docstring")
284 nt.assert_equal(oinspect.getdoc(b), "custom docstring")
277 nt.assert_equal(oinspect.getdoc(c), "standard docstring")
285 nt.assert_equal(oinspect.getdoc(c), "standard docstring")
General Comments 0
You need to be logged in to leave comments. Login now