On 1/14/19 6:30 PM, Yasuhito FUTATSUKI wrote:
On 1/14/19 3:10 PM, Yasuhito FUTATSUKI wrote:
In article <CAEZNtZ+SKqiATLwDvzyVf=sulpght7usxhzeawnfextpwsb...@mail.gmail.com>
troycurti...@gmail.com writes:
On Thu, Jan 10, 2019 at 9:50 AM Yasuhito FUTATSUKI


PyBytes: Sequence of byte values, e.g. "raw data"
    In Py2: str
    In Py3: bytes

PyStr: Character data
    In Py2: Unicode
    In Py3: str

Unfortunately, PyStr in py3c compatibility layer API is the intersection
of PyString in Python 2, and PyUnicode in Python 3, so we must explicitly
use PyUnicode_* for handling Unicode in py2.

Aha, both of those are partially correct and partially wrong.

PyStr_Check:
    In Py2: PyString_Check, accept string and its subtype object.
            (not accept unicode object)
    In Py3: PyUnicode_Check, accept string and its subtype object.

PyStr_UTF8:
    In Py2: PyString_AsString, accept both of bytes(str) and unicode.
    In Py3: PyUnicode_AsUTF8, accept str(Unicode object).

and, PyUnicode_Check() is also provided for py2.

So, we can use code fragment like:

        ...
    if (PyBytes_Check(input))
      {
        retval = PyBytes_AsString(input);
      }
    else if (PyUnicode_Check(input))
      {
        retval = (char *)PyStr_AsUTF8(input);
      }
     else
         ...

--
Yasuhito FUTATSUKI

Reply via email to