Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #18979

defining class and subclass in C

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.net!newsfeed.eweka.nl!eweka.nl!feeder3.eweka.nl!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <franke.daniel@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.004
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; '#include': 0.05; 'below)': 0.05; 'received:localnet': 0.07; 'python': 0.08; 'null,': 0.09; 'struct': 0.09; 'subclass': 0.09; 'typedef': 0.09; '-shared': 0.16; '-wall': 0.16; 'pyobject': 0.16; '>>>': 0.18; 'loading': 0.18; 'gcc': 0.23; 'static': 0.24; 'code': 0.25; 'expect': 0.26; 'import': 0.27; 'pass': 0.28; 'class': 0.29; 'null)': 0.30; 'equivalent': 0.30; 'skip:( 20': 0.31; 'received:209.85.214': 0.32; 'thanks': 0.32; 'implement': 0.32; 'header:User-Agent:1': 0.33; 'to:addr:python-list': 0.33; 'daniel': 0.34; 'spent': 0.34; 'all.': 0.34; 'running': 0.35; 'however,': 0.35; 'charset:us- ascii': 0.36; 'but': 0.37; 'received:google.com': 0.37; 'could': 0.37; 'some': 0.38; 'received:209.85': 0.38; "i'd": 0.39; 'point': 0.39; 'received:209': 0.39; 'to:addr:python.org': 0.40; 'back': 0.62; 'here': 0.64; 'kindly': 0.65; 'nights': 0.84
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=from:to:subject:date:message-id:user-agent:mime-version :content-transfer-encoding:content-type; bh=Nm7hVRUBD0EiG9nZ/et2TxNUxRqgNCje0f4Ej10EYcE=; b=cz2CzeDX+eIXoiIYQlQjMv0HM8IPJ0yCeggCTM0yDdkOSNcrMcmaNB0LPJkfAytveX NHTkkAik/YlgdeT6zkj868zDnMfkwwTLmc+5y0dOMFmHRCaH6GWTWUf0zZuOhve56cuC V+vU3kxD+/EzYQGu9yT7FygG7F3Op3Av6ENNg=
From Daniel Franke <franke.daniel@gmail.com>
To python-list@python.org
Subject defining class and subclass in C
Date Sat, 14 Jan 2012 22:15:36 +0100
User-Agent KMail/4.7.3 (Linux/3.0.0-14-generic-pae; KDE/4.7.3; i686; ; )
MIME-Version 1.0
Content-Transfer-Encoding 7Bit
Content-Type text/plain; charset="us-ascii"
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.4751.1326575767.27778.python-list@python.org> (permalink)
Lines 70
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1326575767 news.xs4all.nl 6887 [2001:888:2000:d::a6]:37512
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:18979

Show key headers only | View raw


Hi all.

I spent some days and nights on this already and my google-fu is running out.

I'd like to implement the equivalent of this Python code in a C-extension:

>>> class A(object):
...  pass
>>> class B(A):
...  pass
>>> A
<class '__main__.A'>
>>> B
<class '__main__.B'>
>>> B.__bases__
(<class '__main__.A'>,)

However, loading my C-code (quoted below) I get:

>>> import ca
>>> ca
<module 'ca' from 'ca.so'>
>>> ca.ca
<type 'ca.ca'>

Here I'd expect "<class 'ca.ca'>" instead?! And I never managed a proper 
subclass :|

The point of the excercise: with "ca" and "cb" properly implemented, I'd like 
to subclass "cb" not from "ca", but from the Python "class A" - if possible?!

Could somepne kindly point out my mistake(s) and set me back on track?

Thanks

	Daniel

--
#include <Python.h>

typedef struct {
  PyObject_HEAD
} ca;

static PyTypeObject ca_Type = {
  PyObject_HEAD_INIT(NULL)
};

PyMODINIT_FUNC initca(void) {
  PyObject *ca;

  ca_Type.tp_name      = "ca.ca";
  ca_Type.tp_basicsize = sizeof(ca);
  ca_Type.tp_flags     = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE;

  if (PyType_Ready(&ca_Type) < 0)
    return;

  ca = Py_InitModule3("ca", NULL, "ca module");
  if (ca == NULL)
    return;

  Py_INCREF(&ca_Type);
  PyModule_AddObject(ca, "ca", (PyObject*)&ca_Type);
}

$ gcc -Wall -Wextra -g -fPIC -I/usr/include/python2.7 ca.c \
      -shared -Wl,-soname,ca.so -o ca.so -lpython2.7

Back to comp.lang.python | Previous | NextNext in thread | Find similar | Unroll thread


Thread

defining class and subclass in C Daniel Franke <franke.daniel@gmail.com> - 2012-01-14 22:15 +0100
  Re: defining class and subclass in C Tim Roberts <timr@probo.com> - 2012-01-14 17:26 -0800

csiph-web