comparison src/if_python3.c @ 4377:8ec7323f417d v7.3.937

updated for version 7.3.937 Problem: More can be shared between Python 2 and 3. Solution: Move code to if_py_both.h. (ZyX)
author Bram Moolenaar <bram@vim.org>
date Sun, 12 May 2013 18:44:48 +0200
parents 04736b4030ec
children 358c10968c7f
comparison
equal deleted inserted replaced
4376:249da4a43d4c 4377:8ec7323f417d
619 if (bytes != NULL) \ 619 if (bytes != NULL) \
620 Py_XDECREF(bytes); 620 Py_XDECREF(bytes);
621 621
622 #define DESTRUCTOR_FINISH(self) Py_TYPE(self)->tp_free((PyObject*)self); 622 #define DESTRUCTOR_FINISH(self) Py_TYPE(self)->tp_free((PyObject*)self);
623 623
624 #define WIN_PYTHON_REF(win) win->w_python3_ref
625 #define BUF_PYTHON_REF(buf) buf->b_python3_ref
626
624 static void 627 static void
625 call_PyObject_Free(void *p) 628 call_PyObject_Free(void *p)
626 { 629 {
627 #ifdef Py_DEBUG 630 #ifdef Py_DEBUG
628 _PyObject_DebugFree(p); 631 _PyObject_DebugFree(p);
1065 /* mp_subscript */ (binaryfunc)BufferSubscript, 1068 /* mp_subscript */ (binaryfunc)BufferSubscript,
1066 /* mp_ass_subscript */ (objobjargproc)BufferAsSubscript, 1069 /* mp_ass_subscript */ (objobjargproc)BufferAsSubscript,
1067 }; 1070 };
1068 1071
1069 1072
1070 /* Buffer object - Definitions 1073 /* Buffer object
1071 */ 1074 */
1072
1073 static PyObject *
1074 BufferNew(buf_T *buf)
1075 {
1076 /* We need to handle deletion of buffers underneath us.
1077 * If we add a "b_python3_ref" field to the buf_T structure,
1078 * then we can get at it in buf_freeall() in vim. We then
1079 * need to create only ONE Python object per buffer - if
1080 * we try to create a second, just INCREF the existing one
1081 * and return it. The (single) Python object referring to
1082 * the buffer is stored in "b_python3_ref".
1083 * Question: what to do on a buf_freeall(). We'll probably
1084 * have to either delete the Python object (DECREF it to
1085 * zero - a bad idea, as it leaves dangling refs!) or
1086 * set the buf_T * value to an invalid value (-1?), which
1087 * means we need checks in all access functions... Bah.
1088 */
1089
1090 BufferObject *self;
1091
1092 if (buf->b_python3_ref != NULL)
1093 {
1094 self = buf->b_python3_ref;
1095 Py_INCREF(self);
1096 }
1097 else
1098 {
1099 self = PyObject_NEW(BufferObject, &BufferType);
1100 buf->b_python3_ref = self;
1101 if (self == NULL)
1102 return NULL;
1103 self->buf = buf;
1104 }
1105
1106 return (PyObject *)(self);
1107 }
1108 1075
1109 static PyObject * 1076 static PyObject *
1110 BufferGetattro(PyObject *self, PyObject*nameobj) 1077 BufferGetattro(PyObject *self, PyObject*nameobj)
1111 { 1078 {
1112 PyObject *r; 1079 PyObject *r;
1129 return Py_BuildValue("[sssss]", "name", "number", 1096 return Py_BuildValue("[sssss]", "name", "number",
1130 "append", "mark", "range"); 1097 "append", "mark", "range");
1131 } 1098 }
1132 1099
1133 /******************/ 1100 /******************/
1134
1135 static Py_ssize_t
1136 BufferLength(PyObject *self)
1137 {
1138 if (CheckBuffer((BufferObject *)(self)))
1139 return -1;
1140
1141 return (Py_ssize_t)(((BufferObject *)(self))->buf->b_ml.ml_line_count);
1142 }
1143
1144 static PyObject *
1145 BufferItem(PyObject *self, Py_ssize_t n)
1146 {
1147 return RBItem((BufferObject *)(self), n, 1,
1148 (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count);
1149 }
1150
1151 static PyObject *
1152 BufferSlice(PyObject *self, Py_ssize_t lo, Py_ssize_t hi)
1153 {
1154 return RBSlice((BufferObject *)(self), lo, hi, 1,
1155 (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count);
1156 }
1157 1101
1158 static PyObject * 1102 static PyObject *
1159 BufferSubscript(PyObject *self, PyObject* idx) 1103 BufferSubscript(PyObject *self, PyObject* idx)
1160 { 1104 {
1161 if (PyLong_Check(idx)) 1105 if (PyLong_Check(idx))
1340 1284
1341 /* Window object - Implementation 1285 /* Window object - Implementation
1342 */ 1286 */
1343 1287
1344 static PyObject * 1288 static PyObject *
1345 WindowNew(win_T *win)
1346 {
1347 /* We need to handle deletion of windows underneath us.
1348 * If we add a "w_python3_ref" field to the win_T structure,
1349 * then we can get at it in win_free() in vim. We then
1350 * need to create only ONE Python object per window - if
1351 * we try to create a second, just INCREF the existing one
1352 * and return it. The (single) Python object referring to
1353 * the window is stored in "w_python3_ref".
1354 * On a win_free() we set the Python object's win_T* field
1355 * to an invalid value. We trap all uses of a window
1356 * object, and reject them if the win_T* field is invalid.
1357 */
1358
1359 WindowObject *self;
1360
1361 if (win->w_python3_ref)
1362 {
1363 self = win->w_python3_ref;
1364 Py_INCREF(self);
1365 }
1366 else
1367 {
1368 self = PyObject_NEW(WindowObject, &WindowType);
1369 if (self == NULL)
1370 return NULL;
1371 self->win = win;
1372 win->w_python3_ref = self;
1373 }
1374
1375 return (PyObject *)(self);
1376 }
1377
1378 static PyObject *
1379 WindowGetattro(PyObject *self, PyObject *nameobj) 1289 WindowGetattro(PyObject *self, PyObject *nameobj)
1380 { 1290 {
1381 PyObject *r; 1291 PyObject *r;
1382 1292
1383 GET_ATTR_STRING(name, nameobj); 1293 GET_ATTR_STRING(name, nameobj);
1573 */ 1483 */
1574 1484
1575 void 1485 void
1576 python3_buffer_free(buf_T *buf) 1486 python3_buffer_free(buf_T *buf)
1577 { 1487 {
1578 if (buf->b_python3_ref != NULL) 1488 if (BUF_PYTHON_REF(buf) != NULL)
1579 { 1489 {
1580 BufferObject *bp = buf->b_python3_ref; 1490 BufferObject *bp = BUF_PYTHON_REF(buf);
1581 bp->buf = INVALID_BUFFER_VALUE; 1491 bp->buf = INVALID_BUFFER_VALUE;
1582 buf->b_python3_ref = NULL; 1492 BUF_PYTHON_REF(buf) = NULL;
1583 } 1493 }
1584 } 1494 }
1585 1495
1586 #if defined(FEAT_WINDOWS) || defined(PROTO) 1496 #if defined(FEAT_WINDOWS) || defined(PROTO)
1587 void 1497 void
1588 python3_window_free(win_T *win) 1498 python3_window_free(win_T *win)
1589 { 1499 {
1590 if (win->w_python3_ref != NULL) 1500 if (WIN_PYTHON_REF(win) != NULL)
1591 { 1501 {
1592 WindowObject *wp = win->w_python3_ref; 1502 WindowObject *wp = WIN_PYTHON_REF(win);
1593 wp->win = INVALID_WINDOW_VALUE; 1503 wp->win = INVALID_WINDOW_VALUE;
1594 win->w_python3_ref = NULL; 1504 WIN_PYTHON_REF(win) = NULL;
1595 } 1505 }
1596 } 1506 }
1597 #endif 1507 #endif
1598 1508
1599 static BufListObject TheBufferList = 1509 static BufListObject TheBufferList =