[Pythonmac-SIG] Supplying Unicode strings to pyobjc

Ben Byram-Wigfield ben.bw at me.com
Sun Apr 10 03:23:42 EDT 2022


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)

But that will only work for “ASCII” range characters. Any characters greater than 128 gives:

ValueError: depythonifying 'char', got 'int' of wrong magnitude

It all works perfectly in python2. I presume because objc wants “const char *filename”, rather than an actual string.

How should I be presenting these strings?

I’ve been struggling with this for years, and so stayed on python2 as long as possible.

Thanks

Ben Byram-Wigfield


More information about the Pythonmac-SIG mailing list