Re: error:depythonifying 'charptr', got 'str'

Ronald Oussoren via Pyobjc-dev <[email protected]> Fri, 18 Jun 2021 15:09:23 +0200
Newsgroups gmane.comp.python.pyobjc.devel
Message-ID <[email protected]>
--===============6662313212223103940==
Content-Type: multipart/alternative;
	boundary="Apple-Mail=_17A63BC9-1E33-4322-80D0-BAC605C54172"


--Apple-Mail=_17A63BC9-1E33-4322-80D0-BAC605C54172
Content-Transfer-Encoding: quoted-printable
Content-Type: text/plain;
	charset=utf-8



> On 18 Jun 2021, at 14:10, Liu Aubrey <[email protected]> wrote:
>=20
> Dear teams:
>    I am confused by this error:ValueError: depythonifying 'charptr', =
got =E2=80=98str=E2=80=99 for few days and cannot find the reason.
> My pyobjc version is 7.3, macOS version is 10.15.7 and the objective-c =
function is below
>=20
> // Function to read Test Device Register
>  -(BOOL) readRegisterAtAddress:(NSUInteger)address
>   withLength:(NSUInteger)length
>   data:(uint8_t *)data
>   error:(NSError **)error;

Is this an API in one of the frameworks from Apple?

If it is not you=E2=80=99ll have to register some metadata to use this =
API because the method interface is too complicated to handle unaided.

Something like this should work (replace =E2=80=98ClassName=E2=80=99 by =
the name of the actual class):

import objc
objc.registerMetaDataForSelector(b'ClassName', =
b'readRegisterAtAddress:withLength:data:error:=E2=80=99,=20
    {=20
       'retval': { 'type=E2=80=99: objc._C_NSBOOL },=20
       'arguments': {=20
          4: { 'type_modifier=E2=80=99: objc._C_OUT, =
'c_array_length_in_arg': 3 },=20
          5: { 'type_modifier=E2=80=99: objc._C_OUT }
       }
    }
)

This tells PyObjC that:
* The return value is a boolean, otherwise the return value will be a =
small integer
* The 4th argument is an output argument and is a buffer whose size is =
in the 3rd argument
* The 5th argument is a pass-by-reference output argument

Note that counting includes implicit arguments for the Objective-C =
selector, therefore the first =E2=80=9Creal=E2=80=9D argument has offset =
2.

Usage of the API:

ok, data, error =3D =
device.device.readRegisterAtAddress_withLength_data_(0x1c, 4, None, =
None)

Ronald
  =20
>=20
> I used it in this way
> data =3D '\x00'*4
> device.device.readRegisterAtAddress_withLength_data_error_(0x1c, 4, =
data, None)
> Can you help me to solve this issue.
> Thank you very much.
>=20
> Aubrey.Liu
>=20
> =E5=8F=91=E8=87=AA=E6=88=91=E7=9A=84iPhone
> _______________________________________________
> Pyobjc-dev mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/pyobjc-dev

=E2=80=94

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


--Apple-Mail=_17A63BC9-1E33-4322-80D0-BAC605C54172
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 18 Jun 2021, at 14:10, Liu Aubrey &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"">Dear teams:<br class=3D""> &nbsp;&nbsp;&nbsp;I am confused by =
this error:ValueError: depythonifying 'charptr', got =E2=80=98str=E2=80=99=
 for few days and cannot find the reason.<br class=3D"">My pyobjc =
version is 7.3, macOS version is 10.15.7 and the objective-c function is =
below<br class=3D""><br class=3D"">// Function to read Test Device =
Register<br class=3D""> &nbsp;-(BOOL) =
readRegisterAtAddress:(NSUInteger)address<br class=3D""> =
&nbsp;&nbsp;withLength:(NSUInteger)length<br class=3D""> =
&nbsp;&nbsp;data:(uint8_t *)data<br class=3D""> =
&nbsp;&nbsp;error:(NSError **)error;<br =
class=3D""></div></div></blockquote><div><br class=3D""></div>Is this an =
API in one of the frameworks from Apple?</div><div><br =
class=3D""></div><div>If it is not you=E2=80=99ll have to register some =
metadata to use this API because the method interface is too complicated =
to handle unaided.</div><div><br class=3D""></div><div>Something like =
this should work (replace =E2=80=98ClassName=E2=80=99 by the name of the =
actual class):</div><div><br class=3D""></div><div>import =
objc</div><div><div style=3D"margin: 0px; font-stretch: normal; =
font-size: 11px; line-height: normal; font-family: Menlo;" =
class=3D""><span style=3D"font-variant-ligatures: no-common-ligatures" =
class=3D"">objc.registerMetaDataForSelector(b'ClassName', =
b'readRegisterAtAddress:withLength:data:error:=E2=80=99,&nbsp;</span></div=
><div style=3D"margin: 0px; font-stretch: normal; font-size: 11px; =
line-height: normal; font-family: Menlo;" class=3D""><span =
style=3D"font-variant-ligatures: no-common-ligatures" class=3D"">&nbsp; =
&nbsp; {&nbsp;</span></div><div style=3D"margin: 0px; font-stretch: =
normal; font-size: 11px; line-height: normal; font-family: Menlo;" =
class=3D""><span style=3D"font-variant-ligatures: no-common-ligatures" =
class=3D"">&nbsp; &nbsp; &nbsp; &nbsp;'retval': { 'type=E2=80=99: =
objc._C_NSBOOL },&nbsp;</span></div><div style=3D"margin: 0px; =
font-stretch: normal; font-size: 11px; line-height: normal; font-family: =
Menlo;" class=3D""><span style=3D"font-variant-ligatures: =
no-common-ligatures" class=3D"">&nbsp; &nbsp; &nbsp; &nbsp;'arguments': =
{&nbsp;</span></div><div style=3D"margin: 0px; font-stretch: normal; =
font-size: 11px; line-height: normal; font-family: Menlo;" =
class=3D""><span style=3D"font-variant-ligatures: no-common-ligatures" =
class=3D"">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 4: { 'type_modifier=E2=80=99=
: objc._C_OUT, 'c_array_length_in_arg': 3 },&nbsp;</span></div><div =
style=3D"margin: 0px; font-stretch: normal; font-size: 11px; =
line-height: normal; font-family: Menlo;" class=3D""><span =
style=3D"font-variant-ligatures: no-common-ligatures" class=3D"">&nbsp; =
&nbsp; &nbsp; &nbsp; &nbsp; 5: { 'type_modifier=E2=80=99: objc._C_OUT =
}</span></div><div style=3D"margin: 0px; font-stretch: normal; =
font-size: 11px; line-height: normal; font-family: Menlo;" =
class=3D""><span style=3D"font-variant-ligatures: no-common-ligatures" =
class=3D"">&nbsp; &nbsp; &nbsp; &nbsp;}</span></div><div style=3D"margin: =
0px; font-stretch: normal; font-size: 11px; line-height: normal; =
font-family: Menlo;" class=3D""><span style=3D"font-variant-ligatures: =
no-common-ligatures" class=3D"">&nbsp; &nbsp; }</span></div><div =
style=3D"margin: 0px; font-stretch: normal; font-size: 11px; =
line-height: normal; font-family: Menlo;" class=3D""><span =
style=3D"font-variant-ligatures: no-common-ligatures" =
class=3D"">)</span></div></div><div><br class=3D""></div><div>This tells =
PyObjC that:</div><div>* The return value is a boolean, otherwise the =
return value will be a small integer</div><div>* The 4th argument is an =
output argument and is a buffer whose size is in the 3rd =
argument</div><div>* The 5th argument is a pass-by-reference output =
argument</div><div><br class=3D""></div><div>Note that counting includes =
implicit arguments for the Objective-C selector, therefore the first =
=E2=80=9Creal=E2=80=9D argument has offset 2.</div><div><br =
class=3D""></div><div>Usage of the API:</div><div><br =
class=3D""></div><div>ok, data, error =3D =
device.device.readRegisterAtAddress_withLength_data_(0x1c, 4, None, =
None)</div><div><br class=3D""></div><div>Ronald</div><div>&nbsp; =
&nbsp;</div><div><blockquote type=3D"cite" class=3D""><div class=3D""><div=
 class=3D""><br class=3D"">I used it in this way<br class=3D"">data =3D =
'\x00'*4<br =
class=3D"">device.device.readRegisterAtAddress_withLength_data_error_(0x1c=
, 4, data, None)<br class=3D"">Can you help me to solve this issue.<br =
class=3D"">Thank you very much.<br class=3D""><br class=3D"">Aubrey.Liu<br=
 class=3D""><br class=3D"">=E5=8F=91=E8=87=AA=E6=88=91=E7=9A=84iPhone<br =
class=3D"">_______________________________________________<br =
class=3D"">Pyobjc-dev mailing list<br class=3D""><a =
href=3D"mailto:[email protected]" =
class=3D"">[email protected]</a><br =
class=3D"">https://lists.sourceforge.net/lists/listinfo/pyobjc-dev<br =
class=3D""></div></div></blockquote></div><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=_17A63BC9-1E33-4322-80D0-BAC605C54172--


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


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

_______________________________________________
Pyobjc-dev mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/pyobjc-dev

--===============6662313212223103940==--