Is crawling the stack "bad"? Why?

Carl Friedrich Bolz cfbolz at gmx.de
Wed Feb 27 00:25:07 CET 2008


Paul Rubin wrote:
> Russell Warren <russandheather at gmail.com> writes:
>> That is exactly where I started (creating my own request handler,
>> snagging the IP address and stashing it), but I couldn't come up with
>> a stash location that would work for a threaded server.
> 
> How about a dictionary indexed by by the thread name.  It's pretty
> lame, though, that the rpc server module itself doesn't make the
> request available to the rpc responder.  Maybe you should submit a
> patch.
> 
>> My biggest specific fear at the moment is that sys._frame will do
>> funky things with multiple threads,
> 
> You should not rely on anything that implementation specific at all.
> What happens if you want to switch to pypy?

Apart from the fact that the idea of walking the stack to get info is
indeed rather crazy, PyPy supports sys._getframe and friends perfectly
fine (I think even Jython does, but I am not quite sure). In general
PyPy tries to implement all these "internals" of CPython as closely as
it is sane to do so. Stuff like inspecting code, function, frame, method
objects is very closely mirrored but of course small differences exist:


cfbolz at gauss:~$ python
Python 2.5.1 (r251:54863, Oct  5 2007, 13:36:32)
[GCC 4.1.3 20070929 (prerelease) (Ubuntu 4.1.2-16ubuntu2)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> dir(dir)
['__call__', '__class__', '__cmp__', '__delattr__', '__doc__',
'__getattribute__', '__hash__', '__init__', '__module__', '__name__',
'__new__', '__reduce__', '__reduce_ex__', '__repr__', '__self__',
'__setattr__', '__str__']


cfbolz at gauss:~$ pypy-c-46371-faassen
Python 2.4.1 (pypy 1.0.0 build 46371) on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>> dir(dir)
['__call__', '__class__', '__delattr__', '__dict__', '__doc__',
'__getattribute__', '__hash__', '__init__', '__module__', '__name__',
'__new__', '__reduce__', '__reduce_ex__', '__repr__', '__self__',
'__setattr__', '__setstate__', '__str__', '__weakref__', 'func_closure',
'func_code', 'func_defaults', 'func_dict', 'func_doc', 'func_globals',
'func_name']
>>>> dir.func_code
<internal-code object at 0x0826e460>
>>>> dir.func_name
'dir'


Cheers,

Carl Friedrich Bolz




More information about the Python-list mailing list