[Python-Dev] Help with changes in stack from 2.7 to 3.x

2014-04-25 Thread Andrew Konstantaras

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

___
Python-Dev mailing list
Python-Dev@python.org
https://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
https://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com


Re: [Python-Dev] Help with changes in stack from 2.7 to 3.x

2014-04-25 Thread Chris Angelico
On Sat, Apr 26, 2014 at 1:11 PM, Andrew Konstantaras akon...@icloud.com wrote:
 Can anyone point me in the direction to find this information?  Any help is
 appreciated.

I'd recommend python-list rather than python-dev (the latter is for
the development *of* Python, rather than development *with* Python).
But to be quite honest, I don't think you should be doing this at all.
It's extremely odd for this function to even work, and most users will
not expect that renaming a local variable will materially change
anything. Fiddling around with stack backtraces in string form and
using those to find the source code line and fetching parameter names?
Definitely weird, definitely not recommended, and not at all
surprising that it broke between versions.

ChrisA
___
Python-Dev mailing list
Python-Dev@python.org
https://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
https://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com