Is it possible to get the Physical memory address of a variable in python?

Carl Banks pavlovevidence at gmail.com
Tue Nov 10 03:30:13 CET 2009


On Nov 9, 4:47 pm, Ognjen Bezanov <Ogn... at mailshack.com> wrote:
> Hello all,
>
> Say I have a python variable:
>
> a = "hello"
>
> Is it possible for me to get the physical address of that variable (i.e.
> where it is in RAM)?
>
> I know that id(a) will give me it's memory address, but the address
> given does not seem to correlate with the physical memory. Is this even
> possible?


I'm going to guess that

A. You don't really want physical address but logical address (i.e.,
the address where you might access the memory with a C pointer), and

B. You want the address not of the object itself, but of the data
within (that is, you'd want the "pointer" you receive to point to the
ASCII string hello)

Python's way to handle cases like this is called buffer protocol, but
it only operates at the C-extension level.  There is a set of
functions in the C API that look like this

PyObject_AsReadBuffer(obj,**buffer,*len)

Calling this will return the address of the string "hello" in
*buffer.   See C API Reference Manual for more details.  This function
can also be called from ctypes, but I don't remember the exact
procedure for modifying input arguments through a pointer.

Note: If you really want a PHYSCIAL RAM address you'll have to make
some OS-specific system call (I think) with the logical address,
probably needing superuser privileges.  Obviously this call isn't
exposed in Python, because there's no use for it in Python.  Unless
you're programming DMA transfers or something like that, there's no
use for it in C, either.


Carl Banks



More information about the Python-list mailing list