Show More
@@ -182,10 +182,11 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__'): |
|
|||
186 |
|
|
185 | try: | |
187 |
|
|
186 | src = inspect.getsource(obj.__class__) | |
188 |
|
|
187 | except (OSError, TypeError): | |
|
188 | return None | |||
|
189 | except OSError: | |||
189 |
|
|
190 | return None | |
190 |
|
191 | |||
191 | return src |
|
192 | return src | |
@@ -308,17 +309,17 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 |
|
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 |
|
|
315 | try: | |
316 |
|
|
316 | fname = inspect.getabsfile(obj.__class__) | |
317 |
|
|
317 | except (OSError, TypeError): | |
318 |
|
|
318 | # Can happen for builtins | |
319 |
|
|
319 | pass | |
320 | except: |
|
320 | except OSError: | |
321 | pass |
|
321 | pass | |
|
322 | ||||
322 | return cast_unicode(fname) |
|
323 | return cast_unicode(fname) | |
323 |
|
324 | |||
324 |
|
325 | |||
@@ -341,15 +342,14 def find_source_lines(obj): | |||||
341 | obj = _get_wrapped(obj) |
|
342 | obj = _get_wrapped(obj) | |
342 |
|
343 | |||
343 | try: |
|
344 | try: | |
344 | try: |
|
|||
345 |
|
|
345 | lineno = inspect.getsourcelines(obj)[1] | |
346 |
|
|
346 | except TypeError: | |
347 |
|
|
347 | # For instances, try the class object like getsource() does | |
348 | if hasattr(obj, '__class__'): |
|
348 | try: | |
349 |
|
|
349 | lineno = inspect.getsourcelines(obj.__class__)[1] | |
350 | else: |
|
350 | except (OSError, TypeError): | |
351 |
|
|
351 | return None | |
352 | except: |
|
352 | except OSError: | |
353 | return None |
|
353 | return None | |
354 |
|
354 | |||
355 | return lineno |
|
355 | return lineno |
@@ -6,8 +6,11 | |||||
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 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 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 = |
|
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 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