]>
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
488 f
<< tab4
<< "if (" << src
<< ") {\n"
489 << tab8
<< "if (SWIG_GetPtrObj(" << src
<< ",(void **) &" << dest
<< ",";
491 f
<< tab4
<< "if (" << src
<< ") {\n"
492 << tab8
<< "if (" << src
<< " == Py_None) { " << dest
<< " = NULL; }\n"
493 << tab8
<< "else if (SWIG_GetPtrObj(" << src
<< ",(void **) &" << dest
<< ",";
495 // If we're passing a void pointer, we give the pointer conversion a NULL
496 // pointer, otherwise pass in the expected type.
498 if (t
->type
== T_VOID
) f
<< "(char *) 0 )) {\n";
500 f
<< "\"" << t
->print_mangle() << "\")) {\n";
502 // This part handles the type checking according to three different
503 // levels. 0 = no checking, 1 = warning message, 2 = strict.
506 case 0: // No type checking
510 case 1: // Warning message only
512 // Change this part to how you want to handle a type-mismatch warning.
513 // By default, it will just print to stderr.
515 f
<< tab8
<< tab4
<< "fprintf(stderr,\"Warning : type mismatch in " << srcname
516 << " of " << iname
<< ". Expected " << t
->print_mangle()
517 << ", received %s\\n\"," << src
<< ");\n"
521 case 2: // Super strict mode.
523 // Change this part to return an error.
525 f
<< tab8
<< tab4
<< "PyErr_SetString(PyExc_TypeError,\"Type error in " << srcname
526 << " of " << iname
<< ". Expected " << t
->print_mangle() << ".\");\n"
527 << tab8
<< "return " << ret
<< ";\n"
532 fprintf(stderr
,"SWIG Error. Unknown strictness level\n");
538 // ----------------------------------------------------------------------
539 // PYTHON::emit_function_header()
541 // Return the code to be used as a function header
542 // ----------------------------------------------------------------------
543 void PYTHON::emit_function_header(WrapperFunction
&emit_to
, char *wname
)
546 emit_to
.def
<< "static PyObject *" << wname
547 << "(PyObject *self, PyObject *args) {";
549 emit_to
.def
<< "static PyObject *" << wname
550 << "(PyObject *self, PyObject *args, PyObject *kwargs) {";
552 emit_to
.code
<< tab4
<< "self = self;\n";
555 // ----------------------------------------------------------------------
556 // PYTHON::convert_self()
558 // Called during the function generation process, to determine what to
559 // use as the "self" variable during the call. Derived classes may emit code
560 // to convert the real self pointer into a usable pointer.
562 // Returns the name of the variable to use as the self pointer
563 // ----------------------------------------------------------------------
564 char *PYTHON::convert_self(WrapperFunction
&)
566 // Default behaviour is no translation
570 // ----------------------------------------------------------------------
571 // PYTHON::make_funcname_wrapper()
573 // Called to create a name for a wrapper function
574 // ----------------------------------------------------------------------
575 char *PYTHON::make_funcname_wrapper(char *fnName
)
577 return name_wrapper(fnName
,"");
580 // ----------------------------------------------------------------------
581 // PYTHON::create_command(char *cname, char *iname)
583 // Create a new command in the interpreter. Used for C++ inheritance
585 // ----------------------------------------------------------------------
587 void PYTHON::create_command(char *cname
, char *iname
) {
589 // Create the name of the wrapper function
591 char *wname
= name_wrapper(cname
,"");
593 // Now register the function with the interpreter.
595 add_method(iname
, wname
);
599 // ----------------------------------------------------------------------
600 // PYTHON::create_function(char *name, char *iname, DataType *d,
603 // This function creates a wrapper function and registers it with the
607 // name = actual name of the function that's being wrapped
608 // iname = name of the function in the interpreter (may be different)
609 // d = Return datatype of the functions.
610 // l = A linked list containing function parameter information.
612 // ----------------------------------------------------------------------
614 void PYTHON::create_function(char *name
, char *iname
, DataType
*d
, ParmList
*l
)
618 String wname
, self_name
, call_name
;
619 char source
[64], target
[64], temp
[256], argnum
[20];
625 String cleanup
, outarg
;
636 // Make a valid name for this function. This removes special symbols
637 // that would cause problems in the C compiler.
639 wname
= make_funcname_wrapper(iname
);
641 // Now emit the function declaration for the wrapper function. You
642 // should modify this to return the appropriate types and use the
643 // appropriate parameters.
645 emit_function_header(f
, wname
);
647 f
.add_local("PyObject *","_resultobj");
649 // Get the function usage string for later use
651 usage
= usage_func(iname
,d
,l
);
653 // Write code to extract function parameters.
654 // This is done in one pass, but we need to construct three independent
656 // 1. Python format string such as "iis"
657 // 2. The actual arguments to put values into
658 // 3. Pointer conversion code.
660 // If there is a type mapping, we will extract the Python argument
661 // as a raw PyObject and let the user deal with it.
664 pcount
= emit_args(d
, l
, f
);
666 parse_args
<< tab4
<< "if(!PyArg_ParseTuple(args,\"";
668 parse_args
<< tab4
<< "if(!PyArg_ParseTupleAndKeywords(args,kwargs,\"";
669 arglist
<< ",_kwnames";
674 numopt
= l
->numopt(); // Get number of optional arguments
675 if (numopt
) have_defarg
= 1;
681 // Generate source and target strings
682 sprintf(source
,"_obj%d",i
);
683 sprintf(target
,"_arg%d",i
);
684 sprintf(argnum
,"%d",j
+1);
686 // Only consider this argument if it's not ignored
690 // Add an optional argument separator if needed
692 if (j
== pcount
-numopt
) {
696 if (strlen(p
->name
)) {
697 kwargs
<< "\"" << p
->name
<< "\",";
699 kwargs
<< "\"arg" << j
+1 << "\",";
700 // kwargs << "\"\",";
703 // Look for input typemap
705 if ((tm
= typemap_lookup("in","python",p
->t
,p
->name
,source
,target
,&f
))) {
706 parse_args
<< "O"; // Grab the argument as a raw PyObject
707 f
.add_local("PyObject *",source
,"0");
708 arglist
<< "&" << source
;
709 if (i
>= (pcount
-numopt
))
710 get_pointers
<< tab4
<< "if (" << source
<< ")\n";
711 get_pointers
<< tm
<< "\n";
712 get_pointers
.replace("$argnum", argnum
);
713 get_pointers
.replace("$arg",source
);
716 // Check if this parameter is a pointer. If not, we'll get values
718 if (!p
->t
->is_pointer
) {
719 // Extract a parameter by "value"
723 // Handle integers here. Usually this can be done as a single
724 // case if you appropriate cast things. However, if you have
725 // special cases, you'll need to add more code.
727 case T_INT
: case T_UINT
: case T_SINT
:
730 case T_SHORT
: case T_USHORT
: case T_SSHORT
:
733 case T_LONG
: case T_ULONG
: case T_SLONG
:
736 case T_SCHAR
: case T_UCHAR
:
754 tempval
<< "(int) " << p
->defvalue
;
756 tempb
<< "tempbool" << i
;
759 f
.add_local("int",tempb
.get());
761 f
.add_local("int",tempb
.get(),tempval
.get());
762 get_pointers
<< tab4
<< target
<< " = " << p
->t
->print_cast() << " " << tempb
<< ";\n";
763 arglist
<< "&" << tempb
;
767 // Void.. Do nothing.
772 // User defined. This is usually invalid. No way to pass a
773 // complex type by "value". We'll just pass into the unsupported
778 // Unsupported data type
781 fprintf(stderr
,"%s : Line %d. Unable to use type %s as a function argument.\n",input_file
, line_number
, p
->t
->print_type());
785 // Emit code for parameter list
787 if ((p
->t
->type
!= T_VOID
) && (p
->t
->type
!= T_BOOL
))
788 arglist
<< "&_arg" << i
;
792 // Is some other kind of variable.
794 if ((p
->t
->type
== T_CHAR
) && (p
->t
->is_pointer
== 1)) {
796 arglist
<< "&_arg" << i
;
799 // Have some sort of pointer variable. Create a temporary local
800 // variable for the string and read the pointer value into it.
803 sprintf(source
,"_argo%d", i
);
804 sprintf(target
,"_arg%d", i
);
805 sprintf(temp
,"argument %d",i
+1);
807 f
.add_local("PyObject *", source
,"0");
808 arglist
<< "&" << source
;
809 get_pointer(iname
, temp
, source
, target
, p
->t
, get_pointers
, "NULL");
815 // Check if there was any constraint code
816 if ((tm
= typemap_lookup("check","python",p
->t
,p
->name
,source
,target
))) {
818 check
.replace("$argnum", argnum
);
820 // Check if there was any cleanup code
821 if ((tm
= typemap_lookup("freearg","python",p
->t
,p
->name
,target
,source
))) {
822 cleanup
<< tm
<< "\n";
823 cleanup
.replace("$argnum", argnum
);
824 cleanup
.replace("$arg",source
);
826 if ((tm
= typemap_lookup("argout","python",p
->t
,p
->name
,target
,"_resultobj"))) {
827 outarg
<< tm
<< "\n";
828 outarg
.replace("$argnum", argnum
);
829 outarg
.replace("$arg",source
);
832 if ((tm
= typemap_lookup("build","python",p
->t
,p
->name
,source
,target
))) {
842 f
.locals
<< tab4
<< "char *_kwnames[] = " << kwargs
<< ";\n";
845 parse_args
<< ":" << iname
<< "\""; // No additional arguments
846 parse_args
<< arglist
<< ")) \n"
847 << tab8
<< "return NULL;\n";
849 self_name
= convert_self(f
);
851 /* Now slap the whole first part of the wrapper function together */
853 f
.code
<< parse_args
<< get_pointers
<< check
;
856 // Special handling for build values
861 l
->sub_parmnames(build
); // Replace all parameter names
862 for (i
= 0; i
< l
->nparms
; i
++) {
864 if (strlen(p
->name
) > 0) {
865 sprintf(temp1
,"_in_%s", p
->name
);
867 sprintf(temp1
,"_in_arg%d", i
);
869 sprintf(temp2
,"_obj%d",i
);
870 build
.replaceid(temp1
,temp2
);
875 // This function emits code to call the real function. Assuming you read
876 // the parameters in correctly, this will work.
879 call_name
<< self_name
<< name
;
880 emit_func_call(call_name
,d
,l
,f
);
882 // Now emit code to return the functions return value (if any).
883 // If there was a result, it was saved in _result.
884 // If the function is a void type, don't do anything.
886 if ((strncmp(name
, "new_", 4) != 0) && // don't use the out typemap for constructors
887 (tm
= typemap_lookup("out","python",d
,iname
,"_result","_resultobj"))) {
888 // Yep. Use it instead of the default
889 f
.code
<< tm
<< "\n";
892 if ((d
->type
!= T_VOID
) || (d
->is_pointer
)) {
893 // Now have return value, figure out what to do with it.
895 if (!d
->is_pointer
) {
897 // Function returns a "value"
901 // Return an integer type
903 case T_INT
: case T_SINT
: case T_UINT
: case T_BOOL
:
904 f
.code
<< tab4
<< "_resultobj = Py_BuildValue(\"i\",_result);\n";
906 case T_SHORT
: case T_SSHORT
: case T_USHORT
:
907 f
.code
<< tab4
<< "_resultobj = Py_BuildValue(\"h\",_result);\n";
909 case T_LONG
: case T_SLONG
: case T_ULONG
:
910 f
.code
<< tab4
<< "_resultobj = Py_BuildValue(\"l\",_result);\n";
912 case T_SCHAR
: case T_UCHAR
:
913 f
.code
<< tab4
<< "_resultobj = Py_BuildValue(\"b\",_result);\n";
916 // Return a floating point value
919 f
.code
<< tab4
<< "_resultobj = Py_BuildValue(\"d\",_result);\n";
922 f
.code
<< tab4
<< "_resultobj = Py_BuildValue(\"f\",_result);\n";
925 // Return a single ASCII value. Usually we need to convert
926 // it to a NULL-terminate string and return that instead.
929 f
.code
<< tab4
<< "_resultobj = Py_BuildValue(\"c\",_result);\n";
934 // Return something by value
935 // We're living dangerously here, but life is short...play hard
937 // Oops. Need another local variable
938 f
.add_local("char","_ptemp[128]");
941 f
.code
<< tab4
<< "SWIG_MakePtr(_ptemp, (void *) _result,\""
942 << d
->print_mangle() << "\");\n";
944 // Return a character string containing our pointer.
946 f
.code
<< tab4
<< "_resultobj = Py_BuildValue(\"s\",_ptemp);\n";
949 fprintf(stderr
,"%s: Line %d. Unable to use return type %s in function %s.\n", input_file
, line_number
, d
->print_type(), name
);
954 // Return type is a pointer. We'll see if it's a char * and return
955 // a string. Otherwise, we'll convert it into a SWIG pointer and return
958 if ((d
->type
== T_CHAR
) && (d
->is_pointer
== 1)) {
960 // Return a character string
961 f
.code
<< tab4
<< "_resultobj = Py_BuildValue(\"s\", _result);\n";
963 // If declared as a new object, free the result
967 // Build a SWIG pointer.
968 f
.add_local("char","_ptemp[128]");
969 f
.code
<< tab4
<< "if (_result) {\n"
970 << tab8
<< "SWIG_MakePtr(_ptemp, (char *) _result,\""
971 << d
->print_mangle() << "\");\n";
973 // Return a character string containing our pointer.
974 f
.code
<< tab8
<< "_resultobj = Py_BuildValue(\"s\",_ptemp);\n";
975 f
.code
<< tab4
<< "} else {\n"
976 << tab8
<< "Py_INCREF(Py_None);\n"
977 << tab8
<< "_resultobj = Py_None;\n"
982 // no return value and no output args
983 //if (!have_output) {
984 f
.code
<< tab4
<< "Py_INCREF(Py_None);\n";
985 f
.code
<< tab4
<< "_resultobj = Py_None;\n";
990 // Check to see if there were any output arguments, if so we're going to
991 // create a Python list object out of the current result
995 // If there was any other cleanup needed, do that
999 // Look to see if there is any newfree cleanup code
1002 if ((tm
= typemap_lookup("newfree","python",d
,iname
,"_result",""))) {
1003 f
.code
<< tm
<< "\n";
1007 // See if there is any argument cleanup code
1009 if ((tm
= typemap_lookup("ret","python",d
,iname
,"_result",""))) {
1010 // Yep. Use it instead of the default
1011 f
.code
<< tm
<< "\n";
1014 f
.code
<< tab4
<< "return _resultobj;\n";
1017 // Substitute the cleanup code
1018 f
.code
.replace("$cleanup",cleanup
);
1020 // Substitute the function name
1021 f
.code
.replace("$name",iname
);
1023 // Dump the function out
1024 f
.print(f_wrappers
);
1026 // Now register the function with the interpreter.
1028 add_method(iname
, wname
);
1030 // Create a documentation entry for this
1033 static DocEntry
*last_doc_entry
= 0;
1034 doc_entry
->usage
<< usage
;
1035 if (last_doc_entry
!= doc_entry
) {
1036 doc_entry
->cinfo
<< "returns " << d
->print_type();
1037 last_doc_entry
= doc_entry
;
1041 // ---------------------------------------------------------------------------
1042 // Create a shadow for this function (if enabled and not in a member function)
1043 // ---------------------------------------------------------------------------
1045 if ((shadow
) && (!(shadow
& PYSHADOW_MEMBER
))) {
1048 int need_wrapper
= 0;
1049 int munge_return
= 0;
1050 int have_optional
= 0;
1052 // Check return code for modification
1053 if ((hash
.lookup(d
->name
)) && (d
->is_pointer
<=1)) {
1058 if (docstring
&& doc_entry
)
1061 // If no modification is needed. We're just going to play some
1062 // symbol table games instead
1064 if (!need_wrapper
) {
1065 func
<< iname
<< " = " << module << "." << iname
<< "\n\n";
1067 func
<< "def " << iname
<< "(*_args, **_kwargs):\n";
1069 // Create a docstring for this
1070 if (docstring
&& doc_entry
) {
1071 func
<< tab4
<< "\"\"\"" << add_docstring(doc_entry
) << "\"\"\"\n";
1074 func
<< tab4
<< "val = " << module << "." << iname
<< "(*_args,**_kwargs)\n";
1077 // If the output of this object has been remapped in any way, we're
1078 // going to return it as a bare object.
1080 if (!typemap_check("out",typemap_lang
,d
,iname
)) {
1082 // If there are output arguments, we are going to return the value
1083 // unchanged. Otherwise, emit some shadow class conversion code.
1086 func
<< tab4
<< "if val: val = " << (char *) hash
.lookup(d
->name
) << "Ptr(val)";
1087 if (((hash
.lookup(d
->name
)) && (d
->is_pointer
< 1)) ||
1088 ((hash
.lookup(d
->name
)) && (d
->is_pointer
== 1) && NewObject
))
1089 func
<< "; val.thisown = 1\n";
1093 // Does nothing--returns the value unmolested
1097 func
<< tab4
<< "return val\n\n";
1102 // -----------------------------------------------------------------------
1103 // PYTHON::link_variable(char *name, char *iname, DataType *d)
1106 // name = the real name of the variable being linked
1107 // iname = Name of the variable in the interpreter (may be different)
1108 // d = Datatype of the variable.
1110 // This creates a pair of functions for evaluating/setting the value
1111 // of a variable. These are then added to the special SWIG global
1113 // -----------------------------------------------------------------------
1115 void PYTHON::link_variable(char *name
, char *iname
, DataType
*t
) {
1118 static int have_globals
= 0;
1121 WrapperFunction getf
, setf
;
1123 // If this is our first call, add the globals variable to the
1124 // Python dictionary.
1126 if (!have_globals
) {
1127 fprintf(f_init
,"\t PyDict_SetItemString(d,\"%s\", SWIG_globals);\n",global_name
);
1129 if ((shadow
) && (!(shadow
& PYSHADOW_MEMBER
))) {
1130 vars
<< global_name
<< " = " << module << "." << global_name
<< "\n";
1133 // First make a sanitized version of the function name (in case it's some
1134 // funky C++ thing).
1136 wname
= name_wrapper(name
,"");
1138 // ---------------------------------------------------------------------
1139 // Create a function for setting the value of the variable
1140 // ---------------------------------------------------------------------
1142 setf
.def
<< "static int " << wname
<< "_set(PyObject *val) {";
1143 if (!(Status
& STAT_READONLY
)) {
1144 if ((tm
= typemap_lookup("varin","python",t
,name
,"val",name
))) {
1145 setf
.code
<< tm
<< "\n";
1146 setf
.code
.replace("$name",iname
);
1148 if ((t
->type
!= T_VOID
) || (t
->is_pointer
)) {
1149 if (!t
->is_pointer
) {
1151 // Have a real value here
1154 case T_INT
: case T_SHORT
: case T_LONG
:
1155 case T_UINT
: case T_USHORT
: case T_ULONG
:
1156 case T_SINT
: case T_SSHORT
: case T_SLONG
:
1157 case T_SCHAR
: case T_UCHAR
: case T_BOOL
:
1158 // Get an integer value
1159 setf
.add_local(t
->print_type(), "tval");
1160 setf
.code
<< tab4
<< "tval = " << t
->print_cast() << "PyInt_AsLong(val);\n"
1161 << tab4
<< "if (PyErr_Occurred()) {\n"
1162 << tab8
<< "PyErr_SetString(PyExc_TypeError,\"C variable '"
1163 << iname
<< "'(" << t
->print_type() << ")\");\n"
1164 << tab8
<< "return 1; \n"
1166 << tab4
<< name
<< " = tval;\n";
1169 case T_FLOAT
: case T_DOUBLE
:
1170 // Get a floating point value
1171 setf
.add_local(t
->print_type(), "tval");
1172 setf
.code
<< tab4
<< "tval = " << t
->print_cast() << "PyFloat_AsDouble(val);\n"
1173 << tab4
<< "if (PyErr_Occurred()) {\n"
1174 << tab8
<< "PyErr_SetString(PyExc_TypeError,\"C variable '"
1175 << iname
<< "'(" << t
->print_type() << ")\");\n"
1176 << tab8
<< "return 1; \n"
1178 << tab4
<< name
<< " = tval;\n";
1181 // A single ascii character
1184 setf
.add_local("char *", "tval");
1185 setf
.code
<< tab4
<< "tval = (char *) PyString_AsString(val);\n"
1186 << tab4
<< "if (PyErr_Occurred()) {\n"
1187 << tab8
<< "PyErr_SetString(PyExc_TypeError,\"C variable '"
1188 << iname
<< "'(" << t
->print_type() << ")\");\n"
1189 << tab8
<< "return 1; \n"
1191 << tab4
<< name
<< " = *tval;\n";
1195 setf
.add_local(t
->print_type(),"temp");
1196 get_pointer(iname
,"value","val","temp",t
,setf
.code
,"1");
1197 setf
.code
<< tab4
<< name
<< " = *temp;\n";
1201 fprintf(stderr
,"%s : Line %d. Unable to link with type %s.\n", input_file
, line_number
, t
->print_type());
1205 // Parse a pointer value
1207 if ((t
->type
== T_CHAR
) && (t
->is_pointer
== 1)) {
1208 setf
.add_local("char *", "tval");
1209 setf
.code
<< tab4
<< "tval = (char *) PyString_AsString(val);\n"
1210 << tab4
<< "if (PyErr_Occurred()) {\n"
1211 << tab8
<< "PyErr_SetString(PyExc_TypeError,\"C variable '"
1212 << iname
<< "'(" << t
->print_type() << ")\");\n"
1213 << tab8
<< "return 1; \n"
1217 setf
.code
<< tab4
<< "if (" << name
<< ") delete [] " << name
<< ";\n"
1218 << tab4
<< name
<< " = new char[strlen(tval)+1];\n"
1219 << tab4
<< "strcpy((char *)" << name
<< ",tval);\n";
1221 setf
.code
<< tab4
<< "if (" << name
<< ") free(" << name
<< ");\n"
1222 << tab4
<< name
<< " = (char *) malloc(strlen(tval)+1);\n"
1223 << tab4
<< "strcpy((char *)" << name
<< ",tval);\n";
1227 // Is a generic pointer value.
1229 setf
.add_local(t
->print_type(),"temp");
1230 get_pointer(iname
,"value","val","temp",t
,setf
.code
,"1");
1231 setf
.code
<< tab4
<< name
<< " = temp;\n";
1236 setf
.code
<< tab4
<< "return 0;\n";
1238 // Is a readonly variable. Issue an error
1239 setf
.code
<< tab4
<< "PyErr_SetString(PyExc_TypeError,\"Variable " << iname
1240 << " is read-only.\");\n"
1241 << tab4
<< "return 1;\n";
1246 // Dump out function for setting value
1248 setf
.print(f_wrappers
);
1250 // ----------------------------------------------------------------
1251 // Create a function for getting the value of a variable
1252 // ----------------------------------------------------------------
1254 getf
.def
<< "static PyObject *" << wname
<< "_get() {";
1255 getf
.add_local("PyObject *","pyobj");
1256 if ((tm
= typemap_lookup("varout","python",t
,name
,name
,"pyobj"))) {
1257 getf
.code
<< tm
<< "\n";
1258 getf
.code
.replace("$name",iname
);
1259 } else if ((tm
= typemap_lookup("out","python",t
,name
,name
,"pyobj"))) {
1260 getf
.code
<< tm
<< "\n";
1261 getf
.code
.replace("$name",iname
);
1263 if ((t
->type
!= T_VOID
) || (t
->is_pointer
)) {
1264 if (!t
->is_pointer
) {
1266 /* Is a normal datatype */
1268 case T_INT
: case T_SINT
: case T_UINT
:
1269 case T_SHORT
: case T_SSHORT
: case T_USHORT
:
1270 case T_LONG
: case T_SLONG
: case T_ULONG
:
1271 case T_SCHAR
: case T_UCHAR
: case T_BOOL
:
1272 getf
.code
<< tab4
<< "pyobj = PyInt_FromLong((long) " << name
<< ");\n";
1274 case T_FLOAT
: case T_DOUBLE
:
1275 getf
.code
<< tab4
<< "pyobj = PyFloat_FromDouble((double) " << name
<< ");\n";
1278 getf
.add_local("char","ptemp[2]");
1279 getf
.code
<< tab4
<< "ptemp[0] = " << name
<< ";\n"
1280 << tab4
<< "ptemp[1] = 0;\n"
1281 << tab4
<< "pyobj = PyString_FromString(ptemp);\n";
1284 // Hack this into a pointer
1285 getf
.add_local("char", "ptemp[128]");
1287 getf
.code
<< tab4
<< "SWIG_MakePtr(ptemp,(char *) &" << name
1288 << "," << quote
<< t
->print_mangle() << quote
<< ");\n"
1289 << tab4
<< "pyobj = PyString_FromString(ptemp);\n";
1293 fprintf(stderr
,"Unable to link with type %s\n", t
->print_type());
1298 // Is some sort of pointer value
1299 if ((t
->type
== T_CHAR
) && (t
->is_pointer
== 1)) {
1300 getf
.code
<< tab4
<< "if (" << name
<< ")\n"
1301 << tab8
<< "pyobj = PyString_FromString(" << name
<< ");\n"
1302 << tab4
<< "else pyobj = PyString_FromString(\"(NULL)\");\n";
1304 getf
.add_local("char","ptemp[128]");
1305 getf
.code
<< tab4
<< "SWIG_MakePtr(ptemp, (char *) " << name
<< ",\""
1306 << t
->print_mangle() << "\");\n"
1307 << tab4
<< "pyobj = PyString_FromString(ptemp);\n";
1313 getf
.code
<< tab4
<< "return pyobj;\n"
1316 getf
.print(f_wrappers
);
1318 // Now add this to the variable linking mechanism
1320 fprintf(f_init
,"\t SWIG_addvarlink(SWIG_globals,\"%s\",%s_get, %s_set);\n", iname
, wname
, wname
);
1323 // Fill in the documentation entry
1326 doc_entry
->usage
<< usage_var(iname
, t
);
1327 doc_entry
->cinfo
<< "Global : " << t
->print_type() << " " << name
;
1330 // ----------------------------------------------------------
1331 // Output a shadow variable. (If applicable and possible)
1332 // ----------------------------------------------------------
1333 if ((shadow
) && (!(shadow
& PYSHADOW_MEMBER
))) {
1334 if ((hash
.lookup(t
->name
)) && (t
->is_pointer
<= 1)) {
1335 vars
<< iname
<< " = " << (char *) hash
.lookup(t
->name
) << "Ptr(" << module << "." << global_name
1336 << "." << iname
<< ")\n";
1341 // -----------------------------------------------------------------------
1342 // PYTHON::declare_const(char *name, char *iname, DataType *type, char *value)
1344 // Makes a constant as defined with #define. Constants are added to the
1345 // module's dictionary and are **NOT** guaranteed to be read-only,
1348 // ------------------------------------------------------------------------
1350 void PYTHON::declare_const(char *name
, char *, DataType
*type
, char *value
) {
1354 // Make a static python object
1356 if ((tm
= typemap_lookup("const","python",type
,name
,value
,name
))) {
1357 fprintf(f_init
,"%s\n",tm
);
1360 if ((type
->type
== T_USER
) && (!type
->is_pointer
)) {
1361 fprintf(stderr
,"%s : Line %d. Unsupported constant value.\n", input_file
, line_number
);
1365 if (type
->is_pointer
== 0) {
1366 switch(type
->type
) {
1367 case T_INT
:case T_SINT
: case T_UINT
: case T_BOOL
:
1368 case T_SHORT
: case T_SSHORT
: case T_USHORT
:
1369 case T_LONG
: case T_SLONG
: case T_ULONG
:
1370 case T_SCHAR
: case T_UCHAR
:
1371 fprintf(f_init
,"\t PyDict_SetItemString(d,\"%s\", PyInt_FromLong((long) %s));\n",name
,value
);
1375 fprintf(f_init
,"\t PyDict_SetItemString(d,\"%s\", PyFloat_FromDouble((double) %s));\n",name
,value
);
1378 fprintf(f_init
,"\t PyDict_SetItemString(d,\"%s\", PyString_FromString(\"%s\"));\n",name
,value
);
1381 fprintf(stderr
,"%s : Line %d. Unsupported constant value.\n", input_file
, line_number
);
1385 if ((type
->type
== T_CHAR
) && (type
->is_pointer
== 1)) {
1386 fprintf(f_init
,"\t PyDict_SetItemString(d,\"%s\", PyString_FromString(\"%s\"));\n",name
,value
);
1388 // A funky user-defined type. We're going to munge it into a string pointer value
1389 fprintf(f_init
,"\t {\n");
1390 fprintf(f_init
,"\t\t char %s_char[%d];\n", name
, (int) strlen(type
->print_mangle()) + 20);
1391 fprintf(f_init
,"\t\t SWIG_MakePtr(%s_char, (void *) (%s),\"%s\");\n",
1392 name
, value
, type
->print_mangle());
1393 fprintf(f_init
,"\t\t PyDict_SetItemString(d,\"%s\", PyString_FromString(%s_char));\n",name
,name
);
1394 fprintf(f_init
,"\t }\n");
1398 if ((shadow
) && (!(shadow
& PYSHADOW_MEMBER
))) {
1399 vars
<< name
<< " = " << module << "." << name
<< "\n";
1402 doc_entry
->usage
= "";
1403 doc_entry
->usage
<< usage_const(name
,type
,value
);
1404 doc_entry
->cinfo
= "";
1405 doc_entry
->cinfo
<< "Constant: " << type
->print_type();
1409 // ----------------------------------------------------------------------
1410 // PYTHON::usage_var(char *iname, DataType *t)
1412 // This function produces a string indicating how to use a variable.
1413 // It is called by the documentation system to produce syntactically
1414 // correct documentation entires.
1416 // s is a pointer to a character pointer. You should create
1417 // a string and set this pointer to point to it.
1418 // ----------------------------------------------------------------------
1420 char *PYTHON::usage_var(char *iname
, DataType
*) {
1425 temp
<< global_name
<< "." << iname
;
1427 // Create result. Don't modify this
1432 // ---------------------------------------------------------------------------
1433 // PYTHON::usage_func(char *iname, DataType *t, ParmList *l)
1435 // Produces a string indicating how to call a function in the target
1438 // ---------------------------------------------------------------------------
1440 char *PYTHON::usage_func(char *iname
, DataType
*, ParmList
*l
) {
1447 temp
<< iname
<< "(";
1449 // Now go through and print parameters
1450 // You probably don't need to change this
1457 /* If parameter has been named, use that. Otherwise, just print a type */
1459 if ((p
->t
->type
!= T_VOID
) || (p
->t
->is_pointer
)) {
1460 if (strlen(p
->name
) > 0) {
1463 temp
<< p
->t
->print_type();
1474 if ((!p
->ignore
) && (i
> 0))
1482 // Create result. Don't change this
1489 // ----------------------------------------------------------------------
1490 // PYTHON::usage_const(char *iname, DataType *type, char *value)
1492 // Produces a string for a constant. Really about the same as
1493 // usage_var() except we'll indicate the value of the constant.
1494 // ----------------------------------------------------------------------
1496 char *PYTHON::usage_const(char *iname
, DataType
*, char *value
) {
1500 temp
<< iname
<< " = " << value
;
1505 // -----------------------------------------------------------------------
1506 // PYTHON::add_native(char *name, char *funcname)
1508 // Add a native module name to the methods list.
1509 // -----------------------------------------------------------------------
1511 void PYTHON::add_native(char *name
, char *funcname
) {
1512 add_method(name
, funcname
);
1514 func
<< name
<< " = " << module << "." << name
<< "\n\n";
1518 // -----------------------------------------------------------------------
1519 // PYTHON::cpp_class_decl(char *name, char *rename, char *type)
1521 // Treatment of an empty class definition. Used to handle
1522 // shadow classes across modules.
1523 // -----------------------------------------------------------------------
1525 void PYTHON::cpp_class_decl(char *name
, char *rename
, char *type
) {
1528 hash
.add(name
,copy_string(rename
));
1529 // Add full name of datatype to the hash table
1530 if (strlen(type
) > 0) {
1531 sprintf(temp
,"%s %s", type
, name
);
1532 hash
.add(temp
,copy_string(rename
));
1537 // -----------------------------------------------------------------------
1538 // PYTHON::pragma(char *name, char *type)
1540 // Pragma directive. Used to do various python specific things
1541 // -----------------------------------------------------------------------
1543 void PYTHON::pragma(char *lang
, char *cmd
, char *value
) {
1545 if (strcmp(lang
,"python") == 0) {
1546 if (strcmp(cmd
,"CODE") == 0) {
1548 fprintf(f_shadow
,"%s\n",value
);
1550 } else if (strcmp(cmd
,"code") == 0) {
1552 fprintf(f_shadow
,"%s\n",value
);
1554 } else if (strcmp(cmd
,"include") == 0) {
1557 if (get_file(value
,pragma_include
) == -1) {
1558 fprintf(stderr
,"%s : Line %d. Unable to locate file %s\n", input_file
, line_number
, value
);
1563 fprintf(stderr
,"%s : Line %d. Unrecognized pragma.\n", input_file
, line_number
);
1570 PyPragma(char *method
, char *text
) : m_method(method
), m_text(text
), next(0) { }
1571 ~PyPragma() { if (next
) delete next
; }
1577 static PyPragma
*pragmas
= 0;
1579 // -----------------------------------------------------------------------------
1580 // PYTHON::cpp_pragma(Pragma *plist)
1582 // Handle C++ pragmas
1583 // -----------------------------------------------------------------------------
1585 void PYTHON::cpp_pragma(Pragma
*plist
) {
1586 PyPragma
*pyp1
= 0, *pyp2
= 0;
1592 if (strcmp(plist
->lang
,"python") == 0) {
1593 if (strcmp(plist
->name
,"addtomethod") == 0) {
1594 // parse value, expected to be in the form "methodName:line"
1595 String temp
= plist
->value
;
1596 char* txtptr
= strchr(temp
.get(), ':');
1598 // add name and line to a list in current_class
1601 pyp1
= new PyPragma(temp
,txtptr
);
1610 fprintf(stderr
,"%s : Line %d. Malformed addtomethod pragma. Should be \"methodName:text\"\n",
1611 plist
->filename
.get(),plist
->lineno
);
1613 } else if (strcmp(plist
->name
, "addtoclass") == 0) {
1614 pyp1
= new PyPragma("__class__",plist
->value
);
1624 plist
= plist
->next
;
1628 // --------------------------------------------------------------------------------
1629 // PYTHON::emitAddPragmas(String& output, char* name, char* spacing);
1631 // Search the current class pragma for any text belonging to name.
1632 // Append the text properly spaced to the output string.
1633 // --------------------------------------------------------------------------------
1635 void PYTHON::emitAddPragmas(String
& output
, char* name
, char* spacing
)
1637 PyPragma
*p
= pragmas
;
1639 if (strcmp(p
->m_method
,name
) == 0) {
1640 output
<< spacing
<< p
->m_text
<< "\n";