Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c1a6836

Browse files
committed
Issue #10193: Simplified instrospection used by turtle module
1 parent 7424dd3 commit c1a6836

1 file changed

Lines changed: 25 additions & 20 deletions

File tree

Lib/turtle.py

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@
109109
import math
110110
import time
111111
import os
112+
import inspect
112113

113114
from os.path import isfile, split, join
114115
from copy import deepcopy
@@ -3889,31 +3890,35 @@ def read_docstrings(lang):
38893890

38903891

38913892
def getmethparlist(ob):
3892-
"Get strings describing the arguments for the given object"
3893-
argText1 = argText2 = ""
3893+
"""Get strings describing the arguments for the given object
3894+
3895+
Returns a pair of strings representing function parameter lists
3896+
including parenthesis. The first string is suitable for use in
3897+
function definition and the second is suitable for use in function
3898+
call. The "self" parameter is not included.
3899+
"""
3900+
defText = callText = ""
38943901
# bit of a hack for methods - turn it into a function
38953902
# but we drop the "self" param.
38963903
# Try and build one for Python defined functions
3897-
argOffset = 1
3898-
counter = ob.__code__.co_argcount
3899-
items2 = list(ob.__code__.co_varnames[argOffset:counter])
3900-
realArgs = ob.__code__.co_varnames[argOffset:counter]
3904+
args, varargs, varkw = inspect.getargs(ob.__code__)
3905+
items2 = args[1:]
3906+
realArgs = args[1:]
39013907
defaults = ob.__defaults__ or []
3902-
defaults = list(map(lambda name: "=%s" % repr(name), defaults))
3908+
defaults = ["=%r" % (value,) for value in defaults]
39033909
defaults = [""] * (len(realArgs)-len(defaults)) + defaults
3904-
items1 = list(map(lambda arg, dflt: arg+dflt, realArgs, defaults))
3905-
if ob.__code__.co_flags & 0x4:
3906-
items1.append("*"+ob.__code__.co_varnames[counter])
3907-
items2.append("*"+ob.__code__.co_varnames[counter])
3908-
counter += 1
3909-
if ob.__code__.co_flags & 0x8:
3910-
items1.append("**"+ob.__code__.co_varnames[counter])
3911-
items2.append("**"+ob.__code__.co_varnames[counter])
3912-
argText1 = ", ".join(items1)
3913-
argText1 = "(%s)" % argText1
3914-
argText2 = ", ".join(items2)
3915-
argText2 = "(%s)" % argText2
3916-
return argText1, argText2
3910+
items1 = [arg + dflt for arg, dflt in zip(realArgs, defaults)]
3911+
if varargs is not None:
3912+
items1.append("*" + varargs)
3913+
items2.append("*" + varargs)
3914+
if varkw is not None:
3915+
items1.append("**" + varkw)
3916+
items2.append("**" + varkw)
3917+
defText = ", ".join(items1)
3918+
defText = "(%s)" % defText
3919+
callText = ", ".join(items2)
3920+
callText = "(%s)" % callText
3921+
return defText, callText
39173922

39183923
def _turtle_docrevise(docstr):
39193924
"""To reduce docstrings from RawTurtle class for functions

0 commit comments

Comments
 (0)