Mercurial > vim
comparison src/if_py_both.h @ 4589:fa39483a1363 v7.3.1042
updated for version 7.3.1042
Problem: Python: can't assign to vim.Buffer.name.
Solution: Python patch 3. (ZyX)
author | Bram Moolenaar <bram@vim.org> |
---|---|
date | Wed, 29 May 2013 22:02:22 +0200 |
parents | 63c9b681c3db |
children | 0cf552b325b5 |
comparison
equal
deleted
inserted
replaced
4588:15cf120f2a68 | 4589:fa39483a1363 |
---|---|
28 | 28 |
29 #define INVALID_BUFFER_VALUE ((buf_T *)(-1)) | 29 #define INVALID_BUFFER_VALUE ((buf_T *)(-1)) |
30 #define INVALID_WINDOW_VALUE ((win_T *)(-1)) | 30 #define INVALID_WINDOW_VALUE ((win_T *)(-1)) |
31 #define INVALID_TABPAGE_VALUE ((tabpage_T *)(-1)) | 31 #define INVALID_TABPAGE_VALUE ((tabpage_T *)(-1)) |
32 | 32 |
33 #define DICTKEY_DECL \ | |
34 PyObject *dictkey_todecref; | |
35 #define DICTKEY_GET(err) \ | |
36 if (!(key = StringToChars(keyObject, &dictkey_todecref))) \ | |
37 return err; | |
38 #define DICTKEY_UNREF \ | |
39 Py_XDECREF(dictkey_todecref); | |
40 | |
33 typedef void (*rangeinitializer)(void *); | 41 typedef void (*rangeinitializer)(void *); |
34 typedef void (*runner)(const char *, void * | 42 typedef void (*runner)(const char *, void * |
35 #ifdef PY_CAN_RECURSE | 43 #ifdef PY_CAN_RECURSE |
36 , PyGILState_STATE * | 44 , PyGILState_STATE * |
37 #endif | 45 #endif |
60 * release a lock on the Vim data structures | 68 * release a lock on the Vim data structures |
61 */ | 69 */ |
62 static void | 70 static void |
63 Python_Release_Vim(void) | 71 Python_Release_Vim(void) |
64 { | 72 { |
73 } | |
74 | |
75 /* | |
76 * The "todecref" argument holds a pointer to PyObject * that must be | |
77 * DECREF'ed after returned char_u * is no longer needed or NULL if all what | |
78 * was needed to generate returned value is object. | |
79 * | |
80 * Use Py_XDECREF to decrement reference count. | |
81 */ | |
82 static char_u * | |
83 StringToChars(PyObject *object, PyObject **todecref) | |
84 { | |
85 char_u *p; | |
86 PyObject *bytes = NULL; | |
87 | |
88 if (PyBytes_Check(object)) | |
89 { | |
90 | |
91 if (PyString_AsStringAndSize(object, (char **) &p, NULL) == -1) | |
92 return NULL; | |
93 if (p == NULL) | |
94 return NULL; | |
95 | |
96 *todecref = NULL; | |
97 } | |
98 else if (PyUnicode_Check(object)) | |
99 { | |
100 bytes = PyUnicode_AsEncodedString(object, (char *)ENC_OPT, NULL); | |
101 if (bytes == NULL) | |
102 return NULL; | |
103 | |
104 if(PyString_AsStringAndSize(bytes, (char **) &p, NULL) == -1) | |
105 return NULL; | |
106 if (p == NULL) | |
107 return NULL; | |
108 | |
109 *todecref = bytes; | |
110 } | |
111 else | |
112 { | |
113 PyErr_SetString(PyExc_TypeError, _("object must be string")); | |
114 return NULL; | |
115 } | |
116 | |
117 return (char_u *) p; | |
65 } | 118 } |
66 | 119 |
67 /* Output buffer management | 120 /* Output buffer management |
68 */ | 121 */ |
69 | 122 |
1584 break; | 1637 break; |
1585 } | 1638 } |
1586 return VimTryEnd(); | 1639 return VimTryEnd(); |
1587 } | 1640 } |
1588 | 1641 |
1642 static void * | |
1643 py_memsave(void *p, size_t len) | |
1644 { | |
1645 void *r; | |
1646 if (!(r = PyMem_Malloc(len))) | |
1647 return NULL; | |
1648 mch_memmove(r, p, len); | |
1649 return r; | |
1650 } | |
1651 | |
1652 #define PY_STRSAVE(s) ((char_u *) py_memsave(s, STRLEN(s) + 1)) | |
1653 | |
1589 static int | 1654 static int |
1590 OptionsAssItem(OptionsObject *self, PyObject *keyObject, PyObject *valObject) | 1655 OptionsAssItem(OptionsObject *self, PyObject *keyObject, PyObject *valObject) |
1591 { | 1656 { |
1592 char_u *key; | 1657 char_u *key; |
1593 int flags; | 1658 int flags; |
1668 self->opt_type, self->from); | 1733 self->opt_type, self->from); |
1669 } | 1734 } |
1670 else | 1735 else |
1671 { | 1736 { |
1672 char_u *val; | 1737 char_u *val; |
1673 if (PyBytes_Check(valObject)) | 1738 PyObject *todecref; |
1674 { | 1739 |
1675 | 1740 if ((val = StringToChars(valObject, &todecref))) |
1676 if (PyString_AsStringAndSize(valObject, (char **) &val, NULL) == -1) | 1741 { |
1677 { | 1742 r = set_option_value_for(key, 0, val, opt_flags, |
1678 DICTKEY_UNREF | 1743 self->opt_type, self->from); |
1679 return -1; | 1744 Py_XDECREF(todecref); |
1680 } | |
1681 if (val == NULL) | |
1682 { | |
1683 DICTKEY_UNREF | |
1684 return -1; | |
1685 } | |
1686 | |
1687 val = vim_strsave(val); | |
1688 } | |
1689 else if (PyUnicode_Check(valObject)) | |
1690 { | |
1691 PyObject *bytes; | |
1692 | |
1693 bytes = PyUnicode_AsEncodedString(valObject, (char *)ENC_OPT, NULL); | |
1694 if (bytes == NULL) | |
1695 { | |
1696 DICTKEY_UNREF | |
1697 return -1; | |
1698 } | |
1699 | |
1700 if(PyString_AsStringAndSize(bytes, (char **) &val, NULL) == -1) | |
1701 { | |
1702 DICTKEY_UNREF | |
1703 return -1; | |
1704 } | |
1705 if (val == NULL) | |
1706 { | |
1707 DICTKEY_UNREF | |
1708 return -1; | |
1709 } | |
1710 | |
1711 val = vim_strsave(val); | |
1712 Py_XDECREF(bytes); | |
1713 } | 1745 } |
1714 else | 1746 else |
1715 { | 1747 r = -1; |
1716 PyErr_SetString(PyExc_TypeError, _("object must be string")); | |
1717 DICTKEY_UNREF | |
1718 return -1; | |
1719 } | |
1720 | |
1721 r = set_option_value_for(key, 0, val, opt_flags, | |
1722 self->opt_type, self->from); | |
1723 vim_free(val); | |
1724 } | 1748 } |
1725 | 1749 |
1726 DICTKEY_UNREF | 1750 DICTKEY_UNREF |
1727 | 1751 |
1728 return r; | 1752 return r; |
2539 | 2563 |
2540 if (new_len == 0) /* avoid allocating zero bytes */ | 2564 if (new_len == 0) /* avoid allocating zero bytes */ |
2541 array = NULL; | 2565 array = NULL; |
2542 else | 2566 else |
2543 { | 2567 { |
2544 array = (char **)alloc((unsigned)(new_len * sizeof(char *))); | 2568 array = PyMem_New(char *, new_len); |
2545 if (array == NULL) | 2569 if (array == NULL) |
2546 { | 2570 { |
2547 PyErr_NoMemory(); | 2571 PyErr_NoMemory(); |
2548 return FAIL; | 2572 return FAIL; |
2549 } | 2573 } |
2556 array[i] = StringToLine(line); | 2580 array[i] = StringToLine(line); |
2557 if (array[i] == NULL) | 2581 if (array[i] == NULL) |
2558 { | 2582 { |
2559 while (i) | 2583 while (i) |
2560 vim_free(array[--i]); | 2584 vim_free(array[--i]); |
2561 vim_free(array); | 2585 PyMem_Free(array); |
2562 return FAIL; | 2586 return FAIL; |
2563 } | 2587 } |
2564 } | 2588 } |
2565 | 2589 |
2566 VimTryStart(); | 2590 VimTryStart(); |
2633 | 2657 |
2634 /* Free the array of old_len. All of its contents have now | 2658 /* Free the array of old_len. All of its contents have now |
2635 * been dealt with (either freed, or the responsibility passed | 2659 * been dealt with (either freed, or the responsibility passed |
2636 * to vim. | 2660 * to vim. |
2637 */ | 2661 */ |
2638 vim_free(array); | 2662 PyMem_Free(array); |
2639 | 2663 |
2640 /* Adjust marks. Invalidate any which lie in the | 2664 /* Adjust marks. Invalidate any which lie in the |
2641 * changed range, and move any in the remainder of the buffer. | 2665 * changed range, and move any in the remainder of the buffer. |
2642 */ | 2666 */ |
2643 mark_adjust((linenr_T)lo, (linenr_T)(hi - 1), | 2667 mark_adjust((linenr_T)lo, (linenr_T)(hi - 1), |
2715 PyInt i; | 2739 PyInt i; |
2716 PyInt size = PyList_Size(lines); | 2740 PyInt size = PyList_Size(lines); |
2717 char **array; | 2741 char **array; |
2718 buf_T *savebuf; | 2742 buf_T *savebuf; |
2719 | 2743 |
2720 array = (char **)alloc((unsigned)(size * sizeof(char *))); | 2744 array = PyMem_New(char *, size); |
2721 if (array == NULL) | 2745 if (array == NULL) |
2722 { | 2746 { |
2723 PyErr_NoMemory(); | 2747 PyErr_NoMemory(); |
2724 return FAIL; | 2748 return FAIL; |
2725 } | 2749 } |
2731 | 2755 |
2732 if (array[i] == NULL) | 2756 if (array[i] == NULL) |
2733 { | 2757 { |
2734 while (i) | 2758 while (i) |
2735 vim_free(array[--i]); | 2759 vim_free(array[--i]); |
2736 vim_free(array); | 2760 PyMem_Free(array); |
2737 return FAIL; | 2761 return FAIL; |
2738 } | 2762 } |
2739 } | 2763 } |
2740 | 2764 |
2741 PyErr_Clear(); | 2765 PyErr_Clear(); |
2766 } | 2790 } |
2767 | 2791 |
2768 /* Free the array of lines. All of its contents have now | 2792 /* Free the array of lines. All of its contents have now |
2769 * been freed. | 2793 * been freed. |
2770 */ | 2794 */ |
2771 vim_free(array); | 2795 PyMem_Free(array); |
2772 | 2796 |
2773 restore_buffer(savebuf); | 2797 restore_buffer(savebuf); |
2774 update_screen(VALID); | 2798 update_screen(VALID); |
2775 | 2799 |
2776 if (VimTryEnd()) | 2800 if (VimTryEnd()) |
3175 (PyObject *) self); | 3199 (PyObject *) self); |
3176 else if (strcmp(name,"__members__") == 0) | 3200 else if (strcmp(name,"__members__") == 0) |
3177 return Py_BuildValue("[ssss]", "name", "number", "vars", "options"); | 3201 return Py_BuildValue("[ssss]", "name", "number", "vars", "options"); |
3178 else | 3202 else |
3179 return NULL; | 3203 return NULL; |
3204 } | |
3205 | |
3206 static int | |
3207 BufferSetattr(BufferObject *self, char *name, PyObject *valObject) | |
3208 { | |
3209 if (CheckBuffer(self)) | |
3210 return -1; | |
3211 | |
3212 if (strcmp(name, "name") == 0) | |
3213 { | |
3214 char_u *val; | |
3215 aco_save_T aco; | |
3216 int r; | |
3217 PyObject *todecref; | |
3218 | |
3219 if (!(val = StringToChars(valObject, &todecref))) | |
3220 return -1; | |
3221 | |
3222 VimTryStart(); | |
3223 /* Using aucmd_*: autocommands will be executed by rename_buffer */ | |
3224 aucmd_prepbuf(&aco, self->buf); | |
3225 r = rename_buffer(val); | |
3226 aucmd_restbuf(&aco); | |
3227 Py_XDECREF(todecref); | |
3228 if (VimTryEnd()) | |
3229 return -1; | |
3230 | |
3231 if (r == FAIL) | |
3232 { | |
3233 PyErr_SetVim(_("failed to rename buffer")); | |
3234 return -1; | |
3235 } | |
3236 return 0; | |
3237 } | |
3238 else | |
3239 { | |
3240 PyErr_SetString(PyExc_AttributeError, name); | |
3241 return -1; | |
3242 } | |
3180 } | 3243 } |
3181 | 3244 |
3182 static PyObject * | 3245 static PyObject * |
3183 BufferAppend(BufferObject *self, PyObject *args) | 3246 BufferAppend(BufferObject *self, PyObject *args) |
3184 { | 3247 { |
4038 if(PyString_AsStringAndSize(bytes, (char **) &result, NULL) == -1) | 4101 if(PyString_AsStringAndSize(bytes, (char **) &result, NULL) == -1) |
4039 return -1; | 4102 return -1; |
4040 if (result == NULL) | 4103 if (result == NULL) |
4041 return -1; | 4104 return -1; |
4042 | 4105 |
4043 if (set_string_copy(result, tv) == -1) | 4106 if (set_string_copy(result, tv)) |
4044 { | 4107 { |
4045 Py_XDECREF(bytes); | 4108 Py_XDECREF(bytes); |
4046 return -1; | 4109 return -1; |
4047 } | 4110 } |
4048 Py_XDECREF(bytes); | 4111 Py_XDECREF(bytes); |
4167 BufferType.tp_flags = Py_TPFLAGS_DEFAULT; | 4230 BufferType.tp_flags = Py_TPFLAGS_DEFAULT; |
4168 BufferType.tp_doc = "vim buffer object"; | 4231 BufferType.tp_doc = "vim buffer object"; |
4169 BufferType.tp_methods = BufferMethods; | 4232 BufferType.tp_methods = BufferMethods; |
4170 #if PY_MAJOR_VERSION >= 3 | 4233 #if PY_MAJOR_VERSION >= 3 |
4171 BufferType.tp_getattro = (getattrofunc)BufferGetattro; | 4234 BufferType.tp_getattro = (getattrofunc)BufferGetattro; |
4235 BufferType.tp_setattro = (setattrofunc)BufferSetattro; | |
4172 BufferType.tp_alloc = call_PyType_GenericAlloc; | 4236 BufferType.tp_alloc = call_PyType_GenericAlloc; |
4173 BufferType.tp_new = call_PyType_GenericNew; | 4237 BufferType.tp_new = call_PyType_GenericNew; |
4174 BufferType.tp_free = call_PyObject_Free; | 4238 BufferType.tp_free = call_PyObject_Free; |
4175 #else | 4239 #else |
4176 BufferType.tp_getattr = (getattrfunc)BufferGetattr; | 4240 BufferType.tp_getattr = (getattrfunc)BufferGetattr; |
4241 BufferType.tp_setattr = (setattrfunc)BufferSetattr; | |
4177 #endif | 4242 #endif |
4178 | 4243 |
4179 vim_memset(&WindowType, 0, sizeof(WindowType)); | 4244 vim_memset(&WindowType, 0, sizeof(WindowType)); |
4180 WindowType.tp_name = "vim.window"; | 4245 WindowType.tp_name = "vim.window"; |
4181 WindowType.tp_basicsize = sizeof(WindowObject); | 4246 WindowType.tp_basicsize = sizeof(WindowObject); |