Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swig tool and C++. Being too clever

Tags:

python

swig

http://www.swig.org/papers/PyTutorial98/PyTutorial98.pdf It comes from above link:

enter image description here

I know that it is an old publication so it is possible that information is outdated.

I would like to ask:

"Seems to work fine with C++ if you aren't being too clever" What does it mean, to be too clever?

Is there known situation/case that I shuold be very careful where I am programming C++ modules and extending Python using swig tool?

like image 693
Gilgamesz Avatar asked Nov 08 '16 08:11

Gilgamesz


2 Answers

This PDF appears to be a copy of slides from a presentation given by David Beazley at the 7th International Python Conference. My guess is there was a joke or verbal explanation of what he meant by that phrase.

Seems to work fine with C++ if you aren't being too clever

Here is a link to his website if you want to get in touch with him and ask him directly. His twitter account is dabeaz, which may (or may not) be a better way of contacting him.

like image 157
eric.christensen Avatar answered Nov 05 '22 05:11

eric.christensen


The slide is strange and misleading. SWIG does not transform pass-by-value into pass-by-reference at all. Let me try to clarify by an example:

Let's say that as in the example you have the C++ function

double dot_product(Vector a, Vector b);

Now in plain C++ (no SWIG, no wrapping) you may use this function as in the following examples:

1.

Vector a = Vector(1,0);
Vector b = Vector(0,1);
double zero = dot_product(a, b);

2.

Vector *a = new Vector(1,0);
Vector *b = new Vector(0,1);
double zero = dot_product(*a, *b);

In both cases, the function is in fact called in exactly the same way using call-by-value.

SWIG wraps all objects into a structure that contains a pointer to the object, so under the hood SWIG passes pointers around for everything, and therefore uses a syntax as in the second example. But there is no conversion / transformation of call semantics going on whatsoever.

To answer your questions:

"Seems to work fine with C++ if you aren't being too clever" What does it mean, to be too clever?

I have no idea. As stated in another answer, likely a joke.

Is there known situation/case that I shuold be very careful where I am programming C++ modules and extending Python using swig tool?

This is a very broad question, and there certainly are pitfalls, especially related to memory management. However, this particular "transformation" is not an issue.

For reference, here is the relevant entry in the SWIG manual. Note that it is worded differently: The function is transformed to accept pointers. Nothing is said about "call semantics" (since this is a non-issue).

like image 21
m7thon Avatar answered Nov 05 '22 04:11

m7thon