Access memory address in python
Asked Answered
R

6

43

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.

Renee answered 23/11, 2011 at 23:42 Comment(3)
What exactly is the goal of this?Alves
Python doesn't have pointers that can access arbitrary memory.Reathareave
@GregHewgill: it looks to me like a program can actually access memory in the same way as in other languages (through the ctypes module–see Mark Tolonen's answer).Aldus
S
52

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.

Stylography answered 24/11, 2011 at 0:22 Comment(0)
H
10

In Python, you don't generally use pointers to access memory unless you're interfacing with a C application. If that is what you need, have a look at the ctypes module for the accessor functions.

Hook answered 24/11, 2011 at 0:10 Comment(0)
J
6

"I want to read the content of memory pointed by ptr"

Write code in C. Use the code from Python. http://docs.python.org/extending/extending.html

Jobholder answered 23/11, 2011 at 23:56 Comment(0)
D
2

You can (for example):

import ctypes
example_string = "Hello world!"
address = id(example_string)
string_from_address = ctypes.cast(address , ctypes.py_object).value
print(string_from_address)

What this does is import ctypes like usual, Creates an example string, Gets its address, Uses ctypes to cast it from memory. And finally prints it.

Domingadomingo answered 13/11, 2022 at 12:16 Comment(0)
K
1

Are you trying to "reverse" id to get the Python object thing from the result of id(thing)? I don't even know if that can be done, and it definitely shouldn't be done; it would defeat garbage collection and lead to a lack of memory safety. If your program does that it means you effectively have references to things (the id numbers) that the garbage collector doesn't know are references, so it could release objects you're still using (depending on what happens in the rest of the program).

If you're trying to read raw memory from a pointer that you've got returned from a C extension or something, then the other answers may help you.

Karaite answered 24/11, 2011 at 1:49 Comment(1)
I tested recovering a deleted object by id(thing) a while back simply out of curiosity. It is indeed possible, but (depending on garbage collection) usually results in a double-free on exit.Shaniqua
P
1

If you trying to access large binary files like images and videos and don't necessarily need them in the memory forever, consider using weakref.

Psilocybin answered 1/1, 2020 at 19:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.