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