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

[PATCH] (5/?) Python interface improvements: add attributes with scope dictionaries

Expand Messages
  • ZyX
    # HG changeset patch # User ZyX # Date 1366478866 -14400 # Branch python-extended-2 # Node ID 10f7e8e41f8e23d30cf6f9f894f96d6fd4744e41 # Parent
    Message 1 of 49 , Apr 23, 2013
    • 0 Attachment
      # HG changeset patch
      # User ZyX <kp-pav@...>
      # Date 1366478866 -14400
      # Branch python-extended-2
      # Node ID 10f7e8e41f8e23d30cf6f9f894f96d6fd4744e41
      # Parent 3d61e036e5a6856a08e6e67bd9f372ebd79de15e
      Add vim.vars, vim.vvars, buffer.vars, window.vars attributes

      diff -r 3d61e036e5a6 -r 10f7e8e41f8e runtime/doc/if_pyth.txt
      --- a/runtime/doc/if_pyth.txt Sat Apr 20 20:51:52 2013 +0400
      +++ b/runtime/doc/if_pyth.txt Sat Apr 20 21:27:46 2013 +0400
      @@ -237,6 +237,11 @@
      "current range". A range is a bit like a buffer, but with all access
      restricted to a subset of lines. See |python-range| for more details.

      +vim.vars *python-vars*
      +vim.vvars *python-vvars*
      + Dictionary-like objects holding dictionaries with global (|g:|) and
      + vim (|v:|) variables respectively. Identical to `vim.bindeval("g:")`,
      + but faster.

      Output from Python *python-output*
      Vim displays all Python code output in the Vim message area. Normal
      @@ -307,6 +312,7 @@
      :py n = len(b) # number of lines
      :py (row,col) = b.mark('a') # named mark
      :py r = b.range(1,5) # a sub-range of the buffer
      + :py b.vars["foo"] = "bar" # assign b:foo variable

      ==============================================================================
      4. Range objects *python-range*
      @@ -354,6 +360,9 @@
      This is a tuple, (row,col).
      height (read-write) The window height, in rows
      width (read-write) The window width, in columns
      + vars (read-only) The window |w:| variables. Attribute is
      + unassignable, but you can change window
      + variables this way
      The height attribute is writable only if the screen is split horizontally.
      The width attribute is writable only if the screen is split vertically.

      diff -r 3d61e036e5a6 -r 10f7e8e41f8e src/eval.c
      --- a/src/eval.c Sat Apr 20 20:51:52 2013 +0400
      +++ b/src/eval.c Sat Apr 20 21:27:46 2013 +0400
      @@ -113,12 +113,6 @@
      static char *e_nofunc = N_("E130: Unknown function: %s");
      static char *e_illvar = N_("E461: Illegal variable name: %s");

      -/*
      - * All user-defined global variables are stored in dictionary "globvardict".
      - * "globvars_var" is the variable that is used for "g:".
      - */
      -static dict_T globvardict;
      -static dictitem_T globvars_var;
      #define globvarht globvardict.dv_hashtab

      /*
      @@ -370,12 +364,6 @@
      #define vv_list vv_di.di_tv.vval.v_list
      #define vv_tv vv_di.di_tv

      -/*
      - * The v: variables are stored in dictionary "vimvardict".
      - * "vimvars_var" is the variable that is used for the "l:" scope.
      - */
      -static dict_T vimvardict;
      -static dictitem_T vimvars_var;
      #define vimvarht vimvardict.dv_hashtab

      static void prepare_vimvar __ARGS((int idx, typval_T *save_tv));
      diff -r 3d61e036e5a6 -r 10f7e8e41f8e src/globals.h
      --- a/src/globals.h Sat Apr 20 20:51:52 2013 +0400
      +++ b/src/globals.h Sat Apr 20 21:27:46 2013 +0400
      @@ -180,6 +180,10 @@
      EXTERN int emsg_severe INIT(= FALSE); /* use message of next of several
      emsg() calls for throw */
      EXTERN int did_endif INIT(= FALSE); /* just had ":endif" */
      +EXTERN dict_T vimvardict; /* Dictionary with v: variables */
      +EXTERN dictitem_T vimvars_var; /* Variable used for v: */
      +EXTERN dict_T globvardict; /* Dictionary with g: variables */
      +EXTERN dictitem_T globvars_var; /* Variable used for g: */
      #endif
      EXTERN int did_emsg; /* set by emsg() when the message
      is displayed or thrown */
      diff -r 3d61e036e5a6 -r 10f7e8e41f8e src/if_py_both.h
      --- a/src/if_py_both.h Sat Apr 20 20:51:52 2013 +0400
      +++ b/src/if_py_both.h Sat Apr 20 21:27:46 2013 +0400
      @@ -1531,8 +1531,10 @@
      else if (strcmp(name, "width") == 0)
      return Py_BuildValue("l", (long)(W_WIDTH(this->win)));
      #endif
      + else if (strcmp(name, "vars") == 0)
      + return DictionaryNew(this->win->w_vars);
      else if (strcmp(name,"__members__") == 0)
      - return Py_BuildValue("[sss]", "buffer", "cursor", "height");
      + return Py_BuildValue("[ssss]", "buffer", "cursor", "height", "vars");
      else
      return NULL;
      }
      @@ -2494,8 +2496,10 @@
      return Py_BuildValue("s", this->buf->b_ffname);
      else if (strcmp(name, "number") == 0)
      return Py_BuildValue(Py_ssize_t_fmt, this->buf->b_fnum);
      + else if (strcmp(name, "vars") == 0)
      + return DictionaryNew(this->buf->b_vars);
      else if (strcmp(name,"__members__") == 0)
      - return Py_BuildValue("[ss]", "name", "number");
      + return Py_BuildValue("[sss]", "name", "number", "vars");
      else
      return NULL;
      }
      diff -r 3d61e036e5a6 -r 10f7e8e41f8e src/if_python.c
      --- a/src/if_python.c Sat Apr 20 20:51:52 2013 +0400
      +++ b/src/if_python.c Sat Apr 20 21:27:46 2013 +0400
      @@ -1089,7 +1089,7 @@
      {
      PyObject *r;

      - if (CheckWindow((BufferObject *)(self)))
      + if (CheckWindow((WindowObject *)(self)))
      return NULL;

      r = BufferAttr((BufferObject *)(self), name);
      @@ -1310,6 +1310,7 @@
      {
      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};

      @@ -1333,6 +1334,12 @@
      PyDict_SetItemString(dict, "buffers", (PyObject *)(void *)&TheBufferList);
      PyDict_SetItemString(dict, "current", (PyObject *)(void *)&TheCurrent);
      PyDict_SetItemString(dict, "windows", (PyObject *)(void *)&TheWindowList);
      + tmp = DictionaryNew(&globvardict);
      + PyDict_SetItemString(dict, "vars", tmp);
      + Py_DECREF(tmp);
      + tmp = DictionaryNew(&vimvardict);
      + PyDict_SetItemString(dict, "vvars", 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));
      diff -r 3d61e036e5a6 -r 10f7e8e41f8e src/if_python3.c
      --- a/src/if_python3.c Sat Apr 20 20:51:52 2013 +0400
      +++ b/src/if_python3.c Sat Apr 20 21:27:46 2013 +0400
      @@ -1647,6 +1647,9 @@
      Py_INCREF((PyObject *)(void *)&TheWindowList);
      PyModule_AddObject(mod, "windows", (PyObject *)(void *)&TheWindowList);

      + PyModule_AddObject(mod, "vars", DictionaryNew(&globvardict));
      + PyModule_AddObject(mod, "vvars", DictionaryNew(&vimvardict));
      +
      #define ADD_INT_CONSTANT(name, value) \
      tmp = PyLong_FromLong(value); \
      Py_INCREF(tmp); \
      diff -r 3d61e036e5a6 -r 10f7e8e41f8e src/testdir/test86.in
      --- a/src/testdir/test86.in Sat Apr 20 20:51:52 2013 +0400
      +++ b/src/testdir/test86.in Sat Apr 20 21:27:46 2013 +0400
      @@ -346,6 +346,19 @@
      :$put =string(pyeval('l'))
      :py l = ll[-10:10]
      :$put =string(pyeval('l'))
      +:"
      +:" Vars
      +:let g:foo = 'bac'
      +:let w:abc = 'def'
      +:let b:baz = 'bar'
      +:try
      +: throw "Abc"
      +:catch
      +: put =pyeval('vim.vvars[''exception'']')
      +:endtry
      +:put =pyeval('vim.vars[''foo'']')
      +:put =pyeval('vim.current.window.vars[''abc'']')
      +:put =pyeval('vim.current.buffer.vars[''baz'']')
      :endfun
      :"
      :call Test()
      diff -r 3d61e036e5a6 -r 10f7e8e41f8e src/testdir/test86.ok
      --- a/src/testdir/test86.ok Sat Apr 20 20:51:52 2013 +0400
      +++ b/src/testdir/test86.ok Sat Apr 20 21:27:46 2013 +0400
      @@ -76,3 +76,7 @@
      [0, 1, 2, 3, 4, 5]
      [0, 1, 2, 3, 4, 5]
      [0, 1, 2, 3, 4, 5]
      +Abc
      +bac
      +def
      +bar
      diff -r 3d61e036e5a6 -r 10f7e8e41f8e src/testdir/test87.in
      --- a/src/testdir/test87.in Sat Apr 20 20:51:52 2013 +0400
      +++ b/src/testdir/test87.in Sat Apr 20 21:27:46 2013 +0400
      @@ -315,6 +315,19 @@
      :py3 trace_main()
      :py3 sys.settrace(None)
      :$put =string(l)
      +:"
      +:" Vars
      +:let g:foo = 'bac'
      +:let w:abc = 'def'
      +:let b:baz = 'bar'
      +:try
      +: throw "Abc"
      +:catch
      +: put =py3eval('vim.vvars[''exception'']')
      +:endtry
      +:put =py3eval('vim.vars[''foo'']')
      +:put =py3eval('vim.current.window.vars[''abc'']')
      +:put =py3eval('vim.current.buffer.vars[''baz'']')
      :endfun
      :"
      :call Test()
      diff -r 3d61e036e5a6 -r 10f7e8e41f8e src/testdir/test87.ok
      --- a/src/testdir/test87.ok Sat Apr 20 20:51:52 2013 +0400
      +++ b/src/testdir/test87.ok Sat Apr 20 21:27:46 2013 +0400
      @@ -65,3 +65,7 @@
      vim: Vim(let):E861:
      [1]
      [1, 10, 11, 10, 11, 10, 11, 10, 11, 10, 11, 10, 1]
      +Abc
      +bac
      +def
      +bar

      --
      --
      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
      ... Because you have problems with encoding somewhere: there were no question marks in the patch, but Russian text. I just was not aware of the strange choice:
      Message 49 of 49 , May 15, 2013
      • 0 Attachment
        > After including this patch the tests were failing. The .ok files had
        > "????" where the test results in "window". I assume those question
        > marks were wrong, so I fixed the .ok files for that. But I wonder why
        > this happened.

        Because you have problems with encoding somewhere: there were no question marks in the patch, but Russian text. I just was not aware of the strange choice: using translated strings in `repr()`. I think translated messages here are just as “good” as translated exceptions; needless to say I have never seen them translated in other projects.

        --
        --
        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.