<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:p="urn:schemas-microsoft-com:office:powerpoint" xmlns:a="urn:schemas-microsoft-com:office:access" xmlns:dt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882" xmlns:s="uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882" xmlns:rs="urn:schemas-microsoft-com:rowset" xmlns:z="#RowsetSchema" xmlns:b="urn:schemas-microsoft-com:office:publisher" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" xmlns:c="urn:schemas-microsoft-com:office:component:spreadsheet" xmlns:odc="urn:schemas-microsoft-com:office:odc" xmlns:oa="urn:schemas-microsoft-com:office:activation" xmlns:html="http://www.w3.org/TR/REC-html40" xmlns:q="http://schemas.xmlsoap.org/soap/envelope/" xmlns:D="DAV:" xmlns:x2="http://schemas.microsoft.com/office/excel/2003/xml" xmlns:ois="http://schemas.microsoft.com/sharepoint/soap/ois/" xmlns:dir="http://schemas.microsoft.com/sharepoint/soap/directory/" xmlns:ds="http://www.w3.org/2000/09/xmldsig#" xmlns:dsp="http://schemas.microsoft.com/sharepoint/dsp" xmlns:udc="http://schemas.microsoft.com/data/udc" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:sub="http://schemas.microsoft.com/sharepoint/soap/2002/1/alerts/" xmlns:ec="http://www.w3.org/2001/04/xmlenc#" xmlns:sp="http://schemas.microsoft.com/sharepoint/" xmlns:sps="http://schemas.microsoft.com/sharepoint/soap/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:udcxf="http://schemas.microsoft.com/data/udc/xmlfile" xmlns:wf="http://schemas.microsoft.com/sharepoint/soap/workflow/" xmlns:mver="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns:mrels="http://schemas.openxmlformats.org/package/2006/relationships" xmlns:ex12t="http://schemas.microsoft.com/exchange/services/2006/types" xmlns:ex12m="http://schemas.microsoft.com/exchange/services/2006/messages" xmlns:Z="urn:schemas-microsoft-com:" xmlns:st="&#1;" xmlns="http://www.w3.org/TR/REC-html40">

<head>
<meta http-equiv=Content-Type content="text/html; charset=utf-8">
<meta name=Generator content="Microsoft Word 12 (filtered medium)">
<style>
<!--
 /* Font Definitions */
 @font-face
        {font-family:"Cambria Math";
        panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
        {font-family:Calibri;
        panose-1:2 15 5 2 2 2 4 3 2 4;}
@font-face
        {font-family:Tahoma;
        panose-1:2 11 6 4 3 5 4 4 2 4;}
 /* Style Definitions */
 p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0in;
        margin-bottom:.0001pt;
        font-size:12.0pt;
        font-family:"Times New Roman","serif";}
a:link, span.MsoHyperlink
        {mso-style-priority:99;
        color:blue;
        text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
        {mso-style-priority:99;
        color:purple;
        text-decoration:underline;}
span.EmailStyle17
        {mso-style-type:personal-reply;
        font-family:"Calibri","sans-serif";
        color:#1F497D;}
.MsoChpDefault
        {mso-style-type:export-only;}
@page Section1
        {size:8.5in 11.0in;
        margin:1.0in 1.0in 1.0in 1.0in;}
div.Section1
        {page:Section1;}
-->
</style>
<!--[if gte mso 9]><xml>
 <o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
 <o:shapelayout v:ext="edit">
  <o:idmap v:ext="edit" data="1" />
 </o:shapelayout></xml><![endif]-->
</head>

<body lang=EN-US link=blue vlink=purple>

<div class=Section1>

<p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";
color:#1F497D'>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.<o:p></o:p></span></p>

<p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";
color:#1F497D'><o:p>&nbsp;</o:p></span></p>

<p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";
color:#1F497D'>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).<o:p></o:p></span></p>

<p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";
color:#1F497D'><o:p>&nbsp;</o:p></span></p>

<div style='border:none;border-top:solid #B5C4DF 1.0pt;padding:3.0pt 0in 0in 0in'>

<p class=MsoNormal><b><span style='font-size:10.0pt;font-family:"Tahoma","sans-serif"'>From:</span></b><span
style='font-size:10.0pt;font-family:"Tahoma","sans-serif"'> users-bounces@lists.ironpython.com
[mailto:users-bounces@lists.ironpython.com] <b>On Behalf Of </b>Ori Peleg<br>
<b>Sent:</b> Thursday, November 06, 2008 2:27 PM<br>
<b>To:</b> users@lists.ironpython.com<br>
<b>Subject:</b> [IronPython] Porting from CPython: Figuring out the calling
code's directory<o:p></o:p></span></p>

</div>

<p class=MsoNormal><o:p>&nbsp;</o:p></p>

<div>

<div>

<div>

<p class=MsoNormal>Hi,<br>
<br>
I'm porting Testoob to IronPython and have a module function that needs to know
the filename where the code calling it is defined.<br>
<br>
In CPython I get the current frame with sys._getframe(), then climb<br>
the stack with frame.f_back until I find a different file:<br>
<br>
def _first_external_frame():<br>
&nbsp; &nbsp;import sys<br>
<br>
&nbsp; &nbsp;# find the first frame with a filename different than this one<br>
&nbsp; &nbsp;frame = sys._getframe()<br>
&nbsp; &nbsp;while frame.f_globals[&quot;__file__&quot;] == __file__:<br>
&nbsp; &nbsp; &nbsp; &nbsp;frame = frame.f_back<br>
<br>
&nbsp; &nbsp;return frame<br>
<br>
def _calling_module_directory():<br>
&nbsp; &nbsp;from os.path import dirname, normpath<br>
&nbsp; &nbsp;return
normpath(dirname(_first_external_frame().f_globals[&quot;__file__&quot;]))<o:p></o:p></p>

<div>

<p class=MsoNormal><br>
I know I can't climb the stack in IronPython, but is there another way<br>
to do it? Maybe some available .NET assembly metadata?<br>
<br>
BTW - the use case is for building test suites: you can have a<br>
subpackage of tests and define __init__.py like this:<br>
<br>
def suite():<br>
&nbsp; &nbsp;import testoob<br>
&nbsp; &nbsp;return
testoob.collecting.collect_from_files(&quot;test_*.py&quot;)<br>
<br>
Thanks,<br>
<span style='color:#888888'>orip.</span><o:p></o:p></p>

</div>

</div>

</div>

<p class=MsoNormal><br>
-- <br>
Check out my blog: <a href="http://orip.org">http://orip.org</a><o:p></o:p></p>

</div>

</div>

</body>

</html>