我可以从其内存地址获取python对象吗? [英] Can I get a python object from its memory address?

查看:182
本文介绍了我可以从其内存地址获取python对象吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习如何将Qt与PyQt一起使用,我有一个带有StandardItemModel的QTabelView我已成功填充模型并将itemChanged信号连接到一个插槽。我想乱搞IPython中返回的任何对象,所以目前我有这条线:

I'm learning how to use Qt with PyQt, and I have a QTabelView with a StandardItemModel I've populated the model successfully and hooked up the itemChanged signal to a slot. I'd l'd like to mess around with whatever object is returned in IPython, so currently I have the line:

def itemChangedSlot(epw, item):
    new_data = item.data()
    print new_data
    print item

打印

<PyQt4.QtGui.QStandardItem object at 0x07C5F930>
<PyQt4.QtCore.QVariant object at 0x07D331F0>

在IPython会话中,是否可以使用此内存地址获取对象?我在Google上没有看到任何内容,也许我没有正确的术语?

In the IPython session is it possible to get the object using this memory address? I'm not seeing anything on Google, maybe I don't have my terminology right?

推荐答案

你几乎肯定会问错误的问题,而Raymond Hettinger的回答几乎肯定是你真正想要的。

You're almost certainly asking the wrong question, and Raymond Hettinger's answer is almost certainly what you really want.

这样的事情可能有助于深入了解CPython解释器的内部用于学习目的或审计它的安全漏洞或某些东西......但即便如此,你可能会更好将Python解释器嵌入到程序中并编写将所需内容暴露给Python解释器的函数,或至少编写一个允许您操作CPython对象的C扩展模块。

Something like this might be useful trying to dig into the internals of the CPython interpreter for learning purposes or auditing it for security holes or something… But even then, you're probably better off embedding the Python interpreter into a program and writing functions that expose whatever you want into the Python interpreter, or at least writing a C extension module that lets you manipulate CPython objects.

但是,你真的需要这样做的机会......

But, on the off chance that you really do need to do this…

首先,没有可靠的方法来从获取地址再版。具有有用的 eval -able表示的大多数对象将为您提供相应的对象。例如,('1',1)的repr是('1',1),不是< tuple at 0x10ed51908> 。此外,即使对于没有有用表示的对象,返回< TYPE at ADDR> 也只是许多类型遵循的未声明约定(以及用户定义类的默认值) ,不是你可以信赖的东西。

First, there is no reliable way to even get the address from the repr. Most objects with a useful eval-able representation will give you that instead. For example, the repr of ('1', 1) is "('1', 1)", not <tuple at 0x10ed51908>. Also, even for objects that have no useful representation, returning <TYPE at ADDR> is just an unstated convention that many types follow (and a default for user-defined classes), not something you can rely on.

但是,由于你可能只关心CPython,你可以依赖 id

However, since you presumably only care about CPython, you can rely on id:

CPython实现细节:这是内存中对象的地址。

CPython implementation detail: This is the address of the object in memory.

(当然,如果你有对象调用 id (或 repr ),你不需要通过指针取消引用它,如果你没有这个对象,它可能是垃圾收集所以没有什么可以解除引用,但也许你还有它,只是不记得你把它放在哪里......)

(Of course if you have the object to call id (or repr) on, you don't need to dereference it via pointer, and if you don't have the object, it's probably been garbage collected so there's nothing to dereference, but maybe you still have it and just can't remember where you put it…)

接下来,你用这个地址做什么?好吧,Python没有公开任何函数来执行与 id 相反的操作。但 Python C API 已有详细记录,如果您的Python是围绕共享库构建,可以通过 ctypes <访问C API。 / code> ,只需加载即可。事实上, ctypes 提供了一个特殊的变量,可以自动加载正确的共享库来调用C API, ctypes.pythonapi

Next, what do you do with this address? Well, Python doesn't expose any functions to do the opposite of id. But the Python C API is well documented—and, if your Python is built around a shared library, that C API can be accessed via ctypes, just by loading it up. In fact, ctypes provides a special variable that automatically loads the right shared library to call the C API on, ctypes.pythonapi.

在非常旧版本的 ctypes 中,您可能必须明确地查找并加载它,例如 pydll = ctypes.cdll.LoadLibrary('/ usr / lib / libpython2.5.so')(这是用于安装在/ usr / lib中的Python 2.5的linux;显然如果有的话这些细节有所不同,确切的命令行会有所不同。)

In very old versions of ctypes, you may have to find and load it explicitly, like pydll = ctypes.cdll.LoadLibrary('/usr/lib/libpython2.5.so') (This is for linux with Python 2.5 installed into /usr/lib; obviously if any of those details differ, the exact command line will differ.)

当然,崩溃Python解释器要比做任何有用的东西要容易得多,但这并非不可能做任何有用的事情,你可以尝试一下它。

Of course it's much easier to crash the Python interpreter doing this than to do anything useful, but it's not impossible to do anything useful, and you may have fun experimenting with it.

这篇关于我可以从其内存地址获取python对象吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆