Mercurial > vim
annotate src/if_py_both.h @ 7589:362a686dbbae
Added tag v7.4.1094 for changeset 0738fa9c4971876a78fdcf3f87ca754b741b1e15
author | Christian Brabandt <cb@256bit.org> |
---|---|
date | Fri, 15 Jan 2016 16:00:06 +0100 |
parents | de43f10a3850 |
children | 066ef357ea91 |
rev | line source |
---|---|
3618 | 1 /* vi:set ts=8 sts=4 sw=4 noet: |
2399
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
2 * |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
3 * VIM - Vi IMproved by Bram Moolenaar |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
4 * |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
5 * Do ":help uganda" in Vim to read copying and usage conditions. |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
6 * Do ":help credits" in Vim to see a list of people who contributed. |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
7 * See README.txt for an overview of the Vim source code. |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
8 */ |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
9 /* |
4385 | 10 * Python extensions by Paul Moore, David Leonard, Roland Puntaier, Nikolay |
11 * Pavlov. | |
2399
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
12 * |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
13 * Common code for if_python.c and if_python3.c. |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
14 */ |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
15 |
5588 | 16 #ifdef __BORLANDC__ |
17 /* Disable Warning W8060: Possibly incorrect assignment in function ... */ | |
18 # pragma warn -8060 | |
19 #endif | |
20 | |
5088
34c629c3b4ba
updated for version 7.3.1287
Bram Moolenaar <bram@vim.org>
parents:
4995
diff
changeset
|
21 static char_u e_py_systemexit[] = "E880: Can't handle SystemExit of %s exception in vim"; |
34c629c3b4ba
updated for version 7.3.1287
Bram Moolenaar <bram@vim.org>
parents:
4995
diff
changeset
|
22 |
3734 | 23 #if PY_VERSION_HEX < 0x02050000 |
24 typedef int Py_ssize_t; /* Python 2.4 and earlier don't have this type. */ | |
25 #endif | |
26 | |
2919 | 27 #ifdef FEAT_MBYTE |
4966
620d9b59d4ed
updated for version 7.3.1228
Bram Moolenaar <bram@vim.org>
parents:
4964
diff
changeset
|
28 # define ENC_OPT ((char *)p_enc) |
2919 | 29 #else |
30 # define ENC_OPT "latin1" | |
31 #endif | |
4435 | 32 #define DOPY_FUNC "_vim_pydo" |
2919 | 33 |
4833
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
34 static const char *vim_special_path = "_vim_path_"; |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
35 |
4968
b6e693e1f946
updated for version 7.3.1229
Bram Moolenaar <bram@vim.org>
parents:
4966
diff
changeset
|
36 #define PyErr_SET_STRING(exc, str) PyErr_SetString(exc, _(str)) |
4385 | 37 #define PyErr_SetVim(str) PyErr_SetString(VimError, str) |
4968
b6e693e1f946
updated for version 7.3.1229
Bram Moolenaar <bram@vim.org>
parents:
4966
diff
changeset
|
38 #define PyErr_SET_VIM(str) PyErr_SET_STRING(VimError, str) |
5608 | 39 #define PyErr_FORMAT(exc, str, arg) PyErr_Format(exc, _(str), arg) |
40 #define PyErr_FORMAT2(exc, str, arg1, arg2) PyErr_Format(exc, _(str), arg1,arg2) | |
41 #define PyErr_VIM_FORMAT(str, arg) PyErr_FORMAT(VimError, str, arg) | |
4970
f5c822e5a0eb
updated for version 7.3.1230
Bram Moolenaar <bram@vim.org>
parents:
4968
diff
changeset
|
42 |
f5c822e5a0eb
updated for version 7.3.1230
Bram Moolenaar <bram@vim.org>
parents:
4968
diff
changeset
|
43 #define Py_TYPE_NAME(obj) (obj->ob_type->tp_name == NULL \ |
f5c822e5a0eb
updated for version 7.3.1230
Bram Moolenaar <bram@vim.org>
parents:
4968
diff
changeset
|
44 ? "(NULL)" \ |
f5c822e5a0eb
updated for version 7.3.1230
Bram Moolenaar <bram@vim.org>
parents:
4968
diff
changeset
|
45 : obj->ob_type->tp_name) |
4968
b6e693e1f946
updated for version 7.3.1229
Bram Moolenaar <bram@vim.org>
parents:
4966
diff
changeset
|
46 |
b6e693e1f946
updated for version 7.3.1229
Bram Moolenaar <bram@vim.org>
parents:
4966
diff
changeset
|
47 #define RAISE_NO_EMPTY_KEYS PyErr_SET_STRING(PyExc_ValueError, \ |
4978
f4969f8f66e9
updated for version 7.3.1234
Bram Moolenaar <bram@vim.org>
parents:
4976
diff
changeset
|
48 N_("empty keys are not allowed")) |
f4969f8f66e9
updated for version 7.3.1234
Bram Moolenaar <bram@vim.org>
parents:
4976
diff
changeset
|
49 #define RAISE_LOCKED_DICTIONARY PyErr_SET_VIM(N_("dictionary is locked")) |
f4969f8f66e9
updated for version 7.3.1234
Bram Moolenaar <bram@vim.org>
parents:
4976
diff
changeset
|
50 #define RAISE_LOCKED_LIST PyErr_SET_VIM(N_("list is locked")) |
f4969f8f66e9
updated for version 7.3.1234
Bram Moolenaar <bram@vim.org>
parents:
4976
diff
changeset
|
51 #define RAISE_UNDO_FAIL PyErr_SET_VIM(N_("cannot save undo information")) |
f4969f8f66e9
updated for version 7.3.1234
Bram Moolenaar <bram@vim.org>
parents:
4976
diff
changeset
|
52 #define RAISE_DELETE_LINE_FAIL PyErr_SET_VIM(N_("cannot delete line")) |
f4969f8f66e9
updated for version 7.3.1234
Bram Moolenaar <bram@vim.org>
parents:
4976
diff
changeset
|
53 #define RAISE_INSERT_LINE_FAIL PyErr_SET_VIM(N_("cannot insert line")) |
f4969f8f66e9
updated for version 7.3.1234
Bram Moolenaar <bram@vim.org>
parents:
4976
diff
changeset
|
54 #define RAISE_REPLACE_LINE_FAIL PyErr_SET_VIM(N_("cannot replace line")) |
4970
f5c822e5a0eb
updated for version 7.3.1230
Bram Moolenaar <bram@vim.org>
parents:
4968
diff
changeset
|
55 #define RAISE_KEY_ADD_FAIL(key) \ |
4978
f4969f8f66e9
updated for version 7.3.1234
Bram Moolenaar <bram@vim.org>
parents:
4976
diff
changeset
|
56 PyErr_VIM_FORMAT(N_("failed to add key '%s' to dictionary"), key) |
4970
f5c822e5a0eb
updated for version 7.3.1230
Bram Moolenaar <bram@vim.org>
parents:
4968
diff
changeset
|
57 #define RAISE_INVALID_INDEX_TYPE(idx) \ |
4978
f4969f8f66e9
updated for version 7.3.1234
Bram Moolenaar <bram@vim.org>
parents:
4976
diff
changeset
|
58 PyErr_FORMAT(PyExc_TypeError, N_("index must be int or slice, not %s"), \ |
4970
f5c822e5a0eb
updated for version 7.3.1230
Bram Moolenaar <bram@vim.org>
parents:
4968
diff
changeset
|
59 Py_TYPE_NAME(idx)); |
4659
80b0081824fa
updated for version 7.3.1077
Bram Moolenaar <bram@vim.org>
parents:
4653
diff
changeset
|
60 |
4385 | 61 #define INVALID_BUFFER_VALUE ((buf_T *)(-1)) |
62 #define INVALID_WINDOW_VALUE ((win_T *)(-1)) | |
4401 | 63 #define INVALID_TABPAGE_VALUE ((tabpage_T *)(-1)) |
4385 | 64 |
4486 | 65 typedef void (*rangeinitializer)(void *); |
4511
ce94a870b59b
updated for version 7.3.1003
Bram Moolenaar <bram@vim.org>
parents:
4509
diff
changeset
|
66 typedef void (*runner)(const char *, void * |
ce94a870b59b
updated for version 7.3.1003
Bram Moolenaar <bram@vim.org>
parents:
4509
diff
changeset
|
67 #ifdef PY_CAN_RECURSE |
ce94a870b59b
updated for version 7.3.1003
Bram Moolenaar <bram@vim.org>
parents:
4509
diff
changeset
|
68 , PyGILState_STATE * |
ce94a870b59b
updated for version 7.3.1003
Bram Moolenaar <bram@vim.org>
parents:
4509
diff
changeset
|
69 #endif |
ce94a870b59b
updated for version 7.3.1003
Bram Moolenaar <bram@vim.org>
parents:
4509
diff
changeset
|
70 ); |
4486 | 71 |
4385 | 72 static int ConvertFromPyObject(PyObject *, typval_T *); |
73 static int _ConvertFromPyObject(PyObject *, typval_T *, PyObject *); | |
4627
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
74 static int ConvertFromPyMapping(PyObject *, typval_T *); |
4431 | 75 static PyObject *WindowNew(win_T *, tabpage_T *); |
76 static PyObject *BufferNew (buf_T *); | |
77 static PyObject *LineToString(const char *); | |
4385 | 78 |
79 static PyInt RangeStart; | |
80 static PyInt RangeEnd; | |
81 | |
4486 | 82 static PyObject *globals; |
83 | |
4704
542af01979be
updated for version 7.3.1099
Bram Moolenaar <bram@vim.org>
parents:
4702
diff
changeset
|
84 static PyObject *py_chdir; |
542af01979be
updated for version 7.3.1099
Bram Moolenaar <bram@vim.org>
parents:
4702
diff
changeset
|
85 static PyObject *py_fchdir; |
542af01979be
updated for version 7.3.1099
Bram Moolenaar <bram@vim.org>
parents:
4702
diff
changeset
|
86 static PyObject *py_getcwd; |
4833
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
87 static PyObject *vim_module; |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
88 static PyObject *vim_special_path_object; |
4704
542af01979be
updated for version 7.3.1099
Bram Moolenaar <bram@vim.org>
parents:
4702
diff
changeset
|
89 |
4855
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
90 static PyObject *py_find_module; |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
91 static PyObject *py_load_module; |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
92 |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
93 static PyObject *VimError; |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
94 |
2399
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
95 /* |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
96 * obtain a lock on the Vim data structures |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
97 */ |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
98 static void |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
99 Python_Lock_Vim(void) |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
100 { |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
101 } |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
102 |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
103 /* |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
104 * release a lock on the Vim data structures |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
105 */ |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
106 static void |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
107 Python_Release_Vim(void) |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
108 { |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
109 } |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
110 |
4589
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
111 /* |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
112 * The "todecref" argument holds a pointer to PyObject * that must be |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
113 * DECREF'ed after returned char_u * is no longer needed or NULL if all what |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
114 * was needed to generate returned value is object. |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
115 * |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
116 * Use Py_XDECREF to decrement reference count. |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
117 */ |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
118 static char_u * |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
119 StringToChars(PyObject *obj, PyObject **todecref) |
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
120 { |
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
121 char_u *str; |
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
122 |
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
123 if (PyBytes_Check(obj)) |
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
124 { |
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
125 |
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
126 if (PyBytes_AsStringAndSize(obj, (char **) &str, NULL) == -1 |
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
127 || str == NULL) |
4589
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
128 return NULL; |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
129 |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
130 *todecref = NULL; |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
131 } |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
132 else if (PyUnicode_Check(obj)) |
4589
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
133 { |
4966
620d9b59d4ed
updated for version 7.3.1228
Bram Moolenaar <bram@vim.org>
parents:
4964
diff
changeset
|
134 PyObject *bytes; |
620d9b59d4ed
updated for version 7.3.1228
Bram Moolenaar <bram@vim.org>
parents:
4964
diff
changeset
|
135 |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
136 if (!(bytes = PyUnicode_AsEncodedString(obj, ENC_OPT, NULL))) |
4589
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
137 return NULL; |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
138 |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
139 if(PyBytes_AsStringAndSize(bytes, (char **) &str, NULL) == -1 |
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
140 || str == NULL) |
4966
620d9b59d4ed
updated for version 7.3.1228
Bram Moolenaar <bram@vim.org>
parents:
4964
diff
changeset
|
141 { |
620d9b59d4ed
updated for version 7.3.1228
Bram Moolenaar <bram@vim.org>
parents:
4964
diff
changeset
|
142 Py_DECREF(bytes); |
4589
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
143 return NULL; |
4966
620d9b59d4ed
updated for version 7.3.1228
Bram Moolenaar <bram@vim.org>
parents:
4964
diff
changeset
|
144 } |
4589
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
145 |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
146 *todecref = bytes; |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
147 } |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
148 else |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
149 { |
4988
e130cc3d17af
updated for version 7.3.1239
Bram Moolenaar <bram@vim.org>
parents:
4984
diff
changeset
|
150 #if PY_MAJOR_VERSION < 3 |
4970
f5c822e5a0eb
updated for version 7.3.1230
Bram Moolenaar <bram@vim.org>
parents:
4968
diff
changeset
|
151 PyErr_FORMAT(PyExc_TypeError, |
4988
e130cc3d17af
updated for version 7.3.1239
Bram Moolenaar <bram@vim.org>
parents:
4984
diff
changeset
|
152 N_("expected str() or unicode() instance, but got %s"), |
e130cc3d17af
updated for version 7.3.1239
Bram Moolenaar <bram@vim.org>
parents:
4984
diff
changeset
|
153 Py_TYPE_NAME(obj)); |
4970
f5c822e5a0eb
updated for version 7.3.1230
Bram Moolenaar <bram@vim.org>
parents:
4968
diff
changeset
|
154 #else |
4988
e130cc3d17af
updated for version 7.3.1239
Bram Moolenaar <bram@vim.org>
parents:
4984
diff
changeset
|
155 PyErr_FORMAT(PyExc_TypeError, |
e130cc3d17af
updated for version 7.3.1239
Bram Moolenaar <bram@vim.org>
parents:
4984
diff
changeset
|
156 N_("expected bytes() or str() instance, but got %s"), |
e130cc3d17af
updated for version 7.3.1239
Bram Moolenaar <bram@vim.org>
parents:
4984
diff
changeset
|
157 Py_TYPE_NAME(obj)); |
4970
f5c822e5a0eb
updated for version 7.3.1230
Bram Moolenaar <bram@vim.org>
parents:
4968
diff
changeset
|
158 #endif |
4589
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
159 return NULL; |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
160 } |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
161 |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
162 return (char_u *) str; |
4589
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
163 } |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
164 |
4972
537bbfff0c5c
updated for version 7.3.1231
Bram Moolenaar <bram@vim.org>
parents:
4970
diff
changeset
|
165 #define NUMBER_LONG 1 |
537bbfff0c5c
updated for version 7.3.1231
Bram Moolenaar <bram@vim.org>
parents:
4970
diff
changeset
|
166 #define NUMBER_INT 2 |
537bbfff0c5c
updated for version 7.3.1231
Bram Moolenaar <bram@vim.org>
parents:
4970
diff
changeset
|
167 #define NUMBER_NATURAL 4 |
537bbfff0c5c
updated for version 7.3.1231
Bram Moolenaar <bram@vim.org>
parents:
4970
diff
changeset
|
168 #define NUMBER_UNSIGNED 8 |
537bbfff0c5c
updated for version 7.3.1231
Bram Moolenaar <bram@vim.org>
parents:
4970
diff
changeset
|
169 |
537bbfff0c5c
updated for version 7.3.1231
Bram Moolenaar <bram@vim.org>
parents:
4970
diff
changeset
|
170 static int |
537bbfff0c5c
updated for version 7.3.1231
Bram Moolenaar <bram@vim.org>
parents:
4970
diff
changeset
|
171 NumberToLong(PyObject *obj, long *result, int flags) |
537bbfff0c5c
updated for version 7.3.1231
Bram Moolenaar <bram@vim.org>
parents:
4970
diff
changeset
|
172 { |
537bbfff0c5c
updated for version 7.3.1231
Bram Moolenaar <bram@vim.org>
parents:
4970
diff
changeset
|
173 #if PY_MAJOR_VERSION < 3 |
537bbfff0c5c
updated for version 7.3.1231
Bram Moolenaar <bram@vim.org>
parents:
4970
diff
changeset
|
174 if (PyInt_Check(obj)) |
537bbfff0c5c
updated for version 7.3.1231
Bram Moolenaar <bram@vim.org>
parents:
4970
diff
changeset
|
175 { |
537bbfff0c5c
updated for version 7.3.1231
Bram Moolenaar <bram@vim.org>
parents:
4970
diff
changeset
|
176 *result = PyInt_AsLong(obj); |
537bbfff0c5c
updated for version 7.3.1231
Bram Moolenaar <bram@vim.org>
parents:
4970
diff
changeset
|
177 if (PyErr_Occurred()) |
537bbfff0c5c
updated for version 7.3.1231
Bram Moolenaar <bram@vim.org>
parents:
4970
diff
changeset
|
178 return -1; |
537bbfff0c5c
updated for version 7.3.1231
Bram Moolenaar <bram@vim.org>
parents:
4970
diff
changeset
|
179 } |
537bbfff0c5c
updated for version 7.3.1231
Bram Moolenaar <bram@vim.org>
parents:
4970
diff
changeset
|
180 else |
537bbfff0c5c
updated for version 7.3.1231
Bram Moolenaar <bram@vim.org>
parents:
4970
diff
changeset
|
181 #endif |
537bbfff0c5c
updated for version 7.3.1231
Bram Moolenaar <bram@vim.org>
parents:
4970
diff
changeset
|
182 if (PyLong_Check(obj)) |
537bbfff0c5c
updated for version 7.3.1231
Bram Moolenaar <bram@vim.org>
parents:
4970
diff
changeset
|
183 { |
537bbfff0c5c
updated for version 7.3.1231
Bram Moolenaar <bram@vim.org>
parents:
4970
diff
changeset
|
184 *result = PyLong_AsLong(obj); |
537bbfff0c5c
updated for version 7.3.1231
Bram Moolenaar <bram@vim.org>
parents:
4970
diff
changeset
|
185 if (PyErr_Occurred()) |
537bbfff0c5c
updated for version 7.3.1231
Bram Moolenaar <bram@vim.org>
parents:
4970
diff
changeset
|
186 return -1; |
537bbfff0c5c
updated for version 7.3.1231
Bram Moolenaar <bram@vim.org>
parents:
4970
diff
changeset
|
187 } |
537bbfff0c5c
updated for version 7.3.1231
Bram Moolenaar <bram@vim.org>
parents:
4970
diff
changeset
|
188 else if (PyNumber_Check(obj)) |
537bbfff0c5c
updated for version 7.3.1231
Bram Moolenaar <bram@vim.org>
parents:
4970
diff
changeset
|
189 { |
537bbfff0c5c
updated for version 7.3.1231
Bram Moolenaar <bram@vim.org>
parents:
4970
diff
changeset
|
190 PyObject *num; |
537bbfff0c5c
updated for version 7.3.1231
Bram Moolenaar <bram@vim.org>
parents:
4970
diff
changeset
|
191 |
537bbfff0c5c
updated for version 7.3.1231
Bram Moolenaar <bram@vim.org>
parents:
4970
diff
changeset
|
192 if (!(num = PyNumber_Long(obj))) |
537bbfff0c5c
updated for version 7.3.1231
Bram Moolenaar <bram@vim.org>
parents:
4970
diff
changeset
|
193 return -1; |
537bbfff0c5c
updated for version 7.3.1231
Bram Moolenaar <bram@vim.org>
parents:
4970
diff
changeset
|
194 |
537bbfff0c5c
updated for version 7.3.1231
Bram Moolenaar <bram@vim.org>
parents:
4970
diff
changeset
|
195 *result = PyLong_AsLong(num); |
537bbfff0c5c
updated for version 7.3.1231
Bram Moolenaar <bram@vim.org>
parents:
4970
diff
changeset
|
196 |
537bbfff0c5c
updated for version 7.3.1231
Bram Moolenaar <bram@vim.org>
parents:
4970
diff
changeset
|
197 Py_DECREF(num); |
537bbfff0c5c
updated for version 7.3.1231
Bram Moolenaar <bram@vim.org>
parents:
4970
diff
changeset
|
198 |
537bbfff0c5c
updated for version 7.3.1231
Bram Moolenaar <bram@vim.org>
parents:
4970
diff
changeset
|
199 if (PyErr_Occurred()) |
537bbfff0c5c
updated for version 7.3.1231
Bram Moolenaar <bram@vim.org>
parents:
4970
diff
changeset
|
200 return -1; |
537bbfff0c5c
updated for version 7.3.1231
Bram Moolenaar <bram@vim.org>
parents:
4970
diff
changeset
|
201 } |
537bbfff0c5c
updated for version 7.3.1231
Bram Moolenaar <bram@vim.org>
parents:
4970
diff
changeset
|
202 else |
537bbfff0c5c
updated for version 7.3.1231
Bram Moolenaar <bram@vim.org>
parents:
4970
diff
changeset
|
203 { |
4988
e130cc3d17af
updated for version 7.3.1239
Bram Moolenaar <bram@vim.org>
parents:
4984
diff
changeset
|
204 #if PY_MAJOR_VERSION < 3 |
4972
537bbfff0c5c
updated for version 7.3.1231
Bram Moolenaar <bram@vim.org>
parents:
4970
diff
changeset
|
205 PyErr_FORMAT(PyExc_TypeError, |
4978
f4969f8f66e9
updated for version 7.3.1234
Bram Moolenaar <bram@vim.org>
parents:
4976
diff
changeset
|
206 N_("expected int(), long() or something supporting " |
4988
e130cc3d17af
updated for version 7.3.1239
Bram Moolenaar <bram@vim.org>
parents:
4984
diff
changeset
|
207 "coercing to long(), but got %s"), |
e130cc3d17af
updated for version 7.3.1239
Bram Moolenaar <bram@vim.org>
parents:
4984
diff
changeset
|
208 Py_TYPE_NAME(obj)); |
4972
537bbfff0c5c
updated for version 7.3.1231
Bram Moolenaar <bram@vim.org>
parents:
4970
diff
changeset
|
209 #else |
4988
e130cc3d17af
updated for version 7.3.1239
Bram Moolenaar <bram@vim.org>
parents:
4984
diff
changeset
|
210 PyErr_FORMAT(PyExc_TypeError, |
4978
f4969f8f66e9
updated for version 7.3.1234
Bram Moolenaar <bram@vim.org>
parents:
4976
diff
changeset
|
211 N_("expected int() or something supporting coercing to int(), " |
4988
e130cc3d17af
updated for version 7.3.1239
Bram Moolenaar <bram@vim.org>
parents:
4984
diff
changeset
|
212 "but got %s"), |
e130cc3d17af
updated for version 7.3.1239
Bram Moolenaar <bram@vim.org>
parents:
4984
diff
changeset
|
213 Py_TYPE_NAME(obj)); |
4972
537bbfff0c5c
updated for version 7.3.1231
Bram Moolenaar <bram@vim.org>
parents:
4970
diff
changeset
|
214 #endif |
537bbfff0c5c
updated for version 7.3.1231
Bram Moolenaar <bram@vim.org>
parents:
4970
diff
changeset
|
215 return -1; |
537bbfff0c5c
updated for version 7.3.1231
Bram Moolenaar <bram@vim.org>
parents:
4970
diff
changeset
|
216 } |
537bbfff0c5c
updated for version 7.3.1231
Bram Moolenaar <bram@vim.org>
parents:
4970
diff
changeset
|
217 |
537bbfff0c5c
updated for version 7.3.1231
Bram Moolenaar <bram@vim.org>
parents:
4970
diff
changeset
|
218 if (flags & NUMBER_INT) |
537bbfff0c5c
updated for version 7.3.1231
Bram Moolenaar <bram@vim.org>
parents:
4970
diff
changeset
|
219 { |
537bbfff0c5c
updated for version 7.3.1231
Bram Moolenaar <bram@vim.org>
parents:
4970
diff
changeset
|
220 if (*result > INT_MAX) |
537bbfff0c5c
updated for version 7.3.1231
Bram Moolenaar <bram@vim.org>
parents:
4970
diff
changeset
|
221 { |
537bbfff0c5c
updated for version 7.3.1231
Bram Moolenaar <bram@vim.org>
parents:
4970
diff
changeset
|
222 PyErr_SET_STRING(PyExc_OverflowError, |
4978
f4969f8f66e9
updated for version 7.3.1234
Bram Moolenaar <bram@vim.org>
parents:
4976
diff
changeset
|
223 N_("value is too large to fit into C int type")); |
4972
537bbfff0c5c
updated for version 7.3.1231
Bram Moolenaar <bram@vim.org>
parents:
4970
diff
changeset
|
224 return -1; |
537bbfff0c5c
updated for version 7.3.1231
Bram Moolenaar <bram@vim.org>
parents:
4970
diff
changeset
|
225 } |
537bbfff0c5c
updated for version 7.3.1231
Bram Moolenaar <bram@vim.org>
parents:
4970
diff
changeset
|
226 else if (*result < INT_MIN) |
537bbfff0c5c
updated for version 7.3.1231
Bram Moolenaar <bram@vim.org>
parents:
4970
diff
changeset
|
227 { |
537bbfff0c5c
updated for version 7.3.1231
Bram Moolenaar <bram@vim.org>
parents:
4970
diff
changeset
|
228 PyErr_SET_STRING(PyExc_OverflowError, |
4978
f4969f8f66e9
updated for version 7.3.1234
Bram Moolenaar <bram@vim.org>
parents:
4976
diff
changeset
|
229 N_("value is too small to fit into C int type")); |
4972
537bbfff0c5c
updated for version 7.3.1231
Bram Moolenaar <bram@vim.org>
parents:
4970
diff
changeset
|
230 return -1; |
537bbfff0c5c
updated for version 7.3.1231
Bram Moolenaar <bram@vim.org>
parents:
4970
diff
changeset
|
231 } |
537bbfff0c5c
updated for version 7.3.1231
Bram Moolenaar <bram@vim.org>
parents:
4970
diff
changeset
|
232 } |
537bbfff0c5c
updated for version 7.3.1231
Bram Moolenaar <bram@vim.org>
parents:
4970
diff
changeset
|
233 |
537bbfff0c5c
updated for version 7.3.1231
Bram Moolenaar <bram@vim.org>
parents:
4970
diff
changeset
|
234 if (flags & NUMBER_NATURAL) |
537bbfff0c5c
updated for version 7.3.1231
Bram Moolenaar <bram@vim.org>
parents:
4970
diff
changeset
|
235 { |
537bbfff0c5c
updated for version 7.3.1231
Bram Moolenaar <bram@vim.org>
parents:
4970
diff
changeset
|
236 if (*result <= 0) |
537bbfff0c5c
updated for version 7.3.1231
Bram Moolenaar <bram@vim.org>
parents:
4970
diff
changeset
|
237 { |
537bbfff0c5c
updated for version 7.3.1231
Bram Moolenaar <bram@vim.org>
parents:
4970
diff
changeset
|
238 PyErr_SET_STRING(PyExc_ValueError, |
5695 | 239 N_("number must be greater than zero")); |
4972
537bbfff0c5c
updated for version 7.3.1231
Bram Moolenaar <bram@vim.org>
parents:
4970
diff
changeset
|
240 return -1; |
537bbfff0c5c
updated for version 7.3.1231
Bram Moolenaar <bram@vim.org>
parents:
4970
diff
changeset
|
241 } |
537bbfff0c5c
updated for version 7.3.1231
Bram Moolenaar <bram@vim.org>
parents:
4970
diff
changeset
|
242 } |
537bbfff0c5c
updated for version 7.3.1231
Bram Moolenaar <bram@vim.org>
parents:
4970
diff
changeset
|
243 else if (flags & NUMBER_UNSIGNED) |
537bbfff0c5c
updated for version 7.3.1231
Bram Moolenaar <bram@vim.org>
parents:
4970
diff
changeset
|
244 { |
537bbfff0c5c
updated for version 7.3.1231
Bram Moolenaar <bram@vim.org>
parents:
4970
diff
changeset
|
245 if (*result < 0) |
537bbfff0c5c
updated for version 7.3.1231
Bram Moolenaar <bram@vim.org>
parents:
4970
diff
changeset
|
246 { |
537bbfff0c5c
updated for version 7.3.1231
Bram Moolenaar <bram@vim.org>
parents:
4970
diff
changeset
|
247 PyErr_SET_STRING(PyExc_ValueError, |
4978
f4969f8f66e9
updated for version 7.3.1234
Bram Moolenaar <bram@vim.org>
parents:
4976
diff
changeset
|
248 N_("number must be greater or equal to zero")); |
4972
537bbfff0c5c
updated for version 7.3.1231
Bram Moolenaar <bram@vim.org>
parents:
4970
diff
changeset
|
249 return -1; |
537bbfff0c5c
updated for version 7.3.1231
Bram Moolenaar <bram@vim.org>
parents:
4970
diff
changeset
|
250 } |
537bbfff0c5c
updated for version 7.3.1231
Bram Moolenaar <bram@vim.org>
parents:
4970
diff
changeset
|
251 } |
537bbfff0c5c
updated for version 7.3.1231
Bram Moolenaar <bram@vim.org>
parents:
4970
diff
changeset
|
252 |
537bbfff0c5c
updated for version 7.3.1231
Bram Moolenaar <bram@vim.org>
parents:
4970
diff
changeset
|
253 return 0; |
537bbfff0c5c
updated for version 7.3.1231
Bram Moolenaar <bram@vim.org>
parents:
4970
diff
changeset
|
254 } |
537bbfff0c5c
updated for version 7.3.1231
Bram Moolenaar <bram@vim.org>
parents:
4970
diff
changeset
|
255 |
4599
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
256 static int |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
257 add_string(PyObject *list, char *s) |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
258 { |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
259 PyObject *string; |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
260 |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
261 if (!(string = PyString_FromString(s))) |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
262 return -1; |
4966
620d9b59d4ed
updated for version 7.3.1228
Bram Moolenaar <bram@vim.org>
parents:
4964
diff
changeset
|
263 |
4599
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
264 if (PyList_Append(list, string)) |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
265 { |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
266 Py_DECREF(string); |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
267 return -1; |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
268 } |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
269 |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
270 Py_DECREF(string); |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
271 return 0; |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
272 } |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
273 |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
274 static PyObject * |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
275 ObjectDir(PyObject *self, char **attributes) |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
276 { |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
277 PyMethodDef *method; |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
278 char **attr; |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
279 PyObject *ret; |
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
280 |
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
281 if (!(ret = PyList_New(0))) |
4599
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
282 return NULL; |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
283 |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
284 if (self) |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
285 for (method = self->ob_type->tp_methods ; method->ml_name != NULL ; ++method) |
5088
34c629c3b4ba
updated for version 7.3.1287
Bram Moolenaar <bram@vim.org>
parents:
4995
diff
changeset
|
286 if (add_string(ret, (char *)method->ml_name)) |
4599
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
287 { |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
288 Py_DECREF(ret); |
4599
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
289 return NULL; |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
290 } |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
291 |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
292 for (attr = attributes ; *attr ; ++attr) |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
293 if (add_string(ret, *attr)) |
4599
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
294 { |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
295 Py_DECREF(ret); |
4599
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
296 return NULL; |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
297 } |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
298 |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
299 #if PY_MAJOR_VERSION < 3 |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
300 if (add_string(ret, "__members__")) |
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
301 { |
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
302 Py_DECREF(ret); |
4599
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
303 return NULL; |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
304 } |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
305 #endif |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
306 |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
307 return ret; |
4599
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
308 } |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
309 |
4385 | 310 /* Output buffer management |
2399
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
311 */ |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
312 |
2582 | 313 /* Function to write a line, points to either msg() or emsg(). */ |
2399
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
314 typedef void (*writefn)(char_u *); |
4385 | 315 |
316 static PyTypeObject OutputType; | |
2399
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
317 |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
318 typedef struct |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
319 { |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
320 PyObject_HEAD |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
321 long softspace; |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
322 long error; |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
323 } OutputObject; |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
324 |
4599
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
325 static char *OutputAttrs[] = { |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
326 "softspace", |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
327 NULL |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
328 }; |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
329 |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
330 static PyObject * |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
331 OutputDir(PyObject *self) |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
332 { |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
333 return ObjectDir(self, OutputAttrs); |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
334 } |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
335 |
3826 | 336 static int |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
337 OutputSetattr(OutputObject *self, char *name, PyObject *valObject) |
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
338 { |
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
339 if (valObject == NULL) |
3826 | 340 { |
4968
b6e693e1f946
updated for version 7.3.1229
Bram Moolenaar <bram@vim.org>
parents:
4966
diff
changeset
|
341 PyErr_SET_STRING(PyExc_AttributeError, |
4978
f4969f8f66e9
updated for version 7.3.1234
Bram Moolenaar <bram@vim.org>
parents:
4976
diff
changeset
|
342 N_("can't delete OutputObject attributes")); |
3826 | 343 return -1; |
344 } | |
345 | |
346 if (strcmp(name, "softspace") == 0) | |
347 { | |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
348 if (NumberToLong(valObject, &(self->softspace), NUMBER_UNSIGNED)) |
3826 | 349 return -1; |
350 return 0; | |
351 } | |
352 | |
4978
f4969f8f66e9
updated for version 7.3.1234
Bram Moolenaar <bram@vim.org>
parents:
4976
diff
changeset
|
353 PyErr_FORMAT(PyExc_AttributeError, N_("invalid attribute: %s"), name); |
3826 | 354 return -1; |
355 } | |
356 | |
4385 | 357 /* Buffer IO, we write one whole line at a time. */ |
358 static garray_T io_ga = {0, 0, 1, 80, NULL}; | |
359 static writefn old_fn = NULL; | |
360 | |
361 static void | |
362 PythonIO_Flush(void) | |
363 { | |
364 if (old_fn != NULL && io_ga.ga_len > 0) | |
365 { | |
366 ((char_u *)io_ga.ga_data)[io_ga.ga_len] = NUL; | |
367 old_fn((char_u *)io_ga.ga_data); | |
368 } | |
369 io_ga.ga_len = 0; | |
370 } | |
371 | |
372 static void | |
373 writer(writefn fn, char_u *str, PyInt n) | |
374 { | |
375 char_u *ptr; | |
376 | |
377 /* Flush when switching output function. */ | |
378 if (fn != old_fn) | |
379 PythonIO_Flush(); | |
380 old_fn = fn; | |
381 | |
382 /* Write each NL separated line. Text after the last NL is kept for | |
383 * writing later. */ | |
384 while (n > 0 && (ptr = memchr(str, '\n', n)) != NULL) | |
385 { | |
386 PyInt len = ptr - str; | |
387 | |
388 if (ga_grow(&io_ga, (int)(len + 1)) == FAIL) | |
389 break; | |
390 | |
391 mch_memmove(((char *)io_ga.ga_data) + io_ga.ga_len, str, (size_t)len); | |
392 ((char *)io_ga.ga_data)[io_ga.ga_len + len] = NUL; | |
393 fn((char_u *)io_ga.ga_data); | |
394 str = ptr + 1; | |
395 n -= len + 1; | |
396 io_ga.ga_len = 0; | |
397 } | |
398 | |
399 /* Put the remaining text into io_ga for later printing. */ | |
400 if (n > 0 && ga_grow(&io_ga, (int)(n + 1)) == OK) | |
401 { | |
402 mch_memmove(((char *)io_ga.ga_data) + io_ga.ga_len, str, (size_t)n); | |
403 io_ga.ga_len += (int)n; | |
404 } | |
405 } | |
406 | |
4962
b34d719b13cd
updated for version 7.3.1226
Bram Moolenaar <bram@vim.org>
parents:
4922
diff
changeset
|
407 static int |
b34d719b13cd
updated for version 7.3.1226
Bram Moolenaar <bram@vim.org>
parents:
4922
diff
changeset
|
408 write_output(OutputObject *self, PyObject *string) |
b34d719b13cd
updated for version 7.3.1226
Bram Moolenaar <bram@vim.org>
parents:
4922
diff
changeset
|
409 { |
b34d719b13cd
updated for version 7.3.1226
Bram Moolenaar <bram@vim.org>
parents:
4922
diff
changeset
|
410 Py_ssize_t len = 0; |
b34d719b13cd
updated for version 7.3.1226
Bram Moolenaar <bram@vim.org>
parents:
4922
diff
changeset
|
411 char *str = NULL; |
b34d719b13cd
updated for version 7.3.1226
Bram Moolenaar <bram@vim.org>
parents:
4922
diff
changeset
|
412 int error = self->error; |
b34d719b13cd
updated for version 7.3.1226
Bram Moolenaar <bram@vim.org>
parents:
4922
diff
changeset
|
413 |
b34d719b13cd
updated for version 7.3.1226
Bram Moolenaar <bram@vim.org>
parents:
4922
diff
changeset
|
414 if (!PyArg_Parse(string, "et#", ENC_OPT, &str, &len)) |
b34d719b13cd
updated for version 7.3.1226
Bram Moolenaar <bram@vim.org>
parents:
4922
diff
changeset
|
415 return -1; |
2399
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
416 |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
417 Py_BEGIN_ALLOW_THREADS |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
418 Python_Lock_Vim(); |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
419 writer((writefn)(error ? emsg : msg), (char_u *)str, len); |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
420 Python_Release_Vim(); |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
421 Py_END_ALLOW_THREADS |
2894 | 422 PyMem_Free(str); |
2399
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
423 |
4962
b34d719b13cd
updated for version 7.3.1226
Bram Moolenaar <bram@vim.org>
parents:
4922
diff
changeset
|
424 return 0; |
b34d719b13cd
updated for version 7.3.1226
Bram Moolenaar <bram@vim.org>
parents:
4922
diff
changeset
|
425 } |
b34d719b13cd
updated for version 7.3.1226
Bram Moolenaar <bram@vim.org>
parents:
4922
diff
changeset
|
426 |
b34d719b13cd
updated for version 7.3.1226
Bram Moolenaar <bram@vim.org>
parents:
4922
diff
changeset
|
427 static PyObject * |
b34d719b13cd
updated for version 7.3.1226
Bram Moolenaar <bram@vim.org>
parents:
4922
diff
changeset
|
428 OutputWrite(OutputObject *self, PyObject *string) |
b34d719b13cd
updated for version 7.3.1226
Bram Moolenaar <bram@vim.org>
parents:
4922
diff
changeset
|
429 { |
b34d719b13cd
updated for version 7.3.1226
Bram Moolenaar <bram@vim.org>
parents:
4922
diff
changeset
|
430 if (write_output(self, string)) |
b34d719b13cd
updated for version 7.3.1226
Bram Moolenaar <bram@vim.org>
parents:
4922
diff
changeset
|
431 return NULL; |
b34d719b13cd
updated for version 7.3.1226
Bram Moolenaar <bram@vim.org>
parents:
4922
diff
changeset
|
432 |
2399
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
433 Py_INCREF(Py_None); |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
434 return Py_None; |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
435 } |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
436 |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
437 static PyObject * |
4962
b34d719b13cd
updated for version 7.3.1226
Bram Moolenaar <bram@vim.org>
parents:
4922
diff
changeset
|
438 OutputWritelines(OutputObject *self, PyObject *seq) |
b34d719b13cd
updated for version 7.3.1226
Bram Moolenaar <bram@vim.org>
parents:
4922
diff
changeset
|
439 { |
4619
90beab957ba9
updated for version 7.3.1057
Bram Moolenaar <bram@vim.org>
parents:
4617
diff
changeset
|
440 PyObject *iterator; |
90beab957ba9
updated for version 7.3.1057
Bram Moolenaar <bram@vim.org>
parents:
4617
diff
changeset
|
441 PyObject *item; |
90beab957ba9
updated for version 7.3.1057
Bram Moolenaar <bram@vim.org>
parents:
4617
diff
changeset
|
442 |
90beab957ba9
updated for version 7.3.1057
Bram Moolenaar <bram@vim.org>
parents:
4617
diff
changeset
|
443 if (!(iterator = PyObject_GetIter(seq))) |
90beab957ba9
updated for version 7.3.1057
Bram Moolenaar <bram@vim.org>
parents:
4617
diff
changeset
|
444 return NULL; |
90beab957ba9
updated for version 7.3.1057
Bram Moolenaar <bram@vim.org>
parents:
4617
diff
changeset
|
445 |
90beab957ba9
updated for version 7.3.1057
Bram Moolenaar <bram@vim.org>
parents:
4617
diff
changeset
|
446 while ((item = PyIter_Next(iterator))) |
3618 | 447 { |
4962
b34d719b13cd
updated for version 7.3.1226
Bram Moolenaar <bram@vim.org>
parents:
4922
diff
changeset
|
448 if (write_output(self, item)) |
3618 | 449 { |
4619
90beab957ba9
updated for version 7.3.1057
Bram Moolenaar <bram@vim.org>
parents:
4617
diff
changeset
|
450 Py_DECREF(iterator); |
90beab957ba9
updated for version 7.3.1057
Bram Moolenaar <bram@vim.org>
parents:
4617
diff
changeset
|
451 Py_DECREF(item); |
2399
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
452 return NULL; |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
453 } |
4619
90beab957ba9
updated for version 7.3.1057
Bram Moolenaar <bram@vim.org>
parents:
4617
diff
changeset
|
454 Py_DECREF(item); |
2399
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
455 } |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
456 |
4619
90beab957ba9
updated for version 7.3.1057
Bram Moolenaar <bram@vim.org>
parents:
4617
diff
changeset
|
457 Py_DECREF(iterator); |
90beab957ba9
updated for version 7.3.1057
Bram Moolenaar <bram@vim.org>
parents:
4617
diff
changeset
|
458 |
90beab957ba9
updated for version 7.3.1057
Bram Moolenaar <bram@vim.org>
parents:
4617
diff
changeset
|
459 /* Iterator may have finished due to an exception */ |
90beab957ba9
updated for version 7.3.1057
Bram Moolenaar <bram@vim.org>
parents:
4617
diff
changeset
|
460 if (PyErr_Occurred()) |
90beab957ba9
updated for version 7.3.1057
Bram Moolenaar <bram@vim.org>
parents:
4617
diff
changeset
|
461 return NULL; |
90beab957ba9
updated for version 7.3.1057
Bram Moolenaar <bram@vim.org>
parents:
4617
diff
changeset
|
462 |
2399
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
463 Py_INCREF(Py_None); |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
464 return Py_None; |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
465 } |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
466 |
2727 | 467 static PyObject * |
7191
692dac7183de
commit https://github.com/vim/vim/commit/d424747d5821c2873e24d25d3407d08b25ea3443
Christian Brabandt <cb@256bit.org>
parents:
6598
diff
changeset
|
468 AlwaysNone(PyObject *self UNUSED) |
2727 | 469 { |
470 /* do nothing */ | |
471 Py_INCREF(Py_None); | |
472 return Py_None; | |
473 } | |
474 | |
7191
692dac7183de
commit https://github.com/vim/vim/commit/d424747d5821c2873e24d25d3407d08b25ea3443
Christian Brabandt <cb@256bit.org>
parents:
6598
diff
changeset
|
475 static PyObject * |
692dac7183de
commit https://github.com/vim/vim/commit/d424747d5821c2873e24d25d3407d08b25ea3443
Christian Brabandt <cb@256bit.org>
parents:
6598
diff
changeset
|
476 AlwaysFalse(PyObject *self UNUSED) |
692dac7183de
commit https://github.com/vim/vim/commit/d424747d5821c2873e24d25d3407d08b25ea3443
Christian Brabandt <cb@256bit.org>
parents:
6598
diff
changeset
|
477 { |
692dac7183de
commit https://github.com/vim/vim/commit/d424747d5821c2873e24d25d3407d08b25ea3443
Christian Brabandt <cb@256bit.org>
parents:
6598
diff
changeset
|
478 /* do nothing */ |
7202
de43f10a3850
commit https://github.com/vim/vim/commit/e7427f4b7e1af6a63600183be6b4c5724beb2f66
Christian Brabandt <cb@256bit.org>
parents:
7191
diff
changeset
|
479 PyObject *ret = Py_False; |
de43f10a3850
commit https://github.com/vim/vim/commit/e7427f4b7e1af6a63600183be6b4c5724beb2f66
Christian Brabandt <cb@256bit.org>
parents:
7191
diff
changeset
|
480 Py_INCREF(ret); |
de43f10a3850
commit https://github.com/vim/vim/commit/e7427f4b7e1af6a63600183be6b4c5724beb2f66
Christian Brabandt <cb@256bit.org>
parents:
7191
diff
changeset
|
481 return ret; |
7191
692dac7183de
commit https://github.com/vim/vim/commit/d424747d5821c2873e24d25d3407d08b25ea3443
Christian Brabandt <cb@256bit.org>
parents:
6598
diff
changeset
|
482 } |
692dac7183de
commit https://github.com/vim/vim/commit/d424747d5821c2873e24d25d3407d08b25ea3443
Christian Brabandt <cb@256bit.org>
parents:
6598
diff
changeset
|
483 |
692dac7183de
commit https://github.com/vim/vim/commit/d424747d5821c2873e24d25d3407d08b25ea3443
Christian Brabandt <cb@256bit.org>
parents:
6598
diff
changeset
|
484 static PyObject * |
692dac7183de
commit https://github.com/vim/vim/commit/d424747d5821c2873e24d25d3407d08b25ea3443
Christian Brabandt <cb@256bit.org>
parents:
6598
diff
changeset
|
485 AlwaysTrue(PyObject *self UNUSED) |
692dac7183de
commit https://github.com/vim/vim/commit/d424747d5821c2873e24d25d3407d08b25ea3443
Christian Brabandt <cb@256bit.org>
parents:
6598
diff
changeset
|
486 { |
692dac7183de
commit https://github.com/vim/vim/commit/d424747d5821c2873e24d25d3407d08b25ea3443
Christian Brabandt <cb@256bit.org>
parents:
6598
diff
changeset
|
487 /* do nothing */ |
7202
de43f10a3850
commit https://github.com/vim/vim/commit/e7427f4b7e1af6a63600183be6b4c5724beb2f66
Christian Brabandt <cb@256bit.org>
parents:
7191
diff
changeset
|
488 PyObject *ret = Py_True; |
de43f10a3850
commit https://github.com/vim/vim/commit/e7427f4b7e1af6a63600183be6b4c5724beb2f66
Christian Brabandt <cb@256bit.org>
parents:
7191
diff
changeset
|
489 Py_INCREF(ret); |
de43f10a3850
commit https://github.com/vim/vim/commit/e7427f4b7e1af6a63600183be6b4c5724beb2f66
Christian Brabandt <cb@256bit.org>
parents:
7191
diff
changeset
|
490 return ret; |
7191
692dac7183de
commit https://github.com/vim/vim/commit/d424747d5821c2873e24d25d3407d08b25ea3443
Christian Brabandt <cb@256bit.org>
parents:
6598
diff
changeset
|
491 } |
692dac7183de
commit https://github.com/vim/vim/commit/d424747d5821c2873e24d25d3407d08b25ea3443
Christian Brabandt <cb@256bit.org>
parents:
6598
diff
changeset
|
492 |
2399
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
493 /***************/ |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
494 |
4385 | 495 static struct PyMethodDef OutputMethods[] = { |
4492 | 496 /* name, function, calling, doc */ |
4962
b34d719b13cd
updated for version 7.3.1226
Bram Moolenaar <bram@vim.org>
parents:
4922
diff
changeset
|
497 {"write", (PyCFunction)OutputWrite, METH_O, ""}, |
b34d719b13cd
updated for version 7.3.1226
Bram Moolenaar <bram@vim.org>
parents:
4922
diff
changeset
|
498 {"writelines", (PyCFunction)OutputWritelines, METH_O, ""}, |
7191
692dac7183de
commit https://github.com/vim/vim/commit/d424747d5821c2873e24d25d3407d08b25ea3443
Christian Brabandt <cb@256bit.org>
parents:
6598
diff
changeset
|
499 {"flush", (PyCFunction)AlwaysNone, METH_NOARGS, ""}, |
692dac7183de
commit https://github.com/vim/vim/commit/d424747d5821c2873e24d25d3407d08b25ea3443
Christian Brabandt <cb@256bit.org>
parents:
6598
diff
changeset
|
500 {"close", (PyCFunction)AlwaysNone, METH_NOARGS, ""}, |
692dac7183de
commit https://github.com/vim/vim/commit/d424747d5821c2873e24d25d3407d08b25ea3443
Christian Brabandt <cb@256bit.org>
parents:
6598
diff
changeset
|
501 {"isatty", (PyCFunction)AlwaysFalse, METH_NOARGS, ""}, |
692dac7183de
commit https://github.com/vim/vim/commit/d424747d5821c2873e24d25d3407d08b25ea3443
Christian Brabandt <cb@256bit.org>
parents:
6598
diff
changeset
|
502 {"readable", (PyCFunction)AlwaysFalse, METH_NOARGS, ""}, |
692dac7183de
commit https://github.com/vim/vim/commit/d424747d5821c2873e24d25d3407d08b25ea3443
Christian Brabandt <cb@256bit.org>
parents:
6598
diff
changeset
|
503 {"seekable", (PyCFunction)AlwaysFalse, METH_NOARGS, ""}, |
692dac7183de
commit https://github.com/vim/vim/commit/d424747d5821c2873e24d25d3407d08b25ea3443
Christian Brabandt <cb@256bit.org>
parents:
6598
diff
changeset
|
504 {"writable", (PyCFunction)AlwaysTrue, METH_NOARGS, ""}, |
4599
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
505 {"__dir__", (PyCFunction)OutputDir, METH_NOARGS, ""}, |
4492 | 506 { NULL, NULL, 0, NULL} |
4385 | 507 }; |
2399
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
508 |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
509 static OutputObject Output = |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
510 { |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
511 PyObject_HEAD_INIT(&OutputType) |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
512 0, |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
513 0 |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
514 }; |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
515 |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
516 static OutputObject Error = |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
517 { |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
518 PyObject_HEAD_INIT(&OutputType) |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
519 0, |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
520 1 |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
521 }; |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
522 |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
523 static int |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
524 PythonIO_Init_io(void) |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
525 { |
4829
ff3935926449
updated for version 7.3.1161
Bram Moolenaar <bram@vim.org>
parents:
4754
diff
changeset
|
526 if (PySys_SetObject("stdout", (PyObject *)(void *)&Output)) |
ff3935926449
updated for version 7.3.1161
Bram Moolenaar <bram@vim.org>
parents:
4754
diff
changeset
|
527 return -1; |
ff3935926449
updated for version 7.3.1161
Bram Moolenaar <bram@vim.org>
parents:
4754
diff
changeset
|
528 if (PySys_SetObject("stderr", (PyObject *)(void *)&Error)) |
ff3935926449
updated for version 7.3.1161
Bram Moolenaar <bram@vim.org>
parents:
4754
diff
changeset
|
529 return -1; |
2399
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
530 |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
531 if (PyErr_Occurred()) |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
532 { |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
533 EMSG(_("E264: Python: Error initialising I/O objects")); |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
534 return -1; |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
535 } |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
536 |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
537 return 0; |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
538 } |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
539 |
4855
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
540 typedef struct |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
541 { |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
542 PyObject_HEAD |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
543 PyObject *module; |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
544 } LoaderObject; |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
545 static PyTypeObject LoaderType; |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
546 |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
547 static void |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
548 LoaderDestructor(LoaderObject *self) |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
549 { |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
550 Py_DECREF(self->module); |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
551 DESTRUCTOR_FINISH(self); |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
552 } |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
553 |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
554 static PyObject * |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
555 LoaderLoadModule(LoaderObject *self, PyObject *args UNUSED) |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
556 { |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
557 PyObject *ret = self->module; |
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
558 |
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
559 Py_INCREF(ret); |
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
560 return ret; |
4855
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
561 } |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
562 |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
563 static struct PyMethodDef LoaderMethods[] = { |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
564 /* name, function, calling, doc */ |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
565 {"load_module", (PyCFunction)LoaderLoadModule, METH_VARARGS, ""}, |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
566 { NULL, NULL, 0, NULL} |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
567 }; |
2399
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
568 |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
569 /* Check to see whether a Vim error has been reported, or a keyboard |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
570 * interrupt has been detected. |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
571 */ |
4498 | 572 |
573 static void | |
574 VimTryStart(void) | |
575 { | |
576 ++trylevel; | |
577 } | |
578 | |
2399
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
579 static int |
4498 | 580 VimTryEnd(void) |
581 { | |
582 --trylevel; | |
5088
34c629c3b4ba
updated for version 7.3.1287
Bram Moolenaar <bram@vim.org>
parents:
4995
diff
changeset
|
583 /* Without this it stops processing all subsequent VimL commands and |
34c629c3b4ba
updated for version 7.3.1287
Bram Moolenaar <bram@vim.org>
parents:
4995
diff
changeset
|
584 * generates strange error messages if I e.g. try calling Test() in a |
34c629c3b4ba
updated for version 7.3.1287
Bram Moolenaar <bram@vim.org>
parents:
4995
diff
changeset
|
585 * cycle */ |
4976
4ed713442c51
updated for version 7.3.1233
Bram Moolenaar <bram@vim.org>
parents:
4974
diff
changeset
|
586 did_emsg = FALSE; |
4ed713442c51
updated for version 7.3.1233
Bram Moolenaar <bram@vim.org>
parents:
4974
diff
changeset
|
587 /* Keyboard interrupt should be preferred over anything else */ |
4498 | 588 if (got_int) |
589 { | |
5629 | 590 if (did_throw) |
5469 | 591 discard_current_exception(); |
592 got_int = FALSE; | |
4498 | 593 PyErr_SetNone(PyExc_KeyboardInterrupt); |
4976
4ed713442c51
updated for version 7.3.1233
Bram Moolenaar <bram@vim.org>
parents:
4974
diff
changeset
|
594 return -1; |
4498 | 595 } |
5517 | 596 else if (msg_list != NULL && *msg_list != NULL) |
597 { | |
598 int should_free; | |
599 char_u *msg; | |
600 | |
601 msg = get_exception_string(*msg_list, ET_ERROR, NULL, &should_free); | |
602 | |
603 if (msg == NULL) | |
604 { | |
605 PyErr_NoMemory(); | |
606 return -1; | |
607 } | |
608 | |
609 PyErr_SetVim((char *) msg); | |
610 | |
611 free_global_msglist(); | |
612 | |
613 if (should_free) | |
614 vim_free(msg); | |
615 | |
616 return -1; | |
617 } | |
4498 | 618 else if (!did_throw) |
4976
4ed713442c51
updated for version 7.3.1233
Bram Moolenaar <bram@vim.org>
parents:
4974
diff
changeset
|
619 return (PyErr_Occurred() ? -1 : 0); |
4ed713442c51
updated for version 7.3.1233
Bram Moolenaar <bram@vim.org>
parents:
4974
diff
changeset
|
620 /* Python exception is preferred over vim one; unlikely to occur though */ |
4498 | 621 else if (PyErr_Occurred()) |
4976
4ed713442c51
updated for version 7.3.1233
Bram Moolenaar <bram@vim.org>
parents:
4974
diff
changeset
|
622 { |
5629 | 623 discard_current_exception(); |
4976
4ed713442c51
updated for version 7.3.1233
Bram Moolenaar <bram@vim.org>
parents:
4974
diff
changeset
|
624 return -1; |
4ed713442c51
updated for version 7.3.1233
Bram Moolenaar <bram@vim.org>
parents:
4974
diff
changeset
|
625 } |
4ed713442c51
updated for version 7.3.1233
Bram Moolenaar <bram@vim.org>
parents:
4974
diff
changeset
|
626 /* Finally transform VimL exception to python one */ |
4498 | 627 else |
628 { | |
5088
34c629c3b4ba
updated for version 7.3.1287
Bram Moolenaar <bram@vim.org>
parents:
4995
diff
changeset
|
629 PyErr_SetVim((char *)current_exception->value); |
4498 | 630 discard_current_exception(); |
4976
4ed713442c51
updated for version 7.3.1233
Bram Moolenaar <bram@vim.org>
parents:
4974
diff
changeset
|
631 return -1; |
4498 | 632 } |
633 } | |
634 | |
635 static int | |
636 VimCheckInterrupt(void) | |
2399
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
637 { |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
638 if (got_int) |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
639 { |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
640 PyErr_SetNone(PyExc_KeyboardInterrupt); |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
641 return 1; |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
642 } |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
643 return 0; |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
644 } |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
645 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
646 /* Vim module - Implementation |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
647 */ |
4385 | 648 |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
649 static PyObject * |
4964
5cee875f3096
updated for version 7.3.1227
Bram Moolenaar <bram@vim.org>
parents:
4962
diff
changeset
|
650 VimCommand(PyObject *self UNUSED, PyObject *string) |
5cee875f3096
updated for version 7.3.1227
Bram Moolenaar <bram@vim.org>
parents:
4962
diff
changeset
|
651 { |
5cee875f3096
updated for version 7.3.1227
Bram Moolenaar <bram@vim.org>
parents:
4962
diff
changeset
|
652 char_u *cmd; |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
653 PyObject *ret; |
4964
5cee875f3096
updated for version 7.3.1227
Bram Moolenaar <bram@vim.org>
parents:
4962
diff
changeset
|
654 PyObject *todecref; |
5cee875f3096
updated for version 7.3.1227
Bram Moolenaar <bram@vim.org>
parents:
4962
diff
changeset
|
655 |
5cee875f3096
updated for version 7.3.1227
Bram Moolenaar <bram@vim.org>
parents:
4962
diff
changeset
|
656 if (!(cmd = StringToChars(string, &todecref))) |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
657 return NULL; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
658 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
659 Py_BEGIN_ALLOW_THREADS |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
660 Python_Lock_Vim(); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
661 |
4498 | 662 VimTryStart(); |
4964
5cee875f3096
updated for version 7.3.1227
Bram Moolenaar <bram@vim.org>
parents:
4962
diff
changeset
|
663 do_cmdline_cmd(cmd); |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
664 update_screen(VALID); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
665 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
666 Python_Release_Vim(); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
667 Py_END_ALLOW_THREADS |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
668 |
4498 | 669 if (VimTryEnd()) |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
670 ret = NULL; |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
671 else |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
672 ret = Py_None; |
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
673 |
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
674 Py_XINCREF(ret); |
4964
5cee875f3096
updated for version 7.3.1227
Bram Moolenaar <bram@vim.org>
parents:
4962
diff
changeset
|
675 Py_XDECREF(todecref); |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
676 return ret; |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
677 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
678 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
679 /* |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
680 * Function to translate a typval_T into a PyObject; this will recursively |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
681 * translate lists/dictionaries into their Python equivalents. |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
682 * |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
683 * The depth parameter is to avoid infinite recursion, set it to 1 when |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
684 * you call VimToPython. |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
685 */ |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
686 static PyObject * |
4601
19d406a8509d
updated for version 7.3.1048
Bram Moolenaar <bram@vim.org>
parents:
4599
diff
changeset
|
687 VimToPython(typval_T *our_tv, int depth, PyObject *lookup_dict) |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
688 { |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
689 PyObject *ret; |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
690 PyObject *newObj; |
3618 | 691 char ptrBuf[sizeof(void *) * 2 + 3]; |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
692 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
693 /* Avoid infinite recursion */ |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
694 if (depth > 100) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
695 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
696 Py_INCREF(Py_None); |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
697 ret = Py_None; |
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
698 return ret; |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
699 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
700 |
4601
19d406a8509d
updated for version 7.3.1048
Bram Moolenaar <bram@vim.org>
parents:
4599
diff
changeset
|
701 /* Check if we run into a recursive loop. The item must be in lookup_dict |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
702 * then and we can use it again. */ |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
703 if ((our_tv->v_type == VAR_LIST && our_tv->vval.v_list != NULL) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
704 || (our_tv->v_type == VAR_DICT && our_tv->vval.v_dict != NULL)) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
705 { |
3618 | 706 sprintf(ptrBuf, "%p", |
707 our_tv->v_type == VAR_LIST ? (void *)our_tv->vval.v_list | |
708 : (void *)our_tv->vval.v_dict); | |
4595
29b2be2bba0d
updated for version 7.3.1045
Bram Moolenaar <bram@vim.org>
parents:
4593
diff
changeset
|
709 |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
710 if ((ret = PyDict_GetItemString(lookup_dict, ptrBuf))) |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
711 { |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
712 Py_INCREF(ret); |
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
713 return ret; |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
714 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
715 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
716 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
717 if (our_tv->v_type == VAR_STRING) |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
718 ret = PyString_FromString(our_tv->vval.v_string == NULL |
3852 | 719 ? "" : (char *)our_tv->vval.v_string); |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
720 else if (our_tv->v_type == VAR_NUMBER) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
721 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
722 char buf[NUMBUFLEN]; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
723 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
724 /* For backwards compatibility numbers are stored as strings. */ |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
725 sprintf(buf, "%ld", (long)our_tv->vval.v_number); |
5088
34c629c3b4ba
updated for version 7.3.1287
Bram Moolenaar <bram@vim.org>
parents:
4995
diff
changeset
|
726 ret = PyString_FromString((char *)buf); |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
727 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
728 # ifdef FEAT_FLOAT |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
729 else if (our_tv->v_type == VAR_FLOAT) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
730 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
731 char buf[NUMBUFLEN]; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
732 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
733 sprintf(buf, "%f", our_tv->vval.v_float); |
5088
34c629c3b4ba
updated for version 7.3.1287
Bram Moolenaar <bram@vim.org>
parents:
4995
diff
changeset
|
734 ret = PyString_FromString((char *)buf); |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
735 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
736 # endif |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
737 else if (our_tv->v_type == VAR_LIST) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
738 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
739 list_T *list = our_tv->vval.v_list; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
740 listitem_T *curr; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
741 |
4595
29b2be2bba0d
updated for version 7.3.1045
Bram Moolenaar <bram@vim.org>
parents:
4593
diff
changeset
|
742 if (list == NULL) |
29b2be2bba0d
updated for version 7.3.1045
Bram Moolenaar <bram@vim.org>
parents:
4593
diff
changeset
|
743 return NULL; |
29b2be2bba0d
updated for version 7.3.1045
Bram Moolenaar <bram@vim.org>
parents:
4593
diff
changeset
|
744 |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
745 if (!(ret = PyList_New(0))) |
4595
29b2be2bba0d
updated for version 7.3.1045
Bram Moolenaar <bram@vim.org>
parents:
4593
diff
changeset
|
746 return NULL; |
29b2be2bba0d
updated for version 7.3.1045
Bram Moolenaar <bram@vim.org>
parents:
4593
diff
changeset
|
747 |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
748 if (PyDict_SetItemString(lookup_dict, ptrBuf, ret)) |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
749 { |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
750 Py_DECREF(ret); |
4595
29b2be2bba0d
updated for version 7.3.1045
Bram Moolenaar <bram@vim.org>
parents:
4593
diff
changeset
|
751 return NULL; |
29b2be2bba0d
updated for version 7.3.1045
Bram Moolenaar <bram@vim.org>
parents:
4593
diff
changeset
|
752 } |
29b2be2bba0d
updated for version 7.3.1045
Bram Moolenaar <bram@vim.org>
parents:
4593
diff
changeset
|
753 |
29b2be2bba0d
updated for version 7.3.1045
Bram Moolenaar <bram@vim.org>
parents:
4593
diff
changeset
|
754 for (curr = list->lv_first; curr != NULL; curr = curr->li_next) |
29b2be2bba0d
updated for version 7.3.1045
Bram Moolenaar <bram@vim.org>
parents:
4593
diff
changeset
|
755 { |
4601
19d406a8509d
updated for version 7.3.1048
Bram Moolenaar <bram@vim.org>
parents:
4599
diff
changeset
|
756 if (!(newObj = VimToPython(&curr->li_tv, depth + 1, lookup_dict))) |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
757 { |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
758 Py_DECREF(ret); |
4595
29b2be2bba0d
updated for version 7.3.1045
Bram Moolenaar <bram@vim.org>
parents:
4593
diff
changeset
|
759 return NULL; |
29b2be2bba0d
updated for version 7.3.1045
Bram Moolenaar <bram@vim.org>
parents:
4593
diff
changeset
|
760 } |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
761 if (PyList_Append(ret, newObj)) |
4595
29b2be2bba0d
updated for version 7.3.1045
Bram Moolenaar <bram@vim.org>
parents:
4593
diff
changeset
|
762 { |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
763 Py_DECREF(newObj); |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
764 Py_DECREF(ret); |
4595
29b2be2bba0d
updated for version 7.3.1045
Bram Moolenaar <bram@vim.org>
parents:
4593
diff
changeset
|
765 return NULL; |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
766 } |
4595
29b2be2bba0d
updated for version 7.3.1045
Bram Moolenaar <bram@vim.org>
parents:
4593
diff
changeset
|
767 Py_DECREF(newObj); |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
768 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
769 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
770 else if (our_tv->v_type == VAR_DICT) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
771 { |
4595
29b2be2bba0d
updated for version 7.3.1045
Bram Moolenaar <bram@vim.org>
parents:
4593
diff
changeset
|
772 |
6598 | 773 hashtab_T *ht; |
774 long_u todo; | |
4595
29b2be2bba0d
updated for version 7.3.1045
Bram Moolenaar <bram@vim.org>
parents:
4593
diff
changeset
|
775 hashitem_T *hi; |
29b2be2bba0d
updated for version 7.3.1045
Bram Moolenaar <bram@vim.org>
parents:
4593
diff
changeset
|
776 dictitem_T *di; |
6598 | 777 |
4595
29b2be2bba0d
updated for version 7.3.1045
Bram Moolenaar <bram@vim.org>
parents:
4593
diff
changeset
|
778 if (our_tv->vval.v_dict == NULL) |
29b2be2bba0d
updated for version 7.3.1045
Bram Moolenaar <bram@vim.org>
parents:
4593
diff
changeset
|
779 return NULL; |
6598 | 780 ht = &our_tv->vval.v_dict->dv_hashtab; |
4595
29b2be2bba0d
updated for version 7.3.1045
Bram Moolenaar <bram@vim.org>
parents:
4593
diff
changeset
|
781 |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
782 if (!(ret = PyDict_New())) |
4595
29b2be2bba0d
updated for version 7.3.1045
Bram Moolenaar <bram@vim.org>
parents:
4593
diff
changeset
|
783 return NULL; |
29b2be2bba0d
updated for version 7.3.1045
Bram Moolenaar <bram@vim.org>
parents:
4593
diff
changeset
|
784 |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
785 if (PyDict_SetItemString(lookup_dict, ptrBuf, ret)) |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
786 { |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
787 Py_DECREF(ret); |
4595
29b2be2bba0d
updated for version 7.3.1045
Bram Moolenaar <bram@vim.org>
parents:
4593
diff
changeset
|
788 return NULL; |
29b2be2bba0d
updated for version 7.3.1045
Bram Moolenaar <bram@vim.org>
parents:
4593
diff
changeset
|
789 } |
29b2be2bba0d
updated for version 7.3.1045
Bram Moolenaar <bram@vim.org>
parents:
4593
diff
changeset
|
790 |
6598 | 791 todo = ht->ht_used; |
4595
29b2be2bba0d
updated for version 7.3.1045
Bram Moolenaar <bram@vim.org>
parents:
4593
diff
changeset
|
792 for (hi = ht->ht_array; todo > 0; ++hi) |
29b2be2bba0d
updated for version 7.3.1045
Bram Moolenaar <bram@vim.org>
parents:
4593
diff
changeset
|
793 { |
29b2be2bba0d
updated for version 7.3.1045
Bram Moolenaar <bram@vim.org>
parents:
4593
diff
changeset
|
794 if (!HASHITEM_EMPTY(hi)) |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
795 { |
4595
29b2be2bba0d
updated for version 7.3.1045
Bram Moolenaar <bram@vim.org>
parents:
4593
diff
changeset
|
796 --todo; |
29b2be2bba0d
updated for version 7.3.1045
Bram Moolenaar <bram@vim.org>
parents:
4593
diff
changeset
|
797 |
29b2be2bba0d
updated for version 7.3.1045
Bram Moolenaar <bram@vim.org>
parents:
4593
diff
changeset
|
798 di = dict_lookup(hi); |
4601
19d406a8509d
updated for version 7.3.1048
Bram Moolenaar <bram@vim.org>
parents:
4599
diff
changeset
|
799 if (!(newObj = VimToPython(&di->di_tv, depth + 1, lookup_dict))) |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
800 { |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
801 Py_DECREF(ret); |
4595
29b2be2bba0d
updated for version 7.3.1045
Bram Moolenaar <bram@vim.org>
parents:
4593
diff
changeset
|
802 return NULL; |
29b2be2bba0d
updated for version 7.3.1045
Bram Moolenaar <bram@vim.org>
parents:
4593
diff
changeset
|
803 } |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
804 if (PyDict_SetItemString(ret, (char *)hi->hi_key, newObj)) |
4595
29b2be2bba0d
updated for version 7.3.1045
Bram Moolenaar <bram@vim.org>
parents:
4593
diff
changeset
|
805 { |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
806 Py_DECREF(ret); |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
807 Py_DECREF(newObj); |
4595
29b2be2bba0d
updated for version 7.3.1045
Bram Moolenaar <bram@vim.org>
parents:
4593
diff
changeset
|
808 return NULL; |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
809 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
810 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
811 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
812 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
813 else |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
814 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
815 Py_INCREF(Py_None); |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
816 ret = Py_None; |
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
817 } |
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
818 |
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
819 return ret; |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
820 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
821 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
822 static PyObject * |
4500 | 823 VimEval(PyObject *self UNUSED, PyObject *args) |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
824 { |
4964
5cee875f3096
updated for version 7.3.1227
Bram Moolenaar <bram@vim.org>
parents:
4962
diff
changeset
|
825 char_u *expr; |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
826 typval_T *our_tv; |
4964
5cee875f3096
updated for version 7.3.1227
Bram Moolenaar <bram@vim.org>
parents:
4962
diff
changeset
|
827 PyObject *string; |
5cee875f3096
updated for version 7.3.1227
Bram Moolenaar <bram@vim.org>
parents:
4962
diff
changeset
|
828 PyObject *todecref; |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
829 PyObject *ret; |
4964
5cee875f3096
updated for version 7.3.1227
Bram Moolenaar <bram@vim.org>
parents:
4962
diff
changeset
|
830 PyObject *lookup_dict; |
5cee875f3096
updated for version 7.3.1227
Bram Moolenaar <bram@vim.org>
parents:
4962
diff
changeset
|
831 |
5cee875f3096
updated for version 7.3.1227
Bram Moolenaar <bram@vim.org>
parents:
4962
diff
changeset
|
832 if (!PyArg_ParseTuple(args, "O", &string)) |
5cee875f3096
updated for version 7.3.1227
Bram Moolenaar <bram@vim.org>
parents:
4962
diff
changeset
|
833 return NULL; |
5cee875f3096
updated for version 7.3.1227
Bram Moolenaar <bram@vim.org>
parents:
4962
diff
changeset
|
834 |
5cee875f3096
updated for version 7.3.1227
Bram Moolenaar <bram@vim.org>
parents:
4962
diff
changeset
|
835 if (!(expr = StringToChars(string, &todecref))) |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
836 return NULL; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
837 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
838 Py_BEGIN_ALLOW_THREADS |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
839 Python_Lock_Vim(); |
4498 | 840 VimTryStart(); |
4964
5cee875f3096
updated for version 7.3.1227
Bram Moolenaar <bram@vim.org>
parents:
4962
diff
changeset
|
841 our_tv = eval_expr(expr, NULL); |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
842 Python_Release_Vim(); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
843 Py_END_ALLOW_THREADS |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
844 |
4964
5cee875f3096
updated for version 7.3.1227
Bram Moolenaar <bram@vim.org>
parents:
4962
diff
changeset
|
845 Py_XDECREF(todecref); |
5cee875f3096
updated for version 7.3.1227
Bram Moolenaar <bram@vim.org>
parents:
4962
diff
changeset
|
846 |
4498 | 847 if (VimTryEnd()) |
848 return NULL; | |
849 | |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
850 if (our_tv == NULL) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
851 { |
4978
f4969f8f66e9
updated for version 7.3.1234
Bram Moolenaar <bram@vim.org>
parents:
4976
diff
changeset
|
852 PyErr_SET_VIM(N_("invalid expression")); |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
853 return NULL; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
854 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
855 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
856 /* Convert the Vim type into a Python type. Create a dictionary that's |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
857 * used to check for recursive loops. */ |
4617
21a99611149b
updated for version 7.3.1056
Bram Moolenaar <bram@vim.org>
parents:
4611
diff
changeset
|
858 if (!(lookup_dict = PyDict_New())) |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
859 ret = NULL; |
4617
21a99611149b
updated for version 7.3.1056
Bram Moolenaar <bram@vim.org>
parents:
4611
diff
changeset
|
860 else |
21a99611149b
updated for version 7.3.1056
Bram Moolenaar <bram@vim.org>
parents:
4611
diff
changeset
|
861 { |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
862 ret = VimToPython(our_tv, 1, lookup_dict); |
4617
21a99611149b
updated for version 7.3.1056
Bram Moolenaar <bram@vim.org>
parents:
4611
diff
changeset
|
863 Py_DECREF(lookup_dict); |
21a99611149b
updated for version 7.3.1056
Bram Moolenaar <bram@vim.org>
parents:
4611
diff
changeset
|
864 } |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
865 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
866 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
867 Py_BEGIN_ALLOW_THREADS |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
868 Python_Lock_Vim(); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
869 free_tv(our_tv); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
870 Python_Release_Vim(); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
871 Py_END_ALLOW_THREADS |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
872 |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
873 return ret; |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
874 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
875 |
3618 | 876 static PyObject *ConvertToPyObject(typval_T *); |
877 | |
878 static PyObject * | |
4964
5cee875f3096
updated for version 7.3.1227
Bram Moolenaar <bram@vim.org>
parents:
4962
diff
changeset
|
879 VimEvalPy(PyObject *self UNUSED, PyObject *string) |
5cee875f3096
updated for version 7.3.1227
Bram Moolenaar <bram@vim.org>
parents:
4962
diff
changeset
|
880 { |
3618 | 881 typval_T *our_tv; |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
882 PyObject *ret; |
4964
5cee875f3096
updated for version 7.3.1227
Bram Moolenaar <bram@vim.org>
parents:
4962
diff
changeset
|
883 char_u *expr; |
5cee875f3096
updated for version 7.3.1227
Bram Moolenaar <bram@vim.org>
parents:
4962
diff
changeset
|
884 PyObject *todecref; |
5cee875f3096
updated for version 7.3.1227
Bram Moolenaar <bram@vim.org>
parents:
4962
diff
changeset
|
885 |
5cee875f3096
updated for version 7.3.1227
Bram Moolenaar <bram@vim.org>
parents:
4962
diff
changeset
|
886 if (!(expr = StringToChars(string, &todecref))) |
3618 | 887 return NULL; |
888 | |
889 Py_BEGIN_ALLOW_THREADS | |
890 Python_Lock_Vim(); | |
4498 | 891 VimTryStart(); |
4964
5cee875f3096
updated for version 7.3.1227
Bram Moolenaar <bram@vim.org>
parents:
4962
diff
changeset
|
892 our_tv = eval_expr(expr, NULL); |
3618 | 893 Python_Release_Vim(); |
894 Py_END_ALLOW_THREADS | |
895 | |
4964
5cee875f3096
updated for version 7.3.1227
Bram Moolenaar <bram@vim.org>
parents:
4962
diff
changeset
|
896 Py_XDECREF(todecref); |
5cee875f3096
updated for version 7.3.1227
Bram Moolenaar <bram@vim.org>
parents:
4962
diff
changeset
|
897 |
4498 | 898 if (VimTryEnd()) |
899 return NULL; | |
900 | |
3618 | 901 if (our_tv == NULL) |
902 { | |
4978
f4969f8f66e9
updated for version 7.3.1234
Bram Moolenaar <bram@vim.org>
parents:
4976
diff
changeset
|
903 PyErr_SET_VIM(N_("invalid expression")); |
3618 | 904 return NULL; |
905 } | |
906 | |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
907 ret = ConvertToPyObject(our_tv); |
3618 | 908 Py_BEGIN_ALLOW_THREADS |
909 Python_Lock_Vim(); | |
910 free_tv(our_tv); | |
911 Python_Release_Vim(); | |
912 Py_END_ALLOW_THREADS | |
913 | |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
914 return ret; |
3618 | 915 } |
916 | |
917 static PyObject * | |
4964
5cee875f3096
updated for version 7.3.1227
Bram Moolenaar <bram@vim.org>
parents:
4962
diff
changeset
|
918 VimStrwidth(PyObject *self UNUSED, PyObject *string) |
5cee875f3096
updated for version 7.3.1227
Bram Moolenaar <bram@vim.org>
parents:
4962
diff
changeset
|
919 { |
5cee875f3096
updated for version 7.3.1227
Bram Moolenaar <bram@vim.org>
parents:
4962
diff
changeset
|
920 char_u *str; |
5cee875f3096
updated for version 7.3.1227
Bram Moolenaar <bram@vim.org>
parents:
4962
diff
changeset
|
921 PyObject *todecref; |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
922 int len; |
4964
5cee875f3096
updated for version 7.3.1227
Bram Moolenaar <bram@vim.org>
parents:
4962
diff
changeset
|
923 |
5cee875f3096
updated for version 7.3.1227
Bram Moolenaar <bram@vim.org>
parents:
4962
diff
changeset
|
924 if (!(str = StringToChars(string, &todecref))) |
3618 | 925 return NULL; |
926 | |
3988 | 927 #ifdef FEAT_MBYTE |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
928 len = mb_string2cells(str, (int)STRLEN(str)); |
3988 | 929 #else |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
930 len = STRLEN(str); |
3988 | 931 #endif |
4964
5cee875f3096
updated for version 7.3.1227
Bram Moolenaar <bram@vim.org>
parents:
4962
diff
changeset
|
932 |
5cee875f3096
updated for version 7.3.1227
Bram Moolenaar <bram@vim.org>
parents:
4962
diff
changeset
|
933 Py_XDECREF(todecref); |
5cee875f3096
updated for version 7.3.1227
Bram Moolenaar <bram@vim.org>
parents:
4962
diff
changeset
|
934 |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
935 return PyLong_FromLong(len); |
3618 | 936 } |
937 | |
4704
542af01979be
updated for version 7.3.1099
Bram Moolenaar <bram@vim.org>
parents:
4702
diff
changeset
|
938 static PyObject * |
542af01979be
updated for version 7.3.1099
Bram Moolenaar <bram@vim.org>
parents:
4702
diff
changeset
|
939 _VimChdir(PyObject *_chdir, PyObject *args, PyObject *kwargs) |
542af01979be
updated for version 7.3.1099
Bram Moolenaar <bram@vim.org>
parents:
4702
diff
changeset
|
940 { |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
941 PyObject *ret; |
4704
542af01979be
updated for version 7.3.1099
Bram Moolenaar <bram@vim.org>
parents:
4702
diff
changeset
|
942 PyObject *newwd; |
542af01979be
updated for version 7.3.1099
Bram Moolenaar <bram@vim.org>
parents:
4702
diff
changeset
|
943 PyObject *todecref; |
542af01979be
updated for version 7.3.1099
Bram Moolenaar <bram@vim.org>
parents:
4702
diff
changeset
|
944 char_u *new_dir; |
542af01979be
updated for version 7.3.1099
Bram Moolenaar <bram@vim.org>
parents:
4702
diff
changeset
|
945 |
4754
be1e4acb30ca
updated for version 7.3.1124
Bram Moolenaar <bram@vim.org>
parents:
4722
diff
changeset
|
946 if (_chdir == NULL) |
be1e4acb30ca
updated for version 7.3.1124
Bram Moolenaar <bram@vim.org>
parents:
4722
diff
changeset
|
947 return NULL; |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
948 if (!(ret = PyObject_Call(_chdir, args, kwargs))) |
4704
542af01979be
updated for version 7.3.1099
Bram Moolenaar <bram@vim.org>
parents:
4702
diff
changeset
|
949 return NULL; |
542af01979be
updated for version 7.3.1099
Bram Moolenaar <bram@vim.org>
parents:
4702
diff
changeset
|
950 |
542af01979be
updated for version 7.3.1099
Bram Moolenaar <bram@vim.org>
parents:
4702
diff
changeset
|
951 if (!(newwd = PyObject_CallFunctionObjArgs(py_getcwd, NULL))) |
542af01979be
updated for version 7.3.1099
Bram Moolenaar <bram@vim.org>
parents:
4702
diff
changeset
|
952 { |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
953 Py_DECREF(ret); |
4704
542af01979be
updated for version 7.3.1099
Bram Moolenaar <bram@vim.org>
parents:
4702
diff
changeset
|
954 return NULL; |
542af01979be
updated for version 7.3.1099
Bram Moolenaar <bram@vim.org>
parents:
4702
diff
changeset
|
955 } |
542af01979be
updated for version 7.3.1099
Bram Moolenaar <bram@vim.org>
parents:
4702
diff
changeset
|
956 |
542af01979be
updated for version 7.3.1099
Bram Moolenaar <bram@vim.org>
parents:
4702
diff
changeset
|
957 if (!(new_dir = StringToChars(newwd, &todecref))) |
542af01979be
updated for version 7.3.1099
Bram Moolenaar <bram@vim.org>
parents:
4702
diff
changeset
|
958 { |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
959 Py_DECREF(ret); |
4704
542af01979be
updated for version 7.3.1099
Bram Moolenaar <bram@vim.org>
parents:
4702
diff
changeset
|
960 Py_DECREF(newwd); |
542af01979be
updated for version 7.3.1099
Bram Moolenaar <bram@vim.org>
parents:
4702
diff
changeset
|
961 return NULL; |
542af01979be
updated for version 7.3.1099
Bram Moolenaar <bram@vim.org>
parents:
4702
diff
changeset
|
962 } |
542af01979be
updated for version 7.3.1099
Bram Moolenaar <bram@vim.org>
parents:
4702
diff
changeset
|
963 |
542af01979be
updated for version 7.3.1099
Bram Moolenaar <bram@vim.org>
parents:
4702
diff
changeset
|
964 VimTryStart(); |
542af01979be
updated for version 7.3.1099
Bram Moolenaar <bram@vim.org>
parents:
4702
diff
changeset
|
965 |
542af01979be
updated for version 7.3.1099
Bram Moolenaar <bram@vim.org>
parents:
4702
diff
changeset
|
966 if (vim_chdir(new_dir)) |
542af01979be
updated for version 7.3.1099
Bram Moolenaar <bram@vim.org>
parents:
4702
diff
changeset
|
967 { |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
968 Py_DECREF(ret); |
4704
542af01979be
updated for version 7.3.1099
Bram Moolenaar <bram@vim.org>
parents:
4702
diff
changeset
|
969 Py_DECREF(newwd); |
542af01979be
updated for version 7.3.1099
Bram Moolenaar <bram@vim.org>
parents:
4702
diff
changeset
|
970 Py_XDECREF(todecref); |
542af01979be
updated for version 7.3.1099
Bram Moolenaar <bram@vim.org>
parents:
4702
diff
changeset
|
971 |
542af01979be
updated for version 7.3.1099
Bram Moolenaar <bram@vim.org>
parents:
4702
diff
changeset
|
972 if (VimTryEnd()) |
542af01979be
updated for version 7.3.1099
Bram Moolenaar <bram@vim.org>
parents:
4702
diff
changeset
|
973 return NULL; |
542af01979be
updated for version 7.3.1099
Bram Moolenaar <bram@vim.org>
parents:
4702
diff
changeset
|
974 |
4978
f4969f8f66e9
updated for version 7.3.1234
Bram Moolenaar <bram@vim.org>
parents:
4976
diff
changeset
|
975 PyErr_SET_VIM(N_("failed to change directory")); |
4704
542af01979be
updated for version 7.3.1099
Bram Moolenaar <bram@vim.org>
parents:
4702
diff
changeset
|
976 return NULL; |
542af01979be
updated for version 7.3.1099
Bram Moolenaar <bram@vim.org>
parents:
4702
diff
changeset
|
977 } |
542af01979be
updated for version 7.3.1099
Bram Moolenaar <bram@vim.org>
parents:
4702
diff
changeset
|
978 |
542af01979be
updated for version 7.3.1099
Bram Moolenaar <bram@vim.org>
parents:
4702
diff
changeset
|
979 Py_DECREF(newwd); |
542af01979be
updated for version 7.3.1099
Bram Moolenaar <bram@vim.org>
parents:
4702
diff
changeset
|
980 Py_XDECREF(todecref); |
542af01979be
updated for version 7.3.1099
Bram Moolenaar <bram@vim.org>
parents:
4702
diff
changeset
|
981 |
542af01979be
updated for version 7.3.1099
Bram Moolenaar <bram@vim.org>
parents:
4702
diff
changeset
|
982 post_chdir(FALSE); |
542af01979be
updated for version 7.3.1099
Bram Moolenaar <bram@vim.org>
parents:
4702
diff
changeset
|
983 |
542af01979be
updated for version 7.3.1099
Bram Moolenaar <bram@vim.org>
parents:
4702
diff
changeset
|
984 if (VimTryEnd()) |
542af01979be
updated for version 7.3.1099
Bram Moolenaar <bram@vim.org>
parents:
4702
diff
changeset
|
985 { |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
986 Py_DECREF(ret); |
4704
542af01979be
updated for version 7.3.1099
Bram Moolenaar <bram@vim.org>
parents:
4702
diff
changeset
|
987 return NULL; |
542af01979be
updated for version 7.3.1099
Bram Moolenaar <bram@vim.org>
parents:
4702
diff
changeset
|
988 } |
542af01979be
updated for version 7.3.1099
Bram Moolenaar <bram@vim.org>
parents:
4702
diff
changeset
|
989 |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
990 return ret; |
4704
542af01979be
updated for version 7.3.1099
Bram Moolenaar <bram@vim.org>
parents:
4702
diff
changeset
|
991 } |
542af01979be
updated for version 7.3.1099
Bram Moolenaar <bram@vim.org>
parents:
4702
diff
changeset
|
992 |
542af01979be
updated for version 7.3.1099
Bram Moolenaar <bram@vim.org>
parents:
4702
diff
changeset
|
993 static PyObject * |
542af01979be
updated for version 7.3.1099
Bram Moolenaar <bram@vim.org>
parents:
4702
diff
changeset
|
994 VimChdir(PyObject *self UNUSED, PyObject *args, PyObject *kwargs) |
542af01979be
updated for version 7.3.1099
Bram Moolenaar <bram@vim.org>
parents:
4702
diff
changeset
|
995 { |
542af01979be
updated for version 7.3.1099
Bram Moolenaar <bram@vim.org>
parents:
4702
diff
changeset
|
996 return _VimChdir(py_chdir, args, kwargs); |
542af01979be
updated for version 7.3.1099
Bram Moolenaar <bram@vim.org>
parents:
4702
diff
changeset
|
997 } |
542af01979be
updated for version 7.3.1099
Bram Moolenaar <bram@vim.org>
parents:
4702
diff
changeset
|
998 |
542af01979be
updated for version 7.3.1099
Bram Moolenaar <bram@vim.org>
parents:
4702
diff
changeset
|
999 static PyObject * |
542af01979be
updated for version 7.3.1099
Bram Moolenaar <bram@vim.org>
parents:
4702
diff
changeset
|
1000 VimFchdir(PyObject *self UNUSED, PyObject *args, PyObject *kwargs) |
542af01979be
updated for version 7.3.1099
Bram Moolenaar <bram@vim.org>
parents:
4702
diff
changeset
|
1001 { |
542af01979be
updated for version 7.3.1099
Bram Moolenaar <bram@vim.org>
parents:
4702
diff
changeset
|
1002 return _VimChdir(py_fchdir, args, kwargs); |
542af01979be
updated for version 7.3.1099
Bram Moolenaar <bram@vim.org>
parents:
4702
diff
changeset
|
1003 } |
542af01979be
updated for version 7.3.1099
Bram Moolenaar <bram@vim.org>
parents:
4702
diff
changeset
|
1004 |
4833
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
1005 typedef struct { |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
1006 PyObject *callable; |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
1007 PyObject *result; |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
1008 } map_rtp_data; |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
1009 |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
1010 static void |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
1011 map_rtp_callback(char_u *path, void *_data) |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
1012 { |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
1013 void **data = (void **) _data; |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
1014 PyObject *pathObject; |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
1015 map_rtp_data *mr_data = *((map_rtp_data **) data); |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
1016 |
5088
34c629c3b4ba
updated for version 7.3.1287
Bram Moolenaar <bram@vim.org>
parents:
4995
diff
changeset
|
1017 if (!(pathObject = PyString_FromString((char *)path))) |
4833
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
1018 { |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
1019 *data = NULL; |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
1020 return; |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
1021 } |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
1022 |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
1023 mr_data->result = PyObject_CallFunctionObjArgs(mr_data->callable, |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
1024 pathObject, NULL); |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
1025 |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
1026 Py_DECREF(pathObject); |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
1027 |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
1028 if (!mr_data->result || mr_data->result != Py_None) |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
1029 *data = NULL; |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
1030 else |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
1031 { |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
1032 Py_DECREF(mr_data->result); |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
1033 mr_data->result = NULL; |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
1034 } |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
1035 } |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
1036 |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
1037 static PyObject * |
4964
5cee875f3096
updated for version 7.3.1227
Bram Moolenaar <bram@vim.org>
parents:
4962
diff
changeset
|
1038 VimForeachRTP(PyObject *self UNUSED, PyObject *callable) |
4833
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
1039 { |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
1040 map_rtp_data data; |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
1041 |
4964
5cee875f3096
updated for version 7.3.1227
Bram Moolenaar <bram@vim.org>
parents:
4962
diff
changeset
|
1042 data.callable = callable; |
4833
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
1043 data.result = NULL; |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
1044 |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
1045 do_in_runtimepath(NULL, FALSE, &map_rtp_callback, &data); |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
1046 |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
1047 if (data.result == NULL) |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
1048 { |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
1049 if (PyErr_Occurred()) |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
1050 return NULL; |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
1051 else |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
1052 { |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
1053 Py_INCREF(Py_None); |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
1054 return Py_None; |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
1055 } |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
1056 } |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
1057 return data.result; |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
1058 } |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
1059 |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
1060 /* |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
1061 * _vim_runtimepath_ special path implementation. |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
1062 */ |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
1063 |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
1064 static void |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
1065 map_finder_callback(char_u *path, void *_data) |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
1066 { |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
1067 void **data = (void **) _data; |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
1068 PyObject *list = *((PyObject **) data); |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
1069 PyObject *pathObject1, *pathObject2; |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
1070 char *pathbuf; |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
1071 size_t pathlen; |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
1072 |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
1073 pathlen = STRLEN(path); |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
1074 |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
1075 #if PY_MAJOR_VERSION < 3 |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
1076 # define PY_MAIN_DIR_STRING "python2" |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
1077 #else |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
1078 # define PY_MAIN_DIR_STRING "python3" |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
1079 #endif |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
1080 #define PY_ALTERNATE_DIR_STRING "pythonx" |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
1081 |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
1082 #define PYTHONX_STRING_LENGTH 7 /* STRLEN("pythonx") */ |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
1083 if (!(pathbuf = PyMem_New(char, |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
1084 pathlen + STRLEN(PATHSEPSTR) + PYTHONX_STRING_LENGTH + 1))) |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
1085 { |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
1086 PyErr_NoMemory(); |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
1087 *data = NULL; |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
1088 return; |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
1089 } |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
1090 |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
1091 mch_memmove(pathbuf, path, pathlen + 1); |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
1092 add_pathsep((char_u *) pathbuf); |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
1093 |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
1094 pathlen = STRLEN(pathbuf); |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
1095 mch_memmove(pathbuf + pathlen, PY_MAIN_DIR_STRING, |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
1096 PYTHONX_STRING_LENGTH + 1); |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
1097 |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
1098 if (!(pathObject1 = PyString_FromString(pathbuf))) |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
1099 { |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
1100 *data = NULL; |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
1101 PyMem_Free(pathbuf); |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
1102 return; |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
1103 } |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
1104 |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
1105 mch_memmove(pathbuf + pathlen, PY_ALTERNATE_DIR_STRING, |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
1106 PYTHONX_STRING_LENGTH + 1); |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
1107 |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
1108 if (!(pathObject2 = PyString_FromString(pathbuf))) |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
1109 { |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
1110 Py_DECREF(pathObject1); |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
1111 PyMem_Free(pathbuf); |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
1112 *data = NULL; |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
1113 return; |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
1114 } |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
1115 |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
1116 PyMem_Free(pathbuf); |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
1117 |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
1118 if (PyList_Append(list, pathObject1) |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
1119 || PyList_Append(list, pathObject2)) |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
1120 *data = NULL; |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
1121 |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
1122 Py_DECREF(pathObject1); |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
1123 Py_DECREF(pathObject2); |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
1124 } |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
1125 |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
1126 static PyObject * |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
1127 Vim_GetPaths(PyObject *self UNUSED) |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
1128 { |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
1129 PyObject *ret; |
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
1130 |
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
1131 if (!(ret = PyList_New(0))) |
4833
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
1132 return NULL; |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
1133 |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
1134 do_in_runtimepath(NULL, FALSE, &map_finder_callback, ret); |
4833
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
1135 |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
1136 if (PyErr_Occurred()) |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
1137 { |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
1138 Py_DECREF(ret); |
4833
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
1139 return NULL; |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
1140 } |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
1141 |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
1142 return ret; |
4833
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
1143 } |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
1144 |
4855
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
1145 static PyObject * |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
1146 call_load_module(char *name, int len, PyObject *find_module_result) |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
1147 { |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
1148 PyObject *fd, *pathname, *description; |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
1149 |
4970
f5c822e5a0eb
updated for version 7.3.1230
Bram Moolenaar <bram@vim.org>
parents:
4968
diff
changeset
|
1150 if (!PyTuple_Check(find_module_result)) |
f5c822e5a0eb
updated for version 7.3.1230
Bram Moolenaar <bram@vim.org>
parents:
4968
diff
changeset
|
1151 { |
f5c822e5a0eb
updated for version 7.3.1230
Bram Moolenaar <bram@vim.org>
parents:
4968
diff
changeset
|
1152 PyErr_FORMAT(PyExc_TypeError, |
4978
f4969f8f66e9
updated for version 7.3.1234
Bram Moolenaar <bram@vim.org>
parents:
4976
diff
changeset
|
1153 N_("expected 3-tuple as imp.find_module() result, but got %s"), |
4970
f5c822e5a0eb
updated for version 7.3.1230
Bram Moolenaar <bram@vim.org>
parents:
4968
diff
changeset
|
1154 Py_TYPE_NAME(find_module_result)); |
f5c822e5a0eb
updated for version 7.3.1230
Bram Moolenaar <bram@vim.org>
parents:
4968
diff
changeset
|
1155 return NULL; |
f5c822e5a0eb
updated for version 7.3.1230
Bram Moolenaar <bram@vim.org>
parents:
4968
diff
changeset
|
1156 } |
f5c822e5a0eb
updated for version 7.3.1230
Bram Moolenaar <bram@vim.org>
parents:
4968
diff
changeset
|
1157 if (PyTuple_GET_SIZE(find_module_result) != 3) |
f5c822e5a0eb
updated for version 7.3.1230
Bram Moolenaar <bram@vim.org>
parents:
4968
diff
changeset
|
1158 { |
f5c822e5a0eb
updated for version 7.3.1230
Bram Moolenaar <bram@vim.org>
parents:
4968
diff
changeset
|
1159 PyErr_FORMAT(PyExc_TypeError, |
4978
f4969f8f66e9
updated for version 7.3.1234
Bram Moolenaar <bram@vim.org>
parents:
4976
diff
changeset
|
1160 N_("expected 3-tuple as imp.find_module() result, but got " |
f4969f8f66e9
updated for version 7.3.1234
Bram Moolenaar <bram@vim.org>
parents:
4976
diff
changeset
|
1161 "tuple of size %d"), |
4970
f5c822e5a0eb
updated for version 7.3.1230
Bram Moolenaar <bram@vim.org>
parents:
4968
diff
changeset
|
1162 (int) PyTuple_GET_SIZE(find_module_result)); |
4855
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
1163 return NULL; |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
1164 } |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
1165 |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
1166 if (!(fd = PyTuple_GET_ITEM(find_module_result, 0)) |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
1167 || !(pathname = PyTuple_GET_ITEM(find_module_result, 1)) |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
1168 || !(description = PyTuple_GET_ITEM(find_module_result, 2))) |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
1169 { |
4968
b6e693e1f946
updated for version 7.3.1229
Bram Moolenaar <bram@vim.org>
parents:
4966
diff
changeset
|
1170 PyErr_SET_STRING(PyExc_RuntimeError, |
4978
f4969f8f66e9
updated for version 7.3.1234
Bram Moolenaar <bram@vim.org>
parents:
4976
diff
changeset
|
1171 N_("internal error: imp.find_module returned tuple with NULL")); |
4855
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
1172 return NULL; |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
1173 } |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
1174 |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
1175 return PyObject_CallFunction(py_load_module, |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
1176 "s#OOO", name, len, fd, pathname, description); |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
1177 } |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
1178 |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
1179 static PyObject * |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
1180 find_module(char *fullname, char *tail, PyObject *new_path) |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
1181 { |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
1182 PyObject *find_module_result; |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
1183 PyObject *module; |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
1184 char *dot; |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
1185 |
5088
34c629c3b4ba
updated for version 7.3.1287
Bram Moolenaar <bram@vim.org>
parents:
4995
diff
changeset
|
1186 if ((dot = (char *)vim_strchr((char_u *) tail, '.'))) |
4855
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
1187 { |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
1188 /* |
4859
e48cabe98453
updated for version 7.3.1176
Bram Moolenaar <bram@vim.org>
parents:
4855
diff
changeset
|
1189 * There is a dot in the name: call find_module recursively without the |
4855
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
1190 * first component |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
1191 */ |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
1192 PyObject *newest_path; |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
1193 int partlen = (int) (dot - 1 - tail); |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
1194 |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
1195 if (!(find_module_result = PyObject_CallFunction(py_find_module, |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
1196 "s#O", tail, partlen, new_path))) |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
1197 return NULL; |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
1198 |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
1199 if (!(module = call_load_module( |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
1200 fullname, |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
1201 ((int) (tail - fullname)) + partlen, |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
1202 find_module_result))) |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
1203 { |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
1204 Py_DECREF(find_module_result); |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
1205 return NULL; |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
1206 } |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
1207 |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
1208 Py_DECREF(find_module_result); |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
1209 |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
1210 if (!(newest_path = PyObject_GetAttrString(module, "__path__"))) |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
1211 { |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
1212 Py_DECREF(module); |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
1213 return NULL; |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
1214 } |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
1215 |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
1216 Py_DECREF(module); |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
1217 |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
1218 module = find_module(fullname, dot + 1, newest_path); |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
1219 |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
1220 Py_DECREF(newest_path); |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
1221 |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
1222 return module; |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
1223 } |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
1224 else |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
1225 { |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
1226 if (!(find_module_result = PyObject_CallFunction(py_find_module, |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
1227 "sO", tail, new_path))) |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
1228 return NULL; |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
1229 |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
1230 if (!(module = call_load_module( |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
1231 fullname, |
4859
e48cabe98453
updated for version 7.3.1176
Bram Moolenaar <bram@vim.org>
parents:
4855
diff
changeset
|
1232 (int)STRLEN(fullname), |
4855
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
1233 find_module_result))) |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
1234 { |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
1235 Py_DECREF(find_module_result); |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
1236 return NULL; |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
1237 } |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
1238 |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
1239 Py_DECREF(find_module_result); |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
1240 |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
1241 return module; |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
1242 } |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
1243 } |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
1244 |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
1245 static PyObject * |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
1246 FinderFindModule(PyObject *self, PyObject *args) |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
1247 { |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
1248 char *fullname; |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
1249 PyObject *module; |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
1250 PyObject *new_path; |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
1251 LoaderObject *loader; |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
1252 |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
1253 if (!PyArg_ParseTuple(args, "s", &fullname)) |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
1254 return NULL; |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
1255 |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
1256 if (!(new_path = Vim_GetPaths(self))) |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
1257 return NULL; |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
1258 |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
1259 module = find_module(fullname, fullname, new_path); |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
1260 |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
1261 Py_DECREF(new_path); |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
1262 |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
1263 if (!module) |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
1264 { |
4984
5b2c8f3b3906
updated for version 7.3.1237
Bram Moolenaar <bram@vim.org>
parents:
4982
diff
changeset
|
1265 if (PyErr_Occurred()) |
5b2c8f3b3906
updated for version 7.3.1237
Bram Moolenaar <bram@vim.org>
parents:
4982
diff
changeset
|
1266 { |
5b2c8f3b3906
updated for version 7.3.1237
Bram Moolenaar <bram@vim.org>
parents:
4982
diff
changeset
|
1267 if (PyErr_ExceptionMatches(PyExc_ImportError)) |
5b2c8f3b3906
updated for version 7.3.1237
Bram Moolenaar <bram@vim.org>
parents:
4982
diff
changeset
|
1268 PyErr_Clear(); |
5b2c8f3b3906
updated for version 7.3.1237
Bram Moolenaar <bram@vim.org>
parents:
4982
diff
changeset
|
1269 else |
5b2c8f3b3906
updated for version 7.3.1237
Bram Moolenaar <bram@vim.org>
parents:
4982
diff
changeset
|
1270 return NULL; |
5b2c8f3b3906
updated for version 7.3.1237
Bram Moolenaar <bram@vim.org>
parents:
4982
diff
changeset
|
1271 } |
5b2c8f3b3906
updated for version 7.3.1237
Bram Moolenaar <bram@vim.org>
parents:
4982
diff
changeset
|
1272 |
4855
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
1273 Py_INCREF(Py_None); |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
1274 return Py_None; |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
1275 } |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
1276 |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
1277 if (!(loader = PyObject_NEW(LoaderObject, &LoaderType))) |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
1278 { |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
1279 Py_DECREF(module); |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
1280 return NULL; |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
1281 } |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
1282 |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
1283 loader->module = module; |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
1284 |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
1285 return (PyObject *) loader; |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
1286 } |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
1287 |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
1288 static PyObject * |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
1289 VimPathHook(PyObject *self UNUSED, PyObject *args) |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
1290 { |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
1291 char *path; |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
1292 |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
1293 if (PyArg_ParseTuple(args, "s", &path) |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
1294 && STRCMP(path, vim_special_path) == 0) |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
1295 { |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
1296 Py_INCREF(vim_module); |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
1297 return vim_module; |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
1298 } |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
1299 |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
1300 PyErr_Clear(); |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
1301 PyErr_SetNone(PyExc_ImportError); |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
1302 return NULL; |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
1303 } |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
1304 |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1305 /* |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1306 * Vim module - Definitions |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1307 */ |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1308 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1309 static struct PyMethodDef VimMethods[] = { |
4833
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
1310 /* name, function, calling, documentation */ |
4964
5cee875f3096
updated for version 7.3.1227
Bram Moolenaar <bram@vim.org>
parents:
4962
diff
changeset
|
1311 {"command", VimCommand, METH_O, "Execute a Vim ex-mode command" }, |
4833
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
1312 {"eval", VimEval, METH_VARARGS, "Evaluate an expression using Vim evaluator" }, |
4964
5cee875f3096
updated for version 7.3.1227
Bram Moolenaar <bram@vim.org>
parents:
4962
diff
changeset
|
1313 {"bindeval", VimEvalPy, METH_O, "Like eval(), but returns objects attached to vim ones"}, |
5cee875f3096
updated for version 7.3.1227
Bram Moolenaar <bram@vim.org>
parents:
4962
diff
changeset
|
1314 {"strwidth", VimStrwidth, METH_O, "Screen string width, counts <Tab> as having width 1"}, |
4833
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
1315 {"chdir", (PyCFunction)VimChdir, METH_VARARGS|METH_KEYWORDS, "Change directory"}, |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
1316 {"fchdir", (PyCFunction)VimFchdir, METH_VARARGS|METH_KEYWORDS, "Change directory"}, |
4964
5cee875f3096
updated for version 7.3.1227
Bram Moolenaar <bram@vim.org>
parents:
4962
diff
changeset
|
1317 {"foreach_rtp", VimForeachRTP, METH_O, "Call given callable for each path in &rtp"}, |
4833
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
1318 {"find_module", FinderFindModule, METH_VARARGS, "Internal use only, returns loader object for any input it receives"}, |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
1319 {"path_hook", VimPathHook, METH_VARARGS, "Hook function to install in sys.path_hooks"}, |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
1320 {"_get_paths", (PyCFunction)Vim_GetPaths, METH_NOARGS, "Get &rtp-based additions to sys.path"}, |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
1321 { NULL, NULL, 0, NULL} |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1322 }; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1323 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1324 /* |
4397 | 1325 * Generic iterator object |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1326 */ |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1327 |
4397 | 1328 static PyTypeObject IterType; |
1329 | |
1330 typedef PyObject *(*nextfun)(void **); | |
1331 typedef void (*destructorfun)(void *); | |
4433 | 1332 typedef int (*traversefun)(void *, visitproc, void *); |
1333 typedef int (*clearfun)(void **); | |
4397 | 1334 |
4438 | 1335 /* Main purpose of this object is removing the need for do python |
1336 * initialization (i.e. PyType_Ready and setting type attributes) for a big | |
1337 * bunch of objects. */ | |
4385 | 1338 |
4319 | 1339 typedef struct |
1340 { | |
1341 PyObject_HEAD | |
4397 | 1342 void *cur; |
1343 nextfun next; | |
1344 destructorfun destruct; | |
4433 | 1345 traversefun traverse; |
1346 clearfun clear; | |
4397 | 1347 } IterObject; |
1348 | |
1349 static PyObject * | |
4433 | 1350 IterNew(void *start, destructorfun destruct, nextfun next, traversefun traverse, |
1351 clearfun clear) | |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1352 { |
4397 | 1353 IterObject *self; |
1354 | |
4500 | 1355 self = PyObject_GC_New(IterObject, &IterType); |
4397 | 1356 self->cur = start; |
1357 self->next = next; | |
1358 self->destruct = destruct; | |
4433 | 1359 self->traverse = traverse; |
1360 self->clear = clear; | |
4397 | 1361 |
1362 return (PyObject *)(self); | |
1363 } | |
1364 | |
1365 static void | |
4488 | 1366 IterDestructor(IterObject *self) |
4397 | 1367 { |
4500 | 1368 PyObject_GC_UnTrack((void *)(self)); |
4488 | 1369 self->destruct(self->cur); |
4500 | 1370 PyObject_GC_Del((void *)(self)); |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1371 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1372 |
4433 | 1373 static int |
4488 | 1374 IterTraverse(IterObject *self, visitproc visit, void *arg) |
4433 | 1375 { |
4488 | 1376 if (self->traverse != NULL) |
1377 return self->traverse(self->cur, visit, arg); | |
4433 | 1378 else |
1379 return 0; | |
1380 } | |
1381 | |
4438 | 1382 /* Mac OSX defines clear() somewhere. */ |
1383 #ifdef clear | |
1384 # undef clear | |
1385 #endif | |
1386 | |
4433 | 1387 static int |
4488 | 1388 IterClear(IterObject *self) |
4433 | 1389 { |
4488 | 1390 if (self->clear != NULL) |
1391 return self->clear(&self->cur); | |
4433 | 1392 else |
1393 return 0; | |
1394 } | |
1395 | |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1396 static PyObject * |
4488 | 1397 IterNext(IterObject *self) |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1398 { |
4488 | 1399 return self->next(&self->cur); |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1400 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1401 |
4397 | 1402 static PyObject * |
1403 IterIter(PyObject *self) | |
1404 { | |
4609
12421d8a45d5
updated for version 7.3.1052
Bram Moolenaar <bram@vim.org>
parents:
4607
diff
changeset
|
1405 Py_INCREF(self); |
4397 | 1406 return self; |
1407 } | |
4393 | 1408 |
3618 | 1409 typedef struct pylinkedlist_S { |
1410 struct pylinkedlist_S *pll_next; | |
1411 struct pylinkedlist_S *pll_prev; | |
1412 PyObject *pll_obj; | |
1413 } pylinkedlist_T; | |
1414 | |
1415 static pylinkedlist_T *lastdict = NULL; | |
1416 static pylinkedlist_T *lastlist = NULL; | |
1417 | |
1418 static void | |
1419 pyll_remove(pylinkedlist_T *ref, pylinkedlist_T **last) | |
1420 { | |
1421 if (ref->pll_prev == NULL) | |
1422 { | |
1423 if (ref->pll_next == NULL) | |
1424 { | |
1425 *last = NULL; | |
1426 return; | |
1427 } | |
1428 } | |
1429 else | |
1430 ref->pll_prev->pll_next = ref->pll_next; | |
1431 | |
1432 if (ref->pll_next == NULL) | |
1433 *last = ref->pll_prev; | |
1434 else | |
1435 ref->pll_next->pll_prev = ref->pll_prev; | |
1436 } | |
1437 | |
1438 static void | |
1439 pyll_add(PyObject *self, pylinkedlist_T *ref, pylinkedlist_T **last) | |
1440 { | |
1441 if (*last == NULL) | |
1442 ref->pll_prev = NULL; | |
1443 else | |
1444 { | |
1445 (*last)->pll_next = ref; | |
1446 ref->pll_prev = *last; | |
1447 } | |
1448 ref->pll_next = NULL; | |
1449 ref->pll_obj = self; | |
1450 *last = ref; | |
1451 } | |
1452 | |
1453 static PyTypeObject DictionaryType; | |
1454 | |
1455 typedef struct | |
1456 { | |
1457 PyObject_HEAD | |
1458 dict_T *dict; | |
1459 pylinkedlist_T ref; | |
1460 } DictionaryObject; | |
1461 | |
4627
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1462 static PyObject *DictionaryUpdate(DictionaryObject *, PyObject *, PyObject *); |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1463 |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1464 #define NEW_DICTIONARY(dict) DictionaryNew(&DictionaryType, dict) |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1465 |
3618 | 1466 static PyObject * |
4627
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1467 DictionaryNew(PyTypeObject *subtype, dict_T *dict) |
3618 | 1468 { |
1469 DictionaryObject *self; | |
1470 | |
4627
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1471 self = (DictionaryObject *) subtype->tp_alloc(subtype, 0); |
3618 | 1472 if (self == NULL) |
1473 return NULL; | |
1474 self->dict = dict; | |
1475 ++dict->dv_refcount; | |
1476 | |
1477 pyll_add((PyObject *)(self), &self->ref, &lastdict); | |
1478 | |
1479 return (PyObject *)(self); | |
1480 } | |
1481 | |
4627
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1482 static dict_T * |
4922
8dd2769ab75c
updated for version 7.3.1206
Bram Moolenaar <bram@vim.org>
parents:
4918
diff
changeset
|
1483 py_dict_alloc(void) |
4627
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1484 { |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
1485 dict_T *ret; |
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
1486 |
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
1487 if (!(ret = dict_alloc())) |
4627
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1488 { |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1489 PyErr_NoMemory(); |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1490 return NULL; |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1491 } |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
1492 ++ret->dv_refcount; |
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
1493 |
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
1494 return ret; |
4627
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1495 } |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1496 |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1497 static PyObject * |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1498 DictionaryConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs) |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1499 { |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1500 DictionaryObject *self; |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1501 dict_T *dict; |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1502 |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1503 if (!(dict = py_dict_alloc())) |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1504 return NULL; |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1505 |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1506 self = (DictionaryObject *) DictionaryNew(subtype, dict); |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1507 |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1508 --dict->dv_refcount; |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1509 |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1510 if (kwargs || PyTuple_Size(args)) |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1511 { |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1512 PyObject *tmp; |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1513 if (!(tmp = DictionaryUpdate(self, args, kwargs))) |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1514 { |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1515 Py_DECREF(self); |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1516 return NULL; |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1517 } |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1518 |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1519 Py_DECREF(tmp); |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1520 } |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1521 |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1522 return (PyObject *)(self); |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1523 } |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1524 |
4319 | 1525 static void |
4488 | 1526 DictionaryDestructor(DictionaryObject *self) |
4319 | 1527 { |
4488 | 1528 pyll_remove(&self->ref, &lastdict); |
1529 dict_unref(self->dict); | |
4319 | 1530 |
1531 DESTRUCTOR_FINISH(self); | |
1532 } | |
1533 | |
4599
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
1534 static char *DictionaryAttrs[] = { |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
1535 "locked", "scope", |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
1536 NULL |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
1537 }; |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
1538 |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
1539 static PyObject * |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
1540 DictionaryDir(PyObject *self) |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
1541 { |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
1542 return ObjectDir(self, DictionaryAttrs); |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
1543 } |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
1544 |
3618 | 1545 static int |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
1546 DictionarySetattr(DictionaryObject *self, char *name, PyObject *valObject) |
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
1547 { |
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
1548 if (valObject == NULL) |
3828 | 1549 { |
4968
b6e693e1f946
updated for version 7.3.1229
Bram Moolenaar <bram@vim.org>
parents:
4966
diff
changeset
|
1550 PyErr_SET_STRING(PyExc_AttributeError, |
4978
f4969f8f66e9
updated for version 7.3.1234
Bram Moolenaar <bram@vim.org>
parents:
4976
diff
changeset
|
1551 N_("cannot delete vim.Dictionary attributes")); |
3828 | 1552 return -1; |
1553 } | |
1554 | |
1555 if (strcmp(name, "locked") == 0) | |
1556 { | |
4488 | 1557 if (self->dict->dv_lock == VAR_FIXED) |
3828 | 1558 { |
4978
f4969f8f66e9
updated for version 7.3.1234
Bram Moolenaar <bram@vim.org>
parents:
4976
diff
changeset
|
1559 PyErr_SET_STRING(PyExc_TypeError, |
f4969f8f66e9
updated for version 7.3.1234
Bram Moolenaar <bram@vim.org>
parents:
4976
diff
changeset
|
1560 N_("cannot modify fixed dictionary")); |
3828 | 1561 return -1; |
1562 } | |
1563 else | |
1564 { | |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
1565 int istrue = PyObject_IsTrue(valObject); |
4411 | 1566 if (istrue == -1) |
1567 return -1; | |
1568 else if (istrue) | |
4488 | 1569 self->dict->dv_lock = VAR_LOCKED; |
3828 | 1570 else |
4488 | 1571 self->dict->dv_lock = 0; |
3828 | 1572 } |
1573 return 0; | |
1574 } | |
1575 else | |
1576 { | |
4978
f4969f8f66e9
updated for version 7.3.1234
Bram Moolenaar <bram@vim.org>
parents:
4976
diff
changeset
|
1577 PyErr_FORMAT(PyExc_AttributeError, N_("cannot set attribute %s"), name); |
3828 | 1578 return -1; |
1579 } | |
1580 } | |
1581 | |
1582 static PyInt | |
4488 | 1583 DictionaryLength(DictionaryObject *self) |
3618 | 1584 { |
4488 | 1585 return ((PyInt) (self->dict->dv_hashtab.ht_used)); |
3618 | 1586 } |
1587 | |
4627
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1588 #define DICT_FLAG_HAS_DEFAULT 0x01 |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1589 #define DICT_FLAG_POP 0x02 |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1590 #define DICT_FLAG_NONE_DEFAULT 0x04 |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1591 #define DICT_FLAG_RETURN_BOOL 0x08 /* Incompatible with DICT_FLAG_POP */ |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1592 #define DICT_FLAG_RETURN_PAIR 0x10 |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1593 |
3618 | 1594 static PyObject * |
4627
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1595 _DictionaryItem(DictionaryObject *self, PyObject *args, int flags) |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1596 { |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1597 PyObject *keyObject; |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1598 PyObject *defObject = ((flags & DICT_FLAG_NONE_DEFAULT)? Py_None : NULL); |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
1599 PyObject *ret; |
3618 | 1600 char_u *key; |
3792 | 1601 dictitem_T *di; |
4627
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1602 dict_T *dict = self->dict; |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1603 hashitem_T *hi; |
4659
80b0081824fa
updated for version 7.3.1077
Bram Moolenaar <bram@vim.org>
parents:
4653
diff
changeset
|
1604 PyObject *todecref; |
3618 | 1605 |
4627
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1606 if (flags & DICT_FLAG_HAS_DEFAULT) |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1607 { |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1608 if (!PyArg_ParseTuple(args, "O|O", &keyObject, &defObject)) |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1609 return NULL; |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1610 } |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1611 else |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1612 keyObject = args; |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1613 |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1614 if (flags & DICT_FLAG_RETURN_BOOL) |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1615 defObject = Py_False; |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1616 |
4659
80b0081824fa
updated for version 7.3.1077
Bram Moolenaar <bram@vim.org>
parents:
4653
diff
changeset
|
1617 if (!(key = StringToChars(keyObject, &todecref))) |
80b0081824fa
updated for version 7.3.1077
Bram Moolenaar <bram@vim.org>
parents:
4653
diff
changeset
|
1618 return NULL; |
80b0081824fa
updated for version 7.3.1077
Bram Moolenaar <bram@vim.org>
parents:
4653
diff
changeset
|
1619 |
80b0081824fa
updated for version 7.3.1077
Bram Moolenaar <bram@vim.org>
parents:
4653
diff
changeset
|
1620 if (*key == NUL) |
80b0081824fa
updated for version 7.3.1077
Bram Moolenaar <bram@vim.org>
parents:
4653
diff
changeset
|
1621 { |
80b0081824fa
updated for version 7.3.1077
Bram Moolenaar <bram@vim.org>
parents:
4653
diff
changeset
|
1622 RAISE_NO_EMPTY_KEYS; |
4702
26f2dbea7443
updated for version 7.3.1098
Bram Moolenaar <bram@vim.org>
parents:
4698
diff
changeset
|
1623 Py_XDECREF(todecref); |
4659
80b0081824fa
updated for version 7.3.1077
Bram Moolenaar <bram@vim.org>
parents:
4653
diff
changeset
|
1624 return NULL; |
80b0081824fa
updated for version 7.3.1077
Bram Moolenaar <bram@vim.org>
parents:
4653
diff
changeset
|
1625 } |
3792 | 1626 |
4627
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1627 hi = hash_find(&dict->dv_hashtab, key); |
3792 | 1628 |
4659
80b0081824fa
updated for version 7.3.1077
Bram Moolenaar <bram@vim.org>
parents:
4653
diff
changeset
|
1629 Py_XDECREF(todecref); |
3824 | 1630 |
4627
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1631 if (HASHITEM_EMPTY(hi)) |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1632 { |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1633 if (defObject) |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1634 { |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1635 Py_INCREF(defObject); |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1636 return defObject; |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1637 } |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1638 else |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1639 { |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1640 PyErr_SetObject(PyExc_KeyError, keyObject); |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1641 return NULL; |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1642 } |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1643 } |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1644 else if (flags & DICT_FLAG_RETURN_BOOL) |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1645 { |
5666 | 1646 ret = Py_True; |
1647 Py_INCREF(ret); | |
1648 return ret; | |
4627
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1649 } |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1650 |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1651 di = dict_lookup(hi); |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1652 |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
1653 if (!(ret = ConvertToPyObject(&di->di_tv))) |
4627
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1654 return NULL; |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1655 |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1656 if (flags & DICT_FLAG_POP) |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1657 { |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1658 if (dict->dv_lock) |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1659 { |
4970
f5c822e5a0eb
updated for version 7.3.1230
Bram Moolenaar <bram@vim.org>
parents:
4968
diff
changeset
|
1660 RAISE_LOCKED_DICTIONARY; |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
1661 Py_DECREF(ret); |
4627
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1662 return NULL; |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1663 } |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1664 |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1665 hash_remove(&dict->dv_hashtab, hi); |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1666 dictitem_free(di); |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1667 } |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1668 |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
1669 return ret; |
4627
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1670 } |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1671 |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1672 static PyObject * |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1673 DictionaryItem(DictionaryObject *self, PyObject *keyObject) |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1674 { |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1675 return _DictionaryItem(self, keyObject, 0); |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1676 } |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1677 |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1678 static int |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1679 DictionaryContains(DictionaryObject *self, PyObject *keyObject) |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1680 { |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1681 PyObject *rObj = _DictionaryItem(self, keyObject, DICT_FLAG_RETURN_BOOL); |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
1682 int ret; |
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
1683 |
5426 | 1684 if (rObj == NULL) |
1685 return -1; | |
1686 | |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
1687 ret = (rObj == Py_True); |
4627
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1688 |
4982
39980afcf54a
updated for version 7.3.1236
Bram Moolenaar <bram@vim.org>
parents:
4978
diff
changeset
|
1689 Py_DECREF(rObj); |
4627
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1690 |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
1691 return ret; |
4627
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1692 } |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1693 |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1694 typedef struct |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1695 { |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1696 hashitem_T *ht_array; |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1697 long_u ht_used; |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1698 hashtab_T *ht; |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1699 hashitem_T *hi; |
4667
9e7ef781d494
updated for version 7.3.1081
Bram Moolenaar <bram@vim.org>
parents:
4659
diff
changeset
|
1700 long_u todo; |
4627
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1701 } dictiterinfo_T; |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1702 |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1703 static PyObject * |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1704 DictionaryIterNext(dictiterinfo_T **dii) |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1705 { |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
1706 PyObject *ret; |
4627
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1707 |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1708 if (!(*dii)->todo) |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1709 return NULL; |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1710 |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1711 if ((*dii)->ht->ht_array != (*dii)->ht_array || |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1712 (*dii)->ht->ht_used != (*dii)->ht_used) |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1713 { |
4968
b6e693e1f946
updated for version 7.3.1229
Bram Moolenaar <bram@vim.org>
parents:
4966
diff
changeset
|
1714 PyErr_SET_STRING(PyExc_RuntimeError, |
4978
f4969f8f66e9
updated for version 7.3.1234
Bram Moolenaar <bram@vim.org>
parents:
4976
diff
changeset
|
1715 N_("hashtab changed during iteration")); |
3792 | 1716 return NULL; |
1717 } | |
3618 | 1718 |
4627
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1719 while (((*dii)->todo) && HASHITEM_EMPTY((*dii)->hi)) |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1720 ++((*dii)->hi); |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1721 |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1722 --((*dii)->todo); |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1723 |
5088
34c629c3b4ba
updated for version 7.3.1287
Bram Moolenaar <bram@vim.org>
parents:
4995
diff
changeset
|
1724 if (!(ret = PyBytes_FromString((char *)(*dii)->hi->hi_key))) |
4627
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1725 return NULL; |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1726 |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
1727 return ret; |
4627
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1728 } |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1729 |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1730 static PyObject * |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1731 DictionaryIter(DictionaryObject *self) |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1732 { |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1733 dictiterinfo_T *dii; |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1734 hashtab_T *ht; |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1735 |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1736 if (!(dii = PyMem_New(dictiterinfo_T, 1))) |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1737 { |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1738 PyErr_NoMemory(); |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1739 return NULL; |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1740 } |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1741 |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1742 ht = &self->dict->dv_hashtab; |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1743 dii->ht_array = ht->ht_array; |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1744 dii->ht_used = ht->ht_used; |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1745 dii->ht = ht; |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1746 dii->hi = dii->ht_array; |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1747 dii->todo = dii->ht_used; |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1748 |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1749 return IterNew(dii, |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1750 (destructorfun) PyMem_Free, (nextfun) DictionaryIterNext, |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1751 NULL, NULL); |
3618 | 1752 } |
1753 | |
1754 static PyInt | |
4922
8dd2769ab75c
updated for version 7.3.1206
Bram Moolenaar <bram@vim.org>
parents:
4918
diff
changeset
|
1755 DictionaryAssItem( |
8dd2769ab75c
updated for version 7.3.1206
Bram Moolenaar <bram@vim.org>
parents:
4918
diff
changeset
|
1756 DictionaryObject *self, PyObject *keyObject, PyObject *valObject) |
3618 | 1757 { |
1758 char_u *key; | |
1759 typval_T tv; | |
4601
19d406a8509d
updated for version 7.3.1048
Bram Moolenaar <bram@vim.org>
parents:
4599
diff
changeset
|
1760 dict_T *dict = self->dict; |
3618 | 1761 dictitem_T *di; |
4659
80b0081824fa
updated for version 7.3.1077
Bram Moolenaar <bram@vim.org>
parents:
4653
diff
changeset
|
1762 PyObject *todecref; |
3618 | 1763 |
4601
19d406a8509d
updated for version 7.3.1048
Bram Moolenaar <bram@vim.org>
parents:
4599
diff
changeset
|
1764 if (dict->dv_lock) |
3618 | 1765 { |
4970
f5c822e5a0eb
updated for version 7.3.1230
Bram Moolenaar <bram@vim.org>
parents:
4968
diff
changeset
|
1766 RAISE_LOCKED_DICTIONARY; |
3618 | 1767 return -1; |
1768 } | |
1769 | |
4659
80b0081824fa
updated for version 7.3.1077
Bram Moolenaar <bram@vim.org>
parents:
4653
diff
changeset
|
1770 if (!(key = StringToChars(keyObject, &todecref))) |
80b0081824fa
updated for version 7.3.1077
Bram Moolenaar <bram@vim.org>
parents:
4653
diff
changeset
|
1771 return -1; |
4702
26f2dbea7443
updated for version 7.3.1098
Bram Moolenaar <bram@vim.org>
parents:
4698
diff
changeset
|
1772 |
4659
80b0081824fa
updated for version 7.3.1077
Bram Moolenaar <bram@vim.org>
parents:
4653
diff
changeset
|
1773 if (*key == NUL) |
80b0081824fa
updated for version 7.3.1077
Bram Moolenaar <bram@vim.org>
parents:
4653
diff
changeset
|
1774 { |
80b0081824fa
updated for version 7.3.1077
Bram Moolenaar <bram@vim.org>
parents:
4653
diff
changeset
|
1775 RAISE_NO_EMPTY_KEYS; |
4706
bf917ee1fad2
updated for version 7.3.1100
Bram Moolenaar <bram@vim.org>
parents:
4704
diff
changeset
|
1776 Py_XDECREF(todecref); |
4659
80b0081824fa
updated for version 7.3.1077
Bram Moolenaar <bram@vim.org>
parents:
4653
diff
changeset
|
1777 return -1; |
80b0081824fa
updated for version 7.3.1077
Bram Moolenaar <bram@vim.org>
parents:
4653
diff
changeset
|
1778 } |
3618 | 1779 |
4601
19d406a8509d
updated for version 7.3.1048
Bram Moolenaar <bram@vim.org>
parents:
4599
diff
changeset
|
1780 di = dict_find(dict, key, -1); |
3618 | 1781 |
1782 if (valObject == NULL) | |
1783 { | |
3636 | 1784 hashitem_T *hi; |
1785 | |
3618 | 1786 if (di == NULL) |
1787 { | |
4659
80b0081824fa
updated for version 7.3.1077
Bram Moolenaar <bram@vim.org>
parents:
4653
diff
changeset
|
1788 Py_XDECREF(todecref); |
4403 | 1789 PyErr_SetObject(PyExc_KeyError, keyObject); |
3618 | 1790 return -1; |
1791 } | |
4601
19d406a8509d
updated for version 7.3.1048
Bram Moolenaar <bram@vim.org>
parents:
4599
diff
changeset
|
1792 hi = hash_find(&dict->dv_hashtab, di->di_key); |
19d406a8509d
updated for version 7.3.1048
Bram Moolenaar <bram@vim.org>
parents:
4599
diff
changeset
|
1793 hash_remove(&dict->dv_hashtab, hi); |
3618 | 1794 dictitem_free(di); |
4706
bf917ee1fad2
updated for version 7.3.1100
Bram Moolenaar <bram@vim.org>
parents:
4704
diff
changeset
|
1795 Py_XDECREF(todecref); |
3618 | 1796 return 0; |
1797 } | |
1798 | |
1799 if (ConvertFromPyObject(valObject, &tv) == -1) | |
4706
bf917ee1fad2
updated for version 7.3.1100
Bram Moolenaar <bram@vim.org>
parents:
4704
diff
changeset
|
1800 { |
bf917ee1fad2
updated for version 7.3.1100
Bram Moolenaar <bram@vim.org>
parents:
4704
diff
changeset
|
1801 Py_XDECREF(todecref); |
3618 | 1802 return -1; |
4706
bf917ee1fad2
updated for version 7.3.1100
Bram Moolenaar <bram@vim.org>
parents:
4704
diff
changeset
|
1803 } |
3618 | 1804 |
1805 if (di == NULL) | |
1806 { | |
4627
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1807 if (!(di = dictitem_alloc(key))) |
3618 | 1808 { |
4659
80b0081824fa
updated for version 7.3.1077
Bram Moolenaar <bram@vim.org>
parents:
4653
diff
changeset
|
1809 Py_XDECREF(todecref); |
3618 | 1810 PyErr_NoMemory(); |
1811 return -1; | |
1812 } | |
1813 di->di_tv.v_lock = 0; | |
4627
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1814 di->di_tv.v_type = VAR_UNKNOWN; |
3618 | 1815 |
4601
19d406a8509d
updated for version 7.3.1048
Bram Moolenaar <bram@vim.org>
parents:
4599
diff
changeset
|
1816 if (dict_add(dict, di) == FAIL) |
3618 | 1817 { |
1818 vim_free(di); | |
4627
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1819 dictitem_free(di); |
4970
f5c822e5a0eb
updated for version 7.3.1230
Bram Moolenaar <bram@vim.org>
parents:
4968
diff
changeset
|
1820 RAISE_KEY_ADD_FAIL(key); |
f5c822e5a0eb
updated for version 7.3.1230
Bram Moolenaar <bram@vim.org>
parents:
4968
diff
changeset
|
1821 Py_XDECREF(todecref); |
3618 | 1822 return -1; |
1823 } | |
1824 } | |
1825 else | |
1826 clear_tv(&di->di_tv); | |
1827 | |
4659
80b0081824fa
updated for version 7.3.1077
Bram Moolenaar <bram@vim.org>
parents:
4653
diff
changeset
|
1828 Py_XDECREF(todecref); |
3618 | 1829 |
1830 copy_tv(&tv, &di->di_tv); | |
4509
b498224f5b41
updated for version 7.3.1002
Bram Moolenaar <bram@vim.org>
parents:
4500
diff
changeset
|
1831 clear_tv(&tv); |
3618 | 1832 return 0; |
1833 } | |
1834 | |
4627
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1835 typedef PyObject *(*hi_to_py)(hashitem_T *); |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1836 |
3618 | 1837 static PyObject * |
4627
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1838 DictionaryListObjects(DictionaryObject *self, hi_to_py hiconvert) |
3618 | 1839 { |
4488 | 1840 dict_T *dict = self->dict; |
3618 | 1841 long_u todo = dict->dv_hashtab.ht_used; |
1842 Py_ssize_t i = 0; | |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
1843 PyObject *ret; |
3618 | 1844 hashitem_T *hi; |
4627
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1845 PyObject *newObj; |
3618 | 1846 |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
1847 ret = PyList_New(todo); |
3618 | 1848 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi) |
1849 { | |
1850 if (!HASHITEM_EMPTY(hi)) | |
1851 { | |
4627
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1852 if (!(newObj = hiconvert(hi))) |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1853 { |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
1854 Py_DECREF(ret); |
4627
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1855 return NULL; |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1856 } |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
1857 PyList_SET_ITEM(ret, i, newObj); |
3618 | 1858 --todo; |
1859 ++i; | |
1860 } | |
1861 } | |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
1862 return ret; |
3618 | 1863 } |
1864 | |
4627
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1865 static PyObject * |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1866 dict_key(hashitem_T *hi) |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1867 { |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1868 return PyBytes_FromString((char *)(hi->hi_key)); |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1869 } |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1870 |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1871 static PyObject * |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1872 DictionaryListKeys(DictionaryObject *self) |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1873 { |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1874 return DictionaryListObjects(self, dict_key); |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1875 } |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1876 |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1877 static PyObject * |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1878 dict_val(hashitem_T *hi) |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1879 { |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1880 dictitem_T *di; |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1881 |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1882 di = dict_lookup(hi); |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1883 return ConvertToPyObject(&di->di_tv); |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1884 } |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1885 |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1886 static PyObject * |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1887 DictionaryListValues(DictionaryObject *self) |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1888 { |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1889 return DictionaryListObjects(self, dict_val); |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1890 } |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1891 |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1892 static PyObject * |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1893 dict_item(hashitem_T *hi) |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1894 { |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1895 PyObject *keyObject; |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1896 PyObject *valObject; |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
1897 PyObject *ret; |
4627
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1898 |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1899 if (!(keyObject = dict_key(hi))) |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1900 return NULL; |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1901 |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1902 if (!(valObject = dict_val(hi))) |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1903 { |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1904 Py_DECREF(keyObject); |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1905 return NULL; |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1906 } |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1907 |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
1908 ret = Py_BuildValue("(OO)", keyObject, valObject); |
4627
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1909 |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1910 Py_DECREF(keyObject); |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1911 Py_DECREF(valObject); |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1912 |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
1913 return ret; |
4627
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1914 } |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1915 |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1916 static PyObject * |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1917 DictionaryListItems(DictionaryObject *self) |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1918 { |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1919 return DictionaryListObjects(self, dict_item); |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1920 } |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1921 |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1922 static PyObject * |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1923 DictionaryUpdate(DictionaryObject *self, PyObject *args, PyObject *kwargs) |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1924 { |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1925 dict_T *dict = self->dict; |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1926 |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1927 if (dict->dv_lock) |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1928 { |
4970
f5c822e5a0eb
updated for version 7.3.1230
Bram Moolenaar <bram@vim.org>
parents:
4968
diff
changeset
|
1929 RAISE_LOCKED_DICTIONARY; |
4627
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1930 return NULL; |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1931 } |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1932 |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1933 if (kwargs) |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1934 { |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1935 typval_T tv; |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1936 |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1937 if (ConvertFromPyMapping(kwargs, &tv) == -1) |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1938 return NULL; |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1939 |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1940 VimTryStart(); |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1941 dict_extend(self->dict, tv.vval.v_dict, (char_u *) "force"); |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1942 clear_tv(&tv); |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1943 if (VimTryEnd()) |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1944 return NULL; |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1945 } |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1946 else |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1947 { |
5659 | 1948 PyObject *obj = NULL; |
1949 | |
1950 if (!PyArg_ParseTuple(args, "|O", &obj)) | |
4627
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1951 return NULL; |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1952 |
5659 | 1953 if (obj == NULL) |
1954 { | |
1955 Py_INCREF(Py_None); | |
1956 return Py_None; | |
1957 } | |
1958 | |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
1959 if (PyObject_HasAttrString(obj, "keys")) |
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
1960 return DictionaryUpdate(self, NULL, obj); |
4627
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1961 else |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1962 { |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1963 PyObject *iterator; |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1964 PyObject *item; |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1965 |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
1966 if (!(iterator = PyObject_GetIter(obj))) |
4627
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1967 return NULL; |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1968 |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1969 while ((item = PyIter_Next(iterator))) |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1970 { |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1971 PyObject *fast; |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1972 PyObject *keyObject; |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1973 PyObject *valObject; |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1974 PyObject *todecref; |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1975 char_u *key; |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1976 dictitem_T *di; |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1977 |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1978 if (!(fast = PySequence_Fast(item, ""))) |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1979 { |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1980 Py_DECREF(iterator); |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1981 Py_DECREF(item); |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1982 return NULL; |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1983 } |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1984 |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1985 Py_DECREF(item); |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1986 |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1987 if (PySequence_Fast_GET_SIZE(fast) != 2) |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1988 { |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1989 Py_DECREF(iterator); |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1990 Py_DECREF(fast); |
4970
f5c822e5a0eb
updated for version 7.3.1230
Bram Moolenaar <bram@vim.org>
parents:
4968
diff
changeset
|
1991 PyErr_FORMAT(PyExc_ValueError, |
4978
f4969f8f66e9
updated for version 7.3.1234
Bram Moolenaar <bram@vim.org>
parents:
4976
diff
changeset
|
1992 N_("expected sequence element of size 2, " |
f4969f8f66e9
updated for version 7.3.1234
Bram Moolenaar <bram@vim.org>
parents:
4976
diff
changeset
|
1993 "but got sequence of size %d"), |
4982
39980afcf54a
updated for version 7.3.1236
Bram Moolenaar <bram@vim.org>
parents:
4978
diff
changeset
|
1994 (int) PySequence_Fast_GET_SIZE(fast)); |
4627
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1995 return NULL; |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1996 } |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1997 |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1998 keyObject = PySequence_Fast_GET_ITEM(fast, 0); |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
1999 |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
2000 if (!(key = StringToChars(keyObject, &todecref))) |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
2001 { |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
2002 Py_DECREF(iterator); |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
2003 Py_DECREF(fast); |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
2004 return NULL; |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
2005 } |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
2006 |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
2007 di = dictitem_alloc(key); |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
2008 |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
2009 Py_XDECREF(todecref); |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
2010 |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
2011 if (di == NULL) |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
2012 { |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
2013 Py_DECREF(fast); |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
2014 Py_DECREF(iterator); |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
2015 PyErr_NoMemory(); |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
2016 return NULL; |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
2017 } |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
2018 di->di_tv.v_lock = 0; |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
2019 di->di_tv.v_type = VAR_UNKNOWN; |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
2020 |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
2021 valObject = PySequence_Fast_GET_ITEM(fast, 1); |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
2022 |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
2023 if (ConvertFromPyObject(valObject, &di->di_tv) == -1) |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
2024 { |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
2025 Py_DECREF(iterator); |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
2026 Py_DECREF(fast); |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
2027 dictitem_free(di); |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
2028 return NULL; |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
2029 } |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
2030 |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
2031 Py_DECREF(fast); |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
2032 |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
2033 if (dict_add(dict, di) == FAIL) |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
2034 { |
4970
f5c822e5a0eb
updated for version 7.3.1230
Bram Moolenaar <bram@vim.org>
parents:
4968
diff
changeset
|
2035 RAISE_KEY_ADD_FAIL(di->di_key); |
4627
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
2036 Py_DECREF(iterator); |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
2037 dictitem_free(di); |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
2038 return NULL; |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
2039 } |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
2040 } |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
2041 |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
2042 Py_DECREF(iterator); |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
2043 |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
2044 /* Iterator may have finished due to an exception */ |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
2045 if (PyErr_Occurred()) |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
2046 return NULL; |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
2047 } |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
2048 } |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
2049 Py_INCREF(Py_None); |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
2050 return Py_None; |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
2051 } |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
2052 |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
2053 static PyObject * |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
2054 DictionaryGet(DictionaryObject *self, PyObject *args) |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
2055 { |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
2056 return _DictionaryItem(self, args, |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
2057 DICT_FLAG_HAS_DEFAULT|DICT_FLAG_NONE_DEFAULT); |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
2058 } |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
2059 |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
2060 static PyObject * |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
2061 DictionaryPop(DictionaryObject *self, PyObject *args) |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
2062 { |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
2063 return _DictionaryItem(self, args, DICT_FLAG_HAS_DEFAULT|DICT_FLAG_POP); |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
2064 } |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
2065 |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
2066 static PyObject * |
4698
2db005052371
updated for version 7.3.1096
Bram Moolenaar <bram@vim.org>
parents:
4667
diff
changeset
|
2067 DictionaryPopItem(DictionaryObject *self) |
2db005052371
updated for version 7.3.1096
Bram Moolenaar <bram@vim.org>
parents:
4667
diff
changeset
|
2068 { |
2db005052371
updated for version 7.3.1096
Bram Moolenaar <bram@vim.org>
parents:
4667
diff
changeset
|
2069 hashitem_T *hi; |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
2070 PyObject *ret; |
4698
2db005052371
updated for version 7.3.1096
Bram Moolenaar <bram@vim.org>
parents:
4667
diff
changeset
|
2071 PyObject *valObject; |
2db005052371
updated for version 7.3.1096
Bram Moolenaar <bram@vim.org>
parents:
4667
diff
changeset
|
2072 dictitem_T *di; |
2db005052371
updated for version 7.3.1096
Bram Moolenaar <bram@vim.org>
parents:
4667
diff
changeset
|
2073 |
2db005052371
updated for version 7.3.1096
Bram Moolenaar <bram@vim.org>
parents:
4667
diff
changeset
|
2074 if (self->dict->dv_hashtab.ht_used == 0) |
2db005052371
updated for version 7.3.1096
Bram Moolenaar <bram@vim.org>
parents:
4667
diff
changeset
|
2075 { |
2db005052371
updated for version 7.3.1096
Bram Moolenaar <bram@vim.org>
parents:
4667
diff
changeset
|
2076 PyErr_SetNone(PyExc_KeyError); |
4627
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
2077 return NULL; |
4698
2db005052371
updated for version 7.3.1096
Bram Moolenaar <bram@vim.org>
parents:
4667
diff
changeset
|
2078 } |
2db005052371
updated for version 7.3.1096
Bram Moolenaar <bram@vim.org>
parents:
4667
diff
changeset
|
2079 |
2db005052371
updated for version 7.3.1096
Bram Moolenaar <bram@vim.org>
parents:
4667
diff
changeset
|
2080 hi = self->dict->dv_hashtab.ht_array; |
2db005052371
updated for version 7.3.1096
Bram Moolenaar <bram@vim.org>
parents:
4667
diff
changeset
|
2081 while (HASHITEM_EMPTY(hi)) |
2db005052371
updated for version 7.3.1096
Bram Moolenaar <bram@vim.org>
parents:
4667
diff
changeset
|
2082 ++hi; |
2db005052371
updated for version 7.3.1096
Bram Moolenaar <bram@vim.org>
parents:
4667
diff
changeset
|
2083 |
2db005052371
updated for version 7.3.1096
Bram Moolenaar <bram@vim.org>
parents:
4667
diff
changeset
|
2084 di = dict_lookup(hi); |
2db005052371
updated for version 7.3.1096
Bram Moolenaar <bram@vim.org>
parents:
4667
diff
changeset
|
2085 |
2db005052371
updated for version 7.3.1096
Bram Moolenaar <bram@vim.org>
parents:
4667
diff
changeset
|
2086 if (!(valObject = ConvertToPyObject(&di->di_tv))) |
2db005052371
updated for version 7.3.1096
Bram Moolenaar <bram@vim.org>
parents:
4667
diff
changeset
|
2087 return NULL; |
2db005052371
updated for version 7.3.1096
Bram Moolenaar <bram@vim.org>
parents:
4667
diff
changeset
|
2088 |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
2089 if (!(ret = Py_BuildValue("(" Py_bytes_fmt "O)", hi->hi_key, valObject))) |
4698
2db005052371
updated for version 7.3.1096
Bram Moolenaar <bram@vim.org>
parents:
4667
diff
changeset
|
2090 { |
2db005052371
updated for version 7.3.1096
Bram Moolenaar <bram@vim.org>
parents:
4667
diff
changeset
|
2091 Py_DECREF(valObject); |
2db005052371
updated for version 7.3.1096
Bram Moolenaar <bram@vim.org>
parents:
4667
diff
changeset
|
2092 return NULL; |
2db005052371
updated for version 7.3.1096
Bram Moolenaar <bram@vim.org>
parents:
4667
diff
changeset
|
2093 } |
2db005052371
updated for version 7.3.1096
Bram Moolenaar <bram@vim.org>
parents:
4667
diff
changeset
|
2094 |
2db005052371
updated for version 7.3.1096
Bram Moolenaar <bram@vim.org>
parents:
4667
diff
changeset
|
2095 hash_remove(&self->dict->dv_hashtab, hi); |
2db005052371
updated for version 7.3.1096
Bram Moolenaar <bram@vim.org>
parents:
4667
diff
changeset
|
2096 dictitem_free(di); |
2db005052371
updated for version 7.3.1096
Bram Moolenaar <bram@vim.org>
parents:
4667
diff
changeset
|
2097 |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
2098 return ret; |
4627
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
2099 } |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
2100 |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
2101 static PyObject * |
4964
5cee875f3096
updated for version 7.3.1227
Bram Moolenaar <bram@vim.org>
parents:
4962
diff
changeset
|
2102 DictionaryHasKey(DictionaryObject *self, PyObject *keyObject) |
5cee875f3096
updated for version 7.3.1227
Bram Moolenaar <bram@vim.org>
parents:
4962
diff
changeset
|
2103 { |
4627
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
2104 return _DictionaryItem(self, keyObject, DICT_FLAG_RETURN_BOOL); |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
2105 } |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
2106 |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
2107 static PySequenceMethods DictionaryAsSeq = { |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
2108 0, /* sq_length */ |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
2109 0, /* sq_concat */ |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
2110 0, /* sq_repeat */ |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
2111 0, /* sq_item */ |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
2112 0, /* sq_slice */ |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
2113 0, /* sq_ass_item */ |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
2114 0, /* sq_ass_slice */ |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
2115 (objobjproc) DictionaryContains, /* sq_contains */ |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
2116 0, /* sq_inplace_concat */ |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
2117 0, /* sq_inplace_repeat */ |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
2118 }; |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
2119 |
4385 | 2120 static PyMappingMethods DictionaryAsMapping = { |
2121 (lenfunc) DictionaryLength, | |
2122 (binaryfunc) DictionaryItem, | |
2123 (objobjargproc) DictionaryAssItem, | |
2124 }; | |
2125 | |
3618 | 2126 static struct PyMethodDef DictionaryMethods[] = { |
4492 | 2127 {"keys", (PyCFunction)DictionaryListKeys, METH_NOARGS, ""}, |
4627
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
2128 {"values", (PyCFunction)DictionaryListValues, METH_NOARGS, ""}, |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
2129 {"items", (PyCFunction)DictionaryListItems, METH_NOARGS, ""}, |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
2130 {"update", (PyCFunction)DictionaryUpdate, METH_VARARGS|METH_KEYWORDS, ""}, |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
2131 {"get", (PyCFunction)DictionaryGet, METH_VARARGS, ""}, |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
2132 {"pop", (PyCFunction)DictionaryPop, METH_VARARGS, ""}, |
4698
2db005052371
updated for version 7.3.1096
Bram Moolenaar <bram@vim.org>
parents:
4667
diff
changeset
|
2133 {"popitem", (PyCFunction)DictionaryPopItem, METH_NOARGS, ""}, |
4964
5cee875f3096
updated for version 7.3.1227
Bram Moolenaar <bram@vim.org>
parents:
4962
diff
changeset
|
2134 {"has_key", (PyCFunction)DictionaryHasKey, METH_O, ""}, |
4599
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
2135 {"__dir__", (PyCFunction)DictionaryDir, METH_NOARGS, ""}, |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
2136 { NULL, NULL, 0, NULL} |
3618 | 2137 }; |
2138 | |
2139 static PyTypeObject ListType; | |
2140 | |
2141 typedef struct | |
2142 { | |
2143 PyObject_HEAD | |
2144 list_T *list; | |
2145 pylinkedlist_T ref; | |
2146 } ListObject; | |
2147 | |
4629
e4e48d4ee040
updated for version 7.3.1062
Bram Moolenaar <bram@vim.org>
parents:
4627
diff
changeset
|
2148 #define NEW_LIST(list) ListNew(&ListType, list) |
e4e48d4ee040
updated for version 7.3.1062
Bram Moolenaar <bram@vim.org>
parents:
4627
diff
changeset
|
2149 |
3618 | 2150 static PyObject * |
4629
e4e48d4ee040
updated for version 7.3.1062
Bram Moolenaar <bram@vim.org>
parents:
4627
diff
changeset
|
2151 ListNew(PyTypeObject *subtype, list_T *list) |
3618 | 2152 { |
2153 ListObject *self; | |
2154 | |
4629
e4e48d4ee040
updated for version 7.3.1062
Bram Moolenaar <bram@vim.org>
parents:
4627
diff
changeset
|
2155 self = (ListObject *) subtype->tp_alloc(subtype, 0); |
3618 | 2156 if (self == NULL) |
2157 return NULL; | |
2158 self->list = list; | |
2159 ++list->lv_refcount; | |
2160 | |
2161 pyll_add((PyObject *)(self), &self->ref, &lastlist); | |
2162 | |
2163 return (PyObject *)(self); | |
2164 } | |
2165 | |
4629
e4e48d4ee040
updated for version 7.3.1062
Bram Moolenaar <bram@vim.org>
parents:
4627
diff
changeset
|
2166 static list_T * |
5166
467efeee8f9e
updated for version 7.4a.009
Bram Moolenaar <bram@vim.org>
parents:
5139
diff
changeset
|
2167 py_list_alloc(void) |
4629
e4e48d4ee040
updated for version 7.3.1062
Bram Moolenaar <bram@vim.org>
parents:
4627
diff
changeset
|
2168 { |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
2169 list_T *ret; |
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
2170 |
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
2171 if (!(ret = list_alloc())) |
4629
e4e48d4ee040
updated for version 7.3.1062
Bram Moolenaar <bram@vim.org>
parents:
4627
diff
changeset
|
2172 { |
e4e48d4ee040
updated for version 7.3.1062
Bram Moolenaar <bram@vim.org>
parents:
4627
diff
changeset
|
2173 PyErr_NoMemory(); |
e4e48d4ee040
updated for version 7.3.1062
Bram Moolenaar <bram@vim.org>
parents:
4627
diff
changeset
|
2174 return NULL; |
e4e48d4ee040
updated for version 7.3.1062
Bram Moolenaar <bram@vim.org>
parents:
4627
diff
changeset
|
2175 } |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
2176 ++ret->lv_refcount; |
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
2177 |
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
2178 return ret; |
4629
e4e48d4ee040
updated for version 7.3.1062
Bram Moolenaar <bram@vim.org>
parents:
4627
diff
changeset
|
2179 } |
e4e48d4ee040
updated for version 7.3.1062
Bram Moolenaar <bram@vim.org>
parents:
4627
diff
changeset
|
2180 |
e4e48d4ee040
updated for version 7.3.1062
Bram Moolenaar <bram@vim.org>
parents:
4627
diff
changeset
|
2181 static int |
e4e48d4ee040
updated for version 7.3.1062
Bram Moolenaar <bram@vim.org>
parents:
4627
diff
changeset
|
2182 list_py_concat(list_T *l, PyObject *obj, PyObject *lookup_dict) |
e4e48d4ee040
updated for version 7.3.1062
Bram Moolenaar <bram@vim.org>
parents:
4627
diff
changeset
|
2183 { |
e4e48d4ee040
updated for version 7.3.1062
Bram Moolenaar <bram@vim.org>
parents:
4627
diff
changeset
|
2184 PyObject *iterator; |
e4e48d4ee040
updated for version 7.3.1062
Bram Moolenaar <bram@vim.org>
parents:
4627
diff
changeset
|
2185 PyObject *item; |
e4e48d4ee040
updated for version 7.3.1062
Bram Moolenaar <bram@vim.org>
parents:
4627
diff
changeset
|
2186 listitem_T *li; |
e4e48d4ee040
updated for version 7.3.1062
Bram Moolenaar <bram@vim.org>
parents:
4627
diff
changeset
|
2187 |
e4e48d4ee040
updated for version 7.3.1062
Bram Moolenaar <bram@vim.org>
parents:
4627
diff
changeset
|
2188 if (!(iterator = PyObject_GetIter(obj))) |
e4e48d4ee040
updated for version 7.3.1062
Bram Moolenaar <bram@vim.org>
parents:
4627
diff
changeset
|
2189 return -1; |
e4e48d4ee040
updated for version 7.3.1062
Bram Moolenaar <bram@vim.org>
parents:
4627
diff
changeset
|
2190 |
e4e48d4ee040
updated for version 7.3.1062
Bram Moolenaar <bram@vim.org>
parents:
4627
diff
changeset
|
2191 while ((item = PyIter_Next(iterator))) |
e4e48d4ee040
updated for version 7.3.1062
Bram Moolenaar <bram@vim.org>
parents:
4627
diff
changeset
|
2192 { |
e4e48d4ee040
updated for version 7.3.1062
Bram Moolenaar <bram@vim.org>
parents:
4627
diff
changeset
|
2193 if (!(li = listitem_alloc())) |
e4e48d4ee040
updated for version 7.3.1062
Bram Moolenaar <bram@vim.org>
parents:
4627
diff
changeset
|
2194 { |
e4e48d4ee040
updated for version 7.3.1062
Bram Moolenaar <bram@vim.org>
parents:
4627
diff
changeset
|
2195 PyErr_NoMemory(); |
e4e48d4ee040
updated for version 7.3.1062
Bram Moolenaar <bram@vim.org>
parents:
4627
diff
changeset
|
2196 Py_DECREF(item); |
e4e48d4ee040
updated for version 7.3.1062
Bram Moolenaar <bram@vim.org>
parents:
4627
diff
changeset
|
2197 Py_DECREF(iterator); |
e4e48d4ee040
updated for version 7.3.1062
Bram Moolenaar <bram@vim.org>
parents:
4627
diff
changeset
|
2198 return -1; |
e4e48d4ee040
updated for version 7.3.1062
Bram Moolenaar <bram@vim.org>
parents:
4627
diff
changeset
|
2199 } |
e4e48d4ee040
updated for version 7.3.1062
Bram Moolenaar <bram@vim.org>
parents:
4627
diff
changeset
|
2200 li->li_tv.v_lock = 0; |
e4e48d4ee040
updated for version 7.3.1062
Bram Moolenaar <bram@vim.org>
parents:
4627
diff
changeset
|
2201 li->li_tv.v_type = VAR_UNKNOWN; |
e4e48d4ee040
updated for version 7.3.1062
Bram Moolenaar <bram@vim.org>
parents:
4627
diff
changeset
|
2202 |
e4e48d4ee040
updated for version 7.3.1062
Bram Moolenaar <bram@vim.org>
parents:
4627
diff
changeset
|
2203 if (_ConvertFromPyObject(item, &li->li_tv, lookup_dict) == -1) |
e4e48d4ee040
updated for version 7.3.1062
Bram Moolenaar <bram@vim.org>
parents:
4627
diff
changeset
|
2204 { |
e4e48d4ee040
updated for version 7.3.1062
Bram Moolenaar <bram@vim.org>
parents:
4627
diff
changeset
|
2205 Py_DECREF(item); |
e4e48d4ee040
updated for version 7.3.1062
Bram Moolenaar <bram@vim.org>
parents:
4627
diff
changeset
|
2206 Py_DECREF(iterator); |
e4e48d4ee040
updated for version 7.3.1062
Bram Moolenaar <bram@vim.org>
parents:
4627
diff
changeset
|
2207 listitem_free(li); |
e4e48d4ee040
updated for version 7.3.1062
Bram Moolenaar <bram@vim.org>
parents:
4627
diff
changeset
|
2208 return -1; |
e4e48d4ee040
updated for version 7.3.1062
Bram Moolenaar <bram@vim.org>
parents:
4627
diff
changeset
|
2209 } |
e4e48d4ee040
updated for version 7.3.1062
Bram Moolenaar <bram@vim.org>
parents:
4627
diff
changeset
|
2210 |
e4e48d4ee040
updated for version 7.3.1062
Bram Moolenaar <bram@vim.org>
parents:
4627
diff
changeset
|
2211 Py_DECREF(item); |
e4e48d4ee040
updated for version 7.3.1062
Bram Moolenaar <bram@vim.org>
parents:
4627
diff
changeset
|
2212 |
e4e48d4ee040
updated for version 7.3.1062
Bram Moolenaar <bram@vim.org>
parents:
4627
diff
changeset
|
2213 list_append(l, li); |
e4e48d4ee040
updated for version 7.3.1062
Bram Moolenaar <bram@vim.org>
parents:
4627
diff
changeset
|
2214 } |
e4e48d4ee040
updated for version 7.3.1062
Bram Moolenaar <bram@vim.org>
parents:
4627
diff
changeset
|
2215 |
e4e48d4ee040
updated for version 7.3.1062
Bram Moolenaar <bram@vim.org>
parents:
4627
diff
changeset
|
2216 Py_DECREF(iterator); |
e4e48d4ee040
updated for version 7.3.1062
Bram Moolenaar <bram@vim.org>
parents:
4627
diff
changeset
|
2217 |
e4e48d4ee040
updated for version 7.3.1062
Bram Moolenaar <bram@vim.org>
parents:
4627
diff
changeset
|
2218 /* Iterator may have finished due to an exception */ |
e4e48d4ee040
updated for version 7.3.1062
Bram Moolenaar <bram@vim.org>
parents:
4627
diff
changeset
|
2219 if (PyErr_Occurred()) |
e4e48d4ee040
updated for version 7.3.1062
Bram Moolenaar <bram@vim.org>
parents:
4627
diff
changeset
|
2220 return -1; |
e4e48d4ee040
updated for version 7.3.1062
Bram Moolenaar <bram@vim.org>
parents:
4627
diff
changeset
|
2221 |
e4e48d4ee040
updated for version 7.3.1062
Bram Moolenaar <bram@vim.org>
parents:
4627
diff
changeset
|
2222 return 0; |
e4e48d4ee040
updated for version 7.3.1062
Bram Moolenaar <bram@vim.org>
parents:
4627
diff
changeset
|
2223 } |
e4e48d4ee040
updated for version 7.3.1062
Bram Moolenaar <bram@vim.org>
parents:
4627
diff
changeset
|
2224 |
e4e48d4ee040
updated for version 7.3.1062
Bram Moolenaar <bram@vim.org>
parents:
4627
diff
changeset
|
2225 static PyObject * |
e4e48d4ee040
updated for version 7.3.1062
Bram Moolenaar <bram@vim.org>
parents:
4627
diff
changeset
|
2226 ListConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs) |
e4e48d4ee040
updated for version 7.3.1062
Bram Moolenaar <bram@vim.org>
parents:
4627
diff
changeset
|
2227 { |
e4e48d4ee040
updated for version 7.3.1062
Bram Moolenaar <bram@vim.org>
parents:
4627
diff
changeset
|
2228 list_T *list; |
e4e48d4ee040
updated for version 7.3.1062
Bram Moolenaar <bram@vim.org>
parents:
4627
diff
changeset
|
2229 PyObject *obj = NULL; |
e4e48d4ee040
updated for version 7.3.1062
Bram Moolenaar <bram@vim.org>
parents:
4627
diff
changeset
|
2230 |
e4e48d4ee040
updated for version 7.3.1062
Bram Moolenaar <bram@vim.org>
parents:
4627
diff
changeset
|
2231 if (kwargs) |
e4e48d4ee040
updated for version 7.3.1062
Bram Moolenaar <bram@vim.org>
parents:
4627
diff
changeset
|
2232 { |
4968
b6e693e1f946
updated for version 7.3.1229
Bram Moolenaar <bram@vim.org>
parents:
4966
diff
changeset
|
2233 PyErr_SET_STRING(PyExc_TypeError, |
4978
f4969f8f66e9
updated for version 7.3.1234
Bram Moolenaar <bram@vim.org>
parents:
4976
diff
changeset
|
2234 N_("list constructor does not accept keyword arguments")); |
4629
e4e48d4ee040
updated for version 7.3.1062
Bram Moolenaar <bram@vim.org>
parents:
4627
diff
changeset
|
2235 return NULL; |
e4e48d4ee040
updated for version 7.3.1062
Bram Moolenaar <bram@vim.org>
parents:
4627
diff
changeset
|
2236 } |
e4e48d4ee040
updated for version 7.3.1062
Bram Moolenaar <bram@vim.org>
parents:
4627
diff
changeset
|
2237 |
e4e48d4ee040
updated for version 7.3.1062
Bram Moolenaar <bram@vim.org>
parents:
4627
diff
changeset
|
2238 if (!PyArg_ParseTuple(args, "|O", &obj)) |
e4e48d4ee040
updated for version 7.3.1062
Bram Moolenaar <bram@vim.org>
parents:
4627
diff
changeset
|
2239 return NULL; |
e4e48d4ee040
updated for version 7.3.1062
Bram Moolenaar <bram@vim.org>
parents:
4627
diff
changeset
|
2240 |
e4e48d4ee040
updated for version 7.3.1062
Bram Moolenaar <bram@vim.org>
parents:
4627
diff
changeset
|
2241 if (!(list = py_list_alloc())) |
e4e48d4ee040
updated for version 7.3.1062
Bram Moolenaar <bram@vim.org>
parents:
4627
diff
changeset
|
2242 return NULL; |
e4e48d4ee040
updated for version 7.3.1062
Bram Moolenaar <bram@vim.org>
parents:
4627
diff
changeset
|
2243 |
e4e48d4ee040
updated for version 7.3.1062
Bram Moolenaar <bram@vim.org>
parents:
4627
diff
changeset
|
2244 if (obj) |
e4e48d4ee040
updated for version 7.3.1062
Bram Moolenaar <bram@vim.org>
parents:
4627
diff
changeset
|
2245 { |
e4e48d4ee040
updated for version 7.3.1062
Bram Moolenaar <bram@vim.org>
parents:
4627
diff
changeset
|
2246 PyObject *lookup_dict; |
e4e48d4ee040
updated for version 7.3.1062
Bram Moolenaar <bram@vim.org>
parents:
4627
diff
changeset
|
2247 |
e4e48d4ee040
updated for version 7.3.1062
Bram Moolenaar <bram@vim.org>
parents:
4627
diff
changeset
|
2248 if (!(lookup_dict = PyDict_New())) |
e4e48d4ee040
updated for version 7.3.1062
Bram Moolenaar <bram@vim.org>
parents:
4627
diff
changeset
|
2249 { |
e4e48d4ee040
updated for version 7.3.1062
Bram Moolenaar <bram@vim.org>
parents:
4627
diff
changeset
|
2250 list_unref(list); |
e4e48d4ee040
updated for version 7.3.1062
Bram Moolenaar <bram@vim.org>
parents:
4627
diff
changeset
|
2251 return NULL; |
e4e48d4ee040
updated for version 7.3.1062
Bram Moolenaar <bram@vim.org>
parents:
4627
diff
changeset
|
2252 } |
e4e48d4ee040
updated for version 7.3.1062
Bram Moolenaar <bram@vim.org>
parents:
4627
diff
changeset
|
2253 |
e4e48d4ee040
updated for version 7.3.1062
Bram Moolenaar <bram@vim.org>
parents:
4627
diff
changeset
|
2254 if (list_py_concat(list, obj, lookup_dict) == -1) |
e4e48d4ee040
updated for version 7.3.1062
Bram Moolenaar <bram@vim.org>
parents:
4627
diff
changeset
|
2255 { |
e4e48d4ee040
updated for version 7.3.1062
Bram Moolenaar <bram@vim.org>
parents:
4627
diff
changeset
|
2256 Py_DECREF(lookup_dict); |
e4e48d4ee040
updated for version 7.3.1062
Bram Moolenaar <bram@vim.org>
parents:
4627
diff
changeset
|
2257 list_unref(list); |
e4e48d4ee040
updated for version 7.3.1062
Bram Moolenaar <bram@vim.org>
parents:
4627
diff
changeset
|
2258 return NULL; |
e4e48d4ee040
updated for version 7.3.1062
Bram Moolenaar <bram@vim.org>
parents:
4627
diff
changeset
|
2259 } |
e4e48d4ee040
updated for version 7.3.1062
Bram Moolenaar <bram@vim.org>
parents:
4627
diff
changeset
|
2260 |
e4e48d4ee040
updated for version 7.3.1062
Bram Moolenaar <bram@vim.org>
parents:
4627
diff
changeset
|
2261 Py_DECREF(lookup_dict); |
e4e48d4ee040
updated for version 7.3.1062
Bram Moolenaar <bram@vim.org>
parents:
4627
diff
changeset
|
2262 } |
e4e48d4ee040
updated for version 7.3.1062
Bram Moolenaar <bram@vim.org>
parents:
4627
diff
changeset
|
2263 |
e4e48d4ee040
updated for version 7.3.1062
Bram Moolenaar <bram@vim.org>
parents:
4627
diff
changeset
|
2264 return ListNew(subtype, list); |
e4e48d4ee040
updated for version 7.3.1062
Bram Moolenaar <bram@vim.org>
parents:
4627
diff
changeset
|
2265 } |
e4e48d4ee040
updated for version 7.3.1062
Bram Moolenaar <bram@vim.org>
parents:
4627
diff
changeset
|
2266 |
4319 | 2267 static void |
4488 | 2268 ListDestructor(ListObject *self) |
4319 | 2269 { |
4488 | 2270 pyll_remove(&self->ref, &lastlist); |
2271 list_unref(self->list); | |
4319 | 2272 |
2273 DESTRUCTOR_FINISH(self); | |
2274 } | |
2275 | |
3618 | 2276 static PyInt |
4488 | 2277 ListLength(ListObject *self) |
3618 | 2278 { |
4488 | 2279 return ((PyInt) (self->list->lv_len)); |
3618 | 2280 } |
2281 | |
2282 static PyObject * | |
5608 | 2283 ListIndex(ListObject *self, Py_ssize_t index) |
3618 | 2284 { |
2285 listitem_T *li; | |
2286 | |
4488 | 2287 if (index >= ListLength(self)) |
3618 | 2288 { |
4978
f4969f8f66e9
updated for version 7.3.1234
Bram Moolenaar <bram@vim.org>
parents:
4976
diff
changeset
|
2289 PyErr_SET_STRING(PyExc_IndexError, N_("list index out of range")); |
3618 | 2290 return NULL; |
2291 } | |
4488 | 2292 li = list_find(self->list, (long) index); |
3618 | 2293 if (li == NULL) |
2294 { | |
4970
f5c822e5a0eb
updated for version 7.3.1230
Bram Moolenaar <bram@vim.org>
parents:
4968
diff
changeset
|
2295 /* No more suitable format specifications in python-2.3 */ |
4978
f4969f8f66e9
updated for version 7.3.1234
Bram Moolenaar <bram@vim.org>
parents:
4976
diff
changeset
|
2296 PyErr_VIM_FORMAT(N_("internal error: failed to get vim list item %d"), |
4970
f5c822e5a0eb
updated for version 7.3.1230
Bram Moolenaar <bram@vim.org>
parents:
4968
diff
changeset
|
2297 (int) index); |
3618 | 2298 return NULL; |
2299 } | |
2300 return ConvertToPyObject(&li->li_tv); | |
2301 } | |
2302 | |
2303 static PyObject * | |
5608 | 2304 ListSlice(ListObject *self, Py_ssize_t first, Py_ssize_t step, |
2305 Py_ssize_t slicelen) | |
3618 | 2306 { |
2307 PyInt i; | |
2308 PyObject *list; | |
5608 | 2309 |
2310 if (step == 0) | |
2311 { | |
2312 PyErr_SET_STRING(PyExc_ValueError, N_("slice step cannot be zero")); | |
2313 return NULL; | |
2314 } | |
2315 | |
2316 list = PyList_New(slicelen); | |
3618 | 2317 if (list == NULL) |
2318 return NULL; | |
2319 | |
5608 | 2320 for (i = 0; i < slicelen; ++i) |
2321 { | |
2322 PyObject *item; | |
2323 | |
2324 item = ListIndex(self, first + i*step); | |
3618 | 2325 if (item == NULL) |
2326 { | |
2327 Py_DECREF(list); | |
2328 return NULL; | |
2329 } | |
2330 | |
5608 | 2331 PyList_SET_ITEM(list, i, item); |
3618 | 2332 } |
2333 | |
2334 return list; | |
2335 } | |
2336 | |
5608 | 2337 static PyObject * |
2338 ListItem(ListObject *self, PyObject* idx) | |
2339 { | |
2340 #if PY_MAJOR_VERSION < 3 | |
2341 if (PyInt_Check(idx)) | |
2342 { | |
2343 long _idx = PyInt_AsLong(idx); | |
2344 return ListIndex(self, _idx); | |
2345 } | |
2346 else | |
2347 #endif | |
2348 if (PyLong_Check(idx)) | |
2349 { | |
2350 long _idx = PyLong_AsLong(idx); | |
2351 return ListIndex(self, _idx); | |
2352 } | |
2353 else if (PySlice_Check(idx)) | |
2354 { | |
2355 Py_ssize_t start, stop, step, slicelen; | |
2356 | |
5768 | 2357 if (PySlice_GetIndicesEx((PySliceObject_T *)idx, ListLength(self), |
5608 | 2358 &start, &stop, &step, &slicelen) < 0) |
2359 return NULL; | |
2360 return ListSlice(self, start, step, slicelen); | |
2361 } | |
2362 else | |
2363 { | |
2364 RAISE_INVALID_INDEX_TYPE(idx); | |
2365 return NULL; | |
2366 } | |
2367 } | |
2368 | |
2369 static void | |
2370 list_restore(Py_ssize_t numadded, Py_ssize_t numreplaced, Py_ssize_t slicelen, | |
2371 list_T *l, listitem_T **lis, listitem_T *lastaddedli) | |
2372 { | |
2373 while (numreplaced--) | |
2374 { | |
2375 list_insert(l, lis[numreplaced], lis[slicelen + numreplaced]); | |
2376 listitem_remove(l, lis[slicelen + numreplaced]); | |
2377 } | |
2378 while (numadded--) | |
2379 { | |
2380 listitem_T *next; | |
2381 | |
2382 next = lastaddedli->li_prev; | |
2383 listitem_remove(l, lastaddedli); | |
2384 lastaddedli = next; | |
2385 } | |
2386 } | |
2387 | |
2388 static int | |
2389 ListAssSlice(ListObject *self, Py_ssize_t first, | |
2390 Py_ssize_t step, Py_ssize_t slicelen, PyObject *obj) | |
2391 { | |
2392 PyObject *iterator; | |
2393 PyObject *item; | |
2394 listitem_T *li; | |
2395 listitem_T *lastaddedli = NULL; | |
2396 listitem_T *next; | |
2397 typval_T v; | |
2398 list_T *l = self->list; | |
2399 PyInt i; | |
2400 PyInt j; | |
2401 PyInt numreplaced = 0; | |
2402 PyInt numadded = 0; | |
2403 PyInt size; | |
5655 | 2404 listitem_T **lis = NULL; |
5608 | 2405 |
2406 size = ListLength(self); | |
2407 | |
2408 if (l->lv_lock) | |
2409 { | |
2410 RAISE_LOCKED_LIST; | |
2411 return -1; | |
2412 } | |
2413 | |
2414 if (step == 0) | |
2415 { | |
2416 PyErr_SET_STRING(PyExc_ValueError, N_("slice step cannot be zero")); | |
2417 return -1; | |
2418 } | |
2419 | |
2420 if (step != 1 && slicelen == 0) | |
2421 { | |
2422 /* Nothing to do. Only error out if obj has some items. */ | |
2423 int ret = 0; | |
2424 | |
2425 if (obj == NULL) | |
2426 return 0; | |
2427 | |
2428 if (!(iterator = PyObject_GetIter(obj))) | |
2429 return -1; | |
2430 | |
2431 if ((item = PyIter_Next(iterator))) | |
2432 { | |
2433 PyErr_FORMAT(PyExc_ValueError, | |
5695 | 2434 N_("attempt to assign sequence of size greater than %d " |
5608 | 2435 "to extended slice"), 0); |
2436 Py_DECREF(item); | |
2437 ret = -1; | |
2438 } | |
2439 Py_DECREF(iterator); | |
2440 return ret; | |
2441 } | |
2442 | |
2443 if (obj != NULL) | |
2444 /* XXX May allocate zero bytes. */ | |
2445 if (!(lis = PyMem_New(listitem_T *, slicelen * 2))) | |
2446 { | |
2447 PyErr_NoMemory(); | |
2448 return -1; | |
2449 } | |
2450 | |
2451 if (first == size) | |
2452 li = NULL; | |
2453 else | |
2454 { | |
2455 li = list_find(l, (long) first); | |
2456 if (li == NULL) | |
2457 { | |
2458 PyErr_VIM_FORMAT(N_("internal error: no vim list item %d"), | |
2459 (int)first); | |
2460 if (obj != NULL) | |
2461 PyMem_Free(lis); | |
2462 return -1; | |
2463 } | |
2464 i = slicelen; | |
2465 while (i-- && li != NULL) | |
2466 { | |
2467 j = step; | |
2468 next = li; | |
2469 if (step > 0) | |
2470 while (next != NULL && ((next = next->li_next) != NULL) && --j); | |
2471 else | |
2472 while (next != NULL && ((next = next->li_prev) != NULL) && ++j); | |
2473 | |
2474 if (obj == NULL) | |
2475 listitem_remove(l, li); | |
2476 else | |
2477 lis[slicelen - i - 1] = li; | |
2478 | |
2479 li = next; | |
2480 } | |
2481 if (li == NULL && i != -1) | |
2482 { | |
2483 PyErr_SET_VIM(N_("internal error: not enough list items")); | |
2484 if (obj != NULL) | |
2485 PyMem_Free(lis); | |
2486 return -1; | |
2487 } | |
2488 } | |
2489 | |
2490 if (obj == NULL) | |
2491 return 0; | |
2492 | |
2493 if (!(iterator = PyObject_GetIter(obj))) | |
2494 { | |
2495 PyMem_Free(lis); | |
2496 return -1; | |
2497 } | |
2498 | |
2499 i = 0; | |
2500 while ((item = PyIter_Next(iterator))) | |
2501 { | |
2502 if (ConvertFromPyObject(item, &v) == -1) | |
2503 { | |
2504 Py_DECREF(iterator); | |
2505 Py_DECREF(item); | |
2506 PyMem_Free(lis); | |
2507 return -1; | |
2508 } | |
2509 Py_DECREF(item); | |
2510 if (list_insert_tv(l, &v, numreplaced < slicelen | |
2511 ? lis[numreplaced] | |
2512 : li) == FAIL) | |
2513 { | |
2514 clear_tv(&v); | |
2515 PyErr_SET_VIM(N_("internal error: failed to add item to list")); | |
2516 list_restore(numadded, numreplaced, slicelen, l, lis, lastaddedli); | |
2517 PyMem_Free(lis); | |
2518 return -1; | |
2519 } | |
2520 if (numreplaced < slicelen) | |
2521 { | |
2522 lis[slicelen + numreplaced] = lis[numreplaced]->li_prev; | |
5871 | 2523 vimlist_remove(l, lis[numreplaced], lis[numreplaced]); |
5608 | 2524 numreplaced++; |
2525 } | |
2526 else | |
2527 { | |
2528 if (li) | |
2529 lastaddedli = li->li_prev; | |
2530 else | |
2531 lastaddedli = l->lv_last; | |
2532 numadded++; | |
2533 } | |
2534 clear_tv(&v); | |
2535 if (step != 1 && i >= slicelen) | |
2536 { | |
2537 Py_DECREF(iterator); | |
2538 PyErr_FORMAT(PyExc_ValueError, | |
5695 | 2539 N_("attempt to assign sequence of size greater than %d " |
5668 | 2540 "to extended slice"), (int) slicelen); |
5608 | 2541 list_restore(numadded, numreplaced, slicelen, l, lis, lastaddedli); |
2542 PyMem_Free(lis); | |
2543 return -1; | |
2544 } | |
2545 ++i; | |
2546 } | |
2547 Py_DECREF(iterator); | |
2548 | |
2549 if (step != 1 && i != slicelen) | |
2550 { | |
2551 PyErr_FORMAT2(PyExc_ValueError, | |
5668 | 2552 N_("attempt to assign sequence of size %d to extended slice " |
2553 "of size %d"), (int) i, (int) slicelen); | |
5608 | 2554 list_restore(numadded, numreplaced, slicelen, l, lis, lastaddedli); |
2555 PyMem_Free(lis); | |
2556 return -1; | |
2557 } | |
2558 | |
2559 if (PyErr_Occurred()) | |
2560 { | |
2561 list_restore(numadded, numreplaced, slicelen, l, lis, lastaddedli); | |
2562 PyMem_Free(lis); | |
2563 return -1; | |
2564 } | |
2565 | |
2566 for (i = 0; i < numreplaced; i++) | |
2567 listitem_free(lis[i]); | |
2568 if (step == 1) | |
2569 for (i = numreplaced; i < slicelen; i++) | |
2570 listitem_remove(l, lis[i]); | |
2571 | |
2572 PyMem_Free(lis); | |
2573 | |
2574 return 0; | |
2575 } | |
2576 | |
2577 static int | |
2578 ListAssIndex(ListObject *self, Py_ssize_t index, PyObject *obj) | |
2579 { | |
2580 typval_T tv; | |
2581 list_T *l = self->list; | |
2582 listitem_T *li; | |
2583 Py_ssize_t length = ListLength(self); | |
2584 | |
2585 if (l->lv_lock) | |
2586 { | |
2587 RAISE_LOCKED_LIST; | |
2588 return -1; | |
2589 } | |
2590 if (index > length || (index == length && obj == NULL)) | |
2591 { | |
2592 PyErr_SET_STRING(PyExc_IndexError, N_("list index out of range")); | |
2593 return -1; | |
2594 } | |
2595 | |
2596 if (obj == NULL) | |
2597 { | |
2598 li = list_find(l, (long) index); | |
5871 | 2599 vimlist_remove(l, li, li); |
5608 | 2600 clear_tv(&li->li_tv); |
2601 vim_free(li); | |
2602 return 0; | |
2603 } | |
2604 | |
2605 if (ConvertFromPyObject(obj, &tv) == -1) | |
2606 return -1; | |
2607 | |
2608 if (index == length) | |
2609 { | |
2610 if (list_append_tv(l, &tv) == FAIL) | |
2611 { | |
2612 clear_tv(&tv); | |
2613 PyErr_SET_VIM(N_("failed to add item to list")); | |
2614 return -1; | |
2615 } | |
2616 } | |
2617 else | |
2618 { | |
2619 li = list_find(l, (long) index); | |
2620 clear_tv(&li->li_tv); | |
2621 copy_tv(&tv, &li->li_tv); | |
2622 clear_tv(&tv); | |
2623 } | |
2624 return 0; | |
2625 } | |
2626 | |
2627 static Py_ssize_t | |
2628 ListAssItem(ListObject *self, PyObject *idx, PyObject *obj) | |
2629 { | |
2630 #if PY_MAJOR_VERSION < 3 | |
2631 if (PyInt_Check(idx)) | |
2632 { | |
2633 long _idx = PyInt_AsLong(idx); | |
2634 return ListAssIndex(self, _idx, obj); | |
2635 } | |
2636 else | |
2637 #endif | |
2638 if (PyLong_Check(idx)) | |
2639 { | |
2640 long _idx = PyLong_AsLong(idx); | |
2641 return ListAssIndex(self, _idx, obj); | |
2642 } | |
2643 else if (PySlice_Check(idx)) | |
2644 { | |
2645 Py_ssize_t start, stop, step, slicelen; | |
2646 | |
5768 | 2647 if (PySlice_GetIndicesEx((PySliceObject_T *)idx, ListLength(self), |
5608 | 2648 &start, &stop, &step, &slicelen) < 0) |
2649 return -1; | |
2650 return ListAssSlice(self, start, step, slicelen, | |
2651 obj); | |
2652 } | |
2653 else | |
2654 { | |
2655 RAISE_INVALID_INDEX_TYPE(idx); | |
2656 return -1; | |
2657 } | |
2658 } | |
2659 | |
2660 static PyObject * | |
2661 ListConcatInPlace(ListObject *self, PyObject *obj) | |
2662 { | |
2663 list_T *l = self->list; | |
2664 PyObject *lookup_dict; | |
2665 | |
2666 if (l->lv_lock) | |
2667 { | |
2668 RAISE_LOCKED_LIST; | |
2669 return NULL; | |
2670 } | |
2671 | |
2672 if (!(lookup_dict = PyDict_New())) | |
2673 return NULL; | |
2674 | |
2675 if (list_py_concat(l, obj, lookup_dict) == -1) | |
2676 { | |
2677 Py_DECREF(lookup_dict); | |
2678 return NULL; | |
2679 } | |
2680 Py_DECREF(lookup_dict); | |
2681 | |
2682 Py_INCREF(self); | |
2683 return (PyObject *)(self); | |
2684 } | |
2685 | |
4397 | 2686 typedef struct |
2687 { | |
2688 listwatch_T lw; | |
2689 list_T *list; | |
2690 } listiterinfo_T; | |
2691 | |
2692 static void | |
2693 ListIterDestruct(listiterinfo_T *lii) | |
2694 { | |
2695 list_rem_watch(lii->list, &lii->lw); | |
2696 PyMem_Free(lii); | |
2697 } | |
2698 | |
2699 static PyObject * | |
2700 ListIterNext(listiterinfo_T **lii) | |
2701 { | |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
2702 PyObject *ret; |
4397 | 2703 |
2704 if (!((*lii)->lw.lw_item)) | |
2705 return NULL; | |
2706 | |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
2707 if (!(ret = ConvertToPyObject(&((*lii)->lw.lw_item->li_tv)))) |
4397 | 2708 return NULL; |
2709 | |
2710 (*lii)->lw.lw_item = (*lii)->lw.lw_item->li_next; | |
2711 | |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
2712 return ret; |
4397 | 2713 } |
2714 | |
2715 static PyObject * | |
4488 | 2716 ListIter(ListObject *self) |
4397 | 2717 { |
2718 listiterinfo_T *lii; | |
4488 | 2719 list_T *l = self->list; |
4397 | 2720 |
2721 if (!(lii = PyMem_New(listiterinfo_T, 1))) | |
2722 { | |
2723 PyErr_NoMemory(); | |
2724 return NULL; | |
2725 } | |
2726 | |
2727 list_add_watch(l, &lii->lw); | |
2728 lii->lw.lw_item = l->lv_first; | |
2729 lii->list = l; | |
2730 | |
2731 return IterNew(lii, | |
4433 | 2732 (destructorfun) ListIterDestruct, (nextfun) ListIterNext, |
2733 NULL, NULL); | |
4397 | 2734 } |
2735 | |
4599
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
2736 static char *ListAttrs[] = { |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
2737 "locked", |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
2738 NULL |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
2739 }; |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
2740 |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
2741 static PyObject * |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
2742 ListDir(PyObject *self) |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
2743 { |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
2744 return ObjectDir(self, ListAttrs); |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
2745 } |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
2746 |
3828 | 2747 static int |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
2748 ListSetattr(ListObject *self, char *name, PyObject *valObject) |
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
2749 { |
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
2750 if (valObject == NULL) |
3828 | 2751 { |
4968
b6e693e1f946
updated for version 7.3.1229
Bram Moolenaar <bram@vim.org>
parents:
4966
diff
changeset
|
2752 PyErr_SET_STRING(PyExc_AttributeError, |
4978
f4969f8f66e9
updated for version 7.3.1234
Bram Moolenaar <bram@vim.org>
parents:
4976
diff
changeset
|
2753 N_("cannot delete vim.List attributes")); |
3828 | 2754 return -1; |
2755 } | |
2756 | |
2757 if (strcmp(name, "locked") == 0) | |
2758 { | |
4488 | 2759 if (self->list->lv_lock == VAR_FIXED) |
3828 | 2760 { |
4978
f4969f8f66e9
updated for version 7.3.1234
Bram Moolenaar <bram@vim.org>
parents:
4976
diff
changeset
|
2761 PyErr_SET_STRING(PyExc_TypeError, N_("cannot modify fixed list")); |
3828 | 2762 return -1; |
2763 } | |
2764 else | |
2765 { | |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
2766 int istrue = PyObject_IsTrue(valObject); |
4411 | 2767 if (istrue == -1) |
2768 return -1; | |
2769 else if (istrue) | |
4488 | 2770 self->list->lv_lock = VAR_LOCKED; |
3828 | 2771 else |
4488 | 2772 self->list->lv_lock = 0; |
3828 | 2773 } |
2774 return 0; | |
2775 } | |
2776 else | |
2777 { | |
4978
f4969f8f66e9
updated for version 7.3.1234
Bram Moolenaar <bram@vim.org>
parents:
4976
diff
changeset
|
2778 PyErr_FORMAT(PyExc_AttributeError, N_("cannot set attribute %s"), name); |
3828 | 2779 return -1; |
2780 } | |
2781 } | |
2782 | |
5608 | 2783 static PySequenceMethods ListAsSeq = { |
2784 (lenfunc) ListLength, /* sq_length, len(x) */ | |
2785 (binaryfunc) 0, /* RangeConcat, sq_concat, x+y */ | |
2786 0, /* RangeRepeat, sq_repeat, x*n */ | |
2787 (PyIntArgFunc) ListIndex, /* sq_item, x[i] */ | |
2788 0, /* was_sq_slice, x[i:j] */ | |
2789 (PyIntObjArgProc) ListAssIndex, /* sq_as_item, x[i]=v */ | |
2790 0, /* was_sq_ass_slice, x[i:j]=v */ | |
2791 0, /* sq_contains */ | |
2792 (binaryfunc) ListConcatInPlace,/* sq_inplace_concat */ | |
2793 0, /* sq_inplace_repeat */ | |
2794 }; | |
2795 | |
2796 static PyMappingMethods ListAsMapping = { | |
2797 /* mp_length */ (lenfunc) ListLength, | |
2798 /* mp_subscript */ (binaryfunc) ListItem, | |
2799 /* mp_ass_subscript */ (objobjargproc) ListAssItem, | |
2800 }; | |
2801 | |
3618 | 2802 static struct PyMethodDef ListMethods[] = { |
4599
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
2803 {"extend", (PyCFunction)ListConcatInPlace, METH_O, ""}, |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
2804 {"__dir__", (PyCFunction)ListDir, METH_NOARGS, ""}, |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
2805 { NULL, NULL, 0, NULL} |
3618 | 2806 }; |
2807 | |
2808 typedef struct | |
2809 { | |
2810 PyObject_HEAD | |
2811 char_u *name; | |
2812 } FunctionObject; | |
2813 | |
2814 static PyTypeObject FunctionType; | |
2815 | |
4631
4157fef7b950
updated for version 7.3.1063
Bram Moolenaar <bram@vim.org>
parents:
4629
diff
changeset
|
2816 #define NEW_FUNCTION(name) FunctionNew(&FunctionType, name) |
4157fef7b950
updated for version 7.3.1063
Bram Moolenaar <bram@vim.org>
parents:
4629
diff
changeset
|
2817 |
3618 | 2818 static PyObject * |
4631
4157fef7b950
updated for version 7.3.1063
Bram Moolenaar <bram@vim.org>
parents:
4629
diff
changeset
|
2819 FunctionNew(PyTypeObject *subtype, char_u *name) |
3618 | 2820 { |
2821 FunctionObject *self; | |
2822 | |
4631
4157fef7b950
updated for version 7.3.1063
Bram Moolenaar <bram@vim.org>
parents:
4629
diff
changeset
|
2823 self = (FunctionObject *) subtype->tp_alloc(subtype, 0); |
4157fef7b950
updated for version 7.3.1063
Bram Moolenaar <bram@vim.org>
parents:
4629
diff
changeset
|
2824 |
3618 | 2825 if (self == NULL) |
2826 return NULL; | |
4631
4157fef7b950
updated for version 7.3.1063
Bram Moolenaar <bram@vim.org>
parents:
4629
diff
changeset
|
2827 |
4157fef7b950
updated for version 7.3.1063
Bram Moolenaar <bram@vim.org>
parents:
4629
diff
changeset
|
2828 if (isdigit(*name)) |
4157fef7b950
updated for version 7.3.1063
Bram Moolenaar <bram@vim.org>
parents:
4629
diff
changeset
|
2829 { |
4157fef7b950
updated for version 7.3.1063
Bram Moolenaar <bram@vim.org>
parents:
4629
diff
changeset
|
2830 if (!translated_function_exists(name)) |
4157fef7b950
updated for version 7.3.1063
Bram Moolenaar <bram@vim.org>
parents:
4629
diff
changeset
|
2831 { |
4970
f5c822e5a0eb
updated for version 7.3.1230
Bram Moolenaar <bram@vim.org>
parents:
4968
diff
changeset
|
2832 PyErr_FORMAT(PyExc_ValueError, |
4978
f4969f8f66e9
updated for version 7.3.1234
Bram Moolenaar <bram@vim.org>
parents:
4976
diff
changeset
|
2833 N_("unnamed function %s does not exist"), name); |
4631
4157fef7b950
updated for version 7.3.1063
Bram Moolenaar <bram@vim.org>
parents:
4629
diff
changeset
|
2834 return NULL; |
4157fef7b950
updated for version 7.3.1063
Bram Moolenaar <bram@vim.org>
parents:
4629
diff
changeset
|
2835 } |
4157fef7b950
updated for version 7.3.1063
Bram Moolenaar <bram@vim.org>
parents:
4629
diff
changeset
|
2836 self->name = vim_strsave(name); |
4157fef7b950
updated for version 7.3.1063
Bram Moolenaar <bram@vim.org>
parents:
4629
diff
changeset
|
2837 func_ref(self->name); |
4157fef7b950
updated for version 7.3.1063
Bram Moolenaar <bram@vim.org>
parents:
4629
diff
changeset
|
2838 } |
4157fef7b950
updated for version 7.3.1063
Bram Moolenaar <bram@vim.org>
parents:
4629
diff
changeset
|
2839 else |
4641
59e6c2bd68e3
updated for version 7.3.1068
Bram Moolenaar <bram@vim.org>
parents:
4635
diff
changeset
|
2840 if ((self->name = get_expanded_name(name, |
59e6c2bd68e3
updated for version 7.3.1068
Bram Moolenaar <bram@vim.org>
parents:
4635
diff
changeset
|
2841 vim_strchr(name, AUTOLOAD_CHAR) == NULL)) |
59e6c2bd68e3
updated for version 7.3.1068
Bram Moolenaar <bram@vim.org>
parents:
4635
diff
changeset
|
2842 == NULL) |
4631
4157fef7b950
updated for version 7.3.1063
Bram Moolenaar <bram@vim.org>
parents:
4629
diff
changeset
|
2843 { |
4978
f4969f8f66e9
updated for version 7.3.1234
Bram Moolenaar <bram@vim.org>
parents:
4976
diff
changeset
|
2844 PyErr_FORMAT(PyExc_ValueError, |
f4969f8f66e9
updated for version 7.3.1234
Bram Moolenaar <bram@vim.org>
parents:
4976
diff
changeset
|
2845 N_("function %s does not exist"), name); |
4641
59e6c2bd68e3
updated for version 7.3.1068
Bram Moolenaar <bram@vim.org>
parents:
4635
diff
changeset
|
2846 return NULL; |
4631
4157fef7b950
updated for version 7.3.1063
Bram Moolenaar <bram@vim.org>
parents:
4629
diff
changeset
|
2847 } |
4157fef7b950
updated for version 7.3.1063
Bram Moolenaar <bram@vim.org>
parents:
4629
diff
changeset
|
2848 |
4157fef7b950
updated for version 7.3.1063
Bram Moolenaar <bram@vim.org>
parents:
4629
diff
changeset
|
2849 return (PyObject *)(self); |
4157fef7b950
updated for version 7.3.1063
Bram Moolenaar <bram@vim.org>
parents:
4629
diff
changeset
|
2850 } |
4157fef7b950
updated for version 7.3.1063
Bram Moolenaar <bram@vim.org>
parents:
4629
diff
changeset
|
2851 |
4157fef7b950
updated for version 7.3.1063
Bram Moolenaar <bram@vim.org>
parents:
4629
diff
changeset
|
2852 static PyObject * |
4157fef7b950
updated for version 7.3.1063
Bram Moolenaar <bram@vim.org>
parents:
4629
diff
changeset
|
2853 FunctionConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs) |
4157fef7b950
updated for version 7.3.1063
Bram Moolenaar <bram@vim.org>
parents:
4629
diff
changeset
|
2854 { |
4157fef7b950
updated for version 7.3.1063
Bram Moolenaar <bram@vim.org>
parents:
4629
diff
changeset
|
2855 PyObject *self; |
4157fef7b950
updated for version 7.3.1063
Bram Moolenaar <bram@vim.org>
parents:
4629
diff
changeset
|
2856 char_u *name; |
4157fef7b950
updated for version 7.3.1063
Bram Moolenaar <bram@vim.org>
parents:
4629
diff
changeset
|
2857 |
4157fef7b950
updated for version 7.3.1063
Bram Moolenaar <bram@vim.org>
parents:
4629
diff
changeset
|
2858 if (kwargs) |
4157fef7b950
updated for version 7.3.1063
Bram Moolenaar <bram@vim.org>
parents:
4629
diff
changeset
|
2859 { |
4968
b6e693e1f946
updated for version 7.3.1229
Bram Moolenaar <bram@vim.org>
parents:
4966
diff
changeset
|
2860 PyErr_SET_STRING(PyExc_TypeError, |
4978
f4969f8f66e9
updated for version 7.3.1234
Bram Moolenaar <bram@vim.org>
parents:
4976
diff
changeset
|
2861 N_("function constructor does not accept keyword arguments")); |
3618 | 2862 return NULL; |
2863 } | |
4631
4157fef7b950
updated for version 7.3.1063
Bram Moolenaar <bram@vim.org>
parents:
4629
diff
changeset
|
2864 |
4964
5cee875f3096
updated for version 7.3.1227
Bram Moolenaar <bram@vim.org>
parents:
4962
diff
changeset
|
2865 if (!PyArg_ParseTuple(args, "et", "ascii", &name)) |
4631
4157fef7b950
updated for version 7.3.1063
Bram Moolenaar <bram@vim.org>
parents:
4629
diff
changeset
|
2866 return NULL; |
4157fef7b950
updated for version 7.3.1063
Bram Moolenaar <bram@vim.org>
parents:
4629
diff
changeset
|
2867 |
4157fef7b950
updated for version 7.3.1063
Bram Moolenaar <bram@vim.org>
parents:
4629
diff
changeset
|
2868 self = FunctionNew(subtype, name); |
4157fef7b950
updated for version 7.3.1063
Bram Moolenaar <bram@vim.org>
parents:
4629
diff
changeset
|
2869 |
4964
5cee875f3096
updated for version 7.3.1227
Bram Moolenaar <bram@vim.org>
parents:
4962
diff
changeset
|
2870 PyMem_Free(name); |
5cee875f3096
updated for version 7.3.1227
Bram Moolenaar <bram@vim.org>
parents:
4962
diff
changeset
|
2871 |
4631
4157fef7b950
updated for version 7.3.1063
Bram Moolenaar <bram@vim.org>
parents:
4629
diff
changeset
|
2872 return self; |
3618 | 2873 } |
2874 | |
4319 | 2875 static void |
4488 | 2876 FunctionDestructor(FunctionObject *self) |
4319 | 2877 { |
4488 | 2878 func_unref(self->name); |
4631
4157fef7b950
updated for version 7.3.1063
Bram Moolenaar <bram@vim.org>
parents:
4629
diff
changeset
|
2879 vim_free(self->name); |
4319 | 2880 |
2881 DESTRUCTOR_FINISH(self); | |
2882 } | |
2883 | |
4599
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
2884 static char *FunctionAttrs[] = { |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
2885 "softspace", |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
2886 NULL |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
2887 }; |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
2888 |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
2889 static PyObject * |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
2890 FunctionDir(PyObject *self) |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
2891 { |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
2892 return ObjectDir(self, FunctionAttrs); |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
2893 } |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
2894 |
3618 | 2895 static PyObject * |
4488 | 2896 FunctionCall(FunctionObject *self, PyObject *argsObject, PyObject *kwargs) |
3618 | 2897 { |
4488 | 2898 char_u *name = self->name; |
3618 | 2899 typval_T args; |
2900 typval_T selfdicttv; | |
2901 typval_T rettv; | |
2902 dict_T *selfdict = NULL; | |
2903 PyObject *selfdictObject; | |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
2904 PyObject *ret; |
3618 | 2905 int error; |
2906 | |
2907 if (ConvertFromPyObject(argsObject, &args) == -1) | |
2908 return NULL; | |
2909 | |
2910 if (kwargs != NULL) | |
2911 { | |
2912 selfdictObject = PyDict_GetItemString(kwargs, "self"); | |
2913 if (selfdictObject != NULL) | |
2914 { | |
4627
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
2915 if (ConvertFromPyMapping(selfdictObject, &selfdicttv) == -1) |
4509
b498224f5b41
updated for version 7.3.1002
Bram Moolenaar <bram@vim.org>
parents:
4500
diff
changeset
|
2916 { |
b498224f5b41
updated for version 7.3.1002
Bram Moolenaar <bram@vim.org>
parents:
4500
diff
changeset
|
2917 clear_tv(&args); |
3618 | 2918 return NULL; |
4509
b498224f5b41
updated for version 7.3.1002
Bram Moolenaar <bram@vim.org>
parents:
4500
diff
changeset
|
2919 } |
3618 | 2920 selfdict = selfdicttv.vval.v_dict; |
2921 } | |
2922 } | |
2923 | |
4415 | 2924 Py_BEGIN_ALLOW_THREADS |
2925 Python_Lock_Vim(); | |
2926 | |
4498 | 2927 VimTryStart(); |
3618 | 2928 error = func_call(name, &args, selfdict, &rettv); |
4415 | 2929 |
2930 Python_Release_Vim(); | |
2931 Py_END_ALLOW_THREADS | |
2932 | |
4498 | 2933 if (VimTryEnd()) |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
2934 ret = NULL; |
4498 | 2935 else if (error != OK) |
3618 | 2936 { |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
2937 ret = NULL; |
4978
f4969f8f66e9
updated for version 7.3.1234
Bram Moolenaar <bram@vim.org>
parents:
4976
diff
changeset
|
2938 PyErr_VIM_FORMAT(N_("failed to run function %s"), (char *)name); |
3618 | 2939 } |
2940 else | |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
2941 ret = ConvertToPyObject(&rettv); |
3618 | 2942 |
2943 clear_tv(&args); | |
2944 clear_tv(&rettv); | |
4509
b498224f5b41
updated for version 7.3.1002
Bram Moolenaar <bram@vim.org>
parents:
4500
diff
changeset
|
2945 if (selfdict != NULL) |
b498224f5b41
updated for version 7.3.1002
Bram Moolenaar <bram@vim.org>
parents:
4500
diff
changeset
|
2946 clear_tv(&selfdicttv); |
3618 | 2947 |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
2948 return ret; |
3618 | 2949 } |
2950 | |
4625
cb5c1e37ad4d
updated for version 7.3.1060
Bram Moolenaar <bram@vim.org>
parents:
4623
diff
changeset
|
2951 static PyObject * |
cb5c1e37ad4d
updated for version 7.3.1060
Bram Moolenaar <bram@vim.org>
parents:
4623
diff
changeset
|
2952 FunctionRepr(FunctionObject *self) |
cb5c1e37ad4d
updated for version 7.3.1060
Bram Moolenaar <bram@vim.org>
parents:
4623
diff
changeset
|
2953 { |
4976
4ed713442c51
updated for version 7.3.1233
Bram Moolenaar <bram@vim.org>
parents:
4974
diff
changeset
|
2954 #ifdef Py_TRACE_REFS |
5088
34c629c3b4ba
updated for version 7.3.1287
Bram Moolenaar <bram@vim.org>
parents:
4995
diff
changeset
|
2955 /* For unknown reason self->name may be NULL after calling |
4976
4ed713442c51
updated for version 7.3.1233
Bram Moolenaar <bram@vim.org>
parents:
4974
diff
changeset
|
2956 * Finalize */ |
4ed713442c51
updated for version 7.3.1233
Bram Moolenaar <bram@vim.org>
parents:
4974
diff
changeset
|
2957 return PyString_FromFormat("<vim.Function '%s'>", |
5088
34c629c3b4ba
updated for version 7.3.1287
Bram Moolenaar <bram@vim.org>
parents:
4995
diff
changeset
|
2958 (self->name == NULL ? "<NULL>" : (char *)self->name)); |
4976
4ed713442c51
updated for version 7.3.1233
Bram Moolenaar <bram@vim.org>
parents:
4974
diff
changeset
|
2959 #else |
5088
34c629c3b4ba
updated for version 7.3.1287
Bram Moolenaar <bram@vim.org>
parents:
4995
diff
changeset
|
2960 return PyString_FromFormat("<vim.Function '%s'>", (char *)self->name); |
4976
4ed713442c51
updated for version 7.3.1233
Bram Moolenaar <bram@vim.org>
parents:
4974
diff
changeset
|
2961 #endif |
4625
cb5c1e37ad4d
updated for version 7.3.1060
Bram Moolenaar <bram@vim.org>
parents:
4623
diff
changeset
|
2962 } |
cb5c1e37ad4d
updated for version 7.3.1060
Bram Moolenaar <bram@vim.org>
parents:
4623
diff
changeset
|
2963 |
3618 | 2964 static struct PyMethodDef FunctionMethods[] = { |
4599
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
2965 {"__dir__", (PyCFunction)FunctionDir, METH_NOARGS, ""}, |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
2966 { NULL, NULL, 0, NULL} |
3618 | 2967 }; |
2968 | |
4350 | 2969 /* |
2970 * Options object | |
2971 */ | |
2972 | |
2973 static PyTypeObject OptionsType; | |
2974 | |
2975 typedef int (*checkfun)(void *); | |
2976 | |
2977 typedef struct | |
2978 { | |
2979 PyObject_HEAD | |
5610 | 2980 int opt_type; |
2981 void *from; | |
2982 checkfun Check; | |
2983 PyObject *fromObj; | |
4350 | 2984 } OptionsObject; |
2985 | |
4433 | 2986 static int |
2987 dummy_check(void *arg UNUSED) | |
2988 { | |
2989 return 0; | |
2990 } | |
2991 | |
2992 static PyObject * | |
2993 OptionsNew(int opt_type, void *from, checkfun Check, PyObject *fromObj) | |
2994 { | |
2995 OptionsObject *self; | |
2996 | |
4500 | 2997 self = PyObject_GC_New(OptionsObject, &OptionsType); |
4433 | 2998 if (self == NULL) |
2999 return NULL; | |
3000 | |
3001 self->opt_type = opt_type; | |
3002 self->from = from; | |
3003 self->Check = Check; | |
3004 self->fromObj = fromObj; | |
3005 if (fromObj) | |
3006 Py_INCREF(fromObj); | |
3007 | |
3008 return (PyObject *)(self); | |
3009 } | |
3010 | |
3011 static void | |
4488 | 3012 OptionsDestructor(OptionsObject *self) |
4433 | 3013 { |
4500 | 3014 PyObject_GC_UnTrack((void *)(self)); |
3015 Py_XDECREF(self->fromObj); | |
3016 PyObject_GC_Del((void *)(self)); | |
4433 | 3017 } |
3018 | |
3019 static int | |
4488 | 3020 OptionsTraverse(OptionsObject *self, visitproc visit, void *arg) |
4433 | 3021 { |
4488 | 3022 Py_VISIT(self->fromObj); |
4433 | 3023 return 0; |
3024 } | |
3025 | |
3026 static int | |
4488 | 3027 OptionsClear(OptionsObject *self) |
4433 | 3028 { |
4488 | 3029 Py_CLEAR(self->fromObj); |
4433 | 3030 return 0; |
3031 } | |
3032 | |
4350 | 3033 static PyObject * |
4488 | 3034 OptionsItem(OptionsObject *self, PyObject *keyObject) |
4350 | 3035 { |
3036 char_u *key; | |
3037 int flags; | |
3038 long numval; | |
3039 char_u *stringval; | |
4659
80b0081824fa
updated for version 7.3.1077
Bram Moolenaar <bram@vim.org>
parents:
4653
diff
changeset
|
3040 PyObject *todecref; |
4350 | 3041 |
4488 | 3042 if (self->Check(self->from)) |
4350 | 3043 return NULL; |
3044 | |
4659
80b0081824fa
updated for version 7.3.1077
Bram Moolenaar <bram@vim.org>
parents:
4653
diff
changeset
|
3045 if (!(key = StringToChars(keyObject, &todecref))) |
80b0081824fa
updated for version 7.3.1077
Bram Moolenaar <bram@vim.org>
parents:
4653
diff
changeset
|
3046 return NULL; |
4702
26f2dbea7443
updated for version 7.3.1098
Bram Moolenaar <bram@vim.org>
parents:
4698
diff
changeset
|
3047 |
4659
80b0081824fa
updated for version 7.3.1077
Bram Moolenaar <bram@vim.org>
parents:
4653
diff
changeset
|
3048 if (*key == NUL) |
80b0081824fa
updated for version 7.3.1077
Bram Moolenaar <bram@vim.org>
parents:
4653
diff
changeset
|
3049 { |
80b0081824fa
updated for version 7.3.1077
Bram Moolenaar <bram@vim.org>
parents:
4653
diff
changeset
|
3050 RAISE_NO_EMPTY_KEYS; |
4702
26f2dbea7443
updated for version 7.3.1098
Bram Moolenaar <bram@vim.org>
parents:
4698
diff
changeset
|
3051 Py_XDECREF(todecref); |
4659
80b0081824fa
updated for version 7.3.1077
Bram Moolenaar <bram@vim.org>
parents:
4653
diff
changeset
|
3052 return NULL; |
80b0081824fa
updated for version 7.3.1077
Bram Moolenaar <bram@vim.org>
parents:
4653
diff
changeset
|
3053 } |
4350 | 3054 |
3055 flags = get_option_value_strict(key, &numval, &stringval, | |
4488 | 3056 self->opt_type, self->from); |
4350 | 3057 |
4659
80b0081824fa
updated for version 7.3.1077
Bram Moolenaar <bram@vim.org>
parents:
4653
diff
changeset
|
3058 Py_XDECREF(todecref); |
4350 | 3059 |
3060 if (flags == 0) | |
3061 { | |
4403 | 3062 PyErr_SetObject(PyExc_KeyError, keyObject); |
4350 | 3063 return NULL; |
3064 } | |
3065 | |
3066 if (flags & SOPT_UNSET) | |
3067 { | |
3068 Py_INCREF(Py_None); | |
3069 return Py_None; | |
3070 } | |
3071 else if (flags & SOPT_BOOL) | |
3072 { | |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
3073 PyObject *ret; |
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
3074 ret = numval ? Py_True : Py_False; |
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
3075 Py_INCREF(ret); |
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
3076 return ret; |
4350 | 3077 } |
3078 else if (flags & SOPT_NUM) | |
3079 return PyInt_FromLong(numval); | |
3080 else if (flags & SOPT_STRING) | |
3081 { | |
3082 if (stringval) | |
4509
b498224f5b41
updated for version 7.3.1002
Bram Moolenaar <bram@vim.org>
parents:
4500
diff
changeset
|
3083 { |
5088
34c629c3b4ba
updated for version 7.3.1287
Bram Moolenaar <bram@vim.org>
parents:
4995
diff
changeset
|
3084 PyObject *ret = PyBytes_FromString((char *)stringval); |
4509
b498224f5b41
updated for version 7.3.1002
Bram Moolenaar <bram@vim.org>
parents:
4500
diff
changeset
|
3085 vim_free(stringval); |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
3086 return ret; |
4509
b498224f5b41
updated for version 7.3.1002
Bram Moolenaar <bram@vim.org>
parents:
4500
diff
changeset
|
3087 } |
4350 | 3088 else |
3089 { | |
4968
b6e693e1f946
updated for version 7.3.1229
Bram Moolenaar <bram@vim.org>
parents:
4966
diff
changeset
|
3090 PyErr_SET_STRING(PyExc_RuntimeError, |
4978
f4969f8f66e9
updated for version 7.3.1234
Bram Moolenaar <bram@vim.org>
parents:
4976
diff
changeset
|
3091 N_("unable to get option value")); |
4350 | 3092 return NULL; |
3093 } | |
3094 } | |
3095 else | |
3096 { | |
4978
f4969f8f66e9
updated for version 7.3.1234
Bram Moolenaar <bram@vim.org>
parents:
4976
diff
changeset
|
3097 PyErr_SET_VIM(N_("internal error: unknown option type")); |
4350 | 3098 return NULL; |
3099 } | |
3100 } | |
3101 | |
3102 static int | |
5610 | 3103 OptionsContains(OptionsObject *self, PyObject *keyObject) |
3104 { | |
3105 char_u *key; | |
3106 PyObject *todecref; | |
3107 | |
3108 if (!(key = StringToChars(keyObject, &todecref))) | |
3109 return -1; | |
3110 | |
3111 if (*key == NUL) | |
3112 { | |
3113 Py_XDECREF(todecref); | |
3114 return 0; | |
3115 } | |
3116 | |
3117 if (get_option_value_strict(key, NULL, NULL, self->opt_type, NULL)) | |
3118 { | |
3119 Py_XDECREF(todecref); | |
3120 return 1; | |
3121 } | |
3122 else | |
3123 { | |
3124 Py_XDECREF(todecref); | |
3125 return 0; | |
3126 } | |
3127 } | |
3128 | |
3129 typedef struct | |
3130 { | |
3131 void *lastoption; | |
3132 int opt_type; | |
3133 } optiterinfo_T; | |
3134 | |
3135 static PyObject * | |
3136 OptionsIterNext(optiterinfo_T **oii) | |
3137 { | |
3138 char_u *name; | |
3139 | |
3140 if ((name = option_iter_next(&((*oii)->lastoption), (*oii)->opt_type))) | |
3141 return PyString_FromString((char *)name); | |
3142 | |
3143 return NULL; | |
3144 } | |
3145 | |
3146 static PyObject * | |
3147 OptionsIter(OptionsObject *self) | |
3148 { | |
3149 optiterinfo_T *oii; | |
3150 | |
3151 if (!(oii = PyMem_New(optiterinfo_T, 1))) | |
3152 { | |
3153 PyErr_NoMemory(); | |
3154 return NULL; | |
3155 } | |
3156 | |
3157 oii->opt_type = self->opt_type; | |
3158 oii->lastoption = NULL; | |
3159 | |
3160 return IterNew(oii, | |
3161 (destructorfun) PyMem_Free, (nextfun) OptionsIterNext, | |
3162 NULL, NULL); | |
3163 } | |
3164 | |
3165 static int | |
4922
8dd2769ab75c
updated for version 7.3.1206
Bram Moolenaar <bram@vim.org>
parents:
4918
diff
changeset
|
3166 set_option_value_err(char_u *key, int numval, char_u *stringval, int opt_flags) |
4513
cadb57fbb781
updated for version 7.3.1004
Bram Moolenaar <bram@vim.org>
parents:
4511
diff
changeset
|
3167 { |
cadb57fbb781
updated for version 7.3.1004
Bram Moolenaar <bram@vim.org>
parents:
4511
diff
changeset
|
3168 char_u *errmsg; |
cadb57fbb781
updated for version 7.3.1004
Bram Moolenaar <bram@vim.org>
parents:
4511
diff
changeset
|
3169 |
cadb57fbb781
updated for version 7.3.1004
Bram Moolenaar <bram@vim.org>
parents:
4511
diff
changeset
|
3170 if ((errmsg = set_option_value(key, numval, stringval, opt_flags))) |
cadb57fbb781
updated for version 7.3.1004
Bram Moolenaar <bram@vim.org>
parents:
4511
diff
changeset
|
3171 { |
cadb57fbb781
updated for version 7.3.1004
Bram Moolenaar <bram@vim.org>
parents:
4511
diff
changeset
|
3172 if (VimTryEnd()) |
cadb57fbb781
updated for version 7.3.1004
Bram Moolenaar <bram@vim.org>
parents:
4511
diff
changeset
|
3173 return FAIL; |
cadb57fbb781
updated for version 7.3.1004
Bram Moolenaar <bram@vim.org>
parents:
4511
diff
changeset
|
3174 PyErr_SetVim((char *)errmsg); |
cadb57fbb781
updated for version 7.3.1004
Bram Moolenaar <bram@vim.org>
parents:
4511
diff
changeset
|
3175 return FAIL; |
cadb57fbb781
updated for version 7.3.1004
Bram Moolenaar <bram@vim.org>
parents:
4511
diff
changeset
|
3176 } |
cadb57fbb781
updated for version 7.3.1004
Bram Moolenaar <bram@vim.org>
parents:
4511
diff
changeset
|
3177 return OK; |
cadb57fbb781
updated for version 7.3.1004
Bram Moolenaar <bram@vim.org>
parents:
4511
diff
changeset
|
3178 } |
cadb57fbb781
updated for version 7.3.1004
Bram Moolenaar <bram@vim.org>
parents:
4511
diff
changeset
|
3179 |
cadb57fbb781
updated for version 7.3.1004
Bram Moolenaar <bram@vim.org>
parents:
4511
diff
changeset
|
3180 static int |
4922
8dd2769ab75c
updated for version 7.3.1206
Bram Moolenaar <bram@vim.org>
parents:
4918
diff
changeset
|
3181 set_option_value_for( |
8dd2769ab75c
updated for version 7.3.1206
Bram Moolenaar <bram@vim.org>
parents:
4918
diff
changeset
|
3182 char_u *key, |
8dd2769ab75c
updated for version 7.3.1206
Bram Moolenaar <bram@vim.org>
parents:
4918
diff
changeset
|
3183 int numval, |
8dd2769ab75c
updated for version 7.3.1206
Bram Moolenaar <bram@vim.org>
parents:
4918
diff
changeset
|
3184 char_u *stringval, |
8dd2769ab75c
updated for version 7.3.1206
Bram Moolenaar <bram@vim.org>
parents:
4918
diff
changeset
|
3185 int opt_flags, |
8dd2769ab75c
updated for version 7.3.1206
Bram Moolenaar <bram@vim.org>
parents:
4918
diff
changeset
|
3186 int opt_type, |
8dd2769ab75c
updated for version 7.3.1206
Bram Moolenaar <bram@vim.org>
parents:
4918
diff
changeset
|
3187 void *from) |
4350 | 3188 { |
4509
b498224f5b41
updated for version 7.3.1002
Bram Moolenaar <bram@vim.org>
parents:
4500
diff
changeset
|
3189 win_T *save_curwin = NULL; |
b498224f5b41
updated for version 7.3.1002
Bram Moolenaar <bram@vim.org>
parents:
4500
diff
changeset
|
3190 tabpage_T *save_curtab = NULL; |
b498224f5b41
updated for version 7.3.1002
Bram Moolenaar <bram@vim.org>
parents:
4500
diff
changeset
|
3191 buf_T *save_curbuf = NULL; |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
3192 int set_ret = 0; |
4498 | 3193 |
3194 VimTryStart(); | |
4350 | 3195 switch (opt_type) |
3196 { | |
3197 case SREQ_WIN: | |
4429 | 3198 if (switch_win(&save_curwin, &save_curtab, (win_T *)from, |
4918
0792cc5133ce
updated for version 7.3.1204
Bram Moolenaar <bram@vim.org>
parents:
4859
diff
changeset
|
3199 win_find_tabpage((win_T *)from), FALSE) == FAIL) |
4350 | 3200 { |
6454 | 3201 restore_win(save_curwin, save_curtab, TRUE); |
4498 | 3202 if (VimTryEnd()) |
3203 return -1; | |
4978
f4969f8f66e9
updated for version 7.3.1234
Bram Moolenaar <bram@vim.org>
parents:
4976
diff
changeset
|
3204 PyErr_SET_VIM(N_("problem while switching windows")); |
4350 | 3205 return -1; |
3206 } | |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
3207 set_ret = set_option_value_err(key, numval, stringval, opt_flags); |
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
3208 restore_win(save_curwin, save_curtab, TRUE); |
4350 | 3209 break; |
3210 case SREQ_BUF: | |
4429 | 3211 switch_buffer(&save_curbuf, (buf_T *)from); |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
3212 set_ret = set_option_value_err(key, numval, stringval, opt_flags); |
4429 | 3213 restore_buffer(save_curbuf); |
4350 | 3214 break; |
3215 case SREQ_GLOBAL: | |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
3216 set_ret = set_option_value_err(key, numval, stringval, opt_flags); |
4350 | 3217 break; |
3218 } | |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
3219 if (set_ret == FAIL) |
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
3220 return -1; |
4498 | 3221 return VimTryEnd(); |
4350 | 3222 } |
3223 | |
3224 static int | |
4488 | 3225 OptionsAssItem(OptionsObject *self, PyObject *keyObject, PyObject *valObject) |
4350 | 3226 { |
3227 char_u *key; | |
3228 int flags; | |
3229 int opt_flags; | |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
3230 int ret = 0; |
4659
80b0081824fa
updated for version 7.3.1077
Bram Moolenaar <bram@vim.org>
parents:
4653
diff
changeset
|
3231 PyObject *todecref; |
4350 | 3232 |
4488 | 3233 if (self->Check(self->from)) |
4350 | 3234 return -1; |
3235 | |
4659
80b0081824fa
updated for version 7.3.1077
Bram Moolenaar <bram@vim.org>
parents:
4653
diff
changeset
|
3236 if (!(key = StringToChars(keyObject, &todecref))) |
80b0081824fa
updated for version 7.3.1077
Bram Moolenaar <bram@vim.org>
parents:
4653
diff
changeset
|
3237 return -1; |
4702
26f2dbea7443
updated for version 7.3.1098
Bram Moolenaar <bram@vim.org>
parents:
4698
diff
changeset
|
3238 |
4659
80b0081824fa
updated for version 7.3.1077
Bram Moolenaar <bram@vim.org>
parents:
4653
diff
changeset
|
3239 if (*key == NUL) |
80b0081824fa
updated for version 7.3.1077
Bram Moolenaar <bram@vim.org>
parents:
4653
diff
changeset
|
3240 { |
80b0081824fa
updated for version 7.3.1077
Bram Moolenaar <bram@vim.org>
parents:
4653
diff
changeset
|
3241 RAISE_NO_EMPTY_KEYS; |
4702
26f2dbea7443
updated for version 7.3.1098
Bram Moolenaar <bram@vim.org>
parents:
4698
diff
changeset
|
3242 Py_XDECREF(todecref); |
4659
80b0081824fa
updated for version 7.3.1077
Bram Moolenaar <bram@vim.org>
parents:
4653
diff
changeset
|
3243 return -1; |
80b0081824fa
updated for version 7.3.1077
Bram Moolenaar <bram@vim.org>
parents:
4653
diff
changeset
|
3244 } |
4350 | 3245 |
3246 flags = get_option_value_strict(key, NULL, NULL, | |
4488 | 3247 self->opt_type, self->from); |
4350 | 3248 |
3249 if (flags == 0) | |
3250 { | |
4403 | 3251 PyErr_SetObject(PyExc_KeyError, keyObject); |
4659
80b0081824fa
updated for version 7.3.1077
Bram Moolenaar <bram@vim.org>
parents:
4653
diff
changeset
|
3252 Py_XDECREF(todecref); |
4350 | 3253 return -1; |
3254 } | |
3255 | |
3256 if (valObject == NULL) | |
3257 { | |
4488 | 3258 if (self->opt_type == SREQ_GLOBAL) |
4350 | 3259 { |
4970
f5c822e5a0eb
updated for version 7.3.1230
Bram Moolenaar <bram@vim.org>
parents:
4968
diff
changeset
|
3260 PyErr_FORMAT(PyExc_ValueError, |
4978
f4969f8f66e9
updated for version 7.3.1234
Bram Moolenaar <bram@vim.org>
parents:
4976
diff
changeset
|
3261 N_("unable to unset global option %s"), key); |
4659
80b0081824fa
updated for version 7.3.1077
Bram Moolenaar <bram@vim.org>
parents:
4653
diff
changeset
|
3262 Py_XDECREF(todecref); |
4350 | 3263 return -1; |
3264 } | |
3265 else if (!(flags & SOPT_GLOBAL)) | |
3266 { | |
4970
f5c822e5a0eb
updated for version 7.3.1230
Bram Moolenaar <bram@vim.org>
parents:
4968
diff
changeset
|
3267 PyErr_FORMAT(PyExc_ValueError, |
4978
f4969f8f66e9
updated for version 7.3.1234
Bram Moolenaar <bram@vim.org>
parents:
4976
diff
changeset
|
3268 N_("unable to unset option %s " |
f4969f8f66e9
updated for version 7.3.1234
Bram Moolenaar <bram@vim.org>
parents:
4976
diff
changeset
|
3269 "which does not have global value"), key); |
4659
80b0081824fa
updated for version 7.3.1077
Bram Moolenaar <bram@vim.org>
parents:
4653
diff
changeset
|
3270 Py_XDECREF(todecref); |
4350 | 3271 return -1; |
3272 } | |
3273 else | |
3274 { | |
4488 | 3275 unset_global_local_option(key, self->from); |
4659
80b0081824fa
updated for version 7.3.1077
Bram Moolenaar <bram@vim.org>
parents:
4653
diff
changeset
|
3276 Py_XDECREF(todecref); |
4350 | 3277 return 0; |
3278 } | |
3279 } | |
3280 | |
4488 | 3281 opt_flags = (self->opt_type ? OPT_LOCAL : OPT_GLOBAL); |
4350 | 3282 |
3283 if (flags & SOPT_BOOL) | |
3284 { | |
4411 | 3285 int istrue = PyObject_IsTrue(valObject); |
4513
cadb57fbb781
updated for version 7.3.1004
Bram Moolenaar <bram@vim.org>
parents:
4511
diff
changeset
|
3286 |
4411 | 3287 if (istrue == -1) |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
3288 ret = -1; |
4587
63c9b681c3db
updated for version 7.3.1041
Bram Moolenaar <bram@vim.org>
parents:
4575
diff
changeset
|
3289 else |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
3290 ret = set_option_value_for(key, istrue, NULL, |
4587
63c9b681c3db
updated for version 7.3.1041
Bram Moolenaar <bram@vim.org>
parents:
4575
diff
changeset
|
3291 opt_flags, self->opt_type, self->from); |
4350 | 3292 } |
3293 else if (flags & SOPT_NUM) | |
3294 { | |
4972
537bbfff0c5c
updated for version 7.3.1231
Bram Moolenaar <bram@vim.org>
parents:
4970
diff
changeset
|
3295 long val; |
537bbfff0c5c
updated for version 7.3.1231
Bram Moolenaar <bram@vim.org>
parents:
4970
diff
changeset
|
3296 |
537bbfff0c5c
updated for version 7.3.1231
Bram Moolenaar <bram@vim.org>
parents:
4970
diff
changeset
|
3297 if (NumberToLong(valObject, &val, NUMBER_INT)) |
4350 | 3298 { |
4659
80b0081824fa
updated for version 7.3.1077
Bram Moolenaar <bram@vim.org>
parents:
4653
diff
changeset
|
3299 Py_XDECREF(todecref); |
4350 | 3300 return -1; |
3301 } | |
3302 | |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
3303 ret = set_option_value_for(key, (int) val, NULL, opt_flags, |
4488 | 3304 self->opt_type, self->from); |
4350 | 3305 } |
3306 else | |
3307 { | |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
3308 char_u *val; |
5525 | 3309 PyObject *todecref2; |
3310 | |
3311 if ((val = StringToChars(valObject, &todecref2))) | |
3312 { | |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
3313 ret = set_option_value_for(key, 0, val, opt_flags, |
4589
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
3314 self->opt_type, self->from); |
5525 | 3315 Py_XDECREF(todecref2); |
3316 } | |
4350 | 3317 else |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
3318 ret = -1; |
4350 | 3319 } |
3320 | |
4659
80b0081824fa
updated for version 7.3.1077
Bram Moolenaar <bram@vim.org>
parents:
4653
diff
changeset
|
3321 Py_XDECREF(todecref); |
4587
63c9b681c3db
updated for version 7.3.1041
Bram Moolenaar <bram@vim.org>
parents:
4575
diff
changeset
|
3322 |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
3323 return ret; |
4350 | 3324 } |
3325 | |
5610 | 3326 static PySequenceMethods OptionsAsSeq = { |
3327 0, /* sq_length */ | |
3328 0, /* sq_concat */ | |
3329 0, /* sq_repeat */ | |
3330 0, /* sq_item */ | |
3331 0, /* sq_slice */ | |
3332 0, /* sq_ass_item */ | |
3333 0, /* sq_ass_slice */ | |
3334 (objobjproc) OptionsContains, /* sq_contains */ | |
3335 0, /* sq_inplace_concat */ | |
3336 0, /* sq_inplace_repeat */ | |
3337 }; | |
3338 | |
4350 | 3339 static PyMappingMethods OptionsAsMapping = { |
3340 (lenfunc) NULL, | |
3341 (binaryfunc) OptionsItem, | |
3342 (objobjargproc) OptionsAssItem, | |
3343 }; | |
3344 | |
4401 | 3345 /* Tabpage object |
3346 */ | |
3347 | |
3348 typedef struct | |
3349 { | |
3350 PyObject_HEAD | |
3351 tabpage_T *tab; | |
3352 } TabPageObject; | |
3353 | |
3354 static PyObject *WinListNew(TabPageObject *tabObject); | |
3355 | |
3356 static PyTypeObject TabPageType; | |
3357 | |
3358 static int | |
4488 | 3359 CheckTabPage(TabPageObject *self) |
4401 | 3360 { |
4488 | 3361 if (self->tab == INVALID_TABPAGE_VALUE) |
4401 | 3362 { |
4978
f4969f8f66e9
updated for version 7.3.1234
Bram Moolenaar <bram@vim.org>
parents:
4976
diff
changeset
|
3363 PyErr_SET_VIM(N_("attempt to refer to deleted tab page")); |
4401 | 3364 return -1; |
3365 } | |
3366 | |
3367 return 0; | |
3368 } | |
3369 | |
3370 static PyObject * | |
3371 TabPageNew(tabpage_T *tab) | |
3372 { | |
3373 TabPageObject *self; | |
3374 | |
3375 if (TAB_PYTHON_REF(tab)) | |
3376 { | |
3377 self = TAB_PYTHON_REF(tab); | |
3378 Py_INCREF(self); | |
3379 } | |
3380 else | |
3381 { | |
3382 self = PyObject_NEW(TabPageObject, &TabPageType); | |
3383 if (self == NULL) | |
3384 return NULL; | |
3385 self->tab = tab; | |
3386 TAB_PYTHON_REF(tab) = self; | |
3387 } | |
3388 | |
3389 return (PyObject *)(self); | |
3390 } | |
3391 | |
3392 static void | |
4488 | 3393 TabPageDestructor(TabPageObject *self) |
4401 | 3394 { |
4488 | 3395 if (self->tab && self->tab != INVALID_TABPAGE_VALUE) |
3396 TAB_PYTHON_REF(self->tab) = NULL; | |
4401 | 3397 |
3398 DESTRUCTOR_FINISH(self); | |
3399 } | |
3400 | |
4599
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
3401 static char *TabPageAttrs[] = { |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
3402 "windows", "number", "vars", "window", "valid", |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
3403 NULL |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
3404 }; |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
3405 |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
3406 static PyObject * |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
3407 TabPageDir(PyObject *self) |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
3408 { |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
3409 return ObjectDir(self, TabPageAttrs); |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
3410 } |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
3411 |
4401 | 3412 static PyObject * |
4593
0cf552b325b5
updated for version 7.3.1044
Bram Moolenaar <bram@vim.org>
parents:
4589
diff
changeset
|
3413 TabPageAttrValid(TabPageObject *self, char *name) |
0cf552b325b5
updated for version 7.3.1044
Bram Moolenaar <bram@vim.org>
parents:
4589
diff
changeset
|
3414 { |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
3415 PyObject *ret; |
4593
0cf552b325b5
updated for version 7.3.1044
Bram Moolenaar <bram@vim.org>
parents:
4589
diff
changeset
|
3416 |
0cf552b325b5
updated for version 7.3.1044
Bram Moolenaar <bram@vim.org>
parents:
4589
diff
changeset
|
3417 if (strcmp(name, "valid") != 0) |
0cf552b325b5
updated for version 7.3.1044
Bram Moolenaar <bram@vim.org>
parents:
4589
diff
changeset
|
3418 return NULL; |
0cf552b325b5
updated for version 7.3.1044
Bram Moolenaar <bram@vim.org>
parents:
4589
diff
changeset
|
3419 |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
3420 ret = ((self->tab == INVALID_TABPAGE_VALUE) ? Py_False : Py_True); |
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
3421 Py_INCREF(ret); |
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
3422 return ret; |
4593
0cf552b325b5
updated for version 7.3.1044
Bram Moolenaar <bram@vim.org>
parents:
4589
diff
changeset
|
3423 } |
0cf552b325b5
updated for version 7.3.1044
Bram Moolenaar <bram@vim.org>
parents:
4589
diff
changeset
|
3424 |
0cf552b325b5
updated for version 7.3.1044
Bram Moolenaar <bram@vim.org>
parents:
4589
diff
changeset
|
3425 static PyObject * |
4488 | 3426 TabPageAttr(TabPageObject *self, char *name) |
4401 | 3427 { |
3428 if (strcmp(name, "windows") == 0) | |
4488 | 3429 return WinListNew(self); |
4401 | 3430 else if (strcmp(name, "number") == 0) |
4488 | 3431 return PyLong_FromLong((long) get_tab_number(self->tab)); |
4401 | 3432 else if (strcmp(name, "vars") == 0) |
4627
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
3433 return NEW_DICTIONARY(self->tab->tp_vars); |
4401 | 3434 else if (strcmp(name, "window") == 0) |
3435 { | |
3436 /* For current tab window.c does not bother to set or update tp_curwin | |
3437 */ | |
4488 | 3438 if (self->tab == curtab) |
4431 | 3439 return WindowNew(curwin, curtab); |
4401 | 3440 else |
4488 | 3441 return WindowNew(self->tab->tp_curwin, self->tab); |
4401 | 3442 } |
4599
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
3443 else if (strcmp(name, "__members__") == 0) |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
3444 return ObjectDir(NULL, TabPageAttrs); |
4401 | 3445 return NULL; |
3446 } | |
3447 | |
3448 static PyObject * | |
4488 | 3449 TabPageRepr(TabPageObject *self) |
4401 | 3450 { |
4488 | 3451 if (self->tab == INVALID_TABPAGE_VALUE) |
4623
548b889fe3cf
updated for version 7.3.1059
Bram Moolenaar <bram@vim.org>
parents:
4619
diff
changeset
|
3452 return PyString_FromFormat("<tabpage object (deleted) at %p>", (self)); |
4401 | 3453 else |
3454 { | |
4488 | 3455 int t = get_tab_number(self->tab); |
4401 | 3456 |
3457 if (t == 0) | |
4623
548b889fe3cf
updated for version 7.3.1059
Bram Moolenaar <bram@vim.org>
parents:
4619
diff
changeset
|
3458 return PyString_FromFormat("<tabpage object (unknown) at %p>", |
548b889fe3cf
updated for version 7.3.1059
Bram Moolenaar <bram@vim.org>
parents:
4619
diff
changeset
|
3459 (self)); |
4401 | 3460 else |
4623
548b889fe3cf
updated for version 7.3.1059
Bram Moolenaar <bram@vim.org>
parents:
4619
diff
changeset
|
3461 return PyString_FromFormat("<tabpage %d>", t - 1); |
4401 | 3462 } |
3463 } | |
3464 | |
3465 static struct PyMethodDef TabPageMethods[] = { | |
4599
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
3466 /* name, function, calling, documentation */ |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
3467 {"__dir__", (PyCFunction)TabPageDir, METH_NOARGS, ""}, |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
3468 { NULL, NULL, 0, NULL} |
4401 | 3469 }; |
3470 | |
3471 /* | |
3472 * Window list object | |
3473 */ | |
3474 | |
3475 static PyTypeObject TabListType; | |
3476 static PySequenceMethods TabListAsSeq; | |
3477 | |
3478 typedef struct | |
3479 { | |
3480 PyObject_HEAD | |
3481 } TabListObject; | |
3482 | |
3483 static PyInt | |
3484 TabListLength(PyObject *self UNUSED) | |
3485 { | |
3486 tabpage_T *tp = first_tabpage; | |
3487 PyInt n = 0; | |
3488 | |
3489 while (tp != NULL) | |
3490 { | |
3491 ++n; | |
3492 tp = tp->tp_next; | |
3493 } | |
3494 | |
3495 return n; | |
3496 } | |
3497 | |
3498 static PyObject * | |
3499 TabListItem(PyObject *self UNUSED, PyInt n) | |
3500 { | |
3501 tabpage_T *tp; | |
3502 | |
3503 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, --n) | |
3504 if (n == 0) | |
3505 return TabPageNew(tp); | |
3506 | |
4978
f4969f8f66e9
updated for version 7.3.1234
Bram Moolenaar <bram@vim.org>
parents:
4976
diff
changeset
|
3507 PyErr_SET_STRING(PyExc_IndexError, N_("no such tab page")); |
4401 | 3508 return NULL; |
3509 } | |
3510 | |
4962
b34d719b13cd
updated for version 7.3.1226
Bram Moolenaar <bram@vim.org>
parents:
4922
diff
changeset
|
3511 /* |
b34d719b13cd
updated for version 7.3.1226
Bram Moolenaar <bram@vim.org>
parents:
4922
diff
changeset
|
3512 * Window object |
4385 | 3513 */ |
3514 | |
3515 typedef struct | |
3516 { | |
3517 PyObject_HEAD | |
3518 win_T *win; | |
4431 | 3519 TabPageObject *tabObject; |
4385 | 3520 } WindowObject; |
3521 | |
3522 static PyTypeObject WindowType; | |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3523 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3524 static int |
4488 | 3525 CheckWindow(WindowObject *self) |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3526 { |
4488 | 3527 if (self->win == INVALID_WINDOW_VALUE) |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3528 { |
4978
f4969f8f66e9
updated for version 7.3.1234
Bram Moolenaar <bram@vim.org>
parents:
4976
diff
changeset
|
3529 PyErr_SET_VIM(N_("attempt to refer to deleted window")); |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3530 return -1; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3531 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3532 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3533 return 0; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3534 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3535 |
4319 | 3536 static PyObject * |
4431 | 3537 WindowNew(win_T *win, tabpage_T *tab) |
4377 | 3538 { |
3539 /* We need to handle deletion of windows underneath us. | |
3540 * If we add a "w_python*_ref" field to the win_T structure, | |
3541 * then we can get at it in win_free() in vim. We then | |
3542 * need to create only ONE Python object per window - if | |
3543 * we try to create a second, just INCREF the existing one | |
3544 * and return it. The (single) Python object referring to | |
3545 * the window is stored in "w_python*_ref". | |
3546 * On a win_free() we set the Python object's win_T* field | |
3547 * to an invalid value. We trap all uses of a window | |
3548 * object, and reject them if the win_T* field is invalid. | |
3549 * | |
4385 | 3550 * Python2 and Python3 get different fields and different objects: |
4377 | 3551 * w_python_ref and w_python3_ref fields respectively. |
3552 */ | |
3553 | |
3554 WindowObject *self; | |
3555 | |
3556 if (WIN_PYTHON_REF(win)) | |
3557 { | |
3558 self = WIN_PYTHON_REF(win); | |
3559 Py_INCREF(self); | |
3560 } | |
3561 else | |
3562 { | |
4500 | 3563 self = PyObject_GC_New(WindowObject, &WindowType); |
4377 | 3564 if (self == NULL) |
3565 return NULL; | |
3566 self->win = win; | |
3567 WIN_PYTHON_REF(win) = self; | |
3568 } | |
3569 | |
4431 | 3570 self->tabObject = ((TabPageObject *)(TabPageNew(tab))); |
3571 | |
4377 | 3572 return (PyObject *)(self); |
3573 } | |
3574 | |
4385 | 3575 static void |
4488 | 3576 WindowDestructor(WindowObject *self) |
4385 | 3577 { |
4500 | 3578 PyObject_GC_UnTrack((void *)(self)); |
4488 | 3579 if (self->win && self->win != INVALID_WINDOW_VALUE) |
3580 WIN_PYTHON_REF(self->win) = NULL; | |
4500 | 3581 Py_XDECREF(((PyObject *)(self->tabObject))); |
3582 PyObject_GC_Del((void *)(self)); | |
3583 } | |
3584 | |
3585 static int | |
3586 WindowTraverse(WindowObject *self, visitproc visit, void *arg) | |
3587 { | |
3588 Py_VISIT(((PyObject *)(self->tabObject))); | |
3589 return 0; | |
3590 } | |
3591 | |
3592 static int | |
3593 WindowClear(WindowObject *self) | |
3594 { | |
3595 Py_CLEAR(self->tabObject); | |
3596 return 0; | |
4385 | 3597 } |
3598 | |
4431 | 3599 static win_T * |
3600 get_firstwin(TabPageObject *tabObject) | |
3601 { | |
3602 if (tabObject) | |
3603 { | |
3604 if (CheckTabPage(tabObject)) | |
3605 return NULL; | |
3606 /* For current tab window.c does not bother to set or update tp_firstwin | |
3607 */ | |
3608 else if (tabObject->tab == curtab) | |
3609 return firstwin; | |
3610 else | |
3611 return tabObject->tab->tp_firstwin; | |
3612 } | |
3613 else | |
3614 return firstwin; | |
3615 } | |
4599
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
3616 static char *WindowAttrs[] = { |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
3617 "buffer", "cursor", "height", "vars", "options", "number", "row", "col", |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
3618 "tabpage", "valid", |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
3619 NULL |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
3620 }; |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
3621 |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
3622 static PyObject * |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
3623 WindowDir(PyObject *self) |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
3624 { |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
3625 return ObjectDir(self, WindowAttrs); |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
3626 } |
4431 | 3627 |
4377 | 3628 static PyObject * |
4593
0cf552b325b5
updated for version 7.3.1044
Bram Moolenaar <bram@vim.org>
parents:
4589
diff
changeset
|
3629 WindowAttrValid(WindowObject *self, char *name) |
0cf552b325b5
updated for version 7.3.1044
Bram Moolenaar <bram@vim.org>
parents:
4589
diff
changeset
|
3630 { |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
3631 PyObject *ret; |
4593
0cf552b325b5
updated for version 7.3.1044
Bram Moolenaar <bram@vim.org>
parents:
4589
diff
changeset
|
3632 |
0cf552b325b5
updated for version 7.3.1044
Bram Moolenaar <bram@vim.org>
parents:
4589
diff
changeset
|
3633 if (strcmp(name, "valid") != 0) |
0cf552b325b5
updated for version 7.3.1044
Bram Moolenaar <bram@vim.org>
parents:
4589
diff
changeset
|
3634 return NULL; |
0cf552b325b5
updated for version 7.3.1044
Bram Moolenaar <bram@vim.org>
parents:
4589
diff
changeset
|
3635 |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
3636 ret = ((self->win == INVALID_WINDOW_VALUE) ? Py_False : Py_True); |
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
3637 Py_INCREF(ret); |
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
3638 return ret; |
4593
0cf552b325b5
updated for version 7.3.1044
Bram Moolenaar <bram@vim.org>
parents:
4589
diff
changeset
|
3639 } |
0cf552b325b5
updated for version 7.3.1044
Bram Moolenaar <bram@vim.org>
parents:
4589
diff
changeset
|
3640 |
0cf552b325b5
updated for version 7.3.1044
Bram Moolenaar <bram@vim.org>
parents:
4589
diff
changeset
|
3641 static PyObject * |
4488 | 3642 WindowAttr(WindowObject *self, char *name) |
4319 | 3643 { |
3644 if (strcmp(name, "buffer") == 0) | |
4488 | 3645 return (PyObject *)BufferNew(self->win->w_buffer); |
4319 | 3646 else if (strcmp(name, "cursor") == 0) |
3647 { | |
4488 | 3648 pos_T *pos = &self->win->w_cursor; |
4319 | 3649 |
3650 return Py_BuildValue("(ll)", (long)(pos->lnum), (long)(pos->col)); | |
3651 } | |
3652 else if (strcmp(name, "height") == 0) | |
4488 | 3653 return PyLong_FromLong((long)(self->win->w_height)); |
4383 | 3654 #ifdef FEAT_WINDOWS |
3655 else if (strcmp(name, "row") == 0) | |
4488 | 3656 return PyLong_FromLong((long)(self->win->w_winrow)); |
4383 | 3657 #endif |
4319 | 3658 #ifdef FEAT_VERTSPLIT |
3659 else if (strcmp(name, "width") == 0) | |
4488 | 3660 return PyLong_FromLong((long)(W_WIDTH(self->win))); |
4383 | 3661 else if (strcmp(name, "col") == 0) |
4488 | 3662 return PyLong_FromLong((long)(W_WINCOL(self->win))); |
4319 | 3663 #endif |
4323 | 3664 else if (strcmp(name, "vars") == 0) |
4627
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
3665 return NEW_DICTIONARY(self->win->w_vars); |
4350 | 3666 else if (strcmp(name, "options") == 0) |
4488 | 3667 return OptionsNew(SREQ_WIN, self->win, (checkfun) CheckWindow, |
3668 (PyObject *) self); | |
4379 | 3669 else if (strcmp(name, "number") == 0) |
4431 | 3670 { |
4488 | 3671 if (CheckTabPage(self->tabObject)) |
4431 | 3672 return NULL; |
3673 return PyLong_FromLong((long) | |
4488 | 3674 get_win_number(self->win, get_firstwin(self->tabObject))); |
4431 | 3675 } |
3676 else if (strcmp(name, "tabpage") == 0) | |
3677 { | |
4488 | 3678 Py_INCREF(self->tabObject); |
3679 return (PyObject *)(self->tabObject); | |
4431 | 3680 } |
4599
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
3681 else if (strcmp(name, "__members__") == 0) |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
3682 return ObjectDir(NULL, WindowAttrs); |
4319 | 3683 else |
3684 return NULL; | |
3685 } | |
3686 | |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3687 static int |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
3688 WindowSetattr(WindowObject *self, char *name, PyObject *valObject) |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3689 { |
4488 | 3690 if (CheckWindow(self)) |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3691 return -1; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3692 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3693 if (strcmp(name, "buffer") == 0) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3694 { |
4978
f4969f8f66e9
updated for version 7.3.1234
Bram Moolenaar <bram@vim.org>
parents:
4976
diff
changeset
|
3695 PyErr_SET_STRING(PyExc_TypeError, N_("readonly attribute: buffer")); |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3696 return -1; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3697 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3698 else if (strcmp(name, "cursor") == 0) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3699 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3700 long lnum; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3701 long col; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3702 |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
3703 if (!PyArg_Parse(valObject, "(ll)", &lnum, &col)) |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3704 return -1; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3705 |
4488 | 3706 if (lnum <= 0 || lnum > self->win->w_buffer->b_ml.ml_line_count) |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3707 { |
4978
f4969f8f66e9
updated for version 7.3.1234
Bram Moolenaar <bram@vim.org>
parents:
4976
diff
changeset
|
3708 PyErr_SET_VIM(N_("cursor position outside buffer")); |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3709 return -1; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3710 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3711 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3712 /* Check for keyboard interrupts */ |
4498 | 3713 if (VimCheckInterrupt()) |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3714 return -1; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3715 |
4488 | 3716 self->win->w_cursor.lnum = lnum; |
3717 self->win->w_cursor.col = col; | |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3718 #ifdef FEAT_VIRTUALEDIT |
4488 | 3719 self->win->w_cursor.coladd = 0; |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3720 #endif |
2933 | 3721 /* When column is out of range silently correct it. */ |
4488 | 3722 check_cursor_col_win(self->win); |
2933 | 3723 |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3724 update_screen(VALID); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3725 return 0; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3726 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3727 else if (strcmp(name, "height") == 0) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3728 { |
4972
537bbfff0c5c
updated for version 7.3.1231
Bram Moolenaar <bram@vim.org>
parents:
4970
diff
changeset
|
3729 long height; |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3730 win_T *savewin; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3731 |
4982
39980afcf54a
updated for version 7.3.1236
Bram Moolenaar <bram@vim.org>
parents:
4978
diff
changeset
|
3732 if (NumberToLong(valObject, &height, NUMBER_INT|NUMBER_UNSIGNED)) |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3733 return -1; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3734 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3735 #ifdef FEAT_GUI |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3736 need_mouse_correct = TRUE; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3737 #endif |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3738 savewin = curwin; |
4488 | 3739 curwin = self->win; |
4498 | 3740 |
3741 VimTryStart(); | |
4972
537bbfff0c5c
updated for version 7.3.1231
Bram Moolenaar <bram@vim.org>
parents:
4970
diff
changeset
|
3742 win_setheight((int) height); |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3743 curwin = savewin; |
4498 | 3744 if (VimTryEnd()) |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3745 return -1; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3746 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3747 return 0; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3748 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3749 #ifdef FEAT_VERTSPLIT |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3750 else if (strcmp(name, "width") == 0) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3751 { |
4972
537bbfff0c5c
updated for version 7.3.1231
Bram Moolenaar <bram@vim.org>
parents:
4970
diff
changeset
|
3752 long width; |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3753 win_T *savewin; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3754 |
4982
39980afcf54a
updated for version 7.3.1236
Bram Moolenaar <bram@vim.org>
parents:
4978
diff
changeset
|
3755 if (NumberToLong(valObject, &width, NUMBER_INT|NUMBER_UNSIGNED)) |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3756 return -1; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3757 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3758 #ifdef FEAT_GUI |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3759 need_mouse_correct = TRUE; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3760 #endif |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3761 savewin = curwin; |
4488 | 3762 curwin = self->win; |
4498 | 3763 |
3764 VimTryStart(); | |
4972
537bbfff0c5c
updated for version 7.3.1231
Bram Moolenaar <bram@vim.org>
parents:
4970
diff
changeset
|
3765 win_setwidth((int) width); |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3766 curwin = savewin; |
4498 | 3767 if (VimTryEnd()) |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3768 return -1; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3769 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3770 return 0; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3771 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3772 #endif |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3773 else |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3774 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3775 PyErr_SetString(PyExc_AttributeError, name); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3776 return -1; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3777 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3778 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3779 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3780 static PyObject * |
4488 | 3781 WindowRepr(WindowObject *self) |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3782 { |
4488 | 3783 if (self->win == INVALID_WINDOW_VALUE) |
4623
548b889fe3cf
updated for version 7.3.1059
Bram Moolenaar <bram@vim.org>
parents:
4619
diff
changeset
|
3784 return PyString_FromFormat("<window object (deleted) at %p>", (self)); |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3785 else |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3786 { |
4488 | 3787 int w = get_win_number(self->win, firstwin); |
4379 | 3788 |
3789 if (w == 0) | |
4623
548b889fe3cf
updated for version 7.3.1059
Bram Moolenaar <bram@vim.org>
parents:
4619
diff
changeset
|
3790 return PyString_FromFormat("<window object (unknown) at %p>", |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3791 (self)); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3792 else |
4623
548b889fe3cf
updated for version 7.3.1059
Bram Moolenaar <bram@vim.org>
parents:
4619
diff
changeset
|
3793 return PyString_FromFormat("<window %d>", w - 1); |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3794 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3795 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3796 |
4385 | 3797 static struct PyMethodDef WindowMethods[] = { |
4599
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
3798 /* name, function, calling, documentation */ |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
3799 {"__dir__", (PyCFunction)WindowDir, METH_NOARGS, ""}, |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
3800 { NULL, NULL, 0, NULL} |
4385 | 3801 }; |
3802 | |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3803 /* |
4385 | 3804 * Window list object |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3805 */ |
4319 | 3806 |
4385 | 3807 static PyTypeObject WinListType; |
3808 static PySequenceMethods WinListAsSeq; | |
3809 | |
4319 | 3810 typedef struct |
3811 { | |
3812 PyObject_HEAD | |
4401 | 3813 TabPageObject *tabObject; |
4319 | 3814 } WinListObject; |
3815 | |
4401 | 3816 static PyObject * |
3817 WinListNew(TabPageObject *tabObject) | |
3818 { | |
3819 WinListObject *self; | |
3820 | |
3821 self = PyObject_NEW(WinListObject, &WinListType); | |
3822 self->tabObject = tabObject; | |
3823 Py_INCREF(tabObject); | |
3824 | |
3825 return (PyObject *)(self); | |
3826 } | |
3827 | |
3828 static void | |
4488 | 3829 WinListDestructor(WinListObject *self) |
4401 | 3830 { |
4488 | 3831 TabPageObject *tabObject = self->tabObject; |
4401 | 3832 |
3833 if (tabObject) | |
4523
57393dc4b811
updated for version 7.3.1009
Bram Moolenaar <bram@vim.org>
parents:
4513
diff
changeset
|
3834 { |
4401 | 3835 Py_DECREF((PyObject *)(tabObject)); |
4523
57393dc4b811
updated for version 7.3.1009
Bram Moolenaar <bram@vim.org>
parents:
4513
diff
changeset
|
3836 } |
4401 | 3837 |
3838 DESTRUCTOR_FINISH(self); | |
3839 } | |
3840 | |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3841 static PyInt |
4488 | 3842 WinListLength(WinListObject *self) |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3843 { |
4401 | 3844 win_T *w; |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3845 PyInt n = 0; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3846 |
4488 | 3847 if (!(w = get_firstwin(self->tabObject))) |
4401 | 3848 return -1; |
3849 | |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3850 while (w != NULL) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3851 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3852 ++n; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3853 w = W_NEXT(w); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3854 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3855 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3856 return n; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3857 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3858 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3859 static PyObject * |
4488 | 3860 WinListItem(WinListObject *self, PyInt n) |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3861 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3862 win_T *w; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3863 |
4488 | 3864 if (!(w = get_firstwin(self->tabObject))) |
4401 | 3865 return NULL; |
3866 | |
3867 for (; w != NULL; w = W_NEXT(w), --n) | |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3868 if (n == 0) |
4488 | 3869 return WindowNew(w, self->tabObject? self->tabObject->tab: curtab); |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3870 |
4978
f4969f8f66e9
updated for version 7.3.1234
Bram Moolenaar <bram@vim.org>
parents:
4976
diff
changeset
|
3871 PyErr_SET_STRING(PyExc_IndexError, N_("no such window")); |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3872 return NULL; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3873 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3874 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3875 /* Convert a Python string into a Vim line. |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3876 * |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3877 * The result is in allocated memory. All internal nulls are replaced by |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3878 * newline characters. It is an error for the string to contain newline |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3879 * characters. |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3880 * |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3881 * On errors, the Python exception data is set, and NULL is returned. |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3882 */ |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3883 static char * |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3884 StringToLine(PyObject *obj) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3885 { |
4966
620d9b59d4ed
updated for version 7.3.1228
Bram Moolenaar <bram@vim.org>
parents:
4964
diff
changeset
|
3886 char *str; |
620d9b59d4ed
updated for version 7.3.1228
Bram Moolenaar <bram@vim.org>
parents:
4964
diff
changeset
|
3887 char *save; |
620d9b59d4ed
updated for version 7.3.1228
Bram Moolenaar <bram@vim.org>
parents:
4964
diff
changeset
|
3888 PyObject *bytes = NULL; |
4982
39980afcf54a
updated for version 7.3.1236
Bram Moolenaar <bram@vim.org>
parents:
4978
diff
changeset
|
3889 Py_ssize_t len = 0; |
4966
620d9b59d4ed
updated for version 7.3.1228
Bram Moolenaar <bram@vim.org>
parents:
4964
diff
changeset
|
3890 PyInt i; |
620d9b59d4ed
updated for version 7.3.1228
Bram Moolenaar <bram@vim.org>
parents:
4964
diff
changeset
|
3891 char *p; |
620d9b59d4ed
updated for version 7.3.1228
Bram Moolenaar <bram@vim.org>
parents:
4964
diff
changeset
|
3892 |
620d9b59d4ed
updated for version 7.3.1228
Bram Moolenaar <bram@vim.org>
parents:
4964
diff
changeset
|
3893 if (PyBytes_Check(obj)) |
620d9b59d4ed
updated for version 7.3.1228
Bram Moolenaar <bram@vim.org>
parents:
4964
diff
changeset
|
3894 { |
620d9b59d4ed
updated for version 7.3.1228
Bram Moolenaar <bram@vim.org>
parents:
4964
diff
changeset
|
3895 if (PyBytes_AsStringAndSize(obj, &str, &len) == -1 |
620d9b59d4ed
updated for version 7.3.1228
Bram Moolenaar <bram@vim.org>
parents:
4964
diff
changeset
|
3896 || str == NULL) |
620d9b59d4ed
updated for version 7.3.1228
Bram Moolenaar <bram@vim.org>
parents:
4964
diff
changeset
|
3897 return NULL; |
620d9b59d4ed
updated for version 7.3.1228
Bram Moolenaar <bram@vim.org>
parents:
4964
diff
changeset
|
3898 } |
620d9b59d4ed
updated for version 7.3.1228
Bram Moolenaar <bram@vim.org>
parents:
4964
diff
changeset
|
3899 else if (PyUnicode_Check(obj)) |
620d9b59d4ed
updated for version 7.3.1228
Bram Moolenaar <bram@vim.org>
parents:
4964
diff
changeset
|
3900 { |
620d9b59d4ed
updated for version 7.3.1228
Bram Moolenaar <bram@vim.org>
parents:
4964
diff
changeset
|
3901 if (!(bytes = PyUnicode_AsEncodedString(obj, ENC_OPT, NULL))) |
620d9b59d4ed
updated for version 7.3.1228
Bram Moolenaar <bram@vim.org>
parents:
4964
diff
changeset
|
3902 return NULL; |
620d9b59d4ed
updated for version 7.3.1228
Bram Moolenaar <bram@vim.org>
parents:
4964
diff
changeset
|
3903 |
4995
b4a2eaf28b51
updated for version 7.3.1242
Bram Moolenaar <bram@vim.org>
parents:
4988
diff
changeset
|
3904 if (PyBytes_AsStringAndSize(bytes, &str, &len) == -1 |
4966
620d9b59d4ed
updated for version 7.3.1228
Bram Moolenaar <bram@vim.org>
parents:
4964
diff
changeset
|
3905 || str == NULL) |
620d9b59d4ed
updated for version 7.3.1228
Bram Moolenaar <bram@vim.org>
parents:
4964
diff
changeset
|
3906 { |
620d9b59d4ed
updated for version 7.3.1228
Bram Moolenaar <bram@vim.org>
parents:
4964
diff
changeset
|
3907 Py_DECREF(bytes); |
620d9b59d4ed
updated for version 7.3.1228
Bram Moolenaar <bram@vim.org>
parents:
4964
diff
changeset
|
3908 return NULL; |
620d9b59d4ed
updated for version 7.3.1228
Bram Moolenaar <bram@vim.org>
parents:
4964
diff
changeset
|
3909 } |
620d9b59d4ed
updated for version 7.3.1228
Bram Moolenaar <bram@vim.org>
parents:
4964
diff
changeset
|
3910 } |
4995
b4a2eaf28b51
updated for version 7.3.1242
Bram Moolenaar <bram@vim.org>
parents:
4988
diff
changeset
|
3911 else |
b4a2eaf28b51
updated for version 7.3.1242
Bram Moolenaar <bram@vim.org>
parents:
4988
diff
changeset
|
3912 { |
b4a2eaf28b51
updated for version 7.3.1242
Bram Moolenaar <bram@vim.org>
parents:
4988
diff
changeset
|
3913 #if PY_MAJOR_VERSION < 3 |
b4a2eaf28b51
updated for version 7.3.1242
Bram Moolenaar <bram@vim.org>
parents:
4988
diff
changeset
|
3914 PyErr_FORMAT(PyExc_TypeError, |
b4a2eaf28b51
updated for version 7.3.1242
Bram Moolenaar <bram@vim.org>
parents:
4988
diff
changeset
|
3915 N_("expected str() or unicode() instance, but got %s"), |
b4a2eaf28b51
updated for version 7.3.1242
Bram Moolenaar <bram@vim.org>
parents:
4988
diff
changeset
|
3916 Py_TYPE_NAME(obj)); |
b4a2eaf28b51
updated for version 7.3.1242
Bram Moolenaar <bram@vim.org>
parents:
4988
diff
changeset
|
3917 #else |
b4a2eaf28b51
updated for version 7.3.1242
Bram Moolenaar <bram@vim.org>
parents:
4988
diff
changeset
|
3918 PyErr_FORMAT(PyExc_TypeError, |
b4a2eaf28b51
updated for version 7.3.1242
Bram Moolenaar <bram@vim.org>
parents:
4988
diff
changeset
|
3919 N_("expected bytes() or str() instance, but got %s"), |
b4a2eaf28b51
updated for version 7.3.1242
Bram Moolenaar <bram@vim.org>
parents:
4988
diff
changeset
|
3920 Py_TYPE_NAME(obj)); |
b4a2eaf28b51
updated for version 7.3.1242
Bram Moolenaar <bram@vim.org>
parents:
4988
diff
changeset
|
3921 #endif |
b4a2eaf28b51
updated for version 7.3.1242
Bram Moolenaar <bram@vim.org>
parents:
4988
diff
changeset
|
3922 return NULL; |
b4a2eaf28b51
updated for version 7.3.1242
Bram Moolenaar <bram@vim.org>
parents:
4988
diff
changeset
|
3923 } |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3924 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3925 /* |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3926 * Error checking: String must not contain newlines, as we |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3927 * are replacing a single line, and we must replace it with |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3928 * a single line. |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3929 * A trailing newline is removed, so that append(f.readlines()) works. |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3930 */ |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3931 p = memchr(str, '\n', len); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3932 if (p != NULL) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3933 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3934 if (p == str + len - 1) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3935 --len; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3936 else |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3937 { |
4978
f4969f8f66e9
updated for version 7.3.1234
Bram Moolenaar <bram@vim.org>
parents:
4976
diff
changeset
|
3938 PyErr_SET_VIM(N_("string cannot contain newlines")); |
4976
4ed713442c51
updated for version 7.3.1233
Bram Moolenaar <bram@vim.org>
parents:
4974
diff
changeset
|
3939 Py_XDECREF(bytes); |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3940 return NULL; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3941 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3942 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3943 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3944 /* Create a copy of the string, with internal nulls replaced by |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3945 * newline characters, as is the vim convention. |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3946 */ |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3947 save = (char *)alloc((unsigned)(len+1)); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3948 if (save == NULL) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3949 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3950 PyErr_NoMemory(); |
4976
4ed713442c51
updated for version 7.3.1233
Bram Moolenaar <bram@vim.org>
parents:
4974
diff
changeset
|
3951 Py_XDECREF(bytes); |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3952 return NULL; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3953 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3954 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3955 for (i = 0; i < len; ++i) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3956 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3957 if (str[i] == '\0') |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3958 save[i] = '\n'; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3959 else |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3960 save[i] = str[i]; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3961 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3962 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3963 save[i] = '\0'; |
4966
620d9b59d4ed
updated for version 7.3.1228
Bram Moolenaar <bram@vim.org>
parents:
4964
diff
changeset
|
3964 Py_XDECREF(bytes); /* Python 2 does nothing here */ |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3965 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3966 return save; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3967 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3968 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3969 /* Get a line from the specified buffer. The line number is |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3970 * in Vim format (1-based). The line is returned as a Python |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3971 * string object. |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3972 */ |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3973 static PyObject * |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3974 GetBufferLine(buf_T *buf, PyInt n) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3975 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3976 return LineToString((char *)ml_get_buf(buf, (linenr_T)n, FALSE)); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3977 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3978 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3979 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3980 /* Get a list of lines from the specified buffer. The line numbers |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3981 * are in Vim format (1-based). The range is from lo up to, but not |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3982 * including, hi. The list is returned as a Python list of string objects. |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3983 */ |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3984 static PyObject * |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3985 GetBufferLineList(buf_T *buf, PyInt lo, PyInt hi) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3986 { |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
3987 PyInt i; |
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
3988 PyInt n = hi - lo; |
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
3989 PyObject *list = PyList_New(n); |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3990 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3991 if (list == NULL) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3992 return NULL; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3993 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3994 for (i = 0; i < n; ++i) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3995 { |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
3996 PyObject *string = LineToString( |
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
3997 (char *)ml_get_buf(buf, (linenr_T)(lo+i), FALSE)); |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3998 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3999 /* Error check - was the Python string creation OK? */ |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
4000 if (string == NULL) |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4001 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4002 Py_DECREF(list); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4003 return NULL; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4004 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4005 |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
4006 PyList_SET_ITEM(list, i, string); |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4007 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4008 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4009 /* The ownership of the Python list is passed to the caller (ie, |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4010 * the caller should Py_DECREF() the object when it is finished |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4011 * with it). |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4012 */ |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4013 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4014 return list; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4015 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4016 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4017 /* |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4018 * Check if deleting lines made the cursor position invalid. |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4019 * Changed the lines from "lo" to "hi" and added "extra" lines (negative if |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4020 * deleted). |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4021 */ |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4022 static void |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4023 py_fix_cursor(linenr_T lo, linenr_T hi, linenr_T extra) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4024 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4025 if (curwin->w_cursor.lnum >= lo) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4026 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4027 /* Adjust the cursor position if it's in/after the changed |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4028 * lines. */ |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4029 if (curwin->w_cursor.lnum >= hi) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4030 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4031 curwin->w_cursor.lnum += extra; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4032 check_cursor_col(); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4033 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4034 else if (extra < 0) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4035 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4036 curwin->w_cursor.lnum = lo; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4037 check_cursor(); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4038 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4039 else |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4040 check_cursor_col(); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4041 changed_cline_bef_curs(); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4042 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4043 invalidate_botline(); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4044 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4045 |
2894 | 4046 /* |
5235
6fa64615c8d3
updated for version 7.4a.043
Bram Moolenaar <bram@vim.org>
parents:
5202
diff
changeset
|
4047 * Find a window that contains "buf" and switch to it. |
6fa64615c8d3
updated for version 7.4a.043
Bram Moolenaar <bram@vim.org>
parents:
5202
diff
changeset
|
4048 * If there is no such window, use the current window and change "curbuf". |
6fa64615c8d3
updated for version 7.4a.043
Bram Moolenaar <bram@vim.org>
parents:
5202
diff
changeset
|
4049 * Caller must initialize save_curbuf to NULL. |
6fa64615c8d3
updated for version 7.4a.043
Bram Moolenaar <bram@vim.org>
parents:
5202
diff
changeset
|
4050 * restore_win_for_buf() MUST be called later! |
6fa64615c8d3
updated for version 7.4a.043
Bram Moolenaar <bram@vim.org>
parents:
5202
diff
changeset
|
4051 */ |
6fa64615c8d3
updated for version 7.4a.043
Bram Moolenaar <bram@vim.org>
parents:
5202
diff
changeset
|
4052 static void |
6fa64615c8d3
updated for version 7.4a.043
Bram Moolenaar <bram@vim.org>
parents:
5202
diff
changeset
|
4053 switch_to_win_for_buf( |
6fa64615c8d3
updated for version 7.4a.043
Bram Moolenaar <bram@vim.org>
parents:
5202
diff
changeset
|
4054 buf_T *buf, |
6fa64615c8d3
updated for version 7.4a.043
Bram Moolenaar <bram@vim.org>
parents:
5202
diff
changeset
|
4055 win_T **save_curwinp, |
6fa64615c8d3
updated for version 7.4a.043
Bram Moolenaar <bram@vim.org>
parents:
5202
diff
changeset
|
4056 tabpage_T **save_curtabp, |
6fa64615c8d3
updated for version 7.4a.043
Bram Moolenaar <bram@vim.org>
parents:
5202
diff
changeset
|
4057 buf_T **save_curbufp) |
6fa64615c8d3
updated for version 7.4a.043
Bram Moolenaar <bram@vim.org>
parents:
5202
diff
changeset
|
4058 { |
6fa64615c8d3
updated for version 7.4a.043
Bram Moolenaar <bram@vim.org>
parents:
5202
diff
changeset
|
4059 win_T *wp; |
6fa64615c8d3
updated for version 7.4a.043
Bram Moolenaar <bram@vim.org>
parents:
5202
diff
changeset
|
4060 tabpage_T *tp; |
6fa64615c8d3
updated for version 7.4a.043
Bram Moolenaar <bram@vim.org>
parents:
5202
diff
changeset
|
4061 |
6454 | 4062 if (find_win_for_buf(buf, &wp, &tp) == FAIL) |
5235
6fa64615c8d3
updated for version 7.4a.043
Bram Moolenaar <bram@vim.org>
parents:
5202
diff
changeset
|
4063 switch_buffer(save_curbufp, buf); |
6454 | 4064 else if (switch_win(save_curwinp, save_curtabp, wp, tp, TRUE) == FAIL) |
4065 { | |
4066 restore_win(*save_curwinp, *save_curtabp, TRUE); | |
4067 switch_buffer(save_curbufp, buf); | |
4068 } | |
5235
6fa64615c8d3
updated for version 7.4a.043
Bram Moolenaar <bram@vim.org>
parents:
5202
diff
changeset
|
4069 } |
6fa64615c8d3
updated for version 7.4a.043
Bram Moolenaar <bram@vim.org>
parents:
5202
diff
changeset
|
4070 |
6fa64615c8d3
updated for version 7.4a.043
Bram Moolenaar <bram@vim.org>
parents:
5202
diff
changeset
|
4071 static void |
6fa64615c8d3
updated for version 7.4a.043
Bram Moolenaar <bram@vim.org>
parents:
5202
diff
changeset
|
4072 restore_win_for_buf( |
6fa64615c8d3
updated for version 7.4a.043
Bram Moolenaar <bram@vim.org>
parents:
5202
diff
changeset
|
4073 win_T *save_curwin, |
6fa64615c8d3
updated for version 7.4a.043
Bram Moolenaar <bram@vim.org>
parents:
5202
diff
changeset
|
4074 tabpage_T *save_curtab, |
6fa64615c8d3
updated for version 7.4a.043
Bram Moolenaar <bram@vim.org>
parents:
5202
diff
changeset
|
4075 buf_T *save_curbuf) |
6fa64615c8d3
updated for version 7.4a.043
Bram Moolenaar <bram@vim.org>
parents:
5202
diff
changeset
|
4076 { |
6fa64615c8d3
updated for version 7.4a.043
Bram Moolenaar <bram@vim.org>
parents:
5202
diff
changeset
|
4077 if (save_curbuf == NULL) |
6fa64615c8d3
updated for version 7.4a.043
Bram Moolenaar <bram@vim.org>
parents:
5202
diff
changeset
|
4078 restore_win(save_curwin, save_curtab, TRUE); |
6fa64615c8d3
updated for version 7.4a.043
Bram Moolenaar <bram@vim.org>
parents:
5202
diff
changeset
|
4079 else |
6fa64615c8d3
updated for version 7.4a.043
Bram Moolenaar <bram@vim.org>
parents:
5202
diff
changeset
|
4080 restore_buffer(save_curbuf); |
6fa64615c8d3
updated for version 7.4a.043
Bram Moolenaar <bram@vim.org>
parents:
5202
diff
changeset
|
4081 } |
6fa64615c8d3
updated for version 7.4a.043
Bram Moolenaar <bram@vim.org>
parents:
5202
diff
changeset
|
4082 |
6fa64615c8d3
updated for version 7.4a.043
Bram Moolenaar <bram@vim.org>
parents:
5202
diff
changeset
|
4083 /* |
2894 | 4084 * Replace a line in the specified buffer. The line number is |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4085 * in Vim format (1-based). The replacement line is given as |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4086 * a Python string object. The object is checked for validity |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4087 * and correct format. Errors are returned as a value of FAIL. |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4088 * The return value is OK on success. |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4089 * If OK is returned and len_change is not NULL, *len_change |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4090 * is set to the change in the buffer length. |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4091 */ |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4092 static int |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4093 SetBufferLine(buf_T *buf, PyInt n, PyObject *line, PyInt *len_change) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4094 { |
5235
6fa64615c8d3
updated for version 7.4a.043
Bram Moolenaar <bram@vim.org>
parents:
5202
diff
changeset
|
4095 buf_T *save_curbuf = NULL; |
6fa64615c8d3
updated for version 7.4a.043
Bram Moolenaar <bram@vim.org>
parents:
5202
diff
changeset
|
4096 win_T *save_curwin = NULL; |
6fa64615c8d3
updated for version 7.4a.043
Bram Moolenaar <bram@vim.org>
parents:
5202
diff
changeset
|
4097 tabpage_T *save_curtab = NULL; |
6fa64615c8d3
updated for version 7.4a.043
Bram Moolenaar <bram@vim.org>
parents:
5202
diff
changeset
|
4098 |
4352 | 4099 /* First of all, we check the type of the supplied Python object. |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4100 * There are three cases: |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4101 * 1. NULL, or None - this is a deletion. |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4102 * 2. A string - this is a replacement. |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4103 * 3. Anything else - this is an error. |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4104 */ |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4105 if (line == Py_None || line == NULL) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4106 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4107 PyErr_Clear(); |
5235
6fa64615c8d3
updated for version 7.4a.043
Bram Moolenaar <bram@vim.org>
parents:
5202
diff
changeset
|
4108 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf); |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4109 |
4498 | 4110 VimTryStart(); |
4111 | |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4112 if (u_savedel((linenr_T)n, 1L) == FAIL) |
4970
f5c822e5a0eb
updated for version 7.3.1230
Bram Moolenaar <bram@vim.org>
parents:
4968
diff
changeset
|
4113 RAISE_UNDO_FAIL; |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4114 else if (ml_delete((linenr_T)n, FALSE) == FAIL) |
4978
f4969f8f66e9
updated for version 7.3.1234
Bram Moolenaar <bram@vim.org>
parents:
4976
diff
changeset
|
4115 RAISE_DELETE_LINE_FAIL; |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4116 else |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4117 { |
5235
6fa64615c8d3
updated for version 7.4a.043
Bram Moolenaar <bram@vim.org>
parents:
5202
diff
changeset
|
4118 if (buf == curbuf) |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4119 py_fix_cursor((linenr_T)n, (linenr_T)n + 1, (linenr_T)-1); |
5235
6fa64615c8d3
updated for version 7.4a.043
Bram Moolenaar <bram@vim.org>
parents:
5202
diff
changeset
|
4120 if (save_curbuf == NULL) |
6fa64615c8d3
updated for version 7.4a.043
Bram Moolenaar <bram@vim.org>
parents:
5202
diff
changeset
|
4121 /* Only adjust marks if we managed to switch to a window that |
6fa64615c8d3
updated for version 7.4a.043
Bram Moolenaar <bram@vim.org>
parents:
5202
diff
changeset
|
4122 * holds the buffer, otherwise line numbers will be invalid. */ |
6fa64615c8d3
updated for version 7.4a.043
Bram Moolenaar <bram@vim.org>
parents:
5202
diff
changeset
|
4123 deleted_lines_mark((linenr_T)n, 1L); |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4124 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4125 |
5235
6fa64615c8d3
updated for version 7.4a.043
Bram Moolenaar <bram@vim.org>
parents:
5202
diff
changeset
|
4126 restore_win_for_buf(save_curwin, save_curtab, save_curbuf); |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4127 |
4498 | 4128 if (VimTryEnd()) |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4129 return FAIL; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4130 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4131 if (len_change) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4132 *len_change = -1; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4133 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4134 return OK; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4135 } |
4966
620d9b59d4ed
updated for version 7.3.1228
Bram Moolenaar <bram@vim.org>
parents:
4964
diff
changeset
|
4136 else if (PyBytes_Check(line) || PyUnicode_Check(line)) |
620d9b59d4ed
updated for version 7.3.1228
Bram Moolenaar <bram@vim.org>
parents:
4964
diff
changeset
|
4137 { |
620d9b59d4ed
updated for version 7.3.1228
Bram Moolenaar <bram@vim.org>
parents:
4964
diff
changeset
|
4138 char *save = StringToLine(line); |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4139 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4140 if (save == NULL) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4141 return FAIL; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4142 |
4498 | 4143 VimTryStart(); |
4144 | |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4145 /* We do not need to free "save" if ml_replace() consumes it. */ |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4146 PyErr_Clear(); |
5235
6fa64615c8d3
updated for version 7.4a.043
Bram Moolenaar <bram@vim.org>
parents:
5202
diff
changeset
|
4147 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf); |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4148 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4149 if (u_savesub((linenr_T)n) == FAIL) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4150 { |
4970
f5c822e5a0eb
updated for version 7.3.1230
Bram Moolenaar <bram@vim.org>
parents:
4968
diff
changeset
|
4151 RAISE_UNDO_FAIL; |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4152 vim_free(save); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4153 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4154 else if (ml_replace((linenr_T)n, (char_u *)save, FALSE) == FAIL) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4155 { |
4978
f4969f8f66e9
updated for version 7.3.1234
Bram Moolenaar <bram@vim.org>
parents:
4976
diff
changeset
|
4156 RAISE_REPLACE_LINE_FAIL; |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4157 vim_free(save); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4158 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4159 else |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4160 changed_bytes((linenr_T)n, 0); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4161 |
5235
6fa64615c8d3
updated for version 7.4a.043
Bram Moolenaar <bram@vim.org>
parents:
5202
diff
changeset
|
4162 restore_win_for_buf(save_curwin, save_curtab, save_curbuf); |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4163 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4164 /* Check that the cursor is not beyond the end of the line now. */ |
5235
6fa64615c8d3
updated for version 7.4a.043
Bram Moolenaar <bram@vim.org>
parents:
5202
diff
changeset
|
4165 if (buf == curbuf) |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4166 check_cursor_col(); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4167 |
4498 | 4168 if (VimTryEnd()) |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4169 return FAIL; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4170 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4171 if (len_change) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4172 *len_change = 0; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4173 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4174 return OK; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4175 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4176 else |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4177 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4178 PyErr_BadArgument(); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4179 return FAIL; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4180 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4181 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4182 |
2894 | 4183 /* Replace a range of lines in the specified buffer. The line numbers are in |
4184 * Vim format (1-based). The range is from lo up to, but not including, hi. | |
4185 * The replacement lines are given as a Python list of string objects. The | |
4186 * list is checked for validity and correct format. Errors are returned as a | |
4187 * value of FAIL. The return value is OK on success. | |
4188 * If OK is returned and len_change is not NULL, *len_change | |
4189 * is set to the change in the buffer length. | |
4190 */ | |
4191 static int | |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
4192 SetBufferLineList( |
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
4193 buf_T *buf, |
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
4194 PyInt lo, |
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
4195 PyInt hi, |
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
4196 PyObject *list, |
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
4197 PyInt *len_change) |
2894 | 4198 { |
5235
6fa64615c8d3
updated for version 7.4a.043
Bram Moolenaar <bram@vim.org>
parents:
5202
diff
changeset
|
4199 buf_T *save_curbuf = NULL; |
6fa64615c8d3
updated for version 7.4a.043
Bram Moolenaar <bram@vim.org>
parents:
5202
diff
changeset
|
4200 win_T *save_curwin = NULL; |
6fa64615c8d3
updated for version 7.4a.043
Bram Moolenaar <bram@vim.org>
parents:
5202
diff
changeset
|
4201 tabpage_T *save_curtab = NULL; |
6fa64615c8d3
updated for version 7.4a.043
Bram Moolenaar <bram@vim.org>
parents:
5202
diff
changeset
|
4202 |
4352 | 4203 /* First of all, we check the type of the supplied Python object. |
2894 | 4204 * There are three cases: |
4205 * 1. NULL, or None - this is a deletion. | |
4206 * 2. A list - this is a replacement. | |
4207 * 3. Anything else - this is an error. | |
4208 */ | |
4209 if (list == Py_None || list == NULL) | |
4210 { | |
4211 PyInt i; | |
4212 PyInt n = (int)(hi - lo); | |
4213 | |
4214 PyErr_Clear(); | |
4498 | 4215 VimTryStart(); |
5235
6fa64615c8d3
updated for version 7.4a.043
Bram Moolenaar <bram@vim.org>
parents:
5202
diff
changeset
|
4216 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf); |
2894 | 4217 |
4218 if (u_savedel((linenr_T)lo, (long)n) == FAIL) | |
4970
f5c822e5a0eb
updated for version 7.3.1230
Bram Moolenaar <bram@vim.org>
parents:
4968
diff
changeset
|
4219 RAISE_UNDO_FAIL; |
2894 | 4220 else |
4221 { | |
4222 for (i = 0; i < n; ++i) | |
4223 { | |
4224 if (ml_delete((linenr_T)lo, FALSE) == FAIL) | |
4225 { | |
4978
f4969f8f66e9
updated for version 7.3.1234
Bram Moolenaar <bram@vim.org>
parents:
4976
diff
changeset
|
4226 RAISE_DELETE_LINE_FAIL; |
2894 | 4227 break; |
4228 } | |
4229 } | |
6176 | 4230 if (buf == curbuf && (save_curwin != NULL || save_curbuf == NULL)) |
4231 /* Using an existing window for the buffer, adjust the cursor | |
4232 * position. */ | |
2894 | 4233 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)-n); |
5235
6fa64615c8d3
updated for version 7.4a.043
Bram Moolenaar <bram@vim.org>
parents:
5202
diff
changeset
|
4234 if (save_curbuf == NULL) |
6fa64615c8d3
updated for version 7.4a.043
Bram Moolenaar <bram@vim.org>
parents:
5202
diff
changeset
|
4235 /* Only adjust marks if we managed to switch to a window that |
6fa64615c8d3
updated for version 7.4a.043
Bram Moolenaar <bram@vim.org>
parents:
5202
diff
changeset
|
4236 * holds the buffer, otherwise line numbers will be invalid. */ |
6fa64615c8d3
updated for version 7.4a.043
Bram Moolenaar <bram@vim.org>
parents:
5202
diff
changeset
|
4237 deleted_lines_mark((linenr_T)lo, (long)i); |
2894 | 4238 } |
4239 | |
5235
6fa64615c8d3
updated for version 7.4a.043
Bram Moolenaar <bram@vim.org>
parents:
5202
diff
changeset
|
4240 restore_win_for_buf(save_curwin, save_curtab, save_curbuf); |
2894 | 4241 |
4498 | 4242 if (VimTryEnd()) |
2894 | 4243 return FAIL; |
4244 | |
4245 if (len_change) | |
4246 *len_change = -n; | |
4247 | |
4248 return OK; | |
4249 } | |
4250 else if (PyList_Check(list)) | |
4251 { | |
4252 PyInt i; | |
4253 PyInt new_len = PyList_Size(list); | |
4254 PyInt old_len = hi - lo; | |
4255 PyInt extra = 0; /* lines added to text, can be negative */ | |
4256 char **array; | |
4257 | |
4258 if (new_len == 0) /* avoid allocating zero bytes */ | |
4259 array = NULL; | |
4260 else | |
4261 { | |
4589
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
4262 array = PyMem_New(char *, new_len); |
2894 | 4263 if (array == NULL) |
4264 { | |
4265 PyErr_NoMemory(); | |
4266 return FAIL; | |
4267 } | |
4268 } | |
4269 | |
4270 for (i = 0; i < new_len; ++i) | |
4271 { | |
4633
3857d399ab41
updated for version 7.3.1064
Bram Moolenaar <bram@vim.org>
parents:
4631
diff
changeset
|
4272 PyObject *line; |
3857d399ab41
updated for version 7.3.1064
Bram Moolenaar <bram@vim.org>
parents:
4631
diff
changeset
|
4273 |
3857d399ab41
updated for version 7.3.1064
Bram Moolenaar <bram@vim.org>
parents:
4631
diff
changeset
|
4274 if (!(line = PyList_GetItem(list, i)) || |
3857d399ab41
updated for version 7.3.1064
Bram Moolenaar <bram@vim.org>
parents:
4631
diff
changeset
|
4275 !(array[i] = StringToLine(line))) |
2894 | 4276 { |
4277 while (i) | |
4278 vim_free(array[--i]); | |
4589
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
4279 PyMem_Free(array); |
2894 | 4280 return FAIL; |
4281 } | |
4282 } | |
4283 | |
4498 | 4284 VimTryStart(); |
2894 | 4285 PyErr_Clear(); |
4429 | 4286 |
4633
3857d399ab41
updated for version 7.3.1064
Bram Moolenaar <bram@vim.org>
parents:
4631
diff
changeset
|
4287 /* START of region without "return". Must call restore_buffer()! */ |
5235
6fa64615c8d3
updated for version 7.4a.043
Bram Moolenaar <bram@vim.org>
parents:
5202
diff
changeset
|
4288 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf); |
2894 | 4289 |
4290 if (u_save((linenr_T)(lo-1), (linenr_T)hi) == FAIL) | |
4970
f5c822e5a0eb
updated for version 7.3.1230
Bram Moolenaar <bram@vim.org>
parents:
4968
diff
changeset
|
4291 RAISE_UNDO_FAIL; |
2894 | 4292 |
4293 /* If the size of the range is reducing (ie, new_len < old_len) we | |
4294 * need to delete some old_len. We do this at the start, by | |
4295 * repeatedly deleting line "lo". | |
4296 */ | |
4297 if (!PyErr_Occurred()) | |
4298 { | |
4299 for (i = 0; i < old_len - new_len; ++i) | |
4300 if (ml_delete((linenr_T)lo, FALSE) == FAIL) | |
4301 { | |
4978
f4969f8f66e9
updated for version 7.3.1234
Bram Moolenaar <bram@vim.org>
parents:
4976
diff
changeset
|
4302 RAISE_DELETE_LINE_FAIL; |
2894 | 4303 break; |
4304 } | |
4305 extra -= i; | |
4306 } | |
4307 | |
4308 /* For as long as possible, replace the existing old_len with the | |
4309 * new old_len. This is a more efficient operation, as it requires | |
4310 * less memory allocation and freeing. | |
4311 */ | |
4312 if (!PyErr_Occurred()) | |
4313 { | |
4314 for (i = 0; i < old_len && i < new_len; ++i) | |
4315 if (ml_replace((linenr_T)(lo+i), (char_u *)array[i], FALSE) | |
4316 == FAIL) | |
4317 { | |
4978
f4969f8f66e9
updated for version 7.3.1234
Bram Moolenaar <bram@vim.org>
parents:
4976
diff
changeset
|
4318 RAISE_REPLACE_LINE_FAIL; |
2894 | 4319 break; |
4320 } | |
4321 } | |
4322 else | |
4323 i = 0; | |
4324 | |
4325 /* Now we may need to insert the remaining new old_len. If we do, we | |
4326 * must free the strings as we finish with them (we can't pass the | |
4327 * responsibility to vim in this case). | |
4328 */ | |
4329 if (!PyErr_Occurred()) | |
4330 { | |
4331 while (i < new_len) | |
4332 { | |
4333 if (ml_append((linenr_T)(lo + i - 1), | |
4334 (char_u *)array[i], 0, FALSE) == FAIL) | |
4335 { | |
4978
f4969f8f66e9
updated for version 7.3.1234
Bram Moolenaar <bram@vim.org>
parents:
4976
diff
changeset
|
4336 RAISE_INSERT_LINE_FAIL; |
2894 | 4337 break; |
4338 } | |
4339 vim_free(array[i]); | |
4340 ++i; | |
4341 ++extra; | |
4342 } | |
4343 } | |
4344 | |
4345 /* Free any left-over old_len, as a result of an error */ | |
4346 while (i < new_len) | |
4347 { | |
4348 vim_free(array[i]); | |
4349 ++i; | |
4350 } | |
4351 | |
4352 /* Free the array of old_len. All of its contents have now | |
4353 * been dealt with (either freed, or the responsibility passed | |
4354 * to vim. | |
4355 */ | |
4589
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
4356 PyMem_Free(array); |
2894 | 4357 |
4358 /* Adjust marks. Invalidate any which lie in the | |
4359 * changed range, and move any in the remainder of the buffer. | |
5235
6fa64615c8d3
updated for version 7.4a.043
Bram Moolenaar <bram@vim.org>
parents:
5202
diff
changeset
|
4360 * Only adjust marks if we managed to switch to a window that holds |
6fa64615c8d3
updated for version 7.4a.043
Bram Moolenaar <bram@vim.org>
parents:
5202
diff
changeset
|
4361 * the buffer, otherwise line numbers will be invalid. */ |
6fa64615c8d3
updated for version 7.4a.043
Bram Moolenaar <bram@vim.org>
parents:
5202
diff
changeset
|
4362 if (save_curbuf == NULL) |
6fa64615c8d3
updated for version 7.4a.043
Bram Moolenaar <bram@vim.org>
parents:
5202
diff
changeset
|
4363 mark_adjust((linenr_T)lo, (linenr_T)(hi - 1), |
2894 | 4364 (long)MAXLNUM, (long)extra); |
4365 changed_lines((linenr_T)lo, 0, (linenr_T)hi, (long)extra); | |
4366 | |
5235
6fa64615c8d3
updated for version 7.4a.043
Bram Moolenaar <bram@vim.org>
parents:
5202
diff
changeset
|
4367 if (buf == curbuf) |
2894 | 4368 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)extra); |
4369 | |
4633
3857d399ab41
updated for version 7.3.1064
Bram Moolenaar <bram@vim.org>
parents:
4631
diff
changeset
|
4370 /* END of region without "return". */ |
5235
6fa64615c8d3
updated for version 7.4a.043
Bram Moolenaar <bram@vim.org>
parents:
5202
diff
changeset
|
4371 restore_win_for_buf(save_curwin, save_curtab, save_curbuf); |
2894 | 4372 |
4498 | 4373 if (VimTryEnd()) |
2894 | 4374 return FAIL; |
4375 | |
4376 if (len_change) | |
4377 *len_change = new_len - old_len; | |
4378 | |
4379 return OK; | |
4380 } | |
4381 else | |
4382 { | |
4383 PyErr_BadArgument(); | |
4384 return FAIL; | |
4385 } | |
4386 } | |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4387 |
4352 | 4388 /* Insert a number of lines into the specified buffer after the specified line. |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4389 * The line number is in Vim format (1-based). The lines to be inserted are |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4390 * given as a Python list of string objects or as a single string. The lines |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4391 * to be added are checked for validity and correct format. Errors are |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4392 * returned as a value of FAIL. The return value is OK on success. |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4393 * If OK is returned and len_change is not NULL, *len_change |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4394 * is set to the change in the buffer length. |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4395 */ |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4396 static int |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4397 InsertBufferLines(buf_T *buf, PyInt n, PyObject *lines, PyInt *len_change) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4398 { |
5202
8edba3805d78
updated for version 7.4a.027
Bram Moolenaar <bram@vim.org>
parents:
5166
diff
changeset
|
4399 buf_T *save_curbuf = NULL; |
8edba3805d78
updated for version 7.4a.027
Bram Moolenaar <bram@vim.org>
parents:
5166
diff
changeset
|
4400 win_T *save_curwin = NULL; |
8edba3805d78
updated for version 7.4a.027
Bram Moolenaar <bram@vim.org>
parents:
5166
diff
changeset
|
4401 tabpage_T *save_curtab = NULL; |
8edba3805d78
updated for version 7.4a.027
Bram Moolenaar <bram@vim.org>
parents:
5166
diff
changeset
|
4402 |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4403 /* First of all, we check the type of the supplied Python object. |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4404 * It must be a string or a list, or the call is in error. |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4405 */ |
4966
620d9b59d4ed
updated for version 7.3.1228
Bram Moolenaar <bram@vim.org>
parents:
4964
diff
changeset
|
4406 if (PyBytes_Check(lines) || PyUnicode_Check(lines)) |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4407 { |
5202
8edba3805d78
updated for version 7.4a.027
Bram Moolenaar <bram@vim.org>
parents:
5166
diff
changeset
|
4408 char *str = StringToLine(lines); |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4409 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4410 if (str == NULL) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4411 return FAIL; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4412 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4413 PyErr_Clear(); |
4498 | 4414 VimTryStart(); |
5235
6fa64615c8d3
updated for version 7.4a.043
Bram Moolenaar <bram@vim.org>
parents:
5202
diff
changeset
|
4415 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf); |
5202
8edba3805d78
updated for version 7.4a.027
Bram Moolenaar <bram@vim.org>
parents:
5166
diff
changeset
|
4416 |
8edba3805d78
updated for version 7.4a.027
Bram Moolenaar <bram@vim.org>
parents:
5166
diff
changeset
|
4417 if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL) |
4970
f5c822e5a0eb
updated for version 7.3.1230
Bram Moolenaar <bram@vim.org>
parents:
4968
diff
changeset
|
4418 RAISE_UNDO_FAIL; |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4419 else if (ml_append((linenr_T)n, (char_u *)str, 0, FALSE) == FAIL) |
4978
f4969f8f66e9
updated for version 7.3.1234
Bram Moolenaar <bram@vim.org>
parents:
4976
diff
changeset
|
4420 RAISE_INSERT_LINE_FAIL; |
5202
8edba3805d78
updated for version 7.4a.027
Bram Moolenaar <bram@vim.org>
parents:
5166
diff
changeset
|
4421 else if (save_curbuf == NULL) |
8edba3805d78
updated for version 7.4a.027
Bram Moolenaar <bram@vim.org>
parents:
5166
diff
changeset
|
4422 /* Only adjust marks if we managed to switch to a window that |
8edba3805d78
updated for version 7.4a.027
Bram Moolenaar <bram@vim.org>
parents:
5166
diff
changeset
|
4423 * holds the buffer, otherwise line numbers will be invalid. */ |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4424 appended_lines_mark((linenr_T)n, 1L); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4425 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4426 vim_free(str); |
5235
6fa64615c8d3
updated for version 7.4a.043
Bram Moolenaar <bram@vim.org>
parents:
5202
diff
changeset
|
4427 restore_win_for_buf(save_curwin, save_curtab, save_curbuf); |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4428 update_screen(VALID); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4429 |
4498 | 4430 if (VimTryEnd()) |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4431 return FAIL; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4432 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4433 if (len_change) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4434 *len_change = 1; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4435 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4436 return OK; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4437 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4438 else if (PyList_Check(lines)) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4439 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4440 PyInt i; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4441 PyInt size = PyList_Size(lines); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4442 char **array; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4443 |
4589
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
4444 array = PyMem_New(char *, size); |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4445 if (array == NULL) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4446 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4447 PyErr_NoMemory(); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4448 return FAIL; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4449 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4450 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4451 for (i = 0; i < size; ++i) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4452 { |
4633
3857d399ab41
updated for version 7.3.1064
Bram Moolenaar <bram@vim.org>
parents:
4631
diff
changeset
|
4453 PyObject *line; |
3857d399ab41
updated for version 7.3.1064
Bram Moolenaar <bram@vim.org>
parents:
4631
diff
changeset
|
4454 |
3857d399ab41
updated for version 7.3.1064
Bram Moolenaar <bram@vim.org>
parents:
4631
diff
changeset
|
4455 if (!(line = PyList_GetItem(lines, i)) || |
3857d399ab41
updated for version 7.3.1064
Bram Moolenaar <bram@vim.org>
parents:
4631
diff
changeset
|
4456 !(array[i] = StringToLine(line))) |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4457 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4458 while (i) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4459 vim_free(array[--i]); |
4589
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
4460 PyMem_Free(array); |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4461 return FAIL; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4462 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4463 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4464 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4465 PyErr_Clear(); |
4498 | 4466 VimTryStart(); |
5235
6fa64615c8d3
updated for version 7.4a.043
Bram Moolenaar <bram@vim.org>
parents:
5202
diff
changeset
|
4467 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf); |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4468 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4469 if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL) |
4970
f5c822e5a0eb
updated for version 7.3.1230
Bram Moolenaar <bram@vim.org>
parents:
4968
diff
changeset
|
4470 RAISE_UNDO_FAIL; |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4471 else |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4472 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4473 for (i = 0; i < size; ++i) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4474 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4475 if (ml_append((linenr_T)(n + i), |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4476 (char_u *)array[i], 0, FALSE) == FAIL) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4477 { |
4978
f4969f8f66e9
updated for version 7.3.1234
Bram Moolenaar <bram@vim.org>
parents:
4976
diff
changeset
|
4478 RAISE_INSERT_LINE_FAIL; |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4479 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4480 /* Free the rest of the lines */ |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4481 while (i < size) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4482 vim_free(array[i++]); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4483 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4484 break; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4485 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4486 vim_free(array[i]); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4487 } |
5235
6fa64615c8d3
updated for version 7.4a.043
Bram Moolenaar <bram@vim.org>
parents:
5202
diff
changeset
|
4488 if (i > 0 && save_curbuf == NULL) |
6fa64615c8d3
updated for version 7.4a.043
Bram Moolenaar <bram@vim.org>
parents:
5202
diff
changeset
|
4489 /* Only adjust marks if we managed to switch to a window that |
6fa64615c8d3
updated for version 7.4a.043
Bram Moolenaar <bram@vim.org>
parents:
5202
diff
changeset
|
4490 * holds the buffer, otherwise line numbers will be invalid. */ |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4491 appended_lines_mark((linenr_T)n, (long)i); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4492 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4493 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4494 /* Free the array of lines. All of its contents have now |
5202
8edba3805d78
updated for version 7.4a.027
Bram Moolenaar <bram@vim.org>
parents:
5166
diff
changeset
|
4495 * been freed. */ |
4589
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
4496 PyMem_Free(array); |
5235
6fa64615c8d3
updated for version 7.4a.043
Bram Moolenaar <bram@vim.org>
parents:
5202
diff
changeset
|
4497 restore_win_for_buf(save_curwin, save_curtab, save_curbuf); |
6fa64615c8d3
updated for version 7.4a.043
Bram Moolenaar <bram@vim.org>
parents:
5202
diff
changeset
|
4498 |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4499 update_screen(VALID); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4500 |
4498 | 4501 if (VimTryEnd()) |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4502 return FAIL; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4503 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4504 if (len_change) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4505 *len_change = size; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4506 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4507 return OK; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4508 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4509 else |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4510 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4511 PyErr_BadArgument(); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4512 return FAIL; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4513 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4514 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4515 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4516 /* |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4517 * Common routines for buffers and line ranges |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4518 * ------------------------------------------- |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4519 */ |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4520 |
4385 | 4521 typedef struct |
4522 { | |
4523 PyObject_HEAD | |
4524 buf_T *buf; | |
4525 } BufferObject; | |
4526 | |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4527 static int |
4488 | 4528 CheckBuffer(BufferObject *self) |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4529 { |
4488 | 4530 if (self->buf == INVALID_BUFFER_VALUE) |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4531 { |
4978
f4969f8f66e9
updated for version 7.3.1234
Bram Moolenaar <bram@vim.org>
parents:
4976
diff
changeset
|
4532 PyErr_SET_VIM(N_("attempt to refer to deleted buffer")); |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4533 return -1; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4534 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4535 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4536 return 0; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4537 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4538 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4539 static PyObject * |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4540 RBItem(BufferObject *self, PyInt n, PyInt start, PyInt end) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4541 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4542 if (CheckBuffer(self)) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4543 return NULL; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4544 |
4387 | 4545 if (end == -1) |
4546 end = self->buf->b_ml.ml_line_count; | |
4547 | |
4389 | 4548 if (n < 0) |
4549 n += end - start + 1; | |
4550 | |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4551 if (n < 0 || n > end - start) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4552 { |
4978
f4969f8f66e9
updated for version 7.3.1234
Bram Moolenaar <bram@vim.org>
parents:
4976
diff
changeset
|
4553 PyErr_SET_STRING(PyExc_IndexError, N_("line number out of range")); |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4554 return NULL; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4555 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4556 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4557 return GetBufferLine(self->buf, n+start); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4558 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4559 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4560 static PyObject * |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4561 RBSlice(BufferObject *self, PyInt lo, PyInt hi, PyInt start, PyInt end) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4562 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4563 PyInt size; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4564 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4565 if (CheckBuffer(self)) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4566 return NULL; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4567 |
4387 | 4568 if (end == -1) |
4569 end = self->buf->b_ml.ml_line_count; | |
4570 | |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4571 size = end - start + 1; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4572 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4573 if (lo < 0) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4574 lo = 0; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4575 else if (lo > size) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4576 lo = size; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4577 if (hi < 0) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4578 hi = 0; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4579 if (hi < lo) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4580 hi = lo; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4581 else if (hi > size) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4582 hi = size; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4583 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4584 return GetBufferLineList(self->buf, lo+start, hi+start); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4585 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4586 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4587 static PyInt |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
4588 RBAsItem( |
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
4589 BufferObject *self, |
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
4590 PyInt n, |
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
4591 PyObject *valObject, |
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
4592 PyInt start, |
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
4593 PyInt end, |
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
4594 PyInt *new_end) |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4595 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4596 PyInt len_change; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4597 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4598 if (CheckBuffer(self)) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4599 return -1; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4600 |
4387 | 4601 if (end == -1) |
4602 end = self->buf->b_ml.ml_line_count; | |
4603 | |
4389 | 4604 if (n < 0) |
4605 n += end - start + 1; | |
4606 | |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4607 if (n < 0 || n > end - start) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4608 { |
4978
f4969f8f66e9
updated for version 7.3.1234
Bram Moolenaar <bram@vim.org>
parents:
4976
diff
changeset
|
4609 PyErr_SET_STRING(PyExc_IndexError, N_("line number out of range")); |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4610 return -1; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4611 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4612 |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
4613 if (SetBufferLine(self->buf, n+start, valObject, &len_change) == FAIL) |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4614 return -1; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4615 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4616 if (new_end) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4617 *new_end = end + len_change; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4618 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4619 return 0; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4620 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4621 |
2894 | 4622 static PyInt |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
4623 RBAsSlice( |
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
4624 BufferObject *self, |
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
4625 PyInt lo, |
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
4626 PyInt hi, |
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
4627 PyObject *valObject, |
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
4628 PyInt start, |
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
4629 PyInt end, |
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
4630 PyInt *new_end) |
2894 | 4631 { |
4632 PyInt size; | |
4633 PyInt len_change; | |
4634 | |
4635 /* Self must be a valid buffer */ | |
4636 if (CheckBuffer(self)) | |
4637 return -1; | |
4638 | |
4387 | 4639 if (end == -1) |
4640 end = self->buf->b_ml.ml_line_count; | |
4641 | |
2894 | 4642 /* Sort out the slice range */ |
4643 size = end - start + 1; | |
4644 | |
4645 if (lo < 0) | |
4646 lo = 0; | |
4647 else if (lo > size) | |
4648 lo = size; | |
4649 if (hi < 0) | |
4650 hi = 0; | |
4651 if (hi < lo) | |
4652 hi = lo; | |
4653 else if (hi > size) | |
4654 hi = size; | |
4655 | |
4656 if (SetBufferLineList(self->buf, lo + start, hi + start, | |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
4657 valObject, &len_change) == FAIL) |
2894 | 4658 return -1; |
4659 | |
4660 if (new_end) | |
4661 *new_end = end + len_change; | |
4662 | |
4663 return 0; | |
4664 } | |
4665 | |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4666 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4667 static PyObject * |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
4668 RBAppend( |
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
4669 BufferObject *self, |
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
4670 PyObject *args, |
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
4671 PyInt start, |
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
4672 PyInt end, |
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
4673 PyInt *new_end) |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4674 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4675 PyObject *lines; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4676 PyInt len_change; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4677 PyInt max; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4678 PyInt n; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4679 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4680 if (CheckBuffer(self)) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4681 return NULL; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4682 |
4387 | 4683 if (end == -1) |
4684 end = self->buf->b_ml.ml_line_count; | |
4685 | |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4686 max = n = end - start + 1; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4687 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4688 if (!PyArg_ParseTuple(args, "O|n", &lines, &n)) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4689 return NULL; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4690 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4691 if (n < 0 || n > max) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4692 { |
4978
f4969f8f66e9
updated for version 7.3.1234
Bram Moolenaar <bram@vim.org>
parents:
4976
diff
changeset
|
4693 PyErr_SET_STRING(PyExc_IndexError, N_("line number out of range")); |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4694 return NULL; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4695 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4696 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4697 if (InsertBufferLines(self->buf, n + start - 1, lines, &len_change) == FAIL) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4698 return NULL; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4699 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4700 if (new_end) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4701 *new_end = end + len_change; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4702 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4703 Py_INCREF(Py_None); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4704 return Py_None; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4705 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4706 |
4385 | 4707 /* Range object |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4708 */ |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4709 |
4319 | 4710 static PyTypeObject RangeType; |
4385 | 4711 static PySequenceMethods RangeAsSeq; |
4712 static PyMappingMethods RangeAsMapping; | |
4319 | 4713 |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4714 typedef struct |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4715 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4716 PyObject_HEAD |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4717 BufferObject *buf; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4718 PyInt start; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4719 PyInt end; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4720 } RangeObject; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4721 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4722 static PyObject * |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4723 RangeNew(buf_T *buf, PyInt start, PyInt end) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4724 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4725 BufferObject *bufr; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4726 RangeObject *self; |
4500 | 4727 self = PyObject_GC_New(RangeObject, &RangeType); |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4728 if (self == NULL) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4729 return NULL; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4730 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4731 bufr = (BufferObject *)BufferNew(buf); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4732 if (bufr == NULL) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4733 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4734 Py_DECREF(self); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4735 return NULL; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4736 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4737 Py_INCREF(bufr); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4738 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4739 self->buf = bufr; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4740 self->start = start; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4741 self->end = end; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4742 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4743 return (PyObject *)(self); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4744 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4745 |
4319 | 4746 static void |
4488 | 4747 RangeDestructor(RangeObject *self) |
4319 | 4748 { |
4500 | 4749 PyObject_GC_UnTrack((void *)(self)); |
4498 | 4750 Py_XDECREF(self->buf); |
4500 | 4751 PyObject_GC_Del((void *)(self)); |
4752 } | |
4753 | |
4754 static int | |
4755 RangeTraverse(RangeObject *self, visitproc visit, void *arg) | |
4756 { | |
4757 Py_VISIT(((PyObject *)(self->buf))); | |
4758 return 0; | |
4759 } | |
4760 | |
4761 static int | |
4762 RangeClear(RangeObject *self) | |
4763 { | |
4764 Py_CLEAR(self->buf); | |
4765 return 0; | |
4319 | 4766 } |
4767 | |
4385 | 4768 static PyInt |
4488 | 4769 RangeLength(RangeObject *self) |
4385 | 4770 { |
4771 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */ | |
4488 | 4772 if (CheckBuffer(self->buf)) |
4385 | 4773 return -1; /* ??? */ |
4774 | |
4488 | 4775 return (self->end - self->start + 1); |
4385 | 4776 } |
4777 | |
4778 static PyObject * | |
4488 | 4779 RangeItem(RangeObject *self, PyInt n) |
4385 | 4780 { |
4488 | 4781 return RBItem(self->buf, n, self->start, self->end); |
4385 | 4782 } |
4783 | |
4784 static PyObject * | |
4488 | 4785 RangeSlice(RangeObject *self, PyInt lo, PyInt hi) |
4385 | 4786 { |
4488 | 4787 return RBSlice(self->buf, lo, hi, self->start, self->end); |
4385 | 4788 } |
4789 | |
4599
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
4790 static char *RangeAttrs[] = { |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
4791 "start", "end", |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
4792 NULL |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
4793 }; |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
4794 |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
4795 static PyObject * |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
4796 RangeDir(PyObject *self) |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
4797 { |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
4798 return ObjectDir(self, RangeAttrs); |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
4799 } |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
4800 |
4385 | 4801 static PyObject * |
4488 | 4802 RangeAppend(RangeObject *self, PyObject *args) |
4385 | 4803 { |
4488 | 4804 return RBAppend(self->buf, args, self->start, self->end, &self->end); |
4385 | 4805 } |
4806 | |
4807 static PyObject * | |
4488 | 4808 RangeRepr(RangeObject *self) |
4385 | 4809 { |
4488 | 4810 if (self->buf->buf == INVALID_BUFFER_VALUE) |
4623
548b889fe3cf
updated for version 7.3.1059
Bram Moolenaar <bram@vim.org>
parents:
4619
diff
changeset
|
4811 return PyString_FromFormat("<range object (for deleted buffer) at %p>", |
548b889fe3cf
updated for version 7.3.1059
Bram Moolenaar <bram@vim.org>
parents:
4619
diff
changeset
|
4812 (self)); |
4385 | 4813 else |
4814 { | |
4488 | 4815 char *name = (char *)self->buf->buf->b_fname; |
4385 | 4816 |
4817 if (name == NULL) | |
4818 name = ""; | |
4623
548b889fe3cf
updated for version 7.3.1059
Bram Moolenaar <bram@vim.org>
parents:
4619
diff
changeset
|
4819 |
548b889fe3cf
updated for version 7.3.1059
Bram Moolenaar <bram@vim.org>
parents:
4619
diff
changeset
|
4820 return PyString_FromFormat("<range %s (%d:%d)>", |
4653
b943fd24c351
updated for version 7.3.1074
Bram Moolenaar <bram@vim.org>
parents:
4645
diff
changeset
|
4821 name, (int)self->start, (int)self->end); |
4385 | 4822 } |
4823 } | |
4824 | |
4825 static struct PyMethodDef RangeMethods[] = { | |
4492 | 4826 /* name, function, calling, documentation */ |
4827 {"append", (PyCFunction)RangeAppend, METH_VARARGS, "Append data to the Vim range" }, | |
4599
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
4828 {"__dir__", (PyCFunction)RangeDir, METH_NOARGS, ""}, |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
4829 { NULL, NULL, 0, NULL} |
4385 | 4830 }; |
4831 | |
4319 | 4832 static PyTypeObject BufferType; |
4833 static PySequenceMethods BufferAsSeq; | |
4834 static PyMappingMethods BufferAsMapping; | |
4835 | |
4836 static PyObject * | |
4377 | 4837 BufferNew(buf_T *buf) |
4838 { | |
4839 /* We need to handle deletion of buffers underneath us. | |
4840 * If we add a "b_python*_ref" field to the buf_T structure, | |
4841 * then we can get at it in buf_freeall() in vim. We then | |
4842 * need to create only ONE Python object per buffer - if | |
4843 * we try to create a second, just INCREF the existing one | |
4844 * and return it. The (single) Python object referring to | |
4845 * the buffer is stored in "b_python*_ref". | |
4846 * Question: what to do on a buf_freeall(). We'll probably | |
4847 * have to either delete the Python object (DECREF it to | |
4848 * zero - a bad idea, as it leaves dangling refs!) or | |
4849 * set the buf_T * value to an invalid value (-1?), which | |
4850 * means we need checks in all access functions... Bah. | |
4851 * | |
4385 | 4852 * Python2 and Python3 get different fields and different objects: |
4377 | 4853 * b_python_ref and b_python3_ref fields respectively. |
4854 */ | |
4855 | |
4856 BufferObject *self; | |
4857 | |
4858 if (BUF_PYTHON_REF(buf) != NULL) | |
4859 { | |
4860 self = BUF_PYTHON_REF(buf); | |
4861 Py_INCREF(self); | |
4862 } | |
4863 else | |
4864 { | |
4865 self = PyObject_NEW(BufferObject, &BufferType); | |
4866 if (self == NULL) | |
4867 return NULL; | |
4868 self->buf = buf; | |
4869 BUF_PYTHON_REF(buf) = self; | |
4870 } | |
4871 | |
4872 return (PyObject *)(self); | |
4873 } | |
4874 | |
4385 | 4875 static void |
4488 | 4876 BufferDestructor(BufferObject *self) |
4319 | 4877 { |
4488 | 4878 if (self->buf && self->buf != INVALID_BUFFER_VALUE) |
4879 BUF_PYTHON_REF(self->buf) = NULL; | |
4385 | 4880 |
4881 DESTRUCTOR_FINISH(self); | |
4319 | 4882 } |
4883 | |
4377 | 4884 static PyInt |
4488 | 4885 BufferLength(BufferObject *self) |
4377 | 4886 { |
4887 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */ | |
4488 | 4888 if (CheckBuffer(self)) |
4377 | 4889 return -1; /* ??? */ |
4890 | |
4488 | 4891 return (PyInt)(self->buf->b_ml.ml_line_count); |
4377 | 4892 } |
4893 | |
4894 static PyObject * | |
4488 | 4895 BufferItem(BufferObject *self, PyInt n) |
4377 | 4896 { |
4488 | 4897 return RBItem(self, n, 1, -1); |
4377 | 4898 } |
4899 | |
4900 static PyObject * | |
4488 | 4901 BufferSlice(BufferObject *self, PyInt lo, PyInt hi) |
4377 | 4902 { |
4488 | 4903 return RBSlice(self, lo, hi, 1, -1); |
4377 | 4904 } |
4905 | |
4599
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
4906 static char *BufferAttrs[] = { |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
4907 "name", "number", "vars", "options", "valid", |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
4908 NULL |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
4909 }; |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
4910 |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
4911 static PyObject * |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
4912 BufferDir(PyObject *self) |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
4913 { |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
4914 return ObjectDir(self, BufferAttrs); |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
4915 } |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
4916 |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4917 static PyObject * |
4593
0cf552b325b5
updated for version 7.3.1044
Bram Moolenaar <bram@vim.org>
parents:
4589
diff
changeset
|
4918 BufferAttrValid(BufferObject *self, char *name) |
0cf552b325b5
updated for version 7.3.1044
Bram Moolenaar <bram@vim.org>
parents:
4589
diff
changeset
|
4919 { |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
4920 PyObject *ret; |
4593
0cf552b325b5
updated for version 7.3.1044
Bram Moolenaar <bram@vim.org>
parents:
4589
diff
changeset
|
4921 |
0cf552b325b5
updated for version 7.3.1044
Bram Moolenaar <bram@vim.org>
parents:
4589
diff
changeset
|
4922 if (strcmp(name, "valid") != 0) |
0cf552b325b5
updated for version 7.3.1044
Bram Moolenaar <bram@vim.org>
parents:
4589
diff
changeset
|
4923 return NULL; |
0cf552b325b5
updated for version 7.3.1044
Bram Moolenaar <bram@vim.org>
parents:
4589
diff
changeset
|
4924 |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
4925 ret = ((self->buf == INVALID_BUFFER_VALUE) ? Py_False : Py_True); |
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
4926 Py_INCREF(ret); |
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
4927 return ret; |
4593
0cf552b325b5
updated for version 7.3.1044
Bram Moolenaar <bram@vim.org>
parents:
4589
diff
changeset
|
4928 } |
0cf552b325b5
updated for version 7.3.1044
Bram Moolenaar <bram@vim.org>
parents:
4589
diff
changeset
|
4929 |
0cf552b325b5
updated for version 7.3.1044
Bram Moolenaar <bram@vim.org>
parents:
4589
diff
changeset
|
4930 static PyObject * |
4488 | 4931 BufferAttr(BufferObject *self, char *name) |
4385 | 4932 { |
4933 if (strcmp(name, "name") == 0) | |
4597
00eecb2f8e3e
updated for version 7.3.1046
Bram Moolenaar <bram@vim.org>
parents:
4595
diff
changeset
|
4934 return PyString_FromString((self->buf->b_ffname == NULL |
5088
34c629c3b4ba
updated for version 7.3.1287
Bram Moolenaar <bram@vim.org>
parents:
4995
diff
changeset
|
4935 ? "" : (char *)self->buf->b_ffname)); |
4385 | 4936 else if (strcmp(name, "number") == 0) |
4488 | 4937 return Py_BuildValue(Py_ssize_t_fmt, self->buf->b_fnum); |
4385 | 4938 else if (strcmp(name, "vars") == 0) |
4627
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
4939 return NEW_DICTIONARY(self->buf->b_vars); |
4385 | 4940 else if (strcmp(name, "options") == 0) |
4488 | 4941 return OptionsNew(SREQ_BUF, self->buf, (checkfun) CheckBuffer, |
4942 (PyObject *) self); | |
4599
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
4943 else if (strcmp(name, "__members__") == 0) |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
4944 return ObjectDir(NULL, BufferAttrs); |
4385 | 4945 else |
4946 return NULL; | |
4947 } | |
4948 | |
4589
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
4949 static int |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
4950 BufferSetattr(BufferObject *self, char *name, PyObject *valObject) |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
4951 { |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
4952 if (CheckBuffer(self)) |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
4953 return -1; |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
4954 |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
4955 if (strcmp(name, "name") == 0) |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
4956 { |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
4957 char_u *val; |
4589
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
4958 aco_save_T aco; |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
4959 int ren_ret; |
4589
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
4960 PyObject *todecref; |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
4961 |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
4962 if (!(val = StringToChars(valObject, &todecref))) |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
4963 return -1; |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
4964 |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
4965 VimTryStart(); |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
4966 /* Using aucmd_*: autocommands will be executed by rename_buffer */ |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
4967 aucmd_prepbuf(&aco, self->buf); |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
4968 ren_ret = rename_buffer(val); |
4589
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
4969 aucmd_restbuf(&aco); |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
4970 Py_XDECREF(todecref); |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
4971 if (VimTryEnd()) |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
4972 return -1; |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
4973 |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
4974 if (ren_ret == FAIL) |
4589
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
4975 { |
4978
f4969f8f66e9
updated for version 7.3.1234
Bram Moolenaar <bram@vim.org>
parents:
4976
diff
changeset
|
4976 PyErr_SET_VIM(N_("failed to rename buffer")); |
4589
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
4977 return -1; |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
4978 } |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
4979 return 0; |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
4980 } |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
4981 else |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
4982 { |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
4983 PyErr_SetString(PyExc_AttributeError, name); |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
4984 return -1; |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
4985 } |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
4986 } |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
4987 |
4385 | 4988 static PyObject * |
4488 | 4989 BufferAppend(BufferObject *self, PyObject *args) |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4990 { |
4488 | 4991 return RBAppend(self, args, 1, -1, NULL); |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4992 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4993 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4994 static PyObject * |
4964
5cee875f3096
updated for version 7.3.1227
Bram Moolenaar <bram@vim.org>
parents:
4962
diff
changeset
|
4995 BufferMark(BufferObject *self, PyObject *pmarkObject) |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4996 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
4997 pos_T *posp; |
4964
5cee875f3096
updated for version 7.3.1227
Bram Moolenaar <bram@vim.org>
parents:
4962
diff
changeset
|
4998 char_u *pmark; |
5cee875f3096
updated for version 7.3.1227
Bram Moolenaar <bram@vim.org>
parents:
4962
diff
changeset
|
4999 char_u mark; |
4429 | 5000 buf_T *savebuf; |
4964
5cee875f3096
updated for version 7.3.1227
Bram Moolenaar <bram@vim.org>
parents:
4962
diff
changeset
|
5001 PyObject *todecref; |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
5002 |
4488 | 5003 if (CheckBuffer(self)) |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
5004 return NULL; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
5005 |
4964
5cee875f3096
updated for version 7.3.1227
Bram Moolenaar <bram@vim.org>
parents:
4962
diff
changeset
|
5006 if (!(pmark = StringToChars(pmarkObject, &todecref))) |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
5007 return NULL; |
4633
3857d399ab41
updated for version 7.3.1064
Bram Moolenaar <bram@vim.org>
parents:
4631
diff
changeset
|
5008 |
4964
5cee875f3096
updated for version 7.3.1227
Bram Moolenaar <bram@vim.org>
parents:
4962
diff
changeset
|
5009 if (pmark[0] == '\0' || pmark[1] != '\0') |
4633
3857d399ab41
updated for version 7.3.1064
Bram Moolenaar <bram@vim.org>
parents:
4631
diff
changeset
|
5010 { |
4968
b6e693e1f946
updated for version 7.3.1229
Bram Moolenaar <bram@vim.org>
parents:
4966
diff
changeset
|
5011 PyErr_SET_STRING(PyExc_ValueError, |
4978
f4969f8f66e9
updated for version 7.3.1234
Bram Moolenaar <bram@vim.org>
parents:
4976
diff
changeset
|
5012 N_("mark name must be a single character")); |
4976
4ed713442c51
updated for version 7.3.1233
Bram Moolenaar <bram@vim.org>
parents:
4974
diff
changeset
|
5013 Py_XDECREF(todecref); |
4633
3857d399ab41
updated for version 7.3.1064
Bram Moolenaar <bram@vim.org>
parents:
4631
diff
changeset
|
5014 return NULL; |
3857d399ab41
updated for version 7.3.1064
Bram Moolenaar <bram@vim.org>
parents:
4631
diff
changeset
|
5015 } |
3857d399ab41
updated for version 7.3.1064
Bram Moolenaar <bram@vim.org>
parents:
4631
diff
changeset
|
5016 |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
5017 mark = *pmark; |
4964
5cee875f3096
updated for version 7.3.1227
Bram Moolenaar <bram@vim.org>
parents:
4962
diff
changeset
|
5018 |
5cee875f3096
updated for version 7.3.1227
Bram Moolenaar <bram@vim.org>
parents:
4962
diff
changeset
|
5019 Py_XDECREF(todecref); |
5cee875f3096
updated for version 7.3.1227
Bram Moolenaar <bram@vim.org>
parents:
4962
diff
changeset
|
5020 |
4498 | 5021 VimTryStart(); |
4488 | 5022 switch_buffer(&savebuf, self->buf); |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
5023 posp = getmark(mark, FALSE); |
4429 | 5024 restore_buffer(savebuf); |
4498 | 5025 if (VimTryEnd()) |
5026 return NULL; | |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
5027 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
5028 if (posp == NULL) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
5029 { |
4978
f4969f8f66e9
updated for version 7.3.1234
Bram Moolenaar <bram@vim.org>
parents:
4976
diff
changeset
|
5030 PyErr_SET_VIM(N_("invalid mark name")); |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
5031 return NULL; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
5032 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
5033 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
5034 if (posp->lnum <= 0) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
5035 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
5036 /* Or raise an error? */ |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
5037 Py_INCREF(Py_None); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
5038 return Py_None; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
5039 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
5040 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
5041 return Py_BuildValue("(ll)", (long)(posp->lnum), (long)(posp->col)); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
5042 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
5043 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
5044 static PyObject * |
4488 | 5045 BufferRange(BufferObject *self, PyObject *args) |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
5046 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
5047 PyInt start; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
5048 PyInt end; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
5049 |
4488 | 5050 if (CheckBuffer(self)) |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
5051 return NULL; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
5052 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
5053 if (!PyArg_ParseTuple(args, "nn", &start, &end)) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
5054 return NULL; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
5055 |
4488 | 5056 return RangeNew(self->buf, start, end); |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
5057 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
5058 |
4319 | 5059 static PyObject * |
4488 | 5060 BufferRepr(BufferObject *self) |
4319 | 5061 { |
4488 | 5062 if (self->buf == INVALID_BUFFER_VALUE) |
4623
548b889fe3cf
updated for version 7.3.1059
Bram Moolenaar <bram@vim.org>
parents:
4619
diff
changeset
|
5063 return PyString_FromFormat("<buffer object (deleted) at %p>", self); |
4319 | 5064 else |
5065 { | |
4623
548b889fe3cf
updated for version 7.3.1059
Bram Moolenaar <bram@vim.org>
parents:
4619
diff
changeset
|
5066 char *name = (char *)self->buf->b_fname; |
4319 | 5067 |
5068 if (name == NULL) | |
5069 name = ""; | |
4623
548b889fe3cf
updated for version 7.3.1059
Bram Moolenaar <bram@vim.org>
parents:
4619
diff
changeset
|
5070 |
548b889fe3cf
updated for version 7.3.1059
Bram Moolenaar <bram@vim.org>
parents:
4619
diff
changeset
|
5071 return PyString_FromFormat("<buffer %s>", name); |
4319 | 5072 } |
5073 } | |
5074 | |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
5075 static struct PyMethodDef BufferMethods[] = { |
4492 | 5076 /* name, function, calling, documentation */ |
5077 {"append", (PyCFunction)BufferAppend, METH_VARARGS, "Append data to Vim buffer" }, | |
4964
5cee875f3096
updated for version 7.3.1227
Bram Moolenaar <bram@vim.org>
parents:
4962
diff
changeset
|
5078 {"mark", (PyCFunction)BufferMark, METH_O, "Return (row,col) representing position of named mark" }, |
4492 | 5079 {"range", (PyCFunction)BufferRange, METH_VARARGS, "Return a range object which represents the part of the given buffer between line numbers s and e" }, |
4599
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
5080 {"__dir__", (PyCFunction)BufferDir, METH_NOARGS, ""}, |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
5081 { NULL, NULL, 0, NULL} |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
5082 }; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
5083 |
4397 | 5084 /* |
5085 * Buffer list object - Implementation | |
5086 */ | |
5087 | |
5088 static PyTypeObject BufMapType; | |
5089 | |
5090 typedef struct | |
5091 { | |
5092 PyObject_HEAD | |
5093 } BufMapObject; | |
5094 | |
5095 static PyInt | |
5096 BufMapLength(PyObject *self UNUSED) | |
5097 { | |
5098 buf_T *b = firstbuf; | |
5099 PyInt n = 0; | |
5100 | |
5101 while (b) | |
5102 { | |
5103 ++n; | |
5104 b = b->b_next; | |
5105 } | |
5106 | |
5107 return n; | |
5108 } | |
5109 | |
5110 static PyObject * | |
5111 BufMapItem(PyObject *self UNUSED, PyObject *keyObject) | |
5112 { | |
5113 buf_T *b; | |
4972
537bbfff0c5c
updated for version 7.3.1231
Bram Moolenaar <bram@vim.org>
parents:
4970
diff
changeset
|
5114 long bnr; |
537bbfff0c5c
updated for version 7.3.1231
Bram Moolenaar <bram@vim.org>
parents:
4970
diff
changeset
|
5115 |
537bbfff0c5c
updated for version 7.3.1231
Bram Moolenaar <bram@vim.org>
parents:
4970
diff
changeset
|
5116 if (NumberToLong(keyObject, &bnr, NUMBER_INT|NUMBER_NATURAL)) |
4397 | 5117 return NULL; |
4972
537bbfff0c5c
updated for version 7.3.1231
Bram Moolenaar <bram@vim.org>
parents:
4970
diff
changeset
|
5118 |
537bbfff0c5c
updated for version 7.3.1231
Bram Moolenaar <bram@vim.org>
parents:
4970
diff
changeset
|
5119 b = buflist_findnr((int) bnr); |
4397 | 5120 |
5121 if (b) | |
5122 return BufferNew(b); | |
5123 else | |
5124 { | |
4403 | 5125 PyErr_SetObject(PyExc_KeyError, keyObject); |
4397 | 5126 return NULL; |
5127 } | |
5128 } | |
5129 | |
5130 static void | |
5131 BufMapIterDestruct(PyObject *buffer) | |
5132 { | |
5133 /* Iteration was stopped before all buffers were processed */ | |
5134 if (buffer) | |
5135 { | |
5136 Py_DECREF(buffer); | |
5137 } | |
5138 } | |
5139 | |
4433 | 5140 static int |
5141 BufMapIterTraverse(PyObject *buffer, visitproc visit, void *arg) | |
5142 { | |
4500 | 5143 if (buffer) |
5144 Py_VISIT(buffer); | |
4433 | 5145 return 0; |
5146 } | |
5147 | |
5148 static int | |
5149 BufMapIterClear(PyObject **buffer) | |
5150 { | |
4500 | 5151 if (*buffer) |
5152 Py_CLEAR(*buffer); | |
4433 | 5153 return 0; |
5154 } | |
5155 | |
4397 | 5156 static PyObject * |
5157 BufMapIterNext(PyObject **buffer) | |
5158 { | |
5159 PyObject *next; | |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
5160 PyObject *ret; |
4397 | 5161 |
5162 if (!*buffer) | |
5163 return NULL; | |
5164 | |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
5165 ret = *buffer; |
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
5166 |
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
5167 if (CheckBuffer((BufferObject *)(ret))) |
4397 | 5168 { |
5169 *buffer = NULL; | |
5170 return NULL; | |
5171 } | |
5172 | |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
5173 if (!((BufferObject *)(ret))->buf->b_next) |
4397 | 5174 next = NULL; |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
5175 else if (!(next = BufferNew(((BufferObject *)(ret))->buf->b_next))) |
4397 | 5176 return NULL; |
5177 *buffer = next; | |
4438 | 5178 /* Do not increment reference: we no longer hold it (decref), but whoever |
5179 * on other side will hold (incref). Decref+incref = nothing. */ | |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
5180 return ret; |
4397 | 5181 } |
5182 | |
5183 static PyObject * | |
5184 BufMapIter(PyObject *self UNUSED) | |
5185 { | |
5186 PyObject *buffer; | |
5187 | |
5188 buffer = BufferNew(firstbuf); | |
5189 return IterNew(buffer, | |
4433 | 5190 (destructorfun) BufMapIterDestruct, (nextfun) BufMapIterNext, |
5191 (traversefun) BufMapIterTraverse, (clearfun) BufMapIterClear); | |
4397 | 5192 } |
5193 | |
5194 static PyMappingMethods BufMapAsMapping = { | |
5195 (lenfunc) BufMapLength, | |
5196 (binaryfunc) BufMapItem, | |
5197 (objobjargproc) 0, | |
5198 }; | |
5199 | |
4385 | 5200 /* Current items object |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
5201 */ |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
5202 |
4599
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
5203 static char *CurrentAttrs[] = { |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
5204 "buffer", "window", "line", "range", "tabpage", |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
5205 NULL |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
5206 }; |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
5207 |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
5208 static PyObject * |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
5209 CurrentDir(PyObject *self) |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
5210 { |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
5211 return ObjectDir(self, CurrentAttrs); |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
5212 } |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
5213 |
4319 | 5214 static PyObject * |
5215 CurrentGetattr(PyObject *self UNUSED, char *name) | |
5216 { | |
5217 if (strcmp(name, "buffer") == 0) | |
5218 return (PyObject *)BufferNew(curbuf); | |
5219 else if (strcmp(name, "window") == 0) | |
4431 | 5220 return (PyObject *)WindowNew(curwin, curtab); |
4401 | 5221 else if (strcmp(name, "tabpage") == 0) |
5222 return (PyObject *)TabPageNew(curtab); | |
4319 | 5223 else if (strcmp(name, "line") == 0) |
5224 return GetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum); | |
5225 else if (strcmp(name, "range") == 0) | |
5226 return RangeNew(curbuf, RangeStart, RangeEnd); | |
4599
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
5227 else if (strcmp(name, "__members__") == 0) |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
5228 return ObjectDir(NULL, CurrentAttrs); |
4319 | 5229 else |
4599
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
5230 #if PY_MAJOR_VERSION < 3 |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
5231 return Py_FindMethod(WindowMethods, self, name); |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
5232 #else |
4319 | 5233 return NULL; |
4599
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
5234 #endif |
4319 | 5235 } |
5236 | |
5237 static int | |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
5238 CurrentSetattr(PyObject *self UNUSED, char *name, PyObject *valObject) |
4319 | 5239 { |
5240 if (strcmp(name, "line") == 0) | |
5241 { | |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
5242 if (SetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum, valObject, |
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
5243 NULL) == FAIL) |
4319 | 5244 return -1; |
5245 | |
5246 return 0; | |
5247 } | |
4407 | 5248 else if (strcmp(name, "buffer") == 0) |
5249 { | |
5250 int count; | |
5251 | |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
5252 if (valObject->ob_type != &BufferType) |
4407 | 5253 { |
4970
f5c822e5a0eb
updated for version 7.3.1230
Bram Moolenaar <bram@vim.org>
parents:
4968
diff
changeset
|
5254 PyErr_FORMAT(PyExc_TypeError, |
4978
f4969f8f66e9
updated for version 7.3.1234
Bram Moolenaar <bram@vim.org>
parents:
4976
diff
changeset
|
5255 N_("expected vim.Buffer object, but got %s"), |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
5256 Py_TYPE_NAME(valObject)); |
4407 | 5257 return -1; |
5258 } | |
5259 | |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
5260 if (CheckBuffer((BufferObject *)(valObject))) |
4407 | 5261 return -1; |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
5262 count = ((BufferObject *)(valObject))->buf->b_fnum; |
4407 | 5263 |
4498 | 5264 VimTryStart(); |
4407 | 5265 if (do_buffer(DOBUF_GOTO, DOBUF_FIRST, FORWARD, count, 0) == FAIL) |
5266 { | |
4498 | 5267 if (VimTryEnd()) |
5268 return -1; | |
4978
f4969f8f66e9
updated for version 7.3.1234
Bram Moolenaar <bram@vim.org>
parents:
4976
diff
changeset
|
5269 PyErr_VIM_FORMAT(N_("failed to switch to buffer %d"), count); |
4407 | 5270 return -1; |
5271 } | |
5272 | |
4498 | 5273 return VimTryEnd(); |
4407 | 5274 } |
5275 else if (strcmp(name, "window") == 0) | |
5276 { | |
5277 int count; | |
5278 | |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
5279 if (valObject->ob_type != &WindowType) |
4407 | 5280 { |
4970
f5c822e5a0eb
updated for version 7.3.1230
Bram Moolenaar <bram@vim.org>
parents:
4968
diff
changeset
|
5281 PyErr_FORMAT(PyExc_TypeError, |
4978
f4969f8f66e9
updated for version 7.3.1234
Bram Moolenaar <bram@vim.org>
parents:
4976
diff
changeset
|
5282 N_("expected vim.Window object, but got %s"), |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
5283 Py_TYPE_NAME(valObject)); |
4407 | 5284 return -1; |
5285 } | |
5286 | |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
5287 if (CheckWindow((WindowObject *)(valObject))) |
4407 | 5288 return -1; |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
5289 count = get_win_number(((WindowObject *)(valObject))->win, firstwin); |
4407 | 5290 |
5291 if (!count) | |
5292 { | |
4968
b6e693e1f946
updated for version 7.3.1229
Bram Moolenaar <bram@vim.org>
parents:
4966
diff
changeset
|
5293 PyErr_SET_STRING(PyExc_ValueError, |
4978
f4969f8f66e9
updated for version 7.3.1234
Bram Moolenaar <bram@vim.org>
parents:
4976
diff
changeset
|
5294 N_("failed to find window in the current tab page")); |
4407 | 5295 return -1; |
5296 } | |
5297 | |
4498 | 5298 VimTryStart(); |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
5299 win_goto(((WindowObject *)(valObject))->win); |
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
5300 if (((WindowObject *)(valObject))->win != curwin) |
4407 | 5301 { |
4498 | 5302 if (VimTryEnd()) |
5303 return -1; | |
4968
b6e693e1f946
updated for version 7.3.1229
Bram Moolenaar <bram@vim.org>
parents:
4966
diff
changeset
|
5304 PyErr_SET_STRING(PyExc_RuntimeError, |
4978
f4969f8f66e9
updated for version 7.3.1234
Bram Moolenaar <bram@vim.org>
parents:
4976
diff
changeset
|
5305 N_("did not switch to the specified window")); |
4407 | 5306 return -1; |
5307 } | |
5308 | |
4498 | 5309 return VimTryEnd(); |
4407 | 5310 } |
5311 else if (strcmp(name, "tabpage") == 0) | |
5312 { | |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
5313 if (valObject->ob_type != &TabPageType) |
4407 | 5314 { |
4970
f5c822e5a0eb
updated for version 7.3.1230
Bram Moolenaar <bram@vim.org>
parents:
4968
diff
changeset
|
5315 PyErr_FORMAT(PyExc_TypeError, |
4978
f4969f8f66e9
updated for version 7.3.1234
Bram Moolenaar <bram@vim.org>
parents:
4976
diff
changeset
|
5316 N_("expected vim.TabPage object, but got %s"), |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
5317 Py_TYPE_NAME(valObject)); |
4407 | 5318 return -1; |
5319 } | |
5320 | |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
5321 if (CheckTabPage((TabPageObject *)(valObject))) |
4407 | 5322 return -1; |
5323 | |
4498 | 5324 VimTryStart(); |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
5325 goto_tabpage_tp(((TabPageObject *)(valObject))->tab, TRUE, TRUE); |
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
5326 if (((TabPageObject *)(valObject))->tab != curtab) |
4407 | 5327 { |
4498 | 5328 if (VimTryEnd()) |
5329 return -1; | |
4968
b6e693e1f946
updated for version 7.3.1229
Bram Moolenaar <bram@vim.org>
parents:
4966
diff
changeset
|
5330 PyErr_SET_STRING(PyExc_RuntimeError, |
4978
f4969f8f66e9
updated for version 7.3.1234
Bram Moolenaar <bram@vim.org>
parents:
4976
diff
changeset
|
5331 N_("did not switch to the specified tab page")); |
4407 | 5332 return -1; |
5333 } | |
5334 | |
4498 | 5335 return VimTryEnd(); |
4407 | 5336 } |
4319 | 5337 else |
5338 { | |
5339 PyErr_SetString(PyExc_AttributeError, name); | |
5340 return -1; | |
5341 } | |
5342 } | |
5343 | |
4599
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
5344 static struct PyMethodDef CurrentMethods[] = { |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
5345 /* name, function, calling, documentation */ |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
5346 {"__dir__", (PyCFunction)CurrentDir, METH_NOARGS, ""}, |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
5347 { NULL, NULL, 0, NULL} |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
5348 }; |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
5349 |
3618 | 5350 static void |
4486 | 5351 init_range_cmd(exarg_T *eap) |
5352 { | |
5353 RangeStart = eap->line1; | |
5354 RangeEnd = eap->line2; | |
5355 } | |
5356 | |
5357 static void | |
5358 init_range_eval(typval_T *rettv UNUSED) | |
5359 { | |
5360 RangeStart = (PyInt) curwin->w_cursor.lnum; | |
5361 RangeEnd = RangeStart; | |
5362 } | |
5363 | |
5364 static void | |
4511
ce94a870b59b
updated for version 7.3.1003
Bram Moolenaar <bram@vim.org>
parents:
4509
diff
changeset
|
5365 run_cmd(const char *cmd, void *arg UNUSED |
ce94a870b59b
updated for version 7.3.1003
Bram Moolenaar <bram@vim.org>
parents:
4509
diff
changeset
|
5366 #ifdef PY_CAN_RECURSE |
ce94a870b59b
updated for version 7.3.1003
Bram Moolenaar <bram@vim.org>
parents:
4509
diff
changeset
|
5367 , PyGILState_STATE *pygilstate UNUSED |
ce94a870b59b
updated for version 7.3.1003
Bram Moolenaar <bram@vim.org>
parents:
4509
diff
changeset
|
5368 #endif |
ce94a870b59b
updated for version 7.3.1003
Bram Moolenaar <bram@vim.org>
parents:
4509
diff
changeset
|
5369 ) |
4486 | 5370 { |
5088
34c629c3b4ba
updated for version 7.3.1287
Bram Moolenaar <bram@vim.org>
parents:
4995
diff
changeset
|
5371 PyObject *run_ret; |
34c629c3b4ba
updated for version 7.3.1287
Bram Moolenaar <bram@vim.org>
parents:
4995
diff
changeset
|
5372 run_ret = PyRun_String((char *)cmd, Py_file_input, globals, globals); |
34c629c3b4ba
updated for version 7.3.1287
Bram Moolenaar <bram@vim.org>
parents:
4995
diff
changeset
|
5373 if (run_ret != NULL) |
34c629c3b4ba
updated for version 7.3.1287
Bram Moolenaar <bram@vim.org>
parents:
4995
diff
changeset
|
5374 { |
34c629c3b4ba
updated for version 7.3.1287
Bram Moolenaar <bram@vim.org>
parents:
4995
diff
changeset
|
5375 Py_DECREF(run_ret); |
34c629c3b4ba
updated for version 7.3.1287
Bram Moolenaar <bram@vim.org>
parents:
4995
diff
changeset
|
5376 } |
34c629c3b4ba
updated for version 7.3.1287
Bram Moolenaar <bram@vim.org>
parents:
4995
diff
changeset
|
5377 else if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_SystemExit)) |
34c629c3b4ba
updated for version 7.3.1287
Bram Moolenaar <bram@vim.org>
parents:
4995
diff
changeset
|
5378 { |
34c629c3b4ba
updated for version 7.3.1287
Bram Moolenaar <bram@vim.org>
parents:
4995
diff
changeset
|
5379 EMSG2(_(e_py_systemexit), "python"); |
34c629c3b4ba
updated for version 7.3.1287
Bram Moolenaar <bram@vim.org>
parents:
4995
diff
changeset
|
5380 PyErr_Clear(); |
34c629c3b4ba
updated for version 7.3.1287
Bram Moolenaar <bram@vim.org>
parents:
4995
diff
changeset
|
5381 } |
34c629c3b4ba
updated for version 7.3.1287
Bram Moolenaar <bram@vim.org>
parents:
4995
diff
changeset
|
5382 else |
34c629c3b4ba
updated for version 7.3.1287
Bram Moolenaar <bram@vim.org>
parents:
4995
diff
changeset
|
5383 PyErr_PrintEx(1); |
4486 | 5384 } |
5385 | |
5386 static const char *code_hdr = "def " DOPY_FUNC "(line, linenr):\n "; | |
5387 static int code_hdr_len = 30; | |
5388 | |
5389 static void | |
4511
ce94a870b59b
updated for version 7.3.1003
Bram Moolenaar <bram@vim.org>
parents:
4509
diff
changeset
|
5390 run_do(const char *cmd, void *arg UNUSED |
ce94a870b59b
updated for version 7.3.1003
Bram Moolenaar <bram@vim.org>
parents:
4509
diff
changeset
|
5391 #ifdef PY_CAN_RECURSE |
ce94a870b59b
updated for version 7.3.1003
Bram Moolenaar <bram@vim.org>
parents:
4509
diff
changeset
|
5392 , PyGILState_STATE *pygilstate |
ce94a870b59b
updated for version 7.3.1003
Bram Moolenaar <bram@vim.org>
parents:
4509
diff
changeset
|
5393 #endif |
ce94a870b59b
updated for version 7.3.1003
Bram Moolenaar <bram@vim.org>
parents:
4509
diff
changeset
|
5394 ) |
4486 | 5395 { |
5396 PyInt lnum; | |
5397 size_t len; | |
5398 char *code; | |
5399 int status; | |
5400 PyObject *pyfunc, *pymain; | |
5088
34c629c3b4ba
updated for version 7.3.1287
Bram Moolenaar <bram@vim.org>
parents:
4995
diff
changeset
|
5401 PyObject *run_ret; |
4486 | 5402 |
4575
626e9ccb7c48
updated for version 7.3.1035
Bram Moolenaar <bram@vim.org>
parents:
4523
diff
changeset
|
5403 if (u_save((linenr_T)RangeStart - 1, (linenr_T)RangeEnd + 1) != OK) |
4486 | 5404 { |
5405 EMSG(_("cannot save undo information")); | |
5406 return; | |
5407 } | |
5408 | |
5409 len = code_hdr_len + STRLEN(cmd); | |
5410 code = PyMem_New(char, len + 1); | |
5411 memcpy(code, code_hdr, code_hdr_len); | |
5412 STRCPY(code + code_hdr_len, cmd); | |
5088
34c629c3b4ba
updated for version 7.3.1287
Bram Moolenaar <bram@vim.org>
parents:
4995
diff
changeset
|
5413 run_ret = PyRun_String(code, Py_file_input, globals, globals); |
34c629c3b4ba
updated for version 7.3.1287
Bram Moolenaar <bram@vim.org>
parents:
4995
diff
changeset
|
5414 status = -1; |
34c629c3b4ba
updated for version 7.3.1287
Bram Moolenaar <bram@vim.org>
parents:
4995
diff
changeset
|
5415 if (run_ret != NULL) |
34c629c3b4ba
updated for version 7.3.1287
Bram Moolenaar <bram@vim.org>
parents:
4995
diff
changeset
|
5416 { |
34c629c3b4ba
updated for version 7.3.1287
Bram Moolenaar <bram@vim.org>
parents:
4995
diff
changeset
|
5417 status = 0; |
34c629c3b4ba
updated for version 7.3.1287
Bram Moolenaar <bram@vim.org>
parents:
4995
diff
changeset
|
5418 Py_DECREF(run_ret); |
34c629c3b4ba
updated for version 7.3.1287
Bram Moolenaar <bram@vim.org>
parents:
4995
diff
changeset
|
5419 } |
34c629c3b4ba
updated for version 7.3.1287
Bram Moolenaar <bram@vim.org>
parents:
4995
diff
changeset
|
5420 else if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_SystemExit)) |
34c629c3b4ba
updated for version 7.3.1287
Bram Moolenaar <bram@vim.org>
parents:
4995
diff
changeset
|
5421 { |
34c629c3b4ba
updated for version 7.3.1287
Bram Moolenaar <bram@vim.org>
parents:
4995
diff
changeset
|
5422 PyMem_Free(code); |
34c629c3b4ba
updated for version 7.3.1287
Bram Moolenaar <bram@vim.org>
parents:
4995
diff
changeset
|
5423 EMSG2(_(e_py_systemexit), "python"); |
34c629c3b4ba
updated for version 7.3.1287
Bram Moolenaar <bram@vim.org>
parents:
4995
diff
changeset
|
5424 PyErr_Clear(); |
34c629c3b4ba
updated for version 7.3.1287
Bram Moolenaar <bram@vim.org>
parents:
4995
diff
changeset
|
5425 return; |
34c629c3b4ba
updated for version 7.3.1287
Bram Moolenaar <bram@vim.org>
parents:
4995
diff
changeset
|
5426 } |
34c629c3b4ba
updated for version 7.3.1287
Bram Moolenaar <bram@vim.org>
parents:
4995
diff
changeset
|
5427 else |
34c629c3b4ba
updated for version 7.3.1287
Bram Moolenaar <bram@vim.org>
parents:
4995
diff
changeset
|
5428 PyErr_PrintEx(1); |
34c629c3b4ba
updated for version 7.3.1287
Bram Moolenaar <bram@vim.org>
parents:
4995
diff
changeset
|
5429 |
4486 | 5430 PyMem_Free(code); |
5431 | |
5432 if (status) | |
5433 { | |
5434 EMSG(_("failed to run the code")); | |
5435 return; | |
5436 } | |
5437 | |
5438 status = 0; | |
5439 pymain = PyImport_AddModule("__main__"); | |
5440 pyfunc = PyObject_GetAttrString(pymain, DOPY_FUNC); | |
4511
ce94a870b59b
updated for version 7.3.1003
Bram Moolenaar <bram@vim.org>
parents:
4509
diff
changeset
|
5441 #ifdef PY_CAN_RECURSE |
4486 | 5442 PyGILState_Release(*pygilstate); |
4511
ce94a870b59b
updated for version 7.3.1003
Bram Moolenaar <bram@vim.org>
parents:
4509
diff
changeset
|
5443 #endif |
4486 | 5444 |
5445 for (lnum = RangeStart; lnum <= RangeEnd; ++lnum) | |
5446 { | |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
5447 PyObject *line; |
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
5448 PyObject *linenr; |
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
5449 PyObject *ret; |
4486 | 5450 |
4511
ce94a870b59b
updated for version 7.3.1003
Bram Moolenaar <bram@vim.org>
parents:
4509
diff
changeset
|
5451 #ifdef PY_CAN_RECURSE |
4486 | 5452 *pygilstate = PyGILState_Ensure(); |
4511
ce94a870b59b
updated for version 7.3.1003
Bram Moolenaar <bram@vim.org>
parents:
4509
diff
changeset
|
5453 #endif |
4486 | 5454 if (!(line = GetBufferLine(curbuf, lnum))) |
5455 goto err; | |
5456 if (!(linenr = PyInt_FromLong((long) lnum))) | |
5457 { | |
5458 Py_DECREF(line); | |
5459 goto err; | |
5460 } | |
5461 ret = PyObject_CallFunctionObjArgs(pyfunc, line, linenr, NULL); | |
5462 Py_DECREF(line); | |
5463 Py_DECREF(linenr); | |
5464 if (!ret) | |
5465 goto err; | |
5466 | |
5467 if (ret != Py_None) | |
5468 if (SetBufferLine(curbuf, lnum, ret, NULL) == FAIL) | |
5469 goto err; | |
5470 | |
5471 Py_XDECREF(ret); | |
5472 PythonIO_Flush(); | |
4511
ce94a870b59b
updated for version 7.3.1003
Bram Moolenaar <bram@vim.org>
parents:
4509
diff
changeset
|
5473 #ifdef PY_CAN_RECURSE |
4486 | 5474 PyGILState_Release(*pygilstate); |
4511
ce94a870b59b
updated for version 7.3.1003
Bram Moolenaar <bram@vim.org>
parents:
4509
diff
changeset
|
5475 #endif |
4486 | 5476 } |
5477 goto out; | |
5478 err: | |
4511
ce94a870b59b
updated for version 7.3.1003
Bram Moolenaar <bram@vim.org>
parents:
4509
diff
changeset
|
5479 #ifdef PY_CAN_RECURSE |
4486 | 5480 *pygilstate = PyGILState_Ensure(); |
4511
ce94a870b59b
updated for version 7.3.1003
Bram Moolenaar <bram@vim.org>
parents:
4509
diff
changeset
|
5481 #endif |
4486 | 5482 PyErr_PrintEx(0); |
5483 PythonIO_Flush(); | |
5484 status = 1; | |
5485 out: | |
4511
ce94a870b59b
updated for version 7.3.1003
Bram Moolenaar <bram@vim.org>
parents:
4509
diff
changeset
|
5486 #ifdef PY_CAN_RECURSE |
4486 | 5487 if (!status) |
5488 *pygilstate = PyGILState_Ensure(); | |
4511
ce94a870b59b
updated for version 7.3.1003
Bram Moolenaar <bram@vim.org>
parents:
4509
diff
changeset
|
5489 #endif |
4486 | 5490 Py_DECREF(pyfunc); |
5491 PyObject_SetAttrString(pymain, DOPY_FUNC, NULL); | |
5492 if (status) | |
5493 return; | |
5494 check_cursor(); | |
5495 update_curbuf(NOT_VALID); | |
5496 } | |
5497 | |
5498 static void | |
4511
ce94a870b59b
updated for version 7.3.1003
Bram Moolenaar <bram@vim.org>
parents:
4509
diff
changeset
|
5499 run_eval(const char *cmd, typval_T *rettv |
ce94a870b59b
updated for version 7.3.1003
Bram Moolenaar <bram@vim.org>
parents:
4509
diff
changeset
|
5500 #ifdef PY_CAN_RECURSE |
ce94a870b59b
updated for version 7.3.1003
Bram Moolenaar <bram@vim.org>
parents:
4509
diff
changeset
|
5501 , PyGILState_STATE *pygilstate UNUSED |
ce94a870b59b
updated for version 7.3.1003
Bram Moolenaar <bram@vim.org>
parents:
4509
diff
changeset
|
5502 #endif |
ce94a870b59b
updated for version 7.3.1003
Bram Moolenaar <bram@vim.org>
parents:
4509
diff
changeset
|
5503 ) |
4486 | 5504 { |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
5505 PyObject *run_ret; |
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
5506 |
5088
34c629c3b4ba
updated for version 7.3.1287
Bram Moolenaar <bram@vim.org>
parents:
4995
diff
changeset
|
5507 run_ret = PyRun_String((char *)cmd, Py_eval_input, globals, globals); |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
5508 if (run_ret == NULL) |
4486 | 5509 { |
5139
80bab8b1a30d
updated for version 7.3.1312
Bram Moolenaar <bram@vim.org>
parents:
5088
diff
changeset
|
5510 if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_SystemExit)) |
5088
34c629c3b4ba
updated for version 7.3.1287
Bram Moolenaar <bram@vim.org>
parents:
4995
diff
changeset
|
5511 { |
34c629c3b4ba
updated for version 7.3.1287
Bram Moolenaar <bram@vim.org>
parents:
4995
diff
changeset
|
5512 EMSG2(_(e_py_systemexit), "python"); |
34c629c3b4ba
updated for version 7.3.1287
Bram Moolenaar <bram@vim.org>
parents:
4995
diff
changeset
|
5513 PyErr_Clear(); |
34c629c3b4ba
updated for version 7.3.1287
Bram Moolenaar <bram@vim.org>
parents:
4995
diff
changeset
|
5514 } |
5139
80bab8b1a30d
updated for version 7.3.1312
Bram Moolenaar <bram@vim.org>
parents:
5088
diff
changeset
|
5515 else |
80bab8b1a30d
updated for version 7.3.1312
Bram Moolenaar <bram@vim.org>
parents:
5088
diff
changeset
|
5516 { |
80bab8b1a30d
updated for version 7.3.1312
Bram Moolenaar <bram@vim.org>
parents:
5088
diff
changeset
|
5517 if (PyErr_Occurred() && !msg_silent) |
80bab8b1a30d
updated for version 7.3.1312
Bram Moolenaar <bram@vim.org>
parents:
5088
diff
changeset
|
5518 PyErr_PrintEx(0); |
80bab8b1a30d
updated for version 7.3.1312
Bram Moolenaar <bram@vim.org>
parents:
5088
diff
changeset
|
5519 EMSG(_("E858: Eval did not return a valid python object")); |
80bab8b1a30d
updated for version 7.3.1312
Bram Moolenaar <bram@vim.org>
parents:
5088
diff
changeset
|
5520 } |
4486 | 5521 } |
5522 else | |
5523 { | |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
5524 if (ConvertFromPyObject(run_ret, rettv) == -1) |
4486 | 5525 EMSG(_("E859: Failed to convert returned python object to vim value")); |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
5526 Py_DECREF(run_ret); |
4486 | 5527 } |
5528 PyErr_Clear(); | |
5529 } | |
5530 | |
6565 | 5531 static int |
3618 | 5532 set_ref_in_py(const int copyID) |
5533 { | |
5534 pylinkedlist_T *cur; | |
5535 dict_T *dd; | |
5536 list_T *ll; | |
6565 | 5537 int abort = FALSE; |
3618 | 5538 |
5539 if (lastdict != NULL) | |
6565 | 5540 { |
5541 for(cur = lastdict ; !abort && cur != NULL ; cur = cur->pll_prev) | |
3618 | 5542 { |
5543 dd = ((DictionaryObject *) (cur->pll_obj))->dict; | |
5544 if (dd->dv_copyID != copyID) | |
5545 { | |
5546 dd->dv_copyID = copyID; | |
6565 | 5547 abort = abort || set_ref_in_ht(&dd->dv_hashtab, copyID, NULL); |
3618 | 5548 } |
5549 } | |
6565 | 5550 } |
3618 | 5551 |
5552 if (lastlist != NULL) | |
6565 | 5553 { |
5554 for(cur = lastlist ; !abort && cur != NULL ; cur = cur->pll_prev) | |
3618 | 5555 { |
5556 ll = ((ListObject *) (cur->pll_obj))->list; | |
5557 if (ll->lv_copyID != copyID) | |
5558 { | |
5559 ll->lv_copyID = copyID; | |
6565 | 5560 abort = abort || set_ref_in_list(ll, copyID, NULL); |
3618 | 5561 } |
5562 } | |
6565 | 5563 } |
5564 | |
5565 return abort; | |
3618 | 5566 } |
5567 | |
5568 static int | |
5569 set_string_copy(char_u *str, typval_T *tv) | |
5570 { | |
5571 tv->vval.v_string = vim_strsave(str); | |
5572 if (tv->vval.v_string == NULL) | |
5573 { | |
5574 PyErr_NoMemory(); | |
5575 return -1; | |
5576 } | |
5577 return 0; | |
5578 } | |
5579 | |
4385 | 5580 static int |
4601
19d406a8509d
updated for version 7.3.1048
Bram Moolenaar <bram@vim.org>
parents:
4599
diff
changeset
|
5581 pydict_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict) |
4385 | 5582 { |
4601
19d406a8509d
updated for version 7.3.1048
Bram Moolenaar <bram@vim.org>
parents:
4599
diff
changeset
|
5583 dict_T *dict; |
4385 | 5584 char_u *key; |
5585 dictitem_T *di; | |
5586 PyObject *keyObject; | |
5587 PyObject *valObject; | |
5588 Py_ssize_t iter = 0; | |
5589 | |
4659
80b0081824fa
updated for version 7.3.1077
Bram Moolenaar <bram@vim.org>
parents:
4653
diff
changeset
|
5590 if (!(dict = py_dict_alloc())) |
4385 | 5591 return -1; |
5592 | |
5593 tv->v_type = VAR_DICT; | |
4601
19d406a8509d
updated for version 7.3.1048
Bram Moolenaar <bram@vim.org>
parents:
4599
diff
changeset
|
5594 tv->vval.v_dict = dict; |
4385 | 5595 |
5596 while (PyDict_Next(obj, &iter, &keyObject, &valObject)) | |
5597 { | |
4643
6ec3dada4ad3
updated for version 7.3.1069
Bram Moolenaar <bram@vim.org>
parents:
4641
diff
changeset
|
5598 PyObject *todecref = NULL; |
4385 | 5599 |
4607
70600448f9e7
updated for version 7.3.1051
Bram Moolenaar <bram@vim.org>
parents:
4605
diff
changeset
|
5600 if (keyObject == NULL || valObject == NULL) |
4617
21a99611149b
updated for version 7.3.1056
Bram Moolenaar <bram@vim.org>
parents:
4611
diff
changeset
|
5601 { |
21a99611149b
updated for version 7.3.1056
Bram Moolenaar <bram@vim.org>
parents:
4611
diff
changeset
|
5602 dict_unref(dict); |
4385 | 5603 return -1; |
4617
21a99611149b
updated for version 7.3.1056
Bram Moolenaar <bram@vim.org>
parents:
4611
diff
changeset
|
5604 } |
21a99611149b
updated for version 7.3.1056
Bram Moolenaar <bram@vim.org>
parents:
4611
diff
changeset
|
5605 |
4643
6ec3dada4ad3
updated for version 7.3.1069
Bram Moolenaar <bram@vim.org>
parents:
4641
diff
changeset
|
5606 if (!(key = StringToChars(keyObject, &todecref))) |
4617
21a99611149b
updated for version 7.3.1056
Bram Moolenaar <bram@vim.org>
parents:
4611
diff
changeset
|
5607 { |
21a99611149b
updated for version 7.3.1056
Bram Moolenaar <bram@vim.org>
parents:
4611
diff
changeset
|
5608 dict_unref(dict); |
21a99611149b
updated for version 7.3.1056
Bram Moolenaar <bram@vim.org>
parents:
4611
diff
changeset
|
5609 return -1; |
21a99611149b
updated for version 7.3.1056
Bram Moolenaar <bram@vim.org>
parents:
4611
diff
changeset
|
5610 } |
4702
26f2dbea7443
updated for version 7.3.1098
Bram Moolenaar <bram@vim.org>
parents:
4698
diff
changeset
|
5611 |
4643
6ec3dada4ad3
updated for version 7.3.1069
Bram Moolenaar <bram@vim.org>
parents:
4641
diff
changeset
|
5612 if (*key == NUL) |
6ec3dada4ad3
updated for version 7.3.1069
Bram Moolenaar <bram@vim.org>
parents:
4641
diff
changeset
|
5613 { |
6ec3dada4ad3
updated for version 7.3.1069
Bram Moolenaar <bram@vim.org>
parents:
4641
diff
changeset
|
5614 dict_unref(dict); |
6ec3dada4ad3
updated for version 7.3.1069
Bram Moolenaar <bram@vim.org>
parents:
4641
diff
changeset
|
5615 Py_XDECREF(todecref); |
4659
80b0081824fa
updated for version 7.3.1077
Bram Moolenaar <bram@vim.org>
parents:
4653
diff
changeset
|
5616 RAISE_NO_EMPTY_KEYS; |
4643
6ec3dada4ad3
updated for version 7.3.1069
Bram Moolenaar <bram@vim.org>
parents:
4641
diff
changeset
|
5617 return -1; |
6ec3dada4ad3
updated for version 7.3.1069
Bram Moolenaar <bram@vim.org>
parents:
4641
diff
changeset
|
5618 } |
4385 | 5619 |
5620 di = dictitem_alloc(key); | |
5621 | |
4643
6ec3dada4ad3
updated for version 7.3.1069
Bram Moolenaar <bram@vim.org>
parents:
4641
diff
changeset
|
5622 Py_XDECREF(todecref); |
4385 | 5623 |
5624 if (di == NULL) | |
5625 { | |
5626 PyErr_NoMemory(); | |
4617
21a99611149b
updated for version 7.3.1056
Bram Moolenaar <bram@vim.org>
parents:
4611
diff
changeset
|
5627 dict_unref(dict); |
4385 | 5628 return -1; |
5629 } | |
5630 di->di_tv.v_lock = 0; | |
5631 | |
4601
19d406a8509d
updated for version 7.3.1048
Bram Moolenaar <bram@vim.org>
parents:
4599
diff
changeset
|
5632 if (_ConvertFromPyObject(valObject, &di->di_tv, lookup_dict) == -1) |
4385 | 5633 { |
5634 vim_free(di); | |
4617
21a99611149b
updated for version 7.3.1056
Bram Moolenaar <bram@vim.org>
parents:
4611
diff
changeset
|
5635 dict_unref(dict); |
4385 | 5636 return -1; |
5637 } | |
4601
19d406a8509d
updated for version 7.3.1048
Bram Moolenaar <bram@vim.org>
parents:
4599
diff
changeset
|
5638 |
19d406a8509d
updated for version 7.3.1048
Bram Moolenaar <bram@vim.org>
parents:
4599
diff
changeset
|
5639 if (dict_add(dict, di) == FAIL) |
4385 | 5640 { |
4970
f5c822e5a0eb
updated for version 7.3.1230
Bram Moolenaar <bram@vim.org>
parents:
4968
diff
changeset
|
5641 RAISE_KEY_ADD_FAIL(di->di_key); |
4607
70600448f9e7
updated for version 7.3.1051
Bram Moolenaar <bram@vim.org>
parents:
4605
diff
changeset
|
5642 clear_tv(&di->di_tv); |
4385 | 5643 vim_free(di); |
4617
21a99611149b
updated for version 7.3.1056
Bram Moolenaar <bram@vim.org>
parents:
4611
diff
changeset
|
5644 dict_unref(dict); |
4385 | 5645 return -1; |
5646 } | |
5647 } | |
4617
21a99611149b
updated for version 7.3.1056
Bram Moolenaar <bram@vim.org>
parents:
4611
diff
changeset
|
5648 |
21a99611149b
updated for version 7.3.1056
Bram Moolenaar <bram@vim.org>
parents:
4611
diff
changeset
|
5649 --dict->dv_refcount; |
4385 | 5650 return 0; |
5651 } | |
5652 | |
5653 static int | |
4601
19d406a8509d
updated for version 7.3.1048
Bram Moolenaar <bram@vim.org>
parents:
4599
diff
changeset
|
5654 pymap_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict) |
4385 | 5655 { |
4601
19d406a8509d
updated for version 7.3.1048
Bram Moolenaar <bram@vim.org>
parents:
4599
diff
changeset
|
5656 dict_T *dict; |
4385 | 5657 char_u *key; |
5658 dictitem_T *di; | |
5659 PyObject *list; | |
4635
07c534fe9b6c
updated for version 7.3.1065
Bram Moolenaar <bram@vim.org>
parents:
4633
diff
changeset
|
5660 PyObject *iterator; |
4385 | 5661 PyObject *keyObject; |
5662 PyObject *valObject; | |
5663 | |
4659
80b0081824fa
updated for version 7.3.1077
Bram Moolenaar <bram@vim.org>
parents:
4653
diff
changeset
|
5664 if (!(dict = py_dict_alloc())) |
4385 | 5665 return -1; |
5666 | |
5667 tv->v_type = VAR_DICT; | |
4601
19d406a8509d
updated for version 7.3.1048
Bram Moolenaar <bram@vim.org>
parents:
4599
diff
changeset
|
5668 tv->vval.v_dict = dict; |
4385 | 5669 |
4635
07c534fe9b6c
updated for version 7.3.1065
Bram Moolenaar <bram@vim.org>
parents:
4633
diff
changeset
|
5670 if (!(list = PyMapping_Keys(obj))) |
4617
21a99611149b
updated for version 7.3.1056
Bram Moolenaar <bram@vim.org>
parents:
4611
diff
changeset
|
5671 { |
21a99611149b
updated for version 7.3.1056
Bram Moolenaar <bram@vim.org>
parents:
4611
diff
changeset
|
5672 dict_unref(dict); |
4385 | 5673 return -1; |
4617
21a99611149b
updated for version 7.3.1056
Bram Moolenaar <bram@vim.org>
parents:
4611
diff
changeset
|
5674 } |
4635
07c534fe9b6c
updated for version 7.3.1065
Bram Moolenaar <bram@vim.org>
parents:
4633
diff
changeset
|
5675 |
07c534fe9b6c
updated for version 7.3.1065
Bram Moolenaar <bram@vim.org>
parents:
4633
diff
changeset
|
5676 if (!(iterator = PyObject_GetIter(list))) |
07c534fe9b6c
updated for version 7.3.1065
Bram Moolenaar <bram@vim.org>
parents:
4633
diff
changeset
|
5677 { |
07c534fe9b6c
updated for version 7.3.1065
Bram Moolenaar <bram@vim.org>
parents:
4633
diff
changeset
|
5678 dict_unref(dict); |
07c534fe9b6c
updated for version 7.3.1065
Bram Moolenaar <bram@vim.org>
parents:
4633
diff
changeset
|
5679 Py_DECREF(list); |
07c534fe9b6c
updated for version 7.3.1065
Bram Moolenaar <bram@vim.org>
parents:
4633
diff
changeset
|
5680 return -1; |
07c534fe9b6c
updated for version 7.3.1065
Bram Moolenaar <bram@vim.org>
parents:
4633
diff
changeset
|
5681 } |
07c534fe9b6c
updated for version 7.3.1065
Bram Moolenaar <bram@vim.org>
parents:
4633
diff
changeset
|
5682 Py_DECREF(list); |
07c534fe9b6c
updated for version 7.3.1065
Bram Moolenaar <bram@vim.org>
parents:
4633
diff
changeset
|
5683 |
07c534fe9b6c
updated for version 7.3.1065
Bram Moolenaar <bram@vim.org>
parents:
4633
diff
changeset
|
5684 while ((keyObject = PyIter_Next(iterator))) |
4385 | 5685 { |
4643
6ec3dada4ad3
updated for version 7.3.1069
Bram Moolenaar <bram@vim.org>
parents:
4641
diff
changeset
|
5686 PyObject *todecref; |
6ec3dada4ad3
updated for version 7.3.1069
Bram Moolenaar <bram@vim.org>
parents:
4641
diff
changeset
|
5687 |
6ec3dada4ad3
updated for version 7.3.1069
Bram Moolenaar <bram@vim.org>
parents:
4641
diff
changeset
|
5688 if (!(key = StringToChars(keyObject, &todecref))) |
4617
21a99611149b
updated for version 7.3.1056
Bram Moolenaar <bram@vim.org>
parents:
4611
diff
changeset
|
5689 { |
4643
6ec3dada4ad3
updated for version 7.3.1069
Bram Moolenaar <bram@vim.org>
parents:
4641
diff
changeset
|
5690 Py_DECREF(keyObject); |
4635
07c534fe9b6c
updated for version 7.3.1065
Bram Moolenaar <bram@vim.org>
parents:
4633
diff
changeset
|
5691 Py_DECREF(iterator); |
4617
21a99611149b
updated for version 7.3.1056
Bram Moolenaar <bram@vim.org>
parents:
4611
diff
changeset
|
5692 dict_unref(dict); |
21a99611149b
updated for version 7.3.1056
Bram Moolenaar <bram@vim.org>
parents:
4611
diff
changeset
|
5693 return -1; |
21a99611149b
updated for version 7.3.1056
Bram Moolenaar <bram@vim.org>
parents:
4611
diff
changeset
|
5694 } |
4702
26f2dbea7443
updated for version 7.3.1098
Bram Moolenaar <bram@vim.org>
parents:
4698
diff
changeset
|
5695 |
4643
6ec3dada4ad3
updated for version 7.3.1069
Bram Moolenaar <bram@vim.org>
parents:
4641
diff
changeset
|
5696 if (*key == NUL) |
6ec3dada4ad3
updated for version 7.3.1069
Bram Moolenaar <bram@vim.org>
parents:
4641
diff
changeset
|
5697 { |
6ec3dada4ad3
updated for version 7.3.1069
Bram Moolenaar <bram@vim.org>
parents:
4641
diff
changeset
|
5698 Py_DECREF(keyObject); |
6ec3dada4ad3
updated for version 7.3.1069
Bram Moolenaar <bram@vim.org>
parents:
4641
diff
changeset
|
5699 Py_DECREF(iterator); |
6ec3dada4ad3
updated for version 7.3.1069
Bram Moolenaar <bram@vim.org>
parents:
4641
diff
changeset
|
5700 Py_XDECREF(todecref); |
6ec3dada4ad3
updated for version 7.3.1069
Bram Moolenaar <bram@vim.org>
parents:
4641
diff
changeset
|
5701 dict_unref(dict); |
4659
80b0081824fa
updated for version 7.3.1077
Bram Moolenaar <bram@vim.org>
parents:
4653
diff
changeset
|
5702 RAISE_NO_EMPTY_KEYS; |
4643
6ec3dada4ad3
updated for version 7.3.1069
Bram Moolenaar <bram@vim.org>
parents:
4641
diff
changeset
|
5703 return -1; |
6ec3dada4ad3
updated for version 7.3.1069
Bram Moolenaar <bram@vim.org>
parents:
4641
diff
changeset
|
5704 } |
4607
70600448f9e7
updated for version 7.3.1051
Bram Moolenaar <bram@vim.org>
parents:
4605
diff
changeset
|
5705 |
4635
07c534fe9b6c
updated for version 7.3.1065
Bram Moolenaar <bram@vim.org>
parents:
4633
diff
changeset
|
5706 if (!(valObject = PyObject_GetItem(obj, keyObject))) |
4385 | 5707 { |
4635
07c534fe9b6c
updated for version 7.3.1065
Bram Moolenaar <bram@vim.org>
parents:
4633
diff
changeset
|
5708 Py_DECREF(keyObject); |
07c534fe9b6c
updated for version 7.3.1065
Bram Moolenaar <bram@vim.org>
parents:
4633
diff
changeset
|
5709 Py_DECREF(iterator); |
4643
6ec3dada4ad3
updated for version 7.3.1069
Bram Moolenaar <bram@vim.org>
parents:
4641
diff
changeset
|
5710 Py_XDECREF(todecref); |
4617
21a99611149b
updated for version 7.3.1056
Bram Moolenaar <bram@vim.org>
parents:
4611
diff
changeset
|
5711 dict_unref(dict); |
4385 | 5712 return -1; |
5713 } | |
5714 | |
5715 di = dictitem_alloc(key); | |
5716 | |
4635
07c534fe9b6c
updated for version 7.3.1065
Bram Moolenaar <bram@vim.org>
parents:
4633
diff
changeset
|
5717 Py_DECREF(keyObject); |
4643
6ec3dada4ad3
updated for version 7.3.1069
Bram Moolenaar <bram@vim.org>
parents:
4641
diff
changeset
|
5718 Py_XDECREF(todecref); |
4635
07c534fe9b6c
updated for version 7.3.1065
Bram Moolenaar <bram@vim.org>
parents:
4633
diff
changeset
|
5719 |
4385 | 5720 if (di == NULL) |
5721 { | |
4635
07c534fe9b6c
updated for version 7.3.1065
Bram Moolenaar <bram@vim.org>
parents:
4633
diff
changeset
|
5722 Py_DECREF(iterator); |
07c534fe9b6c
updated for version 7.3.1065
Bram Moolenaar <bram@vim.org>
parents:
4633
diff
changeset
|
5723 Py_DECREF(valObject); |
4617
21a99611149b
updated for version 7.3.1056
Bram Moolenaar <bram@vim.org>
parents:
4611
diff
changeset
|
5724 dict_unref(dict); |
4385 | 5725 PyErr_NoMemory(); |
5726 return -1; | |
5727 } | |
5728 di->di_tv.v_lock = 0; | |
5729 | |
4601
19d406a8509d
updated for version 7.3.1048
Bram Moolenaar <bram@vim.org>
parents:
4599
diff
changeset
|
5730 if (_ConvertFromPyObject(valObject, &di->di_tv, lookup_dict) == -1) |
4385 | 5731 { |
4635
07c534fe9b6c
updated for version 7.3.1065
Bram Moolenaar <bram@vim.org>
parents:
4633
diff
changeset
|
5732 Py_DECREF(iterator); |
07c534fe9b6c
updated for version 7.3.1065
Bram Moolenaar <bram@vim.org>
parents:
4633
diff
changeset
|
5733 Py_DECREF(valObject); |
4385 | 5734 vim_free(di); |
4617
21a99611149b
updated for version 7.3.1056
Bram Moolenaar <bram@vim.org>
parents:
4611
diff
changeset
|
5735 dict_unref(dict); |
4385 | 5736 return -1; |
5737 } | |
4607
70600448f9e7
updated for version 7.3.1051
Bram Moolenaar <bram@vim.org>
parents:
4605
diff
changeset
|
5738 |
4635
07c534fe9b6c
updated for version 7.3.1065
Bram Moolenaar <bram@vim.org>
parents:
4633
diff
changeset
|
5739 Py_DECREF(valObject); |
07c534fe9b6c
updated for version 7.3.1065
Bram Moolenaar <bram@vim.org>
parents:
4633
diff
changeset
|
5740 |
4601
19d406a8509d
updated for version 7.3.1048
Bram Moolenaar <bram@vim.org>
parents:
4599
diff
changeset
|
5741 if (dict_add(dict, di) == FAIL) |
4385 | 5742 { |
4970
f5c822e5a0eb
updated for version 7.3.1230
Bram Moolenaar <bram@vim.org>
parents:
4968
diff
changeset
|
5743 RAISE_KEY_ADD_FAIL(di->di_key); |
4635
07c534fe9b6c
updated for version 7.3.1065
Bram Moolenaar <bram@vim.org>
parents:
4633
diff
changeset
|
5744 Py_DECREF(iterator); |
4617
21a99611149b
updated for version 7.3.1056
Bram Moolenaar <bram@vim.org>
parents:
4611
diff
changeset
|
5745 dictitem_free(di); |
21a99611149b
updated for version 7.3.1056
Bram Moolenaar <bram@vim.org>
parents:
4611
diff
changeset
|
5746 dict_unref(dict); |
4385 | 5747 return -1; |
5748 } | |
5749 } | |
4635
07c534fe9b6c
updated for version 7.3.1065
Bram Moolenaar <bram@vim.org>
parents:
4633
diff
changeset
|
5750 Py_DECREF(iterator); |
4617
21a99611149b
updated for version 7.3.1056
Bram Moolenaar <bram@vim.org>
parents:
4611
diff
changeset
|
5751 --dict->dv_refcount; |
4385 | 5752 return 0; |
5753 } | |
5754 | |
5755 static int | |
4601
19d406a8509d
updated for version 7.3.1048
Bram Moolenaar <bram@vim.org>
parents:
4599
diff
changeset
|
5756 pyseq_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict) |
4385 | 5757 { |
5758 list_T *l; | |
5759 | |
4617
21a99611149b
updated for version 7.3.1056
Bram Moolenaar <bram@vim.org>
parents:
4611
diff
changeset
|
5760 if (!(l = py_list_alloc())) |
4385 | 5761 return -1; |
5762 | |
5763 tv->v_type = VAR_LIST; | |
5764 tv->vval.v_list = l; | |
5765 | |
4601
19d406a8509d
updated for version 7.3.1048
Bram Moolenaar <bram@vim.org>
parents:
4599
diff
changeset
|
5766 if (list_py_concat(l, obj, lookup_dict) == -1) |
4617
21a99611149b
updated for version 7.3.1056
Bram Moolenaar <bram@vim.org>
parents:
4611
diff
changeset
|
5767 { |
21a99611149b
updated for version 7.3.1056
Bram Moolenaar <bram@vim.org>
parents:
4611
diff
changeset
|
5768 list_unref(l); |
4385 | 5769 return -1; |
4617
21a99611149b
updated for version 7.3.1056
Bram Moolenaar <bram@vim.org>
parents:
4611
diff
changeset
|
5770 } |
21a99611149b
updated for version 7.3.1056
Bram Moolenaar <bram@vim.org>
parents:
4611
diff
changeset
|
5771 |
21a99611149b
updated for version 7.3.1056
Bram Moolenaar <bram@vim.org>
parents:
4611
diff
changeset
|
5772 --l->lv_refcount; |
4385 | 5773 return 0; |
5774 } | |
5775 | |
3618 | 5776 typedef int (*pytotvfunc)(PyObject *, typval_T *, PyObject *); |
5777 | |
5778 static int | |
5779 convert_dl(PyObject *obj, typval_T *tv, | |
4601
19d406a8509d
updated for version 7.3.1048
Bram Moolenaar <bram@vim.org>
parents:
4599
diff
changeset
|
5780 pytotvfunc py_to_tv, PyObject *lookup_dict) |
3618 | 5781 { |
5782 PyObject *capsule; | |
5783 char hexBuf[sizeof(void *) * 2 + 3]; | |
5784 | |
5785 sprintf(hexBuf, "%p", obj); | |
5786 | |
3638 | 5787 # ifdef PY_USE_CAPSULE |
4601
19d406a8509d
updated for version 7.3.1048
Bram Moolenaar <bram@vim.org>
parents:
4599
diff
changeset
|
5788 capsule = PyDict_GetItemString(lookup_dict, hexBuf); |
3648 | 5789 # else |
4601
19d406a8509d
updated for version 7.3.1048
Bram Moolenaar <bram@vim.org>
parents:
4599
diff
changeset
|
5790 capsule = (PyObject *)PyDict_GetItemString(lookup_dict, hexBuf); |
3648 | 5791 # endif |
3618 | 5792 if (capsule == NULL) |
5793 { | |
3638 | 5794 # ifdef PY_USE_CAPSULE |
3618 | 5795 capsule = PyCapsule_New(tv, NULL, NULL); |
3648 | 5796 # else |
5797 capsule = PyCObject_FromVoidPtr(tv, NULL); | |
5798 # endif | |
4607
70600448f9e7
updated for version 7.3.1051
Bram Moolenaar <bram@vim.org>
parents:
4605
diff
changeset
|
5799 if (PyDict_SetItemString(lookup_dict, hexBuf, capsule)) |
70600448f9e7
updated for version 7.3.1051
Bram Moolenaar <bram@vim.org>
parents:
4605
diff
changeset
|
5800 { |
70600448f9e7
updated for version 7.3.1051
Bram Moolenaar <bram@vim.org>
parents:
4605
diff
changeset
|
5801 Py_DECREF(capsule); |
70600448f9e7
updated for version 7.3.1051
Bram Moolenaar <bram@vim.org>
parents:
4605
diff
changeset
|
5802 tv->v_type = VAR_UNKNOWN; |
70600448f9e7
updated for version 7.3.1051
Bram Moolenaar <bram@vim.org>
parents:
4605
diff
changeset
|
5803 return -1; |
70600448f9e7
updated for version 7.3.1051
Bram Moolenaar <bram@vim.org>
parents:
4605
diff
changeset
|
5804 } |
4976
4ed713442c51
updated for version 7.3.1233
Bram Moolenaar <bram@vim.org>
parents:
4974
diff
changeset
|
5805 |
4ed713442c51
updated for version 7.3.1233
Bram Moolenaar <bram@vim.org>
parents:
4974
diff
changeset
|
5806 Py_DECREF(capsule); |
4ed713442c51
updated for version 7.3.1233
Bram Moolenaar <bram@vim.org>
parents:
4974
diff
changeset
|
5807 |
4601
19d406a8509d
updated for version 7.3.1048
Bram Moolenaar <bram@vim.org>
parents:
4599
diff
changeset
|
5808 if (py_to_tv(obj, tv, lookup_dict) == -1) |
3618 | 5809 { |
5810 tv->v_type = VAR_UNKNOWN; | |
5811 return -1; | |
5812 } | |
5813 /* As we are not using copy_tv which increments reference count we must | |
5814 * do it ourself. */ | |
5815 switch(tv->v_type) | |
5816 { | |
5817 case VAR_DICT: ++tv->vval.v_dict->dv_refcount; break; | |
5818 case VAR_LIST: ++tv->vval.v_list->lv_refcount; break; | |
5819 } | |
5820 } | |
5821 else | |
5822 { | |
3638 | 5823 typval_T *v; |
5824 | |
5825 # ifdef PY_USE_CAPSULE | |
5826 v = PyCapsule_GetPointer(capsule, NULL); | |
5827 # else | |
3648 | 5828 v = PyCObject_AsVoidPtr(capsule); |
3638 | 5829 # endif |
3618 | 5830 copy_tv(v, tv); |
5831 } | |
5832 return 0; | |
5833 } | |
5834 | |
5835 static int | |
4627
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
5836 ConvertFromPyMapping(PyObject *obj, typval_T *tv) |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
5837 { |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
5838 PyObject *lookup_dict; |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
5839 int ret; |
4627
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
5840 |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
5841 if (!(lookup_dict = PyDict_New())) |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
5842 return -1; |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
5843 |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
5844 if (PyType_IsSubtype(obj->ob_type, &DictionaryType)) |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
5845 { |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
5846 tv->v_type = VAR_DICT; |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
5847 tv->vval.v_dict = (((DictionaryObject *)(obj))->dict); |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
5848 ++tv->vval.v_dict->dv_refcount; |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
5849 ret = 0; |
4627
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
5850 } |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
5851 else if (PyDict_Check(obj)) |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
5852 ret = convert_dl(obj, tv, pydict_to_tv, lookup_dict); |
4627
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
5853 else if (PyMapping_Check(obj)) |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
5854 ret = convert_dl(obj, tv, pymap_to_tv, lookup_dict); |
4627
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
5855 else |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
5856 { |
4970
f5c822e5a0eb
updated for version 7.3.1230
Bram Moolenaar <bram@vim.org>
parents:
4968
diff
changeset
|
5857 PyErr_FORMAT(PyExc_TypeError, |
4978
f4969f8f66e9
updated for version 7.3.1234
Bram Moolenaar <bram@vim.org>
parents:
4976
diff
changeset
|
5858 N_("unable to convert %s to vim dictionary"), |
4970
f5c822e5a0eb
updated for version 7.3.1230
Bram Moolenaar <bram@vim.org>
parents:
4968
diff
changeset
|
5859 Py_TYPE_NAME(obj)); |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
5860 ret = -1; |
4627
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
5861 } |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
5862 Py_DECREF(lookup_dict); |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
5863 return ret; |
4627
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
5864 } |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
5865 |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
5866 static int |
3618 | 5867 ConvertFromPyObject(PyObject *obj, typval_T *tv) |
5868 { | |
5869 PyObject *lookup_dict; | |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
5870 int ret; |
3618 | 5871 |
4617
21a99611149b
updated for version 7.3.1056
Bram Moolenaar <bram@vim.org>
parents:
4611
diff
changeset
|
5872 if (!(lookup_dict = PyDict_New())) |
21a99611149b
updated for version 7.3.1056
Bram Moolenaar <bram@vim.org>
parents:
4611
diff
changeset
|
5873 return -1; |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
5874 ret = _ConvertFromPyObject(obj, tv, lookup_dict); |
3618 | 5875 Py_DECREF(lookup_dict); |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
5876 return ret; |
3618 | 5877 } |
5878 | |
5879 static int | |
4601
19d406a8509d
updated for version 7.3.1048
Bram Moolenaar <bram@vim.org>
parents:
4599
diff
changeset
|
5880 _ConvertFromPyObject(PyObject *obj, typval_T *tv, PyObject *lookup_dict) |
3618 | 5881 { |
4627
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
5882 if (PyType_IsSubtype(obj->ob_type, &DictionaryType)) |
3618 | 5883 { |
5884 tv->v_type = VAR_DICT; | |
5885 tv->vval.v_dict = (((DictionaryObject *)(obj))->dict); | |
5886 ++tv->vval.v_dict->dv_refcount; | |
5887 } | |
4976
4ed713442c51
updated for version 7.3.1233
Bram Moolenaar <bram@vim.org>
parents:
4974
diff
changeset
|
5888 else if (PyType_IsSubtype(obj->ob_type, &ListType)) |
3618 | 5889 { |
5890 tv->v_type = VAR_LIST; | |
5891 tv->vval.v_list = (((ListObject *)(obj))->list); | |
5892 ++tv->vval.v_list->lv_refcount; | |
5893 } | |
4976
4ed713442c51
updated for version 7.3.1233
Bram Moolenaar <bram@vim.org>
parents:
4974
diff
changeset
|
5894 else if (PyType_IsSubtype(obj->ob_type, &FunctionType)) |
3618 | 5895 { |
5896 if (set_string_copy(((FunctionObject *) (obj))->name, tv) == -1) | |
5897 return -1; | |
5898 | |
5899 tv->v_type = VAR_FUNC; | |
5900 func_ref(tv->vval.v_string); | |
5901 } | |
5902 else if (PyBytes_Check(obj)) | |
5903 { | |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
5904 char_u *str; |
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
5905 |
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
5906 if (PyBytes_AsStringAndSize(obj, (char **) &str, NULL) == -1) |
3800 | 5907 return -1; |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
5908 if (str == NULL) |
3618 | 5909 return -1; |
5910 | |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
5911 if (set_string_copy(str, tv) == -1) |
3618 | 5912 return -1; |
5913 | |
5914 tv->v_type = VAR_STRING; | |
5915 } | |
5916 else if (PyUnicode_Check(obj)) | |
5917 { | |
5918 PyObject *bytes; | |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
5919 char_u *str; |
3618 | 5920 |
4966
620d9b59d4ed
updated for version 7.3.1228
Bram Moolenaar <bram@vim.org>
parents:
4964
diff
changeset
|
5921 bytes = PyUnicode_AsEncodedString(obj, ENC_OPT, NULL); |
3618 | 5922 if (bytes == NULL) |
5923 return -1; | |
5924 | |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
5925 if(PyBytes_AsStringAndSize(bytes, (char **) &str, NULL) == -1) |
3800 | 5926 return -1; |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
5927 if (str == NULL) |
3618 | 5928 return -1; |
5929 | |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
5930 if (set_string_copy(str, tv)) |
3618 | 5931 { |
5932 Py_XDECREF(bytes); | |
5933 return -1; | |
5934 } | |
5935 Py_XDECREF(bytes); | |
5936 | |
5937 tv->v_type = VAR_STRING; | |
5938 } | |
4321 | 5939 #if PY_MAJOR_VERSION < 3 |
3618 | 5940 else if (PyInt_Check(obj)) |
5941 { | |
5942 tv->v_type = VAR_NUMBER; | |
5943 tv->vval.v_number = (varnumber_T) PyInt_AsLong(obj); | |
4972
537bbfff0c5c
updated for version 7.3.1231
Bram Moolenaar <bram@vim.org>
parents:
4970
diff
changeset
|
5944 if (PyErr_Occurred()) |
537bbfff0c5c
updated for version 7.3.1231
Bram Moolenaar <bram@vim.org>
parents:
4970
diff
changeset
|
5945 return -1; |
3618 | 5946 } |
5947 #endif | |
5948 else if (PyLong_Check(obj)) | |
5949 { | |
5950 tv->v_type = VAR_NUMBER; | |
5951 tv->vval.v_number = (varnumber_T) PyLong_AsLong(obj); | |
4972
537bbfff0c5c
updated for version 7.3.1231
Bram Moolenaar <bram@vim.org>
parents:
4970
diff
changeset
|
5952 if (PyErr_Occurred()) |
537bbfff0c5c
updated for version 7.3.1231
Bram Moolenaar <bram@vim.org>
parents:
4970
diff
changeset
|
5953 return -1; |
3618 | 5954 } |
5955 else if (PyDict_Check(obj)) | |
4601
19d406a8509d
updated for version 7.3.1048
Bram Moolenaar <bram@vim.org>
parents:
4599
diff
changeset
|
5956 return convert_dl(obj, tv, pydict_to_tv, lookup_dict); |
3618 | 5957 #ifdef FEAT_FLOAT |
5958 else if (PyFloat_Check(obj)) | |
5959 { | |
5960 tv->v_type = VAR_FLOAT; | |
5961 tv->vval.v_float = (float_T) PyFloat_AsDouble(obj); | |
5962 } | |
5963 #endif | |
4635
07c534fe9b6c
updated for version 7.3.1065
Bram Moolenaar <bram@vim.org>
parents:
4633
diff
changeset
|
5964 else if (PyObject_HasAttrString(obj, "keys")) |
07c534fe9b6c
updated for version 7.3.1065
Bram Moolenaar <bram@vim.org>
parents:
4633
diff
changeset
|
5965 return convert_dl(obj, tv, pymap_to_tv, lookup_dict); |
4982
39980afcf54a
updated for version 7.3.1236
Bram Moolenaar <bram@vim.org>
parents:
4978
diff
changeset
|
5966 /* PyObject_GetIter can create built-in iterator for any sequence object */ |
4629
e4e48d4ee040
updated for version 7.3.1062
Bram Moolenaar <bram@vim.org>
parents:
4627
diff
changeset
|
5967 else if (PyIter_Check(obj) || PySequence_Check(obj)) |
4601
19d406a8509d
updated for version 7.3.1048
Bram Moolenaar <bram@vim.org>
parents:
4599
diff
changeset
|
5968 return convert_dl(obj, tv, pyseq_to_tv, lookup_dict); |
3618 | 5969 else if (PyMapping_Check(obj)) |
4601
19d406a8509d
updated for version 7.3.1048
Bram Moolenaar <bram@vim.org>
parents:
4599
diff
changeset
|
5970 return convert_dl(obj, tv, pymap_to_tv, lookup_dict); |
4972
537bbfff0c5c
updated for version 7.3.1231
Bram Moolenaar <bram@vim.org>
parents:
4970
diff
changeset
|
5971 else if (PyNumber_Check(obj)) |
537bbfff0c5c
updated for version 7.3.1231
Bram Moolenaar <bram@vim.org>
parents:
4970
diff
changeset
|
5972 { |
537bbfff0c5c
updated for version 7.3.1231
Bram Moolenaar <bram@vim.org>
parents:
4970
diff
changeset
|
5973 PyObject *num; |
537bbfff0c5c
updated for version 7.3.1231
Bram Moolenaar <bram@vim.org>
parents:
4970
diff
changeset
|
5974 |
537bbfff0c5c
updated for version 7.3.1231
Bram Moolenaar <bram@vim.org>
parents:
4970
diff
changeset
|
5975 if (!(num = PyNumber_Long(obj))) |
537bbfff0c5c
updated for version 7.3.1231
Bram Moolenaar <bram@vim.org>
parents:
4970
diff
changeset
|
5976 return -1; |
537bbfff0c5c
updated for version 7.3.1231
Bram Moolenaar <bram@vim.org>
parents:
4970
diff
changeset
|
5977 |
537bbfff0c5c
updated for version 7.3.1231
Bram Moolenaar <bram@vim.org>
parents:
4970
diff
changeset
|
5978 tv->v_type = VAR_NUMBER; |
537bbfff0c5c
updated for version 7.3.1231
Bram Moolenaar <bram@vim.org>
parents:
4970
diff
changeset
|
5979 tv->vval.v_number = (varnumber_T) PyLong_AsLong(num); |
537bbfff0c5c
updated for version 7.3.1231
Bram Moolenaar <bram@vim.org>
parents:
4970
diff
changeset
|
5980 |
537bbfff0c5c
updated for version 7.3.1231
Bram Moolenaar <bram@vim.org>
parents:
4970
diff
changeset
|
5981 Py_DECREF(num); |
537bbfff0c5c
updated for version 7.3.1231
Bram Moolenaar <bram@vim.org>
parents:
4970
diff
changeset
|
5982 } |
3618 | 5983 else |
5984 { | |
4970
f5c822e5a0eb
updated for version 7.3.1230
Bram Moolenaar <bram@vim.org>
parents:
4968
diff
changeset
|
5985 PyErr_FORMAT(PyExc_TypeError, |
4978
f4969f8f66e9
updated for version 7.3.1234
Bram Moolenaar <bram@vim.org>
parents:
4976
diff
changeset
|
5986 N_("unable to convert %s to vim structure"), |
4970
f5c822e5a0eb
updated for version 7.3.1230
Bram Moolenaar <bram@vim.org>
parents:
4968
diff
changeset
|
5987 Py_TYPE_NAME(obj)); |
3618 | 5988 return -1; |
5989 } | |
5990 return 0; | |
5991 } | |
5992 | |
5993 static PyObject * | |
5994 ConvertToPyObject(typval_T *tv) | |
5995 { | |
5996 if (tv == NULL) | |
5997 { | |
4978
f4969f8f66e9
updated for version 7.3.1234
Bram Moolenaar <bram@vim.org>
parents:
4976
diff
changeset
|
5998 PyErr_SET_VIM(N_("internal error: NULL reference passed")); |
3618 | 5999 return NULL; |
6000 } | |
6001 switch (tv->v_type) | |
6002 { | |
6003 case VAR_STRING: | |
3852 | 6004 return PyBytes_FromString(tv->vval.v_string == NULL |
6005 ? "" : (char *)tv->vval.v_string); | |
3618 | 6006 case VAR_NUMBER: |
6007 return PyLong_FromLong((long) tv->vval.v_number); | |
6008 #ifdef FEAT_FLOAT | |
6009 case VAR_FLOAT: | |
6010 return PyFloat_FromDouble((double) tv->vval.v_float); | |
6011 #endif | |
6012 case VAR_LIST: | |
4629
e4e48d4ee040
updated for version 7.3.1062
Bram Moolenaar <bram@vim.org>
parents:
4627
diff
changeset
|
6013 return NEW_LIST(tv->vval.v_list); |
3618 | 6014 case VAR_DICT: |
4627
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
6015 return NEW_DICTIONARY(tv->vval.v_dict); |
3618 | 6016 case VAR_FUNC: |
4631
4157fef7b950
updated for version 7.3.1063
Bram Moolenaar <bram@vim.org>
parents:
4629
diff
changeset
|
6017 return NEW_FUNCTION(tv->vval.v_string == NULL |
3852 | 6018 ? (char_u *)"" : tv->vval.v_string); |
3618 | 6019 case VAR_UNKNOWN: |
6020 Py_INCREF(Py_None); | |
6021 return Py_None; | |
6022 default: | |
4978
f4969f8f66e9
updated for version 7.3.1234
Bram Moolenaar <bram@vim.org>
parents:
4976
diff
changeset
|
6023 PyErr_SET_VIM(N_("internal error: invalid value type")); |
3618 | 6024 return NULL; |
6025 } | |
6026 } | |
4319 | 6027 |
6028 typedef struct | |
6029 { | |
6030 PyObject_HEAD | |
6031 } CurrentObject; | |
6032 static PyTypeObject CurrentType; | |
6033 | |
6034 static void | |
6035 init_structs(void) | |
6036 { | |
6037 vim_memset(&OutputType, 0, sizeof(OutputType)); | |
6038 OutputType.tp_name = "vim.message"; | |
6039 OutputType.tp_basicsize = sizeof(OutputObject); | |
6040 OutputType.tp_flags = Py_TPFLAGS_DEFAULT; | |
6041 OutputType.tp_doc = "vim message object"; | |
6042 OutputType.tp_methods = OutputMethods; | |
6043 #if PY_MAJOR_VERSION >= 3 | |
4488 | 6044 OutputType.tp_getattro = (getattrofunc)OutputGetattro; |
6045 OutputType.tp_setattro = (setattrofunc)OutputSetattro; | |
4319 | 6046 OutputType.tp_alloc = call_PyType_GenericAlloc; |
6047 OutputType.tp_new = call_PyType_GenericNew; | |
6048 OutputType.tp_free = call_PyObject_Free; | |
6049 #else | |
4488 | 6050 OutputType.tp_getattr = (getattrfunc)OutputGetattr; |
6051 OutputType.tp_setattr = (setattrfunc)OutputSetattr; | |
4319 | 6052 #endif |
6053 | |
4397 | 6054 vim_memset(&IterType, 0, sizeof(IterType)); |
6055 IterType.tp_name = "vim.iter"; | |
6056 IterType.tp_basicsize = sizeof(IterObject); | |
4611
49f0fcd9762c
updated for version 7.3.1053
Bram Moolenaar <bram@vim.org>
parents:
4609
diff
changeset
|
6057 IterType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC; |
4397 | 6058 IterType.tp_doc = "generic iterator object"; |
4488 | 6059 IterType.tp_iter = (getiterfunc)IterIter; |
6060 IterType.tp_iternext = (iternextfunc)IterNext; | |
6061 IterType.tp_dealloc = (destructor)IterDestructor; | |
6062 IterType.tp_traverse = (traverseproc)IterTraverse; | |
6063 IterType.tp_clear = (inquiry)IterClear; | |
4397 | 6064 |
4319 | 6065 vim_memset(&BufferType, 0, sizeof(BufferType)); |
6066 BufferType.tp_name = "vim.buffer"; | |
6067 BufferType.tp_basicsize = sizeof(BufferType); | |
4488 | 6068 BufferType.tp_dealloc = (destructor)BufferDestructor; |
6069 BufferType.tp_repr = (reprfunc)BufferRepr; | |
4319 | 6070 BufferType.tp_as_sequence = &BufferAsSeq; |
6071 BufferType.tp_as_mapping = &BufferAsMapping; | |
6072 BufferType.tp_flags = Py_TPFLAGS_DEFAULT; | |
6073 BufferType.tp_doc = "vim buffer object"; | |
6074 BufferType.tp_methods = BufferMethods; | |
6075 #if PY_MAJOR_VERSION >= 3 | |
4488 | 6076 BufferType.tp_getattro = (getattrofunc)BufferGetattro; |
4589
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
6077 BufferType.tp_setattro = (setattrofunc)BufferSetattro; |
4319 | 6078 BufferType.tp_alloc = call_PyType_GenericAlloc; |
6079 BufferType.tp_new = call_PyType_GenericNew; | |
6080 BufferType.tp_free = call_PyObject_Free; | |
6081 #else | |
4488 | 6082 BufferType.tp_getattr = (getattrfunc)BufferGetattr; |
4589
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
6083 BufferType.tp_setattr = (setattrfunc)BufferSetattr; |
4319 | 6084 #endif |
6085 | |
6086 vim_memset(&WindowType, 0, sizeof(WindowType)); | |
6087 WindowType.tp_name = "vim.window"; | |
6088 WindowType.tp_basicsize = sizeof(WindowObject); | |
4488 | 6089 WindowType.tp_dealloc = (destructor)WindowDestructor; |
6090 WindowType.tp_repr = (reprfunc)WindowRepr; | |
4611
49f0fcd9762c
updated for version 7.3.1053
Bram Moolenaar <bram@vim.org>
parents:
4609
diff
changeset
|
6091 WindowType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC; |
4319 | 6092 WindowType.tp_doc = "vim Window object"; |
6093 WindowType.tp_methods = WindowMethods; | |
4488 | 6094 WindowType.tp_traverse = (traverseproc)WindowTraverse; |
6095 WindowType.tp_clear = (inquiry)WindowClear; | |
4319 | 6096 #if PY_MAJOR_VERSION >= 3 |
4488 | 6097 WindowType.tp_getattro = (getattrofunc)WindowGetattro; |
6098 WindowType.tp_setattro = (setattrofunc)WindowSetattro; | |
4319 | 6099 WindowType.tp_alloc = call_PyType_GenericAlloc; |
6100 WindowType.tp_new = call_PyType_GenericNew; | |
6101 WindowType.tp_free = call_PyObject_Free; | |
6102 #else | |
4488 | 6103 WindowType.tp_getattr = (getattrfunc)WindowGetattr; |
6104 WindowType.tp_setattr = (setattrfunc)WindowSetattr; | |
4319 | 6105 #endif |
6106 | |
4401 | 6107 vim_memset(&TabPageType, 0, sizeof(TabPageType)); |
6108 TabPageType.tp_name = "vim.tabpage"; | |
6109 TabPageType.tp_basicsize = sizeof(TabPageObject); | |
4488 | 6110 TabPageType.tp_dealloc = (destructor)TabPageDestructor; |
6111 TabPageType.tp_repr = (reprfunc)TabPageRepr; | |
4401 | 6112 TabPageType.tp_flags = Py_TPFLAGS_DEFAULT; |
6113 TabPageType.tp_doc = "vim tab page object"; | |
6114 TabPageType.tp_methods = TabPageMethods; | |
6115 #if PY_MAJOR_VERSION >= 3 | |
4488 | 6116 TabPageType.tp_getattro = (getattrofunc)TabPageGetattro; |
4401 | 6117 TabPageType.tp_alloc = call_PyType_GenericAlloc; |
6118 TabPageType.tp_new = call_PyType_GenericNew; | |
6119 TabPageType.tp_free = call_PyObject_Free; | |
6120 #else | |
4488 | 6121 TabPageType.tp_getattr = (getattrfunc)TabPageGetattr; |
4401 | 6122 #endif |
6123 | |
4393 | 6124 vim_memset(&BufMapType, 0, sizeof(BufMapType)); |
6125 BufMapType.tp_name = "vim.bufferlist"; | |
6126 BufMapType.tp_basicsize = sizeof(BufMapObject); | |
6127 BufMapType.tp_as_mapping = &BufMapAsMapping; | |
6128 BufMapType.tp_flags = Py_TPFLAGS_DEFAULT; | |
4397 | 6129 BufMapType.tp_iter = BufMapIter; |
4319 | 6130 BufferType.tp_doc = "vim buffer list"; |
6131 | |
6132 vim_memset(&WinListType, 0, sizeof(WinListType)); | |
6133 WinListType.tp_name = "vim.windowlist"; | |
6134 WinListType.tp_basicsize = sizeof(WinListType); | |
6135 WinListType.tp_as_sequence = &WinListAsSeq; | |
6136 WinListType.tp_flags = Py_TPFLAGS_DEFAULT; | |
6137 WinListType.tp_doc = "vim window list"; | |
4488 | 6138 WinListType.tp_dealloc = (destructor)WinListDestructor; |
4401 | 6139 |
6140 vim_memset(&TabListType, 0, sizeof(TabListType)); | |
6141 TabListType.tp_name = "vim.tabpagelist"; | |
6142 TabListType.tp_basicsize = sizeof(TabListType); | |
6143 TabListType.tp_as_sequence = &TabListAsSeq; | |
6144 TabListType.tp_flags = Py_TPFLAGS_DEFAULT; | |
6145 TabListType.tp_doc = "vim tab page list"; | |
4319 | 6146 |
6147 vim_memset(&RangeType, 0, sizeof(RangeType)); | |
6148 RangeType.tp_name = "vim.range"; | |
6149 RangeType.tp_basicsize = sizeof(RangeObject); | |
4488 | 6150 RangeType.tp_dealloc = (destructor)RangeDestructor; |
6151 RangeType.tp_repr = (reprfunc)RangeRepr; | |
4319 | 6152 RangeType.tp_as_sequence = &RangeAsSeq; |
6153 RangeType.tp_as_mapping = &RangeAsMapping; | |
4611
49f0fcd9762c
updated for version 7.3.1053
Bram Moolenaar <bram@vim.org>
parents:
4609
diff
changeset
|
6154 RangeType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC; |
4319 | 6155 RangeType.tp_doc = "vim Range object"; |
6156 RangeType.tp_methods = RangeMethods; | |
4500 | 6157 RangeType.tp_traverse = (traverseproc)RangeTraverse; |
6158 RangeType.tp_clear = (inquiry)RangeClear; | |
4319 | 6159 #if PY_MAJOR_VERSION >= 3 |
4488 | 6160 RangeType.tp_getattro = (getattrofunc)RangeGetattro; |
4319 | 6161 RangeType.tp_alloc = call_PyType_GenericAlloc; |
6162 RangeType.tp_new = call_PyType_GenericNew; | |
6163 RangeType.tp_free = call_PyObject_Free; | |
6164 #else | |
4488 | 6165 RangeType.tp_getattr = (getattrfunc)RangeGetattr; |
4319 | 6166 #endif |
6167 | |
6168 vim_memset(&CurrentType, 0, sizeof(CurrentType)); | |
6169 CurrentType.tp_name = "vim.currentdata"; | |
6170 CurrentType.tp_basicsize = sizeof(CurrentObject); | |
6171 CurrentType.tp_flags = Py_TPFLAGS_DEFAULT; | |
6172 CurrentType.tp_doc = "vim current object"; | |
4599
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
6173 CurrentType.tp_methods = CurrentMethods; |
4319 | 6174 #if PY_MAJOR_VERSION >= 3 |
4488 | 6175 CurrentType.tp_getattro = (getattrofunc)CurrentGetattro; |
6176 CurrentType.tp_setattro = (setattrofunc)CurrentSetattro; | |
4319 | 6177 #else |
4488 | 6178 CurrentType.tp_getattr = (getattrfunc)CurrentGetattr; |
6179 CurrentType.tp_setattr = (setattrfunc)CurrentSetattr; | |
4319 | 6180 #endif |
6181 | |
6182 vim_memset(&DictionaryType, 0, sizeof(DictionaryType)); | |
6183 DictionaryType.tp_name = "vim.dictionary"; | |
6184 DictionaryType.tp_basicsize = sizeof(DictionaryObject); | |
4488 | 6185 DictionaryType.tp_dealloc = (destructor)DictionaryDestructor; |
4627
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
6186 DictionaryType.tp_as_sequence = &DictionaryAsSeq; |
4319 | 6187 DictionaryType.tp_as_mapping = &DictionaryAsMapping; |
4627
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
6188 DictionaryType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE; |
4319 | 6189 DictionaryType.tp_doc = "dictionary pushing modifications to vim structure"; |
6190 DictionaryType.tp_methods = DictionaryMethods; | |
4627
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
6191 DictionaryType.tp_iter = (getiterfunc)DictionaryIter; |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
6192 DictionaryType.tp_new = (newfunc)DictionaryConstructor; |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
6193 DictionaryType.tp_alloc = (allocfunc)PyType_GenericAlloc; |
4319 | 6194 #if PY_MAJOR_VERSION >= 3 |
4488 | 6195 DictionaryType.tp_getattro = (getattrofunc)DictionaryGetattro; |
6196 DictionaryType.tp_setattro = (setattrofunc)DictionarySetattro; | |
4319 | 6197 #else |
4488 | 6198 DictionaryType.tp_getattr = (getattrfunc)DictionaryGetattr; |
6199 DictionaryType.tp_setattr = (setattrfunc)DictionarySetattr; | |
4319 | 6200 #endif |
6201 | |
6202 vim_memset(&ListType, 0, sizeof(ListType)); | |
6203 ListType.tp_name = "vim.list"; | |
4488 | 6204 ListType.tp_dealloc = (destructor)ListDestructor; |
4319 | 6205 ListType.tp_basicsize = sizeof(ListObject); |
6206 ListType.tp_as_sequence = &ListAsSeq; | |
6207 ListType.tp_as_mapping = &ListAsMapping; | |
4629
e4e48d4ee040
updated for version 7.3.1062
Bram Moolenaar <bram@vim.org>
parents:
4627
diff
changeset
|
6208 ListType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE; |
4319 | 6209 ListType.tp_doc = "list pushing modifications to vim structure"; |
6210 ListType.tp_methods = ListMethods; | |
4488 | 6211 ListType.tp_iter = (getiterfunc)ListIter; |
4629
e4e48d4ee040
updated for version 7.3.1062
Bram Moolenaar <bram@vim.org>
parents:
4627
diff
changeset
|
6212 ListType.tp_new = (newfunc)ListConstructor; |
e4e48d4ee040
updated for version 7.3.1062
Bram Moolenaar <bram@vim.org>
parents:
4627
diff
changeset
|
6213 ListType.tp_alloc = (allocfunc)PyType_GenericAlloc; |
4319 | 6214 #if PY_MAJOR_VERSION >= 3 |
4488 | 6215 ListType.tp_getattro = (getattrofunc)ListGetattro; |
6216 ListType.tp_setattro = (setattrofunc)ListSetattro; | |
4319 | 6217 #else |
4488 | 6218 ListType.tp_getattr = (getattrfunc)ListGetattr; |
6219 ListType.tp_setattr = (setattrfunc)ListSetattr; | |
4319 | 6220 #endif |
6221 | |
6222 vim_memset(&FunctionType, 0, sizeof(FunctionType)); | |
4397 | 6223 FunctionType.tp_name = "vim.function"; |
4319 | 6224 FunctionType.tp_basicsize = sizeof(FunctionObject); |
4488 | 6225 FunctionType.tp_dealloc = (destructor)FunctionDestructor; |
6226 FunctionType.tp_call = (ternaryfunc)FunctionCall; | |
4631
4157fef7b950
updated for version 7.3.1063
Bram Moolenaar <bram@vim.org>
parents:
4629
diff
changeset
|
6227 FunctionType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE; |
4319 | 6228 FunctionType.tp_doc = "object that calls vim function"; |
6229 FunctionType.tp_methods = FunctionMethods; | |
4625
cb5c1e37ad4d
updated for version 7.3.1060
Bram Moolenaar <bram@vim.org>
parents:
4623
diff
changeset
|
6230 FunctionType.tp_repr = (reprfunc)FunctionRepr; |
4631
4157fef7b950
updated for version 7.3.1063
Bram Moolenaar <bram@vim.org>
parents:
4629
diff
changeset
|
6231 FunctionType.tp_new = (newfunc)FunctionConstructor; |
4157fef7b950
updated for version 7.3.1063
Bram Moolenaar <bram@vim.org>
parents:
4629
diff
changeset
|
6232 FunctionType.tp_alloc = (allocfunc)PyType_GenericAlloc; |
4319 | 6233 #if PY_MAJOR_VERSION >= 3 |
4488 | 6234 FunctionType.tp_getattro = (getattrofunc)FunctionGetattro; |
4319 | 6235 #else |
4488 | 6236 FunctionType.tp_getattr = (getattrfunc)FunctionGetattr; |
4319 | 6237 #endif |
6238 | |
4350 | 6239 vim_memset(&OptionsType, 0, sizeof(OptionsType)); |
6240 OptionsType.tp_name = "vim.options"; | |
6241 OptionsType.tp_basicsize = sizeof(OptionsObject); | |
5610 | 6242 OptionsType.tp_as_sequence = &OptionsAsSeq; |
4611
49f0fcd9762c
updated for version 7.3.1053
Bram Moolenaar <bram@vim.org>
parents:
4609
diff
changeset
|
6243 OptionsType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC; |
4350 | 6244 OptionsType.tp_doc = "object for manipulating options"; |
5610 | 6245 OptionsType.tp_iter = (getiterfunc)OptionsIter; |
4350 | 6246 OptionsType.tp_as_mapping = &OptionsAsMapping; |
4488 | 6247 OptionsType.tp_dealloc = (destructor)OptionsDestructor; |
6248 OptionsType.tp_traverse = (traverseproc)OptionsTraverse; | |
6249 OptionsType.tp_clear = (inquiry)OptionsClear; | |
4350 | 6250 |
4855
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
6251 vim_memset(&LoaderType, 0, sizeof(LoaderType)); |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
6252 LoaderType.tp_name = "vim.Loader"; |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
6253 LoaderType.tp_basicsize = sizeof(LoaderObject); |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
6254 LoaderType.tp_flags = Py_TPFLAGS_DEFAULT; |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
6255 LoaderType.tp_doc = "vim message object"; |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
6256 LoaderType.tp_methods = LoaderMethods; |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
6257 LoaderType.tp_dealloc = (destructor)LoaderDestructor; |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
6258 |
4319 | 6259 #if PY_MAJOR_VERSION >= 3 |
6260 vim_memset(&vimmodule, 0, sizeof(vimmodule)); | |
6261 vimmodule.m_name = "vim"; | |
6262 vimmodule.m_doc = "Vim Python interface\n"; | |
6263 vimmodule.m_size = -1; | |
6264 vimmodule.m_methods = VimMethods; | |
6265 #endif | |
6266 } | |
4494 | 6267 |
6268 #define PYTYPE_READY(type) \ | |
6269 if (PyType_Ready(&type)) \ | |
6270 return -1; | |
6271 | |
6272 static int | |
5166
467efeee8f9e
updated for version 7.4a.009
Bram Moolenaar <bram@vim.org>
parents:
5139
diff
changeset
|
6273 init_types(void) |
4494 | 6274 { |
6275 PYTYPE_READY(IterType); | |
6276 PYTYPE_READY(BufferType); | |
6277 PYTYPE_READY(RangeType); | |
6278 PYTYPE_READY(WindowType); | |
6279 PYTYPE_READY(TabPageType); | |
6280 PYTYPE_READY(BufMapType); | |
6281 PYTYPE_READY(WinListType); | |
6282 PYTYPE_READY(TabListType); | |
6283 PYTYPE_READY(CurrentType); | |
6284 PYTYPE_READY(DictionaryType); | |
6285 PYTYPE_READY(ListType); | |
6286 PYTYPE_READY(FunctionType); | |
6287 PYTYPE_READY(OptionsType); | |
6288 PYTYPE_READY(OutputType); | |
4851
96e154e825a7
updated for version 7.3.1172
Bram Moolenaar <bram@vim.org>
parents:
4833
diff
changeset
|
6289 PYTYPE_READY(LoaderType); |
4833
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
6290 return 0; |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
6291 } |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
6292 |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
6293 static int |
4922
8dd2769ab75c
updated for version 7.3.1206
Bram Moolenaar <bram@vim.org>
parents:
4918
diff
changeset
|
6294 init_sys_path(void) |
4833
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
6295 { |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
6296 PyObject *path; |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
6297 PyObject *path_hook; |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
6298 PyObject *path_hooks; |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
6299 |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
6300 if (!(path_hook = PyObject_GetAttrString(vim_module, "path_hook"))) |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
6301 return -1; |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
6302 |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
6303 if (!(path_hooks = PySys_GetObject("path_hooks"))) |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
6304 { |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
6305 PyErr_Clear(); |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
6306 path_hooks = PyList_New(1); |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
6307 PyList_SET_ITEM(path_hooks, 0, path_hook); |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
6308 if (PySys_SetObject("path_hooks", path_hooks)) |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
6309 { |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
6310 Py_DECREF(path_hooks); |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
6311 return -1; |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
6312 } |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
6313 Py_DECREF(path_hooks); |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
6314 } |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
6315 else if (PyList_Check(path_hooks)) |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
6316 { |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
6317 if (PyList_Append(path_hooks, path_hook)) |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
6318 { |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
6319 Py_DECREF(path_hook); |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
6320 return -1; |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
6321 } |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
6322 Py_DECREF(path_hook); |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
6323 } |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
6324 else |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
6325 { |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
6326 VimTryStart(); |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
6327 EMSG(_("Failed to set path hook: sys.path_hooks is not a list\n" |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
6328 "You should now do the following:\n" |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
6329 "- append vim.path_hook to sys.path_hooks\n" |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
6330 "- append vim.VIM_SPECIAL_PATH to sys.path\n")); |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
6331 VimTryEnd(); /* Discard the error */ |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
6332 Py_DECREF(path_hook); |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
6333 return 0; |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
6334 } |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
6335 |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
6336 if (!(path = PySys_GetObject("path"))) |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
6337 { |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
6338 PyErr_Clear(); |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
6339 path = PyList_New(1); |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
6340 Py_INCREF(vim_special_path_object); |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
6341 PyList_SET_ITEM(path, 0, vim_special_path_object); |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
6342 if (PySys_SetObject("path", path)) |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
6343 { |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
6344 Py_DECREF(path); |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
6345 return -1; |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
6346 } |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
6347 Py_DECREF(path); |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
6348 } |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
6349 else if (PyList_Check(path)) |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
6350 { |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
6351 if (PyList_Append(path, vim_special_path_object)) |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
6352 return -1; |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
6353 } |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
6354 else |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
6355 { |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
6356 VimTryStart(); |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
6357 EMSG(_("Failed to set path: sys.path is not a list\n" |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
6358 "You should now append vim.VIM_SPECIAL_PATH to sys.path")); |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
6359 VimTryEnd(); /* Discard the error */ |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
6360 } |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
6361 |
4494 | 6362 return 0; |
6363 } | |
6364 | |
6365 static BufMapObject TheBufferMap = | |
6366 { | |
6367 PyObject_HEAD_INIT(&BufMapType) | |
6368 }; | |
6369 | |
6370 static WinListObject TheWindowList = | |
6371 { | |
6372 PyObject_HEAD_INIT(&WinListType) | |
6373 NULL | |
6374 }; | |
6375 | |
6376 static CurrentObject TheCurrent = | |
6377 { | |
6378 PyObject_HEAD_INIT(&CurrentType) | |
6379 }; | |
6380 | |
6381 static TabListObject TheTabPageList = | |
6382 { | |
6383 PyObject_HEAD_INIT(&TabListType) | |
6384 }; | |
6385 | |
6386 static struct numeric_constant { | |
6387 char *name; | |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
6388 int val; |
4494 | 6389 } numeric_constants[] = { |
6390 {"VAR_LOCKED", VAR_LOCKED}, | |
6391 {"VAR_FIXED", VAR_FIXED}, | |
6392 {"VAR_SCOPE", VAR_SCOPE}, | |
6393 {"VAR_DEF_SCOPE", VAR_DEF_SCOPE}, | |
6394 }; | |
6395 | |
6396 static struct object_constant { | |
6397 char *name; | |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
6398 PyObject *valObject; |
4494 | 6399 } object_constants[] = { |
6400 {"buffers", (PyObject *)(void *)&TheBufferMap}, | |
6401 {"windows", (PyObject *)(void *)&TheWindowList}, | |
6402 {"tabpages", (PyObject *)(void *)&TheTabPageList}, | |
6403 {"current", (PyObject *)(void *)&TheCurrent}, | |
4496 | 6404 |
6405 {"Buffer", (PyObject *)&BufferType}, | |
6406 {"Range", (PyObject *)&RangeType}, | |
6407 {"Window", (PyObject *)&WindowType}, | |
6408 {"TabPage", (PyObject *)&TabPageType}, | |
6409 {"Dictionary", (PyObject *)&DictionaryType}, | |
6410 {"List", (PyObject *)&ListType}, | |
6411 {"Function", (PyObject *)&FunctionType}, | |
6412 {"Options", (PyObject *)&OptionsType}, | |
4855
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
6413 {"_Loader", (PyObject *)&LoaderType}, |
4494 | 6414 }; |
6415 | |
6416 #define ADD_OBJECT(m, name, obj) \ | |
4982
39980afcf54a
updated for version 7.3.1236
Bram Moolenaar <bram@vim.org>
parents:
4978
diff
changeset
|
6417 if (PyModule_AddObject(m, name, obj)) \ |
4494 | 6418 return -1; |
6419 | |
6420 #define ADD_CHECKED_OBJECT(m, name, obj) \ | |
6421 { \ | |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
6422 PyObject *valObject = obj; \ |
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
6423 if (!valObject) \ |
4494 | 6424 return -1; \ |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
6425 ADD_OBJECT(m, name, valObject); \ |
4494 | 6426 } |
6427 | |
6428 static int | |
4982
39980afcf54a
updated for version 7.3.1236
Bram Moolenaar <bram@vim.org>
parents:
4978
diff
changeset
|
6429 populate_module(PyObject *m) |
4494 | 6430 { |
4829
ff3935926449
updated for version 7.3.1161
Bram Moolenaar <bram@vim.org>
parents:
4754
diff
changeset
|
6431 int i; |
ff3935926449
updated for version 7.3.1161
Bram Moolenaar <bram@vim.org>
parents:
4754
diff
changeset
|
6432 PyObject *other_module; |
4831
b8eabb6a9687
updated for version 7.3.1162
Bram Moolenaar <bram@vim.org>
parents:
4829
diff
changeset
|
6433 PyObject *attr; |
4855
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
6434 PyObject *imp; |
4494 | 6435 |
6436 for (i = 0; i < (int)(sizeof(numeric_constants) | |
6437 / sizeof(struct numeric_constant)); | |
6438 ++i) | |
6439 ADD_CHECKED_OBJECT(m, numeric_constants[i].name, | |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
6440 PyInt_FromLong(numeric_constants[i].val)); |
4494 | 6441 |
6442 for (i = 0; i < (int)(sizeof(object_constants) | |
6443 / sizeof(struct object_constant)); | |
6444 ++i) | |
6445 { | |
4974
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
6446 PyObject *valObject; |
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
6447 |
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
6448 valObject = object_constants[i].valObject; |
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
6449 Py_INCREF(valObject); |
a594ce86b5ea
updated for version 7.3.1232
Bram Moolenaar <bram@vim.org>
parents:
4972
diff
changeset
|
6450 ADD_OBJECT(m, object_constants[i].name, valObject); |
4494 | 6451 } |
6452 | |
6453 if (!(VimError = PyErr_NewException("vim.error", NULL, NULL))) | |
6454 return -1; | |
6455 ADD_OBJECT(m, "error", VimError); | |
6456 | |
4627
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
6457 ADD_CHECKED_OBJECT(m, "vars", NEW_DICTIONARY(&globvardict)); |
18ba89e06fab
updated for version 7.3.1061
Bram Moolenaar <bram@vim.org>
parents:
4625
diff
changeset
|
6458 ADD_CHECKED_OBJECT(m, "vvars", NEW_DICTIONARY(&vimvardict)); |
4494 | 6459 ADD_CHECKED_OBJECT(m, "options", |
6460 OptionsNew(SREQ_GLOBAL, NULL, dummy_check, NULL)); | |
4704
542af01979be
updated for version 7.3.1099
Bram Moolenaar <bram@vim.org>
parents:
4702
diff
changeset
|
6461 |
4829
ff3935926449
updated for version 7.3.1161
Bram Moolenaar <bram@vim.org>
parents:
4754
diff
changeset
|
6462 if (!(other_module = PyImport_ImportModule("os"))) |
4704
542af01979be
updated for version 7.3.1099
Bram Moolenaar <bram@vim.org>
parents:
4702
diff
changeset
|
6463 return -1; |
4829
ff3935926449
updated for version 7.3.1161
Bram Moolenaar <bram@vim.org>
parents:
4754
diff
changeset
|
6464 ADD_OBJECT(m, "os", other_module); |
ff3935926449
updated for version 7.3.1161
Bram Moolenaar <bram@vim.org>
parents:
4754
diff
changeset
|
6465 |
ff3935926449
updated for version 7.3.1161
Bram Moolenaar <bram@vim.org>
parents:
4754
diff
changeset
|
6466 if (!(py_getcwd = PyObject_GetAttrString(other_module, "getcwd"))) |
4704
542af01979be
updated for version 7.3.1099
Bram Moolenaar <bram@vim.org>
parents:
4702
diff
changeset
|
6467 return -1; |
542af01979be
updated for version 7.3.1099
Bram Moolenaar <bram@vim.org>
parents:
4702
diff
changeset
|
6468 ADD_OBJECT(m, "_getcwd", py_getcwd) |
542af01979be
updated for version 7.3.1099
Bram Moolenaar <bram@vim.org>
parents:
4702
diff
changeset
|
6469 |
4829
ff3935926449
updated for version 7.3.1161
Bram Moolenaar <bram@vim.org>
parents:
4754
diff
changeset
|
6470 if (!(py_chdir = PyObject_GetAttrString(other_module, "chdir"))) |
4704
542af01979be
updated for version 7.3.1099
Bram Moolenaar <bram@vim.org>
parents:
4702
diff
changeset
|
6471 return -1; |
542af01979be
updated for version 7.3.1099
Bram Moolenaar <bram@vim.org>
parents:
4702
diff
changeset
|
6472 ADD_OBJECT(m, "_chdir", py_chdir); |
4982
39980afcf54a
updated for version 7.3.1236
Bram Moolenaar <bram@vim.org>
parents:
4978
diff
changeset
|
6473 if (!(attr = PyObject_GetAttrString(m, "chdir"))) |
4704
542af01979be
updated for version 7.3.1099
Bram Moolenaar <bram@vim.org>
parents:
4702
diff
changeset
|
6474 return -1; |
4831
b8eabb6a9687
updated for version 7.3.1162
Bram Moolenaar <bram@vim.org>
parents:
4829
diff
changeset
|
6475 if (PyObject_SetAttrString(other_module, "chdir", attr)) |
b8eabb6a9687
updated for version 7.3.1162
Bram Moolenaar <bram@vim.org>
parents:
4829
diff
changeset
|
6476 { |
b8eabb6a9687
updated for version 7.3.1162
Bram Moolenaar <bram@vim.org>
parents:
4829
diff
changeset
|
6477 Py_DECREF(attr); |
b8eabb6a9687
updated for version 7.3.1162
Bram Moolenaar <bram@vim.org>
parents:
4829
diff
changeset
|
6478 return -1; |
b8eabb6a9687
updated for version 7.3.1162
Bram Moolenaar <bram@vim.org>
parents:
4829
diff
changeset
|
6479 } |
b8eabb6a9687
updated for version 7.3.1162
Bram Moolenaar <bram@vim.org>
parents:
4829
diff
changeset
|
6480 Py_DECREF(attr); |
4704
542af01979be
updated for version 7.3.1099
Bram Moolenaar <bram@vim.org>
parents:
4702
diff
changeset
|
6481 |
4829
ff3935926449
updated for version 7.3.1161
Bram Moolenaar <bram@vim.org>
parents:
4754
diff
changeset
|
6482 if ((py_fchdir = PyObject_GetAttrString(other_module, "fchdir"))) |
4704
542af01979be
updated for version 7.3.1099
Bram Moolenaar <bram@vim.org>
parents:
4702
diff
changeset
|
6483 { |
542af01979be
updated for version 7.3.1099
Bram Moolenaar <bram@vim.org>
parents:
4702
diff
changeset
|
6484 ADD_OBJECT(m, "_fchdir", py_fchdir); |
4982
39980afcf54a
updated for version 7.3.1236
Bram Moolenaar <bram@vim.org>
parents:
4978
diff
changeset
|
6485 if (!(attr = PyObject_GetAttrString(m, "fchdir"))) |
4704
542af01979be
updated for version 7.3.1099
Bram Moolenaar <bram@vim.org>
parents:
4702
diff
changeset
|
6486 return -1; |
4831
b8eabb6a9687
updated for version 7.3.1162
Bram Moolenaar <bram@vim.org>
parents:
4829
diff
changeset
|
6487 if (PyObject_SetAttrString(other_module, "fchdir", attr)) |
b8eabb6a9687
updated for version 7.3.1162
Bram Moolenaar <bram@vim.org>
parents:
4829
diff
changeset
|
6488 { |
b8eabb6a9687
updated for version 7.3.1162
Bram Moolenaar <bram@vim.org>
parents:
4829
diff
changeset
|
6489 Py_DECREF(attr); |
b8eabb6a9687
updated for version 7.3.1162
Bram Moolenaar <bram@vim.org>
parents:
4829
diff
changeset
|
6490 return -1; |
b8eabb6a9687
updated for version 7.3.1162
Bram Moolenaar <bram@vim.org>
parents:
4829
diff
changeset
|
6491 } |
b8eabb6a9687
updated for version 7.3.1162
Bram Moolenaar <bram@vim.org>
parents:
4829
diff
changeset
|
6492 Py_DECREF(attr); |
4704
542af01979be
updated for version 7.3.1099
Bram Moolenaar <bram@vim.org>
parents:
4702
diff
changeset
|
6493 } |
4722
3534e9b4fa42
updated for version 7.3.1108
Bram Moolenaar <bram@vim.org>
parents:
4706
diff
changeset
|
6494 else |
3534e9b4fa42
updated for version 7.3.1108
Bram Moolenaar <bram@vim.org>
parents:
4706
diff
changeset
|
6495 PyErr_Clear(); |
4704
542af01979be
updated for version 7.3.1099
Bram Moolenaar <bram@vim.org>
parents:
4702
diff
changeset
|
6496 |
4833
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
6497 if (!(vim_special_path_object = PyString_FromString(vim_special_path))) |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
6498 return -1; |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
6499 |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
6500 ADD_OBJECT(m, "VIM_SPECIAL_PATH", vim_special_path_object); |
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
6501 |
4855
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
6502 if (!(imp = PyImport_ImportModule("imp"))) |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
6503 return -1; |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
6504 |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
6505 if (!(py_find_module = PyObject_GetAttrString(imp, "find_module"))) |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
6506 { |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
6507 Py_DECREF(imp); |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
6508 return -1; |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
6509 } |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
6510 |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
6511 if (!(py_load_module = PyObject_GetAttrString(imp, "load_module"))) |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
6512 { |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
6513 Py_DECREF(py_find_module); |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
6514 Py_DECREF(imp); |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
6515 return -1; |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
6516 } |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
6517 |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
6518 Py_DECREF(imp); |
52850ef928f8
updated for version 7.3.1174
Bram Moolenaar <bram@vim.org>
parents:
4851
diff
changeset
|
6519 |
4851
96e154e825a7
updated for version 7.3.1172
Bram Moolenaar <bram@vim.org>
parents:
4833
diff
changeset
|
6520 ADD_OBJECT(m, "_find_module", py_find_module); |
96e154e825a7
updated for version 7.3.1172
Bram Moolenaar <bram@vim.org>
parents:
4833
diff
changeset
|
6521 ADD_OBJECT(m, "_load_module", py_load_module); |
4833
70b1178dec79
updated for version 7.3.1163
Bram Moolenaar <bram@vim.org>
parents:
4831
diff
changeset
|
6522 |
4494 | 6523 return 0; |
6524 } |