473,382 Members | 1,745 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Using alternative PyObject* in dynamically created classes

I would like some of my _dynamically_ created new-style classes to
have instances which are not represented by the standard PyObject, but
one which has an extra void* slot which I want to use for my nefarious
purposes, in extension functions. The pointer need not be initialized
(for now) I just want it to be there in case I want to stuff something
in there later on in the run.

What is the laziest way of achieving this?

As far as I can tell, PyTypeObject (and its allocators, deallocators,
methods in general) only refer to the type of the instance struct via
tp_basicsize (where only the struct's size is held).

So my first attempt merely resets tp_basicsize to the size of my
alternative instance struct, as soon as the class is created. This
goes horribly wrong when instances of the class are deallocated, but I
can't see why.

Where else must I provide information about the instance struct type ?
[I will be away for a few days, from a few hours from now; please
don't take a long response time as a sign of rudeness.]
Jul 18 '05 #1
6 1738
Jacek Generowicz <ja**************@cern.ch> writes:
I would like some of my _dynamically_ created new-style classes to
have instances which are not represented by the standard PyObject, but
one which has an extra void* slot which I want to use for my nefarious
purposes, in extension functions. The pointer need not be initialized
(for now) I just want it to be there in case I want to stuff something
in there later on in the run.

What is the laziest way of achieving this?

As far as I can tell, PyTypeObject (and its allocators, deallocators,
methods in general) only refer to the type of the instance struct via
tp_basicsize (where only the struct's size is held).

So my first attempt merely resets tp_basicsize to the size of my
alternative instance struct, as soon as the class is created. This
goes horribly wrong when instances of the class are deallocated, but I
can't see why.

Where else must I provide information about the instance struct type ?
[I will be away for a few days, from a few hours from now; please
don't take a long response time as a sign of rudeness.]

Hmm ... no takers ?

:-(

Jul 18 '05 #2
Jacek Generowicz <ja**************@cern.ch> writes:
Jacek Generowicz <ja**************@cern.ch> writes:
I would like some of my _dynamically_ created new-style classes to
have instances which are not represented by the standard PyObject, but
one which has an extra void* slot which I want to use for my nefarious
purposes, in extension functions. The pointer need not be initialized
(for now) I just want it to be there in case I want to stuff something
in there later on in the run.

What is the laziest way of achieving this?

As far as I can tell, PyTypeObject (and its allocators, deallocators,
methods in general) only refer to the type of the instance struct via
tp_basicsize (where only the struct's size is held).

So my first attempt merely resets tp_basicsize to the size of my
alternative instance struct, as soon as the class is created. This
goes horribly wrong when instances of the class are deallocated, but I
can't see why.

Where else must I provide information about the instance struct type ?
[I will be away for a few days, from a few hours from now; please
don't take a long response time as a sign of rudeness.]

Hmm ... no takers ?

:-(


Can't you simply create a new, subclassable Python type with an
additonal void* slot, and derive your classes from it? Where's the
problem?

Thomas
Jul 18 '05 #3
Thomas Heller <th*****@python.net> writes:
Jacek Generowicz <ja**************@cern.ch> writes:
Jacek Generowicz <ja**************@cern.ch> writes:
So my first attempt merely resets tp_basicsize to the size of my
alternative instance struct, as soon as the class is created. This
goes horribly wrong when instances of the class are deallocated, but I
can't see why.

OK ... type_new augments the size it finds in tp_basicsize with space
for extra slots ... so resetting tp_basicsize once the type has been
created is indeed too late.
Can't you simply create a new, subclassable Python type with an
additonal void* slot, and derive your classes from it? Where's the
problem?


You mean statically create an extension type with the appropriate size
in the tp_basicsize, and dynamically create types which inherit from
it?

Sounds like it should work.

I'll give it a go, as I now see that my initial approach is guaranteed
to be wrong.

Thanks.
Jul 18 '05 #4

On 23-nov-04, at 14:07, Jacek Generowicz wrote:
Jacek Generowicz <ja**************@cern.ch> writes:
I would like some of my _dynamically_ created new-style classes to
have instances which are not represented by the standard PyObject, but
one which has an extra void* slot which I want to use for my nefarious
purposes, in extension functions. The pointer need not be initialized
(for now) I just want it to be there in case I want to stuff something
in there later on in the run.

What is the laziest way of achieving this?

As far as I can tell, PyTypeObject (and its allocators, deallocators,
methods in general) only refer to the type of the instance struct via
tp_basicsize (where only the struct's size is held).

So my first attempt merely resets tp_basicsize to the size of my
alternative instance struct, as soon as the class is created. This
goes horribly wrong when instances of the class are deallocated, but I
can't see why.

Where else must I provide information about the instance struct type ?
[I will be away for a few days, from a few hours from now; please
don't take a long response time as a sign of rudeness.]

Hmm ... no takers ?

:-(


Appearently not :-).

Do you want to create those classes in Python, or from your extension
module? If you create the class in C its easy to add a hidden slot. You
can even add a field to the type object that contains the location of
that hidden slot.

Ronald

Jul 18 '05 #5
Jacek Generowicz <ja**************@cern.ch> writes:
Thomas Heller <th*****@python.net> writes:
Jacek Generowicz <ja**************@cern.ch> writes:
....
Can't you simply create a new, subclassable Python type with an
additonal void* slot, and derive your classes from it? Where's the
problem?


You mean statically create an extension type with the appropriate size
in the tp_basicsize, and dynamically create types which inherit from
it?

Sounds like it should work.

I'll give it a go, as I now see that my initial approach is guaranteed
to be wrong.


Given that type_new builds the contents of the tp_basicsize slot on
the basis of the supreclass's tp_basicsize, it seems that setting the
size in a superclass is the only way of getting some non-standard size
in there, without re-writing the whole of type_new.

So, I tried this approach. It works.
Jul 18 '05 #6
Ronald Oussoren <ro************@mac.com> writes:
Do you want to create those classes in Python, or from your extension
module?
Both actually.
If you create the class in C its easy to add a hidden slot.


If you create the class statically. I need to be able to create them
dynamically, by calling type.__new__.

Anyway, the way forward seems to be to create a base class with the
appropriate slot, and dynamically create subclasses of it, as Thomas
suggested. type_new adds some slots to whatever it finds in the
superclass' tp_basicsize.
Jul 18 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Phil | last post by:
Hi, I don't understand this strange behaviour: I compile this code : #include <Python.h> #include"Numeric/arrayobject.h" static PyObject *
5
by: eq | last post by:
Hi, I'm trying to create a program(written in C) that does the following things using embedded Python: 1. Create a module(say, "MyModule") 2. Create a class in that module(say, "MyClass") 3....
3
by: Lars Moastuen | last post by:
Im trying to extend a class written in C++ in Python and use this extended class in a C++ call... I made an example to clarify: -- Foo.h -- #ifndef FOO_H #define FOO_H #include <iostream>...
1
by: David | last post by:
I have this error message poping up when I try to import a module I made in C using the Python/C API. Everything compiles like a charm. Gives me this error message : Traceback (most recent...
0
by: John Crowley | last post by:
I'm having an odd problem with viewstate and a dynamically created control inside a repeater template. Basically, I have a repeater setup like this in the aspx:
1
by: Kamal Jeet Singh | last post by:
Hi Friends !! I am facing problem in controlling the dynamically created controls on web page. The problem Scenario is Scenario:- My requirement is to load the web user controls on the web...
2
by: Martin v. Löwis | last post by:
I've been working on PEP 353 for some time now. Please comment, in particular if you are using 64-bit systems. Regards, Martin PEP: 353 Title: Using ssize_t as the index type Version:...
7
by: wardm | last post by:
I have created a Dict object in a C++ App that calls (embedded) Python functions. The Dict is to be used to pass variable data between the C++ App and the python functions. However I cannot get...
2
by: goetzie | last post by:
I am using Python 2.4.1 and Numeric 23.8 and running on Windows XP. I am passing a Numeric array of strings (objects) to a C Extension module using the following python code: import Numeric...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.