[IronPython] Porting from CPython: Figuring out the calling code's directory

Ori Peleg oripel at gmail.com
Thu Nov 6 23:27:05 CET 2008


I'm porting Testoob to IronPython and have a module function that needs to
know the filename where the code calling it is defined.

In CPython I get the current frame with sys._getframe(), then climb
the stack with frame.f_back until I find a different file:

def _first_external_frame():
   import sys

   # find the first frame with a filename different than this one
   frame = sys._getframe()
   while frame.f_globals["__file__"] == __file__:
       frame = frame.f_back

   return frame

def _calling_module_directory():
   from os.path import dirname, normpath
   return normpath(dirname(_first_external_frame().f_globals["__file__"]))

I know I can't climb the stack in IronPython, but is there another way
to do it? Maybe some available .NET assembly metadata?

BTW - the use case is for building test suites: you can have a
subpackage of tests and define __init__.py like this:

def suite():
   import testoob
   return testoob.collecting.collect_from_files("test_*.py")


Check out my blog: http://orip.org
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/ironpython-users/attachments/20081107/b64b13d3/attachment.html>

More information about the Ironpython-users mailing list