[Pythonmac-SIG] Supplying Unicode strings to pyobjc

Ronald Oussoren ronaldoussoren at mac.com
Sun Apr 10 16:09:12 EDT 2022



> On 10 Apr 2022, at 09:23, Ben Byram-Wigfield via Pythonmac-SIG <pythonmac-sig at python.org> wrote:
> 
> I'm converting a lot of python2 scripts that use pyobjc to python3, and having trouble getting them to work. The problem seems to relate to the Unicode changes in python3.
> 
> Something as simple as supplying a string to method like this:
> 
> provider = CGDataProviderCreateWithFilename(filename)
> 
> gives: ValueError: depythonifying 'char', got 'str' of 1
> 
> I can get it to work if I encode the string first:
> 
> filenameNonU = filename.encode('utf-8')
> provider = CGDataProviderCreateWithFilename(filenameNonU)

This is the correct code pattern.  PyObjC is a bit more strict here when running on Python 3: The C argument is a C string, which is represented as a bytes value in Python. In Python 2 the code also accepted unicode (str in Python 3), I removed that because Python 3 treats str and bytes differently.

> 
> But that will only work for “ASCII” range characters. Any characters greater than 128 gives:
> 
> ValueError: depythonifying 'char', got 'int' of wrong magnitude

That’s unexpected, could you file an issue about this on the PyObjC tracker?

Ronald
—

Twitter / micro.blog: @ronaldoussoren
Blog: https://blog.ronaldoussoren.net/

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.python.org/pipermail/pythonmac-sig/attachments/20220410/d131b7f4/attachment.html>


More information about the Pythonmac-SIG mailing list