[GNC-dev] Python: About Wrapping SWIG Objects (GncNumeric)

c.holtermann at gmx.de c.holtermann at gmx.de
Sun Aug 5 20:17:18 EDT 2018


after some time I get back to the gnucash python bindings.

I worked on a str method for GncNumeric. It's in the example_script
I changed it to python3. Then I began to wonder about the relationshipp 
the SwigObject and the wrapping object.

In this case it's something like
<gnucash.gnucash_core_c._gnc_numeric; proxy of <Swig Object of type 
'_gnc_numeric *' at 0x7f11908da840> >
<gnucash.gnucash_core.GncNumeric object at 0x7f118f76d6d8>

When I have the GncNumeric object I can access the SwigObject through 
its instance property.
The other way round seems not as simple.

My method __gncnumeric__str__ overwrites the __str__ method using 

In this method self points to the swig object. I cannot use the methods 
of GncNumeric as
it is the layer of the wrapping object.

To access these methods I used to instantiate a 
GncNumeric(instance=self) as a temporary self.
But that seems not right as this probably already exists.

I wondered if I could know about the wrapping object when I had the 
instance only. I did not
find a way. I stumbled over an interesting comment in

# why reimpliment __new__? Because later on we're going to
# use new to avoid creating new instances when existing instances
# already exist with the same __instance value, or equivalent __instance
# values, where this is desirable...

I did not find "later on". But if that works I can safely do 
GncNumeric(instance=self) as it
would reuse the existing GncNumeric object.

But nevertheless I wondered if we could put a link to the GncNumeric in 
the Swig Level. I tried
(link to my fork)

Interstingly some Swig objects can be added to and some others not. 
GncNumeric works while
QofSession doesn't. So I made it a try block for now.

Having done that I can get to the GncNumeric (sort of higher self) from 
the Swig object:

Well I found it interesting to think these things through. Maybe someone 
can tell me about
the mysterious "later on" in the cited comment. At this point I find it 
useful to have a
way from the instance to the wrapping object through some kind of link 
as I suggested in
the patch to function_class.


Christoph Holtermann

More information about the gnucash-devel mailing list