简体   繁体   English

分配给通过Python CType从共享库导出的变量?

[英]Assign to variable exported from shared library via Python CTypes?

I have a shared lib libtest.so made from test.c : 我有一个由test.c制成的共享库libtest.so

#include <stdio.h>
int value;
void fn() { printf("%d\n", value); }

Now I'm wrapping this library from Python using CTypes. 现在,我使用CTypes从Python包装此库。 I know that I can read value from Python as follows: 我知道我可以从Python 读取 value ,如下所示:

In[2]: c_int.in_dll(lib, "value")
Out[2]: c_int(0)

(I learnt the above from Mapping a global variable from a shared library with ctypes .) (我从使用ctypes的共享库映射全局变量中学到了以上内容。)

However, if I try to write to the same, then it complains: 但是,如果我尝试相同的内容,则会抱怨:

In[3]: c_int.in_dll(lib, "value") = 2
  File "<ipython-input-4-5a1e8ea6a1b4>", line 1
    c_int.in_dll(lib, "value") = 2
                                  ^
SyntaxError: can't assign to function call

Presumably this is because the c_int object doesn't directly "point" to the C memory location. 大概是因为c_int对象没有直接“指向” C存储器位置。

Of course, I can write and export a setValue function that can receive data from Python to modify value from within C, but I am wondering whether there is any way to do without that. 当然,我可以写和导出setValue可以从Python中接收数据修改函数value从C,但我想知道是否有什么办法没有 ,这样做。

Assigning Python function to ctypes pointer variable seems to indicate this is not possible, but would like a specific answer hence posting the specific question. 将Python函数分配给ctypes指针变量似乎表明这是不可能的,但是想要特定的答案,因此发布特定的问题。

Your c_int does point at the value of test.c . 您的c_int 确实指向test.cvalue You will want to write to the value attribute of the c_int to write to the underlying memory that the c_int wraps. 您将需要写入c_intvalue属性以写入c_int包装的基础内存。 Otherwise you would simply be replacing one Python object with another in Python-land and not having any effect in C land. 否则,您将只是在Python区域中用一个Python对象替换另一个Python对象,而在C区域中则没有任何作用。 For example: 例如:

>>> import ctypes
>>> i = ctypes.c_int()
>>> i
c_int(0)
>>> i.value = 10
>>> i
c_int(10)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 ctypes 从共享库映射全局变量 - Mapping a global variable from a shared library with ctypes 通过ctypes在python中加载共享库(cuda),无法动态加载 - loading a shared library (cuda) in python via ctypes, cannot dynamically load 如何调用将 python 方法动态分配给从共享库导出的 C 指针 function - how to call assign dynamically a python method to a C pointer function exported from a shared library 在Python中使用带有ctypes的共享库中的c结构 - using c structures from a shared library with ctypes in Python 从C共享库访问python ctypes结构 - access python ctypes structure from c shared library 当我的共享库由 icc -O3 或 -O2 优化并通过 Python ctypes 使用时,会发生段错误 - Segfault occurs when my shared library is optimized by icc -O3 or -O2 and used via Python ctypes 当通过 Python ctypes 调用时,如何在共享库中强制 malloc 失败 - How to force malloc failure in shared library when called via Python ctypes 在python程序中通过ctypes使用共享库和线程本地存储时内存泄漏 - Memory leak when using shared library with thread local storage via ctypes in a python program 使用 python ctypes 将浮点缓冲区从共享库获取到 python 字符串 - Using python ctypes to get buffer of floats from shared library into python string 使用 ctypes 模块从 python 调用的共享库中捕获打印输出 - Capturing print output from shared library called from python with ctypes module
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM