Re: PyEval_EvalCodeEx return value
John Pinner-3 wrote: I assume that you have read the documentation at http://docs.python.org/c-api/veryhigh.html, and I agree that it's sparse, but the entry for the next entry, PyEval_EvalCodeEx, tells you a little more, as does that for PyEval_EvalFrameEx. It does help, but only a bit. John Pinner-3 wrote: Obviously, it's returning a pointer to a PyObject, and Looking at the source code, that may be NULL, to throw an exception, or an execution frame, or a generator,etc, etc, depending on the code it's been given. I guess that you should be able to inspect the PyObject to see what it is. Yes, that's one possibility. I tried to investigate others. John Pinner-3 wrote: What I'm wondering is, why are you eval-ing code ? A favourite device of C programmers coming to Python (and mea culpa in the past), it's fraught with potential problems and even security risks, and is difficult to debug, and maybe you should be using introspection instead. My application accepts Python scripts from user and executes using embedded Python interpreter. So, I use this particular API. Could you elaborate on the using introspection? John Pinner-3 wrote: And maybe you should be working in Python, and not C++ at this point, do the dynamic stuff in the language best suited, and then return to C++ when needed. My application in C++ interacts with Python programs provided by users. Depending on what these programs request, various PyObject objects are created and returned back, etc. I understand it's difficult to discuss it without long essays or without providing the code, etc. So, I can only discuss and investigate building blocks I use. Anyway, thanks for your comments. Best regards, - -- Mateusz Loskot http://mateusz.loskot.net -- View this message in context: http://old.nabble.com/PyEval_EvalCodeEx-return-value-tp32501833p32503908.html Sent from the Python - python-list mailing list archive at Nabble.com. -- http://mail.python.org/mailman/listinfo/python-list
Re: PyEval_EvalCodeEx return value
On 23/09/11 00:47, Mark Hammond wrote: On 20/09/2011 8:34 PM, Mateusz Loskot wrote: I'm trying to dig out details about what exactly is the return value the of PyEval_EvalCodeEx function in Python 3.x The documentation is sparse, unfortunately. Perhaps I'm looking at wrong function. My aim is simple, I need to execute Python code using Python interpreter embedded in my C++ application. The Python code is a simple script that always returns single value. For example: #! /usr/bin/env python def foo(a, b): return a + b f = foo(2, 3) But, f can be of different type for different script: one returns numeric value, another returns a sequence, so the type is not possible to be determined in advance. I know how to capture Python stdout/stderr. I also know how to access the f attribute using PyObject_GetAttrString and then I can convert f value to C++ type depending on PyObject type. However, I guess there shall be a way to access f value directly from PyEval_EvalCode return object: PyObject* evalRet = ::PyEval_EvalCode(...); But, I can't find any details what the evalRet actually is. Eval is to eval an expression. If you simply eval the expression f in the context of the module you should get the result returned. Obviously though it is designed to eval more complex expressions and in your specific example, doing the getattr thing will also work fine. Hi Mark, So, the result of PyEval_EvalCode strictly depends on the code being evaluated. It makes sense. Thanks for help! Best regards, -- Mateusz Loskot, http://mateusz.loskot.net Charter Member of OSGeo, http://osgeo.org Member of ACCU, http://accu.org -- http://mail.python.org/mailman/listinfo/python-list
Re: PyEval_EvalCodeEx return value
On 20/09/2011 8:34 PM, Mateusz Loskot wrote: Hi, I'm trying to dig out details about what exactly is the return value the of PyEval_EvalCodeEx function in Python 3.x The documentation is sparse, unfortunately. Perhaps I'm looking at wrong function. My aim is simple, I need to execute Python code using Python interpreter embedded in my C++ application. The Python code is a simple script that always returns single value. For example: #! /usr/bin/env python def foo(a, b): return a + b f = foo(2, 3) But, f can be of different type for different script: one returns numeric value, another returns a sequence, so the type is not possible to be determined in advance. I know how to capture Python stdout/stderr. I also know how to access the f attribute using PyObject_GetAttrString and then I can convert f value to C++ type depending on PyObject type. However, I guess there shall be a way to access f value directly from PyEval_EvalCode return object: PyObject* evalRet = ::PyEval_EvalCode(...); But, I can't find any details what the evalRet actually is. Eval is to eval an expression. If you simply eval the expression f in the context of the module you should get the result returned. Obviously though it is designed to eval more complex expressions and in your specific example, doing the getattr thing will also work fine. Mark -- http://mail.python.org/mailman/listinfo/python-list
PyEval_EvalCodeEx return value
Hi, I'm trying to dig out details about what exactly is the return value the of PyEval_EvalCodeEx function in Python 3.x The documentation is sparse, unfortunately. Perhaps I'm looking at wrong function. My aim is simple, I need to execute Python code using Python interpreter embedded in my C++ application. The Python code is a simple script that always returns single value. For example: #! /usr/bin/env python def foo(a, b): return a + b f = foo(2, 3) But, f can be of different type for different script: one returns numeric value, another returns a sequence, so the type is not possible to be determined in advance. I know how to capture Python stdout/stderr. I also know how to access the f attribute using PyObject_GetAttrString and then I can convert f value to C++ type depending on PyObject type. However, I guess there shall be a way to access f value directly from PyEval_EvalCode return object: PyObject* evalRet = ::PyEval_EvalCode(...); But, I can't find any details what the evalRet actually is. Any pointers would be helpful. Best regards, -- Mateusz Loskot, http://mateusz.loskot.net Charter Member of OSGeo, http://osgeo.org Member of ACCU, http://accu.org -- http://mail.python.org/mailman/listinfo/python-list
Re: PyEval_EvalCodeEx return value
On Sep 20, 11:34 am, Mateusz Loskot mate...@loskot.net wrote: Hi, I'm trying to dig out details about what exactly is the return value the of PyEval_EvalCodeEx function in Python 3.x The documentation is sparse, unfortunately. Perhaps I'm looking at wrong function. My aim is simple, I need to execute Python code using Python interpreter embedded in my C++ application. The Python code is a simple script that always returns single value. For example: #! /usr/bin/env python def foo(a, b): return a + b f = foo(2, 3) But, f can be of different type for different script: one returns numeric value, another returns a sequence, so the type is not possible to be determined in advance. I know how to capture Python stdout/stderr. I also know how to access the f attribute using PyObject_GetAttrString and then I can convert f value to C++ type depending on PyObject type. However, I guess there shall be a way to access f value directly from PyEval_EvalCode return object: PyObject* evalRet = ::PyEval_EvalCode(...); But, I can't find any details what the evalRet actually is. I assume that you have read the documentation at http://docs.python.org/c-api/veryhigh.html, and I agree that it's sparse, but the entry for the next entry, PyEval_EvalCodeEx, tells you a little more, as does that for PyEval_EvalFrameEx. Obviously, it's returning a pointer to a PyObject, and Looking at the source code, that may be NULL, to throw an exception, or an execution frame, or a generator,etc, etc, depending on the code it's been given. I guess that you should be able to inspect the PyObject to see what it is. What I'm wondering is, why are you eval-ing code ? A favourite device of C programmers coming to Python (and mea culpa in the past), it's fraught with potential problems and even security risks, and is difficult to debug, and maybe you should be using introspection instead. And maybe you should be working in Python, and not C++ at this point, do the dynamic stuff in the language best suited, and then return to C+ + when needed. Best wishes, John -- Any pointers would be helpful. Best regards, -- Mateusz Loskot,http://mateusz.loskot.net Charter Member of OSGeo,http://osgeo.org Member of ACCU,http://accu.org -- http://mail.python.org/mailman/listinfo/python-list