* removed warnings and fixed some python refcount errors
[blender.git] / source / blender / python / intern / bpy_util.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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * Contributor(s): Campbell Barton
21  *
22  * ***** END GPL LICENSE BLOCK *****
23  */
24
25
26 #include "bpy_util.h"
27 #include "BLI_dynstr.h"
28 #include "MEM_guardedalloc.h"
29 #include "bpy_compat.h"
30
31 PyObject *BPY_flag_to_list(struct BPY_flag_def *flagdef, int flag)
32 {
33         PyObject *list = PyList_New(0);
34         
35         PyObject *item;
36         BPY_flag_def *fd;
37
38         fd= flagdef;
39         while(fd->name) {
40                 if (fd->flag & flag) {
41                         item = PyUnicode_FromString(fd->name);
42                         PyList_Append(list, item);
43                         Py_DECREF(item);
44                 }
45                 fd++;
46         }
47         
48         return list;
49
50 }
51
52 static char *bpy_flag_error_str(BPY_flag_def *flagdef)
53 {
54         BPY_flag_def *fd= flagdef;
55         DynStr *dynstr= BLI_dynstr_new();
56         char *cstring;
57
58         BLI_dynstr_append(dynstr, "Error converting a sequence of strings into a flag.\n\tExpected only these strings...\n\t");
59
60         while(fd->name) {
61                 BLI_dynstr_appendf(dynstr, fd!=flagdef?", '%s'":"'%s'", fd->name);
62                 fd++;
63         }
64         
65         cstring = BLI_dynstr_get_cstring(dynstr);
66         BLI_dynstr_free(dynstr);
67         return cstring;
68 }
69
70 int BPY_flag_from_seq(BPY_flag_def *flagdef, PyObject *seq, int *flag)
71 {
72         int i, error_val= 0;
73         char *cstring;
74         PyObject *item;
75         BPY_flag_def *fd;
76
77         if (PySequence_Check(seq)) {
78                 i= PySequence_Length(seq);
79
80                 while(i--) {
81                         item = PySequence_ITEM(seq, i);
82                         cstring= _PyUnicode_AsString(item);
83                         if(cstring) {
84                                 fd= flagdef;
85                                 while(fd->name) {
86                                         if (strcmp(cstring, fd->name) == 0)
87                                                 (*flag) |= fd->flag;
88                                         fd++;
89                                 }
90                                 if (fd==NULL) { /* could not find a match */
91                                         error_val= 1;
92                                 }
93                         } else {
94                                 error_val= 1;
95                         }
96                         Py_DECREF(item);
97                 }
98         }
99         else {
100                 error_val= 1;
101         }
102
103         if (error_val) {
104                 char *buf = bpy_flag_error_str(flagdef);
105                 PyErr_SetString(PyExc_AttributeError, buf);
106                 MEM_freeN(buf);
107                 return -1; /* error value */
108         }
109
110         return 0; /* ok */
111 }
112
113
114 /* Copied from pythons 3's Object.c */
115 #ifndef Py_CmpToRich
116 PyObject *
117 Py_CmpToRich(int op, int cmp)
118 {
119         PyObject *res;
120         int ok;
121
122         if (PyErr_Occurred())
123                 return NULL;
124         switch (op) {
125         case Py_LT:
126                 ok = cmp <  0;
127                 break;
128         case Py_LE:
129                 ok = cmp <= 0;
130                 break;
131         case Py_EQ:
132                 ok = cmp == 0;
133                 break;
134         case Py_NE:
135                 ok = cmp != 0;
136                 break;
137         case Py_GT:
138                 ok = cmp >  0;
139                 break;
140         case Py_GE:
141                 ok = cmp >= 0;
142                 break;
143         default:
144                 PyErr_BadArgument();
145                 return NULL;
146         }
147         res = ok ? Py_True : Py_False;
148         Py_INCREF(res);
149         return res;
150 }
151 #endif
152
153 /* for debugging */
154 void PyObSpit(char *name, PyObject *var) {
155         fprintf(stderr, "<%s> : ", name);
156         if (var==NULL) {
157                 fprintf(stderr, "<NIL>");
158         }
159         else {
160                 PyObject_Print(var, stderr, 0);
161         }
162         fprintf(stderr, "\n");
163 }
164
165 void PyLineSpit(void) {
166         char *filename;
167         int lineno;
168
169         PyErr_Clear();
170         BPY_getFileAndNum(&filename, &lineno);
171         
172         fprintf(stderr, "%s:%d\n", filename, lineno);
173 }
174
175 void BPY_getFileAndNum(char **filename, int *lineno)
176 {
177         PyObject *getframe, *frame;
178         PyObject *f_lineno= NULL, *co_filename= NULL;
179         
180         if (filename)   *filename= NULL;
181         if (lineno)             *lineno = -1;
182         
183         getframe = PySys_GetObject("_getframe"); // borrowed
184         if (getframe==NULL) {
185                 return;
186         }
187         
188         frame = PyObject_CallObject(getframe, NULL);
189         if (frame==NULL)
190                 return;
191         
192         if (filename) {
193                 co_filename= PyObject_GetAttrStringArgs(frame, 1, "f_code", "co_filename");
194                 if (co_filename==NULL) {
195                         PyErr_SetString(PyExc_SystemError, "Could not access sys._getframe().f_code.co_filename");
196                         Py_DECREF(frame);
197                         return;
198                 }
199                 
200                 *filename = _PyUnicode_AsString(co_filename);
201                 Py_DECREF(co_filename);
202         }
203         
204         if (lineno) {
205                 f_lineno= PyObject_GetAttrString(frame, "f_lineno");
206                 if (f_lineno==NULL) {
207                         PyErr_SetString(PyExc_SystemError, "Could not access sys._getframe().f_lineno");
208                         Py_DECREF(frame);
209                         return;
210                 }
211                 
212                 *lineno = (int)PyLong_AsSsize_t(f_lineno);
213                 Py_DECREF(f_lineno);
214         }
215 }
216
217 /* Would be nice if python had this built in */
218 PyObject *PyObject_GetAttrStringArgs(PyObject *o, Py_ssize_t n, ...)
219 {
220         Py_ssize_t i;
221         PyObject *item= o;
222         char *attr;
223         
224         va_list vargs;
225
226         va_start(vargs, n);
227         for (i=0; i<n; i++) {
228                 attr = va_arg(vargs, char *);
229                 item = PyObject_GetAttrString(item, attr);
230                 
231                 if (item) 
232                         Py_DECREF(item);
233                 else /* python will set the error value here */
234                         break;
235                 
236         }
237         va_end(vargs);
238         
239         Py_INCREF(item); /* final value has is increfed, to match PyObject_GetAttrString */
240         return item;
241 }