Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > where is the PyString_AsString in Python 3.0?

Reply
Thread Tools

where is the PyString_AsString in Python 3.0?

 
 
BigHand
Guest
Posts: n/a
 
      03-06-2009
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.
 
Reply With Quote
 
 
 
 
Benjamin Peterson
Guest
Posts: n/a
 
      03-06-2009
BigHand <heweiwei <at> 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)



 
Reply With Quote
 
 
 
 
BigHand
Guest
Posts: n/a
 
      03-06-2009
On 3月6日, 下午8时50分, Benjamin Peterson <(E-Mail Removed)> wrote:
> BigHand <heweiwei <at> 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
 
Reply With Quote
 
Benjamin Peterson
Guest
Posts: n/a
 
      03-06-2009
BigHand <heweiwei <at> 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.




 
Reply With Quote
 
BigHand
Guest
Posts: n/a
 
      03-07-2009
On Mar 7, 3:50*am, Benjamin Peterson <(E-Mail Removed)> wrote:
> BigHand <heweiwei <at> 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!
 
Reply With Quote
 
BigHand
Guest
Posts: n/a
 
      03-07-2009
On Mar 7, 9:34*am, BigHand <(E-Mail Removed)> wrote:
> On Mar 7, 3:50*am, Benjamin Peterson <(E-Mail Removed)> wrote:
>
> > BigHand <heweiwei <at> 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);


 
Reply With Quote
 
Benjamin Peterson
Guest
Posts: n/a
 
      03-07-2009
BigHand <heweiwei <at> gmail.com> writes:
>
> 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:


> const char *strExcType = PyBytes_AS_STRING(pyStr);
> Py_XDECREF(str_exc_type);
> Py_XDECREF(pyStr);


You can't Py_DECREF() pyStr while holding on to strExcType because
PyBytes_AS_STRING just yields a reference to the internal contents of the object.


 
Reply With Quote
 
Stefan Behnel
Guest
Posts: n/a
 
      03-08-2009
BigHand wrote:
> 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* ?


Are you sure you want the exc_type and not the exc_value? The only major
thing I'd do with the type of an exception is to let Python check for it
using PyErr_ExceptionMatches().

Stefan
 
Reply With Quote
 
Stefan Behnel
Guest
Posts: n/a
 
      03-08-2009
BigHand wrote:
> Finally I got the results now. This did take me 10 hours to solve
> this. the docs of 3.0......


You will have to get used to Unicode. The code you used against the C-API
mimics almost exactly the steps you'd use at the Python level.

Stefan
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: [Python-Dev] [python-committers] [RELEASED] Python 3.2 rc 1 R. David Murray Python 0 01-17-2011 02:23 PM
Re: [Python-Dev] [python-committers] [RELEASED] Python 3.2 rc 1 Senthil Kumaran Python 0 01-17-2011 10:31 AM
Re: [Python-Dev] [Python-3000] RELEASED Python 2.6a1 and 3.0a3 Martin v. Lwis Python 0 03-01-2008 10:51 PM
Re: [Python-Dev] [Python-3000] RELEASED Python 2.6a1 and 3.0a3 Paul Moore Python 0 03-01-2008 10:39 PM
Searching comp.lang.python/python-list@python.org (was: UTF-8) skip@pobox.com Python 0 03-10-2007 02:50 PM



Advertisments