Path: csiph.com!usenet.pasdenom.info!dedibox.gegeweb.org!gegeweb.eu!nntpfeed.proxad.net!proxad.net!feeder1-2.proxad.net!usenet-fr.net!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed2a.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'python,': 0.02; 'else:': 0.03; 'argument': 0.05; 'subject:Python': 0.06; 'list?': 0.07; 'see:': 0.07; 'variables': 0.07; "'a'": 0.09; '34,': 0.09; 'args)': 0.09; 'arguments': 0.09; 'caller': 0.09; 'function,': 0.09; 'iterate': 0.09; 'parsing': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'stack.': 0.09; 'subject:2.7': 0.09; 'used.': 0.09; 'subject:Help': 0.11; 'python': 0.11; 'def': 0.12; 'wrote': 0.14; '2.7': 0.14; 'args:': 0.16; 'callable': 0.16; 'dictionary.': 0.16; 'exception?': 0.16; 'expecting': 0.16; 'modules,': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'subject: \n ': 0.16; 'tuple': 0.16; 'typeerror:': 0.16; 'elements': 0.16; 'wrote:': 0.18; 'stack': 0.19; 'meant': 0.20; 'code,': 0.22; 'import': 0.22; 'header:User-Agent:1': 0.23; 'error': 0.23; 'received:comcast.net': 0.24; 'simpler': 0.24; 'versions': 0.24; "haven't": 0.24; 'source': 0.25; 'skip:" 20': 0.27; 'header:X -Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; 'tried': 0.27; 'point': 0.28; 'function': 0.29; 'appreciated.': 0.29; 'am,': 0.29; 'raise': 0.29; 'andrew': 0.30; 'direction': 0.30; 'code': 0.31; 'easier': 0.31; 'information?': 0.31; 'produces': 0.31; 'anyone': 0.31; 'file': 0.32; '(most': 0.33; 'subject:from': 0.34; 'subject:with': 0.35; "can't": 0.35; 'skip:s 30': 0.35; 'but': 0.35; 'there': 0.35; 'false': 0.36; 'done': 0.36; "didn't": 0.36; 'wrong': 0.37; 'to:addr:python-list': 0.38; 'anything': 0.39; 'recent': 0.39; 'does': 0.39; 'realize': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'mentioned': 0.61; 'entire': 0.61; 'first': 0.61; 'you.': 0.62; "you'll": 0.62; 'information': 0.63; 'map': 0.64; 'provide': 0.64; 'different': 0.65; 'here': 0.66; 'repeat': 0.74; 'dict()': 0.84; 'examining': 0.84; 'fourth': 0.84; 'swimming': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Ned Batchelder Subject: Re: Help with changes in traceback stack from Python 2.7 to Python 3.x Date: Sat, 26 Apr 2014 08:10:33 -0400 References: <9fb27984-1b9e-402b-aa4d-7ca4f26e2386@me.com> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: c-50-133-228-126.hsd1.ma.comcast.net User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:24.0) Gecko/20100101 Thunderbird/24.4.0 In-Reply-To: <9fb27984-1b9e-402b-aa4d-7ca4f26e2386@me.com> X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 104 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1398514244 news.xs4all.nl 2939 [2001:888:2000:d::a6]:55214 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:70631 On 4/26/14 1:50 AM, Andrew Konstantaras wrote: > 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. 1) This is a very strange function. Instead of going to all this trouble, why not use: dctA = dict(strA=strA, intA=intA) Yes, you have to repeat the names, but you'd be done by now, it works on both versions of Python, and people reading your code would understand what it does. 2) Why is your code examining the entire stack? The frame you want is the one just above you. Why are you stringifying the tuple produced by extract_stack when the source line you want is the fourth element? Why are you using a while loop and a counter to iterate over elements of a list? 3) In the error case you return a tuple when the caller is expecting a dict? Why not raise an exception? 4) Your code only works if makeDict is assigned to a name. When I first tried it, I used "print(makeDict(...))", and it failed completely. 5) You haven't mentioned what goes wrong on Python 3, but when I tried it, I got: Traceback (most recent call last): File "foo3.py", line 34, in d = makeDict(a, b) File "foo3.py", line 27, in makeDict newDict = dict(list(tplArgs)) TypeError: 'NoneType' object is not callable Looking at your code, I see: tplArgs = map(None, lstVarNames, args) I didn't realize map accepted a callable of None (TIL!), but it no longer does in Python 3. You'll have to do this a different way. But seriously: just use dict() and be done with it. There's a lot here that can be much simpler if you learn to use Python as it was meant to be used. You are swimming upstream, there are easier ways. > > Can anyone point me in the direction to find this information? Any help > is appreciated. > > ---Andrew > > > -- Ned Batchelder, http://nedbatchelder.com