blob: 270e1a9e25f9bc99726866a45de9f6c4f641a66b [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
ichizok6c3d3e62022-11-04 22:38:11 +000048#ifdef HAVE_DUP
49# undef HAVE_DUP
50#endif
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010051#ifdef HAVE_STRFTIME
52# undef HAVE_STRFTIME
53#endif
54#ifdef HAVE_STRING_H
55# undef HAVE_STRING_H
56#endif
57#ifdef HAVE_PUTENV
58# undef HAVE_PUTENV
59#endif
Bram Moolenaar6df6f472010-07-18 18:04:50 +020060#ifdef HAVE_STDARG_H
Bram Moolenaar2ab2e862019-12-04 21:24:53 +010061# undef HAVE_STDARG_H // Python's config.h defines it as well.
Bram Moolenaar6df6f472010-07-18 18:04:50 +020062#endif
Bram Moolenaar2ab2e862019-12-04 21:24:53 +010063#ifdef _POSIX_C_SOURCE // defined in feature.h
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020064# undef _POSIX_C_SOURCE
65#endif
Bram Moolenaar6df6f472010-07-18 18:04:50 +020066#ifdef _XOPEN_SOURCE
Bram Moolenaar2ab2e862019-12-04 21:24:53 +010067# undef _XOPEN_SOURCE // pyconfig.h defines it as well.
Bram Moolenaar6df6f472010-07-18 18:04:50 +020068#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020069
Bram Moolenaar0bdda372013-06-10 18:36:24 +020070#define PY_SSIZE_T_CLEAN
71
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020072#include <Python.h>
Bram Moolenaar0bdda372013-06-10 18:36:24 +020073
Bram Moolenaar2ab2e862019-12-04 21:24:53 +010074#undef main // Defined in python.h - aargh
75#undef HAVE_FCNTL_H // Clash with os_win32.h
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020076
Bram Moolenaar2ab2e862019-12-04 21:24:53 +010077// The "surrogateescape" error handler is new in Python 3.1
Bram Moolenaar3d64a312011-07-15 15:54:44 +020078#if PY_VERSION_HEX >= 0x030100f0
79# define CODEC_ERROR_HANDLER "surrogateescape"
80#else
81# define CODEC_ERROR_HANDLER NULL
82#endif
83
Philip H5a5f17f2022-11-05 14:05:31 +000084// Suppress Python 3.11 depreciations to see useful warnings
85#if defined(__clang__) && defined(__clang_major__) && __clang_major__ > 11
K.Takata161b6ac2022-11-14 15:31:07 +000086# pragma clang diagnostic ignored "-Wdeprecated-declarations"
Philip H5a5f17f2022-11-05 14:05:31 +000087#endif
88
Bram Moolenaar2ab2e862019-12-04 21:24:53 +010089// Python 3 does not support CObjects, always use Capsules
Bram Moolenaar2afa3232012-06-29 16:28:28 +020090#define PY_USE_CAPSULE
91
Bram Moolenaar2e2f52a2020-12-21 16:03:02 +010092#define ERRORS_DECODE_ARG CODEC_ERROR_HANDLER
93#define ERRORS_ENCODE_ARG ERRORS_DECODE_ARG
94
Bram Moolenaar170bf1a2010-07-24 23:51:45 +020095#define PyInt Py_ssize_t
Bram Moolenaar32ac8cd2013-07-03 18:49:17 +020096#ifndef PyString_Check
97# define PyString_Check(obj) PyUnicode_Check(obj)
98#endif
Bram Moolenaare0324612013-07-09 17:30:55 +020099#define PyString_FromString(repr) \
Bram Moolenaar2e2f52a2020-12-21 16:03:02 +0100100 PyUnicode_Decode(repr, STRLEN(repr), ENC_OPT, ERRORS_DECODE_ARG)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +0200101#define PyString_FromFormat PyUnicode_FromFormat
Bram Moolenaar32ac8cd2013-07-03 18:49:17 +0200102#ifndef PyInt_Check
103# define PyInt_Check(obj) PyLong_Check(obj)
104#endif
Bram Moolenaar77045652012-09-21 13:46:06 +0200105#define PyInt_FromLong(i) PyLong_FromLong(i)
106#define PyInt_AsLong(obj) PyLong_AsLong(obj)
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200107#define Py_ssize_t_fmt "n"
Bram Moolenaara9922d62013-05-30 13:01:18 +0200108#define Py_bytes_fmt "y"
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200109
Bram Moolenaar063a46b2014-01-14 16:36:51 +0100110#define PyIntArgFunc ssizeargfunc
111#define PyIntObjArgProc ssizeobjargproc
112
Bram Moolenaar922a4662014-03-30 16:11:43 +0200113/*
114 * PySlice_GetIndicesEx(): first argument type changed from PySliceObject
115 * to PyObject in Python 3.2 or later.
116 */
117#if PY_VERSION_HEX >= 0x030200f0
118typedef PyObject PySliceObject_T;
119#else
120typedef PySliceObject PySliceObject_T;
121#endif
122
Bram Moolenaar63ff72a2022-02-07 13:54:01 +0000123#ifndef MSWIN
124# define HINSTANCE void *
125#endif
126#if defined(DYNAMIC_PYTHON3) || defined(MSWIN)
127static HINSTANCE hinstPy3 = 0; // Instance of python.dll
128#endif
129
Bram Moolenaar0c1f3f42011-02-25 15:18:50 +0100130#if defined(DYNAMIC_PYTHON3) || defined(PROTO)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200131
Bram Moolenaar4f974752019-02-17 17:44:42 +0100132# ifndef MSWIN
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200133# include <dlfcn.h>
134# define FARPROC void*
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100135# if defined(PY_NO_RTLD_GLOBAL) && defined(PY3_NO_RTLD_GLOBAL)
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200136# define load_dll(n) dlopen((n), RTLD_LAZY)
137# else
138# define load_dll(n) dlopen((n), RTLD_LAZY|RTLD_GLOBAL)
139# endif
140# define close_dll dlclose
141# define symbol_from_dll dlsym
Martin Tournoij1a3e5742021-07-24 13:57:29 +0200142# define load_dll_error dlerror
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200143# else
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200144# define load_dll vimLoadLib
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200145# define close_dll FreeLibrary
146# define symbol_from_dll GetProcAddress
Martin Tournoij1a3e5742021-07-24 13:57:29 +0200147# define load_dll_error GetWin32Error
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200148# endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200149/*
150 * Wrapper defines
151 */
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200152# undef PyArg_Parse
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200153# define PyArg_Parse py3_PyArg_Parse
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200154# undef PyArg_ParseTuple
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200155# define PyArg_ParseTuple py3_PyArg_ParseTuple
Bram Moolenaar19e60942011-06-19 00:27:51 +0200156# define PyMem_Free py3_PyMem_Free
Bram Moolenaardb913952012-06-29 12:54:53 +0200157# define PyMem_Malloc py3_PyMem_Malloc
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200158# define PyDict_SetItemString py3_PyDict_SetItemString
159# define PyErr_BadArgument py3_PyErr_BadArgument
160# define PyErr_Clear py3_PyErr_Clear
Bram Moolenaarc476e522013-06-23 13:46:40 +0200161# define PyErr_Format py3_PyErr_Format
Bram Moolenaar4d369872013-02-20 16:09:43 +0100162# define PyErr_PrintEx py3_PyErr_PrintEx
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200163# define PyErr_NoMemory py3_PyErr_NoMemory
164# define PyErr_Occurred py3_PyErr_Occurred
165# define PyErr_SetNone py3_PyErr_SetNone
166# define PyErr_SetString py3_PyErr_SetString
Bram Moolenaar4d188da2013-05-15 15:35:09 +0200167# define PyErr_SetObject py3_PyErr_SetObject
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200168# define PyErr_ExceptionMatches py3_PyErr_ExceptionMatches
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200169# define PyEval_InitThreads py3_PyEval_InitThreads
170# define PyEval_RestoreThread py3_PyEval_RestoreThread
171# define PyEval_SaveThread py3_PyEval_SaveThread
172# define PyGILState_Ensure py3_PyGILState_Ensure
173# define PyGILState_Release py3_PyGILState_Release
174# define PyLong_AsLong py3_PyLong_AsLong
175# define PyLong_FromLong py3_PyLong_FromLong
176# define PyList_GetItem py3_PyList_GetItem
177# define PyList_Append py3_PyList_Append
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200178# define PyList_Insert py3_PyList_Insert
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200179# define PyList_New py3_PyList_New
180# define PyList_SetItem py3_PyList_SetItem
181# define PyList_Size py3_PyList_Size
Bram Moolenaardb913952012-06-29 12:54:53 +0200182# define PySequence_Check py3_PySequence_Check
183# define PySequence_Size py3_PySequence_Size
184# define PySequence_GetItem py3_PySequence_GetItem
Bram Moolenaara9922d62013-05-30 13:01:18 +0200185# define PySequence_Fast py3_PySequence_Fast
Bram Moolenaardb913952012-06-29 12:54:53 +0200186# define PyTuple_Size py3_PyTuple_Size
187# define PyTuple_GetItem py3_PyTuple_GetItem
Bram Moolenaar3b48b112018-07-04 22:03:25 +0200188# if PY_VERSION_HEX >= 0x030601f0
189# define PySlice_AdjustIndices py3_PySlice_AdjustIndices
190# define PySlice_Unpack py3_PySlice_Unpack
191# endif
192# undef PySlice_GetIndicesEx
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200193# define PySlice_GetIndicesEx py3_PySlice_GetIndicesEx
194# define PyImport_ImportModule py3_PyImport_ImportModule
195# define PyObject_Init py3__PyObject_Init
196# define PyDict_New py3_PyDict_New
197# define PyDict_GetItemString py3_PyDict_GetItemString
Bram Moolenaardb913952012-06-29 12:54:53 +0200198# define PyDict_Next py3_PyDict_Next
199# define PyMapping_Check py3_PyMapping_Check
Bram Moolenaar32ac8cd2013-07-03 18:49:17 +0200200# ifndef PyMapping_Keys
201# define PyMapping_Keys py3_PyMapping_Keys
202# endif
Zdenek Dohnal90478f32021-06-14 15:08:30 +0200203# if PY_VERSION_HEX >= 0x030a00b2
204# define PyIter_Check py3_PyIter_Check
205# endif
Bram Moolenaardb913952012-06-29 12:54:53 +0200206# define PyIter_Next py3_PyIter_Next
207# define PyObject_GetIter py3_PyObject_GetIter
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200208# define PyObject_Repr py3_PyObject_Repr
Bram Moolenaarbcb40972013-05-30 13:22:13 +0200209# define PyObject_GetItem py3_PyObject_GetItem
Bram Moolenaar03db85b2013-05-15 14:51:35 +0200210# define PyObject_IsTrue py3_PyObject_IsTrue
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200211# define PyModule_GetDict py3_PyModule_GetDict
212#undef PyRun_SimpleString
213# define PyRun_SimpleString py3_PyRun_SimpleString
Bram Moolenaardb913952012-06-29 12:54:53 +0200214#undef PyRun_String
215# define PyRun_String py3_PyRun_String
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200216# define PyObject_GetAttrString py3_PyObject_GetAttrString
Bram Moolenaara9922d62013-05-30 13:01:18 +0200217# define PyObject_HasAttrString py3_PyObject_HasAttrString
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200218# define PyObject_SetAttrString py3_PyObject_SetAttrString
219# define PyObject_CallFunctionObjArgs py3_PyObject_CallFunctionObjArgs
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200220# define _PyObject_CallFunction_SizeT py3__PyObject_CallFunction_SizeT
Bram Moolenaarf4258302013-06-02 18:20:17 +0200221# define PyObject_Call py3_PyObject_Call
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200222# define PyEval_GetLocals py3_PyEval_GetLocals
223# define PyEval_GetGlobals py3_PyEval_GetGlobals
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200224# define PySys_SetObject py3_PySys_SetObject
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200225# define PySys_GetObject py3_PySys_GetObject
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200226# define PySys_SetArgv py3_PySys_SetArgv
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200227# define PyType_Ready py3_PyType_Ready
Bram Moolenaaree1b9312020-07-16 22:15:53 +0200228# if PY_VERSION_HEX >= 0x030900b0
229# define PyType_GetFlags py3_PyType_GetFlags
230# endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200231#undef Py_BuildValue
232# define Py_BuildValue py3_Py_BuildValue
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100233# define Py_SetPythonHome py3_Py_SetPythonHome
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200234# define Py_Initialize py3_Py_Initialize
235# define Py_Finalize py3_Py_Finalize
236# define Py_IsInitialized py3_Py_IsInitialized
237# define _Py_NoneStruct (*py3__Py_NoneStruct)
Bram Moolenaar66b79852012-09-21 14:00:35 +0200238# define _Py_FalseStruct (*py3__Py_FalseStruct)
239# define _Py_TrueStruct (*py3__Py_TrueStruct)
Bram Moolenaardb913952012-06-29 12:54:53 +0200240# define _PyObject_NextNotImplemented (*py3__PyObject_NextNotImplemented)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200241# define PyModule_AddObject py3_PyModule_AddObject
242# define PyImport_AppendInittab py3_PyImport_AppendInittab
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200243# define PyImport_AddModule py3_PyImport_AddModule
Bram Moolenaar7bc4f932012-10-14 03:22:56 +0200244# if PY_VERSION_HEX >= 0x030300f0
245# undef _PyUnicode_AsString
Bram Moolenaar9c9cbf12012-10-23 05:17:37 +0200246# define _PyUnicode_AsString py3_PyUnicode_AsUTF8
Bram Moolenaar7bc4f932012-10-14 03:22:56 +0200247# else
248# define _PyUnicode_AsString py3__PyUnicode_AsString
249# endif
Bram Moolenaar19e60942011-06-19 00:27:51 +0200250# undef PyUnicode_AsEncodedString
251# define PyUnicode_AsEncodedString py3_PyUnicode_AsEncodedString
252# undef PyBytes_AsString
253# define PyBytes_AsString py3_PyBytes_AsString
Bram Moolenaar32ac8cd2013-07-03 18:49:17 +0200254# ifndef PyBytes_AsStringAndSize
255# define PyBytes_AsStringAndSize py3_PyBytes_AsStringAndSize
256# endif
Bram Moolenaardb913952012-06-29 12:54:53 +0200257# undef PyBytes_FromString
258# define PyBytes_FromString py3_PyBytes_FromString
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100259# undef PyBytes_FromStringAndSize
260# define PyBytes_FromStringAndSize py3_PyBytes_FromStringAndSize
Bram Moolenaaree1b9312020-07-16 22:15:53 +0200261# if defined(Py_DEBUG) || PY_VERSION_HEX >= 0x030900b0
262# define _Py_Dealloc py3__Py_Dealloc
263# endif
Bram Moolenaardb913952012-06-29 12:54:53 +0200264# define PyFloat_FromDouble py3_PyFloat_FromDouble
265# define PyFloat_AsDouble py3_PyFloat_AsDouble
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200266# define PyObject_GenericGetAttr py3_PyObject_GenericGetAttr
Bram Moolenaar66b79852012-09-21 14:00:35 +0200267# define PyType_Type (*py3_PyType_Type)
Bram Moolenaard4a8c982018-05-15 22:31:18 +0200268# define PyStdPrinter_Type (*py3_PyStdPrinter_Type)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200269# define PySlice_Type (*py3_PySlice_Type)
Bram Moolenaardb913952012-06-29 12:54:53 +0200270# define PyFloat_Type (*py3_PyFloat_Type)
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200271# define PyNumber_Check (*py3_PyNumber_Check)
272# define PyNumber_Long (*py3_PyNumber_Long)
Bram Moolenaar66b79852012-09-21 14:00:35 +0200273# define PyBool_Type (*py3_PyBool_Type)
Bram Moolenaar19e60942011-06-19 00:27:51 +0200274# define PyErr_NewException py3_PyErr_NewException
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200275# ifdef Py_DEBUG
276# define _Py_NegativeRefcount py3__Py_NegativeRefcount
277# define _Py_RefTotal (*py3__Py_RefTotal)
Bram Moolenaar0014a532013-05-29 21:33:39 +0200278# define PyModule_Create2TraceRefs py3_PyModule_Create2TraceRefs
279# else
280# define PyModule_Create2 py3_PyModule_Create2
281# endif
282# if defined(Py_DEBUG) && !defined(Py_DEBUG_NO_PYMALLOC)
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200283# define _PyObject_DebugMalloc py3__PyObject_DebugMalloc
284# define _PyObject_DebugFree py3__PyObject_DebugFree
285# else
286# define PyObject_Malloc py3_PyObject_Malloc
287# define PyObject_Free py3_PyObject_Free
288# endif
Bram Moolenaar774267b2013-05-21 20:51:59 +0200289# define _PyObject_GC_New py3__PyObject_GC_New
290# define PyObject_GC_Del py3_PyObject_GC_Del
291# define PyObject_GC_UnTrack py3_PyObject_GC_UnTrack
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200292# define PyType_GenericAlloc py3_PyType_GenericAlloc
293# define PyType_GenericNew py3_PyType_GenericNew
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200294# undef PyUnicode_FromString
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200295# define PyUnicode_FromString py3_PyUnicode_FromString
Bram Moolenaar1a3b5692013-05-30 12:40:39 +0200296# ifndef PyUnicode_FromFormat
297# define PyUnicode_FromFormat py3_PyUnicode_FromFormat
298# else
299# define Py_UNICODE_USE_UCS_FUNCTIONS
300# ifdef Py_UNICODE_WIDE
301# define PyUnicodeUCS4_FromFormat py3_PyUnicodeUCS4_FromFormat
302# else
303# define PyUnicodeUCS2_FromFormat py3_PyUnicodeUCS2_FromFormat
304# endif
305# endif
Bram Moolenaar19e60942011-06-19 00:27:51 +0200306# undef PyUnicode_Decode
307# define PyUnicode_Decode py3_PyUnicode_Decode
Bram Moolenaardb913952012-06-29 12:54:53 +0200308# define PyType_IsSubtype py3_PyType_IsSubtype
309# define PyCapsule_New py3_PyCapsule_New
310# define PyCapsule_GetPointer py3_PyCapsule_GetPointer
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200311
Bram Moolenaar0014a532013-05-29 21:33:39 +0200312# if defined(Py_DEBUG) && !defined(Py_DEBUG_NO_PYMALLOC)
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200313# undef PyObject_NEW
314# define PyObject_NEW(type, typeobj) \
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200315( (type *) PyObject_Init( \
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200316 (PyObject *) _PyObject_DebugMalloc( _PyObject_SIZE(typeobj) ), (typeobj)) )
Bram Moolenaaree1b9312020-07-16 22:15:53 +0200317# elif PY_VERSION_HEX >= 0x030900b0
318# undef PyObject_NEW
319# define PyObject_NEW(type, typeobj) \
320 ((type *)py3__PyObject_New(typeobj))
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200321# endif
322
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200323/*
324 * Pointers for dynamic link
325 */
326static int (*py3_PySys_SetArgv)(int, wchar_t **);
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100327static void (*py3_Py_SetPythonHome)(wchar_t *home);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200328static void (*py3_Py_Initialize)(void);
329static PyObject* (*py3_PyList_New)(Py_ssize_t size);
330static PyGILState_STATE (*py3_PyGILState_Ensure)(void);
331static void (*py3_PyGILState_Release)(PyGILState_STATE);
332static int (*py3_PySys_SetObject)(char *, PyObject *);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200333static PyObject* (*py3_PySys_GetObject)(char *);
334static int (*py3_PyList_Append)(PyObject *, PyObject *);
335static int (*py3_PyList_Insert)(PyObject *, int, PyObject *);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200336static Py_ssize_t (*py3_PyList_Size)(PyObject *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200337static int (*py3_PySequence_Check)(PyObject *);
338static Py_ssize_t (*py3_PySequence_Size)(PyObject *);
339static PyObject* (*py3_PySequence_GetItem)(PyObject *, Py_ssize_t);
Bram Moolenaara9922d62013-05-30 13:01:18 +0200340static PyObject* (*py3_PySequence_Fast)(PyObject *, const char *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200341static Py_ssize_t (*py3_PyTuple_Size)(PyObject *);
342static PyObject* (*py3_PyTuple_GetItem)(PyObject *, Py_ssize_t);
343static int (*py3_PyMapping_Check)(PyObject *);
Bram Moolenaarbcb40972013-05-30 13:22:13 +0200344static PyObject* (*py3_PyMapping_Keys)(PyObject *);
Bram Moolenaar3b48b112018-07-04 22:03:25 +0200345# if PY_VERSION_HEX >= 0x030601f0
346static int (*py3_PySlice_AdjustIndices)(Py_ssize_t length,
347 Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t step);
348static int (*py3_PySlice_Unpack)(PyObject *slice,
349 Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t *step);
350# endif
Bram Moolenaar922a4662014-03-30 16:11:43 +0200351static int (*py3_PySlice_GetIndicesEx)(PySliceObject_T *r, Py_ssize_t length,
Bram Moolenaar063a46b2014-01-14 16:36:51 +0100352 Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t *step,
353 Py_ssize_t *slicelen);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200354static PyObject* (*py3_PyErr_NoMemory)(void);
355static void (*py3_Py_Finalize)(void);
356static void (*py3_PyErr_SetString)(PyObject *, const char *);
Bram Moolenaar4d188da2013-05-15 15:35:09 +0200357static void (*py3_PyErr_SetObject)(PyObject *, PyObject *);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200358static int (*py3_PyErr_ExceptionMatches)(PyObject *);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200359static int (*py3_PyRun_SimpleString)(char *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200360static PyObject* (*py3_PyRun_String)(char *, int, PyObject *, PyObject *);
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200361static PyObject* (*py3_PyObject_GetAttrString)(PyObject *, const char *);
Bram Moolenaara9922d62013-05-30 13:01:18 +0200362static int (*py3_PyObject_HasAttrString)(PyObject *, const char *);
Bram Moolenaar0b400082013-11-03 00:28:25 +0100363static int (*py3_PyObject_SetAttrString)(PyObject *, const char *, PyObject *);
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200364static PyObject* (*py3_PyObject_CallFunctionObjArgs)(PyObject *, ...);
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200365static PyObject* (*py3__PyObject_CallFunction_SizeT)(PyObject *, char *, ...);
Bram Moolenaarf4258302013-06-02 18:20:17 +0200366static PyObject* (*py3_PyObject_Call)(PyObject *, PyObject *, PyObject *);
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200367static PyObject* (*py3_PyEval_GetGlobals)();
368static PyObject* (*py3_PyEval_GetLocals)();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200369static PyObject* (*py3_PyList_GetItem)(PyObject *, Py_ssize_t);
370static PyObject* (*py3_PyImport_ImportModule)(const char *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200371static PyObject* (*py3_PyImport_AddModule)(const char *);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200372static int (*py3_PyErr_BadArgument)(void);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200373static PyObject* (*py3_PyErr_Occurred)(void);
374static PyObject* (*py3_PyModule_GetDict)(PyObject *);
375static int (*py3_PyList_SetItem)(PyObject *, Py_ssize_t, PyObject *);
376static PyObject* (*py3_PyDict_GetItemString)(PyObject *, const char *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200377static int (*py3_PyDict_Next)(PyObject *, Py_ssize_t *, PyObject **, PyObject **);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200378static PyObject* (*py3_PyLong_FromLong)(long);
379static PyObject* (*py3_PyDict_New)(void);
Zdenek Dohnal90478f32021-06-14 15:08:30 +0200380# if PY_VERSION_HEX >= 0x030a00b2
381static int (*py3_PyIter_Check)(PyObject *o);
382# endif
Bram Moolenaardb913952012-06-29 12:54:53 +0200383static PyObject* (*py3_PyIter_Next)(PyObject *);
384static PyObject* (*py3_PyObject_GetIter)(PyObject *);
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200385static PyObject* (*py3_PyObject_Repr)(PyObject *);
Bram Moolenaarbcb40972013-05-30 13:22:13 +0200386static PyObject* (*py3_PyObject_GetItem)(PyObject *, PyObject *);
Bram Moolenaar03db85b2013-05-15 14:51:35 +0200387static int (*py3_PyObject_IsTrue)(PyObject *);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200388static PyObject* (*py3_Py_BuildValue)(char *, ...);
Bram Moolenaaree1b9312020-07-16 22:15:53 +0200389# if PY_VERSION_HEX >= 0x030900b0
390static int (*py3_PyType_GetFlags)(PyTypeObject *o);
391# endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200392static int (*py3_PyType_Ready)(PyTypeObject *type);
393static int (*py3_PyDict_SetItemString)(PyObject *dp, char *key, PyObject *item);
394static PyObject* (*py3_PyUnicode_FromString)(const char *u);
Bram Moolenaar1a3b5692013-05-30 12:40:39 +0200395# ifndef Py_UNICODE_USE_UCS_FUNCTIONS
396static PyObject* (*py3_PyUnicode_FromFormat)(const char *u, ...);
397# else
398# ifdef Py_UNICODE_WIDE
399static PyObject* (*py3_PyUnicodeUCS4_FromFormat)(const char *u, ...);
400# else
401static PyObject* (*py3_PyUnicodeUCS2_FromFormat)(const char *u, ...);
402# endif
403# endif
Bram Moolenaar19e60942011-06-19 00:27:51 +0200404static PyObject* (*py3_PyUnicode_Decode)(const char *u, Py_ssize_t size,
405 const char *encoding, const char *errors);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200406static long (*py3_PyLong_AsLong)(PyObject *);
407static void (*py3_PyErr_SetNone)(PyObject *);
408static void (*py3_PyEval_InitThreads)(void);
409static void(*py3_PyEval_RestoreThread)(PyThreadState *);
410static PyThreadState*(*py3_PyEval_SaveThread)(void);
411static int (*py3_PyArg_Parse)(PyObject *, char *, ...);
412static int (*py3_PyArg_ParseTuple)(PyObject *, char *, ...);
Bram Moolenaar19e60942011-06-19 00:27:51 +0200413static int (*py3_PyMem_Free)(void *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200414static void* (*py3_PyMem_Malloc)(size_t);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200415static int (*py3_Py_IsInitialized)(void);
416static void (*py3_PyErr_Clear)(void);
Bram Moolenaarc476e522013-06-23 13:46:40 +0200417static PyObject* (*py3_PyErr_Format)(PyObject *, const char *, ...);
Bram Moolenaar4d369872013-02-20 16:09:43 +0100418static void (*py3_PyErr_PrintEx)(int);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200419static PyObject*(*py3__PyObject_Init)(PyObject *, PyTypeObject *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200420static iternextfunc py3__PyObject_NextNotImplemented;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200421static PyObject* py3__Py_NoneStruct;
Bram Moolenaar66b79852012-09-21 14:00:35 +0200422static PyObject* py3__Py_FalseStruct;
423static PyObject* py3__Py_TrueStruct;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200424static int (*py3_PyModule_AddObject)(PyObject *m, const char *name, PyObject *o);
425static int (*py3_PyImport_AppendInittab)(const char *name, PyObject* (*initfunc)(void));
Bram Moolenaar9c9cbf12012-10-23 05:17:37 +0200426# if PY_VERSION_HEX >= 0x030300f0
427static char* (*py3_PyUnicode_AsUTF8)(PyObject *unicode);
428# else
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200429static char* (*py3__PyUnicode_AsString)(PyObject *unicode);
Bram Moolenaar9c9cbf12012-10-23 05:17:37 +0200430# endif
Bram Moolenaar19e60942011-06-19 00:27:51 +0200431static PyObject* (*py3_PyUnicode_AsEncodedString)(PyObject *unicode, const char* encoding, const char* errors);
432static char* (*py3_PyBytes_AsString)(PyObject *bytes);
Bram Moolenaar808c2bc2013-06-23 13:11:18 +0200433static int (*py3_PyBytes_AsStringAndSize)(PyObject *bytes, char **buffer, Py_ssize_t *length);
Bram Moolenaardb913952012-06-29 12:54:53 +0200434static PyObject* (*py3_PyBytes_FromString)(char *str);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100435static PyObject* (*py3_PyBytes_FromStringAndSize)(char *str, Py_ssize_t length);
Bram Moolenaaree1b9312020-07-16 22:15:53 +0200436# if defined(Py_DEBUG) || PY_VERSION_HEX >= 0x030900b0
437static void (*py3__Py_Dealloc)(PyObject *obj);
438# endif
439# if PY_VERSION_HEX >= 0x030900b0
440static PyObject* (*py3__PyObject_New)(PyTypeObject *);
441# endif
Bram Moolenaardb913952012-06-29 12:54:53 +0200442static PyObject* (*py3_PyFloat_FromDouble)(double num);
443static double (*py3_PyFloat_AsDouble)(PyObject *);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200444static PyObject* (*py3_PyObject_GenericGetAttr)(PyObject *obj, PyObject *name);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200445static PyObject* (*py3_PyType_GenericAlloc)(PyTypeObject *type, Py_ssize_t nitems);
446static PyObject* (*py3_PyType_GenericNew)(PyTypeObject *type, PyObject *args, PyObject *kwds);
Bram Moolenaar66b79852012-09-21 14:00:35 +0200447static PyTypeObject* py3_PyType_Type;
Bram Moolenaard4a8c982018-05-15 22:31:18 +0200448static PyTypeObject* py3_PyStdPrinter_Type;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200449static PyTypeObject* py3_PySlice_Type;
Bram Moolenaardb913952012-06-29 12:54:53 +0200450static PyTypeObject* py3_PyFloat_Type;
Bram Moolenaar66b79852012-09-21 14:00:35 +0200451static PyTypeObject* py3_PyBool_Type;
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200452static int (*py3_PyNumber_Check)(PyObject *);
453static PyObject* (*py3_PyNumber_Long)(PyObject *);
Bram Moolenaar19e60942011-06-19 00:27:51 +0200454static PyObject* (*py3_PyErr_NewException)(char *name, PyObject *base, PyObject *dict);
Bram Moolenaardb913952012-06-29 12:54:53 +0200455static PyObject* (*py3_PyCapsule_New)(void *, char *, PyCapsule_Destructor);
456static void* (*py3_PyCapsule_GetPointer)(PyObject *, char *);
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200457# ifdef Py_DEBUG
Bram Moolenaar0014a532013-05-29 21:33:39 +0200458static void (*py3__Py_NegativeRefcount)(const char *fname, int lineno, PyObject *op);
459static Py_ssize_t* py3__Py_RefTotal;
Bram Moolenaar0014a532013-05-29 21:33:39 +0200460static PyObject* (*py3_PyModule_Create2TraceRefs)(struct PyModuleDef* module, int module_api_version);
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200461# else
Bram Moolenaar0014a532013-05-29 21:33:39 +0200462static PyObject* (*py3_PyModule_Create2)(struct PyModuleDef* module, int module_api_version);
463# endif
464# if defined(Py_DEBUG) && !defined(Py_DEBUG_NO_PYMALLOC)
465static void (*py3__PyObject_DebugFree)(void*);
466static void* (*py3__PyObject_DebugMalloc)(size_t);
467# else
468static void (*py3_PyObject_Free)(void*);
469static void* (*py3_PyObject_Malloc)(size_t);
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200470# endif
Bram Moolenaar774267b2013-05-21 20:51:59 +0200471static PyObject*(*py3__PyObject_GC_New)(PyTypeObject *);
472static void(*py3_PyObject_GC_Del)(void *);
473static void(*py3_PyObject_GC_UnTrack)(void *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200474static int (*py3_PyType_IsSubtype)(PyTypeObject *, PyTypeObject *);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200475
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100476// Imported exception objects
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200477static PyObject *p3imp_PyExc_AttributeError;
478static PyObject *p3imp_PyExc_IndexError;
Bram Moolenaaraf6abb92013-04-24 13:04:26 +0200479static PyObject *p3imp_PyExc_KeyError;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200480static PyObject *p3imp_PyExc_KeyboardInterrupt;
481static PyObject *p3imp_PyExc_TypeError;
482static PyObject *p3imp_PyExc_ValueError;
Bram Moolenaar41009372013-07-01 22:03:04 +0200483static PyObject *p3imp_PyExc_SystemExit;
Bram Moolenaar8661b172013-05-15 15:44:28 +0200484static PyObject *p3imp_PyExc_RuntimeError;
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200485static PyObject *p3imp_PyExc_ImportError;
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200486static PyObject *p3imp_PyExc_OverflowError;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200487
488# define PyExc_AttributeError p3imp_PyExc_AttributeError
489# define PyExc_IndexError p3imp_PyExc_IndexError
Bram Moolenaaraf6abb92013-04-24 13:04:26 +0200490# define PyExc_KeyError p3imp_PyExc_KeyError
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200491# define PyExc_KeyboardInterrupt p3imp_PyExc_KeyboardInterrupt
492# define PyExc_TypeError p3imp_PyExc_TypeError
493# define PyExc_ValueError p3imp_PyExc_ValueError
Bram Moolenaar41009372013-07-01 22:03:04 +0200494# define PyExc_SystemExit p3imp_PyExc_SystemExit
Bram Moolenaar8661b172013-05-15 15:44:28 +0200495# define PyExc_RuntimeError p3imp_PyExc_RuntimeError
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200496# define PyExc_ImportError p3imp_PyExc_ImportError
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200497# define PyExc_OverflowError p3imp_PyExc_OverflowError
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200498
499/*
500 * Table of name to function pointer of python.
501 */
502# define PYTHON_PROC FARPROC
503static struct
504{
505 char *name;
506 PYTHON_PROC *ptr;
507} py3_funcname_table[] =
508{
509 {"PySys_SetArgv", (PYTHON_PROC*)&py3_PySys_SetArgv},
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100510 {"Py_SetPythonHome", (PYTHON_PROC*)&py3_Py_SetPythonHome},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200511 {"Py_Initialize", (PYTHON_PROC*)&py3_Py_Initialize},
Bram Moolenaare8cdcef2012-09-12 20:21:43 +0200512 {"_PyArg_ParseTuple_SizeT", (PYTHON_PROC*)&py3_PyArg_ParseTuple},
513 {"_Py_BuildValue_SizeT", (PYTHON_PROC*)&py3_Py_BuildValue},
Bram Moolenaar19e60942011-06-19 00:27:51 +0200514 {"PyMem_Free", (PYTHON_PROC*)&py3_PyMem_Free},
Bram Moolenaardb913952012-06-29 12:54:53 +0200515 {"PyMem_Malloc", (PYTHON_PROC*)&py3_PyMem_Malloc},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200516 {"PyList_New", (PYTHON_PROC*)&py3_PyList_New},
517 {"PyGILState_Ensure", (PYTHON_PROC*)&py3_PyGILState_Ensure},
518 {"PyGILState_Release", (PYTHON_PROC*)&py3_PyGILState_Release},
519 {"PySys_SetObject", (PYTHON_PROC*)&py3_PySys_SetObject},
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200520 {"PySys_GetObject", (PYTHON_PROC*)&py3_PySys_GetObject},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200521 {"PyList_Append", (PYTHON_PROC*)&py3_PyList_Append},
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200522 {"PyList_Insert", (PYTHON_PROC*)&py3_PyList_Insert},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200523 {"PyList_Size", (PYTHON_PROC*)&py3_PyList_Size},
Bram Moolenaardb913952012-06-29 12:54:53 +0200524 {"PySequence_Check", (PYTHON_PROC*)&py3_PySequence_Check},
525 {"PySequence_Size", (PYTHON_PROC*)&py3_PySequence_Size},
526 {"PySequence_GetItem", (PYTHON_PROC*)&py3_PySequence_GetItem},
Bram Moolenaara9922d62013-05-30 13:01:18 +0200527 {"PySequence_Fast", (PYTHON_PROC*)&py3_PySequence_Fast},
Bram Moolenaardb913952012-06-29 12:54:53 +0200528 {"PyTuple_Size", (PYTHON_PROC*)&py3_PyTuple_Size},
529 {"PyTuple_GetItem", (PYTHON_PROC*)&py3_PyTuple_GetItem},
Bram Moolenaar3b48b112018-07-04 22:03:25 +0200530# if PY_VERSION_HEX >= 0x030601f0
531 {"PySlice_AdjustIndices", (PYTHON_PROC*)&py3_PySlice_AdjustIndices},
532 {"PySlice_Unpack", (PYTHON_PROC*)&py3_PySlice_Unpack},
533# endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200534 {"PySlice_GetIndicesEx", (PYTHON_PROC*)&py3_PySlice_GetIndicesEx},
535 {"PyErr_NoMemory", (PYTHON_PROC*)&py3_PyErr_NoMemory},
536 {"Py_Finalize", (PYTHON_PROC*)&py3_Py_Finalize},
537 {"PyErr_SetString", (PYTHON_PROC*)&py3_PyErr_SetString},
Bram Moolenaar4d188da2013-05-15 15:35:09 +0200538 {"PyErr_SetObject", (PYTHON_PROC*)&py3_PyErr_SetObject},
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200539 {"PyErr_ExceptionMatches", (PYTHON_PROC*)&py3_PyErr_ExceptionMatches},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200540 {"PyRun_SimpleString", (PYTHON_PROC*)&py3_PyRun_SimpleString},
Bram Moolenaardb913952012-06-29 12:54:53 +0200541 {"PyRun_String", (PYTHON_PROC*)&py3_PyRun_String},
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200542 {"PyObject_GetAttrString", (PYTHON_PROC*)&py3_PyObject_GetAttrString},
Bram Moolenaara9922d62013-05-30 13:01:18 +0200543 {"PyObject_HasAttrString", (PYTHON_PROC*)&py3_PyObject_HasAttrString},
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200544 {"PyObject_SetAttrString", (PYTHON_PROC*)&py3_PyObject_SetAttrString},
545 {"PyObject_CallFunctionObjArgs", (PYTHON_PROC*)&py3_PyObject_CallFunctionObjArgs},
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200546 {"_PyObject_CallFunction_SizeT", (PYTHON_PROC*)&py3__PyObject_CallFunction_SizeT},
Bram Moolenaarf4258302013-06-02 18:20:17 +0200547 {"PyObject_Call", (PYTHON_PROC*)&py3_PyObject_Call},
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200548 {"PyEval_GetGlobals", (PYTHON_PROC*)&py3_PyEval_GetGlobals},
549 {"PyEval_GetLocals", (PYTHON_PROC*)&py3_PyEval_GetLocals},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200550 {"PyList_GetItem", (PYTHON_PROC*)&py3_PyList_GetItem},
551 {"PyImport_ImportModule", (PYTHON_PROC*)&py3_PyImport_ImportModule},
Bram Moolenaardb913952012-06-29 12:54:53 +0200552 {"PyImport_AddModule", (PYTHON_PROC*)&py3_PyImport_AddModule},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200553 {"PyErr_BadArgument", (PYTHON_PROC*)&py3_PyErr_BadArgument},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200554 {"PyErr_Occurred", (PYTHON_PROC*)&py3_PyErr_Occurred},
555 {"PyModule_GetDict", (PYTHON_PROC*)&py3_PyModule_GetDict},
556 {"PyList_SetItem", (PYTHON_PROC*)&py3_PyList_SetItem},
557 {"PyDict_GetItemString", (PYTHON_PROC*)&py3_PyDict_GetItemString},
Bram Moolenaardb913952012-06-29 12:54:53 +0200558 {"PyDict_Next", (PYTHON_PROC*)&py3_PyDict_Next},
559 {"PyMapping_Check", (PYTHON_PROC*)&py3_PyMapping_Check},
Bram Moolenaarbcb40972013-05-30 13:22:13 +0200560 {"PyMapping_Keys", (PYTHON_PROC*)&py3_PyMapping_Keys},
Zdenek Dohnal90478f32021-06-14 15:08:30 +0200561# if PY_VERSION_HEX >= 0x030a00b2
562 {"PyIter_Check", (PYTHON_PROC*)&py3_PyIter_Check},
563# endif
Bram Moolenaardb913952012-06-29 12:54:53 +0200564 {"PyIter_Next", (PYTHON_PROC*)&py3_PyIter_Next},
565 {"PyObject_GetIter", (PYTHON_PROC*)&py3_PyObject_GetIter},
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200566 {"PyObject_Repr", (PYTHON_PROC*)&py3_PyObject_Repr},
Bram Moolenaarbcb40972013-05-30 13:22:13 +0200567 {"PyObject_GetItem", (PYTHON_PROC*)&py3_PyObject_GetItem},
Bram Moolenaar03db85b2013-05-15 14:51:35 +0200568 {"PyObject_IsTrue", (PYTHON_PROC*)&py3_PyObject_IsTrue},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200569 {"PyLong_FromLong", (PYTHON_PROC*)&py3_PyLong_FromLong},
570 {"PyDict_New", (PYTHON_PROC*)&py3_PyDict_New},
Bram Moolenaaree1b9312020-07-16 22:15:53 +0200571# if PY_VERSION_HEX >= 0x030900b0
572 {"PyType_GetFlags", (PYTHON_PROC*)&py3_PyType_GetFlags},
573# endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200574 {"PyType_Ready", (PYTHON_PROC*)&py3_PyType_Ready},
575 {"PyDict_SetItemString", (PYTHON_PROC*)&py3_PyDict_SetItemString},
576 {"PyLong_AsLong", (PYTHON_PROC*)&py3_PyLong_AsLong},
577 {"PyErr_SetNone", (PYTHON_PROC*)&py3_PyErr_SetNone},
578 {"PyEval_InitThreads", (PYTHON_PROC*)&py3_PyEval_InitThreads},
579 {"PyEval_RestoreThread", (PYTHON_PROC*)&py3_PyEval_RestoreThread},
580 {"PyEval_SaveThread", (PYTHON_PROC*)&py3_PyEval_SaveThread},
Bram Moolenaar5f87b232013-06-13 20:57:50 +0200581 {"_PyArg_Parse_SizeT", (PYTHON_PROC*)&py3_PyArg_Parse},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200582 {"Py_IsInitialized", (PYTHON_PROC*)&py3_Py_IsInitialized},
Bram Moolenaardb913952012-06-29 12:54:53 +0200583 {"_PyObject_NextNotImplemented", (PYTHON_PROC*)&py3__PyObject_NextNotImplemented},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200584 {"_Py_NoneStruct", (PYTHON_PROC*)&py3__Py_NoneStruct},
Bram Moolenaar66b79852012-09-21 14:00:35 +0200585 {"_Py_FalseStruct", (PYTHON_PROC*)&py3__Py_FalseStruct},
586 {"_Py_TrueStruct", (PYTHON_PROC*)&py3__Py_TrueStruct},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200587 {"PyErr_Clear", (PYTHON_PROC*)&py3_PyErr_Clear},
Bram Moolenaarc476e522013-06-23 13:46:40 +0200588 {"PyErr_Format", (PYTHON_PROC*)&py3_PyErr_Format},
Bram Moolenaar4d369872013-02-20 16:09:43 +0100589 {"PyErr_PrintEx", (PYTHON_PROC*)&py3_PyErr_PrintEx},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200590 {"PyObject_Init", (PYTHON_PROC*)&py3__PyObject_Init},
591 {"PyModule_AddObject", (PYTHON_PROC*)&py3_PyModule_AddObject},
592 {"PyImport_AppendInittab", (PYTHON_PROC*)&py3_PyImport_AppendInittab},
Bram Moolenaar9c9cbf12012-10-23 05:17:37 +0200593# if PY_VERSION_HEX >= 0x030300f0
594 {"PyUnicode_AsUTF8", (PYTHON_PROC*)&py3_PyUnicode_AsUTF8},
595# else
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200596 {"_PyUnicode_AsString", (PYTHON_PROC*)&py3__PyUnicode_AsString},
Bram Moolenaar9c9cbf12012-10-23 05:17:37 +0200597# endif
Bram Moolenaar1a3b5692013-05-30 12:40:39 +0200598# ifndef Py_UNICODE_USE_UCS_FUNCTIONS
599 {"PyUnicode_FromFormat", (PYTHON_PROC*)&py3_PyUnicode_FromFormat},
600# else
601# ifdef Py_UNICODE_WIDE
602 {"PyUnicodeUCS4_FromFormat", (PYTHON_PROC*)&py3_PyUnicodeUCS4_FromFormat},
603# else
604 {"PyUnicodeUCS2_FromFormat", (PYTHON_PROC*)&py3_PyUnicodeUCS2_FromFormat},
605# endif
606# endif
Bram Moolenaar19e60942011-06-19 00:27:51 +0200607 {"PyBytes_AsString", (PYTHON_PROC*)&py3_PyBytes_AsString},
Bram Moolenaarcdab9052012-09-05 19:03:56 +0200608 {"PyBytes_AsStringAndSize", (PYTHON_PROC*)&py3_PyBytes_AsStringAndSize},
Bram Moolenaardb913952012-06-29 12:54:53 +0200609 {"PyBytes_FromString", (PYTHON_PROC*)&py3_PyBytes_FromString},
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100610 {"PyBytes_FromStringAndSize", (PYTHON_PROC*)&py3_PyBytes_FromStringAndSize},
Bram Moolenaaree1b9312020-07-16 22:15:53 +0200611# if defined(Py_DEBUG) || PY_VERSION_HEX >= 0x030900b0
612 {"_Py_Dealloc", (PYTHON_PROC*)&py3__Py_Dealloc},
613# endif
614# if PY_VERSION_HEX >= 0x030900b0
615 {"_PyObject_New", (PYTHON_PROC*)&py3__PyObject_New},
616# endif
Bram Moolenaardb913952012-06-29 12:54:53 +0200617 {"PyFloat_FromDouble", (PYTHON_PROC*)&py3_PyFloat_FromDouble},
618 {"PyFloat_AsDouble", (PYTHON_PROC*)&py3_PyFloat_AsDouble},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200619 {"PyObject_GenericGetAttr", (PYTHON_PROC*)&py3_PyObject_GenericGetAttr},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200620 {"PyType_GenericAlloc", (PYTHON_PROC*)&py3_PyType_GenericAlloc},
621 {"PyType_GenericNew", (PYTHON_PROC*)&py3_PyType_GenericNew},
Bram Moolenaar66b79852012-09-21 14:00:35 +0200622 {"PyType_Type", (PYTHON_PROC*)&py3_PyType_Type},
Bram Moolenaard4a8c982018-05-15 22:31:18 +0200623 {"PyStdPrinter_Type", (PYTHON_PROC*)&py3_PyStdPrinter_Type},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200624 {"PySlice_Type", (PYTHON_PROC*)&py3_PySlice_Type},
Bram Moolenaardb913952012-06-29 12:54:53 +0200625 {"PyFloat_Type", (PYTHON_PROC*)&py3_PyFloat_Type},
Bram Moolenaar66b79852012-09-21 14:00:35 +0200626 {"PyBool_Type", (PYTHON_PROC*)&py3_PyBool_Type},
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200627 {"PyNumber_Check", (PYTHON_PROC*)&py3_PyNumber_Check},
628 {"PyNumber_Long", (PYTHON_PROC*)&py3_PyNumber_Long},
Bram Moolenaar19e60942011-06-19 00:27:51 +0200629 {"PyErr_NewException", (PYTHON_PROC*)&py3_PyErr_NewException},
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200630# ifdef Py_DEBUG
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200631 {"_Py_NegativeRefcount", (PYTHON_PROC*)&py3__Py_NegativeRefcount},
632 {"_Py_RefTotal", (PYTHON_PROC*)&py3__Py_RefTotal},
Bram Moolenaar0014a532013-05-29 21:33:39 +0200633 {"PyModule_Create2TraceRefs", (PYTHON_PROC*)&py3_PyModule_Create2TraceRefs},
634# else
635 {"PyModule_Create2", (PYTHON_PROC*)&py3_PyModule_Create2},
636# endif
637# if defined(Py_DEBUG) && !defined(Py_DEBUG_NO_PYMALLOC)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200638 {"_PyObject_DebugFree", (PYTHON_PROC*)&py3__PyObject_DebugFree},
639 {"_PyObject_DebugMalloc", (PYTHON_PROC*)&py3__PyObject_DebugMalloc},
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200640# else
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200641 {"PyObject_Malloc", (PYTHON_PROC*)&py3_PyObject_Malloc},
642 {"PyObject_Free", (PYTHON_PROC*)&py3_PyObject_Free},
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200643# endif
Bram Moolenaar774267b2013-05-21 20:51:59 +0200644 {"_PyObject_GC_New", (PYTHON_PROC*)&py3__PyObject_GC_New},
645 {"PyObject_GC_Del", (PYTHON_PROC*)&py3_PyObject_GC_Del},
646 {"PyObject_GC_UnTrack", (PYTHON_PROC*)&py3_PyObject_GC_UnTrack},
Bram Moolenaardb913952012-06-29 12:54:53 +0200647 {"PyType_IsSubtype", (PYTHON_PROC*)&py3_PyType_IsSubtype},
648 {"PyCapsule_New", (PYTHON_PROC*)&py3_PyCapsule_New},
649 {"PyCapsule_GetPointer", (PYTHON_PROC*)&py3_PyCapsule_GetPointer},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200650 {"", NULL},
651};
652
Bram Moolenaar13a1f3f2019-10-23 21:37:25 +0200653# if PY_VERSION_HEX >= 0x030800f0
654 static inline void
655py3__Py_DECREF(const char *filename UNUSED, int lineno UNUSED, PyObject *op)
656{
Bram Moolenaar13a1f3f2019-10-23 21:37:25 +0200657 if (--op->ob_refcnt != 0)
658 {
659# ifdef Py_REF_DEBUG
660 if (op->ob_refcnt < 0)
661 {
662 _Py_NegativeRefcount(filename, lineno, op);
663 }
664# endif
665 }
666 else
667 {
668 _Py_Dealloc(op);
669 }
670}
671
672# undef Py_DECREF
673# define Py_DECREF(op) py3__Py_DECREF(__FILE__, __LINE__, _PyObject_CAST(op))
674
675 static inline void
676py3__Py_XDECREF(PyObject *op)
677{
678 if (op != NULL)
679 {
680 Py_DECREF(op);
681 }
682}
683
684# undef Py_XDECREF
685# define Py_XDECREF(op) py3__Py_XDECREF(_PyObject_CAST(op))
686# endif
687
Bram Moolenaaree1b9312020-07-16 22:15:53 +0200688# if PY_VERSION_HEX >= 0x030900b0
689 static inline int
690py3_PyType_HasFeature(PyTypeObject *type, unsigned long feature)
691{
692 return ((PyType_GetFlags(type) & feature) != 0);
693}
694# define PyType_HasFeature(t,f) py3_PyType_HasFeature(t,f)
695# endif
696
Zdenek Dohnal90478f32021-06-14 15:08:30 +0200697# if PY_VERSION_HEX >= 0x030a00b2
698 static inline int
699py3__PyObject_TypeCheck(PyObject *ob, PyTypeObject *type)
700{
701 return Py_IS_TYPE(ob, type) || PyType_IsSubtype(Py_TYPE(ob), type);
702}
Zdenek Dohnalfee511c2022-06-27 13:59:00 +0100703# if PY_VERSION_HEX >= 0x030b00b3
704# undef PyObject_TypeCheck
705# define PyObject_TypeCheck(o,t) py3__PyObject_TypeCheck(o,t)
706# else
707# define _PyObject_TypeCheck(o,t) py3__PyObject_TypeCheck(o,t)
708# endif
Zdenek Dohnal90478f32021-06-14 15:08:30 +0200709# endif
710
Bram Moolenaarb2f9e0e2020-12-25 13:52:37 +0100711# ifdef MSWIN
712/*
713 * Look up the library "libname" using the InstallPath registry key.
714 * Return NULL when failed. Return an allocated string when successful.
715 */
716 static char *
717py3_get_system_libname(const char *libname)
718{
719 const char *cp = libname;
720 char subkey[128];
721 HKEY hKey;
722 char installpath[MAXPATHL];
723 LONG len = sizeof(installpath);
724 LSTATUS rc;
725 size_t sysliblen;
726 char *syslibname;
727
728 while (*cp != '\0')
729 {
730 if (*cp == ':' || *cp == '\\' || *cp == '/')
731 {
732 // Bail out if "libname" contains path separator, assume it is
733 // an absolute path.
734 return NULL;
735 }
736 ++cp;
737 }
738 vim_snprintf(subkey, sizeof(subkey),
739# ifdef _WIN64
740 "Software\\Python\\PythonCore\\%d.%d\\InstallPath",
741# else
742 "Software\\Python\\PythonCore\\%d.%d-32\\InstallPath",
743# endif
744 PY_MAJOR_VERSION, PY_MINOR_VERSION);
745 if (RegOpenKeyExA(HKEY_LOCAL_MACHINE, subkey, 0, KEY_QUERY_VALUE, &hKey)
746 != ERROR_SUCCESS)
747 return NULL;
748 rc = RegQueryValueA(hKey, NULL, installpath, &len);
749 RegCloseKey(hKey);
750 if (ERROR_SUCCESS != rc)
751 return NULL;
752 cp = installpath + len;
753 // Just in case registry value contains null terminators.
754 while (cp > installpath && *(cp-1) == '\0')
755 --cp;
756 // Remove trailing path separators.
757 while (cp > installpath && (*(cp-1) == '\\' || *(cp-1) == '/'))
758 --cp;
759 // Ignore if InstallPath is effectively empty.
760 if (cp <= installpath)
761 return NULL;
762 sysliblen = (cp - installpath) + 1 + STRLEN(libname) + 1;
763 syslibname = alloc(sysliblen);
764 vim_snprintf(syslibname, sysliblen, "%.*s\\%s",
765 (int)(cp - installpath), installpath, libname);
766 return syslibname;
767}
768# endif
769
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200770/*
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200771 * Load library and get all pointers.
772 * Parameter 'libname' provides name of DLL.
773 * Return OK or FAIL.
774 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200775 static int
776py3_runtime_link_init(char *libname, int verbose)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200777{
778 int i;
Bram Moolenaar7b24ce02018-03-29 18:15:26 +0200779 PYTHON_PROC *ucs_from_string = (PYTHON_PROC *)&py3_PyUnicode_FromString;
780 PYTHON_PROC *ucs_decode = (PYTHON_PROC *)&py3_PyUnicode_Decode;
781 PYTHON_PROC *ucs_as_encoded_string =
782 (PYTHON_PROC *)&py3_PyUnicode_AsEncodedString;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200783
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100784# if !(defined(PY_NO_RTLD_GLOBAL) && defined(PY3_NO_RTLD_GLOBAL)) && defined(UNIX) && defined(FEAT_PYTHON)
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100785 // Can't have Python and Python3 loaded at the same time.
Dominique Pelleaf4a61a2021-12-27 17:21:41 +0000786 // It causes a crash, because RTLD_GLOBAL is needed for
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100787 // standard C extension libraries of one or both python versions.
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200788 if (python_loaded())
789 {
Bram Moolenaar9dc93ae2011-08-28 16:00:19 +0200790 if (verbose)
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +0000791 emsg(_(e_this_vim_cannot_execute_py3_after_using_python));
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200792 return FAIL;
793 }
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200794# endif
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200795
796 if (hinstPy3 != 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200797 return OK;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200798 hinstPy3 = load_dll(libname);
799
Bram Moolenaarb2f9e0e2020-12-25 13:52:37 +0100800# ifdef MSWIN
801 if (!hinstPy3)
802 {
803 // Attempt to use the path from InstallPath as stored in the registry.
804 char *syslibname = py3_get_system_libname(libname);
805
806 if (syslibname != NULL)
807 {
808 hinstPy3 = load_dll(syslibname);
809 vim_free(syslibname);
810 }
811 }
812# endif
813
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200814 if (!hinstPy3)
815 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200816 if (verbose)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000817 semsg(_(e_could_not_load_library_str_str), libname, load_dll_error());
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200818 return FAIL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200819 }
820
821 for (i = 0; py3_funcname_table[i].ptr; ++i)
822 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200823 if ((*py3_funcname_table[i].ptr = symbol_from_dll(hinstPy3,
824 py3_funcname_table[i].name)) == NULL)
825 {
826 close_dll(hinstPy3);
827 hinstPy3 = 0;
828 if (verbose)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000829 semsg(_(e_could_not_load_library_function_str), py3_funcname_table[i].name);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200830 return FAIL;
831 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200832 }
833
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100834 // Load unicode functions separately as only the ucs2 or the ucs4 functions
835 // will be present in the library.
Bram Moolenaar9c9cbf12012-10-23 05:17:37 +0200836# if PY_VERSION_HEX >= 0x030300f0
Bram Moolenaar7b24ce02018-03-29 18:15:26 +0200837 *ucs_from_string = symbol_from_dll(hinstPy3, "PyUnicode_FromString");
838 *ucs_decode = symbol_from_dll(hinstPy3, "PyUnicode_Decode");
839 *ucs_as_encoded_string = symbol_from_dll(hinstPy3,
Bram Moolenaar7bc4f932012-10-14 03:22:56 +0200840 "PyUnicode_AsEncodedString");
Bram Moolenaar9c9cbf12012-10-23 05:17:37 +0200841# else
Bram Moolenaar7b24ce02018-03-29 18:15:26 +0200842 *ucs_from_string = symbol_from_dll(hinstPy3, "PyUnicodeUCS2_FromString");
843 *ucs_decode = symbol_from_dll(hinstPy3,
Bram Moolenaar19e60942011-06-19 00:27:51 +0200844 "PyUnicodeUCS2_Decode");
Bram Moolenaar7b24ce02018-03-29 18:15:26 +0200845 *ucs_as_encoded_string = symbol_from_dll(hinstPy3,
Bram Moolenaar19e60942011-06-19 00:27:51 +0200846 "PyUnicodeUCS2_AsEncodedString");
Bram Moolenaar7b24ce02018-03-29 18:15:26 +0200847 if (*ucs_from_string == NULL || *ucs_decode == NULL
848 || *ucs_as_encoded_string == NULL)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200849 {
Bram Moolenaar7b24ce02018-03-29 18:15:26 +0200850 *ucs_from_string = symbol_from_dll(hinstPy3,
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200851 "PyUnicodeUCS4_FromString");
Bram Moolenaar7b24ce02018-03-29 18:15:26 +0200852 *ucs_decode = symbol_from_dll(hinstPy3,
Bram Moolenaar19e60942011-06-19 00:27:51 +0200853 "PyUnicodeUCS4_Decode");
Bram Moolenaar7b24ce02018-03-29 18:15:26 +0200854 *ucs_as_encoded_string = symbol_from_dll(hinstPy3,
Bram Moolenaar19e60942011-06-19 00:27:51 +0200855 "PyUnicodeUCS4_AsEncodedString");
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200856 }
Bram Moolenaar9c9cbf12012-10-23 05:17:37 +0200857# endif
Bram Moolenaar7b24ce02018-03-29 18:15:26 +0200858 if (*ucs_from_string == NULL || *ucs_decode == NULL
859 || *ucs_as_encoded_string == NULL)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200860 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200861 close_dll(hinstPy3);
862 hinstPy3 = 0;
863 if (verbose)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000864 semsg(_(e_could_not_load_library_function_str), "PyUnicode_UCSX_*");
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200865 return FAIL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200866 }
867
868 return OK;
869}
870
871/*
872 * If python is enabled (there is installed python on Windows system) return
873 * TRUE, else FALSE.
874 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200875 int
876python3_enabled(int verbose)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200877{
Bram Moolenaar25e4fcd2016-01-09 14:57:47 +0100878 return py3_runtime_link_init((char *)p_py3dll, verbose) == OK;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200879}
880
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100881/*
882 * Load the standard Python exceptions - don't import the symbols from the
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200883 * DLL, as this can cause errors (importing data symbols is not reliable).
884 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200885 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100886get_py3_exceptions(void)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200887{
888 PyObject *exmod = PyImport_ImportModule("builtins");
889 PyObject *exdict = PyModule_GetDict(exmod);
890 p3imp_PyExc_AttributeError = PyDict_GetItemString(exdict, "AttributeError");
891 p3imp_PyExc_IndexError = PyDict_GetItemString(exdict, "IndexError");
Bram Moolenaaraf6abb92013-04-24 13:04:26 +0200892 p3imp_PyExc_KeyError = PyDict_GetItemString(exdict, "KeyError");
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200893 p3imp_PyExc_KeyboardInterrupt = PyDict_GetItemString(exdict, "KeyboardInterrupt");
894 p3imp_PyExc_TypeError = PyDict_GetItemString(exdict, "TypeError");
895 p3imp_PyExc_ValueError = PyDict_GetItemString(exdict, "ValueError");
Bram Moolenaar41009372013-07-01 22:03:04 +0200896 p3imp_PyExc_SystemExit = PyDict_GetItemString(exdict, "SystemExit");
Bram Moolenaar8661b172013-05-15 15:44:28 +0200897 p3imp_PyExc_RuntimeError = PyDict_GetItemString(exdict, "RuntimeError");
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200898 p3imp_PyExc_ImportError = PyDict_GetItemString(exdict, "ImportError");
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200899 p3imp_PyExc_OverflowError = PyDict_GetItemString(exdict, "OverflowError");
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200900 Py_XINCREF(p3imp_PyExc_AttributeError);
901 Py_XINCREF(p3imp_PyExc_IndexError);
Bram Moolenaaraf6abb92013-04-24 13:04:26 +0200902 Py_XINCREF(p3imp_PyExc_KeyError);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200903 Py_XINCREF(p3imp_PyExc_KeyboardInterrupt);
904 Py_XINCREF(p3imp_PyExc_TypeError);
905 Py_XINCREF(p3imp_PyExc_ValueError);
Bram Moolenaar41009372013-07-01 22:03:04 +0200906 Py_XINCREF(p3imp_PyExc_SystemExit);
Bram Moolenaar8661b172013-05-15 15:44:28 +0200907 Py_XINCREF(p3imp_PyExc_RuntimeError);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200908 Py_XINCREF(p3imp_PyExc_ImportError);
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200909 Py_XINCREF(p3imp_PyExc_OverflowError);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200910 Py_XDECREF(exmod);
911}
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100912#endif // DYNAMIC_PYTHON3
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200913
Bram Moolenaardb913952012-06-29 12:54:53 +0200914static int py3initialised = 0;
Bram Moolenaardb913952012-06-29 12:54:53 +0200915#define PYINITIALISED py3initialised
Bram Moolenaarc4f83382017-07-07 14:50:44 +0200916static int python_end_called = FALSE;
Bram Moolenaardb913952012-06-29 12:54:53 +0200917
Bram Moolenaar774267b2013-05-21 20:51:59 +0200918#define DESTRUCTOR_FINISH(self) Py_TYPE(self)->tp_free((PyObject*)self)
Bram Moolenaardb913952012-06-29 12:54:53 +0200919
Bram Moolenaar971db462013-05-12 18:44:48 +0200920#define WIN_PYTHON_REF(win) win->w_python3_ref
921#define BUF_PYTHON_REF(buf) buf->b_python3_ref
Bram Moolenaar5e538ec2013-05-15 15:12:29 +0200922#define TAB_PYTHON_REF(tab) tab->tp_python3_ref
Bram Moolenaar971db462013-05-12 18:44:48 +0200923
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200924 static void
925call_PyObject_Free(void *p)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200926{
Bram Moolenaar0014a532013-05-29 21:33:39 +0200927#if defined(Py_DEBUG) && !defined(Py_DEBUG_NO_PYMALLOC)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200928 _PyObject_DebugFree(p);
929#else
930 PyObject_Free(p);
931#endif
932}
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200933
934 static PyObject *
935call_PyType_GenericNew(PyTypeObject *type, PyObject *args, PyObject *kwds)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200936{
937 return PyType_GenericNew(type,args,kwds);
938}
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200939
940 static PyObject *
941call_PyType_GenericAlloc(PyTypeObject *type, Py_ssize_t nitems)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200942{
943 return PyType_GenericAlloc(type,nitems);
944}
945
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200946static PyObject *OutputGetattro(PyObject *, PyObject *);
947static int OutputSetattro(PyObject *, PyObject *, PyObject *);
948static PyObject *BufferGetattro(PyObject *, PyObject *);
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200949static int BufferSetattro(PyObject *, PyObject *, PyObject *);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +0200950static PyObject *TabPageGetattro(PyObject *, PyObject *);
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200951static PyObject *WindowGetattro(PyObject *, PyObject *);
952static int WindowSetattro(PyObject *, PyObject *, PyObject *);
953static PyObject *RangeGetattro(PyObject *, PyObject *);
954static PyObject *CurrentGetattro(PyObject *, PyObject *);
955static int CurrentSetattro(PyObject *, PyObject *, PyObject *);
956static PyObject *DictionaryGetattro(PyObject *, PyObject *);
957static int DictionarySetattro(PyObject *, PyObject *, PyObject *);
958static PyObject *ListGetattro(PyObject *, PyObject *);
959static int ListSetattro(PyObject *, PyObject *, PyObject *);
960static PyObject *FunctionGetattro(PyObject *, PyObject *);
961
962static struct PyModuleDef vimmodule;
963
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +0200964#define PY_CAN_RECURSE
965
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200966/*
967 * Include the code shared with if_python.c
968 */
969#include "if_py_both.h"
970
Bram Moolenaar828bff12019-03-19 22:11:41 +0100971// NOTE: Must always be used at the start of a block, since it declares "name".
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200972#define GET_ATTR_STRING(name, nameobj) \
973 char *name = ""; \
974 if (PyUnicode_Check(nameobj)) \
Bram Moolenaar828bff12019-03-19 22:11:41 +0100975 name = (char *)_PyUnicode_AsString(nameobj)
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200976
977#define PY3OBJ_DELETED(obj) (obj->ob_base.ob_refcnt<=0)
978
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100979///////////////////////////////////////////////////////
980// Internal function prototypes.
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200981
Bram Moolenaar7854e3a2012-11-28 15:33:14 +0100982static PyObject *Py3Init_vim(void);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200983
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100984///////////////////////////////////////////////////////
985// 1. Python interpreter main program.
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200986
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200987 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100988python3_end(void)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200989{
990 static int recurse = 0;
991
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100992 // If a crash occurs while doing this, don't try again.
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200993 if (recurse != 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200994 return;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200995
Bram Moolenaarc4f83382017-07-07 14:50:44 +0200996 python_end_called = TRUE;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200997 ++recurse;
998
999#ifdef DYNAMIC_PYTHON3
1000 if (hinstPy3)
1001#endif
1002 if (Py_IsInitialized())
1003 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001004 // acquire lock before finalizing
Bram Moolenaar71700b82013-05-15 17:49:05 +02001005 PyGILState_Ensure();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001006
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001007 Py_Finalize();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001008 }
1009
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001010 --recurse;
1011}
1012
Bram Moolenaar094454f2015-10-07 10:39:55 +02001013#if (defined(DYNAMIC_PYTHON3) && defined(DYNAMIC_PYTHON) && defined(FEAT_PYTHON) && defined(UNIX)) || defined(PROTO)
Bram Moolenaar4c3a3262010-07-24 15:42:14 +02001014 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001015python3_loaded(void)
Bram Moolenaar4c3a3262010-07-24 15:42:14 +02001016{
1017 return (hinstPy3 != 0);
1018}
1019#endif
1020
Bram Moolenaar94073162018-01-31 21:49:05 +01001021static wchar_t *py_home_buf = NULL;
1022
Bram Moolenaar56b8dc32020-08-06 21:47:11 +02001023#if defined(MSWIN) && (PY_VERSION_HEX >= 0x030500f0)
Bram Moolenaarc6ed2542020-10-10 23:26:28 +02001024/*
1025 * Return TRUE if stdin is readable from Python 3.
1026 */
1027 static BOOL
1028is_stdin_readable(void)
1029{
1030 DWORD mode, eventnum;
1031 struct _stat st;
1032 int fd = fileno(stdin);
1033 HANDLE hstdin = (HANDLE)_get_osfhandle(fd);
1034
1035 // Check if stdin is connected to the console.
1036 if (GetConsoleMode(hstdin, &mode))
1037 // Check if it is opened as input.
1038 return GetNumberOfConsoleInputEvents(hstdin, &eventnum);
1039
1040 return _fstat(fd, &st) == 0;
1041}
1042
1043// Python 3.5 or later will abort inside Py_Initialize() when stdin has
1044// been closed (i.e. executed by "vim -"). Reconnect stdin to CONIN$.
Bram Moolenaar56b8dc32020-08-06 21:47:11 +02001045// Note that the python DLL is linked to its own stdio DLL which can be
1046// differ from Vim's stdio.
1047 static void
1048reset_stdin(void)
1049{
1050 FILE *(*py__acrt_iob_func)(unsigned) = NULL;
1051 FILE *(*pyfreopen)(const char *, const char *, FILE *) = NULL;
Bram Moolenaar63ff72a2022-02-07 13:54:01 +00001052 HINSTANCE hinst = hinstPy3;
Bram Moolenaar56b8dc32020-08-06 21:47:11 +02001053
Bram Moolenaarc6ed2542020-10-10 23:26:28 +02001054 if (hinst == NULL || is_stdin_readable())
Bram Moolenaar56b8dc32020-08-06 21:47:11 +02001055 return;
1056
1057 // Get "freopen" and "stdin" which are used in the python DLL.
1058 // "stdin" is defined as "__acrt_iob_func(0)" in VC++ 2015 or later.
1059 py__acrt_iob_func = get_dll_import_func(hinst, "__acrt_iob_func");
1060 if (py__acrt_iob_func)
1061 {
1062 HINSTANCE hpystdiodll = find_imported_module_by_funcname(hinst,
Bram Moolenaar253b16a2020-10-06 19:59:06 +02001063 "__acrt_iob_func");
Bram Moolenaar56b8dc32020-08-06 21:47:11 +02001064 if (hpystdiodll)
Bram Moolenaar253b16a2020-10-06 19:59:06 +02001065 pyfreopen = (void *)GetProcAddress(hpystdiodll, "freopen");
Bram Moolenaar56b8dc32020-08-06 21:47:11 +02001066 }
1067
Bram Moolenaarc6ed2542020-10-10 23:26:28 +02001068 // Reconnect stdin to CONIN$.
Bram Moolenaar253b16a2020-10-06 19:59:06 +02001069 if (pyfreopen != NULL)
Bram Moolenaarc6ed2542020-10-10 23:26:28 +02001070 pyfreopen("CONIN$", "r", py__acrt_iob_func(0));
Bram Moolenaar56b8dc32020-08-06 21:47:11 +02001071 else
Bram Moolenaarc6ed2542020-10-10 23:26:28 +02001072 freopen("CONIN$", "r", stdin);
Bram Moolenaar56b8dc32020-08-06 21:47:11 +02001073}
1074#else
1075# define reset_stdin()
1076#endif
1077
Bram Moolenaar63ff72a2022-02-07 13:54:01 +00001078// Python 3.2 or later will abort inside Py_Initialize() when mandatory
1079// modules cannot be loaded (e.g. 'pythonthreehome' is wrongly set.).
1080// Install a hook to python dll's exit() and recover from it.
1081#if defined(MSWIN) && (PY_VERSION_HEX >= 0x030200f0)
1082# define HOOK_EXIT
1083# include <setjmp.h>
1084
1085static jmp_buf exit_hook_jump_buf;
1086static void *orig_exit = NULL;
1087
1088/*
1089 * Function that replaces exit() while calling Py_Initialize().
1090 */
1091 static void
1092hooked_exit(int ret)
1093{
1094 // Recover from exit.
1095 longjmp(exit_hook_jump_buf, 1);
1096}
1097
1098/*
1099 * Install a hook to python dll's exit().
1100 */
1101 static void
1102hook_py_exit(void)
1103{
1104 HINSTANCE hinst = hinstPy3;
1105
1106 if (hinst == NULL || orig_exit != NULL)
1107 return;
1108
1109 orig_exit = hook_dll_import_func(hinst, "exit", (void *)hooked_exit);
1110}
1111
1112/*
1113 * Remove the hook installed by hook_py_exit().
1114 */
1115 static void
1116restore_py_exit(void)
1117{
1118 HINSTANCE hinst = hinstPy3;
1119
1120 if (hinst == NULL)
1121 return;
1122
1123 if (orig_exit != NULL)
1124 hook_dll_import_func(hinst, "exit", orig_exit);
1125 orig_exit = NULL;
1126}
1127#endif
1128
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001129 static int
1130Python3_Init(void)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001131{
1132 if (!py3initialised)
1133 {
1134#ifdef DYNAMIC_PYTHON3
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001135 if (!python3_enabled(TRUE))
1136 {
Bram Moolenaar9a846fb2022-01-01 21:59:18 +00001137 emsg(_(e_sorry_this_command_is_disabled_python_library_could_not_be_found));
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001138 goto fail;
1139 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001140#endif
1141
1142 init_structs();
1143
Bram Moolenaar94073162018-01-31 21:49:05 +01001144 if (*p_py3home != NUL)
1145 {
1146 size_t len = mbstowcs(NULL, (char *)p_py3home, 0) + 1;
Bram Moolenaar644d37b2010-11-16 19:26:02 +01001147
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001148 // The string must not change later, make a copy in static memory.
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001149 py_home_buf = ALLOC_MULT(wchar_t, len);
Bram Moolenaar94073162018-01-31 21:49:05 +01001150 if (py_home_buf != NULL && mbstowcs(
1151 py_home_buf, (char *)p_py3home, len) != (size_t)-1)
1152 Py_SetPythonHome(py_home_buf);
1153 }
Bram Moolenaar644d37b2010-11-16 19:26:02 +01001154#ifdef PYTHON3_HOME
Bram Moolenaar94073162018-01-31 21:49:05 +01001155 else if (mch_getenv((char_u *)"PYTHONHOME") == NULL)
Bram Moolenaar10005652015-12-31 21:03:23 +01001156 Py_SetPythonHome(PYTHON3_HOME);
Bram Moolenaar644d37b2010-11-16 19:26:02 +01001157#endif
1158
Bram Moolenaar7bc4f932012-10-14 03:22:56 +02001159 PyImport_AppendInittab("vim", Py3Init_vim);
1160
Bram Moolenaar63ff72a2022-02-07 13:54:01 +00001161#if !defined(DYNAMIC_PYTHON3) && defined(MSWIN)
1162 hinstPy3 = GetModuleHandle(PYTHON3_DLL);
1163#endif
Bram Moolenaar56b8dc32020-08-06 21:47:11 +02001164 reset_stdin();
Bram Moolenaar63ff72a2022-02-07 13:54:01 +00001165
1166#ifdef HOOK_EXIT
1167 // Catch exit() called in Py_Initialize().
1168 hook_py_exit();
1169 if (setjmp(exit_hook_jump_buf) == 0)
Bram Moolenaar63ff72a2022-02-07 13:54:01 +00001170 {
1171 Py_Initialize();
Bram Moolenaar63ff72a2022-02-07 13:54:01 +00001172 restore_py_exit();
Bram Moolenaar63ff72a2022-02-07 13:54:01 +00001173 }
Bram Moolenaar63ff72a2022-02-07 13:54:01 +00001174 else
1175 {
1176 // exit() was called in Py_Initialize().
1177 restore_py_exit();
1178 emsg(_(e_critical_error_in_python3_initialization_check_your_installation));
1179 goto fail;
1180 }
K.Takataa7fbaa42022-12-26 14:46:51 +00001181#else
1182 Py_Initialize();
Bram Moolenaar63ff72a2022-02-07 13:54:01 +00001183#endif
Bram Moolenaard0573012017-10-28 21:11:06 +02001184
Bram Moolenaarefc0d942020-10-11 18:05:02 +02001185#if PY_VERSION_HEX < 0x03090000
1186 // Initialise threads. This is deprecated since Python 3.9.
Bram Moolenaar456f2bb2011-06-12 21:37:13 +02001187 PyEval_InitThreads();
Bram Moolenaarefc0d942020-10-11 18:05:02 +02001188#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001189#ifdef DYNAMIC_PYTHON3
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001190 get_py3_exceptions();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001191#endif
1192
Bram Moolenaar1dc28782013-05-21 19:11:01 +02001193 if (PythonIO_Init_io())
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001194 goto fail;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001195
Bram Moolenaardb913952012-06-29 12:54:53 +02001196 globals = PyModule_GetDict(PyImport_AddModule("__main__"));
1197
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001198 // Remove the element from sys.path that was added because of our
1199 // argv[0] value in Py3Init_vim(). Previously we used an empty
1200 // string, but depending on the OS we then get an empty entry or
1201 // the current directory in sys.path.
1202 // Only after vim has been imported, the element does exist in
1203 // sys.path.
Bram Moolenaar19e60942011-06-19 00:27:51 +02001204 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 +02001205
Bram Moolenaarefc0d942020-10-11 18:05:02 +02001206 // Without the call to PyEval_SaveThread, thread specific state (such
1207 // as the system trace hook), will be lost between invocations of
1208 // Python code.
1209 // GIL may have been created and acquired in PyEval_InitThreads() and
1210 // thread state is created in Py_Initialize(); there
1211 // _PyGILState_NoteThreadState() also sets gilcounter to 1 (python must
1212 // have threads enabled!), so the following does both: unlock GIL and
1213 // save thread state in TLS without deleting thread state
Bram Moolenaar76d711c2013-02-13 14:17:08 +01001214 PyEval_SaveThread();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001215
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001216 py3initialised = 1;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001217 }
1218
1219 return 0;
1220
1221fail:
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001222 // We call PythonIO_Flush() here to print any Python errors.
1223 // This is OK, as it is possible to call this function even
1224 // if PythonIO_Init_io() has not completed successfully (it will
1225 // not do anything in this case).
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001226 PythonIO_Flush();
1227 return -1;
1228}
1229
1230/*
1231 * External interface
1232 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001233 static void
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02001234DoPyCommand(const char *cmd, rangeinitializer init_range, runner run, void *arg)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001235{
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001236#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001237 char *saved_locale;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001238#endif
Bram Moolenaar19e60942011-06-19 00:27:51 +02001239 PyObject *cmdstr;
1240 PyObject *cmdbytes;
Bram Moolenaar71700b82013-05-15 17:49:05 +02001241 PyGILState_STATE pygilstate;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001242
Bram Moolenaarc4f83382017-07-07 14:50:44 +02001243 if (python_end_called)
1244 goto theend;
1245
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001246 if (Python3_Init())
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001247 goto theend;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001248
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02001249 init_range(arg);
1250
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001251 Python_Release_Vim(); // leave Vim
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001252
1253#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001254 // Python only works properly when the LC_NUMERIC locale is "C".
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001255 saved_locale = setlocale(LC_NUMERIC, NULL);
1256 if (saved_locale == NULL || STRCMP(saved_locale, "C") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001257 saved_locale = NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001258 else
1259 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001260 // Need to make a copy, value may change when setting new locale.
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001261 saved_locale = (char *)vim_strsave((char_u *)saved_locale);
1262 (void)setlocale(LC_NUMERIC, "C");
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001263 }
1264#endif
1265
1266 pygilstate = PyGILState_Ensure();
1267
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001268 // PyRun_SimpleString expects a UTF-8 string. Wrong encoding may cause
1269 // SyntaxError (unicode error).
Bram Moolenaar3d64a312011-07-15 15:54:44 +02001270 cmdstr = PyUnicode_Decode(cmd, strlen(cmd),
Bram Moolenaar2e2f52a2020-12-21 16:03:02 +01001271 (char *)ENC_OPT, ERRORS_DECODE_ARG);
1272 cmdbytes = PyUnicode_AsEncodedString(cmdstr, "utf-8", ERRORS_ENCODE_ARG);
Bram Moolenaar19e60942011-06-19 00:27:51 +02001273 Py_XDECREF(cmdstr);
Bram Moolenaardb913952012-06-29 12:54:53 +02001274
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02001275 run(PyBytes_AsString(cmdbytes), arg, &pygilstate);
Bram Moolenaar19e60942011-06-19 00:27:51 +02001276 Py_XDECREF(cmdbytes);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001277
1278 PyGILState_Release(pygilstate);
1279
1280#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
1281 if (saved_locale != NULL)
1282 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001283 (void)setlocale(LC_NUMERIC, saved_locale);
1284 vim_free(saved_locale);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001285 }
1286#endif
1287
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001288 Python_Lock_Vim(); // enter Vim
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001289 PythonIO_Flush();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001290
1291theend:
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001292 return; // keeps lint happy
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001293}
1294
1295/*
Bram Moolenaar368373e2010-07-19 20:46:22 +02001296 * ":py3"
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001297 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001298 void
1299ex_py3(exarg_T *eap)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001300{
1301 char_u *script;
1302
1303 script = script_get(eap, eap->arg);
1304 if (!eap->skip)
1305 {
Bram Moolenaar14816ad2019-02-18 22:04:56 +01001306 if (p_pyx == 0)
1307 p_pyx = 3;
1308
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02001309 DoPyCommand(script == NULL ? (char *) eap->arg : (char *) script,
1310 (rangeinitializer) init_range_cmd,
1311 (runner) run_cmd,
1312 (void *) eap);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001313 }
1314 vim_free(script);
1315}
1316
1317#define BUFFER_SIZE 2048
1318
1319/*
Bram Moolenaar6df6f472010-07-18 18:04:50 +02001320 * ":py3file"
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001321 */
1322 void
1323ex_py3file(exarg_T *eap)
1324{
1325 static char buffer[BUFFER_SIZE];
1326 const char *file;
1327 char *p;
1328 int i;
1329
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01001330 if (p_pyx == 0)
1331 p_pyx = 3;
1332
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001333 // Have to do it like this. PyRun_SimpleFile requires you to pass a
1334 // stdio file pointer, but Vim and the Python DLL are compiled with
1335 // different options under Windows, meaning that stdio pointers aren't
1336 // compatible between the two. Yuk.
1337 //
1338 // construct: exec(compile(open('a_filename', 'rb').read(), 'a_filename', 'exec'))
1339 //
1340 // Using bytes so that Python can detect the source encoding as it normally
1341 // does. The doc does not say "compile" accept bytes, though.
1342 //
1343 // We need to escape any backslashes or single quotes in the file name, so that
1344 // Python won't mangle the file name.
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001345
1346 strcpy(buffer, "exec(compile(open('");
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001347 p = buffer + 19; // size of "exec(compile(open('"
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001348
1349 for (i=0; i<2; ++i)
1350 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001351 file = (char *)eap->arg;
1352 while (*file && p < buffer + (BUFFER_SIZE - 3))
1353 {
1354 if (*file == '\\' || *file == '\'')
1355 *p++ = '\\';
1356 *p++ = *file++;
1357 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001358 // If we didn't finish the file name, we hit a buffer overflow
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001359 if (*file != '\0')
1360 return;
1361 if (i==0)
1362 {
Bram Moolenaar19e60942011-06-19 00:27:51 +02001363 strcpy(p,"','rb').read(),'");
1364 p += 16;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001365 }
1366 else
1367 {
1368 strcpy(p,"','exec'))");
1369 p += 10;
1370 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001371 }
1372
1373
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001374 // Execute the file
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02001375 DoPyCommand(buffer,
1376 (rangeinitializer) init_range_cmd,
1377 (runner) run_cmd,
1378 (void *) eap);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001379}
1380
Bram Moolenaar54e8f002013-05-15 19:44:39 +02001381 void
1382ex_py3do(exarg_T *eap)
Bram Moolenaar3dab2802013-05-15 18:28:13 +02001383{
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01001384 if (p_pyx == 0)
1385 p_pyx = 3;
1386
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02001387 DoPyCommand((char *)eap->arg,
1388 (rangeinitializer)init_range_cmd,
1389 (runner)run_do,
1390 (void *)eap);
Bram Moolenaar3dab2802013-05-15 18:28:13 +02001391}
1392
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001393///////////////////////////////////////////////////////
1394// 2. Python output stream: writes output via [e]msg().
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001395
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001396// Implementation functions
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001397
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001398 static PyObject *
1399OutputGetattro(PyObject *self, PyObject *nameobj)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001400{
Bram Moolenaar77045652012-09-21 13:46:06 +02001401 GET_ATTR_STRING(name, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001402
1403 if (strcmp(name, "softspace") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001404 return PyLong_FromLong(((OutputObject *)(self))->softspace);
Bram Moolenaar6d4431e2016-04-21 20:00:56 +02001405 else if (strcmp(name, "errors") == 0)
1406 return PyString_FromString("strict");
1407 else if (strcmp(name, "encoding") == 0)
1408 return PyString_FromString(ENC_OPT);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001409
1410 return PyObject_GenericGetAttr(self, nameobj);
1411}
1412
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001413 static int
1414OutputSetattro(PyObject *self, PyObject *nameobj, PyObject *val)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001415{
Bram Moolenaar77045652012-09-21 13:46:06 +02001416 GET_ATTR_STRING(name, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001417
Bram Moolenaard6e39182013-05-21 18:30:34 +02001418 return OutputSetattr((OutputObject *)(self), name, val);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001419}
1420
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001421///////////////////////////////////////////////////////
1422// 3. Implementation of the Vim module for Python
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001423
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001424// Window type - Implementation functions
1425// --------------------------------------
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001426
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001427#define WindowType_Check(obj) ((obj)->ob_base.ob_type == &WindowType)
1428
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001429// Buffer type - Implementation functions
1430// --------------------------------------
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001431
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001432#define BufferType_Check(obj) ((obj)->ob_base.ob_type == &BufferType)
1433
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001434static PyObject* BufferSubscript(PyObject *self, PyObject *idx);
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02001435static int BufferAsSubscript(PyObject *self, PyObject *idx, PyObject *val);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001436
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001437// Line range type - Implementation functions
1438// --------------------------------------
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001439
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001440#define RangeType_Check(obj) ((obj)->ob_base.ob_type == &RangeType)
1441
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001442static PyObject* RangeSubscript(PyObject *self, PyObject *idx);
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02001443static int RangeAsItem(PyObject *, Py_ssize_t, PyObject *);
1444static int RangeAsSubscript(PyObject *self, PyObject *idx, PyObject *val);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001445
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001446// Current objects type - Implementation functions
1447// -----------------------------------------------
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001448
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001449static PySequenceMethods BufferAsSeq = {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001450 (lenfunc) BufferLength, // sq_length, len(x)
1451 (binaryfunc) 0, // sq_concat, x+y
1452 (ssizeargfunc) 0, // sq_repeat, x*n
1453 (ssizeargfunc) BufferItem, // sq_item, x[i]
1454 0, // was_sq_slice, x[i:j]
1455 0, // sq_ass_item, x[i]=v
1456 0, // sq_ass_slice, x[i:j]=v
1457 0, // sq_contains
1458 0, // sq_inplace_concat
1459 0, // sq_inplace_repeat
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001460};
1461
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001462static PyMappingMethods BufferAsMapping = {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001463 /* mp_length */ (lenfunc)BufferLength,
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001464 /* mp_subscript */ (binaryfunc)BufferSubscript,
Bram Moolenaar19e60942011-06-19 00:27:51 +02001465 /* mp_ass_subscript */ (objobjargproc)BufferAsSubscript,
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001466};
1467
1468
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001469// Buffer object
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001470
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001471 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02001472BufferGetattro(PyObject *self, PyObject *nameobj)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001473{
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001474 PyObject *r;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001475
Bram Moolenaar77045652012-09-21 13:46:06 +02001476 GET_ATTR_STRING(name, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001477
Bram Moolenaar9e822c02013-05-29 22:15:30 +02001478 if ((r = BufferAttrValid((BufferObject *)(self), name)))
1479 return r;
1480
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001481 if (CheckBuffer((BufferObject *)(self)))
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001482 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001483
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001484 r = BufferAttr((BufferObject *)(self), name);
1485 if (r || PyErr_Occurred())
1486 return r;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001487 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001488 return PyObject_GenericGetAttr(self, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001489}
1490
Bram Moolenaare9ba5162013-05-29 22:02:22 +02001491 static int
1492BufferSetattro(PyObject *self, PyObject *nameobj, PyObject *val)
1493{
1494 GET_ATTR_STRING(name, nameobj);
1495
1496 return BufferSetattr((BufferObject *)(self), name, val);
1497}
1498
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001499//////////////////
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001500
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001501 static PyObject *
1502BufferSubscript(PyObject *self, PyObject* idx)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001503{
Bram Moolenaardb913952012-06-29 12:54:53 +02001504 if (PyLong_Check(idx))
1505 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001506 long _idx = PyLong_AsLong(idx);
Bram Moolenaard6e39182013-05-21 18:30:34 +02001507 return BufferItem((BufferObject *)(self), _idx);
Bram Moolenaardb913952012-06-29 12:54:53 +02001508 } else if (PySlice_Check(idx))
1509 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001510 Py_ssize_t start, stop, step, slicelen;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001511
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02001512 if (CheckBuffer((BufferObject *) self))
1513 return NULL;
1514
Bram Moolenaar922a4662014-03-30 16:11:43 +02001515 if (PySlice_GetIndicesEx((PySliceObject_T *)idx,
Bram Moolenaarbd80f352013-05-12 21:16:23 +02001516 (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count,
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001517 &start, &stop,
Bram Moolenaardb913952012-06-29 12:54:53 +02001518 &step, &slicelen) < 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001519 return NULL;
Bram Moolenaard6e39182013-05-21 18:30:34 +02001520 return BufferSlice((BufferObject *)(self), start, stop);
Bram Moolenaardb913952012-06-29 12:54:53 +02001521 }
1522 else
1523 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001524 RAISE_INVALID_INDEX_TYPE(idx);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001525 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001526 }
1527}
1528
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02001529 static int
Bram Moolenaar19e60942011-06-19 00:27:51 +02001530BufferAsSubscript(PyObject *self, PyObject* idx, PyObject* val)
1531{
Bram Moolenaardb913952012-06-29 12:54:53 +02001532 if (PyLong_Check(idx))
1533 {
Bram Moolenaar19e60942011-06-19 00:27:51 +02001534 long n = PyLong_AsLong(idx);
Bram Moolenaarab589462020-07-06 21:03:06 +02001535
1536 if (CheckBuffer((BufferObject *) self))
1537 return -1;
1538
Bram Moolenaar19e60942011-06-19 00:27:51 +02001539 return RBAsItem((BufferObject *)(self), n, val, 1,
1540 (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count,
1541 NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02001542 } else if (PySlice_Check(idx))
1543 {
Bram Moolenaar19e60942011-06-19 00:27:51 +02001544 Py_ssize_t start, stop, step, slicelen;
1545
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02001546 if (CheckBuffer((BufferObject *) self))
1547 return -1;
1548
Bram Moolenaar922a4662014-03-30 16:11:43 +02001549 if (PySlice_GetIndicesEx((PySliceObject_T *)idx,
Bram Moolenaarbd80f352013-05-12 21:16:23 +02001550 (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count,
Bram Moolenaar19e60942011-06-19 00:27:51 +02001551 &start, &stop,
Bram Moolenaardb913952012-06-29 12:54:53 +02001552 &step, &slicelen) < 0)
Bram Moolenaar19e60942011-06-19 00:27:51 +02001553 return -1;
Bram Moolenaar19e60942011-06-19 00:27:51 +02001554 return RBAsSlice((BufferObject *)(self), start, stop, val, 1,
1555 (PyInt)((BufferObject *)(self))->buf->b_ml.ml_line_count,
1556 NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02001557 }
1558 else
1559 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001560 RAISE_INVALID_INDEX_TYPE(idx);
Bram Moolenaar19e60942011-06-19 00:27:51 +02001561 return -1;
1562 }
1563}
1564
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001565static PySequenceMethods RangeAsSeq = {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001566 (lenfunc) RangeLength, // sq_length, len(x)
1567 (binaryfunc) 0, // RangeConcat, sq_concat, x+y
1568 (ssizeargfunc) 0, // RangeRepeat, sq_repeat, x*n
1569 (ssizeargfunc) RangeItem, // sq_item, x[i]
1570 0, // was_sq_slice, x[i:j]
1571 (ssizeobjargproc) RangeAsItem, // sq_as_item, x[i]=v
1572 0, // sq_ass_slice, x[i:j]=v
1573 0, // sq_contains
1574 0, // sq_inplace_concat
1575 0, // sq_inplace_repeat
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001576};
1577
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001578static PyMappingMethods RangeAsMapping = {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001579 /* mp_length */ (lenfunc)RangeLength,
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001580 /* mp_subscript */ (binaryfunc)RangeSubscript,
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001581 /* mp_ass_subscript */ (objobjargproc)RangeAsSubscript,
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001582};
1583
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001584// Line range object - Implementation
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001585
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001586 static PyObject *
1587RangeGetattro(PyObject *self, PyObject *nameobj)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001588{
Bram Moolenaar77045652012-09-21 13:46:06 +02001589 GET_ATTR_STRING(name, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001590
1591 if (strcmp(name, "start") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001592 return Py_BuildValue("n", ((RangeObject *)(self))->start - 1);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001593 else if (strcmp(name, "end") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001594 return Py_BuildValue("n", ((RangeObject *)(self))->end - 1);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001595 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001596 return PyObject_GenericGetAttr(self, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001597}
1598
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001599////////////////
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001600
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02001601 static int
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001602RangeAsItem(PyObject *self, Py_ssize_t n, PyObject *val)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001603{
1604 return RBAsItem(((RangeObject *)(self))->buf, n, val,
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001605 ((RangeObject *)(self))->start,
1606 ((RangeObject *)(self))->end,
1607 &((RangeObject *)(self))->end);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001608}
1609
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001610 static Py_ssize_t
1611RangeAsSlice(PyObject *self, Py_ssize_t lo, Py_ssize_t hi, PyObject *val)
1612{
1613 return RBAsSlice(((RangeObject *)(self))->buf, lo, hi, val,
1614 ((RangeObject *)(self))->start,
1615 ((RangeObject *)(self))->end,
1616 &((RangeObject *)(self))->end);
1617}
1618
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001619 static PyObject *
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001620RangeSubscript(PyObject *self, PyObject* idx)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001621{
Bram Moolenaardb913952012-06-29 12:54:53 +02001622 if (PyLong_Check(idx))
1623 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001624 long _idx = PyLong_AsLong(idx);
Bram Moolenaard6e39182013-05-21 18:30:34 +02001625 return RangeItem((RangeObject *)(self), _idx);
Bram Moolenaardb913952012-06-29 12:54:53 +02001626 } else if (PySlice_Check(idx))
1627 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001628 Py_ssize_t start, stop, step, slicelen;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001629
Bram Moolenaar922a4662014-03-30 16:11:43 +02001630 if (PySlice_GetIndicesEx((PySliceObject_T *)idx,
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001631 ((RangeObject *)(self))->end-((RangeObject *)(self))->start+1,
1632 &start, &stop,
Bram Moolenaardb913952012-06-29 12:54:53 +02001633 &step, &slicelen) < 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001634 return NULL;
Bram Moolenaard6e39182013-05-21 18:30:34 +02001635 return RangeSlice((RangeObject *)(self), start, stop);
Bram Moolenaardb913952012-06-29 12:54:53 +02001636 }
1637 else
1638 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001639 RAISE_INVALID_INDEX_TYPE(idx);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001640 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001641 }
1642}
1643
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02001644 static int
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001645RangeAsSubscript(PyObject *self, PyObject *idx, PyObject *val)
1646{
Bram Moolenaardb913952012-06-29 12:54:53 +02001647 if (PyLong_Check(idx))
1648 {
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001649 long n = PyLong_AsLong(idx);
1650 return RangeAsItem(self, n, val);
Bram Moolenaarabab0b02019-03-30 18:47:01 +01001651 }
1652 else if (PySlice_Check(idx))
Bram Moolenaardb913952012-06-29 12:54:53 +02001653 {
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001654 Py_ssize_t start, stop, step, slicelen;
1655
Bram Moolenaar922a4662014-03-30 16:11:43 +02001656 if (PySlice_GetIndicesEx((PySliceObject_T *)idx,
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001657 ((RangeObject *)(self))->end-((RangeObject *)(self))->start+1,
1658 &start, &stop,
Bram Moolenaardb913952012-06-29 12:54:53 +02001659 &step, &slicelen) < 0)
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001660 return -1;
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001661 return RangeAsSlice(self, start, stop, val);
Bram Moolenaardb913952012-06-29 12:54:53 +02001662 }
1663 else
1664 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001665 RAISE_INVALID_INDEX_TYPE(idx);
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001666 return -1;
1667 }
1668}
1669
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001670// TabPage object - Implementation
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001671
1672 static PyObject *
1673TabPageGetattro(PyObject *self, PyObject *nameobj)
1674{
1675 PyObject *r;
1676
1677 GET_ATTR_STRING(name, nameobj);
1678
Bram Moolenaar9e822c02013-05-29 22:15:30 +02001679 if ((r = TabPageAttrValid((TabPageObject *)(self), name)))
1680 return r;
1681
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001682 if (CheckTabPage((TabPageObject *)(self)))
1683 return NULL;
1684
1685 r = TabPageAttr((TabPageObject *)(self), name);
1686 if (r || PyErr_Occurred())
1687 return r;
1688 else
1689 return PyObject_GenericGetAttr(self, nameobj);
1690}
1691
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001692// Window object - Implementation
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001693
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001694 static PyObject *
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001695WindowGetattro(PyObject *self, PyObject *nameobj)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001696{
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001697 PyObject *r;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001698
Bram Moolenaar77045652012-09-21 13:46:06 +02001699 GET_ATTR_STRING(name, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001700
Bram Moolenaar9e822c02013-05-29 22:15:30 +02001701 if ((r = WindowAttrValid((WindowObject *)(self), name)))
1702 return r;
1703
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001704 if (CheckWindow((WindowObject *)(self)))
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001705 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001706
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001707 r = WindowAttr((WindowObject *)(self), name);
1708 if (r || PyErr_Occurred())
1709 return r;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001710 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001711 return PyObject_GenericGetAttr(self, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001712}
1713
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001714 static int
1715WindowSetattro(PyObject *self, PyObject *nameobj, PyObject *val)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001716{
Bram Moolenaar77045652012-09-21 13:46:06 +02001717 GET_ATTR_STRING(name, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001718
Bram Moolenaard6e39182013-05-21 18:30:34 +02001719 return WindowSetattr((WindowObject *)(self), name, val);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001720}
1721
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001722// Tab page list object - Definitions
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001723
1724static PySequenceMethods TabListAsSeq = {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001725 (lenfunc) TabListLength, // sq_length, len(x)
1726 (binaryfunc) 0, // sq_concat, x+y
1727 (ssizeargfunc) 0, // sq_repeat, x*n
1728 (ssizeargfunc) TabListItem, // sq_item, x[i]
1729 0, // sq_slice, x[i:j]
1730 (ssizeobjargproc)0, // sq_as_item, x[i]=v
1731 0, // sq_ass_slice, x[i:j]=v
1732 0, // sq_contains
1733 0, // sq_inplace_concat
1734 0, // sq_inplace_repeat
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001735};
1736
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001737// Window list object - Definitions
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001738
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001739static PySequenceMethods WinListAsSeq = {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001740 (lenfunc) WinListLength, // sq_length, len(x)
1741 (binaryfunc) 0, // sq_concat, x+y
1742 (ssizeargfunc) 0, // sq_repeat, x*n
1743 (ssizeargfunc) WinListItem, // sq_item, x[i]
1744 0, // sq_slice, x[i:j]
1745 (ssizeobjargproc)0, // sq_as_item, x[i]=v
1746 0, // sq_ass_slice, x[i:j]=v
1747 0, // sq_contains
1748 0, // sq_inplace_concat
1749 0, // sq_inplace_repeat
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001750};
1751
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001752/*
1753 * Current items object - Implementation
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001754 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001755 static PyObject *
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001756CurrentGetattro(PyObject *self, PyObject *nameobj)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001757{
Bram Moolenaardd8aca62013-05-29 22:36:10 +02001758 PyObject *r;
Bram Moolenaar77045652012-09-21 13:46:06 +02001759 GET_ATTR_STRING(name, nameobj);
Bram Moolenaardd8aca62013-05-29 22:36:10 +02001760 if (!(r = CurrentGetattr(self, name)))
1761 return PyObject_GenericGetAttr(self, nameobj);
1762 return r;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001763}
1764
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001765 static int
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001766CurrentSetattro(PyObject *self, PyObject *nameobj, PyObject *value)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001767{
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001768 GET_ATTR_STRING(name, nameobj);
1769 return CurrentSetattr(self, name, value);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001770}
1771
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001772// Dictionary object - Definitions
Bram Moolenaardb913952012-06-29 12:54:53 +02001773
Bram Moolenaar66b79852012-09-21 14:00:35 +02001774 static PyObject *
1775DictionaryGetattro(PyObject *self, PyObject *nameobj)
1776{
1777 DictionaryObject *this = ((DictionaryObject *) (self));
1778
1779 GET_ATTR_STRING(name, nameobj);
1780
1781 if (strcmp(name, "locked") == 0)
1782 return PyLong_FromLong(this->dict->dv_lock);
1783 else if (strcmp(name, "scope") == 0)
1784 return PyLong_FromLong(this->dict->dv_scope);
1785
1786 return PyObject_GenericGetAttr(self, nameobj);
1787}
1788
1789 static int
1790DictionarySetattro(PyObject *self, PyObject *nameobj, PyObject *val)
1791{
1792 GET_ATTR_STRING(name, nameobj);
Bram Moolenaard6e39182013-05-21 18:30:34 +02001793 return DictionarySetattr((DictionaryObject *)(self), name, val);
Bram Moolenaardb913952012-06-29 12:54:53 +02001794}
1795
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001796// List object - Definitions
Bram Moolenaardb913952012-06-29 12:54:53 +02001797
Bram Moolenaar66b79852012-09-21 14:00:35 +02001798 static PyObject *
1799ListGetattro(PyObject *self, PyObject *nameobj)
1800{
1801 GET_ATTR_STRING(name, nameobj);
1802
1803 if (strcmp(name, "locked") == 0)
1804 return PyLong_FromLong(((ListObject *) (self))->list->lv_lock);
1805
1806 return PyObject_GenericGetAttr(self, nameobj);
1807}
1808
1809 static int
1810ListSetattro(PyObject *self, PyObject *nameobj, PyObject *val)
1811{
1812 GET_ATTR_STRING(name, nameobj);
Bram Moolenaard6e39182013-05-21 18:30:34 +02001813 return ListSetattr((ListObject *)(self), name, val);
Bram Moolenaardb913952012-06-29 12:54:53 +02001814}
1815
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001816// Function object - Definitions
Bram Moolenaardb913952012-06-29 12:54:53 +02001817
Bram Moolenaardb913952012-06-29 12:54:53 +02001818 static PyObject *
1819FunctionGetattro(PyObject *self, PyObject *nameobj)
1820{
Bram Moolenaar8110a092016-04-14 15:56:09 +02001821 PyObject *r;
Bram Moolenaardb913952012-06-29 12:54:53 +02001822 FunctionObject *this = (FunctionObject *)(self);
Bram Moolenaar77045652012-09-21 13:46:06 +02001823
1824 GET_ATTR_STRING(name, nameobj);
Bram Moolenaardb913952012-06-29 12:54:53 +02001825
Bram Moolenaar8110a092016-04-14 15:56:09 +02001826 r = FunctionAttr(this, name);
1827 if (r || PyErr_Occurred())
1828 return r;
1829 else
1830 return PyObject_GenericGetAttr(self, nameobj);
Bram Moolenaardb913952012-06-29 12:54:53 +02001831}
1832
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001833// External interface
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001834
1835 void
1836python3_buffer_free(buf_T *buf)
1837{
Yegappan Lakshmanan0233bdf2023-01-12 12:33:30 +00001838 BufferObject *bp = BUF_PYTHON_REF(buf);
1839 if (bp == NULL)
1840 return;
1841 bp->buf = INVALID_BUFFER_VALUE;
1842 BUF_PYTHON_REF(buf) = NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001843}
1844
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001845 void
1846python3_window_free(win_T *win)
1847{
Yegappan Lakshmanan0233bdf2023-01-12 12:33:30 +00001848 WindowObject *wp = WIN_PYTHON_REF(win);
1849 if (wp == NULL)
1850 return;
1851 wp->win = INVALID_WINDOW_VALUE;
1852 WIN_PYTHON_REF(win) = NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001853}
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001854
1855 void
1856python3_tabpage_free(tabpage_T *tab)
1857{
Yegappan Lakshmanan0233bdf2023-01-12 12:33:30 +00001858 TabPageObject *tp = TAB_PYTHON_REF(tab);
1859 if (tp == NULL)
1860 return;
1861 tp->tab = INVALID_TABPAGE_VALUE;
1862 TAB_PYTHON_REF(tab) = NULL;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001863}
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001864
Bram Moolenaar7854e3a2012-11-28 15:33:14 +01001865 static PyObject *
1866Py3Init_vim(void)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001867{
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001868 // The special value is removed from sys.path in Python3_Init().
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001869 static wchar_t *(argv[2]) = {L"/must>not&exist/foo", NULL};
1870
Bram Moolenaar1dc28782013-05-21 19:11:01 +02001871 if (init_types())
1872 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001873
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001874 // Set sys.argv[] to avoid a crash in warn().
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001875 PySys_SetArgv(1, argv);
1876
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001877 if ((vim_module = PyModule_Create(&vimmodule)) == NULL)
Bram Moolenaar19e60942011-06-19 00:27:51 +02001878 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001879
Bram Moolenaardee2e312013-06-23 16:35:47 +02001880 if (populate_module(vim_module))
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001881 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001882
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001883 if (init_sys_path())
1884 return NULL;
1885
1886 return vim_module;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001887}
1888
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001889//////////////////////////////////////////////////////////////////////////
1890// 4. Utility functions for handling the interface between Vim and Python.
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001891
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001892/*
1893 * Convert a Vim line into a Python string.
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001894 * All internal newlines are replaced by null characters.
1895 *
1896 * On errors, the Python exception data is set, and NULL is returned.
1897 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001898 static PyObject *
1899LineToString(const char *str)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001900{
1901 PyObject *result;
1902 Py_ssize_t len = strlen(str);
Bram Moolenaard672dde2020-02-26 13:43:51 +01001903 char *tmp, *p;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001904
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001905 tmp = alloc(len + 1);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001906 p = tmp;
1907 if (p == NULL)
1908 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001909 PyErr_NoMemory();
1910 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001911 }
1912
1913 while (*str)
1914 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001915 if (*str == '\n')
1916 *p = '\0';
1917 else
1918 *p = *str;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001919
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001920 ++p;
1921 ++str;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001922 }
1923 *p = '\0';
1924
Bram Moolenaar2e2f52a2020-12-21 16:03:02 +01001925 result = PyUnicode_Decode(tmp, len, (char *)ENC_OPT, ERRORS_DECODE_ARG);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001926
1927 vim_free(tmp);
1928 return result;
1929}
1930
Bram Moolenaardb913952012-06-29 12:54:53 +02001931 void
Bram Moolenaar8e9a24a2019-03-19 22:22:55 +01001932do_py3eval(char_u *str, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +02001933{
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02001934 DoPyCommand((char *) str,
1935 (rangeinitializer) init_range_eval,
1936 (runner) run_eval,
1937 (void *) rettv);
Bram Moolenaar8e9a24a2019-03-19 22:22:55 +01001938 if (rettv->v_type == VAR_UNKNOWN)
Bram Moolenaardb913952012-06-29 12:54:53 +02001939 {
Bram Moolenaar8e9a24a2019-03-19 22:22:55 +01001940 rettv->v_type = VAR_NUMBER;
1941 rettv->vval.v_number = 0;
Bram Moolenaardb913952012-06-29 12:54:53 +02001942 }
1943}
1944
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01001945 int
Bram Moolenaar8e9a24a2019-03-19 22:22:55 +01001946set_ref_in_python3(int copyID)
Bram Moolenaardb913952012-06-29 12:54:53 +02001947{
Bram Moolenaarb641df42015-02-03 13:16:04 +01001948 return set_ref_in_py(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02001949}