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
152b3c21
Commit
152b3c21
authored
Aug 30, 2007
by
Kurt B. Kaiser
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Use inspect.py and clean up.
parent
f40080f2
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
61 additions
and
77 deletions
+61
-77
Lib/idlelib/CallTips.py
Lib/idlelib/CallTips.py
+61
-77
No files found.
Lib/idlelib/CallTips.py
View file @
152b3c21
...
...
@@ -8,6 +8,7 @@ which disappear when you type a closing parenthesis.
import
re
import
sys
import
types
import
inspect
from
idlelib
import
CallTipWindow
from
idlelib.HyperParser
import
HyperParser
...
...
@@ -25,42 +26,37 @@ class CallTips:
def
__init__
(
self
,
editwin
=
None
):
if
editwin
is
None
:
# subprocess and test
self
.
editwin
=
None
return
self
.
editwin
=
editwin
self
.
text
=
editwin
.
text
self
.
calltip
=
None
self
.
_make
_calltip_window
=
self
.
_make_tk_calltip_window
else
:
self
.
editwin
=
editwin
self
.
text
=
editwin
.
text
self
.
active_
calltip
=
None
self
.
_calltip_window
=
self
.
_make_tk_calltip_window
def
close
(
self
):
self
.
_
make_
calltip_window
=
None
self
.
_calltip_window
=
None
def
_make_tk_calltip_window
(
self
):
# See __init__ for usage
return
CallTipWindow
.
CallTip
(
self
.
text
)
def
_remove_calltip_window
(
self
,
event
=
None
):
if
self
.
calltip
:
self
.
calltip
.
hidetip
()
self
.
calltip
=
None
if
self
.
active_
calltip
:
self
.
active_
calltip
.
hidetip
()
self
.
active_
calltip
=
None
def
force_open_calltip_event
(
self
,
event
):
"""Happens when the user really wants to open a CallTip, even if a
function call is needed.
"""
"The user selected the menu entry or hotkey, open the tip."
self
.
open_calltip
(
True
)
def
try_open_calltip_event
(
self
,
event
):
"""Happens when it would be nice to open a CallTip, but not really
nec
ce
sary, for example after an opening bracket, so function calls
nec
es
sary, for example after an opening bracket, so function calls
won't be made.
"""
self
.
open_calltip
(
False
)
def
refresh_calltip_event
(
self
,
event
):
"""If there is already a calltip window, check if it is still needed,
and if so, reload it.
"""
if
self
.
calltip
and
self
.
calltip
.
is_active
():
if
self
.
active_calltip
and
self
.
active_calltip
.
is_active
():
self
.
open_calltip
(
False
)
def
open_calltip
(
self
,
evalfuncs
):
...
...
@@ -72,20 +68,22 @@ class CallTips:
return
hp
.
set_index
(
sur_paren
[
0
])
name
=
hp
.
get_expression
()
if
not
name
or
(
not
evalfuncs
and
name
.
find
(
'('
)
!=
-
1
):
if
not
name
:
return
if
not
evalfuncs
and
(
name
.
find
(
'('
)
!=
-
1
):
return
arg
_text
=
self
.
fetch_tip
(
name
)
if
not
arg
_text
:
arg
spec
=
self
.
fetch_tip
(
name
)
if
not
arg
spec
:
return
self
.
calltip
=
self
.
_make
_calltip_window
()
self
.
calltip
.
showtip
(
arg_text
,
sur_paren
[
0
],
sur_paren
[
1
])
self
.
active_calltip
=
self
.
_calltip_window
()
self
.
active_calltip
.
showtip
(
argspec
,
sur_paren
[
0
],
sur_paren
[
1
])
def
fetch_tip
(
self
,
name
):
"""Return the argument list and docstring of a function or class
"""Return the argument list and docstring of a function or class
.
If there is a Python subprocess, get the calltip there. Otherwise,
either
fetch_tip() is running in the subprocess itself or it was called
in an IDLE EditorWindow before any script had been run
.
either
this fetch_tip() is running in the subprocess or it was
called in an IDLE running without the subprocess
.
The subprocess environment is that of the most recently run script. If
two unrelated modules are being edited some calltips in the current
...
...
@@ -103,10 +101,10 @@ class CallTips:
(
name
,),
{})
else
:
entity
=
self
.
get_entity
(
name
)
return
get_arg
_text
(
entity
)
return
get_arg
spec
(
entity
)
def
get_entity
(
self
,
name
):
"Lookup name in a namespace spanning sys.modules and __main.dict__"
"Lookup name in a namespace spanning sys.modules and __main.dict__
.
"
if
name
:
namespace
=
sys
.
modules
.
copy
()
namespace
.
update
(
__main__
.
__dict__
)
...
...
@@ -116,83 +114,66 @@ class CallTips:
return
None
def
_find_constructor
(
class_ob
):
# Given a class object, return a function object used for the
# constructor (ie, __init__() ) or None if we can't find one.
"Find the nearest __init__() in the class tree."
try
:
return
class_ob
.
__init__
.
im_func
except
AttributeError
:
for
base
in
class_ob
.
__bases__
:
rc
=
_find_constructor
(
base
)
if
rc
is
not
None
:
return
rc
return
None
def
get_arg_text
(
ob
):
"""Get a string describing the arguments for the given object"""
arg_text
=
""
init
=
_find_constructor
(
base
)
if
init
:
return
init
return
None
def
get_argspec
(
ob
):
"""Get a string describing the arguments for the given object."""
argspec
=
""
if
ob
is
not
None
:
arg_offset
=
0
if
isinstance
(
ob
,
type
):
# Look for the highest __init__ in the class chain.
fob
=
_find_constructor
(
ob
)
if
fob
is
None
:
fob
=
lambda
:
None
else
:
arg_offset
=
1
elif
isinstance
(
ob
,
types
.
MethodType
):
# bit of a hack for methods - turn it into a function
# but we drop the "self" param.
fob
=
ob
.
im_func
arg_offset
=
1
else
:
fob
=
ob
# Try to build one for Python defined functions
if
isinstance
(
fob
,
(
types
.
FunctionType
,
types
.
LambdaType
)):
argcount
=
fob
.
__code__
.
co_argcount
real_args
=
fob
.
__code__
.
co_varnames
[
arg_offset
:
argcount
]
defaults
=
fob
.
__defaults__
or
[]
defaults
=
list
(
map
(
lambda
name
:
"=%s"
%
repr
(
name
),
defaults
))
defaults
=
[
""
]
*
(
len
(
real_args
)
-
len
(
defaults
))
+
defaults
items
=
list
(
map
(
lambda
arg
,
dflt
:
arg
+
dflt
,
real_args
,
defaults
))
if
fob
.
__code__
.
co_flags
&
0x4
:
items
.
append
(
"..."
)
if
fob
.
__code__
.
co_flags
&
0x8
:
items
.
append
(
"***"
)
arg_text
=
", "
.
join
(
items
)
arg_text
=
"(%s)"
%
re
.
sub
(
"
\
.
\
d+"
,
"<tuple>"
,
arg_text
)
# See if we can use the docstring
argspec
=
inspect
.
formatargspec
(
*
inspect
.
getfullargspec
(
fob
))
pat
=
re
.
compile
(
'self
\
,?
\
s*'
)
argspec
=
pat
.
sub
(
""
,
argspec
)
doc
=
getattr
(
ob
,
"__doc__"
,
""
)
if
doc
:
doc
=
doc
.
lstrip
()
pos
=
doc
.
find
(
"
\
n
"
)
if
pos
<
0
or
pos
>
70
:
pos
=
70
if
arg
_text
:
arg
_text
+=
"
\
n
"
arg
_text
+=
doc
[:
pos
]
return
arg
_text
if
arg
spec
:
arg
spec
+=
"
\
n
"
arg
spec
+=
doc
[:
pos
]
return
arg
spec
#################################################
#
# Test code
#
if
__name__
==
'__main__'
:
def
main
():
def
t1
():
"()"
def
t2
(
a
,
b
=
None
):
"(a, b=None)"
def
t3
(
a
,
*
args
):
"(a,
...
)"
def
t4
(
*
args
):
"(
...
)"
def
t5
(
a
,
*
args
):
"(a,
...
)"
def
t6
(
a
,
b
=
None
,
*
args
,
**
kw
):
"(a, b=None,
..., ***
)"
def
t3
(
a
,
*
args
):
"(a,
*args
)"
def
t4
(
*
args
):
"(
*args
)"
def
t5
(
a
,
*
args
):
"(a,
*args
)"
def
t6
(
a
,
b
=
None
,
*
args
,
**
kw
):
"(a, b=None,
*args, **kw
)"
class
TC
(
object
):
"(ai=None,
...
)"
def
__init__
(
self
,
ai
=
None
,
*
b
):
"(ai=None,
...
)"
"(ai=None,
*b
)"
def
__init__
(
self
,
ai
=
None
,
*
b
):
"(ai=None,
*b
)"
def
t1
(
self
):
"()"
def
t2
(
self
,
ai
,
b
=
None
):
"(ai, b=None)"
def
t3
(
self
,
ai
,
*
args
):
"(ai, ...)"
def
t4
(
self
,
*
args
):
"(...)"
def
t5
(
self
,
ai
,
*
args
):
"(ai, ...)"
def
t6
(
self
,
ai
,
b
=
None
,
*
args
,
**
kw
):
"(ai, b=None, ..., ***)"
def
t3
(
self
,
ai
,
*
args
):
"(ai, *args)"
def
t4
(
self
,
*
args
):
"(*args)"
def
t5
(
self
,
ai
,
*
args
):
"(ai, *args)"
def
t6
(
self
,
ai
,
b
=
None
,
*
args
,
**
kw
):
"(ai, b=None, *args, **kw)"
__main__
.
__dict__
.
update
(
locals
())
def
test
(
tests
):
ct
=
CallTips
()
...
...
@@ -200,16 +181,16 @@ if __name__=='__main__':
for
t
in
tests
:
expected
=
t
.
__doc__
+
"
\
n
"
+
t
.
__doc__
name
=
t
.
__name__
# exercise fetch_tip(), not just get_arg
_text
()
# exercise fetch_tip(), not just get_arg
spec
()
try
:
qualified_name
=
"%s.%s"
%
(
t
.
im_class
.
__name__
,
name
)
except
AttributeError
:
qualified_name
=
name
arg
_text
=
ct
.
fetch_tip
(
qualified_name
)
if
arg
_text
!=
expected
:
arg
spec
=
ct
.
fetch_tip
(
qualified_name
)
if
arg
spec
!=
expected
:
failed
.
append
(
t
)
fmt
=
"%s - expected %s, but got %s"
print
(
fmt
%
(
t
.
__name__
,
expected
,
get_arg
_text
(
t
)))
print
(
fmt
%
(
t
.
__name__
,
expected
,
get_arg
spec
(
t
)))
print
(
"%d of %d tests failed"
%
(
len
(
failed
),
len
(
tests
)))
tc
=
TC
()
...
...
@@ -217,3 +198,6 @@ if __name__=='__main__':
TC
,
tc
.
t1
,
tc
.
t2
,
tc
.
t3
,
tc
.
t4
,
tc
.
t5
,
tc
.
t6
)
test
(
tests
)
if
__name__
==
'__main__'
:
main
()
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