]>
git.saurik.com Git - wxWidgets.git/blob - wxPython/wxSWIG/Modules/python.cxx
1 /*******************************************************************************
2 * Simplified Wrapper and Interface Generator (SWIG)
4 * Author : David Beazley
6 * Department of Computer Science
7 * University of Chicago
10 * beazley@cs.uchicago.edu
12 * Please read the file LICENSE for the copyright and terms by which SWIG
13 * can be used and distributed.
14 *******************************************************************************/
16 /**********************************************************************
22 **************************************************************************/
28 // Structures for managing doc strings
36 static int doc_index
= 0;
37 static DocString
*doc_strings
= 0;
39 static char *usage
= "\
40 Python Options (available with -python)\n\
41 -docstring - Produce docstrings (only applies to shadow classes)\n\
42 -globals name - Set name used to access C global variable ('cvar' by default).\n\
43 -module name - Set module name\n\
44 -keyword - Use keyword arguments\n\
45 -shadow - Generate shadow classes. \n\n";
47 static String pragma_include
;
49 // ---------------------------------------------------------------------
50 // PYTHON::parse_args(int argc, char *argv[])
52 // ---------------------------------------------------------------------
54 void PYTHON::parse_args(int argc
, char *argv
[]) {
58 sprintf(LibDir
,"%s",path
);
62 // Look for additional command line options.
63 for (i
= 1; i
< argc
; i
++) {
65 if(strcmp(argv
[i
],"-module") == 0) {
67 module = new char[strlen(argv
[i
+1])+2];
68 strcpy(module, argv
[i
+1]);
75 } else if (strcmp(argv
[i
],"-globals") == 0) {
77 global_name
= new char[strlen(argv
[i
+1])+1];
78 strcpy(global_name
, argv
[i
+1]);
85 } else if (strcmp(argv
[i
],"-shadow") == 0) {
88 } else if (strcmp(argv
[i
],"-docstring") == 0) {
91 } else if (strcmp(argv
[i
],"-keyword") == 0) {
94 } else if (strcmp(argv
[i
],"-help") == 0) {
99 // Create a symbol for this language
100 add_symbol("SWIGPYTHON",0,0);
102 // Set name of typemaps
104 typemap_lang
= "python";
108 // ---------------------------------------------------------------------
111 // Parse the interface file
112 // ---------------------------------------------------------------------
117 printf("Generating wrappers for Python\n");
120 // Run the SWIG parser
125 // ---------------------------------------------------------------------
126 // PYTHON::set_module(char *mod_name, char **mod_list)
128 // Sets the module name.
129 // Does nothing if it's already set (so it can be overridden as a command
132 //----------------------------------------------------------------------
134 void PYTHON::set_module(char *mod_name
, char **mod_list
) {
137 // If an "import" method has been set and we're in shadow class mode,
138 // output a python command to load the module
141 if (!(strcmp(import_file
,input_file
+strlen(input_file
)-strlen(import_file
)))) {
143 fprintf(f_shadow
,"\nfrom %s import *\n", mod_name
);
152 module = new char[strlen(mod_name
)+1];
153 strcpy(module,mod_name
);
155 // If there was a mod_list specified, make this incredible hack
157 modinit
<< "#define SWIGMODINIT ";
158 modextern
<< "#ifdef __cplusplus\n"
159 << "extern \"C\" {\n"
163 modinit
<< "swig_add_module(\"" << mod_list
[i
] << "\",init"
164 << mod_list
[i
] << "); \\\n";
166 modextern
<< "extern void init" << mod_list
[i
] << "();\n";
169 modextern
<< "#ifdef __cplusplus\n"
172 modinit
<< "/* End of extern module initialization */\n";
177 // ---------------------------------------------------------------------
178 // PYTHON::set_init(char *iname)
180 // Sets the initialization function name.
181 // Does nothing if it's already set
183 //----------------------------------------------------------------------
185 void PYTHON::set_init(char *iname
) {
190 // ---------------------------------------------------------------------
191 // PYTHON::import(char *filename)
193 // Imports a SWIG module as a separate file.
194 //----------------------------------------------------------------------
196 void PYTHON::import(char *filename
) {
197 if (import_file
) delete import_file
;
198 import_file
= copy_string(filename
);
201 // ----------------------------------------------------------------------
202 // PYTHON::add_method(char *name, char *function)
204 // Add some symbols to the methods table
205 // ----------------------------------------------------------------------
207 void PYTHON::add_method(char *name
, char *function
) {
212 n
->name
= new char[strlen(name
)+1];
213 strcpy(n
->name
,name
);
214 n
->function
= new char[strlen(function
)+1];
215 strcpy(n
->function
, function
);
221 // ---------------------------------------------------------------------
222 // PYTHON::print_methods()
224 // Prints out the method array.
225 // ---------------------------------------------------------------------
227 void PYTHON::print_methods() {
231 fprintf(f_wrappers
,"static PyMethodDef %sMethods[] = {\n", module);
235 fprintf(f_wrappers
,"\t { \"%s\", %s, METH_VARARGS },\n", n
->name
, n
->function
);
237 fprintf(f_wrappers
,"\t { \"%s\", (PyCFunction) %s, METH_VARARGS | METH_KEYWORDS },\n", n
->name
, n
->function
);
241 fprintf(f_wrappers
,"\t { NULL, NULL }\n");
242 fprintf(f_wrappers
,"};\n");
243 fprintf(f_wrappers
,"#ifdef __cplusplus\n");
244 fprintf(f_wrappers
,"}\n");
245 fprintf(f_wrappers
,"#endif\n");
248 // ---------------------------------------------------------------------
249 // char *PYTHON::add_docstring(DocEntry *de)
251 // Adds a documentation entry to the doc-string generator. Returns a
252 // unique character symbol that will be used to fill in the doc-string
254 // ---------------------------------------------------------------------
256 char *PYTHON::add_docstring(DocEntry
*de
) {
261 str
<< doc_index
<< "@";
265 s
->name
= copy_string(str
);
266 s
->next
= doc_strings
;
272 // ---------------------------------------------------------------------
273 // PYTHON::headers(void)
275 // ----------------------------------------------------------------------
277 void PYTHON::headers(void)
280 emit_banner(f_header
);
282 fprintf(f_header
,"/* Implementation : PYTHON */\n\n");
283 fprintf(f_header
,"#define SWIGPYTHON\n");
286 if (insert_file("python.swg", f_header
) == -1) {
287 fprintf(stderr
,"SWIG : Fatal error. Unable to locate python.swg. (Possible installation problem).\n");
291 if (insert_file("pyexp.swg", f_header
) == -1) {
292 fprintf(stderr
,"SWIG : Fatal error. Unable to locate pyexp.swg. (Possible installation problem).\n");
299 // --------------------------------------------------------------------
300 // PYTHON::initialize(void)
302 // This function outputs the starting code for a function to initialize
303 // your interface. It is only called once by the parser.
305 // ---------------------------------------------------------------------
307 void PYTHON::initialize(void)
316 fprintf(stderr
,"SWIG : *** Warning. No module name specified.\n");
319 // If shadow classing is enabled, we're going to change the module
323 temp
= new char[strlen(module)+2];
324 sprintf(temp
,"%sc",module);
328 /* Initialize the C code for the module */
329 initialize_cmodule();
330 /* Create a shadow file (if enabled).*/
332 sprintf(filen
,"%s%s.py", output_dir
, oldmodule
);
333 if ((f_shadow
= fopen(filen
,"w")) == 0) {
334 fprintf(stderr
,"Unable to open %s\n", filen
);
337 fprintf(f_shadow
,"# This file was created automatically by SWIG.\n");
338 fprintf(f_shadow
,"import %s\n", module);
341 // Dump out external module declarations
343 if (strlen(modinit
.get()) > 0) {
344 fprintf(f_header
,"%s\n",modinit
.get());
346 if (strlen(modextern
.get()) > 0) {
347 fprintf(f_header
,"%s\n",modextern
.get());
349 fprintf(f_wrappers
,"#ifdef __cplusplus\n");
350 fprintf(f_wrappers
,"extern \"C\" {\n");
351 fprintf(f_wrappers
,"#endif\n");
354 // ---------------------------------------------------------------------
355 // PYTHON::initialize_cmodule(void)
357 // Initializes the C module.
359 // ---------------------------------------------------------------------
360 void PYTHON::initialize_cmodule(void)
363 fprintf(f_header
,"#define SWIG_init init%s\n\n", module);
364 fprintf(f_header
,"#define SWIG_name \"%s\"\n", module);
366 // Output the start of the init function.
367 // Modify this to use the proper return type and arguments used
368 // by the target Language
370 fprintf(f_init
,"static PyObject *SWIG_globals;\n");
372 fprintf(f_init
,"#ifdef __cplusplus\n");
373 fprintf(f_init
,"extern \"C\" \n");
374 fprintf(f_init
,"#endif\n");
376 fprintf(f_init
,"SWIGEXPORT(void) init%s() {\n",module);
377 fprintf(f_init
,"\t PyObject *m, *d;\n");
381 while (InitNames
[i
]) {
382 fprintf(f_init
,"\t %s();\n", InitNames
[i
]);
386 fprintf(f_init
,"\t SWIG_globals = SWIG_newvarlink();\n");
387 fprintf(f_init
,"\t m = Py_InitModule(\"%s\", %sMethods);\n", module, module);
388 fprintf(f_init
,"\t d = PyModule_GetDict(m);\n");
392 // ---------------------------------------------------------------------
393 // PYTHON::close(void)
395 // Called when the end of the interface file is reached. Closes the
396 // initialization function and performs cleanup as necessary.
397 // ---------------------------------------------------------------------
399 void PYTHON::close(void)
404 if ((doc_entry
) && (module)){
406 temp
<< "Python Module : ";
408 module[strlen(module)-1] = 0;
411 doc_entry
->cinfo
<< temp
;
415 fullshadow
<< classes
416 << "\n\n#-------------- FUNCTION WRAPPERS ------------------\n\n"
418 << "\n\n#-------------- VARIABLE WRAPPERS ------------------\n\n"
421 if (strlen(pragma_include
) > 0) {
422 fullshadow
<< "\n\n#-------------- USER INCLUDE -----------------------\n\n"
426 // Go through all of the docstrings and replace the docstrings
431 fullshadow
.replace(s
->name
, s
->de
->text
);
435 fprintf(f_shadow,"\n\n#-------------- FUNCTION WRAPPERS ------------------\n\n");
436 fprintf(f_shadow,"%s",func.get());
437 fprintf(f_shadow,"\n\n#-------------- VARIABLE WRAPPERS ------------------\n\n");
438 fprintf(f_shadow,"%s",vars.get());
439 if (strlen(pragma_include) > 0) {
440 fprintf(f_shadow,"\n\n#-------------- USER INCLUDE -----------------------\n\n");
441 fprintf(f_shadow,"%s",pragma_include.get());
444 fprintf(f_shadow
, "%s", fullshadow
.get());
449 // --------------------------------------------------------------------
450 // PYTHON::close_cmodule(void)
452 // Called to cleanup the C module code
453 // --------------------------------------------------------------------
454 void PYTHON::close_cmodule(void)
456 emit_ptr_equivalence(f_init
);
457 fprintf(f_init
,"}\n");
460 // ----------------------------------------------------------------------
461 // PYTHON::get_pointer(char *iname, char *srcname, char *src, char *target,
462 // DataType *t, WrapperFunction &f, char *ret)
464 // Emits code to get a pointer and do type checking.
465 // iname = name of the function/method (used for error messages)
466 // srcname = Name of source (used for error message reporting).
467 // src = name of variable where source string is located.
468 // dest = name of variable where pointer value is stored.
469 // t = Expected datatype of the parameter
470 // f = Wrapper function object being used to generate code.
471 // ret = return code upon failure.
473 // Note : pointers are stored as strings so you first need to get
474 // a string and then call _swig_get_hex() to extract a point.
476 // This module is pretty ugly, but type checking is kind of nasty
478 // ----------------------------------------------------------------------
481 PYTHON::get_pointer(char *iname
, char *srcname
, char *src
, char *dest
,
482 DataType
*t
, String
&f
, char *ret
)
485 // Now get the pointer value from the string and save in dest
487 f
<< tab4
<< "if (" << src
<< ") {\n"
488 << tab8
<< "if (" << src
<< " == Py_None) { " << dest
<< " = NULL; }\n"
489 << tab8
<< "else if (SWIG_GetPtrObj(" << src
<< ",(void **) &" << dest
<< ",";
491 // If we're passing a void pointer, we give the pointer conversion a NULL
492 // pointer, otherwise pass in the expected type.
494 if (t
->type
== T_VOID
) f
<< "(char *) 0 )) {\n";
496 f
<< "\"" << t
->print_mangle() << "\")) {\n";
498 // This part handles the type checking according to three different
499 // levels. 0 = no checking, 1 = warning message, 2 = strict.
502 case 0: // No type checking
506 case 1: // Warning message only
508 // Change this part to how you want to handle a type-mismatch warning.
509 // By default, it will just print to stderr.
511 f
<< tab8
<< tab4
<< "fprintf(stderr,\"Warning : type mismatch in " << srcname
512 << " of " << iname
<< ". Expected " << t
->print_mangle()
513 << ", received %s\\n\"," << src
<< ");\n"
517 case 2: // Super strict mode.
519 // Change this part to return an error.
521 f
<< tab8
<< tab4
<< "PyErr_SetString(PyExc_TypeError,\"Type error in " << srcname
522 << " of " << iname
<< ". Expected " << t
->print_mangle() << ".\");\n"
523 << tab8
<< "return " << ret
<< ";\n"
528 fprintf(stderr
,"SWIG Error. Unknown strictness level\n");
534 // ----------------------------------------------------------------------
535 // PYTHON::emit_function_header()
537 // Return the code to be used as a function header
538 // ----------------------------------------------------------------------
539 void PYTHON::emit_function_header(WrapperFunction
&emit_to
, char *wname
)
542 emit_to
.def
<< "static PyObject *" << wname
543 << "(PyObject *self, PyObject *args) {";
545 emit_to
.def
<< "static PyObject *" << wname
546 << "(PyObject *self, PyObject *args, PyObject *kwargs) {";
548 emit_to
.code
<< tab4
<< "self = self;\n";
551 // ----------------------------------------------------------------------
552 // PYTHON::convert_self()
554 // Called during the function generation process, to determine what to
555 // use as the "self" variable during the call. Derived classes may emit code
556 // to convert the real self pointer into a usable pointer.
558 // Returns the name of the variable to use as the self pointer
559 // ----------------------------------------------------------------------
560 char *PYTHON::convert_self(WrapperFunction
&)
562 // Default behaviour is no translation
566 // ----------------------------------------------------------------------
567 // PYTHON::make_funcname_wrapper()
569 // Called to create a name for a wrapper function
570 // ----------------------------------------------------------------------
571 char *PYTHON::make_funcname_wrapper(char *fnName
)
573 return name_wrapper(fnName
,"");
576 // ----------------------------------------------------------------------
577 // PYTHON::create_command(char *cname, char *iname)
579 // Create a new command in the interpreter. Used for C++ inheritance
581 // ----------------------------------------------------------------------
583 void PYTHON::create_command(char *cname
, char *iname
) {
585 // Create the name of the wrapper function
587 char *wname
= name_wrapper(cname
,"");
589 // Now register the function with the interpreter.
591 add_method(iname
, wname
);
595 // ----------------------------------------------------------------------
596 // PYTHON::create_function(char *name, char *iname, DataType *d,
599 // This function creates a wrapper function and registers it with the
603 // name = actual name of the function that's being wrapped
604 // iname = name of the function in the interpreter (may be different)
605 // d = Return datatype of the functions.
606 // l = A linked list containing function parameter information.
608 // ----------------------------------------------------------------------
610 void PYTHON::create_function(char *name
, char *iname
, DataType
*d
, ParmList
*l
)
614 String wname
, self_name
, call_name
;
615 char source
[64], target
[64], temp
[256], argnum
[20];
621 String cleanup
, outarg
;
632 // Make a valid name for this function. This removes special symbols
633 // that would cause problems in the C compiler.
635 wname
= make_funcname_wrapper(iname
);
637 // Now emit the function declaration for the wrapper function. You
638 // should modify this to return the appropriate types and use the
639 // appropriate parameters.
641 emit_function_header(f
, wname
);
643 f
.add_local("PyObject *","_resultobj");
645 // Get the function usage string for later use
647 usage
= usage_func(iname
,d
,l
);
649 // Write code to extract function parameters.
650 // This is done in one pass, but we need to construct three independent
652 // 1. Python format string such as "iis"
653 // 2. The actual arguments to put values into
654 // 3. Pointer conversion code.
656 // If there is a type mapping, we will extract the Python argument
657 // as a raw PyObject and let the user deal with it.
660 pcount
= emit_args(d
, l
, f
);
662 parse_args
<< tab4
<< "if(!PyArg_ParseTuple(args,\"";
664 parse_args
<< tab4
<< "if(!PyArg_ParseTupleAndKeywords(args,kwargs,\"";
665 arglist
<< ",_kwnames";
670 numopt
= l
->numopt(); // Get number of optional arguments
671 if (numopt
) have_defarg
= 1;
677 // Generate source and target strings
678 sprintf(source
,"_obj%d",i
);
679 sprintf(target
,"_arg%d",i
);
680 sprintf(argnum
,"%d",j
+1);
682 // Only consider this argument if it's not ignored
686 // Add an optional argument separator if needed
688 if (j
== pcount
-numopt
) {
692 if (strlen(p
->name
)) {
693 kwargs
<< "\"" << p
->name
<< "\",";
695 kwargs
<< "\"arg" << j
+1 << "\",";
696 // kwargs << "\"\",";
699 // Look for input typemap
701 if ((tm
= typemap_lookup("in","python",p
->t
,p
->name
,source
,target
,&f
))) {
702 parse_args
<< "O"; // Grab the argument as a raw PyObject
703 f
.add_local("PyObject *",source
,"0");
704 arglist
<< "&" << source
;
705 if (i
>= (pcount
-numopt
))
706 get_pointers
<< tab4
<< "if (" << source
<< ")\n";
707 get_pointers
<< tm
<< "\n";
708 get_pointers
.replace("$argnum", argnum
);
709 get_pointers
.replace("$arg",source
);
712 // Check if this parameter is a pointer. If not, we'll get values
714 if (!p
->t
->is_pointer
) {
715 // Extract a parameter by "value"
719 // Handle integers here. Usually this can be done as a single
720 // case if you appropriate cast things. However, if you have
721 // special cases, you'll need to add more code.
723 case T_INT
: case T_UINT
: case T_SINT
:
726 case T_SHORT
: case T_USHORT
: case T_SSHORT
:
729 case T_LONG
: case T_ULONG
: case T_SLONG
:
732 case T_SCHAR
: case T_UCHAR
:
750 tempval
<< "(int) " << p
->defvalue
;
752 tempb
<< "tempbool" << i
;
755 f
.add_local("int",tempb
.get());
757 f
.add_local("int",tempb
.get(),tempval
.get());
758 get_pointers
<< tab4
<< target
<< " = " << p
->t
->print_cast() << " " << tempb
<< ";\n";
759 arglist
<< "&" << tempb
;
763 // Void.. Do nothing.
768 // User defined. This is usually invalid. No way to pass a
769 // complex type by "value". We'll just pass into the unsupported
774 // Unsupported data type
777 fprintf(stderr
,"%s : Line %d. Unable to use type %s as a function argument.\n",input_file
, line_number
, p
->t
->print_type());
781 // Emit code for parameter list
783 if ((p
->t
->type
!= T_VOID
) && (p
->t
->type
!= T_BOOL
))
784 arglist
<< "&_arg" << i
;
788 // Is some other kind of variable.
790 if ((p
->t
->type
== T_CHAR
) && (p
->t
->is_pointer
== 1)) {
792 arglist
<< "&_arg" << i
;
795 // Have some sort of pointer variable. Create a temporary local
796 // variable for the string and read the pointer value into it.
799 sprintf(source
,"_argo%d", i
);
800 sprintf(target
,"_arg%d", i
);
801 sprintf(temp
,"argument %d",i
+1);
803 f
.add_local("PyObject *", source
,"0");
804 arglist
<< "&" << source
;
805 get_pointer(iname
, temp
, source
, target
, p
->t
, get_pointers
, "NULL");
811 // Check if there was any constraint code
812 if ((tm
= typemap_lookup("check","python",p
->t
,p
->name
,source
,target
))) {
814 check
.replace("$argnum", argnum
);
816 // Check if there was any cleanup code
817 if ((tm
= typemap_lookup("freearg","python",p
->t
,p
->name
,target
,source
))) {
818 cleanup
<< tm
<< "\n";
819 cleanup
.replace("$argnum", argnum
);
820 cleanup
.replace("$arg",source
);
822 if ((tm
= typemap_lookup("argout","python",p
->t
,p
->name
,target
,"_resultobj"))) {
823 outarg
<< tm
<< "\n";
824 outarg
.replace("$argnum", argnum
);
825 outarg
.replace("$arg",source
);
828 if ((tm
= typemap_lookup("build","python",p
->t
,p
->name
,source
,target
))) {
838 f
.locals
<< tab4
<< "char *_kwnames[] = " << kwargs
<< ";\n";
841 parse_args
<< ":" << iname
<< "\""; // No additional arguments
842 parse_args
<< arglist
<< ")) \n"
843 << tab8
<< "return NULL;\n";
845 self_name
= convert_self(f
);
847 /* Now slap the whole first part of the wrapper function together */
849 f
.code
<< parse_args
<< get_pointers
<< check
;
852 // Special handling for build values
857 l
->sub_parmnames(build
); // Replace all parameter names
858 for (i
= 0; i
< l
->nparms
; i
++) {
860 if (strlen(p
->name
) > 0) {
861 sprintf(temp1
,"_in_%s", p
->name
);
863 sprintf(temp1
,"_in_arg%d", i
);
865 sprintf(temp2
,"_obj%d",i
);
866 build
.replaceid(temp1
,temp2
);
871 // This function emits code to call the real function. Assuming you read
872 // the parameters in correctly, this will work.
875 call_name
<< self_name
<< name
;
876 emit_func_call(call_name
,d
,l
,f
);
878 // Now emit code to return the functions return value (if any).
879 // If there was a result, it was saved in _result.
880 // If the function is a void type, don't do anything.
882 if ((strncmp(name
, "new_", 4) != 0) && // don't use the out typemap for constructors
883 (tm
= typemap_lookup("out","python",d
,iname
,"_result","_resultobj"))) {
884 // Yep. Use it instead of the default
885 f
.code
<< tm
<< "\n";
888 if ((d
->type
!= T_VOID
) || (d
->is_pointer
)) {
889 // Now have return value, figure out what to do with it.
891 if (!d
->is_pointer
) {
893 // Function returns a "value"
897 // Return an integer type
899 case T_INT
: case T_SINT
: case T_UINT
: case T_BOOL
:
900 f
.code
<< tab4
<< "_resultobj = Py_BuildValue(\"i\",_result);\n";
902 case T_SHORT
: case T_SSHORT
: case T_USHORT
:
903 f
.code
<< tab4
<< "_resultobj = Py_BuildValue(\"h\",_result);\n";
905 case T_LONG
: case T_SLONG
: case T_ULONG
:
906 f
.code
<< tab4
<< "_resultobj = Py_BuildValue(\"l\",_result);\n";
908 case T_SCHAR
: case T_UCHAR
:
909 f
.code
<< tab4
<< "_resultobj = Py_BuildValue(\"b\",_result);\n";
912 // Return a floating point value
915 f
.code
<< tab4
<< "_resultobj = Py_BuildValue(\"d\",_result);\n";
918 f
.code
<< tab4
<< "_resultobj = Py_BuildValue(\"f\",_result);\n";
921 // Return a single ASCII value. Usually we need to convert
922 // it to a NULL-terminate string and return that instead.
925 f
.code
<< tab4
<< "_resultobj = Py_BuildValue(\"c\",_result);\n";
930 // Return something by value
931 // We're living dangerously here, but life is short...play hard
933 // Oops. Need another local variable
934 f
.add_local("char","_ptemp[128]");
937 f
.code
<< tab4
<< "SWIG_MakePtr(_ptemp, (void *) _result,\""
938 << d
->print_mangle() << "\");\n";
940 // Return a character string containing our pointer.
942 f
.code
<< tab4
<< "_resultobj = Py_BuildValue(\"s\",_ptemp);\n";
945 fprintf(stderr
,"%s: Line %d. Unable to use return type %s in function %s.\n", input_file
, line_number
, d
->print_type(), name
);
950 // Return type is a pointer. We'll see if it's a char * and return
951 // a string. Otherwise, we'll convert it into a SWIG pointer and return
954 if ((d
->type
== T_CHAR
) && (d
->is_pointer
== 1)) {
956 // Return a character string
957 f
.code
<< tab4
<< "_resultobj = Py_BuildValue(\"s\", _result);\n";
959 // If declared as a new object, free the result
963 // Build a SWIG pointer.
964 f
.add_local("char","_ptemp[128]");
965 f
.code
<< tab4
<< "if (_result) {\n"
966 << tab8
<< "SWIG_MakePtr(_ptemp, (char *) _result,\""
967 << d
->print_mangle() << "\");\n";
969 // Return a character string containing our pointer.
970 f
.code
<< tab8
<< "_resultobj = Py_BuildValue(\"s\",_ptemp);\n";
971 f
.code
<< tab4
<< "} else {\n"
972 << tab8
<< "Py_INCREF(Py_None);\n"
973 << tab8
<< "_resultobj = Py_None;\n"
978 // no return value and no output args
979 //if (!have_output) {
980 f
.code
<< tab4
<< "Py_INCREF(Py_None);\n";
981 f
.code
<< tab4
<< "_resultobj = Py_None;\n";
986 // Check to see if there were any output arguments, if so we're going to
987 // create a Python list object out of the current result
991 // If there was any other cleanup needed, do that
995 // Look to see if there is any newfree cleanup code
998 if ((tm
= typemap_lookup("newfree","python",d
,iname
,"_result",""))) {
999 f
.code
<< tm
<< "\n";
1003 // See if there is any argument cleanup code
1005 if ((tm
= typemap_lookup("ret","python",d
,iname
,"_result",""))) {
1006 // Yep. Use it instead of the default
1007 f
.code
<< tm
<< "\n";
1010 f
.code
<< tab4
<< "return _resultobj;\n";
1013 // Substitute the cleanup code
1014 f
.code
.replace("$cleanup",cleanup
);
1016 // Substitute the function name
1017 f
.code
.replace("$name",iname
);
1019 // Dump the function out
1020 f
.print(f_wrappers
);
1022 // Now register the function with the interpreter.
1024 add_method(iname
, wname
);
1026 // Create a documentation entry for this
1029 static DocEntry
*last_doc_entry
= 0;
1030 doc_entry
->usage
<< usage
;
1031 if (last_doc_entry
!= doc_entry
) {
1032 doc_entry
->cinfo
<< "returns " << d
->print_type();
1033 last_doc_entry
= doc_entry
;
1037 // ---------------------------------------------------------------------------
1038 // Create a shadow for this function (if enabled and not in a member function)
1039 // ---------------------------------------------------------------------------
1041 if ((shadow
) && (!(shadow
& PYSHADOW_MEMBER
))) {
1044 int need_wrapper
= 0;
1045 int munge_return
= 0;
1046 int have_optional
= 0;
1048 // Check return code for modification
1049 if ((hash
.lookup(d
->name
)) && (d
->is_pointer
<=1)) {
1054 if (docstring
&& doc_entry
)
1057 // If no modification is needed. We're just going to play some
1058 // symbol table games instead
1060 if (!need_wrapper
) {
1061 func
<< iname
<< " = " << module << "." << iname
<< "\n\n";
1063 func
<< "def " << iname
<< "(*_args, **_kwargs):\n";
1065 // Create a docstring for this
1066 if (docstring
&& doc_entry
) {
1067 func
<< tab4
<< "\"\"\"" << add_docstring(doc_entry
) << "\"\"\"\n";
1070 func
<< tab4
<< "val = apply(" << module << "." << iname
<< ",_args,_kwargs)\n";
1073 // If the output of this object has been remapped in any way, we're
1074 // going to return it as a bare object.
1076 if (!typemap_check("out",typemap_lang
,d
,iname
)) {
1078 // If there are output arguments, we are going to return the value
1079 // unchanged. Otherwise, emit some shadow class conversion code.
1082 func
<< tab4
<< "if val: val = " << (char *) hash
.lookup(d
->name
) << "Ptr(val)";
1083 if (((hash
.lookup(d
->name
)) && (d
->is_pointer
< 1)) ||
1084 ((hash
.lookup(d
->name
)) && (d
->is_pointer
== 1) && NewObject
))
1085 func
<< "; val.thisown = 1\n";
1089 // Does nothing--returns the value unmolested
1093 func
<< tab4
<< "return val\n\n";
1098 // -----------------------------------------------------------------------
1099 // PYTHON::link_variable(char *name, char *iname, DataType *d)
1102 // name = the real name of the variable being linked
1103 // iname = Name of the variable in the interpreter (may be different)
1104 // d = Datatype of the variable.
1106 // This creates a pair of functions for evaluating/setting the value
1107 // of a variable. These are then added to the special SWIG global
1109 // -----------------------------------------------------------------------
1111 void PYTHON::link_variable(char *name
, char *iname
, DataType
*t
) {
1114 static int have_globals
= 0;
1117 WrapperFunction getf
, setf
;
1119 // If this is our first call, add the globals variable to the
1120 // Python dictionary.
1122 if (!have_globals
) {
1123 fprintf(f_init
,"\t PyDict_SetItemString(d,\"%s\", SWIG_globals);\n",global_name
);
1125 if ((shadow
) && (!(shadow
& PYSHADOW_MEMBER
))) {
1126 vars
<< global_name
<< " = " << module << "." << global_name
<< "\n";
1129 // First make a sanitized version of the function name (in case it's some
1130 // funky C++ thing).
1132 wname
= name_wrapper(name
,"");
1134 // ---------------------------------------------------------------------
1135 // Create a function for setting the value of the variable
1136 // ---------------------------------------------------------------------
1138 setf
.def
<< "static int " << wname
<< "_set(PyObject *val) {";
1139 if (!(Status
& STAT_READONLY
)) {
1140 if ((tm
= typemap_lookup("varin","python",t
,name
,"val",name
))) {
1141 setf
.code
<< tm
<< "\n";
1142 setf
.code
.replace("$name",iname
);
1144 if ((t
->type
!= T_VOID
) || (t
->is_pointer
)) {
1145 if (!t
->is_pointer
) {
1147 // Have a real value here
1150 case T_INT
: case T_SHORT
: case T_LONG
:
1151 case T_UINT
: case T_USHORT
: case T_ULONG
:
1152 case T_SINT
: case T_SSHORT
: case T_SLONG
:
1153 case T_SCHAR
: case T_UCHAR
: case T_BOOL
:
1154 // Get an integer value
1155 setf
.add_local(t
->print_type(), "tval");
1156 setf
.code
<< tab4
<< "tval = " << t
->print_cast() << "PyInt_AsLong(val);\n"
1157 << tab4
<< "if (PyErr_Occurred()) {\n"
1158 << tab8
<< "PyErr_SetString(PyExc_TypeError,\"C variable '"
1159 << iname
<< "'(" << t
->print_type() << ")\");\n"
1160 << tab8
<< "return 1; \n"
1162 << tab4
<< name
<< " = tval;\n";
1165 case T_FLOAT
: case T_DOUBLE
:
1166 // Get a floating point value
1167 setf
.add_local(t
->print_type(), "tval");
1168 setf
.code
<< tab4
<< "tval = " << t
->print_cast() << "PyFloat_AsDouble(val);\n"
1169 << tab4
<< "if (PyErr_Occurred()) {\n"
1170 << tab8
<< "PyErr_SetString(PyExc_TypeError,\"C variable '"
1171 << iname
<< "'(" << t
->print_type() << ")\");\n"
1172 << tab8
<< "return 1; \n"
1174 << tab4
<< name
<< " = tval;\n";
1177 // A single ascii character
1180 setf
.add_local("char *", "tval");
1181 setf
.code
<< tab4
<< "tval = (char *) PyString_AsString(val);\n"
1182 << tab4
<< "if (PyErr_Occurred()) {\n"
1183 << tab8
<< "PyErr_SetString(PyExc_TypeError,\"C variable '"
1184 << iname
<< "'(" << t
->print_type() << ")\");\n"
1185 << tab8
<< "return 1; \n"
1187 << tab4
<< name
<< " = *tval;\n";
1191 setf
.add_local(t
->print_type(),"temp");
1192 get_pointer(iname
,"value","val","temp",t
,setf
.code
,"1");
1193 setf
.code
<< tab4
<< name
<< " = *temp;\n";
1197 fprintf(stderr
,"%s : Line %d. Unable to link with type %s.\n", input_file
, line_number
, t
->print_type());
1201 // Parse a pointer value
1203 if ((t
->type
== T_CHAR
) && (t
->is_pointer
== 1)) {
1204 setf
.add_local("char *", "tval");
1205 setf
.code
<< tab4
<< "tval = (char *) PyString_AsString(val);\n"
1206 << tab4
<< "if (PyErr_Occurred()) {\n"
1207 << tab8
<< "PyErr_SetString(PyExc_TypeError,\"C variable '"
1208 << iname
<< "'(" << t
->print_type() << ")\");\n"
1209 << tab8
<< "return 1; \n"
1213 setf
.code
<< tab4
<< "if (" << name
<< ") delete [] " << name
<< ";\n"
1214 << tab4
<< name
<< " = new char[strlen(tval)+1];\n"
1215 << tab4
<< "strcpy((char *)" << name
<< ",tval);\n";
1217 setf
.code
<< tab4
<< "if (" << name
<< ") free(" << name
<< ");\n"
1218 << tab4
<< name
<< " = (char *) malloc(strlen(tval)+1);\n"
1219 << tab4
<< "strcpy((char *)" << name
<< ",tval);\n";
1223 // Is a generic pointer value.
1225 setf
.add_local(t
->print_type(),"temp");
1226 get_pointer(iname
,"value","val","temp",t
,setf
.code
,"1");
1227 setf
.code
<< tab4
<< name
<< " = temp;\n";
1232 setf
.code
<< tab4
<< "return 0;\n";
1234 // Is a readonly variable. Issue an error
1235 setf
.code
<< tab4
<< "PyErr_SetString(PyExc_TypeError,\"Variable " << iname
1236 << " is read-only.\");\n"
1237 << tab4
<< "return 1;\n";
1242 // Dump out function for setting value
1244 setf
.print(f_wrappers
);
1246 // ----------------------------------------------------------------
1247 // Create a function for getting the value of a variable
1248 // ----------------------------------------------------------------
1250 getf
.def
<< "static PyObject *" << wname
<< "_get() {";
1251 getf
.add_local("PyObject *","pyobj");
1252 if ((tm
= typemap_lookup("varout","python",t
,name
,name
,"pyobj"))) {
1253 getf
.code
<< tm
<< "\n";
1254 getf
.code
.replace("$name",iname
);
1255 } else if ((tm
= typemap_lookup("out","python",t
,name
,name
,"pyobj"))) {
1256 getf
.code
<< tm
<< "\n";
1257 getf
.code
.replace("$name",iname
);
1259 if ((t
->type
!= T_VOID
) || (t
->is_pointer
)) {
1260 if (!t
->is_pointer
) {
1262 /* Is a normal datatype */
1264 case T_INT
: case T_SINT
: case T_UINT
:
1265 case T_SHORT
: case T_SSHORT
: case T_USHORT
:
1266 case T_LONG
: case T_SLONG
: case T_ULONG
:
1267 case T_SCHAR
: case T_UCHAR
: case T_BOOL
:
1268 getf
.code
<< tab4
<< "pyobj = PyInt_FromLong((long) " << name
<< ");\n";
1270 case T_FLOAT
: case T_DOUBLE
:
1271 getf
.code
<< tab4
<< "pyobj = PyFloat_FromDouble((double) " << name
<< ");\n";
1274 getf
.add_local("char","ptemp[2]");
1275 getf
.code
<< tab4
<< "ptemp[0] = " << name
<< ";\n"
1276 << tab4
<< "ptemp[1] = 0;\n"
1277 << tab4
<< "pyobj = PyString_FromString(ptemp);\n";
1280 // Hack this into a pointer
1281 getf
.add_local("char", "ptemp[128]");
1283 getf
.code
<< tab4
<< "SWIG_MakePtr(ptemp,(char *) &" << name
1284 << "," << quote
<< t
->print_mangle() << quote
<< ");\n"
1285 << tab4
<< "pyobj = PyString_FromString(ptemp);\n";
1289 fprintf(stderr
,"Unable to link with type %s\n", t
->print_type());
1294 // Is some sort of pointer value
1295 if ((t
->type
== T_CHAR
) && (t
->is_pointer
== 1)) {
1296 getf
.code
<< tab4
<< "if (" << name
<< ")\n"
1297 << tab8
<< "pyobj = PyString_FromString(" << name
<< ");\n"
1298 << tab4
<< "else pyobj = PyString_FromString(\"(NULL)\");\n";
1300 getf
.add_local("char","ptemp[128]");
1301 getf
.code
<< tab4
<< "SWIG_MakePtr(ptemp, (char *) " << name
<< ",\""
1302 << t
->print_mangle() << "\");\n"
1303 << tab4
<< "pyobj = PyString_FromString(ptemp);\n";
1309 getf
.code
<< tab4
<< "return pyobj;\n"
1312 getf
.print(f_wrappers
);
1314 // Now add this to the variable linking mechanism
1316 fprintf(f_init
,"\t SWIG_addvarlink(SWIG_globals,\"%s\",%s_get, %s_set);\n", iname
, wname
, wname
);
1319 // Fill in the documentation entry
1322 doc_entry
->usage
<< usage_var(iname
, t
);
1323 doc_entry
->cinfo
<< "Global : " << t
->print_type() << " " << name
;
1326 // ----------------------------------------------------------
1327 // Output a shadow variable. (If applicable and possible)
1328 // ----------------------------------------------------------
1329 if ((shadow
) && (!(shadow
& PYSHADOW_MEMBER
))) {
1330 if ((hash
.lookup(t
->name
)) && (t
->is_pointer
<= 1)) {
1331 vars
<< iname
<< " = " << (char *) hash
.lookup(t
->name
) << "Ptr(" << module << "." << global_name
1332 << "." << iname
<< ")\n";
1337 // -----------------------------------------------------------------------
1338 // PYTHON::declare_const(char *name, char *iname, DataType *type, char *value)
1340 // Makes a constant as defined with #define. Constants are added to the
1341 // module's dictionary and are **NOT** guaranteed to be read-only,
1344 // ------------------------------------------------------------------------
1346 void PYTHON::declare_const(char *name
, char *, DataType
*type
, char *value
) {
1350 // Make a static python object
1352 if ((tm
= typemap_lookup("const","python",type
,name
,value
,name
))) {
1353 fprintf(f_init
,"%s\n",tm
);
1356 if ((type
->type
== T_USER
) && (!type
->is_pointer
)) {
1357 fprintf(stderr
,"%s : Line %d. Unsupported constant value.\n", input_file
, line_number
);
1361 if (type
->is_pointer
== 0) {
1362 switch(type
->type
) {
1363 case T_INT
:case T_SINT
: case T_UINT
: case T_BOOL
:
1364 case T_SHORT
: case T_SSHORT
: case T_USHORT
:
1365 case T_LONG
: case T_SLONG
: case T_ULONG
:
1366 case T_SCHAR
: case T_UCHAR
:
1367 fprintf(f_init
,"\t PyDict_SetItemString(d,\"%s\", PyInt_FromLong((long) %s));\n",name
,value
);
1371 fprintf(f_init
,"\t PyDict_SetItemString(d,\"%s\", PyFloat_FromDouble((double) %s));\n",name
,value
);
1374 fprintf(f_init
,"\t PyDict_SetItemString(d,\"%s\", PyString_FromString(\"%s\"));\n",name
,value
);
1377 fprintf(stderr
,"%s : Line %d. Unsupported constant value.\n", input_file
, line_number
);
1381 if ((type
->type
== T_CHAR
) && (type
->is_pointer
== 1)) {
1382 fprintf(f_init
,"\t PyDict_SetItemString(d,\"%s\", PyString_FromString(\"%s\"));\n",name
,value
);
1384 // A funky user-defined type. We're going to munge it into a string pointer value
1385 fprintf(f_init
,"\t {\n");
1386 fprintf(f_init
,"\t\t char %s_char[%d];\n", name
, (int) strlen(type
->print_mangle()) + 20);
1387 fprintf(f_init
,"\t\t SWIG_MakePtr(%s_char, (void *) (%s),\"%s\");\n",
1388 name
, value
, type
->print_mangle());
1389 fprintf(f_init
,"\t\t PyDict_SetItemString(d,\"%s\", PyString_FromString(%s_char));\n",name
,name
);
1390 fprintf(f_init
,"\t }\n");
1394 if ((shadow
) && (!(shadow
& PYSHADOW_MEMBER
))) {
1395 vars
<< name
<< " = " << module << "." << name
<< "\n";
1398 doc_entry
->usage
= "";
1399 doc_entry
->usage
<< usage_const(name
,type
,value
);
1400 doc_entry
->cinfo
= "";
1401 doc_entry
->cinfo
<< "Constant: " << type
->print_type();
1405 // ----------------------------------------------------------------------
1406 // PYTHON::usage_var(char *iname, DataType *t)
1408 // This function produces a string indicating how to use a variable.
1409 // It is called by the documentation system to produce syntactically
1410 // correct documentation entires.
1412 // s is a pointer to a character pointer. You should create
1413 // a string and set this pointer to point to it.
1414 // ----------------------------------------------------------------------
1416 char *PYTHON::usage_var(char *iname
, DataType
*) {
1421 temp
<< global_name
<< "." << iname
;
1423 // Create result. Don't modify this
1428 // ---------------------------------------------------------------------------
1429 // PYTHON::usage_func(char *iname, DataType *t, ParmList *l)
1431 // Produces a string indicating how to call a function in the target
1434 // ---------------------------------------------------------------------------
1436 char *PYTHON::usage_func(char *iname
, DataType
*, ParmList
*l
) {
1443 temp
<< iname
<< "(";
1445 // Now go through and print parameters
1446 // You probably don't need to change this
1453 /* If parameter has been named, use that. Otherwise, just print a type */
1455 if ((p
->t
->type
!= T_VOID
) || (p
->t
->is_pointer
)) {
1456 if (strlen(p
->name
) > 0) {
1459 temp
<< p
->t
->print_type();
1470 if ((!p
->ignore
) && (i
> 0))
1478 // Create result. Don't change this
1485 // ----------------------------------------------------------------------
1486 // PYTHON::usage_const(char *iname, DataType *type, char *value)
1488 // Produces a string for a constant. Really about the same as
1489 // usage_var() except we'll indicate the value of the constant.
1490 // ----------------------------------------------------------------------
1492 char *PYTHON::usage_const(char *iname
, DataType
*, char *value
) {
1496 temp
<< iname
<< " = " << value
;
1501 // -----------------------------------------------------------------------
1502 // PYTHON::add_native(char *name, char *funcname)
1504 // Add a native module name to the methods list.
1505 // -----------------------------------------------------------------------
1507 void PYTHON::add_native(char *name
, char *funcname
) {
1508 add_method(name
, funcname
);
1510 func
<< name
<< " = " << module << "." << name
<< "\n\n";
1514 // -----------------------------------------------------------------------
1515 // PYTHON::cpp_class_decl(char *name, char *rename, char *type)
1517 // Treatment of an empty class definition. Used to handle
1518 // shadow classes across modules.
1519 // -----------------------------------------------------------------------
1521 void PYTHON::cpp_class_decl(char *name
, char *rename
, char *type
) {
1524 hash
.add(name
,copy_string(rename
));
1525 // Add full name of datatype to the hash table
1526 if (strlen(type
) > 0) {
1527 sprintf(temp
,"%s %s", type
, name
);
1528 hash
.add(temp
,copy_string(rename
));
1533 // -----------------------------------------------------------------------
1534 // PYTHON::pragma(char *name, char *type)
1536 // Pragma directive. Used to do various python specific things
1537 // -----------------------------------------------------------------------
1539 void PYTHON::pragma(char *lang
, char *cmd
, char *value
) {
1541 if (strcmp(lang
,"python") == 0) {
1542 if (strcmp(cmd
,"CODE") == 0) {
1544 fprintf(f_shadow
,"%s\n",value
);
1546 } else if (strcmp(cmd
,"code") == 0) {
1548 fprintf(f_shadow
,"%s\n",value
);
1550 } else if (strcmp(cmd
,"include") == 0) {
1553 if (get_file(value
,pragma_include
) == -1) {
1554 fprintf(stderr
,"%s : Line %d. Unable to locate file %s\n", input_file
, line_number
, value
);
1559 fprintf(stderr
,"%s : Line %d. Unrecognized pragma.\n", input_file
, line_number
);
1566 PyPragma(char *method
, char *text
) : m_method(method
), m_text(text
), next(0) { }
1567 ~PyPragma() { if (next
) delete next
; }
1573 static PyPragma
*pragmas
= 0;
1575 // -----------------------------------------------------------------------------
1576 // PYTHON::cpp_pragma(Pragma *plist)
1578 // Handle C++ pragmas
1579 // -----------------------------------------------------------------------------
1581 void PYTHON::cpp_pragma(Pragma
*plist
) {
1582 PyPragma
*pyp1
= 0, *pyp2
= 0;
1588 if (strcmp(plist
->lang
,"python") == 0) {
1589 if (strcmp(plist
->name
,"addtomethod") == 0) {
1590 // parse value, expected to be in the form "methodName:line"
1591 String temp
= plist
->value
;
1592 char* txtptr
= strchr(temp
.get(), ':');
1594 // add name and line to a list in current_class
1597 pyp1
= new PyPragma(temp
,txtptr
);
1606 fprintf(stderr
,"%s : Line %d. Malformed addtomethod pragma. Should be \"methodName:text\"\n",
1607 plist
->filename
.get(),plist
->lineno
);
1609 } else if (strcmp(plist
->name
, "addtoclass") == 0) {
1610 pyp1
= new PyPragma("__class__",plist
->value
);
1620 plist
= plist
->next
;
1624 // --------------------------------------------------------------------------------
1625 // PYTHON::emitAddPragmas(String& output, char* name, char* spacing);
1627 // Search the current class pragma for any text belonging to name.
1628 // Append the text properly spaced to the output string.
1629 // --------------------------------------------------------------------------------
1631 void PYTHON::emitAddPragmas(String
& output
, char* name
, char* spacing
)
1633 PyPragma
*p
= pragmas
;
1635 if (strcmp(p
->m_method
,name
) == 0) {
1636 output
<< spacing
<< p
->m_text
<< "\n";