comparison src/if_python.c @ 18798:f0f9692d4487 v8.1.2387

patch 8.1.2387: using old C style comments Commit: https://github.com/vim/vim/commit/2ab2e8608f9b2c85432715bb9a7f226fdbf8cd35 Author: Bram Moolenaar <Bram@vim.org> Date: Wed Dec 4 21:24:53 2019 +0100 patch 8.1.2387: using old C style comments Problem: Using old C style comments. Solution: Use // comments where appropriate.
author Bram Moolenaar <Bram@vim.org>
date Wed, 04 Dec 2019 21:30:04 +0100
parents 831f9e74eded
children 4acb165ed0bc
comparison
equal deleted inserted replaced
18797:76af6d0ea316 18798:f0f9692d4487
19 19
20 #include "vim.h" 20 #include "vim.h"
21 21
22 #include <limits.h> 22 #include <limits.h>
23 23
24 /* uncomment this if used with the debug version of python. 24 // uncomment this if used with the debug version of python.
25 * Checked on 2.7.4. */ 25 // Checked on 2.7.4.
26 /* #define Py_DEBUG */ 26 // #define Py_DEBUG
27 /* Note: most of time you can add -DPy_DEBUG to CFLAGS in place of uncommenting 27 // Note: most of time you can add -DPy_DEBUG to CFLAGS in place of uncommenting
28 */ 28 // uncomment this if used with the debug version of python, but without its
29 /* uncomment this if used with the debug version of python, but without its 29 // allocator
30 * allocator */ 30 // #define Py_DEBUG_NO_PYMALLOC
31 /* #define Py_DEBUG_NO_PYMALLOC */ 31
32 32 // Python.h defines _POSIX_THREADS itself (if needed)
33 /* Python.h defines _POSIX_THREADS itself (if needed) */
34 #ifdef _POSIX_THREADS 33 #ifdef _POSIX_THREADS
35 # undef _POSIX_THREADS 34 # undef _POSIX_THREADS
36 #endif 35 #endif
37 36
38 #if defined(MSWIN) && defined(HAVE_FCNTL_H) 37 #if defined(MSWIN) && defined(HAVE_FCNTL_H)
51 #endif 50 #endif
52 #ifdef HAVE_PUTENV 51 #ifdef HAVE_PUTENV
53 # undef HAVE_PUTENV 52 # undef HAVE_PUTENV
54 #endif 53 #endif
55 #ifdef HAVE_STDARG_H 54 #ifdef HAVE_STDARG_H
56 # undef HAVE_STDARG_H /* Python's config.h defines it as well. */ 55 # undef HAVE_STDARG_H // Python's config.h defines it as well.
57 #endif 56 #endif
58 #ifdef _POSIX_C_SOURCE 57 #ifdef _POSIX_C_SOURCE
59 # undef _POSIX_C_SOURCE /* pyconfig.h defines it as well. */ 58 # undef _POSIX_C_SOURCE // pyconfig.h defines it as well.
60 #endif 59 #endif
61 #ifdef _XOPEN_SOURCE 60 #ifdef _XOPEN_SOURCE
62 # undef _XOPEN_SOURCE /* pyconfig.h defines it as well. */ 61 # undef _XOPEN_SOURCE // pyconfig.h defines it as well.
63 #endif 62 #endif
64 63
65 #define PY_SSIZE_T_CLEAN 64 #define PY_SSIZE_T_CLEAN
66 65
67 #include <Python.h> 66 #include <Python.h>
68 67
69 #if !defined(PY_VERSION_HEX) || PY_VERSION_HEX < 0x02050000 68 #if !defined(PY_VERSION_HEX) || PY_VERSION_HEX < 0x02050000
70 # undef PY_SSIZE_T_CLEAN 69 # undef PY_SSIZE_T_CLEAN
71 #endif 70 #endif
72 71
73 #undef main /* Defined in python.h - aargh */ 72 #undef main // Defined in python.h - aargh
74 #undef HAVE_FCNTL_H /* Clash with os_win32.h */ 73 #undef HAVE_FCNTL_H // Clash with os_win32.h
75 74
76 // Perhaps leave this out for Python 2.6, which supports bytes? 75 // Perhaps leave this out for Python 2.6, which supports bytes?
77 #define PyBytes_FromString PyString_FromString 76 #define PyBytes_FromString PyString_FromString
78 #define PyBytes_Check PyString_Check 77 #define PyBytes_Check PyString_Check
79 #define PyBytes_AsStringAndSize PyString_AsStringAndSize 78 #define PyBytes_AsStringAndSize PyString_AsStringAndSize
80 #define PyBytes_FromStringAndSize PyString_FromStringAndSize 79 #define PyBytes_FromStringAndSize PyString_FromStringAndSize
81 80
82 #if !defined(FEAT_PYTHON) && defined(PROTO) 81 #if !defined(FEAT_PYTHON) && defined(PROTO)
83 /* Use this to be able to generate prototypes without python being used. */ 82 // Use this to be able to generate prototypes without python being used.
84 # define PyObject Py_ssize_t 83 # define PyObject Py_ssize_t
85 # define PyThreadState Py_ssize_t 84 # define PyThreadState Py_ssize_t
86 # define PyTypeObject Py_ssize_t 85 # define PyTypeObject Py_ssize_t
87 struct PyMethodDef { Py_ssize_t a; }; 86 struct PyMethodDef { Py_ssize_t a; };
88 # define PySequenceMethods Py_ssize_t 87 # define PySequenceMethods Py_ssize_t
110 # define PyIntIntObjArgProc intintobjargproc 109 # define PyIntIntObjArgProc intintobjargproc
111 # define Py_ssize_t_fmt "i" 110 # define Py_ssize_t_fmt "i"
112 #endif 111 #endif
113 #define Py_bytes_fmt "s" 112 #define Py_bytes_fmt "s"
114 113
115 /* Parser flags */ 114 // Parser flags
116 #define single_input 256 115 #define single_input 256
117 #define file_input 257 116 #define file_input 257
118 #define eval_input 258 117 #define eval_input 258
119 118
120 #if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x020300F0 119 #if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x020300F0
121 /* Python 2.3: can invoke ":python" recursively. */ 120 // Python 2.3: can invoke ":python" recursively.
122 # define PY_CAN_RECURSE 121 # define PY_CAN_RECURSE
123 #endif 122 #endif
124 123
125 #if defined(DYNAMIC_PYTHON) || defined(PROTO) 124 #if defined(DYNAMIC_PYTHON) || defined(PROTO)
126 # ifndef DYNAMIC_PYTHON 125 # ifndef DYNAMIC_PYTHON
127 # define HINSTANCE long_u /* for generating prototypes */ 126 # define HINSTANCE long_u // for generating prototypes
128 # endif 127 # endif
129 128
130 # ifndef MSWIN 129 # ifndef MSWIN
131 # include <dlfcn.h> 130 # include <dlfcn.h>
132 # define FARPROC void* 131 # define FARPROC void*
142 # define load_dll vimLoadLib 141 # define load_dll vimLoadLib
143 # define close_dll FreeLibrary 142 # define close_dll FreeLibrary
144 # define symbol_from_dll GetProcAddress 143 # define symbol_from_dll GetProcAddress
145 # endif 144 # endif
146 145
147 /* This makes if_python.c compile without warnings against Python 2.5 146 // This makes if_python.c compile without warnings against Python 2.5
148 * on Win32 and Win64. */ 147 // on Win32 and Win64.
149 # undef PyRun_SimpleString 148 # undef PyRun_SimpleString
150 # undef PyRun_String 149 # undef PyRun_String
151 # undef PyArg_Parse 150 # undef PyArg_Parse
152 # undef PyArg_ParseTuple 151 # undef PyArg_ParseTuple
153 # undef Py_BuildValue 152 # undef Py_BuildValue
454 # endif 453 # endif
455 # if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02070000 454 # if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02070000
456 static int* dll_Py_NoSiteFlag; 455 static int* dll_Py_NoSiteFlag;
457 # endif 456 # endif
458 457
459 static HINSTANCE hinstPython = 0; /* Instance of python.dll */ 458 static HINSTANCE hinstPython = 0; // Instance of python.dll
460 459
461 /* Imported exception objects */ 460 // Imported exception objects
462 static PyObject *imp_PyExc_AttributeError; 461 static PyObject *imp_PyExc_AttributeError;
463 static PyObject *imp_PyExc_IndexError; 462 static PyObject *imp_PyExc_IndexError;
464 static PyObject *imp_PyExc_KeyError; 463 static PyObject *imp_PyExc_KeyError;
465 static PyObject *imp_PyExc_KeyboardInterrupt; 464 static PyObject *imp_PyExc_KeyboardInterrupt;
466 static PyObject *imp_PyExc_TypeError; 465 static PyObject *imp_PyExc_TypeError;
679 int i; 678 int i;
680 PYTHON_PROC *ucs_as_encoded_string = 679 PYTHON_PROC *ucs_as_encoded_string =
681 (PYTHON_PROC*)&py_PyUnicode_AsEncodedString; 680 (PYTHON_PROC*)&py_PyUnicode_AsEncodedString;
682 681
683 # if !(defined(PY_NO_RTLD_GLOBAL) && defined(PY3_NO_RTLD_GLOBAL)) && defined(UNIX) && defined(FEAT_PYTHON3) 682 # if !(defined(PY_NO_RTLD_GLOBAL) && defined(PY3_NO_RTLD_GLOBAL)) && defined(UNIX) && defined(FEAT_PYTHON3)
684 /* Can't have Python and Python3 loaded at the same time. 683 // Can't have Python and Python3 loaded at the same time.
685 * It cause a crash, because RTLD_GLOBAL is needed for 684 // It cause a crash, because RTLD_GLOBAL is needed for
686 * standard C extension libraries of one or both python versions. */ 685 // standard C extension libraries of one or both python versions.
687 if (python3_loaded()) 686 if (python3_loaded())
688 { 687 {
689 if (verbose) 688 if (verbose)
690 emsg(_("E836: This Vim cannot execute :python after using :py3")); 689 emsg(_("E836: This Vim cannot execute :python after using :py3"));
691 return FAIL; 690 return FAIL;
713 semsg(_(e_loadfunc), python_funcname_table[i].name); 712 semsg(_(e_loadfunc), python_funcname_table[i].name);
714 return FAIL; 713 return FAIL;
715 } 714 }
716 } 715 }
717 716
718 /* Load unicode functions separately as only the ucs2 or the ucs4 functions 717 // Load unicode functions separately as only the ucs2 or the ucs4 functions
719 * will be present in the library. */ 718 // will be present in the library.
720 *ucs_as_encoded_string = symbol_from_dll(hinstPython, 719 *ucs_as_encoded_string = symbol_from_dll(hinstPython,
721 "PyUnicodeUCS2_AsEncodedString"); 720 "PyUnicodeUCS2_AsEncodedString");
722 if (*ucs_as_encoded_string == NULL) 721 if (*ucs_as_encoded_string == NULL)
723 *ucs_as_encoded_string = symbol_from_dll(hinstPython, 722 *ucs_as_encoded_string = symbol_from_dll(hinstPython,
724 "PyUnicodeUCS4_AsEncodedString"); 723 "PyUnicodeUCS4_AsEncodedString");
773 Py_XINCREF(imp_PyExc_RuntimeError); 772 Py_XINCREF(imp_PyExc_RuntimeError);
774 Py_XINCREF(imp_PyExc_ImportError); 773 Py_XINCREF(imp_PyExc_ImportError);
775 Py_XINCREF(imp_PyExc_OverflowError); 774 Py_XINCREF(imp_PyExc_OverflowError);
776 Py_XDECREF(exmod); 775 Py_XDECREF(exmod);
777 } 776 }
778 #endif /* DYNAMIC_PYTHON */ 777 #endif // DYNAMIC_PYTHON
779 778
780 static int initialised = 0; 779 static int initialised = 0;
781 #define PYINITIALISED initialised 780 #define PYINITIALISED initialised
782 static int python_end_called = FALSE; 781 static int python_end_called = FALSE;
783 782
828 * Include the code shared with if_python3.c 827 * Include the code shared with if_python3.c
829 */ 828 */
830 #include "if_py_both.h" 829 #include "if_py_both.h"
831 830
832 831
833 /****************************************************** 832 ///////////////////////////////////////////////////////
834 * Internal function prototypes. 833 // Internal function prototypes.
835 */
836 834
837 static int PythonMod_Init(void); 835 static int PythonMod_Init(void);
838 836
839 837
840 /****************************************************** 838 ///////////////////////////////////////////////////////
841 * 1. Python interpreter main program. 839 // 1. Python interpreter main program.
842 */ 840
843 841 #if PYTHON_API_VERSION < 1007 // Python 1.4
844 #if PYTHON_API_VERSION < 1007 /* Python 1.4 */
845 typedef PyObject PyThreadState; 842 typedef PyObject PyThreadState;
846 #endif 843 #endif
847 844
848 #ifndef PY_CAN_RECURSE 845 #ifndef PY_CAN_RECURSE
849 static PyThreadState *saved_python_thread = NULL; 846 static PyThreadState *saved_python_thread = NULL;
873 void 870 void
874 python_end(void) 871 python_end(void)
875 { 872 {
876 static int recurse = 0; 873 static int recurse = 0;
877 874
878 /* If a crash occurs while doing this, don't try again. */ 875 // If a crash occurs while doing this, don't try again.
879 if (recurse != 0) 876 if (recurse != 0)
880 return; 877 return;
881 878
882 python_end_called = TRUE; 879 python_end_called = TRUE;
883 ++recurse; 880 ++recurse;
886 if (hinstPython && Py_IsInitialized()) 883 if (hinstPython && Py_IsInitialized())
887 { 884 {
888 # ifdef PY_CAN_RECURSE 885 # ifdef PY_CAN_RECURSE
889 PyGILState_Ensure(); 886 PyGILState_Ensure();
890 # else 887 # else
891 Python_RestoreThread(); /* enter python */ 888 Python_RestoreThread(); // enter python
892 # endif 889 # endif
893 Py_Finalize(); 890 Py_Finalize();
894 } 891 }
895 end_dynamic_python(); 892 end_dynamic_python();
896 #else 893 #else
897 if (Py_IsInitialized()) 894 if (Py_IsInitialized())
898 { 895 {
899 # ifdef PY_CAN_RECURSE 896 # ifdef PY_CAN_RECURSE
900 PyGILState_Ensure(); 897 PyGILState_Ensure();
901 # else 898 # else
902 Python_RestoreThread(); /* enter python */ 899 Python_RestoreThread(); // enter python
903 # endif 900 # endif
904 Py_Finalize(); 901 Py_Finalize();
905 } 902 }
906 #endif 903 #endif
907 904
935 } 932 }
936 #endif 933 #endif
937 934
938 if (*p_pyhome != NUL) 935 if (*p_pyhome != NUL)
939 { 936 {
940 /* The string must not change later, make a copy in static memory. */ 937 // The string must not change later, make a copy in static memory.
941 py_home_buf = (char *)vim_strsave(p_pyhome); 938 py_home_buf = (char *)vim_strsave(p_pyhome);
942 if (py_home_buf != NULL) 939 if (py_home_buf != NULL)
943 Py_SetPythonHome(py_home_buf); 940 Py_SetPythonHome(py_home_buf);
944 } 941 }
945 #ifdef PYTHON_HOME 942 #ifdef PYTHON_HOME
948 #endif 945 #endif
949 946
950 init_structs(); 947 init_structs();
951 948
952 #if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02070000 949 #if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02070000
953 /* Disable implicit 'import site', because it may cause Vim to exit 950 // Disable implicit 'import site', because it may cause Vim to exit
954 * when it can't be found. */ 951 // when it can't be found.
955 Py_NoSiteFlag++; 952 Py_NoSiteFlag++;
956 #endif 953 #endif
957 954
958 Py_Initialize(); 955 Py_Initialize();
959 956
960 #if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02070000 957 #if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02070000
961 /* 'import site' explicitly. */ 958 // 'import site' explicitly.
962 site = PyImport_ImportModule("site"); 959 site = PyImport_ImportModule("site");
963 if (site == NULL) 960 if (site == NULL)
964 { 961 {
965 emsg(_("E887: Sorry, this command is disabled, the Python's site module could not be loaded.")); 962 emsg(_("E887: Sorry, this command is disabled, the Python's site module could not be loaded."));
966 goto fail; 963 goto fail;
967 } 964 }
968 Py_DECREF(site); 965 Py_DECREF(site);
969 #endif 966 #endif
970 967
971 /* Initialise threads, and below save the state using 968 // Initialise threads, and below save the state using
972 * PyEval_SaveThread. Without the call to PyEval_SaveThread, thread 969 // PyEval_SaveThread. Without the call to PyEval_SaveThread, thread
973 * specific state (such as the system trace hook), will be lost 970 // specific state (such as the system trace hook), will be lost
974 * between invocations of Python code. */ 971 // between invocations of Python code.
975 PyEval_InitThreads(); 972 PyEval_InitThreads();
976 #ifdef DYNAMIC_PYTHON 973 #ifdef DYNAMIC_PYTHON
977 get_exceptions(); 974 get_exceptions();
978 #endif 975 #endif
979 976
983 if (PythonMod_Init()) 980 if (PythonMod_Init())
984 goto fail; 981 goto fail;
985 982
986 globals = PyModule_GetDict(PyImport_AddModule("__main__")); 983 globals = PyModule_GetDict(PyImport_AddModule("__main__"));
987 984
988 /* Remove the element from sys.path that was added because of our 985 // Remove the element from sys.path that was added because of our
989 * argv[0] value in PythonMod_Init(). Previously we used an empty 986 // argv[0] value in PythonMod_Init(). Previously we used an empty
990 * string, but depending on the OS we then get an empty entry or 987 // string, but depending on the OS we then get an empty entry or
991 * the current directory in sys.path. */ 988 // the current directory in sys.path.
992 PyRun_SimpleString("import sys; sys.path = filter(lambda x: x != '/must>not&exist', sys.path)"); 989 PyRun_SimpleString("import sys; sys.path = filter(lambda x: x != '/must>not&exist', sys.path)");
993 990
994 /* lock is created and acquired in PyEval_InitThreads() and thread 991 // lock is created and acquired in PyEval_InitThreads() and thread
995 * state is created in Py_Initialize() 992 // state is created in Py_Initialize()
996 * there _PyGILState_NoteThreadState() also sets gilcounter to 1 993 // there _PyGILState_NoteThreadState() also sets gilcounter to 1
997 * (python must have threads enabled!) 994 // (python must have threads enabled!)
998 * so the following does both: unlock GIL and save thread state in TLS 995 // so the following does both: unlock GIL and save thread state in TLS
999 * without deleting thread state 996 // without deleting thread state
1000 */
1001 #ifndef PY_CAN_RECURSE 997 #ifndef PY_CAN_RECURSE
1002 saved_python_thread = 998 saved_python_thread =
1003 #endif 999 #endif
1004 PyEval_SaveThread(); 1000 PyEval_SaveThread();
1005 1001
1007 } 1003 }
1008 1004
1009 return 0; 1005 return 0;
1010 1006
1011 fail: 1007 fail:
1012 /* We call PythonIO_Flush() here to print any Python errors. 1008 // We call PythonIO_Flush() here to print any Python errors.
1013 * This is OK, as it is possible to call this function even 1009 // This is OK, as it is possible to call this function even
1014 * if PythonIO_Init_io() has not completed successfully (it will 1010 // if PythonIO_Init_io() has not completed successfully (it will
1015 * not do anything in this case). 1011 // not do anything in this case).
1016 */
1017 PythonIO_Flush(); 1012 PythonIO_Flush();
1018 return -1; 1013 return -1;
1019 } 1014 }
1020 1015
1021 /* 1016 /*
1048 if (Python_Init()) 1043 if (Python_Init())
1049 goto theend; 1044 goto theend;
1050 1045
1051 init_range(arg); 1046 init_range(arg);
1052 1047
1053 Python_Release_Vim(); /* leave vim */ 1048 Python_Release_Vim(); // leave Vim
1054 1049
1055 #if defined(HAVE_LOCALE_H) || defined(X_LOCALE) 1050 #if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
1056 /* Python only works properly when the LC_NUMERIC locale is "C". */ 1051 // Python only works properly when the LC_NUMERIC locale is "C".
1057 saved_locale = setlocale(LC_NUMERIC, NULL); 1052 saved_locale = setlocale(LC_NUMERIC, NULL);
1058 if (saved_locale == NULL || STRCMP(saved_locale, "C") == 0) 1053 if (saved_locale == NULL || STRCMP(saved_locale, "C") == 0)
1059 saved_locale = NULL; 1054 saved_locale = NULL;
1060 else 1055 else
1061 { 1056 {
1062 /* Need to make a copy, value may change when setting new locale. */ 1057 // Need to make a copy, value may change when setting new locale.
1063 saved_locale = (char *) PY_STRSAVE(saved_locale); 1058 saved_locale = (char *) PY_STRSAVE(saved_locale);
1064 (void)setlocale(LC_NUMERIC, "C"); 1059 (void)setlocale(LC_NUMERIC, "C");
1065 } 1060 }
1066 #endif 1061 #endif
1067 1062
1068 #ifdef PY_CAN_RECURSE 1063 #ifdef PY_CAN_RECURSE
1069 pygilstate = PyGILState_Ensure(); 1064 pygilstate = PyGILState_Ensure();
1070 #else 1065 #else
1071 Python_RestoreThread(); /* enter python */ 1066 Python_RestoreThread(); // enter python
1072 #endif 1067 #endif
1073 1068
1074 run((char *) cmd, arg 1069 run((char *) cmd, arg
1075 #ifdef PY_CAN_RECURSE 1070 #ifdef PY_CAN_RECURSE
1076 , &pygilstate 1071 , &pygilstate
1078 ); 1073 );
1079 1074
1080 #ifdef PY_CAN_RECURSE 1075 #ifdef PY_CAN_RECURSE
1081 PyGILState_Release(pygilstate); 1076 PyGILState_Release(pygilstate);
1082 #else 1077 #else
1083 Python_SaveThread(); /* leave python */ 1078 Python_SaveThread(); // leave python
1084 #endif 1079 #endif
1085 1080
1086 #if defined(HAVE_LOCALE_H) || defined(X_LOCALE) 1081 #if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
1087 if (saved_locale != NULL) 1082 if (saved_locale != NULL)
1088 { 1083 {
1089 (void)setlocale(LC_NUMERIC, saved_locale); 1084 (void)setlocale(LC_NUMERIC, saved_locale);
1090 PyMem_Free(saved_locale); 1085 PyMem_Free(saved_locale);
1091 } 1086 }
1092 #endif 1087 #endif
1093 1088
1094 Python_Lock_Vim(); /* enter vim */ 1089 Python_Lock_Vim(); // enter vim
1095 PythonIO_Flush(); 1090 PythonIO_Flush();
1096 1091
1097 theend: 1092 theend:
1098 #ifndef PY_CAN_RECURSE 1093 #ifndef PY_CAN_RECURSE
1099 --recursive; 1094 --recursive;
1136 char *p; 1131 char *p;
1137 1132
1138 if (p_pyx == 0) 1133 if (p_pyx == 0)
1139 p_pyx = 2; 1134 p_pyx = 2;
1140 1135
1141 /* Have to do it like this. PyRun_SimpleFile requires you to pass a 1136 // Have to do it like this. PyRun_SimpleFile requires you to pass a
1142 * stdio file pointer, but Vim and the Python DLL are compiled with 1137 // stdio file pointer, but Vim and the Python DLL are compiled with
1143 * different options under Windows, meaning that stdio pointers aren't 1138 // different options under Windows, meaning that stdio pointers aren't
1144 * compatible between the two. Yuk. 1139 // compatible between the two. Yuk.
1145 * 1140 //
1146 * Put the string "execfile('file')" into buffer. But, we need to 1141 // Put the string "execfile('file')" into buffer. But, we need to
1147 * escape any backslashes or single quotes in the file name, so that 1142 // escape any backslashes or single quotes in the file name, so that
1148 * Python won't mangle the file name. 1143 // Python won't mangle the file name.
1149 */
1150 strcpy(buffer, "execfile('"); 1144 strcpy(buffer, "execfile('");
1151 p = buffer + 10; /* size of "execfile('" */ 1145 p = buffer + 10; // size of "execfile('"
1152 1146
1153 while (*file && p < buffer + (BUFFER_SIZE - 3)) 1147 while (*file && p < buffer + (BUFFER_SIZE - 3))
1154 { 1148 {
1155 if (*file == '\\' || *file == '\'') 1149 if (*file == '\\' || *file == '\'')
1156 *p++ = '\\'; 1150 *p++ = '\\';
1157 *p++ = *file++; 1151 *p++ = *file++;
1158 } 1152 }
1159 1153
1160 /* If we didn't finish the file name, we hit a buffer overflow */ 1154 // If we didn't finish the file name, we hit a buffer overflow
1161 if (*file != '\0') 1155 if (*file != '\0')
1162 return; 1156 return;
1163 1157
1164 /* Put in the terminating "')" and a null */ 1158 // Put in the terminating "')" and a null
1165 *p++ = '\''; 1159 *p++ = '\'';
1166 *p++ = ')'; 1160 *p++ = ')';
1167 *p++ = '\0'; 1161 *p++ = '\0';
1168 1162
1169 /* Execute the file */ 1163 // Execute the file
1170 DoPyCommand(buffer, 1164 DoPyCommand(buffer,
1171 (rangeinitializer) init_range_cmd, 1165 (rangeinitializer) init_range_cmd,
1172 (runner) run_cmd, 1166 (runner) run_cmd,
1173 (void *) eap); 1167 (void *) eap);
1174 } 1168 }
1183 (rangeinitializer) init_range_cmd, 1177 (rangeinitializer) init_range_cmd,
1184 (runner)run_do, 1178 (runner)run_do,
1185 (void *)eap); 1179 (void *)eap);
1186 } 1180 }
1187 1181
1188 /****************************************************** 1182 ///////////////////////////////////////////////////////
1189 * 2. Python output stream: writes output via [e]msg(). 1183 // 2. Python output stream: writes output via [e]msg().
1190 */ 1184
1191 1185 // Implementation functions
1192 /* Implementation functions
1193 */
1194 1186
1195 static PyObject * 1187 static PyObject *
1196 OutputGetattr(PyObject *self, char *name) 1188 OutputGetattr(PyObject *self, char *name)
1197 { 1189 {
1198 if (strcmp(name, "softspace") == 0) 1190 if (strcmp(name, "softspace") == 0)
1204 else if (strcmp(name, "encoding") == 0) 1196 else if (strcmp(name, "encoding") == 0)
1205 return PyString_FromString(ENC_OPT); 1197 return PyString_FromString(ENC_OPT);
1206 return Py_FindMethod(OutputMethods, self, name); 1198 return Py_FindMethod(OutputMethods, self, name);
1207 } 1199 }
1208 1200
1209 /****************************************************** 1201 ///////////////////////////////////////////////////////
1210 * 3. Implementation of the Vim module for Python 1202 // 3. Implementation of the Vim module for Python
1211 */ 1203
1212 1204 // Window type - Implementation functions
1213 /* Window type - Implementation functions 1205 // --------------------------------------
1214 * --------------------------------------
1215 */
1216 1206
1217 #define WindowType_Check(obj) ((obj)->ob_type == &WindowType) 1207 #define WindowType_Check(obj) ((obj)->ob_type == &WindowType)
1218 1208
1219 /* Buffer type - Implementation functions 1209 // Buffer type - Implementation functions
1220 * -------------------------------------- 1210 // --------------------------------------
1221 */
1222 1211
1223 #define BufferType_Check(obj) ((obj)->ob_type == &BufferType) 1212 #define BufferType_Check(obj) ((obj)->ob_type == &BufferType)
1224 1213
1225 static PyInt BufferAssItem(PyObject *, PyInt, PyObject *); 1214 static PyInt BufferAssItem(PyObject *, PyInt, PyObject *);
1226 static PyInt BufferAssSlice(PyObject *, PyInt, PyInt, PyObject *); 1215 static PyInt BufferAssSlice(PyObject *, PyInt, PyInt, PyObject *);
1227 1216
1228 /* Line range type - Implementation functions 1217 // Line range type - Implementation functions
1229 * -------------------------------------- 1218 // --------------------------------------
1230 */
1231 1219
1232 #define RangeType_Check(obj) ((obj)->ob_type == &RangeType) 1220 #define RangeType_Check(obj) ((obj)->ob_type == &RangeType)
1233 1221
1234 static PyInt RangeAssItem(PyObject *, PyInt, PyObject *); 1222 static PyInt RangeAssItem(PyObject *, PyInt, PyObject *);
1235 static PyInt RangeAssSlice(PyObject *, PyInt, PyInt, PyObject *); 1223 static PyInt RangeAssSlice(PyObject *, PyInt, PyInt, PyObject *);
1236 1224
1237 /* Current objects type - Implementation functions 1225 // Current objects type - Implementation functions
1238 * ----------------------------------------------- 1226 // -----------------------------------------------
1239 */
1240 1227
1241 static PySequenceMethods BufferAsSeq = { 1228 static PySequenceMethods BufferAsSeq = {
1242 (PyInquiry) BufferLength, /* sq_length, len(x) */ 1229 (PyInquiry) BufferLength, // sq_length, len(x)
1243 (binaryfunc) 0, /* BufferConcat, sq_concat, x+y */ 1230 (binaryfunc) 0, // BufferConcat, sq_concat, x+y
1244 (PyIntArgFunc) 0, /* BufferRepeat, sq_repeat, x*n */ 1231 (PyIntArgFunc) 0, // BufferRepeat, sq_repeat, x*n
1245 (PyIntArgFunc) BufferItem, /* sq_item, x[i] */ 1232 (PyIntArgFunc) BufferItem, // sq_item, x[i]
1246 (PyIntIntArgFunc) BufferSlice, /* sq_slice, x[i:j] */ 1233 (PyIntIntArgFunc) BufferSlice, // sq_slice, x[i:j]
1247 (PyIntObjArgProc) BufferAssItem, /* sq_ass_item, x[i]=v */ 1234 (PyIntObjArgProc) BufferAssItem, // sq_ass_item, x[i]=v
1248 (PyIntIntObjArgProc) BufferAssSlice, /* sq_ass_slice, x[i:j]=v */ 1235 (PyIntIntObjArgProc) BufferAssSlice, // sq_ass_slice, x[i:j]=v
1249 (objobjproc) 0, 1236 (objobjproc) 0,
1250 (binaryfunc) 0, 1237 (binaryfunc) 0,
1251 0, 1238 0,
1252 }; 1239 };
1253 1240
1254 /* Buffer object - Implementation 1241 // Buffer object - Implementation
1255 */
1256 1242
1257 static PyObject * 1243 static PyObject *
1258 BufferGetattr(PyObject *self, char *name) 1244 BufferGetattr(PyObject *self, char *name)
1259 { 1245 {
1260 PyObject *r; 1246 PyObject *r;
1270 return r; 1256 return r;
1271 else 1257 else
1272 return Py_FindMethod(BufferMethods, self, name); 1258 return Py_FindMethod(BufferMethods, self, name);
1273 } 1259 }
1274 1260
1275 /******************/ 1261 //////////////////
1276 1262
1277 static PyInt 1263 static PyInt
1278 BufferAssItem(PyObject *self, PyInt n, PyObject *val) 1264 BufferAssItem(PyObject *self, PyInt n, PyObject *val)
1279 { 1265 {
1280 return RBAsItem((BufferObject *)(self), n, val, 1, -1, NULL); 1266 return RBAsItem((BufferObject *)(self), n, val, 1, -1, NULL);
1285 { 1271 {
1286 return RBAsSlice((BufferObject *)(self), lo, hi, val, 1, -1, NULL); 1272 return RBAsSlice((BufferObject *)(self), lo, hi, val, 1, -1, NULL);
1287 } 1273 }
1288 1274
1289 static PySequenceMethods RangeAsSeq = { 1275 static PySequenceMethods RangeAsSeq = {
1290 (PyInquiry) RangeLength, /* sq_length, len(x) */ 1276 (PyInquiry) RangeLength, // sq_length, len(x)
1291 (binaryfunc) 0, /* RangeConcat, */ /* sq_concat, x+y */ 1277 (binaryfunc) 0, /* RangeConcat, */ // sq_concat, x+y
1292 (PyIntArgFunc) 0, /* RangeRepeat, */ /* sq_repeat, x*n */ 1278 (PyIntArgFunc) 0, /* RangeRepeat, */ // sq_repeat, x*n
1293 (PyIntArgFunc) RangeItem, /* sq_item, x[i] */ 1279 (PyIntArgFunc) RangeItem, // sq_item, x[i]
1294 (PyIntIntArgFunc) RangeSlice, /* sq_slice, x[i:j] */ 1280 (PyIntIntArgFunc) RangeSlice, // sq_slice, x[i:j]
1295 (PyIntObjArgProc) RangeAssItem, /* sq_ass_item, x[i]=v */ 1281 (PyIntObjArgProc) RangeAssItem, // sq_ass_item, x[i]=v
1296 (PyIntIntObjArgProc) RangeAssSlice, /* sq_ass_slice, x[i:j]=v */ 1282 (PyIntIntObjArgProc) RangeAssSlice, // sq_ass_slice, x[i:j]=v
1297 (objobjproc) 0, 1283 (objobjproc) 0,
1298 #if PY_MAJOR_VERSION >= 2 1284 #if PY_MAJOR_VERSION >= 2
1299 (binaryfunc) 0, 1285 (binaryfunc) 0,
1300 0, 1286 0,
1301 #endif 1287 #endif
1302 }; 1288 };
1303 1289
1304 /* Line range object - Implementation 1290 // Line range object - Implementation
1305 */
1306 1291
1307 static PyObject * 1292 static PyObject *
1308 RangeGetattr(PyObject *self, char *name) 1293 RangeGetattr(PyObject *self, char *name)
1309 { 1294 {
1310 if (strcmp(name, "start") == 0) 1295 if (strcmp(name, "start") == 0)
1315 return ObjectDir(NULL, RangeAttrs); 1300 return ObjectDir(NULL, RangeAttrs);
1316 else 1301 else
1317 return Py_FindMethod(RangeMethods, self, name); 1302 return Py_FindMethod(RangeMethods, self, name);
1318 } 1303 }
1319 1304
1320 /****************/ 1305 ////////////////
1321 1306
1322 static PyInt 1307 static PyInt
1323 RangeAssItem(PyObject *self, PyInt n, PyObject *val) 1308 RangeAssItem(PyObject *self, PyInt n, PyObject *val)
1324 { 1309 {
1325 return RBAsItem(((RangeObject *)(self))->buf, n, val, 1310 return RBAsItem(((RangeObject *)(self))->buf, n, val,
1335 ((RangeObject *)(self))->start, 1320 ((RangeObject *)(self))->start,
1336 ((RangeObject *)(self))->end, 1321 ((RangeObject *)(self))->end,
1337 &((RangeObject *)(self))->end); 1322 &((RangeObject *)(self))->end);
1338 } 1323 }
1339 1324
1340 /* TabPage object - Implementation 1325 // TabPage object - Implementation
1341 */
1342 1326
1343 static PyObject * 1327 static PyObject *
1344 TabPageGetattr(PyObject *self, char *name) 1328 TabPageGetattr(PyObject *self, char *name)
1345 { 1329 {
1346 PyObject *r; 1330 PyObject *r;
1356 return r; 1340 return r;
1357 else 1341 else
1358 return Py_FindMethod(TabPageMethods, self, name); 1342 return Py_FindMethod(TabPageMethods, self, name);
1359 } 1343 }
1360 1344
1361 /* Window object - Implementation 1345 // Window object - Implementation
1362 */
1363 1346
1364 static PyObject * 1347 static PyObject *
1365 WindowGetattr(PyObject *self, char *name) 1348 WindowGetattr(PyObject *self, char *name)
1366 { 1349 {
1367 PyObject *r; 1350 PyObject *r;
1377 return r; 1360 return r;
1378 else 1361 else
1379 return Py_FindMethod(WindowMethods, self, name); 1362 return Py_FindMethod(WindowMethods, self, name);
1380 } 1363 }
1381 1364
1382 /* Tab page list object - Definitions 1365 // Tab page list object - Definitions
1383 */
1384 1366
1385 static PySequenceMethods TabListAsSeq = { 1367 static PySequenceMethods TabListAsSeq = {
1386 (PyInquiry) TabListLength, /* sq_length, len(x) */ 1368 (PyInquiry) TabListLength, // sq_length, len(x)
1387 (binaryfunc) 0, /* sq_concat, x+y */ 1369 (binaryfunc) 0, // sq_concat, x+y
1388 (PyIntArgFunc) 0, /* sq_repeat, x*n */ 1370 (PyIntArgFunc) 0, // sq_repeat, x*n
1389 (PyIntArgFunc) TabListItem, /* sq_item, x[i] */ 1371 (PyIntArgFunc) TabListItem, // sq_item, x[i]
1390 (PyIntIntArgFunc) 0, /* sq_slice, x[i:j] */ 1372 (PyIntIntArgFunc) 0, // sq_slice, x[i:j]
1391 (PyIntObjArgProc) 0, /* sq_ass_item, x[i]=v */ 1373 (PyIntObjArgProc) 0, // sq_ass_item, x[i]=v
1392 (PyIntIntObjArgProc) 0, /* sq_ass_slice, x[i:j]=v */ 1374 (PyIntIntObjArgProc) 0, // sq_ass_slice, x[i:j]=v
1393 (objobjproc) 0, 1375 (objobjproc) 0,
1394 #if PY_MAJOR_VERSION >= 2 1376 #if PY_MAJOR_VERSION >= 2
1395 (binaryfunc) 0, 1377 (binaryfunc) 0,
1396 0, 1378 0,
1397 #endif 1379 #endif
1398 }; 1380 };
1399 1381
1400 /* Window list object - Definitions 1382 // Window list object - Definitions
1401 */
1402 1383
1403 static PySequenceMethods WinListAsSeq = { 1384 static PySequenceMethods WinListAsSeq = {
1404 (PyInquiry) WinListLength, /* sq_length, len(x) */ 1385 (PyInquiry) WinListLength, // sq_length, len(x)
1405 (binaryfunc) 0, /* sq_concat, x+y */ 1386 (binaryfunc) 0, // sq_concat, x+y
1406 (PyIntArgFunc) 0, /* sq_repeat, x*n */ 1387 (PyIntArgFunc) 0, // sq_repeat, x*n
1407 (PyIntArgFunc) WinListItem, /* sq_item, x[i] */ 1388 (PyIntArgFunc) WinListItem, // sq_item, x[i]
1408 (PyIntIntArgFunc) 0, /* sq_slice, x[i:j] */ 1389 (PyIntIntArgFunc) 0, // sq_slice, x[i:j]
1409 (PyIntObjArgProc) 0, /* sq_ass_item, x[i]=v */ 1390 (PyIntObjArgProc) 0, // sq_ass_item, x[i]=v
1410 (PyIntIntObjArgProc) 0, /* sq_ass_slice, x[i:j]=v */ 1391 (PyIntIntObjArgProc) 0, // sq_ass_slice, x[i:j]=v
1411 (objobjproc) 0, 1392 (objobjproc) 0,
1412 #if PY_MAJOR_VERSION >= 2 1393 #if PY_MAJOR_VERSION >= 2
1413 (binaryfunc) 0, 1394 (binaryfunc) 0,
1414 0, 1395 0,
1415 #endif 1396 #endif
1416 }; 1397 };
1417 1398
1418 /* External interface 1399 // External interface
1419 */
1420 1400
1421 void 1401 void
1422 python_buffer_free(buf_T *buf) 1402 python_buffer_free(buf_T *buf)
1423 { 1403 {
1424 if (BUF_PYTHON_REF(buf) != NULL) 1404 if (BUF_PYTHON_REF(buf) != NULL)
1452 } 1432 }
1453 1433
1454 static int 1434 static int
1455 PythonMod_Init(void) 1435 PythonMod_Init(void)
1456 { 1436 {
1457 /* The special value is removed from sys.path in Python_Init(). */ 1437 // The special value is removed from sys.path in Python_Init().
1458 static char *(argv[2]) = {"/must>not&exist/foo", NULL}; 1438 static char *(argv[2]) = {"/must>not&exist/foo", NULL};
1459 1439
1460 if (init_types()) 1440 if (init_types())
1461 return -1; 1441 return -1;
1462 1442
1463 /* Set sys.argv[] to avoid a crash in warn(). */ 1443 // Set sys.argv[] to avoid a crash in warn().
1464 PySys_SetArgv(1, argv); 1444 PySys_SetArgv(1, argv);
1465 1445
1466 vim_module = Py_InitModule4("vim", VimMethods, (char *)NULL, 1446 vim_module = Py_InitModule4("vim", VimMethods, (char *)NULL,
1467 (PyObject *)NULL, PYTHON_API_VERSION); 1447 (PyObject *)NULL, PYTHON_API_VERSION);
1468 1448
1473 return -1; 1453 return -1;
1474 1454
1475 return 0; 1455 return 0;
1476 } 1456 }
1477 1457
1478 /************************************************************************* 1458 //////////////////////////////////////////////////////////////////////////
1479 * 4. Utility functions for handling the interface between Vim and Python. 1459 // 4. Utility functions for handling the interface between Vim and Python.
1480 */ 1460
1481 1461 // Convert a Vim line into a Python string.
1482 /* Convert a Vim line into a Python string. 1462 // All internal newlines are replaced by null characters.
1483 * All internal newlines are replaced by null characters. 1463 //
1484 * 1464 // On errors, the Python exception data is set, and NULL is returned.
1485 * On errors, the Python exception data is set, and NULL is returned.
1486 */
1487 static PyObject * 1465 static PyObject *
1488 LineToString(const char *str) 1466 LineToString(const char *str)
1489 { 1467 {
1490 PyObject *result; 1468 PyObject *result;
1491 PyInt len = strlen(str); 1469 PyInt len = strlen(str);
1492 char *p; 1470 char *p;
1493 1471
1494 /* Allocate an Python string object, with uninitialised contents. We 1472 // Allocate an Python string object, with uninitialised contents. We
1495 * must do it this way, so that we can modify the string in place 1473 // must do it this way, so that we can modify the string in place
1496 * later. See the Python source, Objects/stringobject.c for details. 1474 // later. See the Python source, Objects/stringobject.c for details.
1497 */
1498 result = PyString_FromStringAndSize(NULL, len); 1475 result = PyString_FromStringAndSize(NULL, len);
1499 if (result == NULL) 1476 if (result == NULL)
1500 return NULL; 1477 return NULL;
1501 1478
1502 p = PyString_AsString(result); 1479 p = PyString_AsString(result);
1566 rettv->v_type = VAR_NUMBER; 1543 rettv->v_type = VAR_NUMBER;
1567 rettv->vval.v_number = 0; 1544 rettv->vval.v_number = 0;
1568 } 1545 }
1569 } 1546 }
1570 1547
1571 /* Don't generate a prototype for the next function, it generates an error on 1548 // Don't generate a prototype for the next function, it generates an error on
1572 * newer Python versions. */ 1549 // newer Python versions.
1573 #if PYTHON_API_VERSION < 1007 /* Python 1.4 */ && !defined(PROTO) 1550 #if PYTHON_API_VERSION < 1007 /* Python 1.4 */ && !defined(PROTO)
1574 1551
1575 char * 1552 char *
1576 Py_GetProgramName(void) 1553 Py_GetProgramName(void)
1577 { 1554 {
1578 return "vim"; 1555 return "vim";
1579 } 1556 }
1580 #endif /* Python 1.4 */ 1557 #endif // Python 1.4
1581 1558
1582 int 1559 int
1583 set_ref_in_python(int copyID) 1560 set_ref_in_python(int copyID)
1584 { 1561 {
1585 return set_ref_in_py(copyID); 1562 return set_ref_in_py(copyID);