Loading ...
Sorry, an error occurred while loading the content.

Re: [PATCH] Refactoring: less type casts and less magic variables

Expand Messages
  • ZyX
    ... I meant “magic numbers”. -- -- You received this message from the vim_dev maillist. Do not top-post! Type your reply below the text you are replying
    Message 1 of 7 , May 18, 2013
    • 0 Attachment
      > [PATCH] Refactoring: less type casts and less magic variables

      I meant “magic numbers”.

      --
      --
      You received this message from the "vim_dev" maillist.
      Do not top-post! Type your reply below the text you are replying to.
      For more information, visit http://www.vim.org/maillist.php

      ---
      You received this message because you are subscribed to the Google Groups "vim_dev" group.
      To unsubscribe from this group and stop receiving emails from it, send an email to vim_dev+unsubscribe@....
      For more options, visit https://groups.google.com/groups/opt_out.
    • ZyX
      Another refactoring: now moved most module initialization to if_py_both.h. # HG changeset patch # User ZyX # Date 1368960476 -14400 # Branch
      Message 2 of 7 , May 19, 2013
      • 0 Attachment
        Another refactoring: now moved most module initialization to if_py_both.h.

        # HG changeset patch
        # User ZyX <kp-pav@...>
        # Date 1368960476 -14400
        # Branch python-extended-2
        # Node ID 5c5d22791c61dc77a3685fe3b5e86c377330ea79
        # Parent 2a100913fd7195b95cead858e84dcffe138ac765
        Refactoring: move PyType_Ready calls and vim module population to if_py_both.h

        diff -r 2a100913fd71 -r 5c5d22791c61 src/if_py_both.h
        --- a/src/if_py_both.h Sat May 18 17:58:15 2013 +0400
        +++ b/src/if_py_both.h Sun May 19 14:47:56 2013 +0400
        @@ -4181,3 +4181,108 @@
        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;
        +
        + static int
        +populate_module(PyObject *m, object_adder add_object)
        +{
        + int i;
        +
        + for (i = 0; i < sizeof(numeric_constants) / sizeof(struct numeric_constant);
        + ++i)
        + {
        + PyObject *value;
        + if (!(value = PyInt_FromLong(numeric_constants[i].value)))
        + return -1;
        + ADD_OBJECT(m, numeric_constants[i].name, value);
        + }
        +
        + for (i = 0; i < 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_OBJECT(m, "vars", DictionaryNew(&globvardict));
        + ADD_OBJECT(m, "vvars", DictionaryNew(&vimvardict));
        + ADD_OBJECT(m, "options", OptionsNew(SREQ_GLOBAL, NULL, dummy_check, NULL));
        + return 0;
        +}
        diff -r 2a100913fd71 -r 5c5d22791c61 src/if_python.c
        --- a/src/if_python.c Sat May 18 17:58:15 2013 +0400
        +++ b/src/if_python.c Sun May 19 14:47:56 2013 +0400
        @@ -657,7 +657,6 @@
        * Internal function prototypes.
        */

        -static int PythonIO_Init(void);
        static int PythonMod_Init(void);


        @@ -772,7 +771,7 @@
        get_exceptions();
        #endif

        - if (PythonIO_Init())
        + if (PythonIO_Init_io())
        goto fail;

        if (PythonMod_Init())
        @@ -806,7 +805,7 @@
        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 @@
        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,26 +1230,14 @@
        }
        #endif

        -static BufMapObject TheBufferMap =
        + static int
        +add_object(PyObject *dict, const char *name, PyObject *object)
        {
        - 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)
        -};
        + if (PyDict_SetItemString(dict, (char *) name, object))
        + return -1;
        + Py_DECREF(object);
        + return 0;
        +}

        static int
        PythonMod_Init(void)
        @@ -1272,17 +1248,8 @@
        /* 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 @@
        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);
        }

        /*************************************************************************
        diff -r 2a100913fd71 -r 5c5d22791c61 src/if_python3.c
        --- a/src/if_python3.c Sat May 18 17:58:15 2013 +0400
        +++ b/src/if_python3.c Sun May 19 14:47:56 2013 +0400
        @@ -700,7 +700,6 @@
        * Internal function prototypes.
        */

        -static int PythonIO_Init(void);
        static PyObject *Py3Init_vim(void);

        /******************************************************
        @@ -780,7 +779,7 @@
        get_py3_exceptions();
        #endif

        - if (PythonIO_Init())
        + if (PythonIO_Init_io())
        goto fail;

        globals = PyModule_GetDict(PyImport_AddModule("__main__"));
        @@ -811,7 +810,7 @@
        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 @@
        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,27 +1528,6 @@
        }
        #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)
        {
        @@ -1567,19 +1536,8 @@
        /* 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 @@
        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;

        --
        --
        You received this message from the "vim_dev" maillist.
        Do not top-post! Type your reply below the text you are replying to.
        For more information, visit http://www.vim.org/maillist.php

        ---
        You received this message because you are subscribed to the Google Groups "vim_dev" group.
        To unsubscribe from this group and stop receiving emails from it, send an email to vim_dev+unsubscribe@....
        For more options, visit https://groups.google.com/groups/opt_out.
      • ZyX
        Forgot this. Attached patch contains both this and previous change # HG changeset patch # User ZyX # Date 1368961186 -14400 # Branch
        Message 3 of 7 , May 19, 2013
        • 0 Attachment
          Forgot this. Attached patch contains both this and previous change
          # HG changeset patch
          # User ZyX <kp-pav@...>
          # Date 1368961186 -14400
          # Branch python-extended-2
          # Node ID eea6cccdddc53335661cc56d592f1f412adcf0df
          # Parent 5c5d22791c61dc77a3685fe3b5e86c377330ea79
          Also check *New output for errors

          diff -r 5c5d22791c61 -r eea6cccdddc5 src/if_py_both.h
          --- a/src/if_py_both.h Sun May 19 14:47:56 2013 +0400
          +++ b/src/if_py_both.h Sun May 19 14:59:46 2013 +0400
          @@ -4253,6 +4253,14 @@
          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)
          {
          @@ -4260,12 +4268,8 @@

          for (i = 0; i < sizeof(numeric_constants) / sizeof(struct numeric_constant);
          ++i)
          - {
          - PyObject *value;
          - if (!(value = PyInt_FromLong(numeric_constants[i].value)))
          - return -1;
          - ADD_OBJECT(m, numeric_constants[i].name, value);
          - }
          + ADD_CHECKED_OBJECT(m, numeric_constants[i].name,
          + PyInt_FromLong(numeric_constants[i].value));

          for (i = 0; i < sizeof(object_constants) / sizeof(struct object_constant);
          ++i)
          @@ -4281,8 +4285,9 @@
          return -1;
          ADD_OBJECT(m, "error", VimError);

          - ADD_OBJECT(m, "vars", DictionaryNew(&globvardict));
          - ADD_OBJECT(m, "vvars", DictionaryNew(&vimvardict));
          - ADD_OBJECT(m, "options", OptionsNew(SREQ_GLOBAL, NULL, dummy_check, NULL));
          + 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;
          }

          --
          --
          You received this message from the "vim_dev" maillist.
          Do not top-post! Type your reply below the text you are replying to.
          For more information, visit http://www.vim.org/maillist.php

          ---
          You received this message because you are subscribed to the Google Groups "vim_dev" group.
          To unsubscribe from this group and stop receiving emails from it, send an email to vim_dev+unsubscribe@....
          For more options, visit https://groups.google.com/groups/opt_out.
        • Bram Moolenaar
          ... Where does this go in the list of patches? Before 0/? or after 5/?? -- Any sufficiently advanced technology is indistinguishable from magic. Arthur C.
          Message 4 of 7 , May 19, 2013
          • 0 Attachment
            ZyX wrote:

            > Forgot this. Attached patch contains both this and previous change
            > # HG changeset patch
            > # User ZyX <kp-pav@...>
            > # Date 1368961186 -14400
            > # Branch python-extended-2
            > # Node ID eea6cccdddc53335661cc56d592f1f412adcf0df
            > # Parent 5c5d22791c61dc77a3685fe3b5e86c377330ea79
            > Also check *New output for errors

            Where does this go in the list of patches? Before 0/? or after 5/??


            --
            Any sufficiently advanced technology is indistinguishable from magic.
            Arthur C. Clarke
            Any sufficiently advanced bug is indistinguishable from a feature.
            Rich Kulawiec

            /// Bram Moolenaar -- Bram@... -- 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 ///

            --
            --
            You received this message from the "vim_dev" maillist.
            Do not top-post! Type your reply below the text you are replying to.
            For more information, visit http://www.vim.org/maillist.php

            ---
            You received this message because you are subscribed to the Google Groups "vim_dev" group.
            To unsubscribe from this group and stop receiving emails from it, send an email to vim_dev+unsubscribe@....
            For more options, visit https://groups.google.com/groups/opt_out.
          • ZyX
            ... Nowhere: it is already part of (4/?). This list contains *all* unapplied patches. -- -- You received this message from the vim_dev maillist. Do not
            Message 5 of 7 , May 19, 2013
            • 0 Attachment
              > Where does this go in the list of patches? Before 0/? or after 5/??

              Nowhere: it is already part of (4/?). This list contains *all* unapplied patches.

              --
              --
              You received this message from the "vim_dev" maillist.
              Do not top-post! Type your reply below the text you are replying to.
              For more information, visit http://www.vim.org/maillist.php

              ---
              You received this message because you are subscribed to the Google Groups "vim_dev" group.
              To unsubscribe from this group and stop receiving emails from it, send an email to vim_dev+unsubscribe@....
              For more options, visit https://groups.google.com/groups/opt_out.
            Your message has been successfully submitted and would be delivered to recipients shortly.