mirror of
https://github.com/python/cpython.git
synced 2025-10-17 04:08:28 +00:00
inspect: Fix getsource() to load updated source of reloaded module
Issue #1218234. Initial patch by Berker Peksag.
This commit is contained in:
parent
b532df62b9
commit
ef1e750225
3 changed files with 46 additions and 6 deletions
|
@ -653,11 +653,17 @@ def findsource(object):
|
||||||
in the file and the line number indexes a line in that list. An OSError
|
in the file and the line number indexes a line in that list. An OSError
|
||||||
is raised if the source code cannot be retrieved."""
|
is raised if the source code cannot be retrieved."""
|
||||||
|
|
||||||
file = getfile(object)
|
file = getsourcefile(object)
|
||||||
sourcefile = getsourcefile(object)
|
if file:
|
||||||
if not sourcefile and file[:1] + file[-1:] != '<>':
|
# Invalidate cache if needed.
|
||||||
raise OSError('source code not available')
|
linecache.checkcache(file)
|
||||||
file = sourcefile if sourcefile else file
|
else:
|
||||||
|
file = getfile(object)
|
||||||
|
# Allow filenames in form of "<something>" to pass through.
|
||||||
|
# `doctest` monkeypatches `linecache` module to enable
|
||||||
|
# inspection, so let `linecache.getlines` to be called.
|
||||||
|
if not (file.startswith('<') and file.endswith('>')):
|
||||||
|
raise OSError('source code not available')
|
||||||
|
|
||||||
module = getmodule(object, file)
|
module = getmodule(object, file)
|
||||||
if module:
|
if module:
|
||||||
|
|
|
@ -14,6 +14,7 @@ import re
|
||||||
import shutil
|
import shutil
|
||||||
import sys
|
import sys
|
||||||
import types
|
import types
|
||||||
|
import textwrap
|
||||||
import unicodedata
|
import unicodedata
|
||||||
import unittest
|
import unittest
|
||||||
import unittest.mock
|
import unittest.mock
|
||||||
|
@ -29,6 +30,8 @@ from test.script_helper import assert_python_ok, assert_python_failure
|
||||||
from test import inspect_fodder as mod
|
from test import inspect_fodder as mod
|
||||||
from test import inspect_fodder2 as mod2
|
from test import inspect_fodder2 as mod2
|
||||||
|
|
||||||
|
from test.test_import import _ready_to_import
|
||||||
|
|
||||||
|
|
||||||
# Functions tested in this suite:
|
# Functions tested in this suite:
|
||||||
# ismodule, isclass, ismethod, isfunction, istraceback, isframe, iscode,
|
# ismodule, isclass, ismethod, isfunction, istraceback, isframe, iscode,
|
||||||
|
@ -3262,6 +3265,34 @@ class TestMain(unittest.TestCase):
|
||||||
self.assertEqual(err, b'')
|
self.assertEqual(err, b'')
|
||||||
|
|
||||||
|
|
||||||
|
class TestReload(unittest.TestCase):
|
||||||
|
|
||||||
|
src_before = textwrap.dedent("""\
|
||||||
|
def foo():
|
||||||
|
print("Bla")
|
||||||
|
""")
|
||||||
|
|
||||||
|
src_after = textwrap.dedent("""\
|
||||||
|
def foo():
|
||||||
|
print("Oh no!")
|
||||||
|
""")
|
||||||
|
|
||||||
|
def assertInspectEqual(self, path, source):
|
||||||
|
inspected_src = inspect.getsource(source)
|
||||||
|
with open(path) as src:
|
||||||
|
self.assertEqual(
|
||||||
|
src.read().splitlines(True),
|
||||||
|
inspected_src.splitlines(True)
|
||||||
|
)
|
||||||
|
|
||||||
|
def test_getsource_reload(self):
|
||||||
|
# see issue 1218234
|
||||||
|
with _ready_to_import('reload_bug', self.src_before) as (name, path):
|
||||||
|
module = importlib.import_module(name)
|
||||||
|
self.assertInspectEqual(path, module)
|
||||||
|
with open(path, 'w') as src:
|
||||||
|
src.write(self.src_after)
|
||||||
|
self.assertInspectEqual(path, module)
|
||||||
|
|
||||||
|
|
||||||
def test_main():
|
def test_main():
|
||||||
|
@ -3272,7 +3303,7 @@ def test_main():
|
||||||
TestGetcallargsUnboundMethods, TestGetattrStatic, TestGetGeneratorState,
|
TestGetcallargsUnboundMethods, TestGetattrStatic, TestGetGeneratorState,
|
||||||
TestNoEOL, TestSignatureObject, TestSignatureBind, TestParameterObject,
|
TestNoEOL, TestSignatureObject, TestSignatureBind, TestParameterObject,
|
||||||
TestBoundArguments, TestSignaturePrivateHelpers, TestGetClosureVars,
|
TestBoundArguments, TestSignaturePrivateHelpers, TestGetClosureVars,
|
||||||
TestUnwrap, TestMain
|
TestUnwrap, TestMain, TestReload
|
||||||
)
|
)
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|
|
@ -194,6 +194,9 @@ Core and Builtins
|
||||||
Library
|
Library
|
||||||
-------
|
-------
|
||||||
|
|
||||||
|
- Issue #1218234: Fix inspect.getsource() to load updated source of
|
||||||
|
reloaded module. Initial patch by Berker Peksag.
|
||||||
|
|
||||||
- Issue #21740: Support wrapped callables in pydoc. Patch by Claudiu Popa.
|
- Issue #21740: Support wrapped callables in pydoc. Patch by Claudiu Popa.
|
||||||
|
|
||||||
- Issue #23009: Make sure selectors.EpollSelecrtor.select() works when no
|
- Issue #23009: Make sure selectors.EpollSelecrtor.select() works when no
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue