Re: Supplying Unicode strings to pyobjc

Ronald Oussoren via Pythonmac-SIG <[email protected]> Sun, 10 Apr 2022 22:09:12 +0200
Newsgroups gmane.comp.python.apple
Message-ID <[email protected]>
--===============5626678015314631304==
Content-Type: multipart/alternative;
	boundary="Apple-Mail=_C1966200-44AF-4E2D-A68B-94DDF0EA1444"


--Apple-Mail=_C1966200-44AF-4E2D-A68B-94DDF0EA1444
Content-Transfer-Encoding: quoted-printable
Content-Type: text/plain;
	charset=utf-8



> On 10 Apr 2022, at 09:23, Ben Byram-Wigfield via Pythonmac-SIG =
<[email protected]> wrote:
>=20
> 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.
>=20
> Something as simple as supplying a string to method like this:
>=20
> provider =3D CGDataProviderCreateWithFilename(filename)
>=20
> gives: ValueError: depythonifying 'char', got 'str' of 1
>=20
> I can get it to work if I encode the string first:
>=20
> filenameNonU =3D filename.encode('utf-8')
> provider =3D 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.

>=20
> But that will only work for =E2=80=9CASCII=E2=80=9D range characters. =
Any characters greater than 128 gives:
>=20
> ValueError: depythonifying 'char', got 'int' of wrong magnitude

That=E2=80=99s unexpected, could you file an issue about this on the =
PyObjC tracker?

Ronald
=E2=80=94

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


--Apple-Mail=_C1966200-44AF-4E2D-A68B-94DDF0EA1444
Content-Transfer-Encoding: quoted-printable
Content-Type: text/html;
	charset=utf-8

<html><head><meta http-equiv=3D"Content-Type" content=3D"text/html; =
charset=3Dutf-8"></head><body style=3D"word-wrap: break-word; =
-webkit-nbsp-mode: space; line-break: after-white-space;" class=3D""><br =
class=3D""><div><br class=3D""><blockquote type=3D"cite" class=3D""><div =
class=3D"">On 10 Apr 2022, at 09:23, Ben Byram-Wigfield via =
Pythonmac-SIG &lt;<a href=3D"mailto:[email protected]" =
class=3D"">[email protected]</a>&gt; wrote:</div><br =
class=3D"Apple-interchange-newline"><div class=3D""><div class=3D"">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.<br class=3D""><br class=3D"">Something as =
simple as supplying a string to method like this:<br class=3D""><br =
class=3D"">provider =3D CGDataProviderCreateWithFilename(filename)<br =
class=3D""><br class=3D"">gives: ValueError: depythonifying 'char', got =
'str' of 1<br class=3D""><br class=3D"">I can get it to work if I encode =
the string first:<br class=3D""><br class=3D"">filenameNonU =3D =
filename.encode('utf-8')<br class=3D"">provider =3D =
CGDataProviderCreateWithFilename(filenameNonU)<br =
class=3D""></div></div></blockquote><div><br class=3D""></div>This is =
the correct code pattern. &nbsp;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.</div><div><br class=3D""><blockquote type=3D"cite" =
class=3D""><div class=3D""><div class=3D""><br class=3D"">But that will =
only work for =E2=80=9CASCII=E2=80=9D range characters. Any characters =
greater than 128 gives:<br class=3D""><br class=3D"">ValueError: =
depythonifying 'char', got 'int' of wrong magnitude<br =
class=3D""></div></div></blockquote><div><br class=3D""></div>That=E2=80=99=
s unexpected, could you file an issue about this on the PyObjC =
tracker?</div><div><br class=3D""></div>Ronald<br class=3D""><div =
class=3D"">
<div dir=3D"auto" style=3D"word-wrap: break-word; -webkit-nbsp-mode: =
space; line-break: after-white-space;" class=3D""><div =
style=3D"caret-color: rgb(0, 0, 0); color: rgb(0, 0, 0); font-family: =
Helvetica; font-size: 12px; font-style: normal; font-variant-caps: =
normal; font-weight: normal; letter-spacing: normal; text-align: start; =
text-indent: 0px; text-transform: none; white-space: normal; =
word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: =
none;">=E2=80=94</div><div style=3D"caret-color: rgb(0, 0, 0); color: =
rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: =
normal; font-variant-caps: normal; font-weight: normal; letter-spacing: =
normal; text-align: start; text-indent: 0px; text-transform: none; =
white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; =
text-decoration: none;"><br class=3D""></div><div style=3D"caret-color: =
rgb(0, 0, 0); color: rgb(0, 0, 0); font-family: Helvetica; font-size: =
12px; font-style: normal; font-variant-caps: normal; font-weight: =
normal; letter-spacing: normal; text-align: start; text-indent: 0px; =
text-transform: none; white-space: normal; word-spacing: 0px; =
-webkit-text-stroke-width: 0px; text-decoration: none;">Twitter / =
micro.blog: @ronaldoussoren</div><div style=3D"caret-color: rgb(0, 0, =
0); color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; =
font-style: normal; font-variant-caps: normal; font-weight: normal; =
letter-spacing: normal; text-align: start; text-indent: 0px; =
text-transform: none; white-space: normal; word-spacing: 0px; =
-webkit-text-stroke-width: 0px; text-decoration: none;">Blog: <a =
href=3D"https://blog.ronaldoussoren.net/" =
class=3D"">https://blog.ronaldoussoren.net/</a></div></div>
</div>
<br class=3D""></body></html>=

--Apple-Mail=_C1966200-44AF-4E2D-A68B-94DDF0EA1444--

--===============5626678015314631304==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

_______________________________________________
Pythonmac-SIG maillist  -  [email protected]
https://mail.python.org/mailman/listinfo/pythonmac-sig
unsubscribe: https://mail.python.org/mailman/options/Pythonmac-SIG

--===============5626678015314631304==--