My question is: How can I read the content of a memory address in python? example: ptr = id(7) I want to read the content of memory pointed by ptr. Thanks.
Have a look at ctypes.string_at. Here's an example. It dumps the raw data structure of a CPython integer.
from ctypes import string_at
from sys import getsizeof
a = 0x7fff
print(string_at(id(a),getsizeof(a)).hex())
Output:
0200000000000000d00fbeaafe7f00000100000000000000ff7f0000
Note that this works with the CPython implementation because id()
happens to return the virtual memory address of a Python object, but this is not guaranteed by the Python language itself.