在C中进行子类化 [英] Subclassing in C

查看:83
本文介绍了在C中进行子类化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我已经在C扩展模块中定义了一个类

(或者更确切地说,我想成为)另一个

类的子类。我这样做只需设置我的子类'的类型对象的tp_base指针

。但是,这会导致暴露基类方法的类中的


但仅限于那些。我的子类的方法是隐藏的。

我显然希望有基础的方法

类和子类的新方法可用。


这里是我使用的代码片段:


静态PyTypeObject MyPyType =

{

PyObject_HEAD_INIT(NULL)

0,/ * ob_size * /

" mymod.MyPyType",/ * tp_name * /

sizeof (MyPyType),/ * tp_basicsize * /

...

(析构函数)MyPyType_dealloc,/ * tp_dealloc * /

...

Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,/ * tp_flags * /

...

MyPyType_methods,/ * tp_methods * /

MyPyType_members,/ * tp_members * /

...

(initproc)MyPyType_init,/ * tp_init * /

0,/ * tp_alloc * /

MyPyType_new,/ * tp_new * /

};

PyMODINIT_FUNC initmymod(无效)

{

MyPyType.tp_new = MyPyType_new;

MyPyType.tp_base = pointerToOtherPyType;

if(PyType_Ready(& MyPyType)< 0)

return;

}


在python代码中使用MyPyType时,

MyPyType_methods中定义的方法不可见。如果我注释掉设置tp_base的

行,那么它们会变得可见但是

基类方法不可用。什么是正确的

方式做这种事情?


问候,

Iker

Hello all,

I''ve defined a class in a C extension module that is
(or rather, I would like to be) a subclass of another
class. I do this by simply setting the tp_base pointer
of my subclass''s type object. However, this results
in a class that exposes the methods of the base class,
but ONLY those. The methods of my subclass are hidden.
I''d obviously like to have the methods of the base
class AND the new methods of the subclass available.

Here''s the code snippet I used:

static PyTypeObject MyPyType =
{
PyObject_HEAD_INIT(NULL)
0, /* ob_size */
"mymod.MyPyType", /* tp_name */
sizeof(MyPyType), /* tp_basicsize */
...
(destructor)MyPyType_dealloc, /* tp_dealloc */
...
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
...
MyPyType_methods, /* tp_methods */
MyPyType_members, /* tp_members */
...
(initproc)MyPyType_init, /* tp_init */
0, /* tp_alloc */
MyPyType_new, /* tp_new */
};
PyMODINIT_FUNC initmymod(void)
{
MyPyType.tp_new = MyPyType_new;
MyPyType.tp_base = pointerToOtherPyType;
if (PyType_Ready(&MyPyType) < 0)
return;
}

When using MyPyType in python code, the methods defined in
MyPyType_methods are not visible. If I comment out the
line that sets tp_base then they become visible but the
base class methods are unavailable. What''s the correct
way to do this sort of thing?

Regards,
Iker

推荐答案

Iker Arizmendi< ik ** @ research.att.com>写道:
Iker Arizmendi <ik**@research.att.com> writes:
大家好,

我已经在C扩展模块中定义了一个类
(或者更确切地说,我想要是)另一个类的子类。我这样做只需设置我的子类'的类型对象的tp_base指针
。但是,这会导致暴露基类方法的类,但仅限于那些。我的子类的方法是隐藏的。
我很明显希望有基类的方法和子类的新方法。
Hello all,

I''ve defined a class in a C extension module that is
(or rather, I would like to be) a subclass of another
class. I do this by simply setting the tp_base pointer
of my subclass''s type object. However, this results
in a class that exposes the methods of the base class,
but ONLY those. The methods of my subclass are hidden.
I''d obviously like to have the methods of the base
class AND the new methods of the subclass available.



您是否将tp_getattro设置为PyObject_GenericGetAttr? (只需一个

猜)。


干杯,

mwh


- -

< dash>它就像一辆自行车

< dash>但是有了互联网 - 来自Twisted.Quotes



Are you setting tp_getattro to PyObject_GenericGetAttr? (just a
guess).

Cheers,
mwh

--
<dash> it''s like a bicycle
<dash> but with internet -- from Twisted.Quotes


我在我的子类(*)中将tp_getattr设置为0,但当

我设置它到PyObject_GenericGetAttr Python崩溃了。什么

做PyObject_GenericGetAttr会有什么帮助?

这个案例?


问候,

Iker


(*)我派生的类是在另一个

扩展模块中定义的,它有自己的tp_getattr方法。

这里是堆栈跟踪(通过Valgrind),以防万一:


流程以信号11的默认动作终止(SIGSEGV)

不在地址0x65C9的映射区域内访问

在0x1B97CAC3:PyType_IsSubtype(在/usr/lib/libpython2.3.so.1.0)

由0x1B96C92D:PyObject_GenericGetAttr(在/中usr / lib / libpython2.3.so.1.0)

by 0x1B96C5E8:PyObject_GetAttr(在/usr/lib/libpython2.3.so.1.0)

by 0x1B9A51D2: (在/usr/lib/libpython2.3.so.1.0内)

by 0x1B9A65E2:PyEval_EvalCodeEx(在/usr/lib/libpython2.3.so.1.0中)

by 0x1B9A31B4:PyEval_EvalCode(在/usr/lib/libpython2.3.so.1.0中)

by 0x1B9D4D20 :(在/usr/lib/libpython2.3.so中.1.0)

by 0x1B9D41EE:PyRun_SimpleFileExFlags(在/usr/lib/libpython2.3.so.1.0中)

by 0x1B9D3BEF:PyRun_AnyFileExFlags(在/ usr / lib / libpython2中) .3.so.1.0)

by 0x1B9DC7A3:Py_Main(在/usr/lib/libpython2.3.so.1.0)

by 0x8048640:main(在/ usr中) /bin/python2.3)

I was setting tp_getattr to 0 in my subclass (*), but when
I set it to PyObject_GenericGetAttr Python crashed. What
does PyObject_GenericGetAttr do that would help in
this case?

Regards,
Iker

(*) The class I''m deriving from is defined in another
extension module and it has its own tp_getattr method.
Here''s the stack trace (via Valgrind), just in case:

Process terminating with default action of signal 11 (SIGSEGV)
Access not within mapped region at address 0x65C9
at 0x1B97CAC3: PyType_IsSubtype (in /usr/lib/libpython2.3.so.1.0)
by 0x1B96C92D: PyObject_GenericGetAttr (in /usr/lib/libpython2.3.so.1.0)
by 0x1B96C5E8: PyObject_GetAttr (in /usr/lib/libpython2.3.so.1.0)
by 0x1B9A51D2: (within /usr/lib/libpython2.3.so.1.0)
by 0x1B9A65E2: PyEval_EvalCodeEx (in /usr/lib/libpython2.3.so.1.0)
by 0x1B9A31B4: PyEval_EvalCode (in /usr/lib/libpython2.3.so.1.0)
by 0x1B9D4D20: (within /usr/lib/libpython2.3.so.1.0)
by 0x1B9D41EE: PyRun_SimpleFileExFlags (in /usr/lib/libpython2.3.so.1.0)
by 0x1B9D3BEF: PyRun_AnyFileExFlags (in /usr/lib/libpython2.3.so.1.0)
by 0x1B9DC7A3: Py_Main (in /usr/lib/libpython2.3.so.1.0)
by 0x8048640: main (in /usr/bin/python2.3)

Michael Hudson wrote:
Iker Arizmendi< ik ** @ research.att.com>写道:

Iker Arizmendi <ik**@research.att.com> writes:

大家好,
我已经在C扩展模块中定义了一个类
(或者更确切地说) ,我想成为另一个
类的子类。我这样做只需设置我的子类'的类型对象的tp_base指针
。但是,这会导致暴露基类方法的类,但仅限于那些。我的子类的方法是隐藏的。
我显然希望有基类的方法和子类的新方法。
Hello all,

I''ve defined a class in a C extension module that is
(or rather, I would like to be) a subclass of another
class. I do this by simply setting the tp_base pointer
of my subclass''s type object. However, this results
in a class that exposes the methods of the base class,
but ONLY those. The methods of my subclass are hidden.
I''d obviously like to have the methods of the base
class AND the new methods of the subclass available.



您是否将tp_getattro设置为PyObject_GenericGetAttr? (只是一个猜测)。

干杯,
mwh


Are you setting tp_getattro to PyObject_GenericGetAttr? (just a
guess).

Cheers,
mwh



Iker Arizmendi< ik * *@research.att.com>写道:
Iker Arizmendi <ik**@research.att.com> writes:
我在我的子类(*)中将tp_getattr设置为0,但是当我将它设置为PyObject_GenericGetAttr时,Python崩溃了。 PyObject_GenericGetAttr做什么可以帮助
这种情况?


好​​吧,它确实是通常的属性访问的东西。描述符&所以

on。它是什么对象.__ getattribute__包裹。

问候,Iker

(*)我从中得到的类是在另一个中定义的/>扩展模块,它有自己的tp_getattr方法。


嗯。我接受它*它是* tp_getattr [o]方法不是

PyObject_GenericGetAttr那么?


然后你的初始帖子更有意义;恐怕我没有看到任何明显的理由让PyObject_GenericGetAttr崩溃。

这里是堆栈跟踪(通过Valgrind),以防万一:
I was setting tp_getattr to 0 in my subclass (*), but when
I set it to PyObject_GenericGetAttr Python crashed. What
does PyObject_GenericGetAttr do that would help in
this case?
Well, it does "the usual" attribute access stuff. Descriptors & so
on. It''s what object.__getattribute__ wraps.
Regards,
Iker

(*) The class I''m deriving from is defined in another
extension module and it has its own tp_getattr method.
Hmm. I take it *it''s* tp_getattr[o] method isn''t
PyObject_GenericGetAttr then?

Then your initial post makes more sense; I''m afraid I don''t see
any obvious reason for PyObject_GenericGetAttr to crash.
Here''s the stack trace (via Valgrind), just in case:




来自gdb的堆栈跟踪(即带行号)将更多

使用。


我认为你有两个选择:(a)与gdb交朋友或(b)

发布[链接到,也许]完整代码。


干杯,

mwh

-

那些有不正常标点符号欲望的人应该照顾他们的/>
自己的变态需求。 - Erik Naggum,comp.lang.lisp



A stack trace from gdb (i.e. with line numbers) would have been more
use.

I think you have two options here: (a) make friends with gdb or (b)
post [a link to, maybe] complete code.

Cheers,
mwh

--
Those who have deviant punctuation desires should take care of their
own perverted needs. -- Erik Naggum, comp.lang.lisp


这篇关于在C中进行子类化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆