Python copy.deepcopy()失败,不会引发警告,异常或错误 [英] Python copy.deepcopy() fails without raising warning, exception or error

查看:1005
本文介绍了Python copy.deepcopy()失败,不会引发警告,异常或错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题与昨天发布的另一个问题有关,



由于我提到的线程,我一直在试图确定什么对象可以被复制,腌制,封送和什么对象不能。



在这样做的时候,我偶然发现了这个难题:

  new_obj = copy.deepcopy(my_obj)
function_that_uses_my_new_obj(new_obj)

throws:


  function_that_uses_my_new_obj(new_obj)

RuntimeError:已删除内部C ++对象(Pyside.QtGui.QWidget)


因为 my_obj C ++ 对象,可以理解。



但是,当我尝试:

  function_that_uses_my_new_obj(copy.deepcopy(my_obj))

em> 我根本没有任何东西 。程序正常运行到这一行,停止几秒钟,执行停止,该行之后的代码不运行,没有异常/错误/警告被抛出,Python提示符已准备好接受任何新的命令。 / p>

EDIT



由于某种原因,使用 ()方法而不是 deepcopy()

 code> function_that_uses_my_new_obj(copy.copy(my_obj))

会导致抛出相同的异常。因此,必须有一些点 deepcopy 决定停止或正在停止,并触发执行的结束。

解决方案

你的断言my_obj是一个C ++对象看起来很明显false: my_obj 实际上是一个python包装器一个C ++对象。所以代码如:

  widget = QtGui.QWidget()
my_obj = copy.deepcopy $ b

只会创建一个python wrapper 的副本, 。这解释了为什么尝试调用一个复制的包装方法将产生RuntimeError。包装器的副本从未具有相应的底层C ++对象,因此它的行为好像已被删除。



这种事情可以很容易地发生在正常PySide / PyQt代码中。有时,如果你不小心保留对一个对象的引用,Qt可以删除C ++部分,留下一个空包装器。在这种情况下,您将看到完全相同的RuntimeError。


This question is related to another question I posted yesterday, although it is much more general in nature.

Because of the thread I mentionned, I have been trying to determine what objects can be copied, pickled, marshaled and what objects cannot.

While doing that, I stumbled on this puzzle:

new_obj = copy.deepcopy(my_obj)
function_that_uses_my_new_obj(new_obj)

throws:

    function_that_uses_my_new_obj(new_obj)

RuntimeError: Internal C++ object (Pyside.QtGui.QWidget) already deleted

Now, since my_obj is a C++ object, that error I can understand. And the reason for that particular problem is the main topic of the other thread.

However, when I try:

function_that_uses_my_new_obj(copy.deepcopy(my_obj))

I don't get anything at all. The program runs normally to this line, stops there for a few seconds and the execution is stopped, the code after that line is not run, no exception/error/warning is thrown and the Python prompt is ready to accept any new command.

EDIT

For some reason, using the copy() method instead of deepcopy() like so:

function_that_uses_my_new_obj(copy.copy(my_obj))

results in the same exception being thrown. So there has to be some point at which deepcopy decides to stop or is being stopped and that triggers the end of the execution. What I don't get is why nothing is raised to inform the user...

解决方案

Your assertion that "my_obj is a C++ object" looks pretty obviously false: my_obj is actually a python wrapper around a C++ object. So code such as:

    widget = QtGui.QWidget()
    my_obj = copy.deepcopy(widget)

will only create a copy of the python wrapper, leaving the underlying C++ object untouched. This explains why attempting to call one of the copied wrapped methods will produce that RuntimeError. The copy of the wrapper never had a corresponding underlying C++ object, and so it behaves as if it has been deleted.

This kind of thing can happen quite easily in "normal" PySide/PyQt code. Sometimes, if you don't take care to keep a reference to an object on the python side, Qt can delete the C++ part, leaving you with an "empty" wrapper. And in such situations, you will see the exact same RuntimeError.

这篇关于Python copy.deepcopy()失败,不会引发警告,异常或错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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