blob: 0b05857d5a3bad036af1543ddadf2e3d23b1bdfb [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9/*
10 * Python extensions by Paul Moore.
11 * Changes for Unix by David Leonard.
12 *
13 * This consists of four parts:
14 * 1. Python interpreter main program
15 * 2. Python output stream: writes output via [e]msg().
16 * 3. Implementation of the Vim module for Python
17 * 4. Utility functions for handling the interface between Vim and Python.
18 */
19
20/*
21 * Roland Puntaier 2009/sept/16:
22 * Adaptations to support both python3.x and python2.x
23 */
24
Bram Moolenaar2ab2e862019-12-04 21:24:53 +010025// uncomment this if used with the debug version of python
26// #define Py_DEBUG
27// Note: most of time you can add -DPy_DEBUG to CFLAGS in place of uncommenting
28// uncomment this if used with the debug version of python, but without its
29// allocator
30// #define Py_DEBUG_NO_PYMALLOC
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020031
32#include "vim.h"
33
34#include <limits.h>
35
Bram Moolenaar4f974752019-02-17 17:44:42 +010036#if defined(MSWIN) && defined(HAVE_FCNTL_H)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020037# undef HAVE_FCNTL_H
38#endif
39
40#ifdef _DEBUG
41# undef _DEBUG
42#endif
43
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020044#ifdef F_BLANK
45# undef F_BLANK
46#endif
47
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010048#ifdef HAVE_STRFTIME
49# undef HAVE_STRFTIME
50#endif
51#ifdef HAVE_STRING_H
52# undef HAVE_STRING_H
53#endif
54#ifdef HAVE_PUTENV
55# undef HAVE_PUTENV
56#endif
Bram Moolenaar6df6f472010-07-18 18:04:50 +020057#ifdef HAVE_STDARG_H
Bram Moolenaar2ab2e862019-12-04 21:24:53 +010058# undef HAVE_STDARG_H // Python's config.h defines it as well.
Bram Moolenaar6df6f472010-07-18 18:04:50 +020059#endif
Bram Moolenaar2ab2e862019-12-04 21:24:53 +010060#ifdef _POSIX_C_SOURCE // defined in feature.h
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020061# undef _POSIX_C_SOURCE
62#endif
Bram Moolenaar6df6f472010-07-18 18:04:50 +020063#ifdef _XOPEN_SOURCE
Bram Moolenaar2ab2e862019-12-04 21:24:53 +010064# undef _XOPEN_SOURCE // pyconfig.h defines it as well.
Bram Moolenaar6df6f472010-07-18 18:04:50 +020065#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020066
Bram Moolenaar0bdda372013-06-10 18:36:24 +020067#define PY_SSIZE_T_CLEAN
68
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020069#include <Python.h>
Bram Moolenaar0bdda372013-06-10 18:36:24 +020070
Bram Moolenaar2ab2e862019-12-04 21:24:53 +010071#undef main // Defined in python.h - aargh
72#undef HAVE_FCNTL_H // Clash with os_win32.h
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020073
Bram Moolenaar2ab2e862019-12-04 21:24:53 +010074// The "surrogateescape" error handler is new in Python 3.1
Bram Moolenaar3d64a312011-07-15 15:54:44 +020075#if PY_VERSION_HEX >= 0x030100f0
76# define CODEC_ERROR_HANDLER "surrogateescape"
77#else
78# define CODEC_ERROR_HANDLER NULL
79#endif
80
Bram Moolenaar2ab2e862019-12-04 21:24:53 +010081// Python 3 does not support CObjects, always use Capsules
Bram Moolenaar2afa3232012-06-29 16:28:28 +020082#define PY_USE_CAPSULE
83
Bram Moolenaar2e2f52a2020-12-21 16:03:02 +010084#define ERRORS_DECODE_ARG CODEC_ERROR_HANDLER
85#define ERRORS_ENCODE_ARG ERRORS_DECODE_ARG
86
Bram Moolenaar170bf1a2010-07-24 23:51:45 +020087#define PyInt Py_ssize_t
Bram Moolenaar32ac8cd2013-07-03 18:49:17 +020088#ifndef PyString_Check
89# define PyString_Check(obj) PyUnicode_Check(obj)
90#endif
Bram Moolenaare0324612013-07-09 17:30:55 +020091#define PyString_FromString(repr) \
Bram Moolenaar2e2f52a2020-12-21 16:03:02 +010092 PyUnicode_Decode(repr, STRLEN(repr), ENC_OPT, ERRORS_DECODE_ARG)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +020093#define PyString_FromFormat PyUnicode_FromFormat
Bram Moolenaar32ac8cd2013-07-03 18:49:17 +020094#ifndef PyInt_Check
95# define PyInt_Check(obj) PyLong_Check(obj)
96#endif
Bram Moolenaar77045652012-09-21 13:46:06 +020097#define PyInt_FromLong(i) PyLong_FromLong(i)
98#define PyInt_AsLong(obj) PyLong_AsLong(obj)
Bram Moolenaar4d1da492013-04-24 13:39:15 +020099#define Py_ssize_t_fmt "n"
Bram Moolenaara9922d62013-05-30 13:01:18 +0200100#define Py_bytes_fmt "y"
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200101
Bram Moolenaar063a46b2014-01-14 16:36:51 +0100102#define PyIntArgFunc ssizeargfunc
103#define PyIntObjArgProc ssizeobjargproc
104
Bram Moolenaar922a4662014-03-30 16:11:43 +0200105/*
106 * PySlice_GetIndicesEx(): first argument type changed from PySliceObject
107 * to PyObject in Python 3.2 or later.
108 */
109#if PY_VERSION_HEX >= 0x030200f0
110typedef PyObject PySliceObject_T;
111#else
112typedef PySliceObject PySliceObject_T;
113#endif
114
Bram Moolenaar63ff72a2022-02-07 13:54:01 +0000115#ifndef MSWIN
116# define HINSTANCE void *
117#endif
118#if defined(DYNAMIC_PYTHON3) || defined(MSWIN)
119static HINSTANCE hinstPy3 = 0; // Instance of python.dll
120#endif
121
Bram Moolenaar0c1f3f42011-02-25 15:18:50 +0100122#if defined(DYNAMIC_PYTHON3) || defined(PROTO)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200123
Bram Moolenaar4f974752019-02-17 17:44:42 +0100124# ifndef MSWIN
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200125# include <dlfcn.h>
126# define FARPROC void*
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100127# if defined(PY_NO_RTLD_GLOBAL) && defined(PY3_NO_RTLD_GLOBAL)
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200128# define load_dll(n) dlopen((n), RTLD_LAZY)
129# else
130# define load_dll(n) dlopen((n), RTLD_LAZY|RTLD_GLOBAL)
131# endif
132# define close_dll dlclose
133# define symbol_from_dll dlsym
Martin Tournoij1a3e5742021-07-24 13:57:29 +0200134# define load_dll_error dlerror
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200135# else
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200136# define load_dll vimLoadLib
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200137# define close_dll FreeLibrary
138# define symbol_from_dll GetProcAddress
Martin Tournoij1a3e5742021-07-24 13:57:29 +0200139# define load_dll_error GetWin32Error
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200140# endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200141/*
142 * Wrapper defines
143 */
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200144# undef PyArg_Parse
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200145# define PyArg_Parse py3_PyArg_Parse
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200146# undef PyArg_ParseTuple
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200147# define PyArg_ParseTuple py3_PyArg_ParseTuple
Bram Moolenaar19e60942011-06-19 00:27:51 +0200148# define PyMem_Free py3_PyMem_Free
Bram Moolenaardb913952012-06-29 12:54:53 +0200149# define PyMem_Malloc py3_PyMem_Malloc
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200150# define PyDict_SetItemString py3_PyDict_SetItemString
151# define PyErr_BadArgument py3_PyErr_BadArgument
152# define PyErr_Clear py3_PyErr_Clear
Bram Moolenaarc476e522013-06-23 13:46:40 +0200153# define PyErr_Format py3_PyErr_Format
Bram Moolenaar4d369872013-02-20 16:09:43 +0100154# define PyErr_PrintEx py3_PyErr_PrintEx
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200155# define PyErr_NoMemory py3_PyErr_NoMemory
156# define PyErr_Occurred py3_PyErr_Occurred
157# define PyErr_SetNone py3_PyErr_SetNone
158# define PyErr_SetString py3_PyErr_SetString
Bram Moolenaar4d188da2013-05-15 15:35:09 +0200159# define PyErr_SetObject py3_PyErr_SetObject
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200160# define PyErr_ExceptionMatches py3_PyErr_ExceptionMatches
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200161# define PyEval_InitThreads py3_PyEval_InitThreads
162# define PyEval_RestoreThread py3_PyEval_RestoreThread
163# define PyEval_SaveThread py3_PyEval_SaveThread
164# define PyGILState_Ensure py3_PyGILState_Ensure
165# define PyGILState_Release py3_PyGILState_Release
166# define PyLong_AsLong py3_PyLong_AsLong
167# define PyLong_FromLong py3_PyLong_FromLong
168# define PyList_GetItem py3_PyList_GetItem
169# define PyList_Append py3_PyList_Append
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200170# define PyList_Insert py3_PyList_Insert
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200171# define PyList_New py3_PyList_New
172# define PyList_SetItem py3_PyList_SetItem
173# define PyList_Size py3_PyList_Size
Bram Moolenaardb913952012-06-29 12:54:53 +0200174# define PySequence_Check py3_PySequence_Check
175# define PySequence_Size py3_PySequence_Size
176# define PySequence_GetItem py3_PySequence_GetItem
Bram Moolenaara9922d62013-05-30 13:01:18 +0200177# define PySequence_Fast py3_PySequence_Fast
Bram Moolenaardb913952012-06-29 12:54:53 +0200178# define PyTuple_Size py3_PyTuple_Size
179# define PyTuple_GetItem py3_PyTuple_GetItem
Bram Moolenaar3b48b112018-07-04 22:03:25 +0200180# if PY_VERSION_HEX >= 0x030601f0
181# define PySlice_AdjustIndices py3_PySlice_AdjustIndices
182# define PySlice_Unpack py3_PySlice_Unpack
183# endif
184# undef PySlice_GetIndicesEx
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200185# define PySlice_GetIndicesEx py3_PySlice_GetIndicesEx
186# define PyImport_ImportModule py3_PyImport_ImportModule
187# define PyObject_Init py3__PyObject_Init
188# define PyDict_New py3_PyDict_New
189# define PyDict_GetItemString py3_PyDict_GetItemString
Bram Moolenaardb913952012-06-29 12:54:53 +0200190# define PyDict_Next py3_PyDict_Next
191# define PyMapping_Check py3_PyMapping_Check
Bram Moolenaar32ac8cd2013-07-03 18:49:17 +0200192# ifndef PyMapping_Keys
193# define PyMapping_Keys py3_PyMapping_Keys
194# endif
Zdenek Dohnal90478f32021-06-14 15:08:30 +0200195# if PY_VERSION_HEX >= 0x030a00b2
196# define PyIter_Check py3_PyIter_Check
197# endif
Bram Moolenaardb913952012-06-29 12:54:53 +0200198# define PyIter_Next py3_PyIter_Next
199# define PyObject_GetIter py3_PyObject_GetIter
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200200# define PyObject_Repr py3_PyObject_Repr
Bram Moolenaarbcb40972013-05-30 13:22:13 +0200201# define PyObject_GetItem py3_PyObject_GetItem
Bram Moolenaar03db85b2013-05-15 14:51:35 +0200202# define PyObject_IsTrue py3_PyObject_IsTrue
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200203# define PyModule_GetDict py3_PyModule_GetDict
204#undef PyRun_SimpleString
205# define PyRun_SimpleString py3_PyRun_SimpleString
Bram Moolenaardb913952012-06-29 12:54:53 +0200206#undef PyRun_String
207# define PyRun_String py3_PyRun_String
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200208# define PyObject_GetAttrString py3_PyObject_GetAttrString
Bram Moolenaara9922d62013-05-30 13:01:18 +0200209# define PyObject_HasAttrString py3_PyObject_HasAttrString
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200210# define PyObject_SetAttrString py3_PyObject_SetAttrString
211# define PyObject_CallFunctionObjArgs py3_PyObject_CallFunctionObjArgs
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200212# define _PyObject_CallFunction_SizeT py3__PyObject_CallFunction_SizeT
Bram Moolenaarf4258302013-06-02 18:20:17 +0200213# define PyObject_Call py3_PyObject_Call
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200214# define PyEval_GetLocals py3_PyEval_GetLocals
215# define PyEval_GetGlobals py3_PyEval_GetGlobals
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200216# define PySys_SetObject py3_PySys_SetObject
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200217# define PySys_GetObject py3_PySys_GetObject
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200218# define PySys_SetArgv py3_PySys_SetArgv
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200219# define PyType_Ready py3_PyType_Ready
Bram Moolenaaree1b9312020-07-16 22:15:53 +0200220# if PY_VERSION_HEX >= 0x030900b0
221# define PyType_GetFlags py3_PyType_GetFlags
222# endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200223#undef Py_BuildValue
224# define Py_BuildValue py3_Py_BuildValue
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100225# define Py_SetPythonHome py3_Py_SetPythonHome
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200226# define Py_Initialize py3_Py_Initialize
227# define Py_Finalize py3_Py_Finalize
228# define Py_IsInitialized py3_Py_IsInitialized
229# define _Py_NoneStruct (*py3__Py_NoneStruct)
Bram Moolenaar66b79852012-09-21 14:00:35 +0200230# define _Py_FalseStruct (*py3__Py_FalseStruct)
231# define _Py_TrueStruct (*py3__Py_TrueStruct)
Bram Moolenaardb913952012-06-29 12:54:53 +0200232# define _PyObject_NextNotImplemented (*py3__PyObject_NextNotImplemented)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200233# define PyModule_AddObject py3_PyModule_AddObject
234# define PyImport_AppendInittab py3_PyImport_AppendInittab
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200235# define PyImport_AddModule py3_PyImport_AddModule
Bram Moolenaar7bc4f932012-10-14 03:22:56 +0200236# if PY_VERSION_HEX >= 0x030300f0
237# undef _PyUnicode_AsString
Bram Moolenaar9c9cbf12012-10-23 05:17:37 +0200238# define _PyUnicode_AsString py3_PyUnicode_AsUTF8
Bram Moolenaar7bc4f932012-10-14 03:22:56 +0200239# else
240# define _PyUnicode_AsString py3__PyUnicode_AsString
241# endif
Bram Moolenaar19e60942011-06-19 00:27:51 +0200242# undef PyUnicode_AsEncodedString
243# define PyUnicode_AsEncodedString py3_PyUnicode_AsEncodedString
244# undef PyBytes_AsString
245# define PyBytes_AsString py3_PyBytes_AsString
Bram Moolenaar32ac8cd2013-07-03 18:49:17 +0200246# ifndef PyBytes_AsStringAndSize
247# define PyBytes_AsStringAndSize py3_PyBytes_AsStringAndSize
248# endif
Bram Moolenaardb913952012-06-29 12:54:53 +0200249# undef PyBytes_FromString
250# define PyBytes_FromString py3_PyBytes_FromString
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100251# undef PyBytes_FromStringAndSize
252# define PyBytes_FromStringAndSize py3_PyBytes_FromStringAndSize
Bram Moolenaaree1b9312020-07-16 22:15:53 +0200253# if defined(Py_DEBUG) || PY_VERSION_HEX >= 0x030900b0
254# define _Py_Dealloc py3__Py_Dealloc
255# endif
Bram Moolenaardb913952012-06-29 12:54:53 +0200256# define PyFloat_FromDouble py3_PyFloat_FromDouble
257# define PyFloat_AsDouble py3_PyFloat_AsDouble
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200258# define PyObject_GenericGetAttr py3_PyObject_GenericGetAttr
Bram Moolenaar66b79852012-09-21 14:00:35 +0200259# define PyType_Type (*py3_PyType_Type)
Bram Moolenaard4a8c982018-05-15 22:31:18 +0200260# define PyStdPrinter_Type (*py3_PyStdPrinter_Type)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200261# define PySlice_Type (*py3_PySlice_Type)
Bram Moolenaardb913952012-06-29 12:54:53 +0200262# define PyFloat_Type (*py3_PyFloat_Type)
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200263# define PyNumber_Check (*py3_PyNumber_Check)
264# define PyNumber_Long (*py3_PyNumber_Long)
Bram Moolenaar66b79852012-09-21 14:00:35 +0200265# define PyBool_Type (*py3_PyBool_Type)
Bram Moolenaar19e60942011-06-19 00:27:51 +0200266# define PyErr_NewException py3_PyErr_NewException
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200267# ifdef Py_DEBUG
268# define _Py_NegativeRefcount py3__Py_NegativeRefcount
269# define _Py_RefTotal (*py3__Py_RefTotal)
Bram Moolenaar0014a532013-05-29 21:33:39 +0200270# define PyModule_Create2TraceRefs py3_PyModule_Create2TraceRefs
271# else
272# define PyModule_Create2 py3_PyModule_Create2
273# endif
274# if defined(Py_DEBUG) && !defined(Py_DEBUG_NO_PYMALLOC)
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200275# define _PyObject_DebugMalloc py3__PyObject_DebugMalloc
276# define _PyObject_DebugFree py3__PyObject_DebugFree
277# else
278# define PyObject_Malloc py3_PyObject_Malloc
279# define PyObject_Free py3_PyObject_Free
280# endif
Bram Moolenaar774267b2013-05-21 20:51:59 +0200281# define _PyObject_GC_New py3__PyObject_GC_New
282# define PyObject_GC_Del py3_PyObject_GC_Del
283# define PyObject_GC_UnTrack py3_PyObject_GC_UnTrack
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200284# define PyType_GenericAlloc py3_PyType_GenericAlloc
285# define PyType_GenericNew py3_PyType_GenericNew
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200286# undef PyUnicode_FromString
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200287# define PyUnicode_FromString py3_PyUnicode_FromString
Bram Moolenaar1a3b5692013-05-30 12:40:39 +0200288# ifndef PyUnicode_FromFormat
289# define PyUnicode_FromFormat py3_PyUnicode_FromFormat
290# else
291# define Py_UNICODE_USE_UCS_FUNCTIONS
292# ifdef Py_UNICODE_WIDE
293# define PyUnicodeUCS4_FromFormat py3_PyUnicodeUCS4_FromFormat
294# else
295# define PyUnicodeUCS2_FromFormat py3_PyUnicodeUCS2_FromFormat
296# endif
297# endif
Bram Moolenaar19e60942011-06-19 00:27:51 +0200298# undef PyUnicode_Decode
299# define PyUnicode_Decode py3_PyUnicode_Decode
Bram Moolenaardb913952012-06-29 12:54:53 +0200300# define PyType_IsSubtype py3_PyType_IsSubtype
301# define PyCapsule_New py3_PyCapsule_New
302# define PyCapsule_GetPointer py3_PyCapsule_GetPointer
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200303
Bram Moolenaar0014a532013-05-29 21:33:39 +0200304# if defined(Py_DEBUG) && !defined(Py_DEBUG_NO_PYMALLOC)
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200305# undef PyObject_NEW
306# define PyObject_NEW(type, typeobj) \
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200307( (type *) PyObject_Init( \
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200308 (PyObject *) _PyObject_DebugMalloc( _PyObject_SIZE(typeobj) ), (typeobj)) )
Bram Moolenaaree1b9312020-07-16 22:15:53 +0200309# elif PY_VERSION_HEX >= 0x030900b0
310# undef PyObject_NEW
311# define PyObject_NEW(type, typeobj) \
312 ((type *)py3__PyObject_New(typeobj))
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200313# endif
314
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200315/*
316 * Pointers for dynamic link
317 */
318static int (*py3_PySys_SetArgv)(int, wchar_t **);
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100319static void (*py3_Py_SetPythonHome)(wchar_t *home);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200320static void (*py3_Py_Initialize)(void);
321static PyObject* (*py3_PyList_New)(Py_ssize_t size);
322static PyGILState_STATE (*py3_PyGILState_Ensure)(void);
323static void (*py3_PyGILState_Release)(PyGILState_STATE);
324static int (*py3_PySys_SetObject)(char *, PyObject *);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200325static PyObject* (*py3_PySys_GetObject)(char *);
326static int (*py3_PyList_Append)(PyObject *, PyObject *);
327static int (*py3_PyList_Insert)(PyObject *, int, PyObject *);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200328static Py_ssize_t (*py3_PyList_Size)(PyObject *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200329static int (*py3_PySequence_Check)(PyObject *);
330static Py_ssize_t (*py3_PySequence_Size)(PyObject *);
331static PyObject* (*py3_PySequence_GetItem)(PyObject *, Py_ssize_t);
Bram Moolenaara9922d62013-05-30 13:01:18 +0200332static PyObject* (*py3_PySequence_Fast)(PyObject *, const char *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200333static Py_ssize_t (*py3_PyTuple_Size)(PyObject *);
334static PyObject* (*py3_PyTuple_GetItem)(PyObject *, Py_ssize_t);
335static int (*py3_PyMapping_Check)(PyObject *);
Bram Moolenaarbcb40972013-05-30 13:22:13 +0200336static PyObject* (*py3_PyMapping_Keys)(PyObject *);
Bram Moolenaar3b48b112018-07-04 22:03:25 +0200337# if PY_VERSION_HEX >= 0x030601f0
338static int (*py3_PySlice_AdjustIndices)(Py_ssize_t length,
339 Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t step);
340static int (*py3_PySlice_Unpack)(PyObject *slice,
341 Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t *step);
342# endif
Bram Moolenaar922a4662014-03-30 16:11:43 +0200343static int (*py3_PySlice_GetIndicesEx)(PySliceObject_T *r, Py_ssize_t length,
Bram Moolenaar063a46b2014-01-14 16:36:51 +0100344 Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t *step,
345 Py_ssize_t *slicelen);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200346static PyObject* (*py3_PyErr_NoMemory)(void);
347static void (*py3_Py_Finalize)(void);
348static void (*py3_PyErr_SetString)(PyObject *, const char *);
Bram Moolenaar4d188da2013-05-15 15:35:09 +0200349static void (*py3_PyErr_SetObject)(PyObject *, PyObject *);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200350static int (*py3_PyErr_ExceptionMatches)(PyObject *);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200351static int (*py3_PyRun_SimpleString)(char *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200352static PyObject* (*py3_PyRun_String)(char *, int, PyObject *, PyObject *);
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200353static PyObject* (*py3_PyObject_GetAttrString)(PyObject *, const char *);
Bram Moolenaara9922d62013-05-30 13:01:18 +0200354static int (*py3_PyObject_HasAttrString)(PyObject *, const char *);
Bram Moolenaar0b400082013-11-03 00:28:25 +0100355static int (*py3_PyObject_SetAttrString)(PyObject *, const char *, PyObject *);
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200356static PyObject* (*py3_PyObject_CallFunctionObjArgs)(PyObject *, ...);
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200357static PyObject* (*py3__PyObject_CallFunction_SizeT)(PyObject *, char *, ...);
Bram Moolenaarf4258302013-06-02 18:20:17 +0200358static PyObject* (*py3_PyObject_Call)(PyObject *, PyObject *, PyObject *);
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200359static PyObject* (*py3_PyEval_GetGlobals)();
360static PyObject* (*py3_PyEval_GetLocals)();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200361static PyObject* (*py3_PyList_GetItem)(PyObject *, Py_ssize_t);
362static PyObject* (*py3_PyImport_ImportModule)(const char *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200363static PyObject* (*py3_PyImport_AddModule)(const char *);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200364static int (*py3_PyErr_BadArgument)(void);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200365static PyObject* (*py3_PyErr_Occurred)(void);
366static PyObject* (*py3_PyModule_GetDict)(PyObject *);
367static int (*py3_PyList_SetItem)(PyObject *, Py_ssize_t, PyObject *);
368static PyObject* (*py3_PyDict_GetItemString)(PyObject *, const char *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200369static int (*py3_PyDict_Next)(PyObject *, Py_ssize_t *, PyObject **, PyObject **);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200370static PyObject* (*py3_PyLong_FromLong)(long);
371static PyObject* (*py3_PyDict_New)(void);
Zdenek Dohnal90478f32021-06-14 15:08:30 +0200372# if PY_VERSION_HEX >= 0x030a00b2
373static int (*py3_PyIter_Check)(PyObject *o);
374# endif
Bram Moolenaardb913952012-06-29 12:54:53 +0200375static PyObject* (*py3_PyIter_Next)(PyObject *);
376static PyObject* (*py3_PyObject_GetIter)(PyObject *);
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200377static PyObject* (*py3_PyObject_Repr)(PyObject *);
Bram Moolenaarbcb40972013-05-30 13:22:13 +0200378static PyObject* (*py3_PyObject_GetItem)(PyObject *, PyObject *);
Bram Moolenaar03db85b2013-05-15 14:51:35 +0200379static int (*py3_PyObject_IsTrue)(PyObject *);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200380static PyObject* (*py3_Py_BuildValue)(char *, ...);
Bram Moolenaaree1b9312020-07-16 22:15:53 +0200381# if PY_VERSION_HEX >= 0x030900b0
382static int (*py3_PyType_GetFlags)(PyTypeObject *o);
383# endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200384static int (*py3_PyType_Ready)(PyTypeObject *type);
385static int (*py3_PyDict_SetItemString)(PyObject *dp, char *key, PyObject *item);
386static PyObject* (*py3_PyUnicode_FromString)(const char *u);
Bram Moolenaar1a3b5692013-05-30 12:40:39 +0200387# ifndef Py_UNICODE_USE_UCS_FUNCTIONS
388static PyObject* (*py3_PyUnicode_FromFormat)(const char *u, ...);
389# else
390# ifdef Py_UNICODE_WIDE
391static PyObject* (*py3_PyUnicodeUCS4_FromFormat)(const char *u, ...);
392# else
393static PyObject* (*py3_PyUnicodeUCS2_FromFormat)(const char *u, ...);
394# endif
395# endif
Bram Moolenaar19e60942011-06-19 00:27:51 +0200396static PyObject* (*py3_PyUnicode_Decode)(const char *u, Py_ssize_t size,
397 const char *encoding, const char *errors);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200398static long (*py3_PyLong_AsLong)(PyObject *);
399static void (*py3_PyErr_SetNone)(PyObject *);
400static void (*py3_PyEval_InitThreads)(void);
401static void(*py3_PyEval_RestoreThread)(PyThreadState *);
402static PyThreadState*(*py3_PyEval_SaveThread)(void);
403static int (*py3_PyArg_Parse)(PyObject *, char *, ...);
404static int (*py3_PyArg_ParseTuple)(PyObject *, char *, ...);
Bram Moolenaar19e60942011-06-19 00:27:51 +0200405static int (*py3_PyMem_Free)(void *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200406static void* (*py3_PyMem_Malloc)(size_t);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200407static int (*py3_Py_IsInitialized)(void);
408static void (*py3_PyErr_Clear)(void);
Bram Moolenaarc476e522013-06-23 13:46:40 +0200409static PyObject* (*py3_PyErr_Format)(PyObject *, const char *, ...);
Bram Moolenaar4d369872013-02-20 16:09:43 +0100410static void (*py3_PyErr_PrintEx)(int);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200411static PyObject*(*py3__PyObject_Init)(PyObject *, PyTypeObject *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200412static iternextfunc py3__PyObject_NextNotImplemented;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200413static PyObject* py3__Py_NoneStruct;
Bram Moolenaar66b79852012-09-21 14:00:35 +0200414static PyObject* py3__Py_FalseStruct;
415static PyObject* py3__Py_TrueStruct;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200416static int (*py3_PyModule_AddObject)(PyObject *m, const char *name, PyObject *o);
417static int (*py3_PyImport_AppendInittab)(const char *name, PyObject* (*initfunc)(void));
Bram Moolenaar9c9cbf12012-10-23 05:17:37 +0200418# if PY_VERSION_HEX >= 0x030300f0
419static char* (*py3_PyUnicode_AsUTF8)(PyObject *unicode);
420# else
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200421static char* (*py3__PyUnicode_AsString)(PyObject *unicode);
Bram Moolenaar9c9cbf12012-10-23 05:17:37 +0200422# endif
Bram Moolenaar19e60942011-06-19 00:27:51 +0200423static PyObject* (*py3_PyUnicode_AsEncodedString)(PyObject *unicode, const char* encoding, const char* errors);
424static char* (*py3_PyBytes_AsString)(PyObject *bytes);
Bram Moolenaar808c2bc2013-06-23 13:11:18 +0200425static int (*py3_PyBytes_AsStringAndSize)(PyObject *bytes, char **buffer, Py_ssize_t *length);
Bram Moolenaardb913952012-06-29 12:54:53 +0200426static PyObject* (*py3_PyBytes_FromString)(char *str);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100427static PyObject* (*py3_PyBytes_FromStringAndSize)(char *str, Py_ssize_t length);
Bram Moolenaaree1b9312020-07-16 22:15:53 +0200428# if defined(Py_DEBUG) || PY_VERSION_HEX >= 0x030900b0
429static void (*py3__Py_Dealloc)(PyObject *obj);
430# endif
431# if PY_VERSION_HEX >= 0x030900b0
432static PyObject* (*py3__PyObject_New)(PyTypeObject *);
433# endif
Bram Moolenaardb913952012-06-29 12:54:53 +0200434static PyObject* (*py3_PyFloat_FromDouble)(double num);
435static double (*py3_PyFloat_AsDouble)(PyObject *);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200436static PyObject* (*py3_PyObject_GenericGetAttr)(PyObject *obj, PyObject *name);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200437static PyObject* (*py3_PyType_GenericAlloc)(PyTypeObject *type, Py_ssize_t nitems);
438static PyObject* (*py3_PyType_GenericNew)(PyTypeObject *type, PyObject *args, PyObject *kwds);
Bram Moolenaar66b79852012-09-21 14:00:35 +0200439static PyTypeObject* py3_PyType_Type;
Bram Moolenaard4a8c982018-05-15 22:31:18 +0200440static PyTypeObject* py3_PyStdPrinter_Type;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200441static PyTypeObject* py3_PySlice_Type;
Bram Moolenaardb913952012-06-29 12:54:53 +0200442static PyTypeObject* py3_PyFloat_Type;
Bram Moolenaar66b79852012-09-21 14:00:35 +0200443static PyTypeObject* py3_PyBool_Type;
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200444static int (*py3_PyNumber_Check)(PyObject *);
445static PyObject* (*py3_PyNumber_Long)(PyObject *);
Bram Moolenaar19e60942011-06-19 00:27:51 +0200446static PyObject* (*py3_PyErr_NewException)(char *name, PyObject *base, PyObject *dict);
Bram Moolenaardb913952012-06-29 12:54:53 +0200447static PyObject* (*py3_PyCapsule_New)(void *, char *, PyCapsule_Destructor);
448static void* (*py3_PyCapsule_GetPointer)(PyObject *, char *);
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200449# ifdef Py_DEBUG
Bram Moolenaar0014a532013-05-29 21:33:39 +0200450static void (*py3__Py_NegativeRefcount)(const char *fname, int lineno, PyObject *op);
451static Py_ssize_t* py3__Py_RefTotal;
Bram Moolenaar0014a532013-05-29 21:33:39 +0200452static PyObject* (*py3_PyModule_Create2TraceRefs)(struct PyModuleDef* module, int module_api_version);
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200453# else
Bram Moolenaar0014a532013-05-29 21:33:39 +0200454static PyObject* (*py3_PyModule_Create2)(struct PyModuleDef* module, int module_api_version);
455# endif
456# if defined(Py_DEBUG) && !defined(Py_DEBUG_NO_PYMALLOC)
457static void (*py3__PyObject_DebugFree)(void*);
458static void* (*py3__PyObject_DebugMalloc)(size_t);
459# else
460static void (*py3_PyObject_Free)(void*);
461static void* (*py3_PyObject_Malloc)(size_t);
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200462# endif
Bram Moolenaar774267b2013-05-21 20:51:59 +0200463static PyObject*(*py3__PyObject_GC_New)(PyTypeObject *);
464static void(*py3_PyObject_GC_Del)(void *);
465static void(*py3_PyObject_GC_UnTrack)(void *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200466static int (*py3_PyType_IsSubtype)(PyTypeObject *, PyTypeObject *);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200467
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100468// Imported exception objects
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200469static PyObject *p3imp_PyExc_AttributeError;
470static PyObject *p3imp_PyExc_IndexError;
Bram Moolenaaraf6abb92013-04-24 13:04:26 +0200471static PyObject *p3imp_PyExc_KeyError;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200472static PyObject *p3imp_PyExc_KeyboardInterrupt;
473static PyObject *p3imp_PyExc_TypeError;
474static PyObject *p3imp_PyExc_ValueError;
Bram Moolenaar41009372013-07-01 22:03:04 +0200475static PyObject *p3imp_PyExc_SystemExit;
Bram Moolenaar8661b172013-05-15 15:44:28 +0200476static PyObject *p3imp_PyExc_RuntimeError;
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200477static PyObject *p3imp_PyExc_ImportError;
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200478static PyObject *p3imp_PyExc_OverflowError;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200479
480# define PyExc_AttributeError p3imp_PyExc_AttributeError
481# define PyExc_IndexError p3imp_PyExc_IndexError
Bram Moolenaaraf6abb92013-04-24 13:04:26 +0200482# define PyExc_KeyError p3imp_PyExc_KeyError
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200483# define PyExc_KeyboardInterrupt p3imp_PyExc_KeyboardInterrupt
484# define PyExc_TypeError p3imp_PyExc_TypeError
485# define PyExc_ValueError p3imp_PyExc_ValueError
Bram Moolenaar41009372013-07-01 22:03:04 +0200486# define PyExc_SystemExit p3imp_PyExc_SystemExit
Bram Moolenaar8661b172013-05-15 15:44:28 +0200487# define PyExc_RuntimeError p3imp_PyExc_RuntimeError
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200488# define PyExc_ImportError p3imp_PyExc_ImportError
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200489# define PyExc_OverflowError p3imp_PyExc_OverflowError
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200490
491/*
492 * Table of name to function pointer of python.
493 */
494# define PYTHON_PROC FARPROC
495static struct
496{
497 char *name;
498 PYTHON_PROC *ptr;
499} py3_funcname_table[] =
500{
501 {"PySys_SetArgv", (PYTHON_PROC*)&py3_PySys_SetArgv},
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100502 {"Py_SetPythonHome", (PYTHON_PROC*)&py3_Py_SetPythonHome},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200503 {"Py_Initialize", (PYTHON_PROC*)&py3_Py_Initialize},
Bram Moolenaare8cdcef2012-09-12 20:21:43 +0200504 {"_PyArg_ParseTuple_SizeT", (PYTHON_PROC*)&py3_PyArg_ParseTuple},
505 {"_Py_BuildValue_SizeT", (PYTHON_PROC*)&py3_Py_BuildValue},
Bram Moolenaar19e60942011-06-19 00:27:51 +0200506 {"PyMem_Free", (PYTHON_PROC*)&py3_PyMem_Free},
Bram Moolenaardb913952012-06-29 12:54:53 +0200507 {"PyMem_Malloc", (PYTHON_PROC*)&py3_PyMem_Malloc},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200508 {"PyList_New", (PYTHON_PROC*)&py3_PyList_New},
509 {"PyGILState_Ensure", (PYTHON_PROC*)&py3_PyGILState_Ensure},
510 {"PyGILState_Release", (PYTHON_PROC*)&py3_PyGILState_Release},
511 {"PySys_SetObject", (PYTHON_PROC*)&py3_PySys_SetObject},
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200512 {"PySys_GetObject", (PYTHON_PROC*)&py3_PySys_GetObject},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200513 {"PyList_Append", (PYTHON_PROC*)&py3_PyList_Append},
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200514 {"PyList_Insert", (PYTHON_PROC*)&py3_PyList_Insert},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200515 {"PyList_Size", (PYTHON_PROC*)&py3_PyList_Size},
Bram Moolenaardb913952012-06-29 12:54:53 +0200516 {"PySequence_Check", (PYTHON_PROC*)&py3_PySequence_Check},
517 {"PySequence_Size", (PYTHON_PROC*)&py3_PySequence_Size},
518 {"PySequence_GetItem", (PYTHON_PROC*)&py3_PySequence_GetItem},
Bram Moolenaara9922d62013-05-30 13:01:18 +0200519 {"PySequence_Fast", (PYTHON_PROC*)&py3_PySequence_Fast},
Bram Moolenaardb913952012-06-29 12:54:53 +0200520 {"PyTuple_Size", (PYTHON_PROC*)&py3_PyTuple_Size},
521 {"PyTuple_GetItem", (PYTHON_PROC*)&py3_PyTuple_GetItem},
Bram Moolenaar3b48b112018-07-04 22:03:25 +0200522# if PY_VERSION_HEX >= 0x030601f0
523 {"PySlice_AdjustIndices", (PYTHON_PROC*)&py3_PySlice_AdjustIndices},
524 {"PySlice_Unpack", (PYTHON_PROC*)&py3_PySlice_Unpack},
525# endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200526 {"PySlice_GetIndicesEx", (PYTHON_PROC*)&py3_PySlice_GetIndicesEx},
527 {"PyErr_NoMemory", (PYTHON_PROC*)&py3_PyErr_NoMemory},
528 {"Py_Finalize", (PYTHON_PROC*)&py3_Py_Finalize},
529 {"PyErr_SetString", (PYTHON_PROC*)&py3_PyErr_SetString},
Bram Moolenaar4d188da2013-05-15 15:35:09 +0200530 {"PyErr_SetObject", (PYTHON_PROC*)&py3_PyErr_SetObject},
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200531 {"PyErr_ExceptionMatches", (PYTHON_PROC*)&py3_PyErr_ExceptionMatches},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200532 {"PyRun_SimpleString", (PYTHON_PROC*)&py3_PyRun_SimpleString},
Bram Moolenaardb913952012-06-29 12:54:53 +0200533 {"PyRun_String", (PYTHON_PROC*)&py3_PyRun_String},
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200534 {"PyObject_GetAttrString", (PYTHON_PROC*)&py3_PyObject_GetAttrString},
Bram Moolenaara9922d62013-05-30 13:01:18 +0200535 {"PyObject_HasAttrString", (PYTHON_PROC*)&py3_PyObject_HasAttrString},
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200536 {"PyObject_SetAttrString", (PYTHON_PROC*)&py3_PyObject_SetAttrString},
537 {"PyObject_CallFunctionObjArgs", (PYTHON_PROC*)&py3_PyObject_CallFunctionObjArgs},
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200538 {"_PyObject_CallFunction_SizeT", (PYTHON_PROC*)&py3__PyObject_CallFunction_SizeT},
Bram Moolenaarf4258302013-06-02 18:20:17 +0200539 {"PyObject_Call", (PYTHON_PROC*)&py3_PyObject_Call},
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200540 {"PyEval_GetGlobals", (PYTHON_PROC*)&py3_PyEval_GetGlobals},
541 {"PyEval_GetLocals", (PYTHON_PROC*)&py3_PyEval_GetLocals},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200542 {"PyList_GetItem", (PYTHON_PROC*)&py3_PyList_GetItem},
543 {"PyImport_ImportModule", (PYTHON_PROC*)&py3_PyImport_ImportModule},
Bram Moolenaardb913952012-06-29 12:54:53 +0200544 {"PyImport_AddModule", (PYTHON_PROC*)&py3_PyImport_AddModule},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200545 {"PyErr_BadArgument", (PYTHON_PROC*)&py3_PyErr_BadArgument},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200546 {"PyErr_Occurred", (PYTHON_PROC*)&py3_PyErr_Occurred},
547 {"PyModule_GetDict", (PYTHON_PROC*)&py3_PyModule_GetDict},
548 {"PyList_SetItem", (PYTHON_PROC*)&py3_PyList_SetItem},
549 {"PyDict_GetItemString", (PYTHON_PROC*)&py3_PyDict_GetItemString},
Bram Moolenaardb913952012-06-29 12:54:53 +0200550 {"PyDict_Next", (PYTHON_PROC*)&py3_PyDict_Next},
551 {"PyMapping_Check", (PYTHON_PROC*)&py3_PyMapping_Check},
Bram Moolenaarbcb40972013-05-30 13:22:13 +0200552 {"PyMapping_Keys", (PYTHON_PROC*)&py3_PyMapping_Keys},
Zdenek Dohnal90478f32021-06-14 15:08:30 +0200553# if PY_VERSION_HEX >= 0x030a00b2
554 {"PyIter_Check", (PYTHON_PROC*)&py3_PyIter_Check},
555# endif
Bram Moolenaardb913952012-06-29 12:54:53 +0200556 {"PyIter_Next", (PYTHON_PROC*)&py3_PyIter_Next},
557 {"PyObject_GetIter", (PYTHON_PROC*)&py3_PyObject_GetIter},
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200558 {"PyObject_Repr", (PYTHON_PROC*)&py3_PyObject_Repr},
Bram Moolenaarbcb40972013-05-30 13:22:13 +0200559 {"PyObject_GetItem", (PYTHON_PROC*)&py3_PyObject_GetItem},
Bram Moolenaar03db85b2013-05-15 14:51:35 +0200560 {"PyObject_IsTrue", (PYTHON_PROC*)&py3_PyObject_IsTrue},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200561 {"PyLong_FromLong", (PYTHON_PROC*)&py3_PyLong_FromLong},
562 {"PyDict_New", (PYTHON_PROC*)&py3_PyDict_New},
Bram Moolenaaree1b9312020-07-16 22:15:53 +0200563# if PY_VERSION_HEX >= 0x030900b0
564 {"PyType_GetFlags", (PYTHON_PROC*)&py3_PyType_GetFlags},
565# endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200566 {"PyType_Ready", (PYTHON_PROC*)&py3_PyType_Ready},
567 {"PyDict_SetItemString", (PYTHON_PROC*)&py3_PyDict_SetItemString},
568 {"PyLong_AsLong", (PYTHON_PROC*)&py3_PyLong_AsLong},
569 {"PyErr_SetNone", (PYTHON_PROC*)&py3_PyErr_SetNone},
570 {"PyEval_InitThreads", (PYTHON_PROC*)&py3_PyEval_InitThreads},
571 {"PyEval_RestoreThread", (PYTHON_PROC*)&py3_PyEval_RestoreThread},
572 {"PyEval_SaveThread", (PYTHON_PROC*)&py3_PyEval_SaveThread},
Bram Moolenaar5f87b232013-06-13 20:57:50 +0200573 {"_PyArg_Parse_SizeT", (PYTHON_PROC*)&py3_PyArg_Parse},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200574 {"Py_IsInitialized", (PYTHON_PROC*)&py3_Py_IsInitialized},
Bram Moolenaardb913952012-06-29 12:54:53 +0200575 {"_PyObject_NextNotImplemented", (PYTHON_PROC*)&py3__PyObject_NextNotImplemented},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200576 {"_Py_NoneStruct", (PYTHON_PROC*)&py3__Py_NoneStruct},
Bram Moolenaar66b79852012-09-21 14:00:35 +0200577 {"_Py_FalseStruct", (PYTHON_PROC*)&py3__Py_FalseStruct},
578 {"_Py_TrueStruct", (PYTHON_PROC*)&py3__Py_TrueStruct},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200579 {"PyErr_Clear", (PYTHON_PROC*)&py3_PyErr_Clear},
Bram Moolenaarc476e522013-06-23 13:46:40 +0200580 {"PyErr_Format", (PYTHON_PROC*)&py3_PyErr_Format},
Bram Moolenaar4d369872013-02-20 16:09:43 +0100581 {"PyErr_PrintEx", (PYTHON_PROC*)&py3_PyErr_PrintEx},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200582 {"PyObject_Init", (PYTHON_PROC*)&py3__PyObject_Init},
583 {"PyModule_AddObject", (PYTHON_PROC*)&py3_PyModule_AddObject},
584 {"PyImport_AppendInittab", (PYTHON_PROC*)&py3_PyImport_AppendInittab},
Bram Moolenaar9c9cbf12012-10-23 05:17:37 +0200585# if PY_VERSION_HEX >= 0x030300f0
586 {"PyUnicode_AsUTF8", (PYTHON_PROC*)&py3_PyUnicode_AsUTF8},
587# else
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200588 {"_PyUnicode_AsString", (PYTHON_PROC*)&py3__PyUnicode_AsString},
Bram Moolenaar9c9cbf12012-10-23 05:17:37 +0200589# endif
Bram Moolenaar1a3b5692013-05-30 12:40:39 +0200590# ifndef Py_UNICODE_USE_UCS_FUNCTIONS
591 {"PyUnicode_FromFormat", (PYTHON_PROC*)&py3_PyUnicode_FromFormat},
592# else
593# ifdef Py_UNICODE_WIDE
594 {"PyUnicodeUCS4_FromFormat", (PYTHON_PROC*)&py3_PyUnicodeUCS4_FromFormat},
595# else
596 {"PyUnicodeUCS2_FromFormat", (PYTHON_PROC*)&py3_PyUnicodeUCS2_FromFormat},
597# endif
598# endif
Bram Moolenaar19e60942011-06-19 00:27:51 +0200599 {"PyBytes_AsString", (PYTHON_PROC*)&py3_PyBytes_AsString},
Bram Moolenaarcdab9052012-09-05 19:03:56 +0200600 {"PyBytes_AsStringAndSize", (PYTHON_PROC*)&py3_PyBytes_AsStringAndSize},
Bram Moolenaardb913952012-06-29 12:54:53 +0200601 {"PyBytes_FromString", (PYTHON_PROC*)&py3_PyBytes_FromString},
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100602 {"PyBytes_FromStringAndSize", (PYTHON_PROC*)&py3_PyBytes_FromStringAndSize},
Bram Moolenaaree1b9312020-07-16 22:15:53 +0200603# if defined(Py_DEBUG) || PY_VERSION_HEX >= 0x030900b0
604 {"_Py_Dealloc", (PYTHON_PROC*)&py3__Py_Dealloc},
605# endif
606# if PY_VERSION_HEX >= 0x030900b0
607 {"_PyObject_New", (PYTHON_PROC*)&py3__PyObject_New},
608# endif
Bram Moolenaardb913952012-06-29 12:54:53 +0200609 {"PyFloat_FromDouble", (PYTHON_PROC*)&py3_PyFloat_FromDouble},
610 {"PyFloat_AsDouble", (PYTHON_PROC*)&py3_PyFloat_AsDouble},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200611 {"PyObject_GenericGetAttr", (PYTHON_PROC*)&py3_PyObject_GenericGetAttr},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200612 {"PyType_GenericAlloc", (PYTHON_PROC*)&py3_PyType_GenericAlloc},
613 {"PyType_GenericNew", (PYTHON_PROC*)&py3_PyType_GenericNew},
Bram Moolenaar66b79852012-09-21 14:00:35 +0200614 {"PyType_Type", (PYTHON_PROC*)&py3_PyType_Type},
Bram Moolenaard4a8c982018-05-15 22:31:18 +0200615 {"PyStdPrinter_Type", (PYTHON_PROC*)&py3_PyStdPrinter_Type},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200616 {"PySlice_Type", (PYTHON_PROC*)&py3_PySlice_Type},
Bram Moolenaardb913952012-06-29 12:54:53 +0200617 {"PyFloat_Type", (PYTHON_PROC*)&py3_PyFloat_Type},
Bram Moolenaar66b79852012-09-21 14:00:35 +0200618 {"PyBool_Type", (PYTHON_PROC*)&py3_PyBool_Type},
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200619 {"PyNumber_Check", (PYTHON_PROC*)&py3_PyNumber_Check},
620 {"PyNumber_Long", (PYTHON_PROC*)&py3_PyNumber_Long},
Bram Moolenaar19e60942011-06-19 00:27:51 +0200621 {"PyErr_NewException", (PYTHON_PROC*)&py3_PyErr_NewException},
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200622# ifdef Py_DEBUG
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200623 {"_Py_NegativeRefcount", (PYTHON_PROC*)&py3__Py_NegativeRefcount},
624 {"_Py_RefTotal", (PYTHON_PROC*)&py3__Py_RefTotal},
Bram Moolenaar0014a532013-05-29 21:33:39 +0200625 {"PyModule_Create2TraceRefs", (PYTHON_PROC*)&py3_PyModule_Create2TraceRefs},
626# else
627 {"PyModule_Create2", (PYTHON_PROC*)&py3_PyModule_Create2},
628# endif
629# if defined(Py_DEBUG) && !defined(Py_DEBUG_NO_PYMALLOC)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200630 {"_PyObject_DebugFree", (PYTHON_PROC*)&py3__PyObject_DebugFree},
631 {"_PyObject_DebugMalloc", (PYTHON_PROC*)&py3__PyObject_DebugMalloc},
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200632# else
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200633 {"PyObject_Malloc", (PYTHON_PROC*)&py3_PyObject_Malloc},
634 {"PyObject_Free", (PYTHON_PROC*)&py3_PyObject_Free},
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200635# endif
Bram Moolenaar774267b2013-05-21 20:51:59 +0200636 {"_PyObject_GC_New", (PYTHON_PROC*)&py3__PyObject_GC_New},
637 {"PyObject_GC_Del", (PYTHON_PROC*)&py3_PyObject_GC_Del},
638 {"PyObject_GC_UnTrack", (PYTHON_PROC*)&py3_PyObject_GC_UnTrack},
Bram Moolenaardb913952012-06-29 12:54:53 +0200639 {"PyType_IsSubtype", (PYTHON_PROC*)&py3_PyType_IsSubtype},
640 {"PyCapsule_New", (PYTHON_PROC*)&py3_PyCapsule_New},
641 {"PyCapsule_GetPointer", (PYTHON_PROC*)&py3_PyCapsule_GetPointer},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200642 {"", NULL},
643};
644
Bram Moolenaar13a1f3f2019-10-23 21:37:25 +0200645# if PY_VERSION_HEX >= 0x030800f0
646 static inline void
647py3__Py_DECREF(const char *filename UNUSED, int lineno UNUSED, PyObject *op)
648{
Bram Moolenaar13a1f3f2019-10-23 21:37:25 +0200649 if (--op->ob_refcnt != 0)
650 {
651# ifdef Py_REF_DEBUG
652 if (op->ob_refcnt < 0)
653 {
654 _Py_NegativeRefcount(filename, lineno, op);
655 }
656# endif
657 }
658 else
659 {
660 _Py_Dealloc(op);
661 }
662}
663
664# undef Py_DECREF
665# define Py_DECREF(op) py3__Py_DECREF(__FILE__, __LINE__, _PyObject_CAST(op))
666
667 static inline void
668py3__Py_XDECREF(PyObject *op)
669{
670 if (op != NULL)
671 {
672 Py_DECREF(op);
673 }
674}
675
676# undef Py_XDECREF
677# define Py_XDECREF(op) py3__Py_XDECREF(_PyObject_CAST(op))
678# endif
679
Bram Moolenaaree1b9312020-07-16 22:15:53 +0200680# if PY_VERSION_HEX >= 0x030900b0
681 static inline int
682py3_PyType_HasFeature(PyTypeObject *type, unsigned long feature)
683{
684 return ((PyType_GetFlags(type) & feature) != 0);
685}
686# define PyType_HasFeature(t,f) py3_PyType_HasFeature(t,f)
687# endif
688
Zdenek Dohnal90478f32021-06-14 15:08:30 +0200689# if PY_VERSION_HEX >= 0x030a00b2
690 static inline int
691py3__PyObject_TypeCheck(PyObject *ob, PyTypeObject *type)
692{
693 return Py_IS_TYPE(ob, type) || PyType_IsSubtype(Py_TYPE(ob), type);
694}
695# define _PyObject_TypeCheck(o,t) py3__PyObject_TypeCheck(o,t)
696# endif
697
Bram Moolenaarb2f9e0e2020-12-25 13:52:37 +0100698# ifdef MSWIN
699/*
700 * Look up the library "libname" using the InstallPath registry key.
701 * Return NULL when failed. Return an allocated string when successful.
702 */
703 static char *
704py3_get_system_libname(const char *libname)
705{
706 const char *cp = libname;
707 char subkey[128];
708 HKEY hKey;
709 char installpath[MAXPATHL];
710 LONG len = sizeof(installpath);
711 LSTATUS rc;
712 size_t sysliblen;
713 char *syslibname;
714
715 while (*cp != '\0')
716 {
717 if (*cp == ':' || *cp == '\\' || *cp == '/')
718 {
719 // Bail out if "libname" contains path separator, assume it is
720 // an absolute path.
721 return NULL;
722 }
723 ++cp;
724 }
725 vim_snprintf(subkey, sizeof(subkey),
726# ifdef _WIN64
727 "Software\\Python\\PythonCore\\%d.%d\\InstallPath",
728# else
729 "Software\\Python\\PythonCore\\%d.%d-32\\InstallPath",
730# endif
731 PY_MAJOR_VERSION, PY_MINOR_VERSION);
732 if (RegOpenKeyExA(HKEY_LOCAL_MACHINE, subkey, 0, KEY_QUERY_VALUE, &hKey)
733 != ERROR_SUCCESS)
734 return NULL;
735 rc = RegQueryValueA(hKey, NULL, installpath, &len);
736 RegCloseKey(hKey);
737 if (ERROR_SUCCESS != rc)
738 return NULL;
739 cp = installpath + len;
740 // Just in case registry value contains null terminators.
741 while (cp > installpath && *(cp-1) == '\0')
742 --cp;
743 // Remove trailing path separators.
744 while (cp > installpath && (*(cp-1) == '\\' || *(cp-1) == '/'))
745 --cp;
746 // Ignore if InstallPath is effectively empty.
747 if (cp <= installpath)
748 return NULL;
749 sysliblen = (cp - installpath) + 1 + STRLEN(libname) + 1;
750 syslibname = alloc(sysliblen);
751 vim_snprintf(syslibname, sysliblen, "%.*s\\%s",
752 (int)(cp - installpath), installpath, libname);
753 return syslibname;
754}
755# endif
756
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200757/*
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200758 * Load library and get all pointers.
759 * Parameter 'libname' provides name of DLL.
760 * Return OK or FAIL.
761 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200762 static int
763py3_runtime_link_init(char *libname, int verbose)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200764{
765 int i;
Bram Moolenaar7b24ce02018-03-29 18:15:26 +0200766 PYTHON_PROC *ucs_from_string = (PYTHON_PROC *)&py3_PyUnicode_FromString;
767 PYTHON_PROC *ucs_decode = (PYTHON_PROC *)&py3_PyUnicode_Decode;
768 PYTHON_PROC *ucs_as_encoded_string =
769 (PYTHON_PROC *)&py3_PyUnicode_AsEncodedString;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200770
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100771# if !(defined(PY_NO_RTLD_GLOBAL) && defined(PY3_NO_RTLD_GLOBAL)) && defined(UNIX) && defined(FEAT_PYTHON)
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100772 // Can't have Python and Python3 loaded at the same time.
Dominique Pelleaf4a61a2021-12-27 17:21:41 +0000773 // It causes a crash, because RTLD_GLOBAL is needed for
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100774 // standard C extension libraries of one or both python versions.
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200775 if (python_loaded())
776 {
Bram Moolenaar9dc93ae2011-08-28 16:00:19 +0200777 if (verbose)
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +0000778 emsg(_(e_this_vim_cannot_execute_py3_after_using_python));
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200779 return FAIL;
780 }
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200781# endif
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200782
783 if (hinstPy3 != 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200784 return OK;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200785 hinstPy3 = load_dll(libname);
786
Bram Moolenaarb2f9e0e2020-12-25 13:52:37 +0100787# ifdef MSWIN
788 if (!hinstPy3)
789 {
790 // Attempt to use the path from InstallPath as stored in the registry.
791 char *syslibname = py3_get_system_libname(libname);
792
793 if (syslibname != NULL)
794 {
795 hinstPy3 = load_dll(syslibname);
796 vim_free(syslibname);
797 }
798 }
799# endif
800
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200801 if (!hinstPy3)
802 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200803 if (verbose)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000804 semsg(_(e_could_not_load_library_str_str), libname, load_dll_error());
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200805 return FAIL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200806 }
807
808 for (i = 0; py3_funcname_table[i].ptr; ++i)
809 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200810 if ((*py3_funcname_table[i].ptr = symbol_from_dll(hinstPy3,
811 py3_funcname_table[i].name)) == NULL)
812 {
813 close_dll(hinstPy3);
814 hinstPy3 = 0;
815 if (verbose)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000816 semsg(_(e_could_not_load_library_function_str), py3_funcname_table[i].name);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200817 return FAIL;
818 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200819 }
820
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100821 // Load unicode functions separately as only the ucs2 or the ucs4 functions
822 // will be present in the library.
Bram Moolenaar9c9cbf12012-10-23 05:17:37 +0200823# if PY_VERSION_HEX >= 0x030300f0
Bram Moolenaar7b24ce02018-03-29 18:15:26 +0200824 *ucs_from_string = symbol_from_dll(hinstPy3, "PyUnicode_FromString");
825 *ucs_decode = symbol_from_dll(hinstPy3, "PyUnicode_Decode");
826 *ucs_as_encoded_string = symbol_from_dll(hinstPy3,
Bram Moolenaar7bc4f932012-10-14 03:22:56 +0200827 "PyUnicode_AsEncodedString");
Bram Moolenaar9c9cbf12012-10-23 05:17:37 +0200828# else
Bram Moolenaar7b24ce02018-03-29 18:15:26 +0200829 *ucs_from_string = symbol_from_dll(hinstPy3, "PyUnicodeUCS2_FromString");
830 *ucs_decode = symbol_from_dll(hinstPy3,
Bram Moolenaar19e60942011-06-19 00:27:51 +0200831 "PyUnicodeUCS2_Decode");
Bram Moolenaar7b24ce02018-03-29 18:15:26 +0200832 *ucs_as_encoded_string = symbol_from_dll(hinstPy3,
Bram Moolenaar19e60942011-06-19 00:27:51 +0200833 "PyUnicodeUCS2_AsEncodedString");
Bram Moolenaar7b24ce02018-03-29 18:15:26 +0200834 if (*ucs_from_string == NULL || *ucs_decode == NULL
835 || *ucs_as_encoded_string == NULL)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200836 {
Bram Moolenaar7b24ce02018-03-29 18:15:26 +0200837 *ucs_from_string = symbol_from_dll(hinstPy3,
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200838 "PyUnicodeUCS4_FromString");
Bram Moolenaar7b24ce02018-03-29 18:15:26 +0200839 *ucs_decode = symbol_from_dll(hinstPy3,
Bram Moolenaar19e60942011-06-19 00:27:51 +0200840 "PyUnicodeUCS4_Decode");
Bram Moolenaar7b24ce02018-03-29 18:15:26 +0200841 *ucs_as_encoded_string = symbol_from_dll(hinstPy3,
Bram Moolenaar19e60942011-06-19 00:27:51 +0200842 "PyUnicodeUCS4_AsEncodedString");
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200843 }
Bram Moolenaar9c9cbf12012-10-23 05:17:37 +0200844# endif
Bram Moolenaar7b24ce02018-03-29 18:15:26 +0200845 if (*ucs_from_string == NULL || *ucs_decode == NULL
846 || *ucs_as_encoded_string == NULL)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200847 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200848 close_dll(hinstPy3);
849 hinstPy3 = 0;
850 if (verbose)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000851 semsg(_(e_could_not_load_library_function_str), "PyUnicode_UCSX_*");
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200852 return FAIL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200853 }
854
855 return OK;
856}
857
858/*
859 * If python is enabled (there is installed python on Windows system) return
860 * TRUE, else FALSE.
861 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200862 int
863python3_enabled(int verbose)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200864{
Bram Moolenaar25e4fcd2016-01-09 14:57:47 +0100865 return py3_runtime_link_init((char *)p_py3dll, verbose) == OK;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200866}
867
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100868/*
869 * Load the standard Python exceptions - don't import the symbols from the
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200870 * DLL, as this can cause errors (importing data symbols is not reliable).
871 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200872 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100873get_py3_exceptions(void)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200874{
875 PyObject *exmod = PyImport_ImportModule("builtins");
876 PyObject *exdict = PyModule_GetDict(exmod);
877 p3imp_PyExc_AttributeError = PyDict_GetItemString(exdict, "AttributeError");
878 p3imp_PyExc_IndexError = PyDict_GetItemString(exdict, "IndexError");
Bram Moolenaaraf6abb92013-04-24 13:04:26 +0200879 p3imp_PyExc_KeyError = PyDict_GetItemString(exdict, "KeyError");
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200880 p3imp_PyExc_KeyboardInterrupt = PyDict_GetItemString(exdict, "KeyboardInterrupt");
881 p3imp_PyExc_TypeError = PyDict_GetItemString(exdict, "TypeError");
882 p3imp_PyExc_ValueError = PyDict_GetItemString(exdict, "ValueError");
Bram Moolenaar41009372013-07-01 22:03:04 +0200883 p3imp_PyExc_SystemExit = PyDict_GetItemString(exdict, "SystemExit");
Bram Moolenaar8661b172013-05-15 15:44:28 +0200884 p3imp_PyExc_RuntimeError = PyDict_GetItemString(exdict, "RuntimeError");
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200885 p3imp_PyExc_ImportError = PyDict_GetItemString(exdict, "ImportError");
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200886 p3imp_PyExc_OverflowError = PyDict_GetItemString(exdict, "OverflowError");
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200887 Py_XINCREF(p3imp_PyExc_AttributeError);
888 Py_XINCREF(p3imp_PyExc_IndexError);
Bram Moolenaaraf6abb92013-04-24 13:04:26 +0200889 Py_XINCREF(p3imp_PyExc_KeyError);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200890 Py_XINCREF(p3imp_PyExc_KeyboardInterrupt);
891 Py_XINCREF(p3imp_PyExc_TypeError);
892 Py_XINCREF(p3imp_PyExc_ValueError);
Bram Moolenaar41009372013-07-01 22:03:04 +0200893 Py_XINCREF(p3imp_PyExc_SystemExit);
Bram Moolenaar8661b172013-05-15 15:44:28 +0200894 Py_XINCREF(p3imp_PyExc_RuntimeError);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200895 Py_XINCREF(p3imp_PyExc_ImportError);
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200896 Py_XINCREF(p3imp_PyExc_OverflowError);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200897 Py_XDECREF(exmod);
898}
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100899#endif // DYNAMIC_PYTHON3
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200900
Bram Moolenaardb913952012-06-29 12:54:53 +0200901static int py3initialised = 0;
Bram Moolenaardb913952012-06-29 12:54:53 +0200902#define PYINITIALISED py3initialised
Bram Moolenaarc4f83382017-07-07 14:50:44 +0200903static int python_end_called = FALSE;
Bram Moolenaardb913952012-06-29 12:54:53 +0200904
Bram Moolenaar774267b2013-05-21 20:51:59 +0200905#define DESTRUCTOR_FINISH(self) Py_TYPE(self)->tp_free((PyObject*)self)
Bram Moolenaardb913952012-06-29 12:54:53 +0200906
Bram Moolenaar971db462013-05-12 18:44:48 +0200907#define WIN_PYTHON_REF(win) win->w_python3_ref
908#define BUF_PYTHON_REF(buf) buf->b_python3_ref
Bram Moolenaar5e538ec2013-05-15 15:12:29 +0200909#define TAB_PYTHON_REF(tab) tab->tp_python3_ref
Bram Moolenaar971db462013-05-12 18:44:48 +0200910
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200911 static void
912call_PyObject_Free(void *p)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200913{
Bram Moolenaar0014a532013-05-29 21:33:39 +0200914#if defined(Py_DEBUG) && !defined(Py_DEBUG_NO_PYMALLOC)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200915 _PyObject_DebugFree(p);
916#else
917 PyObject_Free(p);
918#endif
919}
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200920
921 static PyObject *
922call_PyType_GenericNew(PyTypeObject *type, PyObject *args, PyObject *kwds)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200923{
924 return PyType_GenericNew(type,args,kwds);
925}
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200926
927 static PyObject *
928call_PyType_GenericAlloc(PyTypeObject *type, Py_ssize_t nitems)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200929{
930 return PyType_GenericAlloc(type,nitems);
931}
932
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200933static PyObject *OutputGetattro(PyObject *, PyObject *);
934static int OutputSetattro(PyObject *, PyObject *, PyObject *);
935static PyObject *BufferGetattro(PyObject *, PyObject *);
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200936static int BufferSetattro(PyObject *, PyObject *, PyObject *);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +0200937static PyObject *TabPageGetattro(PyObject *, PyObject *);
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200938static PyObject *WindowGetattro(PyObject *, PyObject *);
939static int WindowSetattro(PyObject *, PyObject *, PyObject *);
940static PyObject *RangeGetattro(PyObject *, PyObject *);
941static PyObject *CurrentGetattro(PyObject *, PyObject *);
942static int CurrentSetattro(PyObject *, PyObject *, PyObject *);
943static PyObject *DictionaryGetattro(PyObject *, PyObject *);
944static int DictionarySetattro(PyObject *, PyObject *, PyObject *);
945static PyObject *ListGetattro(PyObject *, PyObject *);
946static int ListSetattro(PyObject *, PyObject *, PyObject *);
947static PyObject *FunctionGetattro(PyObject *, PyObject *);
948
949static struct PyModuleDef vimmodule;
950
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +0200951#define PY_CAN_RECURSE
952
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200953/*
954 * Include the code shared with if_python.c
955 */
956#include "if_py_both.h"
957
Bram Moolenaar828bff12019-03-19 22:11:41 +0100958// NOTE: Must always be used at the start of a block, since it declares "name".
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200959#define GET_ATTR_STRING(name, nameobj) \
960 char *name = ""; \
961 if (PyUnicode_Check(nameobj)) \
Bram Moolenaar828bff12019-03-19 22:11:41 +0100962 name = (char *)_PyUnicode_AsString(nameobj)
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200963
964#define PY3OBJ_DELETED(obj) (obj->ob_base.ob_refcnt<=0)
965
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100966///////////////////////////////////////////////////////
967// Internal function prototypes.
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200968
Bram Moolenaar7854e3a2012-11-28 15:33:14 +0100969static PyObject *Py3Init_vim(void);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200970
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100971///////////////////////////////////////////////////////
972// 1. Python interpreter main program.
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200973
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200974 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100975python3_end(void)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200976{
977 static int recurse = 0;
978
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100979 // If a crash occurs while doing this, don't try again.
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200980 if (recurse != 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200981 return;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200982
Bram Moolenaarc4f83382017-07-07 14:50:44 +0200983 python_end_called = TRUE;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200984 ++recurse;
985
986#ifdef DYNAMIC_PYTHON3
987 if (hinstPy3)
988#endif
989 if (Py_IsInitialized())
990 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100991 // acquire lock before finalizing
Bram Moolenaar71700b82013-05-15 17:49:05 +0200992 PyGILState_Ensure();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200993
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200994 Py_Finalize();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200995 }
996
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200997 --recurse;
998}
999
Bram Moolenaar094454f2015-10-07 10:39:55 +02001000#if (defined(DYNAMIC_PYTHON3) && defined(DYNAMIC_PYTHON) && defined(FEAT_PYTHON) && defined(UNIX)) || defined(PROTO)
Bram Moolenaar4c3a3262010-07-24 15:42:14 +02001001 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001002python3_loaded(void)
Bram Moolenaar4c3a3262010-07-24 15:42:14 +02001003{
1004 return (hinstPy3 != 0);
1005}
1006#endif
1007
Bram Moolenaar94073162018-01-31 21:49:05 +01001008static wchar_t *py_home_buf = NULL;
1009
Bram Moolenaar56b8dc32020-08-06 21:47:11 +02001010#if defined(MSWIN) && (PY_VERSION_HEX >= 0x030500f0)
Bram Moolenaarc6ed2542020-10-10 23:26:28 +02001011/*
1012 * Return TRUE if stdin is readable from Python 3.
1013 */
1014 static BOOL
1015is_stdin_readable(void)
1016{
1017 DWORD mode, eventnum;
1018 struct _stat st;
1019 int fd = fileno(stdin);
1020 HANDLE hstdin = (HANDLE)_get_osfhandle(fd);
1021
1022 // Check if stdin is connected to the console.
1023 if (GetConsoleMode(hstdin, &mode))
1024 // Check if it is opened as input.
1025 return GetNumberOfConsoleInputEvents(hstdin, &eventnum);
1026
1027 return _fstat(fd, &st) == 0;
1028}
1029
1030// Python 3.5 or later will abort inside Py_Initialize() when stdin has
1031// been closed (i.e. executed by "vim -"). Reconnect stdin to CONIN$.
Bram Moolenaar56b8dc32020-08-06 21:47:11 +02001032// Note that the python DLL is linked to its own stdio DLL which can be
1033// differ from Vim's stdio.
1034 static void
1035reset_stdin(void)
1036{
1037 FILE *(*py__acrt_iob_func)(unsigned) = NULL;
1038 FILE *(*pyfreopen)(const char *, const char *, FILE *) = NULL;
Bram Moolenaar63ff72a2022-02-07 13:54:01 +00001039 HINSTANCE hinst = hinstPy3;
Bram Moolenaar56b8dc32020-08-06 21:47:11 +02001040
Bram Moolenaarc6ed2542020-10-10 23:26:28 +02001041 if (hinst == NULL || is_stdin_readable())
Bram Moolenaar56b8dc32020-08-06 21:47:11 +02001042 return;
1043
1044 // Get "freopen" and "stdin" which are used in the python DLL.
1045 // "stdin" is defined as "__acrt_iob_func(0)" in VC++ 2015 or later.
1046 py__acrt_iob_func = get_dll_import_func(hinst, "__acrt_iob_func");
1047 if (py__acrt_iob_func)
1048 {
1049 HINSTANCE hpystdiodll = find_imported_module_by_funcname(hinst,
Bram Moolenaar253b16a2020-10-06 19:59:06 +02001050 "__acrt_iob_func");
Bram Moolenaar56b8dc32020-08-06 21:47:11 +02001051 if (hpystdiodll)
Bram Moolenaar253b16a2020-10-06 19:59:06 +02001052 pyfreopen = (void *)GetProcAddress(hpystdiodll, "freopen");
Bram Moolenaar56b8dc32020-08-06 21:47:11 +02001053 }
1054
Bram Moolenaarc6ed2542020-10-10 23:26:28 +02001055 // Reconnect stdin to CONIN$.
Bram Moolenaar253b16a2020-10-06 19:59:06 +02001056 if (pyfreopen != NULL)
Bram Moolenaarc6ed2542020-10-10 23:26:28 +02001057 pyfreopen("CONIN$", "r", py__acrt_iob_func(0));
Bram Moolenaar56b8dc32020-08-06 21:47:11 +02001058 else
Bram Moolenaarc6ed2542020-10-10 23:26:28 +02001059 freopen("CONIN$", "r", stdin);
Bram Moolenaar56b8dc32020-08-06 21:47:11 +02001060}
1061#else
1062# define reset_stdin()
1063#endif
1064
Bram Moolenaar63ff72a2022-02-07 13:54:01 +00001065// Python 3.2 or later will abort inside Py_Initialize() when mandatory
1066// modules cannot be loaded (e.g. 'pythonthreehome' is wrongly set.).
1067// Install a hook to python dll's exit() and recover from it.
1068#if defined(MSWIN) && (PY_VERSION_HEX >= 0x030200f0)
1069# define HOOK_EXIT
1070# include <setjmp.h>
1071
1072static jmp_buf exit_hook_jump_buf;
1073static void *orig_exit = NULL;
1074
1075/*
1076 * Function that replaces exit() while calling Py_Initialize().
1077 */
1078 static void
1079hooked_exit(int ret)
1080{
1081 // Recover from exit.
1082 longjmp(exit_hook_jump_buf, 1);
1083}
1084
1085/*
1086 * Install a hook to python dll's exit().
1087 */
1088 static void
1089hook_py_exit(void)
1090{
1091 HINSTANCE hinst = hinstPy3;
1092
1093 if (hinst == NULL || orig_exit != NULL)
1094 return;
1095
1096 orig_exit = hook_dll_import_func(hinst, "exit", (void *)hooked_exit);
1097}
1098
1099/*
1100 * Remove the hook installed by hook_py_exit().
1101 */
1102 static void
1103restore_py_exit(void)
1104{
1105 HINSTANCE hinst = hinstPy3;
1106
1107 if (hinst == NULL)
1108 return;
1109
1110 if (orig_exit != NULL)
1111 hook_dll_import_func(hinst, "exit", orig_exit);
1112 orig_exit = NULL;
1113}
1114#endif
1115
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001116 static int
1117Python3_Init(void)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001118{
1119 if (!py3initialised)
1120 {
1121#ifdef DYNAMIC_PYTHON3
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001122 if (!python3_enabled(TRUE))
1123 {
Bram Moolenaar9a846fb2022-01-01 21:59:18 +00001124 emsg(_(e_sorry_this_command_is_disabled_python_library_could_not_be_found));
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001125 goto fail;
1126 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001127#endif
1128
1129 init_structs();
1130
Bram Moolenaar94073162018-01-31 21:49:05 +01001131 if (*p_py3home != NUL)
1132 {
1133 size_t len = mbstowcs(NULL, (char *)p_py3home, 0) + 1;
Bram Moolenaar644d37b2010-11-16 19:26:02 +01001134
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001135 // The string must not change later, make a copy in static memory.
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001136 py_home_buf = ALLOC_MULT(wchar_t, len);
Bram Moolenaar94073162018-01-31 21:49:05 +01001137 if (py_home_buf != NULL && mbstowcs(
1138 py_home_buf, (char *)p_py3home, len) != (size_t)-1)
1139 Py_SetPythonHome(py_home_buf);
1140 }
Bram Moolenaar644d37b2010-11-16 19:26:02 +01001141#ifdef PYTHON3_HOME
Bram Moolenaar94073162018-01-31 21:49:05 +01001142 else if (mch_getenv((char_u *)"PYTHONHOME") == NULL)
Bram Moolenaar10005652015-12-31 21:03:23 +01001143 Py_SetPythonHome(PYTHON3_HOME);
Bram Moolenaar644d37b2010-11-16 19:26:02 +01001144#endif
1145
Bram Moolenaar7bc4f932012-10-14 03:22:56 +02001146 PyImport_AppendInittab("vim", Py3Init_vim);
1147
Bram Moolenaar63ff72a2022-02-07 13:54:01 +00001148#if !defined(DYNAMIC_PYTHON3) && defined(MSWIN)
1149 hinstPy3 = GetModuleHandle(PYTHON3_DLL);
1150#endif
Bram Moolenaar56b8dc32020-08-06 21:47:11 +02001151 reset_stdin();
Bram Moolenaar63ff72a2022-02-07 13:54:01 +00001152
1153#ifdef HOOK_EXIT
1154 // Catch exit() called in Py_Initialize().
1155 hook_py_exit();
1156 if (setjmp(exit_hook_jump_buf) == 0)
1157#endif
1158 {
1159 Py_Initialize();
1160#ifdef HOOK_EXIT
1161 restore_py_exit();
1162#endif
1163 }
1164#ifdef HOOK_EXIT
1165 else
1166 {
1167 // exit() was called in Py_Initialize().
1168 restore_py_exit();
1169 emsg(_(e_critical_error_in_python3_initialization_check_your_installation));
1170 goto fail;
1171 }
1172#endif
Bram Moolenaard0573012017-10-28 21:11:06 +02001173
Bram Moolenaarefc0d942020-10-11 18:05:02 +02001174#if PY_VERSION_HEX < 0x03090000
1175 // Initialise threads. This is deprecated since Python 3.9.
Bram Moolenaar456f2bb2011-06-12 21:37:13 +02001176 PyEval_InitThreads();
Bram Moolenaarefc0d942020-10-11 18:05:02 +02001177#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001178#ifdef DYNAMIC_PYTHON3
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001179 get_py3_exceptions();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001180#endif
1181
Bram Moolenaar1dc28782013-05-21 19:11:01 +02001182 if (PythonIO_Init_io())
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001183 goto fail;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001184
Bram Moolenaardb913952012-06-29 12:54:53 +02001185 globals = PyModule_GetDict(PyImport_AddModule("__main__"));
1186
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001187 // Remove the element from sys.path that was added because of our
1188 // argv[0] value in Py3Init_vim(). Previously we used an empty
1189 // string, but depending on the OS we then get an empty entry or
1190 // the current directory in sys.path.
1191 // Only after vim has been imported, the element does exist in
1192 // sys.path.
Bram Moolenaar19e60942011-06-19 00:27:51 +02001193 PyRun_SimpleString("import vim; import sys; sys.path = list(filter(lambda x: not x.endswith('must>not&exist'), sys.path))");
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001194
Bram Moolenaarefc0d942020-10-11 18:05:02 +02001195 // Without the call to PyEval_SaveThread, thread specific state (such
1196 // as the system trace hook), will be lost between invocations of
1197 // Python code.
1198 // GIL may have been created and acquired in PyEval_InitThreads() and
1199 // thread state is created in Py_Initialize(); there
1200 // _PyGILState_NoteThreadState() also sets gilcounter to 1 (python must
1201 // have threads enabled!), so the following does both: unlock GIL and
1202 // save thread state in TLS without deleting thread state
Bram Moolenaar76d711c2013-02-13 14:17:08 +01001203 PyEval_SaveThread();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001204
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001205 py3initialised = 1;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001206 }
1207
1208 return 0;
1209
1210fail:
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001211 // We call PythonIO_Flush() here to print any Python errors.
1212 // This is OK, as it is possible to call this function even
1213 // if PythonIO_Init_io() has not completed successfully (it will
1214 // not do anything in this case).
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001215 PythonIO_Flush();
1216 return -1;
1217}
1218
1219/*
1220 * External interface
1221 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001222 static void
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02001223DoPyCommand(const char *cmd, rangeinitializer init_range, runner run, void *arg)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001224{
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001225#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001226 char *saved_locale;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001227#endif
Bram Moolenaar19e60942011-06-19 00:27:51 +02001228 PyObject *cmdstr;
1229 PyObject *cmdbytes;
Bram Moolenaar71700b82013-05-15 17:49:05 +02001230 PyGILState_STATE pygilstate;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001231
Bram Moolenaarc4f83382017-07-07 14:50:44 +02001232 if (python_end_called)
1233 goto theend;
1234
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001235 if (Python3_Init())
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001236 goto theend;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001237
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02001238 init_range(arg);
1239
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001240 Python_Release_Vim(); // leave Vim
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001241
1242#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001243 // Python only works properly when the LC_NUMERIC locale is "C".
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001244 saved_locale = setlocale(LC_NUMERIC, NULL);
1245 if (saved_locale == NULL || STRCMP(saved_locale, "C") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001246 saved_locale = NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001247 else
1248 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001249 // Need to make a copy, value may change when setting new locale.
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001250 saved_locale = (char *)vim_strsave((char_u *)saved_locale);
1251 (void)setlocale(LC_NUMERIC, "C");
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001252 }
1253#endif
1254
1255 pygilstate = PyGILState_Ensure();
1256
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001257 // PyRun_SimpleString expects a UTF-8 string. Wrong encoding may cause
1258 // SyntaxError (unicode error).
Bram Moolenaar3d64a312011-07-15 15:54:44 +02001259 cmdstr = PyUnicode_Decode(cmd, strlen(cmd),
Bram Moolenaar2e2f52a2020-12-21 16:03:02 +01001260 (char *)ENC_OPT, ERRORS_DECODE_ARG);
1261 cmdbytes = PyUnicode_AsEncodedString(cmdstr, "utf-8", ERRORS_ENCODE_ARG);
Bram Moolenaar19e60942011-06-19 00:27:51 +02001262 Py_XDECREF(cmdstr);
Bram Moolenaardb913952012-06-29 12:54:53 +02001263
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02001264 run(PyBytes_AsString(cmdbytes), arg, &pygilstate);
Bram Moolenaar19e60942011-06-19 00:27:51 +02001265 Py_XDECREF(cmdbytes);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001266
1267 PyGILState_Release(pygilstate);
1268
1269#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
1270 if (saved_locale != NULL)
1271 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001272 (void)setlocale(LC_NUMERIC, saved_locale);
1273 vim_free(saved_locale);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001274 }
1275#endif
1276
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001277 Python_Lock_Vim(); // enter Vim
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001278 PythonIO_Flush();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001279
1280theend:
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001281 return; // keeps lint happy
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001282}
1283
1284/*
Bram Moolenaar368373e2010-07-19 20:46:22 +02001285 * ":py3"
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001286 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001287 void
1288ex_py3(exarg_T *eap)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001289{
1290 char_u *script;
1291
1292 script = script_get(eap, eap->arg);
1293 if (!eap->skip)
1294 {
Bram Moolenaar14816ad2019-02-18 22:04:56 +01001295 if (p_pyx == 0)
1296 p_pyx = 3;
1297
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02001298 DoPyCommand(script == NULL ? (char *) eap->arg : (char *) script,
1299 (rangeinitializer) init_range_cmd,
1300 (runner) run_cmd,
1301 (void *) eap);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001302 }
1303 vim_free(script);
1304}
1305
1306#define BUFFER_SIZE 2048
1307
1308/*
Bram Moolenaar6df6f472010-07-18 18:04:50 +02001309 * ":py3file"
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001310 */
1311 void
1312ex_py3file(exarg_T *eap)
1313{
1314 static char buffer[BUFFER_SIZE];
1315 const char *file;
1316 char *p;
1317 int i;
1318
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01001319 if (p_pyx == 0)
1320 p_pyx = 3;
1321
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001322 // Have to do it like this. PyRun_SimpleFile requires you to pass a
1323 // stdio file pointer, but Vim and the Python DLL are compiled with
1324 // different options under Windows, meaning that stdio pointers aren't
1325 // compatible between the two. Yuk.
1326 //
1327 // construct: exec(compile(open('a_filename', 'rb').read(), 'a_filename', 'exec'))
1328 //
1329 // Using bytes so that Python can detect the source encoding as it normally
1330 // does. The doc does not say "compile" accept bytes, though.
1331 //
1332 // We need to escape any backslashes or single quotes in the file name, so that
1333 // Python won't mangle the file name.
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001334
1335 strcpy(buffer, "exec(compile(open('");
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001336 p = buffer + 19; // size of "exec(compile(open('"
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001337
1338 for (i=0; i<2; ++i)
1339 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001340 file = (char *)eap->arg;
1341 while (*file && p < buffer + (BUFFER_SIZE - 3))
1342 {
1343 if (*file == '\\' || *file == '\'')
1344 *p++ = '\\';
1345 *p++ = *file++;
1346 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001347 // If we didn't finish the file name, we hit a buffer overflow
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001348 if (*file != '\0')
1349 return;
1350 if (i==0)
1351 {
Bram Moolenaar19e60942011-06-19 00:27:51 +02001352 strcpy(p,"','rb').read(),'");
1353 p += 16;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001354 }
1355 else
1356 {
1357 strcpy(p,"','exec'))");
1358 p += 10;
1359 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001360 }
1361
1362
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001363 // Execute the file
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02001364 DoPyCommand(buffer,
1365 (rangeinitializer) init_range_cmd,
1366 (runner) run_cmd,
1367 (void *) eap);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001368}
1369
Bram Moolenaar54e8f002013-05-15 19:44:39 +02001370 void
1371ex_py3do(exarg_T *eap)
Bram Moolenaar3dab2802013-05-15 18:28:13 +02001372{
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01001373 if (p_pyx == 0)
1374 p_pyx = 3;
1375
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02001376 DoPyCommand((char *)eap->arg,
1377 (rangeinitializer)init_range_cmd,
1378 (runner)run_do,
1379 (void *)eap);
Bram Moolenaar3dab2802013-05-15 18:28:13 +02001380}
1381
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001382///////////////////////////////////////////////////////
1383// 2. Python output stream: writes output via [e]msg().
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001384
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001385// Implementation functions
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001386
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001387 static PyObject *
1388OutputGetattro(PyObject *self, PyObject *nameobj)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001389{
Bram Moolenaar77045652012-09-21 13:46:06 +02001390 GET_ATTR_STRING(name, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001391
1392 if (strcmp(name, "softspace") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001393 return PyLong_FromLong(((OutputObject *)(self))->softspace);
Bram Moolenaar6d4431e2016-04-21 20:00:56 +02001394 else if (strcmp(name, "errors") == 0)
1395 return PyString_FromString("strict");
1396 else if (strcmp(name, "encoding") == 0)
1397 return PyString_FromString(ENC_OPT);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001398
1399 return PyObject_GenericGetAttr(self, nameobj);
1400}
1401
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001402 static int
1403OutputSetattro(PyObject *self, PyObject *nameobj, PyObject *val)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001404{
Bram Moolenaar77045652012-09-21 13:46:06 +02001405 GET_ATTR_STRING(name, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001406
Bram Moolenaard6e39182013-05-21 18:30:34 +02001407 return OutputSetattr((OutputObject *)(self), name, val);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001408}
1409
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001410///////////////////////////////////////////////////////
1411// 3. Implementation of the Vim module for Python
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001412
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001413// Window type - Implementation functions
1414// --------------------------------------
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001415
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001416#define WindowType_Check(obj) ((obj)->ob_base.ob_type == &WindowType)
1417
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001418// Buffer type - Implementation functions
1419// --------------------------------------
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001420
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001421#define BufferType_Check(obj) ((obj)->ob_base.ob_type == &BufferType)
1422
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001423static PyObject* BufferSubscript(PyObject *self, PyObject *idx);
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02001424static int BufferAsSubscript(PyObject *self, PyObject *idx, PyObject *val);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001425
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001426// Line range type - Implementation functions
1427// --------------------------------------
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001428
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001429#define RangeType_Check(obj) ((obj)->ob_base.ob_type == &RangeType)
1430
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001431static PyObject* RangeSubscript(PyObject *self, PyObject *idx);
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02001432static int RangeAsItem(PyObject *, Py_ssize_t, PyObject *);
1433static int RangeAsSubscript(PyObject *self, PyObject *idx, PyObject *val);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001434
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001435// Current objects type - Implementation functions
1436// -----------------------------------------------
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001437
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001438static PySequenceMethods BufferAsSeq = {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001439 (lenfunc) BufferLength, // sq_length, len(x)
1440 (binaryfunc) 0, // sq_concat, x+y
1441 (ssizeargfunc) 0, // sq_repeat, x*n
1442 (ssizeargfunc) BufferItem, // sq_item, x[i]
1443 0, // was_sq_slice, x[i:j]
1444 0, // sq_ass_item, x[i]=v
1445 0, // sq_ass_slice, x[i:j]=v
1446 0, // sq_contains
1447 0, // sq_inplace_concat
1448 0, // sq_inplace_repeat
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001449};
1450
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001451static PyMappingMethods BufferAsMapping = {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001452 /* mp_length */ (lenfunc)BufferLength,
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001453 /* mp_subscript */ (binaryfunc)BufferSubscript,
Bram Moolenaar19e60942011-06-19 00:27:51 +02001454 /* mp_ass_subscript */ (objobjargproc)BufferAsSubscript,
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001455};
1456
1457
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001458// Buffer object
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001459
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001460 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02001461BufferGetattro(PyObject *self, PyObject *nameobj)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001462{
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001463 PyObject *r;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001464
Bram Moolenaar77045652012-09-21 13:46:06 +02001465 GET_ATTR_STRING(name, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001466
Bram Moolenaar9e822c02013-05-29 22:15:30 +02001467 if ((r = BufferAttrValid((BufferObject *)(self), name)))
1468 return r;
1469
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001470 if (CheckBuffer((BufferObject *)(self)))
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001471 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001472
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001473 r = BufferAttr((BufferObject *)(self), name);
1474 if (r || PyErr_Occurred())
1475 return r;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001476 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001477 return PyObject_GenericGetAttr(self, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001478}
1479
Bram Moolenaare9ba5162013-05-29 22:02:22 +02001480 static int
1481BufferSetattro(PyObject *self, PyObject *nameobj, PyObject *val)
1482{
1483 GET_ATTR_STRING(name, nameobj);
1484
1485 return BufferSetattr((BufferObject *)(self), name, val);
1486}
1487
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001488//////////////////
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001489
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001490 static PyObject *
1491BufferSubscript(PyObject *self, PyObject* idx)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001492{
Bram Moolenaardb913952012-06-29 12:54:53 +02001493 if (PyLong_Check(idx))
1494 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001495 long _idx = PyLong_AsLong(idx);
Bram Moolenaard6e39182013-05-21 18:30:34 +02001496 return BufferItem((BufferObject *)(self), _idx);
Bram Moolenaardb913952012-06-29 12:54:53 +02001497 } else if (PySlice_Check(idx))
1498 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001499 Py_ssize_t start, stop, step, slicelen;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001500
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02001501 if (CheckBuffer((BufferObject *) self))
1502 return NULL;
1503
Bram Moolenaar922a4662014-03-30 16:11:43 +02001504 if (PySlice_GetIndicesEx((PySliceObject_T *)idx,
Bram Moolenaarbd80f352013-05-12 21:16:23 +02001505 (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count,
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001506 &start, &stop,
Bram Moolenaardb913952012-06-29 12:54:53 +02001507 &step, &slicelen) < 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001508 return NULL;
Bram Moolenaard6e39182013-05-21 18:30:34 +02001509 return BufferSlice((BufferObject *)(self), start, stop);
Bram Moolenaardb913952012-06-29 12:54:53 +02001510 }
1511 else
1512 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001513 RAISE_INVALID_INDEX_TYPE(idx);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001514 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001515 }
1516}
1517
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02001518 static int
Bram Moolenaar19e60942011-06-19 00:27:51 +02001519BufferAsSubscript(PyObject *self, PyObject* idx, PyObject* val)
1520{
Bram Moolenaardb913952012-06-29 12:54:53 +02001521 if (PyLong_Check(idx))
1522 {
Bram Moolenaar19e60942011-06-19 00:27:51 +02001523 long n = PyLong_AsLong(idx);
Bram Moolenaarab589462020-07-06 21:03:06 +02001524
1525 if (CheckBuffer((BufferObject *) self))
1526 return -1;
1527
Bram Moolenaar19e60942011-06-19 00:27:51 +02001528 return RBAsItem((BufferObject *)(self), n, val, 1,
1529 (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count,
1530 NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02001531 } else if (PySlice_Check(idx))
1532 {
Bram Moolenaar19e60942011-06-19 00:27:51 +02001533 Py_ssize_t start, stop, step, slicelen;
1534
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02001535 if (CheckBuffer((BufferObject *) self))
1536 return -1;
1537
Bram Moolenaar922a4662014-03-30 16:11:43 +02001538 if (PySlice_GetIndicesEx((PySliceObject_T *)idx,
Bram Moolenaarbd80f352013-05-12 21:16:23 +02001539 (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count,
Bram Moolenaar19e60942011-06-19 00:27:51 +02001540 &start, &stop,
Bram Moolenaardb913952012-06-29 12:54:53 +02001541 &step, &slicelen) < 0)
Bram Moolenaar19e60942011-06-19 00:27:51 +02001542 return -1;
Bram Moolenaar19e60942011-06-19 00:27:51 +02001543 return RBAsSlice((BufferObject *)(self), start, stop, val, 1,
1544 (PyInt)((BufferObject *)(self))->buf->b_ml.ml_line_count,
1545 NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02001546 }
1547 else
1548 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001549 RAISE_INVALID_INDEX_TYPE(idx);
Bram Moolenaar19e60942011-06-19 00:27:51 +02001550 return -1;
1551 }
1552}
1553
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001554static PySequenceMethods RangeAsSeq = {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001555 (lenfunc) RangeLength, // sq_length, len(x)
1556 (binaryfunc) 0, // RangeConcat, sq_concat, x+y
1557 (ssizeargfunc) 0, // RangeRepeat, sq_repeat, x*n
1558 (ssizeargfunc) RangeItem, // sq_item, x[i]
1559 0, // was_sq_slice, x[i:j]
1560 (ssizeobjargproc) RangeAsItem, // sq_as_item, x[i]=v
1561 0, // sq_ass_slice, x[i:j]=v
1562 0, // sq_contains
1563 0, // sq_inplace_concat
1564 0, // sq_inplace_repeat
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001565};
1566
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001567static PyMappingMethods RangeAsMapping = {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001568 /* mp_length */ (lenfunc)RangeLength,
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001569 /* mp_subscript */ (binaryfunc)RangeSubscript,
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001570 /* mp_ass_subscript */ (objobjargproc)RangeAsSubscript,
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001571};
1572
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001573// Line range object - Implementation
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001574
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001575 static PyObject *
1576RangeGetattro(PyObject *self, PyObject *nameobj)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001577{
Bram Moolenaar77045652012-09-21 13:46:06 +02001578 GET_ATTR_STRING(name, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001579
1580 if (strcmp(name, "start") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001581 return Py_BuildValue("n", ((RangeObject *)(self))->start - 1);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001582 else if (strcmp(name, "end") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001583 return Py_BuildValue("n", ((RangeObject *)(self))->end - 1);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001584 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001585 return PyObject_GenericGetAttr(self, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001586}
1587
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001588////////////////
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001589
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02001590 static int
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001591RangeAsItem(PyObject *self, Py_ssize_t n, PyObject *val)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001592{
1593 return RBAsItem(((RangeObject *)(self))->buf, n, val,
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001594 ((RangeObject *)(self))->start,
1595 ((RangeObject *)(self))->end,
1596 &((RangeObject *)(self))->end);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001597}
1598
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001599 static Py_ssize_t
1600RangeAsSlice(PyObject *self, Py_ssize_t lo, Py_ssize_t hi, PyObject *val)
1601{
1602 return RBAsSlice(((RangeObject *)(self))->buf, lo, hi, val,
1603 ((RangeObject *)(self))->start,
1604 ((RangeObject *)(self))->end,
1605 &((RangeObject *)(self))->end);
1606}
1607
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001608 static PyObject *
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001609RangeSubscript(PyObject *self, PyObject* idx)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001610{
Bram Moolenaardb913952012-06-29 12:54:53 +02001611 if (PyLong_Check(idx))
1612 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001613 long _idx = PyLong_AsLong(idx);
Bram Moolenaard6e39182013-05-21 18:30:34 +02001614 return RangeItem((RangeObject *)(self), _idx);
Bram Moolenaardb913952012-06-29 12:54:53 +02001615 } else if (PySlice_Check(idx))
1616 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001617 Py_ssize_t start, stop, step, slicelen;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001618
Bram Moolenaar922a4662014-03-30 16:11:43 +02001619 if (PySlice_GetIndicesEx((PySliceObject_T *)idx,
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001620 ((RangeObject *)(self))->end-((RangeObject *)(self))->start+1,
1621 &start, &stop,
Bram Moolenaardb913952012-06-29 12:54:53 +02001622 &step, &slicelen) < 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001623 return NULL;
Bram Moolenaard6e39182013-05-21 18:30:34 +02001624 return RangeSlice((RangeObject *)(self), start, stop);
Bram Moolenaardb913952012-06-29 12:54:53 +02001625 }
1626 else
1627 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001628 RAISE_INVALID_INDEX_TYPE(idx);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001629 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001630 }
1631}
1632
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02001633 static int
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001634RangeAsSubscript(PyObject *self, PyObject *idx, PyObject *val)
1635{
Bram Moolenaardb913952012-06-29 12:54:53 +02001636 if (PyLong_Check(idx))
1637 {
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001638 long n = PyLong_AsLong(idx);
1639 return RangeAsItem(self, n, val);
Bram Moolenaarabab0b02019-03-30 18:47:01 +01001640 }
1641 else if (PySlice_Check(idx))
Bram Moolenaardb913952012-06-29 12:54:53 +02001642 {
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001643 Py_ssize_t start, stop, step, slicelen;
1644
Bram Moolenaar922a4662014-03-30 16:11:43 +02001645 if (PySlice_GetIndicesEx((PySliceObject_T *)idx,
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001646 ((RangeObject *)(self))->end-((RangeObject *)(self))->start+1,
1647 &start, &stop,
Bram Moolenaardb913952012-06-29 12:54:53 +02001648 &step, &slicelen) < 0)
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001649 return -1;
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001650 return RangeAsSlice(self, start, stop, val);
Bram Moolenaardb913952012-06-29 12:54:53 +02001651 }
1652 else
1653 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001654 RAISE_INVALID_INDEX_TYPE(idx);
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001655 return -1;
1656 }
1657}
1658
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001659// TabPage object - Implementation
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001660
1661 static PyObject *
1662TabPageGetattro(PyObject *self, PyObject *nameobj)
1663{
1664 PyObject *r;
1665
1666 GET_ATTR_STRING(name, nameobj);
1667
Bram Moolenaar9e822c02013-05-29 22:15:30 +02001668 if ((r = TabPageAttrValid((TabPageObject *)(self), name)))
1669 return r;
1670
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001671 if (CheckTabPage((TabPageObject *)(self)))
1672 return NULL;
1673
1674 r = TabPageAttr((TabPageObject *)(self), name);
1675 if (r || PyErr_Occurred())
1676 return r;
1677 else
1678 return PyObject_GenericGetAttr(self, nameobj);
1679}
1680
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001681// Window object - Implementation
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001682
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001683 static PyObject *
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001684WindowGetattro(PyObject *self, PyObject *nameobj)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001685{
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001686 PyObject *r;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001687
Bram Moolenaar77045652012-09-21 13:46:06 +02001688 GET_ATTR_STRING(name, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001689
Bram Moolenaar9e822c02013-05-29 22:15:30 +02001690 if ((r = WindowAttrValid((WindowObject *)(self), name)))
1691 return r;
1692
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001693 if (CheckWindow((WindowObject *)(self)))
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001694 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001695
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001696 r = WindowAttr((WindowObject *)(self), name);
1697 if (r || PyErr_Occurred())
1698 return r;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001699 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001700 return PyObject_GenericGetAttr(self, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001701}
1702
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001703 static int
1704WindowSetattro(PyObject *self, PyObject *nameobj, PyObject *val)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001705{
Bram Moolenaar77045652012-09-21 13:46:06 +02001706 GET_ATTR_STRING(name, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001707
Bram Moolenaard6e39182013-05-21 18:30:34 +02001708 return WindowSetattr((WindowObject *)(self), name, val);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001709}
1710
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001711// Tab page list object - Definitions
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001712
1713static PySequenceMethods TabListAsSeq = {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001714 (lenfunc) TabListLength, // sq_length, len(x)
1715 (binaryfunc) 0, // sq_concat, x+y
1716 (ssizeargfunc) 0, // sq_repeat, x*n
1717 (ssizeargfunc) TabListItem, // sq_item, x[i]
1718 0, // sq_slice, x[i:j]
1719 (ssizeobjargproc)0, // sq_as_item, x[i]=v
1720 0, // sq_ass_slice, x[i:j]=v
1721 0, // sq_contains
1722 0, // sq_inplace_concat
1723 0, // sq_inplace_repeat
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001724};
1725
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001726// Window list object - Definitions
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001727
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001728static PySequenceMethods WinListAsSeq = {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001729 (lenfunc) WinListLength, // sq_length, len(x)
1730 (binaryfunc) 0, // sq_concat, x+y
1731 (ssizeargfunc) 0, // sq_repeat, x*n
1732 (ssizeargfunc) WinListItem, // sq_item, x[i]
1733 0, // sq_slice, x[i:j]
1734 (ssizeobjargproc)0, // sq_as_item, x[i]=v
1735 0, // sq_ass_slice, x[i:j]=v
1736 0, // sq_contains
1737 0, // sq_inplace_concat
1738 0, // sq_inplace_repeat
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001739};
1740
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001741/*
1742 * Current items object - Implementation
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001743 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001744 static PyObject *
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001745CurrentGetattro(PyObject *self, PyObject *nameobj)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001746{
Bram Moolenaardd8aca62013-05-29 22:36:10 +02001747 PyObject *r;
Bram Moolenaar77045652012-09-21 13:46:06 +02001748 GET_ATTR_STRING(name, nameobj);
Bram Moolenaardd8aca62013-05-29 22:36:10 +02001749 if (!(r = CurrentGetattr(self, name)))
1750 return PyObject_GenericGetAttr(self, nameobj);
1751 return r;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001752}
1753
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001754 static int
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001755CurrentSetattro(PyObject *self, PyObject *nameobj, PyObject *value)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001756{
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001757 GET_ATTR_STRING(name, nameobj);
1758 return CurrentSetattr(self, name, value);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001759}
1760
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001761// Dictionary object - Definitions
Bram Moolenaardb913952012-06-29 12:54:53 +02001762
Bram Moolenaar66b79852012-09-21 14:00:35 +02001763 static PyObject *
1764DictionaryGetattro(PyObject *self, PyObject *nameobj)
1765{
1766 DictionaryObject *this = ((DictionaryObject *) (self));
1767
1768 GET_ATTR_STRING(name, nameobj);
1769
1770 if (strcmp(name, "locked") == 0)
1771 return PyLong_FromLong(this->dict->dv_lock);
1772 else if (strcmp(name, "scope") == 0)
1773 return PyLong_FromLong(this->dict->dv_scope);
1774
1775 return PyObject_GenericGetAttr(self, nameobj);
1776}
1777
1778 static int
1779DictionarySetattro(PyObject *self, PyObject *nameobj, PyObject *val)
1780{
1781 GET_ATTR_STRING(name, nameobj);
Bram Moolenaard6e39182013-05-21 18:30:34 +02001782 return DictionarySetattr((DictionaryObject *)(self), name, val);
Bram Moolenaardb913952012-06-29 12:54:53 +02001783}
1784
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001785// List object - Definitions
Bram Moolenaardb913952012-06-29 12:54:53 +02001786
Bram Moolenaar66b79852012-09-21 14:00:35 +02001787 static PyObject *
1788ListGetattro(PyObject *self, PyObject *nameobj)
1789{
1790 GET_ATTR_STRING(name, nameobj);
1791
1792 if (strcmp(name, "locked") == 0)
1793 return PyLong_FromLong(((ListObject *) (self))->list->lv_lock);
1794
1795 return PyObject_GenericGetAttr(self, nameobj);
1796}
1797
1798 static int
1799ListSetattro(PyObject *self, PyObject *nameobj, PyObject *val)
1800{
1801 GET_ATTR_STRING(name, nameobj);
Bram Moolenaard6e39182013-05-21 18:30:34 +02001802 return ListSetattr((ListObject *)(self), name, val);
Bram Moolenaardb913952012-06-29 12:54:53 +02001803}
1804
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001805// Function object - Definitions
Bram Moolenaardb913952012-06-29 12:54:53 +02001806
Bram Moolenaardb913952012-06-29 12:54:53 +02001807 static PyObject *
1808FunctionGetattro(PyObject *self, PyObject *nameobj)
1809{
Bram Moolenaar8110a092016-04-14 15:56:09 +02001810 PyObject *r;
Bram Moolenaardb913952012-06-29 12:54:53 +02001811 FunctionObject *this = (FunctionObject *)(self);
Bram Moolenaar77045652012-09-21 13:46:06 +02001812
1813 GET_ATTR_STRING(name, nameobj);
Bram Moolenaardb913952012-06-29 12:54:53 +02001814
Bram Moolenaar8110a092016-04-14 15:56:09 +02001815 r = FunctionAttr(this, name);
1816 if (r || PyErr_Occurred())
1817 return r;
1818 else
1819 return PyObject_GenericGetAttr(self, nameobj);
Bram Moolenaardb913952012-06-29 12:54:53 +02001820}
1821
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001822// External interface
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001823
1824 void
1825python3_buffer_free(buf_T *buf)
1826{
Bram Moolenaar971db462013-05-12 18:44:48 +02001827 if (BUF_PYTHON_REF(buf) != NULL)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001828 {
Bram Moolenaar971db462013-05-12 18:44:48 +02001829 BufferObject *bp = BUF_PYTHON_REF(buf);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001830 bp->buf = INVALID_BUFFER_VALUE;
Bram Moolenaar971db462013-05-12 18:44:48 +02001831 BUF_PYTHON_REF(buf) = NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001832 }
1833}
1834
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001835 void
1836python3_window_free(win_T *win)
1837{
Bram Moolenaar971db462013-05-12 18:44:48 +02001838 if (WIN_PYTHON_REF(win) != NULL)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001839 {
Bram Moolenaar971db462013-05-12 18:44:48 +02001840 WindowObject *wp = WIN_PYTHON_REF(win);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001841 wp->win = INVALID_WINDOW_VALUE;
Bram Moolenaar971db462013-05-12 18:44:48 +02001842 WIN_PYTHON_REF(win) = NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001843 }
1844}
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001845
1846 void
1847python3_tabpage_free(tabpage_T *tab)
1848{
1849 if (TAB_PYTHON_REF(tab) != NULL)
1850 {
1851 TabPageObject *tp = TAB_PYTHON_REF(tab);
1852 tp->tab = INVALID_TABPAGE_VALUE;
1853 TAB_PYTHON_REF(tab) = NULL;
1854 }
1855}
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001856
Bram Moolenaar7854e3a2012-11-28 15:33:14 +01001857 static PyObject *
1858Py3Init_vim(void)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001859{
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001860 // The special value is removed from sys.path in Python3_Init().
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001861 static wchar_t *(argv[2]) = {L"/must>not&exist/foo", NULL};
1862
Bram Moolenaar1dc28782013-05-21 19:11:01 +02001863 if (init_types())
1864 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001865
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001866 // Set sys.argv[] to avoid a crash in warn().
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001867 PySys_SetArgv(1, argv);
1868
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001869 if ((vim_module = PyModule_Create(&vimmodule)) == NULL)
Bram Moolenaar19e60942011-06-19 00:27:51 +02001870 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001871
Bram Moolenaardee2e312013-06-23 16:35:47 +02001872 if (populate_module(vim_module))
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001873 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001874
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001875 if (init_sys_path())
1876 return NULL;
1877
1878 return vim_module;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001879}
1880
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001881//////////////////////////////////////////////////////////////////////////
1882// 4. Utility functions for handling the interface between Vim and Python.
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001883
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001884/*
1885 * Convert a Vim line into a Python string.
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001886 * All internal newlines are replaced by null characters.
1887 *
1888 * On errors, the Python exception data is set, and NULL is returned.
1889 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001890 static PyObject *
1891LineToString(const char *str)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001892{
1893 PyObject *result;
1894 Py_ssize_t len = strlen(str);
Bram Moolenaard672dde2020-02-26 13:43:51 +01001895 char *tmp, *p;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001896
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001897 tmp = alloc(len + 1);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001898 p = tmp;
1899 if (p == NULL)
1900 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001901 PyErr_NoMemory();
1902 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001903 }
1904
1905 while (*str)
1906 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001907 if (*str == '\n')
1908 *p = '\0';
1909 else
1910 *p = *str;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001911
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001912 ++p;
1913 ++str;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001914 }
1915 *p = '\0';
1916
Bram Moolenaar2e2f52a2020-12-21 16:03:02 +01001917 result = PyUnicode_Decode(tmp, len, (char *)ENC_OPT, ERRORS_DECODE_ARG);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001918
1919 vim_free(tmp);
1920 return result;
1921}
1922
Bram Moolenaardb913952012-06-29 12:54:53 +02001923 void
Bram Moolenaar8e9a24a2019-03-19 22:22:55 +01001924do_py3eval(char_u *str, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +02001925{
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02001926 DoPyCommand((char *) str,
1927 (rangeinitializer) init_range_eval,
1928 (runner) run_eval,
1929 (void *) rettv);
Bram Moolenaar8e9a24a2019-03-19 22:22:55 +01001930 if (rettv->v_type == VAR_UNKNOWN)
Bram Moolenaardb913952012-06-29 12:54:53 +02001931 {
Bram Moolenaar8e9a24a2019-03-19 22:22:55 +01001932 rettv->v_type = VAR_NUMBER;
1933 rettv->vval.v_number = 0;
Bram Moolenaardb913952012-06-29 12:54:53 +02001934 }
1935}
1936
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01001937 int
Bram Moolenaar8e9a24a2019-03-19 22:22:55 +01001938set_ref_in_python3(int copyID)
Bram Moolenaardb913952012-06-29 12:54:53 +02001939{
Bram Moolenaarb641df42015-02-03 13:16:04 +01001940 return set_ref_in_py(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02001941}