comparison src/if_py_both.h @ 4970:f5c822e5a0eb v7.3.1230

updated for version 7.3.1230 Problem: Python: Exception messages are not clear. Solution: Make exception messages more verbose. (ZyX)
author Bram Moolenaar <bram@vim.org>
date Sun, 23 Jun 2013 13:46:40 +0200
parents b6e693e1f946
children 537bbfff0c5c
comparison
equal deleted inserted replaced
4969:dbbe87b3c213 4970:f5c822e5a0eb
27 static const char *vim_special_path = "_vim_path_"; 27 static const char *vim_special_path = "_vim_path_";
28 28
29 #define PyErr_SET_STRING(exc, str) PyErr_SetString(exc, _(str)) 29 #define PyErr_SET_STRING(exc, str) PyErr_SetString(exc, _(str))
30 #define PyErr_SetVim(str) PyErr_SetString(VimError, str) 30 #define PyErr_SetVim(str) PyErr_SetString(VimError, str)
31 #define PyErr_SET_VIM(str) PyErr_SET_STRING(VimError, str) 31 #define PyErr_SET_VIM(str) PyErr_SET_STRING(VimError, str)
32 #define PyErr_FORMAT(exc, str, tail) PyErr_Format(exc, _(str), tail)
33 #define PyErr_VIM_FORMAT(str, tail) PyErr_FORMAT(VimError, str, tail)
34
35 #define Py_TYPE_NAME(obj) (obj->ob_type->tp_name == NULL \
36 ? "(NULL)" \
37 : obj->ob_type->tp_name)
32 38
33 #define RAISE_NO_EMPTY_KEYS PyErr_SET_STRING(PyExc_ValueError, \ 39 #define RAISE_NO_EMPTY_KEYS PyErr_SET_STRING(PyExc_ValueError, \
34 "empty keys are not allowed") 40 "empty keys are not allowed")
41 #define RAISE_LOCKED(type) PyErr_SET_VIM(_(type " is locked"))
42 #define RAISE_LOCKED_DICTIONARY RAISE_LOCKED("dictionary")
43 #define RAISE_LOCKED_LIST RAISE_LOCKED("list")
44 #define RAISE_UNDO_FAIL PyErr_SET_VIM("cannot save undo information")
45 #define RAISE_LINE_FAIL(act) PyErr_SET_VIM("cannot " act " line")
46 #define RAISE_KEY_ADD_FAIL(key) \
47 PyErr_VIM_FORMAT("failed to add key '%s' to dictionary", key)
48 #define RAISE_INVALID_INDEX_TYPE(idx) \
49 PyErr_FORMAT(PyExc_TypeError, "index must be int or slice, not %s", \
50 Py_TYPE_NAME(idx));
35 51
36 #define INVALID_BUFFER_VALUE ((buf_T *)(-1)) 52 #define INVALID_BUFFER_VALUE ((buf_T *)(-1))
37 #define INVALID_WINDOW_VALUE ((win_T *)(-1)) 53 #define INVALID_WINDOW_VALUE ((win_T *)(-1))
38 #define INVALID_TABPAGE_VALUE ((tabpage_T *)(-1)) 54 #define INVALID_TABPAGE_VALUE ((tabpage_T *)(-1))
39 55
120 136
121 *todecref = bytes; 137 *todecref = bytes;
122 } 138 }
123 else 139 else
124 { 140 {
125 PyErr_SET_STRING(PyExc_TypeError, "object must be string"); 141 PyErr_FORMAT(PyExc_TypeError,
142 #if PY_MAJOR_VERSION < 3
143 "expected str() or unicode() instance, but got %s"
144 #else
145 "expected bytes() or str() instance, but got %s"
146 #endif
147 , Py_TYPE_NAME(object));
126 return NULL; 148 return NULL;
127 } 149 }
128 150
129 return (char_u *) p; 151 return (char_u *) p;
130 } 152 }
229 251
230 self->softspace = PyInt_AsLong(val); 252 self->softspace = PyInt_AsLong(val);
231 return 0; 253 return 0;
232 } 254 }
233 255
234 PyErr_SET_STRING(PyExc_AttributeError, "invalid attribute"); 256 PyErr_FORMAT(PyExc_AttributeError, "invalid attribute: %s", name);
235 return -1; 257 return -1;
236 } 258 }
237 259
238 /* Buffer IO, we write one whole line at a time. */ 260 /* Buffer IO, we write one whole line at a time. */
239 static garray_T io_ga = {0, 0, 1, 80, NULL}; 261 static garray_T io_ga = {0, 0, 1, 80, NULL};
965 static PyObject * 987 static PyObject *
966 call_load_module(char *name, int len, PyObject *find_module_result) 988 call_load_module(char *name, int len, PyObject *find_module_result)
967 { 989 {
968 PyObject *fd, *pathname, *description; 990 PyObject *fd, *pathname, *description;
969 991
970 if (!PyTuple_Check(find_module_result) 992 if (!PyTuple_Check(find_module_result))
971 || PyTuple_GET_SIZE(find_module_result) != 3) 993 {
972 { 994 PyErr_FORMAT(PyExc_TypeError,
973 PyErr_SET_STRING(PyExc_TypeError, 995 "expected 3-tuple as imp.find_module() result, but got %s",
974 "expected 3-tuple as imp.find_module() result"); 996 Py_TYPE_NAME(find_module_result));
997 return NULL;
998 }
999 if (PyTuple_GET_SIZE(find_module_result) != 3)
1000 {
1001 PyErr_FORMAT(PyExc_TypeError,
1002 "expected 3-tuple as imp.find_module() result, but got "
1003 "tuple of size %d",
1004 (int) PyTuple_GET_SIZE(find_module_result));
975 return NULL; 1005 return NULL;
976 } 1006 }
977 1007
978 if (!(fd = PyTuple_GET_ITEM(find_module_result, 0)) 1008 if (!(fd = PyTuple_GET_ITEM(find_module_result, 0))
979 || !(pathname = PyTuple_GET_ITEM(find_module_result, 1)) 1009 || !(pathname = PyTuple_GET_ITEM(find_module_result, 1))
1375 } 1405 }
1376 return 0; 1406 return 0;
1377 } 1407 }
1378 else 1408 else
1379 { 1409 {
1380 PyErr_SET_STRING(PyExc_AttributeError, "cannot set this attribute"); 1410 PyErr_FORMAT(PyExc_AttributeError, "cannot set attribute %s", name);
1381 return -1; 1411 return -1;
1382 } 1412 }
1383 } 1413 }
1384 1414
1385 static PyInt 1415 static PyInt
1457 1487
1458 if (flags & DICT_FLAG_POP) 1488 if (flags & DICT_FLAG_POP)
1459 { 1489 {
1460 if (dict->dv_lock) 1490 if (dict->dv_lock)
1461 { 1491 {
1462 PyErr_SET_VIM("dict is locked"); 1492 RAISE_LOCKED_DICTIONARY;
1463 Py_DECREF(r); 1493 Py_DECREF(r);
1464 return NULL; 1494 return NULL;
1465 } 1495 }
1466 1496
1467 hash_remove(&dict->dv_hashtab, hi); 1497 hash_remove(&dict->dv_hashtab, hi);
1560 dictitem_T *di; 1590 dictitem_T *di;
1561 PyObject *todecref; 1591 PyObject *todecref;
1562 1592
1563 if (dict->dv_lock) 1593 if (dict->dv_lock)
1564 { 1594 {
1565 PyErr_SET_VIM("dict is locked"); 1595 RAISE_LOCKED_DICTIONARY;
1566 return -1; 1596 return -1;
1567 } 1597 }
1568 1598
1569 if (!(key = StringToChars(keyObject, &todecref))) 1599 if (!(key = StringToChars(keyObject, &todecref)))
1570 return -1; 1600 return -1;
1612 di->di_tv.v_lock = 0; 1642 di->di_tv.v_lock = 0;
1613 di->di_tv.v_type = VAR_UNKNOWN; 1643 di->di_tv.v_type = VAR_UNKNOWN;
1614 1644
1615 if (dict_add(dict, di) == FAIL) 1645 if (dict_add(dict, di) == FAIL)
1616 { 1646 {
1617 Py_XDECREF(todecref);
1618 vim_free(di); 1647 vim_free(di);
1619 dictitem_free(di); 1648 dictitem_free(di);
1620 PyErr_SET_VIM("failed to add key to dictionary"); 1649 RAISE_KEY_ADD_FAIL(key);
1650 Py_XDECREF(todecref);
1621 return -1; 1651 return -1;
1622 } 1652 }
1623 } 1653 }
1624 else 1654 else
1625 clear_tv(&di->di_tv); 1655 clear_tv(&di->di_tv);
1723 { 1753 {
1724 dict_T *dict = self->dict; 1754 dict_T *dict = self->dict;
1725 1755
1726 if (dict->dv_lock) 1756 if (dict->dv_lock)
1727 { 1757 {
1728 PyErr_SET_VIM("dict is locked"); 1758 RAISE_LOCKED_DICTIONARY;
1729 return NULL; 1759 return NULL;
1730 } 1760 }
1731 1761
1732 if (kwargs) 1762 if (kwargs)
1733 { 1763 {
1779 1809
1780 if (PySequence_Fast_GET_SIZE(fast) != 2) 1810 if (PySequence_Fast_GET_SIZE(fast) != 2)
1781 { 1811 {
1782 Py_DECREF(iterator); 1812 Py_DECREF(iterator);
1783 Py_DECREF(fast); 1813 Py_DECREF(fast);
1784 PyErr_SET_STRING(PyExc_ValueError, 1814 PyErr_FORMAT(PyExc_ValueError,
1785 "expected sequence element of size 2"); 1815 "expected sequence element of size 2, "
1816 "but got sequence of size %d",
1817 PySequence_Fast_GET_SIZE(fast));
1786 return NULL; 1818 return NULL;
1787 } 1819 }
1788 1820
1789 keyObject = PySequence_Fast_GET_ITEM(fast, 0); 1821 keyObject = PySequence_Fast_GET_ITEM(fast, 0);
1790 1822
1821 1853
1822 Py_DECREF(fast); 1854 Py_DECREF(fast);
1823 1855
1824 if (dict_add(dict, di) == FAIL) 1856 if (dict_add(dict, di) == FAIL)
1825 { 1857 {
1858 RAISE_KEY_ADD_FAIL(di->di_key);
1826 Py_DECREF(iterator); 1859 Py_DECREF(iterator);
1827 dictitem_free(di); 1860 dictitem_free(di);
1828 PyErr_SET_VIM("failed to add key to dictionary");
1829 return NULL; 1861 return NULL;
1830 } 1862 }
1831 } 1863 }
1832 1864
1833 Py_DECREF(iterator); 1865 Py_DECREF(iterator);
2083 return NULL; 2115 return NULL;
2084 } 2116 }
2085 li = list_find(self->list, (long) index); 2117 li = list_find(self->list, (long) index);
2086 if (li == NULL) 2118 if (li == NULL)
2087 { 2119 {
2088 PyErr_SET_VIM("internal error: failed to get vim list item"); 2120 /* No more suitable format specifications in python-2.3 */
2121 PyErr_VIM_FORMAT("internal error: failed to get vim list item %d",
2122 (int) index);
2089 return NULL; 2123 return NULL;
2090 } 2124 }
2091 return ConvertToPyObject(&li->li_tv); 2125 return ConvertToPyObject(&li->li_tv);
2092 } 2126 }
2093 2127
2196 listitem_T *li; 2230 listitem_T *li;
2197 Py_ssize_t length = ListLength(self); 2231 Py_ssize_t length = ListLength(self);
2198 2232
2199 if (l->lv_lock) 2233 if (l->lv_lock)
2200 { 2234 {
2201 PyErr_SET_VIM("list is locked"); 2235 RAISE_LOCKED_LIST;
2202 return -1; 2236 return -1;
2203 } 2237 }
2204 if (index>length || (index==length && obj==NULL)) 2238 if (index>length || (index==length && obj==NULL))
2205 { 2239 {
2206 PyErr_SET_STRING(PyExc_IndexError, "list index out of range"); 2240 PyErr_SET_STRING(PyExc_IndexError, "list index out of range");
2250 list_T *l = self->list; 2284 list_T *l = self->list;
2251 PyInt i; 2285 PyInt i;
2252 2286
2253 if (l->lv_lock) 2287 if (l->lv_lock)
2254 { 2288 {
2255 PyErr_SET_VIM("list is locked"); 2289 RAISE_LOCKED_LIST;
2256 return -1; 2290 return -1;
2257 } 2291 }
2258 2292
2259 PROC_RANGE 2293 PROC_RANGE
2260 2294
2263 else 2297 else
2264 { 2298 {
2265 li = list_find(l, (long) first); 2299 li = list_find(l, (long) first);
2266 if (li == NULL) 2300 if (li == NULL)
2267 { 2301 {
2268 PyErr_SET_VIM("internal error: no vim list item"); 2302 PyErr_VIM_FORMAT("internal error: no vim list item %d", (int)first);
2269 return -1; 2303 return -1;
2270 } 2304 }
2271 if (last > first) 2305 if (last > first)
2272 { 2306 {
2273 i = last - first; 2307 i = last - first;
2313 list_T *l = self->list; 2347 list_T *l = self->list;
2314 PyObject *lookup_dict; 2348 PyObject *lookup_dict;
2315 2349
2316 if (l->lv_lock) 2350 if (l->lv_lock)
2317 { 2351 {
2318 PyErr_SET_VIM("list is locked"); 2352 RAISE_LOCKED_LIST;
2319 return NULL; 2353 return NULL;
2320 } 2354 }
2321 2355
2322 if (!(lookup_dict = PyDict_New())) 2356 if (!(lookup_dict = PyDict_New()))
2323 return NULL; 2357 return NULL;
2373 } 2407 }
2374 return 0; 2408 return 0;
2375 } 2409 }
2376 else 2410 else
2377 { 2411 {
2378 PyErr_SET_STRING(PyExc_AttributeError, "cannot set this attribute"); 2412 PyErr_FORMAT(PyExc_AttributeError, "cannot set attribute %s", name);
2379 return -1; 2413 return -1;
2380 } 2414 }
2381 } 2415 }
2382 2416
2383 static struct PyMethodDef ListMethods[] = { 2417 static struct PyMethodDef ListMethods[] = {
2408 2442
2409 if (isdigit(*name)) 2443 if (isdigit(*name))
2410 { 2444 {
2411 if (!translated_function_exists(name)) 2445 if (!translated_function_exists(name))
2412 { 2446 {
2413 PyErr_SET_STRING(PyExc_ValueError, 2447 PyErr_FORMAT(PyExc_ValueError,
2414 "unnamed function does not exist"); 2448 "unnamed function %s does not exist", name);
2415 return NULL; 2449 return NULL;
2416 } 2450 }
2417 self->name = vim_strsave(name); 2451 self->name = vim_strsave(name);
2418 func_ref(self->name); 2452 func_ref(self->name);
2419 } 2453 }
2420 else 2454 else
2421 if ((self->name = get_expanded_name(name, 2455 if ((self->name = get_expanded_name(name,
2422 vim_strchr(name, AUTOLOAD_CHAR) == NULL)) 2456 vim_strchr(name, AUTOLOAD_CHAR) == NULL))
2423 == NULL) 2457 == NULL)
2424 { 2458 {
2425 PyErr_SET_STRING(PyExc_ValueError, "function does not exist"); 2459 PyErr_FORMAT(PyExc_ValueError, "function %s does not exist", name);
2426 return NULL; 2460 return NULL;
2427 } 2461 }
2428 2462
2429 return (PyObject *)(self); 2463 return (PyObject *)(self);
2430 } 2464 }
2513 if (VimTryEnd()) 2547 if (VimTryEnd())
2514 result = NULL; 2548 result = NULL;
2515 else if (error != OK) 2549 else if (error != OK)
2516 { 2550 {
2517 result = NULL; 2551 result = NULL;
2518 PyErr_SET_VIM("failed to run function"); 2552 PyErr_VIM_FORMAT("failed to run function %s", (char *)name);
2519 } 2553 }
2520 else 2554 else
2521 result = ConvertToPyObject(&rettv); 2555 result = ConvertToPyObject(&rettv);
2522 2556
2523 clear_tv(&args); 2557 clear_tv(&args);
2768 2802
2769 if (valObject == NULL) 2803 if (valObject == NULL)
2770 { 2804 {
2771 if (self->opt_type == SREQ_GLOBAL) 2805 if (self->opt_type == SREQ_GLOBAL)
2772 { 2806 {
2773 PyErr_SET_STRING(PyExc_ValueError, "unable to unset global option"); 2807 PyErr_FORMAT(PyExc_ValueError,
2808 "unable to unset global option %s", key);
2774 Py_XDECREF(todecref); 2809 Py_XDECREF(todecref);
2775 return -1; 2810 return -1;
2776 } 2811 }
2777 else if (!(flags & SOPT_GLOBAL)) 2812 else if (!(flags & SOPT_GLOBAL))
2778 { 2813 {
2779 PyErr_SET_STRING(PyExc_ValueError, "unable to unset option " 2814 PyErr_FORMAT(PyExc_ValueError,
2780 "without global value"); 2815 "unable to unset option %s "
2816 "which does not have global value", key);
2781 Py_XDECREF(todecref); 2817 Py_XDECREF(todecref);
2782 return -1; 2818 return -1;
2783 } 2819 }
2784 else 2820 else
2785 { 2821 {
3193 if (CheckWindow(self)) 3229 if (CheckWindow(self))
3194 return -1; 3230 return -1;
3195 3231
3196 if (strcmp(name, "buffer") == 0) 3232 if (strcmp(name, "buffer") == 0)
3197 { 3233 {
3198 PyErr_SET_STRING(PyExc_TypeError, "readonly attribute"); 3234 PyErr_SET_STRING(PyExc_TypeError, "readonly attribute: buffer");
3199 return -1; 3235 return -1;
3200 } 3236 }
3201 else if (strcmp(name, "cursor") == 0) 3237 else if (strcmp(name, "cursor") == 0)
3202 { 3238 {
3203 long lnum; 3239 long lnum;
3556 switch_buffer(&savebuf, buf); 3592 switch_buffer(&savebuf, buf);
3557 3593
3558 VimTryStart(); 3594 VimTryStart();
3559 3595
3560 if (u_savedel((linenr_T)n, 1L) == FAIL) 3596 if (u_savedel((linenr_T)n, 1L) == FAIL)
3561 PyErr_SET_VIM("cannot save undo information"); 3597 RAISE_UNDO_FAIL;
3562 else if (ml_delete((linenr_T)n, FALSE) == FAIL) 3598 else if (ml_delete((linenr_T)n, FALSE) == FAIL)
3563 PyErr_SET_VIM("cannot delete line"); 3599 RAISE_LINE_FAIL("delete");
3564 else 3600 else
3565 { 3601 {
3566 if (buf == savebuf) 3602 if (buf == savebuf)
3567 py_fix_cursor((linenr_T)n, (linenr_T)n + 1, (linenr_T)-1); 3603 py_fix_cursor((linenr_T)n, (linenr_T)n + 1, (linenr_T)-1);
3568 deleted_lines_mark((linenr_T)n, 1L); 3604 deleted_lines_mark((linenr_T)n, 1L);
3592 PyErr_Clear(); 3628 PyErr_Clear();
3593 switch_buffer(&savebuf, buf); 3629 switch_buffer(&savebuf, buf);
3594 3630
3595 if (u_savesub((linenr_T)n) == FAIL) 3631 if (u_savesub((linenr_T)n) == FAIL)
3596 { 3632 {
3597 PyErr_SET_VIM("cannot save undo information"); 3633 RAISE_UNDO_FAIL;
3598 vim_free(save); 3634 vim_free(save);
3599 } 3635 }
3600 else if (ml_replace((linenr_T)n, (char_u *)save, FALSE) == FAIL) 3636 else if (ml_replace((linenr_T)n, (char_u *)save, FALSE) == FAIL)
3601 { 3637 {
3602 PyErr_SET_VIM("cannot replace line"); 3638 RAISE_LINE_FAIL("replace");
3603 vim_free(save); 3639 vim_free(save);
3604 } 3640 }
3605 else 3641 else
3606 changed_bytes((linenr_T)n, 0); 3642 changed_bytes((linenr_T)n, 0);
3607 3643
3652 PyErr_Clear(); 3688 PyErr_Clear();
3653 VimTryStart(); 3689 VimTryStart();
3654 switch_buffer(&savebuf, buf); 3690 switch_buffer(&savebuf, buf);
3655 3691
3656 if (u_savedel((linenr_T)lo, (long)n) == FAIL) 3692 if (u_savedel((linenr_T)lo, (long)n) == FAIL)
3657 PyErr_SET_VIM("cannot save undo information"); 3693 RAISE_UNDO_FAIL;
3658 else 3694 else
3659 { 3695 {
3660 for (i = 0; i < n; ++i) 3696 for (i = 0; i < n; ++i)
3661 { 3697 {
3662 if (ml_delete((linenr_T)lo, FALSE) == FAIL) 3698 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
3663 { 3699 {
3664 PyErr_SET_VIM("cannot delete line"); 3700 RAISE_LINE_FAIL("delete");
3665 break; 3701 break;
3666 } 3702 }
3667 } 3703 }
3668 if (buf == savebuf) 3704 if (buf == savebuf)
3669 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)-n); 3705 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)-n);
3720 3756
3721 /* START of region without "return". Must call restore_buffer()! */ 3757 /* START of region without "return". Must call restore_buffer()! */
3722 switch_buffer(&savebuf, buf); 3758 switch_buffer(&savebuf, buf);
3723 3759
3724 if (u_save((linenr_T)(lo-1), (linenr_T)hi) == FAIL) 3760 if (u_save((linenr_T)(lo-1), (linenr_T)hi) == FAIL)
3725 PyErr_SET_VIM("cannot save undo information"); 3761 RAISE_UNDO_FAIL;
3726 3762
3727 /* If the size of the range is reducing (ie, new_len < old_len) we 3763 /* If the size of the range is reducing (ie, new_len < old_len) we
3728 * need to delete some old_len. We do this at the start, by 3764 * need to delete some old_len. We do this at the start, by
3729 * repeatedly deleting line "lo". 3765 * repeatedly deleting line "lo".
3730 */ 3766 */
3731 if (!PyErr_Occurred()) 3767 if (!PyErr_Occurred())
3732 { 3768 {
3733 for (i = 0; i < old_len - new_len; ++i) 3769 for (i = 0; i < old_len - new_len; ++i)
3734 if (ml_delete((linenr_T)lo, FALSE) == FAIL) 3770 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
3735 { 3771 {
3736 PyErr_SET_VIM("cannot delete line"); 3772 RAISE_LINE_FAIL("delete");
3737 break; 3773 break;
3738 } 3774 }
3739 extra -= i; 3775 extra -= i;
3740 } 3776 }
3741 3777
3747 { 3783 {
3748 for (i = 0; i < old_len && i < new_len; ++i) 3784 for (i = 0; i < old_len && i < new_len; ++i)
3749 if (ml_replace((linenr_T)(lo+i), (char_u *)array[i], FALSE) 3785 if (ml_replace((linenr_T)(lo+i), (char_u *)array[i], FALSE)
3750 == FAIL) 3786 == FAIL)
3751 { 3787 {
3752 PyErr_SET_VIM("cannot replace line"); 3788 RAISE_LINE_FAIL("replace");
3753 break; 3789 break;
3754 } 3790 }
3755 } 3791 }
3756 else 3792 else
3757 i = 0; 3793 i = 0;
3765 while (i < new_len) 3801 while (i < new_len)
3766 { 3802 {
3767 if (ml_append((linenr_T)(lo + i - 1), 3803 if (ml_append((linenr_T)(lo + i - 1),
3768 (char_u *)array[i], 0, FALSE) == FAIL) 3804 (char_u *)array[i], 0, FALSE) == FAIL)
3769 { 3805 {
3770 PyErr_SET_VIM("cannot insert line"); 3806 RAISE_LINE_FAIL("insert");
3771 break; 3807 break;
3772 } 3808 }
3773 vim_free(array[i]); 3809 vim_free(array[i]);
3774 ++i; 3810 ++i;
3775 ++extra; 3811 ++extra;
3842 PyErr_Clear(); 3878 PyErr_Clear();
3843 VimTryStart(); 3879 VimTryStart();
3844 switch_buffer(&savebuf, buf); 3880 switch_buffer(&savebuf, buf);
3845 3881
3846 if (u_save((linenr_T)n, (linenr_T)(n+1)) == FAIL) 3882 if (u_save((linenr_T)n, (linenr_T)(n+1)) == FAIL)
3847 PyErr_SET_VIM("cannot save undo information"); 3883 RAISE_UNDO_FAIL;
3848 else if (ml_append((linenr_T)n, (char_u *)str, 0, FALSE) == FAIL) 3884 else if (ml_append((linenr_T)n, (char_u *)str, 0, FALSE) == FAIL)
3849 PyErr_SET_VIM("cannot insert line"); 3885 RAISE_LINE_FAIL("insert");
3850 else 3886 else
3851 appended_lines_mark((linenr_T)n, 1L); 3887 appended_lines_mark((linenr_T)n, 1L);
3852 3888
3853 vim_free(str); 3889 vim_free(str);
3854 restore_buffer(savebuf); 3890 restore_buffer(savebuf);
3893 PyErr_Clear(); 3929 PyErr_Clear();
3894 VimTryStart(); 3930 VimTryStart();
3895 switch_buffer(&savebuf, buf); 3931 switch_buffer(&savebuf, buf);
3896 3932
3897 if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL) 3933 if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL)
3898 PyErr_SET_VIM("cannot save undo information"); 3934 RAISE_UNDO_FAIL;
3899 else 3935 else
3900 { 3936 {
3901 for (i = 0; i < size; ++i) 3937 for (i = 0; i < size; ++i)
3902 { 3938 {
3903 if (ml_append((linenr_T)(n + i), 3939 if (ml_append((linenr_T)(n + i),
3904 (char_u *)array[i], 0, FALSE) == FAIL) 3940 (char_u *)array[i], 0, FALSE) == FAIL)
3905 { 3941 {
3906 PyErr_SET_VIM("cannot insert line"); 3942 RAISE_LINE_FAIL("insert");
3907 3943
3908 /* Free the rest of the lines */ 3944 /* Free the rest of the lines */
3909 while (i < size) 3945 while (i < size)
3910 vim_free(array[i++]); 3946 vim_free(array[i++]);
3911 3947
4666 { 4702 {
4667 int count; 4703 int count;
4668 4704
4669 if (value->ob_type != &BufferType) 4705 if (value->ob_type != &BufferType)
4670 { 4706 {
4671 PyErr_SET_STRING(PyExc_TypeError, "expected vim.Buffer object"); 4707 PyErr_FORMAT(PyExc_TypeError,
4708 "expected vim.Buffer object, but got %s",
4709 Py_TYPE_NAME(value));
4672 return -1; 4710 return -1;
4673 } 4711 }
4674 4712
4675 if (CheckBuffer((BufferObject *)(value))) 4713 if (CheckBuffer((BufferObject *)(value)))
4676 return -1; 4714 return -1;
4679 VimTryStart(); 4717 VimTryStart();
4680 if (do_buffer(DOBUF_GOTO, DOBUF_FIRST, FORWARD, count, 0) == FAIL) 4718 if (do_buffer(DOBUF_GOTO, DOBUF_FIRST, FORWARD, count, 0) == FAIL)
4681 { 4719 {
4682 if (VimTryEnd()) 4720 if (VimTryEnd())
4683 return -1; 4721 return -1;
4684 PyErr_SET_VIM("failed to switch to given buffer"); 4722 PyErr_VIM_FORMAT("failed to switch to buffer %d", count);
4685 return -1; 4723 return -1;
4686 } 4724 }
4687 4725
4688 return VimTryEnd(); 4726 return VimTryEnd();
4689 } 4727 }
4691 { 4729 {
4692 int count; 4730 int count;
4693 4731
4694 if (value->ob_type != &WindowType) 4732 if (value->ob_type != &WindowType)
4695 { 4733 {
4696 PyErr_SET_STRING(PyExc_TypeError, "expected vim.Window object"); 4734 PyErr_FORMAT(PyExc_TypeError,
4735 "expected vim.Window object, but got %s",
4736 Py_TYPE_NAME(value));
4697 return -1; 4737 return -1;
4698 } 4738 }
4699 4739
4700 if (CheckWindow((WindowObject *)(value))) 4740 if (CheckWindow((WindowObject *)(value)))
4701 return -1; 4741 return -1;
4723 } 4763 }
4724 else if (strcmp(name, "tabpage") == 0) 4764 else if (strcmp(name, "tabpage") == 0)
4725 { 4765 {
4726 if (value->ob_type != &TabPageType) 4766 if (value->ob_type != &TabPageType)
4727 { 4767 {
4728 PyErr_SET_STRING(PyExc_TypeError, "expected vim.TabPage object"); 4768 PyErr_FORMAT(PyExc_TypeError,
4769 "expected vim.TabPage object, but got %s",
4770 Py_TYPE_NAME(value));
4729 return -1; 4771 return -1;
4730 } 4772 }
4731 4773
4732 if (CheckTabPage((TabPageObject *)(value))) 4774 if (CheckTabPage((TabPageObject *)(value)))
4733 return -1; 4775 return -1;
5001 return -1; 5043 return -1;
5002 } 5044 }
5003 5045
5004 if (dict_add(dict, di) == FAIL) 5046 if (dict_add(dict, di) == FAIL)
5005 { 5047 {
5048 RAISE_KEY_ADD_FAIL(di->di_key);
5006 clear_tv(&di->di_tv); 5049 clear_tv(&di->di_tv);
5007 vim_free(di); 5050 vim_free(di);
5008 dict_unref(dict); 5051 dict_unref(dict);
5009 PyErr_SET_VIM("failed to add key to dictionary");
5010 return -1; 5052 return -1;
5011 } 5053 }
5012 } 5054 }
5013 5055
5014 --dict->dv_refcount; 5056 --dict->dv_refcount;
5103 5145
5104 Py_DECREF(valObject); 5146 Py_DECREF(valObject);
5105 5147
5106 if (dict_add(dict, di) == FAIL) 5148 if (dict_add(dict, di) == FAIL)
5107 { 5149 {
5150 RAISE_KEY_ADD_FAIL(di->di_key);
5108 Py_DECREF(iterator); 5151 Py_DECREF(iterator);
5109 dictitem_free(di); 5152 dictitem_free(di);
5110 dict_unref(dict); 5153 dict_unref(dict);
5111 PyErr_SET_VIM("failed to add key to dictionary");
5112 return -1; 5154 return -1;
5113 } 5155 }
5114 } 5156 }
5115 Py_DECREF(iterator); 5157 Py_DECREF(iterator);
5116 --dict->dv_refcount; 5158 --dict->dv_refcount;
5214 r = convert_dl(obj, tv, pydict_to_tv, lookup_dict); 5256 r = convert_dl(obj, tv, pydict_to_tv, lookup_dict);
5215 else if (PyMapping_Check(obj)) 5257 else if (PyMapping_Check(obj))
5216 r = convert_dl(obj, tv, pymap_to_tv, lookup_dict); 5258 r = convert_dl(obj, tv, pymap_to_tv, lookup_dict);
5217 else 5259 else
5218 { 5260 {
5219 PyErr_SET_STRING(PyExc_TypeError, 5261 PyErr_FORMAT(PyExc_TypeError,
5220 "unable to convert object to vim dictionary"); 5262 "unable to convert %s to vim dictionary",
5263 Py_TYPE_NAME(obj));
5221 r = -1; 5264 r = -1;
5222 } 5265 }
5223 Py_DECREF(lookup_dict); 5266 Py_DECREF(lookup_dict);
5224 return r; 5267 return r;
5225 } 5268 }
5324 return convert_dl(obj, tv, pyseq_to_tv, lookup_dict); 5367 return convert_dl(obj, tv, pyseq_to_tv, lookup_dict);
5325 else if (PyMapping_Check(obj)) 5368 else if (PyMapping_Check(obj))
5326 return convert_dl(obj, tv, pymap_to_tv, lookup_dict); 5369 return convert_dl(obj, tv, pymap_to_tv, lookup_dict);
5327 else 5370 else
5328 { 5371 {
5329 PyErr_SET_STRING(PyExc_TypeError, 5372 PyErr_FORMAT(PyExc_TypeError,
5330 "unable to convert to vim structure"); 5373 "unable to convert %s to vim structure",
5374 Py_TYPE_NAME(obj));
5331 return -1; 5375 return -1;
5332 } 5376 }
5333 return 0; 5377 return 0;
5334 } 5378 }
5335 5379
5336 static PyObject * 5380 static PyObject *
5337 ConvertToPyObject(typval_T *tv) 5381 ConvertToPyObject(typval_T *tv)
5338 { 5382 {
5339 if (tv == NULL) 5383 if (tv == NULL)
5340 { 5384 {
5341 PyErr_SET_VIM("NULL reference passed"); 5385 PyErr_SET_VIM("internal error: NULL reference passed");
5342 return NULL; 5386 return NULL;
5343 } 5387 }
5344 switch (tv->v_type) 5388 switch (tv->v_type)
5345 { 5389 {
5346 case VAR_STRING: 5390 case VAR_STRING: