comparison src/if_py_both.h @ 3618:c052f3b79b99 v7.3.569

updated for version 7.3.569 Problem: Evaluating Vim expression in Python is insufficient. Solution: Add vim.bindeval(). Also add pyeval() and py3eval(). (ZyX)
author Bram Moolenaar <bram@vim.org>
date Fri, 29 Jun 2012 12:54:53 +0200
parents e34c620007be
children 0e9b2622c94a
comparison
equal deleted inserted replaced
3617:74d62fbf05c9 3618:c052f3b79b99
1 /* vi:set ts=8 sts=4 sw=4: 1 /* vi:set ts=8 sts=4 sw=4 noet:
2 * 2 *
3 * VIM - Vi IMproved by Bram Moolenaar 3 * VIM - Vi IMproved by Bram Moolenaar
4 * 4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions. 5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed. 6 * Do ":help credits" in Vim to see a list of people who contributed.
103 103
104 if (!PyArg_ParseTuple(args, "O", &list)) 104 if (!PyArg_ParseTuple(args, "O", &list))
105 return NULL; 105 return NULL;
106 Py_INCREF(list); 106 Py_INCREF(list);
107 107
108 if (!PyList_Check(list)) { 108 if (!PyList_Check(list))
109 {
109 PyErr_SetString(PyExc_TypeError, _("writelines() requires list of strings")); 110 PyErr_SetString(PyExc_TypeError, _("writelines() requires list of strings"));
110 Py_DECREF(list); 111 Py_DECREF(list);
111 return NULL; 112 return NULL;
112 } 113 }
113 114
117 { 118 {
118 PyObject *line = PyList_GetItem(list, i); 119 PyObject *line = PyList_GetItem(list, i);
119 char *str = NULL; 120 char *str = NULL;
120 PyInt len; 121 PyInt len;
121 122
122 if (!PyArg_Parse(line, "et#", ENC_OPT, &str, &len)) { 123 if (!PyArg_Parse(line, "et#", ENC_OPT, &str, &len))
124 {
123 PyErr_SetString(PyExc_TypeError, _("writelines() requires list of strings")); 125 PyErr_SetString(PyExc_TypeError, _("writelines() requires list of strings"));
124 Py_DECREF(list); 126 Py_DECREF(list);
125 return NULL; 127 return NULL;
126 } 128 }
127 129
295 static PyObject * 297 static PyObject *
296 VimToPython(typval_T *our_tv, int depth, PyObject *lookupDict) 298 VimToPython(typval_T *our_tv, int depth, PyObject *lookupDict)
297 { 299 {
298 PyObject *result; 300 PyObject *result;
299 PyObject *newObj; 301 PyObject *newObj;
300 char ptrBuf[NUMBUFLEN]; 302 char ptrBuf[sizeof(void *) * 2 + 3];
301 303
302 /* Avoid infinite recursion */ 304 /* Avoid infinite recursion */
303 if (depth > 100) 305 if (depth > 100)
304 { 306 {
305 Py_INCREF(Py_None); 307 Py_INCREF(Py_None);
310 /* Check if we run into a recursive loop. The item must be in lookupDict 312 /* Check if we run into a recursive loop. The item must be in lookupDict
311 * then and we can use it again. */ 313 * then and we can use it again. */
312 if ((our_tv->v_type == VAR_LIST && our_tv->vval.v_list != NULL) 314 if ((our_tv->v_type == VAR_LIST && our_tv->vval.v_list != NULL)
313 || (our_tv->v_type == VAR_DICT && our_tv->vval.v_dict != NULL)) 315 || (our_tv->v_type == VAR_DICT && our_tv->vval.v_dict != NULL))
314 { 316 {
315 sprintf(ptrBuf, PRINTF_DECIMAL_LONG_U, 317 sprintf(ptrBuf, "%p",
316 our_tv->v_type == VAR_LIST ? (long_u)our_tv->vval.v_list 318 our_tv->v_type == VAR_LIST ? (void *)our_tv->vval.v_list
317 : (long_u)our_tv->vval.v_dict); 319 : (void *)our_tv->vval.v_dict);
318 result = PyDict_GetItemString(lookupDict, ptrBuf); 320 result = PyDict_GetItemString(lookupDict, ptrBuf);
319 if (result != NULL) 321 if (result != NULL)
320 { 322 {
321 Py_INCREF(result); 323 Py_INCREF(result);
322 return result; 324 return result;
443 PyErr_SetVim(_("expressions disabled at compile time")); 445 PyErr_SetVim(_("expressions disabled at compile time"));
444 return NULL; 446 return NULL;
445 #endif 447 #endif
446 } 448 }
447 449
450 static PyObject *ConvertToPyObject(typval_T *);
451
452 static PyObject *
453 VimEvalPy(PyObject *self UNUSED, PyObject *args UNUSED)
454 {
455 #ifdef FEAT_EVAL
456 char *expr;
457 typval_T *our_tv;
458 PyObject *result;
459
460 if (!PyArg_ParseTuple(args, "s", &expr))
461 return NULL;
462
463 Py_BEGIN_ALLOW_THREADS
464 Python_Lock_Vim();
465 our_tv = eval_expr((char_u *)expr, NULL);
466
467 Python_Release_Vim();
468 Py_END_ALLOW_THREADS
469
470 if (our_tv == NULL)
471 {
472 PyErr_SetVim(_("invalid expression"));
473 return NULL;
474 }
475
476 result = ConvertToPyObject(our_tv);
477 Py_BEGIN_ALLOW_THREADS
478 Python_Lock_Vim();
479 free_tv(our_tv);
480 Python_Release_Vim();
481 Py_END_ALLOW_THREADS
482
483 return result;
484 #else
485 PyErr_SetVim(_("expressions disabled at compile time"));
486 return NULL;
487 #endif
488 }
489
490 static PyObject *
491 VimStrwidth(PyObject *self UNUSED, PyObject *args)
492 {
493 char *expr;
494
495 if (!PyArg_ParseTuple(args, "s", &expr))
496 return NULL;
497
498 return PyLong_FromLong(mb_string2cells((char_u *)expr, STRLEN(expr)));
499 }
500
448 /* 501 /*
449 * Vim module - Definitions 502 * Vim module - Definitions
450 */ 503 */
451 504
452 static struct PyMethodDef VimMethods[] = { 505 static struct PyMethodDef VimMethods[] = {
453 /* name, function, calling, documentation */ 506 /* name, function, calling, documentation */
454 {"command", VimCommand, 1, "Execute a Vim ex-mode command" }, 507 {"command", VimCommand, 1, "Execute a Vim ex-mode command" },
455 {"eval", VimEval, 1, "Evaluate an expression using Vim evaluator" }, 508 {"eval", VimEval, 1, "Evaluate an expression using Vim evaluator" },
509 {"bindeval", VimEvalPy, 1, "Like eval(), but returns objects attached to vim ones"},
510 {"strwidth", VimStrwidth, 1, "Screen string width, counts <Tab> as having width 1"},
456 { NULL, NULL, 0, NULL } 511 { NULL, NULL, 0, NULL }
457 }; 512 };
458 513
459 typedef struct 514 typedef struct
460 { 515 {
461 PyObject_HEAD 516 PyObject_HEAD
462 buf_T *buf; 517 buf_T *buf;
463 } 518 } BufferObject;
464 BufferObject;
465 519
466 #define INVALID_BUFFER_VALUE ((buf_T *)(-1)) 520 #define INVALID_BUFFER_VALUE ((buf_T *)(-1))
467 521
468 /* 522 /*
469 * Buffer list object - Implementation 523 * Buffer list object - Implementation
502 typedef struct 556 typedef struct
503 { 557 {
504 PyObject_HEAD 558 PyObject_HEAD
505 win_T *win; 559 win_T *win;
506 } WindowObject; 560 } WindowObject;
561
562 static int ConvertFromPyObject(PyObject *, typval_T *);
563 static int _ConvertFromPyObject(PyObject *, typval_T *, PyObject *);
564
565 typedef struct pylinkedlist_S {
566 struct pylinkedlist_S *pll_next;
567 struct pylinkedlist_S *pll_prev;
568 PyObject *pll_obj;
569 } pylinkedlist_T;
570
571 static pylinkedlist_T *lastdict = NULL;
572 static pylinkedlist_T *lastlist = NULL;
573
574 static void
575 pyll_remove(pylinkedlist_T *ref, pylinkedlist_T **last)
576 {
577 if (ref->pll_prev == NULL)
578 {
579 if (ref->pll_next == NULL)
580 {
581 *last = NULL;
582 return;
583 }
584 }
585 else
586 ref->pll_prev->pll_next = ref->pll_next;
587
588 if (ref->pll_next == NULL)
589 *last = ref->pll_prev;
590 else
591 ref->pll_next->pll_prev = ref->pll_prev;
592 }
593
594 static void
595 pyll_add(PyObject *self, pylinkedlist_T *ref, pylinkedlist_T **last)
596 {
597 if (*last == NULL)
598 ref->pll_prev = NULL;
599 else
600 {
601 (*last)->pll_next = ref;
602 ref->pll_prev = *last;
603 }
604 ref->pll_next = NULL;
605 ref->pll_obj = self;
606 *last = ref;
607 }
608
609 static PyTypeObject DictionaryType;
610
611 typedef struct
612 {
613 PyObject_HEAD
614 dict_T *dict;
615 pylinkedlist_T ref;
616 } DictionaryObject;
617
618 static PyObject *
619 DictionaryNew(dict_T *dict)
620 {
621 DictionaryObject *self;
622
623 self = PyObject_NEW(DictionaryObject, &DictionaryType);
624 if (self == NULL)
625 return NULL;
626 self->dict = dict;
627 ++dict->dv_refcount;
628
629 pyll_add((PyObject *)(self), &self->ref, &lastdict);
630
631 return (PyObject *)(self);
632 }
633
634 static int
635 pydict_to_tv(PyObject *obj, typval_T *tv, PyObject *lookupDict)
636 {
637 dict_T *d;
638 char_u *key;
639 dictitem_T *di;
640 PyObject *keyObject;
641 PyObject *valObject;
642 Py_ssize_t iter = 0;
643
644 d = dict_alloc();
645 if (d == NULL)
646 {
647 PyErr_NoMemory();
648 return -1;
649 }
650
651 tv->v_type = VAR_DICT;
652 tv->vval.v_dict = d;
653
654 while (PyDict_Next(obj, &iter, &keyObject, &valObject))
655 {
656 DICTKEY_DECL
657
658 if (keyObject == NULL)
659 return -1;
660 if (valObject == NULL)
661 return -1;
662
663 DICTKEY_GET(-1)
664
665 di = dictitem_alloc(key);
666
667 DICTKEY_UNREF
668
669 if (di == NULL)
670 {
671 PyErr_NoMemory();
672 return -1;
673 }
674 di->di_tv.v_lock = 0;
675
676 if (_ConvertFromPyObject(valObject, &di->di_tv, lookupDict) == -1)
677 {
678 vim_free(di);
679 return -1;
680 }
681 if (dict_add(d, di) == FAIL)
682 {
683 vim_free(di);
684 PyErr_SetVim(_("failed to add key to dictionary"));
685 return -1;
686 }
687 }
688 return 0;
689 }
690
691 static int
692 pymap_to_tv(PyObject *obj, typval_T *tv, PyObject *lookupDict)
693 {
694 dict_T *d;
695 char_u *key;
696 dictitem_T *di;
697 PyObject *list;
698 PyObject *litem;
699 PyObject *keyObject;
700 PyObject *valObject;
701 Py_ssize_t lsize;
702
703 d = dict_alloc();
704 if (d == NULL)
705 {
706 PyErr_NoMemory();
707 return -1;
708 }
709
710 tv->v_type = VAR_DICT;
711 tv->vval.v_dict = d;
712
713 list = PyMapping_Items(obj);
714 lsize = PyList_Size(list);
715 while (lsize--)
716 {
717 DICTKEY_DECL
718
719 litem = PyList_GetItem(list, lsize);
720 if (litem == NULL)
721 {
722 Py_DECREF(list);
723 return -1;
724 }
725
726 keyObject = PyTuple_GetItem(litem, 0);
727 if (keyObject == NULL)
728 {
729 Py_DECREF(list);
730 Py_DECREF(litem);
731 return -1;
732 }
733
734 DICTKEY_GET(-1)
735
736 valObject = PyTuple_GetItem(litem, 1);
737 if (valObject == NULL)
738 {
739 Py_DECREF(list);
740 Py_DECREF(litem);
741 return -1;
742 }
743
744 di = dictitem_alloc(key);
745
746 DICTKEY_UNREF
747
748 if (di == NULL)
749 {
750 Py_DECREF(list);
751 Py_DECREF(litem);
752 PyErr_NoMemory();
753 return -1;
754 }
755 di->di_tv.v_lock = 0;
756
757 if (_ConvertFromPyObject(valObject, &di->di_tv, lookupDict) == -1)
758 {
759 vim_free(di);
760 Py_DECREF(list);
761 Py_DECREF(litem);
762 return -1;
763 }
764 if (dict_add(d, di) == FAIL)
765 {
766 vim_free(di);
767 Py_DECREF(list);
768 Py_DECREF(litem);
769 PyErr_SetVim(_("failed to add key to dictionary"));
770 return -1;
771 }
772 Py_DECREF(litem);
773 }
774 Py_DECREF(list);
775 return 0;
776 }
777
778 static PyInt
779 DictionaryLength(PyObject *self)
780 {
781 return ((PyInt) ((((DictionaryObject *)(self))->dict->dv_hashtab.ht_used)));
782 }
783
784 static PyObject *
785 DictionaryItem(PyObject *self, PyObject *keyObject)
786 {
787 char_u *key;
788 dictitem_T *val;
789 DICTKEY_DECL
790
791 DICTKEY_GET(NULL)
792
793 val = dict_find(((DictionaryObject *) (self))->dict, key, -1);
794
795 DICTKEY_UNREF
796
797 return ConvertToPyObject(&val->di_tv);
798 }
799
800 static PyInt
801 DictionaryAssItem(PyObject *self, PyObject *keyObject, PyObject *valObject)
802 {
803 char_u *key;
804 typval_T tv;
805 dict_T *d = ((DictionaryObject *)(self))->dict;
806 dictitem_T *di;
807 DICTKEY_DECL
808
809 if (d->dv_lock)
810 {
811 PyErr_SetVim(_("dict is locked"));
812 return -1;
813 }
814
815 DICTKEY_GET(-1)
816
817 di = dict_find(d, key, -1);
818
819 if (valObject == NULL)
820 {
821 if (di == NULL)
822 {
823 PyErr_SetString(PyExc_IndexError, _("no such key in dictionary"));
824 return -1;
825 }
826 hashitem_T *hi = hash_find(&d->dv_hashtab, di->di_key);
827 hash_remove(&d->dv_hashtab, hi);
828 dictitem_free(di);
829 return 0;
830 }
831
832 if (ConvertFromPyObject(valObject, &tv) == -1)
833 {
834 return -1;
835 }
836
837 if (di == NULL)
838 {
839 di = dictitem_alloc(key);
840 if (di == NULL)
841 {
842 PyErr_NoMemory();
843 return -1;
844 }
845 di->di_tv.v_lock = 0;
846
847 if (dict_add(d, di) == FAIL)
848 {
849 vim_free(di);
850 PyErr_SetVim(_("failed to add key to dictionary"));
851 return -1;
852 }
853 }
854 else
855 clear_tv(&di->di_tv);
856
857 DICTKEY_UNREF
858
859 copy_tv(&tv, &di->di_tv);
860 return 0;
861 }
862
863 static PyObject *
864 DictionaryListKeys(PyObject *self)
865 {
866 dict_T *dict = ((DictionaryObject *)(self))->dict;
867 long_u todo = dict->dv_hashtab.ht_used;
868 Py_ssize_t i = 0;
869 PyObject *r;
870 hashitem_T *hi;
871
872 r = PyList_New(todo);
873 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
874 {
875 if (!HASHITEM_EMPTY(hi))
876 {
877 PyList_SetItem(r, i, PyBytes_FromString((char *)(hi->hi_key)));
878 --todo;
879 ++i;
880 }
881 }
882 return r;
883 }
884
885 static struct PyMethodDef DictionaryMethods[] = {
886 {"keys", (PyCFunction)DictionaryListKeys, METH_NOARGS, ""},
887 { NULL, NULL, 0, NULL }
888 };
889
890 static PyTypeObject ListType;
891
892 typedef struct
893 {
894 PyObject_HEAD
895 list_T *list;
896 pylinkedlist_T ref;
897 } ListObject;
898
899 static PyObject *
900 ListNew(list_T *list)
901 {
902 ListObject *self;
903
904 self = PyObject_NEW(ListObject, &ListType);
905 if (self == NULL)
906 return NULL;
907 self->list = list;
908 ++list->lv_refcount;
909
910 pyll_add((PyObject *)(self), &self->ref, &lastlist);
911
912 return (PyObject *)(self);
913 }
914
915 static int
916 list_py_concat(list_T *l, PyObject *obj, PyObject *lookupDict)
917 {
918 Py_ssize_t i;
919 Py_ssize_t lsize = PySequence_Size(obj);
920 PyObject *litem;
921 listitem_T *li;
922
923 for(i=0; i<lsize; i++)
924 {
925 li = listitem_alloc();
926 if (li == NULL)
927 {
928 PyErr_NoMemory();
929 return -1;
930 }
931 li->li_tv.v_lock = 0;
932
933 litem = PySequence_GetItem(obj, i);
934 if (litem == NULL)
935 return -1;
936 if (_ConvertFromPyObject(litem, &li->li_tv, lookupDict) == -1)
937 return -1;
938
939 list_append(l, li);
940 }
941 return 0;
942 }
943
944 static int
945 pyseq_to_tv(PyObject *obj, typval_T *tv, PyObject *lookupDict)
946 {
947 list_T *l;
948
949 l = list_alloc();
950 if (l == NULL)
951 {
952 PyErr_NoMemory();
953 return -1;
954 }
955
956 tv->v_type = VAR_LIST;
957 tv->vval.v_list = l;
958
959 if (list_py_concat(l, obj, lookupDict) == -1)
960 return -1;
961
962 return 0;
963 }
964
965 static int
966 pyiter_to_tv(PyObject *obj, typval_T *tv, PyObject *lookupDict)
967 {
968 PyObject *iterator = PyObject_GetIter(obj);
969 PyObject *item;
970 list_T *l;
971 listitem_T *li;
972
973 l = list_alloc();
974
975 if (l == NULL)
976 {
977 PyErr_NoMemory();
978 return -1;
979 }
980
981 tv->vval.v_list = l;
982 tv->v_type = VAR_LIST;
983
984
985 if (iterator == NULL)
986 return -1;
987
988 while ((item = PyIter_Next(obj)))
989 {
990 li = listitem_alloc();
991 if (li == NULL)
992 {
993 PyErr_NoMemory();
994 return -1;
995 }
996 li->li_tv.v_lock = 0;
997
998 if (_ConvertFromPyObject(item, &li->li_tv, lookupDict) == -1)
999 return -1;
1000
1001 list_append(l, li);
1002
1003 Py_DECREF(item);
1004 }
1005
1006 Py_DECREF(iterator);
1007 return 0;
1008 }
1009
1010 static PyInt
1011 ListLength(PyObject *self)
1012 {
1013 return ((PyInt) (((ListObject *) (self))->list->lv_len));
1014 }
1015
1016 static PyObject *
1017 ListItem(PyObject *self, Py_ssize_t index)
1018 {
1019 listitem_T *li;
1020
1021 if (index>=ListLength(self))
1022 {
1023 PyErr_SetString(PyExc_IndexError, "list index out of range");
1024 return NULL;
1025 }
1026 li = list_find(((ListObject *) (self))->list, (long) index);
1027 if (li == NULL)
1028 {
1029 PyErr_SetVim(_("internal error: failed to get vim list item"));
1030 return NULL;
1031 }
1032 return ConvertToPyObject(&li->li_tv);
1033 }
1034
1035 #define PROC_RANGE \
1036 if (last < 0) {\
1037 if (last < -size) \
1038 last = 0; \
1039 else \
1040 last += size; \
1041 } \
1042 if (first < 0) \
1043 first = 0; \
1044 if (first > size) \
1045 first = size; \
1046 if (last > size) \
1047 last = size;
1048
1049 static PyObject *
1050 ListSlice(PyObject *self, Py_ssize_t first, Py_ssize_t last)
1051 {
1052 PyInt i;
1053 PyInt size = ListLength(self);
1054 PyInt n;
1055 PyObject *list;
1056 int reversed = 0;
1057
1058 PROC_RANGE
1059 if (first >= last)
1060 first = last;
1061
1062 n = last-first;
1063 list = PyList_New(n);
1064 if (list == NULL)
1065 return NULL;
1066
1067 for (i = 0; i < n; ++i)
1068 {
1069 PyObject *item = ListItem(self, i);
1070 if (item == NULL)
1071 {
1072 Py_DECREF(list);
1073 return NULL;
1074 }
1075
1076 if ((PyList_SetItem(list, ((reversed)?(n-i-1):(i)), item)))
1077 {
1078 Py_DECREF(item);
1079 Py_DECREF(list);
1080 return NULL;
1081 }
1082 }
1083
1084 return list;
1085 }
1086
1087 static int
1088 ListAssItem(PyObject *self, Py_ssize_t index, PyObject *obj)
1089 {
1090 typval_T tv;
1091 list_T *l = ((ListObject *) (self))->list;
1092 listitem_T *li;
1093 Py_ssize_t length = ListLength(self);
1094
1095 if (l->lv_lock)
1096 {
1097 PyErr_SetVim(_("list is locked"));
1098 return -1;
1099 }
1100 if (index>length || (index==length && obj==NULL))
1101 {
1102 PyErr_SetString(PyExc_IndexError, "list index out of range");
1103 return -1;
1104 }
1105
1106 if (obj == NULL)
1107 {
1108 li = list_find(l, (long) index);
1109 list_remove(l, li, li);
1110 clear_tv(&li->li_tv);
1111 vim_free(li);
1112 return 0;
1113 }
1114
1115 if (ConvertFromPyObject(obj, &tv) == -1)
1116 return -1;
1117
1118 if (index == length)
1119 {
1120 if (list_append_tv(l, &tv) == FAIL)
1121 {
1122 PyErr_SetVim(_("Failed to add item to list"));
1123 return -1;
1124 }
1125 }
1126 else
1127 {
1128 li = list_find(l, (long) index);
1129 clear_tv(&li->li_tv);
1130 copy_tv(&tv, &li->li_tv);
1131 }
1132 return 0;
1133 }
1134
1135 static int
1136 ListAssSlice(PyObject *self, Py_ssize_t first, Py_ssize_t last, PyObject *obj)
1137 {
1138 PyInt size = ListLength(self);
1139 Py_ssize_t i;
1140 Py_ssize_t lsize;
1141 PyObject *litem;
1142 listitem_T *li;
1143 listitem_T *next;
1144 typval_T v;
1145 list_T *l = ((ListObject *) (self))->list;
1146
1147 if (l->lv_lock)
1148 {
1149 PyErr_SetVim(_("list is locked"));
1150 return -1;
1151 }
1152
1153 PROC_RANGE
1154
1155 if (first == size)
1156 li = NULL;
1157 else
1158 {
1159 li = list_find(l, (long) first);
1160 if (li == NULL)
1161 {
1162 PyErr_SetVim(_("internal error: no vim list item"));
1163 return -1;
1164 }
1165 if (last > first)
1166 {
1167 i = last - first;
1168 while (i-- && li != NULL)
1169 {
1170 next = li->li_next;
1171 listitem_remove(l, li);
1172 li = next;
1173 }
1174 }
1175 }
1176
1177 if (obj == NULL)
1178 return 0;
1179
1180 if (!PyList_Check(obj))
1181 {
1182 PyErr_SetString(PyExc_TypeError, _("can only assign lists to slice"));
1183 return -1;
1184 }
1185
1186 lsize = PyList_Size(obj);
1187
1188 for(i=0; i<lsize; i++)
1189 {
1190 litem = PyList_GetItem(obj, i);
1191 if (litem == NULL)
1192 return -1;
1193 if (ConvertFromPyObject(litem, &v) == -1)
1194 return -1;
1195 if (list_insert_tv(l, &v, li) == FAIL)
1196 {
1197 PyErr_SetVim(_("internal error: failed to add item to list"));
1198 return -1;
1199 }
1200 }
1201 return 0;
1202 }
1203
1204 static PyObject *
1205 ListConcatInPlace(PyObject *self, PyObject *obj)
1206 {
1207 list_T *l = ((ListObject *) (self))->list;
1208 PyObject *lookup_dict;
1209
1210 if (l->lv_lock)
1211 {
1212 PyErr_SetVim(_("list is locked"));
1213 return NULL;
1214 }
1215
1216 if (!PySequence_Check(obj))
1217 {
1218 PyErr_SetString(PyExc_TypeError, _("can only concatenate with lists"));
1219 return NULL;
1220 }
1221
1222 lookup_dict = PyDict_New();
1223 if (list_py_concat(l, obj, lookup_dict) == -1)
1224 {
1225 Py_DECREF(lookup_dict);
1226 return NULL;
1227 }
1228 Py_DECREF(lookup_dict);
1229
1230 Py_INCREF(self);
1231 return self;
1232 }
1233
1234 static struct PyMethodDef ListMethods[] = {
1235 {"extend", (PyCFunction)ListConcatInPlace, METH_O, ""},
1236 { NULL, NULL, 0, NULL }
1237 };
1238
1239 typedef struct
1240 {
1241 PyObject_HEAD
1242 char_u *name;
1243 } FunctionObject;
1244
1245 static PyTypeObject FunctionType;
1246
1247 static PyObject *
1248 FunctionNew(char_u *name)
1249 {
1250 FunctionObject *self;
1251
1252 self = PyObject_NEW(FunctionObject, &FunctionType);
1253 if (self == NULL)
1254 return NULL;
1255 self->name = PyMem_New(char_u, STRLEN(name) + 1);
1256 if (self->name == NULL)
1257 {
1258 PyErr_NoMemory();
1259 return NULL;
1260 }
1261 STRCPY(self->name, name);
1262 func_ref(name);
1263 return (PyObject *)(self);
1264 }
1265
1266 static PyObject *
1267 FunctionCall(PyObject *self, PyObject *argsObject, PyObject *kwargs)
1268 {
1269 FunctionObject *this = (FunctionObject *)(self);
1270 char_u *name = this->name;
1271 typval_T args;
1272 typval_T selfdicttv;
1273 typval_T rettv;
1274 dict_T *selfdict = NULL;
1275 PyObject *selfdictObject;
1276 PyObject *result;
1277 int error;
1278
1279 if (ConvertFromPyObject(argsObject, &args) == -1)
1280 return NULL;
1281
1282 if (kwargs != NULL)
1283 {
1284 selfdictObject = PyDict_GetItemString(kwargs, "self");
1285 if (selfdictObject != NULL)
1286 {
1287 if (!PyDict_Check(selfdictObject))
1288 {
1289 PyErr_SetString(PyExc_TypeError, _("'self' argument must be a dictionary"));
1290 clear_tv(&args);
1291 return NULL;
1292 }
1293 if (ConvertFromPyObject(selfdictObject, &selfdicttv) == -1)
1294 return NULL;
1295 selfdict = selfdicttv.vval.v_dict;
1296 }
1297 }
1298
1299 error = func_call(name, &args, selfdict, &rettv);
1300 if (error != OK)
1301 {
1302 result = NULL;
1303 PyErr_SetVim(_("failed to run function"));
1304 }
1305 else
1306 result = ConvertToPyObject(&rettv);
1307
1308 /* FIXME Check what should really be cleared. */
1309 clear_tv(&args);
1310 clear_tv(&rettv);
1311 /*
1312 * if (selfdict!=NULL)
1313 * clear_tv(selfdicttv);
1314 */
1315
1316 return result;
1317 }
1318
1319 static struct PyMethodDef FunctionMethods[] = {
1320 {"__call__", (PyCFunction)FunctionCall, METH_VARARGS|METH_KEYWORDS, ""},
1321 { NULL, NULL, 0, NULL }
1322 };
507 1323
508 #define INVALID_WINDOW_VALUE ((win_T *)(-1)) 1324 #define INVALID_WINDOW_VALUE ((win_T *)(-1))
509 1325
510 static int 1326 static int
511 CheckWindow(WindowObject *this) 1327 CheckWindow(WindowObject *this)
1565 /* name, function, calling, documentation */ 2381 /* name, function, calling, documentation */
1566 {"append", RangeAppend, 1, "Append data to the Vim range" }, 2382 {"append", RangeAppend, 1, "Append data to the Vim range" },
1567 { NULL, NULL, 0, NULL } 2383 { NULL, NULL, 0, NULL }
1568 }; 2384 };
1569 2385
2386 static void
2387 set_ref_in_py(const int copyID)
2388 {
2389 pylinkedlist_T *cur;
2390 dict_T *dd;
2391 list_T *ll;
2392
2393 if (lastdict != NULL)
2394 for(cur = lastdict ; cur != NULL ; cur = cur->pll_prev)
2395 {
2396 dd = ((DictionaryObject *) (cur->pll_obj))->dict;
2397 if (dd->dv_copyID != copyID)
2398 {
2399 dd->dv_copyID = copyID;
2400 set_ref_in_ht(&dd->dv_hashtab, copyID);
2401 }
2402 }
2403
2404 if (lastlist != NULL)
2405 for(cur = lastlist ; cur != NULL ; cur = cur->pll_prev)
2406 {
2407 ll = ((ListObject *) (cur->pll_obj))->list;
2408 if (ll->lv_copyID != copyID)
2409 {
2410 ll->lv_copyID = copyID;
2411 set_ref_in_list(ll, copyID);
2412 }
2413 }
2414 }
2415
2416 static int
2417 set_string_copy(char_u *str, typval_T *tv)
2418 {
2419 tv->vval.v_string = vim_strsave(str);
2420 if (tv->vval.v_string == NULL)
2421 {
2422 PyErr_NoMemory();
2423 return -1;
2424 }
2425 return 0;
2426 }
2427
2428 #ifdef FEAT_EVAL
2429 typedef int (*pytotvfunc)(PyObject *, typval_T *, PyObject *);
2430
2431 static int
2432 convert_dl(PyObject *obj, typval_T *tv,
2433 pytotvfunc py_to_tv, PyObject *lookupDict)
2434 {
2435 PyObject *capsule;
2436 char hexBuf[sizeof(void *) * 2 + 3];
2437
2438 sprintf(hexBuf, "%p", obj);
2439
2440 capsule = PyDict_GetItemString(lookupDict, hexBuf);
2441 if (capsule == NULL)
2442 {
2443 capsule = PyCapsule_New(tv, NULL, NULL);
2444 PyDict_SetItemString(lookupDict, hexBuf, capsule);
2445 Py_DECREF(capsule);
2446 if (py_to_tv(obj, tv, lookupDict) == -1)
2447 {
2448 tv->v_type = VAR_UNKNOWN;
2449 return -1;
2450 }
2451 /* As we are not using copy_tv which increments reference count we must
2452 * do it ourself. */
2453 switch(tv->v_type)
2454 {
2455 case VAR_DICT: ++tv->vval.v_dict->dv_refcount; break;
2456 case VAR_LIST: ++tv->vval.v_list->lv_refcount; break;
2457 }
2458 }
2459 else
2460 {
2461 typval_T *v = PyCapsule_GetPointer(capsule, NULL);
2462 copy_tv(v, tv);
2463 }
2464 return 0;
2465 }
2466
2467 static int
2468 ConvertFromPyObject(PyObject *obj, typval_T *tv)
2469 {
2470 PyObject *lookup_dict;
2471 int r;
2472
2473 lookup_dict = PyDict_New();
2474 r = _ConvertFromPyObject(obj, tv, lookup_dict);
2475 Py_DECREF(lookup_dict);
2476 return r;
2477 }
2478
2479 static int
2480 _ConvertFromPyObject(PyObject *obj, typval_T *tv, PyObject *lookupDict)
2481 {
2482 if (obj->ob_type == &DictionaryType)
2483 {
2484 tv->v_type = VAR_DICT;
2485 tv->vval.v_dict = (((DictionaryObject *)(obj))->dict);
2486 ++tv->vval.v_dict->dv_refcount;
2487 }
2488 else if (obj->ob_type == &ListType)
2489 {
2490 tv->v_type = VAR_LIST;
2491 tv->vval.v_list = (((ListObject *)(obj))->list);
2492 ++tv->vval.v_list->lv_refcount;
2493 }
2494 else if (obj->ob_type == &FunctionType)
2495 {
2496 if (set_string_copy(((FunctionObject *) (obj))->name, tv) == -1)
2497 return -1;
2498
2499 tv->v_type = VAR_FUNC;
2500 func_ref(tv->vval.v_string);
2501 }
2502 #if PY_MAJOR_VERSION >= 3
2503 else if (PyBytes_Check(obj))
2504 {
2505 char_u *result = (char_u *) PyBytes_AsString(obj);
2506
2507 if (result == NULL)
2508 return -1;
2509
2510 if (set_string_copy(result, tv) == -1)
2511 return -1;
2512
2513 tv->v_type = VAR_STRING;
2514 }
2515 else if (PyUnicode_Check(obj))
2516 {
2517 PyObject *bytes;
2518 char_u *result;
2519
2520 bytes = PyString_AsBytes(obj);
2521 if (bytes == NULL)
2522 return -1;
2523
2524 result = (char_u *) PyBytes_AsString(bytes);
2525 if (result == NULL)
2526 return -1;
2527
2528 if (set_string_copy(result, tv) == -1)
2529 {
2530 Py_XDECREF(bytes);
2531 return -1;
2532 }
2533 Py_XDECREF(bytes);
2534
2535 tv->v_type = VAR_STRING;
2536 }
2537 #else
2538 else if (PyUnicode_Check(obj))
2539 {
2540 PyObject *bytes;
2541 char_u *result;
2542
2543 bytes = PyUnicode_AsEncodedString(obj, (char *)ENC_OPT, NULL);
2544 if (bytes == NULL)
2545 return -1;
2546
2547 result=(char_u *) PyString_AsString(bytes);
2548 if (result == NULL)
2549 return -1;
2550
2551 if (set_string_copy(result, tv) == -1)
2552 {
2553 Py_XDECREF(bytes);
2554 return -1;
2555 }
2556 Py_XDECREF(bytes);
2557
2558 tv->v_type = VAR_STRING;
2559 }
2560 else if (PyString_Check(obj))
2561 {
2562 char_u *result = (char_u *) PyString_AsString(obj);
2563
2564 if (result == NULL)
2565 return -1;
2566
2567 if (set_string_copy(result, tv) == -1)
2568 return -1;
2569
2570 tv->v_type = VAR_STRING;
2571 }
2572 else if (PyInt_Check(obj))
2573 {
2574 tv->v_type = VAR_NUMBER;
2575 tv->vval.v_number = (varnumber_T) PyInt_AsLong(obj);
2576 }
2577 #endif
2578 else if (PyLong_Check(obj))
2579 {
2580 tv->v_type = VAR_NUMBER;
2581 tv->vval.v_number = (varnumber_T) PyLong_AsLong(obj);
2582 }
2583 else if (PyDict_Check(obj))
2584 return convert_dl(obj, tv, pydict_to_tv, lookupDict);
2585 #ifdef FEAT_FLOAT
2586 else if (PyFloat_Check(obj))
2587 {
2588 tv->v_type = VAR_FLOAT;
2589 tv->vval.v_float = (float_T) PyFloat_AsDouble(obj);
2590 }
2591 #endif
2592 else if (PyIter_Check(obj))
2593 return convert_dl(obj, tv, pyiter_to_tv, lookupDict);
2594 else if (PySequence_Check(obj))
2595 return convert_dl(obj, tv, pyseq_to_tv, lookupDict);
2596 else if (PyMapping_Check(obj))
2597 return convert_dl(obj, tv, pymap_to_tv, lookupDict);
2598 else
2599 {
2600 PyErr_SetString(PyExc_TypeError, _("unable to convert to vim structure"));
2601 return -1;
2602 }
2603 return 0;
2604 }
2605
2606 static PyObject *
2607 ConvertToPyObject(typval_T *tv)
2608 {
2609 if (tv == NULL)
2610 {
2611 PyErr_SetVim(_("NULL reference passed"));
2612 return NULL;
2613 }
2614 switch (tv->v_type)
2615 {
2616 case VAR_STRING:
2617 return PyBytes_FromString((char *) tv->vval.v_string);
2618 case VAR_NUMBER:
2619 return PyLong_FromLong((long) tv->vval.v_number);
2620 #ifdef FEAT_FLOAT
2621 case VAR_FLOAT:
2622 return PyFloat_FromDouble((double) tv->vval.v_float);
2623 #endif
2624 case VAR_LIST:
2625 return ListNew(tv->vval.v_list);
2626 case VAR_DICT:
2627 return DictionaryNew(tv->vval.v_dict);
2628 case VAR_FUNC:
2629 return FunctionNew(tv->vval.v_string);
2630 case VAR_UNKNOWN:
2631 Py_INCREF(Py_None);
2632 return Py_None;
2633 default:
2634 PyErr_SetVim(_("internal error: invalid value type"));
2635 return NULL;
2636 }
2637 }
2638 #endif