473,418 Members | 2,055 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,418 software developers and data experts.

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_method() 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_method:

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_method return objects of type PClass instead of _PClass...

To this end I try something like this

class PClass2(_PClass2):
...
def troubelsome_method(self):
base=_PClass2.troublesome_method(self)
base.__class__=PClass

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

We have then added the Py_TPFLAGS_HEAPTYPE 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__trigger 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 2397
We have then added the Py_TPFLAGS_HEAPTYPE 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_HEAPTYPE 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_HEAPTYPE 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
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...
6
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...
13
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...
4
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...
5
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: ...
16
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...
4
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
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,...
1
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...
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
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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...
0
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...

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.