swig & Python question

I

It's me

I am playing around with SWING building a Python module using the no brainer
example in http://www.swig.org/tutorial.html. With that first example,


/* File : example.c */

#include <time.h>
double My_variable = 3.0;

int fact(int n) {
if (n <= 1) return 1;
else return n*fact(n-1);
}

int my_mod(int x, int y) {
return (x%y);
}

char *get_time()
{
time_t ltime;
time(&ltime);
return ctime(&ltime);
}
and using the swig file of:
/* example.i */
%module example
%{
/* Put header files here (optional) */
%}

extern double My_variable;
extern int fact(int n);
extern int my_mod(int x, int y);
extern char *get_time();
I was able to execute this Python script:

However, when I try to access the gloabl variable My_variable by doing:

print example.cvar

I get a blank (rather then a value of 3.0).

Why?
 
K

Keith Dart

It's me said:
Oops! Soapy fingers. "SWIG" - not "SWING".

I have used SWIG before, and it's not always such a "no-brainer". In
fact, it rarely is except for trivial examples. But it can work. I think
it is best suited for wrapping large libraries. For small stuff, it
would be better to just do it "manually" using the Python C API.


Good luck.

--
It's not me.




--
\/ \/
(O O)
-- --------------------oOOo~(_)~oOOo----------------------------------------
Keith Dart <[email protected]>
public key: ID: F3D288E4
============================================================================
 
I

It's me

Whether SWIG will work in a "no brainer" way or not depends on the original
code, I think. If the original code uses a very convoluted design, of
course things will get hairy. If the package uses a very clean structure,
I think you will find SWIG works out very nicely.

The intriguing things is, however, once you structure the package to a form
SWIG would work, it opens up the door to support multiple script languages
(and they have a long list of supported script languages).

If you hand crafted it to run the Python-C API, then you can only use Python
as script.

--
It's me


Keith Dart said:
I have used SWIG before, and it's not always such a "no-brainer". In
fact, it rarely is except for trivial examples. But it can work. I think
it is best suited for wrapping large libraries. For small stuff, it
would be better to just do it "manually" using the Python C API.


Good luck.

--
It's not me.




--
\/ \/
(O O)
-- --------------------oOOo~(_)~oOOo-------------------------------------- --
Keith Dart <[email protected]>
public key: ID: F3D288E4
============================================================================
 

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

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top