blob: db88527a5dd3b06a48bc0715022236034943ab2c [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
Martin Tournoij1a3e5742021-07-24 13:57:29 +0200128# define load_dll_error dlerror
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200129# else
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200130# define load_dll vimLoadLib
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200131# define close_dll FreeLibrary
132# define symbol_from_dll GetProcAddress
Martin Tournoij1a3e5742021-07-24 13:57:29 +0200133# define load_dll_error GetWin32Error
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200134# endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200135/*
136 * Wrapper defines
137 */
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200138# undef PyArg_Parse
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200139# define PyArg_Parse py3_PyArg_Parse
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200140# undef PyArg_ParseTuple
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200141# define PyArg_ParseTuple py3_PyArg_ParseTuple
Bram Moolenaar19e60942011-06-19 00:27:51 +0200142# define PyMem_Free py3_PyMem_Free
Bram Moolenaardb913952012-06-29 12:54:53 +0200143# define PyMem_Malloc py3_PyMem_Malloc
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200144# define PyDict_SetItemString py3_PyDict_SetItemString
145# define PyErr_BadArgument py3_PyErr_BadArgument
146# define PyErr_Clear py3_PyErr_Clear
Bram Moolenaarc476e522013-06-23 13:46:40 +0200147# define PyErr_Format py3_PyErr_Format
Bram Moolenaar4d369872013-02-20 16:09:43 +0100148# define PyErr_PrintEx py3_PyErr_PrintEx
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200149# define PyErr_NoMemory py3_PyErr_NoMemory
150# define PyErr_Occurred py3_PyErr_Occurred
151# define PyErr_SetNone py3_PyErr_SetNone
152# define PyErr_SetString py3_PyErr_SetString
Bram Moolenaar4d188da2013-05-15 15:35:09 +0200153# define PyErr_SetObject py3_PyErr_SetObject
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200154# define PyErr_ExceptionMatches py3_PyErr_ExceptionMatches
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200155# define PyEval_InitThreads py3_PyEval_InitThreads
156# define PyEval_RestoreThread py3_PyEval_RestoreThread
157# define PyEval_SaveThread py3_PyEval_SaveThread
158# define PyGILState_Ensure py3_PyGILState_Ensure
159# define PyGILState_Release py3_PyGILState_Release
160# define PyLong_AsLong py3_PyLong_AsLong
161# define PyLong_FromLong py3_PyLong_FromLong
162# define PyList_GetItem py3_PyList_GetItem
163# define PyList_Append py3_PyList_Append
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200164# define PyList_Insert py3_PyList_Insert
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200165# define PyList_New py3_PyList_New
166# define PyList_SetItem py3_PyList_SetItem
167# define PyList_Size py3_PyList_Size
Bram Moolenaardb913952012-06-29 12:54:53 +0200168# define PySequence_Check py3_PySequence_Check
169# define PySequence_Size py3_PySequence_Size
170# define PySequence_GetItem py3_PySequence_GetItem
Bram Moolenaara9922d62013-05-30 13:01:18 +0200171# define PySequence_Fast py3_PySequence_Fast
Bram Moolenaardb913952012-06-29 12:54:53 +0200172# define PyTuple_Size py3_PyTuple_Size
173# define PyTuple_GetItem py3_PyTuple_GetItem
Bram Moolenaar3b48b112018-07-04 22:03:25 +0200174# if PY_VERSION_HEX >= 0x030601f0
175# define PySlice_AdjustIndices py3_PySlice_AdjustIndices
176# define PySlice_Unpack py3_PySlice_Unpack
177# endif
178# undef PySlice_GetIndicesEx
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200179# define PySlice_GetIndicesEx py3_PySlice_GetIndicesEx
180# define PyImport_ImportModule py3_PyImport_ImportModule
181# define PyObject_Init py3__PyObject_Init
182# define PyDict_New py3_PyDict_New
183# define PyDict_GetItemString py3_PyDict_GetItemString
Bram Moolenaardb913952012-06-29 12:54:53 +0200184# define PyDict_Next py3_PyDict_Next
185# define PyMapping_Check py3_PyMapping_Check
Bram Moolenaar32ac8cd2013-07-03 18:49:17 +0200186# ifndef PyMapping_Keys
187# define PyMapping_Keys py3_PyMapping_Keys
188# endif
Zdenek Dohnal90478f32021-06-14 15:08:30 +0200189# if PY_VERSION_HEX >= 0x030a00b2
190# define PyIter_Check py3_PyIter_Check
191# endif
Bram Moolenaardb913952012-06-29 12:54:53 +0200192# define PyIter_Next py3_PyIter_Next
193# define PyObject_GetIter py3_PyObject_GetIter
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200194# define PyObject_Repr py3_PyObject_Repr
Bram Moolenaarbcb40972013-05-30 13:22:13 +0200195# define PyObject_GetItem py3_PyObject_GetItem
Bram Moolenaar03db85b2013-05-15 14:51:35 +0200196# define PyObject_IsTrue py3_PyObject_IsTrue
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200197# define PyModule_GetDict py3_PyModule_GetDict
198#undef PyRun_SimpleString
199# define PyRun_SimpleString py3_PyRun_SimpleString
Bram Moolenaardb913952012-06-29 12:54:53 +0200200#undef PyRun_String
201# define PyRun_String py3_PyRun_String
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200202# define PyObject_GetAttrString py3_PyObject_GetAttrString
Bram Moolenaara9922d62013-05-30 13:01:18 +0200203# define PyObject_HasAttrString py3_PyObject_HasAttrString
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200204# define PyObject_SetAttrString py3_PyObject_SetAttrString
205# define PyObject_CallFunctionObjArgs py3_PyObject_CallFunctionObjArgs
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200206# define _PyObject_CallFunction_SizeT py3__PyObject_CallFunction_SizeT
Bram Moolenaarf4258302013-06-02 18:20:17 +0200207# define PyObject_Call py3_PyObject_Call
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200208# define PyEval_GetLocals py3_PyEval_GetLocals
209# define PyEval_GetGlobals py3_PyEval_GetGlobals
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200210# define PySys_SetObject py3_PySys_SetObject
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200211# define PySys_GetObject py3_PySys_GetObject
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200212# define PySys_SetArgv py3_PySys_SetArgv
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200213# define PyType_Ready py3_PyType_Ready
Bram Moolenaaree1b9312020-07-16 22:15:53 +0200214# if PY_VERSION_HEX >= 0x030900b0
215# define PyType_GetFlags py3_PyType_GetFlags
216# endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200217#undef Py_BuildValue
218# define Py_BuildValue py3_Py_BuildValue
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100219# define Py_SetPythonHome py3_Py_SetPythonHome
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200220# define Py_Initialize py3_Py_Initialize
221# define Py_Finalize py3_Py_Finalize
222# define Py_IsInitialized py3_Py_IsInitialized
223# define _Py_NoneStruct (*py3__Py_NoneStruct)
Bram Moolenaar66b79852012-09-21 14:00:35 +0200224# define _Py_FalseStruct (*py3__Py_FalseStruct)
225# define _Py_TrueStruct (*py3__Py_TrueStruct)
Bram Moolenaardb913952012-06-29 12:54:53 +0200226# define _PyObject_NextNotImplemented (*py3__PyObject_NextNotImplemented)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200227# define PyModule_AddObject py3_PyModule_AddObject
228# define PyImport_AppendInittab py3_PyImport_AppendInittab
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200229# define PyImport_AddModule py3_PyImport_AddModule
Bram Moolenaar7bc4f932012-10-14 03:22:56 +0200230# if PY_VERSION_HEX >= 0x030300f0
231# undef _PyUnicode_AsString
Bram Moolenaar9c9cbf12012-10-23 05:17:37 +0200232# define _PyUnicode_AsString py3_PyUnicode_AsUTF8
Bram Moolenaar7bc4f932012-10-14 03:22:56 +0200233# else
234# define _PyUnicode_AsString py3__PyUnicode_AsString
235# endif
Bram Moolenaar19e60942011-06-19 00:27:51 +0200236# undef PyUnicode_AsEncodedString
237# define PyUnicode_AsEncodedString py3_PyUnicode_AsEncodedString
238# undef PyBytes_AsString
239# define PyBytes_AsString py3_PyBytes_AsString
Bram Moolenaar32ac8cd2013-07-03 18:49:17 +0200240# ifndef PyBytes_AsStringAndSize
241# define PyBytes_AsStringAndSize py3_PyBytes_AsStringAndSize
242# endif
Bram Moolenaardb913952012-06-29 12:54:53 +0200243# undef PyBytes_FromString
244# define PyBytes_FromString py3_PyBytes_FromString
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100245# undef PyBytes_FromStringAndSize
246# define PyBytes_FromStringAndSize py3_PyBytes_FromStringAndSize
Bram Moolenaaree1b9312020-07-16 22:15:53 +0200247# if defined(Py_DEBUG) || PY_VERSION_HEX >= 0x030900b0
248# define _Py_Dealloc py3__Py_Dealloc
249# endif
Bram Moolenaardb913952012-06-29 12:54:53 +0200250# define PyFloat_FromDouble py3_PyFloat_FromDouble
251# define PyFloat_AsDouble py3_PyFloat_AsDouble
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200252# define PyObject_GenericGetAttr py3_PyObject_GenericGetAttr
Bram Moolenaar66b79852012-09-21 14:00:35 +0200253# define PyType_Type (*py3_PyType_Type)
Bram Moolenaard4a8c982018-05-15 22:31:18 +0200254# define PyStdPrinter_Type (*py3_PyStdPrinter_Type)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200255# define PySlice_Type (*py3_PySlice_Type)
Bram Moolenaardb913952012-06-29 12:54:53 +0200256# define PyFloat_Type (*py3_PyFloat_Type)
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200257# define PyNumber_Check (*py3_PyNumber_Check)
258# define PyNumber_Long (*py3_PyNumber_Long)
Bram Moolenaar66b79852012-09-21 14:00:35 +0200259# define PyBool_Type (*py3_PyBool_Type)
Bram Moolenaar19e60942011-06-19 00:27:51 +0200260# define PyErr_NewException py3_PyErr_NewException
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200261# ifdef Py_DEBUG
262# define _Py_NegativeRefcount py3__Py_NegativeRefcount
263# define _Py_RefTotal (*py3__Py_RefTotal)
Bram Moolenaar0014a532013-05-29 21:33:39 +0200264# define PyModule_Create2TraceRefs py3_PyModule_Create2TraceRefs
265# else
266# define PyModule_Create2 py3_PyModule_Create2
267# endif
268# if defined(Py_DEBUG) && !defined(Py_DEBUG_NO_PYMALLOC)
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200269# define _PyObject_DebugMalloc py3__PyObject_DebugMalloc
270# define _PyObject_DebugFree py3__PyObject_DebugFree
271# else
272# define PyObject_Malloc py3_PyObject_Malloc
273# define PyObject_Free py3_PyObject_Free
274# endif
Bram Moolenaar774267b2013-05-21 20:51:59 +0200275# define _PyObject_GC_New py3__PyObject_GC_New
276# define PyObject_GC_Del py3_PyObject_GC_Del
277# define PyObject_GC_UnTrack py3_PyObject_GC_UnTrack
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200278# define PyType_GenericAlloc py3_PyType_GenericAlloc
279# define PyType_GenericNew py3_PyType_GenericNew
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200280# undef PyUnicode_FromString
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200281# define PyUnicode_FromString py3_PyUnicode_FromString
Bram Moolenaar1a3b5692013-05-30 12:40:39 +0200282# ifndef PyUnicode_FromFormat
283# define PyUnicode_FromFormat py3_PyUnicode_FromFormat
284# else
285# define Py_UNICODE_USE_UCS_FUNCTIONS
286# ifdef Py_UNICODE_WIDE
287# define PyUnicodeUCS4_FromFormat py3_PyUnicodeUCS4_FromFormat
288# else
289# define PyUnicodeUCS2_FromFormat py3_PyUnicodeUCS2_FromFormat
290# endif
291# endif
Bram Moolenaar19e60942011-06-19 00:27:51 +0200292# undef PyUnicode_Decode
293# define PyUnicode_Decode py3_PyUnicode_Decode
Bram Moolenaardb913952012-06-29 12:54:53 +0200294# define PyType_IsSubtype py3_PyType_IsSubtype
295# define PyCapsule_New py3_PyCapsule_New
296# define PyCapsule_GetPointer py3_PyCapsule_GetPointer
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200297
Bram Moolenaar0014a532013-05-29 21:33:39 +0200298# if defined(Py_DEBUG) && !defined(Py_DEBUG_NO_PYMALLOC)
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200299# undef PyObject_NEW
300# define PyObject_NEW(type, typeobj) \
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200301( (type *) PyObject_Init( \
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200302 (PyObject *) _PyObject_DebugMalloc( _PyObject_SIZE(typeobj) ), (typeobj)) )
Bram Moolenaaree1b9312020-07-16 22:15:53 +0200303# elif PY_VERSION_HEX >= 0x030900b0
304# undef PyObject_NEW
305# define PyObject_NEW(type, typeobj) \
306 ((type *)py3__PyObject_New(typeobj))
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200307# endif
308
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200309/*
310 * Pointers for dynamic link
311 */
312static int (*py3_PySys_SetArgv)(int, wchar_t **);
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100313static void (*py3_Py_SetPythonHome)(wchar_t *home);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200314static void (*py3_Py_Initialize)(void);
315static PyObject* (*py3_PyList_New)(Py_ssize_t size);
316static PyGILState_STATE (*py3_PyGILState_Ensure)(void);
317static void (*py3_PyGILState_Release)(PyGILState_STATE);
318static int (*py3_PySys_SetObject)(char *, PyObject *);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200319static PyObject* (*py3_PySys_GetObject)(char *);
320static int (*py3_PyList_Append)(PyObject *, PyObject *);
321static int (*py3_PyList_Insert)(PyObject *, int, PyObject *);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200322static Py_ssize_t (*py3_PyList_Size)(PyObject *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200323static int (*py3_PySequence_Check)(PyObject *);
324static Py_ssize_t (*py3_PySequence_Size)(PyObject *);
325static PyObject* (*py3_PySequence_GetItem)(PyObject *, Py_ssize_t);
Bram Moolenaara9922d62013-05-30 13:01:18 +0200326static PyObject* (*py3_PySequence_Fast)(PyObject *, const char *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200327static Py_ssize_t (*py3_PyTuple_Size)(PyObject *);
328static PyObject* (*py3_PyTuple_GetItem)(PyObject *, Py_ssize_t);
329static int (*py3_PyMapping_Check)(PyObject *);
Bram Moolenaarbcb40972013-05-30 13:22:13 +0200330static PyObject* (*py3_PyMapping_Keys)(PyObject *);
Bram Moolenaar3b48b112018-07-04 22:03:25 +0200331# if PY_VERSION_HEX >= 0x030601f0
332static int (*py3_PySlice_AdjustIndices)(Py_ssize_t length,
333 Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t step);
334static int (*py3_PySlice_Unpack)(PyObject *slice,
335 Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t *step);
336# endif
Bram Moolenaar922a4662014-03-30 16:11:43 +0200337static int (*py3_PySlice_GetIndicesEx)(PySliceObject_T *r, Py_ssize_t length,
Bram Moolenaar063a46b2014-01-14 16:36:51 +0100338 Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t *step,
339 Py_ssize_t *slicelen);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200340static PyObject* (*py3_PyErr_NoMemory)(void);
341static void (*py3_Py_Finalize)(void);
342static void (*py3_PyErr_SetString)(PyObject *, const char *);
Bram Moolenaar4d188da2013-05-15 15:35:09 +0200343static void (*py3_PyErr_SetObject)(PyObject *, PyObject *);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200344static int (*py3_PyErr_ExceptionMatches)(PyObject *);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200345static int (*py3_PyRun_SimpleString)(char *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200346static PyObject* (*py3_PyRun_String)(char *, int, PyObject *, PyObject *);
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200347static PyObject* (*py3_PyObject_GetAttrString)(PyObject *, const char *);
Bram Moolenaara9922d62013-05-30 13:01:18 +0200348static int (*py3_PyObject_HasAttrString)(PyObject *, const char *);
Bram Moolenaar0b400082013-11-03 00:28:25 +0100349static int (*py3_PyObject_SetAttrString)(PyObject *, const char *, PyObject *);
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200350static PyObject* (*py3_PyObject_CallFunctionObjArgs)(PyObject *, ...);
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200351static PyObject* (*py3__PyObject_CallFunction_SizeT)(PyObject *, char *, ...);
Bram Moolenaarf4258302013-06-02 18:20:17 +0200352static PyObject* (*py3_PyObject_Call)(PyObject *, PyObject *, PyObject *);
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200353static PyObject* (*py3_PyEval_GetGlobals)();
354static PyObject* (*py3_PyEval_GetLocals)();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200355static PyObject* (*py3_PyList_GetItem)(PyObject *, Py_ssize_t);
356static PyObject* (*py3_PyImport_ImportModule)(const char *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200357static PyObject* (*py3_PyImport_AddModule)(const char *);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200358static int (*py3_PyErr_BadArgument)(void);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200359static PyObject* (*py3_PyErr_Occurred)(void);
360static PyObject* (*py3_PyModule_GetDict)(PyObject *);
361static int (*py3_PyList_SetItem)(PyObject *, Py_ssize_t, PyObject *);
362static PyObject* (*py3_PyDict_GetItemString)(PyObject *, const char *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200363static int (*py3_PyDict_Next)(PyObject *, Py_ssize_t *, PyObject **, PyObject **);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200364static PyObject* (*py3_PyLong_FromLong)(long);
365static PyObject* (*py3_PyDict_New)(void);
Zdenek Dohnal90478f32021-06-14 15:08:30 +0200366# if PY_VERSION_HEX >= 0x030a00b2
367static int (*py3_PyIter_Check)(PyObject *o);
368# endif
Bram Moolenaardb913952012-06-29 12:54:53 +0200369static PyObject* (*py3_PyIter_Next)(PyObject *);
370static PyObject* (*py3_PyObject_GetIter)(PyObject *);
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200371static PyObject* (*py3_PyObject_Repr)(PyObject *);
Bram Moolenaarbcb40972013-05-30 13:22:13 +0200372static PyObject* (*py3_PyObject_GetItem)(PyObject *, PyObject *);
Bram Moolenaar03db85b2013-05-15 14:51:35 +0200373static int (*py3_PyObject_IsTrue)(PyObject *);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200374static PyObject* (*py3_Py_BuildValue)(char *, ...);
Bram Moolenaaree1b9312020-07-16 22:15:53 +0200375# if PY_VERSION_HEX >= 0x030900b0
376static int (*py3_PyType_GetFlags)(PyTypeObject *o);
377# endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200378static int (*py3_PyType_Ready)(PyTypeObject *type);
379static int (*py3_PyDict_SetItemString)(PyObject *dp, char *key, PyObject *item);
380static PyObject* (*py3_PyUnicode_FromString)(const char *u);
Bram Moolenaar1a3b5692013-05-30 12:40:39 +0200381# ifndef Py_UNICODE_USE_UCS_FUNCTIONS
382static PyObject* (*py3_PyUnicode_FromFormat)(const char *u, ...);
383# else
384# ifdef Py_UNICODE_WIDE
385static PyObject* (*py3_PyUnicodeUCS4_FromFormat)(const char *u, ...);
386# else
387static PyObject* (*py3_PyUnicodeUCS2_FromFormat)(const char *u, ...);
388# endif
389# endif
Bram Moolenaar19e60942011-06-19 00:27:51 +0200390static PyObject* (*py3_PyUnicode_Decode)(const char *u, Py_ssize_t size,
391 const char *encoding, const char *errors);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200392static long (*py3_PyLong_AsLong)(PyObject *);
393static void (*py3_PyErr_SetNone)(PyObject *);
394static void (*py3_PyEval_InitThreads)(void);
395static void(*py3_PyEval_RestoreThread)(PyThreadState *);
396static PyThreadState*(*py3_PyEval_SaveThread)(void);
397static int (*py3_PyArg_Parse)(PyObject *, char *, ...);
398static int (*py3_PyArg_ParseTuple)(PyObject *, char *, ...);
Bram Moolenaar19e60942011-06-19 00:27:51 +0200399static int (*py3_PyMem_Free)(void *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200400static void* (*py3_PyMem_Malloc)(size_t);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200401static int (*py3_Py_IsInitialized)(void);
402static void (*py3_PyErr_Clear)(void);
Bram Moolenaarc476e522013-06-23 13:46:40 +0200403static PyObject* (*py3_PyErr_Format)(PyObject *, const char *, ...);
Bram Moolenaar4d369872013-02-20 16:09:43 +0100404static void (*py3_PyErr_PrintEx)(int);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200405static PyObject*(*py3__PyObject_Init)(PyObject *, PyTypeObject *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200406static iternextfunc py3__PyObject_NextNotImplemented;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200407static PyObject* py3__Py_NoneStruct;
Bram Moolenaar66b79852012-09-21 14:00:35 +0200408static PyObject* py3__Py_FalseStruct;
409static PyObject* py3__Py_TrueStruct;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200410static int (*py3_PyModule_AddObject)(PyObject *m, const char *name, PyObject *o);
411static int (*py3_PyImport_AppendInittab)(const char *name, PyObject* (*initfunc)(void));
Bram Moolenaar9c9cbf12012-10-23 05:17:37 +0200412# if PY_VERSION_HEX >= 0x030300f0
413static char* (*py3_PyUnicode_AsUTF8)(PyObject *unicode);
414# else
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200415static char* (*py3__PyUnicode_AsString)(PyObject *unicode);
Bram Moolenaar9c9cbf12012-10-23 05:17:37 +0200416# endif
Bram Moolenaar19e60942011-06-19 00:27:51 +0200417static PyObject* (*py3_PyUnicode_AsEncodedString)(PyObject *unicode, const char* encoding, const char* errors);
418static char* (*py3_PyBytes_AsString)(PyObject *bytes);
Bram Moolenaar808c2bc2013-06-23 13:11:18 +0200419static int (*py3_PyBytes_AsStringAndSize)(PyObject *bytes, char **buffer, Py_ssize_t *length);
Bram Moolenaardb913952012-06-29 12:54:53 +0200420static PyObject* (*py3_PyBytes_FromString)(char *str);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100421static PyObject* (*py3_PyBytes_FromStringAndSize)(char *str, Py_ssize_t length);
Bram Moolenaaree1b9312020-07-16 22:15:53 +0200422# if defined(Py_DEBUG) || PY_VERSION_HEX >= 0x030900b0
423static void (*py3__Py_Dealloc)(PyObject *obj);
424# endif
425# if PY_VERSION_HEX >= 0x030900b0
426static PyObject* (*py3__PyObject_New)(PyTypeObject *);
427# endif
Bram Moolenaardb913952012-06-29 12:54:53 +0200428static PyObject* (*py3_PyFloat_FromDouble)(double num);
429static double (*py3_PyFloat_AsDouble)(PyObject *);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200430static PyObject* (*py3_PyObject_GenericGetAttr)(PyObject *obj, PyObject *name);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200431static PyObject* (*py3_PyType_GenericAlloc)(PyTypeObject *type, Py_ssize_t nitems);
432static PyObject* (*py3_PyType_GenericNew)(PyTypeObject *type, PyObject *args, PyObject *kwds);
Bram Moolenaar66b79852012-09-21 14:00:35 +0200433static PyTypeObject* py3_PyType_Type;
Bram Moolenaard4a8c982018-05-15 22:31:18 +0200434static PyTypeObject* py3_PyStdPrinter_Type;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200435static PyTypeObject* py3_PySlice_Type;
Bram Moolenaardb913952012-06-29 12:54:53 +0200436static PyTypeObject* py3_PyFloat_Type;
Bram Moolenaar66b79852012-09-21 14:00:35 +0200437static PyTypeObject* py3_PyBool_Type;
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200438static int (*py3_PyNumber_Check)(PyObject *);
439static PyObject* (*py3_PyNumber_Long)(PyObject *);
Bram Moolenaar19e60942011-06-19 00:27:51 +0200440static PyObject* (*py3_PyErr_NewException)(char *name, PyObject *base, PyObject *dict);
Bram Moolenaardb913952012-06-29 12:54:53 +0200441static PyObject* (*py3_PyCapsule_New)(void *, char *, PyCapsule_Destructor);
442static void* (*py3_PyCapsule_GetPointer)(PyObject *, char *);
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200443# ifdef Py_DEBUG
Bram Moolenaar0014a532013-05-29 21:33:39 +0200444static void (*py3__Py_NegativeRefcount)(const char *fname, int lineno, PyObject *op);
445static Py_ssize_t* py3__Py_RefTotal;
Bram Moolenaar0014a532013-05-29 21:33:39 +0200446static PyObject* (*py3_PyModule_Create2TraceRefs)(struct PyModuleDef* module, int module_api_version);
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200447# else
Bram Moolenaar0014a532013-05-29 21:33:39 +0200448static PyObject* (*py3_PyModule_Create2)(struct PyModuleDef* module, int module_api_version);
449# endif
450# if defined(Py_DEBUG) && !defined(Py_DEBUG_NO_PYMALLOC)
451static void (*py3__PyObject_DebugFree)(void*);
452static void* (*py3__PyObject_DebugMalloc)(size_t);
453# else
454static void (*py3_PyObject_Free)(void*);
455static void* (*py3_PyObject_Malloc)(size_t);
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200456# endif
Bram Moolenaar774267b2013-05-21 20:51:59 +0200457static PyObject*(*py3__PyObject_GC_New)(PyTypeObject *);
458static void(*py3_PyObject_GC_Del)(void *);
459static void(*py3_PyObject_GC_UnTrack)(void *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200460static int (*py3_PyType_IsSubtype)(PyTypeObject *, PyTypeObject *);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200461
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100462static HINSTANCE hinstPy3 = 0; // Instance of python.dll
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200463
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100464// Imported exception objects
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200465static PyObject *p3imp_PyExc_AttributeError;
466static PyObject *p3imp_PyExc_IndexError;
Bram Moolenaaraf6abb92013-04-24 13:04:26 +0200467static PyObject *p3imp_PyExc_KeyError;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200468static PyObject *p3imp_PyExc_KeyboardInterrupt;
469static PyObject *p3imp_PyExc_TypeError;
470static PyObject *p3imp_PyExc_ValueError;
Bram Moolenaar41009372013-07-01 22:03:04 +0200471static PyObject *p3imp_PyExc_SystemExit;
Bram Moolenaar8661b172013-05-15 15:44:28 +0200472static PyObject *p3imp_PyExc_RuntimeError;
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200473static PyObject *p3imp_PyExc_ImportError;
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200474static PyObject *p3imp_PyExc_OverflowError;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200475
476# define PyExc_AttributeError p3imp_PyExc_AttributeError
477# define PyExc_IndexError p3imp_PyExc_IndexError
Bram Moolenaaraf6abb92013-04-24 13:04:26 +0200478# define PyExc_KeyError p3imp_PyExc_KeyError
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200479# define PyExc_KeyboardInterrupt p3imp_PyExc_KeyboardInterrupt
480# define PyExc_TypeError p3imp_PyExc_TypeError
481# define PyExc_ValueError p3imp_PyExc_ValueError
Bram Moolenaar41009372013-07-01 22:03:04 +0200482# define PyExc_SystemExit p3imp_PyExc_SystemExit
Bram Moolenaar8661b172013-05-15 15:44:28 +0200483# define PyExc_RuntimeError p3imp_PyExc_RuntimeError
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200484# define PyExc_ImportError p3imp_PyExc_ImportError
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200485# define PyExc_OverflowError p3imp_PyExc_OverflowError
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200486
487/*
488 * Table of name to function pointer of python.
489 */
490# define PYTHON_PROC FARPROC
491static struct
492{
493 char *name;
494 PYTHON_PROC *ptr;
495} py3_funcname_table[] =
496{
497 {"PySys_SetArgv", (PYTHON_PROC*)&py3_PySys_SetArgv},
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100498 {"Py_SetPythonHome", (PYTHON_PROC*)&py3_Py_SetPythonHome},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200499 {"Py_Initialize", (PYTHON_PROC*)&py3_Py_Initialize},
Bram Moolenaare8cdcef2012-09-12 20:21:43 +0200500 {"_PyArg_ParseTuple_SizeT", (PYTHON_PROC*)&py3_PyArg_ParseTuple},
501 {"_Py_BuildValue_SizeT", (PYTHON_PROC*)&py3_Py_BuildValue},
Bram Moolenaar19e60942011-06-19 00:27:51 +0200502 {"PyMem_Free", (PYTHON_PROC*)&py3_PyMem_Free},
Bram Moolenaardb913952012-06-29 12:54:53 +0200503 {"PyMem_Malloc", (PYTHON_PROC*)&py3_PyMem_Malloc},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200504 {"PyList_New", (PYTHON_PROC*)&py3_PyList_New},
505 {"PyGILState_Ensure", (PYTHON_PROC*)&py3_PyGILState_Ensure},
506 {"PyGILState_Release", (PYTHON_PROC*)&py3_PyGILState_Release},
507 {"PySys_SetObject", (PYTHON_PROC*)&py3_PySys_SetObject},
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200508 {"PySys_GetObject", (PYTHON_PROC*)&py3_PySys_GetObject},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200509 {"PyList_Append", (PYTHON_PROC*)&py3_PyList_Append},
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200510 {"PyList_Insert", (PYTHON_PROC*)&py3_PyList_Insert},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200511 {"PyList_Size", (PYTHON_PROC*)&py3_PyList_Size},
Bram Moolenaardb913952012-06-29 12:54:53 +0200512 {"PySequence_Check", (PYTHON_PROC*)&py3_PySequence_Check},
513 {"PySequence_Size", (PYTHON_PROC*)&py3_PySequence_Size},
514 {"PySequence_GetItem", (PYTHON_PROC*)&py3_PySequence_GetItem},
Bram Moolenaara9922d62013-05-30 13:01:18 +0200515 {"PySequence_Fast", (PYTHON_PROC*)&py3_PySequence_Fast},
Bram Moolenaardb913952012-06-29 12:54:53 +0200516 {"PyTuple_Size", (PYTHON_PROC*)&py3_PyTuple_Size},
517 {"PyTuple_GetItem", (PYTHON_PROC*)&py3_PyTuple_GetItem},
Bram Moolenaar3b48b112018-07-04 22:03:25 +0200518# if PY_VERSION_HEX >= 0x030601f0
519 {"PySlice_AdjustIndices", (PYTHON_PROC*)&py3_PySlice_AdjustIndices},
520 {"PySlice_Unpack", (PYTHON_PROC*)&py3_PySlice_Unpack},
521# endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200522 {"PySlice_GetIndicesEx", (PYTHON_PROC*)&py3_PySlice_GetIndicesEx},
523 {"PyErr_NoMemory", (PYTHON_PROC*)&py3_PyErr_NoMemory},
524 {"Py_Finalize", (PYTHON_PROC*)&py3_Py_Finalize},
525 {"PyErr_SetString", (PYTHON_PROC*)&py3_PyErr_SetString},
Bram Moolenaar4d188da2013-05-15 15:35:09 +0200526 {"PyErr_SetObject", (PYTHON_PROC*)&py3_PyErr_SetObject},
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200527 {"PyErr_ExceptionMatches", (PYTHON_PROC*)&py3_PyErr_ExceptionMatches},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200528 {"PyRun_SimpleString", (PYTHON_PROC*)&py3_PyRun_SimpleString},
Bram Moolenaardb913952012-06-29 12:54:53 +0200529 {"PyRun_String", (PYTHON_PROC*)&py3_PyRun_String},
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200530 {"PyObject_GetAttrString", (PYTHON_PROC*)&py3_PyObject_GetAttrString},
Bram Moolenaara9922d62013-05-30 13:01:18 +0200531 {"PyObject_HasAttrString", (PYTHON_PROC*)&py3_PyObject_HasAttrString},
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200532 {"PyObject_SetAttrString", (PYTHON_PROC*)&py3_PyObject_SetAttrString},
533 {"PyObject_CallFunctionObjArgs", (PYTHON_PROC*)&py3_PyObject_CallFunctionObjArgs},
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200534 {"_PyObject_CallFunction_SizeT", (PYTHON_PROC*)&py3__PyObject_CallFunction_SizeT},
Bram Moolenaarf4258302013-06-02 18:20:17 +0200535 {"PyObject_Call", (PYTHON_PROC*)&py3_PyObject_Call},
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200536 {"PyEval_GetGlobals", (PYTHON_PROC*)&py3_PyEval_GetGlobals},
537 {"PyEval_GetLocals", (PYTHON_PROC*)&py3_PyEval_GetLocals},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200538 {"PyList_GetItem", (PYTHON_PROC*)&py3_PyList_GetItem},
539 {"PyImport_ImportModule", (PYTHON_PROC*)&py3_PyImport_ImportModule},
Bram Moolenaardb913952012-06-29 12:54:53 +0200540 {"PyImport_AddModule", (PYTHON_PROC*)&py3_PyImport_AddModule},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200541 {"PyErr_BadArgument", (PYTHON_PROC*)&py3_PyErr_BadArgument},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200542 {"PyErr_Occurred", (PYTHON_PROC*)&py3_PyErr_Occurred},
543 {"PyModule_GetDict", (PYTHON_PROC*)&py3_PyModule_GetDict},
544 {"PyList_SetItem", (PYTHON_PROC*)&py3_PyList_SetItem},
545 {"PyDict_GetItemString", (PYTHON_PROC*)&py3_PyDict_GetItemString},
Bram Moolenaardb913952012-06-29 12:54:53 +0200546 {"PyDict_Next", (PYTHON_PROC*)&py3_PyDict_Next},
547 {"PyMapping_Check", (PYTHON_PROC*)&py3_PyMapping_Check},
Bram Moolenaarbcb40972013-05-30 13:22:13 +0200548 {"PyMapping_Keys", (PYTHON_PROC*)&py3_PyMapping_Keys},
Zdenek Dohnal90478f32021-06-14 15:08:30 +0200549# if PY_VERSION_HEX >= 0x030a00b2
550 {"PyIter_Check", (PYTHON_PROC*)&py3_PyIter_Check},
551# endif
Bram Moolenaardb913952012-06-29 12:54:53 +0200552 {"PyIter_Next", (PYTHON_PROC*)&py3_PyIter_Next},
553 {"PyObject_GetIter", (PYTHON_PROC*)&py3_PyObject_GetIter},
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200554 {"PyObject_Repr", (PYTHON_PROC*)&py3_PyObject_Repr},
Bram Moolenaarbcb40972013-05-30 13:22:13 +0200555 {"PyObject_GetItem", (PYTHON_PROC*)&py3_PyObject_GetItem},
Bram Moolenaar03db85b2013-05-15 14:51:35 +0200556 {"PyObject_IsTrue", (PYTHON_PROC*)&py3_PyObject_IsTrue},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200557 {"PyLong_FromLong", (PYTHON_PROC*)&py3_PyLong_FromLong},
558 {"PyDict_New", (PYTHON_PROC*)&py3_PyDict_New},
Bram Moolenaaree1b9312020-07-16 22:15:53 +0200559# if PY_VERSION_HEX >= 0x030900b0
560 {"PyType_GetFlags", (PYTHON_PROC*)&py3_PyType_GetFlags},
561# endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200562 {"PyType_Ready", (PYTHON_PROC*)&py3_PyType_Ready},
563 {"PyDict_SetItemString", (PYTHON_PROC*)&py3_PyDict_SetItemString},
564 {"PyLong_AsLong", (PYTHON_PROC*)&py3_PyLong_AsLong},
565 {"PyErr_SetNone", (PYTHON_PROC*)&py3_PyErr_SetNone},
566 {"PyEval_InitThreads", (PYTHON_PROC*)&py3_PyEval_InitThreads},
567 {"PyEval_RestoreThread", (PYTHON_PROC*)&py3_PyEval_RestoreThread},
568 {"PyEval_SaveThread", (PYTHON_PROC*)&py3_PyEval_SaveThread},
Bram Moolenaar5f87b232013-06-13 20:57:50 +0200569 {"_PyArg_Parse_SizeT", (PYTHON_PROC*)&py3_PyArg_Parse},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200570 {"Py_IsInitialized", (PYTHON_PROC*)&py3_Py_IsInitialized},
Bram Moolenaardb913952012-06-29 12:54:53 +0200571 {"_PyObject_NextNotImplemented", (PYTHON_PROC*)&py3__PyObject_NextNotImplemented},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200572 {"_Py_NoneStruct", (PYTHON_PROC*)&py3__Py_NoneStruct},
Bram Moolenaar66b79852012-09-21 14:00:35 +0200573 {"_Py_FalseStruct", (PYTHON_PROC*)&py3__Py_FalseStruct},
574 {"_Py_TrueStruct", (PYTHON_PROC*)&py3__Py_TrueStruct},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200575 {"PyErr_Clear", (PYTHON_PROC*)&py3_PyErr_Clear},
Bram Moolenaarc476e522013-06-23 13:46:40 +0200576 {"PyErr_Format", (PYTHON_PROC*)&py3_PyErr_Format},
Bram Moolenaar4d369872013-02-20 16:09:43 +0100577 {"PyErr_PrintEx", (PYTHON_PROC*)&py3_PyErr_PrintEx},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200578 {"PyObject_Init", (PYTHON_PROC*)&py3__PyObject_Init},
579 {"PyModule_AddObject", (PYTHON_PROC*)&py3_PyModule_AddObject},
580 {"PyImport_AppendInittab", (PYTHON_PROC*)&py3_PyImport_AppendInittab},
Bram Moolenaar9c9cbf12012-10-23 05:17:37 +0200581# if PY_VERSION_HEX >= 0x030300f0
582 {"PyUnicode_AsUTF8", (PYTHON_PROC*)&py3_PyUnicode_AsUTF8},
583# else
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200584 {"_PyUnicode_AsString", (PYTHON_PROC*)&py3__PyUnicode_AsString},
Bram Moolenaar9c9cbf12012-10-23 05:17:37 +0200585# endif
Bram Moolenaar1a3b5692013-05-30 12:40:39 +0200586# ifndef Py_UNICODE_USE_UCS_FUNCTIONS
587 {"PyUnicode_FromFormat", (PYTHON_PROC*)&py3_PyUnicode_FromFormat},
588# else
589# ifdef Py_UNICODE_WIDE
590 {"PyUnicodeUCS4_FromFormat", (PYTHON_PROC*)&py3_PyUnicodeUCS4_FromFormat},
591# else
592 {"PyUnicodeUCS2_FromFormat", (PYTHON_PROC*)&py3_PyUnicodeUCS2_FromFormat},
593# endif
594# endif
Bram Moolenaar19e60942011-06-19 00:27:51 +0200595 {"PyBytes_AsString", (PYTHON_PROC*)&py3_PyBytes_AsString},
Bram Moolenaarcdab9052012-09-05 19:03:56 +0200596 {"PyBytes_AsStringAndSize", (PYTHON_PROC*)&py3_PyBytes_AsStringAndSize},
Bram Moolenaardb913952012-06-29 12:54:53 +0200597 {"PyBytes_FromString", (PYTHON_PROC*)&py3_PyBytes_FromString},
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100598 {"PyBytes_FromStringAndSize", (PYTHON_PROC*)&py3_PyBytes_FromStringAndSize},
Bram Moolenaaree1b9312020-07-16 22:15:53 +0200599# if defined(Py_DEBUG) || PY_VERSION_HEX >= 0x030900b0
600 {"_Py_Dealloc", (PYTHON_PROC*)&py3__Py_Dealloc},
601# endif
602# if PY_VERSION_HEX >= 0x030900b0
603 {"_PyObject_New", (PYTHON_PROC*)&py3__PyObject_New},
604# endif
Bram Moolenaardb913952012-06-29 12:54:53 +0200605 {"PyFloat_FromDouble", (PYTHON_PROC*)&py3_PyFloat_FromDouble},
606 {"PyFloat_AsDouble", (PYTHON_PROC*)&py3_PyFloat_AsDouble},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200607 {"PyObject_GenericGetAttr", (PYTHON_PROC*)&py3_PyObject_GenericGetAttr},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200608 {"PyType_GenericAlloc", (PYTHON_PROC*)&py3_PyType_GenericAlloc},
609 {"PyType_GenericNew", (PYTHON_PROC*)&py3_PyType_GenericNew},
Bram Moolenaar66b79852012-09-21 14:00:35 +0200610 {"PyType_Type", (PYTHON_PROC*)&py3_PyType_Type},
Bram Moolenaard4a8c982018-05-15 22:31:18 +0200611 {"PyStdPrinter_Type", (PYTHON_PROC*)&py3_PyStdPrinter_Type},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200612 {"PySlice_Type", (PYTHON_PROC*)&py3_PySlice_Type},
Bram Moolenaardb913952012-06-29 12:54:53 +0200613 {"PyFloat_Type", (PYTHON_PROC*)&py3_PyFloat_Type},
Bram Moolenaar66b79852012-09-21 14:00:35 +0200614 {"PyBool_Type", (PYTHON_PROC*)&py3_PyBool_Type},
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200615 {"PyNumber_Check", (PYTHON_PROC*)&py3_PyNumber_Check},
616 {"PyNumber_Long", (PYTHON_PROC*)&py3_PyNumber_Long},
Bram Moolenaar19e60942011-06-19 00:27:51 +0200617 {"PyErr_NewException", (PYTHON_PROC*)&py3_PyErr_NewException},
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200618# ifdef Py_DEBUG
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200619 {"_Py_NegativeRefcount", (PYTHON_PROC*)&py3__Py_NegativeRefcount},
620 {"_Py_RefTotal", (PYTHON_PROC*)&py3__Py_RefTotal},
Bram Moolenaar0014a532013-05-29 21:33:39 +0200621 {"PyModule_Create2TraceRefs", (PYTHON_PROC*)&py3_PyModule_Create2TraceRefs},
622# else
623 {"PyModule_Create2", (PYTHON_PROC*)&py3_PyModule_Create2},
624# endif
625# if defined(Py_DEBUG) && !defined(Py_DEBUG_NO_PYMALLOC)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200626 {"_PyObject_DebugFree", (PYTHON_PROC*)&py3__PyObject_DebugFree},
627 {"_PyObject_DebugMalloc", (PYTHON_PROC*)&py3__PyObject_DebugMalloc},
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200628# else
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200629 {"PyObject_Malloc", (PYTHON_PROC*)&py3_PyObject_Malloc},
630 {"PyObject_Free", (PYTHON_PROC*)&py3_PyObject_Free},
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200631# endif
Bram Moolenaar774267b2013-05-21 20:51:59 +0200632 {"_PyObject_GC_New", (PYTHON_PROC*)&py3__PyObject_GC_New},
633 {"PyObject_GC_Del", (PYTHON_PROC*)&py3_PyObject_GC_Del},
634 {"PyObject_GC_UnTrack", (PYTHON_PROC*)&py3_PyObject_GC_UnTrack},
Bram Moolenaardb913952012-06-29 12:54:53 +0200635 {"PyType_IsSubtype", (PYTHON_PROC*)&py3_PyType_IsSubtype},
636 {"PyCapsule_New", (PYTHON_PROC*)&py3_PyCapsule_New},
637 {"PyCapsule_GetPointer", (PYTHON_PROC*)&py3_PyCapsule_GetPointer},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200638 {"", NULL},
639};
640
Bram Moolenaar13a1f3f2019-10-23 21:37:25 +0200641# if PY_VERSION_HEX >= 0x030800f0
642 static inline void
643py3__Py_DECREF(const char *filename UNUSED, int lineno UNUSED, PyObject *op)
644{
Bram Moolenaar13a1f3f2019-10-23 21:37:25 +0200645 if (--op->ob_refcnt != 0)
646 {
647# ifdef Py_REF_DEBUG
648 if (op->ob_refcnt < 0)
649 {
650 _Py_NegativeRefcount(filename, lineno, op);
651 }
652# endif
653 }
654 else
655 {
656 _Py_Dealloc(op);
657 }
658}
659
660# undef Py_DECREF
661# define Py_DECREF(op) py3__Py_DECREF(__FILE__, __LINE__, _PyObject_CAST(op))
662
663 static inline void
664py3__Py_XDECREF(PyObject *op)
665{
666 if (op != NULL)
667 {
668 Py_DECREF(op);
669 }
670}
671
672# undef Py_XDECREF
673# define Py_XDECREF(op) py3__Py_XDECREF(_PyObject_CAST(op))
674# endif
675
Bram Moolenaaree1b9312020-07-16 22:15:53 +0200676# if PY_VERSION_HEX >= 0x030900b0
677 static inline int
678py3_PyType_HasFeature(PyTypeObject *type, unsigned long feature)
679{
680 return ((PyType_GetFlags(type) & feature) != 0);
681}
682# define PyType_HasFeature(t,f) py3_PyType_HasFeature(t,f)
683# endif
684
Zdenek Dohnal90478f32021-06-14 15:08:30 +0200685# if PY_VERSION_HEX >= 0x030a00b2
686 static inline int
687py3__PyObject_TypeCheck(PyObject *ob, PyTypeObject *type)
688{
689 return Py_IS_TYPE(ob, type) || PyType_IsSubtype(Py_TYPE(ob), type);
690}
691# define _PyObject_TypeCheck(o,t) py3__PyObject_TypeCheck(o,t)
692# endif
693
Bram Moolenaarb2f9e0e2020-12-25 13:52:37 +0100694# ifdef MSWIN
695/*
696 * Look up the library "libname" using the InstallPath registry key.
697 * Return NULL when failed. Return an allocated string when successful.
698 */
699 static char *
700py3_get_system_libname(const char *libname)
701{
702 const char *cp = libname;
703 char subkey[128];
704 HKEY hKey;
705 char installpath[MAXPATHL];
706 LONG len = sizeof(installpath);
707 LSTATUS rc;
708 size_t sysliblen;
709 char *syslibname;
710
711 while (*cp != '\0')
712 {
713 if (*cp == ':' || *cp == '\\' || *cp == '/')
714 {
715 // Bail out if "libname" contains path separator, assume it is
716 // an absolute path.
717 return NULL;
718 }
719 ++cp;
720 }
721 vim_snprintf(subkey, sizeof(subkey),
722# ifdef _WIN64
723 "Software\\Python\\PythonCore\\%d.%d\\InstallPath",
724# else
725 "Software\\Python\\PythonCore\\%d.%d-32\\InstallPath",
726# endif
727 PY_MAJOR_VERSION, PY_MINOR_VERSION);
728 if (RegOpenKeyExA(HKEY_LOCAL_MACHINE, subkey, 0, KEY_QUERY_VALUE, &hKey)
729 != ERROR_SUCCESS)
730 return NULL;
731 rc = RegQueryValueA(hKey, NULL, installpath, &len);
732 RegCloseKey(hKey);
733 if (ERROR_SUCCESS != rc)
734 return NULL;
735 cp = installpath + len;
736 // Just in case registry value contains null terminators.
737 while (cp > installpath && *(cp-1) == '\0')
738 --cp;
739 // Remove trailing path separators.
740 while (cp > installpath && (*(cp-1) == '\\' || *(cp-1) == '/'))
741 --cp;
742 // Ignore if InstallPath is effectively empty.
743 if (cp <= installpath)
744 return NULL;
745 sysliblen = (cp - installpath) + 1 + STRLEN(libname) + 1;
746 syslibname = alloc(sysliblen);
747 vim_snprintf(syslibname, sysliblen, "%.*s\\%s",
748 (int)(cp - installpath), installpath, libname);
749 return syslibname;
750}
751# endif
752
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200753/*
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200754 * Load library and get all pointers.
755 * Parameter 'libname' provides name of DLL.
756 * Return OK or FAIL.
757 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200758 static int
759py3_runtime_link_init(char *libname, int verbose)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200760{
761 int i;
Bram Moolenaar7b24ce02018-03-29 18:15:26 +0200762 PYTHON_PROC *ucs_from_string = (PYTHON_PROC *)&py3_PyUnicode_FromString;
763 PYTHON_PROC *ucs_decode = (PYTHON_PROC *)&py3_PyUnicode_Decode;
764 PYTHON_PROC *ucs_as_encoded_string =
765 (PYTHON_PROC *)&py3_PyUnicode_AsEncodedString;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200766
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100767# if !(defined(PY_NO_RTLD_GLOBAL) && defined(PY3_NO_RTLD_GLOBAL)) && defined(UNIX) && defined(FEAT_PYTHON)
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100768 // Can't have Python and Python3 loaded at the same time.
Dominique Pelleaf4a61a2021-12-27 17:21:41 +0000769 // It causes a crash, because RTLD_GLOBAL is needed for
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100770 // standard C extension libraries of one or both python versions.
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200771 if (python_loaded())
772 {
Bram Moolenaar9dc93ae2011-08-28 16:00:19 +0200773 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100774 emsg(_("E837: This Vim cannot execute :py3 after using :python"));
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200775 return FAIL;
776 }
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200777# endif
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200778
779 if (hinstPy3 != 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200780 return OK;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200781 hinstPy3 = load_dll(libname);
782
Bram Moolenaarb2f9e0e2020-12-25 13:52:37 +0100783# ifdef MSWIN
784 if (!hinstPy3)
785 {
786 // Attempt to use the path from InstallPath as stored in the registry.
787 char *syslibname = py3_get_system_libname(libname);
788
789 if (syslibname != NULL)
790 {
791 hinstPy3 = load_dll(syslibname);
792 vim_free(syslibname);
793 }
794 }
795# endif
796
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200797 if (!hinstPy3)
798 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200799 if (verbose)
Martin Tournoij1a3e5742021-07-24 13:57:29 +0200800 semsg(_(e_loadlib), libname, load_dll_error());
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200801 return FAIL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200802 }
803
804 for (i = 0; py3_funcname_table[i].ptr; ++i)
805 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200806 if ((*py3_funcname_table[i].ptr = symbol_from_dll(hinstPy3,
807 py3_funcname_table[i].name)) == NULL)
808 {
809 close_dll(hinstPy3);
810 hinstPy3 = 0;
811 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100812 semsg(_(e_loadfunc), py3_funcname_table[i].name);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200813 return FAIL;
814 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200815 }
816
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100817 // Load unicode functions separately as only the ucs2 or the ucs4 functions
818 // will be present in the library.
Bram Moolenaar9c9cbf12012-10-23 05:17:37 +0200819# if PY_VERSION_HEX >= 0x030300f0
Bram Moolenaar7b24ce02018-03-29 18:15:26 +0200820 *ucs_from_string = symbol_from_dll(hinstPy3, "PyUnicode_FromString");
821 *ucs_decode = symbol_from_dll(hinstPy3, "PyUnicode_Decode");
822 *ucs_as_encoded_string = symbol_from_dll(hinstPy3,
Bram Moolenaar7bc4f932012-10-14 03:22:56 +0200823 "PyUnicode_AsEncodedString");
Bram Moolenaar9c9cbf12012-10-23 05:17:37 +0200824# else
Bram Moolenaar7b24ce02018-03-29 18:15:26 +0200825 *ucs_from_string = symbol_from_dll(hinstPy3, "PyUnicodeUCS2_FromString");
826 *ucs_decode = symbol_from_dll(hinstPy3,
Bram Moolenaar19e60942011-06-19 00:27:51 +0200827 "PyUnicodeUCS2_Decode");
Bram Moolenaar7b24ce02018-03-29 18:15:26 +0200828 *ucs_as_encoded_string = symbol_from_dll(hinstPy3,
Bram Moolenaar19e60942011-06-19 00:27:51 +0200829 "PyUnicodeUCS2_AsEncodedString");
Bram Moolenaar7b24ce02018-03-29 18:15:26 +0200830 if (*ucs_from_string == NULL || *ucs_decode == NULL
831 || *ucs_as_encoded_string == NULL)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200832 {
Bram Moolenaar7b24ce02018-03-29 18:15:26 +0200833 *ucs_from_string = symbol_from_dll(hinstPy3,
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200834 "PyUnicodeUCS4_FromString");
Bram Moolenaar7b24ce02018-03-29 18:15:26 +0200835 *ucs_decode = symbol_from_dll(hinstPy3,
Bram Moolenaar19e60942011-06-19 00:27:51 +0200836 "PyUnicodeUCS4_Decode");
Bram Moolenaar7b24ce02018-03-29 18:15:26 +0200837 *ucs_as_encoded_string = symbol_from_dll(hinstPy3,
Bram Moolenaar19e60942011-06-19 00:27:51 +0200838 "PyUnicodeUCS4_AsEncodedString");
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200839 }
Bram Moolenaar9c9cbf12012-10-23 05:17:37 +0200840# endif
Bram Moolenaar7b24ce02018-03-29 18:15:26 +0200841 if (*ucs_from_string == NULL || *ucs_decode == NULL
842 || *ucs_as_encoded_string == NULL)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200843 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200844 close_dll(hinstPy3);
845 hinstPy3 = 0;
846 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100847 semsg(_(e_loadfunc), "PyUnicode_UCSX_*");
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200848 return FAIL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200849 }
850
851 return OK;
852}
853
854/*
855 * If python is enabled (there is installed python on Windows system) return
856 * TRUE, else FALSE.
857 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200858 int
859python3_enabled(int verbose)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200860{
Bram Moolenaar25e4fcd2016-01-09 14:57:47 +0100861 return py3_runtime_link_init((char *)p_py3dll, verbose) == OK;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200862}
863
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100864/*
865 * Load the standard Python exceptions - don't import the symbols from the
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200866 * DLL, as this can cause errors (importing data symbols is not reliable).
867 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200868 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100869get_py3_exceptions(void)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200870{
871 PyObject *exmod = PyImport_ImportModule("builtins");
872 PyObject *exdict = PyModule_GetDict(exmod);
873 p3imp_PyExc_AttributeError = PyDict_GetItemString(exdict, "AttributeError");
874 p3imp_PyExc_IndexError = PyDict_GetItemString(exdict, "IndexError");
Bram Moolenaaraf6abb92013-04-24 13:04:26 +0200875 p3imp_PyExc_KeyError = PyDict_GetItemString(exdict, "KeyError");
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200876 p3imp_PyExc_KeyboardInterrupt = PyDict_GetItemString(exdict, "KeyboardInterrupt");
877 p3imp_PyExc_TypeError = PyDict_GetItemString(exdict, "TypeError");
878 p3imp_PyExc_ValueError = PyDict_GetItemString(exdict, "ValueError");
Bram Moolenaar41009372013-07-01 22:03:04 +0200879 p3imp_PyExc_SystemExit = PyDict_GetItemString(exdict, "SystemExit");
Bram Moolenaar8661b172013-05-15 15:44:28 +0200880 p3imp_PyExc_RuntimeError = PyDict_GetItemString(exdict, "RuntimeError");
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200881 p3imp_PyExc_ImportError = PyDict_GetItemString(exdict, "ImportError");
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200882 p3imp_PyExc_OverflowError = PyDict_GetItemString(exdict, "OverflowError");
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200883 Py_XINCREF(p3imp_PyExc_AttributeError);
884 Py_XINCREF(p3imp_PyExc_IndexError);
Bram Moolenaaraf6abb92013-04-24 13:04:26 +0200885 Py_XINCREF(p3imp_PyExc_KeyError);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200886 Py_XINCREF(p3imp_PyExc_KeyboardInterrupt);
887 Py_XINCREF(p3imp_PyExc_TypeError);
888 Py_XINCREF(p3imp_PyExc_ValueError);
Bram Moolenaar41009372013-07-01 22:03:04 +0200889 Py_XINCREF(p3imp_PyExc_SystemExit);
Bram Moolenaar8661b172013-05-15 15:44:28 +0200890 Py_XINCREF(p3imp_PyExc_RuntimeError);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200891 Py_XINCREF(p3imp_PyExc_ImportError);
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200892 Py_XINCREF(p3imp_PyExc_OverflowError);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200893 Py_XDECREF(exmod);
894}
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100895#endif // DYNAMIC_PYTHON3
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200896
Bram Moolenaardb913952012-06-29 12:54:53 +0200897static int py3initialised = 0;
Bram Moolenaardb913952012-06-29 12:54:53 +0200898#define PYINITIALISED py3initialised
Bram Moolenaarc4f83382017-07-07 14:50:44 +0200899static int python_end_called = FALSE;
Bram Moolenaardb913952012-06-29 12:54:53 +0200900
Bram Moolenaar774267b2013-05-21 20:51:59 +0200901#define DESTRUCTOR_FINISH(self) Py_TYPE(self)->tp_free((PyObject*)self)
Bram Moolenaardb913952012-06-29 12:54:53 +0200902
Bram Moolenaar971db462013-05-12 18:44:48 +0200903#define WIN_PYTHON_REF(win) win->w_python3_ref
904#define BUF_PYTHON_REF(buf) buf->b_python3_ref
Bram Moolenaar5e538ec2013-05-15 15:12:29 +0200905#define TAB_PYTHON_REF(tab) tab->tp_python3_ref
Bram Moolenaar971db462013-05-12 18:44:48 +0200906
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200907 static void
908call_PyObject_Free(void *p)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200909{
Bram Moolenaar0014a532013-05-29 21:33:39 +0200910#if defined(Py_DEBUG) && !defined(Py_DEBUG_NO_PYMALLOC)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200911 _PyObject_DebugFree(p);
912#else
913 PyObject_Free(p);
914#endif
915}
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200916
917 static PyObject *
918call_PyType_GenericNew(PyTypeObject *type, PyObject *args, PyObject *kwds)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200919{
920 return PyType_GenericNew(type,args,kwds);
921}
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200922
923 static PyObject *
924call_PyType_GenericAlloc(PyTypeObject *type, Py_ssize_t nitems)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200925{
926 return PyType_GenericAlloc(type,nitems);
927}
928
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200929static PyObject *OutputGetattro(PyObject *, PyObject *);
930static int OutputSetattro(PyObject *, PyObject *, PyObject *);
931static PyObject *BufferGetattro(PyObject *, PyObject *);
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200932static int BufferSetattro(PyObject *, PyObject *, PyObject *);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +0200933static PyObject *TabPageGetattro(PyObject *, PyObject *);
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200934static PyObject *WindowGetattro(PyObject *, PyObject *);
935static int WindowSetattro(PyObject *, PyObject *, PyObject *);
936static PyObject *RangeGetattro(PyObject *, PyObject *);
937static PyObject *CurrentGetattro(PyObject *, PyObject *);
938static int CurrentSetattro(PyObject *, PyObject *, PyObject *);
939static PyObject *DictionaryGetattro(PyObject *, PyObject *);
940static int DictionarySetattro(PyObject *, PyObject *, PyObject *);
941static PyObject *ListGetattro(PyObject *, PyObject *);
942static int ListSetattro(PyObject *, PyObject *, PyObject *);
943static PyObject *FunctionGetattro(PyObject *, PyObject *);
944
945static struct PyModuleDef vimmodule;
946
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +0200947#define PY_CAN_RECURSE
948
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200949/*
950 * Include the code shared with if_python.c
951 */
952#include "if_py_both.h"
953
Bram Moolenaar828bff12019-03-19 22:11:41 +0100954// NOTE: Must always be used at the start of a block, since it declares "name".
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200955#define GET_ATTR_STRING(name, nameobj) \
956 char *name = ""; \
957 if (PyUnicode_Check(nameobj)) \
Bram Moolenaar828bff12019-03-19 22:11:41 +0100958 name = (char *)_PyUnicode_AsString(nameobj)
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200959
960#define PY3OBJ_DELETED(obj) (obj->ob_base.ob_refcnt<=0)
961
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100962///////////////////////////////////////////////////////
963// Internal function prototypes.
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200964
Bram Moolenaar7854e3a2012-11-28 15:33:14 +0100965static PyObject *Py3Init_vim(void);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200966
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100967///////////////////////////////////////////////////////
968// 1. Python interpreter main program.
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200969
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200970 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100971python3_end(void)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200972{
973 static int recurse = 0;
974
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100975 // If a crash occurs while doing this, don't try again.
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200976 if (recurse != 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200977 return;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200978
Bram Moolenaarc4f83382017-07-07 14:50:44 +0200979 python_end_called = TRUE;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200980 ++recurse;
981
982#ifdef DYNAMIC_PYTHON3
983 if (hinstPy3)
984#endif
985 if (Py_IsInitialized())
986 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100987 // acquire lock before finalizing
Bram Moolenaar71700b82013-05-15 17:49:05 +0200988 PyGILState_Ensure();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200989
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200990 Py_Finalize();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200991 }
992
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200993 --recurse;
994}
995
Bram Moolenaar094454f2015-10-07 10:39:55 +0200996#if (defined(DYNAMIC_PYTHON3) && defined(DYNAMIC_PYTHON) && defined(FEAT_PYTHON) && defined(UNIX)) || defined(PROTO)
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200997 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100998python3_loaded(void)
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200999{
1000 return (hinstPy3 != 0);
1001}
1002#endif
1003
Bram Moolenaar94073162018-01-31 21:49:05 +01001004static wchar_t *py_home_buf = NULL;
1005
Bram Moolenaar56b8dc32020-08-06 21:47:11 +02001006#if defined(MSWIN) && (PY_VERSION_HEX >= 0x030500f0)
Bram Moolenaarc6ed2542020-10-10 23:26:28 +02001007/*
1008 * Return TRUE if stdin is readable from Python 3.
1009 */
1010 static BOOL
1011is_stdin_readable(void)
1012{
1013 DWORD mode, eventnum;
1014 struct _stat st;
1015 int fd = fileno(stdin);
1016 HANDLE hstdin = (HANDLE)_get_osfhandle(fd);
1017
1018 // Check if stdin is connected to the console.
1019 if (GetConsoleMode(hstdin, &mode))
1020 // Check if it is opened as input.
1021 return GetNumberOfConsoleInputEvents(hstdin, &eventnum);
1022
1023 return _fstat(fd, &st) == 0;
1024}
1025
1026// Python 3.5 or later will abort inside Py_Initialize() when stdin has
1027// been closed (i.e. executed by "vim -"). Reconnect stdin to CONIN$.
Bram Moolenaar56b8dc32020-08-06 21:47:11 +02001028// Note that the python DLL is linked to its own stdio DLL which can be
1029// differ from Vim's stdio.
1030 static void
1031reset_stdin(void)
1032{
1033 FILE *(*py__acrt_iob_func)(unsigned) = NULL;
1034 FILE *(*pyfreopen)(const char *, const char *, FILE *) = NULL;
1035 HINSTANCE hinst;
1036
1037# ifdef DYNAMIC_PYTHON3
1038 hinst = hinstPy3;
1039# else
1040 hinst = GetModuleHandle(PYTHON3_DLL);
1041# endif
Bram Moolenaarc6ed2542020-10-10 23:26:28 +02001042 if (hinst == NULL || is_stdin_readable())
Bram Moolenaar56b8dc32020-08-06 21:47:11 +02001043 return;
1044
1045 // Get "freopen" and "stdin" which are used in the python DLL.
1046 // "stdin" is defined as "__acrt_iob_func(0)" in VC++ 2015 or later.
1047 py__acrt_iob_func = get_dll_import_func(hinst, "__acrt_iob_func");
1048 if (py__acrt_iob_func)
1049 {
1050 HINSTANCE hpystdiodll = find_imported_module_by_funcname(hinst,
Bram Moolenaar253b16a2020-10-06 19:59:06 +02001051 "__acrt_iob_func");
Bram Moolenaar56b8dc32020-08-06 21:47:11 +02001052 if (hpystdiodll)
Bram Moolenaar253b16a2020-10-06 19:59:06 +02001053 pyfreopen = (void *)GetProcAddress(hpystdiodll, "freopen");
Bram Moolenaar56b8dc32020-08-06 21:47:11 +02001054 }
1055
Bram Moolenaarc6ed2542020-10-10 23:26:28 +02001056 // Reconnect stdin to CONIN$.
Bram Moolenaar253b16a2020-10-06 19:59:06 +02001057 if (pyfreopen != NULL)
Bram Moolenaarc6ed2542020-10-10 23:26:28 +02001058 pyfreopen("CONIN$", "r", py__acrt_iob_func(0));
Bram Moolenaar56b8dc32020-08-06 21:47:11 +02001059 else
Bram Moolenaarc6ed2542020-10-10 23:26:28 +02001060 freopen("CONIN$", "r", stdin);
Bram Moolenaar56b8dc32020-08-06 21:47:11 +02001061}
1062#else
1063# define reset_stdin()
1064#endif
1065
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001066 static int
1067Python3_Init(void)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001068{
1069 if (!py3initialised)
1070 {
1071#ifdef DYNAMIC_PYTHON3
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001072 if (!python3_enabled(TRUE))
1073 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001074 emsg(_("E263: Sorry, this command is disabled, the Python library could not be loaded."));
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001075 goto fail;
1076 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001077#endif
1078
1079 init_structs();
1080
Bram Moolenaar94073162018-01-31 21:49:05 +01001081 if (*p_py3home != NUL)
1082 {
1083 size_t len = mbstowcs(NULL, (char *)p_py3home, 0) + 1;
Bram Moolenaar644d37b2010-11-16 19:26:02 +01001084
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001085 // The string must not change later, make a copy in static memory.
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001086 py_home_buf = ALLOC_MULT(wchar_t, len);
Bram Moolenaar94073162018-01-31 21:49:05 +01001087 if (py_home_buf != NULL && mbstowcs(
1088 py_home_buf, (char *)p_py3home, len) != (size_t)-1)
1089 Py_SetPythonHome(py_home_buf);
1090 }
Bram Moolenaar644d37b2010-11-16 19:26:02 +01001091#ifdef PYTHON3_HOME
Bram Moolenaar94073162018-01-31 21:49:05 +01001092 else if (mch_getenv((char_u *)"PYTHONHOME") == NULL)
Bram Moolenaar10005652015-12-31 21:03:23 +01001093 Py_SetPythonHome(PYTHON3_HOME);
Bram Moolenaar644d37b2010-11-16 19:26:02 +01001094#endif
1095
Bram Moolenaar7bc4f932012-10-14 03:22:56 +02001096 PyImport_AppendInittab("vim", Py3Init_vim);
1097
Bram Moolenaar56b8dc32020-08-06 21:47:11 +02001098 reset_stdin();
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001099 Py_Initialize();
Bram Moolenaard0573012017-10-28 21:11:06 +02001100
Bram Moolenaarefc0d942020-10-11 18:05:02 +02001101#if PY_VERSION_HEX < 0x03090000
1102 // Initialise threads. This is deprecated since Python 3.9.
Bram Moolenaar456f2bb2011-06-12 21:37:13 +02001103 PyEval_InitThreads();
Bram Moolenaarefc0d942020-10-11 18:05:02 +02001104#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001105#ifdef DYNAMIC_PYTHON3
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001106 get_py3_exceptions();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001107#endif
1108
Bram Moolenaar1dc28782013-05-21 19:11:01 +02001109 if (PythonIO_Init_io())
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001110 goto fail;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001111
Bram Moolenaardb913952012-06-29 12:54:53 +02001112 globals = PyModule_GetDict(PyImport_AddModule("__main__"));
1113
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001114 // Remove the element from sys.path that was added because of our
1115 // argv[0] value in Py3Init_vim(). Previously we used an empty
1116 // string, but depending on the OS we then get an empty entry or
1117 // the current directory in sys.path.
1118 // Only after vim has been imported, the element does exist in
1119 // sys.path.
Bram Moolenaar19e60942011-06-19 00:27:51 +02001120 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 +02001121
Bram Moolenaarefc0d942020-10-11 18:05:02 +02001122 // Without the call to PyEval_SaveThread, thread specific state (such
1123 // as the system trace hook), will be lost between invocations of
1124 // Python code.
1125 // GIL may have been created and acquired in PyEval_InitThreads() and
1126 // thread state is created in Py_Initialize(); there
1127 // _PyGILState_NoteThreadState() also sets gilcounter to 1 (python must
1128 // have threads enabled!), so the following does both: unlock GIL and
1129 // save thread state in TLS without deleting thread state
Bram Moolenaar76d711c2013-02-13 14:17:08 +01001130 PyEval_SaveThread();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001131
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001132 py3initialised = 1;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001133 }
1134
1135 return 0;
1136
1137fail:
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001138 // We call PythonIO_Flush() here to print any Python errors.
1139 // This is OK, as it is possible to call this function even
1140 // if PythonIO_Init_io() has not completed successfully (it will
1141 // not do anything in this case).
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001142 PythonIO_Flush();
1143 return -1;
1144}
1145
1146/*
1147 * External interface
1148 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001149 static void
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02001150DoPyCommand(const char *cmd, rangeinitializer init_range, runner run, void *arg)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001151{
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001152#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001153 char *saved_locale;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001154#endif
Bram Moolenaar19e60942011-06-19 00:27:51 +02001155 PyObject *cmdstr;
1156 PyObject *cmdbytes;
Bram Moolenaar71700b82013-05-15 17:49:05 +02001157 PyGILState_STATE pygilstate;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001158
Bram Moolenaarc4f83382017-07-07 14:50:44 +02001159 if (python_end_called)
1160 goto theend;
1161
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001162 if (Python3_Init())
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001163 goto theend;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001164
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02001165 init_range(arg);
1166
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001167 Python_Release_Vim(); // leave Vim
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001168
1169#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001170 // Python only works properly when the LC_NUMERIC locale is "C".
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001171 saved_locale = setlocale(LC_NUMERIC, NULL);
1172 if (saved_locale == NULL || STRCMP(saved_locale, "C") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001173 saved_locale = NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001174 else
1175 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001176 // Need to make a copy, value may change when setting new locale.
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001177 saved_locale = (char *)vim_strsave((char_u *)saved_locale);
1178 (void)setlocale(LC_NUMERIC, "C");
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001179 }
1180#endif
1181
1182 pygilstate = PyGILState_Ensure();
1183
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001184 // PyRun_SimpleString expects a UTF-8 string. Wrong encoding may cause
1185 // SyntaxError (unicode error).
Bram Moolenaar3d64a312011-07-15 15:54:44 +02001186 cmdstr = PyUnicode_Decode(cmd, strlen(cmd),
Bram Moolenaar2e2f52a2020-12-21 16:03:02 +01001187 (char *)ENC_OPT, ERRORS_DECODE_ARG);
1188 cmdbytes = PyUnicode_AsEncodedString(cmdstr, "utf-8", ERRORS_ENCODE_ARG);
Bram Moolenaar19e60942011-06-19 00:27:51 +02001189 Py_XDECREF(cmdstr);
Bram Moolenaardb913952012-06-29 12:54:53 +02001190
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02001191 run(PyBytes_AsString(cmdbytes), arg, &pygilstate);
Bram Moolenaar19e60942011-06-19 00:27:51 +02001192 Py_XDECREF(cmdbytes);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001193
1194 PyGILState_Release(pygilstate);
1195
1196#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
1197 if (saved_locale != NULL)
1198 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001199 (void)setlocale(LC_NUMERIC, saved_locale);
1200 vim_free(saved_locale);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001201 }
1202#endif
1203
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001204 Python_Lock_Vim(); // enter Vim
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001205 PythonIO_Flush();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001206
1207theend:
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001208 return; // keeps lint happy
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001209}
1210
1211/*
Bram Moolenaar368373e2010-07-19 20:46:22 +02001212 * ":py3"
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001213 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001214 void
1215ex_py3(exarg_T *eap)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001216{
1217 char_u *script;
1218
1219 script = script_get(eap, eap->arg);
1220 if (!eap->skip)
1221 {
Bram Moolenaar14816ad2019-02-18 22:04:56 +01001222 if (p_pyx == 0)
1223 p_pyx = 3;
1224
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02001225 DoPyCommand(script == NULL ? (char *) eap->arg : (char *) script,
1226 (rangeinitializer) init_range_cmd,
1227 (runner) run_cmd,
1228 (void *) eap);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001229 }
1230 vim_free(script);
1231}
1232
1233#define BUFFER_SIZE 2048
1234
1235/*
Bram Moolenaar6df6f472010-07-18 18:04:50 +02001236 * ":py3file"
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001237 */
1238 void
1239ex_py3file(exarg_T *eap)
1240{
1241 static char buffer[BUFFER_SIZE];
1242 const char *file;
1243 char *p;
1244 int i;
1245
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01001246 if (p_pyx == 0)
1247 p_pyx = 3;
1248
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001249 // Have to do it like this. PyRun_SimpleFile requires you to pass a
1250 // stdio file pointer, but Vim and the Python DLL are compiled with
1251 // different options under Windows, meaning that stdio pointers aren't
1252 // compatible between the two. Yuk.
1253 //
1254 // construct: exec(compile(open('a_filename', 'rb').read(), 'a_filename', 'exec'))
1255 //
1256 // Using bytes so that Python can detect the source encoding as it normally
1257 // does. The doc does not say "compile" accept bytes, though.
1258 //
1259 // We need to escape any backslashes or single quotes in the file name, so that
1260 // Python won't mangle the file name.
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001261
1262 strcpy(buffer, "exec(compile(open('");
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001263 p = buffer + 19; // size of "exec(compile(open('"
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001264
1265 for (i=0; i<2; ++i)
1266 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001267 file = (char *)eap->arg;
1268 while (*file && p < buffer + (BUFFER_SIZE - 3))
1269 {
1270 if (*file == '\\' || *file == '\'')
1271 *p++ = '\\';
1272 *p++ = *file++;
1273 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001274 // If we didn't finish the file name, we hit a buffer overflow
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001275 if (*file != '\0')
1276 return;
1277 if (i==0)
1278 {
Bram Moolenaar19e60942011-06-19 00:27:51 +02001279 strcpy(p,"','rb').read(),'");
1280 p += 16;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001281 }
1282 else
1283 {
1284 strcpy(p,"','exec'))");
1285 p += 10;
1286 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001287 }
1288
1289
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001290 // Execute the file
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02001291 DoPyCommand(buffer,
1292 (rangeinitializer) init_range_cmd,
1293 (runner) run_cmd,
1294 (void *) eap);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001295}
1296
Bram Moolenaar54e8f002013-05-15 19:44:39 +02001297 void
1298ex_py3do(exarg_T *eap)
Bram Moolenaar3dab2802013-05-15 18:28:13 +02001299{
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01001300 if (p_pyx == 0)
1301 p_pyx = 3;
1302
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02001303 DoPyCommand((char *)eap->arg,
1304 (rangeinitializer)init_range_cmd,
1305 (runner)run_do,
1306 (void *)eap);
Bram Moolenaar3dab2802013-05-15 18:28:13 +02001307}
1308
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001309///////////////////////////////////////////////////////
1310// 2. Python output stream: writes output via [e]msg().
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001311
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001312// Implementation functions
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001313
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001314 static PyObject *
1315OutputGetattro(PyObject *self, PyObject *nameobj)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001316{
Bram Moolenaar77045652012-09-21 13:46:06 +02001317 GET_ATTR_STRING(name, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001318
1319 if (strcmp(name, "softspace") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001320 return PyLong_FromLong(((OutputObject *)(self))->softspace);
Bram Moolenaar6d4431e2016-04-21 20:00:56 +02001321 else if (strcmp(name, "errors") == 0)
1322 return PyString_FromString("strict");
1323 else if (strcmp(name, "encoding") == 0)
1324 return PyString_FromString(ENC_OPT);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001325
1326 return PyObject_GenericGetAttr(self, nameobj);
1327}
1328
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001329 static int
1330OutputSetattro(PyObject *self, PyObject *nameobj, PyObject *val)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001331{
Bram Moolenaar77045652012-09-21 13:46:06 +02001332 GET_ATTR_STRING(name, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001333
Bram Moolenaard6e39182013-05-21 18:30:34 +02001334 return OutputSetattr((OutputObject *)(self), name, val);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001335}
1336
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001337///////////////////////////////////////////////////////
1338// 3. Implementation of the Vim module for Python
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001339
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001340// Window type - Implementation functions
1341// --------------------------------------
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001342
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001343#define WindowType_Check(obj) ((obj)->ob_base.ob_type == &WindowType)
1344
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001345// Buffer type - Implementation functions
1346// --------------------------------------
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001347
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001348#define BufferType_Check(obj) ((obj)->ob_base.ob_type == &BufferType)
1349
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001350static PyObject* BufferSubscript(PyObject *self, PyObject *idx);
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02001351static int BufferAsSubscript(PyObject *self, PyObject *idx, PyObject *val);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001352
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001353// Line range type - Implementation functions
1354// --------------------------------------
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001355
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001356#define RangeType_Check(obj) ((obj)->ob_base.ob_type == &RangeType)
1357
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001358static PyObject* RangeSubscript(PyObject *self, PyObject *idx);
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02001359static int RangeAsItem(PyObject *, Py_ssize_t, PyObject *);
1360static int RangeAsSubscript(PyObject *self, PyObject *idx, PyObject *val);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001361
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001362// Current objects type - Implementation functions
1363// -----------------------------------------------
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001364
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001365static PySequenceMethods BufferAsSeq = {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001366 (lenfunc) BufferLength, // sq_length, len(x)
1367 (binaryfunc) 0, // sq_concat, x+y
1368 (ssizeargfunc) 0, // sq_repeat, x*n
1369 (ssizeargfunc) BufferItem, // sq_item, x[i]
1370 0, // was_sq_slice, x[i:j]
1371 0, // sq_ass_item, x[i]=v
1372 0, // sq_ass_slice, x[i:j]=v
1373 0, // sq_contains
1374 0, // sq_inplace_concat
1375 0, // sq_inplace_repeat
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001376};
1377
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001378static PyMappingMethods BufferAsMapping = {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001379 /* mp_length */ (lenfunc)BufferLength,
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001380 /* mp_subscript */ (binaryfunc)BufferSubscript,
Bram Moolenaar19e60942011-06-19 00:27:51 +02001381 /* mp_ass_subscript */ (objobjargproc)BufferAsSubscript,
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001382};
1383
1384
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001385// Buffer object
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001386
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001387 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02001388BufferGetattro(PyObject *self, PyObject *nameobj)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001389{
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001390 PyObject *r;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001391
Bram Moolenaar77045652012-09-21 13:46:06 +02001392 GET_ATTR_STRING(name, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001393
Bram Moolenaar9e822c02013-05-29 22:15:30 +02001394 if ((r = BufferAttrValid((BufferObject *)(self), name)))
1395 return r;
1396
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001397 if (CheckBuffer((BufferObject *)(self)))
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001398 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001399
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001400 r = BufferAttr((BufferObject *)(self), name);
1401 if (r || PyErr_Occurred())
1402 return r;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001403 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001404 return PyObject_GenericGetAttr(self, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001405}
1406
Bram Moolenaare9ba5162013-05-29 22:02:22 +02001407 static int
1408BufferSetattro(PyObject *self, PyObject *nameobj, PyObject *val)
1409{
1410 GET_ATTR_STRING(name, nameobj);
1411
1412 return BufferSetattr((BufferObject *)(self), name, val);
1413}
1414
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001415//////////////////
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001416
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001417 static PyObject *
1418BufferSubscript(PyObject *self, PyObject* idx)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001419{
Bram Moolenaardb913952012-06-29 12:54:53 +02001420 if (PyLong_Check(idx))
1421 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001422 long _idx = PyLong_AsLong(idx);
Bram Moolenaard6e39182013-05-21 18:30:34 +02001423 return BufferItem((BufferObject *)(self), _idx);
Bram Moolenaardb913952012-06-29 12:54:53 +02001424 } else if (PySlice_Check(idx))
1425 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001426 Py_ssize_t start, stop, step, slicelen;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001427
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02001428 if (CheckBuffer((BufferObject *) self))
1429 return NULL;
1430
Bram Moolenaar922a4662014-03-30 16:11:43 +02001431 if (PySlice_GetIndicesEx((PySliceObject_T *)idx,
Bram Moolenaarbd80f352013-05-12 21:16:23 +02001432 (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count,
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001433 &start, &stop,
Bram Moolenaardb913952012-06-29 12:54:53 +02001434 &step, &slicelen) < 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001435 return NULL;
Bram Moolenaard6e39182013-05-21 18:30:34 +02001436 return BufferSlice((BufferObject *)(self), start, stop);
Bram Moolenaardb913952012-06-29 12:54:53 +02001437 }
1438 else
1439 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001440 RAISE_INVALID_INDEX_TYPE(idx);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001441 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001442 }
1443}
1444
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02001445 static int
Bram Moolenaar19e60942011-06-19 00:27:51 +02001446BufferAsSubscript(PyObject *self, PyObject* idx, PyObject* val)
1447{
Bram Moolenaardb913952012-06-29 12:54:53 +02001448 if (PyLong_Check(idx))
1449 {
Bram Moolenaar19e60942011-06-19 00:27:51 +02001450 long n = PyLong_AsLong(idx);
Bram Moolenaarab589462020-07-06 21:03:06 +02001451
1452 if (CheckBuffer((BufferObject *) self))
1453 return -1;
1454
Bram Moolenaar19e60942011-06-19 00:27:51 +02001455 return RBAsItem((BufferObject *)(self), n, val, 1,
1456 (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count,
1457 NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02001458 } else if (PySlice_Check(idx))
1459 {
Bram Moolenaar19e60942011-06-19 00:27:51 +02001460 Py_ssize_t start, stop, step, slicelen;
1461
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02001462 if (CheckBuffer((BufferObject *) self))
1463 return -1;
1464
Bram Moolenaar922a4662014-03-30 16:11:43 +02001465 if (PySlice_GetIndicesEx((PySliceObject_T *)idx,
Bram Moolenaarbd80f352013-05-12 21:16:23 +02001466 (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count,
Bram Moolenaar19e60942011-06-19 00:27:51 +02001467 &start, &stop,
Bram Moolenaardb913952012-06-29 12:54:53 +02001468 &step, &slicelen) < 0)
Bram Moolenaar19e60942011-06-19 00:27:51 +02001469 return -1;
Bram Moolenaar19e60942011-06-19 00:27:51 +02001470 return RBAsSlice((BufferObject *)(self), start, stop, val, 1,
1471 (PyInt)((BufferObject *)(self))->buf->b_ml.ml_line_count,
1472 NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02001473 }
1474 else
1475 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001476 RAISE_INVALID_INDEX_TYPE(idx);
Bram Moolenaar19e60942011-06-19 00:27:51 +02001477 return -1;
1478 }
1479}
1480
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001481static PySequenceMethods RangeAsSeq = {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001482 (lenfunc) RangeLength, // sq_length, len(x)
1483 (binaryfunc) 0, // RangeConcat, sq_concat, x+y
1484 (ssizeargfunc) 0, // RangeRepeat, sq_repeat, x*n
1485 (ssizeargfunc) RangeItem, // sq_item, x[i]
1486 0, // was_sq_slice, x[i:j]
1487 (ssizeobjargproc) RangeAsItem, // sq_as_item, x[i]=v
1488 0, // sq_ass_slice, x[i:j]=v
1489 0, // sq_contains
1490 0, // sq_inplace_concat
1491 0, // sq_inplace_repeat
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001492};
1493
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001494static PyMappingMethods RangeAsMapping = {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001495 /* mp_length */ (lenfunc)RangeLength,
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001496 /* mp_subscript */ (binaryfunc)RangeSubscript,
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001497 /* mp_ass_subscript */ (objobjargproc)RangeAsSubscript,
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001498};
1499
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001500// Line range object - Implementation
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001501
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001502 static PyObject *
1503RangeGetattro(PyObject *self, PyObject *nameobj)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001504{
Bram Moolenaar77045652012-09-21 13:46:06 +02001505 GET_ATTR_STRING(name, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001506
1507 if (strcmp(name, "start") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001508 return Py_BuildValue("n", ((RangeObject *)(self))->start - 1);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001509 else if (strcmp(name, "end") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001510 return Py_BuildValue("n", ((RangeObject *)(self))->end - 1);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001511 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001512 return PyObject_GenericGetAttr(self, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001513}
1514
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001515////////////////
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001516
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02001517 static int
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001518RangeAsItem(PyObject *self, Py_ssize_t n, PyObject *val)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001519{
1520 return RBAsItem(((RangeObject *)(self))->buf, n, val,
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001521 ((RangeObject *)(self))->start,
1522 ((RangeObject *)(self))->end,
1523 &((RangeObject *)(self))->end);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001524}
1525
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001526 static Py_ssize_t
1527RangeAsSlice(PyObject *self, Py_ssize_t lo, Py_ssize_t hi, PyObject *val)
1528{
1529 return RBAsSlice(((RangeObject *)(self))->buf, lo, hi, val,
1530 ((RangeObject *)(self))->start,
1531 ((RangeObject *)(self))->end,
1532 &((RangeObject *)(self))->end);
1533}
1534
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001535 static PyObject *
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001536RangeSubscript(PyObject *self, PyObject* idx)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001537{
Bram Moolenaardb913952012-06-29 12:54:53 +02001538 if (PyLong_Check(idx))
1539 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001540 long _idx = PyLong_AsLong(idx);
Bram Moolenaard6e39182013-05-21 18:30:34 +02001541 return RangeItem((RangeObject *)(self), _idx);
Bram Moolenaardb913952012-06-29 12:54:53 +02001542 } else if (PySlice_Check(idx))
1543 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001544 Py_ssize_t start, stop, step, slicelen;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001545
Bram Moolenaar922a4662014-03-30 16:11:43 +02001546 if (PySlice_GetIndicesEx((PySliceObject_T *)idx,
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001547 ((RangeObject *)(self))->end-((RangeObject *)(self))->start+1,
1548 &start, &stop,
Bram Moolenaardb913952012-06-29 12:54:53 +02001549 &step, &slicelen) < 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001550 return NULL;
Bram Moolenaard6e39182013-05-21 18:30:34 +02001551 return RangeSlice((RangeObject *)(self), start, stop);
Bram Moolenaardb913952012-06-29 12:54:53 +02001552 }
1553 else
1554 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001555 RAISE_INVALID_INDEX_TYPE(idx);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001556 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001557 }
1558}
1559
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02001560 static int
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001561RangeAsSubscript(PyObject *self, PyObject *idx, PyObject *val)
1562{
Bram Moolenaardb913952012-06-29 12:54:53 +02001563 if (PyLong_Check(idx))
1564 {
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001565 long n = PyLong_AsLong(idx);
1566 return RangeAsItem(self, n, val);
Bram Moolenaarabab0b02019-03-30 18:47:01 +01001567 }
1568 else if (PySlice_Check(idx))
Bram Moolenaardb913952012-06-29 12:54:53 +02001569 {
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001570 Py_ssize_t start, stop, step, slicelen;
1571
Bram Moolenaar922a4662014-03-30 16:11:43 +02001572 if (PySlice_GetIndicesEx((PySliceObject_T *)idx,
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001573 ((RangeObject *)(self))->end-((RangeObject *)(self))->start+1,
1574 &start, &stop,
Bram Moolenaardb913952012-06-29 12:54:53 +02001575 &step, &slicelen) < 0)
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001576 return -1;
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001577 return RangeAsSlice(self, start, stop, val);
Bram Moolenaardb913952012-06-29 12:54:53 +02001578 }
1579 else
1580 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001581 RAISE_INVALID_INDEX_TYPE(idx);
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001582 return -1;
1583 }
1584}
1585
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001586// TabPage object - Implementation
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001587
1588 static PyObject *
1589TabPageGetattro(PyObject *self, PyObject *nameobj)
1590{
1591 PyObject *r;
1592
1593 GET_ATTR_STRING(name, nameobj);
1594
Bram Moolenaar9e822c02013-05-29 22:15:30 +02001595 if ((r = TabPageAttrValid((TabPageObject *)(self), name)))
1596 return r;
1597
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001598 if (CheckTabPage((TabPageObject *)(self)))
1599 return NULL;
1600
1601 r = TabPageAttr((TabPageObject *)(self), name);
1602 if (r || PyErr_Occurred())
1603 return r;
1604 else
1605 return PyObject_GenericGetAttr(self, nameobj);
1606}
1607
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001608// Window object - Implementation
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001609
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001610 static PyObject *
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001611WindowGetattro(PyObject *self, PyObject *nameobj)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001612{
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001613 PyObject *r;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001614
Bram Moolenaar77045652012-09-21 13:46:06 +02001615 GET_ATTR_STRING(name, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001616
Bram Moolenaar9e822c02013-05-29 22:15:30 +02001617 if ((r = WindowAttrValid((WindowObject *)(self), name)))
1618 return r;
1619
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001620 if (CheckWindow((WindowObject *)(self)))
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001621 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001622
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001623 r = WindowAttr((WindowObject *)(self), name);
1624 if (r || PyErr_Occurred())
1625 return r;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001626 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001627 return PyObject_GenericGetAttr(self, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001628}
1629
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001630 static int
1631WindowSetattro(PyObject *self, PyObject *nameobj, PyObject *val)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001632{
Bram Moolenaar77045652012-09-21 13:46:06 +02001633 GET_ATTR_STRING(name, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001634
Bram Moolenaard6e39182013-05-21 18:30:34 +02001635 return WindowSetattr((WindowObject *)(self), name, val);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001636}
1637
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001638// Tab page list object - Definitions
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001639
1640static PySequenceMethods TabListAsSeq = {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001641 (lenfunc) TabListLength, // sq_length, len(x)
1642 (binaryfunc) 0, // sq_concat, x+y
1643 (ssizeargfunc) 0, // sq_repeat, x*n
1644 (ssizeargfunc) TabListItem, // sq_item, x[i]
1645 0, // sq_slice, x[i:j]
1646 (ssizeobjargproc)0, // sq_as_item, x[i]=v
1647 0, // sq_ass_slice, x[i:j]=v
1648 0, // sq_contains
1649 0, // sq_inplace_concat
1650 0, // sq_inplace_repeat
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001651};
1652
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001653// Window list object - Definitions
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001654
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001655static PySequenceMethods WinListAsSeq = {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001656 (lenfunc) WinListLength, // sq_length, len(x)
1657 (binaryfunc) 0, // sq_concat, x+y
1658 (ssizeargfunc) 0, // sq_repeat, x*n
1659 (ssizeargfunc) WinListItem, // sq_item, x[i]
1660 0, // sq_slice, x[i:j]
1661 (ssizeobjargproc)0, // sq_as_item, x[i]=v
1662 0, // sq_ass_slice, x[i:j]=v
1663 0, // sq_contains
1664 0, // sq_inplace_concat
1665 0, // sq_inplace_repeat
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001666};
1667
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001668/*
1669 * Current items object - Implementation
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001670 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001671 static PyObject *
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001672CurrentGetattro(PyObject *self, PyObject *nameobj)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001673{
Bram Moolenaardd8aca62013-05-29 22:36:10 +02001674 PyObject *r;
Bram Moolenaar77045652012-09-21 13:46:06 +02001675 GET_ATTR_STRING(name, nameobj);
Bram Moolenaardd8aca62013-05-29 22:36:10 +02001676 if (!(r = CurrentGetattr(self, name)))
1677 return PyObject_GenericGetAttr(self, nameobj);
1678 return r;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001679}
1680
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001681 static int
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001682CurrentSetattro(PyObject *self, PyObject *nameobj, PyObject *value)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001683{
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001684 GET_ATTR_STRING(name, nameobj);
1685 return CurrentSetattr(self, name, value);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001686}
1687
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001688// Dictionary object - Definitions
Bram Moolenaardb913952012-06-29 12:54:53 +02001689
Bram Moolenaar66b79852012-09-21 14:00:35 +02001690 static PyObject *
1691DictionaryGetattro(PyObject *self, PyObject *nameobj)
1692{
1693 DictionaryObject *this = ((DictionaryObject *) (self));
1694
1695 GET_ATTR_STRING(name, nameobj);
1696
1697 if (strcmp(name, "locked") == 0)
1698 return PyLong_FromLong(this->dict->dv_lock);
1699 else if (strcmp(name, "scope") == 0)
1700 return PyLong_FromLong(this->dict->dv_scope);
1701
1702 return PyObject_GenericGetAttr(self, nameobj);
1703}
1704
1705 static int
1706DictionarySetattro(PyObject *self, PyObject *nameobj, PyObject *val)
1707{
1708 GET_ATTR_STRING(name, nameobj);
Bram Moolenaard6e39182013-05-21 18:30:34 +02001709 return DictionarySetattr((DictionaryObject *)(self), name, val);
Bram Moolenaardb913952012-06-29 12:54:53 +02001710}
1711
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001712// List object - Definitions
Bram Moolenaardb913952012-06-29 12:54:53 +02001713
Bram Moolenaar66b79852012-09-21 14:00:35 +02001714 static PyObject *
1715ListGetattro(PyObject *self, PyObject *nameobj)
1716{
1717 GET_ATTR_STRING(name, nameobj);
1718
1719 if (strcmp(name, "locked") == 0)
1720 return PyLong_FromLong(((ListObject *) (self))->list->lv_lock);
1721
1722 return PyObject_GenericGetAttr(self, nameobj);
1723}
1724
1725 static int
1726ListSetattro(PyObject *self, PyObject *nameobj, PyObject *val)
1727{
1728 GET_ATTR_STRING(name, nameobj);
Bram Moolenaard6e39182013-05-21 18:30:34 +02001729 return ListSetattr((ListObject *)(self), name, val);
Bram Moolenaardb913952012-06-29 12:54:53 +02001730}
1731
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001732// Function object - Definitions
Bram Moolenaardb913952012-06-29 12:54:53 +02001733
Bram Moolenaardb913952012-06-29 12:54:53 +02001734 static PyObject *
1735FunctionGetattro(PyObject *self, PyObject *nameobj)
1736{
Bram Moolenaar8110a092016-04-14 15:56:09 +02001737 PyObject *r;
Bram Moolenaardb913952012-06-29 12:54:53 +02001738 FunctionObject *this = (FunctionObject *)(self);
Bram Moolenaar77045652012-09-21 13:46:06 +02001739
1740 GET_ATTR_STRING(name, nameobj);
Bram Moolenaardb913952012-06-29 12:54:53 +02001741
Bram Moolenaar8110a092016-04-14 15:56:09 +02001742 r = FunctionAttr(this, name);
1743 if (r || PyErr_Occurred())
1744 return r;
1745 else
1746 return PyObject_GenericGetAttr(self, nameobj);
Bram Moolenaardb913952012-06-29 12:54:53 +02001747}
1748
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001749// External interface
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001750
1751 void
1752python3_buffer_free(buf_T *buf)
1753{
Bram Moolenaar971db462013-05-12 18:44:48 +02001754 if (BUF_PYTHON_REF(buf) != NULL)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001755 {
Bram Moolenaar971db462013-05-12 18:44:48 +02001756 BufferObject *bp = BUF_PYTHON_REF(buf);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001757 bp->buf = INVALID_BUFFER_VALUE;
Bram Moolenaar971db462013-05-12 18:44:48 +02001758 BUF_PYTHON_REF(buf) = NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001759 }
1760}
1761
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001762 void
1763python3_window_free(win_T *win)
1764{
Bram Moolenaar971db462013-05-12 18:44:48 +02001765 if (WIN_PYTHON_REF(win) != NULL)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001766 {
Bram Moolenaar971db462013-05-12 18:44:48 +02001767 WindowObject *wp = WIN_PYTHON_REF(win);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001768 wp->win = INVALID_WINDOW_VALUE;
Bram Moolenaar971db462013-05-12 18:44:48 +02001769 WIN_PYTHON_REF(win) = NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001770 }
1771}
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001772
1773 void
1774python3_tabpage_free(tabpage_T *tab)
1775{
1776 if (TAB_PYTHON_REF(tab) != NULL)
1777 {
1778 TabPageObject *tp = TAB_PYTHON_REF(tab);
1779 tp->tab = INVALID_TABPAGE_VALUE;
1780 TAB_PYTHON_REF(tab) = NULL;
1781 }
1782}
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001783
Bram Moolenaar7854e3a2012-11-28 15:33:14 +01001784 static PyObject *
1785Py3Init_vim(void)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001786{
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001787 // The special value is removed from sys.path in Python3_Init().
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001788 static wchar_t *(argv[2]) = {L"/must>not&exist/foo", NULL};
1789
Bram Moolenaar1dc28782013-05-21 19:11:01 +02001790 if (init_types())
1791 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001792
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001793 // Set sys.argv[] to avoid a crash in warn().
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001794 PySys_SetArgv(1, argv);
1795
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001796 if ((vim_module = PyModule_Create(&vimmodule)) == NULL)
Bram Moolenaar19e60942011-06-19 00:27:51 +02001797 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001798
Bram Moolenaardee2e312013-06-23 16:35:47 +02001799 if (populate_module(vim_module))
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001800 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001801
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001802 if (init_sys_path())
1803 return NULL;
1804
1805 return vim_module;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001806}
1807
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001808//////////////////////////////////////////////////////////////////////////
1809// 4. Utility functions for handling the interface between Vim and Python.
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001810
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001811/*
1812 * Convert a Vim line into a Python string.
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001813 * All internal newlines are replaced by null characters.
1814 *
1815 * On errors, the Python exception data is set, and NULL is returned.
1816 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001817 static PyObject *
1818LineToString(const char *str)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001819{
1820 PyObject *result;
1821 Py_ssize_t len = strlen(str);
Bram Moolenaard672dde2020-02-26 13:43:51 +01001822 char *tmp, *p;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001823
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001824 tmp = alloc(len + 1);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001825 p = tmp;
1826 if (p == NULL)
1827 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001828 PyErr_NoMemory();
1829 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001830 }
1831
1832 while (*str)
1833 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001834 if (*str == '\n')
1835 *p = '\0';
1836 else
1837 *p = *str;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001838
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001839 ++p;
1840 ++str;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001841 }
1842 *p = '\0';
1843
Bram Moolenaar2e2f52a2020-12-21 16:03:02 +01001844 result = PyUnicode_Decode(tmp, len, (char *)ENC_OPT, ERRORS_DECODE_ARG);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001845
1846 vim_free(tmp);
1847 return result;
1848}
1849
Bram Moolenaardb913952012-06-29 12:54:53 +02001850 void
Bram Moolenaar8e9a24a2019-03-19 22:22:55 +01001851do_py3eval(char_u *str, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +02001852{
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02001853 DoPyCommand((char *) str,
1854 (rangeinitializer) init_range_eval,
1855 (runner) run_eval,
1856 (void *) rettv);
Bram Moolenaar8e9a24a2019-03-19 22:22:55 +01001857 if (rettv->v_type == VAR_UNKNOWN)
Bram Moolenaardb913952012-06-29 12:54:53 +02001858 {
Bram Moolenaar8e9a24a2019-03-19 22:22:55 +01001859 rettv->v_type = VAR_NUMBER;
1860 rettv->vval.v_number = 0;
Bram Moolenaardb913952012-06-29 12:54:53 +02001861 }
1862}
1863
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01001864 int
Bram Moolenaar8e9a24a2019-03-19 22:22:55 +01001865set_ref_in_python3(int copyID)
Bram Moolenaardb913952012-06-29 12:54:53 +02001866{
Bram Moolenaarb641df42015-02-03 13:16:04 +01001867 return set_ref_in_py(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02001868}