Hi, vim_dev!
I'd like to offer a small patch for python interface, which allows to avoid
inserting print call for every expression in order to see result.
Now:
:py 2 + 2
:py dir.__doc__
After applying patch:
:py 2 + 2
4
:py dir.__doc__
dir([object]) -> list of strings...
So py/py3 commands are more REPL-like. See pyrepl.diff in attachment for
details.
--
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
diff -r 536aa8b0c934 src/if_python.c
--- a/src/if_python.c Wed Aug 15 17:43:31 2012 +0200
+++ b/src/if_python.c Thu Aug 16 16:45:36 2012 +0400
@@ -234,7 +234,8 @@
# define PyCObject_FromVoidPtr dll_PyCObject_FromVoidPtr
# define PyCObject_AsVoidPtr dll_PyCObject_AsVoidPtr
# endif
-
+# define PySys_WriteStdout dll_PySys_WriteStdout
+# define PyObject_Str dll_PyObject_Str
/*
* Pointers for dynamic link
*/
@@ -330,7 +331,8 @@
static PyObject* (*dll_PyCObject_FromVoidPtr)(void *cobj, void (*destr)(void *));
static void* (*dll_PyCObject_AsVoidPtr)(PyObject *);
# endif
-
+static void (*dll_PySys_WriteStdout)(const char *format, ...);
+static PyObject* (*dll_PyObject_Str)(PyObject *);
static HINSTANCE hinstPython = 0; /* Instance of python.dll */
/* Imported exception objects */
@@ -452,6 +454,8 @@
{"PyCObject_FromVoidPtr", (PYTHON_PROC*)&dll_PyCObject_FromVoidPtr},
{"PyCObject_AsVoidPtr", (PYTHON_PROC*)&dll_PyCObject_AsVoidPtr},
# endif
+ {"PySys_WriteStdout", (PYTHON_PROC*)&dll_PySys_WriteStdout},
+ {"PyObject_Str", (PYTHON_PROC*)&dll_PyObject_Str},
{"", NULL},
};
@@ -819,24 +823,38 @@
Python_RestoreThread(); /* enter python */
- if (rettv == NULL)
- PyRun_SimpleString((char *)(cmd));
+ PyObject *r;
+
+ r = PyRun_String((char *)(cmd), Py_eval_input, globals, globals);
+
+ if (r == NULL)
+ {
+ if (rettv != NULL)
+ EMSG(_("E858: Eval did not return a valid python object"));
+ }
else
{
- PyObject *r;
+ if (rettv == NULL)
+ {
+ PyObject *str;
- r = PyRun_String((char *)(cmd), Py_eval_input, globals, globals);
- if (r == NULL)
- EMSG(_("E858: Eval did not return a valid python object"));
+ str = PyObject_Str(r);
+
+ if (str != NULL)
+ PySys_WriteStdout("%.1000s", PyString_AsString(str));
+
+ Py_DECREF(str);
+ }
else
{
if (ConvertFromPyObject(r, rettv) == -1)
EMSG(_("E859: Failed to convert returned python object to vim value"));
- Py_DECREF(r);
}
- PyErr_Clear();
+
+ Py_DECREF(r);
}
+ PyErr_Clear();
Python_SaveThread(); /* leave python */
#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
diff -r 536aa8b0c934 src/if_python3.c
--- a/src/if_python3.c Wed Aug 15 17:43:31 2012 +0200
+++ b/src/if_python3.c Thu Aug 16 16:45:36 2012 +0400
@@ -200,6 +200,8 @@
# define PyType_IsSubtype py3_PyType_IsSubtype
# define PyCapsule_New py3_PyCapsule_New
# define PyCapsule_GetPointer py3_PyCapsule_GetPointer
+# define PySys_WriteStdout py3_PySys_WriteStdout
+# define PyObject_Str py3_PyObject_Str
# ifdef Py_DEBUG
# undef PyObject_NEW
@@ -296,6 +298,8 @@
static void* (*py3_PyObject_Malloc)(size_t);
# endif
static int (*py3_PyType_IsSubtype)(PyTypeObject *, PyTypeObject *);
+static void (*py3_PySys_WriteStdout)(const char *format, ...);
+static PyObject* (*py3_PyObject_Str)(PyObject *);
static HINSTANCE hinstPy3 = 0; /* Instance of python.dll */
@@ -402,6 +406,8 @@
{"PyType_IsSubtype", (PYTHON_PROC*)&py3_PyType_IsSubtype},
{"PyCapsule_New", (PYTHON_PROC*)&py3_PyCapsule_New},
{"PyCapsule_GetPointer", (PYTHON_PROC*)&py3_PyCapsule_GetPointer},
+ {"PySys_WriteStdout", (PYTHON_PROC*)&py3_PySys_WriteStdout},
+ {"PyObject_Str", (PYTHON_PROC*)&py3_PyObject_Str},
{"", NULL},
};
@@ -782,26 +788,41 @@
(char *)ENC_OPT, CODEC_ERROR_HANDLER);
cmdbytes = PyUnicode_AsEncodedString(cmdstr, "utf-8", CODEC_ERROR_HANDLER);
Py_XDECREF(cmdstr);
- if (rettv == NULL)
- PyRun_SimpleString(PyBytes_AsString(cmdbytes));
+
+ PyObject *r;
+
+ r = PyRun_String(PyBytes_AsString(cmdbytes), Py_eval_input, globals, globals);
+
+ if (r == NULL)
+ {
+ if (rettv != NULL)
+ EMSG(_("E860: Eval did not return a valid python 3 object"));
+ }
else
{
- PyObject *r;
+ if (rettv == NULL)
+ {
+ PyObject *str;
- r = PyRun_String(PyBytes_AsString(cmdbytes), Py_eval_input,
- globals, globals);
- if (r == NULL)
- EMSG(_("E860: Eval did not return a valid python 3 object"));
- else
- {
- if (ConvertFromPyObject(r, rettv) == -1)
+ str = PyObject_Str(r);
+
+ if (str != NULL)
+ PySys_WriteStdout("%.1000s", _PyUnicode_AsString(str));
+
+ Py_DECREF(str);
+ }
+ else
+ {
+ if (ConvertFromPyObject(r, rettv) == -1)
EMSG(_("E861: Failed to convert returned python 3 object to vim value"));
- Py_DECREF(r);
- }
- PyErr_Clear();
+ }
+
+ Py_DECREF(r);
}
+
+ PyErr_Clear();
Py_XDECREF(cmdbytes);
-
+
PyGILState_Release(pygilstate);
#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)