summaryrefslogtreecommitdiff
path: root/src/if_python.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/if_python.c')
-rw-r--r--src/if_python.c278
1 files changed, 22 insertions, 256 deletions
diff --git a/src/if_python.c b/src/if_python.c
index 95be35724..70fd0b8e5 100644
--- a/src/if_python.c
+++ b/src/if_python.c
@@ -54,6 +54,8 @@
#undef main /* Defined in python.h - aargh */
#undef HAVE_FCNTL_H /* Clash with os_win32.h */
+static void init_structs(void);
+
#if !defined(FEAT_PYTHON) && defined(PROTO)
/* Use this to be able to generate prototypes without python being used. */
# define PyObject Py_ssize_t
@@ -412,6 +414,12 @@ get_exceptions()
}
#endif /* DYNAMIC_PYTHON */
+/*
+ * Include the code shared with if_python3.c
+ */
+#include "if_py_both.h"
+
+
/******************************************************
* Internal function prototypes.
*/
@@ -437,8 +445,6 @@ static int InsertBufferLines(buf_T *, PyInt, PyObject *, PyInt *);
static PyObject *LineToString(const char *);
static char *StringToLine(PyObject *);
-static int VimErrorCheck(void);
-
#define PyErr_SetVim(str) PyErr_SetString(VimError, str)
/******************************************************
@@ -486,20 +492,6 @@ Python_RestoreThread(void)
#endif
}
-/*
- * obtain a lock on the Vim data structures
- */
-static void Python_Lock_Vim(void)
-{
-}
-
-/*
- * release a lock on the Vim data structures
- */
-static void Python_Release_Vim(void)
-{
-}
-
void
python_end()
{
@@ -550,6 +542,8 @@ Python_Init(void)
}
#endif
+ init_structs();
+
#if !defined(MACOS) || defined(MACOS_X_UNIX)
Py_Initialize();
#else
@@ -743,52 +737,6 @@ ex_pyfile(exarg_T *eap)
static PyObject *OutputGetattr(PyObject *, char *);
static int OutputSetattr(PyObject *, char *, PyObject *);
-static PyObject *OutputWrite(PyObject *, PyObject *);
-static PyObject *OutputWritelines(PyObject *, PyObject *);
-
-typedef void (*writefn)(char_u *);
-static void writer(writefn fn, char_u *str, PyInt n);
-
-/* Output object definition
- */
-
-typedef struct
-{
- PyObject_HEAD
- long softspace;
- long error;
-} OutputObject;
-
-static struct PyMethodDef OutputMethods[] = {
- /* name, function, calling, documentation */
- {"write", OutputWrite, 1, "" },
- {"writelines", OutputWritelines, 1, "" },
- { NULL, NULL, 0, NULL }
-};
-
-static PyTypeObject OutputType = {
- PyObject_HEAD_INIT(0)
- 0,
- "message",
- sizeof(OutputObject),
- 0,
-
- (destructor) 0,
- (printfunc) 0,
- (getattrfunc) OutputGetattr,
- (setattrfunc) OutputSetattr,
- (cmpfunc) 0,
- (reprfunc) 0,
-
- 0, /* as number */
- 0, /* as sequence */
- 0, /* as mapping */
-
- (hashfunc) 0,
- (ternaryfunc) 0,
- (reprfunc) 0
-};
-
/*************/
static PyObject *
@@ -823,186 +771,15 @@ OutputSetattr(PyObject *self, char *name, PyObject *val)
return -1;
}
-/*************/
-
- static PyObject *
-OutputWrite(PyObject *self, PyObject *args)
-{
- int len;
- char *str;
- int error = ((OutputObject *)(self))->error;
-
- if (!PyArg_ParseTuple(args, "s#", &str, &len))
- return NULL;
-
- Py_BEGIN_ALLOW_THREADS
- Python_Lock_Vim();
- writer((writefn)(error ? emsg : msg), (char_u *)str, len);
- Python_Release_Vim();
- Py_END_ALLOW_THREADS
-
- Py_INCREF(Py_None);
- return Py_None;
-}
-
- static PyObject *
-OutputWritelines(PyObject *self, PyObject *args)
-{
- PyInt n;
- PyInt i;
- PyObject *list;
- int error = ((OutputObject *)(self))->error;
-
- if (!PyArg_ParseTuple(args, "O", &list))
- return NULL;
- Py_INCREF(list);
-
- if (!PyList_Check(list)) {
- PyErr_SetString(PyExc_TypeError, _("writelines() requires list of strings"));
- Py_DECREF(list);
- return NULL;
- }
-
- n = PyList_Size(list);
-
- for (i = 0; i < n; ++i)
- {
- PyObject *line = PyList_GetItem(list, i);
- char *str;
- PyInt len;
-
- if (!PyArg_Parse(line, "s#", &str, &len)) {
- PyErr_SetString(PyExc_TypeError, _("writelines() requires list of strings"));
- Py_DECREF(list);
- return NULL;
- }
-
- Py_BEGIN_ALLOW_THREADS
- Python_Lock_Vim();
- writer((writefn)(error ? emsg : msg), (char_u *)str, len);
- Python_Release_Vim();
- Py_END_ALLOW_THREADS
- }
-
- Py_DECREF(list);
- Py_INCREF(Py_None);
- return Py_None;
-}
-
-/* Output buffer management
- */
-
-static char_u *buffer = NULL;
-static PyInt buffer_len = 0;
-static PyInt buffer_size = 0;
-
-static writefn old_fn = NULL;
-
- static void
-buffer_ensure(PyInt n)
-{
- PyInt new_size;
- char_u *new_buffer;
-
- if (n < buffer_size)
- return;
-
- new_size = buffer_size;
- while (new_size < n)
- new_size += 80;
-
- if (new_size != buffer_size)
- {
- new_buffer = alloc((unsigned)new_size);
- if (new_buffer == NULL)
- return;
-
- if (buffer)
- {
- memcpy(new_buffer, buffer, buffer_len);
- vim_free(buffer);
- }
-
- buffer = new_buffer;
- buffer_size = new_size;
- }
-}
-
- static void
-PythonIO_Flush(void)
-{
- if (old_fn && buffer_len)
- {
- buffer[buffer_len] = 0;
- old_fn(buffer);
- }
-
- buffer_len = 0;
-}
-
- static void
-writer(writefn fn, char_u *str, PyInt n)
-{
- char_u *ptr;
-
- if (fn != old_fn && old_fn != NULL)
- PythonIO_Flush();
-
- old_fn = fn;
-
- while (n > 0 && (ptr = memchr(str, '\n', n)) != NULL)
- {
- PyInt len = ptr - str;
-
- buffer_ensure(buffer_len + len + 1);
-
- memcpy(buffer + buffer_len, str, len);
- buffer_len += len;
- buffer[buffer_len] = 0;
- fn(buffer);
- str = ptr + 1;
- n -= len + 1;
- buffer_len = 0;
- }
-
- /* Put the remaining text into the buffer for later printing */
- buffer_ensure(buffer_len + n + 1);
- memcpy(buffer + buffer_len, str, n);
- buffer_len += n;
-}
-
/***************/
-static OutputObject Output =
-{
- PyObject_HEAD_INIT(&OutputType)
- 0,
- 0
-};
-
-static OutputObject Error =
-{
- PyObject_HEAD_INIT(&OutputType)
- 0,
- 1
-};
-
static int
PythonIO_Init(void)
{
/* Fixups... */
OutputType.ob_type = &PyType_Type;
- PySys_SetObject("stdout", (PyObject *)(void *)&Output);
- PySys_SetObject("stderr", (PyObject *)(void *)&Error);
-
- if (PyErr_Occurred())
- {
- EMSG(_("E264: Python: Error initialising I/O objects"));
- return -1;
- }
-
- return 0;
+ return PythonIO_Init_io();
}
/******************************************************
@@ -1013,8 +790,6 @@ PythonIO_Init(void)
* -------------------------------------
*/
-static PyObject *VimError;
-
static PyObject *VimCommand(PyObject *, PyObject *);
static PyObject *VimEval(PyObject *, PyObject *);
@@ -1326,6 +1101,7 @@ VimEval(PyObject *self UNUSED, PyObject *args)
/* Common routines for buffers and line ranges
* -------------------------------------------
*/
+
static int
CheckBuffer(BufferObject *this)
{
@@ -3018,26 +2794,6 @@ StringToLine(PyObject *obj)
return save;
}
-/* Check to see whether a Vim error has been reported, or a keyboard
- * interrupt has been detected.
- */
- static int
-VimErrorCheck(void)
-{
- if (got_int)
- {
- PyErr_SetNone(PyExc_KeyboardInterrupt);
- return 1;
- }
- else if (did_emsg && !PyErr_Occurred())
- {
- PyErr_SetNone(VimError);
- return 1;
- }
-
- return 0;
-}
-
/* Don't generate a prototype for the next function, it generates an error on
* newer Python versions. */
@@ -3049,3 +2805,13 @@ Py_GetProgramName(void)
return "vim";
}
#endif /* Python 1.4 */
+
+ static void
+init_structs(void)
+{
+ vim_memset(&OutputType, 0, sizeof(OutputType));
+ OutputType.tp_name = "message";
+ OutputType.tp_basicsize = sizeof(OutputObject);
+ OutputType.tp_getattr = OutputGetattr;
+ OutputType.tp_setattr = OutputSetattr;
+}