[IronPython] Porting from CPython: Figuring out the calling code's directory
oripel at gmail.com
Sat Nov 8 12:36:03 PST 2008
I guess I'll try a workaround.
On Nov 7, 1:24 am, Dino Viehland <di... at microsoft.com> wrote:
> The short answer is no – we realize frames is a common request and I do have a prototype implementation of them. We’re still working on our 2.1 planning but it might be available there w/ a command line option or it might be something we could port to 2.0.1.
> The longer answer would be potentially with a lot of work and it could be quite slow. You could walk every module, every function, every class and attempt to gather up all the function objects in the world. Then you could call PythonOps.FunctionGetTarget on each one of them. Finally you could do a full .NET stack trace (new System.Diagnostics.StackTrace()) and attempt to figure your caller by filtering out any IronPython/DLR frames. Finally once you have that method you could see if that method is == the .Method property of any of the delegates you got from FunctionGetTarget calls. And I’m not even 100% sure if that’d work – the delegates for a PythonFunction might have a different MethodInfo then the one in the stack trace but I’m not certain (if this is the case it’d be due to the way CLR hides DynamicMethod MethodInfo’s so only the creator has access to them – you might be able to use some hack like a name comparison heuristic or private reflection or something to bring it all together).
> From: users-boun... at lists.ironpython.com [mailto:users-boun... at lists.ironpython.com] On Behalf Of Ori Peleg
> Sent: Thursday, November 06, 2008 2:27 PM
> To: us... at lists.ironpython.com
> Subject: [IronPython] Porting from CPython: Figuring out the calling code's directory
> 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
> Users mailing list
> Us... at lists.ironpython.comhttp://lists.ironpython.com/listinfo.cgi/users-ironpython.com
More information about the Users