changeset 4494:6d517f6e5f0b v7.3.995

updated for version 7.3.995 Problem: Python: Module initialization is duplicated. Solution: Move to shared file. (ZyX)
author Bram Moolenaar <bram@vim.org>
date Tue, 21 May 2013 19:11:01 +0200
parents 36ea23677c9c
children fe5605ef4d72
files src/if_py_both.h src/if_python.c src/if_python3.c src/version.c
diffstat 4 files changed, 133 insertions(+), 146 deletions(-) [+]
line wrap: on
line diff
--- a/src/if_py_both.h
+++ b/src/if_py_both.h
@@ -4181,3 +4181,115 @@ init_structs(void)
     vimmodule.m_methods = VimMethods;
 #endif
 }
+
+#define PYTYPE_READY(type) \
+    if (PyType_Ready(&type)) \
+	return -1;
+
+    static int
+init_types()
+{
+    PYTYPE_READY(IterType);
+    PYTYPE_READY(BufferType);
+    PYTYPE_READY(RangeType);
+    PYTYPE_READY(WindowType);
+    PYTYPE_READY(TabPageType);
+    PYTYPE_READY(BufMapType);
+    PYTYPE_READY(WinListType);
+    PYTYPE_READY(TabListType);
+    PYTYPE_READY(CurrentType);
+    PYTYPE_READY(DictionaryType);
+    PYTYPE_READY(ListType);
+    PYTYPE_READY(FunctionType);
+    PYTYPE_READY(OptionsType);
+    PYTYPE_READY(OutputType);
+    return 0;
+}
+
+static BufMapObject TheBufferMap =
+{
+    PyObject_HEAD_INIT(&BufMapType)
+};
+
+static WinListObject TheWindowList =
+{
+    PyObject_HEAD_INIT(&WinListType)
+    NULL
+};
+
+static CurrentObject TheCurrent =
+{
+    PyObject_HEAD_INIT(&CurrentType)
+};
+
+static TabListObject TheTabPageList =
+{
+    PyObject_HEAD_INIT(&TabListType)
+};
+
+static struct numeric_constant {
+    char	*name;
+    int		value;
+} numeric_constants[] = {
+    {"VAR_LOCKED",	VAR_LOCKED},
+    {"VAR_FIXED",	VAR_FIXED},
+    {"VAR_SCOPE",	VAR_SCOPE},
+    {"VAR_DEF_SCOPE",	VAR_DEF_SCOPE},
+};
+
+static struct object_constant {
+    char	*name;
+    PyObject	*value;
+} object_constants[] = {
+    {"buffers",  (PyObject *)(void *)&TheBufferMap},
+    {"windows",  (PyObject *)(void *)&TheWindowList},
+    {"tabpages", (PyObject *)(void *)&TheTabPageList},
+    {"current",  (PyObject *)(void *)&TheCurrent},
+};
+
+typedef int (*object_adder)(PyObject *, const char *, PyObject *);
+
+#define ADD_OBJECT(m, name, obj) \
+    if (add_object(m, name, obj)) \
+	return -1;
+
+#define ADD_CHECKED_OBJECT(m, name, obj) \
+    { \
+	PyObject	*value = obj; \
+	if (!value) \
+	    return -1; \
+	ADD_OBJECT(m, name, value); \
+    }
+
+    static int
+populate_module(PyObject *m, object_adder add_object)
+{
+    int i;
+
+    for (i = 0; i < (int)(sizeof(numeric_constants)
+					   / sizeof(struct numeric_constant));
+	    ++i)
+	ADD_CHECKED_OBJECT(m, numeric_constants[i].name,
+		PyInt_FromLong(numeric_constants[i].value));
+
+    for (i = 0; i < (int)(sizeof(object_constants)
+					    / sizeof(struct object_constant));
+	    ++i)
+    {
+	PyObject	*value;
+
+	value = object_constants[i].value;
+	Py_INCREF(value);
+	ADD_OBJECT(m, object_constants[i].name, value);
+    }
+
+    if (!(VimError = PyErr_NewException("vim.error", NULL, NULL)))
+	return -1;
+    ADD_OBJECT(m, "error", VimError);
+
+    ADD_CHECKED_OBJECT(m, "vars",  DictionaryNew(&globvardict));
+    ADD_CHECKED_OBJECT(m, "vvars", DictionaryNew(&vimvardict));
+    ADD_CHECKED_OBJECT(m, "options",
+	    OptionsNew(SREQ_GLOBAL, NULL, dummy_check, NULL));
+    return 0;
+}
--- a/src/if_python.c
+++ b/src/if_python.c
@@ -657,7 +657,6 @@ static PyObject *FunctionGetattr(PyObjec
  * Internal function prototypes.
  */
 
-static int PythonIO_Init(void);
 static int PythonMod_Init(void);
 
 
@@ -772,7 +771,7 @@ Python_Init(void)
 	get_exceptions();
 #endif
 
-	if (PythonIO_Init())
+	if (PythonIO_Init_io())
 	    goto fail;
 
 	if (PythonMod_Init())
@@ -806,7 +805,7 @@ Python_Init(void)
 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() has not completed successfully (it will
+     * if PythonIO_Init_io() has not completed successfully (it will
      * not do anything in this case).
      */
     PythonIO_Flush();
@@ -993,17 +992,6 @@ OutputGetattr(PyObject *self, char *name
     return Py_FindMethod(OutputMethods, self, name);
 }
 
-/***************/
-
-    static int
-PythonIO_Init(void)
-{
-    /* Fixups... */
-    PyType_Ready(&OutputType);
-
-    return PythonIO_Init_io();
-}
-
 /******************************************************
  * 3. Implementation of the Vim module for Python
  */
@@ -1242,47 +1230,26 @@ python_tabpage_free(tabpage_T *tab)
 }
 #endif
 
-static BufMapObject TheBufferMap =
-{
-    PyObject_HEAD_INIT(&BufMapType)
-};
-
-static WinListObject TheWindowList =
+    static int
+add_object(PyObject *dict, const char *name, PyObject *object)
 {
-    PyObject_HEAD_INIT(&WinListType)
-    NULL
-};
-
-static CurrentObject TheCurrent =
-{
-    PyObject_HEAD_INIT(&CurrentType)
-};
-
-static TabListObject TheTabPageList =
-{
-    PyObject_HEAD_INIT(&TabListType)
-};
+    if (PyDict_SetItemString(dict, (char *) name, object))
+	return -1;
+    Py_DECREF(object);
+    return 0;
+}
 
     static int
 PythonMod_Init(void)
 {
     PyObject *mod;
     PyObject *dict;
-    PyObject *tmp;
+
     /* The special value is removed from sys.path in Python_Init(). */
     static char *(argv[2]) = {"/must>not&exist/foo", NULL};
 
-    /* Fixups... */
-    PyType_Ready(&IterType);
-    PyType_Ready(&BufferType);
-    PyType_Ready(&RangeType);
-    PyType_Ready(&WindowType);
-    PyType_Ready(&TabPageType);
-    PyType_Ready(&BufMapType);
-    PyType_Ready(&WinListType);
-    PyType_Ready(&TabListType);
-    PyType_Ready(&CurrentType);
-    PyType_Ready(&OptionsType);
+    if (init_types())
+	return -1;
 
     /* Set sys.argv[] to avoid a crash in warn(). */
     PySys_SetArgv(1, argv);
@@ -1290,31 +1257,7 @@ PythonMod_Init(void)
     mod = Py_InitModule4("vim", VimMethods, (char *)NULL, (PyObject *)NULL, PYTHON_API_VERSION);
     dict = PyModule_GetDict(mod);
 
-    VimError = PyErr_NewException("vim.error", NULL, NULL);
-
-    PyDict_SetItemString(dict, "error", VimError);
-    PyDict_SetItemString(dict, "buffers", (PyObject *)(void *)&TheBufferMap);
-    PyDict_SetItemString(dict, "current", (PyObject *)(void *)&TheCurrent);
-    PyDict_SetItemString(dict, "windows", (PyObject *)(void *)&TheWindowList);
-    PyDict_SetItemString(dict, "tabpages", (PyObject *)(void *)&TheTabPageList);
-    tmp = DictionaryNew(&globvardict);
-    PyDict_SetItemString(dict, "vars",    tmp);
-    Py_DECREF(tmp);
-    tmp = DictionaryNew(&vimvardict);
-    PyDict_SetItemString(dict, "vvars",   tmp);
-    Py_DECREF(tmp);
-    tmp = OptionsNew(SREQ_GLOBAL, NULL, dummy_check, NULL);
-    PyDict_SetItemString(dict, "options", tmp);
-    Py_DECREF(tmp);
-    PyDict_SetItemString(dict, "VAR_LOCKED",    PyInt_FromLong(VAR_LOCKED));
-    PyDict_SetItemString(dict, "VAR_FIXED",     PyInt_FromLong(VAR_FIXED));
-    PyDict_SetItemString(dict, "VAR_SCOPE",     PyInt_FromLong(VAR_SCOPE));
-    PyDict_SetItemString(dict, "VAR_DEF_SCOPE", PyInt_FromLong(VAR_DEF_SCOPE));
-
-    if (PyErr_Occurred())
-	return -1;
-
-    return 0;
+    return populate_module(dict, add_object);
 }
 
 /*************************************************************************
--- a/src/if_python3.c
+++ b/src/if_python3.c
@@ -700,7 +700,6 @@ static struct PyModuleDef vimmodule;
  * Internal function prototypes.
  */
 
-static int PythonIO_Init(void);
 static PyObject *Py3Init_vim(void);
 
 /******************************************************
@@ -780,7 +779,7 @@ Python3_Init(void)
 	get_py3_exceptions();
 #endif
 
-	if (PythonIO_Init())
+	if (PythonIO_Init_io())
 	    goto fail;
 
 	globals = PyModule_GetDict(PyImport_AddModule("__main__"));
@@ -811,7 +810,7 @@ Python3_Init(void)
 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() has not completed successfully (it will
+     * if PythonIO_Init_io() has not completed successfully (it will
      * not do anything in this case).
      */
     PythonIO_Flush();
@@ -1008,15 +1007,6 @@ OutputSetattro(PyObject *self, PyObject 
     return OutputSetattr((OutputObject *)(self), name, val);
 }
 
-/***************/
-
-    static int
-PythonIO_Init(void)
-{
-    PyType_Ready(&OutputType);
-    return PythonIO_Init_io();
-}
-
 /******************************************************
  * 3. Implementation of the Vim module for Python
  */
@@ -1538,48 +1528,16 @@ python3_tabpage_free(tabpage_T *tab)
 }
 #endif
 
-static BufMapObject TheBufferMap =
-{
-    PyObject_HEAD_INIT(&BufMapType)
-};
-
-static WinListObject TheWindowList =
-{
-    PyObject_HEAD_INIT(&WinListType)
-    NULL
-};
-
-static CurrentObject TheCurrent =
-{
-    PyObject_HEAD_INIT(&CurrentType)
-};
-
-static TabListObject TheTabPageList =
-{
-    PyObject_HEAD_INIT(&TabListType)
-};
-
     static PyObject *
 Py3Init_vim(void)
 {
     PyObject *mod;
-    PyObject *tmp;
+
     /* The special value is removed from sys.path in Python3_Init(). */
     static wchar_t *(argv[2]) = {L"/must>not&exist/foo", NULL};
 
-    PyType_Ready(&IterType);
-    PyType_Ready(&BufferType);
-    PyType_Ready(&RangeType);
-    PyType_Ready(&WindowType);
-    PyType_Ready(&TabPageType);
-    PyType_Ready(&BufMapType);
-    PyType_Ready(&WinListType);
-    PyType_Ready(&TabListType);
-    PyType_Ready(&CurrentType);
-    PyType_Ready(&DictionaryType);
-    PyType_Ready(&ListType);
-    PyType_Ready(&FunctionType);
-    PyType_Ready(&OptionsType);
+    if (init_types())
+	return NULL;
 
     /* Set sys.argv[] to avoid a crash in warn(). */
     PySys_SetArgv(1, argv);
@@ -1588,35 +1546,7 @@ Py3Init_vim(void)
     if (mod == NULL)
 	return NULL;
 
-    VimError = PyErr_NewException("vim.error", NULL, NULL);
-
-    Py_INCREF(VimError);
-    PyModule_AddObject(mod, "error", VimError);
-    Py_INCREF((PyObject *)(void *)&TheBufferMap);
-    PyModule_AddObject(mod, "buffers", (PyObject *)(void *)&TheBufferMap);
-    Py_INCREF((PyObject *)(void *)&TheCurrent);
-    PyModule_AddObject(mod, "current", (PyObject *)(void *)&TheCurrent);
-    Py_INCREF((PyObject *)(void *)&TheWindowList);
-    PyModule_AddObject(mod, "windows", (PyObject *)(void *)&TheWindowList);
-    Py_INCREF((PyObject *)(void *)&TheTabPageList);
-    PyModule_AddObject(mod, "tabpages", (PyObject *)(void *)&TheTabPageList);
-
-    PyModule_AddObject(mod, "vars", DictionaryNew(&globvardict));
-    PyModule_AddObject(mod, "vvars", DictionaryNew(&vimvardict));
-    PyModule_AddObject(mod, "options",
-	    OptionsNew(SREQ_GLOBAL, NULL, dummy_check, NULL));
-
-#define ADD_INT_CONSTANT(name, value) \
-    tmp = PyLong_FromLong(value); \
-    Py_INCREF(tmp); \
-    PyModule_AddObject(mod, name, tmp)
-
-    ADD_INT_CONSTANT("VAR_LOCKED",     VAR_LOCKED);
-    ADD_INT_CONSTANT("VAR_FIXED",      VAR_FIXED);
-    ADD_INT_CONSTANT("VAR_SCOPE",      VAR_SCOPE);
-    ADD_INT_CONSTANT("VAR_DEF_SCOPE",  VAR_DEF_SCOPE);
-
-    if (PyErr_Occurred())
+    if (populate_module(mod, PyModule_AddObject))
 	return NULL;
 
     return mod;
--- a/src/version.c
+++ b/src/version.c
@@ -729,6 +729,8 @@ static char *(features[]) =
 static int included_patches[] =
 {   /* Add new patch number below this line */
 /**/
+    995,
+/**/
     994,
 /**/
     993,