Mercurial > vim
comparison src/if_python3.c @ 3070:880b7dd69331 v7.3.307
updated for version 7.3.307
Problem: Python 3 doesn't support slice assignment.
Solution: Implement slices. (Brett Overesch, Roland Puntaier)
author | Bram Moolenaar <bram@vim.org> |
---|---|
date | Wed, 14 Sep 2011 15:01:58 +0200 |
parents | e4f3fa1a474e |
children | 6eae1b42c668 |
comparison
equal
deleted
inserted
replaced
3069:c5ee8a44738a | 3070:880b7dd69331 |
---|---|
853 | 853 |
854 #define BufferType_Check(obj) ((obj)->ob_base.ob_type == &BufferType) | 854 #define BufferType_Check(obj) ((obj)->ob_base.ob_type == &BufferType) |
855 | 855 |
856 static Py_ssize_t BufferLength(PyObject *); | 856 static Py_ssize_t BufferLength(PyObject *); |
857 static PyObject *BufferItem(PyObject *, Py_ssize_t); | 857 static PyObject *BufferItem(PyObject *, Py_ssize_t); |
858 static PyObject* BufferSubscript(PyObject *self, PyObject* idx); | 858 static PyObject* BufferSubscript(PyObject *self, PyObject *idx); |
859 static Py_ssize_t BufferAsSubscript(PyObject *self, PyObject* idx, PyObject* val); | 859 static Py_ssize_t BufferAsSubscript(PyObject *self, PyObject *idx, PyObject *val); |
860 | 860 |
861 | 861 |
862 /* Line range type - Implementation functions | 862 /* Line range type - Implementation functions |
863 * -------------------------------------- | 863 * -------------------------------------- |
864 */ | 864 */ |
865 | 865 |
866 #define RangeType_Check(obj) ((obj)->ob_base.ob_type == &RangeType) | 866 #define RangeType_Check(obj) ((obj)->ob_base.ob_type == &RangeType) |
867 | 867 |
868 static PyObject* RangeSubscript(PyObject *self, PyObject* idx); | 868 static PyObject* RangeSubscript(PyObject *self, PyObject *idx); |
869 static Py_ssize_t RangeAsItem(PyObject *, Py_ssize_t, PyObject *); | 869 static Py_ssize_t RangeAsItem(PyObject *, Py_ssize_t, PyObject *); |
870 static Py_ssize_t RangeAsSubscript(PyObject *self, PyObject *idx, PyObject *val); | |
870 | 871 |
871 /* Current objects type - Implementation functions | 872 /* Current objects type - Implementation functions |
872 * ----------------------------------------------- | 873 * ----------------------------------------------- |
873 */ | 874 */ |
874 | 875 |
1033 (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count+1, | 1034 (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count+1, |
1034 &start, &stop, | 1035 &start, &stop, |
1035 &step, &slicelen) < 0) { | 1036 &step, &slicelen) < 0) { |
1036 return NULL; | 1037 return NULL; |
1037 } | 1038 } |
1038 return BufferSlice(self,start,stop); | 1039 return BufferSlice(self, start, stop); |
1039 } else { | 1040 } else { |
1040 PyErr_SetString(PyExc_IndexError, "Index must be int or slice"); | 1041 PyErr_SetString(PyExc_IndexError, "Index must be int or slice"); |
1041 return NULL; | 1042 return NULL; |
1042 } | 1043 } |
1043 } | 1044 } |
1082 }; | 1083 }; |
1083 | 1084 |
1084 PyMappingMethods RangeAsMapping = { | 1085 PyMappingMethods RangeAsMapping = { |
1085 /* mp_length */ (lenfunc)RangeLength, | 1086 /* mp_length */ (lenfunc)RangeLength, |
1086 /* mp_subscript */ (binaryfunc)RangeSubscript, | 1087 /* mp_subscript */ (binaryfunc)RangeSubscript, |
1087 /* mp_ass_subscript */ (objobjargproc)0, | 1088 /* mp_ass_subscript */ (objobjargproc)RangeAsSubscript, |
1088 }; | 1089 }; |
1089 | 1090 |
1090 /* Line range object - Implementation | 1091 /* Line range object - Implementation |
1091 */ | 1092 */ |
1092 | 1093 |
1121 ((RangeObject *)(self))->start, | 1122 ((RangeObject *)(self))->start, |
1122 ((RangeObject *)(self))->end, | 1123 ((RangeObject *)(self))->end, |
1123 &((RangeObject *)(self))->end); | 1124 &((RangeObject *)(self))->end); |
1124 } | 1125 } |
1125 | 1126 |
1127 static Py_ssize_t | |
1128 RangeAsSlice(PyObject *self, Py_ssize_t lo, Py_ssize_t hi, PyObject *val) | |
1129 { | |
1130 return RBAsSlice(((RangeObject *)(self))->buf, lo, hi, val, | |
1131 ((RangeObject *)(self))->start, | |
1132 ((RangeObject *)(self))->end, | |
1133 &((RangeObject *)(self))->end); | |
1134 } | |
1135 | |
1126 static PyObject * | 1136 static PyObject * |
1127 RangeSubscript(PyObject *self, PyObject* idx) | 1137 RangeSubscript(PyObject *self, PyObject* idx) |
1128 { | 1138 { |
1129 if (PyLong_Check(idx)) { | 1139 if (PyLong_Check(idx)) { |
1130 long _idx = PyLong_AsLong(idx); | 1140 long _idx = PyLong_AsLong(idx); |
1136 ((RangeObject *)(self))->end-((RangeObject *)(self))->start+1, | 1146 ((RangeObject *)(self))->end-((RangeObject *)(self))->start+1, |
1137 &start, &stop, | 1147 &start, &stop, |
1138 &step, &slicelen) < 0) { | 1148 &step, &slicelen) < 0) { |
1139 return NULL; | 1149 return NULL; |
1140 } | 1150 } |
1141 return RangeSlice(self,start,stop+1); | 1151 return RangeSlice(self, start, stop); |
1142 } else { | 1152 } else { |
1143 PyErr_SetString(PyExc_IndexError, "Index must be int or slice"); | 1153 PyErr_SetString(PyExc_IndexError, "Index must be int or slice"); |
1144 return NULL; | 1154 return NULL; |
1145 } | 1155 } |
1146 } | 1156 } |
1157 | |
1158 static Py_ssize_t | |
1159 RangeAsSubscript(PyObject *self, PyObject *idx, PyObject *val) | |
1160 { | |
1161 if (PyLong_Check(idx)) { | |
1162 long n = PyLong_AsLong(idx); | |
1163 return RangeAsItem(self, n, val); | |
1164 } else if (PySlice_Check(idx)) { | |
1165 Py_ssize_t start, stop, step, slicelen; | |
1166 | |
1167 if (PySlice_GetIndicesEx((PySliceObject *)idx, | |
1168 ((RangeObject *)(self))->end-((RangeObject *)(self))->start+1, | |
1169 &start, &stop, | |
1170 &step, &slicelen) < 0) { | |
1171 return -1; | |
1172 } | |
1173 return RangeAsSlice(self, start, stop, val); | |
1174 } else { | |
1175 PyErr_SetString(PyExc_IndexError, "Index must be int or slice"); | |
1176 return -1; | |
1177 } | |
1178 } | |
1179 | |
1147 | 1180 |
1148 /* Buffer list object - Definitions | 1181 /* Buffer list object - Definitions |
1149 */ | 1182 */ |
1150 | 1183 |
1151 typedef struct | 1184 typedef struct |