comparison src/if_py_both.h @ 4659:80b0081824fa v7.3.1077

updated for version 7.3.1077 Problem: Python: Allocating dict the wrong way, causing a crash. Solution: Use py_dict_alloc(). Fix some exception problems. (ZyX)
author Bram Moolenaar <bram@vim.org>
date Thu, 30 May 2013 22:06:33 +0200
parents b943fd24c351
children 9e7ef781d494
comparison
equal deleted inserted replaced
4658:3be23951aa25 4659:80b0081824fa
24 #endif 24 #endif
25 #define DOPY_FUNC "_vim_pydo" 25 #define DOPY_FUNC "_vim_pydo"
26 26
27 #define PyErr_SetVim(str) PyErr_SetString(VimError, str) 27 #define PyErr_SetVim(str) PyErr_SetString(VimError, str)
28 28
29 #define RAISE_NO_EMPTY_KEYS PyErr_SetString(PyExc_ValueError, \
30 _("empty keys are not allowed"))
31
29 #define INVALID_BUFFER_VALUE ((buf_T *)(-1)) 32 #define INVALID_BUFFER_VALUE ((buf_T *)(-1))
30 #define INVALID_WINDOW_VALUE ((win_T *)(-1)) 33 #define INVALID_WINDOW_VALUE ((win_T *)(-1))
31 #define INVALID_TABPAGE_VALUE ((tabpage_T *)(-1)) 34 #define INVALID_TABPAGE_VALUE ((tabpage_T *)(-1))
32
33 #define DICTKEY_DECL \
34 PyObject *dictkey_todecref = NULL;
35 #define DICTKEY_GET(err, decref) \
36 if (!(key = StringToChars(keyObject, &dictkey_todecref))) \
37 { \
38 if (decref) \
39 { \
40 Py_DECREF(keyObject); \
41 } \
42 return err; \
43 } \
44 if (decref && !dictkey_todecref) \
45 dictkey_todecref = keyObject; \
46 if (*key == NUL) \
47 { \
48 PyErr_SetString(PyExc_ValueError, _("empty keys are not allowed")); \
49 return err; \
50 }
51 #define DICTKEY_UNREF \
52 Py_XDECREF(dictkey_todecref);
53 35
54 typedef void (*rangeinitializer)(void *); 36 typedef void (*rangeinitializer)(void *);
55 typedef void (*runner)(const char *, void * 37 typedef void (*runner)(const char *, void *
56 #ifdef PY_CAN_RECURSE 38 #ifdef PY_CAN_RECURSE
57 , PyGILState_STATE * 39 , PyGILState_STATE *
1014 PyObject *r; 996 PyObject *r;
1015 char_u *key; 997 char_u *key;
1016 dictitem_T *di; 998 dictitem_T *di;
1017 dict_T *dict = self->dict; 999 dict_T *dict = self->dict;
1018 hashitem_T *hi; 1000 hashitem_T *hi;
1019 1001 PyObject *todecref;
1020 DICTKEY_DECL
1021 1002
1022 if (flags & DICT_FLAG_HAS_DEFAULT) 1003 if (flags & DICT_FLAG_HAS_DEFAULT)
1023 { 1004 {
1024 if (!PyArg_ParseTuple(args, "O|O", &keyObject, &defObject)) 1005 if (!PyArg_ParseTuple(args, "O|O", &keyObject, &defObject))
1025 return NULL; 1006 return NULL;
1028 keyObject = args; 1009 keyObject = args;
1029 1010
1030 if (flags & DICT_FLAG_RETURN_BOOL) 1011 if (flags & DICT_FLAG_RETURN_BOOL)
1031 defObject = Py_False; 1012 defObject = Py_False;
1032 1013
1033 DICTKEY_GET(NULL, 0) 1014 if (!(key = StringToChars(keyObject, &todecref)))
1015 return NULL;
1016
1017 if (*key == NUL)
1018 {
1019 RAISE_NO_EMPTY_KEYS;
1020 return NULL;
1021 }
1034 1022
1035 hi = hash_find(&dict->dv_hashtab, key); 1023 hi = hash_find(&dict->dv_hashtab, key);
1036 1024
1037 DICTKEY_UNREF 1025 Py_XDECREF(todecref);
1038 1026
1039 if (HASHITEM_EMPTY(hi)) 1027 if (HASHITEM_EMPTY(hi))
1040 { 1028 {
1041 if (defObject) 1029 if (defObject)
1042 { 1030 {
1171 { 1159 {
1172 char_u *key; 1160 char_u *key;
1173 typval_T tv; 1161 typval_T tv;
1174 dict_T *dict = self->dict; 1162 dict_T *dict = self->dict;
1175 dictitem_T *di; 1163 dictitem_T *di;
1176 DICTKEY_DECL 1164 PyObject *todecref;
1177 1165
1178 if (dict->dv_lock) 1166 if (dict->dv_lock)
1179 { 1167 {
1180 PyErr_SetVim(_("dict is locked")); 1168 PyErr_SetVim(_("dict is locked"));
1181 return -1; 1169 return -1;
1182 } 1170 }
1183 1171
1184 DICTKEY_GET(-1, 0) 1172 if (!(key = StringToChars(keyObject, &todecref)))
1173 return -1;
1174 if (*key == NUL)
1175 {
1176 RAISE_NO_EMPTY_KEYS;
1177 return -1;
1178 }
1185 1179
1186 di = dict_find(dict, key, -1); 1180 di = dict_find(dict, key, -1);
1187 1181
1188 if (valObject == NULL) 1182 if (valObject == NULL)
1189 { 1183 {
1190 hashitem_T *hi; 1184 hashitem_T *hi;
1191 1185
1192 if (di == NULL) 1186 if (di == NULL)
1193 { 1187 {
1194 DICTKEY_UNREF 1188 Py_XDECREF(todecref);
1195 PyErr_SetObject(PyExc_KeyError, keyObject); 1189 PyErr_SetObject(PyExc_KeyError, keyObject);
1196 return -1; 1190 return -1;
1197 } 1191 }
1198 hi = hash_find(&dict->dv_hashtab, di->di_key); 1192 hi = hash_find(&dict->dv_hashtab, di->di_key);
1199 hash_remove(&dict->dv_hashtab, hi); 1193 hash_remove(&dict->dv_hashtab, hi);
1206 1200
1207 if (di == NULL) 1201 if (di == NULL)
1208 { 1202 {
1209 if (!(di = dictitem_alloc(key))) 1203 if (!(di = dictitem_alloc(key)))
1210 { 1204 {
1205 Py_XDECREF(todecref);
1211 PyErr_NoMemory(); 1206 PyErr_NoMemory();
1212 return -1; 1207 return -1;
1213 } 1208 }
1214 di->di_tv.v_lock = 0; 1209 di->di_tv.v_lock = 0;
1215 di->di_tv.v_type = VAR_UNKNOWN; 1210 di->di_tv.v_type = VAR_UNKNOWN;
1216 1211
1217 if (dict_add(dict, di) == FAIL) 1212 if (dict_add(dict, di) == FAIL)
1218 { 1213 {
1219 DICTKEY_UNREF 1214 Py_XDECREF(todecref);
1220 vim_free(di); 1215 vim_free(di);
1221 dictitem_free(di); 1216 dictitem_free(di);
1222 PyErr_SetVim(_("failed to add key to dictionary")); 1217 PyErr_SetVim(_("failed to add key to dictionary"));
1223 return -1; 1218 return -1;
1224 } 1219 }
1225 } 1220 }
1226 else 1221 else
1227 clear_tv(&di->di_tv); 1222 clear_tv(&di->di_tv);
1228 1223
1229 DICTKEY_UNREF 1224 Py_XDECREF(todecref);
1230 1225
1231 copy_tv(&tv, &di->di_tv); 1226 copy_tv(&tv, &di->di_tv);
1232 clear_tv(&tv); 1227 clear_tv(&tv);
1233 return 0; 1228 return 0;
1234 } 1229 }
2200 { 2195 {
2201 char_u *key; 2196 char_u *key;
2202 int flags; 2197 int flags;
2203 long numval; 2198 long numval;
2204 char_u *stringval; 2199 char_u *stringval;
2205 DICTKEY_DECL 2200 PyObject *todecref;
2206 2201
2207 if (self->Check(self->from)) 2202 if (self->Check(self->from))
2208 return NULL; 2203 return NULL;
2209 2204
2210 DICTKEY_GET(NULL, 0) 2205 if (!(key = StringToChars(keyObject, &todecref)))
2206 return NULL;
2207 if (*key == NUL)
2208 {
2209 RAISE_NO_EMPTY_KEYS;
2210 return NULL;
2211 }
2211 2212
2212 flags = get_option_value_strict(key, &numval, &stringval, 2213 flags = get_option_value_strict(key, &numval, &stringval,
2213 self->opt_type, self->from); 2214 self->opt_type, self->from);
2214 2215
2215 DICTKEY_UNREF 2216 Py_XDECREF(todecref);
2216 2217
2217 if (flags == 0) 2218 if (flags == 0)
2218 { 2219 {
2219 PyErr_SetObject(PyExc_KeyError, keyObject); 2220 PyErr_SetObject(PyExc_KeyError, keyObject);
2220 return NULL; 2221 return NULL;
2327 { 2328 {
2328 char_u *key; 2329 char_u *key;
2329 int flags; 2330 int flags;
2330 int opt_flags; 2331 int opt_flags;
2331 int r = 0; 2332 int r = 0;
2332 DICTKEY_DECL 2333 PyObject *todecref;
2333 2334
2334 if (self->Check(self->from)) 2335 if (self->Check(self->from))
2335 return -1; 2336 return -1;
2336 2337
2337 DICTKEY_GET(-1, 0) 2338 if (!(key = StringToChars(keyObject, &todecref)))
2339 return -1;
2340 if (*key == NUL)
2341 {
2342 RAISE_NO_EMPTY_KEYS;
2343 return -1;
2344 }
2338 2345
2339 flags = get_option_value_strict(key, NULL, NULL, 2346 flags = get_option_value_strict(key, NULL, NULL,
2340 self->opt_type, self->from); 2347 self->opt_type, self->from);
2341 2348
2342 if (flags == 0) 2349 if (flags == 0)
2343 { 2350 {
2344 PyErr_SetObject(PyExc_KeyError, keyObject); 2351 PyErr_SetObject(PyExc_KeyError, keyObject);
2345 DICTKEY_UNREF 2352 Py_XDECREF(todecref);
2346 return -1; 2353 return -1;
2347 } 2354 }
2348 2355
2349 if (valObject == NULL) 2356 if (valObject == NULL)
2350 { 2357 {
2351 if (self->opt_type == SREQ_GLOBAL) 2358 if (self->opt_type == SREQ_GLOBAL)
2352 { 2359 {
2353 PyErr_SetString(PyExc_ValueError, 2360 PyErr_SetString(PyExc_ValueError,
2354 _("unable to unset global option")); 2361 _("unable to unset global option"));
2355 DICTKEY_UNREF 2362 Py_XDECREF(todecref);
2356 return -1; 2363 return -1;
2357 } 2364 }
2358 else if (!(flags & SOPT_GLOBAL)) 2365 else if (!(flags & SOPT_GLOBAL))
2359 { 2366 {
2360 PyErr_SetString(PyExc_ValueError, _("unable to unset option " 2367 PyErr_SetString(PyExc_ValueError, _("unable to unset option "
2361 "without global value")); 2368 "without global value"));
2362 DICTKEY_UNREF 2369 Py_XDECREF(todecref);
2363 return -1; 2370 return -1;
2364 } 2371 }
2365 else 2372 else
2366 { 2373 {
2367 unset_global_local_option(key, self->from); 2374 unset_global_local_option(key, self->from);
2368 DICTKEY_UNREF 2375 Py_XDECREF(todecref);
2369 return 0; 2376 return 0;
2370 } 2377 }
2371 } 2378 }
2372 2379
2373 opt_flags = (self->opt_type ? OPT_LOCAL : OPT_GLOBAL); 2380 opt_flags = (self->opt_type ? OPT_LOCAL : OPT_GLOBAL);
2394 if (PyLong_Check(valObject)) 2401 if (PyLong_Check(valObject))
2395 val = PyLong_AsLong(valObject); 2402 val = PyLong_AsLong(valObject);
2396 else 2403 else
2397 { 2404 {
2398 PyErr_SetString(PyExc_TypeError, _("object must be integer")); 2405 PyErr_SetString(PyExc_TypeError, _("object must be integer"));
2399 DICTKEY_UNREF 2406 Py_XDECREF(todecref);
2400 return -1; 2407 return -1;
2401 } 2408 }
2402 2409
2403 r = set_option_value_for(key, val, NULL, opt_flags, 2410 r = set_option_value_for(key, val, NULL, opt_flags,
2404 self->opt_type, self->from); 2411 self->opt_type, self->from);
2416 } 2423 }
2417 else 2424 else
2418 r = -1; 2425 r = -1;
2419 } 2426 }
2420 2427
2421 DICTKEY_UNREF 2428 Py_XDECREF(todecref);
2422 2429
2423 return r; 2430 return r;
2424 } 2431 }
2425 2432
2426 static PyMappingMethods OptionsAsMapping = { 2433 static PyMappingMethods OptionsAsMapping = {
4526 dictitem_T *di; 4533 dictitem_T *di;
4527 PyObject *keyObject; 4534 PyObject *keyObject;
4528 PyObject *valObject; 4535 PyObject *valObject;
4529 Py_ssize_t iter = 0; 4536 Py_ssize_t iter = 0;
4530 4537
4531 if (!(dict = dict_alloc())) 4538 if (!(dict = py_dict_alloc()))
4532 return -1; 4539 return -1;
4533 4540
4534 tv->v_type = VAR_DICT; 4541 tv->v_type = VAR_DICT;
4535 tv->vval.v_dict = dict; 4542 tv->vval.v_dict = dict;
4536 4543
4551 } 4558 }
4552 if (*key == NUL) 4559 if (*key == NUL)
4553 { 4560 {
4554 dict_unref(dict); 4561 dict_unref(dict);
4555 Py_XDECREF(todecref); 4562 Py_XDECREF(todecref);
4563 RAISE_NO_EMPTY_KEYS;
4556 return -1; 4564 return -1;
4557 } 4565 }
4558 4566
4559 di = dictitem_alloc(key); 4567 di = dictitem_alloc(key);
4560 4568
4598 PyObject *list; 4606 PyObject *list;
4599 PyObject *iterator; 4607 PyObject *iterator;
4600 PyObject *keyObject; 4608 PyObject *keyObject;
4601 PyObject *valObject; 4609 PyObject *valObject;
4602 4610
4603 if (!(dict = dict_alloc())) 4611 if (!(dict = py_dict_alloc()))
4604 return -1; 4612 return -1;
4605 4613
4606 tv->v_type = VAR_DICT; 4614 tv->v_type = VAR_DICT;
4607 tv->vval.v_dict = dict; 4615 tv->vval.v_dict = dict;
4608 4616
4635 { 4643 {
4636 Py_DECREF(keyObject); 4644 Py_DECREF(keyObject);
4637 Py_DECREF(iterator); 4645 Py_DECREF(iterator);
4638 Py_XDECREF(todecref); 4646 Py_XDECREF(todecref);
4639 dict_unref(dict); 4647 dict_unref(dict);
4648 RAISE_NO_EMPTY_KEYS;
4640 return -1; 4649 return -1;
4641 } 4650 }
4642 4651
4643 if (!(valObject = PyObject_GetItem(obj, keyObject))) 4652 if (!(valObject = PyObject_GetItem(obj, keyObject)))
4644 { 4653 {