[Python-Dev] Help with changes in stack from 2.7 to 3.x
Andrew Konstantaras
akonsta at icloud.com
Sat Apr 26 05:11:58 CEST 2014
I wrote the following code that works in Python 2.7 that takes the variables passed to the function into a dictionary. The following call:
strA = 'a'
intA = 1
dctA = makeDict(strA, intA)
produces the following dictionary:
{'strA':'a', 'intA':1}
To access the names passed into the function, I had to find the information by parsing through the stack. The code that used to work is:
from traceback import extract_stack
def makeDict(*args):
strAllStack = str(extract_stack())
intNumLevels = len(extract_stack())
intLevel = 0
blnFinished = False
while not blnFinished:
strStack = str(extract_stack()[intLevel])
if strStack.find("makeDict(")>0:
blnFinished = True
intLevel += 1
if intLevel >= intNumLevels:
blnFinished = True
strStartText = "= makeDict("
intLen = len(strStartText)
intOpenParenLoc = strStack.find(strStartText)
intCloseParenLoc = strStack.find(")", intOpenParenLoc)
strArgs = strStack[ intOpenParenLoc+intLen : intCloseParenLoc ].strip()
lstVarNames = strArgs.split(",")
lstVarNames = [ s.strip() for s in lstVarNames ]
if len(lstVarNames) == len(args):
tplArgs = map(None, lstVarNames, args)
newDict = dict(tplArgs)
return newDict
else:
return "Error, argument name-value mismatch in function 'makeDict'. lstVarNames: " + str(lstVarNames) + "\n args: " + str(args), strAllStack
The same code does not work in Python 3.3.4. I have tried parsing through the stack information and frames and I can't find any reference to the names of the arguments passed to the function. I have tried inspecting the function and other functions in the standard modules, but I can't seem to find anything that will provide this information.
Can anyone point me in the direction to find this information? Any help is appreciated.
---Andrew
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20140426/7594d8e3/attachment.html>
More information about the Python-Dev
mailing list