首页 / PYTHON / 从C中查找python函数参数
从C中查找python函数参数
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了从C中查找python函数参数,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3405字,纯文字阅读大概需要5分钟。
内容图文
![从C中查找python函数参数](/upload/InfoBanner/zyjiaocheng/791/dad2eed2b1524ad592c596fc04bf032e.jpg)
我从C调用python函数.我想知道是否可以确定参数的数量和这些参数的名称.我已经阅读了链接How to find the number of parameters to a Python function from C?,但我真的不明白.
我有这个C函数从pyFunction.py调用函数’add’. ‘add’接受两个参数并返回总和.
static float CallPythonFunc( float *parameters )
{
PyObject *pName, *pModule, *pDict, *pFunc, *pValue, *pArgs;
float ret;
// Initialize the python interpreter
Py_Initialize();
// Make sure we are getting the module from the correct place
// ### This is where we will put the path input
PyRun_SimpleString("import sys");
PyRun_SimpleString("sys.path.append(\"/Developer/IsadoraSDK/IsadoraDemoMathFunction/\")");
// Build the name object
// ### This is where we will put the function input
pName = PyString_FromString("pyFunction");
// Load the module object
pModule = PyImport_Import(pName);
// pDict is a borrowed reference
pDict = PyModule_GetDict(pModule);
// pFunc is a borrowed reference
pFunc = PyDict_GetItemString(pDict, "add");
//
// Somehow get the number of arguments and possible the arguments names from 'add'
//
if (PyCallable_Check(pFunc))
{
// Set the number of arguments
// This is where I would like to pass in number of arguments
pArgs = PyTuple_New( 2 /*number of arguments*/ );
//
// Instead of the following if I had the arguments I could loop through them
// and pass the correct number in
//
// Argument 1
pValue = PyFloat_FromDouble((double)parameters[0]);
PyTuple_SetItem(pArgs, 0, pValue);
// Argument 2
pValue = PyFloat_FromDouble((double)parameters[1]);
PyTuple_SetItem(pArgs, 1, pValue);
// Make the call to the function
pValue = PyObject_CallObject(pFunc, pArgs);
// Set return value
ret = (float)PyFloat_AsDouble(pValue);
// Clean up
Py_DECREF(pArgs);
Py_DECREF(pValue);
}
// Clean up
Py_DECREF(pModule);
Py_DECREF(pName);
// Finish the Python Interpreter
Py_Finalize();
return ret;
}
我真的不熟悉C/C++所以任何帮助都会非常有用.感谢大家的时间!
编辑:
那么类似下面的内容?
PyObject *tuple, *arglist;
tuple = PyObject_CallMethod(pFunc,"inspect.getargspec","add");
arglist = PyTuple_GetItem(tuple,0);
int size = PyObject_Size(arglist);
解决方法:
This answer to the question you linked to似乎是你想要的. inspect.getargspec完全符合你想要的Python方面,并且作为答案陈述,你可以使用PyObject_CallMethod或该链接目标中描述的一个相关函数从你的C代码调用inspect.getargspec,得到返回的元组作为一个PyObject,使用PyTuple_GetItem(returned_tuple,0)来获取参数列表,然后使用列表中的PyObject_Size()或PyObject_Length()来获取参数的数量.您还需要检查返回的元组的第二个和第三个元素,并为不是Py_None的两个元素中的每一个增加参数数量1.请参阅以下代码段了解原因.
>>> import inspect
>>> def testfunc(a, b, c, *d, **e):
pass
>>> inspect.getargspec(testfunc)
ArgSpec(args=['a', 'b', 'c'], varargs='d', keywords='e', defaults=None)
以下是您应该做的事情的示例(并非所有可能的错误都可以检查,但它应该是可能需要的所有NULL检查):
PyObject *pName, *pInspect, *argspec_tuple, *arglist;
int size;
pName = PyString_FromString("inspect");
if (pName)
{
pInspect = PyImport_Import(pName);
Py_DECREF(pName);
if (pInspect)
{
pName = PyString_FromString("getargspec");
if (pName)
{
argspec_tuple = PyObject_CallMethodObjArgs(pInspect, pName, pFunc, NULL);
Py_DECREF(pName);
if (argspec_tuple)
{
arglist = PyTuple_GetItem(argspec_tuple, 0);
if (arglist)
{
size = PyObject_Size(arglist)
+ (PyTuple_GetItem(argspec_tuple, 1) == Py_None ? 0 : 1)
+ (PyTuple_GetItem(argspec_tuple, 2) == Py_None ? 0 : 1); // Haven't actually tested this, but it should work
}
}
}
}
}
内容总结
以上是互联网集市为您收集整理的从C中查找python函数参数全部内容,希望文章能够帮你解决从C中查找python函数参数所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。