blob: c7db9d7a2a8b9160e917a8c481939d17ced973ff [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9/*
10 * Python extensions by Paul Moore.
11 * Changes for Unix by David Leonard.
12 *
13 * This consists of four parts:
14 * 1. Python interpreter main program
15 * 2. Python output stream: writes output via [e]msg().
16 * 3. Implementation of the Vim module for Python
17 * 4. Utility functions for handling the interface between Vim and Python.
18 */
19
20/*
21 * Roland Puntaier 2009/sept/16:
22 * Adaptations to support both python3.x and python2.x
23 */
24
Bram Moolenaar2ab2e862019-12-04 21:24:53 +010025// uncomment this if used with the debug version of python
26// #define Py_DEBUG
27// Note: most of time you can add -DPy_DEBUG to CFLAGS in place of uncommenting
28// uncomment this if used with the debug version of python, but without its
29// allocator
30// #define Py_DEBUG_NO_PYMALLOC
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020031
32#include "vim.h"
33
34#include <limits.h>
35
Bram Moolenaar4f974752019-02-17 17:44:42 +010036#if defined(MSWIN) && defined(HAVE_FCNTL_H)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020037# undef HAVE_FCNTL_H
38#endif
39
40#ifdef _DEBUG
41# undef _DEBUG
42#endif
43
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020044#ifdef F_BLANK
45# undef F_BLANK
46#endif
47
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010048#ifdef HAVE_STRFTIME
49# undef HAVE_STRFTIME
50#endif
51#ifdef HAVE_STRING_H
52# undef HAVE_STRING_H
53#endif
54#ifdef HAVE_PUTENV
55# undef HAVE_PUTENV
56#endif
Bram Moolenaar6df6f472010-07-18 18:04:50 +020057#ifdef HAVE_STDARG_H
Bram Moolenaar2ab2e862019-12-04 21:24:53 +010058# undef HAVE_STDARG_H // Python's config.h defines it as well.
Bram Moolenaar6df6f472010-07-18 18:04:50 +020059#endif
Bram Moolenaar2ab2e862019-12-04 21:24:53 +010060#ifdef _POSIX_C_SOURCE // defined in feature.h
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020061# undef _POSIX_C_SOURCE
62#endif
Bram Moolenaar6df6f472010-07-18 18:04:50 +020063#ifdef _XOPEN_SOURCE
Bram Moolenaar2ab2e862019-12-04 21:24:53 +010064# undef _XOPEN_SOURCE // pyconfig.h defines it as well.
Bram Moolenaar6df6f472010-07-18 18:04:50 +020065#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020066
Bram Moolenaar0bdda372013-06-10 18:36:24 +020067#define PY_SSIZE_T_CLEAN
68
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020069#include <Python.h>
Bram Moolenaar0bdda372013-06-10 18:36:24 +020070
Bram Moolenaar2ab2e862019-12-04 21:24:53 +010071#undef main // Defined in python.h - aargh
72#undef HAVE_FCNTL_H // Clash with os_win32.h
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020073
Bram Moolenaar2ab2e862019-12-04 21:24:53 +010074// The "surrogateescape" error handler is new in Python 3.1
Bram Moolenaar3d64a312011-07-15 15:54:44 +020075#if PY_VERSION_HEX >= 0x030100f0
76# define CODEC_ERROR_HANDLER "surrogateescape"
77#else
78# define CODEC_ERROR_HANDLER NULL
79#endif
80
Bram Moolenaar2ab2e862019-12-04 21:24:53 +010081// Python 3 does not support CObjects, always use Capsules
Bram Moolenaar2afa3232012-06-29 16:28:28 +020082#define PY_USE_CAPSULE
83
Bram Moolenaar2e2f52a2020-12-21 16:03:02 +010084#define ERRORS_DECODE_ARG CODEC_ERROR_HANDLER
85#define ERRORS_ENCODE_ARG ERRORS_DECODE_ARG
86
Bram Moolenaar170bf1a2010-07-24 23:51:45 +020087#define PyInt Py_ssize_t
Bram Moolenaar32ac8cd2013-07-03 18:49:17 +020088#ifndef PyString_Check
89# define PyString_Check(obj) PyUnicode_Check(obj)
90#endif
Bram Moolenaare0324612013-07-09 17:30:55 +020091#define PyString_FromString(repr) \
Bram Moolenaar2e2f52a2020-12-21 16:03:02 +010092 PyUnicode_Decode(repr, STRLEN(repr), ENC_OPT, ERRORS_DECODE_ARG)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +020093#define PyString_FromFormat PyUnicode_FromFormat
Bram Moolenaar32ac8cd2013-07-03 18:49:17 +020094#ifndef PyInt_Check
95# define PyInt_Check(obj) PyLong_Check(obj)
96#endif
Bram Moolenaar77045652012-09-21 13:46:06 +020097#define PyInt_FromLong(i) PyLong_FromLong(i)
98#define PyInt_AsLong(obj) PyLong_AsLong(obj)
Bram Moolenaar4d1da492013-04-24 13:39:15 +020099#define Py_ssize_t_fmt "n"
Bram Moolenaara9922d62013-05-30 13:01:18 +0200100#define Py_bytes_fmt "y"
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200101
Bram Moolenaar063a46b2014-01-14 16:36:51 +0100102#define PyIntArgFunc ssizeargfunc
103#define PyIntObjArgProc ssizeobjargproc
104
Bram Moolenaar922a4662014-03-30 16:11:43 +0200105/*
106 * PySlice_GetIndicesEx(): first argument type changed from PySliceObject
107 * to PyObject in Python 3.2 or later.
108 */
109#if PY_VERSION_HEX >= 0x030200f0
110typedef PyObject PySliceObject_T;
111#else
112typedef PySliceObject PySliceObject_T;
113#endif
114
Bram Moolenaar0c1f3f42011-02-25 15:18:50 +0100115#if defined(DYNAMIC_PYTHON3) || defined(PROTO)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200116
Bram Moolenaar4f974752019-02-17 17:44:42 +0100117# ifndef MSWIN
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200118# include <dlfcn.h>
119# define FARPROC void*
120# define HINSTANCE void*
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100121# if defined(PY_NO_RTLD_GLOBAL) && defined(PY3_NO_RTLD_GLOBAL)
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200122# define load_dll(n) dlopen((n), RTLD_LAZY)
123# else
124# define load_dll(n) dlopen((n), RTLD_LAZY|RTLD_GLOBAL)
125# endif
126# define close_dll dlclose
127# define symbol_from_dll dlsym
128# else
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200129# define load_dll vimLoadLib
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200130# define close_dll FreeLibrary
131# define symbol_from_dll GetProcAddress
132# endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200133/*
134 * Wrapper defines
135 */
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200136# undef PyArg_Parse
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200137# define PyArg_Parse py3_PyArg_Parse
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200138# undef PyArg_ParseTuple
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200139# define PyArg_ParseTuple py3_PyArg_ParseTuple
Bram Moolenaar19e60942011-06-19 00:27:51 +0200140# define PyMem_Free py3_PyMem_Free
Bram Moolenaardb913952012-06-29 12:54:53 +0200141# define PyMem_Malloc py3_PyMem_Malloc
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200142# define PyDict_SetItemString py3_PyDict_SetItemString
143# define PyErr_BadArgument py3_PyErr_BadArgument
144# define PyErr_Clear py3_PyErr_Clear
Bram Moolenaarc476e522013-06-23 13:46:40 +0200145# define PyErr_Format py3_PyErr_Format
Bram Moolenaar4d369872013-02-20 16:09:43 +0100146# define PyErr_PrintEx py3_PyErr_PrintEx
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200147# define PyErr_NoMemory py3_PyErr_NoMemory
148# define PyErr_Occurred py3_PyErr_Occurred
149# define PyErr_SetNone py3_PyErr_SetNone
150# define PyErr_SetString py3_PyErr_SetString
Bram Moolenaar4d188da2013-05-15 15:35:09 +0200151# define PyErr_SetObject py3_PyErr_SetObject
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200152# define PyErr_ExceptionMatches py3_PyErr_ExceptionMatches
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200153# define PyEval_InitThreads py3_PyEval_InitThreads
154# define PyEval_RestoreThread py3_PyEval_RestoreThread
155# define PyEval_SaveThread py3_PyEval_SaveThread
156# define PyGILState_Ensure py3_PyGILState_Ensure
157# define PyGILState_Release py3_PyGILState_Release
158# define PyLong_AsLong py3_PyLong_AsLong
159# define PyLong_FromLong py3_PyLong_FromLong
160# define PyList_GetItem py3_PyList_GetItem
161# define PyList_Append py3_PyList_Append
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200162# define PyList_Insert py3_PyList_Insert
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200163# define PyList_New py3_PyList_New
164# define PyList_SetItem py3_PyList_SetItem
165# define PyList_Size py3_PyList_Size
Bram Moolenaardb913952012-06-29 12:54:53 +0200166# define PySequence_Check py3_PySequence_Check
167# define PySequence_Size py3_PySequence_Size
168# define PySequence_GetItem py3_PySequence_GetItem
Bram Moolenaara9922d62013-05-30 13:01:18 +0200169# define PySequence_Fast py3_PySequence_Fast
Bram Moolenaardb913952012-06-29 12:54:53 +0200170# define PyTuple_Size py3_PyTuple_Size
171# define PyTuple_GetItem py3_PyTuple_GetItem
Bram Moolenaar3b48b112018-07-04 22:03:25 +0200172# if PY_VERSION_HEX >= 0x030601f0
173# define PySlice_AdjustIndices py3_PySlice_AdjustIndices
174# define PySlice_Unpack py3_PySlice_Unpack
175# endif
176# undef PySlice_GetIndicesEx
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200177# define PySlice_GetIndicesEx py3_PySlice_GetIndicesEx
178# define PyImport_ImportModule py3_PyImport_ImportModule
179# define PyObject_Init py3__PyObject_Init
180# define PyDict_New py3_PyDict_New
181# define PyDict_GetItemString py3_PyDict_GetItemString
Bram Moolenaardb913952012-06-29 12:54:53 +0200182# define PyDict_Next py3_PyDict_Next
183# define PyMapping_Check py3_PyMapping_Check
Bram Moolenaar32ac8cd2013-07-03 18:49:17 +0200184# ifndef PyMapping_Keys
185# define PyMapping_Keys py3_PyMapping_Keys
186# endif
Zdenek Dohnal90478f32021-06-14 15:08:30 +0200187# if PY_VERSION_HEX >= 0x030a00b2
188# define PyIter_Check py3_PyIter_Check
189# endif
Bram Moolenaardb913952012-06-29 12:54:53 +0200190# define PyIter_Next py3_PyIter_Next
191# define PyObject_GetIter py3_PyObject_GetIter
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200192# define PyObject_Repr py3_PyObject_Repr
Bram Moolenaarbcb40972013-05-30 13:22:13 +0200193# define PyObject_GetItem py3_PyObject_GetItem
Bram Moolenaar03db85b2013-05-15 14:51:35 +0200194# define PyObject_IsTrue py3_PyObject_IsTrue
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200195# define PyModule_GetDict py3_PyModule_GetDict
196#undef PyRun_SimpleString
197# define PyRun_SimpleString py3_PyRun_SimpleString
Bram Moolenaardb913952012-06-29 12:54:53 +0200198#undef PyRun_String
199# define PyRun_String py3_PyRun_String
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200200# define PyObject_GetAttrString py3_PyObject_GetAttrString
Bram Moolenaara9922d62013-05-30 13:01:18 +0200201# define PyObject_HasAttrString py3_PyObject_HasAttrString
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200202# define PyObject_SetAttrString py3_PyObject_SetAttrString
203# define PyObject_CallFunctionObjArgs py3_PyObject_CallFunctionObjArgs
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200204# define _PyObject_CallFunction_SizeT py3__PyObject_CallFunction_SizeT
Bram Moolenaarf4258302013-06-02 18:20:17 +0200205# define PyObject_Call py3_PyObject_Call
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200206# define PyEval_GetLocals py3_PyEval_GetLocals
207# define PyEval_GetGlobals py3_PyEval_GetGlobals
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200208# define PySys_SetObject py3_PySys_SetObject
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200209# define PySys_GetObject py3_PySys_GetObject
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200210# define PySys_SetArgv py3_PySys_SetArgv
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200211# define PyType_Ready py3_PyType_Ready
Bram Moolenaaree1b9312020-07-16 22:15:53 +0200212# if PY_VERSION_HEX >= 0x030900b0
213# define PyType_GetFlags py3_PyType_GetFlags
214# endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200215#undef Py_BuildValue
216# define Py_BuildValue py3_Py_BuildValue
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100217# define Py_SetPythonHome py3_Py_SetPythonHome
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200218# define Py_Initialize py3_Py_Initialize
219# define Py_Finalize py3_Py_Finalize
220# define Py_IsInitialized py3_Py_IsInitialized
221# define _Py_NoneStruct (*py3__Py_NoneStruct)
Bram Moolenaar66b79852012-09-21 14:00:35 +0200222# define _Py_FalseStruct (*py3__Py_FalseStruct)
223# define _Py_TrueStruct (*py3__Py_TrueStruct)
Bram Moolenaardb913952012-06-29 12:54:53 +0200224# define _PyObject_NextNotImplemented (*py3__PyObject_NextNotImplemented)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200225# define PyModule_AddObject py3_PyModule_AddObject
226# define PyImport_AppendInittab py3_PyImport_AppendInittab
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200227# define PyImport_AddModule py3_PyImport_AddModule
Bram Moolenaar7bc4f932012-10-14 03:22:56 +0200228# if PY_VERSION_HEX >= 0x030300f0
229# undef _PyUnicode_AsString
Bram Moolenaar9c9cbf12012-10-23 05:17:37 +0200230# define _PyUnicode_AsString py3_PyUnicode_AsUTF8
Bram Moolenaar7bc4f932012-10-14 03:22:56 +0200231# else
232# define _PyUnicode_AsString py3__PyUnicode_AsString
233# endif
Bram Moolenaar19e60942011-06-19 00:27:51 +0200234# undef PyUnicode_AsEncodedString
235# define PyUnicode_AsEncodedString py3_PyUnicode_AsEncodedString
236# undef PyBytes_AsString
237# define PyBytes_AsString py3_PyBytes_AsString
Bram Moolenaar32ac8cd2013-07-03 18:49:17 +0200238# ifndef PyBytes_AsStringAndSize
239# define PyBytes_AsStringAndSize py3_PyBytes_AsStringAndSize
240# endif
Bram Moolenaardb913952012-06-29 12:54:53 +0200241# undef PyBytes_FromString
242# define PyBytes_FromString py3_PyBytes_FromString
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100243# undef PyBytes_FromStringAndSize
244# define PyBytes_FromStringAndSize py3_PyBytes_FromStringAndSize
Bram Moolenaaree1b9312020-07-16 22:15:53 +0200245# if defined(Py_DEBUG) || PY_VERSION_HEX >= 0x030900b0
246# define _Py_Dealloc py3__Py_Dealloc
247# endif
Bram Moolenaardb913952012-06-29 12:54:53 +0200248# define PyFloat_FromDouble py3_PyFloat_FromDouble
249# define PyFloat_AsDouble py3_PyFloat_AsDouble
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200250# define PyObject_GenericGetAttr py3_PyObject_GenericGetAttr
Bram Moolenaar66b79852012-09-21 14:00:35 +0200251# define PyType_Type (*py3_PyType_Type)
Bram Moolenaard4a8c982018-05-15 22:31:18 +0200252# define PyStdPrinter_Type (*py3_PyStdPrinter_Type)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200253# define PySlice_Type (*py3_PySlice_Type)
Bram Moolenaardb913952012-06-29 12:54:53 +0200254# define PyFloat_Type (*py3_PyFloat_Type)
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200255# define PyNumber_Check (*py3_PyNumber_Check)
256# define PyNumber_Long (*py3_PyNumber_Long)
Bram Moolenaar66b79852012-09-21 14:00:35 +0200257# define PyBool_Type (*py3_PyBool_Type)
Bram Moolenaar19e60942011-06-19 00:27:51 +0200258# define PyErr_NewException py3_PyErr_NewException
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200259# ifdef Py_DEBUG
260# define _Py_NegativeRefcount py3__Py_NegativeRefcount
261# define _Py_RefTotal (*py3__Py_RefTotal)
Bram Moolenaar0014a532013-05-29 21:33:39 +0200262# define PyModule_Create2TraceRefs py3_PyModule_Create2TraceRefs
263# else
264# define PyModule_Create2 py3_PyModule_Create2
265# endif
266# if defined(Py_DEBUG) && !defined(Py_DEBUG_NO_PYMALLOC)
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200267# define _PyObject_DebugMalloc py3__PyObject_DebugMalloc
268# define _PyObject_DebugFree py3__PyObject_DebugFree
269# else
270# define PyObject_Malloc py3_PyObject_Malloc
271# define PyObject_Free py3_PyObject_Free
272# endif
Bram Moolenaar774267b2013-05-21 20:51:59 +0200273# define _PyObject_GC_New py3__PyObject_GC_New
274# define PyObject_GC_Del py3_PyObject_GC_Del
275# define PyObject_GC_UnTrack py3_PyObject_GC_UnTrack
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200276# define PyType_GenericAlloc py3_PyType_GenericAlloc
277# define PyType_GenericNew py3_PyType_GenericNew
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200278# undef PyUnicode_FromString
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200279# define PyUnicode_FromString py3_PyUnicode_FromString
Bram Moolenaar1a3b5692013-05-30 12:40:39 +0200280# ifndef PyUnicode_FromFormat
281# define PyUnicode_FromFormat py3_PyUnicode_FromFormat
282# else
283# define Py_UNICODE_USE_UCS_FUNCTIONS
284# ifdef Py_UNICODE_WIDE
285# define PyUnicodeUCS4_FromFormat py3_PyUnicodeUCS4_FromFormat
286# else
287# define PyUnicodeUCS2_FromFormat py3_PyUnicodeUCS2_FromFormat
288# endif
289# endif
Bram Moolenaar19e60942011-06-19 00:27:51 +0200290# undef PyUnicode_Decode
291# define PyUnicode_Decode py3_PyUnicode_Decode
Bram Moolenaardb913952012-06-29 12:54:53 +0200292# define PyType_IsSubtype py3_PyType_IsSubtype
293# define PyCapsule_New py3_PyCapsule_New
294# define PyCapsule_GetPointer py3_PyCapsule_GetPointer
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200295
Bram Moolenaar0014a532013-05-29 21:33:39 +0200296# if defined(Py_DEBUG) && !defined(Py_DEBUG_NO_PYMALLOC)
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200297# undef PyObject_NEW
298# define PyObject_NEW(type, typeobj) \
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200299( (type *) PyObject_Init( \
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200300 (PyObject *) _PyObject_DebugMalloc( _PyObject_SIZE(typeobj) ), (typeobj)) )
Bram Moolenaaree1b9312020-07-16 22:15:53 +0200301# elif PY_VERSION_HEX >= 0x030900b0
302# undef PyObject_NEW
303# define PyObject_NEW(type, typeobj) \
304 ((type *)py3__PyObject_New(typeobj))
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200305# endif
306
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200307/*
308 * Pointers for dynamic link
309 */
310static int (*py3_PySys_SetArgv)(int, wchar_t **);
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100311static void (*py3_Py_SetPythonHome)(wchar_t *home);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200312static void (*py3_Py_Initialize)(void);
313static PyObject* (*py3_PyList_New)(Py_ssize_t size);
314static PyGILState_STATE (*py3_PyGILState_Ensure)(void);
315static void (*py3_PyGILState_Release)(PyGILState_STATE);
316static int (*py3_PySys_SetObject)(char *, PyObject *);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200317static PyObject* (*py3_PySys_GetObject)(char *);
318static int (*py3_PyList_Append)(PyObject *, PyObject *);
319static int (*py3_PyList_Insert)(PyObject *, int, PyObject *);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200320static Py_ssize_t (*py3_PyList_Size)(PyObject *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200321static int (*py3_PySequence_Check)(PyObject *);
322static Py_ssize_t (*py3_PySequence_Size)(PyObject *);
323static PyObject* (*py3_PySequence_GetItem)(PyObject *, Py_ssize_t);
Bram Moolenaara9922d62013-05-30 13:01:18 +0200324static PyObject* (*py3_PySequence_Fast)(PyObject *, const char *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200325static Py_ssize_t (*py3_PyTuple_Size)(PyObject *);
326static PyObject* (*py3_PyTuple_GetItem)(PyObject *, Py_ssize_t);
327static int (*py3_PyMapping_Check)(PyObject *);
Bram Moolenaarbcb40972013-05-30 13:22:13 +0200328static PyObject* (*py3_PyMapping_Keys)(PyObject *);
Bram Moolenaar3b48b112018-07-04 22:03:25 +0200329# if PY_VERSION_HEX >= 0x030601f0
330static int (*py3_PySlice_AdjustIndices)(Py_ssize_t length,
331 Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t step);
332static int (*py3_PySlice_Unpack)(PyObject *slice,
333 Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t *step);
334# endif
Bram Moolenaar922a4662014-03-30 16:11:43 +0200335static int (*py3_PySlice_GetIndicesEx)(PySliceObject_T *r, Py_ssize_t length,
Bram Moolenaar063a46b2014-01-14 16:36:51 +0100336 Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t *step,
337 Py_ssize_t *slicelen);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200338static PyObject* (*py3_PyErr_NoMemory)(void);
339static void (*py3_Py_Finalize)(void);
340static void (*py3_PyErr_SetString)(PyObject *, const char *);
Bram Moolenaar4d188da2013-05-15 15:35:09 +0200341static void (*py3_PyErr_SetObject)(PyObject *, PyObject *);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200342static int (*py3_PyErr_ExceptionMatches)(PyObject *);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200343static int (*py3_PyRun_SimpleString)(char *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200344static PyObject* (*py3_PyRun_String)(char *, int, PyObject *, PyObject *);
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200345static PyObject* (*py3_PyObject_GetAttrString)(PyObject *, const char *);
Bram Moolenaara9922d62013-05-30 13:01:18 +0200346static int (*py3_PyObject_HasAttrString)(PyObject *, const char *);
Bram Moolenaar0b400082013-11-03 00:28:25 +0100347static int (*py3_PyObject_SetAttrString)(PyObject *, const char *, PyObject *);
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200348static PyObject* (*py3_PyObject_CallFunctionObjArgs)(PyObject *, ...);
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200349static PyObject* (*py3__PyObject_CallFunction_SizeT)(PyObject *, char *, ...);
Bram Moolenaarf4258302013-06-02 18:20:17 +0200350static PyObject* (*py3_PyObject_Call)(PyObject *, PyObject *, PyObject *);
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200351static PyObject* (*py3_PyEval_GetGlobals)();
352static PyObject* (*py3_PyEval_GetLocals)();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200353static PyObject* (*py3_PyList_GetItem)(PyObject *, Py_ssize_t);
354static PyObject* (*py3_PyImport_ImportModule)(const char *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200355static PyObject* (*py3_PyImport_AddModule)(const char *);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200356static int (*py3_PyErr_BadArgument)(void);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200357static PyObject* (*py3_PyErr_Occurred)(void);
358static PyObject* (*py3_PyModule_GetDict)(PyObject *);
359static int (*py3_PyList_SetItem)(PyObject *, Py_ssize_t, PyObject *);
360static PyObject* (*py3_PyDict_GetItemString)(PyObject *, const char *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200361static int (*py3_PyDict_Next)(PyObject *, Py_ssize_t *, PyObject **, PyObject **);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200362static PyObject* (*py3_PyLong_FromLong)(long);
363static PyObject* (*py3_PyDict_New)(void);
Zdenek Dohnal90478f32021-06-14 15:08:30 +0200364# if PY_VERSION_HEX >= 0x030a00b2
365static int (*py3_PyIter_Check)(PyObject *o);
366# endif
Bram Moolenaardb913952012-06-29 12:54:53 +0200367static PyObject* (*py3_PyIter_Next)(PyObject *);
368static PyObject* (*py3_PyObject_GetIter)(PyObject *);
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200369static PyObject* (*py3_PyObject_Repr)(PyObject *);
Bram Moolenaarbcb40972013-05-30 13:22:13 +0200370static PyObject* (*py3_PyObject_GetItem)(PyObject *, PyObject *);
Bram Moolenaar03db85b2013-05-15 14:51:35 +0200371static int (*py3_PyObject_IsTrue)(PyObject *);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200372static PyObject* (*py3_Py_BuildValue)(char *, ...);
Bram Moolenaaree1b9312020-07-16 22:15:53 +0200373# if PY_VERSION_HEX >= 0x030900b0
374static int (*py3_PyType_GetFlags)(PyTypeObject *o);
375# endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200376static int (*py3_PyType_Ready)(PyTypeObject *type);
377static int (*py3_PyDict_SetItemString)(PyObject *dp, char *key, PyObject *item);
378static PyObject* (*py3_PyUnicode_FromString)(const char *u);
Bram Moolenaar1a3b5692013-05-30 12:40:39 +0200379# ifndef Py_UNICODE_USE_UCS_FUNCTIONS
380static PyObject* (*py3_PyUnicode_FromFormat)(const char *u, ...);
381# else
382# ifdef Py_UNICODE_WIDE
383static PyObject* (*py3_PyUnicodeUCS4_FromFormat)(const char *u, ...);
384# else
385static PyObject* (*py3_PyUnicodeUCS2_FromFormat)(const char *u, ...);
386# endif
387# endif
Bram Moolenaar19e60942011-06-19 00:27:51 +0200388static PyObject* (*py3_PyUnicode_Decode)(const char *u, Py_ssize_t size,
389 const char *encoding, const char *errors);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200390static long (*py3_PyLong_AsLong)(PyObject *);
391static void (*py3_PyErr_SetNone)(PyObject *);
392static void (*py3_PyEval_InitThreads)(void);
393static void(*py3_PyEval_RestoreThread)(PyThreadState *);
394static PyThreadState*(*py3_PyEval_SaveThread)(void);
395static int (*py3_PyArg_Parse)(PyObject *, char *, ...);
396static int (*py3_PyArg_ParseTuple)(PyObject *, char *, ...);
Bram Moolenaar19e60942011-06-19 00:27:51 +0200397static int (*py3_PyMem_Free)(void *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200398static void* (*py3_PyMem_Malloc)(size_t);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200399static int (*py3_Py_IsInitialized)(void);
400static void (*py3_PyErr_Clear)(void);
Bram Moolenaarc476e522013-06-23 13:46:40 +0200401static PyObject* (*py3_PyErr_Format)(PyObject *, const char *, ...);
Bram Moolenaar4d369872013-02-20 16:09:43 +0100402static void (*py3_PyErr_PrintEx)(int);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200403static PyObject*(*py3__PyObject_Init)(PyObject *, PyTypeObject *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200404static iternextfunc py3__PyObject_NextNotImplemented;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200405static PyObject* py3__Py_NoneStruct;
Bram Moolenaar66b79852012-09-21 14:00:35 +0200406static PyObject* py3__Py_FalseStruct;
407static PyObject* py3__Py_TrueStruct;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200408static int (*py3_PyModule_AddObject)(PyObject *m, const char *name, PyObject *o);
409static int (*py3_PyImport_AppendInittab)(const char *name, PyObject* (*initfunc)(void));
Bram Moolenaar9c9cbf12012-10-23 05:17:37 +0200410# if PY_VERSION_HEX >= 0x030300f0
411static char* (*py3_PyUnicode_AsUTF8)(PyObject *unicode);
412# else
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200413static char* (*py3__PyUnicode_AsString)(PyObject *unicode);
Bram Moolenaar9c9cbf12012-10-23 05:17:37 +0200414# endif
Bram Moolenaar19e60942011-06-19 00:27:51 +0200415static PyObject* (*py3_PyUnicode_AsEncodedString)(PyObject *unicode, const char* encoding, const char* errors);
416static char* (*py3_PyBytes_AsString)(PyObject *bytes);
Bram Moolenaar808c2bc2013-06-23 13:11:18 +0200417static int (*py3_PyBytes_AsStringAndSize)(PyObject *bytes, char **buffer, Py_ssize_t *length);
Bram Moolenaardb913952012-06-29 12:54:53 +0200418static PyObject* (*py3_PyBytes_FromString)(char *str);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100419static PyObject* (*py3_PyBytes_FromStringAndSize)(char *str, Py_ssize_t length);
Bram Moolenaaree1b9312020-07-16 22:15:53 +0200420# if defined(Py_DEBUG) || PY_VERSION_HEX >= 0x030900b0
421static void (*py3__Py_Dealloc)(PyObject *obj);
422# endif
423# if PY_VERSION_HEX >= 0x030900b0
424static PyObject* (*py3__PyObject_New)(PyTypeObject *);
425# endif
Bram Moolenaardb913952012-06-29 12:54:53 +0200426static PyObject* (*py3_PyFloat_FromDouble)(double num);
427static double (*py3_PyFloat_AsDouble)(PyObject *);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200428static PyObject* (*py3_PyObject_GenericGetAttr)(PyObject *obj, PyObject *name);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200429static PyObject* (*py3_PyType_GenericAlloc)(PyTypeObject *type, Py_ssize_t nitems);
430static PyObject* (*py3_PyType_GenericNew)(PyTypeObject *type, PyObject *args, PyObject *kwds);
Bram Moolenaar66b79852012-09-21 14:00:35 +0200431static PyTypeObject* py3_PyType_Type;
Bram Moolenaard4a8c982018-05-15 22:31:18 +0200432static PyTypeObject* py3_PyStdPrinter_Type;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200433static PyTypeObject* py3_PySlice_Type;
Bram Moolenaardb913952012-06-29 12:54:53 +0200434static PyTypeObject* py3_PyFloat_Type;
Bram Moolenaar66b79852012-09-21 14:00:35 +0200435static PyTypeObject* py3_PyBool_Type;
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200436static int (*py3_PyNumber_Check)(PyObject *);
437static PyObject* (*py3_PyNumber_Long)(PyObject *);
Bram Moolenaar19e60942011-06-19 00:27:51 +0200438static PyObject* (*py3_PyErr_NewException)(char *name, PyObject *base, PyObject *dict);
Bram Moolenaardb913952012-06-29 12:54:53 +0200439static PyObject* (*py3_PyCapsule_New)(void *, char *, PyCapsule_Destructor);
440static void* (*py3_PyCapsule_GetPointer)(PyObject *, char *);
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200441# ifdef Py_DEBUG
Bram Moolenaar0014a532013-05-29 21:33:39 +0200442static void (*py3__Py_NegativeRefcount)(const char *fname, int lineno, PyObject *op);
443static Py_ssize_t* py3__Py_RefTotal;
Bram Moolenaar0014a532013-05-29 21:33:39 +0200444static PyObject* (*py3_PyModule_Create2TraceRefs)(struct PyModuleDef* module, int module_api_version);
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200445# else
Bram Moolenaar0014a532013-05-29 21:33:39 +0200446static PyObject* (*py3_PyModule_Create2)(struct PyModuleDef* module, int module_api_version);
447# endif
448# if defined(Py_DEBUG) && !defined(Py_DEBUG_NO_PYMALLOC)
449static void (*py3__PyObject_DebugFree)(void*);
450static void* (*py3__PyObject_DebugMalloc)(size_t);
451# else
452static void (*py3_PyObject_Free)(void*);
453static void* (*py3_PyObject_Malloc)(size_t);
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200454# endif
Bram Moolenaar774267b2013-05-21 20:51:59 +0200455static PyObject*(*py3__PyObject_GC_New)(PyTypeObject *);
456static void(*py3_PyObject_GC_Del)(void *);
457static void(*py3_PyObject_GC_UnTrack)(void *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200458static int (*py3_PyType_IsSubtype)(PyTypeObject *, PyTypeObject *);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200459
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100460static HINSTANCE hinstPy3 = 0; // Instance of python.dll
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200461
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100462// Imported exception objects
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200463static PyObject *p3imp_PyExc_AttributeError;
464static PyObject *p3imp_PyExc_IndexError;
Bram Moolenaaraf6abb92013-04-24 13:04:26 +0200465static PyObject *p3imp_PyExc_KeyError;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200466static PyObject *p3imp_PyExc_KeyboardInterrupt;
467static PyObject *p3imp_PyExc_TypeError;
468static PyObject *p3imp_PyExc_ValueError;
Bram Moolenaar41009372013-07-01 22:03:04 +0200469static PyObject *p3imp_PyExc_SystemExit;
Bram Moolenaar8661b172013-05-15 15:44:28 +0200470static PyObject *p3imp_PyExc_RuntimeError;
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200471static PyObject *p3imp_PyExc_ImportError;
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200472static PyObject *p3imp_PyExc_OverflowError;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200473
474# define PyExc_AttributeError p3imp_PyExc_AttributeError
475# define PyExc_IndexError p3imp_PyExc_IndexError
Bram Moolenaaraf6abb92013-04-24 13:04:26 +0200476# define PyExc_KeyError p3imp_PyExc_KeyError
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200477# define PyExc_KeyboardInterrupt p3imp_PyExc_KeyboardInterrupt
478# define PyExc_TypeError p3imp_PyExc_TypeError
479# define PyExc_ValueError p3imp_PyExc_ValueError
Bram Moolenaar41009372013-07-01 22:03:04 +0200480# define PyExc_SystemExit p3imp_PyExc_SystemExit
Bram Moolenaar8661b172013-05-15 15:44:28 +0200481# define PyExc_RuntimeError p3imp_PyExc_RuntimeError
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200482# define PyExc_ImportError p3imp_PyExc_ImportError
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200483# define PyExc_OverflowError p3imp_PyExc_OverflowError
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200484
485/*
486 * Table of name to function pointer of python.
487 */
488# define PYTHON_PROC FARPROC
489static struct
490{
491 char *name;
492 PYTHON_PROC *ptr;
493} py3_funcname_table[] =
494{
495 {"PySys_SetArgv", (PYTHON_PROC*)&py3_PySys_SetArgv},
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100496 {"Py_SetPythonHome", (PYTHON_PROC*)&py3_Py_SetPythonHome},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200497 {"Py_Initialize", (PYTHON_PROC*)&py3_Py_Initialize},
Bram Moolenaare8cdcef2012-09-12 20:21:43 +0200498 {"_PyArg_ParseTuple_SizeT", (PYTHON_PROC*)&py3_PyArg_ParseTuple},
499 {"_Py_BuildValue_SizeT", (PYTHON_PROC*)&py3_Py_BuildValue},
Bram Moolenaar19e60942011-06-19 00:27:51 +0200500 {"PyMem_Free", (PYTHON_PROC*)&py3_PyMem_Free},
Bram Moolenaardb913952012-06-29 12:54:53 +0200501 {"PyMem_Malloc", (PYTHON_PROC*)&py3_PyMem_Malloc},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200502 {"PyList_New", (PYTHON_PROC*)&py3_PyList_New},
503 {"PyGILState_Ensure", (PYTHON_PROC*)&py3_PyGILState_Ensure},
504 {"PyGILState_Release", (PYTHON_PROC*)&py3_PyGILState_Release},
505 {"PySys_SetObject", (PYTHON_PROC*)&py3_PySys_SetObject},
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200506 {"PySys_GetObject", (PYTHON_PROC*)&py3_PySys_GetObject},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200507 {"PyList_Append", (PYTHON_PROC*)&py3_PyList_Append},
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200508 {"PyList_Insert", (PYTHON_PROC*)&py3_PyList_Insert},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200509 {"PyList_Size", (PYTHON_PROC*)&py3_PyList_Size},
Bram Moolenaardb913952012-06-29 12:54:53 +0200510 {"PySequence_Check", (PYTHON_PROC*)&py3_PySequence_Check},
511 {"PySequence_Size", (PYTHON_PROC*)&py3_PySequence_Size},
512 {"PySequence_GetItem", (PYTHON_PROC*)&py3_PySequence_GetItem},
Bram Moolenaara9922d62013-05-30 13:01:18 +0200513 {"PySequence_Fast", (PYTHON_PROC*)&py3_PySequence_Fast},
Bram Moolenaardb913952012-06-29 12:54:53 +0200514 {"PyTuple_Size", (PYTHON_PROC*)&py3_PyTuple_Size},
515 {"PyTuple_GetItem", (PYTHON_PROC*)&py3_PyTuple_GetItem},
Bram Moolenaar3b48b112018-07-04 22:03:25 +0200516# if PY_VERSION_HEX >= 0x030601f0
517 {"PySlice_AdjustIndices", (PYTHON_PROC*)&py3_PySlice_AdjustIndices},
518 {"PySlice_Unpack", (PYTHON_PROC*)&py3_PySlice_Unpack},
519# endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200520 {"PySlice_GetIndicesEx", (PYTHON_PROC*)&py3_PySlice_GetIndicesEx},
521 {"PyErr_NoMemory", (PYTHON_PROC*)&py3_PyErr_NoMemory},
522 {"Py_Finalize", (PYTHON_PROC*)&py3_Py_Finalize},
523 {"PyErr_SetString", (PYTHON_PROC*)&py3_PyErr_SetString},
Bram Moolenaar4d188da2013-05-15 15:35:09 +0200524 {"PyErr_SetObject", (PYTHON_PROC*)&py3_PyErr_SetObject},
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200525 {"PyErr_ExceptionMatches", (PYTHON_PROC*)&py3_PyErr_ExceptionMatches},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200526 {"PyRun_SimpleString", (PYTHON_PROC*)&py3_PyRun_SimpleString},
Bram Moolenaardb913952012-06-29 12:54:53 +0200527 {"PyRun_String", (PYTHON_PROC*)&py3_PyRun_String},
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200528 {"PyObject_GetAttrString", (PYTHON_PROC*)&py3_PyObject_GetAttrString},
Bram Moolenaara9922d62013-05-30 13:01:18 +0200529 {"PyObject_HasAttrString", (PYTHON_PROC*)&py3_PyObject_HasAttrString},
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200530 {"PyObject_SetAttrString", (PYTHON_PROC*)&py3_PyObject_SetAttrString},
531 {"PyObject_CallFunctionObjArgs", (PYTHON_PROC*)&py3_PyObject_CallFunctionObjArgs},
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200532 {"_PyObject_CallFunction_SizeT", (PYTHON_PROC*)&py3__PyObject_CallFunction_SizeT},
Bram Moolenaarf4258302013-06-02 18:20:17 +0200533 {"PyObject_Call", (PYTHON_PROC*)&py3_PyObject_Call},
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200534 {"PyEval_GetGlobals", (PYTHON_PROC*)&py3_PyEval_GetGlobals},
535 {"PyEval_GetLocals", (PYTHON_PROC*)&py3_PyEval_GetLocals},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200536 {"PyList_GetItem", (PYTHON_PROC*)&py3_PyList_GetItem},
537 {"PyImport_ImportModule", (PYTHON_PROC*)&py3_PyImport_ImportModule},
Bram Moolenaardb913952012-06-29 12:54:53 +0200538 {"PyImport_AddModule", (PYTHON_PROC*)&py3_PyImport_AddModule},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200539 {"PyErr_BadArgument", (PYTHON_PROC*)&py3_PyErr_BadArgument},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200540 {"PyErr_Occurred", (PYTHON_PROC*)&py3_PyErr_Occurred},
541 {"PyModule_GetDict", (PYTHON_PROC*)&py3_PyModule_GetDict},
542 {"PyList_SetItem", (PYTHON_PROC*)&py3_PyList_SetItem},
543 {"PyDict_GetItemString", (PYTHON_PROC*)&py3_PyDict_GetItemString},
Bram Moolenaardb913952012-06-29 12:54:53 +0200544 {"PyDict_Next", (PYTHON_PROC*)&py3_PyDict_Next},
545 {"PyMapping_Check", (PYTHON_PROC*)&py3_PyMapping_Check},
Bram Moolenaarbcb40972013-05-30 13:22:13 +0200546 {"PyMapping_Keys", (PYTHON_PROC*)&py3_PyMapping_Keys},
Zdenek Dohnal90478f32021-06-14 15:08:30 +0200547# if PY_VERSION_HEX >= 0x030a00b2
548 {"PyIter_Check", (PYTHON_PROC*)&py3_PyIter_Check},
549# endif
Bram Moolenaardb913952012-06-29 12:54:53 +0200550 {"PyIter_Next", (PYTHON_PROC*)&py3_PyIter_Next},
551 {"PyObject_GetIter", (PYTHON_PROC*)&py3_PyObject_GetIter},
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200552 {"PyObject_Repr", (PYTHON_PROC*)&py3_PyObject_Repr},
Bram Moolenaarbcb40972013-05-30 13:22:13 +0200553 {"PyObject_GetItem", (PYTHON_PROC*)&py3_PyObject_GetItem},
Bram Moolenaar03db85b2013-05-15 14:51:35 +0200554 {"PyObject_IsTrue", (PYTHON_PROC*)&py3_PyObject_IsTrue},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200555 {"PyLong_FromLong", (PYTHON_PROC*)&py3_PyLong_FromLong},
556 {"PyDict_New", (PYTHON_PROC*)&py3_PyDict_New},
Bram Moolenaaree1b9312020-07-16 22:15:53 +0200557# if PY_VERSION_HEX >= 0x030900b0
558 {"PyType_GetFlags", (PYTHON_PROC*)&py3_PyType_GetFlags},
559# endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200560 {"PyType_Ready", (PYTHON_PROC*)&py3_PyType_Ready},
561 {"PyDict_SetItemString", (PYTHON_PROC*)&py3_PyDict_SetItemString},
562 {"PyLong_AsLong", (PYTHON_PROC*)&py3_PyLong_AsLong},
563 {"PyErr_SetNone", (PYTHON_PROC*)&py3_PyErr_SetNone},
564 {"PyEval_InitThreads", (PYTHON_PROC*)&py3_PyEval_InitThreads},
565 {"PyEval_RestoreThread", (PYTHON_PROC*)&py3_PyEval_RestoreThread},
566 {"PyEval_SaveThread", (PYTHON_PROC*)&py3_PyEval_SaveThread},
Bram Moolenaar5f87b232013-06-13 20:57:50 +0200567 {"_PyArg_Parse_SizeT", (PYTHON_PROC*)&py3_PyArg_Parse},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200568 {"Py_IsInitialized", (PYTHON_PROC*)&py3_Py_IsInitialized},
Bram Moolenaardb913952012-06-29 12:54:53 +0200569 {"_PyObject_NextNotImplemented", (PYTHON_PROC*)&py3__PyObject_NextNotImplemented},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200570 {"_Py_NoneStruct", (PYTHON_PROC*)&py3__Py_NoneStruct},
Bram Moolenaar66b79852012-09-21 14:00:35 +0200571 {"_Py_FalseStruct", (PYTHON_PROC*)&py3__Py_FalseStruct},
572 {"_Py_TrueStruct", (PYTHON_PROC*)&py3__Py_TrueStruct},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200573 {"PyErr_Clear", (PYTHON_PROC*)&py3_PyErr_Clear},
Bram Moolenaarc476e522013-06-23 13:46:40 +0200574 {"PyErr_Format", (PYTHON_PROC*)&py3_PyErr_Format},
Bram Moolenaar4d369872013-02-20 16:09:43 +0100575 {"PyErr_PrintEx", (PYTHON_PROC*)&py3_PyErr_PrintEx},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200576 {"PyObject_Init", (PYTHON_PROC*)&py3__PyObject_Init},
577 {"PyModule_AddObject", (PYTHON_PROC*)&py3_PyModule_AddObject},
578 {"PyImport_AppendInittab", (PYTHON_PROC*)&py3_PyImport_AppendInittab},
Bram Moolenaar9c9cbf12012-10-23 05:17:37 +0200579# if PY_VERSION_HEX >= 0x030300f0
580 {"PyUnicode_AsUTF8", (PYTHON_PROC*)&py3_PyUnicode_AsUTF8},
581# else
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200582 {"_PyUnicode_AsString", (PYTHON_PROC*)&py3__PyUnicode_AsString},
Bram Moolenaar9c9cbf12012-10-23 05:17:37 +0200583# endif
Bram Moolenaar1a3b5692013-05-30 12:40:39 +0200584# ifndef Py_UNICODE_USE_UCS_FUNCTIONS
585 {"PyUnicode_FromFormat", (PYTHON_PROC*)&py3_PyUnicode_FromFormat},
586# else
587# ifdef Py_UNICODE_WIDE
588 {"PyUnicodeUCS4_FromFormat", (PYTHON_PROC*)&py3_PyUnicodeUCS4_FromFormat},
589# else
590 {"PyUnicodeUCS2_FromFormat", (PYTHON_PROC*)&py3_PyUnicodeUCS2_FromFormat},
591# endif
592# endif
Bram Moolenaar19e60942011-06-19 00:27:51 +0200593 {"PyBytes_AsString", (PYTHON_PROC*)&py3_PyBytes_AsString},
Bram Moolenaarcdab9052012-09-05 19:03:56 +0200594 {"PyBytes_AsStringAndSize", (PYTHON_PROC*)&py3_PyBytes_AsStringAndSize},
Bram Moolenaardb913952012-06-29 12:54:53 +0200595 {"PyBytes_FromString", (PYTHON_PROC*)&py3_PyBytes_FromString},
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100596 {"PyBytes_FromStringAndSize", (PYTHON_PROC*)&py3_PyBytes_FromStringAndSize},
Bram Moolenaaree1b9312020-07-16 22:15:53 +0200597# if defined(Py_DEBUG) || PY_VERSION_HEX >= 0x030900b0
598 {"_Py_Dealloc", (PYTHON_PROC*)&py3__Py_Dealloc},
599# endif
600# if PY_VERSION_HEX >= 0x030900b0
601 {"_PyObject_New", (PYTHON_PROC*)&py3__PyObject_New},
602# endif
Bram Moolenaardb913952012-06-29 12:54:53 +0200603 {"PyFloat_FromDouble", (PYTHON_PROC*)&py3_PyFloat_FromDouble},
604 {"PyFloat_AsDouble", (PYTHON_PROC*)&py3_PyFloat_AsDouble},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200605 {"PyObject_GenericGetAttr", (PYTHON_PROC*)&py3_PyObject_GenericGetAttr},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200606 {"PyType_GenericAlloc", (PYTHON_PROC*)&py3_PyType_GenericAlloc},
607 {"PyType_GenericNew", (PYTHON_PROC*)&py3_PyType_GenericNew},
Bram Moolenaar66b79852012-09-21 14:00:35 +0200608 {"PyType_Type", (PYTHON_PROC*)&py3_PyType_Type},
Bram Moolenaard4a8c982018-05-15 22:31:18 +0200609 {"PyStdPrinter_Type", (PYTHON_PROC*)&py3_PyStdPrinter_Type},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200610 {"PySlice_Type", (PYTHON_PROC*)&py3_PySlice_Type},
Bram Moolenaardb913952012-06-29 12:54:53 +0200611 {"PyFloat_Type", (PYTHON_PROC*)&py3_PyFloat_Type},
Bram Moolenaar66b79852012-09-21 14:00:35 +0200612 {"PyBool_Type", (PYTHON_PROC*)&py3_PyBool_Type},
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200613 {"PyNumber_Check", (PYTHON_PROC*)&py3_PyNumber_Check},
614 {"PyNumber_Long", (PYTHON_PROC*)&py3_PyNumber_Long},
Bram Moolenaar19e60942011-06-19 00:27:51 +0200615 {"PyErr_NewException", (PYTHON_PROC*)&py3_PyErr_NewException},
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200616# ifdef Py_DEBUG
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200617 {"_Py_NegativeRefcount", (PYTHON_PROC*)&py3__Py_NegativeRefcount},
618 {"_Py_RefTotal", (PYTHON_PROC*)&py3__Py_RefTotal},
Bram Moolenaar0014a532013-05-29 21:33:39 +0200619 {"PyModule_Create2TraceRefs", (PYTHON_PROC*)&py3_PyModule_Create2TraceRefs},
620# else
621 {"PyModule_Create2", (PYTHON_PROC*)&py3_PyModule_Create2},
622# endif
623# if defined(Py_DEBUG) && !defined(Py_DEBUG_NO_PYMALLOC)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200624 {"_PyObject_DebugFree", (PYTHON_PROC*)&py3__PyObject_DebugFree},
625 {"_PyObject_DebugMalloc", (PYTHON_PROC*)&py3__PyObject_DebugMalloc},
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200626# else
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200627 {"PyObject_Malloc", (PYTHON_PROC*)&py3_PyObject_Malloc},
628 {"PyObject_Free", (PYTHON_PROC*)&py3_PyObject_Free},
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200629# endif
Bram Moolenaar774267b2013-05-21 20:51:59 +0200630 {"_PyObject_GC_New", (PYTHON_PROC*)&py3__PyObject_GC_New},
631 {"PyObject_GC_Del", (PYTHON_PROC*)&py3_PyObject_GC_Del},
632 {"PyObject_GC_UnTrack", (PYTHON_PROC*)&py3_PyObject_GC_UnTrack},
Bram Moolenaardb913952012-06-29 12:54:53 +0200633 {"PyType_IsSubtype", (PYTHON_PROC*)&py3_PyType_IsSubtype},
634 {"PyCapsule_New", (PYTHON_PROC*)&py3_PyCapsule_New},
635 {"PyCapsule_GetPointer", (PYTHON_PROC*)&py3_PyCapsule_GetPointer},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200636 {"", NULL},
637};
638
Bram Moolenaar13a1f3f2019-10-23 21:37:25 +0200639# if PY_VERSION_HEX >= 0x030800f0
640 static inline void
641py3__Py_DECREF(const char *filename UNUSED, int lineno UNUSED, PyObject *op)
642{
Bram Moolenaar13a1f3f2019-10-23 21:37:25 +0200643 if (--op->ob_refcnt != 0)
644 {
645# ifdef Py_REF_DEBUG
646 if (op->ob_refcnt < 0)
647 {
648 _Py_NegativeRefcount(filename, lineno, op);
649 }
650# endif
651 }
652 else
653 {
654 _Py_Dealloc(op);
655 }
656}
657
658# undef Py_DECREF
659# define Py_DECREF(op) py3__Py_DECREF(__FILE__, __LINE__, _PyObject_CAST(op))
660
661 static inline void
662py3__Py_XDECREF(PyObject *op)
663{
664 if (op != NULL)
665 {
666 Py_DECREF(op);
667 }
668}
669
670# undef Py_XDECREF
671# define Py_XDECREF(op) py3__Py_XDECREF(_PyObject_CAST(op))
672# endif
673
Bram Moolenaaree1b9312020-07-16 22:15:53 +0200674# if PY_VERSION_HEX >= 0x030900b0
675 static inline int
676py3_PyType_HasFeature(PyTypeObject *type, unsigned long feature)
677{
678 return ((PyType_GetFlags(type) & feature) != 0);
679}
680# define PyType_HasFeature(t,f) py3_PyType_HasFeature(t,f)
681# endif
682
Zdenek Dohnal90478f32021-06-14 15:08:30 +0200683# if PY_VERSION_HEX >= 0x030a00b2
684 static inline int
685py3__PyObject_TypeCheck(PyObject *ob, PyTypeObject *type)
686{
687 return Py_IS_TYPE(ob, type) || PyType_IsSubtype(Py_TYPE(ob), type);
688}
689# define _PyObject_TypeCheck(o,t) py3__PyObject_TypeCheck(o,t)
690# endif
691
Bram Moolenaarb2f9e0e2020-12-25 13:52:37 +0100692# ifdef MSWIN
693/*
694 * Look up the library "libname" using the InstallPath registry key.
695 * Return NULL when failed. Return an allocated string when successful.
696 */
697 static char *
698py3_get_system_libname(const char *libname)
699{
700 const char *cp = libname;
701 char subkey[128];
702 HKEY hKey;
703 char installpath[MAXPATHL];
704 LONG len = sizeof(installpath);
705 LSTATUS rc;
706 size_t sysliblen;
707 char *syslibname;
708
709 while (*cp != '\0')
710 {
711 if (*cp == ':' || *cp == '\\' || *cp == '/')
712 {
713 // Bail out if "libname" contains path separator, assume it is
714 // an absolute path.
715 return NULL;
716 }
717 ++cp;
718 }
719 vim_snprintf(subkey, sizeof(subkey),
720# ifdef _WIN64
721 "Software\\Python\\PythonCore\\%d.%d\\InstallPath",
722# else
723 "Software\\Python\\PythonCore\\%d.%d-32\\InstallPath",
724# endif
725 PY_MAJOR_VERSION, PY_MINOR_VERSION);
726 if (RegOpenKeyExA(HKEY_LOCAL_MACHINE, subkey, 0, KEY_QUERY_VALUE, &hKey)
727 != ERROR_SUCCESS)
728 return NULL;
729 rc = RegQueryValueA(hKey, NULL, installpath, &len);
730 RegCloseKey(hKey);
731 if (ERROR_SUCCESS != rc)
732 return NULL;
733 cp = installpath + len;
734 // Just in case registry value contains null terminators.
735 while (cp > installpath && *(cp-1) == '\0')
736 --cp;
737 // Remove trailing path separators.
738 while (cp > installpath && (*(cp-1) == '\\' || *(cp-1) == '/'))
739 --cp;
740 // Ignore if InstallPath is effectively empty.
741 if (cp <= installpath)
742 return NULL;
743 sysliblen = (cp - installpath) + 1 + STRLEN(libname) + 1;
744 syslibname = alloc(sysliblen);
745 vim_snprintf(syslibname, sysliblen, "%.*s\\%s",
746 (int)(cp - installpath), installpath, libname);
747 return syslibname;
748}
749# endif
750
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200751/*
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200752 * Load library and get all pointers.
753 * Parameter 'libname' provides name of DLL.
754 * Return OK or FAIL.
755 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200756 static int
757py3_runtime_link_init(char *libname, int verbose)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200758{
759 int i;
Bram Moolenaar7b24ce02018-03-29 18:15:26 +0200760 PYTHON_PROC *ucs_from_string = (PYTHON_PROC *)&py3_PyUnicode_FromString;
761 PYTHON_PROC *ucs_decode = (PYTHON_PROC *)&py3_PyUnicode_Decode;
762 PYTHON_PROC *ucs_as_encoded_string =
763 (PYTHON_PROC *)&py3_PyUnicode_AsEncodedString;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200764
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100765# if !(defined(PY_NO_RTLD_GLOBAL) && defined(PY3_NO_RTLD_GLOBAL)) && defined(UNIX) && defined(FEAT_PYTHON)
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100766 // Can't have Python and Python3 loaded at the same time.
767 // It cause a crash, because RTLD_GLOBAL is needed for
768 // standard C extension libraries of one or both python versions.
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200769 if (python_loaded())
770 {
Bram Moolenaar9dc93ae2011-08-28 16:00:19 +0200771 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100772 emsg(_("E837: This Vim cannot execute :py3 after using :python"));
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200773 return FAIL;
774 }
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200775# endif
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200776
777 if (hinstPy3 != 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200778 return OK;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200779 hinstPy3 = load_dll(libname);
780
Bram Moolenaarb2f9e0e2020-12-25 13:52:37 +0100781# ifdef MSWIN
782 if (!hinstPy3)
783 {
784 // Attempt to use the path from InstallPath as stored in the registry.
785 char *syslibname = py3_get_system_libname(libname);
786
787 if (syslibname != NULL)
788 {
789 hinstPy3 = load_dll(syslibname);
790 vim_free(syslibname);
791 }
792 }
793# endif
794
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200795 if (!hinstPy3)
796 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200797 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100798 semsg(_(e_loadlib), libname);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200799 return FAIL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200800 }
801
802 for (i = 0; py3_funcname_table[i].ptr; ++i)
803 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200804 if ((*py3_funcname_table[i].ptr = symbol_from_dll(hinstPy3,
805 py3_funcname_table[i].name)) == NULL)
806 {
807 close_dll(hinstPy3);
808 hinstPy3 = 0;
809 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100810 semsg(_(e_loadfunc), py3_funcname_table[i].name);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200811 return FAIL;
812 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200813 }
814
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100815 // Load unicode functions separately as only the ucs2 or the ucs4 functions
816 // will be present in the library.
Bram Moolenaar9c9cbf12012-10-23 05:17:37 +0200817# if PY_VERSION_HEX >= 0x030300f0
Bram Moolenaar7b24ce02018-03-29 18:15:26 +0200818 *ucs_from_string = symbol_from_dll(hinstPy3, "PyUnicode_FromString");
819 *ucs_decode = symbol_from_dll(hinstPy3, "PyUnicode_Decode");
820 *ucs_as_encoded_string = symbol_from_dll(hinstPy3,
Bram Moolenaar7bc4f932012-10-14 03:22:56 +0200821 "PyUnicode_AsEncodedString");
Bram Moolenaar9c9cbf12012-10-23 05:17:37 +0200822# else
Bram Moolenaar7b24ce02018-03-29 18:15:26 +0200823 *ucs_from_string = symbol_from_dll(hinstPy3, "PyUnicodeUCS2_FromString");
824 *ucs_decode = symbol_from_dll(hinstPy3,
Bram Moolenaar19e60942011-06-19 00:27:51 +0200825 "PyUnicodeUCS2_Decode");
Bram Moolenaar7b24ce02018-03-29 18:15:26 +0200826 *ucs_as_encoded_string = symbol_from_dll(hinstPy3,
Bram Moolenaar19e60942011-06-19 00:27:51 +0200827 "PyUnicodeUCS2_AsEncodedString");
Bram Moolenaar7b24ce02018-03-29 18:15:26 +0200828 if (*ucs_from_string == NULL || *ucs_decode == NULL
829 || *ucs_as_encoded_string == NULL)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200830 {
Bram Moolenaar7b24ce02018-03-29 18:15:26 +0200831 *ucs_from_string = symbol_from_dll(hinstPy3,
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200832 "PyUnicodeUCS4_FromString");
Bram Moolenaar7b24ce02018-03-29 18:15:26 +0200833 *ucs_decode = symbol_from_dll(hinstPy3,
Bram Moolenaar19e60942011-06-19 00:27:51 +0200834 "PyUnicodeUCS4_Decode");
Bram Moolenaar7b24ce02018-03-29 18:15:26 +0200835 *ucs_as_encoded_string = symbol_from_dll(hinstPy3,
Bram Moolenaar19e60942011-06-19 00:27:51 +0200836 "PyUnicodeUCS4_AsEncodedString");
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200837 }
Bram Moolenaar9c9cbf12012-10-23 05:17:37 +0200838# endif
Bram Moolenaar7b24ce02018-03-29 18:15:26 +0200839 if (*ucs_from_string == NULL || *ucs_decode == NULL
840 || *ucs_as_encoded_string == NULL)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200841 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200842 close_dll(hinstPy3);
843 hinstPy3 = 0;
844 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100845 semsg(_(e_loadfunc), "PyUnicode_UCSX_*");
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200846 return FAIL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200847 }
848
849 return OK;
850}
851
852/*
853 * If python is enabled (there is installed python on Windows system) return
854 * TRUE, else FALSE.
855 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200856 int
857python3_enabled(int verbose)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200858{
Bram Moolenaar25e4fcd2016-01-09 14:57:47 +0100859 return py3_runtime_link_init((char *)p_py3dll, verbose) == OK;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200860}
861
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100862/*
863 * Load the standard Python exceptions - don't import the symbols from the
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200864 * DLL, as this can cause errors (importing data symbols is not reliable).
865 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200866 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100867get_py3_exceptions(void)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200868{
869 PyObject *exmod = PyImport_ImportModule("builtins");
870 PyObject *exdict = PyModule_GetDict(exmod);
871 p3imp_PyExc_AttributeError = PyDict_GetItemString(exdict, "AttributeError");
872 p3imp_PyExc_IndexError = PyDict_GetItemString(exdict, "IndexError");
Bram Moolenaaraf6abb92013-04-24 13:04:26 +0200873 p3imp_PyExc_KeyError = PyDict_GetItemString(exdict, "KeyError");
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200874 p3imp_PyExc_KeyboardInterrupt = PyDict_GetItemString(exdict, "KeyboardInterrupt");
875 p3imp_PyExc_TypeError = PyDict_GetItemString(exdict, "TypeError");
876 p3imp_PyExc_ValueError = PyDict_GetItemString(exdict, "ValueError");
Bram Moolenaar41009372013-07-01 22:03:04 +0200877 p3imp_PyExc_SystemExit = PyDict_GetItemString(exdict, "SystemExit");
Bram Moolenaar8661b172013-05-15 15:44:28 +0200878 p3imp_PyExc_RuntimeError = PyDict_GetItemString(exdict, "RuntimeError");
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200879 p3imp_PyExc_ImportError = PyDict_GetItemString(exdict, "ImportError");
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200880 p3imp_PyExc_OverflowError = PyDict_GetItemString(exdict, "OverflowError");
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200881 Py_XINCREF(p3imp_PyExc_AttributeError);
882 Py_XINCREF(p3imp_PyExc_IndexError);
Bram Moolenaaraf6abb92013-04-24 13:04:26 +0200883 Py_XINCREF(p3imp_PyExc_KeyError);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200884 Py_XINCREF(p3imp_PyExc_KeyboardInterrupt);
885 Py_XINCREF(p3imp_PyExc_TypeError);
886 Py_XINCREF(p3imp_PyExc_ValueError);
Bram Moolenaar41009372013-07-01 22:03:04 +0200887 Py_XINCREF(p3imp_PyExc_SystemExit);
Bram Moolenaar8661b172013-05-15 15:44:28 +0200888 Py_XINCREF(p3imp_PyExc_RuntimeError);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200889 Py_XINCREF(p3imp_PyExc_ImportError);
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200890 Py_XINCREF(p3imp_PyExc_OverflowError);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200891 Py_XDECREF(exmod);
892}
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100893#endif // DYNAMIC_PYTHON3
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200894
Bram Moolenaardb913952012-06-29 12:54:53 +0200895static int py3initialised = 0;
Bram Moolenaardb913952012-06-29 12:54:53 +0200896#define PYINITIALISED py3initialised
Bram Moolenaarc4f83382017-07-07 14:50:44 +0200897static int python_end_called = FALSE;
Bram Moolenaardb913952012-06-29 12:54:53 +0200898
Bram Moolenaar774267b2013-05-21 20:51:59 +0200899#define DESTRUCTOR_FINISH(self) Py_TYPE(self)->tp_free((PyObject*)self)
Bram Moolenaardb913952012-06-29 12:54:53 +0200900
Bram Moolenaar971db462013-05-12 18:44:48 +0200901#define WIN_PYTHON_REF(win) win->w_python3_ref
902#define BUF_PYTHON_REF(buf) buf->b_python3_ref
Bram Moolenaar5e538ec2013-05-15 15:12:29 +0200903#define TAB_PYTHON_REF(tab) tab->tp_python3_ref
Bram Moolenaar971db462013-05-12 18:44:48 +0200904
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200905 static void
906call_PyObject_Free(void *p)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200907{
Bram Moolenaar0014a532013-05-29 21:33:39 +0200908#if defined(Py_DEBUG) && !defined(Py_DEBUG_NO_PYMALLOC)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200909 _PyObject_DebugFree(p);
910#else
911 PyObject_Free(p);
912#endif
913}
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200914
915 static PyObject *
916call_PyType_GenericNew(PyTypeObject *type, PyObject *args, PyObject *kwds)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200917{
918 return PyType_GenericNew(type,args,kwds);
919}
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200920
921 static PyObject *
922call_PyType_GenericAlloc(PyTypeObject *type, Py_ssize_t nitems)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200923{
924 return PyType_GenericAlloc(type,nitems);
925}
926
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200927static PyObject *OutputGetattro(PyObject *, PyObject *);
928static int OutputSetattro(PyObject *, PyObject *, PyObject *);
929static PyObject *BufferGetattro(PyObject *, PyObject *);
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200930static int BufferSetattro(PyObject *, PyObject *, PyObject *);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +0200931static PyObject *TabPageGetattro(PyObject *, PyObject *);
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200932static PyObject *WindowGetattro(PyObject *, PyObject *);
933static int WindowSetattro(PyObject *, PyObject *, PyObject *);
934static PyObject *RangeGetattro(PyObject *, PyObject *);
935static PyObject *CurrentGetattro(PyObject *, PyObject *);
936static int CurrentSetattro(PyObject *, PyObject *, PyObject *);
937static PyObject *DictionaryGetattro(PyObject *, PyObject *);
938static int DictionarySetattro(PyObject *, PyObject *, PyObject *);
939static PyObject *ListGetattro(PyObject *, PyObject *);
940static int ListSetattro(PyObject *, PyObject *, PyObject *);
941static PyObject *FunctionGetattro(PyObject *, PyObject *);
942
943static struct PyModuleDef vimmodule;
944
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +0200945#define PY_CAN_RECURSE
946
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200947/*
948 * Include the code shared with if_python.c
949 */
950#include "if_py_both.h"
951
Bram Moolenaar828bff12019-03-19 22:11:41 +0100952// NOTE: Must always be used at the start of a block, since it declares "name".
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200953#define GET_ATTR_STRING(name, nameobj) \
954 char *name = ""; \
955 if (PyUnicode_Check(nameobj)) \
Bram Moolenaar828bff12019-03-19 22:11:41 +0100956 name = (char *)_PyUnicode_AsString(nameobj)
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200957
958#define PY3OBJ_DELETED(obj) (obj->ob_base.ob_refcnt<=0)
959
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100960///////////////////////////////////////////////////////
961// Internal function prototypes.
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200962
Bram Moolenaar7854e3a2012-11-28 15:33:14 +0100963static PyObject *Py3Init_vim(void);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200964
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100965///////////////////////////////////////////////////////
966// 1. Python interpreter main program.
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200967
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200968 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100969python3_end(void)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200970{
971 static int recurse = 0;
972
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100973 // If a crash occurs while doing this, don't try again.
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200974 if (recurse != 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200975 return;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200976
Bram Moolenaarc4f83382017-07-07 14:50:44 +0200977 python_end_called = TRUE;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200978 ++recurse;
979
980#ifdef DYNAMIC_PYTHON3
981 if (hinstPy3)
982#endif
983 if (Py_IsInitialized())
984 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100985 // acquire lock before finalizing
Bram Moolenaar71700b82013-05-15 17:49:05 +0200986 PyGILState_Ensure();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200987
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200988 Py_Finalize();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200989 }
990
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200991 --recurse;
992}
993
Bram Moolenaar094454f2015-10-07 10:39:55 +0200994#if (defined(DYNAMIC_PYTHON3) && defined(DYNAMIC_PYTHON) && defined(FEAT_PYTHON) && defined(UNIX)) || defined(PROTO)
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200995 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100996python3_loaded(void)
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200997{
998 return (hinstPy3 != 0);
999}
1000#endif
1001
Bram Moolenaar94073162018-01-31 21:49:05 +01001002static wchar_t *py_home_buf = NULL;
1003
Bram Moolenaar56b8dc32020-08-06 21:47:11 +02001004#if defined(MSWIN) && (PY_VERSION_HEX >= 0x030500f0)
Bram Moolenaarc6ed2542020-10-10 23:26:28 +02001005/*
1006 * Return TRUE if stdin is readable from Python 3.
1007 */
1008 static BOOL
1009is_stdin_readable(void)
1010{
1011 DWORD mode, eventnum;
1012 struct _stat st;
1013 int fd = fileno(stdin);
1014 HANDLE hstdin = (HANDLE)_get_osfhandle(fd);
1015
1016 // Check if stdin is connected to the console.
1017 if (GetConsoleMode(hstdin, &mode))
1018 // Check if it is opened as input.
1019 return GetNumberOfConsoleInputEvents(hstdin, &eventnum);
1020
1021 return _fstat(fd, &st) == 0;
1022}
1023
1024// Python 3.5 or later will abort inside Py_Initialize() when stdin has
1025// been closed (i.e. executed by "vim -"). Reconnect stdin to CONIN$.
Bram Moolenaar56b8dc32020-08-06 21:47:11 +02001026// Note that the python DLL is linked to its own stdio DLL which can be
1027// differ from Vim's stdio.
1028 static void
1029reset_stdin(void)
1030{
1031 FILE *(*py__acrt_iob_func)(unsigned) = NULL;
1032 FILE *(*pyfreopen)(const char *, const char *, FILE *) = NULL;
1033 HINSTANCE hinst;
1034
1035# ifdef DYNAMIC_PYTHON3
1036 hinst = hinstPy3;
1037# else
1038 hinst = GetModuleHandle(PYTHON3_DLL);
1039# endif
Bram Moolenaarc6ed2542020-10-10 23:26:28 +02001040 if (hinst == NULL || is_stdin_readable())
Bram Moolenaar56b8dc32020-08-06 21:47:11 +02001041 return;
1042
1043 // Get "freopen" and "stdin" which are used in the python DLL.
1044 // "stdin" is defined as "__acrt_iob_func(0)" in VC++ 2015 or later.
1045 py__acrt_iob_func = get_dll_import_func(hinst, "__acrt_iob_func");
1046 if (py__acrt_iob_func)
1047 {
1048 HINSTANCE hpystdiodll = find_imported_module_by_funcname(hinst,
Bram Moolenaar253b16a2020-10-06 19:59:06 +02001049 "__acrt_iob_func");
Bram Moolenaar56b8dc32020-08-06 21:47:11 +02001050 if (hpystdiodll)
Bram Moolenaar253b16a2020-10-06 19:59:06 +02001051 pyfreopen = (void *)GetProcAddress(hpystdiodll, "freopen");
Bram Moolenaar56b8dc32020-08-06 21:47:11 +02001052 }
1053
Bram Moolenaarc6ed2542020-10-10 23:26:28 +02001054 // Reconnect stdin to CONIN$.
Bram Moolenaar253b16a2020-10-06 19:59:06 +02001055 if (pyfreopen != NULL)
Bram Moolenaarc6ed2542020-10-10 23:26:28 +02001056 pyfreopen("CONIN$", "r", py__acrt_iob_func(0));
Bram Moolenaar56b8dc32020-08-06 21:47:11 +02001057 else
Bram Moolenaarc6ed2542020-10-10 23:26:28 +02001058 freopen("CONIN$", "r", stdin);
Bram Moolenaar56b8dc32020-08-06 21:47:11 +02001059}
1060#else
1061# define reset_stdin()
1062#endif
1063
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001064 static int
1065Python3_Init(void)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001066{
1067 if (!py3initialised)
1068 {
1069#ifdef DYNAMIC_PYTHON3
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001070 if (!python3_enabled(TRUE))
1071 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001072 emsg(_("E263: Sorry, this command is disabled, the Python library could not be loaded."));
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001073 goto fail;
1074 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001075#endif
1076
1077 init_structs();
1078
Bram Moolenaar94073162018-01-31 21:49:05 +01001079 if (*p_py3home != NUL)
1080 {
1081 size_t len = mbstowcs(NULL, (char *)p_py3home, 0) + 1;
Bram Moolenaar644d37b2010-11-16 19:26:02 +01001082
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001083 // The string must not change later, make a copy in static memory.
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001084 py_home_buf = ALLOC_MULT(wchar_t, len);
Bram Moolenaar94073162018-01-31 21:49:05 +01001085 if (py_home_buf != NULL && mbstowcs(
1086 py_home_buf, (char *)p_py3home, len) != (size_t)-1)
1087 Py_SetPythonHome(py_home_buf);
1088 }
Bram Moolenaar644d37b2010-11-16 19:26:02 +01001089#ifdef PYTHON3_HOME
Bram Moolenaar94073162018-01-31 21:49:05 +01001090 else if (mch_getenv((char_u *)"PYTHONHOME") == NULL)
Bram Moolenaar10005652015-12-31 21:03:23 +01001091 Py_SetPythonHome(PYTHON3_HOME);
Bram Moolenaar644d37b2010-11-16 19:26:02 +01001092#endif
1093
Bram Moolenaar7bc4f932012-10-14 03:22:56 +02001094 PyImport_AppendInittab("vim", Py3Init_vim);
1095
Bram Moolenaar56b8dc32020-08-06 21:47:11 +02001096 reset_stdin();
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001097 Py_Initialize();
Bram Moolenaard0573012017-10-28 21:11:06 +02001098
Bram Moolenaarefc0d942020-10-11 18:05:02 +02001099#if PY_VERSION_HEX < 0x03090000
1100 // Initialise threads. This is deprecated since Python 3.9.
Bram Moolenaar456f2bb2011-06-12 21:37:13 +02001101 PyEval_InitThreads();
Bram Moolenaarefc0d942020-10-11 18:05:02 +02001102#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001103#ifdef DYNAMIC_PYTHON3
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001104 get_py3_exceptions();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001105#endif
1106
Bram Moolenaar1dc28782013-05-21 19:11:01 +02001107 if (PythonIO_Init_io())
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001108 goto fail;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001109
Bram Moolenaardb913952012-06-29 12:54:53 +02001110 globals = PyModule_GetDict(PyImport_AddModule("__main__"));
1111
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001112 // Remove the element from sys.path that was added because of our
1113 // argv[0] value in Py3Init_vim(). Previously we used an empty
1114 // string, but depending on the OS we then get an empty entry or
1115 // the current directory in sys.path.
1116 // Only after vim has been imported, the element does exist in
1117 // sys.path.
Bram Moolenaar19e60942011-06-19 00:27:51 +02001118 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 +02001119
Bram Moolenaarefc0d942020-10-11 18:05:02 +02001120 // Without the call to PyEval_SaveThread, thread specific state (such
1121 // as the system trace hook), will be lost between invocations of
1122 // Python code.
1123 // GIL may have been created and acquired in PyEval_InitThreads() and
1124 // thread state is created in Py_Initialize(); there
1125 // _PyGILState_NoteThreadState() also sets gilcounter to 1 (python must
1126 // have threads enabled!), so the following does both: unlock GIL and
1127 // save thread state in TLS without deleting thread state
Bram Moolenaar76d711c2013-02-13 14:17:08 +01001128 PyEval_SaveThread();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001129
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001130 py3initialised = 1;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001131 }
1132
1133 return 0;
1134
1135fail:
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001136 // We call PythonIO_Flush() here to print any Python errors.
1137 // This is OK, as it is possible to call this function even
1138 // if PythonIO_Init_io() has not completed successfully (it will
1139 // not do anything in this case).
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001140 PythonIO_Flush();
1141 return -1;
1142}
1143
1144/*
1145 * External interface
1146 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001147 static void
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02001148DoPyCommand(const char *cmd, rangeinitializer init_range, runner run, void *arg)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001149{
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001150#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001151 char *saved_locale;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001152#endif
Bram Moolenaar19e60942011-06-19 00:27:51 +02001153 PyObject *cmdstr;
1154 PyObject *cmdbytes;
Bram Moolenaar71700b82013-05-15 17:49:05 +02001155 PyGILState_STATE pygilstate;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001156
Bram Moolenaarc4f83382017-07-07 14:50:44 +02001157 if (python_end_called)
1158 goto theend;
1159
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001160 if (Python3_Init())
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001161 goto theend;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001162
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02001163 init_range(arg);
1164
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001165 Python_Release_Vim(); // leave Vim
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001166
1167#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001168 // Python only works properly when the LC_NUMERIC locale is "C".
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001169 saved_locale = setlocale(LC_NUMERIC, NULL);
1170 if (saved_locale == NULL || STRCMP(saved_locale, "C") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001171 saved_locale = NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001172 else
1173 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001174 // Need to make a copy, value may change when setting new locale.
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001175 saved_locale = (char *)vim_strsave((char_u *)saved_locale);
1176 (void)setlocale(LC_NUMERIC, "C");
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001177 }
1178#endif
1179
1180 pygilstate = PyGILState_Ensure();
1181
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001182 // PyRun_SimpleString expects a UTF-8 string. Wrong encoding may cause
1183 // SyntaxError (unicode error).
Bram Moolenaar3d64a312011-07-15 15:54:44 +02001184 cmdstr = PyUnicode_Decode(cmd, strlen(cmd),
Bram Moolenaar2e2f52a2020-12-21 16:03:02 +01001185 (char *)ENC_OPT, ERRORS_DECODE_ARG);
1186 cmdbytes = PyUnicode_AsEncodedString(cmdstr, "utf-8", ERRORS_ENCODE_ARG);
Bram Moolenaar19e60942011-06-19 00:27:51 +02001187 Py_XDECREF(cmdstr);
Bram Moolenaardb913952012-06-29 12:54:53 +02001188
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02001189 run(PyBytes_AsString(cmdbytes), arg, &pygilstate);
Bram Moolenaar19e60942011-06-19 00:27:51 +02001190 Py_XDECREF(cmdbytes);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001191
1192 PyGILState_Release(pygilstate);
1193
1194#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
1195 if (saved_locale != NULL)
1196 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001197 (void)setlocale(LC_NUMERIC, saved_locale);
1198 vim_free(saved_locale);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001199 }
1200#endif
1201
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001202 Python_Lock_Vim(); // enter Vim
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001203 PythonIO_Flush();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001204
1205theend:
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001206 return; // keeps lint happy
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001207}
1208
1209/*
Bram Moolenaar368373e2010-07-19 20:46:22 +02001210 * ":py3"
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001211 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001212 void
1213ex_py3(exarg_T *eap)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001214{
1215 char_u *script;
1216
1217 script = script_get(eap, eap->arg);
1218 if (!eap->skip)
1219 {
Bram Moolenaar14816ad2019-02-18 22:04:56 +01001220 if (p_pyx == 0)
1221 p_pyx = 3;
1222
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02001223 DoPyCommand(script == NULL ? (char *) eap->arg : (char *) script,
1224 (rangeinitializer) init_range_cmd,
1225 (runner) run_cmd,
1226 (void *) eap);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001227 }
1228 vim_free(script);
1229}
1230
1231#define BUFFER_SIZE 2048
1232
1233/*
Bram Moolenaar6df6f472010-07-18 18:04:50 +02001234 * ":py3file"
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001235 */
1236 void
1237ex_py3file(exarg_T *eap)
1238{
1239 static char buffer[BUFFER_SIZE];
1240 const char *file;
1241 char *p;
1242 int i;
1243
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01001244 if (p_pyx == 0)
1245 p_pyx = 3;
1246
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001247 // Have to do it like this. PyRun_SimpleFile requires you to pass a
1248 // stdio file pointer, but Vim and the Python DLL are compiled with
1249 // different options under Windows, meaning that stdio pointers aren't
1250 // compatible between the two. Yuk.
1251 //
1252 // construct: exec(compile(open('a_filename', 'rb').read(), 'a_filename', 'exec'))
1253 //
1254 // Using bytes so that Python can detect the source encoding as it normally
1255 // does. The doc does not say "compile" accept bytes, though.
1256 //
1257 // We need to escape any backslashes or single quotes in the file name, so that
1258 // Python won't mangle the file name.
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001259
1260 strcpy(buffer, "exec(compile(open('");
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001261 p = buffer + 19; // size of "exec(compile(open('"
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001262
1263 for (i=0; i<2; ++i)
1264 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001265 file = (char *)eap->arg;
1266 while (*file && p < buffer + (BUFFER_SIZE - 3))
1267 {
1268 if (*file == '\\' || *file == '\'')
1269 *p++ = '\\';
1270 *p++ = *file++;
1271 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001272 // If we didn't finish the file name, we hit a buffer overflow
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001273 if (*file != '\0')
1274 return;
1275 if (i==0)
1276 {
Bram Moolenaar19e60942011-06-19 00:27:51 +02001277 strcpy(p,"','rb').read(),'");
1278 p += 16;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001279 }
1280 else
1281 {
1282 strcpy(p,"','exec'))");
1283 p += 10;
1284 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001285 }
1286
1287
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001288 // Execute the file
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02001289 DoPyCommand(buffer,
1290 (rangeinitializer) init_range_cmd,
1291 (runner) run_cmd,
1292 (void *) eap);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001293}
1294
Bram Moolenaar54e8f002013-05-15 19:44:39 +02001295 void
1296ex_py3do(exarg_T *eap)
Bram Moolenaar3dab2802013-05-15 18:28:13 +02001297{
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01001298 if (p_pyx == 0)
1299 p_pyx = 3;
1300
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02001301 DoPyCommand((char *)eap->arg,
1302 (rangeinitializer)init_range_cmd,
1303 (runner)run_do,
1304 (void *)eap);
Bram Moolenaar3dab2802013-05-15 18:28:13 +02001305}
1306
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001307///////////////////////////////////////////////////////
1308// 2. Python output stream: writes output via [e]msg().
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001309
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001310// Implementation functions
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001311
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001312 static PyObject *
1313OutputGetattro(PyObject *self, PyObject *nameobj)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001314{
Bram Moolenaar77045652012-09-21 13:46:06 +02001315 GET_ATTR_STRING(name, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001316
1317 if (strcmp(name, "softspace") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001318 return PyLong_FromLong(((OutputObject *)(self))->softspace);
Bram Moolenaar6d4431e2016-04-21 20:00:56 +02001319 else if (strcmp(name, "errors") == 0)
1320 return PyString_FromString("strict");
1321 else if (strcmp(name, "encoding") == 0)
1322 return PyString_FromString(ENC_OPT);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001323
1324 return PyObject_GenericGetAttr(self, nameobj);
1325}
1326
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001327 static int
1328OutputSetattro(PyObject *self, PyObject *nameobj, PyObject *val)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001329{
Bram Moolenaar77045652012-09-21 13:46:06 +02001330 GET_ATTR_STRING(name, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001331
Bram Moolenaard6e39182013-05-21 18:30:34 +02001332 return OutputSetattr((OutputObject *)(self), name, val);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001333}
1334
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001335///////////////////////////////////////////////////////
1336// 3. Implementation of the Vim module for Python
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001337
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001338// Window type - Implementation functions
1339// --------------------------------------
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001340
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001341#define WindowType_Check(obj) ((obj)->ob_base.ob_type == &WindowType)
1342
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001343// Buffer type - Implementation functions
1344// --------------------------------------
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001345
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001346#define BufferType_Check(obj) ((obj)->ob_base.ob_type == &BufferType)
1347
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001348static PyObject* BufferSubscript(PyObject *self, PyObject *idx);
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02001349static int BufferAsSubscript(PyObject *self, PyObject *idx, PyObject *val);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001350
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001351// Line range type - Implementation functions
1352// --------------------------------------
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001353
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001354#define RangeType_Check(obj) ((obj)->ob_base.ob_type == &RangeType)
1355
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001356static PyObject* RangeSubscript(PyObject *self, PyObject *idx);
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02001357static int RangeAsItem(PyObject *, Py_ssize_t, PyObject *);
1358static int RangeAsSubscript(PyObject *self, PyObject *idx, PyObject *val);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001359
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001360// Current objects type - Implementation functions
1361// -----------------------------------------------
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001362
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001363static PySequenceMethods BufferAsSeq = {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001364 (lenfunc) BufferLength, // sq_length, len(x)
1365 (binaryfunc) 0, // sq_concat, x+y
1366 (ssizeargfunc) 0, // sq_repeat, x*n
1367 (ssizeargfunc) BufferItem, // sq_item, x[i]
1368 0, // was_sq_slice, x[i:j]
1369 0, // sq_ass_item, x[i]=v
1370 0, // sq_ass_slice, x[i:j]=v
1371 0, // sq_contains
1372 0, // sq_inplace_concat
1373 0, // sq_inplace_repeat
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001374};
1375
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001376static PyMappingMethods BufferAsMapping = {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001377 /* mp_length */ (lenfunc)BufferLength,
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001378 /* mp_subscript */ (binaryfunc)BufferSubscript,
Bram Moolenaar19e60942011-06-19 00:27:51 +02001379 /* mp_ass_subscript */ (objobjargproc)BufferAsSubscript,
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001380};
1381
1382
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001383// Buffer object
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001384
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001385 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02001386BufferGetattro(PyObject *self, PyObject *nameobj)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001387{
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001388 PyObject *r;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001389
Bram Moolenaar77045652012-09-21 13:46:06 +02001390 GET_ATTR_STRING(name, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001391
Bram Moolenaar9e822c02013-05-29 22:15:30 +02001392 if ((r = BufferAttrValid((BufferObject *)(self), name)))
1393 return r;
1394
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001395 if (CheckBuffer((BufferObject *)(self)))
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001396 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001397
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001398 r = BufferAttr((BufferObject *)(self), name);
1399 if (r || PyErr_Occurred())
1400 return r;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001401 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001402 return PyObject_GenericGetAttr(self, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001403}
1404
Bram Moolenaare9ba5162013-05-29 22:02:22 +02001405 static int
1406BufferSetattro(PyObject *self, PyObject *nameobj, PyObject *val)
1407{
1408 GET_ATTR_STRING(name, nameobj);
1409
1410 return BufferSetattr((BufferObject *)(self), name, val);
1411}
1412
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001413//////////////////
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001414
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001415 static PyObject *
1416BufferSubscript(PyObject *self, PyObject* idx)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001417{
Bram Moolenaardb913952012-06-29 12:54:53 +02001418 if (PyLong_Check(idx))
1419 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001420 long _idx = PyLong_AsLong(idx);
Bram Moolenaard6e39182013-05-21 18:30:34 +02001421 return BufferItem((BufferObject *)(self), _idx);
Bram Moolenaardb913952012-06-29 12:54:53 +02001422 } else if (PySlice_Check(idx))
1423 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001424 Py_ssize_t start, stop, step, slicelen;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001425
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02001426 if (CheckBuffer((BufferObject *) self))
1427 return NULL;
1428
Bram Moolenaar922a4662014-03-30 16:11:43 +02001429 if (PySlice_GetIndicesEx((PySliceObject_T *)idx,
Bram Moolenaarbd80f352013-05-12 21:16:23 +02001430 (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count,
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001431 &start, &stop,
Bram Moolenaardb913952012-06-29 12:54:53 +02001432 &step, &slicelen) < 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001433 return NULL;
Bram Moolenaard6e39182013-05-21 18:30:34 +02001434 return BufferSlice((BufferObject *)(self), start, stop);
Bram Moolenaardb913952012-06-29 12:54:53 +02001435 }
1436 else
1437 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001438 RAISE_INVALID_INDEX_TYPE(idx);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001439 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001440 }
1441}
1442
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02001443 static int
Bram Moolenaar19e60942011-06-19 00:27:51 +02001444BufferAsSubscript(PyObject *self, PyObject* idx, PyObject* val)
1445{
Bram Moolenaardb913952012-06-29 12:54:53 +02001446 if (PyLong_Check(idx))
1447 {
Bram Moolenaar19e60942011-06-19 00:27:51 +02001448 long n = PyLong_AsLong(idx);
Bram Moolenaarab589462020-07-06 21:03:06 +02001449
1450 if (CheckBuffer((BufferObject *) self))
1451 return -1;
1452
Bram Moolenaar19e60942011-06-19 00:27:51 +02001453 return RBAsItem((BufferObject *)(self), n, val, 1,
1454 (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count,
1455 NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02001456 } else if (PySlice_Check(idx))
1457 {
Bram Moolenaar19e60942011-06-19 00:27:51 +02001458 Py_ssize_t start, stop, step, slicelen;
1459
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02001460 if (CheckBuffer((BufferObject *) self))
1461 return -1;
1462
Bram Moolenaar922a4662014-03-30 16:11:43 +02001463 if (PySlice_GetIndicesEx((PySliceObject_T *)idx,
Bram Moolenaarbd80f352013-05-12 21:16:23 +02001464 (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count,
Bram Moolenaar19e60942011-06-19 00:27:51 +02001465 &start, &stop,
Bram Moolenaardb913952012-06-29 12:54:53 +02001466 &step, &slicelen) < 0)
Bram Moolenaar19e60942011-06-19 00:27:51 +02001467 return -1;
Bram Moolenaar19e60942011-06-19 00:27:51 +02001468 return RBAsSlice((BufferObject *)(self), start, stop, val, 1,
1469 (PyInt)((BufferObject *)(self))->buf->b_ml.ml_line_count,
1470 NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02001471 }
1472 else
1473 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001474 RAISE_INVALID_INDEX_TYPE(idx);
Bram Moolenaar19e60942011-06-19 00:27:51 +02001475 return -1;
1476 }
1477}
1478
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001479static PySequenceMethods RangeAsSeq = {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001480 (lenfunc) RangeLength, // sq_length, len(x)
1481 (binaryfunc) 0, // RangeConcat, sq_concat, x+y
1482 (ssizeargfunc) 0, // RangeRepeat, sq_repeat, x*n
1483 (ssizeargfunc) RangeItem, // sq_item, x[i]
1484 0, // was_sq_slice, x[i:j]
1485 (ssizeobjargproc) RangeAsItem, // sq_as_item, x[i]=v
1486 0, // sq_ass_slice, x[i:j]=v
1487 0, // sq_contains
1488 0, // sq_inplace_concat
1489 0, // sq_inplace_repeat
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001490};
1491
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001492static PyMappingMethods RangeAsMapping = {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001493 /* mp_length */ (lenfunc)RangeLength,
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001494 /* mp_subscript */ (binaryfunc)RangeSubscript,
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001495 /* mp_ass_subscript */ (objobjargproc)RangeAsSubscript,
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001496};
1497
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001498// Line range object - Implementation
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001499
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001500 static PyObject *
1501RangeGetattro(PyObject *self, PyObject *nameobj)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001502{
Bram Moolenaar77045652012-09-21 13:46:06 +02001503 GET_ATTR_STRING(name, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001504
1505 if (strcmp(name, "start") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001506 return Py_BuildValue("n", ((RangeObject *)(self))->start - 1);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001507 else if (strcmp(name, "end") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001508 return Py_BuildValue("n", ((RangeObject *)(self))->end - 1);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001509 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001510 return PyObject_GenericGetAttr(self, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001511}
1512
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001513////////////////
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001514
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02001515 static int
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001516RangeAsItem(PyObject *self, Py_ssize_t n, PyObject *val)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001517{
1518 return RBAsItem(((RangeObject *)(self))->buf, n, val,
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001519 ((RangeObject *)(self))->start,
1520 ((RangeObject *)(self))->end,
1521 &((RangeObject *)(self))->end);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001522}
1523
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001524 static Py_ssize_t
1525RangeAsSlice(PyObject *self, Py_ssize_t lo, Py_ssize_t hi, PyObject *val)
1526{
1527 return RBAsSlice(((RangeObject *)(self))->buf, lo, hi, val,
1528 ((RangeObject *)(self))->start,
1529 ((RangeObject *)(self))->end,
1530 &((RangeObject *)(self))->end);
1531}
1532
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001533 static PyObject *
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001534RangeSubscript(PyObject *self, PyObject* idx)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001535{
Bram Moolenaardb913952012-06-29 12:54:53 +02001536 if (PyLong_Check(idx))
1537 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001538 long _idx = PyLong_AsLong(idx);
Bram Moolenaard6e39182013-05-21 18:30:34 +02001539 return RangeItem((RangeObject *)(self), _idx);
Bram Moolenaardb913952012-06-29 12:54:53 +02001540 } else if (PySlice_Check(idx))
1541 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001542 Py_ssize_t start, stop, step, slicelen;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001543
Bram Moolenaar922a4662014-03-30 16:11:43 +02001544 if (PySlice_GetIndicesEx((PySliceObject_T *)idx,
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001545 ((RangeObject *)(self))->end-((RangeObject *)(self))->start+1,
1546 &start, &stop,
Bram Moolenaardb913952012-06-29 12:54:53 +02001547 &step, &slicelen) < 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001548 return NULL;
Bram Moolenaard6e39182013-05-21 18:30:34 +02001549 return RangeSlice((RangeObject *)(self), start, stop);
Bram Moolenaardb913952012-06-29 12:54:53 +02001550 }
1551 else
1552 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001553 RAISE_INVALID_INDEX_TYPE(idx);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001554 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001555 }
1556}
1557
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02001558 static int
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001559RangeAsSubscript(PyObject *self, PyObject *idx, PyObject *val)
1560{
Bram Moolenaardb913952012-06-29 12:54:53 +02001561 if (PyLong_Check(idx))
1562 {
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001563 long n = PyLong_AsLong(idx);
1564 return RangeAsItem(self, n, val);
Bram Moolenaarabab0b02019-03-30 18:47:01 +01001565 }
1566 else if (PySlice_Check(idx))
Bram Moolenaardb913952012-06-29 12:54:53 +02001567 {
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001568 Py_ssize_t start, stop, step, slicelen;
1569
Bram Moolenaar922a4662014-03-30 16:11:43 +02001570 if (PySlice_GetIndicesEx((PySliceObject_T *)idx,
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001571 ((RangeObject *)(self))->end-((RangeObject *)(self))->start+1,
1572 &start, &stop,
Bram Moolenaardb913952012-06-29 12:54:53 +02001573 &step, &slicelen) < 0)
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001574 return -1;
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001575 return RangeAsSlice(self, start, stop, val);
Bram Moolenaardb913952012-06-29 12:54:53 +02001576 }
1577 else
1578 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001579 RAISE_INVALID_INDEX_TYPE(idx);
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001580 return -1;
1581 }
1582}
1583
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001584// TabPage object - Implementation
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001585
1586 static PyObject *
1587TabPageGetattro(PyObject *self, PyObject *nameobj)
1588{
1589 PyObject *r;
1590
1591 GET_ATTR_STRING(name, nameobj);
1592
Bram Moolenaar9e822c02013-05-29 22:15:30 +02001593 if ((r = TabPageAttrValid((TabPageObject *)(self), name)))
1594 return r;
1595
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001596 if (CheckTabPage((TabPageObject *)(self)))
1597 return NULL;
1598
1599 r = TabPageAttr((TabPageObject *)(self), name);
1600 if (r || PyErr_Occurred())
1601 return r;
1602 else
1603 return PyObject_GenericGetAttr(self, nameobj);
1604}
1605
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001606// Window object - Implementation
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001607
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001608 static PyObject *
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001609WindowGetattro(PyObject *self, PyObject *nameobj)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001610{
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001611 PyObject *r;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001612
Bram Moolenaar77045652012-09-21 13:46:06 +02001613 GET_ATTR_STRING(name, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001614
Bram Moolenaar9e822c02013-05-29 22:15:30 +02001615 if ((r = WindowAttrValid((WindowObject *)(self), name)))
1616 return r;
1617
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001618 if (CheckWindow((WindowObject *)(self)))
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001619 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001620
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001621 r = WindowAttr((WindowObject *)(self), name);
1622 if (r || PyErr_Occurred())
1623 return r;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001624 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001625 return PyObject_GenericGetAttr(self, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001626}
1627
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001628 static int
1629WindowSetattro(PyObject *self, PyObject *nameobj, PyObject *val)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001630{
Bram Moolenaar77045652012-09-21 13:46:06 +02001631 GET_ATTR_STRING(name, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001632
Bram Moolenaard6e39182013-05-21 18:30:34 +02001633 return WindowSetattr((WindowObject *)(self), name, val);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001634}
1635
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001636// Tab page list object - Definitions
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001637
1638static PySequenceMethods TabListAsSeq = {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001639 (lenfunc) TabListLength, // sq_length, len(x)
1640 (binaryfunc) 0, // sq_concat, x+y
1641 (ssizeargfunc) 0, // sq_repeat, x*n
1642 (ssizeargfunc) TabListItem, // sq_item, x[i]
1643 0, // sq_slice, x[i:j]
1644 (ssizeobjargproc)0, // sq_as_item, x[i]=v
1645 0, // sq_ass_slice, x[i:j]=v
1646 0, // sq_contains
1647 0, // sq_inplace_concat
1648 0, // sq_inplace_repeat
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001649};
1650
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001651// Window list object - Definitions
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001652
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001653static PySequenceMethods WinListAsSeq = {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001654 (lenfunc) WinListLength, // sq_length, len(x)
1655 (binaryfunc) 0, // sq_concat, x+y
1656 (ssizeargfunc) 0, // sq_repeat, x*n
1657 (ssizeargfunc) WinListItem, // sq_item, x[i]
1658 0, // sq_slice, x[i:j]
1659 (ssizeobjargproc)0, // sq_as_item, x[i]=v
1660 0, // sq_ass_slice, x[i:j]=v
1661 0, // sq_contains
1662 0, // sq_inplace_concat
1663 0, // sq_inplace_repeat
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001664};
1665
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001666/*
1667 * Current items object - Implementation
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001668 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001669 static PyObject *
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001670CurrentGetattro(PyObject *self, PyObject *nameobj)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001671{
Bram Moolenaardd8aca62013-05-29 22:36:10 +02001672 PyObject *r;
Bram Moolenaar77045652012-09-21 13:46:06 +02001673 GET_ATTR_STRING(name, nameobj);
Bram Moolenaardd8aca62013-05-29 22:36:10 +02001674 if (!(r = CurrentGetattr(self, name)))
1675 return PyObject_GenericGetAttr(self, nameobj);
1676 return r;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001677}
1678
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001679 static int
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001680CurrentSetattro(PyObject *self, PyObject *nameobj, PyObject *value)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001681{
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001682 GET_ATTR_STRING(name, nameobj);
1683 return CurrentSetattr(self, name, value);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001684}
1685
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001686// Dictionary object - Definitions
Bram Moolenaardb913952012-06-29 12:54:53 +02001687
Bram Moolenaar66b79852012-09-21 14:00:35 +02001688 static PyObject *
1689DictionaryGetattro(PyObject *self, PyObject *nameobj)
1690{
1691 DictionaryObject *this = ((DictionaryObject *) (self));
1692
1693 GET_ATTR_STRING(name, nameobj);
1694
1695 if (strcmp(name, "locked") == 0)
1696 return PyLong_FromLong(this->dict->dv_lock);
1697 else if (strcmp(name, "scope") == 0)
1698 return PyLong_FromLong(this->dict->dv_scope);
1699
1700 return PyObject_GenericGetAttr(self, nameobj);
1701}
1702
1703 static int
1704DictionarySetattro(PyObject *self, PyObject *nameobj, PyObject *val)
1705{
1706 GET_ATTR_STRING(name, nameobj);
Bram Moolenaard6e39182013-05-21 18:30:34 +02001707 return DictionarySetattr((DictionaryObject *)(self), name, val);
Bram Moolenaardb913952012-06-29 12:54:53 +02001708}
1709
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001710// List object - Definitions
Bram Moolenaardb913952012-06-29 12:54:53 +02001711
Bram Moolenaar66b79852012-09-21 14:00:35 +02001712 static PyObject *
1713ListGetattro(PyObject *self, PyObject *nameobj)
1714{
1715 GET_ATTR_STRING(name, nameobj);
1716
1717 if (strcmp(name, "locked") == 0)
1718 return PyLong_FromLong(((ListObject *) (self))->list->lv_lock);
1719
1720 return PyObject_GenericGetAttr(self, nameobj);
1721}
1722
1723 static int
1724ListSetattro(PyObject *self, PyObject *nameobj, PyObject *val)
1725{
1726 GET_ATTR_STRING(name, nameobj);
Bram Moolenaard6e39182013-05-21 18:30:34 +02001727 return ListSetattr((ListObject *)(self), name, val);
Bram Moolenaardb913952012-06-29 12:54:53 +02001728}
1729
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001730// Function object - Definitions
Bram Moolenaardb913952012-06-29 12:54:53 +02001731
Bram Moolenaardb913952012-06-29 12:54:53 +02001732 static PyObject *
1733FunctionGetattro(PyObject *self, PyObject *nameobj)
1734{
Bram Moolenaar8110a092016-04-14 15:56:09 +02001735 PyObject *r;
Bram Moolenaardb913952012-06-29 12:54:53 +02001736 FunctionObject *this = (FunctionObject *)(self);
Bram Moolenaar77045652012-09-21 13:46:06 +02001737
1738 GET_ATTR_STRING(name, nameobj);
Bram Moolenaardb913952012-06-29 12:54:53 +02001739
Bram Moolenaar8110a092016-04-14 15:56:09 +02001740 r = FunctionAttr(this, name);
1741 if (r || PyErr_Occurred())
1742 return r;
1743 else
1744 return PyObject_GenericGetAttr(self, nameobj);
Bram Moolenaardb913952012-06-29 12:54:53 +02001745}
1746
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001747// External interface
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001748
1749 void
1750python3_buffer_free(buf_T *buf)
1751{
Bram Moolenaar971db462013-05-12 18:44:48 +02001752 if (BUF_PYTHON_REF(buf) != NULL)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001753 {
Bram Moolenaar971db462013-05-12 18:44:48 +02001754 BufferObject *bp = BUF_PYTHON_REF(buf);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001755 bp->buf = INVALID_BUFFER_VALUE;
Bram Moolenaar971db462013-05-12 18:44:48 +02001756 BUF_PYTHON_REF(buf) = NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001757 }
1758}
1759
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001760 void
1761python3_window_free(win_T *win)
1762{
Bram Moolenaar971db462013-05-12 18:44:48 +02001763 if (WIN_PYTHON_REF(win) != NULL)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001764 {
Bram Moolenaar971db462013-05-12 18:44:48 +02001765 WindowObject *wp = WIN_PYTHON_REF(win);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001766 wp->win = INVALID_WINDOW_VALUE;
Bram Moolenaar971db462013-05-12 18:44:48 +02001767 WIN_PYTHON_REF(win) = NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001768 }
1769}
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001770
1771 void
1772python3_tabpage_free(tabpage_T *tab)
1773{
1774 if (TAB_PYTHON_REF(tab) != NULL)
1775 {
1776 TabPageObject *tp = TAB_PYTHON_REF(tab);
1777 tp->tab = INVALID_TABPAGE_VALUE;
1778 TAB_PYTHON_REF(tab) = NULL;
1779 }
1780}
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001781
Bram Moolenaar7854e3a2012-11-28 15:33:14 +01001782 static PyObject *
1783Py3Init_vim(void)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001784{
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001785 // The special value is removed from sys.path in Python3_Init().
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001786 static wchar_t *(argv[2]) = {L"/must>not&exist/foo", NULL};
1787
Bram Moolenaar1dc28782013-05-21 19:11:01 +02001788 if (init_types())
1789 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001790
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001791 // Set sys.argv[] to avoid a crash in warn().
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001792 PySys_SetArgv(1, argv);
1793
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001794 if ((vim_module = PyModule_Create(&vimmodule)) == NULL)
Bram Moolenaar19e60942011-06-19 00:27:51 +02001795 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001796
Bram Moolenaardee2e312013-06-23 16:35:47 +02001797 if (populate_module(vim_module))
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001798 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001799
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001800 if (init_sys_path())
1801 return NULL;
1802
1803 return vim_module;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001804}
1805
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001806//////////////////////////////////////////////////////////////////////////
1807// 4. Utility functions for handling the interface between Vim and Python.
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001808
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001809/*
1810 * Convert a Vim line into a Python string.
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001811 * All internal newlines are replaced by null characters.
1812 *
1813 * On errors, the Python exception data is set, and NULL is returned.
1814 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001815 static PyObject *
1816LineToString(const char *str)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001817{
1818 PyObject *result;
1819 Py_ssize_t len = strlen(str);
Bram Moolenaard672dde2020-02-26 13:43:51 +01001820 char *tmp, *p;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001821
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001822 tmp = alloc(len + 1);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001823 p = tmp;
1824 if (p == NULL)
1825 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001826 PyErr_NoMemory();
1827 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001828 }
1829
1830 while (*str)
1831 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001832 if (*str == '\n')
1833 *p = '\0';
1834 else
1835 *p = *str;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001836
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001837 ++p;
1838 ++str;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001839 }
1840 *p = '\0';
1841
Bram Moolenaar2e2f52a2020-12-21 16:03:02 +01001842 result = PyUnicode_Decode(tmp, len, (char *)ENC_OPT, ERRORS_DECODE_ARG);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001843
1844 vim_free(tmp);
1845 return result;
1846}
1847
Bram Moolenaardb913952012-06-29 12:54:53 +02001848 void
Bram Moolenaar8e9a24a2019-03-19 22:22:55 +01001849do_py3eval(char_u *str, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +02001850{
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02001851 DoPyCommand((char *) str,
1852 (rangeinitializer) init_range_eval,
1853 (runner) run_eval,
1854 (void *) rettv);
Bram Moolenaar8e9a24a2019-03-19 22:22:55 +01001855 if (rettv->v_type == VAR_UNKNOWN)
Bram Moolenaardb913952012-06-29 12:54:53 +02001856 {
Bram Moolenaar8e9a24a2019-03-19 22:22:55 +01001857 rettv->v_type = VAR_NUMBER;
1858 rettv->vval.v_number = 0;
Bram Moolenaardb913952012-06-29 12:54:53 +02001859 }
1860}
1861
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01001862 int
Bram Moolenaar8e9a24a2019-03-19 22:22:55 +01001863set_ref_in_python3(int copyID)
Bram Moolenaardb913952012-06-29 12:54:53 +02001864{
Bram Moolenaarb641df42015-02-03 13:16:04 +01001865 return set_ref_in_py(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02001866}