##// END OF EJS Templates
Merge pull request #13371 from Kojoley/inspect-exception-type...
Matthias Bussonnier -
r27237:55ce3d75 merge
parent child Browse files
Show More
@@ -182,11 +182,12 b" def getsource(obj, oname='') -> Union[str,None]:"
182 except TypeError:
182 except TypeError:
183 # The object itself provided no meaningful source, try looking for
183 # The object itself provided no meaningful source, try looking for
184 # its class definition instead.
184 # its class definition instead.
185 if hasattr(obj, '__class__'):
185 try:
186 try:
186 src = inspect.getsource(obj.__class__)
187 src = inspect.getsource(obj.__class__)
187 except (OSError, TypeError):
188 except TypeError:
188 return None
189 return None
189 except OSError:
190 return None
190
191
191 return src
192 return src
192
193
@@ -308,17 +309,17 b' def find_file(obj) -> str:'
308 fname = None
309 fname = None
309 try:
310 try:
310 fname = inspect.getabsfile(obj)
311 fname = inspect.getabsfile(obj)
311 except (OSError, TypeError):
312 except TypeError:
312 # For an instance, the file that matters is where its class was
313 # For an instance, the file that matters is where its class was
313 # declared.
314 # declared.
314 if hasattr(obj, '__class__'):
315 try:
315 try:
316 fname = inspect.getabsfile(obj.__class__)
316 fname = inspect.getabsfile(obj.__class__)
317 except (OSError, TypeError):
317 except (OSError, TypeError):
318 # Can happen for builtins
318 # Can happen for builtins
319 pass
319 pass
320 except OSError:
320 except:
321 pass
321 pass
322
322 return cast_unicode(fname)
323 return cast_unicode(fname)
323
324
324
325
@@ -341,15 +342,14 b' def find_source_lines(obj):'
341 obj = _get_wrapped(obj)
342 obj = _get_wrapped(obj)
342
343
343 try:
344 try:
345 lineno = inspect.getsourcelines(obj)[1]
346 except TypeError:
347 # For instances, try the class object like getsource() does
344 try:
348 try:
345 lineno = inspect.getsourcelines(obj)[1]
349 lineno = inspect.getsourcelines(obj.__class__)[1]
346 except TypeError:
350 except (OSError, TypeError):
347 # For instances, try the class object like getsource() does
351 return None
348 if hasattr(obj, '__class__'):
352 except OSError:
349 lineno = inspect.getsourcelines(obj.__class__)[1]
350 else:
351 lineno = None
352 except:
353 return None
353 return None
354
354
355 return lineno
355 return lineno
@@ -6,8 +6,11 b''
6
6
7
7
8 from inspect import signature, Signature, Parameter
8 from inspect import signature, Signature, Parameter
9 import inspect
9 import os
10 import os
11 import pytest
10 import re
12 import re
13 import sys
11
14
12 from .. import oinspect
15 from .. import oinspect
13
16
@@ -28,6 +31,10 b' def setup_module():'
28 inspector = oinspect.Inspector()
31 inspector = oinspect.Inspector()
29
32
30
33
34 class SourceModuleMainTest:
35 __module__ = "__main__"
36
37
31 #-----------------------------------------------------------------------------
38 #-----------------------------------------------------------------------------
32 # Local utilities
39 # Local utilities
33 #-----------------------------------------------------------------------------
40 #-----------------------------------------------------------------------------
@@ -36,15 +43,28 b' def setup_module():'
36 # defined, if any code is inserted above, the following line will need to be
43 # defined, if any code is inserted above, the following line will need to be
37 # updated. Do NOT insert any whitespace between the next line and the function
44 # updated. Do NOT insert any whitespace between the next line and the function
38 # definition below.
45 # definition below.
39 THIS_LINE_NUMBER = 39 # Put here the actual number of this line
46 THIS_LINE_NUMBER = 46 # Put here the actual number of this line
47
48
49 def test_find_source_lines():
50 assert oinspect.find_source_lines(test_find_source_lines) == THIS_LINE_NUMBER + 3
51 assert oinspect.find_source_lines(type) is None
52 assert oinspect.find_source_lines(SourceModuleMainTest) is None
53 assert oinspect.find_source_lines(SourceModuleMainTest()) is None
54
40
55
41 from unittest import TestCase
56 def test_getsource():
57 assert oinspect.getsource(type) is None
58 assert oinspect.getsource(SourceModuleMainTest) is None
59 assert oinspect.getsource(SourceModuleMainTest()) is None
42
60
43 class Test(TestCase):
44
61
45 def test_find_source_lines(self):
62 def test_inspect_getfile_raises_exception():
46 self.assertEqual(oinspect.find_source_lines(Test.test_find_source_lines),
63 """Check oinspect.find_file/getsource/find_source_lines expectations"""
47 THIS_LINE_NUMBER+6)
64 with pytest.raises(TypeError):
65 inspect.getfile(type)
66 with pytest.raises(OSError if sys.version_info >= (3, 10) else TypeError):
67 inspect.getfile(SourceModuleMainTest)
48
68
49
69
50 # A couple of utilities to ensure these tests work the same from a source or a
70 # A couple of utilities to ensure these tests work the same from a source or a
@@ -59,6 +79,9 b' def match_pyfiles(f1, f2):'
59
79
60 def test_find_file():
80 def test_find_file():
61 match_pyfiles(oinspect.find_file(test_find_file), os.path.abspath(__file__))
81 match_pyfiles(oinspect.find_file(test_find_file), os.path.abspath(__file__))
82 assert oinspect.find_file(type) is None
83 assert oinspect.find_file(SourceModuleMainTest) is None
84 assert oinspect.find_file(SourceModuleMainTest()) is None
62
85
63
86
64 def test_find_file_decorated1():
87 def test_find_file_decorated1():
General Comments 0
You need to be logged in to leave comments. Login now