blob: 0a0d31e69811a2d4f32c88a52948c7546c797563 [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},
Bram Moolenaar66b79852012-09-21 14:00:35 +0200631 {"PyBool_Type", (PYTHON_PROC*)&py3_PyBool_Type},
Zdenek Dohnale5e47092023-08-13 19:37:09 +0200632# if PY_VERSION_HEX >= 0x030c00b0
633 {"PyLong_Type", (PYTHON_PROC*)&py3_PyLong_Type},
634# endif
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200635 {"PyNumber_Check", (PYTHON_PROC*)&py3_PyNumber_Check},
636 {"PyNumber_Long", (PYTHON_PROC*)&py3_PyNumber_Long},
Bram Moolenaar19e60942011-06-19 00:27:51 +0200637 {"PyErr_NewException", (PYTHON_PROC*)&py3_PyErr_NewException},
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200638# ifdef Py_DEBUG
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200639 {"_Py_NegativeRefcount", (PYTHON_PROC*)&py3__Py_NegativeRefcount},
640 {"_Py_RefTotal", (PYTHON_PROC*)&py3__Py_RefTotal},
Bram Moolenaar0014a532013-05-29 21:33:39 +0200641 {"PyModule_Create2TraceRefs", (PYTHON_PROC*)&py3_PyModule_Create2TraceRefs},
642# else
643 {"PyModule_Create2", (PYTHON_PROC*)&py3_PyModule_Create2},
644# endif
645# if defined(Py_DEBUG) && !defined(Py_DEBUG_NO_PYMALLOC)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200646 {"_PyObject_DebugFree", (PYTHON_PROC*)&py3__PyObject_DebugFree},
647 {"_PyObject_DebugMalloc", (PYTHON_PROC*)&py3__PyObject_DebugMalloc},
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200648# else
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200649 {"PyObject_Malloc", (PYTHON_PROC*)&py3_PyObject_Malloc},
650 {"PyObject_Free", (PYTHON_PROC*)&py3_PyObject_Free},
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200651# endif
Bram Moolenaar774267b2013-05-21 20:51:59 +0200652 {"_PyObject_GC_New", (PYTHON_PROC*)&py3__PyObject_GC_New},
653 {"PyObject_GC_Del", (PYTHON_PROC*)&py3_PyObject_GC_Del},
654 {"PyObject_GC_UnTrack", (PYTHON_PROC*)&py3_PyObject_GC_UnTrack},
Bram Moolenaardb913952012-06-29 12:54:53 +0200655 {"PyType_IsSubtype", (PYTHON_PROC*)&py3_PyType_IsSubtype},
656 {"PyCapsule_New", (PYTHON_PROC*)&py3_PyCapsule_New},
657 {"PyCapsule_GetPointer", (PYTHON_PROC*)&py3_PyCapsule_GetPointer},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200658 {"", NULL},
659};
660
Bram Moolenaar13a1f3f2019-10-23 21:37:25 +0200661# if PY_VERSION_HEX >= 0x030800f0
662 static inline void
663py3__Py_DECREF(const char *filename UNUSED, int lineno UNUSED, PyObject *op)
664{
Bram Moolenaar13a1f3f2019-10-23 21:37:25 +0200665 if (--op->ob_refcnt != 0)
666 {
667# ifdef Py_REF_DEBUG
668 if (op->ob_refcnt < 0)
669 {
670 _Py_NegativeRefcount(filename, lineno, op);
671 }
672# endif
673 }
674 else
675 {
676 _Py_Dealloc(op);
677 }
678}
679
680# undef Py_DECREF
681# define Py_DECREF(op) py3__Py_DECREF(__FILE__, __LINE__, _PyObject_CAST(op))
682
683 static inline void
684py3__Py_XDECREF(PyObject *op)
685{
686 if (op != NULL)
687 {
688 Py_DECREF(op);
689 }
690}
691
692# undef Py_XDECREF
693# define Py_XDECREF(op) py3__Py_XDECREF(_PyObject_CAST(op))
694# endif
695
Bram Moolenaaree1b9312020-07-16 22:15:53 +0200696# if PY_VERSION_HEX >= 0x030900b0
697 static inline int
698py3_PyType_HasFeature(PyTypeObject *type, unsigned long feature)
699{
700 return ((PyType_GetFlags(type) & feature) != 0);
701}
702# define PyType_HasFeature(t,f) py3_PyType_HasFeature(t,f)
703# endif
704
Zdenek Dohnal90478f32021-06-14 15:08:30 +0200705# if PY_VERSION_HEX >= 0x030a00b2
706 static inline int
707py3__PyObject_TypeCheck(PyObject *ob, PyTypeObject *type)
708{
709 return Py_IS_TYPE(ob, type) || PyType_IsSubtype(Py_TYPE(ob), type);
710}
Zdenek Dohnalfee511c2022-06-27 13:59:00 +0100711# if PY_VERSION_HEX >= 0x030b00b3
712# undef PyObject_TypeCheck
713# define PyObject_TypeCheck(o,t) py3__PyObject_TypeCheck(o,t)
714# else
715# define _PyObject_TypeCheck(o,t) py3__PyObject_TypeCheck(o,t)
716# endif
Zdenek Dohnal90478f32021-06-14 15:08:30 +0200717# endif
718
Bram Moolenaarb2f9e0e2020-12-25 13:52:37 +0100719# ifdef MSWIN
720/*
721 * Look up the library "libname" using the InstallPath registry key.
722 * Return NULL when failed. Return an allocated string when successful.
723 */
724 static char *
725py3_get_system_libname(const char *libname)
726{
727 const char *cp = libname;
728 char subkey[128];
729 HKEY hKey;
730 char installpath[MAXPATHL];
731 LONG len = sizeof(installpath);
732 LSTATUS rc;
733 size_t sysliblen;
734 char *syslibname;
735
736 while (*cp != '\0')
737 {
738 if (*cp == ':' || *cp == '\\' || *cp == '/')
739 {
740 // Bail out if "libname" contains path separator, assume it is
741 // an absolute path.
742 return NULL;
743 }
744 ++cp;
745 }
746 vim_snprintf(subkey, sizeof(subkey),
747# ifdef _WIN64
748 "Software\\Python\\PythonCore\\%d.%d\\InstallPath",
749# else
750 "Software\\Python\\PythonCore\\%d.%d-32\\InstallPath",
751# endif
752 PY_MAJOR_VERSION, PY_MINOR_VERSION);
753 if (RegOpenKeyExA(HKEY_LOCAL_MACHINE, subkey, 0, KEY_QUERY_VALUE, &hKey)
754 != ERROR_SUCCESS)
755 return NULL;
756 rc = RegQueryValueA(hKey, NULL, installpath, &len);
757 RegCloseKey(hKey);
758 if (ERROR_SUCCESS != rc)
759 return NULL;
760 cp = installpath + len;
761 // Just in case registry value contains null terminators.
762 while (cp > installpath && *(cp-1) == '\0')
763 --cp;
764 // Remove trailing path separators.
765 while (cp > installpath && (*(cp-1) == '\\' || *(cp-1) == '/'))
766 --cp;
767 // Ignore if InstallPath is effectively empty.
768 if (cp <= installpath)
769 return NULL;
770 sysliblen = (cp - installpath) + 1 + STRLEN(libname) + 1;
771 syslibname = alloc(sysliblen);
772 vim_snprintf(syslibname, sysliblen, "%.*s\\%s",
773 (int)(cp - installpath), installpath, libname);
774 return syslibname;
775}
776# endif
777
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200778/*
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200779 * Load library and get all pointers.
780 * Parameter 'libname' provides name of DLL.
781 * Return OK or FAIL.
782 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200783 static int
784py3_runtime_link_init(char *libname, int verbose)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200785{
786 int i;
Bram Moolenaar7b24ce02018-03-29 18:15:26 +0200787 PYTHON_PROC *ucs_from_string = (PYTHON_PROC *)&py3_PyUnicode_FromString;
788 PYTHON_PROC *ucs_decode = (PYTHON_PROC *)&py3_PyUnicode_Decode;
789 PYTHON_PROC *ucs_as_encoded_string =
790 (PYTHON_PROC *)&py3_PyUnicode_AsEncodedString;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200791
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100792# if !(defined(PY_NO_RTLD_GLOBAL) && defined(PY3_NO_RTLD_GLOBAL)) && defined(UNIX) && defined(FEAT_PYTHON)
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100793 // Can't have Python and Python3 loaded at the same time.
Dominique Pelleaf4a61a2021-12-27 17:21:41 +0000794 // It causes a crash, because RTLD_GLOBAL is needed for
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100795 // standard C extension libraries of one or both python versions.
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200796 if (python_loaded())
797 {
Bram Moolenaar9dc93ae2011-08-28 16:00:19 +0200798 if (verbose)
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +0000799 emsg(_(e_this_vim_cannot_execute_py3_after_using_python));
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200800 return FAIL;
801 }
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200802# endif
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200803
804 if (hinstPy3 != 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200805 return OK;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200806 hinstPy3 = load_dll(libname);
807
Bram Moolenaarb2f9e0e2020-12-25 13:52:37 +0100808# ifdef MSWIN
809 if (!hinstPy3)
810 {
811 // Attempt to use the path from InstallPath as stored in the registry.
812 char *syslibname = py3_get_system_libname(libname);
813
814 if (syslibname != NULL)
815 {
816 hinstPy3 = load_dll(syslibname);
817 vim_free(syslibname);
818 }
819 }
820# endif
821
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200822 if (!hinstPy3)
823 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200824 if (verbose)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000825 semsg(_(e_could_not_load_library_str_str), libname, load_dll_error());
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200826 return FAIL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200827 }
828
829 for (i = 0; py3_funcname_table[i].ptr; ++i)
830 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200831 if ((*py3_funcname_table[i].ptr = symbol_from_dll(hinstPy3,
832 py3_funcname_table[i].name)) == NULL)
833 {
834 close_dll(hinstPy3);
835 hinstPy3 = 0;
836 if (verbose)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000837 semsg(_(e_could_not_load_library_function_str), py3_funcname_table[i].name);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200838 return FAIL;
839 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200840 }
841
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100842 // Load unicode functions separately as only the ucs2 or the ucs4 functions
843 // will be present in the library.
Bram Moolenaar9c9cbf12012-10-23 05:17:37 +0200844# if PY_VERSION_HEX >= 0x030300f0
Bram Moolenaar7b24ce02018-03-29 18:15:26 +0200845 *ucs_from_string = symbol_from_dll(hinstPy3, "PyUnicode_FromString");
846 *ucs_decode = symbol_from_dll(hinstPy3, "PyUnicode_Decode");
847 *ucs_as_encoded_string = symbol_from_dll(hinstPy3,
Bram Moolenaar7bc4f932012-10-14 03:22:56 +0200848 "PyUnicode_AsEncodedString");
Bram Moolenaar9c9cbf12012-10-23 05:17:37 +0200849# else
Bram Moolenaar7b24ce02018-03-29 18:15:26 +0200850 *ucs_from_string = symbol_from_dll(hinstPy3, "PyUnicodeUCS2_FromString");
851 *ucs_decode = symbol_from_dll(hinstPy3,
Bram Moolenaar19e60942011-06-19 00:27:51 +0200852 "PyUnicodeUCS2_Decode");
Bram Moolenaar7b24ce02018-03-29 18:15:26 +0200853 *ucs_as_encoded_string = symbol_from_dll(hinstPy3,
Bram Moolenaar19e60942011-06-19 00:27:51 +0200854 "PyUnicodeUCS2_AsEncodedString");
Bram Moolenaar7b24ce02018-03-29 18:15:26 +0200855 if (*ucs_from_string == NULL || *ucs_decode == NULL
856 || *ucs_as_encoded_string == NULL)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200857 {
Bram Moolenaar7b24ce02018-03-29 18:15:26 +0200858 *ucs_from_string = symbol_from_dll(hinstPy3,
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200859 "PyUnicodeUCS4_FromString");
Bram Moolenaar7b24ce02018-03-29 18:15:26 +0200860 *ucs_decode = symbol_from_dll(hinstPy3,
Bram Moolenaar19e60942011-06-19 00:27:51 +0200861 "PyUnicodeUCS4_Decode");
Bram Moolenaar7b24ce02018-03-29 18:15:26 +0200862 *ucs_as_encoded_string = symbol_from_dll(hinstPy3,
Bram Moolenaar19e60942011-06-19 00:27:51 +0200863 "PyUnicodeUCS4_AsEncodedString");
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200864 }
Bram Moolenaar9c9cbf12012-10-23 05:17:37 +0200865# endif
Bram Moolenaar7b24ce02018-03-29 18:15:26 +0200866 if (*ucs_from_string == NULL || *ucs_decode == NULL
867 || *ucs_as_encoded_string == NULL)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200868 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200869 close_dll(hinstPy3);
870 hinstPy3 = 0;
871 if (verbose)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000872 semsg(_(e_could_not_load_library_function_str), "PyUnicode_UCSX_*");
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200873 return FAIL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200874 }
875
876 return OK;
877}
878
879/*
880 * If python is enabled (there is installed python on Windows system) return
881 * TRUE, else FALSE.
882 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200883 int
884python3_enabled(int verbose)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200885{
Bram Moolenaar25e4fcd2016-01-09 14:57:47 +0100886 return py3_runtime_link_init((char *)p_py3dll, verbose) == OK;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200887}
888
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100889/*
890 * Load the standard Python exceptions - don't import the symbols from the
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200891 * DLL, as this can cause errors (importing data symbols is not reliable).
892 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200893 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100894get_py3_exceptions(void)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200895{
896 PyObject *exmod = PyImport_ImportModule("builtins");
897 PyObject *exdict = PyModule_GetDict(exmod);
898 p3imp_PyExc_AttributeError = PyDict_GetItemString(exdict, "AttributeError");
899 p3imp_PyExc_IndexError = PyDict_GetItemString(exdict, "IndexError");
Bram Moolenaaraf6abb92013-04-24 13:04:26 +0200900 p3imp_PyExc_KeyError = PyDict_GetItemString(exdict, "KeyError");
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200901 p3imp_PyExc_KeyboardInterrupt = PyDict_GetItemString(exdict, "KeyboardInterrupt");
902 p3imp_PyExc_TypeError = PyDict_GetItemString(exdict, "TypeError");
903 p3imp_PyExc_ValueError = PyDict_GetItemString(exdict, "ValueError");
Bram Moolenaar41009372013-07-01 22:03:04 +0200904 p3imp_PyExc_SystemExit = PyDict_GetItemString(exdict, "SystemExit");
Bram Moolenaar8661b172013-05-15 15:44:28 +0200905 p3imp_PyExc_RuntimeError = PyDict_GetItemString(exdict, "RuntimeError");
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200906 p3imp_PyExc_ImportError = PyDict_GetItemString(exdict, "ImportError");
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200907 p3imp_PyExc_OverflowError = PyDict_GetItemString(exdict, "OverflowError");
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200908 Py_XINCREF(p3imp_PyExc_AttributeError);
909 Py_XINCREF(p3imp_PyExc_IndexError);
Bram Moolenaaraf6abb92013-04-24 13:04:26 +0200910 Py_XINCREF(p3imp_PyExc_KeyError);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200911 Py_XINCREF(p3imp_PyExc_KeyboardInterrupt);
912 Py_XINCREF(p3imp_PyExc_TypeError);
913 Py_XINCREF(p3imp_PyExc_ValueError);
Bram Moolenaar41009372013-07-01 22:03:04 +0200914 Py_XINCREF(p3imp_PyExc_SystemExit);
Bram Moolenaar8661b172013-05-15 15:44:28 +0200915 Py_XINCREF(p3imp_PyExc_RuntimeError);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200916 Py_XINCREF(p3imp_PyExc_ImportError);
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200917 Py_XINCREF(p3imp_PyExc_OverflowError);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200918 Py_XDECREF(exmod);
919}
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100920#endif // DYNAMIC_PYTHON3
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200921
Bram Moolenaardb913952012-06-29 12:54:53 +0200922static int py3initialised = 0;
Bram Moolenaardb913952012-06-29 12:54:53 +0200923#define PYINITIALISED py3initialised
Bram Moolenaarc4f83382017-07-07 14:50:44 +0200924static int python_end_called = FALSE;
Bram Moolenaardb913952012-06-29 12:54:53 +0200925
Bram Moolenaar774267b2013-05-21 20:51:59 +0200926#define DESTRUCTOR_FINISH(self) Py_TYPE(self)->tp_free((PyObject*)self)
Bram Moolenaardb913952012-06-29 12:54:53 +0200927
Bram Moolenaar971db462013-05-12 18:44:48 +0200928#define WIN_PYTHON_REF(win) win->w_python3_ref
929#define BUF_PYTHON_REF(buf) buf->b_python3_ref
Bram Moolenaar5e538ec2013-05-15 15:12:29 +0200930#define TAB_PYTHON_REF(tab) tab->tp_python3_ref
Bram Moolenaar971db462013-05-12 18:44:48 +0200931
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200932 static void
933call_PyObject_Free(void *p)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200934{
Bram Moolenaar0014a532013-05-29 21:33:39 +0200935#if defined(Py_DEBUG) && !defined(Py_DEBUG_NO_PYMALLOC)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200936 _PyObject_DebugFree(p);
937#else
938 PyObject_Free(p);
939#endif
940}
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200941
942 static PyObject *
943call_PyType_GenericNew(PyTypeObject *type, PyObject *args, PyObject *kwds)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200944{
945 return PyType_GenericNew(type,args,kwds);
946}
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200947
948 static PyObject *
949call_PyType_GenericAlloc(PyTypeObject *type, Py_ssize_t nitems)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200950{
951 return PyType_GenericAlloc(type,nitems);
952}
953
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200954static PyObject *OutputGetattro(PyObject *, PyObject *);
955static int OutputSetattro(PyObject *, PyObject *, PyObject *);
956static PyObject *BufferGetattro(PyObject *, PyObject *);
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200957static int BufferSetattro(PyObject *, PyObject *, PyObject *);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +0200958static PyObject *TabPageGetattro(PyObject *, PyObject *);
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200959static PyObject *WindowGetattro(PyObject *, PyObject *);
960static int WindowSetattro(PyObject *, PyObject *, PyObject *);
961static PyObject *RangeGetattro(PyObject *, PyObject *);
962static PyObject *CurrentGetattro(PyObject *, PyObject *);
963static int CurrentSetattro(PyObject *, PyObject *, PyObject *);
964static PyObject *DictionaryGetattro(PyObject *, PyObject *);
965static int DictionarySetattro(PyObject *, PyObject *, PyObject *);
966static PyObject *ListGetattro(PyObject *, PyObject *);
967static int ListSetattro(PyObject *, PyObject *, PyObject *);
968static PyObject *FunctionGetattro(PyObject *, PyObject *);
969
970static struct PyModuleDef vimmodule;
971
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +0200972#define PY_CAN_RECURSE
973
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200974/*
975 * Include the code shared with if_python.c
976 */
977#include "if_py_both.h"
978
Bram Moolenaar828bff12019-03-19 22:11:41 +0100979// NOTE: Must always be used at the start of a block, since it declares "name".
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200980#define GET_ATTR_STRING(name, nameobj) \
981 char *name = ""; \
982 if (PyUnicode_Check(nameobj)) \
Bram Moolenaar828bff12019-03-19 22:11:41 +0100983 name = (char *)_PyUnicode_AsString(nameobj)
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200984
985#define PY3OBJ_DELETED(obj) (obj->ob_base.ob_refcnt<=0)
986
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100987///////////////////////////////////////////////////////
988// Internal function prototypes.
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200989
Bram Moolenaar7854e3a2012-11-28 15:33:14 +0100990static PyObject *Py3Init_vim(void);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200991
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100992///////////////////////////////////////////////////////
993// 1. Python interpreter main program.
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200994
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200995 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100996python3_end(void)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200997{
998 static int recurse = 0;
999
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001000 // If a crash occurs while doing this, don't try again.
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001001 if (recurse != 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001002 return;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001003
Bram Moolenaarc4f83382017-07-07 14:50:44 +02001004 python_end_called = TRUE;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001005 ++recurse;
1006
1007#ifdef DYNAMIC_PYTHON3
1008 if (hinstPy3)
1009#endif
1010 if (Py_IsInitialized())
1011 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001012 // acquire lock before finalizing
Bram Moolenaar71700b82013-05-15 17:49:05 +02001013 PyGILState_Ensure();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001014
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001015 Py_Finalize();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001016 }
1017
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001018 --recurse;
1019}
1020
Bram Moolenaar094454f2015-10-07 10:39:55 +02001021#if (defined(DYNAMIC_PYTHON3) && defined(DYNAMIC_PYTHON) && defined(FEAT_PYTHON) && defined(UNIX)) || defined(PROTO)
Bram Moolenaar4c3a3262010-07-24 15:42:14 +02001022 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001023python3_loaded(void)
Bram Moolenaar4c3a3262010-07-24 15:42:14 +02001024{
1025 return (hinstPy3 != 0);
1026}
1027#endif
1028
Bram Moolenaar94073162018-01-31 21:49:05 +01001029static wchar_t *py_home_buf = NULL;
1030
Bram Moolenaar56b8dc32020-08-06 21:47:11 +02001031#if defined(MSWIN) && (PY_VERSION_HEX >= 0x030500f0)
Bram Moolenaarc6ed2542020-10-10 23:26:28 +02001032/*
1033 * Return TRUE if stdin is readable from Python 3.
1034 */
1035 static BOOL
1036is_stdin_readable(void)
1037{
1038 DWORD mode, eventnum;
1039 struct _stat st;
1040 int fd = fileno(stdin);
1041 HANDLE hstdin = (HANDLE)_get_osfhandle(fd);
1042
1043 // Check if stdin is connected to the console.
1044 if (GetConsoleMode(hstdin, &mode))
1045 // Check if it is opened as input.
1046 return GetNumberOfConsoleInputEvents(hstdin, &eventnum);
1047
1048 return _fstat(fd, &st) == 0;
1049}
1050
1051// Python 3.5 or later will abort inside Py_Initialize() when stdin has
1052// been closed (i.e. executed by "vim -"). Reconnect stdin to CONIN$.
Bram Moolenaar56b8dc32020-08-06 21:47:11 +02001053// Note that the python DLL is linked to its own stdio DLL which can be
1054// differ from Vim's stdio.
1055 static void
1056reset_stdin(void)
1057{
1058 FILE *(*py__acrt_iob_func)(unsigned) = NULL;
1059 FILE *(*pyfreopen)(const char *, const char *, FILE *) = NULL;
Bram Moolenaar63ff72a2022-02-07 13:54:01 +00001060 HINSTANCE hinst = hinstPy3;
Bram Moolenaar56b8dc32020-08-06 21:47:11 +02001061
Bram Moolenaarc6ed2542020-10-10 23:26:28 +02001062 if (hinst == NULL || is_stdin_readable())
Bram Moolenaar56b8dc32020-08-06 21:47:11 +02001063 return;
1064
1065 // Get "freopen" and "stdin" which are used in the python DLL.
1066 // "stdin" is defined as "__acrt_iob_func(0)" in VC++ 2015 or later.
1067 py__acrt_iob_func = get_dll_import_func(hinst, "__acrt_iob_func");
1068 if (py__acrt_iob_func)
1069 {
1070 HINSTANCE hpystdiodll = find_imported_module_by_funcname(hinst,
Bram Moolenaar253b16a2020-10-06 19:59:06 +02001071 "__acrt_iob_func");
Bram Moolenaar56b8dc32020-08-06 21:47:11 +02001072 if (hpystdiodll)
Bram Moolenaar253b16a2020-10-06 19:59:06 +02001073 pyfreopen = (void *)GetProcAddress(hpystdiodll, "freopen");
Bram Moolenaar56b8dc32020-08-06 21:47:11 +02001074 }
1075
Bram Moolenaarc6ed2542020-10-10 23:26:28 +02001076 // Reconnect stdin to CONIN$.
Bram Moolenaar253b16a2020-10-06 19:59:06 +02001077 if (pyfreopen != NULL)
Bram Moolenaarc6ed2542020-10-10 23:26:28 +02001078 pyfreopen("CONIN$", "r", py__acrt_iob_func(0));
Bram Moolenaar56b8dc32020-08-06 21:47:11 +02001079 else
Bram Moolenaarc6ed2542020-10-10 23:26:28 +02001080 freopen("CONIN$", "r", stdin);
Bram Moolenaar56b8dc32020-08-06 21:47:11 +02001081}
1082#else
1083# define reset_stdin()
1084#endif
1085
Bram Moolenaar63ff72a2022-02-07 13:54:01 +00001086// Python 3.2 or later will abort inside Py_Initialize() when mandatory
1087// modules cannot be loaded (e.g. 'pythonthreehome' is wrongly set.).
1088// Install a hook to python dll's exit() and recover from it.
1089#if defined(MSWIN) && (PY_VERSION_HEX >= 0x030200f0)
1090# define HOOK_EXIT
1091# include <setjmp.h>
1092
1093static jmp_buf exit_hook_jump_buf;
1094static void *orig_exit = NULL;
1095
1096/*
1097 * Function that replaces exit() while calling Py_Initialize().
1098 */
1099 static void
1100hooked_exit(int ret)
1101{
1102 // Recover from exit.
1103 longjmp(exit_hook_jump_buf, 1);
1104}
1105
1106/*
1107 * Install a hook to python dll's exit().
1108 */
1109 static void
1110hook_py_exit(void)
1111{
1112 HINSTANCE hinst = hinstPy3;
1113
1114 if (hinst == NULL || orig_exit != NULL)
1115 return;
1116
1117 orig_exit = hook_dll_import_func(hinst, "exit", (void *)hooked_exit);
1118}
1119
1120/*
1121 * Remove the hook installed by hook_py_exit().
1122 */
1123 static void
1124restore_py_exit(void)
1125{
1126 HINSTANCE hinst = hinstPy3;
1127
1128 if (hinst == NULL)
1129 return;
1130
1131 if (orig_exit != NULL)
1132 hook_dll_import_func(hinst, "exit", orig_exit);
1133 orig_exit = NULL;
1134}
1135#endif
1136
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001137 static int
1138Python3_Init(void)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001139{
1140 if (!py3initialised)
1141 {
1142#ifdef DYNAMIC_PYTHON3
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001143 if (!python3_enabled(TRUE))
1144 {
Bram Moolenaar9a846fb2022-01-01 21:59:18 +00001145 emsg(_(e_sorry_this_command_is_disabled_python_library_could_not_be_found));
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001146 goto fail;
1147 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001148#endif
1149
1150 init_structs();
1151
Bram Moolenaar94073162018-01-31 21:49:05 +01001152 if (*p_py3home != NUL)
1153 {
1154 size_t len = mbstowcs(NULL, (char *)p_py3home, 0) + 1;
Bram Moolenaar644d37b2010-11-16 19:26:02 +01001155
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001156 // The string must not change later, make a copy in static memory.
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001157 py_home_buf = ALLOC_MULT(wchar_t, len);
Bram Moolenaar94073162018-01-31 21:49:05 +01001158 if (py_home_buf != NULL && mbstowcs(
1159 py_home_buf, (char *)p_py3home, len) != (size_t)-1)
1160 Py_SetPythonHome(py_home_buf);
1161 }
Bram Moolenaar644d37b2010-11-16 19:26:02 +01001162#ifdef PYTHON3_HOME
Bram Moolenaar94073162018-01-31 21:49:05 +01001163 else if (mch_getenv((char_u *)"PYTHONHOME") == NULL)
Bram Moolenaar10005652015-12-31 21:03:23 +01001164 Py_SetPythonHome(PYTHON3_HOME);
Bram Moolenaar644d37b2010-11-16 19:26:02 +01001165#endif
1166
Bram Moolenaar7bc4f932012-10-14 03:22:56 +02001167 PyImport_AppendInittab("vim", Py3Init_vim);
1168
Bram Moolenaar63ff72a2022-02-07 13:54:01 +00001169#if !defined(DYNAMIC_PYTHON3) && defined(MSWIN)
1170 hinstPy3 = GetModuleHandle(PYTHON3_DLL);
1171#endif
Bram Moolenaar56b8dc32020-08-06 21:47:11 +02001172 reset_stdin();
Bram Moolenaar63ff72a2022-02-07 13:54:01 +00001173
1174#ifdef HOOK_EXIT
1175 // Catch exit() called in Py_Initialize().
1176 hook_py_exit();
1177 if (setjmp(exit_hook_jump_buf) == 0)
Bram Moolenaar63ff72a2022-02-07 13:54:01 +00001178 {
1179 Py_Initialize();
Bram Moolenaar63ff72a2022-02-07 13:54:01 +00001180 restore_py_exit();
Bram Moolenaar63ff72a2022-02-07 13:54:01 +00001181 }
Bram Moolenaar63ff72a2022-02-07 13:54:01 +00001182 else
1183 {
1184 // exit() was called in Py_Initialize().
1185 restore_py_exit();
1186 emsg(_(e_critical_error_in_python3_initialization_check_your_installation));
1187 goto fail;
1188 }
K.Takataa7fbaa42022-12-26 14:46:51 +00001189#else
1190 Py_Initialize();
Bram Moolenaar63ff72a2022-02-07 13:54:01 +00001191#endif
Bram Moolenaard0573012017-10-28 21:11:06 +02001192
Bram Moolenaarefc0d942020-10-11 18:05:02 +02001193#if PY_VERSION_HEX < 0x03090000
1194 // Initialise threads. This is deprecated since Python 3.9.
Bram Moolenaar456f2bb2011-06-12 21:37:13 +02001195 PyEval_InitThreads();
Bram Moolenaarefc0d942020-10-11 18:05:02 +02001196#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001197#ifdef DYNAMIC_PYTHON3
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001198 get_py3_exceptions();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001199#endif
1200
Bram Moolenaar1dc28782013-05-21 19:11:01 +02001201 if (PythonIO_Init_io())
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001202 goto fail;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001203
Bram Moolenaardb913952012-06-29 12:54:53 +02001204 globals = PyModule_GetDict(PyImport_AddModule("__main__"));
1205
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001206 // Remove the element from sys.path that was added because of our
1207 // argv[0] value in Py3Init_vim(). Previously we used an empty
1208 // string, but depending on the OS we then get an empty entry or
1209 // the current directory in sys.path.
1210 // Only after vim has been imported, the element does exist in
1211 // sys.path.
Bram Moolenaar19e60942011-06-19 00:27:51 +02001212 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 +02001213
Bram Moolenaarefc0d942020-10-11 18:05:02 +02001214 // Without the call to PyEval_SaveThread, thread specific state (such
1215 // as the system trace hook), will be lost between invocations of
1216 // Python code.
1217 // GIL may have been created and acquired in PyEval_InitThreads() and
1218 // thread state is created in Py_Initialize(); there
1219 // _PyGILState_NoteThreadState() also sets gilcounter to 1 (python must
1220 // have threads enabled!), so the following does both: unlock GIL and
1221 // save thread state in TLS without deleting thread state
Bram Moolenaar76d711c2013-02-13 14:17:08 +01001222 PyEval_SaveThread();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001223
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001224 py3initialised = 1;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001225 }
1226
1227 return 0;
1228
1229fail:
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001230 // We call PythonIO_Flush() here to print any Python errors.
1231 // This is OK, as it is possible to call this function even
1232 // if PythonIO_Init_io() has not completed successfully (it will
1233 // not do anything in this case).
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001234 PythonIO_Flush();
1235 return -1;
1236}
1237
1238/*
1239 * External interface
1240 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001241 static void
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02001242DoPyCommand(const char *cmd, rangeinitializer init_range, runner run, void *arg)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001243{
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001244#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001245 char *saved_locale;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001246#endif
Bram Moolenaar19e60942011-06-19 00:27:51 +02001247 PyObject *cmdstr;
1248 PyObject *cmdbytes;
Bram Moolenaar71700b82013-05-15 17:49:05 +02001249 PyGILState_STATE pygilstate;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001250
Bram Moolenaarc4f83382017-07-07 14:50:44 +02001251 if (python_end_called)
1252 goto theend;
1253
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001254 if (Python3_Init())
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001255 goto theend;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001256
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02001257 init_range(arg);
1258
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001259 Python_Release_Vim(); // leave Vim
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001260
1261#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001262 // Python only works properly when the LC_NUMERIC locale is "C".
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001263 saved_locale = setlocale(LC_NUMERIC, NULL);
1264 if (saved_locale == NULL || STRCMP(saved_locale, "C") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001265 saved_locale = NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001266 else
1267 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001268 // Need to make a copy, value may change when setting new locale.
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001269 saved_locale = (char *)vim_strsave((char_u *)saved_locale);
1270 (void)setlocale(LC_NUMERIC, "C");
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001271 }
1272#endif
1273
1274 pygilstate = PyGILState_Ensure();
1275
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001276 // PyRun_SimpleString expects a UTF-8 string. Wrong encoding may cause
1277 // SyntaxError (unicode error).
Bram Moolenaar3d64a312011-07-15 15:54:44 +02001278 cmdstr = PyUnicode_Decode(cmd, strlen(cmd),
Bram Moolenaar2e2f52a2020-12-21 16:03:02 +01001279 (char *)ENC_OPT, ERRORS_DECODE_ARG);
1280 cmdbytes = PyUnicode_AsEncodedString(cmdstr, "utf-8", ERRORS_ENCODE_ARG);
Bram Moolenaar19e60942011-06-19 00:27:51 +02001281 Py_XDECREF(cmdstr);
Bram Moolenaardb913952012-06-29 12:54:53 +02001282
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02001283 run(PyBytes_AsString(cmdbytes), arg, &pygilstate);
Bram Moolenaar19e60942011-06-19 00:27:51 +02001284 Py_XDECREF(cmdbytes);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001285
1286 PyGILState_Release(pygilstate);
1287
1288#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
1289 if (saved_locale != NULL)
1290 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001291 (void)setlocale(LC_NUMERIC, saved_locale);
1292 vim_free(saved_locale);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001293 }
1294#endif
1295
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001296 Python_Lock_Vim(); // enter Vim
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001297 PythonIO_Flush();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001298
1299theend:
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001300 return; // keeps lint happy
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001301}
1302
1303/*
Bram Moolenaar368373e2010-07-19 20:46:22 +02001304 * ":py3"
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001305 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001306 void
1307ex_py3(exarg_T *eap)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001308{
1309 char_u *script;
1310
1311 script = script_get(eap, eap->arg);
1312 if (!eap->skip)
1313 {
Bram Moolenaar14816ad2019-02-18 22:04:56 +01001314 if (p_pyx == 0)
1315 p_pyx = 3;
1316
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02001317 DoPyCommand(script == NULL ? (char *) eap->arg : (char *) script,
1318 (rangeinitializer) init_range_cmd,
1319 (runner) run_cmd,
1320 (void *) eap);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001321 }
1322 vim_free(script);
1323}
1324
1325#define BUFFER_SIZE 2048
1326
1327/*
Bram Moolenaar6df6f472010-07-18 18:04:50 +02001328 * ":py3file"
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001329 */
1330 void
1331ex_py3file(exarg_T *eap)
1332{
1333 static char buffer[BUFFER_SIZE];
1334 const char *file;
1335 char *p;
1336 int i;
1337
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01001338 if (p_pyx == 0)
1339 p_pyx = 3;
1340
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001341 // Have to do it like this. PyRun_SimpleFile requires you to pass a
1342 // stdio file pointer, but Vim and the Python DLL are compiled with
1343 // different options under Windows, meaning that stdio pointers aren't
1344 // compatible between the two. Yuk.
1345 //
1346 // construct: exec(compile(open('a_filename', 'rb').read(), 'a_filename', 'exec'))
1347 //
1348 // Using bytes so that Python can detect the source encoding as it normally
1349 // does. The doc does not say "compile" accept bytes, though.
1350 //
1351 // We need to escape any backslashes or single quotes in the file name, so that
1352 // Python won't mangle the file name.
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001353
1354 strcpy(buffer, "exec(compile(open('");
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001355 p = buffer + 19; // size of "exec(compile(open('"
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001356
1357 for (i=0; i<2; ++i)
1358 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001359 file = (char *)eap->arg;
1360 while (*file && p < buffer + (BUFFER_SIZE - 3))
1361 {
1362 if (*file == '\\' || *file == '\'')
1363 *p++ = '\\';
1364 *p++ = *file++;
1365 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001366 // If we didn't finish the file name, we hit a buffer overflow
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001367 if (*file != '\0')
1368 return;
1369 if (i==0)
1370 {
Bram Moolenaar19e60942011-06-19 00:27:51 +02001371 strcpy(p,"','rb').read(),'");
1372 p += 16;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001373 }
1374 else
1375 {
1376 strcpy(p,"','exec'))");
1377 p += 10;
1378 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001379 }
1380
1381
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001382 // Execute the file
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02001383 DoPyCommand(buffer,
1384 (rangeinitializer) init_range_cmd,
1385 (runner) run_cmd,
1386 (void *) eap);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001387}
1388
Bram Moolenaar54e8f002013-05-15 19:44:39 +02001389 void
1390ex_py3do(exarg_T *eap)
Bram Moolenaar3dab2802013-05-15 18:28:13 +02001391{
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01001392 if (p_pyx == 0)
1393 p_pyx = 3;
1394
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02001395 DoPyCommand((char *)eap->arg,
1396 (rangeinitializer)init_range_cmd,
1397 (runner)run_do,
1398 (void *)eap);
Bram Moolenaar3dab2802013-05-15 18:28:13 +02001399}
1400
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001401///////////////////////////////////////////////////////
1402// 2. Python output stream: writes output via [e]msg().
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001403
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001404// Implementation functions
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001405
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001406 static PyObject *
1407OutputGetattro(PyObject *self, PyObject *nameobj)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001408{
Bram Moolenaar77045652012-09-21 13:46:06 +02001409 GET_ATTR_STRING(name, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001410
1411 if (strcmp(name, "softspace") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001412 return PyLong_FromLong(((OutputObject *)(self))->softspace);
Bram Moolenaar6d4431e2016-04-21 20:00:56 +02001413 else if (strcmp(name, "errors") == 0)
1414 return PyString_FromString("strict");
1415 else if (strcmp(name, "encoding") == 0)
1416 return PyString_FromString(ENC_OPT);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001417
1418 return PyObject_GenericGetAttr(self, nameobj);
1419}
1420
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001421 static int
1422OutputSetattro(PyObject *self, PyObject *nameobj, PyObject *val)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001423{
Bram Moolenaar77045652012-09-21 13:46:06 +02001424 GET_ATTR_STRING(name, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001425
Bram Moolenaard6e39182013-05-21 18:30:34 +02001426 return OutputSetattr((OutputObject *)(self), name, val);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001427}
1428
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001429///////////////////////////////////////////////////////
1430// 3. Implementation of the Vim module for Python
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001431
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001432// Window type - Implementation functions
1433// --------------------------------------
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001434
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001435#define WindowType_Check(obj) ((obj)->ob_base.ob_type == &WindowType)
1436
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001437// Buffer type - Implementation functions
1438// --------------------------------------
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001439
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001440#define BufferType_Check(obj) ((obj)->ob_base.ob_type == &BufferType)
1441
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001442static PyObject* BufferSubscript(PyObject *self, PyObject *idx);
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02001443static int BufferAsSubscript(PyObject *self, PyObject *idx, PyObject *val);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001444
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001445// Line range type - Implementation functions
1446// --------------------------------------
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001447
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001448#define RangeType_Check(obj) ((obj)->ob_base.ob_type == &RangeType)
1449
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001450static PyObject* RangeSubscript(PyObject *self, PyObject *idx);
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02001451static int RangeAsItem(PyObject *, Py_ssize_t, PyObject *);
1452static int RangeAsSubscript(PyObject *self, PyObject *idx, PyObject *val);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001453
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001454// Current objects type - Implementation functions
1455// -----------------------------------------------
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001456
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001457static PySequenceMethods BufferAsSeq = {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001458 (lenfunc) BufferLength, // sq_length, len(x)
1459 (binaryfunc) 0, // sq_concat, x+y
1460 (ssizeargfunc) 0, // sq_repeat, x*n
1461 (ssizeargfunc) BufferItem, // sq_item, x[i]
1462 0, // was_sq_slice, x[i:j]
1463 0, // sq_ass_item, x[i]=v
1464 0, // sq_ass_slice, x[i:j]=v
1465 0, // sq_contains
1466 0, // sq_inplace_concat
1467 0, // sq_inplace_repeat
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001468};
1469
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001470static PyMappingMethods BufferAsMapping = {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001471 /* mp_length */ (lenfunc)BufferLength,
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001472 /* mp_subscript */ (binaryfunc)BufferSubscript,
Bram Moolenaar19e60942011-06-19 00:27:51 +02001473 /* mp_ass_subscript */ (objobjargproc)BufferAsSubscript,
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001474};
1475
1476
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001477// Buffer object
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001478
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001479 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02001480BufferGetattro(PyObject *self, PyObject *nameobj)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001481{
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001482 PyObject *r;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001483
Bram Moolenaar77045652012-09-21 13:46:06 +02001484 GET_ATTR_STRING(name, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001485
Bram Moolenaar9e822c02013-05-29 22:15:30 +02001486 if ((r = BufferAttrValid((BufferObject *)(self), name)))
1487 return r;
1488
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001489 if (CheckBuffer((BufferObject *)(self)))
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001490 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001491
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001492 r = BufferAttr((BufferObject *)(self), name);
1493 if (r || PyErr_Occurred())
1494 return r;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001495 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001496 return PyObject_GenericGetAttr(self, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001497}
1498
Bram Moolenaare9ba5162013-05-29 22:02:22 +02001499 static int
1500BufferSetattro(PyObject *self, PyObject *nameobj, PyObject *val)
1501{
1502 GET_ATTR_STRING(name, nameobj);
1503
1504 return BufferSetattr((BufferObject *)(self), name, val);
1505}
1506
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001507//////////////////
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001508
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001509 static PyObject *
1510BufferSubscript(PyObject *self, PyObject* idx)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001511{
Bram Moolenaardb913952012-06-29 12:54:53 +02001512 if (PyLong_Check(idx))
1513 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001514 long _idx = PyLong_AsLong(idx);
Bram Moolenaard6e39182013-05-21 18:30:34 +02001515 return BufferItem((BufferObject *)(self), _idx);
Bram Moolenaarebfec1c2023-01-22 21:14:53 +00001516 }
1517 else if (PySlice_Check(idx))
Bram Moolenaardb913952012-06-29 12:54:53 +02001518 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001519 Py_ssize_t start, stop, step, slicelen;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001520
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02001521 if (CheckBuffer((BufferObject *) self))
1522 return NULL;
1523
Bram Moolenaar922a4662014-03-30 16:11:43 +02001524 if (PySlice_GetIndicesEx((PySliceObject_T *)idx,
Bram Moolenaarbd80f352013-05-12 21:16:23 +02001525 (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count,
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001526 &start, &stop,
Bram Moolenaardb913952012-06-29 12:54:53 +02001527 &step, &slicelen) < 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001528 return NULL;
Bram Moolenaard6e39182013-05-21 18:30:34 +02001529 return BufferSlice((BufferObject *)(self), start, stop);
Bram Moolenaardb913952012-06-29 12:54:53 +02001530 }
1531 else
1532 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001533 RAISE_INVALID_INDEX_TYPE(idx);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001534 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001535 }
1536}
1537
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02001538 static int
Bram Moolenaar19e60942011-06-19 00:27:51 +02001539BufferAsSubscript(PyObject *self, PyObject* idx, PyObject* val)
1540{
Bram Moolenaardb913952012-06-29 12:54:53 +02001541 if (PyLong_Check(idx))
1542 {
Bram Moolenaar19e60942011-06-19 00:27:51 +02001543 long n = PyLong_AsLong(idx);
Bram Moolenaarab589462020-07-06 21:03:06 +02001544
1545 if (CheckBuffer((BufferObject *) self))
1546 return -1;
1547
Bram Moolenaar19e60942011-06-19 00:27:51 +02001548 return RBAsItem((BufferObject *)(self), n, val, 1,
1549 (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count,
1550 NULL);
Bram Moolenaarebfec1c2023-01-22 21:14:53 +00001551 }
1552 else if (PySlice_Check(idx))
Bram Moolenaardb913952012-06-29 12:54:53 +02001553 {
Bram Moolenaar19e60942011-06-19 00:27:51 +02001554 Py_ssize_t start, stop, step, slicelen;
1555
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02001556 if (CheckBuffer((BufferObject *) self))
1557 return -1;
1558
Bram Moolenaar922a4662014-03-30 16:11:43 +02001559 if (PySlice_GetIndicesEx((PySliceObject_T *)idx,
Bram Moolenaarbd80f352013-05-12 21:16:23 +02001560 (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count,
Bram Moolenaar19e60942011-06-19 00:27:51 +02001561 &start, &stop,
Bram Moolenaardb913952012-06-29 12:54:53 +02001562 &step, &slicelen) < 0)
Bram Moolenaar19e60942011-06-19 00:27:51 +02001563 return -1;
Bram Moolenaar19e60942011-06-19 00:27:51 +02001564 return RBAsSlice((BufferObject *)(self), start, stop, val, 1,
1565 (PyInt)((BufferObject *)(self))->buf->b_ml.ml_line_count,
1566 NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02001567 }
1568 else
1569 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001570 RAISE_INVALID_INDEX_TYPE(idx);
Bram Moolenaar19e60942011-06-19 00:27:51 +02001571 return -1;
1572 }
1573}
1574
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001575static PySequenceMethods RangeAsSeq = {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001576 (lenfunc) RangeLength, // sq_length, len(x)
1577 (binaryfunc) 0, // RangeConcat, sq_concat, x+y
1578 (ssizeargfunc) 0, // RangeRepeat, sq_repeat, x*n
1579 (ssizeargfunc) RangeItem, // sq_item, x[i]
1580 0, // was_sq_slice, x[i:j]
1581 (ssizeobjargproc) RangeAsItem, // sq_as_item, x[i]=v
1582 0, // sq_ass_slice, x[i:j]=v
1583 0, // sq_contains
1584 0, // sq_inplace_concat
1585 0, // sq_inplace_repeat
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001586};
1587
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001588static PyMappingMethods RangeAsMapping = {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001589 /* mp_length */ (lenfunc)RangeLength,
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001590 /* mp_subscript */ (binaryfunc)RangeSubscript,
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001591 /* mp_ass_subscript */ (objobjargproc)RangeAsSubscript,
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001592};
1593
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001594// Line range object - Implementation
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001595
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001596 static PyObject *
1597RangeGetattro(PyObject *self, PyObject *nameobj)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001598{
Bram Moolenaar77045652012-09-21 13:46:06 +02001599 GET_ATTR_STRING(name, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001600
1601 if (strcmp(name, "start") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001602 return Py_BuildValue("n", ((RangeObject *)(self))->start - 1);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001603 else if (strcmp(name, "end") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001604 return Py_BuildValue("n", ((RangeObject *)(self))->end - 1);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001605 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001606 return PyObject_GenericGetAttr(self, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001607}
1608
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001609////////////////
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001610
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02001611 static int
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001612RangeAsItem(PyObject *self, Py_ssize_t n, PyObject *val)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001613{
1614 return RBAsItem(((RangeObject *)(self))->buf, n, val,
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001615 ((RangeObject *)(self))->start,
1616 ((RangeObject *)(self))->end,
1617 &((RangeObject *)(self))->end);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001618}
1619
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001620 static Py_ssize_t
1621RangeAsSlice(PyObject *self, Py_ssize_t lo, Py_ssize_t hi, PyObject *val)
1622{
1623 return RBAsSlice(((RangeObject *)(self))->buf, lo, hi, val,
1624 ((RangeObject *)(self))->start,
1625 ((RangeObject *)(self))->end,
1626 &((RangeObject *)(self))->end);
1627}
1628
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001629 static PyObject *
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001630RangeSubscript(PyObject *self, PyObject* idx)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001631{
Bram Moolenaardb913952012-06-29 12:54:53 +02001632 if (PyLong_Check(idx))
1633 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001634 long _idx = PyLong_AsLong(idx);
Bram Moolenaard6e39182013-05-21 18:30:34 +02001635 return RangeItem((RangeObject *)(self), _idx);
Bram Moolenaarebfec1c2023-01-22 21:14:53 +00001636 }
1637 else if (PySlice_Check(idx))
Bram Moolenaardb913952012-06-29 12:54:53 +02001638 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001639 Py_ssize_t start, stop, step, slicelen;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001640
Bram Moolenaar922a4662014-03-30 16:11:43 +02001641 if (PySlice_GetIndicesEx((PySliceObject_T *)idx,
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001642 ((RangeObject *)(self))->end-((RangeObject *)(self))->start+1,
1643 &start, &stop,
Bram Moolenaardb913952012-06-29 12:54:53 +02001644 &step, &slicelen) < 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001645 return NULL;
Bram Moolenaard6e39182013-05-21 18:30:34 +02001646 return RangeSlice((RangeObject *)(self), start, stop);
Bram Moolenaardb913952012-06-29 12:54:53 +02001647 }
1648 else
1649 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001650 RAISE_INVALID_INDEX_TYPE(idx);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001651 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001652 }
1653}
1654
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02001655 static int
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001656RangeAsSubscript(PyObject *self, PyObject *idx, PyObject *val)
1657{
Bram Moolenaardb913952012-06-29 12:54:53 +02001658 if (PyLong_Check(idx))
1659 {
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001660 long n = PyLong_AsLong(idx);
1661 return RangeAsItem(self, n, val);
Bram Moolenaarabab0b02019-03-30 18:47:01 +01001662 }
1663 else if (PySlice_Check(idx))
Bram Moolenaardb913952012-06-29 12:54:53 +02001664 {
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001665 Py_ssize_t start, stop, step, slicelen;
1666
Bram Moolenaar922a4662014-03-30 16:11:43 +02001667 if (PySlice_GetIndicesEx((PySliceObject_T *)idx,
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001668 ((RangeObject *)(self))->end-((RangeObject *)(self))->start+1,
1669 &start, &stop,
Bram Moolenaardb913952012-06-29 12:54:53 +02001670 &step, &slicelen) < 0)
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001671 return -1;
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001672 return RangeAsSlice(self, start, stop, val);
Bram Moolenaardb913952012-06-29 12:54:53 +02001673 }
1674 else
1675 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001676 RAISE_INVALID_INDEX_TYPE(idx);
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001677 return -1;
1678 }
1679}
1680
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001681// TabPage object - Implementation
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001682
1683 static PyObject *
1684TabPageGetattro(PyObject *self, PyObject *nameobj)
1685{
1686 PyObject *r;
1687
1688 GET_ATTR_STRING(name, nameobj);
1689
Bram Moolenaar9e822c02013-05-29 22:15:30 +02001690 if ((r = TabPageAttrValid((TabPageObject *)(self), name)))
1691 return r;
1692
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001693 if (CheckTabPage((TabPageObject *)(self)))
1694 return NULL;
1695
1696 r = TabPageAttr((TabPageObject *)(self), name);
1697 if (r || PyErr_Occurred())
1698 return r;
1699 else
1700 return PyObject_GenericGetAttr(self, nameobj);
1701}
1702
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001703// Window object - Implementation
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001704
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001705 static PyObject *
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001706WindowGetattro(PyObject *self, PyObject *nameobj)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001707{
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001708 PyObject *r;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001709
Bram Moolenaar77045652012-09-21 13:46:06 +02001710 GET_ATTR_STRING(name, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001711
Bram Moolenaar9e822c02013-05-29 22:15:30 +02001712 if ((r = WindowAttrValid((WindowObject *)(self), name)))
1713 return r;
1714
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001715 if (CheckWindow((WindowObject *)(self)))
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001716 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001717
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001718 r = WindowAttr((WindowObject *)(self), name);
1719 if (r || PyErr_Occurred())
1720 return r;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001721 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001722 return PyObject_GenericGetAttr(self, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001723}
1724
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001725 static int
1726WindowSetattro(PyObject *self, PyObject *nameobj, PyObject *val)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001727{
Bram Moolenaar77045652012-09-21 13:46:06 +02001728 GET_ATTR_STRING(name, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001729
Bram Moolenaard6e39182013-05-21 18:30:34 +02001730 return WindowSetattr((WindowObject *)(self), name, val);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001731}
1732
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001733// Tab page list object - Definitions
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001734
1735static PySequenceMethods TabListAsSeq = {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001736 (lenfunc) TabListLength, // sq_length, len(x)
1737 (binaryfunc) 0, // sq_concat, x+y
1738 (ssizeargfunc) 0, // sq_repeat, x*n
1739 (ssizeargfunc) TabListItem, // sq_item, x[i]
1740 0, // sq_slice, x[i:j]
1741 (ssizeobjargproc)0, // sq_as_item, x[i]=v
1742 0, // sq_ass_slice, x[i:j]=v
1743 0, // sq_contains
1744 0, // sq_inplace_concat
1745 0, // sq_inplace_repeat
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001746};
1747
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001748// Window list object - Definitions
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001749
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001750static PySequenceMethods WinListAsSeq = {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001751 (lenfunc) WinListLength, // sq_length, len(x)
1752 (binaryfunc) 0, // sq_concat, x+y
1753 (ssizeargfunc) 0, // sq_repeat, x*n
1754 (ssizeargfunc) WinListItem, // sq_item, x[i]
1755 0, // sq_slice, x[i:j]
1756 (ssizeobjargproc)0, // sq_as_item, x[i]=v
1757 0, // sq_ass_slice, x[i:j]=v
1758 0, // sq_contains
1759 0, // sq_inplace_concat
1760 0, // sq_inplace_repeat
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001761};
1762
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001763/*
1764 * Current items object - Implementation
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001765 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001766 static PyObject *
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001767CurrentGetattro(PyObject *self, PyObject *nameobj)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001768{
Bram Moolenaardd8aca62013-05-29 22:36:10 +02001769 PyObject *r;
Bram Moolenaar77045652012-09-21 13:46:06 +02001770 GET_ATTR_STRING(name, nameobj);
Bram Moolenaardd8aca62013-05-29 22:36:10 +02001771 if (!(r = CurrentGetattr(self, name)))
1772 return PyObject_GenericGetAttr(self, nameobj);
1773 return r;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001774}
1775
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001776 static int
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001777CurrentSetattro(PyObject *self, PyObject *nameobj, PyObject *value)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001778{
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001779 GET_ATTR_STRING(name, nameobj);
1780 return CurrentSetattr(self, name, value);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001781}
1782
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001783// Dictionary object - Definitions
Bram Moolenaardb913952012-06-29 12:54:53 +02001784
Bram Moolenaar66b79852012-09-21 14:00:35 +02001785 static PyObject *
1786DictionaryGetattro(PyObject *self, PyObject *nameobj)
1787{
1788 DictionaryObject *this = ((DictionaryObject *) (self));
1789
1790 GET_ATTR_STRING(name, nameobj);
1791
1792 if (strcmp(name, "locked") == 0)
1793 return PyLong_FromLong(this->dict->dv_lock);
1794 else if (strcmp(name, "scope") == 0)
1795 return PyLong_FromLong(this->dict->dv_scope);
1796
1797 return PyObject_GenericGetAttr(self, nameobj);
1798}
1799
1800 static int
1801DictionarySetattro(PyObject *self, PyObject *nameobj, PyObject *val)
1802{
1803 GET_ATTR_STRING(name, nameobj);
Bram Moolenaard6e39182013-05-21 18:30:34 +02001804 return DictionarySetattr((DictionaryObject *)(self), name, val);
Bram Moolenaardb913952012-06-29 12:54:53 +02001805}
1806
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001807// List object - Definitions
Bram Moolenaardb913952012-06-29 12:54:53 +02001808
Bram Moolenaar66b79852012-09-21 14:00:35 +02001809 static PyObject *
1810ListGetattro(PyObject *self, PyObject *nameobj)
1811{
1812 GET_ATTR_STRING(name, nameobj);
1813
1814 if (strcmp(name, "locked") == 0)
1815 return PyLong_FromLong(((ListObject *) (self))->list->lv_lock);
1816
1817 return PyObject_GenericGetAttr(self, nameobj);
1818}
1819
1820 static int
1821ListSetattro(PyObject *self, PyObject *nameobj, PyObject *val)
1822{
1823 GET_ATTR_STRING(name, nameobj);
Bram Moolenaard6e39182013-05-21 18:30:34 +02001824 return ListSetattr((ListObject *)(self), name, val);
Bram Moolenaardb913952012-06-29 12:54:53 +02001825}
1826
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001827// Function object - Definitions
Bram Moolenaardb913952012-06-29 12:54:53 +02001828
Bram Moolenaardb913952012-06-29 12:54:53 +02001829 static PyObject *
1830FunctionGetattro(PyObject *self, PyObject *nameobj)
1831{
Bram Moolenaar8110a092016-04-14 15:56:09 +02001832 PyObject *r;
Bram Moolenaardb913952012-06-29 12:54:53 +02001833 FunctionObject *this = (FunctionObject *)(self);
Bram Moolenaar77045652012-09-21 13:46:06 +02001834
1835 GET_ATTR_STRING(name, nameobj);
Bram Moolenaardb913952012-06-29 12:54:53 +02001836
Bram Moolenaar8110a092016-04-14 15:56:09 +02001837 r = FunctionAttr(this, name);
1838 if (r || PyErr_Occurred())
1839 return r;
1840 else
1841 return PyObject_GenericGetAttr(self, nameobj);
Bram Moolenaardb913952012-06-29 12:54:53 +02001842}
1843
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001844// External interface
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001845
1846 void
1847python3_buffer_free(buf_T *buf)
1848{
Yegappan Lakshmanan0233bdf2023-01-12 12:33:30 +00001849 BufferObject *bp = BUF_PYTHON_REF(buf);
1850 if (bp == NULL)
1851 return;
1852 bp->buf = INVALID_BUFFER_VALUE;
1853 BUF_PYTHON_REF(buf) = NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001854}
1855
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001856 void
1857python3_window_free(win_T *win)
1858{
Yegappan Lakshmanan0233bdf2023-01-12 12:33:30 +00001859 WindowObject *wp = WIN_PYTHON_REF(win);
1860 if (wp == NULL)
1861 return;
1862 wp->win = INVALID_WINDOW_VALUE;
1863 WIN_PYTHON_REF(win) = NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001864}
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001865
1866 void
1867python3_tabpage_free(tabpage_T *tab)
1868{
Yegappan Lakshmanan0233bdf2023-01-12 12:33:30 +00001869 TabPageObject *tp = TAB_PYTHON_REF(tab);
1870 if (tp == NULL)
1871 return;
1872 tp->tab = INVALID_TABPAGE_VALUE;
1873 TAB_PYTHON_REF(tab) = NULL;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001874}
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001875
Bram Moolenaar7854e3a2012-11-28 15:33:14 +01001876 static PyObject *
1877Py3Init_vim(void)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001878{
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001879 // The special value is removed from sys.path in Python3_Init().
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001880 static wchar_t *(argv[2]) = {L"/must>not&exist/foo", NULL};
1881
Bram Moolenaar1dc28782013-05-21 19:11:01 +02001882 if (init_types())
1883 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001884
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001885 // Set sys.argv[] to avoid a crash in warn().
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001886 PySys_SetArgv(1, argv);
1887
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001888 if ((vim_module = PyModule_Create(&vimmodule)) == NULL)
Bram Moolenaar19e60942011-06-19 00:27:51 +02001889 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001890
Bram Moolenaardee2e312013-06-23 16:35:47 +02001891 if (populate_module(vim_module))
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001892 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001893
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001894 if (init_sys_path())
1895 return NULL;
1896
1897 return vim_module;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001898}
1899
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001900//////////////////////////////////////////////////////////////////////////
1901// 4. Utility functions for handling the interface between Vim and Python.
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001902
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001903/*
1904 * Convert a Vim line into a Python string.
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001905 * All internal newlines are replaced by null characters.
1906 *
1907 * On errors, the Python exception data is set, and NULL is returned.
1908 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001909 static PyObject *
1910LineToString(const char *str)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001911{
1912 PyObject *result;
1913 Py_ssize_t len = strlen(str);
Bram Moolenaard672dde2020-02-26 13:43:51 +01001914 char *tmp, *p;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001915
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001916 tmp = alloc(len + 1);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001917 p = tmp;
1918 if (p == NULL)
1919 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001920 PyErr_NoMemory();
1921 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001922 }
1923
1924 while (*str)
1925 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001926 if (*str == '\n')
1927 *p = '\0';
1928 else
1929 *p = *str;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001930
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001931 ++p;
1932 ++str;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001933 }
1934 *p = '\0';
1935
Bram Moolenaar2e2f52a2020-12-21 16:03:02 +01001936 result = PyUnicode_Decode(tmp, len, (char *)ENC_OPT, ERRORS_DECODE_ARG);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001937
1938 vim_free(tmp);
1939 return result;
1940}
1941
Bram Moolenaardb913952012-06-29 12:54:53 +02001942 void
Bram Moolenaar8e9a24a2019-03-19 22:22:55 +01001943do_py3eval(char_u *str, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +02001944{
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02001945 DoPyCommand((char *) str,
1946 (rangeinitializer) init_range_eval,
1947 (runner) run_eval,
1948 (void *) rettv);
Bram Moolenaar8e9a24a2019-03-19 22:22:55 +01001949 if (rettv->v_type == VAR_UNKNOWN)
Bram Moolenaardb913952012-06-29 12:54:53 +02001950 {
Bram Moolenaar8e9a24a2019-03-19 22:22:55 +01001951 rettv->v_type = VAR_NUMBER;
1952 rettv->vval.v_number = 0;
Bram Moolenaardb913952012-06-29 12:54:53 +02001953 }
1954}
1955
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01001956 int
Bram Moolenaar8e9a24a2019-03-19 22:22:55 +01001957set_ref_in_python3(int copyID)
Bram Moolenaardb913952012-06-29 12:54:53 +02001958{
Bram Moolenaarb641df42015-02-03 13:16:04 +01001959 return set_ref_in_py(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02001960}