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
c113c24e
Commit
c113c24e
authored
Mar 02, 2001
by
Ka-Ping Yee
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Clarify the purpose of getsourcefile().
Add getabsfile() for getting a most-normalized path.
parent
0a8c29be
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
19 additions
and
12 deletions
+19
-12
Lib/inspect.py
Lib/inspect.py
+19
-12
No files found.
Lib/inspect.py
View file @
c113c24e
...
...
@@ -169,7 +169,7 @@ def getdoc(object):
return
string
.
join
(
lines
,
'
\
n
'
)
def
getfile
(
object
):
"""
Try to guess which (text or binary)
file an object was defined in."""
"""
Work out which source or compiled
file an object was defined in."""
if
ismodule
(
object
):
if
hasattr
(
object
,
'__file__'
):
return
object
.
__file__
...
...
@@ -192,6 +192,21 @@ def getfile(object):
raise
TypeError
,
'arg is not a module, class, method, '
\
'function, traceback, frame, or code object'
def
getsourcefile
(
object
):
"""Return the Python source file an object was defined in, if it exists."""
filename
=
getfile
(
object
)
if
string
.
lower
(
filename
[
-
4
:])
in
[
'.pyc'
,
'.pyo'
]:
filename
=
filename
[:
-
4
]
+
'.py'
if
string
.
lower
(
filename
[
-
3
:])
==
'.py'
and
os
.
path
.
exists
(
filename
):
return
filename
def
getabsfile
(
object
):
"""Return an absolute path to the source file or compiled file for an object.
The idea is for each object to have a unique origin, so this routine normalizes
the result as much as possible."""
return
os
.
path
.
normcase
(
os
.
path
.
abspath
(
getsourcefile
(
object
)
or
getfile
(
object
)))
modulesbyfile
=
{}
def
getmodule
(
object
):
...
...
@@ -199,15 +214,14 @@ def getmodule(object):
if
isclass
(
object
):
return
sys
.
modules
.
get
(
object
.
__module__
)
try
:
file
=
os
.
path
.
abspath
(
getsourcefile
(
object
)
)
file
=
getabsfile
(
object
)
except
TypeError
:
return
None
if
modulesbyfile
.
has_key
(
file
):
return
sys
.
modules
[
modulesbyfile
[
file
]]
for
module
in
sys
.
modules
.
values
():
if
hasattr
(
module
,
'__file__'
):
modulesbyfile
[
os
.
path
.
abspath
(
getsourcefile
(
module
))]
=
module
.
__name__
modulesbyfile
[
getabsfile
(
module
)]
=
module
.
__name__
if
modulesbyfile
.
has_key
(
file
):
return
sys
.
modules
[
modulesbyfile
[
file
]]
main
=
sys
.
modules
[
'__main__'
]
...
...
@@ -221,13 +235,6 @@ def getmodule(object):
if
builtinobject
is
object
:
return
builtin
except
AttributeError
:
pass
def
getsourcefile
(
object
):
"""Try to guess which Python source file an object was defined in."""
filename
=
getfile
(
object
)
if
filename
[
-
4
:]
==
'.pyc'
:
filename
=
filename
[:
-
4
]
+
'.py'
return
filename
def
findsource
(
object
):
"""Return the entire source file and starting line number for an object.
...
...
@@ -571,7 +578,7 @@ def getframeinfo(frame, context=1):
start
=
min
(
start
,
len
(
lines
)
-
context
)
lines
=
lines
[
start
:
start
+
context
]
index
=
lineno
-
1
-
start
except
:
except
IOError
:
lines
=
index
=
None
else
:
lines
=
index
=
None
...
...
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