Blob Blame History Raw
To: vim_dev@googlegroups.com
Subject: Patch 7.3.995
Fcc: outbox
From: Bram Moolenaar <Bram@moolenaar.net>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
------------

Patch 7.3.995
Problem:    Python: Module initialization is duplicated.
Solution:   Move to shared file. (ZyX)
Files:	    src/if_py_both.h, src/if_python3.c, src/if_python.c


*** ../vim-7.3.994/src/if_py_both.h	2013-05-21 19:01:51.000000000 +0200
--- src/if_py_both.h	2013-05-21 19:07:17.000000000 +0200
***************
*** 4181,4183 ****
--- 4181,4295 ----
      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;
+ }
*** ../vim-7.3.994/src/if_python3.c	2013-05-21 19:01:51.000000000 +0200
--- src/if_python3.c	2013-05-21 19:07:40.000000000 +0200
***************
*** 700,706 ****
   * Internal function prototypes.
   */
  
- static int PythonIO_Init(void);
  static PyObject *Py3Init_vim(void);
  
  /******************************************************
--- 700,705 ----
***************
*** 780,786 ****
  	get_py3_exceptions();
  #endif
  
! 	if (PythonIO_Init())
  	    goto fail;
  
  	globals = PyModule_GetDict(PyImport_AddModule("__main__"));
--- 779,785 ----
  	get_py3_exceptions();
  #endif
  
! 	if (PythonIO_Init_io())
  	    goto fail;
  
  	globals = PyModule_GetDict(PyImport_AddModule("__main__"));
***************
*** 811,817 ****
  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
       * not do anything in this case).
       */
      PythonIO_Flush();
--- 810,816 ----
  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).
       */
      PythonIO_Flush();
***************
*** 1008,1022 ****
      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
   */
--- 1007,1012 ----
***************
*** 1538,1585 ****
  }
  #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);
  
      /* Set sys.argv[] to avoid a crash in warn(). */
      PySys_SetArgv(1, argv);
--- 1528,1543 ----
  }
  #endif
  
      static PyObject *
  Py3Init_vim(void)
  {
      PyObject *mod;
! 
      /* 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(). */
      PySys_SetArgv(1, argv);
***************
*** 1588,1622 ****
      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())
  	return NULL;
  
      return mod;
--- 1546,1552 ----
      if (mod == NULL)
  	return NULL;
  
!     if (populate_module(mod, PyModule_AddObject))
  	return NULL;
  
      return mod;
*** ../vim-7.3.994/src/if_python.c	2013-05-21 18:30:29.000000000 +0200
--- src/if_python.c	2013-05-21 19:07:26.000000000 +0200
***************
*** 657,663 ****
   * Internal function prototypes.
   */
  
- static int PythonIO_Init(void);
  static int PythonMod_Init(void);
  
  
--- 657,662 ----
***************
*** 772,778 ****
  	get_exceptions();
  #endif
  
! 	if (PythonIO_Init())
  	    goto fail;
  
  	if (PythonMod_Init())
--- 771,777 ----
  	get_exceptions();
  #endif
  
! 	if (PythonIO_Init_io())
  	    goto fail;
  
  	if (PythonMod_Init())
***************
*** 806,812 ****
  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
       * not do anything in this case).
       */
      PythonIO_Flush();
--- 805,811 ----
  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).
       */
      PythonIO_Flush();
***************
*** 993,1009 ****
      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
   */
--- 992,997 ----
***************
*** 1242,1288 ****
  }
  #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 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);
  
      /* Set sys.argv[] to avoid a crash in warn(). */
      PySys_SetArgv(1, argv);
--- 1230,1255 ----
  }
  #endif
  
!     static int
! add_object(PyObject *dict, const char *name, PyObject *object)
  {
!     if (PyDict_SetItemString(dict, (char *) name, object))
! 	return -1;
!     Py_DECREF(object);
!     return 0;
! }
  
      static int
  PythonMod_Init(void)
  {
      PyObject *mod;
      PyObject *dict;
! 
      /* The special value is removed from sys.path in Python_Init(). */
      static char *(argv[2]) = {"/must>not&exist/foo", NULL};
  
!     if (init_types())
! 	return -1;
  
      /* Set sys.argv[] to avoid a crash in warn(). */
      PySys_SetArgv(1, argv);
***************
*** 1290,1320 ****
      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;
  }
  
  /*************************************************************************
--- 1257,1263 ----
      mod = Py_InitModule4("vim", VimMethods, (char *)NULL, (PyObject *)NULL, PYTHON_API_VERSION);
      dict = PyModule_GetDict(mod);
  
!     return populate_module(dict, add_object);
  }
  
  /*************************************************************************
*** ../vim-7.3.994/src/version.c	2013-05-21 19:01:51.000000000 +0200
--- src/version.c	2013-05-21 19:06:22.000000000 +0200
***************
*** 730,731 ****
--- 730,733 ----
  {   /* Add new patch number below this line */
+ /**/
+     995,
  /**/

-- 
System administrators are just like women: You can't live with them and you
can't live without them.

 /// Bram Moolenaar -- Bram@Moolenaar.net -- http://www.Moolenaar.net   \\\
///        sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
\\\  an exciting new programming language -- http://www.Zimbu.org        ///
 \\\            help me help AIDS victims -- http://ICCF-Holland.org    ///