【问题标题】:Some confustion about PyClass_Check and PyClass_IsSubclass functions关于 PyClass_Check 和 PyClass_IsSubclass 函数的一些困惑
【发布时间】:2014-03-22 15:43:57
【问题描述】:

我在学习python嵌入式C编程时遇到了问题。

这是我的示例: ReadBuf.c

#include "Python.h"

static PyObject* Test_IsInstance(PyObject* self, PyObject* args){
    PyObject* pTest = NULL;
    PyObject* pName = NULL;
    PyObject* moduleDict = NULL;
    PyObject* className = NULL;
    PyObject* pModule = NULL;

    pName = PyString_FromString("common");
    pModule = PyImport_Import(pName);
    if (!pModule){
        printf("can not find client.py\n");
        Py_RETURN_NONE;
    }

    moduleDict = PyModule_GetDict(pModule);
    if (!moduleDict){
        printf("can not get Dict\n");
        Py_RETURN_NONE;
    }

    className = PyDict_GetItemString(moduleDict, "Test");
    if (!className){
        printf("can not get className\n");
        Py_RETURN_NONE;
    }

    PyObject* subClassName = PyDict_GetItemString(moduleDict, "pyTest");
    if (! subClassName){
        printf("can not get subClassName\n");
    }
    int k = PyClass_IsSubclass(subClassName, className);
    if (!k){
        printf("pyTest is not subclass of Test\n");
    }

    int r = PyClass_Check(className);
    if (!r){
        printf("className is not a class\n");
    } else {
        printf("className is a class\n");
    }
    PyObject* pInsTest = PyInstance_New(className, NULL, NULL);
    PyObject_CallMethod(pInsTest, "py_printT", "()");

    int ok = PyArg_ParseTuple(args, "O", &pTest);
    if (!ok){
        printf("parse tuple error!\n");
        Py_RETURN_NONE;
    }
    if (!pTest){
        printf("can not get the instance from python\n");
        Py_RETURN_NONE;
    }

    PyObject_CallMethod(pTest, "py_print", "()"); 

    if (!PyObject_IsInstance(pTest, className)){
        printf("Not an instance for Test\n");
        Py_RETURN_NONE;
    } else {
        printf("an instance for Test\n");
    }
    Py_RETURN_NONE;
}
static PyMethodDef readbuffer[] = {
    {"testIns", Test_IsInstance, METH_VARARGS, "test for instance!"},
    {NULL, NULL}
};

void initReadBuf(){
    PyObject* m;
    m = Py_InitModule("ReadBuf", readbuffer);
}

common.py

class Test(object):
  def __init__(self):
    print "Test class"
  def py_printT(self):
    print "Test py_print"

class pyTest(Test):
  def __init__(self):
    Test.__init__(self)
    print "pyTest class"
  def py_print(self):
    print "pyTest py_print"

client.py

from common import pyTest
import ReadBuf as rb

b = pyTest()
rb.testIns(b)

我在python2.7.2上执行的时候,结果:

Test class
pyTest class
pyTest is not subclass of Test
className is not a class
New instance error
pyTest py_print
an instance for Test

如果我在python2.4.5上执行,结果:

Test class
pyTest class
pyTest is not subclass of Test
className is not a class
New instance error
pyTest py_print
an instance for Test
Exception exceptions.SystemError: 'Objects/classobject.c:528: bad argument to internal     function' in 'garbage collection' ignored
Fatal Python error: unexpected exception during garbage collection
Aborted (core dumped)

我的问题:

  1. 为什么python2.4.5会抛出异常?

  2. 我定义pyTestTest的子类,为什么结果是pyTest不是Test的子类?

  3. 当我执行PyClass_Check 函数时,为什么className 不再是一个类?

  4. 因为className已经不是一个类了,为什么我执行PyObject_IsInstance,结果是真的?

  5. 如果我像下面这样修改Test类的定义,结果是正常的,为什么?

    class Test:   # don't inherit object any more
    

【问题讨论】:

    标签: python c exception embed python-c-api


    【解决方案1】:

    问题是PyClass_* 函数应该与旧式类一起使用。 当您从object 继承时,该类将成为新样式类,您应该使用PyType_* 函数,列出here

    即:

    在新样式实例上对PyClass_* 函数的所有调用的结果都是虚假的。然而PyObject_IsInstance 既适用于旧式类也适用于新式类,这就解释了为什么它无论如何都会返回True

    显然将class Test(object) 更改为class Test “解决”了奇怪的行为,因为Test 现在是一个旧式类,您应该在其上使用PyClass_* 函数。

    Python2.4 是相当老的python 版本,API 之间可能存在一些不一致。您应该为您使用的每个 python 版本重建 C 扩展。 (最新版本的 python 确实有稳定的 ABI,但我相信没有 python2.4)。

    【讨论】:

    • 有效。感谢您的准确回答。
    猜你喜欢
    • 2019-11-07
    • 1970-01-01
    • 1970-01-01
    • 2014-11-02
    • 2018-04-26
    • 2015-04-08
    • 2016-09-15
    • 2017-11-08
    • 1970-01-01
    相关资源
    最近更新 更多