Mercurial > vim
comparison src/if_py_both.h @ 4974:a594ce86b5ea v7.3.1232
updated for version 7.3.1232
Problem: Python: inconsistencies in variable names.
Solution: Rename variables. (ZyX)
author | Bram Moolenaar <bram@vim.org> |
---|---|
date | Sun, 23 Jun 2013 14:30:47 +0200 |
parents | 537bbfff0c5c |
children | 4ed713442c51 |
comparison
equal
deleted
inserted
replaced
4973:c2b13a582971 | 4974:a594ce86b5ea |
---|---|
105 * was needed to generate returned value is object. | 105 * was needed to generate returned value is object. |
106 * | 106 * |
107 * Use Py_XDECREF to decrement reference count. | 107 * Use Py_XDECREF to decrement reference count. |
108 */ | 108 */ |
109 static char_u * | 109 static char_u * |
110 StringToChars(PyObject *object, PyObject **todecref) | 110 StringToChars(PyObject *obj, PyObject **todecref) |
111 { | 111 { |
112 char_u *p; | 112 char_u *str; |
113 | 113 |
114 if (PyBytes_Check(object)) | 114 if (PyBytes_Check(obj)) |
115 { | 115 { |
116 | 116 |
117 if (PyBytes_AsStringAndSize(object, (char **) &p, NULL) == -1 | 117 if (PyBytes_AsStringAndSize(obj, (char **) &str, NULL) == -1 |
118 || p == NULL) | 118 || str == NULL) |
119 return NULL; | 119 return NULL; |
120 | 120 |
121 *todecref = NULL; | 121 *todecref = NULL; |
122 } | 122 } |
123 else if (PyUnicode_Check(object)) | 123 else if (PyUnicode_Check(obj)) |
124 { | 124 { |
125 PyObject *bytes; | 125 PyObject *bytes; |
126 | 126 |
127 if (!(bytes = PyUnicode_AsEncodedString(object, ENC_OPT, NULL))) | 127 if (!(bytes = PyUnicode_AsEncodedString(obj, ENC_OPT, NULL))) |
128 return NULL; | 128 return NULL; |
129 | 129 |
130 if(PyBytes_AsStringAndSize(bytes, (char **) &p, NULL) == -1 | 130 if(PyBytes_AsStringAndSize(bytes, (char **) &str, NULL) == -1 |
131 || p == NULL) | 131 || str == NULL) |
132 { | 132 { |
133 Py_DECREF(bytes); | 133 Py_DECREF(bytes); |
134 return NULL; | 134 return NULL; |
135 } | 135 } |
136 | 136 |
142 #if PY_MAJOR_VERSION < 3 | 142 #if PY_MAJOR_VERSION < 3 |
143 "expected str() or unicode() instance, but got %s" | 143 "expected str() or unicode() instance, but got %s" |
144 #else | 144 #else |
145 "expected bytes() or str() instance, but got %s" | 145 "expected bytes() or str() instance, but got %s" |
146 #endif | 146 #endif |
147 , Py_TYPE_NAME(object)); | 147 , Py_TYPE_NAME(obj)); |
148 return NULL; | 148 return NULL; |
149 } | 149 } |
150 | 150 |
151 return (char_u *) p; | 151 return (char_u *) str; |
152 } | 152 } |
153 | 153 |
154 #define NUMBER_LONG 1 | 154 #define NUMBER_LONG 1 |
155 #define NUMBER_INT 2 | 155 #define NUMBER_INT 2 |
156 #define NUMBER_NATURAL 4 | 156 #define NUMBER_NATURAL 4 |
261 static PyObject * | 261 static PyObject * |
262 ObjectDir(PyObject *self, char **attributes) | 262 ObjectDir(PyObject *self, char **attributes) |
263 { | 263 { |
264 PyMethodDef *method; | 264 PyMethodDef *method; |
265 char **attr; | 265 char **attr; |
266 PyObject *r; | 266 PyObject *ret; |
267 | 267 |
268 if (!(r = PyList_New(0))) | 268 if (!(ret = PyList_New(0))) |
269 return NULL; | 269 return NULL; |
270 | 270 |
271 if (self) | 271 if (self) |
272 for (method = self->ob_type->tp_methods ; method->ml_name != NULL ; ++method) | 272 for (method = self->ob_type->tp_methods ; method->ml_name != NULL ; ++method) |
273 if (add_string(r, (char *) method->ml_name)) | 273 if (add_string(ret, (char *) method->ml_name)) |
274 { | 274 { |
275 Py_DECREF(r); | 275 Py_DECREF(ret); |
276 return NULL; | 276 return NULL; |
277 } | 277 } |
278 | 278 |
279 for (attr = attributes ; *attr ; ++attr) | 279 for (attr = attributes ; *attr ; ++attr) |
280 if (add_string(r, *attr)) | 280 if (add_string(ret, *attr)) |
281 { | 281 { |
282 Py_DECREF(r); | 282 Py_DECREF(ret); |
283 return NULL; | 283 return NULL; |
284 } | 284 } |
285 | 285 |
286 #if PY_MAJOR_VERSION < 3 | 286 #if PY_MAJOR_VERSION < 3 |
287 if (add_string(r, "__members__")) | 287 if (add_string(ret, "__members__")) |
288 { | 288 { |
289 Py_DECREF(r); | 289 Py_DECREF(ret); |
290 return NULL; | 290 return NULL; |
291 } | 291 } |
292 #endif | 292 #endif |
293 | 293 |
294 return r; | 294 return ret; |
295 } | 295 } |
296 | 296 |
297 /* Output buffer management | 297 /* Output buffer management |
298 */ | 298 */ |
299 | 299 |
319 { | 319 { |
320 return ObjectDir(self, OutputAttrs); | 320 return ObjectDir(self, OutputAttrs); |
321 } | 321 } |
322 | 322 |
323 static int | 323 static int |
324 OutputSetattr(OutputObject *self, char *name, PyObject *val) | 324 OutputSetattr(OutputObject *self, char *name, PyObject *valObject) |
325 { | 325 { |
326 if (val == NULL) | 326 if (valObject == NULL) |
327 { | 327 { |
328 PyErr_SET_STRING(PyExc_AttributeError, | 328 PyErr_SET_STRING(PyExc_AttributeError, |
329 "can't delete OutputObject attributes"); | 329 "can't delete OutputObject attributes"); |
330 return -1; | 330 return -1; |
331 } | 331 } |
332 | 332 |
333 if (strcmp(name, "softspace") == 0) | 333 if (strcmp(name, "softspace") == 0) |
334 { | 334 { |
335 if (NumberToLong(val, &(self->softspace), NUMBER_UNSIGNED)) | 335 if (NumberToLong(valObject, &(self->softspace), NUMBER_UNSIGNED)) |
336 return -1; | 336 return -1; |
337 return 0; | 337 return 0; |
338 } | 338 } |
339 | 339 |
340 PyErr_FORMAT(PyExc_AttributeError, "invalid attribute: %s", name); | 340 PyErr_FORMAT(PyExc_AttributeError, "invalid attribute: %s", name); |
516 } | 516 } |
517 | 517 |
518 static PyObject * | 518 static PyObject * |
519 LoaderLoadModule(LoaderObject *self, PyObject *args UNUSED) | 519 LoaderLoadModule(LoaderObject *self, PyObject *args UNUSED) |
520 { | 520 { |
521 PyObject *r = self->module; | 521 PyObject *ret = self->module; |
522 | 522 |
523 Py_INCREF(r); | 523 Py_INCREF(ret); |
524 return r; | 524 return ret; |
525 } | 525 } |
526 | 526 |
527 static struct PyMethodDef LoaderMethods[] = { | 527 static struct PyMethodDef LoaderMethods[] = { |
528 /* name, function, calling, doc */ | 528 /* name, function, calling, doc */ |
529 {"load_module", (PyCFunction)LoaderLoadModule, METH_VARARGS, ""}, | 529 {"load_module", (PyCFunction)LoaderLoadModule, METH_VARARGS, ""}, |
577 | 577 |
578 static PyObject * | 578 static PyObject * |
579 VimCommand(PyObject *self UNUSED, PyObject *string) | 579 VimCommand(PyObject *self UNUSED, PyObject *string) |
580 { | 580 { |
581 char_u *cmd; | 581 char_u *cmd; |
582 PyObject *result; | 582 PyObject *ret; |
583 PyObject *todecref; | 583 PyObject *todecref; |
584 | 584 |
585 if (!(cmd = StringToChars(string, &todecref))) | 585 if (!(cmd = StringToChars(string, &todecref))) |
586 return NULL; | 586 return NULL; |
587 | 587 |
594 | 594 |
595 Python_Release_Vim(); | 595 Python_Release_Vim(); |
596 Py_END_ALLOW_THREADS | 596 Py_END_ALLOW_THREADS |
597 | 597 |
598 if (VimTryEnd()) | 598 if (VimTryEnd()) |
599 result = NULL; | 599 ret = NULL; |
600 else | 600 else |
601 result = Py_None; | 601 ret = Py_None; |
602 | 602 |
603 Py_XINCREF(result); | 603 Py_XINCREF(ret); |
604 Py_XDECREF(todecref); | 604 Py_XDECREF(todecref); |
605 return result; | 605 return ret; |
606 } | 606 } |
607 | 607 |
608 /* | 608 /* |
609 * Function to translate a typval_T into a PyObject; this will recursively | 609 * Function to translate a typval_T into a PyObject; this will recursively |
610 * translate lists/dictionaries into their Python equivalents. | 610 * translate lists/dictionaries into their Python equivalents. |
613 * you call VimToPython. | 613 * you call VimToPython. |
614 */ | 614 */ |
615 static PyObject * | 615 static PyObject * |
616 VimToPython(typval_T *our_tv, int depth, PyObject *lookup_dict) | 616 VimToPython(typval_T *our_tv, int depth, PyObject *lookup_dict) |
617 { | 617 { |
618 PyObject *result; | 618 PyObject *ret; |
619 PyObject *newObj; | 619 PyObject *newObj; |
620 char ptrBuf[sizeof(void *) * 2 + 3]; | 620 char ptrBuf[sizeof(void *) * 2 + 3]; |
621 | 621 |
622 /* Avoid infinite recursion */ | 622 /* Avoid infinite recursion */ |
623 if (depth > 100) | 623 if (depth > 100) |
624 { | 624 { |
625 Py_INCREF(Py_None); | 625 Py_INCREF(Py_None); |
626 result = Py_None; | 626 ret = Py_None; |
627 return result; | 627 return ret; |
628 } | 628 } |
629 | 629 |
630 /* Check if we run into a recursive loop. The item must be in lookup_dict | 630 /* Check if we run into a recursive loop. The item must be in lookup_dict |
631 * then and we can use it again. */ | 631 * then and we can use it again. */ |
632 if ((our_tv->v_type == VAR_LIST && our_tv->vval.v_list != NULL) | 632 if ((our_tv->v_type == VAR_LIST && our_tv->vval.v_list != NULL) |
634 { | 634 { |
635 sprintf(ptrBuf, "%p", | 635 sprintf(ptrBuf, "%p", |
636 our_tv->v_type == VAR_LIST ? (void *)our_tv->vval.v_list | 636 our_tv->v_type == VAR_LIST ? (void *)our_tv->vval.v_list |
637 : (void *)our_tv->vval.v_dict); | 637 : (void *)our_tv->vval.v_dict); |
638 | 638 |
639 if ((result = PyDict_GetItemString(lookup_dict, ptrBuf))) | 639 if ((ret = PyDict_GetItemString(lookup_dict, ptrBuf))) |
640 { | 640 { |
641 Py_INCREF(result); | 641 Py_INCREF(ret); |
642 return result; | 642 return ret; |
643 } | 643 } |
644 } | 644 } |
645 | 645 |
646 if (our_tv->v_type == VAR_STRING) | 646 if (our_tv->v_type == VAR_STRING) |
647 result = PyString_FromString(our_tv->vval.v_string == NULL | 647 ret = PyString_FromString(our_tv->vval.v_string == NULL |
648 ? "" : (char *)our_tv->vval.v_string); | 648 ? "" : (char *)our_tv->vval.v_string); |
649 else if (our_tv->v_type == VAR_NUMBER) | 649 else if (our_tv->v_type == VAR_NUMBER) |
650 { | 650 { |
651 char buf[NUMBUFLEN]; | 651 char buf[NUMBUFLEN]; |
652 | 652 |
653 /* For backwards compatibility numbers are stored as strings. */ | 653 /* For backwards compatibility numbers are stored as strings. */ |
654 sprintf(buf, "%ld", (long)our_tv->vval.v_number); | 654 sprintf(buf, "%ld", (long)our_tv->vval.v_number); |
655 result = PyString_FromString((char *) buf); | 655 ret = PyString_FromString((char *) buf); |
656 } | 656 } |
657 # ifdef FEAT_FLOAT | 657 # ifdef FEAT_FLOAT |
658 else if (our_tv->v_type == VAR_FLOAT) | 658 else if (our_tv->v_type == VAR_FLOAT) |
659 { | 659 { |
660 char buf[NUMBUFLEN]; | 660 char buf[NUMBUFLEN]; |
661 | 661 |
662 sprintf(buf, "%f", our_tv->vval.v_float); | 662 sprintf(buf, "%f", our_tv->vval.v_float); |
663 result = PyString_FromString((char *) buf); | 663 ret = PyString_FromString((char *) buf); |
664 } | 664 } |
665 # endif | 665 # endif |
666 else if (our_tv->v_type == VAR_LIST) | 666 else if (our_tv->v_type == VAR_LIST) |
667 { | 667 { |
668 list_T *list = our_tv->vval.v_list; | 668 list_T *list = our_tv->vval.v_list; |
669 listitem_T *curr; | 669 listitem_T *curr; |
670 | 670 |
671 if (list == NULL) | 671 if (list == NULL) |
672 return NULL; | 672 return NULL; |
673 | 673 |
674 if (!(result = PyList_New(0))) | 674 if (!(ret = PyList_New(0))) |
675 return NULL; | 675 return NULL; |
676 | 676 |
677 if (PyDict_SetItemString(lookup_dict, ptrBuf, result)) | 677 if (PyDict_SetItemString(lookup_dict, ptrBuf, ret)) |
678 { | 678 { |
679 Py_DECREF(result); | 679 Py_DECREF(ret); |
680 return NULL; | 680 return NULL; |
681 } | 681 } |
682 | 682 |
683 for (curr = list->lv_first; curr != NULL; curr = curr->li_next) | 683 for (curr = list->lv_first; curr != NULL; curr = curr->li_next) |
684 { | 684 { |
685 if (!(newObj = VimToPython(&curr->li_tv, depth + 1, lookup_dict))) | 685 if (!(newObj = VimToPython(&curr->li_tv, depth + 1, lookup_dict))) |
686 { | 686 { |
687 Py_DECREF(result); | 687 Py_DECREF(ret); |
688 return NULL; | 688 return NULL; |
689 } | 689 } |
690 if (PyList_Append(result, newObj)) | 690 if (PyList_Append(ret, newObj)) |
691 { | 691 { |
692 Py_DECREF(newObj); | 692 Py_DECREF(newObj); |
693 Py_DECREF(result); | 693 Py_DECREF(ret); |
694 return NULL; | 694 return NULL; |
695 } | 695 } |
696 Py_DECREF(newObj); | 696 Py_DECREF(newObj); |
697 } | 697 } |
698 } | 698 } |
704 hashitem_T *hi; | 704 hashitem_T *hi; |
705 dictitem_T *di; | 705 dictitem_T *di; |
706 if (our_tv->vval.v_dict == NULL) | 706 if (our_tv->vval.v_dict == NULL) |
707 return NULL; | 707 return NULL; |
708 | 708 |
709 if (!(result = PyDict_New())) | 709 if (!(ret = PyDict_New())) |
710 return NULL; | 710 return NULL; |
711 | 711 |
712 if (PyDict_SetItemString(lookup_dict, ptrBuf, result)) | 712 if (PyDict_SetItemString(lookup_dict, ptrBuf, ret)) |
713 { | 713 { |
714 Py_DECREF(result); | 714 Py_DECREF(ret); |
715 return NULL; | 715 return NULL; |
716 } | 716 } |
717 | 717 |
718 for (hi = ht->ht_array; todo > 0; ++hi) | 718 for (hi = ht->ht_array; todo > 0; ++hi) |
719 { | 719 { |
722 --todo; | 722 --todo; |
723 | 723 |
724 di = dict_lookup(hi); | 724 di = dict_lookup(hi); |
725 if (!(newObj = VimToPython(&di->di_tv, depth + 1, lookup_dict))) | 725 if (!(newObj = VimToPython(&di->di_tv, depth + 1, lookup_dict))) |
726 { | 726 { |
727 Py_DECREF(result); | 727 Py_DECREF(ret); |
728 return NULL; | 728 return NULL; |
729 } | 729 } |
730 if (PyDict_SetItemString(result, (char *)hi->hi_key, newObj)) | 730 if (PyDict_SetItemString(ret, (char *)hi->hi_key, newObj)) |
731 { | 731 { |
732 Py_DECREF(result); | 732 Py_DECREF(ret); |
733 Py_DECREF(newObj); | 733 Py_DECREF(newObj); |
734 return NULL; | 734 return NULL; |
735 } | 735 } |
736 } | 736 } |
737 } | 737 } |
738 } | 738 } |
739 else | 739 else |
740 { | 740 { |
741 Py_INCREF(Py_None); | 741 Py_INCREF(Py_None); |
742 result = Py_None; | 742 ret = Py_None; |
743 } | 743 } |
744 | 744 |
745 return result; | 745 return ret; |
746 } | 746 } |
747 | 747 |
748 static PyObject * | 748 static PyObject * |
749 VimEval(PyObject *self UNUSED, PyObject *args) | 749 VimEval(PyObject *self UNUSED, PyObject *args) |
750 { | 750 { |
751 char_u *expr; | 751 char_u *expr; |
752 typval_T *our_tv; | 752 typval_T *our_tv; |
753 PyObject *string; | 753 PyObject *string; |
754 PyObject *todecref; | 754 PyObject *todecref; |
755 PyObject *result; | 755 PyObject *ret; |
756 PyObject *lookup_dict; | 756 PyObject *lookup_dict; |
757 | 757 |
758 if (!PyArg_ParseTuple(args, "O", &string)) | 758 if (!PyArg_ParseTuple(args, "O", &string)) |
759 return NULL; | 759 return NULL; |
760 | 760 |
780 } | 780 } |
781 | 781 |
782 /* Convert the Vim type into a Python type. Create a dictionary that's | 782 /* Convert the Vim type into a Python type. Create a dictionary that's |
783 * used to check for recursive loops. */ | 783 * used to check for recursive loops. */ |
784 if (!(lookup_dict = PyDict_New())) | 784 if (!(lookup_dict = PyDict_New())) |
785 result = NULL; | 785 ret = NULL; |
786 else | 786 else |
787 { | 787 { |
788 result = VimToPython(our_tv, 1, lookup_dict); | 788 ret = VimToPython(our_tv, 1, lookup_dict); |
789 Py_DECREF(lookup_dict); | 789 Py_DECREF(lookup_dict); |
790 } | 790 } |
791 | 791 |
792 | 792 |
793 Py_BEGIN_ALLOW_THREADS | 793 Py_BEGIN_ALLOW_THREADS |
794 Python_Lock_Vim(); | 794 Python_Lock_Vim(); |
795 free_tv(our_tv); | 795 free_tv(our_tv); |
796 Python_Release_Vim(); | 796 Python_Release_Vim(); |
797 Py_END_ALLOW_THREADS | 797 Py_END_ALLOW_THREADS |
798 | 798 |
799 return result; | 799 return ret; |
800 } | 800 } |
801 | 801 |
802 static PyObject *ConvertToPyObject(typval_T *); | 802 static PyObject *ConvertToPyObject(typval_T *); |
803 | 803 |
804 static PyObject * | 804 static PyObject * |
805 VimEvalPy(PyObject *self UNUSED, PyObject *string) | 805 VimEvalPy(PyObject *self UNUSED, PyObject *string) |
806 { | 806 { |
807 typval_T *our_tv; | 807 typval_T *our_tv; |
808 PyObject *result; | 808 PyObject *ret; |
809 char_u *expr; | 809 char_u *expr; |
810 PyObject *todecref; | 810 PyObject *todecref; |
811 | 811 |
812 if (!(expr = StringToChars(string, &todecref))) | 812 if (!(expr = StringToChars(string, &todecref))) |
813 return NULL; | 813 return NULL; |
828 { | 828 { |
829 PyErr_SET_VIM("invalid expression"); | 829 PyErr_SET_VIM("invalid expression"); |
830 return NULL; | 830 return NULL; |
831 } | 831 } |
832 | 832 |
833 result = ConvertToPyObject(our_tv); | 833 ret = ConvertToPyObject(our_tv); |
834 Py_BEGIN_ALLOW_THREADS | 834 Py_BEGIN_ALLOW_THREADS |
835 Python_Lock_Vim(); | 835 Python_Lock_Vim(); |
836 free_tv(our_tv); | 836 free_tv(our_tv); |
837 Python_Release_Vim(); | 837 Python_Release_Vim(); |
838 Py_END_ALLOW_THREADS | 838 Py_END_ALLOW_THREADS |
839 | 839 |
840 return result; | 840 return ret; |
841 } | 841 } |
842 | 842 |
843 static PyObject * | 843 static PyObject * |
844 VimStrwidth(PyObject *self UNUSED, PyObject *string) | 844 VimStrwidth(PyObject *self UNUSED, PyObject *string) |
845 { | 845 { |
846 char_u *str; | 846 char_u *str; |
847 PyObject *todecref; | 847 PyObject *todecref; |
848 int result; | 848 int len; |
849 | 849 |
850 if (!(str = StringToChars(string, &todecref))) | 850 if (!(str = StringToChars(string, &todecref))) |
851 return NULL; | 851 return NULL; |
852 | 852 |
853 #ifdef FEAT_MBYTE | 853 #ifdef FEAT_MBYTE |
854 result = mb_string2cells(str, (int)STRLEN(str)); | 854 len = mb_string2cells(str, (int)STRLEN(str)); |
855 #else | 855 #else |
856 result = STRLEN(str); | 856 len = STRLEN(str); |
857 #endif | 857 #endif |
858 | 858 |
859 Py_XDECREF(todecref); | 859 Py_XDECREF(todecref); |
860 | 860 |
861 return PyLong_FromLong(result); | 861 return PyLong_FromLong(len); |
862 } | 862 } |
863 | 863 |
864 static PyObject * | 864 static PyObject * |
865 _VimChdir(PyObject *_chdir, PyObject *args, PyObject *kwargs) | 865 _VimChdir(PyObject *_chdir, PyObject *args, PyObject *kwargs) |
866 { | 866 { |
867 PyObject *r; | 867 PyObject *ret; |
868 PyObject *newwd; | 868 PyObject *newwd; |
869 PyObject *todecref; | 869 PyObject *todecref; |
870 char_u *new_dir; | 870 char_u *new_dir; |
871 | 871 |
872 if (_chdir == NULL) | 872 if (_chdir == NULL) |
873 return NULL; | 873 return NULL; |
874 if (!(r = PyObject_Call(_chdir, args, kwargs))) | 874 if (!(ret = PyObject_Call(_chdir, args, kwargs))) |
875 return NULL; | 875 return NULL; |
876 | 876 |
877 if (!(newwd = PyObject_CallFunctionObjArgs(py_getcwd, NULL))) | 877 if (!(newwd = PyObject_CallFunctionObjArgs(py_getcwd, NULL))) |
878 { | 878 { |
879 Py_DECREF(r); | 879 Py_DECREF(ret); |
880 return NULL; | 880 return NULL; |
881 } | 881 } |
882 | 882 |
883 if (!(new_dir = StringToChars(newwd, &todecref))) | 883 if (!(new_dir = StringToChars(newwd, &todecref))) |
884 { | 884 { |
885 Py_DECREF(r); | 885 Py_DECREF(ret); |
886 Py_DECREF(newwd); | 886 Py_DECREF(newwd); |
887 return NULL; | 887 return NULL; |
888 } | 888 } |
889 | 889 |
890 VimTryStart(); | 890 VimTryStart(); |
891 | 891 |
892 if (vim_chdir(new_dir)) | 892 if (vim_chdir(new_dir)) |
893 { | 893 { |
894 Py_DECREF(r); | 894 Py_DECREF(ret); |
895 Py_DECREF(newwd); | 895 Py_DECREF(newwd); |
896 Py_XDECREF(todecref); | 896 Py_XDECREF(todecref); |
897 | 897 |
898 if (VimTryEnd()) | 898 if (VimTryEnd()) |
899 return NULL; | 899 return NULL; |
907 | 907 |
908 post_chdir(FALSE); | 908 post_chdir(FALSE); |
909 | 909 |
910 if (VimTryEnd()) | 910 if (VimTryEnd()) |
911 { | 911 { |
912 Py_DECREF(r); | 912 Py_DECREF(ret); |
913 return NULL; | 913 return NULL; |
914 } | 914 } |
915 | 915 |
916 return r; | 916 return ret; |
917 } | 917 } |
918 | 918 |
919 static PyObject * | 919 static PyObject * |
920 VimChdir(PyObject *self UNUSED, PyObject *args, PyObject *kwargs) | 920 VimChdir(PyObject *self UNUSED, PyObject *args, PyObject *kwargs) |
921 { | 921 { |
1050 } | 1050 } |
1051 | 1051 |
1052 static PyObject * | 1052 static PyObject * |
1053 Vim_GetPaths(PyObject *self UNUSED) | 1053 Vim_GetPaths(PyObject *self UNUSED) |
1054 { | 1054 { |
1055 PyObject *r; | 1055 PyObject *ret; |
1056 | 1056 |
1057 if (!(r = PyList_New(0))) | 1057 if (!(ret = PyList_New(0))) |
1058 return NULL; | 1058 return NULL; |
1059 | 1059 |
1060 do_in_runtimepath(NULL, FALSE, &map_finder_callback, r); | 1060 do_in_runtimepath(NULL, FALSE, &map_finder_callback, ret); |
1061 | 1061 |
1062 if (PyErr_Occurred()) | 1062 if (PyErr_Occurred()) |
1063 { | 1063 { |
1064 Py_DECREF(r); | 1064 Py_DECREF(ret); |
1065 return NULL; | 1065 return NULL; |
1066 } | 1066 } |
1067 | 1067 |
1068 return r; | 1068 return ret; |
1069 } | 1069 } |
1070 | 1070 |
1071 static PyObject * | 1071 static PyObject * |
1072 call_load_module(char *name, int len, PyObject *find_module_result) | 1072 call_load_module(char *name, int len, PyObject *find_module_result) |
1073 { | 1073 { |
1398 } | 1398 } |
1399 | 1399 |
1400 static dict_T * | 1400 static dict_T * |
1401 py_dict_alloc(void) | 1401 py_dict_alloc(void) |
1402 { | 1402 { |
1403 dict_T *r; | 1403 dict_T *ret; |
1404 | 1404 |
1405 if (!(r = dict_alloc())) | 1405 if (!(ret = dict_alloc())) |
1406 { | 1406 { |
1407 PyErr_NoMemory(); | 1407 PyErr_NoMemory(); |
1408 return NULL; | 1408 return NULL; |
1409 } | 1409 } |
1410 ++r->dv_refcount; | 1410 ++ret->dv_refcount; |
1411 | 1411 |
1412 return r; | 1412 return ret; |
1413 } | 1413 } |
1414 | 1414 |
1415 static PyObject * | 1415 static PyObject * |
1416 DictionaryConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs) | 1416 DictionaryConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs) |
1417 { | 1417 { |
1459 { | 1459 { |
1460 return ObjectDir(self, DictionaryAttrs); | 1460 return ObjectDir(self, DictionaryAttrs); |
1461 } | 1461 } |
1462 | 1462 |
1463 static int | 1463 static int |
1464 DictionarySetattr(DictionaryObject *self, char *name, PyObject *val) | 1464 DictionarySetattr(DictionaryObject *self, char *name, PyObject *valObject) |
1465 { | 1465 { |
1466 if (val == NULL) | 1466 if (valObject == NULL) |
1467 { | 1467 { |
1468 PyErr_SET_STRING(PyExc_AttributeError, | 1468 PyErr_SET_STRING(PyExc_AttributeError, |
1469 "cannot delete vim.Dictionary attributes"); | 1469 "cannot delete vim.Dictionary attributes"); |
1470 return -1; | 1470 return -1; |
1471 } | 1471 } |
1477 PyErr_SET_STRING(PyExc_TypeError, "cannot modify fixed dictionary"); | 1477 PyErr_SET_STRING(PyExc_TypeError, "cannot modify fixed dictionary"); |
1478 return -1; | 1478 return -1; |
1479 } | 1479 } |
1480 else | 1480 else |
1481 { | 1481 { |
1482 int istrue = PyObject_IsTrue(val); | 1482 int istrue = PyObject_IsTrue(valObject); |
1483 if (istrue == -1) | 1483 if (istrue == -1) |
1484 return -1; | 1484 return -1; |
1485 else if (istrue) | 1485 else if (istrue) |
1486 self->dict->dv_lock = VAR_LOCKED; | 1486 self->dict->dv_lock = VAR_LOCKED; |
1487 else | 1487 else |
1511 static PyObject * | 1511 static PyObject * |
1512 _DictionaryItem(DictionaryObject *self, PyObject *args, int flags) | 1512 _DictionaryItem(DictionaryObject *self, PyObject *args, int flags) |
1513 { | 1513 { |
1514 PyObject *keyObject; | 1514 PyObject *keyObject; |
1515 PyObject *defObject = ((flags & DICT_FLAG_NONE_DEFAULT)? Py_None : NULL); | 1515 PyObject *defObject = ((flags & DICT_FLAG_NONE_DEFAULT)? Py_None : NULL); |
1516 PyObject *r; | 1516 PyObject *ret; |
1517 char_u *key; | 1517 char_u *key; |
1518 dictitem_T *di; | 1518 dictitem_T *di; |
1519 dict_T *dict = self->dict; | 1519 dict_T *dict = self->dict; |
1520 hashitem_T *hi; | 1520 hashitem_T *hi; |
1521 PyObject *todecref; | 1521 PyObject *todecref; |
1564 return Py_True; | 1564 return Py_True; |
1565 } | 1565 } |
1566 | 1566 |
1567 di = dict_lookup(hi); | 1567 di = dict_lookup(hi); |
1568 | 1568 |
1569 if (!(r = ConvertToPyObject(&di->di_tv))) | 1569 if (!(ret = ConvertToPyObject(&di->di_tv))) |
1570 return NULL; | 1570 return NULL; |
1571 | 1571 |
1572 if (flags & DICT_FLAG_POP) | 1572 if (flags & DICT_FLAG_POP) |
1573 { | 1573 { |
1574 if (dict->dv_lock) | 1574 if (dict->dv_lock) |
1575 { | 1575 { |
1576 RAISE_LOCKED_DICTIONARY; | 1576 RAISE_LOCKED_DICTIONARY; |
1577 Py_DECREF(r); | 1577 Py_DECREF(ret); |
1578 return NULL; | 1578 return NULL; |
1579 } | 1579 } |
1580 | 1580 |
1581 hash_remove(&dict->dv_hashtab, hi); | 1581 hash_remove(&dict->dv_hashtab, hi); |
1582 dictitem_free(di); | 1582 dictitem_free(di); |
1583 } | 1583 } |
1584 | 1584 |
1585 return r; | 1585 return ret; |
1586 } | 1586 } |
1587 | 1587 |
1588 static PyObject * | 1588 static PyObject * |
1589 DictionaryItem(DictionaryObject *self, PyObject *keyObject) | 1589 DictionaryItem(DictionaryObject *self, PyObject *keyObject) |
1590 { | 1590 { |
1593 | 1593 |
1594 static int | 1594 static int |
1595 DictionaryContains(DictionaryObject *self, PyObject *keyObject) | 1595 DictionaryContains(DictionaryObject *self, PyObject *keyObject) |
1596 { | 1596 { |
1597 PyObject *rObj = _DictionaryItem(self, keyObject, DICT_FLAG_RETURN_BOOL); | 1597 PyObject *rObj = _DictionaryItem(self, keyObject, DICT_FLAG_RETURN_BOOL); |
1598 int r; | 1598 int ret; |
1599 | 1599 |
1600 r = (rObj == Py_True); | 1600 ret = (rObj == Py_True); |
1601 | 1601 |
1602 Py_DECREF(Py_True); | 1602 Py_DECREF(Py_True); |
1603 | 1603 |
1604 return r; | 1604 return ret; |
1605 } | 1605 } |
1606 | 1606 |
1607 typedef struct | 1607 typedef struct |
1608 { | 1608 { |
1609 hashitem_T *ht_array; | 1609 hashitem_T *ht_array; |
1614 } dictiterinfo_T; | 1614 } dictiterinfo_T; |
1615 | 1615 |
1616 static PyObject * | 1616 static PyObject * |
1617 DictionaryIterNext(dictiterinfo_T **dii) | 1617 DictionaryIterNext(dictiterinfo_T **dii) |
1618 { | 1618 { |
1619 PyObject *r; | 1619 PyObject *ret; |
1620 | 1620 |
1621 if (!(*dii)->todo) | 1621 if (!(*dii)->todo) |
1622 return NULL; | 1622 return NULL; |
1623 | 1623 |
1624 if ((*dii)->ht->ht_array != (*dii)->ht_array || | 1624 if ((*dii)->ht->ht_array != (*dii)->ht_array || |
1632 while (((*dii)->todo) && HASHITEM_EMPTY((*dii)->hi)) | 1632 while (((*dii)->todo) && HASHITEM_EMPTY((*dii)->hi)) |
1633 ++((*dii)->hi); | 1633 ++((*dii)->hi); |
1634 | 1634 |
1635 --((*dii)->todo); | 1635 --((*dii)->todo); |
1636 | 1636 |
1637 if (!(r = PyBytes_FromString((char *) (*dii)->hi->hi_key))) | 1637 if (!(ret = PyBytes_FromString((char *) (*dii)->hi->hi_key))) |
1638 return NULL; | 1638 return NULL; |
1639 | 1639 |
1640 return r; | 1640 return ret; |
1641 } | 1641 } |
1642 | 1642 |
1643 static PyObject * | 1643 static PyObject * |
1644 DictionaryIter(DictionaryObject *self) | 1644 DictionaryIter(DictionaryObject *self) |
1645 { | 1645 { |
1751 DictionaryListObjects(DictionaryObject *self, hi_to_py hiconvert) | 1751 DictionaryListObjects(DictionaryObject *self, hi_to_py hiconvert) |
1752 { | 1752 { |
1753 dict_T *dict = self->dict; | 1753 dict_T *dict = self->dict; |
1754 long_u todo = dict->dv_hashtab.ht_used; | 1754 long_u todo = dict->dv_hashtab.ht_used; |
1755 Py_ssize_t i = 0; | 1755 Py_ssize_t i = 0; |
1756 PyObject *r; | 1756 PyObject *ret; |
1757 hashitem_T *hi; | 1757 hashitem_T *hi; |
1758 PyObject *newObj; | 1758 PyObject *newObj; |
1759 | 1759 |
1760 r = PyList_New(todo); | 1760 ret = PyList_New(todo); |
1761 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi) | 1761 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi) |
1762 { | 1762 { |
1763 if (!HASHITEM_EMPTY(hi)) | 1763 if (!HASHITEM_EMPTY(hi)) |
1764 { | 1764 { |
1765 if (!(newObj = hiconvert(hi))) | 1765 if (!(newObj = hiconvert(hi))) |
1766 { | 1766 { |
1767 Py_DECREF(r); | 1767 Py_DECREF(ret); |
1768 return NULL; | 1768 return NULL; |
1769 } | 1769 } |
1770 PyList_SET_ITEM(r, i, newObj); | 1770 PyList_SET_ITEM(ret, i, newObj); |
1771 --todo; | 1771 --todo; |
1772 ++i; | 1772 ++i; |
1773 } | 1773 } |
1774 } | 1774 } |
1775 return r; | 1775 return ret; |
1776 } | 1776 } |
1777 | 1777 |
1778 static PyObject * | 1778 static PyObject * |
1779 dict_key(hashitem_T *hi) | 1779 dict_key(hashitem_T *hi) |
1780 { | 1780 { |
1805 static PyObject * | 1805 static PyObject * |
1806 dict_item(hashitem_T *hi) | 1806 dict_item(hashitem_T *hi) |
1807 { | 1807 { |
1808 PyObject *keyObject; | 1808 PyObject *keyObject; |
1809 PyObject *valObject; | 1809 PyObject *valObject; |
1810 PyObject *r; | 1810 PyObject *ret; |
1811 | 1811 |
1812 if (!(keyObject = dict_key(hi))) | 1812 if (!(keyObject = dict_key(hi))) |
1813 return NULL; | 1813 return NULL; |
1814 | 1814 |
1815 if (!(valObject = dict_val(hi))) | 1815 if (!(valObject = dict_val(hi))) |
1816 { | 1816 { |
1817 Py_DECREF(keyObject); | 1817 Py_DECREF(keyObject); |
1818 return NULL; | 1818 return NULL; |
1819 } | 1819 } |
1820 | 1820 |
1821 r = Py_BuildValue("(OO)", keyObject, valObject); | 1821 ret = Py_BuildValue("(OO)", keyObject, valObject); |
1822 | 1822 |
1823 Py_DECREF(keyObject); | 1823 Py_DECREF(keyObject); |
1824 Py_DECREF(valObject); | 1824 Py_DECREF(valObject); |
1825 | 1825 |
1826 return r; | 1826 return ret; |
1827 } | 1827 } |
1828 | 1828 |
1829 static PyObject * | 1829 static PyObject * |
1830 DictionaryListItems(DictionaryObject *self) | 1830 DictionaryListItems(DictionaryObject *self) |
1831 { | 1831 { |
1856 if (VimTryEnd()) | 1856 if (VimTryEnd()) |
1857 return NULL; | 1857 return NULL; |
1858 } | 1858 } |
1859 else | 1859 else |
1860 { | 1860 { |
1861 PyObject *object; | 1861 PyObject *obj; |
1862 | 1862 |
1863 if (!PyArg_ParseTuple(args, "O", &object)) | 1863 if (!PyArg_ParseTuple(args, "O", &obj)) |
1864 return NULL; | 1864 return NULL; |
1865 | 1865 |
1866 if (PyObject_HasAttrString(object, "keys")) | 1866 if (PyObject_HasAttrString(obj, "keys")) |
1867 return DictionaryUpdate(self, NULL, object); | 1867 return DictionaryUpdate(self, NULL, obj); |
1868 else | 1868 else |
1869 { | 1869 { |
1870 PyObject *iterator; | 1870 PyObject *iterator; |
1871 PyObject *item; | 1871 PyObject *item; |
1872 | 1872 |
1873 if (!(iterator = PyObject_GetIter(object))) | 1873 if (!(iterator = PyObject_GetIter(obj))) |
1874 return NULL; | 1874 return NULL; |
1875 | 1875 |
1876 while ((item = PyIter_Next(iterator))) | 1876 while ((item = PyIter_Next(iterator))) |
1877 { | 1877 { |
1878 PyObject *fast; | 1878 PyObject *fast; |
1972 | 1972 |
1973 static PyObject * | 1973 static PyObject * |
1974 DictionaryPopItem(DictionaryObject *self) | 1974 DictionaryPopItem(DictionaryObject *self) |
1975 { | 1975 { |
1976 hashitem_T *hi; | 1976 hashitem_T *hi; |
1977 PyObject *r; | 1977 PyObject *ret; |
1978 PyObject *valObject; | 1978 PyObject *valObject; |
1979 dictitem_T *di; | 1979 dictitem_T *di; |
1980 | 1980 |
1981 if (self->dict->dv_hashtab.ht_used == 0) | 1981 if (self->dict->dv_hashtab.ht_used == 0) |
1982 { | 1982 { |
1991 di = dict_lookup(hi); | 1991 di = dict_lookup(hi); |
1992 | 1992 |
1993 if (!(valObject = ConvertToPyObject(&di->di_tv))) | 1993 if (!(valObject = ConvertToPyObject(&di->di_tv))) |
1994 return NULL; | 1994 return NULL; |
1995 | 1995 |
1996 if (!(r = Py_BuildValue("(" Py_bytes_fmt "O)", hi->hi_key, valObject))) | 1996 if (!(ret = Py_BuildValue("(" Py_bytes_fmt "O)", hi->hi_key, valObject))) |
1997 { | 1997 { |
1998 Py_DECREF(valObject); | 1998 Py_DECREF(valObject); |
1999 return NULL; | 1999 return NULL; |
2000 } | 2000 } |
2001 | 2001 |
2002 hash_remove(&self->dict->dv_hashtab, hi); | 2002 hash_remove(&self->dict->dv_hashtab, hi); |
2003 dictitem_free(di); | 2003 dictitem_free(di); |
2004 | 2004 |
2005 return r; | 2005 return ret; |
2006 } | 2006 } |
2007 | 2007 |
2008 static PyObject * | 2008 static PyObject * |
2009 DictionaryHasKey(DictionaryObject *self, PyObject *keyObject) | 2009 DictionaryHasKey(DictionaryObject *self, PyObject *keyObject) |
2010 { | 2010 { |
2073 } | 2073 } |
2074 | 2074 |
2075 static list_T * | 2075 static list_T * |
2076 py_list_alloc() | 2076 py_list_alloc() |
2077 { | 2077 { |
2078 list_T *r; | 2078 list_T *ret; |
2079 | 2079 |
2080 if (!(r = list_alloc())) | 2080 if (!(ret = list_alloc())) |
2081 { | 2081 { |
2082 PyErr_NoMemory(); | 2082 PyErr_NoMemory(); |
2083 return NULL; | 2083 return NULL; |
2084 } | 2084 } |
2085 ++r->lv_refcount; | 2085 ++ret->lv_refcount; |
2086 | 2086 |
2087 return r; | 2087 return ret; |
2088 } | 2088 } |
2089 | 2089 |
2090 static int | 2090 static int |
2091 list_py_concat(list_T *l, PyObject *obj, PyObject *lookup_dict) | 2091 list_py_concat(list_T *l, PyObject *obj, PyObject *lookup_dict) |
2092 { | 2092 { |
2270 } | 2270 } |
2271 | 2271 |
2272 static PyObject * | 2272 static PyObject * |
2273 ListIterNext(listiterinfo_T **lii) | 2273 ListIterNext(listiterinfo_T **lii) |
2274 { | 2274 { |
2275 PyObject *r; | 2275 PyObject *ret; |
2276 | 2276 |
2277 if (!((*lii)->lw.lw_item)) | 2277 if (!((*lii)->lw.lw_item)) |
2278 return NULL; | 2278 return NULL; |
2279 | 2279 |
2280 if (!(r = ConvertToPyObject(&((*lii)->lw.lw_item->li_tv)))) | 2280 if (!(ret = ConvertToPyObject(&((*lii)->lw.lw_item->li_tv)))) |
2281 return NULL; | 2281 return NULL; |
2282 | 2282 |
2283 (*lii)->lw.lw_item = (*lii)->lw.lw_item->li_next; | 2283 (*lii)->lw.lw_item = (*lii)->lw.lw_item->li_next; |
2284 | 2284 |
2285 return r; | 2285 return ret; |
2286 } | 2286 } |
2287 | 2287 |
2288 static PyObject * | 2288 static PyObject * |
2289 ListIter(ListObject *self) | 2289 ListIter(ListObject *self) |
2290 { | 2290 { |
2317 if (l->lv_lock) | 2317 if (l->lv_lock) |
2318 { | 2318 { |
2319 RAISE_LOCKED_LIST; | 2319 RAISE_LOCKED_LIST; |
2320 return -1; | 2320 return -1; |
2321 } | 2321 } |
2322 if (index>length || (index==length && obj==NULL)) | 2322 if (index > length || (index == length && obj == NULL)) |
2323 { | 2323 { |
2324 PyErr_SET_STRING(PyExc_IndexError, "list index out of range"); | 2324 PyErr_SET_STRING(PyExc_IndexError, "list index out of range"); |
2325 return -1; | 2325 return -1; |
2326 } | 2326 } |
2327 | 2327 |
2461 { | 2461 { |
2462 return ObjectDir(self, ListAttrs); | 2462 return ObjectDir(self, ListAttrs); |
2463 } | 2463 } |
2464 | 2464 |
2465 static int | 2465 static int |
2466 ListSetattr(ListObject *self, char *name, PyObject *val) | 2466 ListSetattr(ListObject *self, char *name, PyObject *valObject) |
2467 { | 2467 { |
2468 if (val == NULL) | 2468 if (valObject == NULL) |
2469 { | 2469 { |
2470 PyErr_SET_STRING(PyExc_AttributeError, | 2470 PyErr_SET_STRING(PyExc_AttributeError, |
2471 "cannot delete vim.List attributes"); | 2471 "cannot delete vim.List attributes"); |
2472 return -1; | 2472 return -1; |
2473 } | 2473 } |
2479 PyErr_SET_STRING(PyExc_TypeError, "cannot modify fixed list"); | 2479 PyErr_SET_STRING(PyExc_TypeError, "cannot modify fixed list"); |
2480 return -1; | 2480 return -1; |
2481 } | 2481 } |
2482 else | 2482 else |
2483 { | 2483 { |
2484 int istrue = PyObject_IsTrue(val); | 2484 int istrue = PyObject_IsTrue(valObject); |
2485 if (istrue == -1) | 2485 if (istrue == -1) |
2486 return -1; | 2486 return -1; |
2487 else if (istrue) | 2487 else if (istrue) |
2488 self->list->lv_lock = VAR_LOCKED; | 2488 self->list->lv_lock = VAR_LOCKED; |
2489 else | 2489 else |
2597 typval_T args; | 2597 typval_T args; |
2598 typval_T selfdicttv; | 2598 typval_T selfdicttv; |
2599 typval_T rettv; | 2599 typval_T rettv; |
2600 dict_T *selfdict = NULL; | 2600 dict_T *selfdict = NULL; |
2601 PyObject *selfdictObject; | 2601 PyObject *selfdictObject; |
2602 PyObject *result; | 2602 PyObject *ret; |
2603 int error; | 2603 int error; |
2604 | 2604 |
2605 if (ConvertFromPyObject(argsObject, &args) == -1) | 2605 if (ConvertFromPyObject(argsObject, &args) == -1) |
2606 return NULL; | 2606 return NULL; |
2607 | 2607 |
2627 | 2627 |
2628 Python_Release_Vim(); | 2628 Python_Release_Vim(); |
2629 Py_END_ALLOW_THREADS | 2629 Py_END_ALLOW_THREADS |
2630 | 2630 |
2631 if (VimTryEnd()) | 2631 if (VimTryEnd()) |
2632 result = NULL; | 2632 ret = NULL; |
2633 else if (error != OK) | 2633 else if (error != OK) |
2634 { | 2634 { |
2635 result = NULL; | 2635 ret = NULL; |
2636 PyErr_VIM_FORMAT("failed to run function %s", (char *)name); | 2636 PyErr_VIM_FORMAT("failed to run function %s", (char *)name); |
2637 } | 2637 } |
2638 else | 2638 else |
2639 result = ConvertToPyObject(&rettv); | 2639 ret = ConvertToPyObject(&rettv); |
2640 | 2640 |
2641 clear_tv(&args); | 2641 clear_tv(&args); |
2642 clear_tv(&rettv); | 2642 clear_tv(&rettv); |
2643 if (selfdict != NULL) | 2643 if (selfdict != NULL) |
2644 clear_tv(&selfdicttv); | 2644 clear_tv(&selfdicttv); |
2645 | 2645 |
2646 return result; | 2646 return ret; |
2647 } | 2647 } |
2648 | 2648 |
2649 static PyObject * | 2649 static PyObject * |
2650 FunctionRepr(FunctionObject *self) | 2650 FunctionRepr(FunctionObject *self) |
2651 { | 2651 { |
2759 Py_INCREF(Py_None); | 2759 Py_INCREF(Py_None); |
2760 return Py_None; | 2760 return Py_None; |
2761 } | 2761 } |
2762 else if (flags & SOPT_BOOL) | 2762 else if (flags & SOPT_BOOL) |
2763 { | 2763 { |
2764 PyObject *r; | 2764 PyObject *ret; |
2765 r = numval ? Py_True : Py_False; | 2765 ret = numval ? Py_True : Py_False; |
2766 Py_INCREF(r); | 2766 Py_INCREF(ret); |
2767 return r; | 2767 return ret; |
2768 } | 2768 } |
2769 else if (flags & SOPT_NUM) | 2769 else if (flags & SOPT_NUM) |
2770 return PyInt_FromLong(numval); | 2770 return PyInt_FromLong(numval); |
2771 else if (flags & SOPT_STRING) | 2771 else if (flags & SOPT_STRING) |
2772 { | 2772 { |
2773 if (stringval) | 2773 if (stringval) |
2774 { | 2774 { |
2775 PyObject *r = PyBytes_FromString((char *) stringval); | 2775 PyObject *ret = PyBytes_FromString((char *) stringval); |
2776 vim_free(stringval); | 2776 vim_free(stringval); |
2777 return r; | 2777 return ret; |
2778 } | 2778 } |
2779 else | 2779 else |
2780 { | 2780 { |
2781 PyErr_SET_STRING(PyExc_RuntimeError, | 2781 PyErr_SET_STRING(PyExc_RuntimeError, |
2782 "unable to get option value"); | 2782 "unable to get option value"); |
2815 void *from) | 2815 void *from) |
2816 { | 2816 { |
2817 win_T *save_curwin = NULL; | 2817 win_T *save_curwin = NULL; |
2818 tabpage_T *save_curtab = NULL; | 2818 tabpage_T *save_curtab = NULL; |
2819 buf_T *save_curbuf = NULL; | 2819 buf_T *save_curbuf = NULL; |
2820 int r = 0; | 2820 int set_ret = 0; |
2821 | 2821 |
2822 VimTryStart(); | 2822 VimTryStart(); |
2823 switch (opt_type) | 2823 switch (opt_type) |
2824 { | 2824 { |
2825 case SREQ_WIN: | 2825 case SREQ_WIN: |
2829 if (VimTryEnd()) | 2829 if (VimTryEnd()) |
2830 return -1; | 2830 return -1; |
2831 PyErr_SET_VIM("problem while switching windows"); | 2831 PyErr_SET_VIM("problem while switching windows"); |
2832 return -1; | 2832 return -1; |
2833 } | 2833 } |
2834 r = set_option_value_err(key, numval, stringval, opt_flags); | 2834 set_ret = set_option_value_err(key, numval, stringval, opt_flags); |
2835 restore_win(save_curwin, save_curtab, FALSE); | 2835 restore_win(save_curwin, save_curtab, TRUE); |
2836 if (r == FAIL) | |
2837 return -1; | |
2838 break; | 2836 break; |
2839 case SREQ_BUF: | 2837 case SREQ_BUF: |
2840 switch_buffer(&save_curbuf, (buf_T *)from); | 2838 switch_buffer(&save_curbuf, (buf_T *)from); |
2841 r = set_option_value_err(key, numval, stringval, opt_flags); | 2839 set_ret = set_option_value_err(key, numval, stringval, opt_flags); |
2842 restore_buffer(save_curbuf); | 2840 restore_buffer(save_curbuf); |
2843 if (r == FAIL) | |
2844 return -1; | |
2845 break; | 2841 break; |
2846 case SREQ_GLOBAL: | 2842 case SREQ_GLOBAL: |
2847 r = set_option_value_err(key, numval, stringval, opt_flags); | 2843 set_ret = set_option_value_err(key, numval, stringval, opt_flags); |
2848 if (r == FAIL) | |
2849 return -1; | |
2850 break; | 2844 break; |
2851 } | 2845 } |
2846 if (set_ret == FAIL) | |
2847 return -1; | |
2852 return VimTryEnd(); | 2848 return VimTryEnd(); |
2853 } | 2849 } |
2854 | 2850 |
2855 static int | 2851 static int |
2856 OptionsAssItem(OptionsObject *self, PyObject *keyObject, PyObject *valObject) | 2852 OptionsAssItem(OptionsObject *self, PyObject *keyObject, PyObject *valObject) |
2857 { | 2853 { |
2858 char_u *key; | 2854 char_u *key; |
2859 int flags; | 2855 int flags; |
2860 int opt_flags; | 2856 int opt_flags; |
2861 int r = 0; | 2857 int ret = 0; |
2862 PyObject *todecref; | 2858 PyObject *todecref; |
2863 | 2859 |
2864 if (self->Check(self->from)) | 2860 if (self->Check(self->from)) |
2865 return -1; | 2861 return -1; |
2866 | 2862 |
2914 if (flags & SOPT_BOOL) | 2910 if (flags & SOPT_BOOL) |
2915 { | 2911 { |
2916 int istrue = PyObject_IsTrue(valObject); | 2912 int istrue = PyObject_IsTrue(valObject); |
2917 | 2913 |
2918 if (istrue == -1) | 2914 if (istrue == -1) |
2919 r = -1; | 2915 ret = -1; |
2920 else | 2916 else |
2921 r = set_option_value_for(key, istrue, NULL, | 2917 ret = set_option_value_for(key, istrue, NULL, |
2922 opt_flags, self->opt_type, self->from); | 2918 opt_flags, self->opt_type, self->from); |
2923 } | 2919 } |
2924 else if (flags & SOPT_NUM) | 2920 else if (flags & SOPT_NUM) |
2925 { | 2921 { |
2926 long val; | 2922 long val; |
2929 { | 2925 { |
2930 Py_XDECREF(todecref); | 2926 Py_XDECREF(todecref); |
2931 return -1; | 2927 return -1; |
2932 } | 2928 } |
2933 | 2929 |
2934 r = set_option_value_for(key, (int) val, NULL, opt_flags, | 2930 ret = set_option_value_for(key, (int) val, NULL, opt_flags, |
2935 self->opt_type, self->from); | 2931 self->opt_type, self->from); |
2936 } | 2932 } |
2937 else | 2933 else |
2938 { | 2934 { |
2939 char_u *val; | 2935 char_u *val; |
2940 PyObject *todecref; | 2936 PyObject *todecref; |
2941 | 2937 |
2942 if ((val = StringToChars(valObject, &todecref))) | 2938 if ((val = StringToChars(valObject, &todecref))) |
2943 r = set_option_value_for(key, 0, val, opt_flags, | 2939 ret = set_option_value_for(key, 0, val, opt_flags, |
2944 self->opt_type, self->from); | 2940 self->opt_type, self->from); |
2945 else | 2941 else |
2946 r = -1; | 2942 ret = -1; |
2947 } | 2943 } |
2948 | 2944 |
2949 Py_XDECREF(todecref); | 2945 Py_XDECREF(todecref); |
2950 | 2946 |
2951 return r; | 2947 return ret; |
2952 } | 2948 } |
2953 | 2949 |
2954 static PyMappingMethods OptionsAsMapping = { | 2950 static PyMappingMethods OptionsAsMapping = { |
2955 (lenfunc) NULL, | 2951 (lenfunc) NULL, |
2956 (binaryfunc) OptionsItem, | 2952 (binaryfunc) OptionsItem, |
3025 } | 3021 } |
3026 | 3022 |
3027 static PyObject * | 3023 static PyObject * |
3028 TabPageAttrValid(TabPageObject *self, char *name) | 3024 TabPageAttrValid(TabPageObject *self, char *name) |
3029 { | 3025 { |
3030 PyObject *r; | 3026 PyObject *ret; |
3031 | 3027 |
3032 if (strcmp(name, "valid") != 0) | 3028 if (strcmp(name, "valid") != 0) |
3033 return NULL; | 3029 return NULL; |
3034 | 3030 |
3035 r = ((self->tab == INVALID_TABPAGE_VALUE) ? Py_False : Py_True); | 3031 ret = ((self->tab == INVALID_TABPAGE_VALUE) ? Py_False : Py_True); |
3036 Py_INCREF(r); | 3032 Py_INCREF(ret); |
3037 return r; | 3033 return ret; |
3038 } | 3034 } |
3039 | 3035 |
3040 static PyObject * | 3036 static PyObject * |
3041 TabPageAttr(TabPageObject *self, char *name) | 3037 TabPageAttr(TabPageObject *self, char *name) |
3042 { | 3038 { |
3241 } | 3237 } |
3242 | 3238 |
3243 static PyObject * | 3239 static PyObject * |
3244 WindowAttrValid(WindowObject *self, char *name) | 3240 WindowAttrValid(WindowObject *self, char *name) |
3245 { | 3241 { |
3246 PyObject *r; | 3242 PyObject *ret; |
3247 | 3243 |
3248 if (strcmp(name, "valid") != 0) | 3244 if (strcmp(name, "valid") != 0) |
3249 return NULL; | 3245 return NULL; |
3250 | 3246 |
3251 r = ((self->win == INVALID_WINDOW_VALUE) ? Py_False : Py_True); | 3247 ret = ((self->win == INVALID_WINDOW_VALUE) ? Py_False : Py_True); |
3252 Py_INCREF(r); | 3248 Py_INCREF(ret); |
3253 return r; | 3249 return ret; |
3254 } | 3250 } |
3255 | 3251 |
3256 static PyObject * | 3252 static PyObject * |
3257 WindowAttr(WindowObject *self, char *name) | 3253 WindowAttr(WindowObject *self, char *name) |
3258 { | 3254 { |
3298 else | 3294 else |
3299 return NULL; | 3295 return NULL; |
3300 } | 3296 } |
3301 | 3297 |
3302 static int | 3298 static int |
3303 WindowSetattr(WindowObject *self, char *name, PyObject *val) | 3299 WindowSetattr(WindowObject *self, char *name, PyObject *valObject) |
3304 { | 3300 { |
3305 if (CheckWindow(self)) | 3301 if (CheckWindow(self)) |
3306 return -1; | 3302 return -1; |
3307 | 3303 |
3308 if (strcmp(name, "buffer") == 0) | 3304 if (strcmp(name, "buffer") == 0) |
3313 else if (strcmp(name, "cursor") == 0) | 3309 else if (strcmp(name, "cursor") == 0) |
3314 { | 3310 { |
3315 long lnum; | 3311 long lnum; |
3316 long col; | 3312 long col; |
3317 | 3313 |
3318 if (!PyArg_Parse(val, "(ll)", &lnum, &col)) | 3314 if (!PyArg_Parse(valObject, "(ll)", &lnum, &col)) |
3319 return -1; | 3315 return -1; |
3320 | 3316 |
3321 if (lnum <= 0 || lnum > self->win->w_buffer->b_ml.ml_line_count) | 3317 if (lnum <= 0 || lnum > self->win->w_buffer->b_ml.ml_line_count) |
3322 { | 3318 { |
3323 PyErr_SET_VIM("cursor position outside buffer"); | 3319 PyErr_SET_VIM("cursor position outside buffer"); |
3342 else if (strcmp(name, "height") == 0) | 3338 else if (strcmp(name, "height") == 0) |
3343 { | 3339 { |
3344 long height; | 3340 long height; |
3345 win_T *savewin; | 3341 win_T *savewin; |
3346 | 3342 |
3347 if (NumberToLong(val, &height, NUMBER_INT)) | 3343 if (NumberToLong(valObject, &height, NUMBER_INT)) |
3348 return -1; | 3344 return -1; |
3349 | 3345 |
3350 #ifdef FEAT_GUI | 3346 #ifdef FEAT_GUI |
3351 need_mouse_correct = TRUE; | 3347 need_mouse_correct = TRUE; |
3352 #endif | 3348 #endif |
3365 else if (strcmp(name, "width") == 0) | 3361 else if (strcmp(name, "width") == 0) |
3366 { | 3362 { |
3367 long width; | 3363 long width; |
3368 win_T *savewin; | 3364 win_T *savewin; |
3369 | 3365 |
3370 if (NumberToLong(val, &width, NUMBER_INT)) | 3366 if (NumberToLong(valObject, &width, NUMBER_INT)) |
3371 return -1; | 3367 return -1; |
3372 | 3368 |
3373 #ifdef FEAT_GUI | 3369 #ifdef FEAT_GUI |
3374 need_mouse_correct = TRUE; | 3370 need_mouse_correct = TRUE; |
3375 #endif | 3371 #endif |
3582 * including, hi. The list is returned as a Python list of string objects. | 3578 * including, hi. The list is returned as a Python list of string objects. |
3583 */ | 3579 */ |
3584 static PyObject * | 3580 static PyObject * |
3585 GetBufferLineList(buf_T *buf, PyInt lo, PyInt hi) | 3581 GetBufferLineList(buf_T *buf, PyInt lo, PyInt hi) |
3586 { | 3582 { |
3587 PyInt i; | 3583 PyInt i; |
3588 PyInt n = hi - lo; | 3584 PyInt n = hi - lo; |
3589 PyObject *list = PyList_New(n); | 3585 PyObject *list = PyList_New(n); |
3590 | 3586 |
3591 if (list == NULL) | 3587 if (list == NULL) |
3592 return NULL; | 3588 return NULL; |
3593 | 3589 |
3594 for (i = 0; i < n; ++i) | 3590 for (i = 0; i < n; ++i) |
3595 { | 3591 { |
3596 PyObject *str = LineToString((char *)ml_get_buf(buf, (linenr_T)(lo+i), FALSE)); | 3592 PyObject *string = LineToString( |
3593 (char *)ml_get_buf(buf, (linenr_T)(lo+i), FALSE)); | |
3597 | 3594 |
3598 /* Error check - was the Python string creation OK? */ | 3595 /* Error check - was the Python string creation OK? */ |
3599 if (str == NULL) | 3596 if (string == NULL) |
3600 { | 3597 { |
3601 Py_DECREF(list); | 3598 Py_DECREF(list); |
3602 return NULL; | 3599 return NULL; |
3603 } | 3600 } |
3604 | 3601 |
3605 PyList_SET_ITEM(list, i, str); | 3602 PyList_SET_ITEM(list, i, string); |
3606 } | 3603 } |
3607 | 3604 |
3608 /* The ownership of the Python list is passed to the caller (ie, | 3605 /* The ownership of the Python list is passed to the caller (ie, |
3609 * the caller should Py_DECREF() the object when it is finished | 3606 * the caller should Py_DECREF() the object when it is finished |
3610 * with it). | 3607 * with it). |
3660 * 2. A string - this is a replacement. | 3657 * 2. A string - this is a replacement. |
3661 * 3. Anything else - this is an error. | 3658 * 3. Anything else - this is an error. |
3662 */ | 3659 */ |
3663 if (line == Py_None || line == NULL) | 3660 if (line == Py_None || line == NULL) |
3664 { | 3661 { |
3665 buf_T *savebuf; | 3662 buf_T *savebuf; |
3666 | 3663 |
3667 PyErr_Clear(); | 3664 PyErr_Clear(); |
3668 switch_buffer(&savebuf, buf); | 3665 switch_buffer(&savebuf, buf); |
3669 | 3666 |
3670 VimTryStart(); | 3667 VimTryStart(); |
3745 * value of FAIL. The return value is OK on success. | 3742 * value of FAIL. The return value is OK on success. |
3746 * If OK is returned and len_change is not NULL, *len_change | 3743 * If OK is returned and len_change is not NULL, *len_change |
3747 * is set to the change in the buffer length. | 3744 * is set to the change in the buffer length. |
3748 */ | 3745 */ |
3749 static int | 3746 static int |
3750 SetBufferLineList(buf_T *buf, PyInt lo, PyInt hi, PyObject *list, PyInt *len_change) | 3747 SetBufferLineList( |
3748 buf_T *buf, | |
3749 PyInt lo, | |
3750 PyInt hi, | |
3751 PyObject *list, | |
3752 PyInt *len_change) | |
3751 { | 3753 { |
3752 /* First of all, we check the type of the supplied Python object. | 3754 /* First of all, we check the type of the supplied Python object. |
3753 * There are three cases: | 3755 * There are three cases: |
3754 * 1. NULL, or None - this is a deletion. | 3756 * 1. NULL, or None - this is a deletion. |
3755 * 2. A list - this is a replacement. | 3757 * 2. A list - this is a replacement. |
4122 | 4124 |
4123 return GetBufferLineList(self->buf, lo+start, hi+start); | 4125 return GetBufferLineList(self->buf, lo+start, hi+start); |
4124 } | 4126 } |
4125 | 4127 |
4126 static PyInt | 4128 static PyInt |
4127 RBAsItem(BufferObject *self, PyInt n, PyObject *val, PyInt start, PyInt end, PyInt *new_end) | 4129 RBAsItem( |
4130 BufferObject *self, | |
4131 PyInt n, | |
4132 PyObject *valObject, | |
4133 PyInt start, | |
4134 PyInt end, | |
4135 PyInt *new_end) | |
4128 { | 4136 { |
4129 PyInt len_change; | 4137 PyInt len_change; |
4130 | 4138 |
4131 if (CheckBuffer(self)) | 4139 if (CheckBuffer(self)) |
4132 return -1; | 4140 return -1; |
4141 { | 4149 { |
4142 PyErr_SET_STRING(PyExc_IndexError, "line number out of range"); | 4150 PyErr_SET_STRING(PyExc_IndexError, "line number out of range"); |
4143 return -1; | 4151 return -1; |
4144 } | 4152 } |
4145 | 4153 |
4146 if (SetBufferLine(self->buf, n+start, val, &len_change) == FAIL) | 4154 if (SetBufferLine(self->buf, n+start, valObject, &len_change) == FAIL) |
4147 return -1; | 4155 return -1; |
4148 | 4156 |
4149 if (new_end) | 4157 if (new_end) |
4150 *new_end = end + len_change; | 4158 *new_end = end + len_change; |
4151 | 4159 |
4152 return 0; | 4160 return 0; |
4153 } | 4161 } |
4154 | 4162 |
4155 static PyInt | 4163 static PyInt |
4156 RBAsSlice(BufferObject *self, PyInt lo, PyInt hi, PyObject *val, PyInt start, PyInt end, PyInt *new_end) | 4164 RBAsSlice( |
4165 BufferObject *self, | |
4166 PyInt lo, | |
4167 PyInt hi, | |
4168 PyObject *valObject, | |
4169 PyInt start, | |
4170 PyInt end, | |
4171 PyInt *new_end) | |
4157 { | 4172 { |
4158 PyInt size; | 4173 PyInt size; |
4159 PyInt len_change; | 4174 PyInt len_change; |
4160 | 4175 |
4161 /* Self must be a valid buffer */ | 4176 /* Self must be a valid buffer */ |
4178 hi = lo; | 4193 hi = lo; |
4179 else if (hi > size) | 4194 else if (hi > size) |
4180 hi = size; | 4195 hi = size; |
4181 | 4196 |
4182 if (SetBufferLineList(self->buf, lo + start, hi + start, | 4197 if (SetBufferLineList(self->buf, lo + start, hi + start, |
4183 val, &len_change) == FAIL) | 4198 valObject, &len_change) == FAIL) |
4184 return -1; | 4199 return -1; |
4185 | 4200 |
4186 if (new_end) | 4201 if (new_end) |
4187 *new_end = end + len_change; | 4202 *new_end = end + len_change; |
4188 | 4203 |
4189 return 0; | 4204 return 0; |
4190 } | 4205 } |
4191 | 4206 |
4192 | 4207 |
4193 static PyObject * | 4208 static PyObject * |
4194 RBAppend(BufferObject *self, PyObject *args, PyInt start, PyInt end, PyInt *new_end) | 4209 RBAppend( |
4210 BufferObject *self, | |
4211 PyObject *args, | |
4212 PyInt start, | |
4213 PyInt end, | |
4214 PyInt *new_end) | |
4195 { | 4215 { |
4196 PyObject *lines; | 4216 PyObject *lines; |
4197 PyInt len_change; | 4217 PyInt len_change; |
4198 PyInt max; | 4218 PyInt max; |
4199 PyInt n; | 4219 PyInt n; |
4436 } | 4456 } |
4437 | 4457 |
4438 static PyObject * | 4458 static PyObject * |
4439 BufferAttrValid(BufferObject *self, char *name) | 4459 BufferAttrValid(BufferObject *self, char *name) |
4440 { | 4460 { |
4441 PyObject *r; | 4461 PyObject *ret; |
4442 | 4462 |
4443 if (strcmp(name, "valid") != 0) | 4463 if (strcmp(name, "valid") != 0) |
4444 return NULL; | 4464 return NULL; |
4445 | 4465 |
4446 r = ((self->buf == INVALID_BUFFER_VALUE) ? Py_False : Py_True); | 4466 ret = ((self->buf == INVALID_BUFFER_VALUE) ? Py_False : Py_True); |
4447 Py_INCREF(r); | 4467 Py_INCREF(ret); |
4448 return r; | 4468 return ret; |
4449 } | 4469 } |
4450 | 4470 |
4451 static PyObject * | 4471 static PyObject * |
4452 BufferAttr(BufferObject *self, char *name) | 4472 BufferAttr(BufferObject *self, char *name) |
4453 { | 4473 { |
4473 if (CheckBuffer(self)) | 4493 if (CheckBuffer(self)) |
4474 return -1; | 4494 return -1; |
4475 | 4495 |
4476 if (strcmp(name, "name") == 0) | 4496 if (strcmp(name, "name") == 0) |
4477 { | 4497 { |
4478 char_u *val; | 4498 char_u *val; |
4479 aco_save_T aco; | 4499 aco_save_T aco; |
4480 int r; | 4500 int ren_ret; |
4481 PyObject *todecref; | 4501 PyObject *todecref; |
4482 | 4502 |
4483 if (!(val = StringToChars(valObject, &todecref))) | 4503 if (!(val = StringToChars(valObject, &todecref))) |
4484 return -1; | 4504 return -1; |
4485 | 4505 |
4486 VimTryStart(); | 4506 VimTryStart(); |
4487 /* Using aucmd_*: autocommands will be executed by rename_buffer */ | 4507 /* Using aucmd_*: autocommands will be executed by rename_buffer */ |
4488 aucmd_prepbuf(&aco, self->buf); | 4508 aucmd_prepbuf(&aco, self->buf); |
4489 r = rename_buffer(val); | 4509 ren_ret = rename_buffer(val); |
4490 aucmd_restbuf(&aco); | 4510 aucmd_restbuf(&aco); |
4491 Py_XDECREF(todecref); | 4511 Py_XDECREF(todecref); |
4492 if (VimTryEnd()) | 4512 if (VimTryEnd()) |
4493 return -1; | 4513 return -1; |
4494 | 4514 |
4495 if (r == FAIL) | 4515 if (ren_ret == FAIL) |
4496 { | 4516 { |
4497 PyErr_SET_VIM("failed to rename buffer"); | 4517 PyErr_SET_VIM("failed to rename buffer"); |
4498 return -1; | 4518 return -1; |
4499 } | 4519 } |
4500 return 0; | 4520 return 0; |
4675 | 4695 |
4676 static PyObject * | 4696 static PyObject * |
4677 BufMapIterNext(PyObject **buffer) | 4697 BufMapIterNext(PyObject **buffer) |
4678 { | 4698 { |
4679 PyObject *next; | 4699 PyObject *next; |
4680 PyObject *r; | 4700 PyObject *ret; |
4681 | 4701 |
4682 if (!*buffer) | 4702 if (!*buffer) |
4683 return NULL; | 4703 return NULL; |
4684 | 4704 |
4685 r = *buffer; | 4705 ret = *buffer; |
4686 | 4706 |
4687 if (CheckBuffer((BufferObject *)(r))) | 4707 if (CheckBuffer((BufferObject *)(ret))) |
4688 { | 4708 { |
4689 *buffer = NULL; | 4709 *buffer = NULL; |
4690 return NULL; | 4710 return NULL; |
4691 } | 4711 } |
4692 | 4712 |
4693 if (!((BufferObject *)(r))->buf->b_next) | 4713 if (!((BufferObject *)(ret))->buf->b_next) |
4694 next = NULL; | 4714 next = NULL; |
4695 else if (!(next = BufferNew(((BufferObject *)(r))->buf->b_next))) | 4715 else if (!(next = BufferNew(((BufferObject *)(ret))->buf->b_next))) |
4696 return NULL; | 4716 return NULL; |
4697 *buffer = next; | 4717 *buffer = next; |
4698 /* Do not increment reference: we no longer hold it (decref), but whoever | 4718 /* Do not increment reference: we no longer hold it (decref), but whoever |
4699 * on other side will hold (incref). Decref+incref = nothing. */ | 4719 * on other side will hold (incref). Decref+incref = nothing. */ |
4700 return r; | 4720 return ret; |
4701 } | 4721 } |
4702 | 4722 |
4703 static PyObject * | 4723 static PyObject * |
4704 BufMapIter(PyObject *self UNUSED) | 4724 BufMapIter(PyObject *self UNUSED) |
4705 { | 4725 { |
4753 return NULL; | 4773 return NULL; |
4754 #endif | 4774 #endif |
4755 } | 4775 } |
4756 | 4776 |
4757 static int | 4777 static int |
4758 CurrentSetattr(PyObject *self UNUSED, char *name, PyObject *value) | 4778 CurrentSetattr(PyObject *self UNUSED, char *name, PyObject *valObject) |
4759 { | 4779 { |
4760 if (strcmp(name, "line") == 0) | 4780 if (strcmp(name, "line") == 0) |
4761 { | 4781 { |
4762 if (SetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum, value, NULL) == FAIL) | 4782 if (SetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum, valObject, |
4783 NULL) == FAIL) | |
4763 return -1; | 4784 return -1; |
4764 | 4785 |
4765 return 0; | 4786 return 0; |
4766 } | 4787 } |
4767 else if (strcmp(name, "buffer") == 0) | 4788 else if (strcmp(name, "buffer") == 0) |
4768 { | 4789 { |
4769 int count; | 4790 int count; |
4770 | 4791 |
4771 if (value->ob_type != &BufferType) | 4792 if (valObject->ob_type != &BufferType) |
4772 { | 4793 { |
4773 PyErr_FORMAT(PyExc_TypeError, | 4794 PyErr_FORMAT(PyExc_TypeError, |
4774 "expected vim.Buffer object, but got %s", | 4795 "expected vim.Buffer object, but got %s", |
4775 Py_TYPE_NAME(value)); | 4796 Py_TYPE_NAME(valObject)); |
4776 return -1; | 4797 return -1; |
4777 } | 4798 } |
4778 | 4799 |
4779 if (CheckBuffer((BufferObject *)(value))) | 4800 if (CheckBuffer((BufferObject *)(valObject))) |
4780 return -1; | 4801 return -1; |
4781 count = ((BufferObject *)(value))->buf->b_fnum; | 4802 count = ((BufferObject *)(valObject))->buf->b_fnum; |
4782 | 4803 |
4783 VimTryStart(); | 4804 VimTryStart(); |
4784 if (do_buffer(DOBUF_GOTO, DOBUF_FIRST, FORWARD, count, 0) == FAIL) | 4805 if (do_buffer(DOBUF_GOTO, DOBUF_FIRST, FORWARD, count, 0) == FAIL) |
4785 { | 4806 { |
4786 if (VimTryEnd()) | 4807 if (VimTryEnd()) |
4793 } | 4814 } |
4794 else if (strcmp(name, "window") == 0) | 4815 else if (strcmp(name, "window") == 0) |
4795 { | 4816 { |
4796 int count; | 4817 int count; |
4797 | 4818 |
4798 if (value->ob_type != &WindowType) | 4819 if (valObject->ob_type != &WindowType) |
4799 { | 4820 { |
4800 PyErr_FORMAT(PyExc_TypeError, | 4821 PyErr_FORMAT(PyExc_TypeError, |
4801 "expected vim.Window object, but got %s", | 4822 "expected vim.Window object, but got %s", |
4802 Py_TYPE_NAME(value)); | 4823 Py_TYPE_NAME(valObject)); |
4803 return -1; | 4824 return -1; |
4804 } | 4825 } |
4805 | 4826 |
4806 if (CheckWindow((WindowObject *)(value))) | 4827 if (CheckWindow((WindowObject *)(valObject))) |
4807 return -1; | 4828 return -1; |
4808 count = get_win_number(((WindowObject *)(value))->win, firstwin); | 4829 count = get_win_number(((WindowObject *)(valObject))->win, firstwin); |
4809 | 4830 |
4810 if (!count) | 4831 if (!count) |
4811 { | 4832 { |
4812 PyErr_SET_STRING(PyExc_ValueError, | 4833 PyErr_SET_STRING(PyExc_ValueError, |
4813 "failed to find window in the current tab page"); | 4834 "failed to find window in the current tab page"); |
4814 return -1; | 4835 return -1; |
4815 } | 4836 } |
4816 | 4837 |
4817 VimTryStart(); | 4838 VimTryStart(); |
4818 win_goto(((WindowObject *)(value))->win); | 4839 win_goto(((WindowObject *)(valObject))->win); |
4819 if (((WindowObject *)(value))->win != curwin) | 4840 if (((WindowObject *)(valObject))->win != curwin) |
4820 { | 4841 { |
4821 if (VimTryEnd()) | 4842 if (VimTryEnd()) |
4822 return -1; | 4843 return -1; |
4823 PyErr_SET_STRING(PyExc_RuntimeError, | 4844 PyErr_SET_STRING(PyExc_RuntimeError, |
4824 "did not switch to the specified window"); | 4845 "did not switch to the specified window"); |
4827 | 4848 |
4828 return VimTryEnd(); | 4849 return VimTryEnd(); |
4829 } | 4850 } |
4830 else if (strcmp(name, "tabpage") == 0) | 4851 else if (strcmp(name, "tabpage") == 0) |
4831 { | 4852 { |
4832 if (value->ob_type != &TabPageType) | 4853 if (valObject->ob_type != &TabPageType) |
4833 { | 4854 { |
4834 PyErr_FORMAT(PyExc_TypeError, | 4855 PyErr_FORMAT(PyExc_TypeError, |
4835 "expected vim.TabPage object, but got %s", | 4856 "expected vim.TabPage object, but got %s", |
4836 Py_TYPE_NAME(value)); | 4857 Py_TYPE_NAME(valObject)); |
4837 return -1; | 4858 return -1; |
4838 } | 4859 } |
4839 | 4860 |
4840 if (CheckTabPage((TabPageObject *)(value))) | 4861 if (CheckTabPage((TabPageObject *)(valObject))) |
4841 return -1; | 4862 return -1; |
4842 | 4863 |
4843 VimTryStart(); | 4864 VimTryStart(); |
4844 goto_tabpage_tp(((TabPageObject *)(value))->tab, TRUE, TRUE); | 4865 goto_tabpage_tp(((TabPageObject *)(valObject))->tab, TRUE, TRUE); |
4845 if (((TabPageObject *)(value))->tab != curtab) | 4866 if (((TabPageObject *)(valObject))->tab != curtab) |
4846 { | 4867 { |
4847 if (VimTryEnd()) | 4868 if (VimTryEnd()) |
4848 return -1; | 4869 return -1; |
4849 PyErr_SET_STRING(PyExc_RuntimeError, | 4870 PyErr_SET_STRING(PyExc_RuntimeError, |
4850 "did not switch to the specified tab page"); | 4871 "did not switch to the specified tab page"); |
4932 PyGILState_Release(*pygilstate); | 4953 PyGILState_Release(*pygilstate); |
4933 #endif | 4954 #endif |
4934 | 4955 |
4935 for (lnum = RangeStart; lnum <= RangeEnd; ++lnum) | 4956 for (lnum = RangeStart; lnum <= RangeEnd; ++lnum) |
4936 { | 4957 { |
4937 PyObject *line, *linenr, *ret; | 4958 PyObject *line; |
4959 PyObject *linenr; | |
4960 PyObject *ret; | |
4938 | 4961 |
4939 #ifdef PY_CAN_RECURSE | 4962 #ifdef PY_CAN_RECURSE |
4940 *pygilstate = PyGILState_Ensure(); | 4963 *pygilstate = PyGILState_Ensure(); |
4941 #endif | 4964 #endif |
4942 if (!(line = GetBufferLine(curbuf, lnum))) | 4965 if (!(line = GetBufferLine(curbuf, lnum))) |
4988 #ifdef PY_CAN_RECURSE | 5011 #ifdef PY_CAN_RECURSE |
4989 , PyGILState_STATE *pygilstate UNUSED | 5012 , PyGILState_STATE *pygilstate UNUSED |
4990 #endif | 5013 #endif |
4991 ) | 5014 ) |
4992 { | 5015 { |
4993 PyObject *r; | 5016 PyObject *run_ret; |
4994 | 5017 |
4995 r = PyRun_String((char *) cmd, Py_eval_input, globals, globals); | 5018 run_ret = PyRun_String((char *) cmd, Py_eval_input, globals, globals); |
4996 if (r == NULL) | 5019 if (run_ret == NULL) |
4997 { | 5020 { |
4998 if (PyErr_Occurred() && !msg_silent) | 5021 if (PyErr_Occurred() && !msg_silent) |
4999 PyErr_PrintEx(0); | 5022 PyErr_PrintEx(0); |
5000 EMSG(_("E858: Eval did not return a valid python object")); | 5023 EMSG(_("E858: Eval did not return a valid python object")); |
5001 } | 5024 } |
5002 else | 5025 else |
5003 { | 5026 { |
5004 if (ConvertFromPyObject(r, rettv) == -1) | 5027 if (ConvertFromPyObject(run_ret, rettv) == -1) |
5005 EMSG(_("E859: Failed to convert returned python object to vim value")); | 5028 EMSG(_("E859: Failed to convert returned python object to vim value")); |
5006 Py_DECREF(r); | 5029 Py_DECREF(run_ret); |
5007 } | 5030 } |
5008 PyErr_Clear(); | 5031 PyErr_Clear(); |
5009 } | 5032 } |
5010 | 5033 |
5011 static void | 5034 static void |
5304 | 5327 |
5305 static int | 5328 static int |
5306 ConvertFromPyMapping(PyObject *obj, typval_T *tv) | 5329 ConvertFromPyMapping(PyObject *obj, typval_T *tv) |
5307 { | 5330 { |
5308 PyObject *lookup_dict; | 5331 PyObject *lookup_dict; |
5309 int r; | 5332 int ret; |
5310 | 5333 |
5311 if (!(lookup_dict = PyDict_New())) | 5334 if (!(lookup_dict = PyDict_New())) |
5312 return -1; | 5335 return -1; |
5313 | 5336 |
5314 if (PyType_IsSubtype(obj->ob_type, &DictionaryType)) | 5337 if (PyType_IsSubtype(obj->ob_type, &DictionaryType)) |
5315 { | 5338 { |
5316 tv->v_type = VAR_DICT; | 5339 tv->v_type = VAR_DICT; |
5317 tv->vval.v_dict = (((DictionaryObject *)(obj))->dict); | 5340 tv->vval.v_dict = (((DictionaryObject *)(obj))->dict); |
5318 ++tv->vval.v_dict->dv_refcount; | 5341 ++tv->vval.v_dict->dv_refcount; |
5319 r = 0; | 5342 ret = 0; |
5320 } | 5343 } |
5321 else if (PyDict_Check(obj)) | 5344 else if (PyDict_Check(obj)) |
5322 r = convert_dl(obj, tv, pydict_to_tv, lookup_dict); | 5345 ret = convert_dl(obj, tv, pydict_to_tv, lookup_dict); |
5323 else if (PyMapping_Check(obj)) | 5346 else if (PyMapping_Check(obj)) |
5324 r = convert_dl(obj, tv, pymap_to_tv, lookup_dict); | 5347 ret = convert_dl(obj, tv, pymap_to_tv, lookup_dict); |
5325 else | 5348 else |
5326 { | 5349 { |
5327 PyErr_FORMAT(PyExc_TypeError, | 5350 PyErr_FORMAT(PyExc_TypeError, |
5328 "unable to convert %s to vim dictionary", | 5351 "unable to convert %s to vim dictionary", |
5329 Py_TYPE_NAME(obj)); | 5352 Py_TYPE_NAME(obj)); |
5330 r = -1; | 5353 ret = -1; |
5331 } | 5354 } |
5332 Py_DECREF(lookup_dict); | 5355 Py_DECREF(lookup_dict); |
5333 return r; | 5356 return ret; |
5334 } | 5357 } |
5335 | 5358 |
5336 static int | 5359 static int |
5337 ConvertFromPyObject(PyObject *obj, typval_T *tv) | 5360 ConvertFromPyObject(PyObject *obj, typval_T *tv) |
5338 { | 5361 { |
5339 PyObject *lookup_dict; | 5362 PyObject *lookup_dict; |
5340 int r; | 5363 int ret; |
5341 | 5364 |
5342 if (!(lookup_dict = PyDict_New())) | 5365 if (!(lookup_dict = PyDict_New())) |
5343 return -1; | 5366 return -1; |
5344 r = _ConvertFromPyObject(obj, tv, lookup_dict); | 5367 ret = _ConvertFromPyObject(obj, tv, lookup_dict); |
5345 Py_DECREF(lookup_dict); | 5368 Py_DECREF(lookup_dict); |
5346 return r; | 5369 return ret; |
5347 } | 5370 } |
5348 | 5371 |
5349 static int | 5372 static int |
5350 _ConvertFromPyObject(PyObject *obj, typval_T *tv, PyObject *lookup_dict) | 5373 _ConvertFromPyObject(PyObject *obj, typval_T *tv, PyObject *lookup_dict) |
5351 { | 5374 { |
5369 tv->v_type = VAR_FUNC; | 5392 tv->v_type = VAR_FUNC; |
5370 func_ref(tv->vval.v_string); | 5393 func_ref(tv->vval.v_string); |
5371 } | 5394 } |
5372 else if (PyBytes_Check(obj)) | 5395 else if (PyBytes_Check(obj)) |
5373 { | 5396 { |
5374 char_u *result; | 5397 char_u *str; |
5375 | 5398 |
5376 if (PyBytes_AsStringAndSize(obj, (char **) &result, NULL) == -1) | 5399 if (PyBytes_AsStringAndSize(obj, (char **) &str, NULL) == -1) |
5377 return -1; | 5400 return -1; |
5378 if (result == NULL) | 5401 if (str == NULL) |
5379 return -1; | 5402 return -1; |
5380 | 5403 |
5381 if (set_string_copy(result, tv) == -1) | 5404 if (set_string_copy(str, tv) == -1) |
5382 return -1; | 5405 return -1; |
5383 | 5406 |
5384 tv->v_type = VAR_STRING; | 5407 tv->v_type = VAR_STRING; |
5385 } | 5408 } |
5386 else if (PyUnicode_Check(obj)) | 5409 else if (PyUnicode_Check(obj)) |
5387 { | 5410 { |
5388 PyObject *bytes; | 5411 PyObject *bytes; |
5389 char_u *result; | 5412 char_u *str; |
5390 | 5413 |
5391 bytes = PyUnicode_AsEncodedString(obj, ENC_OPT, NULL); | 5414 bytes = PyUnicode_AsEncodedString(obj, ENC_OPT, NULL); |
5392 if (bytes == NULL) | 5415 if (bytes == NULL) |
5393 return -1; | 5416 return -1; |
5394 | 5417 |
5395 if(PyBytes_AsStringAndSize(bytes, (char **) &result, NULL) == -1) | 5418 if(PyBytes_AsStringAndSize(bytes, (char **) &str, NULL) == -1) |
5396 return -1; | 5419 return -1; |
5397 if (result == NULL) | 5420 if (str == NULL) |
5398 return -1; | 5421 return -1; |
5399 | 5422 |
5400 if (set_string_copy(result, tv)) | 5423 if (set_string_copy(str, tv)) |
5401 { | 5424 { |
5402 Py_XDECREF(bytes); | 5425 Py_XDECREF(bytes); |
5403 return -1; | 5426 return -1; |
5404 } | 5427 } |
5405 Py_XDECREF(bytes); | 5428 Py_XDECREF(bytes); |
5850 PyObject_HEAD_INIT(&TabListType) | 5873 PyObject_HEAD_INIT(&TabListType) |
5851 }; | 5874 }; |
5852 | 5875 |
5853 static struct numeric_constant { | 5876 static struct numeric_constant { |
5854 char *name; | 5877 char *name; |
5855 int value; | 5878 int val; |
5856 } numeric_constants[] = { | 5879 } numeric_constants[] = { |
5857 {"VAR_LOCKED", VAR_LOCKED}, | 5880 {"VAR_LOCKED", VAR_LOCKED}, |
5858 {"VAR_FIXED", VAR_FIXED}, | 5881 {"VAR_FIXED", VAR_FIXED}, |
5859 {"VAR_SCOPE", VAR_SCOPE}, | 5882 {"VAR_SCOPE", VAR_SCOPE}, |
5860 {"VAR_DEF_SCOPE", VAR_DEF_SCOPE}, | 5883 {"VAR_DEF_SCOPE", VAR_DEF_SCOPE}, |
5861 }; | 5884 }; |
5862 | 5885 |
5863 static struct object_constant { | 5886 static struct object_constant { |
5864 char *name; | 5887 char *name; |
5865 PyObject *value; | 5888 PyObject *valObject; |
5866 } object_constants[] = { | 5889 } object_constants[] = { |
5867 {"buffers", (PyObject *)(void *)&TheBufferMap}, | 5890 {"buffers", (PyObject *)(void *)&TheBufferMap}, |
5868 {"windows", (PyObject *)(void *)&TheWindowList}, | 5891 {"windows", (PyObject *)(void *)&TheWindowList}, |
5869 {"tabpages", (PyObject *)(void *)&TheTabPageList}, | 5892 {"tabpages", (PyObject *)(void *)&TheTabPageList}, |
5870 {"current", (PyObject *)(void *)&TheCurrent}, | 5893 {"current", (PyObject *)(void *)&TheCurrent}, |
5887 if (add_object(m, name, obj)) \ | 5910 if (add_object(m, name, obj)) \ |
5888 return -1; | 5911 return -1; |
5889 | 5912 |
5890 #define ADD_CHECKED_OBJECT(m, name, obj) \ | 5913 #define ADD_CHECKED_OBJECT(m, name, obj) \ |
5891 { \ | 5914 { \ |
5892 PyObject *value = obj; \ | 5915 PyObject *valObject = obj; \ |
5893 if (!value) \ | 5916 if (!valObject) \ |
5894 return -1; \ | 5917 return -1; \ |
5895 ADD_OBJECT(m, name, value); \ | 5918 ADD_OBJECT(m, name, valObject); \ |
5896 } | 5919 } |
5897 | 5920 |
5898 static int | 5921 static int |
5899 populate_module(PyObject *m, object_adder add_object, attr_getter get_attr) | 5922 populate_module(PyObject *m, object_adder add_object, attr_getter get_attr) |
5900 { | 5923 { |
5905 | 5928 |
5906 for (i = 0; i < (int)(sizeof(numeric_constants) | 5929 for (i = 0; i < (int)(sizeof(numeric_constants) |
5907 / sizeof(struct numeric_constant)); | 5930 / sizeof(struct numeric_constant)); |
5908 ++i) | 5931 ++i) |
5909 ADD_CHECKED_OBJECT(m, numeric_constants[i].name, | 5932 ADD_CHECKED_OBJECT(m, numeric_constants[i].name, |
5910 PyInt_FromLong(numeric_constants[i].value)); | 5933 PyInt_FromLong(numeric_constants[i].val)); |
5911 | 5934 |
5912 for (i = 0; i < (int)(sizeof(object_constants) | 5935 for (i = 0; i < (int)(sizeof(object_constants) |
5913 / sizeof(struct object_constant)); | 5936 / sizeof(struct object_constant)); |
5914 ++i) | 5937 ++i) |
5915 { | 5938 { |
5916 PyObject *value; | 5939 PyObject *valObject; |
5917 | 5940 |
5918 value = object_constants[i].value; | 5941 valObject = object_constants[i].valObject; |
5919 Py_INCREF(value); | 5942 Py_INCREF(valObject); |
5920 ADD_OBJECT(m, object_constants[i].name, value); | 5943 ADD_OBJECT(m, object_constants[i].name, valObject); |
5921 } | 5944 } |
5922 | 5945 |
5923 if (!(VimError = PyErr_NewException("vim.error", NULL, NULL))) | 5946 if (!(VimError = PyErr_NewException("vim.error", NULL, NULL))) |
5924 return -1; | 5947 return -1; |
5925 ADD_OBJECT(m, "error", VimError); | 5948 ADD_OBJECT(m, "error", VimError); |