3a049e8903105d0e8ca4cbff155c17acb79292ba
[blender.git] / source / blender / python / intern / bpy_interface.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  *
20  * Contributor(s): Michel Selten, Willian P. Germano, Stephen Swaney,
21  * Chris Keith, Chris Want, Ken Hughes, Campbell Barton
22  *
23  * ***** END GPL LICENSE BLOCK *****
24  */
25  
26 /* grr, python redefines */
27 #ifdef _POSIX_C_SOURCE
28 #undef _POSIX_C_SOURCE
29 #endif
30
31 #include <Python.h>
32
33 #include "bpy.h"
34 #include "bpy_rna.h"
35 #include "bpy_util.h"
36
37 #include "DNA_space_types.h"
38 #include "DNA_text_types.h"
39
40 #include "MEM_guardedalloc.h"
41 #include "BLI_path_util.h"
42 #include "BLI_math_base.h"
43 #include "BLI_string.h"
44
45 #include "BKE_utildefines.h"
46 #include "BKE_context.h"
47 #include "BKE_text.h"
48 #include "BKE_font.h" /* only for utf8towchar */
49 #include "BKE_main.h"
50 #include "BKE_global.h" /* only for script checking */
51
52 #include "BPY_extern.h"
53
54 #include "../generic/bpy_internal_import.h" // our own imports
55 #include "../generic/py_capi_utils.h"
56
57 /* for internal use, when starting and ending python scripts */
58
59 /* incase a python script triggers another python call, stop bpy_context_clear from invalidating */
60 static int py_call_level= 0;
61 BPy_StructRNA *bpy_context_module= NULL; /* for fast access */
62
63 // only for tests
64 #define TIME_PY_RUN
65
66 #ifdef TIME_PY_RUN
67 #include "PIL_time.h"
68 static int              bpy_timer_count = 0;
69 static double   bpy_timer; /* time since python starts */
70 static double   bpy_timer_run; /* time for each python script run */
71 static double   bpy_timer_run_tot; /* accumulate python runs */
72 #endif
73
74 void bpy_context_set(bContext *C, PyGILState_STATE *gilstate)
75 {
76         py_call_level++;
77
78         if(gilstate)
79                 *gilstate = PyGILState_Ensure();
80
81         if(py_call_level==1) {
82
83                 if(C) { // XXX - should always be true.
84                         BPy_SetContext(C);
85                         bpy_import_main_set(CTX_data_main(C));
86                 }
87                 else {
88                         fprintf(stderr, "ERROR: Python context called with a NULL Context. this should not happen!\n");
89                 }
90
91                 BPY_update_modules(); /* can give really bad results if this isnt here */
92
93 #ifdef TIME_PY_RUN
94                 if(bpy_timer_count==0) {
95                         /* record time from the beginning */
96                         bpy_timer= PIL_check_seconds_timer();
97                         bpy_timer_run = bpy_timer_run_tot = 0.0;
98                 }
99                 bpy_timer_run= PIL_check_seconds_timer();
100
101
102                 bpy_timer_count++;
103 #endif
104         }
105 }
106
107 /* context should be used but not now because it causes some bugs */
108 void bpy_context_clear(bContext *UNUSED(C), PyGILState_STATE *gilstate)
109 {
110         py_call_level--;
111
112         if(gilstate)
113                 PyGILState_Release(*gilstate);
114
115         if(py_call_level < 0) {
116                 fprintf(stderr, "ERROR: Python context internal state bug. this should not happen!\n");
117         }
118         else if(py_call_level==0) {
119                 // XXX - Calling classes currently wont store the context :\, cant set NULL because of this. but this is very flakey still.
120                 //BPy_SetContext(NULL);
121                 //bpy_import_main_set(NULL);
122
123 #ifdef TIME_PY_RUN
124                 bpy_timer_run_tot += PIL_check_seconds_timer() - bpy_timer_run;
125                 bpy_timer_count++;
126 #endif
127
128         }
129 }
130
131 void BPY_free_compiled_text( struct Text *text )
132 {
133         if( text->compiled ) {
134                 Py_DECREF( ( PyObject * ) text->compiled );
135                 text->compiled = NULL;
136         }
137 }
138
139 void BPY_update_modules( void )
140 {
141 #if 0 // slow, this runs all the time poll, draw etc 100's of time a sec.
142         PyObject *mod= PyImport_ImportModuleLevel("bpy", NULL, NULL, NULL, 0);
143         PyModule_AddObject( mod, "data", BPY_rna_module() );
144         PyModule_AddObject( mod, "types", BPY_rna_types() ); // atm this does not need updating
145 #endif
146
147         /* refreshes the main struct */
148         BPY_update_rna_module();
149         bpy_context_module->ptr.data= (void *)BPy_GetContext();
150 }
151
152 /* must be called before Py_Initialize */
153 void BPY_start_python_path(void)
154 {
155         char *py_path_bundle= BLI_get_folder(BLENDER_PYTHON, NULL);
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 #ifdef __APPLE__
164         /* OSX allow file/directory names to contain : character (represented as / in the Finder)
165          but current Python lib (release 3.1.1) doesn't handle these correctly */
166         if(strchr(py_path_bundle, ':'))
167                 printf("Warning : Blender application is located in a path containing : or / chars\
168                            \nThis may make python import function fail\n");
169 #endif
170         
171 #ifdef _WIN32
172         /* cmake/MSVC debug build crashes without this, why only
173            in this case is unknown.. */
174         {
175                 BLI_setenv("PYTHONPATH", py_path_bundle);       
176         }
177 #endif
178
179         {
180                 static wchar_t py_path_bundle_wchar[FILE_MAX];
181
182                 /* cant use this, on linux gives bug: #23018, TODO: try LANG="en_US.UTF-8" /usr/bin/blender, suggested 22008 */
183                 /* mbstowcs(py_path_bundle_wchar, py_path_bundle, FILE_MAXDIR); */
184
185                 utf8towchar(py_path_bundle_wchar, py_path_bundle);
186
187                 Py_SetPythonHome(py_path_bundle_wchar);
188                 // printf("found python (wchar_t) '%ls'\n", py_path_bundle_wchar);
189         }
190 }
191
192
193
194 void BPY_set_context(bContext *C)
195 {
196         BPy_SetContext(C);
197 }
198
199 /* init-tab */
200 extern PyObject *BPyInit_noise(void);
201 extern PyObject *BPyInit_mathutils(void);
202 // extern PyObject *BPyInit_mathutils_geometry(void); // BPyInit_mathutils calls, py doesnt work with thos :S
203 extern PyObject *BPyInit_bgl(void);
204 extern PyObject *BPyInit_blf(void);
205 extern PyObject *AUD_initPython(void);
206
207 static struct _inittab bpy_internal_modules[]= {
208         {(char *)"noise", BPyInit_noise},
209         {(char *)"mathutils", BPyInit_mathutils},
210 //      {(char *)"mathutils.geometry", BPyInit_mathutils_geometry},
211         {(char *)"bgl", BPyInit_bgl},
212         {(char *)"blf", BPyInit_blf},
213         {(char *)"aud", AUD_initPython},
214         {NULL, NULL}
215 };
216
217 /* call BPY_set_context first */
218 void BPY_start_python( int argc, char **argv )
219 {
220         PyThreadState *py_tstate = NULL;
221         
222         /* not essential but nice to set our name */
223         static wchar_t bprogname_wchar[FILE_MAXDIR+FILE_MAXFILE]; /* python holds a reference */
224         utf8towchar(bprogname_wchar, bprogname);
225         Py_SetProgramName(bprogname_wchar);
226
227         /* builtin modules */
228         PyImport_ExtendInittab(bpy_internal_modules);
229
230         BPY_start_python_path(); /* allow to use our own included python */
231
232         Py_Initialize(  );
233         
234         // PySys_SetArgv( argc, argv); // broken in py3, not a huge deal
235         /* sigh, why do python guys not have a char** version anymore? :( */
236         {
237                 int i;
238                 PyObject *py_argv= PyList_New(argc);
239                 for (i=0; i<argc; i++)
240                         PyList_SET_ITEM(py_argv, i, PyC_UnicodeFromByte(argv[i])); /* should fix bug #20021 - utf path name problems, by replacing PyUnicode_FromString */
241
242                 PySys_SetObject("argv", py_argv);
243                 Py_DECREF(py_argv);
244         }
245         
246         /* Initialize thread support (also acquires lock) */
247         PyEval_InitThreads();
248         
249         
250         /* bpy.* and lets us import it */
251         BPy_init_modules();
252
253         { /* our own import and reload functions */
254                 PyObject *item;
255                 //PyObject *m = PyImport_AddModule("__builtin__");
256                 //PyObject *d = PyModule_GetDict(m);
257                 PyObject *d = PyEval_GetBuiltins(  );
258                 PyDict_SetItemString(d, "reload",               item=PyCFunction_New(&bpy_reload_meth, NULL));  Py_DECREF(item);
259                 PyDict_SetItemString(d, "__import__",   item=PyCFunction_New(&bpy_import_meth, NULL));  Py_DECREF(item);
260         }
261         
262         pyrna_alloc_types();
263
264         py_tstate = PyGILState_GetThisThreadState();
265         PyEval_ReleaseThread(py_tstate);
266 }
267
268 void BPY_end_python( void )
269 {
270         // fprintf(stderr, "Ending Python!\n");
271
272         PyGILState_Ensure(); /* finalizing, no need to grab the state */
273         
274         // free other python data.
275         pyrna_free_types();
276
277         /* clear all python data from structs */
278         
279         Py_Finalize(  );
280         
281 #ifdef TIME_PY_RUN
282         // measure time since py started
283         bpy_timer = PIL_check_seconds_timer() - bpy_timer;
284
285         printf("*bpy stats* - ");
286         printf("tot exec: %d,  ", bpy_timer_count);
287         printf("tot run: %.4fsec,  ", bpy_timer_run_tot);
288         if(bpy_timer_count>0)
289                 printf("average run: %.6fsec,  ", (bpy_timer_run_tot/bpy_timer_count));
290
291         if(bpy_timer>0.0)
292                 printf("tot usage %.4f%%", (bpy_timer_run_tot/bpy_timer)*100.0);
293
294         printf("\n");
295
296         // fprintf(stderr, "Ending Python Done!\n");
297
298 #endif
299
300 }
301
302 /* Can run a file or text block */
303 int BPY_run_python_script( bContext *C, const char *fn, struct Text *text, struct ReportList *reports)
304 {
305         PyObject *py_dict= NULL, *py_result= NULL;
306         PyGILState_STATE gilstate;
307         
308         if (fn==NULL && text==NULL) {
309                 return 0;
310         }
311
312         bpy_context_set(C, &gilstate);
313
314         if (text) {
315                 char fn_dummy[FILE_MAXDIR];
316                 bpy_text_filename_get(fn_dummy, text);
317                 
318                 if( !text->compiled ) { /* if it wasn't already compiled, do it now */
319                         char *buf = txt_to_buf( text );
320
321                         text->compiled =
322                                 Py_CompileString( buf, fn_dummy, Py_file_input );
323
324                         MEM_freeN( buf );
325
326                         if( PyErr_Occurred(  ) ) {
327                                 BPY_free_compiled_text( text );
328                         }
329                 }
330
331                 if(text->compiled) {
332                         py_dict = PyC_DefaultNameSpace(fn_dummy);
333                         py_result =  PyEval_EvalCode(text->compiled, py_dict, py_dict);
334                 }
335                 
336         }
337         else {
338                 FILE *fp= fopen(fn, "r");
339
340                 if(fp) {
341                         py_dict = PyC_DefaultNameSpace(fn);
342
343 #ifdef _WIN32
344                         /* Previously we used PyRun_File to run directly the code on a FILE 
345                          * object, but as written in the Python/C API Ref Manual, chapter 2,
346                          * 'FILE structs for different C libraries can be different and 
347                          * incompatible'.
348                          * So now we load the script file data to a buffer */
349                         {
350                                 char *pystring;
351
352                                 fclose(fp);
353
354                                 pystring= MEM_mallocN(strlen(fn) + 32, "pystring");
355                                 pystring[0]= '\0';
356                                 sprintf(pystring, "exec(open(r'%s').read())", fn);
357                                 py_result = PyRun_String( pystring, Py_file_input, py_dict, py_dict );
358                                 MEM_freeN(pystring);
359                         }
360 #else
361                         py_result = PyRun_File(fp, fn, Py_file_input, py_dict, py_dict);
362                         fclose(fp);
363 #endif
364                 }
365                 else {
366                         PyErr_Format(PyExc_SystemError, "Python file \"%s\" could not be opened: %s", fn, strerror(errno));
367                         py_result= NULL;
368                 }
369         }
370         
371         if (!py_result) {
372                 BPy_errors_to_report(reports);
373         } else {
374                 Py_DECREF( py_result );
375         }
376
377 /* super annoying, undo _PyModule_Clear() */    
378 #define PYMODULE_CLEAR_WORKAROUND
379
380         if(py_dict) {
381 #ifdef PYMODULE_CLEAR_WORKAROUND
382                 PyObject *py_dict_back= PyDict_Copy(py_dict);
383                 Py_INCREF(py_dict);
384 #endif
385                 /* normal */
386                 PyDict_SetItemString(PyThreadState_GET()->interp->modules, "__main__", Py_None);
387 #ifdef PYMODULE_CLEAR_WORKAROUND
388                 PyDict_Clear(py_dict);
389                 PyDict_Update(py_dict, py_dict_back);
390                 Py_DECREF(py_dict);
391                 Py_DECREF(py_dict_back);
392 #endif
393 #undef PYMODULE_CLEAR_WORKAROUND
394         }
395         
396         bpy_context_clear(C, &gilstate);
397
398         return py_result ? 1:0;
399 }
400
401
402 /* TODO - move into bpy_space.c ? */
403 /* GUI interface routines */
404
405 /* Copied from Draw.c */
406 static void exit_pydraw( SpaceScript * sc, short err )
407 {
408         Script *script = NULL;
409
410         if( !sc || !sc->script )
411                 return;
412
413         script = sc->script;
414
415         if( err ) {
416                 BPy_errors_to_report(NULL); // TODO, reports
417                 script->flags = 0;      /* mark script struct for deletion */
418                 SCRIPT_SET_NULL(script);
419                 script->scriptname[0] = '\0';
420                 script->scriptarg[0] = '\0';
421 // XXX 2.5              error_pyscript();
422 // XXX 2.5              scrarea_queue_redraw( sc->area );
423         }
424
425 #if 0 // XXX 2.5
426         BPy_Set_DrawButtonsList(sc->but_refs);
427         BPy_Free_DrawButtonsList(); /*clear all temp button references*/
428 #endif
429
430         sc->but_refs = NULL;
431         
432         Py_XDECREF( ( PyObject * ) script->py_draw );
433         Py_XDECREF( ( PyObject * ) script->py_event );
434         Py_XDECREF( ( PyObject * ) script->py_button );
435
436         script->py_draw = script->py_event = script->py_button = NULL;
437 }
438
439 static int bpy_run_script_init(bContext *C, SpaceScript * sc)
440 {
441         if (sc->script==NULL) 
442                 return 0;
443         
444         if (sc->script->py_draw==NULL && sc->script->scriptname[0] != '\0')
445                 BPY_run_python_script(C, sc->script->scriptname, NULL, NULL);
446                 
447         if (sc->script->py_draw==NULL)
448                 return 0;
449         
450         return 1;
451 }
452
453 int BPY_run_script_space_draw(const struct bContext *C, SpaceScript * sc)
454 {
455         if (bpy_run_script_init( (bContext *)C, sc)) {
456                 PyGILState_STATE gilstate = PyGILState_Ensure();
457                 PyObject *result = PyObject_CallObject( sc->script->py_draw, NULL );
458                 
459                 if (result==NULL)
460                         exit_pydraw(sc, 1);
461                         
462                 PyGILState_Release(gilstate);
463         }
464         return 1;
465 }
466
467 // XXX - not used yet, listeners dont get a context
468 int BPY_run_script_space_listener(bContext *C, SpaceScript * sc)
469 {
470         if (bpy_run_script_init(C, sc)) {
471                 PyGILState_STATE gilstate = PyGILState_Ensure();
472                 
473                 PyObject *result = PyObject_CallObject( sc->script->py_draw, NULL );
474                 
475                 if (result==NULL)
476                         exit_pydraw(sc, 1);
477                         
478                 PyGILState_Release(gilstate);
479         }
480         return 1;
481 }
482
483 void BPY_DECREF(void *pyob_ptr)
484 {
485         PyGILState_STATE gilstate = PyGILState_Ensure();
486         Py_DECREF((PyObject *)pyob_ptr);
487         PyGILState_Release(gilstate);
488 }
489
490 #if 0
491 /* called from the the scripts window, assume context is ok */
492 int BPY_run_python_script_space(const char *modulename, const char *func)
493 {
494         PyObject *py_dict, *py_result= NULL;
495         char pystring[512];
496         PyGILState_STATE gilstate;
497         
498         /* for calling the module function */
499         PyObject *py_func, 
500         
501         gilstate = PyGILState_Ensure();
502         
503         py_dict = PyC_DefaultNameSpace("<dummy>");
504         
505         PyObject *module = PyImport_ImportModule(scpt->script.filename);
506         if (module==NULL) {
507                 PyErr_SetFormat(PyExc_SystemError, "could not import '%s'", scpt->script.filename);
508         }
509         else {
510                 py_func = PyObject_GetAttrString(modulename, func);
511                 if (py_func==NULL) {
512                         PyErr_SetFormat(PyExc_SystemError, "module has no function '%s.%s'\n", scpt->script.filename, func);
513                 }
514                 else {
515                         Py_DECREF(py_func);
516                         if (!PyCallable_Check(py_func)) {
517                                 PyErr_SetFormat(PyExc_SystemError, "module item is not callable '%s.%s'\n", scpt->script.filename, func);
518                         }
519                         else {
520                                 py_result= PyObject_CallObject(py_func, NULL); // XXX will need args eventually
521                         }
522                 }
523         }
524         
525         if (!py_result) {
526                 BPy_errors_to_report(NULL); // TODO - reports
527         } else
528                 Py_DECREF( py_result );
529         
530         Py_XDECREF(module);
531         
532         PyDict_SetItemString(PyThreadState_GET()->interp->modules, "__main__", Py_None);
533         
534         PyGILState_Release(gilstate);
535         return 1;
536 }
537 #endif
538
539
540 int BPY_eval_button(bContext *C, const char *expr, double *value)
541 {
542         PyGILState_STATE gilstate;
543         PyObject *py_dict, *mod, *retval;
544         int error_ret = 0;
545         
546         if (!value || !expr) return -1;
547
548         if(expr[0]=='\0') {
549                 *value= 0.0;
550                 return error_ret;
551         }
552
553         bpy_context_set(C, &gilstate);
554         
555         py_dict= PyC_DefaultNameSpace("<blender button>");
556
557         mod = PyImport_ImportModule("math");
558         if (mod) {
559                 PyDict_Merge(py_dict, PyModule_GetDict(mod), 0); /* 0 - dont overwrite existing values */
560                 Py_DECREF(mod);
561         }
562         else { /* highly unlikely but possibly */
563                 PyErr_Print();
564                 PyErr_Clear();
565         }
566         
567         retval = PyRun_String(expr, Py_eval_input, py_dict, py_dict);
568         
569         if (retval == NULL) {
570                 error_ret= -1;
571         }
572         else {
573                 double val;
574
575                 if(PyTuple_Check(retval)) {
576                         /* Users my have typed in 10km, 2m
577                          * add up all values */
578                         int i;
579                         val= 0.0;
580
581                         for(i=0; i<PyTuple_GET_SIZE(retval); i++) {
582                                 val+= PyFloat_AsDouble(PyTuple_GET_ITEM(retval, i));
583                         }
584                 }
585                 else {
586                         val = PyFloat_AsDouble(retval);
587                 }
588                 Py_DECREF(retval);
589                 
590                 if(val==-1 && PyErr_Occurred()) {
591                         error_ret= -1;
592                 }
593                 else if (!finite(val)) {
594                         *value= 0.0;
595                 }
596                 else {
597                         *value= val;
598                 }
599         }
600         
601         if(error_ret) {
602                 BPy_errors_to_report(CTX_wm_reports(C));
603         }
604
605         PyDict_SetItemString(PyThreadState_GET()->interp->modules, "__main__", Py_None);
606         
607         bpy_context_clear(C, &gilstate);
608         
609         return error_ret;
610 }
611
612 int BPY_eval_string(bContext *C, const char *expr)
613 {
614         PyGILState_STATE gilstate;
615         PyObject *py_dict, *retval;
616         int error_ret = 0;
617
618         if (!expr) return -1;
619
620         if(expr[0]=='\0') {
621                 return error_ret;
622         }
623
624         bpy_context_set(C, &gilstate);
625
626         py_dict= PyC_DefaultNameSpace("<blender string>");
627
628         retval = PyRun_String(expr, Py_eval_input, py_dict, py_dict);
629
630         if (retval == NULL) {
631                 error_ret= -1;
632
633                 BPy_errors_to_report(CTX_wm_reports(C));
634         }
635         else {
636                 Py_DECREF(retval);
637         }
638
639         PyDict_SetItemString(PyThreadState_GET()->interp->modules, "__main__", Py_None);
640         
641         bpy_context_clear(C, &gilstate);
642         
643         return error_ret;
644 }
645
646
647 void BPY_load_user_modules(bContext *C)
648 {
649         PyGILState_STATE gilstate;
650         Main *bmain= CTX_data_main(C);
651         Text *text;
652
653         /* can happen on file load */
654         if(bmain==NULL)
655                 return;
656
657         bpy_context_set(C, &gilstate);
658
659         for(text=CTX_data_main(C)->text.first; text; text= text->id.next) {
660                 if(text->flags & TXT_ISSCRIPT && BLI_testextensie(text->id.name+2, ".py")) {
661                         if(!(G.f & G_SCRIPT_AUTOEXEC)) {
662                                 printf("scripts disabled for \"%s\", skipping '%s'\n", bmain->name, text->id.name+2);
663                         }
664                         else {
665                                 PyObject *module= bpy_text_import(text);
666
667                                 if (module==NULL) {
668                                         PyErr_Print();
669                                         PyErr_Clear();
670                                 }
671                                 else {
672                                         Py_DECREF(module);
673                                 }
674                         }
675                 }
676         }
677         bpy_context_clear(C, &gilstate);
678 }
679
680 int BPY_context_get(bContext *C, const char *member, bContextDataResult *result)
681 {
682         PyObject *pyctx= (PyObject *)CTX_py_dict_get(C);
683         PyObject *item= PyDict_GetItemString(pyctx, member);
684         PointerRNA *ptr= NULL;
685         int done= 0;
686
687         if(item==NULL) {
688                 /* pass */
689         }
690         else if(item==Py_None) {
691                 /* pass */
692         }
693         else if(BPy_StructRNA_Check(item)) {
694                 ptr= &(((BPy_StructRNA *)item)->ptr);
695
696                 //result->ptr= ((BPy_StructRNA *)item)->ptr;
697                 CTX_data_pointer_set(result, ptr->id.data, ptr->type, ptr->data);
698                 done= 1;
699         }
700         else if (PySequence_Check(item)) {
701                 PyObject *seq_fast= PySequence_Fast(item, "bpy_context_get sequence conversion");
702                 if (seq_fast==NULL) {
703                         PyErr_Print();
704                         PyErr_Clear();
705                 }
706                 else {
707                         int len= PySequence_Fast_GET_SIZE(seq_fast);
708                         int i;
709                         for(i = 0; i < len; i++) {
710                                 PyObject *list_item= PySequence_Fast_GET_ITEM(seq_fast, i);
711
712                                 if(BPy_StructRNA_Check(list_item)) {
713                                         /*
714                                         CollectionPointerLink *link= MEM_callocN(sizeof(CollectionPointerLink), "bpy_context_get");
715                                         link->ptr= ((BPy_StructRNA *)item)->ptr;
716                                         BLI_addtail(&result->list, link);
717                                         */
718                                         ptr= &(((BPy_StructRNA *)list_item)->ptr);
719                                         CTX_data_list_add(result, ptr->id.data, ptr->type, ptr->data);
720                                 }
721                                 else {
722                                         printf("List item not a valid type\n");
723                                 }
724
725                         }
726                         Py_DECREF(seq_fast);
727
728                         done= 1;
729                 }
730         }
731
732         if(done==0) {
733                 if (item)       printf("Context '%s' not a valid type\n", member);
734                 else            printf("Context '%s' not found\n", member);
735         }
736         else {
737                 printf("Context '%s' found\n", member);
738         }
739
740         return done;
741 }
742