comparison src/if_python.c @ 4435:9b800f0a757f v7.3.966

updated for version 7.3.966 Problem: There is ":py3do" but no ":pydo". Solution: Add the ":pydo" command. (Lilydjwg)
author Bram Moolenaar <bram@vim.org>
date Fri, 17 May 2013 16:40:06 +0200
parents 7d81f4e96728
children 8fe768bc1234
comparison
equal deleted inserted replaced
4434:edfd1b971b87 4435:9b800f0a757f
196 # define PyMapping_Check dll_PyMapping_Check 196 # define PyMapping_Check dll_PyMapping_Check
197 # define PyIter_Next dll_PyIter_Next 197 # define PyIter_Next dll_PyIter_Next
198 # define PyModule_GetDict dll_PyModule_GetDict 198 # define PyModule_GetDict dll_PyModule_GetDict
199 # define PyRun_SimpleString dll_PyRun_SimpleString 199 # define PyRun_SimpleString dll_PyRun_SimpleString
200 # define PyRun_String dll_PyRun_String 200 # define PyRun_String dll_PyRun_String
201 # define PyObject_GetAttrString dll_PyObject_GetAttrString
202 # define PyObject_SetAttrString dll_PyObject_SetAttrString
203 # define PyObject_CallFunctionObjArgs dll_PyObject_CallFunctionObjArgs
201 # define PyString_AsString dll_PyString_AsString 204 # define PyString_AsString dll_PyString_AsString
202 # define PyString_AsStringAndSize dll_PyString_AsStringAndSize 205 # define PyString_AsStringAndSize dll_PyString_AsStringAndSize
203 # define PyString_FromString dll_PyString_FromString 206 # define PyString_FromString dll_PyString_FromString
204 # define PyString_FromStringAndSize dll_PyString_FromStringAndSize 207 # define PyString_FromStringAndSize dll_PyString_FromStringAndSize
205 # define PyString_Size dll_PyString_Size 208 # define PyString_Size dll_PyString_Size
301 static int (*dll_PyMapping_Check)(PyObject *); 304 static int (*dll_PyMapping_Check)(PyObject *);
302 static PyObject* (*dll_PyIter_Next)(PyObject *); 305 static PyObject* (*dll_PyIter_Next)(PyObject *);
303 static PyObject*(*dll_PyModule_GetDict)(PyObject *); 306 static PyObject*(*dll_PyModule_GetDict)(PyObject *);
304 static int(*dll_PyRun_SimpleString)(char *); 307 static int(*dll_PyRun_SimpleString)(char *);
305 static PyObject *(*dll_PyRun_String)(char *, int, PyObject *, PyObject *); 308 static PyObject *(*dll_PyRun_String)(char *, int, PyObject *, PyObject *);
309 static PyObject* (*dll_PyObject_GetAttrString)(PyObject *, const char *);
310 static PyObject* (*dll_PyObject_SetAttrString)(PyObject *, const char *, PyObject *);
311 static PyObject* (*dll_PyObject_CallFunctionObjArgs)(PyObject *, ...);
306 static char*(*dll_PyString_AsString)(PyObject *); 312 static char*(*dll_PyString_AsString)(PyObject *);
307 static int(*dll_PyString_AsStringAndSize)(PyObject *, char **, int *); 313 static int(*dll_PyString_AsStringAndSize)(PyObject *, char **, int *);
308 static PyObject*(*dll_PyString_FromString)(const char *); 314 static PyObject*(*dll_PyString_FromString)(const char *);
309 static PyObject*(*dll_PyString_FromStringAndSize)(const char *, PyInt); 315 static PyObject*(*dll_PyString_FromStringAndSize)(const char *, PyInt);
310 static PyInt(*dll_PyString_Size)(PyObject *); 316 static PyInt(*dll_PyString_Size)(PyObject *);
438 {"PyMapping_Check", (PYTHON_PROC*)&dll_PyMapping_Check}, 444 {"PyMapping_Check", (PYTHON_PROC*)&dll_PyMapping_Check},
439 {"PyIter_Next", (PYTHON_PROC*)&dll_PyIter_Next}, 445 {"PyIter_Next", (PYTHON_PROC*)&dll_PyIter_Next},
440 {"PyModule_GetDict", (PYTHON_PROC*)&dll_PyModule_GetDict}, 446 {"PyModule_GetDict", (PYTHON_PROC*)&dll_PyModule_GetDict},
441 {"PyRun_SimpleString", (PYTHON_PROC*)&dll_PyRun_SimpleString}, 447 {"PyRun_SimpleString", (PYTHON_PROC*)&dll_PyRun_SimpleString},
442 {"PyRun_String", (PYTHON_PROC*)&dll_PyRun_String}, 448 {"PyRun_String", (PYTHON_PROC*)&dll_PyRun_String},
449 {"PyObject_GetAttrString", (PYTHON_PROC*)&dll_PyObject_GetAttrString},
450 {"PyObject_SetAttrString", (PYTHON_PROC*)&dll_PyObject_SetAttrString},
451 {"PyObject_CallFunctionObjArgs", (PYTHON_PROC*)&dll_PyObject_CallFunctionObjArgs},
443 {"PyString_AsString", (PYTHON_PROC*)&dll_PyString_AsString}, 452 {"PyString_AsString", (PYTHON_PROC*)&dll_PyString_AsString},
444 {"PyString_AsStringAndSize", (PYTHON_PROC*)&dll_PyString_AsStringAndSize}, 453 {"PyString_AsStringAndSize", (PYTHON_PROC*)&dll_PyString_AsStringAndSize},
445 {"PyString_FromString", (PYTHON_PROC*)&dll_PyString_FromString}, 454 {"PyString_FromString", (PYTHON_PROC*)&dll_PyString_FromString},
446 {"PyString_FromStringAndSize", (PYTHON_PROC*)&dll_PyString_FromStringAndSize}, 455 {"PyString_FromStringAndSize", (PYTHON_PROC*)&dll_PyString_FromStringAndSize},
447 {"PyString_Size", (PYTHON_PROC*)&dll_PyString_Size}, 456 {"PyString_Size", (PYTHON_PROC*)&dll_PyString_Size},
993 1002
994 /* Execute the file */ 1003 /* Execute the file */
995 DoPythonCommand(eap, buffer, NULL); 1004 DoPythonCommand(eap, buffer, NULL);
996 } 1005 }
997 1006
1007 void
1008 ex_pydo(exarg_T *eap)
1009 {
1010 linenr_T i;
1011 const char *code_hdr = "def " DOPY_FUNC "(line, linenr):\n ";
1012 const char *s = (const char *) eap->arg;
1013 size_t len;
1014 char *code;
1015 int status;
1016 PyObject *pyfunc, *pymain;
1017 PyGILState_STATE pygilstate;
1018
1019 if (Python_Init())
1020 return;
1021
1022 if (u_save(eap->line1 - 1, eap->line2 + 1) != OK)
1023 {
1024 EMSG(_("cannot save undo information"));
1025 return;
1026 }
1027 len = strlen(code_hdr) + strlen(s);
1028 code = malloc(len + 1);
1029 STRCPY(code, code_hdr);
1030 STRNCAT(code, s, len + 1);
1031 pygilstate = PyGILState_Ensure();
1032 status = PyRun_SimpleString(code);
1033 vim_free(code);
1034 if (status)
1035 {
1036 EMSG(_("failed to run the code"));
1037 return;
1038 }
1039 status = 0; /* good */
1040 pymain = PyImport_AddModule("__main__");
1041 pyfunc = PyObject_GetAttrString(pymain, DOPY_FUNC);
1042 PyGILState_Release(pygilstate);
1043
1044 for (i = eap->line1; i <= eap->line2; i++)
1045 {
1046 const char *line;
1047 PyObject *pyline, *pylinenr, *pyret;
1048
1049 line = (char *)ml_get(i);
1050 pygilstate = PyGILState_Ensure();
1051 pyline = PyString_FromStringAndSize(line, strlen(line));
1052 pylinenr = PyLong_FromLong(i);
1053 pyret = PyObject_CallFunctionObjArgs(pyfunc, pyline, pylinenr, NULL);
1054 Py_DECREF(pyline);
1055 Py_DECREF(pylinenr);
1056 if (!pyret)
1057 {
1058 PyErr_PrintEx(0);
1059 PythonIO_Flush();
1060 status = 1;
1061 goto out;
1062 }
1063
1064 if (pyret && pyret != Py_None)
1065 {
1066 if (!PyString_Check(pyret))
1067 {
1068 EMSG(_("E863: return value must be an instance of str"));
1069 Py_XDECREF(pyret);
1070 status = 1;
1071 goto out;
1072 }
1073 ml_replace(i, (char_u *) PyString_AsString(pyret), 1);
1074 changed();
1075 #ifdef SYNTAX_HL
1076 syn_changed(i); /* recompute syntax hl. for this line */
1077 #endif
1078 }
1079 Py_XDECREF(pyret);
1080 PythonIO_Flush();
1081 PyGILState_Release(pygilstate);
1082 }
1083 pygilstate = PyGILState_Ensure();
1084 out:
1085 Py_DECREF(pyfunc);
1086 PyObject_SetAttrString(pymain, DOPY_FUNC, NULL);
1087 PyGILState_Release(pygilstate);
1088 if (status)
1089 return;
1090 check_cursor();
1091 update_curbuf(NOT_VALID);
1092 }
1093
998 /****************************************************** 1094 /******************************************************
999 * 2. Python output stream: writes output via [e]msg(). 1095 * 2. Python output stream: writes output via [e]msg().
1000 */ 1096 */
1001 1097
1002 /* Implementation functions 1098 /* Implementation functions