where is the PyString_AsString in Python 3.0?
Guys: I know that there is no PyString_AsString in Python3.0, could you guys give me instruction about how can I do with the following ? PyObject *exc_type = NULL, *exc_value = NULL, *exc_tb = NULL; PyErr_Fetch(&exc_type, &exc_value, &exc_tb); how do I transfer the exc_type in a char* ? thanks in advance!. B.R. BH. -- http://mail.python.org/mailman/listinfo/python-list
Re: where is the PyString_AsString in Python 3.0?
On 3月6日, 下午8时50分, Benjamin Peterson wrote: > BigHand gmail.com> writes: > > > > > Guys: > > I know that there is no PyString_AsString in Python3.0, > > could you guys give me instruction about how can I do with the > > following ? > > There is no PyString_AsString. Everything string is unicode now. (PyUnicode > API) hello,Ben, could you give me an example? I almost know the PyUnicode API,but the docs of 3.0 is too brief for me. B.R. BH -- http://mail.python.org/mailman/listinfo/python-list
Re: where is the PyString_AsString in Python 3.0?
On Mar 7, 3:50 am, Benjamin Peterson wrote: > BigHand gmail.com> writes: > > > > There is no PyString_AsString. Everything > >> string is unicode now. (PyUnicode API) > > hello,Ben, > > could you give me an example? I almost know the > > PyUnicode API,but the > > docs of 3.0 is too brief for me. > > PyString_FromString -> PyUnicode_FromString > PyString_Concat -> PyUnicode_Concat > etc... > > To get a char * you have to explicitly encode the string with > PyUnicode_AsEncodedString. thanks Ben! -- http://mail.python.org/mailman/listinfo/python-list
Re: where is the PyString_AsString in Python 3.0?
On Mar 7, 9:34 am, BigHand wrote: > On Mar 7, 3:50 am, Benjamin Peterson wrote: > > > BigHand gmail.com> writes: > > > > > There is no PyString_AsString. Everything > > >> string is unicode now. (PyUnicode API) > > > hello,Ben, > > > could you give me an example? I almost know the > > > PyUnicode API,but the > > > docs of 3.0 is too brief for me. > > > PyString_FromString -> PyUnicode_FromString > > PyString_Concat -> PyUnicode_Concat > > etc... > > > To get a char * you have to explicitly encode the string with > > PyUnicode_AsEncodedString. > > thanks Ben! Finally I got the results now. This did take me 10 hours to solve this. the docs of 3.0.. I hope this could help someone else: PyObject *exc_type = NULL, *exc_value = NULL, *exc_tb = NULL; PyErr_Fetch(&exc_type, &exc_value, &exc_tb); PyObject* str_exc_type = PyObject_Repr(exc_type); //Now a unicode object PyObject* pyStr = PyUnicode_AsEncodedString(str_exc_type, "utf-8", "Error ~"); const char *strExcType = PyBytes_AS_STRING(pyStr); Py_XDECREF(str_exc_type); Py_XDECREF(pyStr); Py_XDECREF(exc_type); Py_XDECREF(exc_value); Py_XDECREF(exc_tb); -- http://mail.python.org/mailman/listinfo/python-list
retrieve traceback in embedded python of Python3.0?
Guys:
I have a embedded python in MFC app. to execute a py script of a.py,
the is only one line in a.py, it "a()" , normally ,excute this script
file ,you will get a
"the exception type is "
"The exception value is name 'a' is not defined "
Python3.0 with VS2005.
here is the brief code:
Py_Initialize();
PyObject *m, *d, *v;
m = PyImport_AddModule("__main__");
d = PyModule_GetDict(m);
v = PyRun_File(fp, pStr, Py_file_input, d, d); //~~~the py
script is a.py
PyObject *exc_type = NULL, *exc_value = NULL, *exc_tb = NULL;
PyErr_Fetch(&exc_type, &exc_value, &exc_tb); //~~~after fetch , the
exc_type, exc_value, exc_tb are not "NULL"
PyObject * modTB = PyImport_ImportModule("traceback");
PyObject* pyUStr = PyUnicode_FromString("format_exception");
PyObject* listTB = PyObject_CallMethodObjArgs(modTB, pyUStr,
exc_type, exc_value, exc_tb, NULL);
in the PyObject_CallMethodObjArgs(modTB, pyUStr, exc_type, exc_value,
exc_tb, NULL), I get modTB, pyUStr, exc_type, exc_value, exc_tb are
not NULL, but the listTB is always NULL, I can retrieve the list...
any body could enlight me?
--
http://mail.python.org/mailman/listinfo/python-list
Re: retrieve traceback in embedded python of Python3.0?
On Mar 7, 11:40 am, BigHand wrote:
> Guys:
> I have a embedded python in MFC app. to execute a py script of a.py,
> the is only one line in a.py, it "a()" , normally ,excute this script
> file ,you will get a
> "the exception type is "
> "The exception value is name 'a' is not defined "
>
> Python3.0 with VS2005.
> here is the brief code:
> Py_Initialize();
> PyObject *m, *d, *v;
> m = PyImport_AddModule("__main__");
> d = PyModule_GetDict(m);
> v = PyRun_File(fp, pStr, Py_file_input, d, d); //~~~the py
> script is a.py
> PyObject *exc_type = NULL, *exc_value = NULL, *exc_tb = NULL;
> PyErr_Fetch(&exc_type, &exc_value, &exc_tb); //~~~after fetch , the
> exc_type, exc_value, exc_tb are not "NULL"
> PyObject * modTB = PyImport_ImportModule("traceback");
> PyObject* pyUStr = PyUnicode_FromString("format_exception");
> PyObject* listTB = PyObject_CallMethodObjArgs(modTB, pyUStr,
> exc_type, exc_value, exc_tb, NULL);
>
> in the PyObject_CallMethodObjArgs(modTB, pyUStr, exc_type, exc_value,
> exc_tb, NULL), I get modTB, pyUStr, exc_type, exc_value, exc_tb are
> not NULL, but the listTB is always NULL, I can retrieve the list...
>
> any body could enlight me?
it's "I can't retrieve the trace back list."
--
http://mail.python.org/mailman/listinfo/python-list
Re: retrieve traceback in embedded python of Python3.0?
On Mar 7, 11:40 am, BigHand wrote:
> Guys:
> I have a embedded python in MFC app. to execute a py script of a.py,
> the is only one line in a.py, it "a()" , normally ,excute this script
> file ,you will get a
> "the exception type is "
> "The exception value is name 'a' is not defined "
>
> Python3.0 with VS2005.
> here is the brief code:
> Py_Initialize();
> PyObject *m, *d, *v;
> m = PyImport_AddModule("__main__");
> d = PyModule_GetDict(m);
> v = PyRun_File(fp, pStr, Py_file_input, d, d); //~~~the py
> script is a.py
> PyObject *exc_type = NULL, *exc_value = NULL, *exc_tb = NULL;
> PyErr_Fetch(&exc_type, &exc_value, &exc_tb); //~~~after fetch , the
> exc_type, exc_value, exc_tb are not "NULL"
> PyObject * modTB = PyImport_ImportModule("traceback");
> PyObject* pyUStr = PyUnicode_FromString("format_exception");
> PyObject* listTB = PyObject_CallMethodObjArgs(modTB, pyUStr,
> exc_type, exc_value, exc_tb, NULL);
>
> in the PyObject_CallMethodObjArgs(modTB, pyUStr, exc_type, exc_value,
> exc_tb, NULL), I get modTB, pyUStr, exc_type, exc_value, exc_tb are
> not NULL, but the listTB is always NULL, I can retrieve the list...
>
> any body could enlight me?
it's "I can't retrieve the traceback list."
--
http://mail.python.org/mailman/listinfo/python-list
pass bool values to the Python function in embedded python 3.0
Guys, How do I transfer the parameters ?
in python code:
traceback.format_exception(exc_type, exc_val, exc_tb, 2, True)
in C++ code:
obFunc_format_exception = PyObject_GetAttrString(modTB,
"format_exception");
tbArgs = Py_BuildValue("OOOii", exc_type, exc_value, exc_tb, 2, 1);
tbResultList = PyObject_CallObject(obFunc_format_exception , tbArgs);
how do I pass True to the Python function in the C++ code?
--
http://mail.python.org/mailman/listinfo/python-list
Re: pass bool values to the Python function in embedded python 3.0
On Mar 7, 5:38 pm, BigHand wrote:
> Guys, How do I transfer the parameters ?
> in python code:
> traceback.format_exception(exc_type, exc_val, exc_tb, 2, True)
>
> in C++ code:
> obFunc_format_exception = PyObject_GetAttrString(modTB,
> "format_exception");
> tbArgs = Py_BuildValue("OOOii", exc_type, exc_value, exc_tb, 2, 1);
> tbResultList = PyObject_CallObject(obFunc_format_exception , tbArgs);
>
> how do I pass True to the Python function in the C++ code?
build the python object
tbArgs = Py_BuildValue("O", exc_type, exc_value, exc_tb, Py_None,
Py_True);
tbResultList = PyObject_CallObject(obFunc_format_exception , tbArgs);
but I can't get the format_exception called successfully.
could anyone help me ?
--
http://mail.python.org/mailman/listinfo/python-list
Re: ANN: eric 4.3.1 released
On Mar 7, 6:59 pm, Detlev Offenbach wrote: > Hi, > > I just uploaded eric 4.3.1. It is a maintenance release fixing some bugs. > It is available via the eric4 web site. > > http://eric-ide.python-projects.org/index.html > > Eric is a Python (and Ruby) IDE that comes with batteries included. > Please see the a.m. web site for more details. > > Regards, > Detlev > -- > Detlev Offenbach > [email protected] thanks,man -- http://mail.python.org/mailman/listinfo/python-list
can't print the exception cause/context in Python3.0?
Here is copy from my IDLE of python 3.0 >>> import sys >>> import traceback >>> def a(): b() >>> def b(): return tuple()[0] >>> try: a() except: exc_typ, exc_val, exc_tb = sys.exc_info() >>> traceback.print_tb(exc_tb) File "", line 2, in File "", line 2, in a File "", line 2, in b but this doesn't output the cause/context like 2.6: or on the sample of: http://docs.python.org/3.0/library/traceback.html?highlight=format_exception#traceback.format_exception I want the exception printted like this: File "", line 2, in "a()" File "", line 2, in a"b()" File "", line 2, in b "return tuple()[0]" how should I do? -- http://mail.python.org/mailman/listinfo/python-list
Re: can't print the exception cause/context in Python3.0?
On 3月7日, 下午11时21分, "Gabriel Genellina" wrote: > En Sat, 07 Mar 2009 11:46:08 -0200, BigHand escribió: > > > > > Here is copy from my IDLE of python 3.0 > > >>>> traceback.print_tb(exc_tb) > > File "", line 2, in > > File "", line 2, in a > > File "", line 2, in b > > > but this doesn't output the cause/context like 2.6: or on the sample > > of: > >http://docs.python.org/3.0/library/traceback.html?highlight=format_ex... > > > I want the exception printted like this: > > File "", line 2, in "a()" > > File "", line 2, in a "b()" > > File "", line 2, in b "return tuple()[0]" > > Put your code in a true module stored in a file, so the source lines can > be retrieved. > > -- > Gabriel Genellina hello,Gabriel, I don't understand you.could you give me more details? http://docs.python.org/3.0/library/traceback.html?highlight=format_exception#traceback.format_exception the up page is from python 3.0's doc. maybe from a older version of Python, even I excute these file in a py script file, it's can't retrieve the source line either. -- http://mail.python.org/mailman/listinfo/python-list
Re: pass bool values to the Python function in embedded python 3.0
On 3月7日, 下午10时38分, "Gabriel Genellina" wrote:
> En Sat, 07 Mar 2009 07:38:29 -0200, BigHand escribió:
>
> > how do I pass True to the Python function in the C++ code?
>
> (I've already suggested using PyErr_Print/PyTraceback_Print instead)
>
> See the section "Boolean Objects" in the C API Reference:
>
> "PyObject* Py_True
> The Python True object. This object has no methods. It needs to be treated
> just like any other object with respect to reference counts."
>
> --
> Gabriel Genellina
tbArgs = Py_BuildValue("O", exc_type, exc_value, exc_tb,
Py_None,
Py_True);
tbResultList = PyObject_CallObject(obFunc_format_exception , tbArgs);
should increase the ref count of Py_True first?
this can't call the python function format_exception successfully
either.
B.R.
BH
--
http://mail.python.org/mailman/listinfo/python-list
Re: can't print the exception cause/context in Python3.0?
On Mar 8, 9:49 pm, "Gabriel Genellina" wrote:
> En Sat, 07 Mar 2009 21:18:22 -0200, BigHand escribió:
>
> > On 3月7日, 下午11时21分, "Gabriel Genellina"
> > wrote:
> >> En Sat, 07 Mar 2009 11:46:08 -0200, BigHand
> >> escribió:
>
> >> > I want the exception printted like this:
> >> > File "", line 2, in "a()"
> >> > File "", line 2, in a "b()"
> >> > File "", line 2, in b "return tuple()[0]"
>
> >> Put your code in a true module stored in a file, so the source lines
> >> can
> >> be retrieved.
> > I don't understand you.could you give me more details?
>
> C:\TEMP>type tbtest.py
> import sys
> import traceback
>
> def a(): b()
>
> def b(): raise ValueError
>
> print("\none\n")
> try: a()
> except:
> exc_typ, exc_val, exc_tb = sys.exc_info()
> traceback.print_tb(exc_tb)
>
> print("\ntwo\n")
> try: a()
> except:
> exc_typ, exc_val, exc_tb = sys.exc_info()
> traceback.print_tb(exc_tb)
>
> print("\nthree\n")
> a()
>
> C:\TEMP>python30 tbtest.py
>
> one
>
> File "tbtest.py", line 9, in
> try: a()
> File "tbtest.py", line 4, in a
> def a(): b()
> File "tbtest.py", line 6, in b
> def b(): raise ValueError
>
> two
>
> File "tbtest.py", line 15, in
> try: a()
> File "tbtest.py", line 4, in a
> def a(): b()
> File "tbtest.py", line 6, in b
> def b(): raise ValueError
>
> three
>
> Traceback (most recent call last):
> File "tbtest.py", line 21, in
> a()
> File "tbtest.py", line 4, in a
> def a(): b()
> File "tbtest.py", line 6, in b
> def b(): raise ValueError
> ValueError
>
> C:\TEMP>
>
> --
> Gabriel Genellina
hello.Gabriel,
thanks very much!
My another issue is that, I have an embedded python3.0 in my MFC app,
use the PyRun_File to execute the Py script file, and call the python
function traceback.print_tb to print the traceback info,
but I can't get souce showed in the traceback.
--
http://mail.python.org/mailman/listinfo/python-list
Re: retrieve traceback in embedded python of Python3.0?
On Mar 7, 9:22 pm, "Gabriel Genellina" wrote:
> En Sat, 07 Mar 2009 01:43:05 -0200, BigHand escribió:
>
>
>
> > On Mar 7, 11:40 am, BigHand wrote:
> >> Guys:
> >> I have a embedded python in MFC app. to execute a py script of a.py,
> >> the is only one line in a.py, it "a()" , normally ,excute this script
> >> file ,you will get a
> >> "the exception type is "
> >> "The exception value is name 'a' is not defined "
>
> >> Python3.0 with VS2005.
> >> here is the brief code:
> >> Py_Initialize();
> >> PyObject *m, *d, *v;
> >> m = PyImport_AddModule("__main__");
> >> d = PyModule_GetDict(m);
> >> v = PyRun_File(fp, pStr, Py_file_input, d, d); //~~~the py
> >> script is a.py
> >> PyObject *exc_type = NULL, *exc_value = NULL, *exc_tb = NULL;
> >> PyErr_Fetch(&exc_type, &exc_value, &exc_tb); //~~~after fetch , the
> >> exc_type, exc_value, exc_tb are not "NULL"
> >> PyObject * modTB = PyImport_ImportModule("traceback");
> >> PyObject* pyUStr = PyUnicode_FromString("format_exception");
> >> PyObject* listTB = PyObject_CallMethodObjArgs(modTB, pyUStr,
> >> exc_type, exc_value, exc_tb, NULL);
>
> >> in the PyObject_CallMethodObjArgs(modTB, pyUStr, exc_type, exc_value,
> >> exc_tb, NULL), I get modTB, pyUStr, exc_type, exc_value, exc_tb are
> >> not NULL, but the listTB is always NULL, I can retrieve the list...
>
> >> any body could enlight me?
>
> > it's "I can't retrieve the traceback list."
>
> PyErr_Print or PyTraceback_Print aren't suitable for you? Those functions
> are much easier to use from C code, while the traceback module is intended
> to be used in Python code.
>
> --
> Gabriel Genellina
Hello,Gabriel,
I gave up using the format_exception, and use calling the python
fucntion of print_tb.
--
http://mail.python.org/mailman/listinfo/python-list
Re: can't print the exception cause/context in Python3.0?
On Mar 9, 9:02 am, BigHand wrote:
> On Mar 8, 9:49 pm, "Gabriel Genellina" wrote:
>
>
>
> > En Sat, 07 Mar 2009 21:18:22 -0200, BigHand escribió:
>
> > > On 3月7日, 下午11时21分, "Gabriel Genellina"
> > > wrote:
> > >> En Sat, 07 Mar 2009 11:46:08 -0200, BigHand
> > >> escribió:
>
> > >> > I want the exception printted like this:
> > >> > File "", line 2, in "a()"
> > >> > File "", line 2, in a "b()"
> > >> > File "", line 2, in b "return tuple()[0]"
>
> > >> Put your code in a true module stored in a file, so the source lines
> > >> can
> > >> be retrieved.
> > > I don't understand you.could you give me more details?
>
> > C:\TEMP>type tbtest.py
> > import sys
> > import traceback
>
> > def a(): b()
>
> > def b(): raise ValueError
>
> > print("\none\n")
> > try: a()
> > except:
> > exc_typ, exc_val, exc_tb = sys.exc_info()
> > traceback.print_tb(exc_tb)
>
> > print("\ntwo\n")
> > try: a()
> > except:
> > exc_typ, exc_val, exc_tb = sys.exc_info()
> > traceback.print_tb(exc_tb)
>
> > print("\nthree\n")
> > a()
>
> > C:\TEMP>python30 tbtest.py
>
> > one
>
> > File "tbtest.py", line 9, in
> > try: a()
> > File "tbtest.py", line 4, in a
> > def a(): b()
> > File "tbtest.py", line 6, in b
> > def b(): raise ValueError
>
> > two
>
> > File "tbtest.py", line 15, in
> > try: a()
> > File "tbtest.py", line 4, in a
> > def a(): b()
> > File "tbtest.py", line 6, in b
> > def b(): raise ValueError
>
> > three
>
> > Traceback (most recent call last):
> > File "tbtest.py", line 21, in
> > a()
> > File "tbtest.py", line 4, in a
> > def a(): b()
> > File "tbtest.py", line 6, in b
> > def b(): raise ValueError
> > ValueError
>
> > C:\TEMP>
>
> > --
> > Gabriel Genellina
>
> hello.Gabriel,
> thanks very much!
>
> My another issue is that, I have an embedded python3.0 in my MFC app,
> use the PyRun_File to execute the Py script file, and call the python
> function traceback.print_tb to print the traceback info,
>
> but I can't get souce showed in the traceback.
solve it.
I need to "import traceback" first in the script file. or I can't get
the source retrieved later.
--
http://mail.python.org/mailman/listinfo/python-list
Any idea of stopping the execution of PyRun_File()
Hi,All I have an embedded python application. which is a MFC app with Python interpreter embedded. In the App, I have a separate thread to execute a Python script (using the PyRun_File), but if the user want to stop the executing script, how should I do? A possible way is terminate the thread of executing the scripts by TerminateThread(). but TerminateThread() is unsafe and not recommended. guys, could you guide me on this? B.R. Harry -- http://mail.python.org/mailman/listinfo/python-list
Re: Any idea of stopping the execution of PyRun_File()
On Jun 10, 7:41 am, "Gabriel Genellina" wrote: > En Mon, 08 Jun 2009 22:15:22 -0300, BigHand escribió: > > > I have an embedded python application. which is a MFC app with > > Python interpreter embedded. > > > In the App, I have a separate thread to execute a Python script > > (using the PyRun_File), but if the user want to stop the executing > > script, how should I do? > > > A possible way is terminate the thread of executing the scripts by > > TerminateThread(). but TerminateThread() is unsafe and not > > recommended. > > > guys, could you guide me on this? > > You could use PyThreadState_SetAsyncExc (to "inject" an exception), or > perhaps PyErr_SetInterrupt (to emulate ^C, which in turn generates a > KeyboardInterrupt). This should work fine for well-behaved scripts, but if > it ignores all exceptions like this: > try: ... > except: pass > you'll have to look at ceval.c how to break out of the running loop. > > (this is yet another argument against indiscriminately using a bare except > clause...) > > -- > Gabriel Genellina hello.Gabriel. PyThreadState_SetAsyncExc cause me a win32 exception of "Stack overflow" > msvcr80d.dll!_getptd_noexit() Line 592 C msvcr80d.dll!_getptd() Line 658 + 0x5 bytesC msvcr80d.dll!_LocaleUpdate::_LocaleUpdate(localeinfo_struct * plocinfo=0x) Line 264 + 0x5 bytes C++ msvcr80d.dll!_output_l(_iobuf * stream=0x10311d40, const char * format=0x1e26e0ac, localeinfo_struct * plocinfo=0x, char * argptr=0x000333c4) Line 1028 C++ msvcr80d.dll!fprintf(_iobuf * str=0x10311d40, const char * format=0x1e26e0ac, ...) Line 70 + 0x13 bytes C python30_d.dll!Py_FatalError(const char * msg=0x1e26a2d4) Line 2000 + 0x19 bytesC python30_d.dll!PyThreadState_Get() Line 349 + 0xa bytesC python30_d.dll!PyErr_Occurred() Line 133 + 0x5 bytes C python30_d.dll!Py_FatalError(const char * msg=0x1e26a2d4) Line 2001 + 0x5 bytes C python30_d.dll!PyThreadState_Get() Line 349 + 0xa bytesC python30_d.dll!PyErr_Occurred() Line 133 + 0x5 bytes C python30_d.dll!Py_FatalError(const char * msg=0x1e26a2d4) Line 2001 + 0x5 bytes C python30_d.dll!PyThreadState_Get() Line 349 + 0xa bytesC python30_d.dll!PyErr_Occurred() Line 133 + 0x5 bytes C python30_d.dll!Py_FatalError(const char * msg=0x1e26a2d4) Line 2001 + 0x5 bytes C python30_d.dll!PyThreadState_Get() Line 349 + 0xa bytesC python30_d.dll!PyErr_Occurred() Line 133 + 0x5 bytes C python30_d.dll!Py_FatalError(const char * msg=0x1e26a2d4) Line 2001 + 0x5 bytes C python30_d.dll!PyThreadState_Get() Line 349 + 0xa bytesC python30_d.dll!PyErr_Occurred() Line 133 + 0x5 bytes C python30_d.dll!Py_FatalError(const char * msg=0x1e26a2d4) Line 2001 + 0x5 bytes C python30_d.dll!PyThreadState_Get() Line 349 + 0xa bytesC python30_d.dll!PyErr_Occurred() Line 133 + 0x5 bytes C python30_d.dll!Py_FatalError(const char * msg=0x1e26a2d4) Line 2001 + 0x5 bytes C python30_d.dll!PyThreadState_Get() Line 349 + 0xa bytesC My app is a MFC app, it have the main thread and the sub thread, the sub thread run the script(Py_RunFile), That I am looking for a way to stop the Py_RunFile from the main thread. that looks like I have to use the TerminateThread() . thanks.Gabriel, could you find some example for me? -- http://mail.python.org/mailman/listinfo/python-list
