cmake option to disable SDL,
[blender.git] / source / blender / python / intern / bpy_interface.c
1
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include <sys/stat.h>
6
7 #ifndef WIN32
8 #include <dirent.h>
9 #else
10 #include "BLI_winstuff.h"
11 #endif
12
13 #include <Python.h>
14 #include "compile.h"            /* for the PyCodeObject */
15 #include "eval.h"               /* for PyEval_EvalCode */
16
17 #include "bpy_compat.h"
18
19 #include "bpy_rna.h"
20 #include "bpy_operator.h"
21 #include "bpy_ui.h"
22 #include "bpy_util.h"
23
24 #include "DNA_anim_types.h"
25 #include "DNA_space_types.h"
26 #include "DNA_text_types.h"
27
28 #include "MEM_guardedalloc.h"
29
30 #include "BLI_util.h"
31 #include "BLI_fileops.h"
32 #include "BLI_string.h"
33
34 #include "BKE_context.h"
35 #include "BKE_fcurve.h"
36 #include "BKE_text.h"
37
38 #include "BPY_extern.h"
39
40 #include "../generic/bpy_internal_import.h" // our own imports
41 /* external util modules */
42
43 #include "../generic/Mathutils.h"
44 #include "../generic/Geometry.h"
45 #include "../generic/BGL.h"
46
47
48 void BPY_free_compiled_text( struct Text *text )
49 {
50         if( text->compiled ) {
51                 Py_DECREF( ( PyObject * ) text->compiled );
52                 text->compiled = NULL;
53         }
54 }
55
56 /*****************************************************************************
57 * Description: Creates the bpy module and adds it to sys.modules for importing
58 *****************************************************************************/
59 static void bpy_init_modules( void )
60 {
61         PyObject *mod;
62         
63         mod = PyModule_New("bpy");
64         
65         PyModule_AddObject( mod, "data", BPY_rna_module() );
66         /* PyModule_AddObject( mod, "doc", BPY_rna_doc() ); */
67         PyModule_AddObject( mod, "types", BPY_rna_types() );
68         PyModule_AddObject( mod, "props", BPY_rna_props() );
69         PyModule_AddObject( mod, "__ops__", BPY_operator_module() ); /* ops is now a python module that does the conversion from SOME_OT_foo -> some.foo */
70         PyModule_AddObject( mod, "ui", BPY_ui_module() ); // XXX very experimental, consider this a test, especially PyCObject is not meant to be permanent
71         
72         /* add the module so we can import it */
73         PyDict_SetItemString(PySys_GetObject("modules"), "bpy", mod);
74         Py_DECREF(mod);
75
76
77         /* stand alone utility modules not related to blender directly */
78         Geometry_Init("Geometry");
79         Mathutils_Init("Mathutils");
80         BGL_Init("BGL");
81 }
82
83 #if (PY_VERSION_HEX < 0x02050000)
84 PyObject *PyImport_ImportModuleLevel(char *name, void *a, void *b, void *c, int d)
85 {
86         return PyImport_ImportModule(name);
87 }
88 #endif
89
90 void BPY_update_modules( void )
91 {
92         PyObject *mod= PyImport_ImportModuleLevel("bpy", NULL, NULL, NULL, 0);
93         PyModule_AddObject( mod, "data", BPY_rna_module() );
94         PyModule_AddObject( mod, "types", BPY_rna_types() );
95 }
96
97 /*****************************************************************************
98 * Description: This function creates a new Python dictionary object.
99 *****************************************************************************/
100 static PyObject *CreateGlobalDictionary( bContext *C )
101 {
102         PyObject *mod;
103         PyObject *dict = PyDict_New(  );
104         PyObject *item = PyUnicode_FromString( "__main__" );
105         PyDict_SetItemString( dict, "__builtins__", PyEval_GetBuiltins(  ) );
106         PyDict_SetItemString( dict, "__name__", item );
107         Py_DECREF(item);
108         
109         // XXX - evil, need to access context
110         BPy_SetContext(C);
111         
112         // XXX - put somewhere more logical
113         {
114                 PyMethodDef *ml;
115                 static PyMethodDef bpy_prop_meths[] = {
116                         {"FloatProperty", (PyCFunction)BPy_FloatProperty, METH_VARARGS|METH_KEYWORDS, ""},
117                         {"IntProperty", (PyCFunction)BPy_IntProperty, METH_VARARGS|METH_KEYWORDS, ""},
118                         {"BoolProperty", (PyCFunction)BPy_BoolProperty, METH_VARARGS|METH_KEYWORDS, ""},
119                         {"StringProperty", (PyCFunction)BPy_StringProperty, METH_VARARGS|METH_KEYWORDS, ""},
120                         {NULL, NULL, 0, NULL}
121                 };
122                 
123                 for(ml = bpy_prop_meths; ml->ml_name; ml++) {
124                         PyDict_SetItemString( dict, ml->ml_name, PyCFunction_New(ml, NULL));
125                 }
126         }
127         
128         /* add bpy to global namespace */
129         mod= PyImport_ImportModuleLevel("bpy", NULL, NULL, NULL, 0);
130         PyDict_SetItemString( dict, "bpy", mod );
131         Py_DECREF(mod);
132         
133         return dict;
134 }
135
136 /* Use this so we can include our own python bundle */
137 #if 0
138 wchar_t* Py_GetPath(void)
139 {
140         int i;
141         static wchar_t py_path[FILE_MAXDIR] = L"";
142         char *dirname= BLI_gethome_folder("python");
143         if(dirname) {
144                 i= mbstowcs(py_path, dirname, FILE_MAXDIR);
145                 printf("py path %s, %d\n", dirname, i);
146         }
147         return py_path;
148 }
149 #endif
150
151
152 /* must be called before Py_Initialize */
153 void BPY_start_python_path(void)
154 {
155         char *py_path_bundle= BLI_gethome_folder("python");
156
157         if(py_path_bundle==NULL)
158                 return;
159
160         /* set the environment path */
161         printf("found bundled python: %s\n", py_path_bundle);
162
163 #if (defined(WIN32) || defined(WIN64))
164 #if defined(FREE_WINDOWS)
165         {
166                 char py_path[FILE_MAXDIR + 11] = "";
167                 sprintf(py_path, "PYTHONPATH=%s", py_path_bundle);
168                 putenv(py_path);
169         }
170 #else
171         _putenv_s("PYTHONPATH", py_path_bundle);
172 #endif
173 #else
174 #ifdef __sgi
175         {
176                 char py_path[FILE_MAXDIR + 11] = "";
177                 sprintf(py_path, "PYTHONPATH=%s", py_path_bundle);
178                 putenv(py_path);
179         }
180 #else
181         setenv("PYTHONPATH", py_path_bundle, 1);
182 #endif
183 #endif
184
185 }
186
187
188 void BPY_start_python( int argc, char **argv )
189 {
190         PyThreadState *py_tstate = NULL;
191         
192         BPY_start_python_path(); /* allow to use our own included python */
193
194         Py_Initialize(  );
195         
196 #if (PY_VERSION_HEX < 0x03000000)
197         PySys_SetArgv( argc, argv);
198 #else
199         /* sigh, why do python guys not have a char** version anymore? :( */
200         {
201                 int i;
202                 PyObject *py_argv= PyList_New(argc);
203
204                 for (i=0; i<argc; i++)
205                         PyList_SET_ITEM(py_argv, i, PyUnicode_FromString(argv[i]));
206
207                 PySys_SetObject("argv", py_argv);
208                 Py_DECREF(py_argv);
209         }
210 #endif
211         
212         /* Initialize thread support (also acquires lock) */
213         PyEval_InitThreads();
214         
215         
216         /* bpy.* and lets us import it */
217         bpy_init_modules(); 
218
219         { /* our own import and reload functions */
220                 PyObject *item;
221                 //PyObject *m = PyImport_AddModule("__builtin__");
222                 //PyObject *d = PyModule_GetDict(m);
223                 PyObject *d = PyEval_GetBuiltins(  );
224                 PyDict_SetItemString(d, "reload",               item=PyCFunction_New(bpy_reload_meth, NULL));   Py_DECREF(item);
225                 PyDict_SetItemString(d, "__import__",   item=PyCFunction_New(bpy_import_meth, NULL));   Py_DECREF(item);
226         }
227         
228         py_tstate = PyGILState_GetThisThreadState();
229         PyEval_ReleaseThread(py_tstate);
230 }
231
232 void BPY_end_python( void )
233 {
234         PyGILState_Ensure(); /* finalizing, no need to grab the state */
235         
236         // free other python data.
237         //BPY_rna_free_types();
238         
239         Py_Finalize(  );
240         
241         return;
242 }
243
244 /* Can run a file or text block */
245 int BPY_run_python_script( bContext *C, const char *fn, struct Text *text, struct ReportList *reports)
246 {
247         PyObject *py_dict, *py_result;
248         PyGILState_STATE gilstate;
249         
250         if (fn==NULL && text==NULL) {
251                 return 0;
252         }
253         
254         //BPY_start_python();
255         
256         gilstate = PyGILState_Ensure();
257
258         BPY_update_modules(); /* can give really bad results if this isnt here */
259         bpy_import_main_set(CTX_data_main(C));
260         
261         py_dict = CreateGlobalDictionary(C);
262
263         if (text) {
264                 
265                 if( !text->compiled ) { /* if it wasn't already compiled, do it now */
266                         char *buf = txt_to_buf( text );
267
268                         text->compiled =
269                                 Py_CompileString( buf, text->id.name+2, Py_file_input );
270
271                         MEM_freeN( buf );
272
273                         if( PyErr_Occurred(  ) ) {
274                                 BPy_errors_to_report(reports);
275                                 BPY_free_compiled_text( text );
276                                 PyGILState_Release(gilstate);
277                                 return 0;
278                         }
279                 }
280                 py_result =  PyEval_EvalCode( text->compiled, py_dict, py_dict );
281                 
282         } else {
283                 char pystring[512];
284                 /* TODO - look into a better way to run a file */
285                 sprintf(pystring, "exec(open(r'%s').read())", fn);      
286                 py_result = PyRun_String( pystring, Py_file_input, py_dict, py_dict );                  
287         }
288         
289         if (!py_result) {
290                 BPy_errors_to_report(reports);
291         } else {
292                 Py_DECREF( py_result );
293         }
294         
295         Py_DECREF(py_dict);
296         PyGILState_Release(gilstate);
297         bpy_import_main_set(NULL);
298         
299         //BPY_end_python();
300         return py_result ? 1:0;
301 }
302
303
304 /* TODO - move into bpy_space.c ? */
305 /* GUI interface routines */
306
307 /* Copied from Draw.c */
308 static void exit_pydraw( SpaceScript * sc, short err )
309 {
310         Script *script = NULL;
311
312         if( !sc || !sc->script )
313                 return;
314
315         script = sc->script;
316
317         if( err ) {
318                 BPy_errors_to_report(NULL); // TODO, reports
319                 script->flags = 0;      /* mark script struct for deletion */
320                 SCRIPT_SET_NULL(script);
321                 script->scriptname[0] = '\0';
322                 script->scriptarg[0] = '\0';
323 // XXX 2.5              error_pyscript();
324 // XXX 2.5              scrarea_queue_redraw( sc->area );
325         }
326
327 #if 0 // XXX 2.5
328         BPy_Set_DrawButtonsList(sc->but_refs);
329         BPy_Free_DrawButtonsList(); /*clear all temp button references*/
330 #endif
331
332         sc->but_refs = NULL;
333         
334         Py_XDECREF( ( PyObject * ) script->py_draw );
335         Py_XDECREF( ( PyObject * ) script->py_event );
336         Py_XDECREF( ( PyObject * ) script->py_button );
337
338         script->py_draw = script->py_event = script->py_button = NULL;
339 }
340
341 static int bpy_run_script_init(bContext *C, SpaceScript * sc)
342 {
343         if (sc->script==NULL) 
344                 return 0;
345         
346         if (sc->script->py_draw==NULL && sc->script->scriptname[0] != '\0')
347                 BPY_run_python_script(C, sc->script->scriptname, NULL, NULL);
348                 
349         if (sc->script->py_draw==NULL)
350                 return 0;
351         
352         return 1;
353 }
354
355 int BPY_run_script_space_draw(const struct bContext *C, SpaceScript * sc)
356 {
357         if (bpy_run_script_init( (bContext *)C, sc)) {
358                 PyGILState_STATE gilstate = PyGILState_Ensure();
359                 PyObject *result = PyObject_CallObject( sc->script->py_draw, NULL );
360                 
361                 if (result==NULL)
362                         exit_pydraw(sc, 1);
363                         
364                 PyGILState_Release(gilstate);
365         }
366         return 1;
367 }
368
369 // XXX - not used yet, listeners dont get a context
370 int BPY_run_script_space_listener(bContext *C, SpaceScript * sc)
371 {
372         if (bpy_run_script_init(C, sc)) {
373                 PyGILState_STATE gilstate = PyGILState_Ensure();
374                 
375                 PyObject *result = PyObject_CallObject( sc->script->py_draw, NULL );
376                 
377                 if (result==NULL)
378                         exit_pydraw(sc, 1);
379                         
380                 PyGILState_Release(gilstate);
381         }
382         return 1;
383 }
384
385 void BPY_DECREF(void *pyob_ptr)
386 {
387         Py_DECREF((PyObject *)pyob_ptr);
388 }
389
390 #if 0
391 /* called from the the scripts window, assume context is ok */
392 int BPY_run_python_script_space(const char *modulename, const char *func)
393 {
394         PyObject *py_dict, *py_result= NULL;
395         char pystring[512];
396         PyGILState_STATE gilstate;
397         
398         /* for calling the module function */
399         PyObject *py_func, 
400         
401         gilstate = PyGILState_Ensure();
402         
403         py_dict = CreateGlobalDictionary(C);
404         
405         PyObject *module = PyImport_ImportModule(scpt->script.filename);
406         if (module==NULL) {
407                 PyErr_SetFormat(PyExc_SystemError, "could not import '%s'", scpt->script.filename);
408         }
409         else {
410                 py_func = PyObject_GetAttrString(modulename, func);
411                 if (py_func==NULL) {
412                         PyErr_SetFormat(PyExc_SystemError, "module has no function '%s.%s'\n", scpt->script.filename, func);
413                 }
414                 else {
415                         Py_DECREF(py_func);
416                         if (!PyCallable_Check(py_func)) {
417                                 PyErr_SetFormat(PyExc_SystemError, "module item is not callable '%s.%s'\n", scpt->script.filename, func);
418                         }
419                         else {
420                                 py_result= PyObject_CallObject(py_func, NULL); // XXX will need args eventually
421                         }
422                 }
423         }
424         
425         if (!py_result) {
426                 BPy_errors_to_report(NULL); // TODO - reports
427         } else
428                 Py_DECREF( py_result );
429         
430         Py_XDECREF(module);
431         
432         Py_DECREF(py_dict);
433         
434         PyGILState_Release(gilstate);
435         return 1;
436 }
437 #endif
438
439 // #define TIME_REGISTRATION
440
441 #ifdef TIME_REGISTRATION
442 #include "PIL_time.h"
443 #endif
444
445 /* for use by BPY_run_ui_scripts only */
446 static int bpy_import_module(char *modname, int reload)
447 {
448         PyObject *mod= PyImport_ImportModuleLevel(modname, NULL, NULL, NULL, 0);
449         if (mod) {
450                 if (reload) {
451                         PyObject *mod_orig= mod;
452                         mod= PyImport_ReloadModule(mod);
453                         Py_DECREF(mod_orig);
454                 }
455         }
456
457         if(mod) {
458                 Py_DECREF(mod); /* could be NULL from reloading */
459                 return 0;
460         } else {
461                 return -1;
462         }
463 }
464
465 /* XXX this is temporary, need a proper script registration system for 2.5 */
466 void BPY_run_ui_scripts(bContext *C, int reload)
467 {
468 #ifdef TIME_REGISTRATION
469         double time = PIL_check_seconds_timer();
470 #endif
471         DIR *dir; 
472         struct dirent *de;
473         char *file_extension;
474         char *dirname;
475         char path[FILE_MAX];
476         char *dirs[] = {"ui", "io", NULL};
477         int a, err;
478         
479         PyGILState_STATE gilstate;
480         PyObject *sys_path;
481
482         gilstate = PyGILState_Ensure();
483         
484         // XXX - evil, need to access context
485         BPy_SetContext(C);
486         bpy_import_main_set(CTX_data_main(C));
487
488
489         sys_path= PySys_GetObject("path"); /* borrow */
490         PyList_Insert(sys_path, 0, Py_None); /* place holder, resizes the list */
491
492         for(a=0; dirs[a]; a++) {
493                 dirname= BLI_gethome_folder(dirs[a]);
494
495                 if(!dirname)
496                         continue;
497
498                 dir = opendir(dirname);
499
500                 if(!dir)
501                         continue;
502                 
503                 /* set the first dir in the sys.path for fast importing of modules */
504                 PyList_SetItem(sys_path, 0, PyUnicode_FromString(dirname)); /* steals the ref */
505                         
506                 while((de = readdir(dir)) != NULL) {
507                         /* We could stat the file but easier just to let python
508                          * import it and complain if theres a problem */
509                         err = 0;
510
511                         if (de->d_name[0] == '.') {
512                                 /* do nothing, probably .svn */
513                         }
514                         else if ((file_extension = strstr(de->d_name, ".py"))) {
515                                 /* normal py files? */
516                                 if(file_extension && file_extension[3] == '\0') {
517                                         de->d_name[(file_extension - de->d_name) + 1] = '\0';
518                                         bpy_import_module(de->d_name, reload);
519                                 }
520                         }
521 #ifndef __linux__
522                         else if( BLI_join_dirfile(path, dirname, de->d_name), S_ISDIR(BLI_exists(path))) {
523 #else
524                         else if(de->d_type==DT_DIR) {
525                                 BLI_join_dirfile(path, dirname, de->d_name);
526 #endif
527                                 /* support packages */
528                                 BLI_join_dirfile(path, path, "__init__.py");
529
530                                 if(BLI_exists(path)) {
531                                         bpy_import_module(de->d_name, reload);
532                                 }
533                         }
534
535                         if(err==-1) {
536                                 BPy_errors_to_report(NULL);
537                                 fprintf(stderr, "unable to import %s/%s\n", dirname, de->d_name);
538                         }
539                 }
540
541                 closedir(dir);
542         }
543         
544         PyList_SetSlice(sys_path, 0, 1, NULL); /* remove the first item */
545
546         bpy_import_main_set(NULL);
547         
548         PyGILState_Release(gilstate);
549 #ifdef TIME_REGISTRATION
550         printf("script time %f\n", (PIL_check_seconds_timer()-time));
551 #endif
552 }
553
554 /* ****************************************** */
555 /* Drivers - PyExpression Evaluation */
556
557 /* for pydrivers (drivers using one-line Python expressions to express relationships between targets) */
558 PyObject *bpy_pydriver_Dict = NULL;
559
560 /* For faster execution we keep a special dictionary for pydrivers, with
561  * the needed modules and aliases. 
562  */
563 static int bpy_pydriver_create_dict(void)
564 {
565         PyObject *d, *mod;
566         
567         /* validate namespace for driver evaluation */
568         if (bpy_pydriver_Dict) return -1;
569
570         d = PyDict_New();
571         if (d == NULL) 
572                 return -1;
573         else
574                 bpy_pydriver_Dict = d;
575
576         /* import some modules: builtins, bpy, math, (Blender.noise )*/
577         PyDict_SetItemString(d, "__builtins__", PyEval_GetBuiltins());
578
579         mod = PyImport_ImportModule("math");
580         if (mod) {
581                 PyDict_Merge(d, PyModule_GetDict(mod), 0); /* 0 - dont overwrite existing values */
582                 
583                 /* Only keep for backwards compat! - just import all math into root, they are standard */
584                 PyDict_SetItemString(d, "math", mod);
585                 PyDict_SetItemString(d, "m", mod);
586                 Py_DECREF(mod);
587         } 
588         
589         /* add bpy to global namespace */
590         mod= PyImport_ImportModuleLevel("bpy", NULL, NULL, NULL, 0);
591         if (mod) {
592                 PyDict_SetItemString(bpy_pydriver_Dict, "bpy", mod);
593                 Py_DECREF(mod);
594         }
595         
596         
597 #if 0 // non existant yet
598         mod = PyImport_ImportModule("Blender.Noise");
599         if (mod) {
600                 PyDict_SetItemString(d, "noise", mod);
601                 PyDict_SetItemString(d, "n", mod);
602                 Py_DECREF(mod);
603         } else {
604                 PyErr_Clear();
605         }
606         
607         /* If there's a Blender text called pydrivers.py, import it.
608          * Users can add their own functions to this module. 
609          */
610         if (G.f & G_DOSCRIPTLINKS) {
611                 mod = importText("pydrivers"); /* can also use PyImport_Import() */
612                 if (mod) {
613                         PyDict_SetItemString(d, "pydrivers", mod);
614                         PyDict_SetItemString(d, "p", mod);
615                         Py_DECREF(mod);
616                 } else {
617                         PyErr_Clear();
618                 }
619         }
620 #endif // non existant yet
621         
622         return 0;
623 }
624
625 /* Update function, it gets rid of pydrivers global dictionary, forcing
626  * BPY_pydriver_eval to recreate it. This function is used to force
627  * reloading the Blender text module "pydrivers.py", if available, so
628  * updates in it reach pydriver evaluation. 
629  */
630 void BPY_pydriver_update(void)
631 {
632         PyGILState_STATE gilstate = PyGILState_Ensure();
633
634         if (bpy_pydriver_Dict) { /* free the global dict used by pydrivers */
635                 PyDict_Clear(bpy_pydriver_Dict);
636                 Py_DECREF(bpy_pydriver_Dict);
637                 bpy_pydriver_Dict = NULL;
638         }
639
640         PyGILState_Release(gilstate);
641
642         return;
643 }
644
645 /* error return function for BPY_eval_pydriver */
646 static float pydriver_error(ChannelDriver *driver) 
647 {
648         if (bpy_pydriver_Dict) { /* free the global dict used by pydrivers */
649                 PyDict_Clear(bpy_pydriver_Dict);
650                 Py_DECREF(bpy_pydriver_Dict);
651                 bpy_pydriver_Dict = NULL;
652         }
653
654         driver->flag |= DRIVER_FLAG_INVALID; /* py expression failed */
655         fprintf(stderr, "\nError in Driver: The following Python expression failed:\n\t'%s'\n\n", driver->expression);
656         
657         BPy_errors_to_report(NULL); // TODO - reports
658
659         return 0.0f;
660 }
661
662 /* This evals py driver expressions, 'expr' is a Python expression that
663  * should evaluate to a float number, which is returned. 
664  */
665 float BPY_pydriver_eval (ChannelDriver *driver)
666 {
667         PyObject *driver_vars=NULL;
668         PyObject *retval;
669         PyGILState_STATE gilstate;
670         
671         DriverTarget *dtar;
672         float result = 0.0f; /* default return */
673         char *expr = NULL;
674         short targets_ok= 1;
675         
676         /* sanity checks - should driver be executed? */
677         if ((driver == NULL) /*|| (G.f & G_DOSCRIPTLINKS)==0*/) 
678                 return result;
679         
680         /* get the py expression to be evaluated */
681         expr = driver->expression; 
682         if ((expr == NULL) || (expr[0]=='\0')) 
683                 return result;
684
685         gilstate = PyGILState_Ensure();
686         
687         /* init global dictionary for py-driver evaluation settings */
688         if (!bpy_pydriver_Dict) {
689                 if (bpy_pydriver_create_dict() != 0) {
690                         fprintf(stderr, "Pydriver error: couldn't create Python dictionary");
691                         PyGILState_Release(gilstate);
692                         return result;
693                 }
694         }
695         
696         /* add target values to a dict that will be used as '__locals__' dict */
697         driver_vars = PyDict_New(); // XXX do we need to decref this?
698         for (dtar= driver->targets.first; dtar; dtar= dtar->next) {
699                 PyObject *driver_arg = NULL;
700                 float tval = 0.0f;
701                 
702                 /* try to get variable value */
703                 tval= driver_get_target_value(driver, dtar);
704                 driver_arg= PyFloat_FromDouble((double)tval);
705                 
706                 /* try to add to dictionary */
707                 if (PyDict_SetItemString(driver_vars, dtar->name, driver_arg)) {
708                         /* this target failed - bad name */
709                         if (targets_ok) {
710                                 /* first one - print some extra info for easier identification */
711                                 fprintf(stderr, "\nBPY_pydriver_eval() - Error while evaluating PyDriver:\n");
712                                 targets_ok= 0;
713                         }
714                         
715                         fprintf(stderr, "\tBPY_pydriver_eval() - couldn't add variable '%s' to namespace \n", dtar->name);
716                         BPy_errors_to_report(NULL); // TODO - reports
717                 }
718         }
719         
720         /* execute expression to get a value */
721         retval = PyRun_String(expr, Py_eval_input, bpy_pydriver_Dict, driver_vars);
722         
723         /* decref the driver vars first...  */
724         Py_DECREF(driver_vars);
725         
726         /* process the result */
727         if (retval == NULL) {
728                 result = pydriver_error(driver);
729                 PyGILState_Release(gilstate);
730                 return result;
731         }
732
733         result = (float)PyFloat_AsDouble(retval);
734         Py_DECREF(retval);
735         
736         if ((result == -1) && PyErr_Occurred()) {
737                 result = pydriver_error(driver);
738                 PyGILState_Release(gilstate);
739                 return result;
740         }
741         
742         /* all fine, make sure the "invalid expression" flag is cleared */
743         driver->flag &= ~DRIVER_FLAG_INVALID;
744
745         PyGILState_Release(gilstate);
746
747         return result;
748 }