我正在写一个C扩展,我很失落如何接受一个dict作为参数.由于文档没有关于如何实现这一点的任何细节,我试图将参数解析为Python对象,然后将其作为dict操作:
PyTypeObject *dict;
if(!PyArg_ParseTuple(args, "o", &dict))
return NULL;
Run Code Online (Sandbox Code Playgroud)
但代码在解析时失败:
Python 2.7.2 (default, Jun 20 2012, 16:23:33)
[GCC 4.2.1 Compatible Apple Clang 4.0 (tags/Apple/clang-418.0.60)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import plotting
>>> plotting.plot({'wff':0})
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: must be impossible<bad format char>, not dict
Run Code Online (Sandbox Code Playgroud)
据我所知,从错误消息格式char是错误的(我认为"o"应代表任何Python对象).
将Python dict解析为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) 如果我具有以下函数,myobj但未传递可选参数,则是否myobj保留NULL或设置为Py_None?
static PyObject * myfunc(PyObject * self, PyObject * args) {
PyObject * myobj = NULL;
if (!PyArg_ParseTuple(args, "|O", &myobj)) {
return NULL;
}
// ...
}
Run Code Online (Sandbox Code Playgroud)
根据解析参数和建立值,
|指示Python参数列表中的其余参数是可选的。与可选参数相对应的C变量应初始化为其默认值-如果未指定可选参数,则PyArg_ParseTuple()不会触及相应C变量的内容。
这适用于PyObject *s吗?显然,它是C中存在的一个指针,因此可以说它是一个C变量,但是它是一个指向python对象的指针,所以也可以说它不算作C变量。
只是找不到合适的功能.谢谢你的建议.
当我从不同的 C 线程调用 C-API 的 Py_Finalize() 而不是我进行 python 调用时,我得到一个错误输出。
我看到的错误是:
Exception ignored in: <module 'threading' from 'C:\\Python34-32\\Lib\\threading.py'>
Traceback (most recent call last):
File "C:\Python34-32\Lib\threading.py", line 1289, in _shutdown
assert tlock.locked()
AssertionError:
Run Code Online (Sandbox Code Playgroud)
这只发生在 Python 3.X(用 3.4.2 测试)中,在 Python 2.7 中完全相同的代码没有任何问题。
这是一个最小的例子,它显示了在使用 C 线程时发生的情况,但不是当所有事情都发生在单个 c 线程上时:
#include <iostream>
#include <fstream>
#include <thread>
#include <cassert>
#include <Python.h>
void make_file()
{
std::fstream file("my_test.py", std::ios::out);
file <<
"import threading\n" <<
"def my_function():\n" <<
" pass\n" ;
file.close();
}
void exec()
{
PyGILState_STATE gstate = PyGILState_Ensure(); …Run Code Online (Sandbox Code Playgroud) 假设我有一个方法"m()"的python对象"o",我想把它传递给:
PyObject *f(PyObject *self, PyObject *args)
{
PyObject *o;
PyArg_ParseTuple(args, "O", &o);
//o.m();
}
Run Code Online (Sandbox Code Playgroud)
显然,最后一个注释行无法编译,我想知道是否有办法实现这一点.
处理PyList_Append时,我在Py_DECREF/INCREF上丢失了.任何人都可以对以下代码发表评论吗?
PyObject * bugmaybe(PyObject *self, PyObject *args)
{
PyObject * trio=PyList_New(0);
PyObject * trio_tmp;
PyObject * otmp = PyFloat_FromDouble(1.2);
PyList_Append(trio_tmp,otmp);
//Py_DECREF(otmp);
otmp = PyFloat_FromDouble(2.3);
PyList_Append(trio_tmp,otmp);
//Py_DECREF(otmp);
PyList_Append(trio,trio_tmp);
Py_INCREF(trio_tmp);
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试调试一个在 32 位 Python 2.7 中运行良好的扩展模块,但在 64 位 Python 3.5 中却不是那么好。
我使用了 Python.org 的 AMD64 Web 安装程序,但在我得到的链接中
__imp_PyModule_Create2 (referenced in libboost_python-vc120-mt-gd-1_57.lib(module.obj))
Run Code Online (Sandbox Code Playgroud)
未解决。这是唯一未解决的符号。
这是故意的吗?我看到一个旧的错误报告,它似乎表明稳定 ABI 免于调试版本。(这就是为什么我在 SO 上发帖而不是提交错误报告)
如果是故意的,是否预计我会先与 python35_d.lib 链接,然后与 python35.lib 链接,还是有另一种方法来解决这个问题?