Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > using inspect

Reply
Thread Tools

using inspect

 
 
Ethan Furman
Guest
Posts: n/a
 
      11-12-2009
Greetings!

How wise is it to base code on inspect? Specifically on things like
live frames on the stack and whatnot. It occurs to me that this is
leaning towards implementation details, and away from pure, pristine Python.

As an example, I have this routine in a module I'm working on:

def _get_module():
"get the calling module -- should be the config'ed module"
target = os.path.splitext(inspect.stack()[2][1])[0]
target = __import__(target)
return target

How brittle is this technique?

~Ethan~
 
Reply With Quote
 
 
 
 
Steven D'Aprano
Guest
Posts: n/a
 
      11-12-2009
On Wed, 11 Nov 2009 18:15:22 -0800, Ethan Furman wrote:

> Greetings!
>
> How wise is it to base code on inspect? Specifically on things like
> live frames on the stack and whatnot. It occurs to me that this is
> leaning towards implementation details, and away from pure, pristine
> Python.
>
> As an example, I have this routine in a module I'm working on:
>
> def _get_module():
> "get the calling module -- should be the config'ed module" target =
> os.path.splitext(inspect.stack()[2][1])[0]
> target = __import__(target)
> return target
>
> How brittle is this technique?



Download and install PyPy, CLPython, IronPython and Jython and see how
many it breaks on


I think it's a brittle technique because it is creating a dependency
where there shouldn't be one. What happens if somebody runs your module
directly? It might not need to do anything sensible, but it shouldn't
fail merely because there's no calling module.

But I don't think it's inherently dangerous. It's a bit hacky, well,
probably a lot hacky, but it doesn't seem to use anything specifically
documented as implementation specific:

http://docs.python.org/library/inspe...#inspect.stack




--
Steven
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
inspect.stack() or inspect.currentframe() gives "list index out ofrange error" deluxstar Python 5 09-25-2010 05:12 PM
Using an ASA's AIP SSM module to inspect traffic going into andcoming out of a VPN tunnel. dnash Cisco 0 01-22-2009 05:14 PM
using inspect on pygtk Chris Pax Python 4 10-01-2007 05:05 PM
question about introspection using inspect module Benjamin Rutt Python 4 07-08-2005 04:33 AM
Re: will the pix ever inspect http inbound?? Russ Johnson Cisco 1 10-16-2003 03:41 PM



Advertisments