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