关于PyClass_Check和PyClass_IsSubclass函数的一些问题

时间:2014-02-21 08:30:15

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

当我学习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. 当我执行className函数时,为什么PyClass_Check不再是一个类?

  4. className不再是一个班级,为什么我执行PyObject_IsInstance,结果是真的?

  5. 如果我修改Test类的定义,如下所示,结果是正常的,为什么?

    class Test:   # don't inherit object any more
    

1 个答案:

答案 0 :(得分:4)

问题是PyClass_*函数应该只用 和旧式类。 当您从object继承时,该类将成为新式类,您应该使用列出herePyType_*函数。

即:

对新样式实例的所有PyClass_*函数调用的结果都是假的。但是PyObject_IsInstance适用于旧式和新式类,这解释了为什么它仍会返回True

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

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