检查 PyObjects C 类型
我正在使用 Python 3.2 和 C++。
我需要提取当前存储在 PyObject 中的 C 类型类型。 我已经检查了文档并用谷歌搜索了它,似乎没有其他人需要这样做。
所以我有一个 PyObject 并尝试提取 C 值。我有实际从对象中提取值所需的函数列表,但我首先需要知道对象本身中存储的内容以便调用正确的函数。
以防万一这有助于理解这里是我正在尝试的一些示例代码。
//Variable is a custom variant type to allow for generic functionality.
Variable ExtractArgument( PyObject * arg )
{
Variable value;
PyObject* result;
//now here is the problem, I need to know which Python function to call in order to
//extract the correct type;
value = PyLong_FromLong( arg );
//or
value = PyFloat_FromDouble( arg )
//ect.
return value;
}
希望我能有一些看起来像这样的东西,
Variable ExtractArgument( PyObject * arg )
{
Variable value;
PyObject* result;
//PyType is not the actual variable to that holds the type_macro, GetType should be
//replaced by the function I am trying to find
PyType type = GetType( arg );
switch( type )
{
case T_INT: value = static_cast<int>(PyLong_FromLong( arg ));
break;
case T_FLOAT: value = static_cast<float>(PyFloat_FromDouble( arg ));
break;
case T_DOUBLE: value = PyDouble_FromDouble( arg );
break;
//ect.
}
return value;
}
抱歉,如果这个问题太长,或者信息太多。第一次发帖,不想遗漏任何可能有帮助的内容。 感谢您就这个问题给我的任何帮助或见解。
I am using Python 3.2 and C++.
I need to extract what kind of C type is currently being stored in a PyObject.
I have checked over the documentation and googled it as well and no one else seems to have needed to do this.
So I have a PyObject and am attempting to extract the C Value. I have the list of functions need to actually extract the value from the object, but what I need to know first is what is being stored in the object itself as to call the correct function.
Just in case this helps understand here is some example code of what I am attempting.
//Variable is a custom variant type to allow for generic functionality.
Variable ExtractArgument( PyObject * arg )
{
Variable value;
PyObject* result;
//now here is the problem, I need to know which Python function to call in order to
//extract the correct type;
value = PyLong_FromLong( arg );
//or
value = PyFloat_FromDouble( arg )
//ect.
return value;
}
Hopefully I could have something that looks sort of like this
Variable ExtractArgument( PyObject * arg )
{
Variable value;
PyObject* result;
//PyType is not the actual variable to that holds the type_macro, GetType should be
//replaced by the function I am trying to find
PyType type = GetType( arg );
switch( type )
{
case T_INT: value = static_cast<int>(PyLong_FromLong( arg ));
break;
case T_FLOAT: value = static_cast<float>(PyFloat_FromDouble( arg ));
break;
case T_DOUBLE: value = PyDouble_FromDouble( arg );
break;
//ect.
}
return value;
}
Sorry if this question is to long, or too much info. First time post and didn't want to leave anything out that may help.
Thank you for any help or insight you can give me on this issue.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Python 对象没有 C 类型,它们有 Python 类型。例如,整数可以是C长整数或长整数。您可以使用
PyInt_Check(obj)
、PyList_Check(obj)
等检查类型。如果返回 true,那么您就知道您拥有该类型的对象。请注意,
PyLong_FromLong
等则相反。他们获取 C 值并将其转换为 PyObject*。所以你是在反向使用它们。我认为你的意思是PyInt_AsLong
。Python objects don't have a C type, they have a Python type. For example, an integer can be an C long or a long integer. You can check for the type with
PyInt_Check(obj)
,PyList_Check(obj)
, etc. If that returns true then you know you have an object of that type.Note that
PyLong_FromLong
and such go the other way. They take a C value and turn it into a PyObject*. So you're using them backwards. I think you meantPyInt_AsLong
.这并不完全是 Python 类型的工作方式;它们不会一对一地映射到 C 类型。没有 Python C/API 函数可以告诉您特定值适合哪种 C 类型。但是,有诸如
PyFloat_Check()
和PyInt_Check()
之类的函数检查类型(并考虑子类)。PyArg_ParseTuple()
(及其变体)也有说明符,告诉 Python 适当地转换传入的参数。通常使用的是后者;使用 PyArg_ParseTuple() 解码传递给 C 函数的参数,如果您需要以不同的方式处理不同的类型,请将它们作为不同的参数传递(通常作为命名参数)。目前尚不清楚该方法是否可以为你工作。第二个最常见的事情是尝试使用不同的函数转换参数而不首先进行类型检查,并且在失败时尝试另一个转换函数。显式类型检查通常是最不常见的替代方案。
That's not quite how Python types work; they don't map to C types one-to-one. There is no Python C/API function that will tell you what C type a particular value will fit in. There are, however, functions like
PyFloat_Check()
andPyInt_Check()
that check the type (and consider subclasses as well.) There are also specifiers toPyArg_ParseTuple()
(and its variants) that tell Python to convert a passed-in argument appropriately.The usual thing to use is the latter; decode the arguments passed to a C function with
PyArg_ParseTuple()
, and if you need different types being passed treated differently, pass them as different arguments (usually as named arguments.) It's not clear if that approach can work for you. The second most usual thing to do is try to convert the argument using different functions without doing a typecheck first, and simply try another conversion function when one fails. The explicit typecheck is generally the least common alternative.