Skip to content
Projeler
Gruplar
Parçacıklar
Yardım
Yükleniyor...
Oturum aç / Kaydol
Gezinmeyi değiştir
C
cpython
Proje
Proje
Ayrıntılar
Etkinlik
Cycle Analytics
Depo (repository)
Depo (repository)
Dosyalar
Kayıtlar (commit)
Dallar (branch)
Etiketler
Katkıda bulunanlar
Grafik
Karşılaştır
Grafikler
Konular (issue)
0
Konular (issue)
0
Liste
Pano
Etiketler
Kilometre Taşları
Birleştirme (merge) Talepleri
0
Birleştirme (merge) Talepleri
0
CI / CD
CI / CD
İş akışları (pipeline)
İşler
Zamanlamalar
Grafikler
Paketler
Paketler
Wiki
Wiki
Parçacıklar
Parçacıklar
Üyeler
Üyeler
Collapse sidebar
Close sidebar
Etkinlik
Grafik
Grafikler
Yeni bir konu (issue) oluştur
İşler
Kayıtlar (commit)
Konu (issue) Panoları
Kenar çubuğunu aç
Batuhan Osman TASKAYA
cpython
Commits
62f19e42
Kaydet (Commit)
62f19e42
authored
Eyl 08, 2006
tarafından
Nick Coghlan
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
Backport inspect.py fix from rev 51803
üst
8de403a5
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
31 additions
and
2 deletions
+31
-2
inspect.py
Lib/inspect.py
+17
-2
test_inspect.py
Lib/test/test_inspect.py
+11
-0
NEWS
Misc/NEWS
+3
-0
No files found.
Lib/inspect.py
Dosyayı görüntüle @
62f19e42
...
...
@@ -403,6 +403,7 @@ def getabsfile(object, _filename=None):
return
os
.
path
.
normcase
(
os
.
path
.
abspath
(
_filename
))
modulesbyfile
=
{}
_filesbymodname
=
{}
def
getmodule
(
object
,
_filename
=
None
):
"""Return the module an object was defined in, or None if not found."""
...
...
@@ -410,19 +411,32 @@ def getmodule(object, _filename=None):
return
object
if
hasattr
(
object
,
'__module__'
):
return
sys
.
modules
.
get
(
object
.
__module__
)
# Try the filename to modulename cache
if
_filename
is
not
None
and
_filename
in
modulesbyfile
:
return
sys
.
modules
.
get
(
modulesbyfile
[
_filename
])
# Try the cache again with the absolute file name
try
:
file
=
getabsfile
(
object
,
_filename
)
except
TypeError
:
return
None
if
file
in
modulesbyfile
:
return
sys
.
modules
.
get
(
modulesbyfile
[
file
])
for
module
in
sys
.
modules
.
values
():
# Update the filename to module name cache and check yet again
# Copy sys.modules in order to cope with changes while iterating
for
modname
,
module
in
sys
.
modules
.
items
():
if
ismodule
(
module
)
and
hasattr
(
module
,
'__file__'
):
f
=
module
.
__file__
if
f
==
_filesbymodname
.
get
(
modname
,
None
):
# Have already mapped this module, so skip it
continue
_filesbymodname
[
modname
]
=
f
f
=
getabsfile
(
module
)
# Always map to the name the module knows itself by
modulesbyfile
[
f
]
=
modulesbyfile
[
os
.
path
.
realpath
(
f
)]
=
module
.
__name__
if
file
in
modulesbyfile
:
return
sys
.
modules
.
get
(
modulesbyfile
[
file
])
# Check the main module
main
=
sys
.
modules
[
'__main__'
]
if
not
hasattr
(
object
,
'__name__'
):
return
None
...
...
@@ -430,6 +444,7 @@ def getmodule(object, _filename=None):
mainobject
=
getattr
(
main
,
object
.
__name__
)
if
mainobject
is
object
:
return
main
# Check builtins
builtin
=
sys
.
modules
[
'__builtin__'
]
if
hasattr
(
builtin
,
object
.
__name__
):
builtinobject
=
getattr
(
builtin
,
object
.
__name__
)
...
...
@@ -444,7 +459,7 @@ def findsource(object):
in the file and the line number indexes a line in that list. An IOError
is raised if the source code cannot be retrieved."""
file
=
getsourcefile
(
object
)
or
getfile
(
object
)
module
=
getmodule
(
object
)
module
=
getmodule
(
object
,
file
)
if
module
:
lines
=
linecache
.
getlines
(
file
,
module
.
__dict__
)
else
:
...
...
Lib/test/test_inspect.py
Dosyayı görüntüle @
62f19e42
...
...
@@ -178,7 +178,18 @@ class TestRetrievingSourceCode(GetSourceBase):
self
.
assertEqual
(
inspect
.
getcomments
(
mod
.
StupidGit
),
'# line 20
\n
'
)
def
test_getmodule
(
self
):
# Check actual module
self
.
assertEqual
(
inspect
.
getmodule
(
mod
),
mod
)
# Check class (uses __module__ attribute)
self
.
assertEqual
(
inspect
.
getmodule
(
mod
.
StupidGit
),
mod
)
# Check a method (no __module__ attribute, falls back to filename)
self
.
assertEqual
(
inspect
.
getmodule
(
mod
.
StupidGit
.
abuse
),
mod
)
# Do it again (check the caching isn't broken)
self
.
assertEqual
(
inspect
.
getmodule
(
mod
.
StupidGit
.
abuse
),
mod
)
# Check a builtin
self
.
assertEqual
(
inspect
.
getmodule
(
str
),
sys
.
modules
[
"__builtin__"
])
# Check filename override
self
.
assertEqual
(
inspect
.
getmodule
(
None
,
modfile
),
mod
)
def
test_getsource
(
self
):
self
.
assertSourceEqual
(
git
.
abuse
,
29
,
39
)
...
...
Misc/NEWS
Dosyayı görüntüle @
62f19e42
...
...
@@ -46,6 +46,9 @@ Core and builtins
Library
-------
-
Patch
#
1553314
:
Fix
the
inspect
.
py
slowdown
that
was
hurting
IPython
&
SAGE
by
adding
smarter
caching
in
inspect
.
getmodule
()
-
Fix
missing
import
of
the
types
module
in
logging
.
config
.
-
Patch
#
1550886
:
Fix
decimal
module
context
management
implementation
...
...
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