Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
7de29687
Commit
7de29687
authored
Dec 08, 2014
by
Yury Selivanov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
inspect: Fix getsource() to load updated source of reloaded module
Issue #1218234. Initial patch by Berker Peksag.
parent
f8b44a4f
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
46 additions
and
6 deletions
+46
-6
Lib/inspect.py
Lib/inspect.py
+11
-5
Lib/test/test_inspect.py
Lib/test/test_inspect.py
+32
-1
Misc/NEWS
Misc/NEWS
+3
-0
No files found.
Lib/inspect.py
View file @
7de29687
...
...
@@ -652,11 +652,17 @@ def findsource(object):
in the file and the line number indexes a line in that list. An OSError
is raised if the source code cannot be retrieved."""
file
=
getfile
(
object
)
sourcefile
=
getsourcefile
(
object
)
if
not
sourcefile
and
file
[:
1
]
+
file
[
-
1
:]
!=
'<>'
:
raise
OSError
(
'source code not available'
)
file
=
sourcefile
if
sourcefile
else
file
file
=
getsourcefile
(
object
)
if
file
:
# Invalidate cache if needed.
linecache
.
checkcache
(
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
)
if
module
:
...
...
Lib/test/test_inspect.py
View file @
7de29687
...
...
@@ -12,6 +12,7 @@ import re
import
shutil
import
sys
import
types
import
textwrap
import
unicodedata
import
unittest
import
unittest.mock
...
...
@@ -27,6 +28,8 @@ from test.script_helper import assert_python_ok, assert_python_failure
from
test
import
inspect_fodder
as
mod
from
test
import
inspect_fodder2
as
mod2
from
test.test_import
import
_ready_to_import
# Functions tested in this suite:
# ismodule, isclass, ismethod, isfunction, istraceback, isframe, iscode,
...
...
@@ -3087,6 +3090,34 @@ class TestMain(unittest.TestCase):
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
():
...
...
@@ -3097,7 +3128,7 @@ def test_main():
TestGetcallargsUnboundMethods
,
TestGetattrStatic
,
TestGetGeneratorState
,
TestNoEOL
,
TestSignatureObject
,
TestSignatureBind
,
TestParameterObject
,
TestBoundArguments
,
TestSignaturePrivateHelpers
,
TestGetClosureVars
,
TestUnwrap
,
TestMain
TestUnwrap
,
TestMain
,
TestReload
)
if
__name__
==
"__main__"
:
...
...
Misc/NEWS
View file @
7de29687
...
...
@@ -39,6 +39,9 @@ Core and Builtins
Library
-------
- Issue #1218234: Fix inspect.getsource() to load updated source of
reloaded module. Initial patch by Berker Peksag.
- Issue #22959: In the constructor of http.client.HTTPSConnection, prefer the
context'
s
check_hostname
attribute
over
the
*
check_hostname
*
parameter
.
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment