Mercurial > vim
comparison src/if_py_both.h @ 4964:5cee875f3096 v7.3.1227
updated for version 7.3.1227
Problem: Inconsistent string conversion.
Solution: Use 'encoding' instead of utf-8. Use METH_O in place of
METH_VARARGS where appropriate. (ZyX)
author | Bram Moolenaar <bram@vim.org> |
---|---|
date | Sun, 23 Jun 2013 13:00:44 +0200 |
parents | b34d719b13cd |
children | 620d9b59d4ed |
comparison
equal
deleted
inserted
replaced
4963:bb1c0f320e74 | 4964:5cee875f3096 |
---|---|
464 | 464 |
465 /* Vim module - Implementation | 465 /* Vim module - Implementation |
466 */ | 466 */ |
467 | 467 |
468 static PyObject * | 468 static PyObject * |
469 VimCommand(PyObject *self UNUSED, PyObject *args) | 469 VimCommand(PyObject *self UNUSED, PyObject *string) |
470 { | 470 { |
471 char *cmd; | 471 char_u *cmd; |
472 PyObject *result; | 472 PyObject *result; |
473 | 473 PyObject *todecref; |
474 if (!PyArg_ParseTuple(args, "s", &cmd)) | 474 |
475 return NULL; | 475 if (!(cmd = StringToChars(string, &todecref))) |
476 | 476 return NULL; |
477 PyErr_Clear(); | |
478 | 477 |
479 Py_BEGIN_ALLOW_THREADS | 478 Py_BEGIN_ALLOW_THREADS |
480 Python_Lock_Vim(); | 479 Python_Lock_Vim(); |
481 | 480 |
482 VimTryStart(); | 481 VimTryStart(); |
483 do_cmdline_cmd((char_u *)cmd); | 482 do_cmdline_cmd(cmd); |
484 update_screen(VALID); | 483 update_screen(VALID); |
485 | 484 |
486 Python_Release_Vim(); | 485 Python_Release_Vim(); |
487 Py_END_ALLOW_THREADS | 486 Py_END_ALLOW_THREADS |
488 | 487 |
489 if (VimTryEnd()) | 488 if (VimTryEnd()) |
490 result = NULL; | 489 result = NULL; |
491 else | 490 else |
492 result = Py_None; | 491 result = Py_None; |
493 | 492 |
494 | |
495 Py_XINCREF(result); | 493 Py_XINCREF(result); |
494 Py_XDECREF(todecref); | |
496 return result; | 495 return result; |
497 } | 496 } |
498 | 497 |
499 /* | 498 /* |
500 * Function to translate a typval_T into a PyObject; this will recursively | 499 * Function to translate a typval_T into a PyObject; this will recursively |
639 } | 638 } |
640 | 639 |
641 static PyObject * | 640 static PyObject * |
642 VimEval(PyObject *self UNUSED, PyObject *args) | 641 VimEval(PyObject *self UNUSED, PyObject *args) |
643 { | 642 { |
644 char *expr; | 643 char_u *expr; |
645 typval_T *our_tv; | 644 typval_T *our_tv; |
645 PyObject *string; | |
646 PyObject *todecref; | |
646 PyObject *result; | 647 PyObject *result; |
647 PyObject *lookup_dict; | 648 PyObject *lookup_dict; |
648 | 649 |
649 if (!PyArg_ParseTuple(args, "s", &expr)) | 650 if (!PyArg_ParseTuple(args, "O", &string)) |
651 return NULL; | |
652 | |
653 if (!(expr = StringToChars(string, &todecref))) | |
650 return NULL; | 654 return NULL; |
651 | 655 |
652 Py_BEGIN_ALLOW_THREADS | 656 Py_BEGIN_ALLOW_THREADS |
653 Python_Lock_Vim(); | 657 Python_Lock_Vim(); |
654 VimTryStart(); | 658 VimTryStart(); |
655 our_tv = eval_expr((char_u *)expr, NULL); | 659 our_tv = eval_expr(expr, NULL); |
656 Python_Release_Vim(); | 660 Python_Release_Vim(); |
657 Py_END_ALLOW_THREADS | 661 Py_END_ALLOW_THREADS |
662 | |
663 Py_XDECREF(todecref); | |
658 | 664 |
659 if (VimTryEnd()) | 665 if (VimTryEnd()) |
660 return NULL; | 666 return NULL; |
661 | 667 |
662 if (our_tv == NULL) | 668 if (our_tv == NULL) |
686 } | 692 } |
687 | 693 |
688 static PyObject *ConvertToPyObject(typval_T *); | 694 static PyObject *ConvertToPyObject(typval_T *); |
689 | 695 |
690 static PyObject * | 696 static PyObject * |
691 VimEvalPy(PyObject *self UNUSED, PyObject *args) | 697 VimEvalPy(PyObject *self UNUSED, PyObject *string) |
692 { | 698 { |
693 char *expr; | |
694 typval_T *our_tv; | 699 typval_T *our_tv; |
695 PyObject *result; | 700 PyObject *result; |
696 | 701 char_u *expr; |
697 if (!PyArg_ParseTuple(args, "s", &expr)) | 702 PyObject *todecref; |
703 | |
704 if (!(expr = StringToChars(string, &todecref))) | |
698 return NULL; | 705 return NULL; |
699 | 706 |
700 Py_BEGIN_ALLOW_THREADS | 707 Py_BEGIN_ALLOW_THREADS |
701 Python_Lock_Vim(); | 708 Python_Lock_Vim(); |
702 VimTryStart(); | 709 VimTryStart(); |
703 our_tv = eval_expr((char_u *)expr, NULL); | 710 our_tv = eval_expr(expr, NULL); |
704 Python_Release_Vim(); | 711 Python_Release_Vim(); |
705 Py_END_ALLOW_THREADS | 712 Py_END_ALLOW_THREADS |
713 | |
714 Py_XDECREF(todecref); | |
706 | 715 |
707 if (VimTryEnd()) | 716 if (VimTryEnd()) |
708 return NULL; | 717 return NULL; |
709 | 718 |
710 if (our_tv == NULL) | 719 if (our_tv == NULL) |
722 | 731 |
723 return result; | 732 return result; |
724 } | 733 } |
725 | 734 |
726 static PyObject * | 735 static PyObject * |
727 VimStrwidth(PyObject *self UNUSED, PyObject *args) | 736 VimStrwidth(PyObject *self UNUSED, PyObject *string) |
728 { | 737 { |
729 char *expr; | 738 char_u *str; |
730 | 739 PyObject *todecref; |
731 if (!PyArg_ParseTuple(args, "s", &expr)) | 740 int result; |
732 return NULL; | 741 |
733 | 742 if (!(str = StringToChars(string, &todecref))) |
734 return PyLong_FromLong( | 743 return NULL; |
744 | |
735 #ifdef FEAT_MBYTE | 745 #ifdef FEAT_MBYTE |
736 mb_string2cells((char_u *)expr, (int)STRLEN(expr)) | 746 result = mb_string2cells(str, (int)STRLEN(str)); |
737 #else | 747 #else |
738 STRLEN(expr) | 748 result = STRLEN(str); |
739 #endif | 749 #endif |
740 ); | 750 |
751 Py_XDECREF(todecref); | |
752 | |
753 return PyLong_FromLong(result); | |
741 } | 754 } |
742 | 755 |
743 static PyObject * | 756 static PyObject * |
744 _VimChdir(PyObject *_chdir, PyObject *args, PyObject *kwargs) | 757 _VimChdir(PyObject *_chdir, PyObject *args, PyObject *kwargs) |
745 { | 758 { |
838 mr_data->result = NULL; | 851 mr_data->result = NULL; |
839 } | 852 } |
840 } | 853 } |
841 | 854 |
842 static PyObject * | 855 static PyObject * |
843 VimForeachRTP(PyObject *self UNUSED, PyObject *args) | 856 VimForeachRTP(PyObject *self UNUSED, PyObject *callable) |
844 { | 857 { |
845 map_rtp_data data; | 858 map_rtp_data data; |
846 | 859 |
847 if (!PyArg_ParseTuple(args, "O", &data.callable)) | 860 data.callable = callable; |
848 return NULL; | |
849 | |
850 data.result = NULL; | 861 data.result = NULL; |
851 | 862 |
852 do_in_runtimepath(NULL, FALSE, &map_rtp_callback, &data); | 863 do_in_runtimepath(NULL, FALSE, &map_rtp_callback, &data); |
853 | 864 |
854 if (data.result == NULL) | 865 if (data.result == NULL) |
1097 * Vim module - Definitions | 1108 * Vim module - Definitions |
1098 */ | 1109 */ |
1099 | 1110 |
1100 static struct PyMethodDef VimMethods[] = { | 1111 static struct PyMethodDef VimMethods[] = { |
1101 /* name, function, calling, documentation */ | 1112 /* name, function, calling, documentation */ |
1102 {"command", VimCommand, METH_VARARGS, "Execute a Vim ex-mode command" }, | 1113 {"command", VimCommand, METH_O, "Execute a Vim ex-mode command" }, |
1103 {"eval", VimEval, METH_VARARGS, "Evaluate an expression using Vim evaluator" }, | 1114 {"eval", VimEval, METH_VARARGS, "Evaluate an expression using Vim evaluator" }, |
1104 {"bindeval", VimEvalPy, METH_VARARGS, "Like eval(), but returns objects attached to vim ones"}, | 1115 {"bindeval", VimEvalPy, METH_O, "Like eval(), but returns objects attached to vim ones"}, |
1105 {"strwidth", VimStrwidth, METH_VARARGS, "Screen string width, counts <Tab> as having width 1"}, | 1116 {"strwidth", VimStrwidth, METH_O, "Screen string width, counts <Tab> as having width 1"}, |
1106 {"chdir", (PyCFunction)VimChdir, METH_VARARGS|METH_KEYWORDS, "Change directory"}, | 1117 {"chdir", (PyCFunction)VimChdir, METH_VARARGS|METH_KEYWORDS, "Change directory"}, |
1107 {"fchdir", (PyCFunction)VimFchdir, METH_VARARGS|METH_KEYWORDS, "Change directory"}, | 1118 {"fchdir", (PyCFunction)VimFchdir, METH_VARARGS|METH_KEYWORDS, "Change directory"}, |
1108 {"foreach_rtp", VimForeachRTP, METH_VARARGS, "Call given callable for each path in &rtp"}, | 1119 {"foreach_rtp", VimForeachRTP, METH_O, "Call given callable for each path in &rtp"}, |
1109 {"find_module", FinderFindModule, METH_VARARGS, "Internal use only, returns loader object for any input it receives"}, | 1120 {"find_module", FinderFindModule, METH_VARARGS, "Internal use only, returns loader object for any input it receives"}, |
1110 {"path_hook", VimPathHook, METH_VARARGS, "Hook function to install in sys.path_hooks"}, | 1121 {"path_hook", VimPathHook, METH_VARARGS, "Hook function to install in sys.path_hooks"}, |
1111 {"_get_paths", (PyCFunction)Vim_GetPaths, METH_NOARGS, "Get &rtp-based additions to sys.path"}, | 1122 {"_get_paths", (PyCFunction)Vim_GetPaths, METH_NOARGS, "Get &rtp-based additions to sys.path"}, |
1112 { NULL, NULL, 0, NULL} | 1123 { NULL, NULL, 0, NULL} |
1113 }; | 1124 }; |
1731 } | 1742 } |
1732 else | 1743 else |
1733 { | 1744 { |
1734 PyObject *object; | 1745 PyObject *object; |
1735 | 1746 |
1736 if (!PyArg_Parse(args, "(O)", &object)) | 1747 if (!PyArg_ParseTuple(args, "O", &object)) |
1737 return NULL; | 1748 return NULL; |
1738 | 1749 |
1739 if (PyObject_HasAttrString(object, "keys")) | 1750 if (PyObject_HasAttrString(object, "keys")) |
1740 return DictionaryUpdate(self, NULL, object); | 1751 return DictionaryUpdate(self, NULL, object); |
1741 else | 1752 else |
1875 | 1886 |
1876 return r; | 1887 return r; |
1877 } | 1888 } |
1878 | 1889 |
1879 static PyObject * | 1890 static PyObject * |
1880 DictionaryHasKey(DictionaryObject *self, PyObject *args) | 1891 DictionaryHasKey(DictionaryObject *self, PyObject *keyObject) |
1881 { | 1892 { |
1882 PyObject *keyObject; | |
1883 | |
1884 if (!PyArg_ParseTuple(args, "O", &keyObject)) | |
1885 return NULL; | |
1886 | |
1887 return _DictionaryItem(self, keyObject, DICT_FLAG_RETURN_BOOL); | 1893 return _DictionaryItem(self, keyObject, DICT_FLAG_RETURN_BOOL); |
1888 } | 1894 } |
1889 | 1895 |
1890 static PySequenceMethods DictionaryAsSeq = { | 1896 static PySequenceMethods DictionaryAsSeq = { |
1891 0, /* sq_length */ | 1897 0, /* sq_length */ |
1912 {"items", (PyCFunction)DictionaryListItems, METH_NOARGS, ""}, | 1918 {"items", (PyCFunction)DictionaryListItems, METH_NOARGS, ""}, |
1913 {"update", (PyCFunction)DictionaryUpdate, METH_VARARGS|METH_KEYWORDS, ""}, | 1919 {"update", (PyCFunction)DictionaryUpdate, METH_VARARGS|METH_KEYWORDS, ""}, |
1914 {"get", (PyCFunction)DictionaryGet, METH_VARARGS, ""}, | 1920 {"get", (PyCFunction)DictionaryGet, METH_VARARGS, ""}, |
1915 {"pop", (PyCFunction)DictionaryPop, METH_VARARGS, ""}, | 1921 {"pop", (PyCFunction)DictionaryPop, METH_VARARGS, ""}, |
1916 {"popitem", (PyCFunction)DictionaryPopItem, METH_NOARGS, ""}, | 1922 {"popitem", (PyCFunction)DictionaryPopItem, METH_NOARGS, ""}, |
1917 {"has_key", (PyCFunction)DictionaryHasKey, METH_VARARGS, ""}, | 1923 {"has_key", (PyCFunction)DictionaryHasKey, METH_O, ""}, |
1918 {"__dir__", (PyCFunction)DictionaryDir, METH_NOARGS, ""}, | 1924 {"__dir__", (PyCFunction)DictionaryDir, METH_NOARGS, ""}, |
1919 { NULL, NULL, 0, NULL} | 1925 { NULL, NULL, 0, NULL} |
1920 }; | 1926 }; |
1921 | 1927 |
1922 static PyTypeObject ListType; | 1928 static PyTypeObject ListType; |
2432 PyErr_SetString(PyExc_TypeError, | 2438 PyErr_SetString(PyExc_TypeError, |
2433 _("function constructor does not accept keyword arguments")); | 2439 _("function constructor does not accept keyword arguments")); |
2434 return NULL; | 2440 return NULL; |
2435 } | 2441 } |
2436 | 2442 |
2437 if (!PyArg_ParseTuple(args, "s", &name)) | 2443 if (!PyArg_ParseTuple(args, "et", "ascii", &name)) |
2438 return NULL; | 2444 return NULL; |
2439 | 2445 |
2440 self = FunctionNew(subtype, name); | 2446 self = FunctionNew(subtype, name); |
2447 | |
2448 PyMem_Free(name); | |
2441 | 2449 |
2442 return self; | 2450 return self; |
2443 } | 2451 } |
2444 | 2452 |
2445 static void | 2453 static void |
4381 { | 4389 { |
4382 return RBAppend(self, args, 1, -1, NULL); | 4390 return RBAppend(self, args, 1, -1, NULL); |
4383 } | 4391 } |
4384 | 4392 |
4385 static PyObject * | 4393 static PyObject * |
4386 BufferMark(BufferObject *self, PyObject *args) | 4394 BufferMark(BufferObject *self, PyObject *pmarkObject) |
4387 { | 4395 { |
4388 pos_T *posp; | 4396 pos_T *posp; |
4389 char *pmark; | 4397 char_u *pmark; |
4390 char mark; | 4398 char_u mark; |
4391 buf_T *savebuf; | 4399 buf_T *savebuf; |
4400 PyObject *todecref; | |
4392 | 4401 |
4393 if (CheckBuffer(self)) | 4402 if (CheckBuffer(self)) |
4394 return NULL; | 4403 return NULL; |
4395 | 4404 |
4396 if (!PyArg_ParseTuple(args, "s", &pmark)) | 4405 if (!(pmark = StringToChars(pmarkObject, &todecref))) |
4397 return NULL; | 4406 return NULL; |
4398 | 4407 |
4399 if (STRLEN(pmark) != 1) | 4408 if (pmark[0] == '\0' || pmark[1] != '\0') |
4400 { | 4409 { |
4401 PyErr_SetString(PyExc_ValueError, | 4410 PyErr_SetString(PyExc_ValueError, |
4402 _("mark name must be a single character")); | 4411 _("mark name must be a single character")); |
4403 return NULL; | 4412 return NULL; |
4404 } | 4413 } |
4405 | 4414 |
4406 mark = *pmark; | 4415 mark = *pmark; |
4416 | |
4417 Py_XDECREF(todecref); | |
4418 | |
4407 VimTryStart(); | 4419 VimTryStart(); |
4408 switch_buffer(&savebuf, self->buf); | 4420 switch_buffer(&savebuf, self->buf); |
4409 posp = getmark(mark, FALSE); | 4421 posp = getmark(mark, FALSE); |
4410 restore_buffer(savebuf); | 4422 restore_buffer(savebuf); |
4411 if (VimTryEnd()) | 4423 if (VimTryEnd()) |
4459 } | 4471 } |
4460 | 4472 |
4461 static struct PyMethodDef BufferMethods[] = { | 4473 static struct PyMethodDef BufferMethods[] = { |
4462 /* name, function, calling, documentation */ | 4474 /* name, function, calling, documentation */ |
4463 {"append", (PyCFunction)BufferAppend, METH_VARARGS, "Append data to Vim buffer" }, | 4475 {"append", (PyCFunction)BufferAppend, METH_VARARGS, "Append data to Vim buffer" }, |
4464 {"mark", (PyCFunction)BufferMark, METH_VARARGS, "Return (row,col) representing position of named mark" }, | 4476 {"mark", (PyCFunction)BufferMark, METH_O, "Return (row,col) representing position of named mark" }, |
4465 {"range", (PyCFunction)BufferRange, METH_VARARGS, "Return a range object which represents the part of the given buffer between line numbers s and e" }, | 4477 {"range", (PyCFunction)BufferRange, METH_VARARGS, "Return a range object which represents the part of the given buffer between line numbers s and e" }, |
4466 {"__dir__", (PyCFunction)BufferDir, METH_NOARGS, ""}, | 4478 {"__dir__", (PyCFunction)BufferDir, METH_NOARGS, ""}, |
4467 { NULL, NULL, 0, NULL} | 4479 { NULL, NULL, 0, NULL} |
4468 }; | 4480 }; |
4469 | 4481 |