blob: e4d4a90ed0ee29f79bceb8d581f9174f6596c316 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9/*
10 * Python extensions by Paul Moore.
11 * Changes for Unix by David Leonard.
12 *
13 * This consists of four parts:
14 * 1. Python interpreter main program
15 * 2. Python output stream: writes output via [e]msg().
16 * 3. Implementation of the Vim module for Python
17 * 4. Utility functions for handling the interface between Vim and Python.
18 */
19
20/*
21 * Roland Puntaier 2009/sept/16:
22 * Adaptations to support both python3.x and python2.x
23 */
24
Bram Moolenaar2ab2e862019-12-04 21:24:53 +010025// uncomment this if used with the debug version of python
26// #define Py_DEBUG
27// Note: most of time you can add -DPy_DEBUG to CFLAGS in place of uncommenting
28// uncomment this if used with the debug version of python, but without its
29// allocator
30// #define Py_DEBUG_NO_PYMALLOC
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020031
32#include "vim.h"
33
34#include <limits.h>
35
Bram Moolenaar4f974752019-02-17 17:44:42 +010036#if defined(MSWIN) && defined(HAVE_FCNTL_H)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020037# undef HAVE_FCNTL_H
38#endif
39
40#ifdef _DEBUG
41# undef _DEBUG
42#endif
43
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020044#ifdef F_BLANK
45# undef F_BLANK
46#endif
47
ichizok6c3d3e62022-11-04 22:38:11 +000048#ifdef HAVE_DUP
49# undef HAVE_DUP
50#endif
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010051#ifdef HAVE_STRFTIME
52# undef HAVE_STRFTIME
53#endif
54#ifdef HAVE_STRING_H
55# undef HAVE_STRING_H
56#endif
57#ifdef HAVE_PUTENV
58# undef HAVE_PUTENV
59#endif
Bram Moolenaar6df6f472010-07-18 18:04:50 +020060#ifdef HAVE_STDARG_H
Bram Moolenaar2ab2e862019-12-04 21:24:53 +010061# undef HAVE_STDARG_H // Python's config.h defines it as well.
Bram Moolenaar6df6f472010-07-18 18:04:50 +020062#endif
Bram Moolenaar2ab2e862019-12-04 21:24:53 +010063#ifdef _POSIX_C_SOURCE // defined in feature.h
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020064# undef _POSIX_C_SOURCE
65#endif
Bram Moolenaar6df6f472010-07-18 18:04:50 +020066#ifdef _XOPEN_SOURCE
Bram Moolenaar2ab2e862019-12-04 21:24:53 +010067# undef _XOPEN_SOURCE // pyconfig.h defines it as well.
Bram Moolenaar6df6f472010-07-18 18:04:50 +020068#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020069
Bram Moolenaar0bdda372013-06-10 18:36:24 +020070#define PY_SSIZE_T_CLEAN
Zdenek Dohnale5e47092023-08-13 19:37:09 +020071#define PyLong_Type (*py3_PyLong_Type)
72#define PyBool_Type (*py3_PyBool_Type)
Bram Moolenaar0bdda372013-06-10 18:36:24 +020073
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020074#include <Python.h>
Bram Moolenaar0bdda372013-06-10 18:36:24 +020075
Bram Moolenaar2ab2e862019-12-04 21:24:53 +010076#undef main // Defined in python.h - aargh
77#undef HAVE_FCNTL_H // Clash with os_win32.h
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020078
Bram Moolenaar2ab2e862019-12-04 21:24:53 +010079// The "surrogateescape" error handler is new in Python 3.1
Bram Moolenaar3d64a312011-07-15 15:54:44 +020080#if PY_VERSION_HEX >= 0x030100f0
81# define CODEC_ERROR_HANDLER "surrogateescape"
82#else
83# define CODEC_ERROR_HANDLER NULL
84#endif
85
Philip H5a5f17f2022-11-05 14:05:31 +000086// Suppress Python 3.11 depreciations to see useful warnings
Philip H422b9dc2023-08-11 22:38:48 +020087#ifdef __GNUC__
88# pragma GCC diagnostic push
89# pragma GCC diagnostic ignored "-Wdeprecated-declarations"
Philip H5a5f17f2022-11-05 14:05:31 +000090#endif
91
Bram Moolenaar2ab2e862019-12-04 21:24:53 +010092// Python 3 does not support CObjects, always use Capsules
Bram Moolenaar2afa3232012-06-29 16:28:28 +020093#define PY_USE_CAPSULE
94
Bram Moolenaar2e2f52a2020-12-21 16:03:02 +010095#define ERRORS_DECODE_ARG CODEC_ERROR_HANDLER
96#define ERRORS_ENCODE_ARG ERRORS_DECODE_ARG
97
Bram Moolenaar170bf1a2010-07-24 23:51:45 +020098#define PyInt Py_ssize_t
Bram Moolenaar32ac8cd2013-07-03 18:49:17 +020099#ifndef PyString_Check
100# define PyString_Check(obj) PyUnicode_Check(obj)
101#endif
Bram Moolenaare0324612013-07-09 17:30:55 +0200102#define PyString_FromString(repr) \
Bram Moolenaar2e2f52a2020-12-21 16:03:02 +0100103 PyUnicode_Decode(repr, STRLEN(repr), ENC_OPT, ERRORS_DECODE_ARG)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +0200104#define PyString_FromFormat PyUnicode_FromFormat
Bram Moolenaar32ac8cd2013-07-03 18:49:17 +0200105#ifndef PyInt_Check
106# define PyInt_Check(obj) PyLong_Check(obj)
107#endif
Bram Moolenaar77045652012-09-21 13:46:06 +0200108#define PyInt_FromLong(i) PyLong_FromLong(i)
109#define PyInt_AsLong(obj) PyLong_AsLong(obj)
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200110#define Py_ssize_t_fmt "n"
Bram Moolenaara9922d62013-05-30 13:01:18 +0200111#define Py_bytes_fmt "y"
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200112
Bram Moolenaar063a46b2014-01-14 16:36:51 +0100113#define PyIntArgFunc ssizeargfunc
114#define PyIntObjArgProc ssizeobjargproc
115
Bram Moolenaar922a4662014-03-30 16:11:43 +0200116/*
117 * PySlice_GetIndicesEx(): first argument type changed from PySliceObject
118 * to PyObject in Python 3.2 or later.
119 */
120#if PY_VERSION_HEX >= 0x030200f0
121typedef PyObject PySliceObject_T;
122#else
123typedef PySliceObject PySliceObject_T;
124#endif
125
Bram Moolenaar63ff72a2022-02-07 13:54:01 +0000126#ifndef MSWIN
127# define HINSTANCE void *
128#endif
129#if defined(DYNAMIC_PYTHON3) || defined(MSWIN)
130static HINSTANCE hinstPy3 = 0; // Instance of python.dll
131#endif
132
Bram Moolenaar0c1f3f42011-02-25 15:18:50 +0100133#if defined(DYNAMIC_PYTHON3) || defined(PROTO)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200134
Bram Moolenaar4f974752019-02-17 17:44:42 +0100135# ifndef MSWIN
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200136# include <dlfcn.h>
137# define FARPROC void*
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100138# if defined(PY_NO_RTLD_GLOBAL) && defined(PY3_NO_RTLD_GLOBAL)
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200139# define load_dll(n) dlopen((n), RTLD_LAZY)
140# else
141# define load_dll(n) dlopen((n), RTLD_LAZY|RTLD_GLOBAL)
142# endif
143# define close_dll dlclose
144# define symbol_from_dll dlsym
Martin Tournoij1a3e5742021-07-24 13:57:29 +0200145# define load_dll_error dlerror
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200146# else
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200147# define load_dll vimLoadLib
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200148# define close_dll FreeLibrary
149# define symbol_from_dll GetProcAddress
Martin Tournoij1a3e5742021-07-24 13:57:29 +0200150# define load_dll_error GetWin32Error
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200151# endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200152/*
153 * Wrapper defines
154 */
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200155# undef PyArg_Parse
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200156# define PyArg_Parse py3_PyArg_Parse
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200157# undef PyArg_ParseTuple
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200158# define PyArg_ParseTuple py3_PyArg_ParseTuple
Bram Moolenaar19e60942011-06-19 00:27:51 +0200159# define PyMem_Free py3_PyMem_Free
Bram Moolenaardb913952012-06-29 12:54:53 +0200160# define PyMem_Malloc py3_PyMem_Malloc
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200161# define PyDict_SetItemString py3_PyDict_SetItemString
162# define PyErr_BadArgument py3_PyErr_BadArgument
163# define PyErr_Clear py3_PyErr_Clear
Bram Moolenaarc476e522013-06-23 13:46:40 +0200164# define PyErr_Format py3_PyErr_Format
Bram Moolenaar4d369872013-02-20 16:09:43 +0100165# define PyErr_PrintEx py3_PyErr_PrintEx
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200166# define PyErr_NoMemory py3_PyErr_NoMemory
167# define PyErr_Occurred py3_PyErr_Occurred
168# define PyErr_SetNone py3_PyErr_SetNone
169# define PyErr_SetString py3_PyErr_SetString
Bram Moolenaar4d188da2013-05-15 15:35:09 +0200170# define PyErr_SetObject py3_PyErr_SetObject
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200171# define PyErr_ExceptionMatches py3_PyErr_ExceptionMatches
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200172# define PyEval_InitThreads py3_PyEval_InitThreads
173# define PyEval_RestoreThread py3_PyEval_RestoreThread
174# define PyEval_SaveThread py3_PyEval_SaveThread
175# define PyGILState_Ensure py3_PyGILState_Ensure
176# define PyGILState_Release py3_PyGILState_Release
177# define PyLong_AsLong py3_PyLong_AsLong
178# define PyLong_FromLong py3_PyLong_FromLong
179# define PyList_GetItem py3_PyList_GetItem
180# define PyList_Append py3_PyList_Append
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200181# define PyList_Insert py3_PyList_Insert
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200182# define PyList_New py3_PyList_New
183# define PyList_SetItem py3_PyList_SetItem
184# define PyList_Size py3_PyList_Size
Bram Moolenaardb913952012-06-29 12:54:53 +0200185# define PySequence_Check py3_PySequence_Check
186# define PySequence_Size py3_PySequence_Size
187# define PySequence_GetItem py3_PySequence_GetItem
Bram Moolenaara9922d62013-05-30 13:01:18 +0200188# define PySequence_Fast py3_PySequence_Fast
Bram Moolenaardb913952012-06-29 12:54:53 +0200189# define PyTuple_Size py3_PyTuple_Size
190# define PyTuple_GetItem py3_PyTuple_GetItem
Bram Moolenaar3b48b112018-07-04 22:03:25 +0200191# if PY_VERSION_HEX >= 0x030601f0
192# define PySlice_AdjustIndices py3_PySlice_AdjustIndices
193# define PySlice_Unpack py3_PySlice_Unpack
194# endif
195# undef PySlice_GetIndicesEx
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200196# define PySlice_GetIndicesEx py3_PySlice_GetIndicesEx
197# define PyImport_ImportModule py3_PyImport_ImportModule
198# define PyObject_Init py3__PyObject_Init
199# define PyDict_New py3_PyDict_New
200# define PyDict_GetItemString py3_PyDict_GetItemString
Bram Moolenaardb913952012-06-29 12:54:53 +0200201# define PyDict_Next py3_PyDict_Next
202# define PyMapping_Check py3_PyMapping_Check
Bram Moolenaar32ac8cd2013-07-03 18:49:17 +0200203# ifndef PyMapping_Keys
204# define PyMapping_Keys py3_PyMapping_Keys
205# endif
Zdenek Dohnal90478f32021-06-14 15:08:30 +0200206# if PY_VERSION_HEX >= 0x030a00b2
207# define PyIter_Check py3_PyIter_Check
208# endif
Bram Moolenaardb913952012-06-29 12:54:53 +0200209# define PyIter_Next py3_PyIter_Next
210# define PyObject_GetIter py3_PyObject_GetIter
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200211# define PyObject_Repr py3_PyObject_Repr
Bram Moolenaarbcb40972013-05-30 13:22:13 +0200212# define PyObject_GetItem py3_PyObject_GetItem
Bram Moolenaar03db85b2013-05-15 14:51:35 +0200213# define PyObject_IsTrue py3_PyObject_IsTrue
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200214# define PyModule_GetDict py3_PyModule_GetDict
215#undef PyRun_SimpleString
216# define PyRun_SimpleString py3_PyRun_SimpleString
Bram Moolenaardb913952012-06-29 12:54:53 +0200217#undef PyRun_String
218# define PyRun_String py3_PyRun_String
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200219# define PyObject_GetAttrString py3_PyObject_GetAttrString
Bram Moolenaara9922d62013-05-30 13:01:18 +0200220# define PyObject_HasAttrString py3_PyObject_HasAttrString
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200221# define PyObject_SetAttrString py3_PyObject_SetAttrString
222# define PyObject_CallFunctionObjArgs py3_PyObject_CallFunctionObjArgs
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200223# define _PyObject_CallFunction_SizeT py3__PyObject_CallFunction_SizeT
Bram Moolenaarf4258302013-06-02 18:20:17 +0200224# define PyObject_Call py3_PyObject_Call
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200225# define PyEval_GetLocals py3_PyEval_GetLocals
226# define PyEval_GetGlobals py3_PyEval_GetGlobals
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200227# define PySys_SetObject py3_PySys_SetObject
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200228# define PySys_GetObject py3_PySys_GetObject
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200229# define PySys_SetArgv py3_PySys_SetArgv
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200230# define PyType_Ready py3_PyType_Ready
Bram Moolenaaree1b9312020-07-16 22:15:53 +0200231# if PY_VERSION_HEX >= 0x030900b0
232# define PyType_GetFlags py3_PyType_GetFlags
233# endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200234#undef Py_BuildValue
235# define Py_BuildValue py3_Py_BuildValue
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100236# define Py_SetPythonHome py3_Py_SetPythonHome
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200237# define Py_Initialize py3_Py_Initialize
238# define Py_Finalize py3_Py_Finalize
239# define Py_IsInitialized py3_Py_IsInitialized
240# define _Py_NoneStruct (*py3__Py_NoneStruct)
Bram Moolenaar66b79852012-09-21 14:00:35 +0200241# define _Py_FalseStruct (*py3__Py_FalseStruct)
242# define _Py_TrueStruct (*py3__Py_TrueStruct)
Bram Moolenaardb913952012-06-29 12:54:53 +0200243# define _PyObject_NextNotImplemented (*py3__PyObject_NextNotImplemented)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200244# define PyModule_AddObject py3_PyModule_AddObject
245# define PyImport_AppendInittab py3_PyImport_AppendInittab
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200246# define PyImport_AddModule py3_PyImport_AddModule
Bram Moolenaar7bc4f932012-10-14 03:22:56 +0200247# if PY_VERSION_HEX >= 0x030300f0
248# undef _PyUnicode_AsString
Bram Moolenaar9c9cbf12012-10-23 05:17:37 +0200249# define _PyUnicode_AsString py3_PyUnicode_AsUTF8
Bram Moolenaar7bc4f932012-10-14 03:22:56 +0200250# else
251# define _PyUnicode_AsString py3__PyUnicode_AsString
252# endif
Bram Moolenaar19e60942011-06-19 00:27:51 +0200253# undef PyUnicode_AsEncodedString
254# define PyUnicode_AsEncodedString py3_PyUnicode_AsEncodedString
255# undef PyBytes_AsString
256# define PyBytes_AsString py3_PyBytes_AsString
Bram Moolenaar32ac8cd2013-07-03 18:49:17 +0200257# ifndef PyBytes_AsStringAndSize
258# define PyBytes_AsStringAndSize py3_PyBytes_AsStringAndSize
259# endif
Bram Moolenaardb913952012-06-29 12:54:53 +0200260# undef PyBytes_FromString
261# define PyBytes_FromString py3_PyBytes_FromString
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100262# undef PyBytes_FromStringAndSize
263# define PyBytes_FromStringAndSize py3_PyBytes_FromStringAndSize
Bram Moolenaaree1b9312020-07-16 22:15:53 +0200264# if defined(Py_DEBUG) || PY_VERSION_HEX >= 0x030900b0
265# define _Py_Dealloc py3__Py_Dealloc
266# endif
Bram Moolenaardb913952012-06-29 12:54:53 +0200267# define PyFloat_FromDouble py3_PyFloat_FromDouble
268# define PyFloat_AsDouble py3_PyFloat_AsDouble
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200269# define PyObject_GenericGetAttr py3_PyObject_GenericGetAttr
Bram Moolenaar66b79852012-09-21 14:00:35 +0200270# define PyType_Type (*py3_PyType_Type)
Bram Moolenaard4a8c982018-05-15 22:31:18 +0200271# define PyStdPrinter_Type (*py3_PyStdPrinter_Type)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200272# define PySlice_Type (*py3_PySlice_Type)
Bram Moolenaardb913952012-06-29 12:54:53 +0200273# define PyFloat_Type (*py3_PyFloat_Type)
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200274# define PyNumber_Check (*py3_PyNumber_Check)
275# define PyNumber_Long (*py3_PyNumber_Long)
Bram Moolenaar19e60942011-06-19 00:27:51 +0200276# define PyErr_NewException py3_PyErr_NewException
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200277# ifdef Py_DEBUG
278# define _Py_NegativeRefcount py3__Py_NegativeRefcount
279# define _Py_RefTotal (*py3__Py_RefTotal)
Bram Moolenaar0014a532013-05-29 21:33:39 +0200280# define PyModule_Create2TraceRefs py3_PyModule_Create2TraceRefs
281# else
282# define PyModule_Create2 py3_PyModule_Create2
283# endif
284# if defined(Py_DEBUG) && !defined(Py_DEBUG_NO_PYMALLOC)
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200285# define _PyObject_DebugMalloc py3__PyObject_DebugMalloc
286# define _PyObject_DebugFree py3__PyObject_DebugFree
287# else
288# define PyObject_Malloc py3_PyObject_Malloc
289# define PyObject_Free py3_PyObject_Free
290# endif
Bram Moolenaar774267b2013-05-21 20:51:59 +0200291# define _PyObject_GC_New py3__PyObject_GC_New
292# define PyObject_GC_Del py3_PyObject_GC_Del
293# define PyObject_GC_UnTrack py3_PyObject_GC_UnTrack
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200294# define PyType_GenericAlloc py3_PyType_GenericAlloc
295# define PyType_GenericNew py3_PyType_GenericNew
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200296# undef PyUnicode_FromString
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200297# define PyUnicode_FromString py3_PyUnicode_FromString
Bram Moolenaar1a3b5692013-05-30 12:40:39 +0200298# ifndef PyUnicode_FromFormat
299# define PyUnicode_FromFormat py3_PyUnicode_FromFormat
300# else
301# define Py_UNICODE_USE_UCS_FUNCTIONS
302# ifdef Py_UNICODE_WIDE
303# define PyUnicodeUCS4_FromFormat py3_PyUnicodeUCS4_FromFormat
304# else
305# define PyUnicodeUCS2_FromFormat py3_PyUnicodeUCS2_FromFormat
306# endif
307# endif
Bram Moolenaar19e60942011-06-19 00:27:51 +0200308# undef PyUnicode_Decode
309# define PyUnicode_Decode py3_PyUnicode_Decode
Bram Moolenaardb913952012-06-29 12:54:53 +0200310# define PyType_IsSubtype py3_PyType_IsSubtype
311# define PyCapsule_New py3_PyCapsule_New
312# define PyCapsule_GetPointer py3_PyCapsule_GetPointer
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200313
Bram Moolenaar0014a532013-05-29 21:33:39 +0200314# if defined(Py_DEBUG) && !defined(Py_DEBUG_NO_PYMALLOC)
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200315# undef PyObject_NEW
316# define PyObject_NEW(type, typeobj) \
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200317( (type *) PyObject_Init( \
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200318 (PyObject *) _PyObject_DebugMalloc( _PyObject_SIZE(typeobj) ), (typeobj)) )
Bram Moolenaaree1b9312020-07-16 22:15:53 +0200319# elif PY_VERSION_HEX >= 0x030900b0
320# undef PyObject_NEW
321# define PyObject_NEW(type, typeobj) \
322 ((type *)py3__PyObject_New(typeobj))
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200323# endif
324
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200325/*
326 * Pointers for dynamic link
327 */
328static int (*py3_PySys_SetArgv)(int, wchar_t **);
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100329static void (*py3_Py_SetPythonHome)(wchar_t *home);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200330static void (*py3_Py_Initialize)(void);
331static PyObject* (*py3_PyList_New)(Py_ssize_t size);
332static PyGILState_STATE (*py3_PyGILState_Ensure)(void);
333static void (*py3_PyGILState_Release)(PyGILState_STATE);
334static int (*py3_PySys_SetObject)(char *, PyObject *);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200335static PyObject* (*py3_PySys_GetObject)(char *);
336static int (*py3_PyList_Append)(PyObject *, PyObject *);
337static int (*py3_PyList_Insert)(PyObject *, int, PyObject *);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200338static Py_ssize_t (*py3_PyList_Size)(PyObject *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200339static int (*py3_PySequence_Check)(PyObject *);
340static Py_ssize_t (*py3_PySequence_Size)(PyObject *);
341static PyObject* (*py3_PySequence_GetItem)(PyObject *, Py_ssize_t);
Bram Moolenaara9922d62013-05-30 13:01:18 +0200342static PyObject* (*py3_PySequence_Fast)(PyObject *, const char *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200343static Py_ssize_t (*py3_PyTuple_Size)(PyObject *);
344static PyObject* (*py3_PyTuple_GetItem)(PyObject *, Py_ssize_t);
345static int (*py3_PyMapping_Check)(PyObject *);
Bram Moolenaarbcb40972013-05-30 13:22:13 +0200346static PyObject* (*py3_PyMapping_Keys)(PyObject *);
Bram Moolenaar3b48b112018-07-04 22:03:25 +0200347# if PY_VERSION_HEX >= 0x030601f0
348static int (*py3_PySlice_AdjustIndices)(Py_ssize_t length,
349 Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t step);
350static int (*py3_PySlice_Unpack)(PyObject *slice,
351 Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t *step);
352# endif
Bram Moolenaar922a4662014-03-30 16:11:43 +0200353static int (*py3_PySlice_GetIndicesEx)(PySliceObject_T *r, Py_ssize_t length,
Bram Moolenaar063a46b2014-01-14 16:36:51 +0100354 Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t *step,
355 Py_ssize_t *slicelen);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200356static PyObject* (*py3_PyErr_NoMemory)(void);
357static void (*py3_Py_Finalize)(void);
358static void (*py3_PyErr_SetString)(PyObject *, const char *);
Bram Moolenaar4d188da2013-05-15 15:35:09 +0200359static void (*py3_PyErr_SetObject)(PyObject *, PyObject *);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200360static int (*py3_PyErr_ExceptionMatches)(PyObject *);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200361static int (*py3_PyRun_SimpleString)(char *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200362static PyObject* (*py3_PyRun_String)(char *, int, PyObject *, PyObject *);
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200363static PyObject* (*py3_PyObject_GetAttrString)(PyObject *, const char *);
Bram Moolenaara9922d62013-05-30 13:01:18 +0200364static int (*py3_PyObject_HasAttrString)(PyObject *, const char *);
Bram Moolenaar0b400082013-11-03 00:28:25 +0100365static int (*py3_PyObject_SetAttrString)(PyObject *, const char *, PyObject *);
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200366static PyObject* (*py3_PyObject_CallFunctionObjArgs)(PyObject *, ...);
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200367static PyObject* (*py3__PyObject_CallFunction_SizeT)(PyObject *, char *, ...);
Bram Moolenaarf4258302013-06-02 18:20:17 +0200368static PyObject* (*py3_PyObject_Call)(PyObject *, PyObject *, PyObject *);
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200369static PyObject* (*py3_PyEval_GetGlobals)();
370static PyObject* (*py3_PyEval_GetLocals)();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200371static PyObject* (*py3_PyList_GetItem)(PyObject *, Py_ssize_t);
372static PyObject* (*py3_PyImport_ImportModule)(const char *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200373static PyObject* (*py3_PyImport_AddModule)(const char *);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200374static int (*py3_PyErr_BadArgument)(void);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200375static PyObject* (*py3_PyErr_Occurred)(void);
376static PyObject* (*py3_PyModule_GetDict)(PyObject *);
377static int (*py3_PyList_SetItem)(PyObject *, Py_ssize_t, PyObject *);
378static PyObject* (*py3_PyDict_GetItemString)(PyObject *, const char *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200379static int (*py3_PyDict_Next)(PyObject *, Py_ssize_t *, PyObject **, PyObject **);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200380static PyObject* (*py3_PyLong_FromLong)(long);
381static PyObject* (*py3_PyDict_New)(void);
Zdenek Dohnal90478f32021-06-14 15:08:30 +0200382# if PY_VERSION_HEX >= 0x030a00b2
383static int (*py3_PyIter_Check)(PyObject *o);
384# endif
Bram Moolenaardb913952012-06-29 12:54:53 +0200385static PyObject* (*py3_PyIter_Next)(PyObject *);
386static PyObject* (*py3_PyObject_GetIter)(PyObject *);
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200387static PyObject* (*py3_PyObject_Repr)(PyObject *);
Bram Moolenaarbcb40972013-05-30 13:22:13 +0200388static PyObject* (*py3_PyObject_GetItem)(PyObject *, PyObject *);
Bram Moolenaar03db85b2013-05-15 14:51:35 +0200389static int (*py3_PyObject_IsTrue)(PyObject *);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200390static PyObject* (*py3_Py_BuildValue)(char *, ...);
Bram Moolenaaree1b9312020-07-16 22:15:53 +0200391# if PY_VERSION_HEX >= 0x030900b0
392static int (*py3_PyType_GetFlags)(PyTypeObject *o);
393# endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200394static int (*py3_PyType_Ready)(PyTypeObject *type);
395static int (*py3_PyDict_SetItemString)(PyObject *dp, char *key, PyObject *item);
396static PyObject* (*py3_PyUnicode_FromString)(const char *u);
Bram Moolenaar1a3b5692013-05-30 12:40:39 +0200397# ifndef Py_UNICODE_USE_UCS_FUNCTIONS
398static PyObject* (*py3_PyUnicode_FromFormat)(const char *u, ...);
399# else
400# ifdef Py_UNICODE_WIDE
401static PyObject* (*py3_PyUnicodeUCS4_FromFormat)(const char *u, ...);
402# else
403static PyObject* (*py3_PyUnicodeUCS2_FromFormat)(const char *u, ...);
404# endif
405# endif
Bram Moolenaar19e60942011-06-19 00:27:51 +0200406static PyObject* (*py3_PyUnicode_Decode)(const char *u, Py_ssize_t size,
407 const char *encoding, const char *errors);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200408static long (*py3_PyLong_AsLong)(PyObject *);
409static void (*py3_PyErr_SetNone)(PyObject *);
410static void (*py3_PyEval_InitThreads)(void);
411static void(*py3_PyEval_RestoreThread)(PyThreadState *);
412static PyThreadState*(*py3_PyEval_SaveThread)(void);
413static int (*py3_PyArg_Parse)(PyObject *, char *, ...);
414static int (*py3_PyArg_ParseTuple)(PyObject *, char *, ...);
Bram Moolenaar19e60942011-06-19 00:27:51 +0200415static int (*py3_PyMem_Free)(void *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200416static void* (*py3_PyMem_Malloc)(size_t);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200417static int (*py3_Py_IsInitialized)(void);
418static void (*py3_PyErr_Clear)(void);
Bram Moolenaarc476e522013-06-23 13:46:40 +0200419static PyObject* (*py3_PyErr_Format)(PyObject *, const char *, ...);
Bram Moolenaar4d369872013-02-20 16:09:43 +0100420static void (*py3_PyErr_PrintEx)(int);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200421static PyObject*(*py3__PyObject_Init)(PyObject *, PyTypeObject *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200422static iternextfunc py3__PyObject_NextNotImplemented;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200423static PyObject* py3__Py_NoneStruct;
Bram Moolenaar66b79852012-09-21 14:00:35 +0200424static PyObject* py3__Py_FalseStruct;
425static PyObject* py3__Py_TrueStruct;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200426static int (*py3_PyModule_AddObject)(PyObject *m, const char *name, PyObject *o);
427static int (*py3_PyImport_AppendInittab)(const char *name, PyObject* (*initfunc)(void));
Bram Moolenaar9c9cbf12012-10-23 05:17:37 +0200428# if PY_VERSION_HEX >= 0x030300f0
429static char* (*py3_PyUnicode_AsUTF8)(PyObject *unicode);
430# else
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200431static char* (*py3__PyUnicode_AsString)(PyObject *unicode);
Bram Moolenaar9c9cbf12012-10-23 05:17:37 +0200432# endif
Bram Moolenaar19e60942011-06-19 00:27:51 +0200433static PyObject* (*py3_PyUnicode_AsEncodedString)(PyObject *unicode, const char* encoding, const char* errors);
434static char* (*py3_PyBytes_AsString)(PyObject *bytes);
Bram Moolenaar808c2bc2013-06-23 13:11:18 +0200435static int (*py3_PyBytes_AsStringAndSize)(PyObject *bytes, char **buffer, Py_ssize_t *length);
Bram Moolenaardb913952012-06-29 12:54:53 +0200436static PyObject* (*py3_PyBytes_FromString)(char *str);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100437static PyObject* (*py3_PyBytes_FromStringAndSize)(char *str, Py_ssize_t length);
Bram Moolenaaree1b9312020-07-16 22:15:53 +0200438# if defined(Py_DEBUG) || PY_VERSION_HEX >= 0x030900b0
439static void (*py3__Py_Dealloc)(PyObject *obj);
440# endif
441# if PY_VERSION_HEX >= 0x030900b0
442static PyObject* (*py3__PyObject_New)(PyTypeObject *);
443# endif
Bram Moolenaardb913952012-06-29 12:54:53 +0200444static PyObject* (*py3_PyFloat_FromDouble)(double num);
445static double (*py3_PyFloat_AsDouble)(PyObject *);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200446static PyObject* (*py3_PyObject_GenericGetAttr)(PyObject *obj, PyObject *name);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200447static PyObject* (*py3_PyType_GenericAlloc)(PyTypeObject *type, Py_ssize_t nitems);
448static PyObject* (*py3_PyType_GenericNew)(PyTypeObject *type, PyObject *args, PyObject *kwds);
Bram Moolenaar66b79852012-09-21 14:00:35 +0200449static PyTypeObject* py3_PyType_Type;
Bram Moolenaard4a8c982018-05-15 22:31:18 +0200450static PyTypeObject* py3_PyStdPrinter_Type;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200451static PyTypeObject* py3_PySlice_Type;
Bram Moolenaardb913952012-06-29 12:54:53 +0200452static PyTypeObject* py3_PyFloat_Type;
Zdenek Dohnale5e47092023-08-13 19:37:09 +0200453PyTypeObject* py3_PyBool_Type;
454# if PY_VERSION_HEX >= 0x030c00b0
455PyTypeObject* py3_PyLong_Type;
456# endif
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200457static int (*py3_PyNumber_Check)(PyObject *);
458static PyObject* (*py3_PyNumber_Long)(PyObject *);
Bram Moolenaar19e60942011-06-19 00:27:51 +0200459static PyObject* (*py3_PyErr_NewException)(char *name, PyObject *base, PyObject *dict);
Bram Moolenaardb913952012-06-29 12:54:53 +0200460static PyObject* (*py3_PyCapsule_New)(void *, char *, PyCapsule_Destructor);
461static void* (*py3_PyCapsule_GetPointer)(PyObject *, char *);
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200462# ifdef Py_DEBUG
Bram Moolenaar0014a532013-05-29 21:33:39 +0200463static void (*py3__Py_NegativeRefcount)(const char *fname, int lineno, PyObject *op);
464static Py_ssize_t* py3__Py_RefTotal;
Bram Moolenaar0014a532013-05-29 21:33:39 +0200465static PyObject* (*py3_PyModule_Create2TraceRefs)(struct PyModuleDef* module, int module_api_version);
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200466# else
Bram Moolenaar0014a532013-05-29 21:33:39 +0200467static PyObject* (*py3_PyModule_Create2)(struct PyModuleDef* module, int module_api_version);
468# endif
469# if defined(Py_DEBUG) && !defined(Py_DEBUG_NO_PYMALLOC)
470static void (*py3__PyObject_DebugFree)(void*);
471static void* (*py3__PyObject_DebugMalloc)(size_t);
472# else
473static void (*py3_PyObject_Free)(void*);
474static void* (*py3_PyObject_Malloc)(size_t);
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200475# endif
Bram Moolenaar774267b2013-05-21 20:51:59 +0200476static PyObject*(*py3__PyObject_GC_New)(PyTypeObject *);
477static void(*py3_PyObject_GC_Del)(void *);
478static void(*py3_PyObject_GC_UnTrack)(void *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200479static int (*py3_PyType_IsSubtype)(PyTypeObject *, PyTypeObject *);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200480
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100481// Imported exception objects
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200482static PyObject *p3imp_PyExc_AttributeError;
483static PyObject *p3imp_PyExc_IndexError;
Bram Moolenaaraf6abb92013-04-24 13:04:26 +0200484static PyObject *p3imp_PyExc_KeyError;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200485static PyObject *p3imp_PyExc_KeyboardInterrupt;
486static PyObject *p3imp_PyExc_TypeError;
487static PyObject *p3imp_PyExc_ValueError;
Bram Moolenaar41009372013-07-01 22:03:04 +0200488static PyObject *p3imp_PyExc_SystemExit;
Bram Moolenaar8661b172013-05-15 15:44:28 +0200489static PyObject *p3imp_PyExc_RuntimeError;
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200490static PyObject *p3imp_PyExc_ImportError;
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200491static PyObject *p3imp_PyExc_OverflowError;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200492
493# define PyExc_AttributeError p3imp_PyExc_AttributeError
494# define PyExc_IndexError p3imp_PyExc_IndexError
Bram Moolenaaraf6abb92013-04-24 13:04:26 +0200495# define PyExc_KeyError p3imp_PyExc_KeyError
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200496# define PyExc_KeyboardInterrupt p3imp_PyExc_KeyboardInterrupt
497# define PyExc_TypeError p3imp_PyExc_TypeError
498# define PyExc_ValueError p3imp_PyExc_ValueError
Bram Moolenaar41009372013-07-01 22:03:04 +0200499# define PyExc_SystemExit p3imp_PyExc_SystemExit
Bram Moolenaar8661b172013-05-15 15:44:28 +0200500# define PyExc_RuntimeError p3imp_PyExc_RuntimeError
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200501# define PyExc_ImportError p3imp_PyExc_ImportError
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200502# define PyExc_OverflowError p3imp_PyExc_OverflowError
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200503
504/*
505 * Table of name to function pointer of python.
506 */
507# define PYTHON_PROC FARPROC
508static struct
509{
510 char *name;
511 PYTHON_PROC *ptr;
512} py3_funcname_table[] =
513{
514 {"PySys_SetArgv", (PYTHON_PROC*)&py3_PySys_SetArgv},
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100515 {"Py_SetPythonHome", (PYTHON_PROC*)&py3_Py_SetPythonHome},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200516 {"Py_Initialize", (PYTHON_PROC*)&py3_Py_Initialize},
Bram Moolenaare8cdcef2012-09-12 20:21:43 +0200517 {"_PyArg_ParseTuple_SizeT", (PYTHON_PROC*)&py3_PyArg_ParseTuple},
518 {"_Py_BuildValue_SizeT", (PYTHON_PROC*)&py3_Py_BuildValue},
Bram Moolenaar19e60942011-06-19 00:27:51 +0200519 {"PyMem_Free", (PYTHON_PROC*)&py3_PyMem_Free},
Bram Moolenaardb913952012-06-29 12:54:53 +0200520 {"PyMem_Malloc", (PYTHON_PROC*)&py3_PyMem_Malloc},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200521 {"PyList_New", (PYTHON_PROC*)&py3_PyList_New},
522 {"PyGILState_Ensure", (PYTHON_PROC*)&py3_PyGILState_Ensure},
523 {"PyGILState_Release", (PYTHON_PROC*)&py3_PyGILState_Release},
524 {"PySys_SetObject", (PYTHON_PROC*)&py3_PySys_SetObject},
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200525 {"PySys_GetObject", (PYTHON_PROC*)&py3_PySys_GetObject},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200526 {"PyList_Append", (PYTHON_PROC*)&py3_PyList_Append},
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200527 {"PyList_Insert", (PYTHON_PROC*)&py3_PyList_Insert},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200528 {"PyList_Size", (PYTHON_PROC*)&py3_PyList_Size},
Bram Moolenaardb913952012-06-29 12:54:53 +0200529 {"PySequence_Check", (PYTHON_PROC*)&py3_PySequence_Check},
530 {"PySequence_Size", (PYTHON_PROC*)&py3_PySequence_Size},
531 {"PySequence_GetItem", (PYTHON_PROC*)&py3_PySequence_GetItem},
Bram Moolenaara9922d62013-05-30 13:01:18 +0200532 {"PySequence_Fast", (PYTHON_PROC*)&py3_PySequence_Fast},
Bram Moolenaardb913952012-06-29 12:54:53 +0200533 {"PyTuple_Size", (PYTHON_PROC*)&py3_PyTuple_Size},
534 {"PyTuple_GetItem", (PYTHON_PROC*)&py3_PyTuple_GetItem},
Bram Moolenaar3b48b112018-07-04 22:03:25 +0200535# if PY_VERSION_HEX >= 0x030601f0
536 {"PySlice_AdjustIndices", (PYTHON_PROC*)&py3_PySlice_AdjustIndices},
537 {"PySlice_Unpack", (PYTHON_PROC*)&py3_PySlice_Unpack},
538# endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200539 {"PySlice_GetIndicesEx", (PYTHON_PROC*)&py3_PySlice_GetIndicesEx},
540 {"PyErr_NoMemory", (PYTHON_PROC*)&py3_PyErr_NoMemory},
541 {"Py_Finalize", (PYTHON_PROC*)&py3_Py_Finalize},
542 {"PyErr_SetString", (PYTHON_PROC*)&py3_PyErr_SetString},
Bram Moolenaar4d188da2013-05-15 15:35:09 +0200543 {"PyErr_SetObject", (PYTHON_PROC*)&py3_PyErr_SetObject},
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200544 {"PyErr_ExceptionMatches", (PYTHON_PROC*)&py3_PyErr_ExceptionMatches},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200545 {"PyRun_SimpleString", (PYTHON_PROC*)&py3_PyRun_SimpleString},
Bram Moolenaardb913952012-06-29 12:54:53 +0200546 {"PyRun_String", (PYTHON_PROC*)&py3_PyRun_String},
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200547 {"PyObject_GetAttrString", (PYTHON_PROC*)&py3_PyObject_GetAttrString},
Bram Moolenaara9922d62013-05-30 13:01:18 +0200548 {"PyObject_HasAttrString", (PYTHON_PROC*)&py3_PyObject_HasAttrString},
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200549 {"PyObject_SetAttrString", (PYTHON_PROC*)&py3_PyObject_SetAttrString},
550 {"PyObject_CallFunctionObjArgs", (PYTHON_PROC*)&py3_PyObject_CallFunctionObjArgs},
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200551 {"_PyObject_CallFunction_SizeT", (PYTHON_PROC*)&py3__PyObject_CallFunction_SizeT},
Bram Moolenaarf4258302013-06-02 18:20:17 +0200552 {"PyObject_Call", (PYTHON_PROC*)&py3_PyObject_Call},
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200553 {"PyEval_GetGlobals", (PYTHON_PROC*)&py3_PyEval_GetGlobals},
554 {"PyEval_GetLocals", (PYTHON_PROC*)&py3_PyEval_GetLocals},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200555 {"PyList_GetItem", (PYTHON_PROC*)&py3_PyList_GetItem},
556 {"PyImport_ImportModule", (PYTHON_PROC*)&py3_PyImport_ImportModule},
Bram Moolenaardb913952012-06-29 12:54:53 +0200557 {"PyImport_AddModule", (PYTHON_PROC*)&py3_PyImport_AddModule},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200558 {"PyErr_BadArgument", (PYTHON_PROC*)&py3_PyErr_BadArgument},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200559 {"PyErr_Occurred", (PYTHON_PROC*)&py3_PyErr_Occurred},
560 {"PyModule_GetDict", (PYTHON_PROC*)&py3_PyModule_GetDict},
561 {"PyList_SetItem", (PYTHON_PROC*)&py3_PyList_SetItem},
562 {"PyDict_GetItemString", (PYTHON_PROC*)&py3_PyDict_GetItemString},
Bram Moolenaardb913952012-06-29 12:54:53 +0200563 {"PyDict_Next", (PYTHON_PROC*)&py3_PyDict_Next},
564 {"PyMapping_Check", (PYTHON_PROC*)&py3_PyMapping_Check},
Bram Moolenaarbcb40972013-05-30 13:22:13 +0200565 {"PyMapping_Keys", (PYTHON_PROC*)&py3_PyMapping_Keys},
Zdenek Dohnal90478f32021-06-14 15:08:30 +0200566# if PY_VERSION_HEX >= 0x030a00b2
567 {"PyIter_Check", (PYTHON_PROC*)&py3_PyIter_Check},
568# endif
Bram Moolenaardb913952012-06-29 12:54:53 +0200569 {"PyIter_Next", (PYTHON_PROC*)&py3_PyIter_Next},
570 {"PyObject_GetIter", (PYTHON_PROC*)&py3_PyObject_GetIter},
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200571 {"PyObject_Repr", (PYTHON_PROC*)&py3_PyObject_Repr},
Bram Moolenaarbcb40972013-05-30 13:22:13 +0200572 {"PyObject_GetItem", (PYTHON_PROC*)&py3_PyObject_GetItem},
Bram Moolenaar03db85b2013-05-15 14:51:35 +0200573 {"PyObject_IsTrue", (PYTHON_PROC*)&py3_PyObject_IsTrue},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200574 {"PyLong_FromLong", (PYTHON_PROC*)&py3_PyLong_FromLong},
575 {"PyDict_New", (PYTHON_PROC*)&py3_PyDict_New},
Bram Moolenaaree1b9312020-07-16 22:15:53 +0200576# if PY_VERSION_HEX >= 0x030900b0
577 {"PyType_GetFlags", (PYTHON_PROC*)&py3_PyType_GetFlags},
578# endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200579 {"PyType_Ready", (PYTHON_PROC*)&py3_PyType_Ready},
580 {"PyDict_SetItemString", (PYTHON_PROC*)&py3_PyDict_SetItemString},
581 {"PyLong_AsLong", (PYTHON_PROC*)&py3_PyLong_AsLong},
582 {"PyErr_SetNone", (PYTHON_PROC*)&py3_PyErr_SetNone},
583 {"PyEval_InitThreads", (PYTHON_PROC*)&py3_PyEval_InitThreads},
584 {"PyEval_RestoreThread", (PYTHON_PROC*)&py3_PyEval_RestoreThread},
585 {"PyEval_SaveThread", (PYTHON_PROC*)&py3_PyEval_SaveThread},
Bram Moolenaar5f87b232013-06-13 20:57:50 +0200586 {"_PyArg_Parse_SizeT", (PYTHON_PROC*)&py3_PyArg_Parse},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200587 {"Py_IsInitialized", (PYTHON_PROC*)&py3_Py_IsInitialized},
Bram Moolenaardb913952012-06-29 12:54:53 +0200588 {"_PyObject_NextNotImplemented", (PYTHON_PROC*)&py3__PyObject_NextNotImplemented},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200589 {"_Py_NoneStruct", (PYTHON_PROC*)&py3__Py_NoneStruct},
Bram Moolenaar66b79852012-09-21 14:00:35 +0200590 {"_Py_FalseStruct", (PYTHON_PROC*)&py3__Py_FalseStruct},
591 {"_Py_TrueStruct", (PYTHON_PROC*)&py3__Py_TrueStruct},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200592 {"PyErr_Clear", (PYTHON_PROC*)&py3_PyErr_Clear},
Bram Moolenaarc476e522013-06-23 13:46:40 +0200593 {"PyErr_Format", (PYTHON_PROC*)&py3_PyErr_Format},
Bram Moolenaar4d369872013-02-20 16:09:43 +0100594 {"PyErr_PrintEx", (PYTHON_PROC*)&py3_PyErr_PrintEx},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200595 {"PyObject_Init", (PYTHON_PROC*)&py3__PyObject_Init},
596 {"PyModule_AddObject", (PYTHON_PROC*)&py3_PyModule_AddObject},
597 {"PyImport_AppendInittab", (PYTHON_PROC*)&py3_PyImport_AppendInittab},
Bram Moolenaar9c9cbf12012-10-23 05:17:37 +0200598# if PY_VERSION_HEX >= 0x030300f0
599 {"PyUnicode_AsUTF8", (PYTHON_PROC*)&py3_PyUnicode_AsUTF8},
600# else
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200601 {"_PyUnicode_AsString", (PYTHON_PROC*)&py3__PyUnicode_AsString},
Bram Moolenaar9c9cbf12012-10-23 05:17:37 +0200602# endif
Bram Moolenaar1a3b5692013-05-30 12:40:39 +0200603# ifndef Py_UNICODE_USE_UCS_FUNCTIONS
604 {"PyUnicode_FromFormat", (PYTHON_PROC*)&py3_PyUnicode_FromFormat},
605# else
606# ifdef Py_UNICODE_WIDE
607 {"PyUnicodeUCS4_FromFormat", (PYTHON_PROC*)&py3_PyUnicodeUCS4_FromFormat},
608# else
609 {"PyUnicodeUCS2_FromFormat", (PYTHON_PROC*)&py3_PyUnicodeUCS2_FromFormat},
610# endif
611# endif
Bram Moolenaar19e60942011-06-19 00:27:51 +0200612 {"PyBytes_AsString", (PYTHON_PROC*)&py3_PyBytes_AsString},
Bram Moolenaarcdab9052012-09-05 19:03:56 +0200613 {"PyBytes_AsStringAndSize", (PYTHON_PROC*)&py3_PyBytes_AsStringAndSize},
Bram Moolenaardb913952012-06-29 12:54:53 +0200614 {"PyBytes_FromString", (PYTHON_PROC*)&py3_PyBytes_FromString},
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100615 {"PyBytes_FromStringAndSize", (PYTHON_PROC*)&py3_PyBytes_FromStringAndSize},
Bram Moolenaaree1b9312020-07-16 22:15:53 +0200616# if defined(Py_DEBUG) || PY_VERSION_HEX >= 0x030900b0
617 {"_Py_Dealloc", (PYTHON_PROC*)&py3__Py_Dealloc},
618# endif
619# if PY_VERSION_HEX >= 0x030900b0
620 {"_PyObject_New", (PYTHON_PROC*)&py3__PyObject_New},
621# endif
Bram Moolenaardb913952012-06-29 12:54:53 +0200622 {"PyFloat_FromDouble", (PYTHON_PROC*)&py3_PyFloat_FromDouble},
623 {"PyFloat_AsDouble", (PYTHON_PROC*)&py3_PyFloat_AsDouble},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200624 {"PyObject_GenericGetAttr", (PYTHON_PROC*)&py3_PyObject_GenericGetAttr},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200625 {"PyType_GenericAlloc", (PYTHON_PROC*)&py3_PyType_GenericAlloc},
626 {"PyType_GenericNew", (PYTHON_PROC*)&py3_PyType_GenericNew},
Bram Moolenaar66b79852012-09-21 14:00:35 +0200627 {"PyType_Type", (PYTHON_PROC*)&py3_PyType_Type},
Bram Moolenaard4a8c982018-05-15 22:31:18 +0200628 {"PyStdPrinter_Type", (PYTHON_PROC*)&py3_PyStdPrinter_Type},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200629 {"PySlice_Type", (PYTHON_PROC*)&py3_PySlice_Type},
Bram Moolenaardb913952012-06-29 12:54:53 +0200630 {"PyFloat_Type", (PYTHON_PROC*)&py3_PyFloat_Type},
Zdenek Dohnal15a0a022023-08-15 22:52:01 +0200631# if PY_VERSION_HEX < 0x030c00b0
Bram Moolenaar66b79852012-09-21 14:00:35 +0200632 {"PyBool_Type", (PYTHON_PROC*)&py3_PyBool_Type},
Zdenek Dohnale5e47092023-08-13 19:37:09 +0200633# endif
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200634 {"PyNumber_Check", (PYTHON_PROC*)&py3_PyNumber_Check},
635 {"PyNumber_Long", (PYTHON_PROC*)&py3_PyNumber_Long},
Bram Moolenaar19e60942011-06-19 00:27:51 +0200636 {"PyErr_NewException", (PYTHON_PROC*)&py3_PyErr_NewException},
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200637# ifdef Py_DEBUG
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200638 {"_Py_NegativeRefcount", (PYTHON_PROC*)&py3__Py_NegativeRefcount},
639 {"_Py_RefTotal", (PYTHON_PROC*)&py3__Py_RefTotal},
Bram Moolenaar0014a532013-05-29 21:33:39 +0200640 {"PyModule_Create2TraceRefs", (PYTHON_PROC*)&py3_PyModule_Create2TraceRefs},
641# else
642 {"PyModule_Create2", (PYTHON_PROC*)&py3_PyModule_Create2},
643# endif
644# if defined(Py_DEBUG) && !defined(Py_DEBUG_NO_PYMALLOC)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200645 {"_PyObject_DebugFree", (PYTHON_PROC*)&py3__PyObject_DebugFree},
646 {"_PyObject_DebugMalloc", (PYTHON_PROC*)&py3__PyObject_DebugMalloc},
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200647# else
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200648 {"PyObject_Malloc", (PYTHON_PROC*)&py3_PyObject_Malloc},
649 {"PyObject_Free", (PYTHON_PROC*)&py3_PyObject_Free},
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200650# endif
Bram Moolenaar774267b2013-05-21 20:51:59 +0200651 {"_PyObject_GC_New", (PYTHON_PROC*)&py3__PyObject_GC_New},
652 {"PyObject_GC_Del", (PYTHON_PROC*)&py3_PyObject_GC_Del},
653 {"PyObject_GC_UnTrack", (PYTHON_PROC*)&py3_PyObject_GC_UnTrack},
Bram Moolenaardb913952012-06-29 12:54:53 +0200654 {"PyType_IsSubtype", (PYTHON_PROC*)&py3_PyType_IsSubtype},
655 {"PyCapsule_New", (PYTHON_PROC*)&py3_PyCapsule_New},
656 {"PyCapsule_GetPointer", (PYTHON_PROC*)&py3_PyCapsule_GetPointer},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200657 {"", NULL},
658};
659
Bram Moolenaar13a1f3f2019-10-23 21:37:25 +0200660# if PY_VERSION_HEX >= 0x030800f0
661 static inline void
662py3__Py_DECREF(const char *filename UNUSED, int lineno UNUSED, PyObject *op)
663{
Bram Moolenaar13a1f3f2019-10-23 21:37:25 +0200664 if (--op->ob_refcnt != 0)
665 {
666# ifdef Py_REF_DEBUG
667 if (op->ob_refcnt < 0)
668 {
669 _Py_NegativeRefcount(filename, lineno, op);
670 }
671# endif
672 }
673 else
674 {
675 _Py_Dealloc(op);
676 }
677}
678
679# undef Py_DECREF
680# define Py_DECREF(op) py3__Py_DECREF(__FILE__, __LINE__, _PyObject_CAST(op))
681
682 static inline void
683py3__Py_XDECREF(PyObject *op)
684{
685 if (op != NULL)
686 {
687 Py_DECREF(op);
688 }
689}
690
691# undef Py_XDECREF
692# define Py_XDECREF(op) py3__Py_XDECREF(_PyObject_CAST(op))
693# endif
694
Bram Moolenaaree1b9312020-07-16 22:15:53 +0200695# if PY_VERSION_HEX >= 0x030900b0
696 static inline int
697py3_PyType_HasFeature(PyTypeObject *type, unsigned long feature)
698{
699 return ((PyType_GetFlags(type) & feature) != 0);
700}
701# define PyType_HasFeature(t,f) py3_PyType_HasFeature(t,f)
702# endif
703
Zdenek Dohnal90478f32021-06-14 15:08:30 +0200704# if PY_VERSION_HEX >= 0x030a00b2
705 static inline int
706py3__PyObject_TypeCheck(PyObject *ob, PyTypeObject *type)
707{
708 return Py_IS_TYPE(ob, type) || PyType_IsSubtype(Py_TYPE(ob), type);
709}
Zdenek Dohnalfee511c2022-06-27 13:59:00 +0100710# if PY_VERSION_HEX >= 0x030b00b3
711# undef PyObject_TypeCheck
712# define PyObject_TypeCheck(o,t) py3__PyObject_TypeCheck(o,t)
713# else
714# define _PyObject_TypeCheck(o,t) py3__PyObject_TypeCheck(o,t)
715# endif
Zdenek Dohnal90478f32021-06-14 15:08:30 +0200716# endif
717
Bram Moolenaarb2f9e0e2020-12-25 13:52:37 +0100718# ifdef MSWIN
719/*
720 * Look up the library "libname" using the InstallPath registry key.
721 * Return NULL when failed. Return an allocated string when successful.
722 */
723 static char *
724py3_get_system_libname(const char *libname)
725{
726 const char *cp = libname;
727 char subkey[128];
728 HKEY hKey;
729 char installpath[MAXPATHL];
730 LONG len = sizeof(installpath);
731 LSTATUS rc;
732 size_t sysliblen;
733 char *syslibname;
734
735 while (*cp != '\0')
736 {
737 if (*cp == ':' || *cp == '\\' || *cp == '/')
738 {
739 // Bail out if "libname" contains path separator, assume it is
740 // an absolute path.
741 return NULL;
742 }
743 ++cp;
744 }
745 vim_snprintf(subkey, sizeof(subkey),
746# ifdef _WIN64
747 "Software\\Python\\PythonCore\\%d.%d\\InstallPath",
748# else
749 "Software\\Python\\PythonCore\\%d.%d-32\\InstallPath",
750# endif
751 PY_MAJOR_VERSION, PY_MINOR_VERSION);
752 if (RegOpenKeyExA(HKEY_LOCAL_MACHINE, subkey, 0, KEY_QUERY_VALUE, &hKey)
753 != ERROR_SUCCESS)
754 return NULL;
755 rc = RegQueryValueA(hKey, NULL, installpath, &len);
756 RegCloseKey(hKey);
757 if (ERROR_SUCCESS != rc)
758 return NULL;
759 cp = installpath + len;
760 // Just in case registry value contains null terminators.
761 while (cp > installpath && *(cp-1) == '\0')
762 --cp;
763 // Remove trailing path separators.
764 while (cp > installpath && (*(cp-1) == '\\' || *(cp-1) == '/'))
765 --cp;
766 // Ignore if InstallPath is effectively empty.
767 if (cp <= installpath)
768 return NULL;
769 sysliblen = (cp - installpath) + 1 + STRLEN(libname) + 1;
770 syslibname = alloc(sysliblen);
771 vim_snprintf(syslibname, sysliblen, "%.*s\\%s",
772 (int)(cp - installpath), installpath, libname);
773 return syslibname;
774}
775# endif
776
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200777/*
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200778 * Load library and get all pointers.
779 * Parameter 'libname' provides name of DLL.
780 * Return OK or FAIL.
781 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200782 static int
783py3_runtime_link_init(char *libname, int verbose)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200784{
785 int i;
Bram Moolenaar7b24ce02018-03-29 18:15:26 +0200786 PYTHON_PROC *ucs_from_string = (PYTHON_PROC *)&py3_PyUnicode_FromString;
787 PYTHON_PROC *ucs_decode = (PYTHON_PROC *)&py3_PyUnicode_Decode;
788 PYTHON_PROC *ucs_as_encoded_string =
789 (PYTHON_PROC *)&py3_PyUnicode_AsEncodedString;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200790
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100791# if !(defined(PY_NO_RTLD_GLOBAL) && defined(PY3_NO_RTLD_GLOBAL)) && defined(UNIX) && defined(FEAT_PYTHON)
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100792 // Can't have Python and Python3 loaded at the same time.
Dominique Pelleaf4a61a2021-12-27 17:21:41 +0000793 // It causes a crash, because RTLD_GLOBAL is needed for
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100794 // standard C extension libraries of one or both python versions.
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200795 if (python_loaded())
796 {
Bram Moolenaar9dc93ae2011-08-28 16:00:19 +0200797 if (verbose)
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +0000798 emsg(_(e_this_vim_cannot_execute_py3_after_using_python));
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200799 return FAIL;
800 }
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200801# endif
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200802
803 if (hinstPy3 != 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200804 return OK;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200805 hinstPy3 = load_dll(libname);
806
Bram Moolenaarb2f9e0e2020-12-25 13:52:37 +0100807# ifdef MSWIN
808 if (!hinstPy3)
809 {
810 // Attempt to use the path from InstallPath as stored in the registry.
811 char *syslibname = py3_get_system_libname(libname);
812
813 if (syslibname != NULL)
814 {
815 hinstPy3 = load_dll(syslibname);
816 vim_free(syslibname);
817 }
818 }
819# endif
820
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200821 if (!hinstPy3)
822 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200823 if (verbose)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000824 semsg(_(e_could_not_load_library_str_str), libname, load_dll_error());
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200825 return FAIL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200826 }
827
828 for (i = 0; py3_funcname_table[i].ptr; ++i)
829 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200830 if ((*py3_funcname_table[i].ptr = symbol_from_dll(hinstPy3,
831 py3_funcname_table[i].name)) == NULL)
832 {
833 close_dll(hinstPy3);
834 hinstPy3 = 0;
835 if (verbose)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000836 semsg(_(e_could_not_load_library_function_str), py3_funcname_table[i].name);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200837 return FAIL;
838 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200839 }
840
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100841 // Load unicode functions separately as only the ucs2 or the ucs4 functions
842 // will be present in the library.
Bram Moolenaar9c9cbf12012-10-23 05:17:37 +0200843# if PY_VERSION_HEX >= 0x030300f0
Bram Moolenaar7b24ce02018-03-29 18:15:26 +0200844 *ucs_from_string = symbol_from_dll(hinstPy3, "PyUnicode_FromString");
845 *ucs_decode = symbol_from_dll(hinstPy3, "PyUnicode_Decode");
846 *ucs_as_encoded_string = symbol_from_dll(hinstPy3,
Bram Moolenaar7bc4f932012-10-14 03:22:56 +0200847 "PyUnicode_AsEncodedString");
Bram Moolenaar9c9cbf12012-10-23 05:17:37 +0200848# else
Bram Moolenaar7b24ce02018-03-29 18:15:26 +0200849 *ucs_from_string = symbol_from_dll(hinstPy3, "PyUnicodeUCS2_FromString");
850 *ucs_decode = symbol_from_dll(hinstPy3,
Bram Moolenaar19e60942011-06-19 00:27:51 +0200851 "PyUnicodeUCS2_Decode");
Bram Moolenaar7b24ce02018-03-29 18:15:26 +0200852 *ucs_as_encoded_string = symbol_from_dll(hinstPy3,
Bram Moolenaar19e60942011-06-19 00:27:51 +0200853 "PyUnicodeUCS2_AsEncodedString");
Bram Moolenaar7b24ce02018-03-29 18:15:26 +0200854 if (*ucs_from_string == NULL || *ucs_decode == NULL
855 || *ucs_as_encoded_string == NULL)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200856 {
Bram Moolenaar7b24ce02018-03-29 18:15:26 +0200857 *ucs_from_string = symbol_from_dll(hinstPy3,
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200858 "PyUnicodeUCS4_FromString");
Bram Moolenaar7b24ce02018-03-29 18:15:26 +0200859 *ucs_decode = symbol_from_dll(hinstPy3,
Bram Moolenaar19e60942011-06-19 00:27:51 +0200860 "PyUnicodeUCS4_Decode");
Bram Moolenaar7b24ce02018-03-29 18:15:26 +0200861 *ucs_as_encoded_string = symbol_from_dll(hinstPy3,
Bram Moolenaar19e60942011-06-19 00:27:51 +0200862 "PyUnicodeUCS4_AsEncodedString");
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200863 }
Bram Moolenaar9c9cbf12012-10-23 05:17:37 +0200864# endif
Bram Moolenaar7b24ce02018-03-29 18:15:26 +0200865 if (*ucs_from_string == NULL || *ucs_decode == NULL
866 || *ucs_as_encoded_string == NULL)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200867 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200868 close_dll(hinstPy3);
869 hinstPy3 = 0;
870 if (verbose)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000871 semsg(_(e_could_not_load_library_function_str), "PyUnicode_UCSX_*");
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200872 return FAIL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200873 }
874
875 return OK;
876}
877
878/*
879 * If python is enabled (there is installed python on Windows system) return
880 * TRUE, else FALSE.
881 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200882 int
883python3_enabled(int verbose)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200884{
Bram Moolenaar25e4fcd2016-01-09 14:57:47 +0100885 return py3_runtime_link_init((char *)p_py3dll, verbose) == OK;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200886}
887
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100888/*
889 * Load the standard Python exceptions - don't import the symbols from the
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200890 * DLL, as this can cause errors (importing data symbols is not reliable).
891 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200892 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100893get_py3_exceptions(void)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200894{
895 PyObject *exmod = PyImport_ImportModule("builtins");
896 PyObject *exdict = PyModule_GetDict(exmod);
897 p3imp_PyExc_AttributeError = PyDict_GetItemString(exdict, "AttributeError");
898 p3imp_PyExc_IndexError = PyDict_GetItemString(exdict, "IndexError");
Bram Moolenaaraf6abb92013-04-24 13:04:26 +0200899 p3imp_PyExc_KeyError = PyDict_GetItemString(exdict, "KeyError");
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200900 p3imp_PyExc_KeyboardInterrupt = PyDict_GetItemString(exdict, "KeyboardInterrupt");
901 p3imp_PyExc_TypeError = PyDict_GetItemString(exdict, "TypeError");
902 p3imp_PyExc_ValueError = PyDict_GetItemString(exdict, "ValueError");
Bram Moolenaar41009372013-07-01 22:03:04 +0200903 p3imp_PyExc_SystemExit = PyDict_GetItemString(exdict, "SystemExit");
Bram Moolenaar8661b172013-05-15 15:44:28 +0200904 p3imp_PyExc_RuntimeError = PyDict_GetItemString(exdict, "RuntimeError");
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200905 p3imp_PyExc_ImportError = PyDict_GetItemString(exdict, "ImportError");
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200906 p3imp_PyExc_OverflowError = PyDict_GetItemString(exdict, "OverflowError");
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200907 Py_XINCREF(p3imp_PyExc_AttributeError);
908 Py_XINCREF(p3imp_PyExc_IndexError);
Bram Moolenaaraf6abb92013-04-24 13:04:26 +0200909 Py_XINCREF(p3imp_PyExc_KeyError);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200910 Py_XINCREF(p3imp_PyExc_KeyboardInterrupt);
911 Py_XINCREF(p3imp_PyExc_TypeError);
912 Py_XINCREF(p3imp_PyExc_ValueError);
Bram Moolenaar41009372013-07-01 22:03:04 +0200913 Py_XINCREF(p3imp_PyExc_SystemExit);
Bram Moolenaar8661b172013-05-15 15:44:28 +0200914 Py_XINCREF(p3imp_PyExc_RuntimeError);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200915 Py_XINCREF(p3imp_PyExc_ImportError);
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200916 Py_XINCREF(p3imp_PyExc_OverflowError);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200917 Py_XDECREF(exmod);
918}
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100919#endif // DYNAMIC_PYTHON3
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200920
Bram Moolenaardb913952012-06-29 12:54:53 +0200921static int py3initialised = 0;
Bram Moolenaardb913952012-06-29 12:54:53 +0200922#define PYINITIALISED py3initialised
Bram Moolenaarc4f83382017-07-07 14:50:44 +0200923static int python_end_called = FALSE;
Bram Moolenaardb913952012-06-29 12:54:53 +0200924
Bram Moolenaar774267b2013-05-21 20:51:59 +0200925#define DESTRUCTOR_FINISH(self) Py_TYPE(self)->tp_free((PyObject*)self)
Bram Moolenaardb913952012-06-29 12:54:53 +0200926
Bram Moolenaar971db462013-05-12 18:44:48 +0200927#define WIN_PYTHON_REF(win) win->w_python3_ref
928#define BUF_PYTHON_REF(buf) buf->b_python3_ref
Bram Moolenaar5e538ec2013-05-15 15:12:29 +0200929#define TAB_PYTHON_REF(tab) tab->tp_python3_ref
Bram Moolenaar971db462013-05-12 18:44:48 +0200930
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200931 static void
932call_PyObject_Free(void *p)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200933{
Bram Moolenaar0014a532013-05-29 21:33:39 +0200934#if defined(Py_DEBUG) && !defined(Py_DEBUG_NO_PYMALLOC)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200935 _PyObject_DebugFree(p);
936#else
937 PyObject_Free(p);
938#endif
939}
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200940
941 static PyObject *
942call_PyType_GenericNew(PyTypeObject *type, PyObject *args, PyObject *kwds)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200943{
944 return PyType_GenericNew(type,args,kwds);
945}
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200946
947 static PyObject *
948call_PyType_GenericAlloc(PyTypeObject *type, Py_ssize_t nitems)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200949{
950 return PyType_GenericAlloc(type,nitems);
951}
952
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200953static PyObject *OutputGetattro(PyObject *, PyObject *);
954static int OutputSetattro(PyObject *, PyObject *, PyObject *);
955static PyObject *BufferGetattro(PyObject *, PyObject *);
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200956static int BufferSetattro(PyObject *, PyObject *, PyObject *);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +0200957static PyObject *TabPageGetattro(PyObject *, PyObject *);
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200958static PyObject *WindowGetattro(PyObject *, PyObject *);
959static int WindowSetattro(PyObject *, PyObject *, PyObject *);
960static PyObject *RangeGetattro(PyObject *, PyObject *);
961static PyObject *CurrentGetattro(PyObject *, PyObject *);
962static int CurrentSetattro(PyObject *, PyObject *, PyObject *);
963static PyObject *DictionaryGetattro(PyObject *, PyObject *);
964static int DictionarySetattro(PyObject *, PyObject *, PyObject *);
965static PyObject *ListGetattro(PyObject *, PyObject *);
966static int ListSetattro(PyObject *, PyObject *, PyObject *);
967static PyObject *FunctionGetattro(PyObject *, PyObject *);
968
969static struct PyModuleDef vimmodule;
970
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +0200971#define PY_CAN_RECURSE
972
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200973/*
974 * Include the code shared with if_python.c
975 */
976#include "if_py_both.h"
977
Bram Moolenaar828bff12019-03-19 22:11:41 +0100978// NOTE: Must always be used at the start of a block, since it declares "name".
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200979#define GET_ATTR_STRING(name, nameobj) \
980 char *name = ""; \
981 if (PyUnicode_Check(nameobj)) \
Bram Moolenaar828bff12019-03-19 22:11:41 +0100982 name = (char *)_PyUnicode_AsString(nameobj)
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200983
984#define PY3OBJ_DELETED(obj) (obj->ob_base.ob_refcnt<=0)
985
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100986///////////////////////////////////////////////////////
987// Internal function prototypes.
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200988
Bram Moolenaar7854e3a2012-11-28 15:33:14 +0100989static PyObject *Py3Init_vim(void);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200990
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100991///////////////////////////////////////////////////////
992// 1. Python interpreter main program.
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200993
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200994 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100995python3_end(void)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200996{
997 static int recurse = 0;
998
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100999 // If a crash occurs while doing this, don't try again.
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001000 if (recurse != 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001001 return;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001002
Bram Moolenaarc4f83382017-07-07 14:50:44 +02001003 python_end_called = TRUE;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001004 ++recurse;
1005
1006#ifdef DYNAMIC_PYTHON3
1007 if (hinstPy3)
1008#endif
1009 if (Py_IsInitialized())
1010 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001011 // acquire lock before finalizing
Bram Moolenaar71700b82013-05-15 17:49:05 +02001012 PyGILState_Ensure();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001013
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001014 Py_Finalize();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001015 }
1016
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001017 --recurse;
1018}
1019
Bram Moolenaar094454f2015-10-07 10:39:55 +02001020#if (defined(DYNAMIC_PYTHON3) && defined(DYNAMIC_PYTHON) && defined(FEAT_PYTHON) && defined(UNIX)) || defined(PROTO)
Bram Moolenaar4c3a3262010-07-24 15:42:14 +02001021 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001022python3_loaded(void)
Bram Moolenaar4c3a3262010-07-24 15:42:14 +02001023{
1024 return (hinstPy3 != 0);
1025}
1026#endif
1027
Bram Moolenaar94073162018-01-31 21:49:05 +01001028static wchar_t *py_home_buf = NULL;
1029
Bram Moolenaar56b8dc32020-08-06 21:47:11 +02001030#if defined(MSWIN) && (PY_VERSION_HEX >= 0x030500f0)
Bram Moolenaarc6ed2542020-10-10 23:26:28 +02001031/*
1032 * Return TRUE if stdin is readable from Python 3.
1033 */
1034 static BOOL
1035is_stdin_readable(void)
1036{
1037 DWORD mode, eventnum;
1038 struct _stat st;
1039 int fd = fileno(stdin);
1040 HANDLE hstdin = (HANDLE)_get_osfhandle(fd);
1041
1042 // Check if stdin is connected to the console.
1043 if (GetConsoleMode(hstdin, &mode))
1044 // Check if it is opened as input.
1045 return GetNumberOfConsoleInputEvents(hstdin, &eventnum);
1046
1047 return _fstat(fd, &st) == 0;
1048}
1049
1050// Python 3.5 or later will abort inside Py_Initialize() when stdin has
1051// been closed (i.e. executed by "vim -"). Reconnect stdin to CONIN$.
Bram Moolenaar56b8dc32020-08-06 21:47:11 +02001052// Note that the python DLL is linked to its own stdio DLL which can be
1053// differ from Vim's stdio.
1054 static void
1055reset_stdin(void)
1056{
1057 FILE *(*py__acrt_iob_func)(unsigned) = NULL;
1058 FILE *(*pyfreopen)(const char *, const char *, FILE *) = NULL;
Bram Moolenaar63ff72a2022-02-07 13:54:01 +00001059 HINSTANCE hinst = hinstPy3;
Bram Moolenaar56b8dc32020-08-06 21:47:11 +02001060
Bram Moolenaarc6ed2542020-10-10 23:26:28 +02001061 if (hinst == NULL || is_stdin_readable())
Bram Moolenaar56b8dc32020-08-06 21:47:11 +02001062 return;
1063
1064 // Get "freopen" and "stdin" which are used in the python DLL.
1065 // "stdin" is defined as "__acrt_iob_func(0)" in VC++ 2015 or later.
1066 py__acrt_iob_func = get_dll_import_func(hinst, "__acrt_iob_func");
1067 if (py__acrt_iob_func)
1068 {
1069 HINSTANCE hpystdiodll = find_imported_module_by_funcname(hinst,
Bram Moolenaar253b16a2020-10-06 19:59:06 +02001070 "__acrt_iob_func");
Bram Moolenaar56b8dc32020-08-06 21:47:11 +02001071 if (hpystdiodll)
Bram Moolenaar253b16a2020-10-06 19:59:06 +02001072 pyfreopen = (void *)GetProcAddress(hpystdiodll, "freopen");
Bram Moolenaar56b8dc32020-08-06 21:47:11 +02001073 }
1074
Bram Moolenaarc6ed2542020-10-10 23:26:28 +02001075 // Reconnect stdin to CONIN$.
Bram Moolenaar253b16a2020-10-06 19:59:06 +02001076 if (pyfreopen != NULL)
Bram Moolenaarc6ed2542020-10-10 23:26:28 +02001077 pyfreopen("CONIN$", "r", py__acrt_iob_func(0));
Bram Moolenaar56b8dc32020-08-06 21:47:11 +02001078 else
Bram Moolenaarc6ed2542020-10-10 23:26:28 +02001079 freopen("CONIN$", "r", stdin);
Bram Moolenaar56b8dc32020-08-06 21:47:11 +02001080}
1081#else
1082# define reset_stdin()
1083#endif
1084
Bram Moolenaar63ff72a2022-02-07 13:54:01 +00001085// Python 3.2 or later will abort inside Py_Initialize() when mandatory
1086// modules cannot be loaded (e.g. 'pythonthreehome' is wrongly set.).
1087// Install a hook to python dll's exit() and recover from it.
1088#if defined(MSWIN) && (PY_VERSION_HEX >= 0x030200f0)
1089# define HOOK_EXIT
1090# include <setjmp.h>
1091
1092static jmp_buf exit_hook_jump_buf;
1093static void *orig_exit = NULL;
1094
1095/*
1096 * Function that replaces exit() while calling Py_Initialize().
1097 */
1098 static void
1099hooked_exit(int ret)
1100{
1101 // Recover from exit.
1102 longjmp(exit_hook_jump_buf, 1);
1103}
1104
1105/*
1106 * Install a hook to python dll's exit().
1107 */
1108 static void
1109hook_py_exit(void)
1110{
1111 HINSTANCE hinst = hinstPy3;
1112
1113 if (hinst == NULL || orig_exit != NULL)
1114 return;
1115
1116 orig_exit = hook_dll_import_func(hinst, "exit", (void *)hooked_exit);
1117}
1118
1119/*
1120 * Remove the hook installed by hook_py_exit().
1121 */
1122 static void
1123restore_py_exit(void)
1124{
1125 HINSTANCE hinst = hinstPy3;
1126
1127 if (hinst == NULL)
1128 return;
1129
1130 if (orig_exit != NULL)
1131 hook_dll_import_func(hinst, "exit", orig_exit);
1132 orig_exit = NULL;
1133}
1134#endif
1135
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001136 static int
1137Python3_Init(void)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001138{
1139 if (!py3initialised)
1140 {
1141#ifdef DYNAMIC_PYTHON3
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001142 if (!python3_enabled(TRUE))
1143 {
Bram Moolenaar9a846fb2022-01-01 21:59:18 +00001144 emsg(_(e_sorry_this_command_is_disabled_python_library_could_not_be_found));
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001145 goto fail;
1146 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001147#endif
1148
1149 init_structs();
1150
Bram Moolenaar94073162018-01-31 21:49:05 +01001151 if (*p_py3home != NUL)
1152 {
1153 size_t len = mbstowcs(NULL, (char *)p_py3home, 0) + 1;
Bram Moolenaar644d37b2010-11-16 19:26:02 +01001154
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001155 // The string must not change later, make a copy in static memory.
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001156 py_home_buf = ALLOC_MULT(wchar_t, len);
Bram Moolenaar94073162018-01-31 21:49:05 +01001157 if (py_home_buf != NULL && mbstowcs(
1158 py_home_buf, (char *)p_py3home, len) != (size_t)-1)
1159 Py_SetPythonHome(py_home_buf);
1160 }
Bram Moolenaar644d37b2010-11-16 19:26:02 +01001161#ifdef PYTHON3_HOME
Bram Moolenaar94073162018-01-31 21:49:05 +01001162 else if (mch_getenv((char_u *)"PYTHONHOME") == NULL)
Bram Moolenaar10005652015-12-31 21:03:23 +01001163 Py_SetPythonHome(PYTHON3_HOME);
Bram Moolenaar644d37b2010-11-16 19:26:02 +01001164#endif
1165
Bram Moolenaar7bc4f932012-10-14 03:22:56 +02001166 PyImport_AppendInittab("vim", Py3Init_vim);
1167
Bram Moolenaar63ff72a2022-02-07 13:54:01 +00001168#if !defined(DYNAMIC_PYTHON3) && defined(MSWIN)
1169 hinstPy3 = GetModuleHandle(PYTHON3_DLL);
1170#endif
Bram Moolenaar56b8dc32020-08-06 21:47:11 +02001171 reset_stdin();
Bram Moolenaar63ff72a2022-02-07 13:54:01 +00001172
1173#ifdef HOOK_EXIT
1174 // Catch exit() called in Py_Initialize().
1175 hook_py_exit();
1176 if (setjmp(exit_hook_jump_buf) == 0)
Bram Moolenaar63ff72a2022-02-07 13:54:01 +00001177 {
1178 Py_Initialize();
Bram Moolenaar63ff72a2022-02-07 13:54:01 +00001179 restore_py_exit();
Bram Moolenaar63ff72a2022-02-07 13:54:01 +00001180 }
Bram Moolenaar63ff72a2022-02-07 13:54:01 +00001181 else
1182 {
1183 // exit() was called in Py_Initialize().
1184 restore_py_exit();
1185 emsg(_(e_critical_error_in_python3_initialization_check_your_installation));
1186 goto fail;
1187 }
K.Takataa7fbaa42022-12-26 14:46:51 +00001188#else
1189 Py_Initialize();
Bram Moolenaar63ff72a2022-02-07 13:54:01 +00001190#endif
Bram Moolenaard0573012017-10-28 21:11:06 +02001191
Bram Moolenaarefc0d942020-10-11 18:05:02 +02001192#if PY_VERSION_HEX < 0x03090000
1193 // Initialise threads. This is deprecated since Python 3.9.
Bram Moolenaar456f2bb2011-06-12 21:37:13 +02001194 PyEval_InitThreads();
Bram Moolenaarefc0d942020-10-11 18:05:02 +02001195#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001196#ifdef DYNAMIC_PYTHON3
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001197 get_py3_exceptions();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001198#endif
1199
Bram Moolenaar1dc28782013-05-21 19:11:01 +02001200 if (PythonIO_Init_io())
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001201 goto fail;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001202
Bram Moolenaardb913952012-06-29 12:54:53 +02001203 globals = PyModule_GetDict(PyImport_AddModule("__main__"));
1204
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001205 // Remove the element from sys.path that was added because of our
1206 // argv[0] value in Py3Init_vim(). Previously we used an empty
1207 // string, but depending on the OS we then get an empty entry or
1208 // the current directory in sys.path.
1209 // Only after vim has been imported, the element does exist in
1210 // sys.path.
Bram Moolenaar19e60942011-06-19 00:27:51 +02001211 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 +02001212
Bram Moolenaarefc0d942020-10-11 18:05:02 +02001213 // Without the call to PyEval_SaveThread, thread specific state (such
1214 // as the system trace hook), will be lost between invocations of
1215 // Python code.
1216 // GIL may have been created and acquired in PyEval_InitThreads() and
1217 // thread state is created in Py_Initialize(); there
1218 // _PyGILState_NoteThreadState() also sets gilcounter to 1 (python must
1219 // have threads enabled!), so the following does both: unlock GIL and
1220 // save thread state in TLS without deleting thread state
Bram Moolenaar76d711c2013-02-13 14:17:08 +01001221 PyEval_SaveThread();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001222
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001223 py3initialised = 1;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001224 }
1225
1226 return 0;
1227
1228fail:
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001229 // We call PythonIO_Flush() here to print any Python errors.
1230 // This is OK, as it is possible to call this function even
1231 // if PythonIO_Init_io() has not completed successfully (it will
1232 // not do anything in this case).
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001233 PythonIO_Flush();
1234 return -1;
1235}
1236
1237/*
1238 * External interface
1239 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001240 static void
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02001241DoPyCommand(const char *cmd, rangeinitializer init_range, runner run, void *arg)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001242{
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001243#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001244 char *saved_locale;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001245#endif
Bram Moolenaar19e60942011-06-19 00:27:51 +02001246 PyObject *cmdstr;
1247 PyObject *cmdbytes;
Bram Moolenaar71700b82013-05-15 17:49:05 +02001248 PyGILState_STATE pygilstate;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001249
Bram Moolenaarc4f83382017-07-07 14:50:44 +02001250 if (python_end_called)
1251 goto theend;
1252
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001253 if (Python3_Init())
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001254 goto theend;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001255
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02001256 init_range(arg);
1257
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001258 Python_Release_Vim(); // leave Vim
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001259
1260#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001261 // Python only works properly when the LC_NUMERIC locale is "C".
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001262 saved_locale = setlocale(LC_NUMERIC, NULL);
1263 if (saved_locale == NULL || STRCMP(saved_locale, "C") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001264 saved_locale = NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001265 else
1266 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001267 // Need to make a copy, value may change when setting new locale.
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001268 saved_locale = (char *)vim_strsave((char_u *)saved_locale);
1269 (void)setlocale(LC_NUMERIC, "C");
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001270 }
1271#endif
1272
1273 pygilstate = PyGILState_Ensure();
1274
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001275 // PyRun_SimpleString expects a UTF-8 string. Wrong encoding may cause
1276 // SyntaxError (unicode error).
Bram Moolenaar3d64a312011-07-15 15:54:44 +02001277 cmdstr = PyUnicode_Decode(cmd, strlen(cmd),
Bram Moolenaar2e2f52a2020-12-21 16:03:02 +01001278 (char *)ENC_OPT, ERRORS_DECODE_ARG);
1279 cmdbytes = PyUnicode_AsEncodedString(cmdstr, "utf-8", ERRORS_ENCODE_ARG);
Bram Moolenaar19e60942011-06-19 00:27:51 +02001280 Py_XDECREF(cmdstr);
Bram Moolenaardb913952012-06-29 12:54:53 +02001281
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02001282 run(PyBytes_AsString(cmdbytes), arg, &pygilstate);
Bram Moolenaar19e60942011-06-19 00:27:51 +02001283 Py_XDECREF(cmdbytes);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001284
1285 PyGILState_Release(pygilstate);
1286
1287#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
1288 if (saved_locale != NULL)
1289 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001290 (void)setlocale(LC_NUMERIC, saved_locale);
1291 vim_free(saved_locale);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001292 }
1293#endif
1294
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001295 Python_Lock_Vim(); // enter Vim
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001296 PythonIO_Flush();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001297
1298theend:
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001299 return; // keeps lint happy
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001300}
1301
1302/*
Bram Moolenaar368373e2010-07-19 20:46:22 +02001303 * ":py3"
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001304 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001305 void
1306ex_py3(exarg_T *eap)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001307{
1308 char_u *script;
1309
1310 script = script_get(eap, eap->arg);
1311 if (!eap->skip)
1312 {
Bram Moolenaar14816ad2019-02-18 22:04:56 +01001313 if (p_pyx == 0)
1314 p_pyx = 3;
1315
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02001316 DoPyCommand(script == NULL ? (char *) eap->arg : (char *) script,
1317 (rangeinitializer) init_range_cmd,
1318 (runner) run_cmd,
1319 (void *) eap);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001320 }
1321 vim_free(script);
1322}
1323
1324#define BUFFER_SIZE 2048
1325
1326/*
Bram Moolenaar6df6f472010-07-18 18:04:50 +02001327 * ":py3file"
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001328 */
1329 void
1330ex_py3file(exarg_T *eap)
1331{
1332 static char buffer[BUFFER_SIZE];
1333 const char *file;
1334 char *p;
1335 int i;
1336
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01001337 if (p_pyx == 0)
1338 p_pyx = 3;
1339
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001340 // Have to do it like this. PyRun_SimpleFile requires you to pass a
1341 // stdio file pointer, but Vim and the Python DLL are compiled with
1342 // different options under Windows, meaning that stdio pointers aren't
1343 // compatible between the two. Yuk.
1344 //
1345 // construct: exec(compile(open('a_filename', 'rb').read(), 'a_filename', 'exec'))
1346 //
1347 // Using bytes so that Python can detect the source encoding as it normally
1348 // does. The doc does not say "compile" accept bytes, though.
1349 //
1350 // We need to escape any backslashes or single quotes in the file name, so that
1351 // Python won't mangle the file name.
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001352
1353 strcpy(buffer, "exec(compile(open('");
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001354 p = buffer + 19; // size of "exec(compile(open('"
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001355
1356 for (i=0; i<2; ++i)
1357 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001358 file = (char *)eap->arg;
1359 while (*file && p < buffer + (BUFFER_SIZE - 3))
1360 {
1361 if (*file == '\\' || *file == '\'')
1362 *p++ = '\\';
1363 *p++ = *file++;
1364 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001365 // If we didn't finish the file name, we hit a buffer overflow
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001366 if (*file != '\0')
1367 return;
1368 if (i==0)
1369 {
Bram Moolenaar19e60942011-06-19 00:27:51 +02001370 strcpy(p,"','rb').read(),'");
1371 p += 16;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001372 }
1373 else
1374 {
1375 strcpy(p,"','exec'))");
1376 p += 10;
1377 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001378 }
1379
1380
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001381 // Execute the file
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02001382 DoPyCommand(buffer,
1383 (rangeinitializer) init_range_cmd,
1384 (runner) run_cmd,
1385 (void *) eap);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001386}
1387
Bram Moolenaar54e8f002013-05-15 19:44:39 +02001388 void
1389ex_py3do(exarg_T *eap)
Bram Moolenaar3dab2802013-05-15 18:28:13 +02001390{
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01001391 if (p_pyx == 0)
1392 p_pyx = 3;
1393
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02001394 DoPyCommand((char *)eap->arg,
1395 (rangeinitializer)init_range_cmd,
1396 (runner)run_do,
1397 (void *)eap);
Bram Moolenaar3dab2802013-05-15 18:28:13 +02001398}
1399
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001400///////////////////////////////////////////////////////
1401// 2. Python output stream: writes output via [e]msg().
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001402
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001403// Implementation functions
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001404
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001405 static PyObject *
1406OutputGetattro(PyObject *self, PyObject *nameobj)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001407{
Bram Moolenaar77045652012-09-21 13:46:06 +02001408 GET_ATTR_STRING(name, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001409
1410 if (strcmp(name, "softspace") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001411 return PyLong_FromLong(((OutputObject *)(self))->softspace);
Bram Moolenaar6d4431e2016-04-21 20:00:56 +02001412 else if (strcmp(name, "errors") == 0)
1413 return PyString_FromString("strict");
1414 else if (strcmp(name, "encoding") == 0)
1415 return PyString_FromString(ENC_OPT);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001416
1417 return PyObject_GenericGetAttr(self, nameobj);
1418}
1419
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001420 static int
1421OutputSetattro(PyObject *self, PyObject *nameobj, PyObject *val)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001422{
Bram Moolenaar77045652012-09-21 13:46:06 +02001423 GET_ATTR_STRING(name, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001424
Bram Moolenaard6e39182013-05-21 18:30:34 +02001425 return OutputSetattr((OutputObject *)(self), name, val);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001426}
1427
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001428///////////////////////////////////////////////////////
1429// 3. Implementation of the Vim module for Python
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001430
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001431// Window type - Implementation functions
1432// --------------------------------------
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001433
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001434#define WindowType_Check(obj) ((obj)->ob_base.ob_type == &WindowType)
1435
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001436// Buffer type - Implementation functions
1437// --------------------------------------
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001438
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001439#define BufferType_Check(obj) ((obj)->ob_base.ob_type == &BufferType)
1440
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001441static PyObject* BufferSubscript(PyObject *self, PyObject *idx);
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02001442static int BufferAsSubscript(PyObject *self, PyObject *idx, PyObject *val);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001443
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001444// Line range type - Implementation functions
1445// --------------------------------------
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001446
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001447#define RangeType_Check(obj) ((obj)->ob_base.ob_type == &RangeType)
1448
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001449static PyObject* RangeSubscript(PyObject *self, PyObject *idx);
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02001450static int RangeAsItem(PyObject *, Py_ssize_t, PyObject *);
1451static int RangeAsSubscript(PyObject *self, PyObject *idx, PyObject *val);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001452
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001453// Current objects type - Implementation functions
1454// -----------------------------------------------
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001455
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001456static PySequenceMethods BufferAsSeq = {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001457 (lenfunc) BufferLength, // sq_length, len(x)
1458 (binaryfunc) 0, // sq_concat, x+y
1459 (ssizeargfunc) 0, // sq_repeat, x*n
1460 (ssizeargfunc) BufferItem, // sq_item, x[i]
1461 0, // was_sq_slice, x[i:j]
1462 0, // sq_ass_item, x[i]=v
1463 0, // sq_ass_slice, x[i:j]=v
1464 0, // sq_contains
1465 0, // sq_inplace_concat
1466 0, // sq_inplace_repeat
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001467};
1468
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001469static PyMappingMethods BufferAsMapping = {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001470 /* mp_length */ (lenfunc)BufferLength,
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001471 /* mp_subscript */ (binaryfunc)BufferSubscript,
Bram Moolenaar19e60942011-06-19 00:27:51 +02001472 /* mp_ass_subscript */ (objobjargproc)BufferAsSubscript,
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001473};
1474
1475
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001476// Buffer object
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001477
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001478 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02001479BufferGetattro(PyObject *self, PyObject *nameobj)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001480{
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001481 PyObject *r;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001482
Bram Moolenaar77045652012-09-21 13:46:06 +02001483 GET_ATTR_STRING(name, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001484
Bram Moolenaar9e822c02013-05-29 22:15:30 +02001485 if ((r = BufferAttrValid((BufferObject *)(self), name)))
1486 return r;
1487
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001488 if (CheckBuffer((BufferObject *)(self)))
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001489 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001490
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001491 r = BufferAttr((BufferObject *)(self), name);
1492 if (r || PyErr_Occurred())
1493 return r;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001494 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001495 return PyObject_GenericGetAttr(self, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001496}
1497
Bram Moolenaare9ba5162013-05-29 22:02:22 +02001498 static int
1499BufferSetattro(PyObject *self, PyObject *nameobj, PyObject *val)
1500{
1501 GET_ATTR_STRING(name, nameobj);
1502
1503 return BufferSetattr((BufferObject *)(self), name, val);
1504}
1505
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001506//////////////////
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001507
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001508 static PyObject *
1509BufferSubscript(PyObject *self, PyObject* idx)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001510{
Bram Moolenaardb913952012-06-29 12:54:53 +02001511 if (PyLong_Check(idx))
1512 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001513 long _idx = PyLong_AsLong(idx);
Bram Moolenaard6e39182013-05-21 18:30:34 +02001514 return BufferItem((BufferObject *)(self), _idx);
Bram Moolenaarebfec1c2023-01-22 21:14:53 +00001515 }
1516 else if (PySlice_Check(idx))
Bram Moolenaardb913952012-06-29 12:54:53 +02001517 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001518 Py_ssize_t start, stop, step, slicelen;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001519
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02001520 if (CheckBuffer((BufferObject *) self))
1521 return NULL;
1522
Bram Moolenaar922a4662014-03-30 16:11:43 +02001523 if (PySlice_GetIndicesEx((PySliceObject_T *)idx,
Bram Moolenaarbd80f352013-05-12 21:16:23 +02001524 (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count,
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001525 &start, &stop,
Bram Moolenaardb913952012-06-29 12:54:53 +02001526 &step, &slicelen) < 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001527 return NULL;
Bram Moolenaard6e39182013-05-21 18:30:34 +02001528 return BufferSlice((BufferObject *)(self), start, stop);
Bram Moolenaardb913952012-06-29 12:54:53 +02001529 }
1530 else
1531 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001532 RAISE_INVALID_INDEX_TYPE(idx);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001533 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001534 }
1535}
1536
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02001537 static int
Bram Moolenaar19e60942011-06-19 00:27:51 +02001538BufferAsSubscript(PyObject *self, PyObject* idx, PyObject* val)
1539{
Bram Moolenaardb913952012-06-29 12:54:53 +02001540 if (PyLong_Check(idx))
1541 {
Bram Moolenaar19e60942011-06-19 00:27:51 +02001542 long n = PyLong_AsLong(idx);
Bram Moolenaarab589462020-07-06 21:03:06 +02001543
1544 if (CheckBuffer((BufferObject *) self))
1545 return -1;
1546
Bram Moolenaar19e60942011-06-19 00:27:51 +02001547 return RBAsItem((BufferObject *)(self), n, val, 1,
1548 (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count,
1549 NULL);
Bram Moolenaarebfec1c2023-01-22 21:14:53 +00001550 }
1551 else if (PySlice_Check(idx))
Bram Moolenaardb913952012-06-29 12:54:53 +02001552 {
Bram Moolenaar19e60942011-06-19 00:27:51 +02001553 Py_ssize_t start, stop, step, slicelen;
1554
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02001555 if (CheckBuffer((BufferObject *) self))
1556 return -1;
1557
Bram Moolenaar922a4662014-03-30 16:11:43 +02001558 if (PySlice_GetIndicesEx((PySliceObject_T *)idx,
Bram Moolenaarbd80f352013-05-12 21:16:23 +02001559 (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count,
Bram Moolenaar19e60942011-06-19 00:27:51 +02001560 &start, &stop,
Bram Moolenaardb913952012-06-29 12:54:53 +02001561 &step, &slicelen) < 0)
Bram Moolenaar19e60942011-06-19 00:27:51 +02001562 return -1;
Bram Moolenaar19e60942011-06-19 00:27:51 +02001563 return RBAsSlice((BufferObject *)(self), start, stop, val, 1,
1564 (PyInt)((BufferObject *)(self))->buf->b_ml.ml_line_count,
1565 NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02001566 }
1567 else
1568 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001569 RAISE_INVALID_INDEX_TYPE(idx);
Bram Moolenaar19e60942011-06-19 00:27:51 +02001570 return -1;
1571 }
1572}
1573
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001574static PySequenceMethods RangeAsSeq = {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001575 (lenfunc) RangeLength, // sq_length, len(x)
1576 (binaryfunc) 0, // RangeConcat, sq_concat, x+y
1577 (ssizeargfunc) 0, // RangeRepeat, sq_repeat, x*n
1578 (ssizeargfunc) RangeItem, // sq_item, x[i]
1579 0, // was_sq_slice, x[i:j]
1580 (ssizeobjargproc) RangeAsItem, // sq_as_item, x[i]=v
1581 0, // sq_ass_slice, x[i:j]=v
1582 0, // sq_contains
1583 0, // sq_inplace_concat
1584 0, // sq_inplace_repeat
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001585};
1586
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001587static PyMappingMethods RangeAsMapping = {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001588 /* mp_length */ (lenfunc)RangeLength,
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001589 /* mp_subscript */ (binaryfunc)RangeSubscript,
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001590 /* mp_ass_subscript */ (objobjargproc)RangeAsSubscript,
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001591};
1592
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001593// Line range object - Implementation
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001594
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001595 static PyObject *
1596RangeGetattro(PyObject *self, PyObject *nameobj)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001597{
Bram Moolenaar77045652012-09-21 13:46:06 +02001598 GET_ATTR_STRING(name, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001599
1600 if (strcmp(name, "start") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001601 return Py_BuildValue("n", ((RangeObject *)(self))->start - 1);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001602 else if (strcmp(name, "end") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001603 return Py_BuildValue("n", ((RangeObject *)(self))->end - 1);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001604 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001605 return PyObject_GenericGetAttr(self, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001606}
1607
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001608////////////////
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001609
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02001610 static int
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001611RangeAsItem(PyObject *self, Py_ssize_t n, PyObject *val)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001612{
1613 return RBAsItem(((RangeObject *)(self))->buf, n, val,
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001614 ((RangeObject *)(self))->start,
1615 ((RangeObject *)(self))->end,
1616 &((RangeObject *)(self))->end);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001617}
1618
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001619 static Py_ssize_t
1620RangeAsSlice(PyObject *self, Py_ssize_t lo, Py_ssize_t hi, PyObject *val)
1621{
1622 return RBAsSlice(((RangeObject *)(self))->buf, lo, hi, val,
1623 ((RangeObject *)(self))->start,
1624 ((RangeObject *)(self))->end,
1625 &((RangeObject *)(self))->end);
1626}
1627
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001628 static PyObject *
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001629RangeSubscript(PyObject *self, PyObject* idx)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001630{
Bram Moolenaardb913952012-06-29 12:54:53 +02001631 if (PyLong_Check(idx))
1632 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001633 long _idx = PyLong_AsLong(idx);
Bram Moolenaard6e39182013-05-21 18:30:34 +02001634 return RangeItem((RangeObject *)(self), _idx);
Bram Moolenaarebfec1c2023-01-22 21:14:53 +00001635 }
1636 else if (PySlice_Check(idx))
Bram Moolenaardb913952012-06-29 12:54:53 +02001637 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001638 Py_ssize_t start, stop, step, slicelen;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001639
Bram Moolenaar922a4662014-03-30 16:11:43 +02001640 if (PySlice_GetIndicesEx((PySliceObject_T *)idx,
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001641 ((RangeObject *)(self))->end-((RangeObject *)(self))->start+1,
1642 &start, &stop,
Bram Moolenaardb913952012-06-29 12:54:53 +02001643 &step, &slicelen) < 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001644 return NULL;
Bram Moolenaard6e39182013-05-21 18:30:34 +02001645 return RangeSlice((RangeObject *)(self), start, stop);
Bram Moolenaardb913952012-06-29 12:54:53 +02001646 }
1647 else
1648 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001649 RAISE_INVALID_INDEX_TYPE(idx);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001650 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001651 }
1652}
1653
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02001654 static int
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001655RangeAsSubscript(PyObject *self, PyObject *idx, PyObject *val)
1656{
Bram Moolenaardb913952012-06-29 12:54:53 +02001657 if (PyLong_Check(idx))
1658 {
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001659 long n = PyLong_AsLong(idx);
1660 return RangeAsItem(self, n, val);
Bram Moolenaarabab0b02019-03-30 18:47:01 +01001661 }
1662 else if (PySlice_Check(idx))
Bram Moolenaardb913952012-06-29 12:54:53 +02001663 {
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001664 Py_ssize_t start, stop, step, slicelen;
1665
Bram Moolenaar922a4662014-03-30 16:11:43 +02001666 if (PySlice_GetIndicesEx((PySliceObject_T *)idx,
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001667 ((RangeObject *)(self))->end-((RangeObject *)(self))->start+1,
1668 &start, &stop,
Bram Moolenaardb913952012-06-29 12:54:53 +02001669 &step, &slicelen) < 0)
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001670 return -1;
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001671 return RangeAsSlice(self, start, stop, val);
Bram Moolenaardb913952012-06-29 12:54:53 +02001672 }
1673 else
1674 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001675 RAISE_INVALID_INDEX_TYPE(idx);
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001676 return -1;
1677 }
1678}
1679
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001680// TabPage object - Implementation
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001681
1682 static PyObject *
1683TabPageGetattro(PyObject *self, PyObject *nameobj)
1684{
1685 PyObject *r;
1686
1687 GET_ATTR_STRING(name, nameobj);
1688
Bram Moolenaar9e822c02013-05-29 22:15:30 +02001689 if ((r = TabPageAttrValid((TabPageObject *)(self), name)))
1690 return r;
1691
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001692 if (CheckTabPage((TabPageObject *)(self)))
1693 return NULL;
1694
1695 r = TabPageAttr((TabPageObject *)(self), name);
1696 if (r || PyErr_Occurred())
1697 return r;
1698 else
1699 return PyObject_GenericGetAttr(self, nameobj);
1700}
1701
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001702// Window object - Implementation
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001703
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001704 static PyObject *
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001705WindowGetattro(PyObject *self, PyObject *nameobj)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001706{
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001707 PyObject *r;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001708
Bram Moolenaar77045652012-09-21 13:46:06 +02001709 GET_ATTR_STRING(name, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001710
Bram Moolenaar9e822c02013-05-29 22:15:30 +02001711 if ((r = WindowAttrValid((WindowObject *)(self), name)))
1712 return r;
1713
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001714 if (CheckWindow((WindowObject *)(self)))
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001715 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001716
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001717 r = WindowAttr((WindowObject *)(self), name);
1718 if (r || PyErr_Occurred())
1719 return r;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001720 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001721 return PyObject_GenericGetAttr(self, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001722}
1723
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001724 static int
1725WindowSetattro(PyObject *self, PyObject *nameobj, PyObject *val)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001726{
Bram Moolenaar77045652012-09-21 13:46:06 +02001727 GET_ATTR_STRING(name, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001728
Bram Moolenaard6e39182013-05-21 18:30:34 +02001729 return WindowSetattr((WindowObject *)(self), name, val);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001730}
1731
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001732// Tab page list object - Definitions
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001733
1734static PySequenceMethods TabListAsSeq = {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001735 (lenfunc) TabListLength, // sq_length, len(x)
1736 (binaryfunc) 0, // sq_concat, x+y
1737 (ssizeargfunc) 0, // sq_repeat, x*n
1738 (ssizeargfunc) TabListItem, // sq_item, x[i]
1739 0, // sq_slice, x[i:j]
1740 (ssizeobjargproc)0, // sq_as_item, x[i]=v
1741 0, // sq_ass_slice, x[i:j]=v
1742 0, // sq_contains
1743 0, // sq_inplace_concat
1744 0, // sq_inplace_repeat
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001745};
1746
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001747// Window list object - Definitions
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001748
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001749static PySequenceMethods WinListAsSeq = {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001750 (lenfunc) WinListLength, // sq_length, len(x)
1751 (binaryfunc) 0, // sq_concat, x+y
1752 (ssizeargfunc) 0, // sq_repeat, x*n
1753 (ssizeargfunc) WinListItem, // sq_item, x[i]
1754 0, // sq_slice, x[i:j]
1755 (ssizeobjargproc)0, // sq_as_item, x[i]=v
1756 0, // sq_ass_slice, x[i:j]=v
1757 0, // sq_contains
1758 0, // sq_inplace_concat
1759 0, // sq_inplace_repeat
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001760};
1761
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001762/*
1763 * Current items object - Implementation
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001764 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001765 static PyObject *
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001766CurrentGetattro(PyObject *self, PyObject *nameobj)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001767{
Bram Moolenaardd8aca62013-05-29 22:36:10 +02001768 PyObject *r;
Bram Moolenaar77045652012-09-21 13:46:06 +02001769 GET_ATTR_STRING(name, nameobj);
Bram Moolenaardd8aca62013-05-29 22:36:10 +02001770 if (!(r = CurrentGetattr(self, name)))
1771 return PyObject_GenericGetAttr(self, nameobj);
1772 return r;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001773}
1774
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001775 static int
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001776CurrentSetattro(PyObject *self, PyObject *nameobj, PyObject *value)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001777{
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001778 GET_ATTR_STRING(name, nameobj);
1779 return CurrentSetattr(self, name, value);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001780}
1781
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001782// Dictionary object - Definitions
Bram Moolenaardb913952012-06-29 12:54:53 +02001783
Bram Moolenaar66b79852012-09-21 14:00:35 +02001784 static PyObject *
1785DictionaryGetattro(PyObject *self, PyObject *nameobj)
1786{
1787 DictionaryObject *this = ((DictionaryObject *) (self));
1788
1789 GET_ATTR_STRING(name, nameobj);
1790
1791 if (strcmp(name, "locked") == 0)
1792 return PyLong_FromLong(this->dict->dv_lock);
1793 else if (strcmp(name, "scope") == 0)
1794 return PyLong_FromLong(this->dict->dv_scope);
1795
1796 return PyObject_GenericGetAttr(self, nameobj);
1797}
1798
1799 static int
1800DictionarySetattro(PyObject *self, PyObject *nameobj, PyObject *val)
1801{
1802 GET_ATTR_STRING(name, nameobj);
Bram Moolenaard6e39182013-05-21 18:30:34 +02001803 return DictionarySetattr((DictionaryObject *)(self), name, val);
Bram Moolenaardb913952012-06-29 12:54:53 +02001804}
1805
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001806// List object - Definitions
Bram Moolenaardb913952012-06-29 12:54:53 +02001807
Bram Moolenaar66b79852012-09-21 14:00:35 +02001808 static PyObject *
1809ListGetattro(PyObject *self, PyObject *nameobj)
1810{
1811 GET_ATTR_STRING(name, nameobj);
1812
1813 if (strcmp(name, "locked") == 0)
1814 return PyLong_FromLong(((ListObject *) (self))->list->lv_lock);
1815
1816 return PyObject_GenericGetAttr(self, nameobj);
1817}
1818
1819 static int
1820ListSetattro(PyObject *self, PyObject *nameobj, PyObject *val)
1821{
1822 GET_ATTR_STRING(name, nameobj);
Bram Moolenaard6e39182013-05-21 18:30:34 +02001823 return ListSetattr((ListObject *)(self), name, val);
Bram Moolenaardb913952012-06-29 12:54:53 +02001824}
1825
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001826// Function object - Definitions
Bram Moolenaardb913952012-06-29 12:54:53 +02001827
Bram Moolenaardb913952012-06-29 12:54:53 +02001828 static PyObject *
1829FunctionGetattro(PyObject *self, PyObject *nameobj)
1830{
Bram Moolenaar8110a092016-04-14 15:56:09 +02001831 PyObject *r;
Bram Moolenaardb913952012-06-29 12:54:53 +02001832 FunctionObject *this = (FunctionObject *)(self);
Bram Moolenaar77045652012-09-21 13:46:06 +02001833
1834 GET_ATTR_STRING(name, nameobj);
Bram Moolenaardb913952012-06-29 12:54:53 +02001835
Bram Moolenaar8110a092016-04-14 15:56:09 +02001836 r = FunctionAttr(this, name);
1837 if (r || PyErr_Occurred())
1838 return r;
1839 else
1840 return PyObject_GenericGetAttr(self, nameobj);
Bram Moolenaardb913952012-06-29 12:54:53 +02001841}
1842
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001843// External interface
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001844
1845 void
1846python3_buffer_free(buf_T *buf)
1847{
Yegappan Lakshmanan0233bdf2023-01-12 12:33:30 +00001848 BufferObject *bp = BUF_PYTHON_REF(buf);
1849 if (bp == NULL)
1850 return;
1851 bp->buf = INVALID_BUFFER_VALUE;
1852 BUF_PYTHON_REF(buf) = NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001853}
1854
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001855 void
1856python3_window_free(win_T *win)
1857{
Yegappan Lakshmanan0233bdf2023-01-12 12:33:30 +00001858 WindowObject *wp = WIN_PYTHON_REF(win);
1859 if (wp == NULL)
1860 return;
1861 wp->win = INVALID_WINDOW_VALUE;
1862 WIN_PYTHON_REF(win) = NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001863}
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001864
1865 void
1866python3_tabpage_free(tabpage_T *tab)
1867{
Yegappan Lakshmanan0233bdf2023-01-12 12:33:30 +00001868 TabPageObject *tp = TAB_PYTHON_REF(tab);
1869 if (tp == NULL)
1870 return;
1871 tp->tab = INVALID_TABPAGE_VALUE;
1872 TAB_PYTHON_REF(tab) = NULL;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001873}
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001874
Bram Moolenaar7854e3a2012-11-28 15:33:14 +01001875 static PyObject *
1876Py3Init_vim(void)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001877{
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001878 // The special value is removed from sys.path in Python3_Init().
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001879 static wchar_t *(argv[2]) = {L"/must>not&exist/foo", NULL};
1880
Bram Moolenaar1dc28782013-05-21 19:11:01 +02001881 if (init_types())
1882 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001883
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001884 // Set sys.argv[] to avoid a crash in warn().
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001885 PySys_SetArgv(1, argv);
1886
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001887 if ((vim_module = PyModule_Create(&vimmodule)) == NULL)
Bram Moolenaar19e60942011-06-19 00:27:51 +02001888 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001889
Bram Moolenaardee2e312013-06-23 16:35:47 +02001890 if (populate_module(vim_module))
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001891 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001892
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001893 if (init_sys_path())
1894 return NULL;
1895
1896 return vim_module;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001897}
1898
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001899//////////////////////////////////////////////////////////////////////////
1900// 4. Utility functions for handling the interface between Vim and Python.
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001901
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001902/*
1903 * Convert a Vim line into a Python string.
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001904 * All internal newlines are replaced by null characters.
1905 *
1906 * On errors, the Python exception data is set, and NULL is returned.
1907 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001908 static PyObject *
1909LineToString(const char *str)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001910{
1911 PyObject *result;
1912 Py_ssize_t len = strlen(str);
Bram Moolenaard672dde2020-02-26 13:43:51 +01001913 char *tmp, *p;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001914
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001915 tmp = alloc(len + 1);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001916 p = tmp;
1917 if (p == NULL)
1918 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001919 PyErr_NoMemory();
1920 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001921 }
1922
1923 while (*str)
1924 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001925 if (*str == '\n')
1926 *p = '\0';
1927 else
1928 *p = *str;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001929
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001930 ++p;
1931 ++str;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001932 }
1933 *p = '\0';
1934
Bram Moolenaar2e2f52a2020-12-21 16:03:02 +01001935 result = PyUnicode_Decode(tmp, len, (char *)ENC_OPT, ERRORS_DECODE_ARG);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001936
1937 vim_free(tmp);
1938 return result;
1939}
1940
Bram Moolenaardb913952012-06-29 12:54:53 +02001941 void
Bram Moolenaar8e9a24a2019-03-19 22:22:55 +01001942do_py3eval(char_u *str, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +02001943{
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02001944 DoPyCommand((char *) str,
1945 (rangeinitializer) init_range_eval,
1946 (runner) run_eval,
1947 (void *) rettv);
Bram Moolenaar8e9a24a2019-03-19 22:22:55 +01001948 if (rettv->v_type == VAR_UNKNOWN)
Bram Moolenaardb913952012-06-29 12:54:53 +02001949 {
Bram Moolenaar8e9a24a2019-03-19 22:22:55 +01001950 rettv->v_type = VAR_NUMBER;
1951 rettv->vval.v_number = 0;
Bram Moolenaardb913952012-06-29 12:54:53 +02001952 }
1953}
1954
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01001955 int
Bram Moolenaar8e9a24a2019-03-19 22:22:55 +01001956set_ref_in_python3(int copyID)
Bram Moolenaardb913952012-06-29 12:54:53 +02001957{
Bram Moolenaarb641df42015-02-03 13:16:04 +01001958 return set_ref_in_py(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02001959}