From twestley at gmail.com Sun Jul 3 14:05:17 2011 From: twestley at gmail.com (Terry Westley) Date: Sun, 3 Jul 2011 08:05:17 -0400 Subject: [capi-sig] How does CO_FUTURE_DIVISION compiler flag get propagated? Message-ID: I've built Python for the iPhone app store (not jailbreak), http://www.sabonrai.com/PythonMath/ . Like embedding Python in another app, it uses PyRun_SimpleString() to execute commands entered by the user. For evaluating expressions, it uses PyEval_EvalCode() with the dictionary from the __main__ module. My problem is that future division ("from __future__ import division") works within scripts executed by import or execfile() but not when entered interactively in the interpreter like this: >>> from __future__ import division >>> a=2/3 When you do this, you get classic (integer) division, but if you enter it as follows, you get future (float) division. >>> from __future__ import division;a=2/3 It appears that the CO_FUTURE_DIVISION compiler flag is not being retained in the interpreter so that later commands get compiled without that flag. I found a hint in comp.lang.python, but I don't see that PyRun_SimpleStringFlags returns the flags it uses. I guess I could watch for the user to enter the import command and set the flags on every subsequent call but seems really brittle to me. Thanks. Terry From egranata at apple.com Sat Jul 16 02:44:56 2011 From: egranata at apple.com (Enrico Granata) Date: Fri, 15 Jul 2011 17:44:56 -0700 Subject: [capi-sig] Code for a Python function Message-ID: <7BA13DD4-981A-4918-A439-FB8D149AAE1B@apple.com> Dear all, for my current task I have a Python interpreter embedded in a C++ program. There are several ways through which the user can interact with this interpreter, typing code into it, and possibly also def-fining functions. I need to be able to look into all defined functions, find one given its name and (here comes the hard part) read whatever code it currently contains. I looked into the API and there is a PyCodeObject class, but it is subject to change at any time and I do not want to start using code that might just change the next time the user upgrades Python. Is there any documented and portable-across-versions way to read the code into a Python function just given its name? Thanks for any help you'll be willing to provide on this Enrico Granata ? 408.974.5572 | ? egranata@?.com From python_capi at behnel.de Sat Jul 16 06:54:31 2011 From: python_capi at behnel.de (Stefan Behnel) Date: Sat, 16 Jul 2011 06:54:31 +0200 Subject: [capi-sig] Code for a Python function In-Reply-To: <7BA13DD4-981A-4918-A439-FB8D149AAE1B@apple.com> References: <7BA13DD4-981A-4918-A439-FB8D149AAE1B@apple.com> Message-ID: <4E211987.3040700@behnel.de> Enrico Granata, 16.07.2011 02:44: > for my current task I have a Python interpreter embedded in a C++ program. > There are several ways through which the user can interact with this interpreter, typing code into it, and possibly also def-fining functions. > I need to be able to look into all defined functions, find one given its name and (here comes the hard part) read whatever code it currently contains. > I looked into the API and there is a PyCodeObject class, but it is subject to change at any time and I do not want to start using code that might just change the next time the user upgrades Python. Is there any documented and portable-across-versions way to read the code into a Python function just given its name? Hi, as this question refers to Python's introspection capabilities in general, this isn't the perfect place to ask. The general Python list would be a better place. That being said, PyCodeObject doesn't actually contain the source code but the compiled byte code. You didn't state what you wanted to do with the code, so this may or may not be what you want. In fact, you didn't state your actual goals at all, so it's not obvious to me that you will really need the source code for what you are trying to achieve. The code object also contains references to the place the source code originated from (file path and source lines). However, given that you expect users to "type code into the interpreter", instead of loading code from files, this will not allow you to get at the source code when the function originated in the interpreter. Stefan From ideasman42 at gmail.com Sat Jul 16 08:59:10 2011 From: ideasman42 at gmail.com (Campbell Barton) Date: Sat, 16 Jul 2011 16:59:10 +1000 Subject: [capi-sig] Code for a Python function In-Reply-To: <7BA13DD4-981A-4918-A439-FB8D149AAE1B@apple.com> References: <7BA13DD4-981A-4918-A439-FB8D149AAE1B@apple.com> Message-ID: Hi Enrico, Officially, I think what you want to do isn't really supported by python. - What if the python function is loaded from a pyc with no source code? - What if the function is defined in a string which is executed but does not correspond to a valid __file__ you can open and inspect. In practice you can probably manage it if make a few assumptions. You can access these attributes (or their C values directly): myfunc.__code__.co_filename myfunc.__code__.co_firstlineno Personally I'd not worry about the PyCodeObject changing (though without more details on what you do its hard to say). Or you could import the 'inspect' module via the C api and call its getsource() function, however 'inspect' just makes a guess too and isn't guaranteed to give the correct result, this at least gets around changes in PyCodeObject but assumes a full python installation. From egranata at apple.com Sat Jul 16 19:25:20 2011 From: egranata at apple.com (Enrico Granata) Date: Sat, 16 Jul 2011 10:25:20 -0700 Subject: [capi-sig] Code for a Python function In-Reply-To: <4E211987.3040700@behnel.de> References: <7BA13DD4-981A-4918-A439-FB8D149AAE1B@apple.com> <4E211987.3040700@behnel.de> Message-ID: On Jul 15, 2011, at 9:54 PM, Stefan Behnel wrote: > Enrico Granata, 16.07.2011 02:44: >> for my current task I have a Python interpreter embedded in a C++ program. >> There are several ways through which the user can interact with this interpreter, typing code into it, and possibly also def-fining functions. >> I need to be able to look into all defined functions, find one given its name and (here comes the hard part) read whatever code it currently contains. >> I looked into the API and there is a PyCodeObject class, but it is subject to change at any time and I do not want to start using code that might just change the next time the user upgrades Python. Is there any documented and portable-across-versions way to read the code into a Python function just given its name? > > Hi, > > as this question refers to Python's introspection capabilities in general, this isn't the perfect place to ask. The general Python list would be a better place. > Sorry for the mistake. I will try and redirect my question to the appropriate place. > That being said, PyCodeObject doesn't actually contain the source code but the compiled byte code. You didn't state what you wanted to do with the code, so this may or may not be what you want. In fact, you didn't state your actual goals at all, so it's not obvious to me that you will really need the source code for what you are trying to achieve. > Having the source code into a char* or std::string ready for display is good enough for my usage scenario. I cannot really delve into the details unfortunately. > The code object also contains references to the place the source code originated from (file path and source lines). However, given that you expect users to "type code into the interpreter", instead of loading code from files, this will not allow you to get at the source code when the function originated in the interpreter. > > Stefan Thanks, Enrico From egranata at apple.com Sat Jul 16 19:22:03 2011 From: egranata at apple.com (Enrico Granata) Date: Sat, 16 Jul 2011 10:22:03 -0700 Subject: [capi-sig] Code for a Python function In-Reply-To: References: <7BA13DD4-981A-4918-A439-FB8D149AAE1B@apple.com> Message-ID: <3CDC052D-BF40-4A01-88BB-C520303CE701@apple.com> On Jul 15, 2011, at 11:59 PM, Campbell Barton wrote: > Hi Enrico, > Officially, I think what you want to do isn't really supported by python. > - What if the python function is loaded from a pyc with no source code? > - What if the function is defined in a string which is executed but > does not correspond to a valid __file__ you can open and inspect. > Which is exactly the scenario I envision, unfortunately. > In practice you can probably manage it if make a few assumptions. > > You can access these attributes (or their C values directly): > myfunc.__code__.co_filename > myfunc.__code__.co_firstlineno > However these are not going to help me if the function data comes from standard input instead of from a file > Personally I'd not worry about the PyCodeObject changing (though > without more details on what you do its hard to say). > > Or you could import the 'inspect' module via the C api and call its > getsource() function, however 'inspect' just makes a guess too and > isn't guaranteed to give the correct result, this at least gets around > changes in PyCodeObject but assumes a full python installation. I guess this is not viable either, still thanks a lot for your helpful hints. > _______________________________________________ > capi-sig mailing list > capi-sig at python.org > http://mail.python.org/mailman/listinfo/capi-sig