comparison src/if_py_both.h @ 4498:ef02f32d8e53 v7.3.997

updated for version 7.3.997 Problem: Vim and Python exceptions are different. Solution: Make Vim exceptions be Python exceptions. (ZyX)
author Bram Moolenaar <bram@vim.org>
date Tue, 21 May 2013 20:40:40 +0200
parents ebd94eabfd80
children 47e6dec5ce3c
comparison
equal deleted inserted replaced
4497:51bae43f1787 4498:ef02f32d8e53
270 static PyObject *VimError; 270 static PyObject *VimError;
271 271
272 /* Check to see whether a Vim error has been reported, or a keyboard 272 /* Check to see whether a Vim error has been reported, or a keyboard
273 * interrupt has been detected. 273 * interrupt has been detected.
274 */ 274 */
275
276 static void
277 VimTryStart(void)
278 {
279 ++trylevel;
280 }
281
275 static int 282 static int
276 VimErrorCheck(void) 283 VimTryEnd(void)
277 { 284 {
285 --trylevel;
278 if (got_int) 286 if (got_int)
279 { 287 {
280 PyErr_SetNone(PyExc_KeyboardInterrupt); 288 PyErr_SetNone(PyExc_KeyboardInterrupt);
281 return 1; 289 return 1;
282 } 290 }
283 else if (did_emsg && !PyErr_Occurred()) 291 else if (!did_throw)
284 { 292 return 0;
285 PyErr_SetNone(VimError); 293 else if (PyErr_Occurred())
286 return 1; 294 return 1;
287 } 295 else
288 296 {
297 PyErr_SetVim((char *) current_exception->value);
298 discard_current_exception();
299 return 1;
300 }
301 }
302
303 static int
304 VimCheckInterrupt(void)
305 {
306 if (got_int)
307 {
308 PyErr_SetNone(PyExc_KeyboardInterrupt);
309 return 1;
310 }
289 return 0; 311 return 0;
290 } 312 }
291 313
292 /* Vim module - Implementation 314 /* Vim module - Implementation
293 */ 315 */
304 PyErr_Clear(); 326 PyErr_Clear();
305 327
306 Py_BEGIN_ALLOW_THREADS 328 Py_BEGIN_ALLOW_THREADS
307 Python_Lock_Vim(); 329 Python_Lock_Vim();
308 330
331 VimTryStart();
309 do_cmdline_cmd((char_u *)cmd); 332 do_cmdline_cmd((char_u *)cmd);
310 update_screen(VALID); 333 update_screen(VALID);
311 334
312 Python_Release_Vim(); 335 Python_Release_Vim();
313 Py_END_ALLOW_THREADS 336 Py_END_ALLOW_THREADS
314 337
315 if (VimErrorCheck()) 338 if (VimTryEnd())
316 result = NULL; 339 result = NULL;
317 else 340 else
318 result = Py_None; 341 result = Py_None;
342
319 343
320 Py_XINCREF(result); 344 Py_XINCREF(result);
321 return result; 345 return result;
322 } 346 }
323 347
447 if (!PyArg_ParseTuple(args, "s", &expr)) 471 if (!PyArg_ParseTuple(args, "s", &expr))
448 return NULL; 472 return NULL;
449 473
450 Py_BEGIN_ALLOW_THREADS 474 Py_BEGIN_ALLOW_THREADS
451 Python_Lock_Vim(); 475 Python_Lock_Vim();
476 VimTryStart();
452 our_tv = eval_expr((char_u *)expr, NULL); 477 our_tv = eval_expr((char_u *)expr, NULL);
453
454 Python_Release_Vim(); 478 Python_Release_Vim();
455 Py_END_ALLOW_THREADS 479 Py_END_ALLOW_THREADS
480
481 if (VimTryEnd())
482 return NULL;
456 483
457 if (our_tv == NULL) 484 if (our_tv == NULL)
458 { 485 {
459 PyErr_SetVim(_("invalid expression")); 486 PyErr_SetVim(_("invalid expression"));
460 return NULL; 487 return NULL;
488 if (!PyArg_ParseTuple(args, "s", &expr)) 515 if (!PyArg_ParseTuple(args, "s", &expr))
489 return NULL; 516 return NULL;
490 517
491 Py_BEGIN_ALLOW_THREADS 518 Py_BEGIN_ALLOW_THREADS
492 Python_Lock_Vim(); 519 Python_Lock_Vim();
520 VimTryStart();
493 our_tv = eval_expr((char_u *)expr, NULL); 521 our_tv = eval_expr((char_u *)expr, NULL);
494
495 Python_Release_Vim(); 522 Python_Release_Vim();
496 Py_END_ALLOW_THREADS 523 Py_END_ALLOW_THREADS
524
525 if (VimTryEnd())
526 return NULL;
497 527
498 if (our_tv == NULL) 528 if (our_tv == NULL)
499 { 529 {
500 PyErr_SetVim(_("invalid expression")); 530 PyErr_SetVim(_("invalid expression"));
501 return NULL; 531 return NULL;
1322 } 1352 }
1323 1353
1324 Py_BEGIN_ALLOW_THREADS 1354 Py_BEGIN_ALLOW_THREADS
1325 Python_Lock_Vim(); 1355 Python_Lock_Vim();
1326 1356
1357 VimTryStart();
1327 error = func_call(name, &args, selfdict, &rettv); 1358 error = func_call(name, &args, selfdict, &rettv);
1328 1359
1329 Python_Release_Vim(); 1360 Python_Release_Vim();
1330 Py_END_ALLOW_THREADS 1361 Py_END_ALLOW_THREADS
1331 1362
1332 if (error != OK) 1363 if (VimTryEnd())
1364 result = NULL;
1365 else if (error != OK)
1333 { 1366 {
1334 result = NULL; 1367 result = NULL;
1335 PyErr_SetVim(_("failed to run function")); 1368 PyErr_SetVim(_("failed to run function"));
1336 } 1369 }
1337 else 1370 else
1484 void *from; 1517 void *from;
1485 { 1518 {
1486 win_T *save_curwin; 1519 win_T *save_curwin;
1487 tabpage_T *save_curtab; 1520 tabpage_T *save_curtab;
1488 buf_T *save_curbuf; 1521 buf_T *save_curbuf;
1489 int r = 0; 1522
1490 1523 VimTryStart();
1491 switch (opt_type) 1524 switch (opt_type)
1492 { 1525 {
1493 case SREQ_WIN: 1526 case SREQ_WIN:
1494 if (switch_win(&save_curwin, &save_curtab, (win_T *)from, 1527 if (switch_win(&save_curwin, &save_curtab, (win_T *)from,
1495 win_find_tabpage((win_T *)from)) == FAIL) 1528 win_find_tabpage((win_T *)from)) == FAIL)
1496 { 1529 {
1530 if (VimTryEnd())
1531 return -1;
1497 PyErr_SetVim("Problem while switching windows."); 1532 PyErr_SetVim("Problem while switching windows.");
1498 return -1; 1533 return -1;
1499 } 1534 }
1500 set_option_value(key, numval, stringval, opt_flags); 1535 set_option_value(key, numval, stringval, opt_flags);
1501 restore_win(save_curwin, save_curtab); 1536 restore_win(save_curwin, save_curtab);
1507 break; 1542 break;
1508 case SREQ_GLOBAL: 1543 case SREQ_GLOBAL:
1509 set_option_value(key, numval, stringval, opt_flags); 1544 set_option_value(key, numval, stringval, opt_flags);
1510 break; 1545 break;
1511 } 1546 }
1512 return r; 1547 return VimTryEnd();
1513 } 1548 }
1514 1549
1515 static int 1550 static int
1516 OptionsAssItem(OptionsObject *self, PyObject *keyObject, PyObject *valObject) 1551 OptionsAssItem(OptionsObject *self, PyObject *keyObject, PyObject *valObject)
1517 { 1552 {
1959 PyErr_SetVim(_("cursor position outside buffer")); 1994 PyErr_SetVim(_("cursor position outside buffer"));
1960 return -1; 1995 return -1;
1961 } 1996 }
1962 1997
1963 /* Check for keyboard interrupts */ 1998 /* Check for keyboard interrupts */
1964 if (VimErrorCheck()) 1999 if (VimCheckInterrupt())
1965 return -1; 2000 return -1;
1966 2001
1967 self->win->w_cursor.lnum = lnum; 2002 self->win->w_cursor.lnum = lnum;
1968 self->win->w_cursor.col = col; 2003 self->win->w_cursor.col = col;
1969 #ifdef FEAT_VIRTUALEDIT 2004 #ifdef FEAT_VIRTUALEDIT
1986 #ifdef FEAT_GUI 2021 #ifdef FEAT_GUI
1987 need_mouse_correct = TRUE; 2022 need_mouse_correct = TRUE;
1988 #endif 2023 #endif
1989 savewin = curwin; 2024 savewin = curwin;
1990 curwin = self->win; 2025 curwin = self->win;
2026
2027 VimTryStart();
1991 win_setheight(height); 2028 win_setheight(height);
1992 curwin = savewin; 2029 curwin = savewin;
1993 2030 if (VimTryEnd())
1994 /* Check for keyboard interrupts */
1995 if (VimErrorCheck())
1996 return -1; 2031 return -1;
1997 2032
1998 return 0; 2033 return 0;
1999 } 2034 }
2000 #ifdef FEAT_VERTSPLIT 2035 #ifdef FEAT_VERTSPLIT
2009 #ifdef FEAT_GUI 2044 #ifdef FEAT_GUI
2010 need_mouse_correct = TRUE; 2045 need_mouse_correct = TRUE;
2011 #endif 2046 #endif
2012 savewin = curwin; 2047 savewin = curwin;
2013 curwin = self->win; 2048 curwin = self->win;
2049
2050 VimTryStart();
2014 win_setwidth(width); 2051 win_setwidth(width);
2015 curwin = savewin; 2052 curwin = savewin;
2016 2053 if (VimTryEnd())
2017 /* Check for keyboard interrupts */
2018 if (VimErrorCheck())
2019 return -1; 2054 return -1;
2020 2055
2021 return 0; 2056 return 0;
2022 } 2057 }
2023 #endif 2058 #endif
2302 buf_T *savebuf; 2337 buf_T *savebuf;
2303 2338
2304 PyErr_Clear(); 2339 PyErr_Clear();
2305 switch_buffer(&savebuf, buf); 2340 switch_buffer(&savebuf, buf);
2306 2341
2342 VimTryStart();
2343
2307 if (u_savedel((linenr_T)n, 1L) == FAIL) 2344 if (u_savedel((linenr_T)n, 1L) == FAIL)
2308 PyErr_SetVim(_("cannot save undo information")); 2345 PyErr_SetVim(_("cannot save undo information"));
2309 else if (ml_delete((linenr_T)n, FALSE) == FAIL) 2346 else if (ml_delete((linenr_T)n, FALSE) == FAIL)
2310 PyErr_SetVim(_("cannot delete line")); 2347 PyErr_SetVim(_("cannot delete line"));
2311 else 2348 else
2315 deleted_lines_mark((linenr_T)n, 1L); 2352 deleted_lines_mark((linenr_T)n, 1L);
2316 } 2353 }
2317 2354
2318 restore_buffer(savebuf); 2355 restore_buffer(savebuf);
2319 2356
2320 if (PyErr_Occurred() || VimErrorCheck()) 2357 if (VimTryEnd())
2321 return FAIL; 2358 return FAIL;
2322 2359
2323 if (len_change) 2360 if (len_change)
2324 *len_change = -1; 2361 *len_change = -1;
2325 2362
2330 char *save = StringToLine(line); 2367 char *save = StringToLine(line);
2331 buf_T *savebuf; 2368 buf_T *savebuf;
2332 2369
2333 if (save == NULL) 2370 if (save == NULL)
2334 return FAIL; 2371 return FAIL;
2372
2373 VimTryStart();
2335 2374
2336 /* We do not need to free "save" if ml_replace() consumes it. */ 2375 /* We do not need to free "save" if ml_replace() consumes it. */
2337 PyErr_Clear(); 2376 PyErr_Clear();
2338 switch_buffer(&savebuf, buf); 2377 switch_buffer(&savebuf, buf);
2339 2378
2354 2393
2355 /* Check that the cursor is not beyond the end of the line now. */ 2394 /* Check that the cursor is not beyond the end of the line now. */
2356 if (buf == savebuf) 2395 if (buf == savebuf)
2357 check_cursor_col(); 2396 check_cursor_col();
2358 2397
2359 if (PyErr_Occurred() || VimErrorCheck()) 2398 if (VimTryEnd())
2360 return FAIL; 2399 return FAIL;
2361 2400
2362 if (len_change) 2401 if (len_change)
2363 *len_change = 0; 2402 *len_change = 0;
2364 2403
2393 PyInt i; 2432 PyInt i;
2394 PyInt n = (int)(hi - lo); 2433 PyInt n = (int)(hi - lo);
2395 buf_T *savebuf; 2434 buf_T *savebuf;
2396 2435
2397 PyErr_Clear(); 2436 PyErr_Clear();
2437 VimTryStart();
2398 switch_buffer(&savebuf, buf); 2438 switch_buffer(&savebuf, buf);
2399 2439
2400 if (u_savedel((linenr_T)lo, (long)n) == FAIL) 2440 if (u_savedel((linenr_T)lo, (long)n) == FAIL)
2401 PyErr_SetVim(_("cannot save undo information")); 2441 PyErr_SetVim(_("cannot save undo information"));
2402 else 2442 else
2414 deleted_lines_mark((linenr_T)lo, (long)i); 2454 deleted_lines_mark((linenr_T)lo, (long)i);
2415 } 2455 }
2416 2456
2417 restore_buffer(savebuf); 2457 restore_buffer(savebuf);
2418 2458
2419 if (PyErr_Occurred() || VimErrorCheck()) 2459 if (VimTryEnd())
2420 return FAIL; 2460 return FAIL;
2421 2461
2422 if (len_change) 2462 if (len_change)
2423 *len_change = -n; 2463 *len_change = -n;
2424 2464
2457 vim_free(array); 2497 vim_free(array);
2458 return FAIL; 2498 return FAIL;
2459 } 2499 }
2460 } 2500 }
2461 2501
2502 VimTryStart();
2462 PyErr_Clear(); 2503 PyErr_Clear();
2463 2504
2464 // START of region without "return". Must call restore_buffer()! 2505 // START of region without "return". Must call restore_buffer()!
2465 switch_buffer(&savebuf, buf); 2506 switch_buffer(&savebuf, buf);
2466 2507
2543 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)extra); 2584 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)extra);
2544 2585
2545 // END of region without "return". 2586 // END of region without "return".
2546 restore_buffer(savebuf); 2587 restore_buffer(savebuf);
2547 2588
2548 if (PyErr_Occurred() || VimErrorCheck()) 2589 if (VimTryEnd())
2549 return FAIL; 2590 return FAIL;
2550 2591
2551 if (len_change) 2592 if (len_change)
2552 *len_change = new_len - old_len; 2593 *len_change = new_len - old_len;
2553 2594
2581 2622
2582 if (str == NULL) 2623 if (str == NULL)
2583 return FAIL; 2624 return FAIL;
2584 2625
2585 PyErr_Clear(); 2626 PyErr_Clear();
2627 VimTryStart();
2586 switch_buffer(&savebuf, buf); 2628 switch_buffer(&savebuf, buf);
2587 2629
2588 if (u_save((linenr_T)n, (linenr_T)(n+1)) == FAIL) 2630 if (u_save((linenr_T)n, (linenr_T)(n+1)) == FAIL)
2589 PyErr_SetVim(_("cannot save undo information")); 2631 PyErr_SetVim(_("cannot save undo information"));
2590 else if (ml_append((linenr_T)n, (char_u *)str, 0, FALSE) == FAIL) 2632 else if (ml_append((linenr_T)n, (char_u *)str, 0, FALSE) == FAIL)
2594 2636
2595 vim_free(str); 2637 vim_free(str);
2596 restore_buffer(savebuf); 2638 restore_buffer(savebuf);
2597 update_screen(VALID); 2639 update_screen(VALID);
2598 2640
2599 if (PyErr_Occurred() || VimErrorCheck()) 2641 if (VimTryEnd())
2600 return FAIL; 2642 return FAIL;
2601 2643
2602 if (len_change) 2644 if (len_change)
2603 *len_change = 1; 2645 *len_change = 1;
2604 2646
2631 return FAIL; 2673 return FAIL;
2632 } 2674 }
2633 } 2675 }
2634 2676
2635 PyErr_Clear(); 2677 PyErr_Clear();
2678 VimTryStart();
2636 switch_buffer(&savebuf, buf); 2679 switch_buffer(&savebuf, buf);
2637 2680
2638 if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL) 2681 if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL)
2639 PyErr_SetVim(_("cannot save undo information")); 2682 PyErr_SetVim(_("cannot save undo information"));
2640 else 2683 else
2664 vim_free(array); 2707 vim_free(array);
2665 2708
2666 restore_buffer(savebuf); 2709 restore_buffer(savebuf);
2667 update_screen(VALID); 2710 update_screen(VALID);
2668 2711
2669 if (PyErr_Occurred() || VimErrorCheck()) 2712 if (VimTryEnd())
2670 return FAIL; 2713 return FAIL;
2671 2714
2672 if (len_change) 2715 if (len_change)
2673 *len_change = size; 2716 *len_change = size;
2674 2717
2894 } 2937 }
2895 2938
2896 static void 2939 static void
2897 RangeDestructor(RangeObject *self) 2940 RangeDestructor(RangeObject *self)
2898 { 2941 {
2899 Py_DECREF(self->buf); 2942 Py_XDECREF(self->buf);
2900 DESTRUCTOR_FINISH(self); 2943 DESTRUCTOR_FINISH(self);
2901 } 2944 }
2902 2945
2903 static PyInt 2946 static PyInt
2904 RangeLength(RangeObject *self) 2947 RangeLength(RangeObject *self)
3076 3119
3077 if (!PyArg_ParseTuple(args, "s", &pmark)) 3120 if (!PyArg_ParseTuple(args, "s", &pmark))
3078 return NULL; 3121 return NULL;
3079 mark = *pmark; 3122 mark = *pmark;
3080 3123
3124 VimTryStart();
3081 switch_buffer(&savebuf, self->buf); 3125 switch_buffer(&savebuf, self->buf);
3082 posp = getmark(mark, FALSE); 3126 posp = getmark(mark, FALSE);
3083 restore_buffer(savebuf); 3127 restore_buffer(savebuf);
3128 if (VimTryEnd())
3129 return NULL;
3084 3130
3085 if (posp == NULL) 3131 if (posp == NULL)
3086 { 3132 {
3087 PyErr_SetVim(_("invalid mark name")); 3133 PyErr_SetVim(_("invalid mark name"));
3088 return NULL; 3134 return NULL;
3089 } 3135 }
3090
3091 /* Check for keyboard interrupt */
3092 if (VimErrorCheck())
3093 return NULL;
3094 3136
3095 if (posp->lnum <= 0) 3137 if (posp->lnum <= 0)
3096 { 3138 {
3097 /* Or raise an error? */ 3139 /* Or raise an error? */
3098 Py_INCREF(Py_None); 3140 Py_INCREF(Py_None);
3328 3370
3329 if (CheckBuffer((BufferObject *)(value))) 3371 if (CheckBuffer((BufferObject *)(value)))
3330 return -1; 3372 return -1;
3331 count = ((BufferObject *)(value))->buf->b_fnum; 3373 count = ((BufferObject *)(value))->buf->b_fnum;
3332 3374
3375 VimTryStart();
3333 if (do_buffer(DOBUF_GOTO, DOBUF_FIRST, FORWARD, count, 0) == FAIL) 3376 if (do_buffer(DOBUF_GOTO, DOBUF_FIRST, FORWARD, count, 0) == FAIL)
3334 { 3377 {
3378 if (VimTryEnd())
3379 return -1;
3335 PyErr_SetVim(_("failed to switch to given buffer")); 3380 PyErr_SetVim(_("failed to switch to given buffer"));
3336 return -1; 3381 return -1;
3337 } 3382 }
3338 3383
3339 return 0; 3384 return VimTryEnd();
3340 } 3385 }
3341 else if (strcmp(name, "window") == 0) 3386 else if (strcmp(name, "window") == 0)
3342 { 3387 {
3343 int count; 3388 int count;
3344 3389
3357 PyErr_SetString(PyExc_ValueError, 3402 PyErr_SetString(PyExc_ValueError,
3358 _("failed to find window in the current tab page")); 3403 _("failed to find window in the current tab page"));
3359 return -1; 3404 return -1;
3360 } 3405 }
3361 3406
3407 VimTryStart();
3362 win_goto(((WindowObject *)(value))->win); 3408 win_goto(((WindowObject *)(value))->win);
3363 if (((WindowObject *)(value))->win != curwin) 3409 if (((WindowObject *)(value))->win != curwin)
3364 { 3410 {
3411 if (VimTryEnd())
3412 return -1;
3365 PyErr_SetString(PyExc_RuntimeError, 3413 PyErr_SetString(PyExc_RuntimeError,
3366 _("did not switch to the specified window")); 3414 _("did not switch to the specified window"));
3367 return -1; 3415 return -1;
3368 } 3416 }
3369 3417
3370 return 0; 3418 return VimTryEnd();
3371 } 3419 }
3372 else if (strcmp(name, "tabpage") == 0) 3420 else if (strcmp(name, "tabpage") == 0)
3373 { 3421 {
3374 if (value->ob_type != &TabPageType) 3422 if (value->ob_type != &TabPageType)
3375 { 3423 {
3378 } 3426 }
3379 3427
3380 if (CheckTabPage((TabPageObject *)(value))) 3428 if (CheckTabPage((TabPageObject *)(value)))
3381 return -1; 3429 return -1;
3382 3430
3431 VimTryStart();
3383 goto_tabpage_tp(((TabPageObject *)(value))->tab, TRUE, TRUE); 3432 goto_tabpage_tp(((TabPageObject *)(value))->tab, TRUE, TRUE);
3384 if (((TabPageObject *)(value))->tab != curtab) 3433 if (((TabPageObject *)(value))->tab != curtab)
3385 { 3434 {
3435 if (VimTryEnd())
3436 return -1;
3386 PyErr_SetString(PyExc_RuntimeError, 3437 PyErr_SetString(PyExc_RuntimeError,
3387 _("did not switch to the specified tab page")); 3438 _("did not switch to the specified tab page"));
3388 return -1; 3439 return -1;
3389 } 3440 }
3390 3441
3391 return 0; 3442 return VimTryEnd();
3392 } 3443 }
3393 else 3444 else
3394 { 3445 {
3395 PyErr_SetString(PyExc_AttributeError, name); 3446 PyErr_SetString(PyExc_AttributeError, name);
3396 return -1; 3447 return -1;