From ben.bw at me.com Sun Apr 10 03:23:42 2022 From: ben.bw at me.com (Ben Byram-Wigfield) Date: Sun, 10 Apr 2022 08:23:42 +0100 Subject: [Pythonmac-SIG] Supplying Unicode strings to pyobjc Message-ID: 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 From ronaldoussoren at mac.com Sun Apr 10 16:09:12 2022 From: ronaldoussoren at mac.com (Ronald Oussoren) Date: Sun, 10 Apr 2022 22:09:12 +0200 Subject: [Pythonmac-SIG] Supplying Unicode strings to pyobjc In-Reply-To: References: Message-ID: <16906293-68F4-4B56-85CE-5B08AFDC0949@mac.com> > On 10 Apr 2022, at 09:23, Ben Byram-Wigfield via Pythonmac-SIG 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: