[svn.haxx.se] · SVN Dev · SVN Users · SVN Org · TSVN Dev · TSVN Users · Subclipse Dev · Subclipse Users · this month's index

Re: [PATCH]On branch swig-py3: accept both of bytes/str for inputchar * args

From: Yasuhito FUTATSUKI <futatuki_at_yf.bsdclub.org>
Date: Wed, 16 Jan 2019 01:18:11 +0900

On 1/14/19 6:30 PM, Yasuhito FUTATSUKI wrote:
> On 1/14/19 3:10 PM, Yasuhito FUTATSUKI wrote:
>> In article <CAEZNtZ+SKqiATLwDvzyVf=SULPGhT7usxhZEawNfextPWsbEag_at_mail.gmail.com>
>> troycurtisjr_at_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
Received on 2019-01-15 17:20:46 CET

This is an archived mail posted to the Subversion Dev mailing list.

This site is subject to the Apache Privacy Policy and the Apache Public Forum Archive Policy.