summaryrefslogtreecommitdiffstats
path: root/src/if_python3.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/if_python3.c')
-rw-r--r--src/if_python3.c312
1 files changed, 145 insertions, 167 deletions
diff --git a/src/if_python3.c b/src/if_python3.c
index 26b973b9c0..8c22514b74 100644
--- a/src/if_python3.c
+++ b/src/if_python3.c
@@ -22,13 +22,12 @@
* Adaptations to support both python3.x and python2.x
*/
-/* uncomment this if used with the debug version of python */
-/* #define Py_DEBUG */
-/* Note: most of time you can add -DPy_DEBUG to CFLAGS in place of uncommenting
- */
-/* uncomment this if used with the debug version of python, but without its
- * allocator */
-/* #define Py_DEBUG_NO_PYMALLOC */
+// uncomment this if used with the debug version of python
+// #define Py_DEBUG
+// Note: most of time you can add -DPy_DEBUG to CFLAGS in place of uncommenting
+// uncomment this if used with the debug version of python, but without its
+// allocator
+// #define Py_DEBUG_NO_PYMALLOC
#include "vim.h"
@@ -56,30 +55,30 @@
# undef HAVE_PUTENV
#endif
#ifdef HAVE_STDARG_H
-# undef HAVE_STDARG_H /* Python's config.h defines it as well. */
+# undef HAVE_STDARG_H // Python's config.h defines it as well.
#endif
-#ifdef _POSIX_C_SOURCE /* defined in feature.h */
+#ifdef _POSIX_C_SOURCE // defined in feature.h
# undef _POSIX_C_SOURCE
#endif
#ifdef _XOPEN_SOURCE
-# undef _XOPEN_SOURCE /* pyconfig.h defines it as well. */
+# undef _XOPEN_SOURCE // pyconfig.h defines it as well.
#endif
#define PY_SSIZE_T_CLEAN
#include <Python.h>
-#undef main /* Defined in python.h - aargh */
-#undef HAVE_FCNTL_H /* Clash with os_win32.h */
+#undef main // Defined in python.h - aargh
+#undef HAVE_FCNTL_H // Clash with os_win32.h
-/* The "surrogateescape" error handler is new in Python 3.1 */
+// The "surrogateescape" error handler is new in Python 3.1
#if PY_VERSION_HEX >= 0x030100f0
# define CODEC_ERROR_HANDLER "surrogateescape"
#else
# define CODEC_ERROR_HANDLER NULL
#endif
-/* Python 3 does not support CObjects, always use Capsules */
+// Python 3 does not support CObjects, always use Capsules
#define PY_USE_CAPSULE
#define PyInt Py_ssize_t
@@ -432,9 +431,9 @@ static void(*py3_PyObject_GC_Del)(void *);
static void(*py3_PyObject_GC_UnTrack)(void *);
static int (*py3_PyType_IsSubtype)(PyTypeObject *, PyTypeObject *);
-static HINSTANCE hinstPy3 = 0; /* Instance of python.dll */
+static HINSTANCE hinstPy3 = 0; // Instance of python.dll
-/* Imported exception objects */
+// Imported exception objects
static PyObject *p3imp_PyExc_AttributeError;
static PyObject *p3imp_PyExc_IndexError;
static PyObject *p3imp_PyExc_KeyError;
@@ -664,9 +663,9 @@ py3_runtime_link_init(char *libname, int verbose)
(PYTHON_PROC *)&py3_PyUnicode_AsEncodedString;
# if !(defined(PY_NO_RTLD_GLOBAL) && defined(PY3_NO_RTLD_GLOBAL)) && defined(UNIX) && defined(FEAT_PYTHON)
- /* Can't have Python and Python3 loaded at the same time.
- * It cause a crash, because RTLD_GLOBAL is needed for
- * standard C extension libraries of one or both python versions. */
+ // Can't have Python and Python3 loaded at the same time.
+ // It cause a crash, because RTLD_GLOBAL is needed for
+ // standard C extension libraries of one or both python versions.
if (python_loaded())
{
if (verbose)
@@ -699,8 +698,8 @@ py3_runtime_link_init(char *libname, int verbose)
}
}
- /* Load unicode functions separately as only the ucs2 or the ucs4 functions
- * will be present in the library. */
+ // Load unicode functions separately as only the ucs2 or the ucs4 functions
+ // will be present in the library.
# if PY_VERSION_HEX >= 0x030300f0
*ucs_from_string = symbol_from_dll(hinstPy3, "PyUnicode_FromString");
*ucs_decode = symbol_from_dll(hinstPy3, "PyUnicode_Decode");
@@ -746,7 +745,8 @@ python3_enabled(int verbose)
return py3_runtime_link_init((char *)p_py3dll, verbose) == OK;
}
-/* Load the standard Python exceptions - don't import the symbols from the
+/*
+ * Load the standard Python exceptions - don't import the symbols from the
* DLL, as this can cause errors (importing data symbols is not reliable).
*/
static void
@@ -776,7 +776,7 @@ get_py3_exceptions(void)
Py_XINCREF(p3imp_PyExc_OverflowError);
Py_XDECREF(exmod);
}
-#endif /* DYNAMIC_PYTHON3 */
+#endif // DYNAMIC_PYTHON3
static int py3initialised = 0;
#define PYINITIALISED py3initialised
@@ -843,22 +843,20 @@ static struct PyModuleDef vimmodule;
#define PY3OBJ_DELETED(obj) (obj->ob_base.ob_refcnt<=0)
-/******************************************************
- * Internal function prototypes.
- */
+///////////////////////////////////////////////////////
+// Internal function prototypes.
static PyObject *Py3Init_vim(void);
-/******************************************************
- * 1. Python interpreter main program.
- */
+///////////////////////////////////////////////////////
+// 1. Python interpreter main program.
void
python3_end(void)
{
static int recurse = 0;
- /* If a crash occurs while doing this, don't try again. */
+ // If a crash occurs while doing this, don't try again.
if (recurse != 0)
return;
@@ -870,7 +868,7 @@ python3_end(void)
#endif
if (Py_IsInitialized())
{
- /* acquire lock before finalizing */
+ // acquire lock before finalizing
PyGILState_Ensure();
Py_Finalize();
@@ -912,7 +910,7 @@ Python3_Init(void)
{
size_t len = mbstowcs(NULL, (char *)p_py3home, 0) + 1;
- /* The string must not change later, make a copy in static memory. */
+ // The string must not change later, make a copy in static memory.
py_home_buf = ALLOC_MULT(wchar_t, len);
if (py_home_buf != NULL && mbstowcs(
py_home_buf, (char *)p_py3home, len) != (size_t)-1)
@@ -927,10 +925,10 @@ Python3_Init(void)
Py_Initialize();
- /* Initialise threads, and below save the state using
- * PyEval_SaveThread. Without the call to PyEval_SaveThread, thread
- * specific state (such as the system trace hook), will be lost
- * between invocations of Python code. */
+ // Initialise threads, and below save the state using
+ // PyEval_SaveThread. Without the call to PyEval_SaveThread, thread
+ // specific state (such as the system trace hook), will be lost
+ // between invocations of Python code.
PyEval_InitThreads();
#ifdef DYNAMIC_PYTHON3
get_py3_exceptions();
@@ -941,22 +939,20 @@ Python3_Init(void)
globals = PyModule_GetDict(PyImport_AddModule("__main__"));
- /* Remove the element from sys.path that was added because of our
- * argv[0] value in Py3Init_vim(). Previously we used an empty
- * string, but depending on the OS we then get an empty entry or
- * the current directory in sys.path.
- * Only after vim has been imported, the element does exist in
- * sys.path.
- */
+ // Remove the element from sys.path that was added because of our
+ // argv[0] value in Py3Init_vim(). Previously we used an empty
+ // string, but depending on the OS we then get an empty entry or
+ // the current directory in sys.path.
+ // Only after vim has been imported, the element does exist in
+ // sys.path.
PyRun_SimpleString("import vim; import sys; sys.path = list(filter(lambda x: not x.endswith('must>not&exist'), sys.path))");
- /* lock is created and acquired in PyEval_InitThreads() and thread
- * state is created in Py_Initialize()
- * there _PyGILState_NoteThreadState() also sets gilcounter to 1
- * (python must have threads enabled!)
- * so the following does both: unlock GIL and save thread state in TLS
- * without deleting thread state
- */
+ // lock is created and acquired in PyEval_InitThreads() and thread
+ // state is created in Py_Initialize()
+ // there _PyGILState_NoteThreadState() also sets gilcounter to 1
+ // (python must have threads enabled!)
+ // so the following does both: unlock GIL and save thread state in TLS
+ // without deleting thread state
PyEval_SaveThread();
py3initialised = 1;
@@ -965,11 +961,10 @@ Python3_Init(void)
return 0;
fail:
- /* We call PythonIO_Flush() here to print any Python errors.
- * This is OK, as it is possible to call this function even
- * if PythonIO_Init_io() has not completed successfully (it will
- * not do anything in this case).
- */
+ // We call PythonIO_Flush() here to print any Python errors.
+ // This is OK, as it is possible to call this function even
+ // if PythonIO_Init_io() has not completed successfully (it will
+ // not do anything in this case).
PythonIO_Flush();
return -1;
}
@@ -995,16 +990,16 @@ DoPyCommand(const char *cmd, rangeinitializer init_range, runner run, void *arg)
init_range(arg);
- Python_Release_Vim(); /* leave vim */
+ Python_Release_Vim(); // leave Vim
#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
- /* Python only works properly when the LC_NUMERIC locale is "C". */
+ // Python only works properly when the LC_NUMERIC locale is "C".
saved_locale = setlocale(LC_NUMERIC, NULL);
if (saved_locale == NULL || STRCMP(saved_locale, "C") == 0)
saved_locale = NULL;
else
{
- /* Need to make a copy, value may change when setting new locale. */
+ // Need to make a copy, value may change when setting new locale.
saved_locale = (char *)vim_strsave((char_u *)saved_locale);
(void)setlocale(LC_NUMERIC, "C");
}
@@ -1012,8 +1007,8 @@ DoPyCommand(const char *cmd, rangeinitializer init_range, runner run, void *arg)
pygilstate = PyGILState_Ensure();
- /* PyRun_SimpleString expects a UTF-8 string. Wrong encoding may cause
- * SyntaxError (unicode error). */
+ // PyRun_SimpleString expects a UTF-8 string. Wrong encoding may cause
+ // SyntaxError (unicode error).
cmdstr = PyUnicode_Decode(cmd, strlen(cmd),
(char *)ENC_OPT, CODEC_ERROR_HANDLER);
cmdbytes = PyUnicode_AsEncodedString(cmdstr, "utf-8", CODEC_ERROR_HANDLER);
@@ -1032,11 +1027,11 @@ DoPyCommand(const char *cmd, rangeinitializer init_range, runner run, void *arg)
}
#endif
- Python_Lock_Vim(); /* enter vim */
+ Python_Lock_Vim(); // enter Vim
PythonIO_Flush();
theend:
- return; /* keeps lint happy */
+ return; // keeps lint happy
}
/*
@@ -1077,22 +1072,21 @@ ex_py3file(exarg_T *eap)
if (p_pyx == 0)
p_pyx = 3;
- /* Have to do it like this. PyRun_SimpleFile requires you to pass a
- * stdio file pointer, but Vim and the Python DLL are compiled with
- * different options under Windows, meaning that stdio pointers aren't
- * compatible between the two. Yuk.
- *
- * construct: exec(compile(open('a_filename', 'rb').read(), 'a_filename', 'exec'))
- *
- * Using bytes so that Python can detect the source encoding as it normally
- * does. The doc does not say "compile" accept bytes, though.
- *
- * We need to escape any backslashes or single quotes in the file name, so that
- * Python won't mangle the file name.
- */
+ // Have to do it like this. PyRun_SimpleFile requires you to pass a
+ // stdio file pointer, but Vim and the Python DLL are compiled with
+ // different options under Windows, meaning that stdio pointers aren't
+ // compatible between the two. Yuk.
+ //
+ // construct: exec(compile(open('a_filename', 'rb').read(), 'a_filename', 'exec'))
+ //
+ // Using bytes so that Python can detect the source encoding as it normally
+ // does. The doc does not say "compile" accept bytes, though.
+ //
+ // We need to escape any backslashes or single quotes in the file name, so that
+ // Python won't mangle the file name.
strcpy(buffer, "exec(compile(open('");
- p = buffer + 19; /* size of "exec(compile(open('" */
+ p = buffer + 19; // size of "exec(compile(open('"
for (i=0; i<2; ++i)
{
@@ -1103,7 +1097,7 @@ ex_py3file(exarg_T *eap)
*p++ = '\\';
*p++ = *file++;
}
- /* If we didn't finish the file name, we hit a buffer overflow */
+ // If we didn't finish the file name, we hit a buffer overflow
if (*file != '\0')
return;
if (i==0)
@@ -1119,7 +1113,7 @@ ex_py3file(exarg_T *eap)
}
- /* Execute the file */
+ // Execute the file
DoPyCommand(buffer,
(rangeinitializer) init_range_cmd,
(runner) run_cmd,
@@ -1138,12 +1132,10 @@ ex_py3do(exarg_T *eap)
(void *)eap);
}
-/******************************************************
- * 2. Python output stream: writes output via [e]msg().
- */
+///////////////////////////////////////////////////////
+// 2. Python output stream: writes output via [e]msg().
-/* Implementation functions
- */
+// Implementation functions
static PyObject *
OutputGetattro(PyObject *self, PyObject *nameobj)
@@ -1168,28 +1160,24 @@ OutputSetattro(PyObject *self, PyObject *nameobj, PyObject *val)
return OutputSetattr((OutputObject *)(self), name, val);
}
-/******************************************************
- * 3. Implementation of the Vim module for Python
- */
+///////////////////////////////////////////////////////
+// 3. Implementation of the Vim module for Python
-/* Window type - Implementation functions
- * --------------------------------------
- */
+// Window type - Implementation functions
+// --------------------------------------
#define WindowType_Check(obj) ((obj)->ob_base.ob_type == &WindowType)
-/* Buffer type - Implementation functions
- * --------------------------------------
- */
+// Buffer type - Implementation functions
+// --------------------------------------
#define BufferType_Check(obj) ((obj)->ob_base.ob_type == &BufferType)
static PyObject* BufferSubscript(PyObject *self, PyObject *idx);
static Py_ssize_t BufferAsSubscript(PyObject *self, PyObject *idx, PyObject *val);
-/* Line range type - Implementation functions
- * --------------------------------------
- */
+// Line range type - Implementation functions
+// --------------------------------------
#define RangeType_Check(obj) ((obj)->ob_base.ob_type == &RangeType)
@@ -1197,21 +1185,20 @@ static PyObject* RangeSubscript(PyObject *self, PyObject *idx);
static Py_ssize_t RangeAsItem(PyObject *, Py_ssize_t, PyObject *);
static Py_ssize_t RangeAsSubscript(PyObject *self, PyObject *idx, PyObject *val);
-/* Current objects type - Implementation functions
- * -----------------------------------------------
- */
+// Current objects type - Implementation functions
+// -----------------------------------------------
static PySequenceMethods BufferAsSeq = {
- (lenfunc) BufferLength, /* sq_length, len(x) */
- (binaryfunc) 0, /* sq_concat, x+y */
- (ssizeargfunc) 0, /* sq_repeat, x*n */
- (ssizeargfunc) BufferItem, /* sq_item, x[i] */
- 0, /* was_sq_slice, x[i:j] */
- 0, /* sq_ass_item, x[i]=v */
- 0, /* sq_ass_slice, x[i:j]=v */
- 0, /* sq_contains */
- 0, /* sq_inplace_concat */
- 0, /* sq_inplace_repeat */
+ (lenfunc) BufferLength, // sq_length, len(x)
+ (binaryfunc) 0, // sq_concat, x+y
+ (ssizeargfunc) 0, // sq_repeat, x*n
+ (ssizeargfunc) BufferItem, // sq_item, x[i]
+ 0, // was_sq_slice, x[i:j]
+ 0, // sq_ass_item, x[i]=v
+ 0, // sq_ass_slice, x[i:j]=v
+ 0, // sq_contains
+ 0, // sq_inplace_concat
+ 0, // sq_inplace_repeat
};
static PyMappingMethods BufferAsMapping = {
@@ -1221,8 +1208,7 @@ static PyMappingMethods BufferAsMapping = {
};
-/* Buffer object
- */
+// Buffer object
static PyObject *
BufferGetattro(PyObject *self, PyObject *nameobj)
@@ -1252,7 +1238,7 @@ BufferSetattro(PyObject *self, PyObject *nameobj, PyObject *val)
return BufferSetattr((BufferObject *)(self), name, val);
}
-/******************/
+//////////////////
static PyObject *
BufferSubscript(PyObject *self, PyObject* idx)
@@ -1315,16 +1301,16 @@ BufferAsSubscript(PyObject *self, PyObject* idx, PyObject* val)
}
static PySequenceMethods RangeAsSeq = {
- (lenfunc) RangeLength, /* sq_length, len(x) */
- (binaryfunc) 0, /* RangeConcat, sq_concat, x+y */
- (ssizeargfunc) 0, /* RangeRepeat, sq_repeat, x*n */
- (ssizeargfunc) RangeItem, /* sq_item, x[i] */
- 0, /* was_sq_slice, x[i:j] */
- (ssizeobjargproc) RangeAsItem, /* sq_as_item, x[i]=v */
- 0, /* sq_ass_slice, x[i:j]=v */
- 0, /* sq_contains */
- 0, /* sq_inplace_concat */
- 0, /* sq_inplace_repeat */
+ (lenfunc) RangeLength, // sq_length, len(x)
+ (binaryfunc) 0, // RangeConcat, sq_concat, x+y
+ (ssizeargfunc) 0, // RangeRepeat, sq_repeat, x*n
+ (ssizeargfunc) RangeItem, // sq_item, x[i]
+ 0, // was_sq_slice, x[i:j]
+ (ssizeobjargproc) RangeAsItem, // sq_as_item, x[i]=v
+ 0, // sq_ass_slice, x[i:j]=v
+ 0, // sq_contains
+ 0, // sq_inplace_concat
+ 0, // sq_inplace_repeat
};
static PyMappingMethods RangeAsMapping = {
@@ -1333,8 +1319,7 @@ static PyMappingMethods RangeAsMapping = {
/* mp_ass_subscript */ (objobjargproc)RangeAsSubscript,
};
-/* Line range object - Implementation
- */
+// Line range object - Implementation
static PyObject *
RangeGetattro(PyObject *self, PyObject *nameobj)
@@ -1349,7 +1334,7 @@ RangeGetattro(PyObject *self, PyObject *nameobj)
return PyObject_GenericGetAttr(self, nameobj);
}
-/****************/
+////////////////
static Py_ssize_t
RangeAsItem(PyObject *self, Py_ssize_t n, PyObject *val)
@@ -1420,8 +1405,7 @@ RangeAsSubscript(PyObject *self, PyObject *idx, PyObject *val)
}
}
-/* TabPage object - Implementation
- */
+// TabPage object - Implementation
static PyObject *
TabPageGetattro(PyObject *self, PyObject *nameobj)
@@ -1443,8 +1427,7 @@ TabPageGetattro(PyObject *self, PyObject *nameobj)
return PyObject_GenericGetAttr(self, nameobj);
}
-/* Window object - Implementation
- */
+// Window object - Implementation
static PyObject *
WindowGetattro(PyObject *self, PyObject *nameobj)
@@ -1474,39 +1457,38 @@ WindowSetattro(PyObject *self, PyObject *nameobj, PyObject *val)
return WindowSetattr((WindowObject *)(self), name, val);
}
-/* Tab page list object - Definitions
- */
+// Tab page list object - Definitions
static PySequenceMethods TabListAsSeq = {
- (lenfunc) TabListLength, /* sq_length, len(x) */
- (binaryfunc) 0, /* sq_concat, x+y */
- (ssizeargfunc) 0, /* sq_repeat, x*n */
- (ssizeargfunc) TabListItem, /* sq_item, x[i] */
- 0, /* sq_slice, x[i:j] */
- (ssizeobjargproc)0, /* sq_as_item, x[i]=v */
- 0, /* sq_ass_slice, x[i:j]=v */
- 0, /* sq_contains */
- 0, /* sq_inplace_concat */
- 0, /* sq_inplace_repeat */
+ (lenfunc) TabListLength, // sq_length, len(x)
+ (binaryfunc) 0, // sq_concat, x+y
+ (ssizeargfunc) 0, // sq_repeat, x*n
+ (ssizeargfunc) TabListItem, // sq_item, x[i]
+ 0, // sq_slice, x[i:j]
+ (ssizeobjargproc)0, // sq_as_item, x[i]=v
+ 0, // sq_ass_slice, x[i:j]=v
+ 0, // sq_contains
+ 0, // sq_inplace_concat
+ 0, // sq_inplace_repeat
};
-/* Window list object - Definitions
- */
+// Window list object - Definitions
static PySequenceMethods WinListAsSeq = {
- (lenfunc) WinListLength, /* sq_length, len(x) */
- (binaryfunc) 0, /* sq_concat, x+y */
- (ssizeargfunc) 0, /* sq_repeat, x*n */
- (ssizeargfunc) WinListItem, /* sq_item, x[i] */
- 0, /* sq_slice, x[i:j] */
- (ssizeobjargproc)0, /* sq_as_item, x[i]=v */
- 0, /* sq_ass_slice, x[i:j]=v */
- 0, /* sq_contains */
- 0, /* sq_inplace_concat */
- 0, /* sq_inplace_repeat */
+ (lenfunc) WinListLength, // sq_length, len(x)
+ (binaryfunc) 0, // sq_concat, x+y
+ (ssizeargfunc) 0, // sq_repeat, x*n
+ (ssizeargfunc) WinListItem, // sq_item, x[i]
+ 0, // sq_slice, x[i:j]
+ (ssizeobjargproc)0, // sq_as_item, x[i]=v
+ 0, // sq_ass_slice, x[i:j]=v
+ 0, // sq_contains
+ 0, // sq_inplace_concat
+ 0, // sq_inplace_repeat
};
-/* Current items object - Implementation
+/*
+ * Current items object - Implementation
*/
static PyObject *
CurrentGetattro(PyObject *self, PyObject *nameobj)
@@ -1525,8 +1507,7 @@ CurrentSetattro(PyObject *self, PyObject *nameobj, PyObject *value)
return CurrentSetattr(self, name, value);
}
-/* Dictionary object - Definitions
- */
+// Dictionary object - Definitions
static PyObject *
DictionaryGetattro(PyObject *self, PyObject *nameobj)
@@ -1550,8 +1531,7 @@ DictionarySetattro(PyObject *self, PyObject *nameobj, PyObject *val)
return DictionarySetattr((DictionaryObject *)(self), name, val);
}
-/* List object - Definitions
- */
+// List object - Definitions
static PyObject *
ListGetattro(PyObject *self, PyObject *nameobj)
@@ -1571,8 +1551,7 @@ ListSetattro(PyObject *self, PyObject *nameobj, PyObject *val)
return ListSetattr((ListObject *)(self), name, val);
}
-/* Function object - Definitions
- */
+// Function object - Definitions
static PyObject *
FunctionGetattro(PyObject *self, PyObject *nameobj)
@@ -1589,8 +1568,7 @@ FunctionGetattro(PyObject *self, PyObject *nameobj)
return PyObject_GenericGetAttr(self, nameobj);
}
-/* External interface
- */
+// External interface
void
python3_buffer_free(buf_T *buf)
@@ -1628,13 +1606,13 @@ python3_tabpage_free(tabpage_T *tab)
static PyObject *
Py3Init_vim(void)
{
- /* The special value is removed from sys.path in Python3_Init(). */
+ // The special value is removed from sys.path in Python3_Init().
static wchar_t *(argv[2]) = {L"/must>not&exist/foo", NULL};
if (init_types())
return NULL;
- /* Set sys.argv[] to avoid a crash in warn(). */
+ // Set sys.argv[] to avoid a crash in warn().
PySys_SetArgv(1, argv);
if ((vim_module = PyModule_Create(&vimmodule)) == NULL)
@@ -1649,11 +1627,11 @@ Py3Init_vim(void)
return vim_module;
}
-/*************************************************************************
- * 4. Utility functions for handling the interface between Vim and Python.
- */
+//////////////////////////////////////////////////////////////////////////
+// 4. Utility functions for handling the interface between Vim and Python.
-/* Convert a Vim line into a Python string.
+/*
+ * Convert a Vim line into a Python string.
* All internal newlines are replaced by null characters.
*
* On errors, the Python exception data is set, and NULL is returned.