473,701 Members | 2,763 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

assigning to __class__ for an extension type: Is it still possible?

Hello,

We are currently writing python bindings to an existing C++ library,
and we encountered
a problem that some of you may have solved (or that has found
unsolvable :( ):

A C++ class (let's call it CClass) is binded using classical Python
extension API to _PClass, which is accesible through python without
any
problem. The problem is that I want this class to be extended/
extensible
in python, and expose the python-extended version (PClass) to library
users (_PClass should never be used directly nor be retruned by any
function).
The aim is to leave only performance critical methods in C++ so that
the
binding work is minimal, and develop the other methods in python so
that
they are easier to maintain/extend.

We thus have something like this

class PClass(_PClass) :
def overide_method( self,...):
...
def new_method(self ,...):
...

and I can define
a=PClass()
and use my new or overiden method
a.overide_metho d() a.new_method() as intended...

So far, so good, trouble begin when I have a method from another
class
PClass2 derived from _PClass2 which bind the C++ class CClass2, that
should return objects of type PClass:

Let call this method troublesome_met hod:

b=_PClass2()
c=b.troublesome _method()
type(c) gives _PClass.

Now I want to define a python class PClass2 for extending methods of
_PClass2 like I have done for _PClass, in particular I want that
PClass2
troublesome_met hod return objects of type PClass instead of _PClass...

To this end I try something like this

class PClass2(_PClass 2):
...
def troubelsome_met hod(self):
base=_PClass2.t roublesome_meth od(self)
base.__class__= PClass

and I have python complaining about TypeError: __class__ assignment:
only for heap types...

We have then added the Py_TPFLAGS_HEAP TYPE tp_flag, which turn _PClass
into a heap
class and should make this class assignment possible...or so I though:
When the _PClass is turned into a heaptype, assignent to
__class__trigge r a test in
python's typeobject.c on tp_dealloc/tp_free witch raise an exception
TypeError: __class__ assignment: '_PClass' deallocator differs from
'PClass'
I have commented out this test, just to check what happen, and just
got an error later on
TypeError: __class__ assignment: '_PClass' object layout differs from
'PClass'

It seems thus that this approach is not possible, but problem is that
delegation ( embedding a _PClass instance in
PClass (lets say as _base attribute) and automatically forwarding all
methods calls/setattr/getattr to ._base) is far from ideal...
Indeed, we would have to change all other methods that accepted
_PClass, to give them PClass._base, this means wrapping a large
number of methods (from our c++ bindings) for argument-processing...

This is particularly frustrating cause I also have the impression
that
want we want to do was at one time possible in python, let say in
2002-2003, when __class__ was already assignable but before various
safety checks were implemented (assignmenent only for heaptypes, check
on tp_free/tp_dealloc, layout check,...)

Any hint on this problem?
In particular, a list of condition type A and B have to fullfull so
that a=A(); a.__class__=B is possible would be very nice, especially
when A is an extension type (I think the correct term is static type)
while B is defined by a class statement (dynamic type)...This is
something that is not easy to find in the docs, and seems to have
changed quite a lot between revisions 2.2/2.3/2.4/2.5...

Thanks,

Greg.

May 3 '07 #1
1 2416
We have then added the Py_TPFLAGS_HEAP TYPE tp_flag, which turn _PClass
into a heap
class and should make this class assignment possible...
A precision: it seems that just addind Py_TPFLAGS_HEAP TYPE flag in
the
PyTypeObject tp_flags is not all you have to do to turn a static type
into a heap type: indeed, when doing such in the Noddy examples,
I have a segfault when just typing:
n=Noddy()
n

there is an access to the ht_name slot that is apparently non
initialized...

So Maybe the core of the problem is that I do not define the heap type
correctly....Do anybody have (or can tell me where to find) a small
example of an extension module defining a heap class? Similar to the
Noddy examples from the python doc?
I did not find any concrete example of Py_TPFLAGS_HEAP TYPE in the
current doc or on the net...

Best regards,

Greg.

May 3 '07 #2

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

Similar topics

6
1307
by: Kent Johnson | last post by:
In the Python tutorial section 9.1, it says, "the word ``object'' in Python does not necessarily mean a class instance. Like C++ and Modula-3, and unlike Smalltalk, not all types in Python are classes: the basic built-in types like integers and lists are not, and even somewhat more exotic types like files aren't." Is this still correct or was it made obsolete by Python 2.2? Lists and files have __class__ attributes, at least: >>>...
6
3070
by: gregory lielens | last post by:
Hello, I am currently writing python bindings to an existing C++ library, and I just encountered a problem that I hope has been solved by more experienced python programmers: A C++ class (let's call it CClass) is binded using classical Python extension API to _PClass, which is accesible through python without any problem. The problem is that I want this class to be extended/extensible in python, and expose the python-extended version...
13
1944
by: Frans Englich | last post by:
Hello, I am having trouble with throwing class instances around. Perhaps I'm approaching my goals with the wrong solution, but here's nevertheless a stripped down example which demonstrates my scenario: #------------------------------------------------------------------------------------------ class foo: tests = {}
4
6565
by: Paul McGuire | last post by:
I have some places in pyparsing where I've found that the most straightforward way to adjust an instance's behavior is to change its class. I do this by assigning to self.__class__, and things all work fine. (Converting to use of __new__ is not an option - in one case, the change is temporary, and before the end of the function, I change it back again.) Any comments on this practice? Is this intended capability for Python objects, or...
5
2522
by: Barry Kelly | last post by:
I'm running this version of Python: Python 2.4.3 (#1, May 18 2006, 07:40:45) on cygwin I read in the documentation that these two expressions are interchangeable: x.__getattribute__('name') <==> x.name
16
1935
by: John Salerno | last post by:
Let's say I'm making a game and I have this base class: class Character(object): def __init__(self, name, stats): self.name = name self.strength = stats self.dexterity = stats self.intelligence = stats self.luck = stats
4
3173
by: alf | last post by:
Hi, is there a more elegant way to get o.__class__.__name__. For instance I would imagine name(o). -- alf
18
6773
by: Gabriel Rossetti | last post by:
Hello everyone, I had read somewhere that it is preferred to use self.__class__.attribute over ClassName.attribute to access class (aka static) attributes. I had done this and it seamed to work, until I subclassed a class using this technique and from there on things started screwing up. I finally tracked it down to self.__class__.attribute! What was happening is that the child classes each over-rode the class attribute at their level,...
1
1526
by: The Pythonista | last post by:
I've been wondering for a while about whether assigning to __class__ is bad form or not. Specifically, I mean doing so when some other method of implementing the functionality you're after is available (i.e. using an adapter, or something like the strategy pattern). To give an example and a non-example of what I'm talking about, consider the following recipes from the online Python Cookbook: Ring Buffer:...
0
9233
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8978
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7829
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6575
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5905
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4665
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3104
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2399
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2036
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.