- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有一个用 swig 包装的类的 C++ 代码。我无法修改代码或包装。在 python 中,我使用 ctypes 拥有一个指向所述 C++ 类的实例的指针。如何围绕该指针创建一个 swig 包装器?
我知道 swig 对象拥有一个“this”属性,该属性在内部指向包装的对象,但我找不到将其设置为我手头的指针的方法。
感谢您的帮助!
最佳答案
您可以做到这一点,但这需要大量工作,并且解决使 ctypes 或 SWIG 接口(interface)完整且可用的根本问题比强制互换性要简单得多。 (还值得注意的是,从 SWIG 对象创建 ctypes 对象比从 ctypes 对象创建 SWIG 对象更容易)。
为了说明这一点,我创建了以下头文件,我们将使用 SWIG 对其进行包装:
struct Foo {
double d;
char msg[20];
};
然后我用以下接口(interface)包装它:
%module test
%{
#include "test.h"
%}
// Prove I'm not cheating here - we can't even instantiate this class
%nodefaultctor;
%include "test.h"
我还添加了一个测试函数,供我们从 ctypes 调用,该函数未包含 SWIG:
#include "test.h"
// This function returns a Foo, but is only accessible to ctypes
extern "C" Foo *fun1() {
static Foo f = { 1.234, "Hello" };
return &f;
}
您对 SWIG 包装类的 this
属性的猜测是一个很好的起点,但它并不像仅仅更改它那么简单 - 您插入的对象的类型必须与 SWIG 期望的相匹配。它不仅仅是一个表示为 int 的指针:
repr(test.Foo().this) # You'll need to drop the nodefaultctor directive to see this
"<Swig Object of type 'Foo *' at 0x7f621197d1e0>"
如果我们检查 SWIG 生成的源代码,我们可以看到有一个函数需要一个指针、一些类型信息并为我们创建这些对象:
SWIGRUNTIME PyObject *
SwigPyObject_New(void *ptr, swig_type_info *ty, int own);
让我们暂时忽略 SWIGRUNTIME
默认定义为 static
的事实,为了让我们开始尝试这个运行时,我们可以将其重新定义为 extern
。稍后我们将研究无法做到这一点时的解决方法。
因此,我们的目标是获取“仅 ctypes”函数的输出,并通过更多 ctypes 调用将其传递到 SwigPyObject_New
中,以创建可以与 SWIG 的 this 属性交换的内容模块。
为了调用它,我们通常会调用 SWIG_TypeQuery
来查找要使用的正确的 swig_type_info
。然而,这实际上是一个宏,它扩展为传递一些始终是静态的静态变量。因此我们将使用这个函数:
SWIGRUNTIME swig_type_info *
SWIG_Python_TypeQuery(const char *type)
(具有相同的 SWIGRUNTIME
条件)。
此时我们已经得到了足够的信息,可以交换代理对象的 this 属性,如果我们能够构造捐赠者,就可以完成。 (尽管这会泄漏)。我们可以通过两种方法让这个变得更好:
Monkey patch __init__
inside test.Foo
工作。如果您确实不想重新编译 SWIG 接口(interface)内的 %nodefaultctor
,这是最好的选择:
def patched_init(self, ptr):
self.this = ptr
test.Foo.__init__ = patched_init
创建一个仅具有 __init__
的新类,该类在修改 __class__
属性之前设置 this
属性并使用该属性:
class FooCtypesSwigInterop(object):
def __init__(self, ptr):
self.this = ptr
self.__class__ = test.Foo
当您不想破坏 test.Foo
现有的 __init__
实现时,此选项最有意义。
话虽如此,我们现在可以通过以下方式实现我们最初的目标:
import ctypes
import test
# This *must* happen after the import of the real SWIG module
# 0x4 is RTLD_NOLOAD which ensures that we get the same handle as the Python
# import did, even if it was loaded with RTLD_LOCAL as Python is prone to.
swig_module = ctypes.PyDLL('./_test.so',ctypes.RTLD_LOCAL|0x4)
# Note that we used PyDLL instead of CDLL because we need to retain the GIL
# Setup SWIG_Python_TypeQuery to have the right argument/return types
# (Using void* as a substitute for swig_type_info*)
SWIG_Python_TypeQuery = swig_module.SWIG_Python_TypeQuery
SWIG_Python_TypeQuery.argtypes = [ctypes.c_char_p]
SWIG_Python_TypeQuery.restype = ctypes.c_void_p
# Likewise SwigPyObject_New, using ctypes.py_object though for return
SwigPyObject_New = swig_module.SwigPyObject_New
SwigPyObject_New.argtypes = [ctypes.c_void_p, ctypes.c_void_p, ctypes.c_int]
SwigPyObject_New.restype = ctypes.py_object
# Actually do the type query for the type our ctypes function returns:
SWIGTYPE_p_Foo = SWIG_Python_TypeQuery('Foo*')
print(hex(SWIGTYPE_p_Foo))
# Now the ctypes function itself that returns the type we want SWIG managed
fun1 = swig_module.fun1
fun1.argtypes = []
fun1.restype = ctypes.c_void_p
# Make the actual ctypes call we care about here
result = fun1()
print(hex(result))
# And then create a SwigPyObject for it from the void* return type
# Note that 0 means not owned by SWIG
sresult = SwigPyObject_New(result, SWIGTYPE_p_Foo, 0)
print(repr(sresult))
# This is how we jimmy it back into the this attribute of a SWIG type
class FooCtypesSwigInterop(object):
def __init__(self, ptr):
self.this = ptr
self.__class__ = test.Foo
c = FooCtypesSwigInterop(sresult)
# Finally a usable SWIG object from the ctypes call
print(c.msg)
这一切都可以编译并使用:
swig3.0 -python -c++ -Wall test.i
g++ -shared -o _test.so test_wrap.cxx fun1.cc -Wall -Wextra -fPIC -I/usr/include/python2.7/ -std=c++11 -DSWIGRUNTIME=extern
LD_LIBRARY_PATH=. python run.py
并给我们:
0x7fb6eccf29e0
0x7fb6eccf2640
<Swig Object of type 'Foo *' at 0x7fb6ee436720>
Hello
要解决将 SWIGRUNTIME
定义为 static
的问题,您需要再执行一个步骤。使用调试符号或对您已获得但无法修改的 SWIG 二进制模块进行逆向工程以查找我们需要的两个函数的地址,这两个函数未相对于导出的符号导出。然后,您可以使用它们来构造 ctypes 函数指针,而不是通过名称查找它们。当然,购买/查找/重写 SWIG 模块,或者将缺少的功能添加到 ctypes 接口(interface)可能会更容易。
(最后值得注意的是,虽然如果 SWIG 使用 -builtin
运行,则它似乎不适用于此处,但需要进行一些重大更改才能使此答案起作用)。
关于Python swig - 从 ctypes 指针创建 swig 包装实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41193917/
有没有一种方法可以使用标准类型构造函数(例如 int、set、dict、list、tuple 等)以用户定义的方式将用户定义类的实例强制转换为其中一种类型?例如 class Example:
我知道这个问题在Stackoverflow中有很多问题,但是即使有很多答案,这些答案也帮不了我什么,也没有找到答案。 在我的WebAPP中,它可以正常工作,但是当我将其转换为API时,它失败了(主题标
这个问题已经有答案了: Why does the ternary operator unexpectedly cast integers? (3 个回答) 已关闭 9 年前。 最近遇到一个Java的陷
我尝试使用 FirebaseApp.configure() 配置 Firebase,但遇到以下崩溃: *** Terminating app due to uncaught exception 'c
我有一个自连接员工实体类,其中包含与其自身相关的 id、name 和 ref 列。我想创建它的新实例并将其保存到数据库。 首先我创建了一个 Employee 类的实例并将其命名为 manager。然后
我有一个用于添加新公寓的表单,在该表单中我有一个下拉列表,用户可以在其中选择负责的人员。 显然,当您从下拉列表中选择并尝试保存公寓时,我的应用程序认为该人已被修改。它给了我下面的错误,指示我应该首先保
从 Visualforce 页面,我需要检索我们组织的 salesforce 实例的 URL,而不是 Visual Force URL。 例如我需要https://cs1.salesforce.com
我遇到了一些可能的问题答案,但这是关于从 Hibernate 3.4.0GA 升级到 Hibernate 4.1.8 的问题。所以这曾经在以前的版本下工作,我已经四处搜索了为什么它在这个新版本中出现了
似乎一遍又一遍地问这个问题,我仍然找不到解决我问题的答案。我在下面有一个域模型。每个新创建或更新的“安全用户”都需要我确保其具有配置文件,如果没有,则创建一个新的配置文件并分配给它。 配置文件的要求相
我很难调试为什么 JPA 不级联我的 @ManyToMany 关系。我发现的所有答案都与缺少级联语句有关。但我确实拥有它们并且仍然得到: Caused by: org.hibernate.Transi
Play 服务 API 表明有一个叫做 Instance ID 的东西 但是,在 Android Studio 中包含以下内容后,我无法导入 InstanceID 类 compile "com.goo
我正在使用 Seam 框架。我有 2 个实体: 请求.java @Entity @Table(name = "SRV_REQUEST") public class Request { private
This question处理构建一个适当的Monad来自单子(monad)的实例,但仅在某些约束下 - 例如Set .诀窍是将其包装成 ContT ,它将约束推迟到包装/展开其值。 现在我想对 Ap
我正在尝试执行此查询: StringBuffer sb = new StringBuffer(); sb.append("select p from PointsEntity p " + "where
我试图了解是否可以更改我的 hibernate 配置并使用单个 MySQL 实例(而不是我当前拥有的多个 MySQL 实例): 我有一个使用 hibernate 的 Java 应用程序,与 2 个模式
我有一个选项卡滑动布局,其中包括四个选项卡,每个选项卡都有自己的布局和 fragment ,在我的主要 Activity 布局中,viewpager 参与更改选项卡。特定 View (选项卡)在应用程
我看到很多帖子声称他们正在运行 MySql 的 RDS 实例,但无法连接到该实例,但我没有运行 RDS。 我使用 EC2 实例来托管我的 WordPress 博客,该博客是使用 Web 平台安装程序安
因为我在我的 ec-2 实例上的 python 虚拟环境中运行应用程序( Airflow ),并且我想在同一个 ec2 实例上的默认 python 环境中运行命令,所以我认为 ssh 到我自己的实例更
这个问题已经有答案了: How to fix the Hibernate "object references an unsaved transient instance - save the tra
例子: run APP1 .. ... run APP1 ... run APP2 如何在 APP2 中对 Vue 说我需要调用 APP1?
我是一名优秀的程序员,十分优秀!