Title: [ctypes] Add value attribute to non basic pointers.
Type: enhancement Stage:
Components: ctypes Versions: Python 3.10, Python 3.9, Python 3.8
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: amaury.forgeotdarc, belopolsky, eryksun, martin.panter, meador.inge, memeplex
Priority: normal Keywords:

Created on 2016-03-15 02:02 by memeplex, last changed 2021-02-26 17:33 by eryksun.

Messages (5)
msg261794 - (view) Author: Memeplex (memeplex) Date: 2016-03-15 02:02
I know one can do addressof(p.contents), but it's a bit inconsistent that c_void_p and c_char_p contain the same information in the value attribute.
msg262398 - (view) Author: Eryk Sun (eryksun) * (Python triager) Date: 2016-03-25 03:10
The `value` of a c_char_p or c_wchar_p pointer is a Python bytes or str object. Since `value` won't consistently be the address value, it may be better to introduce a read-only `as_void` attribute that can be implemented consistently for all pointer types (including function pointers). Its value would be the same as the `value` of a casted c_void_p (e.g. NULL is mapped to None). The new attribute would be added as a getset descriptor in Pointer_getsets and PyCFuncPtr_getsets. For simple pointer types, it would have to be added specially in PyCSimpleType_new.
msg267953 - (view) Author: Memeplex (memeplex) Date: 2016-06-09 03:23

Maybe ptr.toaddress? As opposed to addressof(ptr). I think ptr.address might be confusing as it's intutive meaning is closer to addressof(ptr).
msg268079 - (view) Author: Martin Panter (martin.panter) * (Python committer) Date: 2016-06-10 03:56
Eryk Sun’s as_void suggestion sounds similar to doing:

ctypes.cast(any_pointer, ctypes.c_void_p)

Why do you want the address? Perhaps it is good enough to get it from a void pointer:

ctypes.cast(any_pointer, ctypes.c_void_p).value

Maybe “pointer_value” would be less confusing than “toaddress”.
msg268262 - (view) Author: Memeplex (memeplex) Date: 2016-06-11 20:10
Martin, there were two reasons:

1. Conciseness: addressof(p.contents) vs. p.value.
2. Uniformity: I expect the value of a pointer to be the address it points to.

Then Eryk pointed out that p.value has already another meaning for simple pointer types, so (2) can't be fully achieved and for (1) it will be necessary to pick another attribute name. But we could still get something shorter and uniform inside the subset of pointer types.

p.as_void makes me expect a c_void_p instance. In that case the address will be p.as_void.value (or p.as_void().value?), which is a bit indirect. I think p.toaddress conveys the exact meaning[1] and consistently follows the naming style of addressof, besides being slightly shorter.

[1] Well, toaddress might mean "get the pointed-to address" or "convert from pointer to address", but in any case the meaning is right.
Date User Action Args
2021-02-26 17:33:21eryksunsetversions: + Python 3.8, Python 3.9, Python 3.10, - Python 2.7, Python 3.6
2016-06-11 20:10:02memeplexsetmessages: + msg268262
2016-06-10 03:56:35martin.pantersetnosy: + martin.panter
messages: + msg268079
2016-06-09 03:23:50memeplexsetmessages: + msg267953
2016-03-25 03:10:30eryksunsetnosy: + eryksun

messages: + msg262398
versions: + Python 2.7
2016-03-15 17:10:13SilentGhostsetnosy: + amaury.forgeotdarc, belopolsky, meador.inge

versions: + Python 3.6
2016-03-15 02:02:09memeplexcreate