The great merge

git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@4141 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
Dave Beazley 2002-11-30 22:01:28 +00:00
commit 516036631c
1508 changed files with 125983 additions and 44037 deletions

View file

@ -35,7 +35,7 @@ CC = @CC@
CXX = @CXX@
OBJC = @CC@ -Wno-import # -Wno-import needed for gcc
CFLAGS =
INCLUDE =
INCLUDES =
LIBS =
# SWIG Options
@ -102,13 +102,13 @@ BUILD_LIBS = $(LIBS) # Dynamic loading
.SUFFIXES: .c .cxx .m
.c.o:
$(CC) $(CCSHARED) $(CFLAGS) $(INCLUDE) -c $<
$(CC) $(CCSHARED) $(CFLAGS) $(INCLUDES) -c $<
.cxx.o:
$(CXX) $(CCSHARED) $(CXXFLAGS) $(INCLUDE) -c $<
$(CXX) $(CCSHARED) $(CXXFLAGS) $(INCLUDES) -c $<
.m.o:
$(OBJC) $(CCSHARED) $(CFLAGS) $(INCLUDE) -c $<
$(OBJC) $(CCSHARED) $(CFLAGS) $(INCLUDES) -c $<
# ----------------------------------------------------------------------
@ -120,7 +120,7 @@ all: $(TARGET)
# Convert the wrapper file into an object file
$(WRAPOBJ) : $(WRAPFILE)
$(SWIGCC) -c $(CCSHARED) $(CFLAGS) $(WRAPFILE) $(INCLUDE) $(PY_INCLUDE)
$(SWIGCC) -c $(CCSHARED) $(CFLAGS) $(WRAPFILE) $(INCLUDES) $(PY_INCLUDE)
$(WRAPFILE) : $(INTERFACE)
$(SWIG) $(SWIGOPT) -o $(WRAPFILE) $(SWIGLIB) $(INTERFACE)

288
SWIG/Lib/python/cstring.i Normal file
View file

@ -0,0 +1,288 @@
/*
* cstring.i
* $Header$
*
* Author(s): David Beazley (beazley@cs.uchicago.edu)
*
* This file provides typemaps and macros for dealing with various forms
* of C character string handling. The primary use of this module
* is in returning character data that has been allocated or changed in
* some way.
*/
%include "fragments.i"
/* %cstring_input_binary(TYPEMAP, SIZE)
*
* Macro makes a function accept binary string data along with
* a size.
*/
%define %cstring_input_binary(TYPEMAP, SIZE)
%apply (char *STRING, int LENGTH) { (TYPEMAP, SIZE) };
%enddef
/*
* %cstring_bounded_output(TYPEMAP, MAX)
*
* This macro is used to return a NULL-terminated output string of
* some maximum length. For example:
*
* %cstring_bounded_output(char *outx, 512);
* void foo(char *outx) {
* sprintf(outx,"blah blah\n");
* }
*
*/
%define %cstring_bounded_output(TYPEMAP,MAX)
%typemap(ignore) TYPEMAP(char temp[MAX+1]) {
$1 = ($1_ltype) temp;
}
%typemap(argout,fragment="t_output_helper") TYPEMAP {
PyObject *o;
$1[MAX] = 0;
o = PyString_FromString($1);
$result = t_output_helper($result,o);
}
%enddef
/*
* %cstring_chunk_output(TYPEMAP, SIZE)
*
* This macro is used to return a chunk of binary string data.
* Embedded NULLs are okay. For example:
*
* %cstring_chunk_output(char *outx, 512);
* void foo(char *outx) {
* memmove(outx, somedata, 512);
* }
*
*/
%define %cstring_chunk_output(TYPEMAP,SIZE)
%typemap(ignore) TYPEMAP(char temp[SIZE]) {
$1 = ($1_ltype) temp;
}
%typemap(argout,fragment="t_output_helper") TYPEMAP {
PyObject *o = PyString_FromStringAndSize($1,SIZE);
$result = t_output_helper($result,o);
}
%enddef
/*
* %cstring_bounded_mutable(TYPEMAP, SIZE)
*
* This macro is used to wrap a string that's going to mutate.
*
* %cstring_bounded_mutable(char *in, 512);
* void foo(in *x) {
* while (*x) {
* *x = toupper(*x);
* x++;
* }
* }
*
*/
%define %cstring_bounded_mutable(TYPEMAP,MAX)
%typemap(in) TYPEMAP(char temp[MAX+1]) {
char *t = PyString_AsString($input);
if (PyErr_Occurred()) SWIG_fail;
strncpy(temp,t,MAX);
$1 = ($1_ltype) temp;
}
%typemap(argout,fragment="t_output_helper") TYPEMAP {
PyObject *o;
$1[MAX] = 0;
o = PyString_FromString($1);
$result = t_output_helper($result,o);
}
%enddef
/*
* %cstring_mutable(TYPEMAP [, expansion])
*
* This macro is used to wrap a string that will mutate in place.
* It may change size up to a user-defined expansion.
*
* %cstring_mutable(char *in);
* void foo(in *x) {
* while (*x) {
* *x = toupper(*x);
* x++;
* }
* }
*
*/
%define %cstring_mutable(TYPEMAP,...)
%typemap(in) TYPEMAP {
char *t = PyString_AsString($input);
int n = PyString_Size($input);
if (PyErr_Occurred()) return SWIG_fail;
$1 = ($1_ltype) t;
#if #__VA_ARGS__ == ""
#if __cplusplus
$1 = ($1_ltype) new char[n+1];
#else
$1 = ($1_ltype) malloc(n+1);
#endif
#else
#if __cplusplus
$1 = ($1_ltype) new char[n+1+__VA_ARGS__];
#else
$1 = ($1_ltype) malloc(n+1+__VA_ARGS__);
#endif
#endif
memmove($1,t,n);
$1[n] = 0;
}
%typemap(argout,fragment="t_output_helper") TYPEMAP {
PyObject *o;
o = PyString_FromString($1);
$result = t_output_helper($result,o);
#if __cplusplus
delete[] $1;
#else
free($1);
#endif
}
%enddef
/*
* %cstring_output_maxsize(TYPEMAP, SIZE)
*
* This macro returns data in a string of some user-defined size.
*
* %cstring_output_maxsize(char *outx, int max) {
* void foo(char *outx, int max) {
* sprintf(outx,"blah blah\n");
* }
*/
%define %cstring_output_maxsize(TYPEMAP, SIZE)
%typemap(in) (TYPEMAP, SIZE) {
$2 = PyInt_AsLong($input);
if (PyErr_Occurred()) return SWIG_fail;
#ifdef __cplusplus
$1 = ($1_ltype) new char[$2+1];
#else
$1 = ($1_ltype) malloc($2+1);
#endif
}
%typemap(argout,fragment="t_output_helper") (TYPEMAP,SIZE) {
PyObject *o;
o = PyString_FromString($1);
$result = t_output_helper($result,o);
#ifdef __cplusplus
delete [] $1;
#else
free($1);
#endif
}
%enddef
/*
* %cstring_output_withsize(TYPEMAP, SIZE)
*
* This macro is used to return character data along with a size
* parameter.
*
* %cstring_output_maxsize(char *outx, int *max) {
* void foo(char *outx, int *max) {
* sprintf(outx,"blah blah\n");
* *max = strlen(outx);
* }
*/
%define %cstring_output_withsize(TYPEMAP, SIZE)
%typemap(in) (TYPEMAP, SIZE) {
int n = PyInt_AsLong($input);
if (PyErr_Occurred()) SWIG_fail;
#ifdef __cplusplus
$1 = ($1_ltype) new char[n+1];
$2 = ($2_ltype) new $*2_ltype;
#else
$1 = ($1_ltype) malloc(n+1);
$2 = ($2_ltype) malloc(sizeof($*2_ltype));
#endif
*$2 = n;
}
%typemap(argout,fragment="t_output_helper") (TYPEMAP,SIZE) {
PyObject *o;
o = PyString_FromStringAndSize($1,*$2);
$result = t_output_helper($result,o);
#ifdef __cplusplus
delete [] $1;
delete $2;
#else
free($1);
free($2);
#endif
}
%enddef
/*
* %cstring_output_allocate(TYPEMAP, RELEASE)
*
* This macro is used to return character data that was
* allocated with new or malloc.
*
* %cstring_output_allocated(char **outx, free($1));
* void foo(char **outx) {
* *outx = (char *) malloc(512);
* sprintf(outx,"blah blah\n");
* }
*/
%define %cstring_output_allocate(TYPEMAP, RELEASE)
%typemap(ignore) TYPEMAP($*1_ltype temp = 0) {
$1 = &temp;
}
%typemap(argout,fragment="t_output_helper") TYPEMAP {
if (*$1) {
PyObject *o = PyString_FromString(*$1);
RELEASE;
$result = t_output_helper($result,o);
}
}
%enddef
/*
* %cstring_output_allocate_size(TYPEMAP, SIZE, RELEASE)
*
* This macro is used to return character data that was
* allocated with new or malloc.
*
* %cstring_output_allocated(char **outx, int *sz, free($1));
* void foo(char **outx, int *sz) {
* *outx = (char *) malloc(512);
* sprintf(outx,"blah blah\n");
* *sz = strlen(outx);
* }
*/
%define %cstring_output_allocate_size(TYPEMAP, SIZE, RELEASE)
%typemap(ignore) (TYPEMAP, SIZE) ($*1_ltype temp = 0, $*2_ltype tempn) {
$1 = &temp;
$2 = &tempn;
}
%typemap(argout,fragment="t_output_helper")(TYPEMAP,SIZE) {
if (*$1) {
PyObject *o = PyString_FromStringAndSize(*$1,*$2);
RELEASE;
$result = t_output_helper($result,o);
}
}
%enddef

View file

@ -1,342 +0,0 @@
//
// embed.i
// SWIG file embedding the Python interpreter in something else.
// This file is based on Python-1.3, but it might work with
// later versions.
//
// This file makes it possible to extend Python and all of its
// built-in functions without having to hack it's setup script.
//
#ifdef AUTODOC
%subsection "embed13.i"
%text %{
This module provides support for building a new version of the
Python 1.3 executable. This will be necessary on systems that do
not support shared libraries and may be necessary with C++
extensions. This file contains everything you need to build
a new version of Python from include files and libraries normally
installed with the Python language.
This module is functionally equivalent to the embed.i library,
but has a number of changes needed to work with older versions
of Python.
%}
#else
%echo "embed.i : Using Python 1.3"
#endif
%wrapper %{
#ifndef NEED_GETOPT
#include <unistd.h>
#endif
#include <pythonrun.h>
typedef struct SWIGPyTab {
char *name;
void (*initfunc)();
} SWIGPyTab;
#ifdef __cplusplus
extern "C"
#endif
void SWIG_init(void); /* Forward reference */
#define inittab python_inittab
/* Grab Python's inittab[] structure */
#ifdef __cplusplus
extern "C" {
#endif
#include <config.c>
#undef inittab
/* Now define our own version of it.
God forbid someone have more than 1000 built-in modules! */
SWIGPyTab inittab[1000];
static int swig_num_modules = 0;
/* Function for adding modules to Python */
static void swig_add_module(char *name, void (*initfunc)()) {
inittab[swig_num_modules].name = name;
inittab[swig_num_modules].initfunc = initfunc;
swig_num_modules++;
inittab[swig_num_modules].name = (char *) 0;
inittab[swig_num_modules].initfunc = (void (*)()) 0;
}
/* Function to add all of Python's build in modules to our interpreter */
static void swig_add_builtin() {
int i = 0;
while (python_inittab[i].name) {
swig_add_module(python_inittab[i].name, python_inittab[i].initfunc);
i++;
}
/* Add SWIG builtin function */
swig_add_module(SWIG_name, SWIG_init);
#ifdef SWIGMODINIT
SWIGMODINIT
#endif
}
#ifdef __cplusplus
}
#endif
/* Interface to getopt(): */
extern int optind;
extern char *optarg;
#ifdef NEED_GETOPT
#ifdef __cplusplus
extern "C" int getopt(int, char **, char *);
#else
extern int getopt(); /* PROTO((int, char **, char *)); -- not standardized */
#endif
#endif
extern int Py_DebugFlag; /* For parser.c, declared in pythonrun.c */
extern int Py_VerboseFlag; /* For import.c, declared in pythonrun.c */
extern int Py_SuppressPrintingFlag; /* For ceval.c, declared in pythonrun.c */
/* Subroutines that live in their own file */
#ifdef __cplusplus
extern "C" {
extern int isatty(int fd);
extern int PySys_SetArgv(int, char **);
#endif
extern char *getversion();
extern char *getcopyright();
#ifdef __cplusplus
}
#endif
/* For getprogramname(); set by main() */
static char *argv0;
/* For getargcargv(); set by main() */
static char **orig_argv;
static int orig_argc;
/* Short usage message (with %s for argv0) */
static char *usage_line =
"usage: %s [-d] [-i] [-s] [-u ] [-v] [-c cmd | file | -] [arg] ...\n";
/* Long usage message, split into parts < 512 bytes */
static char *usage_top = "\n\
Options and arguments (and corresponding environment variables):\n\
-d : debug output from parser (also PYTHONDEBUG=x)\n\
-i : inspect interactively after running script (also PYTHONINSPECT=x)\n\
-s : suppress printing of top level expressions (also PYTHONSUPPRESS=x)\n\
-u : unbuffered stdout and stderr (also PYTHONUNBUFFERED=x)\n\
-v : verbose (trace import statements) (also PYTHONVERBOSE=x)\n\
-c cmd : program passed in as string (terminates option list)\n\
";
static char *usage_bot = "\
file : program read from script file\n\
- : program read from stdin (default; interactive mode if a tty)\n\
arg ...: arguments passed to program in sys.argv[1:]\n\
\n\
Other environment variables:\n\
PYTHONSTARTUP: file executed on interactive startup (no default)\n\
PYTHONPATH : colon-separated list of directories prefixed to the\n\
default module search path. The result is sys.path.\n\
";
/* Main program */
int
main(int argc, char **argv) {
int c;
int sts;
char *command = NULL;
char *filename = NULL;
FILE *fp = stdin;
char *p;
int inspect = 0;
int unbuffered = 0;
swig_add_builtin(); /* Add SWIG built-in modules */
orig_argc = argc; /* For getargcargv() */
orig_argv = argv;
argv0 = argv[0]; /* For getprogramname() */
if ((p = getenv("PYTHONDEBUG")) && *p != '\0')
Py_DebugFlag = 1;
if ((p = getenv("PYTHONSUPPRESS")) && *p != '\0')
Py_SuppressPrintingFlag = 1;
if ((p = getenv("PYTHONVERBOSE")) && *p != '\0')
Py_VerboseFlag = 1;
if ((p = getenv("PYTHONINSPECT")) && *p != '\0')
inspect = 1;
if ((p = getenv("PYTHONUNBUFFERED")) && *p != '\0')
unbuffered = 1;
while ((c = getopt(argc, argv, "c:disuv")) != EOF) {
if (c == 'c') {
/* -c is the last option; following arguments
that look like options are left for the
the command to interpret. */
command = (char *) malloc(strlen(optarg) + 2);
if (command == NULL)
Py_FatalError(
"not enough memory to copy -c argument");
strcpy(command, optarg);
strcat(command, "\n");
break;
}
switch (c) {
case 'd':
Py_DebugFlag++;
break;
case 'i':
inspect++;
break;
case 's':
Py_SuppressPrintingFlag++;
break;
case 'u':
unbuffered++;
break;
case 'v':
Py_VerboseFlag++;
break;
/* This space reserved for other options */
default:
fprintf(stderr, usage_line, argv[0]);
fprintf(stderr, usage_top);
fprintf(stderr, usage_bot);
exit(2);
/*NOTREACHED*/
}
}
if (unbuffered) {
#ifndef MPW
setbuf(stdout, (char *)NULL);
setbuf(stderr, (char *)NULL);
#else
/* On MPW (3.2) unbuffered seems to hang */
setvbuf(stdout, (char *)NULL, _IOLBF, BUFSIZ);
setvbuf(stderr, (char *)NULL, _IOLBF, BUFSIZ);
#endif
}
if (command == NULL && optind < argc &&
strcmp(argv[optind], "-") != 0)
filename = argv[optind];
if (Py_VerboseFlag ||
command == NULL && filename == NULL && isatty((int)fileno(fp)))
fprintf(stderr, "Python %s\n%s\n",
getversion(), getcopyright());
if (filename != NULL) {
if ((fp = fopen(filename, "r")) == NULL) {
fprintf(stderr, "%s: can't open file '%s'\n",
argv[0], filename);
exit(2);
}
}
Py_Initialize();
if (command != NULL) {
/* Backup optind and force sys.argv[0] = '-c' */
optind--;
argv[optind] = "-c";
}
PySys_SetArgv(argc-optind, argv+optind);
if (command) {
sts = PyRun_SimpleString(command) != 0;
}
else {
if (filename == NULL && isatty((int)fileno(fp))) {
char *startup = getenv("PYTHONSTARTUP");
if (startup != NULL && startup[0] != '\0') {
FILE *fp = fopen(startup, "r");
if (fp != NULL) {
(void) PyRun_SimpleFile(fp, startup);
PyErr_Clear();
fclose(fp);
}
}
}
sts = PyRun_AnyFile(
fp, filename == NULL ? "<stdin>" : filename) != 0;
if (filename != NULL)
fclose(fp);
}
if (inspect && isatty((int)fileno(stdin)) &&
(filename != NULL || command != NULL))
sts = PyRun_AnyFile(stdin, "<stdin>") != 0;
Py_Exit(sts);
/*NOTREACHED*/
}
/* Return the program name -- some code out there needs this. */
#ifdef __cplusplus
extern "C"
#endif
char *
getprogramname()
{
return argv0;
}
/* Make the *original* argc/argv available to other modules.
This is rare, but it is needed by the secureware extension. */
#ifdef __cplusplus
extern "C"
#endif
void
getargcargv(int *argc,char ***argv)
{
*argc = orig_argc;
*argv = orig_argv;
}
/* Total Hack to get getpath.c to compile under C++ */
#ifdef __cplusplus
#define malloc (char *) malloc
extern "C" {
#endif
#include <getpath.c>
#ifdef __cplusplus
}
#undef malloc
#endif
%}

View file

@ -1,340 +0,0 @@
//
// embed.i
// SWIG file embedding the Python interpreter in something else.
// This file is based on Python-1.4.
//
// This file makes it possible to extend Python and all of its
// built-in functions without having to hack it's setup script.
//
#ifdef AUTODOC
%subsection "embed.i"
%text %{
This module provides support for building a new version of the
Python executable. This will be necessary on systems that do
not support shared libraries and may be necessary with C++
extensions. This file contains everything you need to build
a new version of Python from include files and libraries normally
installed with the Python language.
This module will automatically grab all of the Python modules
present in your current Python executable (including any special
purpose modules you have enabled such as tkinter). Thus, you
may need to provide additional link libraries when compiling.
This library file only works with Python 1.4. A version compatible
with Python 1.3 is available as embed13.i. A Python 1.5 version is
available as embed15.i As far as I know, this module is C++ safe
(well, it works for me).
%}
#else
%echo "embed.i : Using Python 1.4"
#endif
%wrapper %{
#ifndef NEED_GETOPT
#include <unistd.h>
#endif
#include <pythonrun.h>
#ifdef __cplusplus
extern "C"
#endif
void SWIG_init(); /* Forward reference */
#define inittab python_inittab
/* Grab Python's inittab[] structure */
#ifdef __cplusplus
extern "C" {
#endif
#include <config.c>
#undef inittab
/* Now define our own version of it.
Hopefully someone does not have more than 1000 built-in modules */
struct _inittab inittab[1000];
static int swig_num_modules = 0;
/* Function for adding modules to Python */
static void swig_add_module(char *name, void (*initfunc)()) {
inittab[swig_num_modules].name = name;
inittab[swig_num_modules].initfunc = initfunc;
swig_num_modules++;
inittab[swig_num_modules].name = (char *) 0;
inittab[swig_num_modules].initfunc = 0;
}
/* Function to add all of Python's build in modules to our interpreter */
static void swig_add_builtin() {
int i = 0;
while (python_inittab[i].name) {
swig_add_module(python_inittab[i].name, python_inittab[i].initfunc);
i++;
}
#ifdef SWIGMODINIT
SWIGMODINIT
#endif
/* Add SWIG builtin function */
swig_add_module(SWIG_name, SWIG_init);
}
#ifdef __cplusplus
}
#endif
/* Interface to getopt(): */
extern int optind;
extern char *optarg;
#ifdef NEED_GETOPT
#ifdef __cplusplus
extern "C" int getopt(int, char **, char *);
#else
extern int getopt(); /* PROTO((int, char **, char *)); -- not standardized */
#endif
#endif
extern int Py_DebugFlag; /* For parser.c, declared in pythonrun.c */
extern int Py_VerboseFlag; /* For import.c, declared in pythonrun.c */
extern int Py_SuppressPrintingFlag; /* For ceval.c, declared in pythonrun.c */
/* Subroutines that live in their own file */
#ifdef __cplusplus
extern "C" {
extern int isatty(int fd);
extern void PySys_SetArgv(int, char **);
#endif
extern char *Py_GetVersion();
extern char *Py_GetCopyright();
#ifdef __cplusplus
}
#endif
/* For getprogramname(); set by main() */
static char *argv0;
/* For getargcargv(); set by main() */
static char **orig_argv;
static int orig_argc;
/* Short usage message (with %s for argv0) */
static char *usage_line =
"usage: %s [-d] [-i] [-s] [-u ] [-v] [-c cmd | file | -] [arg] ...\n";
/* Long usage message, split into parts < 512 bytes */
static char *usage_top = "\n\
Options and arguments (and corresponding environment variables):\n\
-d : debug output from parser (also PYTHONDEBUG=x)\n\
-i : inspect interactively after running script (also PYTHONINSPECT=x)\n\
-s : suppress printing of top level expressions (also PYTHONSUPPRESS=x)\n\
-u : unbuffered stdout and stderr (also PYTHONUNBUFFERED=x)\n\
-v : verbose (trace import statements) (also PYTHONVERBOSE=x)\n\
-c cmd : program passed in as string (terminates option list)\n\
";
static char *usage_bot = "\
file : program read from script file\n\
- : program read from stdin (default; interactive mode if a tty)\n\
arg ...: arguments passed to program in sys.argv[1:]\n\
\n\
Other environment variables:\n\
PYTHONSTARTUP: file executed on interactive startup (no default)\n\
PYTHONPATH : colon-separated list of directories prefixed to the\n\
default module search path. The result is sys.path.\n\
";
/* Main program */
int
main(int argc, char **argv) {
int c;
int sts;
char *command = NULL;
char *filename = NULL;
FILE *fp = stdin;
char *p;
int inspect = 0;
int unbuffered = 0;
swig_add_builtin(); /* Add SWIG built-in modules */
orig_argc = argc; /* For getargcargv() */
orig_argv = argv;
argv0 = argv[0]; /* For getprogramname() */
if ((p = getenv("PYTHONDEBUG")) && *p != '\0')
Py_DebugFlag = 1;
if ((p = getenv("PYTHONSUPPRESS")) && *p != '\0')
Py_SuppressPrintingFlag = 1;
if ((p = getenv("PYTHONVERBOSE")) && *p != '\0')
Py_VerboseFlag = 1;
if ((p = getenv("PYTHONINSPECT")) && *p != '\0')
inspect = 1;
if ((p = getenv("PYTHONUNBUFFERED")) && *p != '\0')
unbuffered = 1;
while ((c = getopt(argc, argv, "c:disuv")) != EOF) {
if (c == 'c') {
/* -c is the last option; following arguments
that look like options are left for the
the command to interpret. */
command = (char *) malloc(strlen(optarg) + 2);
if (command == NULL)
Py_FatalError(
"not enough memory to copy -c argument");
strcpy(command, optarg);
strcat(command, "\n");
break;
}
switch (c) {
case 'd':
Py_DebugFlag++;
break;
case 'i':
inspect++;
break;
case 's':
Py_SuppressPrintingFlag++;
break;
case 'u':
unbuffered++;
break;
case 'v':
Py_VerboseFlag++;
break;
/* This space reserved for other options */
default:
fprintf(stderr, usage_line, argv[0]);
fprintf(stderr, usage_top);
fprintf(stderr, usage_bot);
exit(2);
/*NOTREACHED*/
}
}
if (unbuffered) {
#ifndef MPW
setbuf(stdout, (char *)NULL);
setbuf(stderr, (char *)NULL);
#else
/* On MPW (3.2) unbuffered seems to hang */
setvbuf(stdout, (char *)NULL, _IOLBF, BUFSIZ);
setvbuf(stderr, (char *)NULL, _IOLBF, BUFSIZ);
#endif
}
if (command == NULL && optind < argc &&
strcmp(argv[optind], "-") != 0)
filename = argv[optind];
if (Py_VerboseFlag ||
command == NULL && filename == NULL && isatty((int)fileno(fp)))
fprintf(stderr, "Python %s\n%s\n",
Py_GetVersion(), Py_GetCopyright());
if (filename != NULL) {
if ((fp = fopen(filename, "r")) == NULL) {
fprintf(stderr, "%s: can't open file '%s'\n",
argv[0], filename);
exit(2);
}
}
Py_Initialize();
if (command != NULL) {
/* Backup optind and force sys.argv[0] = '-c' */
optind--;
argv[optind] = "-c";
}
PySys_SetArgv(argc-optind, argv+optind);
if (command) {
sts = PyRun_SimpleString(command) != 0;
}
else {
if (filename == NULL && isatty((int)fileno(fp))) {
char *startup = getenv("PYTHONSTARTUP");
if (startup != NULL && startup[0] != '\0') {
FILE *fp = fopen(startup, "r");
if (fp != NULL) {
(void) PyRun_SimpleFile(fp, startup);
PyErr_Clear();
fclose(fp);
}
}
}
sts = PyRun_AnyFile(
fp, filename == NULL ? "<stdin>" : filename) != 0;
if (filename != NULL)
fclose(fp);
}
if (inspect && isatty((int)fileno(stdin)) &&
(filename != NULL || command != NULL))
sts = PyRun_AnyFile(stdin, "<stdin>") != 0;
Py_Exit(sts);
/*NOTREACHED*/
}
/* Return the program name -- some code out there needs this. */
#ifdef __cplusplus
extern "C"
#endif
char *
Py_GetProgramName()
{
return argv0;
}
/* Make the *original* argc/argv available to other modules.
This is rare, but it is needed by the secureware extension. */
#ifdef __cplusplus
extern "C"
#endif
void
getargcargv(int *argc,char ***argv)
{
*argc = orig_argc;
*argv = orig_argv;
}
/* Total Hack to get getpath.c to compile under C++ */
#ifdef __cplusplus
#define malloc (char *) malloc
extern "C" {
#endif
#include <getpath.c>
#ifdef __cplusplus
}
#undef malloc
#endif
%}

View file

@ -0,0 +1,29 @@
/* Helper function to return tuples */
%fragment("t_output_helper","header") %{
static PyObject* t_output_helper(PyObject* target, PyObject* o) {
PyObject* o2;
PyObject* o3;
if (!target) {
target = o;
} else if (target == Py_None) {
Py_DECREF(Py_None);
target = o;
} else {
if (!PyTuple_Check(target)) {
o2 = target;
target = PyTuple_New(1);
PyTuple_SetItem(target, 0, o2);
}
o3 = PyTuple_New(1);
PyTuple_SetItem(o3, 0, o);
o2 = target;
target = PySequence_Concat(o2, o3);
Py_DECREF(o2);
Py_DECREF(o3);
}
return target;
}
%}

View file

@ -1,464 +0,0 @@
//
// SWIG pointer conversion and utility library
//
// Dave Beazley
// April 19, 1997
//
// Python specific implementation. This file is included
// by the file ../pointer.i
%{
#include <ctype.h>
/* Types used by the library */
static swig_type_info *SWIG_POINTER_int_p = 0;
static swig_type_info *SWIG_POINTER_short_p =0;
static swig_type_info *SWIG_POINTER_long_p = 0;
static swig_type_info *SWIG_POINTER_float_p = 0;
static swig_type_info *SWIG_POINTER_double_p = 0;
static swig_type_info *SWIG_POINTER_char_p = 0;
static swig_type_info *SWIG_POINTER_char_pp = 0;
%}
%init %{
SWIG_POINTER_int_p = SWIG_TypeQuery("int *");
SWIG_POINTER_short_p = SWIG_TypeQuery("short *");
SWIG_POINTER_long_p = SWIG_TypeQuery("long *");
SWIG_POINTER_float_p = SWIG_TypeQuery("float *");
SWIG_POINTER_double_p = SWIG_TypeQuery("double *");
SWIG_POINTER_char_p = SWIG_TypeQuery("char *");
SWIG_POINTER_char_pp = SWIG_TypeQuery("char **");
%}
%{
/*------------------------------------------------------------------
ptrvalue(ptr,type = 0)
Attempts to dereference a pointer value. If type is given, it
will try to use that type. Otherwise, this function will attempt
to "guess" the proper datatype by checking against all of the
builtin C datatypes.
------------------------------------------------------------------ */
static PyObject *ptrvalue(PyObject *_PTRVALUE, int index, char *type) {
void *ptr;
char *s;
PyObject *obj;
if (SWIG_ConvertPtr(_PTRVALUE,&ptr,0,0)) {
PyErr_SetString(PyExc_TypeError,"Type error in ptrvalue. Argument is not a valid pointer value.");
return NULL;
}
/* If no datatype was passed, try a few common datatypes first */
if (!type) {
/* No datatype was passed. Type to figure out if it's a common one */
if (!SWIG_ConvertPtr(_PTRVALUE,&ptr,SWIG_POINTER_int_p,0)) {
type = "int";
} else if (!SWIG_ConvertPtr(_PTRVALUE,&ptr,SWIG_POINTER_double_p,0)) {
type = "double";
} else if (!SWIG_ConvertPtr(_PTRVALUE,&ptr,SWIG_POINTER_short_p,0)) {
type = "short";
} else if (!SWIG_ConvertPtr(_PTRVALUE,&ptr,SWIG_POINTER_long_p,0)) {
type = "long";
} else if (!SWIG_ConvertPtr(_PTRVALUE,&ptr,SWIG_POINTER_float_p,0)) {
type = "float";
} else if (!SWIG_ConvertPtr(_PTRVALUE,&ptr,SWIG_POINTER_char_p,0)) {
type = "char";
} else if (!SWIG_ConvertPtr(_PTRVALUE,&ptr,SWIG_POINTER_char_pp,0)) {
type = "char *";
} else {
type = "unknown";
}
}
if (!ptr) {
PyErr_SetString(PyExc_TypeError,"Unable to dereference NULL pointer.");
return NULL;
}
/* Now we have a datatype. Try to figure out what to do about it */
if (strcmp(type,"int") == 0) {
obj = PyInt_FromLong((long) *(((int *) ptr) + index));
} else if (strcmp(type,"double") == 0) {
obj = PyFloat_FromDouble((double) *(((double *) ptr)+index));
} else if (strcmp(type,"short") == 0) {
obj = PyInt_FromLong((long) *(((short *) ptr)+index));
} else if (strcmp(type,"long") == 0) {
obj = PyInt_FromLong((long) *(((long *) ptr)+index));
} else if (strcmp(type,"float") == 0) {
obj = PyFloat_FromDouble((double) *(((float *) ptr)+index));
} else if (strcmp(type,"char") == 0) {
obj = PyString_FromString(((char *) ptr)+index);
} else if (strcmp(type,"char *") == 0) {
char *c = *(((char **) ptr)+index);
if (c) obj = PyString_FromString(c);
else obj = PyString_FromString("NULL");
} else {
PyErr_SetString(PyExc_TypeError,"Unable to dereference unsupported datatype.");
return NULL;
}
return obj;
}
/*------------------------------------------------------------------
ptrcreate(type,value = 0,numelements = 1)
Attempts to create a new object of given type. Type must be
a basic C datatype. Will not create complex objects.
------------------------------------------------------------------ */
static PyObject *ptrcreate(char *type, PyObject *_PYVALUE, int numelements) {
void *ptr;
PyObject *obj;
int sz;
swig_type_info *cast;
char temp[40];
/* Check the type string against a variety of possibilities */
if (strcmp(type,"int") == 0) {
sz = sizeof(int)*numelements;
cast = SWIG_POINTER_int_p;
} else if (strcmp(type,"short") == 0) {
sz = sizeof(short)*numelements;
cast = SWIG_POINTER_short_p;
} else if (strcmp(type,"long") == 0) {
sz = sizeof(long)*numelements;
cast = SWIG_POINTER_long_p;
} else if (strcmp(type,"double") == 0) {
sz = sizeof(double)*numelements;
cast = SWIG_POINTER_double_p;
} else if (strcmp(type,"float") == 0) {
sz = sizeof(float)*numelements;
cast = SWIG_POINTER_float_p;
} else if (strcmp(type,"char") == 0) {
sz = sizeof(char)*numelements;
cast = SWIG_POINTER_char_p;
} else if (strcmp(type,"char *") == 0) {
sz = sizeof(char *)*(numelements+1);
cast = SWIG_POINTER_char_pp;
} else {
PyErr_SetString(PyExc_TypeError,"Unable to create unknown datatype.");
return NULL;
}
/* Create the new object */
ptr = (void *) malloc(sz);
if (!ptr) {
PyErr_SetString(PyExc_MemoryError,"Out of memory in swig_create.");
return NULL;
}
/* Now try to set its default value */
if (_PYVALUE) {
if (strcmp(type,"int") == 0) {
int *ip,i,ivalue;
ivalue = (int) PyInt_AsLong(_PYVALUE);
ip = (int *) ptr;
for (i = 0; i < numelements; i++)
ip[i] = ivalue;
} else if (strcmp(type,"short") == 0) {
short *ip,ivalue;
int i;
ivalue = (short) PyInt_AsLong(_PYVALUE);
ip = (short *) ptr;
for (i = 0; i < numelements; i++)
ip[i] = ivalue;
} else if (strcmp(type,"long") == 0) {
long *ip,ivalue;
int i;
ivalue = (long) PyInt_AsLong(_PYVALUE);
ip = (long *) ptr;
for (i = 0; i < numelements; i++)
ip[i] = ivalue;
} else if (strcmp(type,"double") == 0) {
double *ip,ivalue;
int i;
ivalue = (double) PyFloat_AsDouble(_PYVALUE);
ip = (double *) ptr;
for (i = 0; i < numelements; i++)
ip[i] = ivalue;
} else if (strcmp(type,"float") == 0) {
float *ip,ivalue;
int i;
ivalue = (float) PyFloat_AsDouble(_PYVALUE);
ip = (float *) ptr;
for (i = 0; i < numelements; i++)
ip[i] = ivalue;
} else if (strcmp(type,"char") == 0) {
char *ip,*ivalue;
ivalue = (char *) PyString_AsString(_PYVALUE);
ip = (char *) ptr;
strncpy(ip,ivalue,numelements-1);
} else if (strcmp(type,"char *") == 0) {
char **ip, *ivalue;
int i;
ivalue = (char *) PyString_AsString(_PYVALUE);
ip = (char **) ptr;
for (i = 0; i < numelements; i++) {
if (ivalue) {
ip[i] = (char *) malloc(strlen(ivalue)+1);
strcpy(ip[i],ivalue);
} else {
ip[i] = 0;
}
}
ip[numelements] = 0;
}
}
/* Create the pointer value */
obj = SWIG_NewPointerObj(ptr,cast);
return obj;
}
/*------------------------------------------------------------------
ptrset(ptr,value,index = 0,type = 0)
Attempts to set the value of a pointer variable. If type is
given, we will use that type. Otherwise, we'll guess the datatype.
------------------------------------------------------------------ */
static PyObject *ptrset(PyObject *_PTRVALUE, PyObject *_PYVALUE, int index, char *type) {
void *ptr;
PyObject *obj;
if (SWIG_ConvertPtr(_PTRVALUE,&ptr,0,0)) {
PyErr_SetString(PyExc_TypeError,"Type error in ptrset. Argument is not a valid pointer value.");
return NULL;
}
/* If no datatype was passed, try a few common datatypes first */
if (!type) {
/* No datatype was passed. Type to figure out if it's a common one */
if (!SWIG_ConvertPtr(_PTRVALUE,&ptr,SWIG_POINTER_int_p,0)) {
type = "int";
} else if (!SWIG_ConvertPtr(_PTRVALUE,&ptr,SWIG_POINTER_double_p,0)) {
type = "double";
} else if (!SWIG_ConvertPtr(_PTRVALUE,&ptr,SWIG_POINTER_short_p,0)) {
type = "short";
} else if (!SWIG_ConvertPtr(_PTRVALUE,&ptr,SWIG_POINTER_long_p,0)) {
type = "long";
} else if (!SWIG_ConvertPtr(_PTRVALUE,&ptr,SWIG_POINTER_float_p,0)) {
type = "float";
} else if (!SWIG_ConvertPtr(_PTRVALUE,&ptr,SWIG_POINTER_char_p,0)) {
type = "char";
} else if (!SWIG_ConvertPtr(_PTRVALUE,&ptr,SWIG_POINTER_char_pp,0)) {
type = "char *";
} else {
type = "unknown";
}
}
if (!ptr) {
PyErr_SetString(PyExc_TypeError,"Unable to set NULL pointer.");
return NULL;
}
/* Now we have a datatype. Try to figure out what to do about it */
if (strcmp(type,"int") == 0) {
*(((int *) ptr)+index) = (int) PyInt_AsLong(_PYVALUE);
} else if (strcmp(type,"double") == 0) {
*(((double *) ptr)+index) = (double) PyFloat_AsDouble(_PYVALUE);
} else if (strcmp(type,"short") == 0) {
*(((short *) ptr)+index) = (short) PyInt_AsLong(_PYVALUE);
} else if (strcmp(type,"long") == 0) {
*(((long *) ptr)+index) = (long) PyInt_AsLong(_PYVALUE);
} else if (strcmp(type,"float") == 0) {
*(((float *) ptr)+index) = (float) PyFloat_AsDouble(_PYVALUE);
} else if (strcmp(type,"char") == 0) {
char *c = PyString_AsString(_PYVALUE);
strcpy(((char *) ptr)+index, c);
} else if (strcmp(type,"char *") == 0) {
char *c = PyString_AsString(_PYVALUE);
char **ca = (char **) ptr;
if (ca[index]) free(ca[index]);
if (strcmp(c,"NULL") == 0) {
ca[index] = 0;
} else {
ca[index] = (char *) malloc(strlen(c)+1);
strcpy(ca[index],c);
}
} else {
PyErr_SetString(PyExc_TypeError,"Unable to set unsupported datatype.");
return NULL;
}
Py_INCREF(Py_None);
return Py_None;
}
/*------------------------------------------------------------------
ptradd(ptr,offset)
Adds a value to an existing pointer value. Will do a type-dependent
add for basic datatypes. For other datatypes, will do a byte-add.
------------------------------------------------------------------ */
static PyObject *ptradd(PyObject *_PTRVALUE, int offset) {
char *r;
void *ptr,*junk;
PyObject *obj;
swig_type_info *type;
/* Check to see what kind of object _PTRVALUE is */
/* Try to handle a few common datatypes first */
if (!SWIG_ConvertPtr(_PTRVALUE,&ptr,SWIG_POINTER_int_p,0)) {
ptr = (void *) (((int *) ptr) + offset);
type = SWIG_POINTER_int_p;
} else if (!SWIG_ConvertPtr(_PTRVALUE,&ptr,SWIG_POINTER_double_p,0)) {
ptr = (void *) (((double *) ptr) + offset);
type = SWIG_POINTER_double_p;
} else if (!SWIG_ConvertPtr(_PTRVALUE,&ptr,SWIG_POINTER_short_p,0)) {
ptr = (void *) (((short *) ptr) + offset);
type = SWIG_POINTER_short_p;
} else if (!SWIG_ConvertPtr(_PTRVALUE,&ptr,SWIG_POINTER_long_p,0)) {
ptr = (void *) (((long *) ptr) + offset);
type = SWIG_POINTER_long_p;
} else if (!SWIG_ConvertPtr(_PTRVALUE,&ptr,SWIG_POINTER_float_p,0)) {
ptr = (void *) (((float *) ptr) + offset);
type = SWIG_POINTER_float_p;
} else if (!SWIG_ConvertPtr(_PTRVALUE,&ptr,SWIG_POINTER_char_p,0)) {
ptr = (void *) (((char *) ptr) + offset);
type = SWIG_POINTER_char_p;
} else if (!SWIG_ConvertPtr(_PTRVALUE,&ptr,SWIG_POINTER_char_pp,0)) {
ptr = (void *) (((char *) ptr) + offset);
type = SWIG_POINTER_char_pp;
} else {
PyErr_SetString(PyExc_TypeError,"Type error in ptradd. Argument is not a valid pointer value.");
return NULL;
}
obj = SWIG_NewPointerObj(ptr, type);
return obj;
}
/*------------------------------------------------------------------
ptrfree(ptr)
Destroys a pointer value
------------------------------------------------------------------ */
PyObject *ptrfree(PyObject *_PTRVALUE) {
void *ptr, *junk;
if (SWIG_ConvertPtr(_PTRVALUE,&ptr,0,0)) {
PyErr_SetString(PyExc_TypeError,"Type error in ptrfree. Argument is not a valid pointer value.");
return NULL;
}
/* Check to see if this pointer is a char ** */
if (!SWIG_ConvertPtr(_PTRVALUE,&junk,SWIG_POINTER_char_pp,0)) {
char **c = (char **) ptr;
if (c) {
int i = 0;
while (c[i]) {
free(c[i]);
i++;
}
}
}
if (ptr)
free((char *) ptr);
Py_INCREF(Py_None);
return Py_None;
}
%}
%typemap(python,in) PyObject *ptr, PyObject *value {
$target = $source;
}
%typemap(python,out) PyObject *ptrcast,
PyObject *ptrvalue,
PyObject *ptrcreate,
PyObject *ptrset,
PyObject *ptradd,
PyObject *ptrfree
{
$target = $source;
}
%typemap(python,ret) int ptrset {
if ($source == -1) return NULL;
}
PyObject *ptrvalue(PyObject *ptr, int index = 0, char *type = 0);
// Returns the value that a pointer is pointing to (ie. dereferencing).
// The type is automatically inferred by the pointer type--thus, an
// integer pointer will return an integer, a double will return a double,
// and so on. The index and type fields are optional parameters. When
// an index is specified, this function returns the value of ptr[index].
// This allows array access. When a type is specified, it overrides
// the given pointer type. Examples :
//
// ptrvalue(a) # Returns the value *a
// ptrvalue(a,10) # Returns the value a[10]
// ptrvalue(a,10,"double") # Returns a[10] assuming a is a double *
PyObject *ptrset(PyObject *ptr, PyObject *value, int index = 0, char *type = 0);
// Sets the value pointed to by a pointer. The type is automatically
// inferred from the pointer type so this function will work for
// integers, floats, doubles, etc... The index and type fields are
// optional. When an index is given, it provides array access. When
// type is specified, it overrides the given pointer type. Examples :
//
// ptrset(a,3) # Sets the value *a = 3
// ptrset(a,3,10) # Sets a[10] = 3
// ptrset(a,3,10,"int") # Sets a[10] = 3 assuming a is a int *
PyObject *ptrcreate(char *type, PyObject *value = 0, int nitems = 1);
// Creates a new object and returns a pointer to it. This function
// can be used to create various kinds of objects for use in C functions.
// type specifies the basic C datatype to create and value is an
// optional parameter that can be used to set the initial value of the
// object. nitems is an optional parameter that can be used to create
// an array. This function results in a memory allocation using
// malloc(). Examples :
//
// a = ptrcreate("double") # Create a new double, return pointer
// a = ptrcreate("int",7) # Create an integer, set value to 7
// a = ptrcreate("int",0,1000) # Create an integer array with initial
// # values all set to zero
//
// This function only recognizes a few common C datatypes as listed below :
//
// int, short, long, float, double, char, char *, void
//
// All other datatypes will result in an error. However, other
// datatypes can be created by using the ptrcast function. For
// example:
//
// a = ptrcast(ptrcreate("int",0,100),"unsigned int *")
PyObject *ptrfree(PyObject *ptr);
// Destroys the memory pointed to by ptr. This function calls free()
// and should only be used with objects created by ptrcreate(). Since
// this function calls free, it may work with other objects, but this
// is generally discouraged unless you absolutely know what you're
// doing.
PyObject *ptradd(PyObject *ptr, int offset);
// Adds a value to the current pointer value. For the C datatypes of
// int, short, long, float, double, and char, the offset value is the
// number of objects and works in exactly the same manner as in C. For
// example, the following code steps through the elements of an array
//
// a = ptrcreate("double",0,100); # Create an array double a[100]
// b = a;
// for i in range(0,100):
// ptrset(b,0.0025*i); # set *b = 0.0025*i
// b = ptradd(b,1); # b++ (go to next double)
//
// In this case, adding one to b goes to the next double.
//
// For all other datatypes (including all complex datatypes), the
// offset corresponds to bytes. This function does not perform any
// bounds checking and negative offsets are perfectly legal.

416
SWIG/Lib/python/pyrun.swg Normal file
View file

@ -0,0 +1,416 @@
/***********************************************************************
* python.swg
*
* This file contains the runtime support for Python modules
* and includes code for managing global variables and pointer
* type checking.
*
* Author : David Beazley (beazley@cs.uchicago.edu)
************************************************************************/
#include "Python.h"
#ifdef __cplusplus
extern "C" {
#endif
#define SWIG_PY_INT 1
#define SWIG_PY_FLOAT 2
#define SWIG_PY_STRING 3
#define SWIG_PY_POINTER 4
#define SWIG_PY_BINARY 5
/* Flags for pointer conversion */
#define SWIG_POINTER_EXCEPTION 0x1
#define SWIG_POINTER_DISOWN 0x2
/* Exception handling in wrappers */
#define SWIG_fail goto fail
/* Constant information structure */
typedef struct swig_const_info {
int type;
char *name;
long lvalue;
double dvalue;
void *pvalue;
swig_type_info **ptype;
} swig_const_info;
#ifdef SWIG_NOINCLUDE
SWIGEXPORT(PyObject *) SWIG_newvarlink();
SWIGEXPORT(void) SWIG_addvarlink(PyObject *, char *, PyObject *(*)(void), int (*)(PyObject *));
SWIGEXPORT(int) SWIG_ConvertPtr(PyObject *, void **, swig_type_info *, int);
SWIGEXPORT(int) SWIG_ConvertPacked(PyObject *, void *, int sz, swig_type_info *, int);
SWIGEXPORT(char *) SWIG_PackData(char *c, void *, int);
SWIGEXPORT(char *) SWIG_UnpackData(char *c, void *, int);
SWIGEXPORT(PyObject *) SWIG_NewPointerObj(void *, swig_type_info *,int own);
SWIGEXPORT(PyObject *) SWIG_NewPackedObj(void *, int sz, swig_type_info *);
SWIGEXPORT(void) SWIG_InstallConstants(PyObject *d, swig_const_info constants[]);
#else
/* -----------------------------------------------------------------------------
* global variable support code.
* ----------------------------------------------------------------------------- */
typedef struct swig_globalvar {
char *name; /* Name of global variable */
PyObject *(*get_attr)(void); /* Return the current value */
int (*set_attr)(PyObject *); /* Set the value */
struct swig_globalvar *next;
} swig_globalvar;
typedef struct swig_varlinkobject {
PyObject_HEAD
swig_globalvar *vars;
} swig_varlinkobject;
static PyObject *
swig_varlink_repr(swig_varlinkobject *v) {
v = v;
return PyString_FromString("<Global variables>");
}
static int
swig_varlink_print(swig_varlinkobject *v, FILE *fp, int flags) {
swig_globalvar *var;
flags = flags;
fprintf(fp,"Global variables { ");
for (var = v->vars; var; var=var->next) {
fprintf(fp,"%s", var->name);
if (var->next) fprintf(fp,", ");
}
fprintf(fp," }\n");
return 0;
}
static PyObject *
swig_varlink_getattr(swig_varlinkobject *v, char *n) {
swig_globalvar *var = v->vars;
while (var) {
if (strcmp(var->name,n) == 0) {
return (*var->get_attr)();
}
var = var->next;
}
PyErr_SetString(PyExc_NameError,"Unknown C global variable");
return NULL;
}
static int
swig_varlink_setattr(swig_varlinkobject *v, char *n, PyObject *p) {
swig_globalvar *var = v->vars;
while (var) {
if (strcmp(var->name,n) == 0) {
return (*var->set_attr)(p);
}
var = var->next;
}
PyErr_SetString(PyExc_NameError,"Unknown C global variable");
return 1;
}
statichere PyTypeObject varlinktype = {
PyObject_HEAD_INIT(0)
0,
(char *)"swigvarlink", /* Type name */
sizeof(swig_varlinkobject), /* Basic size */
0, /* Itemsize */
0, /* Deallocator */
(printfunc) swig_varlink_print, /* Print */
(getattrfunc) swig_varlink_getattr, /* get attr */
(setattrfunc) swig_varlink_setattr, /* Set attr */
0, /* tp_compare */
(reprfunc) swig_varlink_repr, /* tp_repr */
0, /* tp_as_number */
0, /* tp_as_mapping*/
0, /* tp_hash */
};
/* Create a variable linking object for use later */
SWIGRUNTIME(PyObject *)
SWIG_newvarlink(void) {
swig_varlinkobject *result = 0;
result = PyMem_NEW(swig_varlinkobject,1);
varlinktype.ob_type = &PyType_Type; /* Patch varlinktype into a PyType */
result->ob_type = &varlinktype;
result->vars = 0;
result->ob_refcnt = 0;
Py_XINCREF((PyObject *) result);
return ((PyObject*) result);
}
SWIGRUNTIME(void)
SWIG_addvarlink(PyObject *p, char *name,
PyObject *(*get_attr)(void), int (*set_attr)(PyObject *p)) {
swig_varlinkobject *v;
swig_globalvar *gv;
v= (swig_varlinkobject *) p;
gv = (swig_globalvar *) malloc(sizeof(swig_globalvar));
gv->name = (char *) malloc(strlen(name)+1);
strcpy(gv->name,name);
gv->get_attr = get_attr;
gv->set_attr = set_attr;
gv->next = v->vars;
v->vars = gv;
}
/* Pack binary data into a string */
SWIGRUNTIME(char *)
SWIG_PackData(char *c, void *ptr, int sz) {
static char hex[17] = "0123456789abcdef";
int i;
unsigned char *u = (unsigned char *) ptr;
register unsigned char uu;
for (i = 0; i < sz; i++,u++) {
uu = *u;
*(c++) = hex[(uu & 0xf0) >> 4];
*(c++) = hex[uu & 0xf];
}
return c;
}
/* Unpack binary data from a string */
SWIGRUNTIME(char *)
SWIG_UnpackData(char *c, void *ptr, int sz) {
register unsigned char uu = 0;
register int d;
unsigned char *u = (unsigned char *) ptr;
int i;
for (i = 0; i < sz; i++, u++) {
d = *(c++);
if ((d >= '0') && (d <= '9'))
uu = ((d - '0') << 4);
else if ((d >= 'a') && (d <= 'f'))
uu = ((d - ('a'-10)) << 4);
d = *(c++);
if ((d >= '0') && (d <= '9'))
uu |= (d - '0');
else if ((d >= 'a') && (d <= 'f'))
uu |= (d - ('a'-10));
*u = uu;
}
return c;
}
/* Convert a pointer value */
SWIGRUNTIME(int)
SWIG_ConvertPtr(PyObject *obj, void **ptr, swig_type_info *ty, int flags) {
swig_type_info *tc;
char *c;
static PyObject *SWIG_this = 0;
int newref = 0;
PyObject *pyobj = 0;
if (!obj) return 0;
if (obj == Py_None) {
*ptr = 0;
return 0;
}
#ifdef SWIG_COBJECT_TYPES
if (!(PyCObject_Check(obj))) {
if (!SWIG_this)
SWIG_this = PyString_FromString("this");
pyobj = obj;
obj = PyObject_GetAttr(obj,SWIG_this);
newref = 1;
if (!obj) goto type_error;
if (!PyCObject_Check(obj)) {
Py_DECREF(obj);
goto type_error;
}
}
*ptr = PyCObject_AsVoidPtr(obj);
c = (char *) PyCObject_GetDesc(obj);
if (newref) Py_DECREF(obj);
goto cobject;
#else
if (!(PyString_Check(obj))) {
if (!SWIG_this)
SWIG_this = PyString_FromString("this");
pyobj = obj;
obj = PyObject_GetAttr(obj,SWIG_this);
newref = 1;
if (!obj) goto type_error;
if (!PyString_Check(obj)) {
Py_DECREF(obj);
goto type_error;
}
}
c = PyString_AsString(obj);
/* Pointer values must start with leading underscore */
if (*c != '_') {
*ptr = (void *) 0;
if (strcmp(c,"NULL") == 0) {
if (newref) { Py_DECREF(obj); }
return 0;
} else {
if (newref) { Py_DECREF(obj); }
goto type_error;
}
}
c++;
c = SWIG_UnpackData(c,ptr,sizeof(void *));
if (newref) { Py_DECREF(obj); }
#endif
#ifdef SWIG_COBJECT_TYPES
cobject:
#endif
if (ty) {
tc = SWIG_TypeCheck(c,ty);
if (!tc) goto type_error;
*ptr = SWIG_TypeCast(tc,(void*) *ptr);
}
if ((pyobj) && (flags & SWIG_POINTER_DISOWN)) {
PyObject *zero = PyInt_FromLong(0);
PyObject_SetAttrString(pyobj,(char*)"thisown",zero);
Py_DECREF(zero);
}
return 0;
type_error:
if (flags & SWIG_POINTER_EXCEPTION) {
if (ty) {
char *temp = (char *) malloc(64+strlen(ty->name));
sprintf(temp,"Type error. Expected %s", ty->name);
PyErr_SetString(PyExc_TypeError, temp);
free((char *) temp);
} else {
PyErr_SetString(PyExc_TypeError,"Expected a pointer");
}
}
return -1;
}
/* Convert a packed value value */
SWIGRUNTIME(int)
SWIG_ConvertPacked(PyObject *obj, void *ptr, int sz, swig_type_info *ty, int flags) {
swig_type_info *tc;
char *c;
if ((!obj) || (!PyString_Check(obj))) goto type_error;
c = PyString_AsString(obj);
/* Pointer values must start with leading underscore */
if (*c != '_') goto type_error;
c++;
c = SWIG_UnpackData(c,ptr,sz);
if (ty) {
tc = SWIG_TypeCheck(c,ty);
if (!tc) goto type_error;
}
return 0;
type_error:
if (flags) {
if (ty) {
char *temp = (char *) malloc(64+strlen(ty->name));
sprintf(temp,"Type error. Expected %s", ty->name);
PyErr_SetString(PyExc_TypeError, temp);
free((char *) temp);
} else {
PyErr_SetString(PyExc_TypeError,"Expected a pointer");
}
}
return -1;
}
/* Create a new pointer object */
SWIGRUNTIME(PyObject *)
SWIG_NewPointerObj(void *ptr, swig_type_info *type, int own) {
PyObject *robj;
if (!ptr) {
Py_INCREF(Py_None);
return Py_None;
}
#ifdef SWIG_COBJECT_TYPES
robj = PyCObject_FromVoidPtrAndDesc((void *) ptr, (char *) type->name, NULL);
#else
{
char result[1024];
char *r = result;
*(r++) = '_';
r = SWIG_PackData(r,&ptr,sizeof(void *));
strcpy(r,type->name);
robj = PyString_FromString(result);
}
#endif
if (!robj || (robj == Py_None)) return robj;
if (type->clientdata) {
PyObject *inst;
PyObject *args = Py_BuildValue((char*)"(O)", robj);
Py_DECREF(robj);
inst = PyObject_CallObject((PyObject *) type->clientdata, args);
Py_DECREF(args);
if (inst) {
if (own) {
PyObject *n = PyInt_FromLong(1);
PyObject_SetAttrString(inst,(char*)"thisown",n);
Py_DECREF(n);
}
robj = inst;
}
}
return robj;
}
SWIGRUNTIME(PyObject *)
SWIG_NewPackedObj(void *ptr, int sz, swig_type_info *type) {
char result[1024];
char *r = result;
if ((2*sz + 1 + strlen(type->name)) > 1000) return 0;
*(r++) = '_';
r = SWIG_PackData(r,ptr,sz);
strcpy(r,type->name);
return PyString_FromString(result);
}
/* Install Constants */
SWIGRUNTIME(void)
SWIG_InstallConstants(PyObject *d, swig_const_info constants[]) {
int i;
PyObject *obj;
for (i = 0; constants[i].type; i++) {
switch(constants[i].type) {
case SWIG_PY_INT:
obj = PyInt_FromLong(constants[i].lvalue);
break;
case SWIG_PY_FLOAT:
obj = PyFloat_FromDouble(constants[i].dvalue);
break;
case SWIG_PY_STRING:
obj = PyString_FromString((char *) constants[i].pvalue);
break;
case SWIG_PY_POINTER:
obj = SWIG_NewPointerObj(constants[i].pvalue, *(constants[i]).ptype,0);
break;
case SWIG_PY_BINARY:
obj = SWIG_NewPackedObj(constants[i].pvalue, constants[i].lvalue, *(constants[i].ptype));
break;
default:
obj = 0;
break;
}
if (obj) {
PyDict_SetItemString(d,constants[i].name,obj);
Py_DECREF(obj);
}
}
}
#endif
#ifdef __cplusplus
}
#endif

View file

@ -1,321 +1,627 @@
/***********************************************************************
/* -----------------------------------------------------------------------------
* python.swg
*
* This file contains the runtime support for Python modules
* and includes code for managing global variables and pointer
* type checking.
*
* Author : David Beazley (beazley@cs.uchicago.edu)
************************************************************************/
#include <stdlib.h>
#include "Python.h"
#ifdef __cplusplus
extern "C" {
#endif
#define SWIG_PY_INT 1
#define SWIG_PY_FLOAT 2
#define SWIG_PY_STRING 3
#define SWIG_PY_POINTER 4
/* Constant information structure */
typedef struct swig_const_info {
int type;
char *name;
long lvalue;
double dvalue;
void *pvalue;
swig_type_info **ptype;
} swig_const_info;
#ifdef SWIG_NOINCLUDE
SWIGEXPORT(PyObject *) SWIG_newvarlink();
SWIGEXPORT(void) SWIG_addvarlink(PyObject *, char *, PyObject *(*)(void), int (*)(PyObject *));
SWIGEXPORT(int) SWIG_ConvertPtr(PyObject *, void **, swig_type_info *, int);
SWIGEXPORT(void) SWIG_MakePtr(char *c, void *, swig_type_info *);
SWIGEXPORT(PyObject *) SWIG_NewPointerObj(void *, swig_type_info *);
SWIGEXPORT(void) SWIG_InstallConstants(PyObject *d, swig_const_info constants[]);
#else
/* -----------------------------------------------------------------------------
* global variable support code.
* Python configuration module.
* ----------------------------------------------------------------------------- */
typedef struct swig_globalvar {
char *name; /* Name of global variable */
PyObject *(*get_attr)(void); /* Return the current value */
int (*set_attr)(PyObject *); /* Set the value */
struct swig_globalvar *next;
} swig_globalvar;
/* Python.h has to appear first */
typedef struct swig_varlinkobject {
PyObject_HEAD
swig_globalvar *vars;
} swig_varlinkobject;
%insert(runtime) %{
#include "Python.h"
%}
static PyObject *
swig_varlink_repr(swig_varlinkobject *v) {
v = v;
return PyString_FromString("<Global variables>");
%insert(runtime) "common.swg"; // Common type-checking code
%insert(runtime) "pyrun.swg"; // Python run-time code
/* Special directive for shadow code */
#define %shadow %insert("shadow")
#define %pythoncode %insert("python")
/* -----------------------------------------------------------------------------
* standard typemaps
* ----------------------------------------------------------------------------- */
/* --- Input arguments --- */
/* Primitive datatypes. These only supply a parse code to PyTuple_ParseArgs */
%typemap(in,parse="i") int "";
%typemap(in,parse="h") short "";
%typemap(in,parse="l") long "";
%typemap(in,parse="b") signed char "";
%typemap(in) unsigned int, unsigned short, unsigned long, unsigned char
"$1 = ($1_ltype) PyInt_AsLong($input);
if (PyErr_Occurred()) SWIG_fail;";
%typemap(in) long long
"$1 = (long long) PyLong_AsLongLong($input);
if (PyErr_Occurred()) SWIG_fail;";
%typemap(in) unsigned long long
"$1 = (unsigned long long) PyLong_AsUnsignedLongLong($input);
if (PyErr_Occurred()) SWIG_fail;";
%typemap(in,parse="f") float "";
%typemap(in,parse="d") double "";
%typemap(in,parse="c") char "";
%typemap(in,parse="s") char *, char [ANY] "";
/* Boolean values. Have to convert from a long since */
%typemap(in) bool "$1 = (bool) PyInt_AsLong($input);
if (PyErr_Occurred()) SWIG_fail;";
/* Enum values. */
%typemap(in,parse="i") enum SWIGTYPE "";
/* Pointers, references, and arrays */
%typemap(in) SWIGTYPE *,
SWIGTYPE []
"if ((SWIG_ConvertPtr($input,(void **) &$1, $1_descriptor,SWIG_POINTER_EXCEPTION | $disown )) == -1) SWIG_fail;"
/* Additional check for null references */
%typemap(in) SWIGTYPE &
"if ((SWIG_ConvertPtr($input,(void **) &$1, $1_descriptor,SWIG_POINTER_EXCEPTION | $disown )) == -1) SWIG_fail;
if ($1 == NULL) { PyErr_SetString(PyExc_TypeError,\"null reference\"); SWIG_fail; }"
/* Void pointer. Accepts any kind of pointer */
%typemap(in) void * "if ((SWIG_ConvertPtr($input,(void **) &$1, 0, SWIG_POINTER_EXCEPTION | $disown )) == -1) SWIG_fail;"
/* Object passed by value. Convert to a pointer */
%typemap(in) SWIGTYPE ($&1_ltype argp) "if ((SWIG_ConvertPtr($input,(void **) &argp, $&1_descriptor,SWIG_POINTER_EXCEPTION) == -1)) SWIG_fail;
$1 = *argp; ";
/* Pointer to a class member */
%typemap(in) SWIGTYPE (CLASS::*) "if ((SWIG_ConvertPacked($input, (void *) &$1, sizeof($1_type), $1_descriptor,SWIG_POINTER_EXCEPTION)) == -1) SWIG_fail;";
/* Const primitive references. Passed by value */
%typemap(in) const int & (int temp),
const short & (short temp),
const long & (long temp),
const unsigned int & (unsigned int temp),
const unsigned short & (unsigned short temp),
const unsigned long & (unsigned long temp),
const signed char & (signed char temp),
const unsigned char & (unsigned char temp),
const bool & (bool temp)
"temp = ($*1_ltype) PyInt_AsLong($input);
if (PyErr_Occurred()) SWIG_fail;
$1 = &temp;";
%typemap(in) const float & (float temp),
const double & (double temp)
"temp = ($*1_ltype) PyFloat_AsDouble($input);
if (PyErr_Occurred()) SWIG_fail;
$1 = &temp;";
%typemap(in) const long long & (long long temp)
"temp = (long long) PyLong_AsLongLong($input);
if (PyErr_Occurred()) SWIG_fail;
$1 = &temp;";
%typemap(in) const unsigned long long & (unsigned long long temp)
"temp = (unsigned long long) PyLong_AsUnsignedLongLong($input);
if (PyErr_Occurred()) SWIG_fail;
$1 = &temp;";
%typemap(in) const char &(char temp) {
char *stemp = PyString_AsString($input);
if (PyErr_Occurred()) SWIG_fail;
temp = *stemp;
$1 = &temp;
}
static int
swig_varlink_print(swig_varlinkobject *v, FILE *fp, int flags) {
swig_globalvar *var;
flags = flags;
fprintf(fp,"Global variables { ");
for (var = v->vars; var; var=var->next) {
fprintf(fp,"%s", var->name);
if (var->next) fprintf(fp,", ");
}
fprintf(fp," }\n");
return 0;
/* --- Output values --- */
%typemap(out) int, unsigned int,
short, unsigned short,
long, unsigned long,
signed char, unsigned char,
bool, enum SWIGTYPE
"$result = PyInt_FromLong((long)$1);";
%typemap(out) long long "$result = PyLong_FromLongLong($1);";
%typemap(out) unsigned long long "$result = PyLong_FromUnsignedLongLong($1);";
%typemap(out) float, double "$result = PyFloat_FromDouble($1);";
%typemap(out) char * "$result = $1 ? PyString_FromString($1) : Py_BuildValue((char*)\"\");";
%typemap(out) char "$result = Py_BuildValue((char*)\"c\",$1);";
/* Pointers, references, and arrays */
%typemap(out) SWIGTYPE *, SWIGTYPE &, SWIGTYPE [] "$result = SWIG_NewPointerObj((void *) $1, $1_descriptor, $owner);";
/* Dynamic casts */
%typemap(out) SWIGTYPE *DYNAMIC, SWIGTYPE &DYNAMIC {
swig_type_info *ty = SWIG_TypeDynamicCast($1_descriptor, (void **) &$1);
$result = SWIG_NewPointerObj((void *) $1, ty, $owner);
}
static PyObject *
swig_varlink_getattr(swig_varlinkobject *v, char *n) {
swig_globalvar *var = v->vars;
while (var) {
if (strcmp(var->name,n) == 0) {
return (*var->get_attr)();
}
var = var->next;
/* Member pointer */
%typemap(out) SWIGTYPE (CLASS::*) "$result = SWIG_NewPackedObj((void *) &$1, sizeof($1_type), $1_descriptor);";
/* Void */
%typemap(out) void "Py_INCREF(Py_None); $result = Py_None;";
/* Special typemap for character array return values */
%typemap(out) char [ANY], const char [ANY] "$result = PyString_FromString($1);";
/* Primitive types--return by value */
%typemap(out) SWIGTYPE
#ifdef __cplusplus
{
$&1_ltype resultptr;
resultptr = new $1_ltype(($1_ltype &) $1);
$result = SWIG_NewPointerObj((void *) resultptr, $&1_descriptor, 1);
}
#else
{
$&1_ltype resultptr;
resultptr = ($&1_ltype) malloc(sizeof($1_type));
memmove(resultptr, &$1, sizeof($1_type));
$result = SWIG_NewPointerObj((void *) resultptr, $&1_descriptor, 1);
}
#endif
/* References to primitive types. Return by value */
%typemap(out) const int &, const unsigned int &,
const short &, const unsigned short &,
const long &, const unsigned long &,
const signed char &, const unsigned char &,
const bool &
"$result = PyInt_FromLong((long) *($1));";
%typemap(out) const float &, const double &
"$result = PyFloat_FromDouble((double) *($1));";
%typemap(out) const long long &
"$result = PyLong_FromLongLong(*($1));";
%typemap(out) const unsigned long long &
"$result = PyLong_FromUnsignedLongLong(*($1));";
%typemap(out) const char &
"$result = PyString_FromStringAndSize($1,1);";
/* --- Variable input --- */
%typemap(varin) int, unsigned int, short, unsigned short, long, unsigned long, signed char, unsigned char, bool, enum SWIGTYPE
{
long temp = PyInt_AsLong($input);
if (PyErr_Occurred()) {
PyErr_SetString(PyExc_TypeError, "C variable '$name ($1_ltype)'");
return 1;
}
PyErr_SetString(PyExc_NameError,"Unknown C global variable");
return NULL;
$1 = ($1_type) temp;
}
static int
swig_varlink_setattr(swig_varlinkobject *v, char *n, PyObject *p) {
swig_globalvar *var = v->vars;
while (var) {
if (strcmp(var->name,n) == 0) {
return (*var->set_attr)(p);
}
var = var->next;
%typemap(varin) long long {
long long temp = PyLong_AsLongLong($input);
if (PyErr_Occurred()) {
PyErr_SetString(PyExc_TypeError, "C variable '$name ($1_ltype)'");
return 1;
}
$1 = temp;
}
%typemap(varin) unsigned long long {
unsigned long long temp = PyLong_AsUnsignedLongLong($input);
if (PyErr_Occurred()) {
PyErr_SetString(PyExc_TypeError, "C variable '$name ($1_ltype)'");
return 1;
}
$1 = temp;
}
%typemap(varin) float, double {
double temp = PyFloat_AsDouble($input);
if (PyErr_Occurred()) {
PyErr_SetString(PyExc_TypeError, "C variable '$name ($1_ltype)'");
return 1;
}
PyErr_SetString(PyExc_NameError,"Unknown C global variable");
$1 = ($1_ltype) temp;
}
/* A single character */
%typemap(varin) char {
char *temp = PyString_AsString($input);
if (PyErr_Occurred()) {
PyErr_SetString(PyExc_TypeError, "C variable '$name ($1_ltype)'");
return 1;
}
$1 = *temp;
}
/* A string */
#ifdef __cplusplus
%typemap(varin) char * {
char *temp = (char *) PyString_AsString($input);
if (PyErr_Occurred()) {
PyErr_SetString(PyExc_TypeError, "C variable '$name ($1_ltype)'");
return 1;
}
if ($1) delete [] $1;
$1 = ($type) new char[strlen(temp)+1];
strcpy((char*)$1,temp);
}
%typemap(varin,warning="451:Setting const char * variable may leak memory") const char * {
char *temp = (char *) PyString_AsString($input);
if (PyErr_Occurred()) {
PyErr_SetString(PyExc_TypeError, "C variable '$name ($1_ltype)'");
return 1;
}
$1 = ($type) new char[strlen(temp)+1];
strcpy((char*)$1,temp);
}
#else
%typemap(varin) char * {
char *temp = (char *) PyString_AsString($input);
if (PyErr_Occurred()) {
PyErr_SetString(PyExc_TypeError, "C variable '$name ($1_ltype)'");
return 1;
}
if ($1) free((char*) $1);
$1 = ($type) malloc(strlen(temp)+1);
strcpy((char*)$1,temp);
}
%typemap(varin,warning="451:Setting const char * variable may leak memory") const char * {
char *temp = (char *) PyString_AsString($input);
if (PyErr_Occurred()) {
PyErr_SetString(PyExc_TypeError, "C variable '$name ($1_ltype)'");
return 1;
}
$1 = ($type) malloc(strlen(temp)+1);
strcpy((char*)$1,temp);
}
#endif
%typemap(varin) SWIGTYPE [] {
PyErr_SetString(PyExc_TypeError, "Variable $name is read-only.");
return 1;
}
statichere PyTypeObject varlinktype = {
PyObject_HEAD_INIT(0)
0,
"swigvarlink", /* Type name */
sizeof(swig_varlinkobject), /* Basic size */
0, /* Itemsize */
0, /* Deallocator */
(printfunc) swig_varlink_print, /* Print */
(getattrfunc) swig_varlink_getattr, /* get attr */
(setattrfunc) swig_varlink_setattr, /* Set attr */
0, /* tp_compare */
(reprfunc) swig_varlink_repr, /* tp_repr */
0, /* tp_as_number */
0, /* tp_as_mapping*/
0, /* tp_hash */
};
/* Create a variable linking object for use later */
SWIGRUNTIME(PyObject *)
SWIG_newvarlink(void) {
swig_varlinkobject *result = 0;
result = PyMem_NEW(swig_varlinkobject,1);
varlinktype.ob_type = &PyType_Type; /* Patch varlinktype into a PyType */
result->ob_type = &varlinktype;
result->vars = 0;
result->ob_refcnt = 0;
Py_XINCREF((PyObject *) result);
return ((PyObject*) result);
/* Special case for string array variables */
%typemap(varin) char [ANY] {
char *temp = (char *) PyString_AsString($input);
if (PyErr_Occurred()) {
PyErr_SetString(PyExc_TypeError, "C variable '$name ($1_ltype)'");
return 1;
}
strncpy($1,temp,$1_dim0);
}
SWIGRUNTIME(void)
SWIG_addvarlink(PyObject *p, char *name,
PyObject *(*get_attr)(void), int (*set_attr)(PyObject *p)) {
swig_varlinkobject *v;
swig_globalvar *gv;
v= (swig_varlinkobject *) p;
gv = (swig_globalvar *) malloc(sizeof(swig_globalvar));
gv->name = (char *) malloc(strlen(name)+1);
strcpy(gv->name,name);
gv->get_attr = get_attr;
gv->set_attr = set_attr;
gv->next = v->vars;
v->vars = gv;
}
/* Convert a pointer value */
SWIGRUNTIME(int)
SWIG_ConvertPtr(PyObject *obj, void **ptr, swig_type_info *ty, int flags) {
unsigned long p;
register int d;
swig_type_info *tc;
char *c;
static PyObject *SWIG_this = 0;
int newref = 0;
if (!obj || (obj == Py_None)) {
return 0;
%typemap(varin) SWIGTYPE * {
void *temp;
if ((SWIG_ConvertPtr($input,(void **) &temp, $1_descriptor, SWIG_POINTER_EXCEPTION | SWIG_POINTER_DISOWN)) == -1) {
PyErr_SetString(PyExc_TypeError, "C variable '$name ($1_ltype)'");
return 1;
}
#ifdef SWIG_COBJECT_TYPES
if (!(PyCObject_Check(obj))) {
if (!SWIG_this)
SWIG_this = PyString_InternFromString("this");
obj = PyObject_GetAttr(obj,SWIG_this);
newref = 1;
if (!obj) goto type_error;
if (!PyCObject_Check(obj)) {
Py_DECREF(obj);
goto type_error;
}
}
*ptr = PyCObject_AsVoidPtr(obj);
c = (char *) PyCObject_GetDesc(obj);
if (newref) Py_DECREF(obj);
goto cobject;
#else
if (!(PyString_Check(obj))) {
if (!SWIG_this)
SWIG_this = PyString_InternFromString("this");
obj = PyObject_GetAttr(obj,SWIG_this);
newref = 1;
if (!obj) goto type_error;
if (!PyString_Check(obj)) {
Py_DECREF(obj);
goto type_error;
}
}
c = PyString_AsString(obj);
p = 0;
/* Pointer values must start with leading underscore */
if (*c != '_') {
*ptr = (void *) 0;
if (strcmp(c,"NULL") == 0) {
if (newref) { Py_DECREF(obj); }
return 0;
} else {
if (newref) { Py_DECREF(obj); }
goto type_error;
}
}
c++;
/* Extract hex value from pointer */
while ((d = *c)) {
if ((d >= '0') && (d <= '9'))
p = (p << 4) + (d - '0');
else if ((d >= 'a') && (d <= 'f'))
p = (p << 4) + (d - ('a'-10));
else
break;
c++;
}
*ptr = (void *) p;
if (newref) { Py_DECREF(obj); }
#endif
#ifdef SWIG_COBJECT_TYPES
cobject:
#endif
if (ty) {
tc = SWIG_TypeCheck(c,ty);
if (!tc) goto type_error;
*ptr = SWIG_TypeCast(tc,(void*)p);
}
return 0;
type_error:
if (flags) {
if (ty) {
char *temp = (char *) malloc(64+strlen(ty->name));
sprintf(temp,"Type error. Expected %s", ty->name);
PyErr_SetString(PyExc_TypeError, temp);
free((char *) temp);
} else {
PyErr_SetString(PyExc_TypeError,"Expected a pointer");
}
}
return -1;
$1 = ($1_ltype) temp;
}
/* Take a pointer and convert it to a string */
SWIGRUNTIME(void)
SWIG_MakePtr(char *c, void *ptr, swig_type_info *ty) {
static char hex[17] = "0123456789abcdef";
unsigned long p, s;
char result[32], *r;
r = result;
p = (unsigned long) ptr;
if (p > 0) {
while (p > 0) {
s = p & 0xf;
*(r++) = hex[s];
p = p >> 4;
}
*r = '_';
while (r >= result)
*(c++) = *(r--);
strcpy (c, ty->name);
%typemap(varin) SWIGTYPE & {
void *temp;
if ((SWIG_ConvertPtr($input,(void **) &temp, $1_descriptor, SWIG_POINTER_EXCEPTION)) == -1 || temp == NULL) {
PyErr_SetString(PyExc_TypeError, "C variable '$name ($1_ltype)'");
return 1;
}
$1 = ($1_ltype) temp;
}
%typemap(varin) void * {
void * temp;
if ((SWIG_ConvertPtr($input,(void **) &temp, 0, SWIG_POINTER_EXCEPTION | SWIG_POINTER_DISOWN)) == -1) {
PyErr_SetString(PyExc_TypeError, "C variable '$name ($1_ltype)'");
return 1;
}
$1 = ($1_ltype) temp;
}
%typemap(varin) SWIGTYPE (CLASS::*) {
char temp[sizeof($1_type)];
if ((SWIG_ConvertPacked($input,(void *) temp, sizeof($1_type), $1_descriptor, SWIG_POINTER_EXCEPTION)) == -1) {
PyErr_SetString(PyExc_TypeError, "C variable '$name ($1_ltype)'");
return 1;
}
memmove((void *) &$1,temp,sizeof($1_type));
}
%typemap(varin) SWIGTYPE {
$&1_ltype temp;
if ((SWIG_ConvertPtr($input, (void **) &temp, $&1_descriptor, SWIG_POINTER_EXCEPTION)) == -1) {
PyErr_SetString(PyExc_TypeError, "C variable '$name ($1_ltype)'");
return 1;
}
$1 = *(($&1_type) temp);
}
/* --- Variable output --- */
%typemap(varout) int, unsigned int,
short, unsigned short,
long, unsigned long,
signed char, unsigned char,
bool, enum SWIGTYPE
"$result = PyInt_FromLong((long)$1);";
%typemap(varout) long long "$result = PyLong_FromLongLong($1);";
%typemap(varout) unsigned long long "$result = PyLong_FromUnsignedLongLong($1);";
%typemap(varout) float, double "$result = PyFloat_FromDouble($1);";
%typemap(varout) char * "$result = $1 ? PyString_FromString($1) : Py_BuildValue((char*)\"\");";
%typemap(varout) char "$result = Py_BuildValue((char*)\"c\",$1);";
/* Pointers, references, and arrays */
%typemap(varout) SWIGTYPE *, SWIGTYPE &, SWIGTYPE [] "$result = SWIG_NewPointerObj((void *) $1, $1_descriptor, 0);";
/* Member pointer */
%typemap(varout) SWIGTYPE (CLASS::*) "$result = SWIG_NewPackedObj((void *) &$1, sizeof($1_type), $1_descriptor);";
/* Void */
%typemap(varout) void "Py_INCREF(Py_None); $result = Py_None;";
/* Special typemap for character array return values */
%typemap(varout) char [ANY], const char [ANY] "$result = PyString_FromString($1);";
%typemap(varout) SWIGTYPE "$result = SWIG_NewPointerObj((void *) &$1, $&1_descriptor, 0);";
/* --- Constants --- */
%typemap(consttab) int, unsigned int, short, unsigned short, long, unsigned long, unsigned char, signed char, bool, enum SWIGTYPE
{ SWIG_PY_INT, (char *)"$symname", (long) $value, 0, 0, 0}
%typemap(consttab) float, double
{ SWIG_PY_FLOAT, (char*)"$symname", 0, (double) $value, 0, 0}
%typemap(consttab) char, char *
{ SWIG_PY_STRING, (char*)"$symname", 0, 0, (void *)"$value", 0}
%typemap(consttab) SWIGTYPE *, SWIGTYPE &, SWIGTYPE []
{ SWIG_PY_POINTER, (char*)"$symname", 0, 0, (void *)$value, &$1_descriptor}
%typemap(consttab) SWIGTYPE (CLASS::*)
{ SWIG_PY_BINARY, (char *)"$symname", sizeof($type), 0, (void *)&$value, &$1_descriptor}
%typemap(constcode) long long "PyDict_SetItemString(d,\"$symname\", PyLong_FromLongLong($value));";
%typemap(constcode) unsigned long long "PyDict_SetItemString(d,\"$symname\", PyLong_FromUnsignedLongLong($value));";
/* ------------------------------------------------------------
* String & length
* ------------------------------------------------------------ */
%typemap(in) (char *STRING, int LENGTH) {
$1 = ($1_ltype) PyString_AsString($input);
$2 = ($2_ltype) PyString_Size($input);
}
/* ------------------------------------------------------------
* ANSI C typemaps
* ------------------------------------------------------------ */
%typemap(in) size_t "$1 = (size_t) PyInt_AsLong($input);
if (PyErr_Occurred()) SWIG_fail;";
%typemap(out) size_t = long;
%typemap(varin) size_t = long;
%typemap(varout) size_t = long;
%typemap(consttab) size_t = long;
/* ------------------------------------------------------------
* PyObject * - Just pass straight through unmodified
* ------------------------------------------------------------ */
%typemap(in) PyObject * "$1 = $input;";
%typemap(out) PyObject * "$result = $1;";
/* ------------------------------------------------------------
* Typechecking rules
* ------------------------------------------------------------ */
%typecheck(SWIG_TYPECHECK_INTEGER)
int, short, long,
unsigned int, unsigned short, unsigned long,
signed char, unsigned char,
long long, unsigned long long,
const int &, const short &, const long &,
const unsigned int &, const unsigned short &, const unsigned long &,
const long long &, const unsigned long long &,
enum SWIGTYPE,
bool, const bool &
{
$1 = (PyInt_Check($input) || PyLong_Check($input)) ? 1 : 0;
}
%typecheck(SWIG_TYPECHECK_DOUBLE)
float, double,
const float &, const double &
{
$1 = (PyFloat_Check($input) || PyInt_Check($input) || PyLong_Check($input)) ? 1 : 0;
}
%typecheck(SWIG_TYPECHECK_CHAR) char {
$1 = (PyString_Check($input) && (PyString_Size($input) == 1)) ? 1 : 0;
}
%typecheck(SWIG_TYPECHECK_STRING) char * {
$1 = PyString_Check($input) ? 1 : 0;
}
%typecheck(SWIG_TYPECHECK_POINTER) SWIGTYPE *, SWIGTYPE &, SWIGTYPE [] {
void *ptr;
if (SWIG_ConvertPtr($input, (void **) &ptr, $1_descriptor, 0) == -1) {
$1 = 0;
PyErr_Clear();
} else {
strcpy (c, "NULL");
$1 = 1;
}
}
/* Create a new pointer object */
SWIGRUNTIME(PyObject *)
SWIG_NewPointerObj(void *ptr, swig_type_info *type) {
char result[512];
PyObject *robj;
if (!ptr) {
Py_INCREF(Py_None);
return Py_None;
}
#ifdef SWIG_COBJECT_TYPES
robj = PyCObject_FromVoidPtrAndDesc((void *) ptr, type->name, NULL);
#else
SWIG_MakePtr(result,ptr,type);
robj = PyString_FromString(result);
#endif
return robj;
}
/* Install Constants */
SWIGRUNTIME(void)
SWIG_InstallConstants(PyObject *d, swig_const_info constants[]) {
int i;
PyObject *obj;
for (i = 0; constants[i].type; i++) {
switch(constants[i].type) {
case SWIG_PY_INT:
obj = PyInt_FromLong(constants[i].lvalue);
break;
case SWIG_PY_FLOAT:
obj = PyFloat_FromDouble(constants[i].dvalue);
break;
case SWIG_PY_STRING:
obj = PyString_FromString((char *) constants[i].pvalue);
break;
case SWIG_PY_POINTER:
obj = SWIG_NewPointerObj(constants[i].pvalue, *(constants[i]).ptype);
break;
default:
obj = 0;
break;
}
if (obj) {
PyDict_SetItemString(d,constants[i].name,obj);
Py_DECREF(obj);
}
%typecheck(SWIG_TYPECHECK_POINTER) SWIGTYPE {
void *ptr;
if (SWIG_ConvertPtr($input, (void **) &ptr, $&1_descriptor, 0) == -1) {
$1 = 0;
PyErr_Clear();
} else {
$1 = 1;
}
}
#endif
%typecheck(SWIG_TYPECHECK_VOIDPTR) void * {
void *ptr;
if (SWIG_ConvertPtr($input, (void **) &ptr, 0, 0) == -1) {
$1 = 0;
PyErr_Clear();
} else {
$1 = 1;
}
}
/* ------------------------------------------------------------
* Exception handling
* ------------------------------------------------------------ */
%typemap(throws) int,
long,
short,
unsigned int,
unsigned long,
unsigned short {
PyErr_SetObject(PyExc_RuntimeError, PyInt_FromLong((long) $1));
SWIG_fail;
}
%typemap(throws) SWIGTYPE CLASS {
$&1_ltype temp = new $1_ltype($1);
if ($&1_descriptor->clientdata) {
PyErr_SetObject((PyObject *) ($&1_descriptor->clientdata), SWIG_NewPointerObj(temp,$&1_descriptor,1));
} else {
PyErr_SetObject(PyExc_RuntimeError, SWIG_NewPointerObj(temp,$&1_descriptor,1));
}
SWIG_fail;
}
%typemap(throws) SWIGTYPE {
PyErr_SetString(PyExc_RuntimeError,"$1_type");
SWIG_fail;
}
%typemap(throws) char * {
PyErr_SetString(PyExc_RuntimeError, $1);
SWIG_fail;
}
/* ------------------------------------------------------------
* Overloaded operator support
* ------------------------------------------------------------ */
#ifdef __cplusplus
}
%rename(__add__) *::operator+;
%rename(__pos__) *::operator+();
%rename(__pos__) *::operator+() const;
%rename(__sub__) *::operator-;
%rename(__neg__) *::operator-();
%rename(__neg__) *::operator-() const;
%rename(__mul__) *::operator*;
%rename(__div__) *::operator/;
%rename(__mod__) *::operator%;
%rename(__lshift__) *::operator<<;
%rename(__rshift__) *::operator>>;
%rename(__and__) *::operator&;
%rename(__or__) *::operator|;
%rename(__xor__) *::operator^;
%rename(__invert__) *::operator~;
%rename(__iadd__) *::operator+=;
%rename(__isub__) *::operator-=;
%rename(__imul__) *::operator*=;
%rename(__idiv__) *::operator/=;
%rename(__imod__) *::operator%=;
%rename(__ilshift__) *::operator<<=;
%rename(__irshift__) *::operator>>=;
%rename(__iand__) *::operator&=;
%rename(__ior__) *::operator|=;
%rename(__ixor__) *::operator^=;
%rename(__lt__) *::operator<;
%rename(__le__) *::operator<=;
%rename(__gt__) *::operator>;
%rename(__ge__) *::operator>=;
%rename(__eq__) *::operator==;
%rename(__ne__) *::operator!=;
/* Special cases */
%rename(__call__) *::operator();
/* Ignored operators */
%ignorewarn("362:operator= ignored") operator=;
%ignorewarn("383:operator++ ignored") operator++;
%ignorewarn("384:operator-- ignored") operator--;
%ignorewarn("381:operator&& ignored") operator&&;
%ignorewarn("382:operator|| ignored") operator||;
%ignorewarn("386:operator->* ignored") operator->*;
%ignorewarn("389:operator[] ignored (consider using %extend)") operator[];
#endif
/* Warnings for certain Python keywords */
#define PYKW(x) %namewarn("314:" #x " is a python keyword") #x
PYKW(and);
PYKW(assert);
PYKW(break);
PYKW(class);
PYKW(continue);
PYKW(def);
PYKW(del);
PYKW(elif);
PYKW(else);
PYKW(except);
PYKW(exec);
PYKW(finally);
PYKW(for);
PYKW(from);
PYKW(global);
PYKW(if);
PYKW(import);
PYKW(in);
PYKW(is);
PYKW(lambda);
PYKW(not);
PYKW(or);
PYKW(pass);
PYKW(print);
PYKW(raise);
PYKW(return);
PYKW(try);
PYKW(while);
/* The start of the Python initialization function */
%init %{
#ifdef __cplusplus
extern "C"
#endif
SWIGEXPORT(void) SWIG_init(void) {
static PyObject *SWIG_globals = 0;
static int typeinit = 0;
PyObject *m, *d;
int i;
if (!SWIG_globals) SWIG_globals = SWIG_newvarlink();
m = Py_InitModule((char *) SWIG_name, SwigMethods);
d = PyModule_GetDict(m);
if (!typeinit) {
for (i = 0; swig_types_initial[i]; i++) {
swig_types[i] = SWIG_TypeRegister(swig_types_initial[i]);
}
typeinit = 1;
}
SWIG_InstallConstants(d,swig_const_table);
%}

View file

@ -0,0 +1,26 @@
//
// SWIG typemaps for STL - common utilities
// Luigi Ballabio
// Aug 3, 2002
//
// Python implementation
%{
#include <string>
PyObject* SwigInt_FromBool(bool b) {
return PyInt_FromLong(b ? 1L : 0L);
}
double SwigNumber_Check(PyObject* o) {
return PyFloat_Check(o) || PyInt_Check(o);
}
double SwigNumber_AsDouble(PyObject* o) {
return (PyFloat_Check(o) ? PyFloat_AsDouble(o) : double(PyInt_AsLong(o)));
}
PyObject* SwigString_FromString(const std::string& s) {
return PyString_FromString(s.c_str());
}
std::string SwigString_AsString(PyObject* o) {
return std::string(PyString_AsString(o));
}
%}

View file

@ -0,0 +1,64 @@
#ifndef __swig_std_complex_i__
#define __swig_std_complex_i__
#ifdef SWIG
%{
#include <complex>
%}
namespace std
{
template <class T> class complex;
%define specialize_std_complex(T)
%typemap(in) complex<T> {
if (PyComplex_Check($input)) {
$1 = std::complex<T>(PyComplex_RealAsDouble($input),
PyComplex_ImagAsDouble($input));
} else if (PyFloat_Check($input)) {
$1 = std::complex<T>(PyFloat_AsDouble($input), 0);
} else if (PyInt_Check($input)) {
$1 = std::complex<T>(PyInt_AsLong($input), 0);
}
else {
PyErr_SetString(PyExc_TypeError,"Expected a complex");
SWIG_fail;
}
}
%typemap(in) const complex<T>& (std::complex<T> temp) {
if (PyComplex_Check($input)) {
temp = std::complex<T>(PyComplex_RealAsDouble($input),
PyComplex_ImagAsDouble($input));
$1 = &temp;
} else if (PyFloat_Check($input)) {
temp = std::complex<T>(PyFloat_AsDouble($input), 0);
$1 = &temp;
} else if (PyInt_Check($input)) {
temp = std::complex<T>(PyInt_AsLong($input), 0);
$1 = &temp;
} else {
PyErr_SetString(PyExc_TypeError,"Expected a complex");
SWIG_fail;
}
}
%typemap(out) complex<T> {
$result = PyComplex_FromDoubles($1.real(), $1.imag());
}
%typemap(out) const complex<T> & {
$result = PyComplex_FromDoubles($1->real(), $1->imag());
}
%enddef
specialize_std_complex(double);
specialize_std_complex(float);
}
#endif // SWIG
#endif //__swig_std_complex_i__

View file

@ -0,0 +1,23 @@
/* Default std_deque wrapper */
%module std_deque
%rename(__getitem__) std::deque::getitem;
%rename(__setitem__) std::deque::setitem;
%rename(__delitem__) std::deque::delitem;
%rename(__getslice__) std::deque::getslice;
%rename(__setslice__) std::deque::setslice;
%rename(__delslice__) std::deque::delslice;
%extend std::deque {
int __len__() {
return (int) self->size();
}
int __nonzero__() {
return ! self->empty();
}
void append(const T &x) {
self->push_back(x);
}
};
%include "_std_deque.i"

245
SWIG/Lib/python/std_list.i Normal file
View file

@ -0,0 +1,245 @@
//
// SWIG typemaps for std::list types
// Jing Cao
// Aug 1st, 2002
//
// Python implementation
%module std_list
%{
#include <list>
#include <stdexcept>
%}
%include "exception.i"
%exception std::list::__getitem__ {
try {
$action
} catch (std::out_of_range& e) {
SWIG_exception(SWIG_IndexError,const_cast<char*>(e.what()));
}
}
%exception std::list::__setitem__ {
try {
$action
} catch (std::out_of_range& e) {
SWIG_exception(SWIG_IndexError,const_cast<char*>(e.what()));
}
}
%exception std::list::__delitem__ {
try {
$action
} catch (std::out_of_range& e) {
SWIG_exception(SWIG_IndexError,const_cast<char*>(e.what()));
}
}
namespace std{
template<class T> class list
{
public:
typedef T &reference;
typedef const T& const_reference;
typedef T &iterator;
typedef const T& const_iterator;
list();
list(unsigned int size, const T& value = T());
list(const list<T> &);
~list();
void assign(unsigned int n, const T& value);
void swap(list<T> &x);
const_reference front();
const_reference back();
const_iterator begin();
const_iterator end();
void resize(unsigned int n, T c = T());
bool empty() const;
void push_front(const T& x);
void push_back(const T& x);
void pop_front();
void pop_back();
void clear();
unsigned int size() const;
unsigned int max_size() const;
void resize(unsigned int n, const T& value);
void remove(const T& value);
void unique();
void reverse();
void sort();
%extend
{
const_reference __getitem__(int i)
{
std::list<T>::iterator first = self->begin();
int size = int(self->size());
if (i<0) i += size;
if (i>=0 && i<size)
{
for (int k=0;k<i;k++)
{
first++;
}
return *first;
}
else throw std::out_of_range("list index out of range");
}
void __setitem__(int i, const T& x)
{
std::list<T>::iterator first = self->begin();
int size = int(self->size());
if (i<0) i += size;
if (i>=0 && i<size)
{
for (int k=0;k<i;k++)
{
first++;
}
*first = x;
}
else throw std::out_of_range("list index out of range");
}
void __delitem__(int i)
{
std::list<T>::iterator first = self->begin();
int size = int(self->size());
if (i<0) i += size;
if (i>=0 && i<size)
{
for (int k=0;k<i;k++)
{
first++;
}
self->erase(first);
}
else throw std::out_of_range("list index out of range");
}
std::list<T> __getslice__(int i,int j)
{
std::list<T>::iterator first = self->begin();
std::list<T>::iterator end = self->end();
int size = int(self->size());
if (i<0) i += size;
if (j<0) j += size;
if (i<0) i = 0;
if (j>size) j = size;
if (i>=j) i=j;
if (i>=0 && i<size && j>=0)
{
for (int k=0;k<i;k++)
{
first++;
}
for (int m=0;m<j;m++)
{
end++;
}
std::list<T> tmp(j-i);
if (j>i) std::copy(first,end,tmp.begin());
return tmp;
}
else throw std::out_of_range("list index out of range");
}
void __delslice__(int i,int j)
{
std::list<T>::iterator first = self->begin();
std::list<T>::iterator end = self->end();
int size = int(self->size());
if (i<0) i += size;
if (j<0) j += size;
if (i<0) i = 0;
if (j>size) j = size;
for (int k=0;k<i;k++)
{
first++;
}
for (int m=0;m<=j;m++)
{
end++;
}
self->erase(first,end);
}
void __setslice__(int i,int j, const std::list<T>& v)
{
std::list<T>::iterator first = self->begin();
std::list<T>::iterator end = self->end();
int size = int(self->size());
if (i<0) i += size;
if (j<0) j += size;
if (i<0) i = 0;
if (j>size) j = size;
for (int k=0;k<i;k++)
{
first++;
}
for (int m=0;m<=j;m++)
{
end++;
}
if (int(v.size()) == j-i)
{
std::copy(v.begin(),v.end(),first);
}
else {
self->erase(first,end);
if (i+1 <= self->size())
{
first = self->begin();
for (int k=0;k<i;k++)
{
first++;
}
self->insert(first,v.begin(),v.end());
}
else self->insert(self->end(),v.begin(),v.end());
}
}
unsigned int __len__()
{
return self->size();
}
bool __nonzero__()
{
return !(self->empty());
}
void append(const T& x)
{
self->push_back(x);
}
void pop()
{
self->pop_back();
}
};
};
}

View file

@ -0,0 +1,54 @@
//
// SWIG typemaps for std::string
// Luigi Ballabio
// Apr 8, 2002
//
// Python implementation
// ------------------------------------------------------------------------
// std::string is typemapped by value
// This can prevent exporting methods which return a string
// in order for the user to modify it.
// However, I think I'll wait until someone asks for it...
// ------------------------------------------------------------------------
%include exception.i
%{
#include <string>
%}
namespace std {
class string;
/* Overloading check */
%typemap(typecheck) string = char *;
%typemap(typecheck) const string & = char *;
%typemap(in) string {
if (PyString_Check($input))
$1 = std::string(PyString_AsString($input));
else
SWIG_exception(SWIG_TypeError, "string expected");
}
%typemap(in) const string & (std::string temp) {
if (PyString_Check($input)) {
temp = std::string(PyString_AsString($input));
$1 = &temp;
} else {
SWIG_exception(SWIG_TypeError, "string expected");
}
}
%typemap(out) string {
$result = PyString_FromString($1.c_str());
}
%typemap(out) const string & {
$result = PyString_FromString($1->c_str());
}
}

View file

@ -0,0 +1,726 @@
//
// SWIG typemaps for std::vector types
// Luigi Ballabio
// Apr 8, 2002
//
// Python implementation
%include std_common.i
%include exception.i
// __getitem__ is required to raise an IndexError for for-loops to work
// other methods which can raise are made to throw an IndexError as well
%exception std::vector::__getitem__ {
try {
$action
} catch (std::out_of_range& e) {
SWIG_exception(SWIG_IndexError,const_cast<char*>(e.what()));
}
}
%exception std::vector::__setitem__ {
try {
$action
} catch (std::out_of_range& e) {
SWIG_exception(SWIG_IndexError,const_cast<char*>(e.what()));
}
}
%exception std::vector::__delitem__ {
try {
$action
} catch (std::out_of_range& e) {
SWIG_exception(SWIG_IndexError,const_cast<char*>(e.what()));
}
}
%exception std::vector::pop {
try {
$action
} catch (std::out_of_range& e) {
SWIG_exception(SWIG_IndexError,const_cast<char*>(e.what()));
}
}
// ------------------------------------------------------------------------
// std::vector
//
// The aim of all that follows would be to integrate std::vector with
// Python as much as possible, namely, to allow the user to pass and
// be returned Python tuples or lists.
// const declarations are used to guess the intent of the function being
// exported; therefore, the following rationale is applied:
//
// -- f(std::vector<T>), f(const std::vector<T>&), f(const std::vector<T>*):
// the parameter being read-only, either a Python sequence or a
// previously wrapped std::vector<T> can be passed.
// -- f(std::vector<T>&), f(std::vector<T>*):
// the parameter must be modified; therefore, only a wrapped std::vector
// can be passed.
// -- std::vector<T> f():
// the vector is returned by copy; therefore, a Python sequence of T:s
// is returned which is most easily used in other Python functions
// -- std::vector<T>& f(), std::vector<T>* f(), const std::vector<T>& f(),
// const std::vector<T>* f():
// the vector is returned by reference; therefore, a wrapped std::vector
// is returned
// ------------------------------------------------------------------------
%{
#include <vector>
#include <algorithm>
#include <stdexcept>
%}
// exported class
namespace std {
template<class T> class vector {
%typemap(in) vector<T> (std::vector<T>* v) {
if (PyTuple_Check($input) || PyList_Check($input)) {
unsigned int size = (PyTuple_Check($input) ?
PyTuple_Size($input) :
PyList_Size($input));
$1 = std::vector<T >(size);
for (unsigned int i=0; i<size; i++) {
T* x;
PyObject* o = PySequence_GetItem($input,i);
if ((SWIG_ConvertPtr(o,(void **) &x,
$descriptor(T *),0)) != -1) {
(($1_type &)$1)[i] = *x;
Py_DECREF(o);
} else {
Py_DECREF(o);
PyErr_SetString(PyExc_TypeError,
"vector<" #T "> expected");
SWIG_fail;
}
}
} else if (SWIG_ConvertPtr($input,(void **) &v,
$&1_descriptor,1) != -1){
$1 = *v;
} else {
PyErr_SetString(PyExc_TypeError,"vector<" #T "> expected");
SWIG_fail;
}
}
%typemap(in) const vector<T>& (std::vector<T> temp,
std::vector<T>* v),
const vector<T>* (std::vector<T> temp,
std::vector<T>* v) {
if (PyTuple_Check($input) || PyList_Check($input)) {
unsigned int size = (PyTuple_Check($input) ?
PyTuple_Size($input) :
PyList_Size($input));
temp = std::vector<T >(size);
$1 = &temp;
for (unsigned int i=0; i<size; i++) {
T* x;
PyObject* o = PySequence_GetItem($input,i);
if ((SWIG_ConvertPtr(o,(void **) &x,
$descriptor(T *),0)) != -1) {
temp[i] = *x;
Py_DECREF(o);
} else {
Py_DECREF(o);
PyErr_SetString(PyExc_TypeError,
"vector<" #T "> expected");
SWIG_fail;
}
}
} else if (SWIG_ConvertPtr($input,(void **) &v,
$1_descriptor,1) != -1){
$1 = v;
} else {
PyErr_SetString(PyExc_TypeError,"vector<" #T "> expected");
SWIG_fail;
}
}
%typemap(out) vector<T> {
$result = PyTuple_New($1.size());
for (unsigned int i=0; i<$1.size(); i++) {
T* ptr = new T((($1_type &)$1)[i]);
PyTuple_SetItem($result,i,
SWIG_NewPointerObj((void *) ptr,
$descriptor(T *), 1));
}
}
%typecheck(SWIG_TYPECHECK_VECTOR) vector<T> {
/* native sequence? */
if (PyTuple_Check($input) || PyList_Check($input)) {
unsigned int size = (PyTuple_Check($input) ?
PyTuple_Size($input) :
PyList_Size($input));
if (size == 0) {
/* an empty sequence can be of any type */
$1 = 1;
} else {
/* check the first element only */
T* x;
PyObject* o = PySequence_GetItem($input,0);
if ((SWIG_ConvertPtr(o,(void **) &x,
$descriptor(T *),0)) != -1)
$1 = 1;
else
$1 = 0;
}
} else {
/* wrapped vector? */
std::vector<T >* v;
if (SWIG_ConvertPtr($input,(void **) &v,
$&1_descriptor,0) != -1)
$1 = 1;
else
$1 = 0;
}
}
%typecheck(SWIG_TYPECHECK_VECTOR) const vector<T>&,
const vector<T>* {
/* native sequence? */
if (PyTuple_Check($input) || PyList_Check($input)) {
unsigned int size = (PyTuple_Check($input) ?
PyTuple_Size($input) :
PyList_Size($input));
if (size == 0) {
/* an empty sequence can be of any type */
$1 = 1;
} else {
/* check the first element only */
T* x;
PyObject* o = PySequence_GetItem($input,0);
if ((SWIG_ConvertPtr(o,(void **) &x,
$descriptor(T *),0)) != -1)
$1 = 1;
else
$1 = 0;
}
} else {
/* wrapped vector? */
std::vector<T >* v;
if (SWIG_ConvertPtr($input,(void **) &v,
$1_descriptor,0) != -1)
$1 = 1;
else
$1 = 0;
}
}
public:
vector(unsigned int size = 0);
vector(unsigned int size, const T& value);
vector(const vector<T> &);
%rename(__len__) size;
unsigned int size() const;
void clear();
%rename(append) push_back;
void push_back(const T& x);
%extend {
bool __nonzero__() {
return !(self->empty());
}
T pop() {
if (self->size() == 0)
throw std::out_of_range("pop from empty vector");
T x = self->back();
self->pop_back();
return x;
}
T& __getitem__(int i) {
int size = int(self->size());
if (i<0) i += size;
if (i>=0 && i<size)
return (*self)[i];
else
throw std::out_of_range("vector index out of range");
}
std::vector<T> __getslice__(int i, int j) {
int size = int(self->size());
if (i<0) i = size+i;
if (j<0) j = size+j;
if (i<0) i = 0;
if (j>size) j = size;
std::vector<T > tmp(j-i);
std::copy(self->begin()+i,self->begin()+j,tmp.begin());
return tmp;
}
void __setitem__(int i, const T& x) {
int size = int(self->size());
if (i<0) i+= size;
if (i>=0 && i<size)
(*self)[i] = x;
else
throw std::out_of_range("vector index out of range");
}
void __setslice__(int i, int j, const std::vector<T>& v) {
int size = int(self->size());
if (i<0) i = size+i;
if (j<0) j = size+j;
if (i<0) i = 0;
if (j>size) j = size;
if (int(v.size()) == j-i) {
std::copy(v.begin(),v.end(),self->begin()+i);
} else {
self->erase(self->begin()+i,self->begin()+j);
if (i+1 <= self->size()) {
self->insert(self->begin()+i,v.begin(),v.end());
} else {
self->insert(self->end(),v.begin(),v.end());
}
}
}
void __delitem__(int i) {
int size = int(self->size());
if (i<0) i+= size;
if (i>=0 && i<size)
self->erase(self->begin()+i);
else
throw std::out_of_range("vector index out of range");
}
void __delslice__(int i, int j) {
int size = int(self->size());
if (i<0) i = size+i;
if (j<0) j = size+j;
if (i<0) i = 0;
if (j>size) j = size;
self->erase(self->begin()+i,self->begin()+j);
}
}
};
// Partial specialization for vectors of pointers. [ beazley ]
template<class T> class vector<T*> {
%typemap(in) vector<T> (std::vector<T>* v) {
if (PyTuple_Check($input) || PyList_Check($input)) {
unsigned int size = (PyTuple_Check($input) ?
PyTuple_Size($input) :
PyList_Size($input));
$1 = std::vector<T >(size);
for (unsigned int i=0; i<size; i++) {
T x;
PyObject* o = PySequence_GetItem($input,i);
if ((SWIG_ConvertPtr(o,(void **) &x,
$descriptor(T),0)) != -1) {
(($1_type &)$1)[i] = x;
Py_DECREF(o);
} else {
Py_DECREF(o);
PyErr_SetString(PyExc_TypeError,
"vector<" #T "> expected");
SWIG_fail;
}
}
} else if (SWIG_ConvertPtr($input,(void **) &v,
$&1_descriptor,1) != -1){
$1 = *v;
} else {
PyErr_SetString(PyExc_TypeError,"vector<" #T "> expected");
SWIG_fail;
}
}
%typemap(in) const vector<T>& (std::vector<T> temp,
std::vector<T>* v),
const vector<T>* (std::vector<T> temp,
std::vector<T>* v) {
if (PyTuple_Check($input) || PyList_Check($input)) {
unsigned int size = (PyTuple_Check($input) ?
PyTuple_Size($input) :
PyList_Size($input));
temp = std::vector<T >(size);
$1 = &temp;
for (unsigned int i=0; i<size; i++) {
T x;
PyObject* o = PySequence_GetItem($input,i);
if ((SWIG_ConvertPtr(o,(void **) &x,
$descriptor(T),0)) != -1) {
temp[i] = x;
Py_DECREF(o);
} else {
Py_DECREF(o);
PyErr_SetString(PyExc_TypeError,
"vector<" #T "> expected");
SWIG_fail;
}
}
} else if (SWIG_ConvertPtr($input,(void **) &v,
$1_descriptor,1) != -1){
$1 = v;
} else {
PyErr_SetString(PyExc_TypeError,"vector<" #T "> expected");
SWIG_fail;
}
}
%typemap(out) vector<T> {
$result = PyTuple_New($1.size());
for (unsigned int i=0; i<$1.size(); i++) {
T ptr = (($1_type &)$1)[i];
PyTuple_SetItem($result,i,
SWIG_NewPointerObj((void *) ptr,
$descriptor(T), 0));
}
}
%typecheck(SWIG_TYPECHECK_VECTOR) vector<T> {
/* native sequence? */
if (PyTuple_Check($input) || PyList_Check($input)) {
unsigned int size = (PyTuple_Check($input) ?
PyTuple_Size($input) :
PyList_Size($input));
if (size == 0) {
/* an empty sequence can be of any type */
$1 = 1;
} else {
/* check the first element only */
T x;
PyObject* o = PySequence_GetItem($input,0);
if ((SWIG_ConvertPtr(o,(void **) &x,
$descriptor(T),0)) != -1)
$1 = 1;
else
$1 = 0;
}
} else {
/* wrapped vector? */
std::vector<T >* v;
if (SWIG_ConvertPtr($input,(void **) &v,
$&1_descriptor,0) != -1)
$1 = 1;
else
$1 = 0;
}
}
%typecheck(SWIG_TYPECHECK_VECTOR) const vector<T>&,
const vector<T>* {
/* native sequence? */
if (PyTuple_Check($input) || PyList_Check($input)) {
unsigned int size = (PyTuple_Check($input) ?
PyTuple_Size($input) :
PyList_Size($input));
if (size == 0) {
/* an empty sequence can be of any type */
$1 = 1;
} else {
/* check the first element only */
T x;
PyObject* o = PySequence_GetItem($input,0);
if ((SWIG_ConvertPtr(o,(void **) &x,
$descriptor(T),0)) != -1)
$1 = 1;
else
$1 = 0;
}
} else {
/* wrapped vector? */
std::vector<T >* v;
if (SWIG_ConvertPtr($input,(void **) &v,
$1_descriptor,0) != -1)
$1 = 1;
else
$1 = 0;
}
}
public:
vector(unsigned int size = 0);
vector(unsigned int size, const T& value);
vector(const vector<T> &);
%rename(__len__) size;
unsigned int size() const;
void clear();
%rename(append) push_back;
void push_back(T x);
%extend {
bool __nonzero__() {
return !(self->empty());
}
T pop() {
if (self->size() == 0)
throw std::out_of_range("pop from empty vector");
T x = self->back();
self->pop_back();
return x;
}
T __getitem__(int i) {
int size = int(self->size());
if (i<0) i += size;
if (i>=0 && i<size)
return (*self)[i];
else
throw std::out_of_range("vector index out of range");
}
std::vector<T> __getslice__(int i, int j) {
int size = int(self->size());
if (i<0) i = size+i;
if (j<0) j = size+j;
if (i<0) i = 0;
if (j>size) j = size;
std::vector<T > tmp(j-i);
std::copy(self->begin()+i,self->begin()+j,tmp.begin());
return tmp;
}
void __setitem__(int i, const T& x) {
int size = int(self->size());
if (i<0) i+= size;
if (i>=0 && i<size)
(*self)[i] = x;
else
throw std::out_of_range("vector index out of range");
}
void __setslice__(int i, int j, const std::vector<T>& v) {
int size = int(self->size());
if (i<0) i = size+i;
if (j<0) j = size+j;
if (i<0) i = 0;
if (j>size) j = size;
if (int(v.size()) == j-i) {
std::copy(v.begin(),v.end(),self->begin()+i);
} else {
self->erase(self->begin()+i,self->begin()+j);
if (i+1 <= self->size())
self->insert(self->begin()+i,v.begin(),v.end());
else
self->insert(self->end(),v.begin(),v.end());
}
}
void __delitem__(int i) {
int size = int(self->size());
if (i<0) i+= size;
if (i>=0 && i<size)
self->erase(self->begin()+i);
else
throw std::out_of_range("vector index out of range");
}
void __delslice__(int i, int j) {
int size = int(self->size());
if (i<0) i = size+i;
if (j<0) j = size+j;
if (i<0) i = 0;
if (j>size) j = size;
self->erase(self->begin()+i,self->begin()+j);
}
}
};
// specializations for built-ins
%define specialize_std_vector(T,CHECK,CONVERT_FROM,CONVERT_TO)
template<> class vector<T> {
%typemap(in) vector<T> (std::vector<T>* v) {
if (PyTuple_Check($input) || PyList_Check($input)) {
unsigned int size = (PyTuple_Check($input) ?
PyTuple_Size($input) :
PyList_Size($input));
$1 = std::vector<T >(size);
for (unsigned int i=0; i<size; i++) {
PyObject* o = PySequence_GetItem($input,i);
if (CHECK(o)) {
(($1_type &)$1)[i] = (T)(CONVERT_FROM(o));
Py_DECREF(o);
} else {
Py_DECREF(o);
PyErr_SetString(PyExc_TypeError,
"vector<" #T "> expected");
SWIG_fail;
}
}
} else if (SWIG_ConvertPtr($input,(void **) &v,
$&1_descriptor,1) != -1){
$1 = *v;
} else {
PyErr_SetString(PyExc_TypeError,"vector<" #T "> expected");
SWIG_fail;
}
}
%typemap(in) const vector<T>& (std::vector<T> temp,
std::vector<T>* v),
const vector<T>* (std::vector<T> temp,
std::vector<T>* v) {
if (PyTuple_Check($input) || PyList_Check($input)) {
unsigned int size = (PyTuple_Check($input) ?
PyTuple_Size($input) :
PyList_Size($input));
temp = std::vector<T >(size);
$1 = &temp;
for (unsigned int i=0; i<size; i++) {
PyObject* o = PySequence_GetItem($input,i);
if (CHECK(o)) {
temp[i] = (T)(CONVERT_FROM(o));
Py_DECREF(o);
} else {
Py_DECREF(o);
PyErr_SetString(PyExc_TypeError,
"vector<" #T "> expected");
SWIG_fail;
}
}
} else if (SWIG_ConvertPtr($input,(void **) &v,
$1_descriptor,1) != -1){
$1 = v;
} else {
PyErr_SetString(PyExc_TypeError,"vector<" #T "> expected");
SWIG_fail;
}
}
%typemap(out) vector<T> {
$result = PyTuple_New($1.size());
for (unsigned int i=0; i<$1.size(); i++)
PyTuple_SetItem($result,i,
CONVERT_TO((($1_type &)$1)[i]));
}
%typecheck(SWIG_TYPECHECK_VECTOR) vector<T> {
/* native sequence? */
if (PyTuple_Check($input) || PyList_Check($input)) {
unsigned int size = (PyTuple_Check($input) ?
PyTuple_Size($input) :
PyList_Size($input));
if (size == 0) {
/* an empty sequence can be of any type */
$1 = 1;
} else {
/* check the first element only */
PyObject* o = PySequence_GetItem($input,0);
if (CHECK(o))
$1 = 1;
else
$1 = 0;
}
} else {
/* wrapped vector? */
std::vector<T >* v;
if (SWIG_ConvertPtr($input,(void **) &v,
$&1_descriptor,0) != -1)
$1 = 1;
else
$1 = 0;
}
}
%typecheck(SWIG_TYPECHECK_VECTOR) const vector<T>&,
const vector<T>* {
/* native sequence? */
if (PyTuple_Check($input) || PyList_Check($input)) {
unsigned int size = (PyTuple_Check($input) ?
PyTuple_Size($input) :
PyList_Size($input));
if (size == 0) {
/* an empty sequence can be of any type */
$1 = 1;
} else {
/* check the first element only */
PyObject* o = PySequence_GetItem($input,0);
if (CHECK(o))
$1 = 1;
else
$1 = 0;
}
} else {
/* wrapped vector? */
std::vector<T >* v;
if (SWIG_ConvertPtr($input,(void **) &v,
$1_descriptor,0) != -1)
$1 = 1;
else
$1 = 0;
}
}
public:
vector(unsigned int size = 0);
vector(unsigned int size, const T& value);
vector(const vector<T> &);
%rename(__len__) size;
unsigned int size() const;
%rename(__nonzero__) empty;
bool empty() const;
void clear();
%rename(append) push_back;
void push_back(T x);
%extend {
T pop() {
if (self->size() == 0)
throw std::out_of_range("pop from empty vector");
T x = self->back();
self->pop_back();
return x;
}
T __getitem__(int i) {
int size = int(self->size());
if (i<0) i += size;
if (i>=0 && i<size)
return (*self)[i];
else
throw std::out_of_range("vector index out of range");
}
std::vector<T> __getslice__(int i, int j) {
int size = int(self->size());
if (i<0) i = size+i;
if (j<0) j = size+j;
if (i<0) i = 0;
if (j>size) j = size;
std::vector<T > tmp(j-i);
std::copy(self->begin()+i,self->begin()+j,tmp.begin());
return tmp;
}
void __setitem__(int i, T x) {
int size = int(self->size());
if (i<0) i+= size;
if (i>=0 && i<size)
(*self)[i] = x;
else
throw std::out_of_range("vector index out of range");
}
void __setslice__(int i, int j, const std::vector<T>& v) {
int size = int(self->size());
if (i<0) i = size+i;
if (j<0) j = size+j;
if (i<0) i = 0;
if (j>size) j = size;
if (int(v.size()) == j-i) {
std::copy(v.begin(),v.end(),self->begin()+i);
} else {
self->erase(self->begin()+i,self->begin()+j);
if (i+1 <= self->size())
self->insert(self->begin()+i,v.begin(),v.end());
else
self->insert(self->end(),v.begin(),v.end());
}
}
void __delitem__(int i) {
int size = int(self->size());
if (i<0) i+= size;
if (i>=0 && i<size)
self->erase(self->begin()+i);
else
throw std::out_of_range("vector index out of range");
}
void __delslice__(int i, int j) {
int size = int(self->size());
if (i<0) i = size+i;
if (j<0) j = size+j;
if (i<0) i = 0;
if (j>size) j = size;
self->erase(self->begin()+i,self->begin()+j);
}
}
};
%enddef
specialize_std_vector(bool,PyInt_Check,PyInt_AsLong,SwigInt_FromBool);
specialize_std_vector(int,PyInt_Check,PyInt_AsLong,PyInt_FromLong);
specialize_std_vector(short,PyInt_Check,PyInt_AsLong,PyInt_FromLong);
specialize_std_vector(long,PyInt_Check,PyInt_AsLong,PyInt_FromLong);
specialize_std_vector(unsigned int,PyInt_Check,\
PyInt_AsLong,PyInt_FromLong);
specialize_std_vector(unsigned short,PyInt_Check,\
PyInt_AsLong,PyInt_FromLong);
specialize_std_vector(unsigned long,PyInt_Check,\
PyInt_AsLong,PyInt_FromLong);
specialize_std_vector(double,SwigNumber_Check,\
SwigNumber_AsDouble,PyFloat_FromDouble);
specialize_std_vector(float,SwigNumber_Check,\
SwigNumber_AsDouble,PyFloat_FromDouble);
specialize_std_vector(std::string,PyString_Check,\
SwigString_AsString,SwigString_FromString);
}

9
SWIG/Lib/python/stl.i Normal file
View file

@ -0,0 +1,9 @@
//
// SWIG typemaps for STL types
// Luigi Ballabio and Manu ???
// Apr 26, 2002
//
%include std_string.i
%include std_vector.i

View file

@ -12,19 +12,6 @@
// Disclaimer : Unless you really understand how typemaps work, this file
// probably isn't going to make much sense.
//
#ifdef AUTODOC
%section "Typemap Library (Python)",info,after,pre,nosort,skip=1,chop_left=3,chop_right=0,chop_top=0,chop_bottom=0
%text %{
%include typemaps.i
The SWIG typemap library provides a language independent mechanism for
supporting output arguments, input values, and other C function
calling mechanisms. The primary use of the library is to provide a
better interface to certain C function--especially those involving
pointers.
%}
#endif
// ------------------------------------------------------------------------
// Pointer handling
@ -37,11 +24,7 @@ pointers.
// These remap a C pointer to be an "INPUT" value which is passed by value
// instead of reference.
#ifdef AUTODOC
%subsection "Input Methods"
%text %{
/*
The following methods can be applied to turn a pointer into a simple
"input" value. That is, instead of passing a pointer to an object,
you would use a real value instead.
@ -49,10 +32,13 @@ you would use a real value instead.
int *INPUT
short *INPUT
long *INPUT
long long *INPUT
unsigned int *INPUT
unsigned short *INPUT
unsigned long *INPUT
unsigned long long *INPUT
unsigned char *INPUT
bool *INPUT
float *INPUT
double *INPUT
@ -73,73 +59,40 @@ or you can use the %apply directive :
%apply double *INPUT { double *a, double *b };
double fadd(double *a, double *b);
%}
#endif
*/
%typemap(python,in) double *INPUT(double temp)
%define INPUT_TYPEMAP(type, converter)
%typemap(in) type *INPUT(type temp), type &INPUT(type temp)
{
temp = PyFloat_AsDouble($source);
$target = &temp;
temp = converter($input);
if (PyErr_Occurred()) SWIG_fail;
$1 = &temp;
}
%typemap(typecheck) type *INPUT = type;
%typemap(typecheck) type &INPUT = type;
%enddef
%typemap(python,in) float *INPUT(float temp)
{
temp = (float) PyFloat_AsDouble($source);
$target = &temp;
}
INPUT_TYPEMAP(float, PyFloat_AsDouble);
INPUT_TYPEMAP(double, PyFloat_AsDouble);
INPUT_TYPEMAP(int, PyInt_AsLong);
INPUT_TYPEMAP(short, PyInt_AsLong);
INPUT_TYPEMAP(long, PyInt_AsLong);
INPUT_TYPEMAP(long long, PyLong_AsLongLong);
INPUT_TYPEMAP(unsigned int, PyInt_AsLong);
INPUT_TYPEMAP(unsigned short, PyInt_AsLong);
INPUT_TYPEMAP(unsigned long, PyInt_AsLong);
INPUT_TYPEMAP(unsigned long long, PyLong_AsUnsignedLongLong);
INPUT_TYPEMAP(unsigned char, PyInt_AsLong);
INPUT_TYPEMAP(signed char, PyInt_AsLong);
INPUT_TYPEMAP(bool, PyInt_AsLong);
%typemap(python,in) int *INPUT(int temp)
{
temp = (int) PyInt_AsLong($source);
$target = &temp;
}
#undef INPUT_TYPEMAP
%typemap(python,in) short *INPUT(short temp)
{
temp = (short) PyInt_AsLong($source);
$target = &temp;
}
%typemap(python,in) long *INPUT(long temp)
{
temp = (long) PyInt_AsLong($source);
$target = &temp;
}
%typemap(python,in) unsigned int *INPUT(unsigned int temp)
{
temp = (unsigned int) PyInt_AsLong($source);
$target = &temp;
}
%typemap(python,in) unsigned short *INPUT(unsigned short temp)
{
temp = (unsigned short) PyInt_AsLong($source);
$target = &temp;
}
%typemap(python,in) unsigned long *INPUT(unsigned long temp)
{
temp = (unsigned long) PyInt_AsLong($source);
$target = &temp;
}
%typemap(python,in) unsigned char *INPUT(unsigned char temp)
{
temp = (unsigned char) PyInt_AsLong($source);
$target = &temp;
}
%typemap(python,in) signed char *INPUT(signed char temp)
{
temp = (unsigned char) PyInt_AsLong($source);
$target = &temp;
}
// OUTPUT typemaps. These typemaps are used for parameters that
// are output only. The output value is appended to the result as
// a list element.
#ifdef AUTODOC
%subsection "Output Methods"
%text %{
/*
The following methods can be applied to turn a pointer into an "output"
value. When calling a function, no input value would be given for
a parameter, but an output value would be returned. In the case of
@ -148,15 +101,16 @@ multiple output values, they are returned in the form of a Python tuple.
int *OUTPUT
short *OUTPUT
long *OUTPUT
long long *OUTPUT
unsigned int *OUTPUT
unsigned short *OUTPUT
unsigned long *OUTPUT
unsigned long long *OUTPUT
unsigned char *OUTPUT
bool *OUTPUT
float *OUTPUT
double *OUTPUT
A Python List can also be returned by using L_OUTPUT instead of OUTPUT.
For example, suppose you were trying to wrap the modf() function in the
C math library which splits x into integral and fractional parts (and
returns the integer part in one of its parameters).K:
@ -176,70 +130,8 @@ or you can use the %apply directive :
The Python output of the function would be a tuple containing both
output values.
%}
#endif
// Helper function for List output
%{
static PyObject* l_output_helper(PyObject* target, PyObject* o) {
PyObject* o2;
if (!target) {
target = o;
} else if (target == Py_None) {
Py_DECREF(Py_None);
target = o;
} else {
if (!PyList_Check(target)) {
o2 = target;
target = PyList_New(0);
PyList_Append(target, o2);
Py_XDECREF(o2);
}
PyList_Append(target,o);
Py_XDECREF(o);
}
return target;
}
%}
// Force the argument to be ignored.
%typemap(python,ignore) int *L_OUTPUT(int temp),
short *L_OUTPUT(short temp),
long *L_OUTPUT(long temp),
unsigned int *L_OUTPUT(unsigned int temp),
unsigned short *L_OUTPUT(unsigned short temp),
unsigned long *L_OUTPUT(unsigned long temp),
unsigned char *L_OUTPUT(unsigned char temp),
signed char *L_OUTPUT(signed char temp),
float *L_OUTPUT(float temp),
double *L_OUTPUT(double temp)
{
$target = &temp;
}
%typemap(python,argout) int *L_OUTPUT,
short *L_OUTPUT,
long *L_OUTPUT,
unsigned int *L_OUTPUT,
unsigned short *L_OUTPUT,
unsigned long *L_OUTPUT,
unsigned char *L_OUTPUT,
signed char *L_OUTPUT
{
PyObject *o;
o = PyInt_FromLong((long) (*$source));
l_output_helper($target,o);
}
%typemap(python,argout) float *L_OUTPUT,
double *L_OUTPUT
{
PyObject *o;
o = PyFloat_FromDouble((double) (*$source));
$target = l_output_helper($target,o);
}
*/
// These typemaps contributed by Robin Dunn
//----------------------------------------------------------------------
@ -250,139 +142,52 @@ static PyObject* l_output_helper(PyObject* target, PyObject* o) {
// Author: Robin Dunn
//----------------------------------------------------------------------
%{
static PyObject* t_output_helper(PyObject* target, PyObject* o) {
PyObject* o2;
PyObject* o3;
%include "fragments.i"
if (!target) {
target = o;
} else if (target == Py_None) {
Py_DECREF(Py_None);
target = o;
} else {
if (!PyTuple_Check(target)) {
o2 = target;
target = PyTuple_New(1);
PyTuple_SetItem(target, 0, o2);
}
o3 = PyTuple_New(1);
PyTuple_SetItem(o3, 0, o);
o2 = target;
target = PySequence_Concat(o2, o3);
Py_DECREF(o2);
Py_DECREF(o3);
}
return target;
%define OUTPUT_TYPEMAP(type, converter, convtype)
%typemap(in,numinputs=0) type *OUTPUT(type temp), type &OUTPUT(type temp) "$1 = &temp;";
%typemap(argout,fragment="t_output_helper") type *OUTPUT, type &OUTPUT {
PyObject *o = converter(convtype (*$1));
$result = t_output_helper($result,o);
}
%}
%enddef
// Force the argument to be ignored.
%typemap(python,ignore) int *T_OUTPUT(int temp),
short *T_OUTPUT(short temp),
long *T_OUTPUT(long temp),
unsigned int *T_OUTPUT(unsigned int temp),
unsigned short *T_OUTPUT(unsigned short temp),
unsigned long *T_OUTPUT(unsigned long temp),
unsigned char *T_OUTPUT(unsigned char temp),
float *T_OUTPUT(float temp),
double *T_OUTPUT(double temp)
{
$target = &temp;
}
OUTPUT_TYPEMAP(int, PyInt_FromLong, (long));
OUTPUT_TYPEMAP(short, PyInt_FromLong, (long));
OUTPUT_TYPEMAP(long, PyInt_FromLong, (long));
OUTPUT_TYPEMAP(long long, PyLong_FromLongLong, (long long));
OUTPUT_TYPEMAP(unsigned int, PyInt_FromLong, (long));
OUTPUT_TYPEMAP(unsigned short, PyInt_FromLong, (long));
OUTPUT_TYPEMAP(unsigned long, PyInt_FromLong, (long));
OUTPUT_TYPEMAP(unsigned long long, PyLong_FromUnsignedLongLong, (unsigned long long));
OUTPUT_TYPEMAP(unsigned char, PyInt_FromLong, (long));
OUTPUT_TYPEMAP(signed char, PyInt_FromLong, (long));
OUTPUT_TYPEMAP(bool, PyInt_FromLong, (long));
OUTPUT_TYPEMAP(float, PyFloat_FromDouble, (double));
OUTPUT_TYPEMAP(double, PyFloat_FromDouble, (double));
%typemap(python,argout) int *T_OUTPUT,
short *T_OUTPUT,
long *T_OUTPUT,
unsigned int *T_OUTPUT,
unsigned short *T_OUTPUT,
unsigned long *T_OUTPUT,
unsigned char *T_OUTPUT
{
PyObject *o;
o = PyInt_FromLong((long) (*$source));
$target = t_output_helper($target, o);
}
%typemap(python,argout) float *T_OUTPUT,
double *T_OUTPUT
{
PyObject *o;
o = PyFloat_FromDouble((double) (*$source));
$target = t_output_helper($target, o);
}
// Set the default output typemap
#ifdef OUTPUT_LIST
%typemap(python,ignore) int *OUTPUT = int *L_OUTPUT;
%typemap(python,ignore) short *OUTPUT = short *L_OUTPUT;
%typemap(python,ignore) long *OUTPUT = long *L_OUTPUT;
%typemap(python,ignore) unsigned *OUTPUT = unsigned *L_OUTPUT;
%typemap(python,ignore) unsigned short *OUTPUT = unsigned short *L_OUTPUT;
%typemap(python,ignore) unsigned long *OUTPUT = unsigned long *L_OUTPUT;
%typemap(python,ignore) unsigned char *OUTPUT = unsigned char *L_OUTPUT;
%typemap(python,ignore) signed char *OUTPUT = signed char *L_OUTPUT;
%typemap(python,ignore) double *OUTPUT = double *L_OUTPUT;
%typemap(python,ignore) float *OUTPUT = float *L_OUTPUT;
%typemap(python,argout) int *OUTPUT = int *L_OUTPUT;
%typemap(python,argout) short *OUTPUT = short *L_OUTPUT;
%typemap(python,argout) long *OUTPUT = long *L_OUTPUT;
%typemap(python,argout) unsigned *OUTPUT = unsigned *L_OUTPUT;
%typemap(python,argout) unsigned short *OUTPUT = unsigned short *L_OUTPUT;
%typemap(python,argout) unsigned long *OUTPUT = unsigned long *L_OUTPUT;
%typemap(python,argout) unsigned char *OUTPUT = unsigned char *L_OUTPUT;
%typemap(python,argout) signed char *OUTPUT = signed char *L_OUTPUT;
%typemap(python,argout) double *OUTPUT = double *L_OUTPUT;
%typemap(python,argout) float *OUTPUT = float *L_OUTPUT;
#else
%typemap(python,ignore) int *OUTPUT = int *T_OUTPUT;
%typemap(python,ignore) short *OUTPUT = short *T_OUTPUT;
%typemap(python,ignore) long *OUTPUT = long *T_OUTPUT;
%typemap(python,ignore) unsigned *OUTPUT = unsigned *T_OUTPUT;
%typemap(python,ignore) unsigned short *OUTPUT = unsigned short *T_OUTPUT;
%typemap(python,ignore) unsigned long *OUTPUT = unsigned long *T_OUTPUT;
%typemap(python,ignore) unsigned char *OUTPUT = unsigned char *T_OUTPUT;
%typemap(python,ignore) signed char *OUTPUT = signed char *T_OUTPUT;
%typemap(python,ignore) double *OUTPUT = double *T_OUTPUT;
%typemap(python,ignore) float *OUTPUT = float *T_OUTPUT;
%typemap(python,argout) int *OUTPUT = int *T_OUTPUT;
%typemap(python,argout) short *OUTPUT = short *T_OUTPUT;
%typemap(python,argout) long *OUTPUT = long *T_OUTPUT;
%typemap(python,argout) unsigned *OUTPUT = unsigned *T_OUTPUT;
%typemap(python,argout) unsigned short *OUTPUT = unsigned short *T_OUTPUT;
%typemap(python,argout) unsigned long *OUTPUT = unsigned long *T_OUTPUT;
%typemap(python,argout) unsigned char *OUTPUT = unsigned char *T_OUTPUT;
%typemap(python,argout) signed char *OUTPUT = signed char *T_OUTPUT;
%typemap(python,argout) double *OUTPUT = double *T_OUTPUT;
%typemap(python,argout) float *OUTPUT = float *T_OUTPUT;
#endif
#undef OUTPUT_TYPEMAP
// INOUT
// Mappings for an argument that is both an input and output
// parameter
#ifdef AUTODOC
%subsection "Input/Output Methods"
%text %{
/*
The following methods can be applied to make a function parameter both
an input and output value. This combines the behavior of both the
"INPUT" and "OUTPUT" methods described earlier. Output values are
returned in the form of a Python tuple. To return a Python list,
using L_INOUT instead.
returned in the form of a Python tuple.
int *INOUT
short *INOUT
long *INOUT
long long *INOUT
unsigned int *INOUT
unsigned short *INOUT
unsigned long *INOUT
unsigned long long *INOUT
unsigned char *INOUT
bool *INOUT
float *INOUT
double *INOUT
@ -413,149 +218,92 @@ to a Python variable you might do this :
Note : previous versions of SWIG used the symbol 'BOTH' to mark
input/output arguments. This is still supported, but will be slowly
phased out in future releases.
%}
#endif
*/
%typemap(python,in) int *INOUT = int *INPUT;
%typemap(python,in) short *INOUT = short *INPUT;
%typemap(python,in) long *INOUT = long *INPUT;
%typemap(python,in) unsigned *INOUT = unsigned *INPUT;
%typemap(python,in) unsigned short *INOUT = unsigned short *INPUT;
%typemap(python,in) unsigned long *INOUT = unsigned long *INPUT;
%typemap(python,in) unsigned char *INOUT = unsigned char *INPUT;
%typemap(python,in) float *INOUT = float *INPUT;
%typemap(python,in) double *INOUT = double *INPUT;
%typemap(in) int *INOUT = int *INPUT;
%typemap(in) short *INOUT = short *INPUT;
%typemap(in) long *INOUT = long *INPUT;
%typemap(in) long long *INOUT = long long *INPUT;
%typemap(in) unsigned *INOUT = unsigned *INPUT;
%typemap(in) unsigned short *INOUT = unsigned short *INPUT;
%typemap(in) unsigned long *INOUT = unsigned long *INPUT;
%typemap(in) unsigned long long *INOUT = unsigned long long *INPUT;
%typemap(in) unsigned char *INOUT = unsigned char *INPUT;
%typemap(in) bool *INOUT = bool *INPUT;
%typemap(in) float *INOUT = float *INPUT;
%typemap(in) double *INOUT = double *INPUT;
%typemap(python,argout) int *INOUT = int *OUTPUT;
%typemap(python,argout) short *INOUT = short *OUTPUT;
%typemap(python,argout) long *INOUT = long *OUTPUT;
%typemap(python,argout) unsigned *INOUT = unsigned *OUTPUT;
%typemap(python,argout) unsigned short *INOUT = unsigned short *OUTPUT;
%typemap(python,argout) unsigned long *INOUT = unsigned long *OUTPUT;
%typemap(python,argout) unsigned char *INOUT = unsigned char *OUTPUT;
%typemap(python,argout) float *INOUT = float *OUTPUT;
%typemap(python,argout) double *INOUT = double *OUTPUT;
%typemap(in) int &INOUT = int &INPUT;
%typemap(in) short &INOUT = short &INPUT;
%typemap(in) long &INOUT = long &INPUT;
%typemap(in) long long &INOUT = long long &INPUT;
%typemap(in) unsigned &INOUT = unsigned &INPUT;
%typemap(in) unsigned short &INOUT = unsigned short &INPUT;
%typemap(in) unsigned long &INOUT = unsigned long &INPUT;
%typemap(in) unsigned long long &INOUT = unsigned long long &INPUT;
%typemap(in) unsigned char &INOUT = unsigned char &INPUT;
%typemap(in) bool &INOUT = bool &INPUT;
%typemap(in) float &INOUT = float &INPUT;
%typemap(in) double &INOUT = double &INPUT;
%typemap(python,in) int *T_INOUT = int *INPUT;
%typemap(python,in) short *T_INOUT = short *INPUT;
%typemap(python,in) long *T_INOUT = long *INPUT;
%typemap(python,in) unsigned *T_INOUT = unsigned *INPUT;
%typemap(python,in) unsigned short *T_INOUT = unsigned short *INPUT;
%typemap(python,in) unsigned long *T_INOUT = unsigned long *INPUT;
%typemap(python,in) unsigned char *T_INOUT = unsigned char *INPUT;
%typemap(python,in) float *T_INOUT = float *INPUT;
%typemap(python,in) double *T_INOUT = double *INPUT;
%typemap(argout) int *INOUT = int *OUTPUT;
%typemap(argout) short *INOUT = short *OUTPUT;
%typemap(argout) long *INOUT = long *OUTPUT;
%typemap(argout) long long *INOUT = long long *OUTPUT;
%typemap(argout) unsigned *INOUT = unsigned *OUTPUT;
%typemap(argout) unsigned short *INOUT = unsigned short *OUTPUT;
%typemap(argout) unsigned long *INOUT = unsigned long *OUTPUT;
%typemap(argout) unsigned long long *INOUT = unsigned long long *OUTPUT;
%typemap(argout) unsigned char *INOUT = unsigned char *OUTPUT;
%typemap(argout) bool *INOUT = bool *OUTPUT;
%typemap(argout) float *INOUT = float *OUTPUT;
%typemap(argout) double *INOUT = double *OUTPUT;
%typemap(python,argout) int *T_INOUT = int *T_OUTPUT;
%typemap(python,argout) short *T_INOUT = short *T_OUTPUT;
%typemap(python,argout) long *T_INOUT = long *T_OUTPUT;
%typemap(python,argout) unsigned *T_INOUT = unsigned *T_OUTPUT;
%typemap(python,argout) unsigned short *T_INOUT = unsigned short *T_OUTPUT;
%typemap(python,argout) unsigned long *T_INOUT = unsigned long *T_OUTPUT;
%typemap(python,argout) unsigned char *T_INOUT = unsigned char *T_OUTPUT;
%typemap(python,argout) float *T_INOUT = float *T_OUTPUT;
%typemap(python,argout) double *T_INOUT = double *T_OUTPUT;
%typemap(argout) int &INOUT = int &OUTPUT;
%typemap(argout) short &INOUT = short &OUTPUT;
%typemap(argout) long &INOUT = long &OUTPUT;
%typemap(argout) long long &INOUT = long long &OUTPUT;
%typemap(argout) unsigned &INOUT = unsigned &OUTPUT;
%typemap(argout) unsigned short &INOUT = unsigned short &OUTPUT;
%typemap(argout) unsigned long &INOUT = unsigned long &OUTPUT;
%typemap(argout) unsigned long long &INOUT = unsigned long long &OUTPUT;
%typemap(argout) unsigned char &INOUT = unsigned char &OUTPUT;
%typemap(argout) bool &INOUT = bool &OUTPUT;
%typemap(argout) float &INOUT = float &OUTPUT;
%typemap(argout) double &INOUT = double &OUTPUT;
%typemap(python,in) int *L_INOUT = int *INPUT;
%typemap(python,in) short *L_INOUT = short *INPUT;
%typemap(python,in) long *L_INOUT = long *INPUT;
%typemap(python,in) unsigned *L_INOUT = unsigned *INPUT;
%typemap(python,in) unsigned short *L_INOUT = unsigned short *INPUT;
%typemap(python,in) unsigned long *L_INOUT = unsigned long *INPUT;
%typemap(python,in) unsigned char *L_INOUT = unsigned char *INPUT;
%typemap(python,in) float *L_INOUT = float *INPUT;
%typemap(python,in) double *L_INOUT = double *INPUT;
/* Overloading information */
%typemap(python,argout) int *L_INOUT = int *L_OUTPUT;
%typemap(python,argout) short *L_INOUT = short *L_OUTPUT;
%typemap(python,argout) long *L_INOUT = long *L_OUTPUT;
%typemap(python,argout) unsigned *L_INOUT = unsigned *L_OUTPUT;
%typemap(python,argout) unsigned short *L_INOUT = unsigned short *L_OUTPUT;
%typemap(python,argout) unsigned long *L_INOUT = unsigned long *L_OUTPUT;
%typemap(python,argout) unsigned char *L_INOUT = unsigned char *L_OUTPUT;
%typemap(python,argout) float *L_INOUT = float *L_OUTPUT;
%typemap(python,argout) double *L_INOUT = double *L_OUTPUT;
%typemap(typecheck) double *INOUT = double;
%typemap(typecheck) bool *INOUT = bool;
%typemap(typecheck) signed char *INOUT = signed char;
%typemap(typecheck) unsigned char *INOUT = unsigned char;
%typemap(typecheck) unsigned long *INOUT = unsigned long;
%typemap(typecheck) unsigned long long *INOUT = unsigned long long;
%typemap(typecheck) unsigned short *INOUT = unsigned short;
%typemap(typecheck) unsigned int *INOUT = unsigned int;
%typemap(typecheck) long *INOUT = long;
%typemap(typecheck) long long *INOUT = long long;
%typemap(typecheck) short *INOUT = short;
%typemap(typecheck) int *INOUT = int;
%typemap(typecheck) float *INOUT = float;
// Backwards compatibility
%typemap(python,in) int *BOTH = int *INOUT;
%typemap(python,in) short *BOTH = short *INOUT;
%typemap(python,in) long *BOTH = long *INOUT;
%typemap(python,in) unsigned *BOTH = unsigned *INOUT;
%typemap(python,in) unsigned short *BOTH = unsigned short *INOUT;
%typemap(python,in) unsigned long *BOTH = unsigned long *INOUT;
%typemap(python,in) unsigned char *BOTH = unsigned char *INOUT;
%typemap(python,in) float *BOTH = float *INOUT;
%typemap(python,in) double *BOTH = double *INOUT;
%typemap(python,argout) int *BOTH = int *INOUT;
%typemap(python,argout) short *BOTH = short *INOUT;
%typemap(python,argout) long *BOTH = long *INOUT;
%typemap(python,argout) unsigned *BOTH = unsigned *INOUT;
%typemap(python,argout) unsigned short *BOTH = unsigned short *INOUT;
%typemap(python,argout) unsigned long *BOTH = unsigned long *INOUT;
%typemap(python,argout) unsigned char *BOTH = unsigned char *INOUT;
%typemap(python,argout) float *BOTH = float *INOUT;
%typemap(python,argout) double *BOTH = double *INOUT;
%typemap(python,in) int *T_BOTH = int *T_INOUT;
%typemap(python,in) short *T_BOTH = short *T_INOUT;
%typemap(python,in) long *T_BOTH = long *T_INOUT;
%typemap(python,in) unsigned *T_BOTH = unsigned *T_INOUT;
%typemap(python,in) unsigned short *T_BOTH = unsigned short *T_INOUT;
%typemap(python,in) unsigned long *T_BOTH = unsigned long *T_INOUT;
%typemap(python,in) unsigned char *T_BOTH = unsigned char *T_INOUT;
%typemap(python,in) float *T_BOTH = float *T_INOUT;
%typemap(python,in) double *T_BOTH = double *T_INOUT;
%typemap(python,argout) int *T_BOTH = int *T_INOUT;
%typemap(python,argout) short *T_BOTH = short *T_INOUT;
%typemap(python,argout) long *T_BOTH = long *T_INOUT;
%typemap(python,argout) unsigned *T_BOTH = unsigned *T_INOUT;
%typemap(python,argout) unsigned short *T_BOTH = unsigned short *T_INOUT;
%typemap(python,argout) unsigned long *T_BOTH = unsigned long *T_INOUT;
%typemap(python,argout) unsigned char *T_BOTH = unsigned char *T_INOUT;
%typemap(python,argout) float *T_BOTH = float *T_INOUT;
%typemap(python,argout) double *T_BOTH = double *T_INOUT;
// --------------------------------------------------------------------
// Special types
//
// --------------------------------------------------------------------
#ifdef AUTODOC
%subsection "Special Methods"
%text %{
The typemaps.i library also provides the following mappings :
PyObject *
When a PyObject * appears as either an input value or return
value of a function, SWIG passes it through unmodified. Thus,
if you want to write a C function that operates on PyObjects,
it is easy to write. For example :
%include typemaps.i
PyObject *spam(PyObject *obj1, int n);
Unlike normal Python wrapper functions, These functions can
use any combination of parameters that you wish.
%}
#endif
%typemap(typecheck) double &INOUT = double;
%typemap(typecheck) bool &INOUT = bool;
%typemap(typecheck) signed char &INOUT = signed char;
%typemap(typecheck) unsigned char &INOUT = unsigned char;
%typemap(typecheck) unsigned long &INOUT = unsigned long;
%typemap(typecheck) unsigned long long &INOUT = unsigned long long;
%typemap(typecheck) unsigned short &INOUT = unsigned short;
%typemap(typecheck) unsigned int &INOUT = unsigned int;
%typemap(typecheck) long &INOUT = long;
%typemap(typecheck) long long &INOUT = long long;
%typemap(typecheck) short &INOUT = short;
%typemap(typecheck) int &INOUT = int;
%typemap(typecheck) float &INOUT = float;
// If a PyObject * appears as either an argument or a function return
// value, simply pass it straight through.
%typemap(python,in) PyObject * {
$target = $source;
}
%typemap(python,out) PyObject * {
$target = $source;
}

View file

@ -1,441 +0,0 @@
//
// SWIG Typemap library
// Dave Beazley
// May 5, 1997
//
// Python implementation
//
// This library provides standard typemaps for modifying SWIG's behavior.
// With enough entries in this file, I hope that very few people actually
// ever need to write a typemap.
//
#ifdef AUTODOC
%section "Typemap Library (Python)",info,after,pre,nosort,skip=1,chop_left=3,chop_right=0,chop_top=0,chop_bottom=0
%text %{
%include typemaps.i
The SWIG typemap library provides a language independent mechanism for
supporting output arguments, input values, and other C function
calling mechanisms. The primary use of the library is to provide a
better interface to certain C function--especially those involving
pointers.
%}
#endif
// ------------------------------------------------------------------------
// Pointer handling
//
// These mappings provide support for input/output arguments and common
// uses for C/C++ pointers.
// ------------------------------------------------------------------------
// INPUT typemaps.
// These remap a C pointer to be an "INPUT" value which is passed by value
// instead of reference.
#ifdef AUTODOC
%subsection "Input Methods"
%text %{
The following methods can be applied to turn a pointer into a simple
"input" value. That is, instead of passing a pointer to an object,
you would use a real value instead.
int *INPUT
short *INPUT
long *INPUT
unsigned int *INPUT
unsigned short *INPUT
unsigned long *INPUT
unsigned char *INPUT
float *INPUT
double *INPUT
To use these, suppose you had a C function like this :
double fadd(double *a, double *b) {
return *a+*b;
}
You could wrap it with SWIG as follows :
%include typemaps.i
double fadd(double *INPUT, double *INPUT);
or you can use the %apply directive :
%include typemaps.i
%apply double *INPUT { double *a, double *b };
double fadd(double *a, double *b);
%}
#endif
%typemap(python,in) double *INPUT(double temp)
{
temp = PyFloat_AsDouble($source);
$target = &temp;
}
%typemap(python,in) float *INPUT(float temp)
{
temp = (float) PyFloat_AsDouble($source);
$target = &temp;
}
%typemap(python,in) int *INPUT(int temp)
{
temp = (int) PyInt_AsLong($source);
$target = &temp;
}
%typemap(python,in) short *INPUT(short temp)
{
temp = (short) PyInt_AsLong($source);
$target = &temp;
}
%typemap(python,in) long *INPUT(long temp)
{
temp = (long) PyInt_AsLong($source);
$target = &temp;
}
%typemap(python,in) unsigned int *INPUT(unsigned int temp)
{
temp = (unsigned int) PyInt_AsLong($source);
$target = &temp;
}
%typemap(python,in) unsigned short *INPUT(unsigned short temp)
{
temp = (unsigned short) PyInt_AsLong($source);
$target = &temp;
}
%typemap(python,in) unsigned long *INPUT(unsigned long temp)
{
temp = (unsigned long) PyInt_AsLong($source);
$target = &temp;
}
%typemap(python,in) unsigned char *INPUT(unsigned char temp)
{
temp = (unsigned char) PyInt_AsLong($source);
$target = &temp;
}
// OUTPUT typemaps. These typemaps are used for parameters that
// are output only. The output value is appended to the result as
// a list element.
#ifdef AUTODOC
%subsection "Output Methods"
%text %{
The following methods can be applied to turn a pointer into an "output"
value. When calling a function, no input value would be given for
a parameter, but an output value would be returned. In the case of
multiple output values, they are returned in the form of a Python list.
int *OUTPUT
short *OUTPUT
long *OUTPUT
unsigned int *OUTPUT
unsigned short *OUTPUT
unsigned long *OUTPUT
unsigned char *OUTPUT
float *OUTPUT
double *OUTPUT
A Python Tuple can also be replaced by using T_OUTPUT instead of OUTPUT.
For example, suppose you were trying to wrap the modf() function in the
C math library which splits x into integral and fractional parts (and
returns the integer part in one of its parameters).K:
double modf(double x, double *ip);
You could wrap it with SWIG as follows :
%include typemaps.i
double modf(double x, double *OUTPUT);
or you can use the %apply directive :
%include typemaps.i
%apply double *OUTPUT { double *ip };
double modf(double x, double *ip);
The Python output of the function would be a list containing both
output values.
%}
#endif
// Force the argument to be ignored.
%typemap(python,ignore) int *OUTPUT(int temp),
short *OUTPUT(short temp),
long *OUTPUT(long temp),
unsigned int *OUTPUT(unsigned int temp),
unsigned short *OUTPUT(unsigned short temp),
unsigned long *OUTPUT(unsigned long temp),
unsigned char *OUTPUT(unsigned char temp),
float *OUTPUT(float temp),
double *OUTPUT(double temp)
{
$target = &temp;
}
%typemap(python,argout) int *OUTPUT,
short *OUTPUT,
long *OUTPUT,
unsigned int *OUTPUT,
unsigned short *OUTPUT,
unsigned long *OUTPUT,
unsigned char *OUTPUT
{
PyObject *o;
o = PyInt_FromLong((long) (*$source));
if (!$target) {
$target = o;
} else if ($target == Py_None) {
Py_DECREF(Py_None);
$target = o;
} else {
if (!PyList_Check($target)) {
PyObject *o2 = $target;
$target = PyList_New(0);
PyList_Append($target,o2);
Py_XDECREF(o2);
}
PyList_Append($target,o);
Py_XDECREF(o);
}
}
%typemap(python,argout) float *OUTPUT,
double *OUTPUT
{
PyObject *o;
o = PyFloat_FromDouble((double) (*$source));
if (!$target) {
$target = o;
} else if ($target == Py_None) {
Py_DECREF(Py_None);
$target = o;
} else {
if (!PyList_Check($target)) {
PyObject *o2 = $target;
$target = PyList_New(0);
PyList_Append($target,o2);
Py_XDECREF(o2);
}
PyList_Append($target,o);
Py_XDECREF(o);
}
}
// These typemaps contributed by Robin Dunn
//----------------------------------------------------------------------
//
// T_OUTPUT typemap (and helper function) to return multiple argouts as
// a tuple instead of a list.
//
// Author: Robin Dunn
//----------------------------------------------------------------------
%{
static PyObject* t_output_helper(PyObject* target, PyObject* o) {
PyObject* o2;
PyObject* o3;
if (!target) {
target = o;
} else if (target == Py_None) {
Py_DECREF(Py_None);
target = o;
} else {
if (!PyTuple_Check(target)) {
o2 = target;
target = PyTuple_New(1);
PyTuple_SetItem(target, 0, o2);
}
o3 = PyTuple_New(1);
PyTuple_SetItem(o3, 0, o);
o2 = target;
target = PySequence_Concat(o2, o3);
Py_DECREF(o2);
Py_DECREF(o3);
}
return target;
}
%}
// Force the argument to be ignored.
%typemap(python,ignore) int *T_OUTPUT(int temp),
short *T_OUTPUT(short temp),
long *T_OUTPUT(long temp),
unsigned int *T_OUTPUT(unsigned int temp),
unsigned short *T_OUTPUT(unsigned short temp),
unsigned long *T_OUTPUT(unsigned long temp),
unsigned char *T_OUTPUT(unsigned char temp),
float *T_OUTPUT(float temp),
double *T_OUTPUT(double temp)
{
$target = &temp;
}
%typemap(python,argout) int *T_OUTPUT,
short *T_OUTPUT,
long *T_OUTPUT,
unsigned int *T_OUTPUT,
unsigned short *T_OUTPUT,
unsigned long *T_OUTPUT,
unsigned char *T_OUTPUT
{
PyObject *o;
o = PyInt_FromLong((long) (*$source));
$target = t_output_helper($target, o);
}
%typemap(python,argout) float *T_OUTPUT,
double *T_OUTPUT
{
PyObject *o;
o = PyFloat_FromDouble((double) (*$source));
$target = t_output_helper($target, o);
}
// BOTH
// Mappings for an argument that is both an input and output
// parameter
#ifdef AUTODOC
%subsection "Input/Output Methods"
%text %{
The following methods can be applied to make a function parameter both
an input and output value. This combines the behavior of both the
"INPUT" and "OUTPUT" methods described earlier. Output values are
returned in the form of a Python list. To return a Python tuple,
using T_BOTH instead.
int *BOTH
short *BOTH
long *BOTH
unsigned int *BOTH
unsigned short *BOTH
unsigned long *BOTH
unsigned char *BOTH
float *BOTH
double *BOTH
For example, suppose you were trying to wrap the following function :
void neg(double *x) {
*x = -(*x);
}
You could wrap it with SWIG as follows :
%include typemaps.i
void neg(double *BOTH);
or you can use the %apply directive :
%include typemaps.i
%apply double *BOTH { double *x };
void neg(double *x);
Unlike C, this mapping does not directly modify the input value (since
this makes no sense in Python). Rather, the modified input value shows
up as the return value of the function. Thus, to apply this function
to a Python variable you might do this :
x = neg(x)
%}
#endif
%typemap(python,in) int *BOTH = int *INPUT;
%typemap(python,in) short *BOTH = short *INPUT;
%typemap(python,in) long *BOTH = long *INPUT;
%typemap(python,in) unsigned *BOTH = unsigned *INPUT;
%typemap(python,in) unsigned short *BOTH = unsigned short *INPUT;
%typemap(python,in) unsigned long *BOTH = unsigned long *INPUT;
%typemap(python,in) unsigned char *BOTH = unsigned char *INPUT;
%typemap(python,in) float *BOTH = float *INPUT;
%typemap(python,in) double *BOTH = double *INPUT;
%typemap(python,argout) int *BOTH = int *OUTPUT;
%typemap(python,argout) short *BOTH = short *OUTPUT;
%typemap(python,argout) long *BOTH = long *OUTPUT;
%typemap(python,argout) unsigned *BOTH = unsigned *OUTPUT;
%typemap(python,argout) unsigned short *BOTH = unsigned short *OUTPUT;
%typemap(python,argout) unsigned long *BOTH = unsigned long *OUTPUT;
%typemap(python,argout) unsigned char *BOTH = unsigned char *OUTPUT;
%typemap(python,argout) float *BOTH = float *OUTPUT;
%typemap(python,argout) double *BOTH = double *OUTPUT;
%typemap(python,in) int *T_BOTH = int *INPUT;
%typemap(python,in) short *T_BOTH = short *INPUT;
%typemap(python,in) long *T_BOTH = long *INPUT;
%typemap(python,in) unsigned *T_BOTH = unsigned *INPUT;
%typemap(python,in) unsigned short *T_BOTH = unsigned short *INPUT;
%typemap(python,in) unsigned long *T_BOTH = unsigned long *INPUT;
%typemap(python,in) unsigned char *T_BOTH = unsigned char *INPUT;
%typemap(python,in) float *T_BOTH = float *INPUT;
%typemap(python,in) double *T_BOTH = double *INPUT;
%typemap(python,argout) int *T_BOTH = int *T_OUTPUT;
%typemap(python,argout) short *T_BOTH = short *T_OUTPUT;
%typemap(python,argout) long *T_BOTH = long *T_OUTPUT;
%typemap(python,argout) unsigned *T_BOTH = unsigned *T_OUTPUT;
%typemap(python,argout) unsigned short *T_BOTH = unsigned short *T_OUTPUT;
%typemap(python,argout) unsigned long *T_BOTH = unsigned long *T_OUTPUT;
%typemap(python,argout) unsigned char *T_BOTH = unsigned char *T_OUTPUT;
%typemap(python,argout) float *T_BOTH = float *T_OUTPUT;
%typemap(python,argout) double *T_BOTH = double *T_OUTPUT;
// --------------------------------------------------------------------
// Special types
//
// --------------------------------------------------------------------
#ifdef AUTODOC
%subsection "Special Methods"
%text %{
The typemaps.i library also provides the following mappings :
PyObject *
When a PyObject * appears as either an input value or return
value of a function, SWIG passes it through unmodified. Thus,
if you want to write a C function that operates on PyObjects,
it is easy to write. For example :
%include typemaps.i
PyObject *spam(PyObject *obj1, int n);
Unlike normal Python wrapper functions, These functions can
use any combination of parameters that you wish.
%}
#endif
// If a PyObject * appears as either an argument or a function return
// value, simply pass it straight through.
%typemap(python,in) PyObject * {
$target = $source;
}
%typemap(python,out) PyObject * {
$target = $source;
}