如何公开这样的"静态"变量
class MyClass:
X = 1
Y = 2
Run Code Online (Sandbox Code Playgroud)
通过C API?PyTypeObject上看起来像它的唯一变量是tp_members,但我在PyMemberDef中看不到任何标志,表明该成员应该是每个类,而不是每个实例.
为了进一步澄清,因为它可能会改变答案,我试图将C枚举暴露给Python,以便枚举
enum MyFlags {
Alpha = 0,
Beta = 1
};
Run Code Online (Sandbox Code Playgroud)
可以在Python中访问:
module.MyFlags.Alpha
module.MyFlags.Beta
Run Code Online (Sandbox Code Playgroud) 我在嵌入Python(2.7.1)的C应用程序中有这个代码:
{
PyObject *user_dict;
PyObject *user_func;
PyObject *result;
PyObject *header_tuple;
PyObject *original_recipients;
PyObject *working_recipients;
if (!Py_IsInitialized())
{
Py_Initialize();
}
if (!expy_exim_dict)
{
PyObject *module = Py_InitModule(expy_exim_module, expy_exim_methods); /* Borrowed reference */
Py_INCREF(module); /* convert to New reference */
expy_exim_dict = PyModule_GetDict(module); /* Borrowed reference */
Py_INCREF(expy_exim_dict); /* convert to New reference */
}
if (!expy_user_module)
{
if (expy_path_add)
{
PyObject *sys_module;
PyObject *sys_dict;
PyObject *sys_path;
PyObject *add_value;
sys_module = PyImport_ImportModule("sys"); /* New Reference */
if (!sys_module)
{
PyErr_Clear();
*return_text = …Run Code Online (Sandbox Code Playgroud) 我有一个简单的python脚本
import _tph
str = u'??????, <b>???!</b>' # Some unicode string with a russian characters
_tph.strip_tags(str)
Run Code Online (Sandbox Code Playgroud)
和C库,编译成_tph.so.这是一个strip_tags功能:
PyObject *strip_tags(PyObject *self, PyObject *args) {
PyUnicodeObject *string;
Py_ssize_t length;
PyArg_ParseTuple(args, "u#", &string, &length);
printf("%d, %d\n", string->length, length);
// ...
}
Run Code Online (Sandbox Code Playgroud)
printf功能打印这个:1080,19.所以,str长度实际上是19个符号,但是从地狱深处我得到的那些1080个字符?
当我打印时string,我得到了我的strnull char,然后是很多垃圾字节.
垃圾内存看起来像这样:
u'\ u041f\u0440\u0438\u0432\u0435\u0442,<b>\u043c\u0438\u0440!</ b>\x00\x00\u0299\Ub7024000\U08c55800\Ub7025904\x00\Ub777351c\U08c79e58\x00\U08c7a0b4\X00\Ub7025904\Ub7025954\Ub702594c\Ub702591c\Ub702592c\Ub7025934\X00\X00\X00
我怎么能在这里得到正常的字符串?
这个错误报告指出,截至2007年6月,Python解释器在使用嵌入式Python解释器在C/C++应用程序中调用Py_Finalize之后不会清理所有已分配的内存.建议在应用程序终止时调用Py_Finalize一次.
此错误报告指出,从版本3.3和2011年3月开始,解释器仍会泄漏内存.
有谁知道这个问题的当前状态?我担心因为我有一个应用程序,每个运行实例多次调用解释器,我遇到内存泄漏.
我已经使用boost :: python来处理引用计数,并清除了在运行之间运行Python程序所创建的所有引用的全局字典.我有一些单身人士课 - 这可能是问题吗?
这是一个易处理的问题还是Python解释器中的错误?
我有一个问题,如何清除由PyList_Append()形成的列表?是否有关于Python/C扩展API函数的文档?谢谢.
我ctypes在Python中使用它来打开一个用C++编写的文件.
我的C++代码:
extern "C" {
void openfile(const char *filename) {
cout<<"File to open for writing = " <<filename<<endl;
FILE *fp = fopen(filename,"w");
fprintf(fp,"writing into file");
fclose(fp);
}
}
Run Code Online (Sandbox Code Playgroud)
我的Python代码:
>>> import ctypes
>>> lib = ctypes.cdll.LoadLibrary('/in/vrtime/mahesh/blue/rnd/software/test/test.so')
>>> outfile = "myfirstfile.txt"
>>> lib.openfile(outfile)
File to open for writing = m
Run Code Online (Sandbox Code Playgroud)
我得到的文件名是m,这是char我文件的第一个字符.
如何将整个字符串传递给C端?
我的makefile:
SHELL := /bin/bash
.PHONY: all
all:
pip install runcython
makecython++ stitch_wrapper.pyx "" "stitch_rects.cpp ./hungarian/hungarian.cpp"
hungarian: hungarian/hungarian.so
hungarian/hungarian.so:
cd hungarian && \
TF_INC=$$(python -c 'import tensorflow as tf; print(tf.sysconfig.get_include())') && \
if [ `uname` == Darwin ];\
then g++ -std=c++11 -shared hungarian.cc -o hungarian.so -fPIC -I $$TF_INC -undefined dynamic_lookup;\
else g++ -std=c++11 -shared hungarian.cc -o hungarian.so -fPIC -I $$TF_INC; fi
Run Code Online (Sandbox Code Playgroud)
我已经安装了
-cython
-runcython
-python-dev
-python3-dev
-cffi
Run Code Online (Sandbox Code Playgroud)
不幸的是,我继续得到错误:
pkg-config: command not found
.cpp:4:20: fatal error: Python.h: No such file or directory …Run Code Online (Sandbox Code Playgroud) 我一直在进行一个项目,在该项目中我想删除boost依赖项并将其替换为Python C API。
我花了一些时间了解Python C API,并且看到了
catch (error_already_set const &)
我阅读了boost文档,但是它解释了它的使用位置。但是我想知道为什么需要它,以及如何使用本机Python C api实现相同的功能。
我正在编写一个生成多个C线程的C程序,每个线程一个Python子解释器。子解释器不共享任何可变的Python变量,它们彼此隔离。(它们确实对从C程序的main()函数公开的公共PyObject(不可变)具有只读访问权限)。
在子解释器之间不共享GIL的情况下,是否可以在Python 3.7或3.8中实现?
这是我一直在尝试的伪代码:
void *spawnInterpreter(void* p) {
…
PyThreadState* save_tstate = PyThreadState_Swap(NULL);
PyThreadState* tstate = Py_NewInterpreter();
PyThreadState_Swap(save_tstate);
//do some Python work (with variables that are NOT shared with other thread’s sub-interpreter
PyRun_SimpleString( . . .);
. . .
}
int main() {
...
pthread_create(&thread1, NULL, spawnInterpreter, “in1”);
pthread_create(&thread2, NULL, spawnInterpreter, "in2");
...
}
Run Code Online (Sandbox Code Playgroud)
我可以使它在3.6中工作(无需获取GIL或PyThreadState在C线程中进行管理),但是在Python 3.7中,我得到了:
[New Thread 0x7ffff5f78700 (LWP 16392)]
Fatal Python error: drop_gil: GIL is not locked
Run Code Online (Sandbox Code Playgroud) 让我说我有一个PyListObject,我想附加一个PyObject然后我可以使用PyList_Append记录在其中的API List Objects C-API.但是对于我的用例,我想要pop一个元素PyListObject(即my_list.pop()在python层中).
但是List Objects C-API文档没有提到有关pop操作的任何内容.
那么有关于PyListPopAPI函数的文档吗?
python ×10
python-c-api ×10
c ×3
cpython ×2
boost-python ×1
c++ ×1
ctypes ×1
cython ×1
embedding ×1
list ×1
python-3.x ×1
ubuntu ×1
unicode ×1