where is the PyString_AsString in Python 3.0?

Discussion in 'Python' started by BigHand, Mar 6, 2009.

  1. BigHand

    BigHand Guest

    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.
     
    BigHand, Mar 6, 2009
    #1
    1. Advertising

  2. 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)
     
    Benjamin Peterson, Mar 6, 2009
    #2
    1. Advertising

  3. BigHand

    BigHand Guest

    On 3月6æ—¥, 下åˆ8æ—¶50分, Benjamin Peterson <> 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
     
    BigHand, Mar 6, 2009
    #3
  4. 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.
     
    Benjamin Peterson, Mar 6, 2009
    #4
  5. BigHand

    BigHand Guest

    On Mar 7, 3:50 am, Benjamin Peterson <> 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!
     
    BigHand, Mar 7, 2009
    #5
  6. BigHand

    BigHand Guest

    On Mar 7, 9:34 am, BigHand <> wrote:
    > On Mar 7, 3:50 am, Benjamin Peterson <> 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);
     
    BigHand, Mar 7, 2009
    #6
  7. 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.
     
    Benjamin Peterson, Mar 7, 2009
    #7
  8. 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
     
    Stefan Behnel, Mar 8, 2009
    #8
  9. 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
     
    Stefan Behnel, Mar 8, 2009
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    0
    Views:
    757
  2. Paul Moore
    Replies:
    0
    Views:
    634
    Paul Moore
    Mar 1, 2008
  3. Martin v. Löwis
    Replies:
    0
    Views:
    674
    Martin v. Löwis
    Mar 1, 2008
  4. Senthil Kumaran
    Replies:
    0
    Views:
    590
    Senthil Kumaran
    Jan 17, 2011
  5. R. David Murray
    Replies:
    0
    Views:
    779
    R. David Murray
    Jan 17, 2011
Loading...

Share This Page