Can't override class |__new__

J

jelle feringa

Hi,

I'm working with a C++ module ( CGAL, comp.geom. with exact arithmic )
and am having troubles finding a way to override how the modules returns
objects. What I'm trying to do is to extend the Facet class, but when I try
to use my version of the class, the parent class is still being returned

import CGAL

CGAL.Facet = OtherFacet
CGAL.Polyhedron.Facet = OtherFacet

p = CGAL.Polyhedron_3()
p.make_triangle()

for f in p.facets:
print f

<CGAL.Polyhedron.Facet object at 0x47ed50>
# Here I was expecting a OtherFacet object

Is there a way of getting around this, or is this behaviour hardcoded in the
C++ module?
Can I perhaps alter it using the new module or __new__ method?

Many thanks in advance,

-jelle
 
A

Aaron Brady

Hi,

I'm working with a C++ module ( CGAL, comp.geom. with exact arithmic )
and am having troubles finding a way to override how the modules returns
objects. What I'm trying to do is to extend the Facet class, but when I try
to use my version of the class, the parent class is still being returned

import CGAL

CGAL.Facet = OtherFacet
CGAL.Polyhedron.Facet = OtherFacet

p = CGAL.Polyhedron_3()
p.make_triangle()

for f in p.facets:
    print f

<CGAL.Polyhedron.Facet object at 0x47ed50>
# Here I was expecting a OtherFacet object

Is there a way of getting around this, or is this behaviour hardcoded in the
C++ module?
Can I perhaps alter it using  the new module or __new__ method?

Many thanks in advance,

-jelle

Hello. I don't have your module, but it sounds like the C is doing
something like this (pseudocode):

PyObject *make_triangle(...) {
...
new_ob= PyObject_New( &CGAL_Polyhedron_Facet_Type );
...
}

Regardless of CGAL's dictionary, it instantiates a Facet. What you
want it to do is (pseudocode):

PyObject *make_triangle(...) {
...
/* get the current 'Facet' member */
class_ob= PyObject_GetAttr( CGAL, "Facet" );
/* instantiate it */
new_ob= PyObject_New( class_ob );
...
}

Depending on the details, you may need only to cut-and-paste your own
'make_triangle' function, and just replace the line I showed.

Do you have a link to the source for CGAL?
 
J

jelle feringa

Hi Aaron,

Thanks so much for your feedback.
Regardless of CGAL's dictionary, it instantiates a Facet.

True, when I add attributes to it, they are disregarded when looping
through the facets later on.
Depending on the details, you may need only to cut-and-paste your own
'make_triangle' function, and just replace the line I showed.

Ok, thanks so much for pointing me in the right direction.
I suppose there is no way to fix this on the interpreter level...
Thanks so much for your help in finding this bug!
Do you have a link to the source for CGAL?

In fact I'm using the python wrappers of CGAL.
Here's a link to the Polyhedron module.
In fact, I can add methods and attributes to this class, just the Facet instances
do not behave as expected.

https://gforge.inria.fr/plugins/scmsvn/viewcvs.php/trunk/cgal-
python/bindings/Polyhedron/Py_Polyhedron_3.cpp?rev=167&root
=cgal-python&view=markup

Again, thanks for you help,

-jelle
 
L

Lie Ryan

jelle said:
Hi,

I'm working with a C++ module ( CGAL, comp.geom. with exact arithmic )
and am having troubles finding a way to override how the modules returns
objects. What I'm trying to do is to extend the Facet class, but when I try
to use my version of the class, the parent class is still being returned

import CGAL

CGAL.Facet = OtherFacet
CGAL.Polyhedron.Facet = OtherFacet

p = CGAL.Polyhedron_3()

You're not creating Facet object here, not even Polyhedron.Facet.
p.make_triangle()

for f in p.facets:
print f

<CGAL.Polyhedron.Facet object at 0x47ed50>
# Here I was expecting a OtherFacet object

Is there a way of getting around this, or is this behaviour hardcoded in the
C++ module?

I think so. External module written in C/C++ is often statically linked.
 
J

jelle feringa

CGAL.Facet = OtherFacet
You're not creating Facet object here, not even Polyhedron.Facet.

Right, which is not the point; I'm trying to override the Facet, a topological
entity of which a Polyhedron is composed of .
( vertex -> halfedge -> facet -> polyhedron )
I think so. External module written in C/C++ is often statically linked.

I'm sorry, but I don't follow you; what has linking to do with changing
the behaviour of the compiled module?

Thanks,

-jelle
 
L

Lie Ryan

jelle said:
Right, which is not the point; I'm trying to override the Facet, a topological
entity of which a Polyhedron is composed of .
( vertex -> halfedge -> facet -> polyhedron )


I'm sorry, but I don't follow you; what has linking to do with changing
the behaviour of the compiled module?

Aaron explained it better than I did. Basically the C/C++ module uses
C/C++ version of Facet class directly without consulting python
interpreter, this is what I meant by static linking.

Since python is dynamic language, all reference is dynamic, but C/C++
are static language, extensions written in C/C++ have to make extra
effort to create dynamic reference.
 
J

jelle feringa

Aaron explained it better than I did. Basically the C/C++ module uses
C/C++ version of Facet class directly without consulting python
interpreter, this is what I meant by static linking.

Thanks so much for the explanation Lie, I understood it fully now.
Thanks again,

-jelle
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top