blob: 188e9c3e6d6645593db211dd267b0d102ee33af0 [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
Bram Moolenaar2ab2e862019-12-04 21:24:53 +010084// Python 3 does not support CObjects, always use Capsules
Bram Moolenaar2afa3232012-06-29 16:28:28 +020085#define PY_USE_CAPSULE
86
Bram Moolenaar2e2f52a2020-12-21 16:03:02 +010087#define ERRORS_DECODE_ARG CODEC_ERROR_HANDLER
88#define ERRORS_ENCODE_ARG ERRORS_DECODE_ARG
89
Bram Moolenaar170bf1a2010-07-24 23:51:45 +020090#define PyInt Py_ssize_t
Bram Moolenaar32ac8cd2013-07-03 18:49:17 +020091#ifndef PyString_Check
92# define PyString_Check(obj) PyUnicode_Check(obj)
93#endif
Bram Moolenaare0324612013-07-09 17:30:55 +020094#define PyString_FromString(repr) \
Bram Moolenaar2e2f52a2020-12-21 16:03:02 +010095 PyUnicode_Decode(repr, STRLEN(repr), ENC_OPT, ERRORS_DECODE_ARG)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +020096#define PyString_FromFormat PyUnicode_FromFormat
Bram Moolenaar32ac8cd2013-07-03 18:49:17 +020097#ifndef PyInt_Check
98# define PyInt_Check(obj) PyLong_Check(obj)
99#endif
Bram Moolenaar77045652012-09-21 13:46:06 +0200100#define PyInt_FromLong(i) PyLong_FromLong(i)
101#define PyInt_AsLong(obj) PyLong_AsLong(obj)
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200102#define Py_ssize_t_fmt "n"
Bram Moolenaara9922d62013-05-30 13:01:18 +0200103#define Py_bytes_fmt "y"
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200104
Bram Moolenaar063a46b2014-01-14 16:36:51 +0100105#define PyIntArgFunc ssizeargfunc
106#define PyIntObjArgProc ssizeobjargproc
107
Bram Moolenaar922a4662014-03-30 16:11:43 +0200108/*
109 * PySlice_GetIndicesEx(): first argument type changed from PySliceObject
110 * to PyObject in Python 3.2 or later.
111 */
112#if PY_VERSION_HEX >= 0x030200f0
113typedef PyObject PySliceObject_T;
114#else
115typedef PySliceObject PySliceObject_T;
116#endif
117
Bram Moolenaar63ff72a2022-02-07 13:54:01 +0000118#ifndef MSWIN
119# define HINSTANCE void *
120#endif
121#if defined(DYNAMIC_PYTHON3) || defined(MSWIN)
122static HINSTANCE hinstPy3 = 0; // Instance of python.dll
123#endif
124
Bram Moolenaar0c1f3f42011-02-25 15:18:50 +0100125#if defined(DYNAMIC_PYTHON3) || defined(PROTO)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200126
Bram Moolenaar4f974752019-02-17 17:44:42 +0100127# ifndef MSWIN
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200128# include <dlfcn.h>
129# define FARPROC void*
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100130# if defined(PY_NO_RTLD_GLOBAL) && defined(PY3_NO_RTLD_GLOBAL)
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200131# define load_dll(n) dlopen((n), RTLD_LAZY)
132# else
133# define load_dll(n) dlopen((n), RTLD_LAZY|RTLD_GLOBAL)
134# endif
135# define close_dll dlclose
136# define symbol_from_dll dlsym
Martin Tournoij1a3e5742021-07-24 13:57:29 +0200137# define load_dll_error dlerror
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200138# else
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200139# define load_dll vimLoadLib
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200140# define close_dll FreeLibrary
141# define symbol_from_dll GetProcAddress
Martin Tournoij1a3e5742021-07-24 13:57:29 +0200142# define load_dll_error GetWin32Error
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200143# endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200144/*
145 * Wrapper defines
146 */
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200147# undef PyArg_Parse
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200148# define PyArg_Parse py3_PyArg_Parse
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200149# undef PyArg_ParseTuple
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200150# define PyArg_ParseTuple py3_PyArg_ParseTuple
Bram Moolenaar19e60942011-06-19 00:27:51 +0200151# define PyMem_Free py3_PyMem_Free
Bram Moolenaardb913952012-06-29 12:54:53 +0200152# define PyMem_Malloc py3_PyMem_Malloc
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200153# define PyDict_SetItemString py3_PyDict_SetItemString
154# define PyErr_BadArgument py3_PyErr_BadArgument
155# define PyErr_Clear py3_PyErr_Clear
Bram Moolenaarc476e522013-06-23 13:46:40 +0200156# define PyErr_Format py3_PyErr_Format
Bram Moolenaar4d369872013-02-20 16:09:43 +0100157# define PyErr_PrintEx py3_PyErr_PrintEx
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200158# define PyErr_NoMemory py3_PyErr_NoMemory
159# define PyErr_Occurred py3_PyErr_Occurred
160# define PyErr_SetNone py3_PyErr_SetNone
161# define PyErr_SetString py3_PyErr_SetString
Bram Moolenaar4d188da2013-05-15 15:35:09 +0200162# define PyErr_SetObject py3_PyErr_SetObject
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200163# define PyErr_ExceptionMatches py3_PyErr_ExceptionMatches
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200164# define PyEval_InitThreads py3_PyEval_InitThreads
165# define PyEval_RestoreThread py3_PyEval_RestoreThread
166# define PyEval_SaveThread py3_PyEval_SaveThread
167# define PyGILState_Ensure py3_PyGILState_Ensure
168# define PyGILState_Release py3_PyGILState_Release
169# define PyLong_AsLong py3_PyLong_AsLong
170# define PyLong_FromLong py3_PyLong_FromLong
171# define PyList_GetItem py3_PyList_GetItem
172# define PyList_Append py3_PyList_Append
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200173# define PyList_Insert py3_PyList_Insert
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200174# define PyList_New py3_PyList_New
175# define PyList_SetItem py3_PyList_SetItem
176# define PyList_Size py3_PyList_Size
Bram Moolenaardb913952012-06-29 12:54:53 +0200177# define PySequence_Check py3_PySequence_Check
178# define PySequence_Size py3_PySequence_Size
179# define PySequence_GetItem py3_PySequence_GetItem
Bram Moolenaara9922d62013-05-30 13:01:18 +0200180# define PySequence_Fast py3_PySequence_Fast
Bram Moolenaardb913952012-06-29 12:54:53 +0200181# define PyTuple_Size py3_PyTuple_Size
182# define PyTuple_GetItem py3_PyTuple_GetItem
Bram Moolenaar3b48b112018-07-04 22:03:25 +0200183# if PY_VERSION_HEX >= 0x030601f0
184# define PySlice_AdjustIndices py3_PySlice_AdjustIndices
185# define PySlice_Unpack py3_PySlice_Unpack
186# endif
187# undef PySlice_GetIndicesEx
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200188# define PySlice_GetIndicesEx py3_PySlice_GetIndicesEx
189# define PyImport_ImportModule py3_PyImport_ImportModule
190# define PyObject_Init py3__PyObject_Init
191# define PyDict_New py3_PyDict_New
192# define PyDict_GetItemString py3_PyDict_GetItemString
Bram Moolenaardb913952012-06-29 12:54:53 +0200193# define PyDict_Next py3_PyDict_Next
194# define PyMapping_Check py3_PyMapping_Check
Bram Moolenaar32ac8cd2013-07-03 18:49:17 +0200195# ifndef PyMapping_Keys
196# define PyMapping_Keys py3_PyMapping_Keys
197# endif
Zdenek Dohnal90478f32021-06-14 15:08:30 +0200198# if PY_VERSION_HEX >= 0x030a00b2
199# define PyIter_Check py3_PyIter_Check
200# endif
Bram Moolenaardb913952012-06-29 12:54:53 +0200201# define PyIter_Next py3_PyIter_Next
202# define PyObject_GetIter py3_PyObject_GetIter
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200203# define PyObject_Repr py3_PyObject_Repr
Bram Moolenaarbcb40972013-05-30 13:22:13 +0200204# define PyObject_GetItem py3_PyObject_GetItem
Bram Moolenaar03db85b2013-05-15 14:51:35 +0200205# define PyObject_IsTrue py3_PyObject_IsTrue
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200206# define PyModule_GetDict py3_PyModule_GetDict
207#undef PyRun_SimpleString
208# define PyRun_SimpleString py3_PyRun_SimpleString
Bram Moolenaardb913952012-06-29 12:54:53 +0200209#undef PyRun_String
210# define PyRun_String py3_PyRun_String
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200211# define PyObject_GetAttrString py3_PyObject_GetAttrString
Bram Moolenaara9922d62013-05-30 13:01:18 +0200212# define PyObject_HasAttrString py3_PyObject_HasAttrString
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200213# define PyObject_SetAttrString py3_PyObject_SetAttrString
214# define PyObject_CallFunctionObjArgs py3_PyObject_CallFunctionObjArgs
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200215# define _PyObject_CallFunction_SizeT py3__PyObject_CallFunction_SizeT
Bram Moolenaarf4258302013-06-02 18:20:17 +0200216# define PyObject_Call py3_PyObject_Call
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200217# define PyEval_GetLocals py3_PyEval_GetLocals
218# define PyEval_GetGlobals py3_PyEval_GetGlobals
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200219# define PySys_SetObject py3_PySys_SetObject
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200220# define PySys_GetObject py3_PySys_GetObject
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200221# define PySys_SetArgv py3_PySys_SetArgv
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200222# define PyType_Ready py3_PyType_Ready
Bram Moolenaaree1b9312020-07-16 22:15:53 +0200223# if PY_VERSION_HEX >= 0x030900b0
224# define PyType_GetFlags py3_PyType_GetFlags
225# endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200226#undef Py_BuildValue
227# define Py_BuildValue py3_Py_BuildValue
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100228# define Py_SetPythonHome py3_Py_SetPythonHome
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200229# define Py_Initialize py3_Py_Initialize
230# define Py_Finalize py3_Py_Finalize
231# define Py_IsInitialized py3_Py_IsInitialized
232# define _Py_NoneStruct (*py3__Py_NoneStruct)
Bram Moolenaar66b79852012-09-21 14:00:35 +0200233# define _Py_FalseStruct (*py3__Py_FalseStruct)
234# define _Py_TrueStruct (*py3__Py_TrueStruct)
Bram Moolenaardb913952012-06-29 12:54:53 +0200235# define _PyObject_NextNotImplemented (*py3__PyObject_NextNotImplemented)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200236# define PyModule_AddObject py3_PyModule_AddObject
237# define PyImport_AppendInittab py3_PyImport_AppendInittab
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200238# define PyImport_AddModule py3_PyImport_AddModule
Bram Moolenaar7bc4f932012-10-14 03:22:56 +0200239# if PY_VERSION_HEX >= 0x030300f0
240# undef _PyUnicode_AsString
Bram Moolenaar9c9cbf12012-10-23 05:17:37 +0200241# define _PyUnicode_AsString py3_PyUnicode_AsUTF8
Bram Moolenaar7bc4f932012-10-14 03:22:56 +0200242# else
243# define _PyUnicode_AsString py3__PyUnicode_AsString
244# endif
Bram Moolenaar19e60942011-06-19 00:27:51 +0200245# undef PyUnicode_AsEncodedString
246# define PyUnicode_AsEncodedString py3_PyUnicode_AsEncodedString
247# undef PyBytes_AsString
248# define PyBytes_AsString py3_PyBytes_AsString
Bram Moolenaar32ac8cd2013-07-03 18:49:17 +0200249# ifndef PyBytes_AsStringAndSize
250# define PyBytes_AsStringAndSize py3_PyBytes_AsStringAndSize
251# endif
Bram Moolenaardb913952012-06-29 12:54:53 +0200252# undef PyBytes_FromString
253# define PyBytes_FromString py3_PyBytes_FromString
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100254# undef PyBytes_FromStringAndSize
255# define PyBytes_FromStringAndSize py3_PyBytes_FromStringAndSize
Bram Moolenaaree1b9312020-07-16 22:15:53 +0200256# if defined(Py_DEBUG) || PY_VERSION_HEX >= 0x030900b0
257# define _Py_Dealloc py3__Py_Dealloc
258# endif
Bram Moolenaardb913952012-06-29 12:54:53 +0200259# define PyFloat_FromDouble py3_PyFloat_FromDouble
260# define PyFloat_AsDouble py3_PyFloat_AsDouble
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200261# define PyObject_GenericGetAttr py3_PyObject_GenericGetAttr
Bram Moolenaar66b79852012-09-21 14:00:35 +0200262# define PyType_Type (*py3_PyType_Type)
Bram Moolenaard4a8c982018-05-15 22:31:18 +0200263# define PyStdPrinter_Type (*py3_PyStdPrinter_Type)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200264# define PySlice_Type (*py3_PySlice_Type)
Bram Moolenaardb913952012-06-29 12:54:53 +0200265# define PyFloat_Type (*py3_PyFloat_Type)
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200266# define PyNumber_Check (*py3_PyNumber_Check)
267# define PyNumber_Long (*py3_PyNumber_Long)
Bram Moolenaar66b79852012-09-21 14:00:35 +0200268# define PyBool_Type (*py3_PyBool_Type)
Bram Moolenaar19e60942011-06-19 00:27:51 +0200269# define PyErr_NewException py3_PyErr_NewException
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200270# ifdef Py_DEBUG
271# define _Py_NegativeRefcount py3__Py_NegativeRefcount
272# define _Py_RefTotal (*py3__Py_RefTotal)
Bram Moolenaar0014a532013-05-29 21:33:39 +0200273# define PyModule_Create2TraceRefs py3_PyModule_Create2TraceRefs
274# else
275# define PyModule_Create2 py3_PyModule_Create2
276# endif
277# if defined(Py_DEBUG) && !defined(Py_DEBUG_NO_PYMALLOC)
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200278# define _PyObject_DebugMalloc py3__PyObject_DebugMalloc
279# define _PyObject_DebugFree py3__PyObject_DebugFree
280# else
281# define PyObject_Malloc py3_PyObject_Malloc
282# define PyObject_Free py3_PyObject_Free
283# endif
Bram Moolenaar774267b2013-05-21 20:51:59 +0200284# define _PyObject_GC_New py3__PyObject_GC_New
285# define PyObject_GC_Del py3_PyObject_GC_Del
286# define PyObject_GC_UnTrack py3_PyObject_GC_UnTrack
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200287# define PyType_GenericAlloc py3_PyType_GenericAlloc
288# define PyType_GenericNew py3_PyType_GenericNew
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200289# undef PyUnicode_FromString
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200290# define PyUnicode_FromString py3_PyUnicode_FromString
Bram Moolenaar1a3b5692013-05-30 12:40:39 +0200291# ifndef PyUnicode_FromFormat
292# define PyUnicode_FromFormat py3_PyUnicode_FromFormat
293# else
294# define Py_UNICODE_USE_UCS_FUNCTIONS
295# ifdef Py_UNICODE_WIDE
296# define PyUnicodeUCS4_FromFormat py3_PyUnicodeUCS4_FromFormat
297# else
298# define PyUnicodeUCS2_FromFormat py3_PyUnicodeUCS2_FromFormat
299# endif
300# endif
Bram Moolenaar19e60942011-06-19 00:27:51 +0200301# undef PyUnicode_Decode
302# define PyUnicode_Decode py3_PyUnicode_Decode
Bram Moolenaardb913952012-06-29 12:54:53 +0200303# define PyType_IsSubtype py3_PyType_IsSubtype
304# define PyCapsule_New py3_PyCapsule_New
305# define PyCapsule_GetPointer py3_PyCapsule_GetPointer
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200306
Bram Moolenaar0014a532013-05-29 21:33:39 +0200307# if defined(Py_DEBUG) && !defined(Py_DEBUG_NO_PYMALLOC)
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200308# undef PyObject_NEW
309# define PyObject_NEW(type, typeobj) \
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200310( (type *) PyObject_Init( \
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200311 (PyObject *) _PyObject_DebugMalloc( _PyObject_SIZE(typeobj) ), (typeobj)) )
Bram Moolenaaree1b9312020-07-16 22:15:53 +0200312# elif PY_VERSION_HEX >= 0x030900b0
313# undef PyObject_NEW
314# define PyObject_NEW(type, typeobj) \
315 ((type *)py3__PyObject_New(typeobj))
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200316# endif
317
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200318/*
319 * Pointers for dynamic link
320 */
321static int (*py3_PySys_SetArgv)(int, wchar_t **);
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100322static void (*py3_Py_SetPythonHome)(wchar_t *home);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200323static void (*py3_Py_Initialize)(void);
324static PyObject* (*py3_PyList_New)(Py_ssize_t size);
325static PyGILState_STATE (*py3_PyGILState_Ensure)(void);
326static void (*py3_PyGILState_Release)(PyGILState_STATE);
327static int (*py3_PySys_SetObject)(char *, PyObject *);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200328static PyObject* (*py3_PySys_GetObject)(char *);
329static int (*py3_PyList_Append)(PyObject *, PyObject *);
330static int (*py3_PyList_Insert)(PyObject *, int, PyObject *);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200331static Py_ssize_t (*py3_PyList_Size)(PyObject *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200332static int (*py3_PySequence_Check)(PyObject *);
333static Py_ssize_t (*py3_PySequence_Size)(PyObject *);
334static PyObject* (*py3_PySequence_GetItem)(PyObject *, Py_ssize_t);
Bram Moolenaara9922d62013-05-30 13:01:18 +0200335static PyObject* (*py3_PySequence_Fast)(PyObject *, const char *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200336static Py_ssize_t (*py3_PyTuple_Size)(PyObject *);
337static PyObject* (*py3_PyTuple_GetItem)(PyObject *, Py_ssize_t);
338static int (*py3_PyMapping_Check)(PyObject *);
Bram Moolenaarbcb40972013-05-30 13:22:13 +0200339static PyObject* (*py3_PyMapping_Keys)(PyObject *);
Bram Moolenaar3b48b112018-07-04 22:03:25 +0200340# if PY_VERSION_HEX >= 0x030601f0
341static int (*py3_PySlice_AdjustIndices)(Py_ssize_t length,
342 Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t step);
343static int (*py3_PySlice_Unpack)(PyObject *slice,
344 Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t *step);
345# endif
Bram Moolenaar922a4662014-03-30 16:11:43 +0200346static int (*py3_PySlice_GetIndicesEx)(PySliceObject_T *r, Py_ssize_t length,
Bram Moolenaar063a46b2014-01-14 16:36:51 +0100347 Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t *step,
348 Py_ssize_t *slicelen);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200349static PyObject* (*py3_PyErr_NoMemory)(void);
350static void (*py3_Py_Finalize)(void);
351static void (*py3_PyErr_SetString)(PyObject *, const char *);
Bram Moolenaar4d188da2013-05-15 15:35:09 +0200352static void (*py3_PyErr_SetObject)(PyObject *, PyObject *);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200353static int (*py3_PyErr_ExceptionMatches)(PyObject *);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200354static int (*py3_PyRun_SimpleString)(char *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200355static PyObject* (*py3_PyRun_String)(char *, int, PyObject *, PyObject *);
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200356static PyObject* (*py3_PyObject_GetAttrString)(PyObject *, const char *);
Bram Moolenaara9922d62013-05-30 13:01:18 +0200357static int (*py3_PyObject_HasAttrString)(PyObject *, const char *);
Bram Moolenaar0b400082013-11-03 00:28:25 +0100358static int (*py3_PyObject_SetAttrString)(PyObject *, const char *, PyObject *);
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200359static PyObject* (*py3_PyObject_CallFunctionObjArgs)(PyObject *, ...);
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200360static PyObject* (*py3__PyObject_CallFunction_SizeT)(PyObject *, char *, ...);
Bram Moolenaarf4258302013-06-02 18:20:17 +0200361static PyObject* (*py3_PyObject_Call)(PyObject *, PyObject *, PyObject *);
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200362static PyObject* (*py3_PyEval_GetGlobals)();
363static PyObject* (*py3_PyEval_GetLocals)();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200364static PyObject* (*py3_PyList_GetItem)(PyObject *, Py_ssize_t);
365static PyObject* (*py3_PyImport_ImportModule)(const char *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200366static PyObject* (*py3_PyImport_AddModule)(const char *);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200367static int (*py3_PyErr_BadArgument)(void);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200368static PyObject* (*py3_PyErr_Occurred)(void);
369static PyObject* (*py3_PyModule_GetDict)(PyObject *);
370static int (*py3_PyList_SetItem)(PyObject *, Py_ssize_t, PyObject *);
371static PyObject* (*py3_PyDict_GetItemString)(PyObject *, const char *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200372static int (*py3_PyDict_Next)(PyObject *, Py_ssize_t *, PyObject **, PyObject **);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200373static PyObject* (*py3_PyLong_FromLong)(long);
374static PyObject* (*py3_PyDict_New)(void);
Zdenek Dohnal90478f32021-06-14 15:08:30 +0200375# if PY_VERSION_HEX >= 0x030a00b2
376static int (*py3_PyIter_Check)(PyObject *o);
377# endif
Bram Moolenaardb913952012-06-29 12:54:53 +0200378static PyObject* (*py3_PyIter_Next)(PyObject *);
379static PyObject* (*py3_PyObject_GetIter)(PyObject *);
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200380static PyObject* (*py3_PyObject_Repr)(PyObject *);
Bram Moolenaarbcb40972013-05-30 13:22:13 +0200381static PyObject* (*py3_PyObject_GetItem)(PyObject *, PyObject *);
Bram Moolenaar03db85b2013-05-15 14:51:35 +0200382static int (*py3_PyObject_IsTrue)(PyObject *);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200383static PyObject* (*py3_Py_BuildValue)(char *, ...);
Bram Moolenaaree1b9312020-07-16 22:15:53 +0200384# if PY_VERSION_HEX >= 0x030900b0
385static int (*py3_PyType_GetFlags)(PyTypeObject *o);
386# endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200387static int (*py3_PyType_Ready)(PyTypeObject *type);
388static int (*py3_PyDict_SetItemString)(PyObject *dp, char *key, PyObject *item);
389static PyObject* (*py3_PyUnicode_FromString)(const char *u);
Bram Moolenaar1a3b5692013-05-30 12:40:39 +0200390# ifndef Py_UNICODE_USE_UCS_FUNCTIONS
391static PyObject* (*py3_PyUnicode_FromFormat)(const char *u, ...);
392# else
393# ifdef Py_UNICODE_WIDE
394static PyObject* (*py3_PyUnicodeUCS4_FromFormat)(const char *u, ...);
395# else
396static PyObject* (*py3_PyUnicodeUCS2_FromFormat)(const char *u, ...);
397# endif
398# endif
Bram Moolenaar19e60942011-06-19 00:27:51 +0200399static PyObject* (*py3_PyUnicode_Decode)(const char *u, Py_ssize_t size,
400 const char *encoding, const char *errors);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200401static long (*py3_PyLong_AsLong)(PyObject *);
402static void (*py3_PyErr_SetNone)(PyObject *);
403static void (*py3_PyEval_InitThreads)(void);
404static void(*py3_PyEval_RestoreThread)(PyThreadState *);
405static PyThreadState*(*py3_PyEval_SaveThread)(void);
406static int (*py3_PyArg_Parse)(PyObject *, char *, ...);
407static int (*py3_PyArg_ParseTuple)(PyObject *, char *, ...);
Bram Moolenaar19e60942011-06-19 00:27:51 +0200408static int (*py3_PyMem_Free)(void *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200409static void* (*py3_PyMem_Malloc)(size_t);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200410static int (*py3_Py_IsInitialized)(void);
411static void (*py3_PyErr_Clear)(void);
Bram Moolenaarc476e522013-06-23 13:46:40 +0200412static PyObject* (*py3_PyErr_Format)(PyObject *, const char *, ...);
Bram Moolenaar4d369872013-02-20 16:09:43 +0100413static void (*py3_PyErr_PrintEx)(int);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200414static PyObject*(*py3__PyObject_Init)(PyObject *, PyTypeObject *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200415static iternextfunc py3__PyObject_NextNotImplemented;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200416static PyObject* py3__Py_NoneStruct;
Bram Moolenaar66b79852012-09-21 14:00:35 +0200417static PyObject* py3__Py_FalseStruct;
418static PyObject* py3__Py_TrueStruct;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200419static int (*py3_PyModule_AddObject)(PyObject *m, const char *name, PyObject *o);
420static int (*py3_PyImport_AppendInittab)(const char *name, PyObject* (*initfunc)(void));
Bram Moolenaar9c9cbf12012-10-23 05:17:37 +0200421# if PY_VERSION_HEX >= 0x030300f0
422static char* (*py3_PyUnicode_AsUTF8)(PyObject *unicode);
423# else
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200424static char* (*py3__PyUnicode_AsString)(PyObject *unicode);
Bram Moolenaar9c9cbf12012-10-23 05:17:37 +0200425# endif
Bram Moolenaar19e60942011-06-19 00:27:51 +0200426static PyObject* (*py3_PyUnicode_AsEncodedString)(PyObject *unicode, const char* encoding, const char* errors);
427static char* (*py3_PyBytes_AsString)(PyObject *bytes);
Bram Moolenaar808c2bc2013-06-23 13:11:18 +0200428static int (*py3_PyBytes_AsStringAndSize)(PyObject *bytes, char **buffer, Py_ssize_t *length);
Bram Moolenaardb913952012-06-29 12:54:53 +0200429static PyObject* (*py3_PyBytes_FromString)(char *str);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100430static PyObject* (*py3_PyBytes_FromStringAndSize)(char *str, Py_ssize_t length);
Bram Moolenaaree1b9312020-07-16 22:15:53 +0200431# if defined(Py_DEBUG) || PY_VERSION_HEX >= 0x030900b0
432static void (*py3__Py_Dealloc)(PyObject *obj);
433# endif
434# if PY_VERSION_HEX >= 0x030900b0
435static PyObject* (*py3__PyObject_New)(PyTypeObject *);
436# endif
Bram Moolenaardb913952012-06-29 12:54:53 +0200437static PyObject* (*py3_PyFloat_FromDouble)(double num);
438static double (*py3_PyFloat_AsDouble)(PyObject *);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200439static PyObject* (*py3_PyObject_GenericGetAttr)(PyObject *obj, PyObject *name);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200440static PyObject* (*py3_PyType_GenericAlloc)(PyTypeObject *type, Py_ssize_t nitems);
441static PyObject* (*py3_PyType_GenericNew)(PyTypeObject *type, PyObject *args, PyObject *kwds);
Bram Moolenaar66b79852012-09-21 14:00:35 +0200442static PyTypeObject* py3_PyType_Type;
Bram Moolenaard4a8c982018-05-15 22:31:18 +0200443static PyTypeObject* py3_PyStdPrinter_Type;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200444static PyTypeObject* py3_PySlice_Type;
Bram Moolenaardb913952012-06-29 12:54:53 +0200445static PyTypeObject* py3_PyFloat_Type;
Bram Moolenaar66b79852012-09-21 14:00:35 +0200446static PyTypeObject* py3_PyBool_Type;
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200447static int (*py3_PyNumber_Check)(PyObject *);
448static PyObject* (*py3_PyNumber_Long)(PyObject *);
Bram Moolenaar19e60942011-06-19 00:27:51 +0200449static PyObject* (*py3_PyErr_NewException)(char *name, PyObject *base, PyObject *dict);
Bram Moolenaardb913952012-06-29 12:54:53 +0200450static PyObject* (*py3_PyCapsule_New)(void *, char *, PyCapsule_Destructor);
451static void* (*py3_PyCapsule_GetPointer)(PyObject *, char *);
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200452# ifdef Py_DEBUG
Bram Moolenaar0014a532013-05-29 21:33:39 +0200453static void (*py3__Py_NegativeRefcount)(const char *fname, int lineno, PyObject *op);
454static Py_ssize_t* py3__Py_RefTotal;
Bram Moolenaar0014a532013-05-29 21:33:39 +0200455static PyObject* (*py3_PyModule_Create2TraceRefs)(struct PyModuleDef* module, int module_api_version);
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200456# else
Bram Moolenaar0014a532013-05-29 21:33:39 +0200457static PyObject* (*py3_PyModule_Create2)(struct PyModuleDef* module, int module_api_version);
458# endif
459# if defined(Py_DEBUG) && !defined(Py_DEBUG_NO_PYMALLOC)
460static void (*py3__PyObject_DebugFree)(void*);
461static void* (*py3__PyObject_DebugMalloc)(size_t);
462# else
463static void (*py3_PyObject_Free)(void*);
464static void* (*py3_PyObject_Malloc)(size_t);
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200465# endif
Bram Moolenaar774267b2013-05-21 20:51:59 +0200466static PyObject*(*py3__PyObject_GC_New)(PyTypeObject *);
467static void(*py3_PyObject_GC_Del)(void *);
468static void(*py3_PyObject_GC_UnTrack)(void *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200469static int (*py3_PyType_IsSubtype)(PyTypeObject *, PyTypeObject *);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200470
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100471// Imported exception objects
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200472static PyObject *p3imp_PyExc_AttributeError;
473static PyObject *p3imp_PyExc_IndexError;
Bram Moolenaaraf6abb92013-04-24 13:04:26 +0200474static PyObject *p3imp_PyExc_KeyError;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200475static PyObject *p3imp_PyExc_KeyboardInterrupt;
476static PyObject *p3imp_PyExc_TypeError;
477static PyObject *p3imp_PyExc_ValueError;
Bram Moolenaar41009372013-07-01 22:03:04 +0200478static PyObject *p3imp_PyExc_SystemExit;
Bram Moolenaar8661b172013-05-15 15:44:28 +0200479static PyObject *p3imp_PyExc_RuntimeError;
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200480static PyObject *p3imp_PyExc_ImportError;
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200481static PyObject *p3imp_PyExc_OverflowError;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200482
483# define PyExc_AttributeError p3imp_PyExc_AttributeError
484# define PyExc_IndexError p3imp_PyExc_IndexError
Bram Moolenaaraf6abb92013-04-24 13:04:26 +0200485# define PyExc_KeyError p3imp_PyExc_KeyError
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200486# define PyExc_KeyboardInterrupt p3imp_PyExc_KeyboardInterrupt
487# define PyExc_TypeError p3imp_PyExc_TypeError
488# define PyExc_ValueError p3imp_PyExc_ValueError
Bram Moolenaar41009372013-07-01 22:03:04 +0200489# define PyExc_SystemExit p3imp_PyExc_SystemExit
Bram Moolenaar8661b172013-05-15 15:44:28 +0200490# define PyExc_RuntimeError p3imp_PyExc_RuntimeError
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200491# define PyExc_ImportError p3imp_PyExc_ImportError
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200492# define PyExc_OverflowError p3imp_PyExc_OverflowError
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200493
494/*
495 * Table of name to function pointer of python.
496 */
497# define PYTHON_PROC FARPROC
498static struct
499{
500 char *name;
501 PYTHON_PROC *ptr;
502} py3_funcname_table[] =
503{
504 {"PySys_SetArgv", (PYTHON_PROC*)&py3_PySys_SetArgv},
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100505 {"Py_SetPythonHome", (PYTHON_PROC*)&py3_Py_SetPythonHome},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200506 {"Py_Initialize", (PYTHON_PROC*)&py3_Py_Initialize},
Bram Moolenaare8cdcef2012-09-12 20:21:43 +0200507 {"_PyArg_ParseTuple_SizeT", (PYTHON_PROC*)&py3_PyArg_ParseTuple},
508 {"_Py_BuildValue_SizeT", (PYTHON_PROC*)&py3_Py_BuildValue},
Bram Moolenaar19e60942011-06-19 00:27:51 +0200509 {"PyMem_Free", (PYTHON_PROC*)&py3_PyMem_Free},
Bram Moolenaardb913952012-06-29 12:54:53 +0200510 {"PyMem_Malloc", (PYTHON_PROC*)&py3_PyMem_Malloc},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200511 {"PyList_New", (PYTHON_PROC*)&py3_PyList_New},
512 {"PyGILState_Ensure", (PYTHON_PROC*)&py3_PyGILState_Ensure},
513 {"PyGILState_Release", (PYTHON_PROC*)&py3_PyGILState_Release},
514 {"PySys_SetObject", (PYTHON_PROC*)&py3_PySys_SetObject},
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200515 {"PySys_GetObject", (PYTHON_PROC*)&py3_PySys_GetObject},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200516 {"PyList_Append", (PYTHON_PROC*)&py3_PyList_Append},
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200517 {"PyList_Insert", (PYTHON_PROC*)&py3_PyList_Insert},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200518 {"PyList_Size", (PYTHON_PROC*)&py3_PyList_Size},
Bram Moolenaardb913952012-06-29 12:54:53 +0200519 {"PySequence_Check", (PYTHON_PROC*)&py3_PySequence_Check},
520 {"PySequence_Size", (PYTHON_PROC*)&py3_PySequence_Size},
521 {"PySequence_GetItem", (PYTHON_PROC*)&py3_PySequence_GetItem},
Bram Moolenaara9922d62013-05-30 13:01:18 +0200522 {"PySequence_Fast", (PYTHON_PROC*)&py3_PySequence_Fast},
Bram Moolenaardb913952012-06-29 12:54:53 +0200523 {"PyTuple_Size", (PYTHON_PROC*)&py3_PyTuple_Size},
524 {"PyTuple_GetItem", (PYTHON_PROC*)&py3_PyTuple_GetItem},
Bram Moolenaar3b48b112018-07-04 22:03:25 +0200525# if PY_VERSION_HEX >= 0x030601f0
526 {"PySlice_AdjustIndices", (PYTHON_PROC*)&py3_PySlice_AdjustIndices},
527 {"PySlice_Unpack", (PYTHON_PROC*)&py3_PySlice_Unpack},
528# endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200529 {"PySlice_GetIndicesEx", (PYTHON_PROC*)&py3_PySlice_GetIndicesEx},
530 {"PyErr_NoMemory", (PYTHON_PROC*)&py3_PyErr_NoMemory},
531 {"Py_Finalize", (PYTHON_PROC*)&py3_Py_Finalize},
532 {"PyErr_SetString", (PYTHON_PROC*)&py3_PyErr_SetString},
Bram Moolenaar4d188da2013-05-15 15:35:09 +0200533 {"PyErr_SetObject", (PYTHON_PROC*)&py3_PyErr_SetObject},
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200534 {"PyErr_ExceptionMatches", (PYTHON_PROC*)&py3_PyErr_ExceptionMatches},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200535 {"PyRun_SimpleString", (PYTHON_PROC*)&py3_PyRun_SimpleString},
Bram Moolenaardb913952012-06-29 12:54:53 +0200536 {"PyRun_String", (PYTHON_PROC*)&py3_PyRun_String},
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200537 {"PyObject_GetAttrString", (PYTHON_PROC*)&py3_PyObject_GetAttrString},
Bram Moolenaara9922d62013-05-30 13:01:18 +0200538 {"PyObject_HasAttrString", (PYTHON_PROC*)&py3_PyObject_HasAttrString},
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200539 {"PyObject_SetAttrString", (PYTHON_PROC*)&py3_PyObject_SetAttrString},
540 {"PyObject_CallFunctionObjArgs", (PYTHON_PROC*)&py3_PyObject_CallFunctionObjArgs},
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200541 {"_PyObject_CallFunction_SizeT", (PYTHON_PROC*)&py3__PyObject_CallFunction_SizeT},
Bram Moolenaarf4258302013-06-02 18:20:17 +0200542 {"PyObject_Call", (PYTHON_PROC*)&py3_PyObject_Call},
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200543 {"PyEval_GetGlobals", (PYTHON_PROC*)&py3_PyEval_GetGlobals},
544 {"PyEval_GetLocals", (PYTHON_PROC*)&py3_PyEval_GetLocals},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200545 {"PyList_GetItem", (PYTHON_PROC*)&py3_PyList_GetItem},
546 {"PyImport_ImportModule", (PYTHON_PROC*)&py3_PyImport_ImportModule},
Bram Moolenaardb913952012-06-29 12:54:53 +0200547 {"PyImport_AddModule", (PYTHON_PROC*)&py3_PyImport_AddModule},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200548 {"PyErr_BadArgument", (PYTHON_PROC*)&py3_PyErr_BadArgument},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200549 {"PyErr_Occurred", (PYTHON_PROC*)&py3_PyErr_Occurred},
550 {"PyModule_GetDict", (PYTHON_PROC*)&py3_PyModule_GetDict},
551 {"PyList_SetItem", (PYTHON_PROC*)&py3_PyList_SetItem},
552 {"PyDict_GetItemString", (PYTHON_PROC*)&py3_PyDict_GetItemString},
Bram Moolenaardb913952012-06-29 12:54:53 +0200553 {"PyDict_Next", (PYTHON_PROC*)&py3_PyDict_Next},
554 {"PyMapping_Check", (PYTHON_PROC*)&py3_PyMapping_Check},
Bram Moolenaarbcb40972013-05-30 13:22:13 +0200555 {"PyMapping_Keys", (PYTHON_PROC*)&py3_PyMapping_Keys},
Zdenek Dohnal90478f32021-06-14 15:08:30 +0200556# if PY_VERSION_HEX >= 0x030a00b2
557 {"PyIter_Check", (PYTHON_PROC*)&py3_PyIter_Check},
558# endif
Bram Moolenaardb913952012-06-29 12:54:53 +0200559 {"PyIter_Next", (PYTHON_PROC*)&py3_PyIter_Next},
560 {"PyObject_GetIter", (PYTHON_PROC*)&py3_PyObject_GetIter},
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200561 {"PyObject_Repr", (PYTHON_PROC*)&py3_PyObject_Repr},
Bram Moolenaarbcb40972013-05-30 13:22:13 +0200562 {"PyObject_GetItem", (PYTHON_PROC*)&py3_PyObject_GetItem},
Bram Moolenaar03db85b2013-05-15 14:51:35 +0200563 {"PyObject_IsTrue", (PYTHON_PROC*)&py3_PyObject_IsTrue},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200564 {"PyLong_FromLong", (PYTHON_PROC*)&py3_PyLong_FromLong},
565 {"PyDict_New", (PYTHON_PROC*)&py3_PyDict_New},
Bram Moolenaaree1b9312020-07-16 22:15:53 +0200566# if PY_VERSION_HEX >= 0x030900b0
567 {"PyType_GetFlags", (PYTHON_PROC*)&py3_PyType_GetFlags},
568# endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200569 {"PyType_Ready", (PYTHON_PROC*)&py3_PyType_Ready},
570 {"PyDict_SetItemString", (PYTHON_PROC*)&py3_PyDict_SetItemString},
571 {"PyLong_AsLong", (PYTHON_PROC*)&py3_PyLong_AsLong},
572 {"PyErr_SetNone", (PYTHON_PROC*)&py3_PyErr_SetNone},
573 {"PyEval_InitThreads", (PYTHON_PROC*)&py3_PyEval_InitThreads},
574 {"PyEval_RestoreThread", (PYTHON_PROC*)&py3_PyEval_RestoreThread},
575 {"PyEval_SaveThread", (PYTHON_PROC*)&py3_PyEval_SaveThread},
Bram Moolenaar5f87b232013-06-13 20:57:50 +0200576 {"_PyArg_Parse_SizeT", (PYTHON_PROC*)&py3_PyArg_Parse},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200577 {"Py_IsInitialized", (PYTHON_PROC*)&py3_Py_IsInitialized},
Bram Moolenaardb913952012-06-29 12:54:53 +0200578 {"_PyObject_NextNotImplemented", (PYTHON_PROC*)&py3__PyObject_NextNotImplemented},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200579 {"_Py_NoneStruct", (PYTHON_PROC*)&py3__Py_NoneStruct},
Bram Moolenaar66b79852012-09-21 14:00:35 +0200580 {"_Py_FalseStruct", (PYTHON_PROC*)&py3__Py_FalseStruct},
581 {"_Py_TrueStruct", (PYTHON_PROC*)&py3__Py_TrueStruct},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200582 {"PyErr_Clear", (PYTHON_PROC*)&py3_PyErr_Clear},
Bram Moolenaarc476e522013-06-23 13:46:40 +0200583 {"PyErr_Format", (PYTHON_PROC*)&py3_PyErr_Format},
Bram Moolenaar4d369872013-02-20 16:09:43 +0100584 {"PyErr_PrintEx", (PYTHON_PROC*)&py3_PyErr_PrintEx},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200585 {"PyObject_Init", (PYTHON_PROC*)&py3__PyObject_Init},
586 {"PyModule_AddObject", (PYTHON_PROC*)&py3_PyModule_AddObject},
587 {"PyImport_AppendInittab", (PYTHON_PROC*)&py3_PyImport_AppendInittab},
Bram Moolenaar9c9cbf12012-10-23 05:17:37 +0200588# if PY_VERSION_HEX >= 0x030300f0
589 {"PyUnicode_AsUTF8", (PYTHON_PROC*)&py3_PyUnicode_AsUTF8},
590# else
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200591 {"_PyUnicode_AsString", (PYTHON_PROC*)&py3__PyUnicode_AsString},
Bram Moolenaar9c9cbf12012-10-23 05:17:37 +0200592# endif
Bram Moolenaar1a3b5692013-05-30 12:40:39 +0200593# ifndef Py_UNICODE_USE_UCS_FUNCTIONS
594 {"PyUnicode_FromFormat", (PYTHON_PROC*)&py3_PyUnicode_FromFormat},
595# else
596# ifdef Py_UNICODE_WIDE
597 {"PyUnicodeUCS4_FromFormat", (PYTHON_PROC*)&py3_PyUnicodeUCS4_FromFormat},
598# else
599 {"PyUnicodeUCS2_FromFormat", (PYTHON_PROC*)&py3_PyUnicodeUCS2_FromFormat},
600# endif
601# endif
Bram Moolenaar19e60942011-06-19 00:27:51 +0200602 {"PyBytes_AsString", (PYTHON_PROC*)&py3_PyBytes_AsString},
Bram Moolenaarcdab9052012-09-05 19:03:56 +0200603 {"PyBytes_AsStringAndSize", (PYTHON_PROC*)&py3_PyBytes_AsStringAndSize},
Bram Moolenaardb913952012-06-29 12:54:53 +0200604 {"PyBytes_FromString", (PYTHON_PROC*)&py3_PyBytes_FromString},
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100605 {"PyBytes_FromStringAndSize", (PYTHON_PROC*)&py3_PyBytes_FromStringAndSize},
Bram Moolenaaree1b9312020-07-16 22:15:53 +0200606# if defined(Py_DEBUG) || PY_VERSION_HEX >= 0x030900b0
607 {"_Py_Dealloc", (PYTHON_PROC*)&py3__Py_Dealloc},
608# endif
609# if PY_VERSION_HEX >= 0x030900b0
610 {"_PyObject_New", (PYTHON_PROC*)&py3__PyObject_New},
611# endif
Bram Moolenaardb913952012-06-29 12:54:53 +0200612 {"PyFloat_FromDouble", (PYTHON_PROC*)&py3_PyFloat_FromDouble},
613 {"PyFloat_AsDouble", (PYTHON_PROC*)&py3_PyFloat_AsDouble},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200614 {"PyObject_GenericGetAttr", (PYTHON_PROC*)&py3_PyObject_GenericGetAttr},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200615 {"PyType_GenericAlloc", (PYTHON_PROC*)&py3_PyType_GenericAlloc},
616 {"PyType_GenericNew", (PYTHON_PROC*)&py3_PyType_GenericNew},
Bram Moolenaar66b79852012-09-21 14:00:35 +0200617 {"PyType_Type", (PYTHON_PROC*)&py3_PyType_Type},
Bram Moolenaard4a8c982018-05-15 22:31:18 +0200618 {"PyStdPrinter_Type", (PYTHON_PROC*)&py3_PyStdPrinter_Type},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200619 {"PySlice_Type", (PYTHON_PROC*)&py3_PySlice_Type},
Bram Moolenaardb913952012-06-29 12:54:53 +0200620 {"PyFloat_Type", (PYTHON_PROC*)&py3_PyFloat_Type},
Bram Moolenaar66b79852012-09-21 14:00:35 +0200621 {"PyBool_Type", (PYTHON_PROC*)&py3_PyBool_Type},
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200622 {"PyNumber_Check", (PYTHON_PROC*)&py3_PyNumber_Check},
623 {"PyNumber_Long", (PYTHON_PROC*)&py3_PyNumber_Long},
Bram Moolenaar19e60942011-06-19 00:27:51 +0200624 {"PyErr_NewException", (PYTHON_PROC*)&py3_PyErr_NewException},
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200625# ifdef Py_DEBUG
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200626 {"_Py_NegativeRefcount", (PYTHON_PROC*)&py3__Py_NegativeRefcount},
627 {"_Py_RefTotal", (PYTHON_PROC*)&py3__Py_RefTotal},
Bram Moolenaar0014a532013-05-29 21:33:39 +0200628 {"PyModule_Create2TraceRefs", (PYTHON_PROC*)&py3_PyModule_Create2TraceRefs},
629# else
630 {"PyModule_Create2", (PYTHON_PROC*)&py3_PyModule_Create2},
631# endif
632# if defined(Py_DEBUG) && !defined(Py_DEBUG_NO_PYMALLOC)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200633 {"_PyObject_DebugFree", (PYTHON_PROC*)&py3__PyObject_DebugFree},
634 {"_PyObject_DebugMalloc", (PYTHON_PROC*)&py3__PyObject_DebugMalloc},
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200635# else
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200636 {"PyObject_Malloc", (PYTHON_PROC*)&py3_PyObject_Malloc},
637 {"PyObject_Free", (PYTHON_PROC*)&py3_PyObject_Free},
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200638# endif
Bram Moolenaar774267b2013-05-21 20:51:59 +0200639 {"_PyObject_GC_New", (PYTHON_PROC*)&py3__PyObject_GC_New},
640 {"PyObject_GC_Del", (PYTHON_PROC*)&py3_PyObject_GC_Del},
641 {"PyObject_GC_UnTrack", (PYTHON_PROC*)&py3_PyObject_GC_UnTrack},
Bram Moolenaardb913952012-06-29 12:54:53 +0200642 {"PyType_IsSubtype", (PYTHON_PROC*)&py3_PyType_IsSubtype},
643 {"PyCapsule_New", (PYTHON_PROC*)&py3_PyCapsule_New},
644 {"PyCapsule_GetPointer", (PYTHON_PROC*)&py3_PyCapsule_GetPointer},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200645 {"", NULL},
646};
647
Bram Moolenaar13a1f3f2019-10-23 21:37:25 +0200648# if PY_VERSION_HEX >= 0x030800f0
649 static inline void
650py3__Py_DECREF(const char *filename UNUSED, int lineno UNUSED, PyObject *op)
651{
Bram Moolenaar13a1f3f2019-10-23 21:37:25 +0200652 if (--op->ob_refcnt != 0)
653 {
654# ifdef Py_REF_DEBUG
655 if (op->ob_refcnt < 0)
656 {
657 _Py_NegativeRefcount(filename, lineno, op);
658 }
659# endif
660 }
661 else
662 {
663 _Py_Dealloc(op);
664 }
665}
666
667# undef Py_DECREF
668# define Py_DECREF(op) py3__Py_DECREF(__FILE__, __LINE__, _PyObject_CAST(op))
669
670 static inline void
671py3__Py_XDECREF(PyObject *op)
672{
673 if (op != NULL)
674 {
675 Py_DECREF(op);
676 }
677}
678
679# undef Py_XDECREF
680# define Py_XDECREF(op) py3__Py_XDECREF(_PyObject_CAST(op))
681# endif
682
Bram Moolenaaree1b9312020-07-16 22:15:53 +0200683# if PY_VERSION_HEX >= 0x030900b0
684 static inline int
685py3_PyType_HasFeature(PyTypeObject *type, unsigned long feature)
686{
687 return ((PyType_GetFlags(type) & feature) != 0);
688}
689# define PyType_HasFeature(t,f) py3_PyType_HasFeature(t,f)
690# endif
691
Zdenek Dohnal90478f32021-06-14 15:08:30 +0200692# if PY_VERSION_HEX >= 0x030a00b2
693 static inline int
694py3__PyObject_TypeCheck(PyObject *ob, PyTypeObject *type)
695{
696 return Py_IS_TYPE(ob, type) || PyType_IsSubtype(Py_TYPE(ob), type);
697}
Zdenek Dohnalfee511c2022-06-27 13:59:00 +0100698# if PY_VERSION_HEX >= 0x030b00b3
699# undef PyObject_TypeCheck
700# define PyObject_TypeCheck(o,t) py3__PyObject_TypeCheck(o,t)
701# else
702# define _PyObject_TypeCheck(o,t) py3__PyObject_TypeCheck(o,t)
703# endif
Zdenek Dohnal90478f32021-06-14 15:08:30 +0200704# endif
705
Bram Moolenaarb2f9e0e2020-12-25 13:52:37 +0100706# ifdef MSWIN
707/*
708 * Look up the library "libname" using the InstallPath registry key.
709 * Return NULL when failed. Return an allocated string when successful.
710 */
711 static char *
712py3_get_system_libname(const char *libname)
713{
714 const char *cp = libname;
715 char subkey[128];
716 HKEY hKey;
717 char installpath[MAXPATHL];
718 LONG len = sizeof(installpath);
719 LSTATUS rc;
720 size_t sysliblen;
721 char *syslibname;
722
723 while (*cp != '\0')
724 {
725 if (*cp == ':' || *cp == '\\' || *cp == '/')
726 {
727 // Bail out if "libname" contains path separator, assume it is
728 // an absolute path.
729 return NULL;
730 }
731 ++cp;
732 }
733 vim_snprintf(subkey, sizeof(subkey),
734# ifdef _WIN64
735 "Software\\Python\\PythonCore\\%d.%d\\InstallPath",
736# else
737 "Software\\Python\\PythonCore\\%d.%d-32\\InstallPath",
738# endif
739 PY_MAJOR_VERSION, PY_MINOR_VERSION);
740 if (RegOpenKeyExA(HKEY_LOCAL_MACHINE, subkey, 0, KEY_QUERY_VALUE, &hKey)
741 != ERROR_SUCCESS)
742 return NULL;
743 rc = RegQueryValueA(hKey, NULL, installpath, &len);
744 RegCloseKey(hKey);
745 if (ERROR_SUCCESS != rc)
746 return NULL;
747 cp = installpath + len;
748 // Just in case registry value contains null terminators.
749 while (cp > installpath && *(cp-1) == '\0')
750 --cp;
751 // Remove trailing path separators.
752 while (cp > installpath && (*(cp-1) == '\\' || *(cp-1) == '/'))
753 --cp;
754 // Ignore if InstallPath is effectively empty.
755 if (cp <= installpath)
756 return NULL;
757 sysliblen = (cp - installpath) + 1 + STRLEN(libname) + 1;
758 syslibname = alloc(sysliblen);
759 vim_snprintf(syslibname, sysliblen, "%.*s\\%s",
760 (int)(cp - installpath), installpath, libname);
761 return syslibname;
762}
763# endif
764
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200765/*
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200766 * Load library and get all pointers.
767 * Parameter 'libname' provides name of DLL.
768 * Return OK or FAIL.
769 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200770 static int
771py3_runtime_link_init(char *libname, int verbose)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200772{
773 int i;
Bram Moolenaar7b24ce02018-03-29 18:15:26 +0200774 PYTHON_PROC *ucs_from_string = (PYTHON_PROC *)&py3_PyUnicode_FromString;
775 PYTHON_PROC *ucs_decode = (PYTHON_PROC *)&py3_PyUnicode_Decode;
776 PYTHON_PROC *ucs_as_encoded_string =
777 (PYTHON_PROC *)&py3_PyUnicode_AsEncodedString;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200778
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100779# if !(defined(PY_NO_RTLD_GLOBAL) && defined(PY3_NO_RTLD_GLOBAL)) && defined(UNIX) && defined(FEAT_PYTHON)
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100780 // Can't have Python and Python3 loaded at the same time.
Dominique Pelleaf4a61a2021-12-27 17:21:41 +0000781 // It causes a crash, because RTLD_GLOBAL is needed for
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100782 // standard C extension libraries of one or both python versions.
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200783 if (python_loaded())
784 {
Bram Moolenaar9dc93ae2011-08-28 16:00:19 +0200785 if (verbose)
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +0000786 emsg(_(e_this_vim_cannot_execute_py3_after_using_python));
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200787 return FAIL;
788 }
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200789# endif
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200790
791 if (hinstPy3 != 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200792 return OK;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200793 hinstPy3 = load_dll(libname);
794
Bram Moolenaarb2f9e0e2020-12-25 13:52:37 +0100795# ifdef MSWIN
796 if (!hinstPy3)
797 {
798 // Attempt to use the path from InstallPath as stored in the registry.
799 char *syslibname = py3_get_system_libname(libname);
800
801 if (syslibname != NULL)
802 {
803 hinstPy3 = load_dll(syslibname);
804 vim_free(syslibname);
805 }
806 }
807# endif
808
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200809 if (!hinstPy3)
810 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200811 if (verbose)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000812 semsg(_(e_could_not_load_library_str_str), libname, load_dll_error());
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200813 return FAIL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200814 }
815
816 for (i = 0; py3_funcname_table[i].ptr; ++i)
817 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200818 if ((*py3_funcname_table[i].ptr = symbol_from_dll(hinstPy3,
819 py3_funcname_table[i].name)) == NULL)
820 {
821 close_dll(hinstPy3);
822 hinstPy3 = 0;
823 if (verbose)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000824 semsg(_(e_could_not_load_library_function_str), py3_funcname_table[i].name);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200825 return FAIL;
826 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200827 }
828
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100829 // Load unicode functions separately as only the ucs2 or the ucs4 functions
830 // will be present in the library.
Bram Moolenaar9c9cbf12012-10-23 05:17:37 +0200831# if PY_VERSION_HEX >= 0x030300f0
Bram Moolenaar7b24ce02018-03-29 18:15:26 +0200832 *ucs_from_string = symbol_from_dll(hinstPy3, "PyUnicode_FromString");
833 *ucs_decode = symbol_from_dll(hinstPy3, "PyUnicode_Decode");
834 *ucs_as_encoded_string = symbol_from_dll(hinstPy3,
Bram Moolenaar7bc4f932012-10-14 03:22:56 +0200835 "PyUnicode_AsEncodedString");
Bram Moolenaar9c9cbf12012-10-23 05:17:37 +0200836# else
Bram Moolenaar7b24ce02018-03-29 18:15:26 +0200837 *ucs_from_string = symbol_from_dll(hinstPy3, "PyUnicodeUCS2_FromString");
838 *ucs_decode = symbol_from_dll(hinstPy3,
Bram Moolenaar19e60942011-06-19 00:27:51 +0200839 "PyUnicodeUCS2_Decode");
Bram Moolenaar7b24ce02018-03-29 18:15:26 +0200840 *ucs_as_encoded_string = symbol_from_dll(hinstPy3,
Bram Moolenaar19e60942011-06-19 00:27:51 +0200841 "PyUnicodeUCS2_AsEncodedString");
Bram Moolenaar7b24ce02018-03-29 18:15:26 +0200842 if (*ucs_from_string == NULL || *ucs_decode == NULL
843 || *ucs_as_encoded_string == NULL)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200844 {
Bram Moolenaar7b24ce02018-03-29 18:15:26 +0200845 *ucs_from_string = symbol_from_dll(hinstPy3,
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200846 "PyUnicodeUCS4_FromString");
Bram Moolenaar7b24ce02018-03-29 18:15:26 +0200847 *ucs_decode = symbol_from_dll(hinstPy3,
Bram Moolenaar19e60942011-06-19 00:27:51 +0200848 "PyUnicodeUCS4_Decode");
Bram Moolenaar7b24ce02018-03-29 18:15:26 +0200849 *ucs_as_encoded_string = symbol_from_dll(hinstPy3,
Bram Moolenaar19e60942011-06-19 00:27:51 +0200850 "PyUnicodeUCS4_AsEncodedString");
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200851 }
Bram Moolenaar9c9cbf12012-10-23 05:17:37 +0200852# endif
Bram Moolenaar7b24ce02018-03-29 18:15:26 +0200853 if (*ucs_from_string == NULL || *ucs_decode == NULL
854 || *ucs_as_encoded_string == NULL)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200855 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200856 close_dll(hinstPy3);
857 hinstPy3 = 0;
858 if (verbose)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000859 semsg(_(e_could_not_load_library_function_str), "PyUnicode_UCSX_*");
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200860 return FAIL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200861 }
862
863 return OK;
864}
865
866/*
867 * If python is enabled (there is installed python on Windows system) return
868 * TRUE, else FALSE.
869 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200870 int
871python3_enabled(int verbose)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200872{
Bram Moolenaar25e4fcd2016-01-09 14:57:47 +0100873 return py3_runtime_link_init((char *)p_py3dll, verbose) == OK;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200874}
875
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100876/*
877 * Load the standard Python exceptions - don't import the symbols from the
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200878 * DLL, as this can cause errors (importing data symbols is not reliable).
879 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200880 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100881get_py3_exceptions(void)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200882{
883 PyObject *exmod = PyImport_ImportModule("builtins");
884 PyObject *exdict = PyModule_GetDict(exmod);
885 p3imp_PyExc_AttributeError = PyDict_GetItemString(exdict, "AttributeError");
886 p3imp_PyExc_IndexError = PyDict_GetItemString(exdict, "IndexError");
Bram Moolenaaraf6abb92013-04-24 13:04:26 +0200887 p3imp_PyExc_KeyError = PyDict_GetItemString(exdict, "KeyError");
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200888 p3imp_PyExc_KeyboardInterrupt = PyDict_GetItemString(exdict, "KeyboardInterrupt");
889 p3imp_PyExc_TypeError = PyDict_GetItemString(exdict, "TypeError");
890 p3imp_PyExc_ValueError = PyDict_GetItemString(exdict, "ValueError");
Bram Moolenaar41009372013-07-01 22:03:04 +0200891 p3imp_PyExc_SystemExit = PyDict_GetItemString(exdict, "SystemExit");
Bram Moolenaar8661b172013-05-15 15:44:28 +0200892 p3imp_PyExc_RuntimeError = PyDict_GetItemString(exdict, "RuntimeError");
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200893 p3imp_PyExc_ImportError = PyDict_GetItemString(exdict, "ImportError");
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200894 p3imp_PyExc_OverflowError = PyDict_GetItemString(exdict, "OverflowError");
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200895 Py_XINCREF(p3imp_PyExc_AttributeError);
896 Py_XINCREF(p3imp_PyExc_IndexError);
Bram Moolenaaraf6abb92013-04-24 13:04:26 +0200897 Py_XINCREF(p3imp_PyExc_KeyError);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200898 Py_XINCREF(p3imp_PyExc_KeyboardInterrupt);
899 Py_XINCREF(p3imp_PyExc_TypeError);
900 Py_XINCREF(p3imp_PyExc_ValueError);
Bram Moolenaar41009372013-07-01 22:03:04 +0200901 Py_XINCREF(p3imp_PyExc_SystemExit);
Bram Moolenaar8661b172013-05-15 15:44:28 +0200902 Py_XINCREF(p3imp_PyExc_RuntimeError);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200903 Py_XINCREF(p3imp_PyExc_ImportError);
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200904 Py_XINCREF(p3imp_PyExc_OverflowError);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200905 Py_XDECREF(exmod);
906}
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100907#endif // DYNAMIC_PYTHON3
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200908
Bram Moolenaardb913952012-06-29 12:54:53 +0200909static int py3initialised = 0;
Bram Moolenaardb913952012-06-29 12:54:53 +0200910#define PYINITIALISED py3initialised
Bram Moolenaarc4f83382017-07-07 14:50:44 +0200911static int python_end_called = FALSE;
Bram Moolenaardb913952012-06-29 12:54:53 +0200912
Bram Moolenaar774267b2013-05-21 20:51:59 +0200913#define DESTRUCTOR_FINISH(self) Py_TYPE(self)->tp_free((PyObject*)self)
Bram Moolenaardb913952012-06-29 12:54:53 +0200914
Bram Moolenaar971db462013-05-12 18:44:48 +0200915#define WIN_PYTHON_REF(win) win->w_python3_ref
916#define BUF_PYTHON_REF(buf) buf->b_python3_ref
Bram Moolenaar5e538ec2013-05-15 15:12:29 +0200917#define TAB_PYTHON_REF(tab) tab->tp_python3_ref
Bram Moolenaar971db462013-05-12 18:44:48 +0200918
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200919 static void
920call_PyObject_Free(void *p)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200921{
Bram Moolenaar0014a532013-05-29 21:33:39 +0200922#if defined(Py_DEBUG) && !defined(Py_DEBUG_NO_PYMALLOC)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200923 _PyObject_DebugFree(p);
924#else
925 PyObject_Free(p);
926#endif
927}
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200928
929 static PyObject *
930call_PyType_GenericNew(PyTypeObject *type, PyObject *args, PyObject *kwds)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200931{
932 return PyType_GenericNew(type,args,kwds);
933}
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200934
935 static PyObject *
936call_PyType_GenericAlloc(PyTypeObject *type, Py_ssize_t nitems)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200937{
938 return PyType_GenericAlloc(type,nitems);
939}
940
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200941static PyObject *OutputGetattro(PyObject *, PyObject *);
942static int OutputSetattro(PyObject *, PyObject *, PyObject *);
943static PyObject *BufferGetattro(PyObject *, PyObject *);
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200944static int BufferSetattro(PyObject *, PyObject *, PyObject *);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +0200945static PyObject *TabPageGetattro(PyObject *, PyObject *);
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200946static PyObject *WindowGetattro(PyObject *, PyObject *);
947static int WindowSetattro(PyObject *, PyObject *, PyObject *);
948static PyObject *RangeGetattro(PyObject *, PyObject *);
949static PyObject *CurrentGetattro(PyObject *, PyObject *);
950static int CurrentSetattro(PyObject *, PyObject *, PyObject *);
951static PyObject *DictionaryGetattro(PyObject *, PyObject *);
952static int DictionarySetattro(PyObject *, PyObject *, PyObject *);
953static PyObject *ListGetattro(PyObject *, PyObject *);
954static int ListSetattro(PyObject *, PyObject *, PyObject *);
955static PyObject *FunctionGetattro(PyObject *, PyObject *);
956
957static struct PyModuleDef vimmodule;
958
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +0200959#define PY_CAN_RECURSE
960
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200961/*
962 * Include the code shared with if_python.c
963 */
964#include "if_py_both.h"
965
Bram Moolenaar828bff12019-03-19 22:11:41 +0100966// NOTE: Must always be used at the start of a block, since it declares "name".
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200967#define GET_ATTR_STRING(name, nameobj) \
968 char *name = ""; \
969 if (PyUnicode_Check(nameobj)) \
Bram Moolenaar828bff12019-03-19 22:11:41 +0100970 name = (char *)_PyUnicode_AsString(nameobj)
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200971
972#define PY3OBJ_DELETED(obj) (obj->ob_base.ob_refcnt<=0)
973
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100974///////////////////////////////////////////////////////
975// Internal function prototypes.
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200976
Bram Moolenaar7854e3a2012-11-28 15:33:14 +0100977static PyObject *Py3Init_vim(void);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200978
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100979///////////////////////////////////////////////////////
980// 1. Python interpreter main program.
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200981
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200982 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100983python3_end(void)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200984{
985 static int recurse = 0;
986
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100987 // If a crash occurs while doing this, don't try again.
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200988 if (recurse != 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200989 return;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200990
Bram Moolenaarc4f83382017-07-07 14:50:44 +0200991 python_end_called = TRUE;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200992 ++recurse;
993
994#ifdef DYNAMIC_PYTHON3
995 if (hinstPy3)
996#endif
997 if (Py_IsInitialized())
998 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100999 // acquire lock before finalizing
Bram Moolenaar71700b82013-05-15 17:49:05 +02001000 PyGILState_Ensure();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001001
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001002 Py_Finalize();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001003 }
1004
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001005 --recurse;
1006}
1007
Bram Moolenaar094454f2015-10-07 10:39:55 +02001008#if (defined(DYNAMIC_PYTHON3) && defined(DYNAMIC_PYTHON) && defined(FEAT_PYTHON) && defined(UNIX)) || defined(PROTO)
Bram Moolenaar4c3a3262010-07-24 15:42:14 +02001009 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001010python3_loaded(void)
Bram Moolenaar4c3a3262010-07-24 15:42:14 +02001011{
1012 return (hinstPy3 != 0);
1013}
1014#endif
1015
Bram Moolenaar94073162018-01-31 21:49:05 +01001016static wchar_t *py_home_buf = NULL;
1017
Bram Moolenaar56b8dc32020-08-06 21:47:11 +02001018#if defined(MSWIN) && (PY_VERSION_HEX >= 0x030500f0)
Bram Moolenaarc6ed2542020-10-10 23:26:28 +02001019/*
1020 * Return TRUE if stdin is readable from Python 3.
1021 */
1022 static BOOL
1023is_stdin_readable(void)
1024{
1025 DWORD mode, eventnum;
1026 struct _stat st;
1027 int fd = fileno(stdin);
1028 HANDLE hstdin = (HANDLE)_get_osfhandle(fd);
1029
1030 // Check if stdin is connected to the console.
1031 if (GetConsoleMode(hstdin, &mode))
1032 // Check if it is opened as input.
1033 return GetNumberOfConsoleInputEvents(hstdin, &eventnum);
1034
1035 return _fstat(fd, &st) == 0;
1036}
1037
1038// Python 3.5 or later will abort inside Py_Initialize() when stdin has
1039// been closed (i.e. executed by "vim -"). Reconnect stdin to CONIN$.
Bram Moolenaar56b8dc32020-08-06 21:47:11 +02001040// Note that the python DLL is linked to its own stdio DLL which can be
1041// differ from Vim's stdio.
1042 static void
1043reset_stdin(void)
1044{
1045 FILE *(*py__acrt_iob_func)(unsigned) = NULL;
1046 FILE *(*pyfreopen)(const char *, const char *, FILE *) = NULL;
Bram Moolenaar63ff72a2022-02-07 13:54:01 +00001047 HINSTANCE hinst = hinstPy3;
Bram Moolenaar56b8dc32020-08-06 21:47:11 +02001048
Bram Moolenaarc6ed2542020-10-10 23:26:28 +02001049 if (hinst == NULL || is_stdin_readable())
Bram Moolenaar56b8dc32020-08-06 21:47:11 +02001050 return;
1051
1052 // Get "freopen" and "stdin" which are used in the python DLL.
1053 // "stdin" is defined as "__acrt_iob_func(0)" in VC++ 2015 or later.
1054 py__acrt_iob_func = get_dll_import_func(hinst, "__acrt_iob_func");
1055 if (py__acrt_iob_func)
1056 {
1057 HINSTANCE hpystdiodll = find_imported_module_by_funcname(hinst,
Bram Moolenaar253b16a2020-10-06 19:59:06 +02001058 "__acrt_iob_func");
Bram Moolenaar56b8dc32020-08-06 21:47:11 +02001059 if (hpystdiodll)
Bram Moolenaar253b16a2020-10-06 19:59:06 +02001060 pyfreopen = (void *)GetProcAddress(hpystdiodll, "freopen");
Bram Moolenaar56b8dc32020-08-06 21:47:11 +02001061 }
1062
Bram Moolenaarc6ed2542020-10-10 23:26:28 +02001063 // Reconnect stdin to CONIN$.
Bram Moolenaar253b16a2020-10-06 19:59:06 +02001064 if (pyfreopen != NULL)
Bram Moolenaarc6ed2542020-10-10 23:26:28 +02001065 pyfreopen("CONIN$", "r", py__acrt_iob_func(0));
Bram Moolenaar56b8dc32020-08-06 21:47:11 +02001066 else
Bram Moolenaarc6ed2542020-10-10 23:26:28 +02001067 freopen("CONIN$", "r", stdin);
Bram Moolenaar56b8dc32020-08-06 21:47:11 +02001068}
1069#else
1070# define reset_stdin()
1071#endif
1072
Bram Moolenaar63ff72a2022-02-07 13:54:01 +00001073// Python 3.2 or later will abort inside Py_Initialize() when mandatory
1074// modules cannot be loaded (e.g. 'pythonthreehome' is wrongly set.).
1075// Install a hook to python dll's exit() and recover from it.
1076#if defined(MSWIN) && (PY_VERSION_HEX >= 0x030200f0)
1077# define HOOK_EXIT
1078# include <setjmp.h>
1079
1080static jmp_buf exit_hook_jump_buf;
1081static void *orig_exit = NULL;
1082
1083/*
1084 * Function that replaces exit() while calling Py_Initialize().
1085 */
1086 static void
1087hooked_exit(int ret)
1088{
1089 // Recover from exit.
1090 longjmp(exit_hook_jump_buf, 1);
1091}
1092
1093/*
1094 * Install a hook to python dll's exit().
1095 */
1096 static void
1097hook_py_exit(void)
1098{
1099 HINSTANCE hinst = hinstPy3;
1100
1101 if (hinst == NULL || orig_exit != NULL)
1102 return;
1103
1104 orig_exit = hook_dll_import_func(hinst, "exit", (void *)hooked_exit);
1105}
1106
1107/*
1108 * Remove the hook installed by hook_py_exit().
1109 */
1110 static void
1111restore_py_exit(void)
1112{
1113 HINSTANCE hinst = hinstPy3;
1114
1115 if (hinst == NULL)
1116 return;
1117
1118 if (orig_exit != NULL)
1119 hook_dll_import_func(hinst, "exit", orig_exit);
1120 orig_exit = NULL;
1121}
1122#endif
1123
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001124 static int
1125Python3_Init(void)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001126{
1127 if (!py3initialised)
1128 {
1129#ifdef DYNAMIC_PYTHON3
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001130 if (!python3_enabled(TRUE))
1131 {
Bram Moolenaar9a846fb2022-01-01 21:59:18 +00001132 emsg(_(e_sorry_this_command_is_disabled_python_library_could_not_be_found));
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001133 goto fail;
1134 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001135#endif
1136
1137 init_structs();
1138
Bram Moolenaar94073162018-01-31 21:49:05 +01001139 if (*p_py3home != NUL)
1140 {
1141 size_t len = mbstowcs(NULL, (char *)p_py3home, 0) + 1;
Bram Moolenaar644d37b2010-11-16 19:26:02 +01001142
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001143 // The string must not change later, make a copy in static memory.
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001144 py_home_buf = ALLOC_MULT(wchar_t, len);
Bram Moolenaar94073162018-01-31 21:49:05 +01001145 if (py_home_buf != NULL && mbstowcs(
1146 py_home_buf, (char *)p_py3home, len) != (size_t)-1)
1147 Py_SetPythonHome(py_home_buf);
1148 }
Bram Moolenaar644d37b2010-11-16 19:26:02 +01001149#ifdef PYTHON3_HOME
Bram Moolenaar94073162018-01-31 21:49:05 +01001150 else if (mch_getenv((char_u *)"PYTHONHOME") == NULL)
Bram Moolenaar10005652015-12-31 21:03:23 +01001151 Py_SetPythonHome(PYTHON3_HOME);
Bram Moolenaar644d37b2010-11-16 19:26:02 +01001152#endif
1153
Bram Moolenaar7bc4f932012-10-14 03:22:56 +02001154 PyImport_AppendInittab("vim", Py3Init_vim);
1155
Bram Moolenaar63ff72a2022-02-07 13:54:01 +00001156#if !defined(DYNAMIC_PYTHON3) && defined(MSWIN)
1157 hinstPy3 = GetModuleHandle(PYTHON3_DLL);
1158#endif
Bram Moolenaar56b8dc32020-08-06 21:47:11 +02001159 reset_stdin();
Bram Moolenaar63ff72a2022-02-07 13:54:01 +00001160
1161#ifdef HOOK_EXIT
1162 // Catch exit() called in Py_Initialize().
1163 hook_py_exit();
1164 if (setjmp(exit_hook_jump_buf) == 0)
1165#endif
1166 {
1167 Py_Initialize();
1168#ifdef HOOK_EXIT
1169 restore_py_exit();
1170#endif
1171 }
1172#ifdef HOOK_EXIT
1173 else
1174 {
1175 // exit() was called in Py_Initialize().
1176 restore_py_exit();
1177 emsg(_(e_critical_error_in_python3_initialization_check_your_installation));
1178 goto fail;
1179 }
1180#endif
Bram Moolenaard0573012017-10-28 21:11:06 +02001181
Bram Moolenaarefc0d942020-10-11 18:05:02 +02001182#if PY_VERSION_HEX < 0x03090000
1183 // Initialise threads. This is deprecated since Python 3.9.
Bram Moolenaar456f2bb2011-06-12 21:37:13 +02001184 PyEval_InitThreads();
Bram Moolenaarefc0d942020-10-11 18:05:02 +02001185#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001186#ifdef DYNAMIC_PYTHON3
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001187 get_py3_exceptions();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001188#endif
1189
Bram Moolenaar1dc28782013-05-21 19:11:01 +02001190 if (PythonIO_Init_io())
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001191 goto fail;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001192
Bram Moolenaardb913952012-06-29 12:54:53 +02001193 globals = PyModule_GetDict(PyImport_AddModule("__main__"));
1194
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001195 // Remove the element from sys.path that was added because of our
1196 // argv[0] value in Py3Init_vim(). Previously we used an empty
1197 // string, but depending on the OS we then get an empty entry or
1198 // the current directory in sys.path.
1199 // Only after vim has been imported, the element does exist in
1200 // sys.path.
Bram Moolenaar19e60942011-06-19 00:27:51 +02001201 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 +02001202
Bram Moolenaarefc0d942020-10-11 18:05:02 +02001203 // Without the call to PyEval_SaveThread, thread specific state (such
1204 // as the system trace hook), will be lost between invocations of
1205 // Python code.
1206 // GIL may have been created and acquired in PyEval_InitThreads() and
1207 // thread state is created in Py_Initialize(); there
1208 // _PyGILState_NoteThreadState() also sets gilcounter to 1 (python must
1209 // have threads enabled!), so the following does both: unlock GIL and
1210 // save thread state in TLS without deleting thread state
Bram Moolenaar76d711c2013-02-13 14:17:08 +01001211 PyEval_SaveThread();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001212
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001213 py3initialised = 1;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001214 }
1215
1216 return 0;
1217
1218fail:
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001219 // We call PythonIO_Flush() here to print any Python errors.
1220 // This is OK, as it is possible to call this function even
1221 // if PythonIO_Init_io() has not completed successfully (it will
1222 // not do anything in this case).
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001223 PythonIO_Flush();
1224 return -1;
1225}
1226
1227/*
1228 * External interface
1229 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001230 static void
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02001231DoPyCommand(const char *cmd, rangeinitializer init_range, runner run, void *arg)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001232{
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001233#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001234 char *saved_locale;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001235#endif
Bram Moolenaar19e60942011-06-19 00:27:51 +02001236 PyObject *cmdstr;
1237 PyObject *cmdbytes;
Bram Moolenaar71700b82013-05-15 17:49:05 +02001238 PyGILState_STATE pygilstate;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001239
Bram Moolenaarc4f83382017-07-07 14:50:44 +02001240 if (python_end_called)
1241 goto theend;
1242
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001243 if (Python3_Init())
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001244 goto theend;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001245
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02001246 init_range(arg);
1247
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001248 Python_Release_Vim(); // leave Vim
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001249
1250#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001251 // Python only works properly when the LC_NUMERIC locale is "C".
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001252 saved_locale = setlocale(LC_NUMERIC, NULL);
1253 if (saved_locale == NULL || STRCMP(saved_locale, "C") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001254 saved_locale = NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001255 else
1256 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001257 // Need to make a copy, value may change when setting new locale.
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001258 saved_locale = (char *)vim_strsave((char_u *)saved_locale);
1259 (void)setlocale(LC_NUMERIC, "C");
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001260 }
1261#endif
1262
1263 pygilstate = PyGILState_Ensure();
1264
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001265 // PyRun_SimpleString expects a UTF-8 string. Wrong encoding may cause
1266 // SyntaxError (unicode error).
Bram Moolenaar3d64a312011-07-15 15:54:44 +02001267 cmdstr = PyUnicode_Decode(cmd, strlen(cmd),
Bram Moolenaar2e2f52a2020-12-21 16:03:02 +01001268 (char *)ENC_OPT, ERRORS_DECODE_ARG);
1269 cmdbytes = PyUnicode_AsEncodedString(cmdstr, "utf-8", ERRORS_ENCODE_ARG);
Bram Moolenaar19e60942011-06-19 00:27:51 +02001270 Py_XDECREF(cmdstr);
Bram Moolenaardb913952012-06-29 12:54:53 +02001271
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02001272 run(PyBytes_AsString(cmdbytes), arg, &pygilstate);
Bram Moolenaar19e60942011-06-19 00:27:51 +02001273 Py_XDECREF(cmdbytes);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001274
1275 PyGILState_Release(pygilstate);
1276
1277#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
1278 if (saved_locale != NULL)
1279 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001280 (void)setlocale(LC_NUMERIC, saved_locale);
1281 vim_free(saved_locale);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001282 }
1283#endif
1284
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001285 Python_Lock_Vim(); // enter Vim
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001286 PythonIO_Flush();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001287
1288theend:
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001289 return; // keeps lint happy
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001290}
1291
1292/*
Bram Moolenaar368373e2010-07-19 20:46:22 +02001293 * ":py3"
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001294 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001295 void
1296ex_py3(exarg_T *eap)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001297{
1298 char_u *script;
1299
1300 script = script_get(eap, eap->arg);
1301 if (!eap->skip)
1302 {
Bram Moolenaar14816ad2019-02-18 22:04:56 +01001303 if (p_pyx == 0)
1304 p_pyx = 3;
1305
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02001306 DoPyCommand(script == NULL ? (char *) eap->arg : (char *) script,
1307 (rangeinitializer) init_range_cmd,
1308 (runner) run_cmd,
1309 (void *) eap);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001310 }
1311 vim_free(script);
1312}
1313
1314#define BUFFER_SIZE 2048
1315
1316/*
Bram Moolenaar6df6f472010-07-18 18:04:50 +02001317 * ":py3file"
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001318 */
1319 void
1320ex_py3file(exarg_T *eap)
1321{
1322 static char buffer[BUFFER_SIZE];
1323 const char *file;
1324 char *p;
1325 int i;
1326
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01001327 if (p_pyx == 0)
1328 p_pyx = 3;
1329
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001330 // Have to do it like this. PyRun_SimpleFile requires you to pass a
1331 // stdio file pointer, but Vim and the Python DLL are compiled with
1332 // different options under Windows, meaning that stdio pointers aren't
1333 // compatible between the two. Yuk.
1334 //
1335 // construct: exec(compile(open('a_filename', 'rb').read(), 'a_filename', 'exec'))
1336 //
1337 // Using bytes so that Python can detect the source encoding as it normally
1338 // does. The doc does not say "compile" accept bytes, though.
1339 //
1340 // We need to escape any backslashes or single quotes in the file name, so that
1341 // Python won't mangle the file name.
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001342
1343 strcpy(buffer, "exec(compile(open('");
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001344 p = buffer + 19; // size of "exec(compile(open('"
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001345
1346 for (i=0; i<2; ++i)
1347 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001348 file = (char *)eap->arg;
1349 while (*file && p < buffer + (BUFFER_SIZE - 3))
1350 {
1351 if (*file == '\\' || *file == '\'')
1352 *p++ = '\\';
1353 *p++ = *file++;
1354 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001355 // If we didn't finish the file name, we hit a buffer overflow
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001356 if (*file != '\0')
1357 return;
1358 if (i==0)
1359 {
Bram Moolenaar19e60942011-06-19 00:27:51 +02001360 strcpy(p,"','rb').read(),'");
1361 p += 16;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001362 }
1363 else
1364 {
1365 strcpy(p,"','exec'))");
1366 p += 10;
1367 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001368 }
1369
1370
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001371 // Execute the file
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02001372 DoPyCommand(buffer,
1373 (rangeinitializer) init_range_cmd,
1374 (runner) run_cmd,
1375 (void *) eap);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001376}
1377
Bram Moolenaar54e8f002013-05-15 19:44:39 +02001378 void
1379ex_py3do(exarg_T *eap)
Bram Moolenaar3dab2802013-05-15 18:28:13 +02001380{
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01001381 if (p_pyx == 0)
1382 p_pyx = 3;
1383
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02001384 DoPyCommand((char *)eap->arg,
1385 (rangeinitializer)init_range_cmd,
1386 (runner)run_do,
1387 (void *)eap);
Bram Moolenaar3dab2802013-05-15 18:28:13 +02001388}
1389
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001390///////////////////////////////////////////////////////
1391// 2. Python output stream: writes output via [e]msg().
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001392
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001393// Implementation functions
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001394
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001395 static PyObject *
1396OutputGetattro(PyObject *self, PyObject *nameobj)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001397{
Bram Moolenaar77045652012-09-21 13:46:06 +02001398 GET_ATTR_STRING(name, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001399
1400 if (strcmp(name, "softspace") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001401 return PyLong_FromLong(((OutputObject *)(self))->softspace);
Bram Moolenaar6d4431e2016-04-21 20:00:56 +02001402 else if (strcmp(name, "errors") == 0)
1403 return PyString_FromString("strict");
1404 else if (strcmp(name, "encoding") == 0)
1405 return PyString_FromString(ENC_OPT);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001406
1407 return PyObject_GenericGetAttr(self, nameobj);
1408}
1409
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001410 static int
1411OutputSetattro(PyObject *self, PyObject *nameobj, PyObject *val)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001412{
Bram Moolenaar77045652012-09-21 13:46:06 +02001413 GET_ATTR_STRING(name, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001414
Bram Moolenaard6e39182013-05-21 18:30:34 +02001415 return OutputSetattr((OutputObject *)(self), name, val);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001416}
1417
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001418///////////////////////////////////////////////////////
1419// 3. Implementation of the Vim module for Python
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001420
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001421// Window type - Implementation functions
1422// --------------------------------------
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001423
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001424#define WindowType_Check(obj) ((obj)->ob_base.ob_type == &WindowType)
1425
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001426// Buffer type - Implementation functions
1427// --------------------------------------
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001428
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001429#define BufferType_Check(obj) ((obj)->ob_base.ob_type == &BufferType)
1430
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001431static PyObject* BufferSubscript(PyObject *self, PyObject *idx);
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02001432static int BufferAsSubscript(PyObject *self, PyObject *idx, PyObject *val);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001433
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001434// Line range type - Implementation functions
1435// --------------------------------------
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001436
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001437#define RangeType_Check(obj) ((obj)->ob_base.ob_type == &RangeType)
1438
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001439static PyObject* RangeSubscript(PyObject *self, PyObject *idx);
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02001440static int RangeAsItem(PyObject *, Py_ssize_t, PyObject *);
1441static int RangeAsSubscript(PyObject *self, PyObject *idx, PyObject *val);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001442
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001443// Current objects type - Implementation functions
1444// -----------------------------------------------
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001445
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001446static PySequenceMethods BufferAsSeq = {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001447 (lenfunc) BufferLength, // sq_length, len(x)
1448 (binaryfunc) 0, // sq_concat, x+y
1449 (ssizeargfunc) 0, // sq_repeat, x*n
1450 (ssizeargfunc) BufferItem, // sq_item, x[i]
1451 0, // was_sq_slice, x[i:j]
1452 0, // sq_ass_item, x[i]=v
1453 0, // sq_ass_slice, x[i:j]=v
1454 0, // sq_contains
1455 0, // sq_inplace_concat
1456 0, // sq_inplace_repeat
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001457};
1458
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001459static PyMappingMethods BufferAsMapping = {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001460 /* mp_length */ (lenfunc)BufferLength,
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001461 /* mp_subscript */ (binaryfunc)BufferSubscript,
Bram Moolenaar19e60942011-06-19 00:27:51 +02001462 /* mp_ass_subscript */ (objobjargproc)BufferAsSubscript,
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001463};
1464
1465
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001466// Buffer object
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001467
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001468 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02001469BufferGetattro(PyObject *self, PyObject *nameobj)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001470{
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001471 PyObject *r;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001472
Bram Moolenaar77045652012-09-21 13:46:06 +02001473 GET_ATTR_STRING(name, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001474
Bram Moolenaar9e822c02013-05-29 22:15:30 +02001475 if ((r = BufferAttrValid((BufferObject *)(self), name)))
1476 return r;
1477
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001478 if (CheckBuffer((BufferObject *)(self)))
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001479 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001480
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001481 r = BufferAttr((BufferObject *)(self), name);
1482 if (r || PyErr_Occurred())
1483 return r;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001484 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001485 return PyObject_GenericGetAttr(self, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001486}
1487
Bram Moolenaare9ba5162013-05-29 22:02:22 +02001488 static int
1489BufferSetattro(PyObject *self, PyObject *nameobj, PyObject *val)
1490{
1491 GET_ATTR_STRING(name, nameobj);
1492
1493 return BufferSetattr((BufferObject *)(self), name, val);
1494}
1495
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001496//////////////////
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001497
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001498 static PyObject *
1499BufferSubscript(PyObject *self, PyObject* idx)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001500{
Bram Moolenaardb913952012-06-29 12:54:53 +02001501 if (PyLong_Check(idx))
1502 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001503 long _idx = PyLong_AsLong(idx);
Bram Moolenaard6e39182013-05-21 18:30:34 +02001504 return BufferItem((BufferObject *)(self), _idx);
Bram Moolenaardb913952012-06-29 12:54:53 +02001505 } else if (PySlice_Check(idx))
1506 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001507 Py_ssize_t start, stop, step, slicelen;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001508
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02001509 if (CheckBuffer((BufferObject *) self))
1510 return NULL;
1511
Bram Moolenaar922a4662014-03-30 16:11:43 +02001512 if (PySlice_GetIndicesEx((PySliceObject_T *)idx,
Bram Moolenaarbd80f352013-05-12 21:16:23 +02001513 (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count,
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001514 &start, &stop,
Bram Moolenaardb913952012-06-29 12:54:53 +02001515 &step, &slicelen) < 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001516 return NULL;
Bram Moolenaard6e39182013-05-21 18:30:34 +02001517 return BufferSlice((BufferObject *)(self), start, stop);
Bram Moolenaardb913952012-06-29 12:54:53 +02001518 }
1519 else
1520 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001521 RAISE_INVALID_INDEX_TYPE(idx);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001522 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001523 }
1524}
1525
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02001526 static int
Bram Moolenaar19e60942011-06-19 00:27:51 +02001527BufferAsSubscript(PyObject *self, PyObject* idx, PyObject* val)
1528{
Bram Moolenaardb913952012-06-29 12:54:53 +02001529 if (PyLong_Check(idx))
1530 {
Bram Moolenaar19e60942011-06-19 00:27:51 +02001531 long n = PyLong_AsLong(idx);
Bram Moolenaarab589462020-07-06 21:03:06 +02001532
1533 if (CheckBuffer((BufferObject *) self))
1534 return -1;
1535
Bram Moolenaar19e60942011-06-19 00:27:51 +02001536 return RBAsItem((BufferObject *)(self), n, val, 1,
1537 (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count,
1538 NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02001539 } else if (PySlice_Check(idx))
1540 {
Bram Moolenaar19e60942011-06-19 00:27:51 +02001541 Py_ssize_t start, stop, step, slicelen;
1542
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02001543 if (CheckBuffer((BufferObject *) self))
1544 return -1;
1545
Bram Moolenaar922a4662014-03-30 16:11:43 +02001546 if (PySlice_GetIndicesEx((PySliceObject_T *)idx,
Bram Moolenaarbd80f352013-05-12 21:16:23 +02001547 (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count,
Bram Moolenaar19e60942011-06-19 00:27:51 +02001548 &start, &stop,
Bram Moolenaardb913952012-06-29 12:54:53 +02001549 &step, &slicelen) < 0)
Bram Moolenaar19e60942011-06-19 00:27:51 +02001550 return -1;
Bram Moolenaar19e60942011-06-19 00:27:51 +02001551 return RBAsSlice((BufferObject *)(self), start, stop, val, 1,
1552 (PyInt)((BufferObject *)(self))->buf->b_ml.ml_line_count,
1553 NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02001554 }
1555 else
1556 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001557 RAISE_INVALID_INDEX_TYPE(idx);
Bram Moolenaar19e60942011-06-19 00:27:51 +02001558 return -1;
1559 }
1560}
1561
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001562static PySequenceMethods RangeAsSeq = {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001563 (lenfunc) RangeLength, // sq_length, len(x)
1564 (binaryfunc) 0, // RangeConcat, sq_concat, x+y
1565 (ssizeargfunc) 0, // RangeRepeat, sq_repeat, x*n
1566 (ssizeargfunc) RangeItem, // sq_item, x[i]
1567 0, // was_sq_slice, x[i:j]
1568 (ssizeobjargproc) RangeAsItem, // sq_as_item, x[i]=v
1569 0, // sq_ass_slice, x[i:j]=v
1570 0, // sq_contains
1571 0, // sq_inplace_concat
1572 0, // sq_inplace_repeat
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001573};
1574
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001575static PyMappingMethods RangeAsMapping = {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001576 /* mp_length */ (lenfunc)RangeLength,
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001577 /* mp_subscript */ (binaryfunc)RangeSubscript,
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001578 /* mp_ass_subscript */ (objobjargproc)RangeAsSubscript,
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001579};
1580
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001581// Line range object - Implementation
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001582
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001583 static PyObject *
1584RangeGetattro(PyObject *self, PyObject *nameobj)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001585{
Bram Moolenaar77045652012-09-21 13:46:06 +02001586 GET_ATTR_STRING(name, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001587
1588 if (strcmp(name, "start") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001589 return Py_BuildValue("n", ((RangeObject *)(self))->start - 1);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001590 else if (strcmp(name, "end") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001591 return Py_BuildValue("n", ((RangeObject *)(self))->end - 1);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001592 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001593 return PyObject_GenericGetAttr(self, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001594}
1595
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001596////////////////
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001597
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02001598 static int
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001599RangeAsItem(PyObject *self, Py_ssize_t n, PyObject *val)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001600{
1601 return RBAsItem(((RangeObject *)(self))->buf, n, val,
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001602 ((RangeObject *)(self))->start,
1603 ((RangeObject *)(self))->end,
1604 &((RangeObject *)(self))->end);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001605}
1606
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001607 static Py_ssize_t
1608RangeAsSlice(PyObject *self, Py_ssize_t lo, Py_ssize_t hi, PyObject *val)
1609{
1610 return RBAsSlice(((RangeObject *)(self))->buf, lo, hi, val,
1611 ((RangeObject *)(self))->start,
1612 ((RangeObject *)(self))->end,
1613 &((RangeObject *)(self))->end);
1614}
1615
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001616 static PyObject *
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001617RangeSubscript(PyObject *self, PyObject* idx)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001618{
Bram Moolenaardb913952012-06-29 12:54:53 +02001619 if (PyLong_Check(idx))
1620 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001621 long _idx = PyLong_AsLong(idx);
Bram Moolenaard6e39182013-05-21 18:30:34 +02001622 return RangeItem((RangeObject *)(self), _idx);
Bram Moolenaardb913952012-06-29 12:54:53 +02001623 } else if (PySlice_Check(idx))
1624 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001625 Py_ssize_t start, stop, step, slicelen;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001626
Bram Moolenaar922a4662014-03-30 16:11:43 +02001627 if (PySlice_GetIndicesEx((PySliceObject_T *)idx,
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001628 ((RangeObject *)(self))->end-((RangeObject *)(self))->start+1,
1629 &start, &stop,
Bram Moolenaardb913952012-06-29 12:54:53 +02001630 &step, &slicelen) < 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001631 return NULL;
Bram Moolenaard6e39182013-05-21 18:30:34 +02001632 return RangeSlice((RangeObject *)(self), start, stop);
Bram Moolenaardb913952012-06-29 12:54:53 +02001633 }
1634 else
1635 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001636 RAISE_INVALID_INDEX_TYPE(idx);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001637 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001638 }
1639}
1640
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02001641 static int
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001642RangeAsSubscript(PyObject *self, PyObject *idx, PyObject *val)
1643{
Bram Moolenaardb913952012-06-29 12:54:53 +02001644 if (PyLong_Check(idx))
1645 {
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001646 long n = PyLong_AsLong(idx);
1647 return RangeAsItem(self, n, val);
Bram Moolenaarabab0b02019-03-30 18:47:01 +01001648 }
1649 else if (PySlice_Check(idx))
Bram Moolenaardb913952012-06-29 12:54:53 +02001650 {
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001651 Py_ssize_t start, stop, step, slicelen;
1652
Bram Moolenaar922a4662014-03-30 16:11:43 +02001653 if (PySlice_GetIndicesEx((PySliceObject_T *)idx,
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001654 ((RangeObject *)(self))->end-((RangeObject *)(self))->start+1,
1655 &start, &stop,
Bram Moolenaardb913952012-06-29 12:54:53 +02001656 &step, &slicelen) < 0)
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001657 return -1;
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001658 return RangeAsSlice(self, start, stop, val);
Bram Moolenaardb913952012-06-29 12:54:53 +02001659 }
1660 else
1661 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001662 RAISE_INVALID_INDEX_TYPE(idx);
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001663 return -1;
1664 }
1665}
1666
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001667// TabPage object - Implementation
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001668
1669 static PyObject *
1670TabPageGetattro(PyObject *self, PyObject *nameobj)
1671{
1672 PyObject *r;
1673
1674 GET_ATTR_STRING(name, nameobj);
1675
Bram Moolenaar9e822c02013-05-29 22:15:30 +02001676 if ((r = TabPageAttrValid((TabPageObject *)(self), name)))
1677 return r;
1678
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001679 if (CheckTabPage((TabPageObject *)(self)))
1680 return NULL;
1681
1682 r = TabPageAttr((TabPageObject *)(self), name);
1683 if (r || PyErr_Occurred())
1684 return r;
1685 else
1686 return PyObject_GenericGetAttr(self, nameobj);
1687}
1688
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001689// Window object - Implementation
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001690
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001691 static PyObject *
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001692WindowGetattro(PyObject *self, PyObject *nameobj)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001693{
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001694 PyObject *r;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001695
Bram Moolenaar77045652012-09-21 13:46:06 +02001696 GET_ATTR_STRING(name, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001697
Bram Moolenaar9e822c02013-05-29 22:15:30 +02001698 if ((r = WindowAttrValid((WindowObject *)(self), name)))
1699 return r;
1700
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001701 if (CheckWindow((WindowObject *)(self)))
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001702 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001703
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001704 r = WindowAttr((WindowObject *)(self), name);
1705 if (r || PyErr_Occurred())
1706 return r;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001707 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001708 return PyObject_GenericGetAttr(self, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001709}
1710
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001711 static int
1712WindowSetattro(PyObject *self, PyObject *nameobj, PyObject *val)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001713{
Bram Moolenaar77045652012-09-21 13:46:06 +02001714 GET_ATTR_STRING(name, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001715
Bram Moolenaard6e39182013-05-21 18:30:34 +02001716 return WindowSetattr((WindowObject *)(self), name, val);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001717}
1718
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001719// Tab page list object - Definitions
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001720
1721static PySequenceMethods TabListAsSeq = {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001722 (lenfunc) TabListLength, // sq_length, len(x)
1723 (binaryfunc) 0, // sq_concat, x+y
1724 (ssizeargfunc) 0, // sq_repeat, x*n
1725 (ssizeargfunc) TabListItem, // sq_item, x[i]
1726 0, // sq_slice, x[i:j]
1727 (ssizeobjargproc)0, // sq_as_item, x[i]=v
1728 0, // sq_ass_slice, x[i:j]=v
1729 0, // sq_contains
1730 0, // sq_inplace_concat
1731 0, // sq_inplace_repeat
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001732};
1733
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001734// Window list object - Definitions
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001735
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001736static PySequenceMethods WinListAsSeq = {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001737 (lenfunc) WinListLength, // sq_length, len(x)
1738 (binaryfunc) 0, // sq_concat, x+y
1739 (ssizeargfunc) 0, // sq_repeat, x*n
1740 (ssizeargfunc) WinListItem, // sq_item, x[i]
1741 0, // sq_slice, x[i:j]
1742 (ssizeobjargproc)0, // sq_as_item, x[i]=v
1743 0, // sq_ass_slice, x[i:j]=v
1744 0, // sq_contains
1745 0, // sq_inplace_concat
1746 0, // sq_inplace_repeat
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001747};
1748
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001749/*
1750 * Current items object - Implementation
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001751 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001752 static PyObject *
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001753CurrentGetattro(PyObject *self, PyObject *nameobj)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001754{
Bram Moolenaardd8aca62013-05-29 22:36:10 +02001755 PyObject *r;
Bram Moolenaar77045652012-09-21 13:46:06 +02001756 GET_ATTR_STRING(name, nameobj);
Bram Moolenaardd8aca62013-05-29 22:36:10 +02001757 if (!(r = CurrentGetattr(self, name)))
1758 return PyObject_GenericGetAttr(self, nameobj);
1759 return r;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001760}
1761
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001762 static int
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001763CurrentSetattro(PyObject *self, PyObject *nameobj, PyObject *value)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001764{
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001765 GET_ATTR_STRING(name, nameobj);
1766 return CurrentSetattr(self, name, value);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001767}
1768
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001769// Dictionary object - Definitions
Bram Moolenaardb913952012-06-29 12:54:53 +02001770
Bram Moolenaar66b79852012-09-21 14:00:35 +02001771 static PyObject *
1772DictionaryGetattro(PyObject *self, PyObject *nameobj)
1773{
1774 DictionaryObject *this = ((DictionaryObject *) (self));
1775
1776 GET_ATTR_STRING(name, nameobj);
1777
1778 if (strcmp(name, "locked") == 0)
1779 return PyLong_FromLong(this->dict->dv_lock);
1780 else if (strcmp(name, "scope") == 0)
1781 return PyLong_FromLong(this->dict->dv_scope);
1782
1783 return PyObject_GenericGetAttr(self, nameobj);
1784}
1785
1786 static int
1787DictionarySetattro(PyObject *self, PyObject *nameobj, PyObject *val)
1788{
1789 GET_ATTR_STRING(name, nameobj);
Bram Moolenaard6e39182013-05-21 18:30:34 +02001790 return DictionarySetattr((DictionaryObject *)(self), name, val);
Bram Moolenaardb913952012-06-29 12:54:53 +02001791}
1792
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001793// List object - Definitions
Bram Moolenaardb913952012-06-29 12:54:53 +02001794
Bram Moolenaar66b79852012-09-21 14:00:35 +02001795 static PyObject *
1796ListGetattro(PyObject *self, PyObject *nameobj)
1797{
1798 GET_ATTR_STRING(name, nameobj);
1799
1800 if (strcmp(name, "locked") == 0)
1801 return PyLong_FromLong(((ListObject *) (self))->list->lv_lock);
1802
1803 return PyObject_GenericGetAttr(self, nameobj);
1804}
1805
1806 static int
1807ListSetattro(PyObject *self, PyObject *nameobj, PyObject *val)
1808{
1809 GET_ATTR_STRING(name, nameobj);
Bram Moolenaard6e39182013-05-21 18:30:34 +02001810 return ListSetattr((ListObject *)(self), name, val);
Bram Moolenaardb913952012-06-29 12:54:53 +02001811}
1812
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001813// Function object - Definitions
Bram Moolenaardb913952012-06-29 12:54:53 +02001814
Bram Moolenaardb913952012-06-29 12:54:53 +02001815 static PyObject *
1816FunctionGetattro(PyObject *self, PyObject *nameobj)
1817{
Bram Moolenaar8110a092016-04-14 15:56:09 +02001818 PyObject *r;
Bram Moolenaardb913952012-06-29 12:54:53 +02001819 FunctionObject *this = (FunctionObject *)(self);
Bram Moolenaar77045652012-09-21 13:46:06 +02001820
1821 GET_ATTR_STRING(name, nameobj);
Bram Moolenaardb913952012-06-29 12:54:53 +02001822
Bram Moolenaar8110a092016-04-14 15:56:09 +02001823 r = FunctionAttr(this, name);
1824 if (r || PyErr_Occurred())
1825 return r;
1826 else
1827 return PyObject_GenericGetAttr(self, nameobj);
Bram Moolenaardb913952012-06-29 12:54:53 +02001828}
1829
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001830// External interface
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001831
1832 void
1833python3_buffer_free(buf_T *buf)
1834{
Bram Moolenaar971db462013-05-12 18:44:48 +02001835 if (BUF_PYTHON_REF(buf) != NULL)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001836 {
Bram Moolenaar971db462013-05-12 18:44:48 +02001837 BufferObject *bp = BUF_PYTHON_REF(buf);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001838 bp->buf = INVALID_BUFFER_VALUE;
Bram Moolenaar971db462013-05-12 18:44:48 +02001839 BUF_PYTHON_REF(buf) = NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001840 }
1841}
1842
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001843 void
1844python3_window_free(win_T *win)
1845{
Bram Moolenaar971db462013-05-12 18:44:48 +02001846 if (WIN_PYTHON_REF(win) != NULL)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001847 {
Bram Moolenaar971db462013-05-12 18:44:48 +02001848 WindowObject *wp = WIN_PYTHON_REF(win);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001849 wp->win = INVALID_WINDOW_VALUE;
Bram Moolenaar971db462013-05-12 18:44:48 +02001850 WIN_PYTHON_REF(win) = NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001851 }
1852}
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001853
1854 void
1855python3_tabpage_free(tabpage_T *tab)
1856{
1857 if (TAB_PYTHON_REF(tab) != NULL)
1858 {
1859 TabPageObject *tp = TAB_PYTHON_REF(tab);
1860 tp->tab = INVALID_TABPAGE_VALUE;
1861 TAB_PYTHON_REF(tab) = NULL;
1862 }
1863}
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}