Show More
@@ -184,8 +184,10 b" def getsource(obj, oname='') -> Union[str,None]:" | |||
|
184 | 184 | # its class definition instead. |
|
185 | 185 | try: |
|
186 | 186 | src = inspect.getsource(obj.__class__) |
|
187 | except TypeError: | |
|
187 | except (OSError, TypeError): | |
|
188 | 188 | return None |
|
189 | except OSError: | |
|
190 | return None | |
|
189 | 191 | |
|
190 | 192 | return src |
|
191 | 193 | |
@@ -315,8 +317,9 b' def find_file(obj) -> str:' | |||
|
315 | 317 | except (OSError, TypeError): |
|
316 | 318 | # Can happen for builtins |
|
317 | 319 | pass |
|
318 | except: | |
|
320 | except OSError: | |
|
319 | 321 | pass |
|
322 | ||
|
320 | 323 | return cast_unicode(fname) |
|
321 | 324 | |
|
322 | 325 | |
@@ -339,12 +342,14 b' def find_source_lines(obj):' | |||
|
339 | 342 | obj = _get_wrapped(obj) |
|
340 | 343 | |
|
341 | 344 | try: |
|
345 | lineno = inspect.getsourcelines(obj)[1] | |
|
346 | except TypeError: | |
|
347 | # For instances, try the class object like getsource() does | |
|
342 | 348 | try: |
|
343 | lineno = inspect.getsourcelines(obj)[1] | |
|
344 | except TypeError: | |
|
345 | # For instances, try the class object like getsource() does | |
|
346 | 349 | lineno = inspect.getsourcelines(obj.__class__)[1] |
|
347 | except: | |
|
350 | except (OSError, TypeError): | |
|
351 | return None | |
|
352 | except OSError: | |
|
348 | 353 | return None |
|
349 | 354 | |
|
350 | 355 | return lineno |
@@ -6,8 +6,11 b'' | |||
|
6 | 6 | |
|
7 | 7 | |
|
8 | 8 | from inspect import signature, Signature, Parameter |
|
9 | import inspect | |
|
9 | 10 | import os |
|
11 | import pytest | |
|
10 | 12 | import re |
|
13 | import sys | |
|
11 | 14 | |
|
12 | 15 | from .. import oinspect |
|
13 | 16 | |
@@ -28,6 +31,10 b' def setup_module():' | |||
|
28 | 31 | inspector = oinspect.Inspector() |
|
29 | 32 | |
|
30 | 33 | |
|
34 | class SourceModuleMainTest: | |
|
35 | __module__ = "__main__" | |
|
36 | ||
|
37 | ||
|
31 | 38 | #----------------------------------------------------------------------------- |
|
32 | 39 | # Local utilities |
|
33 | 40 | #----------------------------------------------------------------------------- |
@@ -36,15 +43,28 b' def setup_module():' | |||
|
36 | 43 | # defined, if any code is inserted above, the following line will need to be |
|
37 | 44 | # updated. Do NOT insert any whitespace between the next line and the function |
|
38 | 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): | |
|
46 | self.assertEqual(oinspect.find_source_lines(Test.test_find_source_lines), | |
|
47 | THIS_LINE_NUMBER+6) | |
|
62 | def test_inspect_getfile_raises_exception(): | |
|
63 | """Check oinspect.find_file/getsource/find_source_lines expectations""" | |
|
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 | 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 | 80 | def test_find_file(): |
|
61 | 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 | 87 | def test_find_file_decorated1(): |
General Comments 0
You need to be logged in to leave comments.
Login now