/* vi:set ts=8 sts=4 sw=4: * * VIM - Vi IMproved by Bram Moolenaar * * Do ":help uganda" in Vim to read copying and usage conditions. * Do ":help credits" in Vim to see a list of people who contributed. * See README.txt for an overview of the Vim source code. */ /* * Python extensions by Paul Moore. * Changes for Unix by David Leonard. * * This consists of four parts: * 1. Python interpreter main program * 2. Python output stream: writes output via [e]msg(). * 3. Implementation of the Vim module for Python * 4. Utility functions for handling the interface between Vim and Python. */ /* * Roland Puntaier 2009/sept/16: * Adaptations to support both python3.x and python2.x */ /* uncomment this if used with the debug version of python */ /* #define Py_DEBUG */ /* Note: most of time you can add -DPy_DEBUG to CFLAGS in place of uncommenting */ /* uncomment this if used with the debug version of python, but without its * allocator */ /* #define Py_DEBUG_NO_PYMALLOC */ #include "vim.h" #include /* Python.h defines _POSIX_THREADS itself (if needed) */ #ifdef _POSIX_THREADS # undef _POSIX_THREADS #endif #if defined(_WIN32) && defined(HAVE_FCNTL_H) # undef HAVE_FCNTL_H #endif #ifdef _DEBUG # undef _DEBUG #endif #ifdef F_BLANK # undef F_BLANK #endif #ifdef HAVE_STDARG_H # undef HAVE_STDARG_H /* Python's config.h defines it as well. */ #endif #ifdef _POSIX_C_SOURCE /* defined in feature.h */ # undef _POSIX_C_SOURCE #endif #ifdef _XOPEN_SOURCE # undef _XOPEN_SOURCE /* pyconfig.h defines it as well. */ #endif #define PY_SSIZE_T_CLEAN #include #if defined(MACOS) && !defined(MACOS_X_UNIX) # include "macglue.h" # include #endif #undef main /* Defined in python.h - aargh */ #undef HAVE_FCNTL_H /* Clash with os_win32.h */ /* The "surrogateescape" error handler is new in Python 3.1 */ #if PY_VERSION_HEX >= 0x030100f0 # define CODEC_ERROR_HANDLER "surrogateescape" #else # define CODEC_ERROR_HANDLER NULL #endif /* Python 3 does not support CObjects, always use Capsules */ #define PY_USE_CAPSULE #define PyInt Py_ssize_t #ifndef PyString_Check # define PyString_Check(obj) PyUnicode_Check(obj) #endif #define PyString_FromString(repr) \ PyUnicode_Decode(repr, STRLEN(repr), ENC_OPT, NULL) #define PyString_FromFormat PyUnicode_FromFormat #ifndef PyInt_Check # define PyInt_Check(obj) PyLong_Check(obj) #endif #define PyInt_FromLong(i) PyLong_FromLong(i) #define PyInt_AsLong(obj) PyLong_AsLong(obj) #define Py_ssize_t_fmt "n" #define Py_bytes_fmt "y" #define PyIntArgFunc ssizeargfunc #define PyIntObjArgProc ssizeobjargproc /* * PySlice_GetIndicesEx(): first argument type changed from PySliceObject * to PyObject in Python 3.2 or later. */ #if PY_VERSION_HEX >= 0x030200f0 typedef PyObject PySliceObject_T; #else typedef PySliceObject PySliceObject_T; #endif #if defined(DYNAMIC_PYTHON3) || defined(PROTO) # ifndef WIN3264 # include # define FARPROC void* # define HINSTANCE void* # if defined(PY_NO_RTLD_GLOBAL) && defined(PY3_NO_RTLD_GLOBAL) # define load_dll(n) dlopen((n), RTLD_LAZY) # else # define load_dll(n) dlopen((n), RTLD_LAZY|RTLD_GLOBAL) # endif # define close_dll dlclose # define symbol_from_dll dlsym # else # define load_dll vimLoadLib # define close_dll FreeLibrary # define symbol_from_dll GetProcAddress # endif /* * Wrapper defines */ # undef PyArg_Parse # define PyArg_Parse py3_PyArg_Parse # undef PyArg_ParseTuple # define PyArg_ParseTuple py3_PyArg_ParseTuple # define PyMem_Free py3_PyMem_Free # define PyMem_Malloc py3_PyMem_Malloc # define PyDict_SetItemString py3_PyDict_SetItemString # define PyErr_BadArgument py3_PyErr_BadArgument # define PyErr_Clear py3_PyErr_Clear # define PyErr_Format py3_PyErr_Format # define PyErr_PrintEx py3_PyErr_PrintEx # define PyErr_NoMemory py3_PyErr_NoMemory # define PyErr_Occurred py3_PyErr_Occurred # define PyErr_SetNone py3_PyErr_SetNone # define PyErr_SetString py3_PyErr_SetString # define PyErr_SetObject py3_PyErr_SetObject # define PyErr_ExceptionMatches py3_PyErr_ExceptionMatches # define PyEval_InitThreads py3_PyEval_InitThreads # define PyEval_RestoreThread py3_PyEval_RestoreThread # define PyEval_SaveThread py3_PyEval_SaveThread # define PyGILState_Ensure py3_PyGILState_Ensure # define PyGILState_Release py3_PyGILState_Release # define PyLong_AsLong py3_PyLong_AsLong # define PyLong_FromLong py3_PyLong_FromLong # define PyList_GetItem py3_PyList_GetItem # define PyList_Append py3_PyList_Append # define PyList_Insert py3_PyList_Insert # define PyList_New py3_PyList_New # define PyList_SetItem py3_PyList_SetItem # define PyList_Size py3_PyList_Size # define PySequence_Check py3_PySequence_Check # define PySequence_Size py3_PySequence_Size # define PySequence_GetItem py3_PySequence_GetItem # define PySequence_Fast py3_PySequence_Fast # define PyTuple_Size py3_PyTuple_Size # define PyTuple_GetItem py3_PyTuple_GetItem # define PySlice_GetIndicesEx py3_PySlice_GetIndicesEx # define PyImport_ImportModule py3_PyImport_ImportModule # define PyObject_Init py3__PyObject_Init # define PyDict_New py3_PyDict_New # define PyDict_GetItemString py3_PyDict_GetItemString # define PyDict_Next py3_PyDict_Next # define PyMapping_Check py3_PyMapping_Check # ifndef PyMapping_Keys # define PyMapping_Keys py3_PyMapping_Keys # endif # define PyIter_Next py3_PyIter_Next # define PyObject_GetIter py3_PyObject_GetIter # define PyObject_Repr py3_PyObject_Repr # define PyObject_GetItem py3_PyObject_GetItem # define PyObject_IsTrue py3_PyObject_IsTrue # define PyModule_GetDict py3_PyModule_GetDict #undef PyRun_SimpleString # define PyRun_SimpleString py3_PyRun_SimpleString #undef PyRun_String # define PyRun_String py3_PyRun_String # define PyObject_GetAttrString py3_PyObject_GetAttrString # define PyObject_HasAttrString py3_PyObject_HasAttrString # define PyObject_SetAttrString py3_PyObject_SetAttrString # define PyObject_CallFunctionObjArgs py3_PyObject_CallFunctionObjArgs # define _PyObject_CallFunction_SizeT py3__PyObject_CallFunction_SizeT # define PyObject_Call py3_PyObject_Call # define PyEval_GetLocals py3_PyEval_GetLocals # define PyEval_GetGlobals py3_PyEval_GetGlobals # define PySys_SetObject py3_PySys_SetObject # define PySys_GetObject py3_PySys_GetObject # define PySys_SetArgv py3_PySys_SetArgv # define PyType_Ready py3_PyType_Ready #undef Py_BuildValue # define Py_BuildValue py3_Py_BuildValue # define Py_SetPythonHome py3_Py_SetPythonHome # define Py_Initialize py3_Py_Initialize # define Py_Finalize py3_Py_Finalize # define Py_IsInitialized py3_Py_IsInitialized # define _Py_NoneStruct (*py3__Py_NoneStruct) # define _Py_FalseStruct (*py3__Py_FalseStruct) # define _Py_TrueStruct (*py3__Py_TrueStruct) # define _PyObject_NextNotImplemented (*py3__PyObject_NextNotImplemented) # define PyModule_AddObject py3_PyModule_AddObject # define PyImport_AppendInittab py3_PyImport_AppendInittab # define PyImport_AddModule py3_PyImport_AddModule # if PY_VERSION_HEX >= 0x030300f0 # undef _PyUnicode_AsString # define _PyUnicode_AsString py3_PyUnicode_AsUTF8 # else # define _PyUnicode_AsString py3__PyUnicode_AsString # endif # undef PyUnicode_AsEncodedString # define PyUnicode_AsEncodedString py3_PyUnicode_AsEncodedString # undef PyBytes_AsString # define PyBytes_AsString py3_PyBytes_AsString # ifndef PyBytes_AsStringAndSize # define PyBytes_AsStringAndSize py3_PyBytes_AsStringAndSize # endif # undef PyBytes_FromString # define PyBytes_FromString py3_PyBytes_FromString # define PyFloat_FromDouble py3_PyFloat_FromDouble # define PyFloat_AsDouble py3_PyFloat_AsDouble # define PyObject_GenericGetAttr py3_PyObject_GenericGetAttr # define PyType_Type (*py3_PyType_Type) # define PySlice_Type (*py3_PySlice_Type) # define PyFloat_Type (*py3_PyFloat_Type) # define PyNumber_Check (*py3_PyNumber_Check) # define PyNumber_Long (*py3_PyNumber_Long) # define PyBool_Type (*py3_PyBool_Type) # define PyErr_NewException py3_PyErr_NewException # ifdef Py_DEBUG # define _Py_NegativeRefcount py3__Py_NegativeRefcount # define _Py_RefTotal (*py3__Py_RefTotal) # define _Py_Dealloc py3__Py_Dealloc # define PyModule_Create2TraceRefs py3_PyModule_Create2TraceRefs # else # define PyModule_Create2 py3_PyModule_Create2 # endif # if defined(Py_DEBUG) && !defined(Py_DEBUG_NO_PYMALLOC) # define _PyObject_DebugMalloc py3__PyObject_DebugMalloc # define _PyObject_DebugFree py3__PyObject_DebugFree # else # define PyObject_Malloc py3_PyObject_Malloc # define PyObject_Free py3_PyObject_Free # endif # define _PyObject_GC_New py3__PyObject_GC_New # define PyObject_GC_Del py3_PyObject_GC_Del # define PyObject_GC_UnTrack py3_PyObject_GC_UnTrack # define PyType_GenericAlloc py3_PyType_GenericAlloc # define PyType_GenericNew py3_PyType_GenericNew # undef PyUnicode_FromString # define PyUnicode_FromString py3_PyUnicode_FromString # ifndef PyUnicode_FromFormat # define PyUnicode_FromFormat py3_PyUnicode_FromFormat # else # define Py_UNICODE_USE_UCS_FUNCTIONS # ifdef Py_UNICODE_WIDE # define PyUnicodeUCS4_FromFormat py3_PyUnicodeUCS4_FromFormat # else # define PyUnicodeUCS2_FromFormat py3_PyUnicodeUCS2_FromFormat # endif # endif # undef PyUnicode_Decode # define PyUnicode_Decode py3_PyUnicode_Decode # define PyType_IsSubtype py3_PyType_IsSubtype # define PyCapsule_New py3_PyCapsule_New # define PyCapsule_GetPointer py3_PyCapsule_GetPointer # if defined(Py_DEBUG) && !de
/*
 * Generic implementation of 64-bit atomics using spinlocks,
 * useful on processors that don't have 64-bit atomic instructions.
 *
 * Copyright © 2009 Paul Mackerras, IBM Corp. <paulus@au1.ibm.com>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version
 * 2 of the License, or (at your option) any later version.
 */
#include <linux/types.h>
#include <linux/cache.h>
#include <linux/spinlock.h>
#include <linux/init.h>
#include <linux/export.h>
#include <linux/atomic.h>

/*
 * We use a hashed array of spinlocks to provide exclusive access
 * to each atomic64_t variable.  Since this is expected to used on
 * systems with small numbers of CPUs (<= 4 or so), we use a
 * relatively small array of 16 spinlocks to avoid wasting too much
 * memory on the spinlock array.
 */
#define NR_LOCKS	16

/*
 * Ensure each lock is in a separate cacheline.
 */
static union {
	raw_spinlock_t lock;
	char pad[L1_CACHE_BYTES];
} atomic64_lock[NR_LOCKS] __cacheline_aligned_in_smp = {
	[0 ... (NR_LOCKS - 1)] = {
		.lock =  __RAW_SPIN_LOCK_UNLOCKED(atomic64_lock.lock),
	},
};

static inline raw_spinlock_t *lock_addr(const atomic64_t *v)
{
	unsigned long addr = (unsigned long) v;

	addr >>= L1_CACHE_SHIFT;
	addr ^= (addr >> 8) ^ (addr >> 16);
	return &atomic64_lock[addr & (NR_LOCKS - 1)].lock;
}

long long atomic64_read(const atomic64_t *v)
{
	unsigned long flags;
	raw_spinlock_t *lock = lock_addr(v);
	long long val;

	raw_spin_lock_irqsave(lock, flags);
	val = v->counter;
	raw_spin_unlock_irqrestore(lock, flags);
	return val;
}
EXPORT_SYMBOL(atomic64_read);

void atomic64_set(atomic64_t *v, long long i)
{
	unsigned long flags;
	raw_spinlock_t *lock = lock_addr(v);

	raw_spin_lock_irqsave(lock, flags);
	v->counter = i;
	raw_spin_unlock_irqrestore(lock, flags);
}
EXPORT_SYMBOL(atomic64_set);

#define ATOMIC64_OP(op, c_op)						\
void atomic64_##op(long long a, atomic64_t *v)				\
{									\
	unsigned long flags;						\
	raw_spinlock_t *lock = lock_addr(v);				\
									\
	raw_spin_lock_irqsave(lock, flags);				\
	v->counter c_op a;						\
	raw_spin_unlock_irqrestore(lock, flags);			\
}									\
EXPORT_SYMBOL(atomic64_##op);

#define ATOMIC64_OP_RETURN(op, c_op)					\
long long atomic64_##op##_return(long long a, atomic64_t *v)		\
{									\
	unsigned long flags;						\
	raw_spinlock_t *lock = lock_addr(v);				\
	long long val;							\
									\
	raw_spin_lock_irqsave(lock, flags);				\
	val = (v->counter c_op a);					\
	raw_spin_unlock_irqrestore(lock, flags);			\
	return val;							\
}									\
EXPORT_SYMBOL(atomic64_##op##_return);

#define ATOMIC64_FETCH_OP(op, c_op)					\
long long atomic64_fetch_##op(long long a, atomic64_t *v)		\
{									\
	unsigned long flags;						\
	raw_spinlock_t *lock = lock_addr(v);				\
	long long val;							\
									\
	raw_spin_lock_irqsave(lock, flags);				\
	val = v->counter;						\
	v->counter c_op a;						\
	raw_spin_unlock_irqrestore(lock, flags);			\
	return val;							\
}									\
EXPORT_SYMBOL(atomic64_fetch_##op);

#define ATOMIC64_OPS(op, c_op)						\
	ATOMIC64_OP(op, c_op)						\
	ATOMIC64_OP_RETURN(op, c_op)					\
	ATOMIC64_FETCH_OP(op, c_op)

ATOMIC64_OPS(add, +=)
ATOMIC64_OPS(sub, -=)

#undef ATOMIC64_OPS
#define ATOMIC64_OPS(op, c_op)						\
	ATOMIC64_OP(op, c_op)						\
	ATOMIC64_OP_RETURN(op, c_op)					\
	ATOMIC64_FETCH_OP(op, c_op)

ATOMIC64_OPS(and, &=)
ATOMIC64_OPS(or, |=)
ATOMIC64_OPS(xor, ^=)

#undef ATOMIC64_OPS
#undef ATOMIC64_FETCH_OP
#undef ATOMIC64_OP_RETURN
#undef ATOMIC64_OP

long long atomic64_dec_if_positive(atomic64_t *v)
{
	unsigned long flags;
	raw_spinlock_t *lock = lock_addr(v);
	long long val;

	raw_spin_lock_irqsave(lock, flags);
	val = v->counter - 1;
	if (val >= 0)
		v->counter = val;
	raw_spin_unlock_irqrestore(lock, flags);
	return val;
}
EXPORT_SYMBOL(atomic64_dec_if_positive);

long long atomic64_cmpxchg(atomic64_t *v, long long o, long long n)
{
	unsigned long flags;
	raw_spinlock_t *lock = lock_addr(v);
	long long val;

	raw_spin_lock_irqsave(lock, flags);
	val = v->counter;
	if (val == o)
		v->counter = n;
	raw_spin_unlock_irqrestore(lock, flags);
	return val;
}
EXPORT_SYMBOL(atomic64_cmpxchg);

long long atomic64_xchg(atomic64_t *v, long long new)
{
	unsigned long flags;
	raw_spinlock_t *lock = lock_addr(v);
	long long val;

	raw_spin_lock_irqsave(lock, flags);
	val = v->counter;
	v->counter = new;
	raw_spin_unlock_irqrestore(lock, flags);
	return val;
}
EXPORT_SYMBOL(atomic64_xchg);

int atomic64_add_unless(atomic64_t *v, long long a, long long u)
{
	unsigned long flags;
	raw_spinlock_t *lock = lock_addr(v);
	int ret = 0;

	raw_spin_lock_irqsave(lock, flags);
	if (v->counter != u) {
		v->counter += a;
		ret = 1;
	}
	raw_spin_unlock_irqrestore(lock, flags);
	return ret;
}
EXPORT_SYMBOL(atomic64_add_unless);
b_type == &WindowType) /* Buffer type - Implementation functions * -------------------------------------- */ #define BufferType_Check(obj) ((obj)->ob_base.ob_type == &BufferType) static PyObject* BufferSubscript(PyObject *self, PyObject *idx); static Py_ssize_t BufferAsSubscript(PyObject *self, PyObject *idx, PyObject *val); /* Line range type - Implementation functions * -------------------------------------- */ #define RangeType_Check(obj) ((obj)->ob_base.ob_type == &RangeType) static PyObject* RangeSubscript(PyObject *self, PyObject *idx); static Py_ssize_t RangeAsItem(PyObject *, Py_ssize_t, PyObject *); static Py_ssize_t RangeAsSubscript(PyObject *self, PyObject *idx, PyObject *val); /* Current objects type - Implementation functions * ----------------------------------------------- */ static PySequenceMethods BufferAsSeq = { (lenfunc) BufferLength, /* sq_length, len(x) */ (binaryfunc) 0, /* sq_concat, x+y */ (ssizeargfunc) 0, /* sq_repeat, x*n */ (ssizeargfunc) BufferItem, /* sq_item, x[i] */ 0, /* was_sq_slice, x[i:j] */ 0, /* sq_ass_item, x[i]=v */ 0, /* sq_ass_slice, x[i:j]=v */ 0, /* sq_contains */ 0, /* sq_inplace_concat */ 0, /* sq_inplace_repeat */ }; static PyMappingMethods BufferAsMapping = { /* mp_length */ (lenfunc)BufferLength, /* mp_subscript */ (binaryfunc)BufferSubscript, /* mp_ass_subscript */ (objobjargproc)BufferAsSubscript, }; /* Buffer object */ static PyObject * BufferGetattro(PyObject *self, PyObject *nameobj) { PyObject *r; GET_ATTR_STRING(name, nameobj); if ((r = BufferAttrValid((BufferObject *)(self), name))) return r; if (CheckBuffer((BufferObject *)(self))) return NULL; r = BufferAttr((BufferObject *)(self), name); if (r || PyErr_Occurred()) return r; else return PyObject_GenericGetAttr(self, nameobj); } static int BufferSetattro(PyObject *self, PyObject *nameobj, PyObject *val) { GET_ATTR_STRING(name, nameobj); return BufferSetattr((BufferObject *)(self), name, val); } /******************/ static PyObject * BufferSubscript(PyObject *self, PyObject* idx) { if (PyLong_Check(idx)) { long _idx = PyLong_AsLong(idx); return BufferItem((BufferObject *)(self), _idx); } else if (PySlice_Check(idx)) { Py_ssize_t start, stop, step, slicelen; if (CheckBuffer((BufferObject *) self)) return NULL; if (PySlice_GetIndicesEx((PySliceObject_T *)idx, (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count, &start, &stop, &step, &slicelen) < 0) { return NULL; } return BufferSlice((BufferObject *)(self), start, stop); } else { RAISE_INVALID_INDEX_TYPE(idx); return NULL; } } static Py_ssize_t BufferAsSubscript(PyObject *self, PyObject* idx, PyObject* val) { if (PyLong_Check(idx)) { long n = PyLong_AsLong(idx); return RBAsItem((BufferObject *)(self), n, val, 1, (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count, NULL); } else if (PySlice_Check(idx)) { Py_ssize_t start, stop, step, slicelen; if (CheckBuffer((BufferObject *) self)) return -1; if (PySlice_GetIndicesEx((PySliceObject_T *)idx, (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count, &start, &stop, &step, &slicelen) < 0) { return -1; } return RBAsSlice((BufferObject *)(self), start, stop, val, 1, (PyInt)((BufferObject *)(self))->buf->b_ml.ml_line_count, NULL); } else { RAISE_INVALID_INDEX_TYPE(idx); return -1; } } static PySequenceMethods RangeAsSeq = { (lenfunc) RangeLength, /* sq_length, len(x) */ (binaryfunc) 0, /* RangeConcat, sq_concat, x+y */ (ssizeargfunc) 0, /* RangeRepeat, sq_repeat, x*n */ (ssizeargfunc) RangeItem, /* sq_item, x[i] */ 0, /* was_sq_slice, x[i:j] */ (ssizeobjargproc) RangeAsItem, /* sq_as_item, x[i]=v */ 0, /* sq_ass_slice, x[i:j]=v */ 0, /* sq_contains */ 0, /* sq_inplace_concat */ 0, /* sq_inplace_repeat */ }; static PyMappingMethods RangeAsMapping = { /* mp_length */ (lenfunc)RangeLength, /* mp_subscript */ (binaryfunc)RangeSubscript, /* mp_ass_subscript */ (objobjargproc)RangeAsSubscript, }; /* Line range object - Implementation */ static PyObject * RangeGetattro(PyObject *self, PyObject *nameobj) { GET_ATTR_STRING(name, nameobj); if (strcmp(name, "start") == 0) return Py_BuildValue("n", ((RangeObject *)(self))->start - 1); else if (strcmp(name, "end") == 0) return Py_BuildValue("n", ((RangeObject *)(self))->end - 1); else return PyObject_GenericGetAttr(self, nameobj); } /****************/ static Py_ssize_t RangeAsItem(PyObject *self, Py_ssize_t n, PyObject *val) { return RBAsItem(((RangeObject *)(self))->buf, n, val, ((RangeObject *)(self))->start, ((RangeObject *)(self))->end, &((RangeObject *)(self))->end); } static Py_ssize_t RangeAsSlice(PyObject *self, Py_ssize_t lo, Py_ssize_t hi, PyObject *val) { return RBAsSlice(((RangeObject *)(self))->buf, lo, hi, val, ((RangeObject *)(self))->start, ((RangeObject *)(self))->end, &((RangeObject *)(self))->end); } static PyObject * RangeSubscript(PyObject *self, PyObject* idx) { if (PyLong_Check(idx)) { long _idx = PyLong_AsLong(idx); return RangeItem((RangeObject *)(self), _idx); } else if (PySlice_Check(idx)) { Py_ssize_t start, stop, step, slicelen; if (PySlice_GetIndicesEx((PySliceObject_T *)idx, ((RangeObject *)(self))->end-((RangeObject *)(self))->start+1, &start, &stop, &step, &slicelen) < 0) { return NULL; } return RangeSlice((RangeObject *)(self), start, stop); } else { RAISE_INVALID_INDEX_TYPE(idx); return NULL; } } static Py_ssize_t RangeAsSubscript(PyObject *self, PyObject *idx, PyObject *val) { if (PyLong_Check(idx)) { long n = PyLong_AsLong(idx); return RangeAsItem(self, n, val); } else if (PySlice_Check(idx)) { Py_ssize_t start, stop, step, slicelen; if (PySlice_GetIndicesEx((PySliceObject_T *)idx, ((RangeObject *)(self))->end-((RangeObject *)(self))->start+1, &start, &stop, &step, &slicelen) < 0) { return -1; } return RangeAsSlice(self, start, stop, val); } else { RAISE_INVALID_INDEX_TYPE(idx); return -1; } } /* TabPage object - Implementation */ static PyObject * TabPageGetattro(PyObject *self, PyObject *nameobj) { PyObject *r; GET_ATTR_STRING(name, nameobj); if ((r = TabPageAttrValid((TabPageObject *)(self), name))) return r; if (CheckTabPage((TabPageObject *)(self))) return NULL; r = TabPageAttr((TabPageObject *)(self), name); if (r || PyErr_Occurred()) return r; else return PyObject_GenericGetAttr(self, nameobj); } /* Window object - Implementation */ static PyObject * WindowGetattro(PyObject *self, PyObject *nameobj) { PyObject *r; GET_ATTR_STRING(name, nameobj); if ((r = WindowAttrValid((WindowObject *)(self), name))) return r; if (CheckWindow((WindowObject *)(self))) return NULL; r = WindowAttr((WindowObject *)(self), name); if (r || PyErr_Occurred()) return r; else return PyObject_GenericGetAttr(self, nameobj); } static int WindowSetattro(PyObject *self, PyObject *nameobj, PyObject *val) { GET_ATTR_STRING(name, nameobj); return WindowSetattr((WindowObject *)(self), name, val); } /* Tab page list object - Definitions */ static PySequenceMethods TabListAsSeq = { (lenfunc) TabListLength, /* sq_length, len(x) */ (binaryfunc) 0, /* sq_concat, x+y */ (ssizeargfunc) 0, /* sq_repeat, x*n */ (ssizeargfunc) TabListItem, /* sq_item, x[i] */ 0, /* sq_slice, x[i:j] */ (ssizeobjargproc)0, /* sq_as_item, x[i]=v */ 0, /* sq_ass_slice, x[i:j]=v */ 0, /* sq_contains */ 0, /* sq_inplace_concat */ 0, /* sq_inplace_repeat */ }; /* Window list object - Definitions */ static PySequenceMethods WinListAsSeq = { (lenfunc) WinListLength, /* sq_length, len(x) */ (binaryfunc) 0, /* sq_concat, x+y */ (ssizeargfunc) 0, /* sq_repeat, x*n */ (ssizeargfunc) WinListItem, /* sq_item, x[i] */ 0, /* sq_slice, x[i:j] */ (ssizeobjargproc)0, /* sq_as_item, x[i]=v */ 0, /* sq_ass_slice, x[i:j]=v */ 0, /* sq_contains */ 0, /* sq_inplace_concat */ 0, /* sq_inplace_repeat */ }; /* Current items object - Implementation */ static PyObject * CurrentGetattro(PyObject *self, PyObject *nameobj) { PyObject *r; GET_ATTR_STRING(name, nameobj); if (!(r = CurrentGetattr(self, name))) return PyObject_GenericGetAttr(self, nameobj); return r; } static int CurrentSetattro(PyObject *self, PyObject *nameobj, PyObject *value) { GET_ATTR_STRING(name, nameobj); return CurrentSetattr(self, name, value); } /* Dictionary object - Definitions */ static PyObject * DictionaryGetattro(PyObject *self, PyObject *nameobj) { DictionaryObject *this = ((DictionaryObject *) (self)); GET_ATTR_STRING(name, nameobj); if (strcmp(name, "locked") == 0) return PyLong_FromLong(this->dict->dv_lock); else if (strcmp(name, "scope") == 0) return PyLong_FromLong(this->dict->dv_scope); return PyObject_GenericGetAttr(self, nameobj); } static int DictionarySetattro(PyObject *self, PyObject *nameobj, PyObject *val) { GET_ATTR_STRING(name, nameobj); return DictionarySetattr((DictionaryObject *)(self), name, val); } /* List object - Definitions */ static PyObject * ListGetattro(PyObject *self, PyObject *nameobj) { GET_ATTR_STRING(name, nameobj); if (strcmp(name, "locked") == 0) return PyLong_FromLong(((ListObject *) (self))->list->lv_lock); return PyObject_GenericGetAttr(self, nameobj); } static int ListSetattro(PyObject *self, PyObject *nameobj, PyObject *val) { GET_ATTR_STRING(name, nameobj); return ListSetattr((ListObject *)(self), name, val); } /* Function object - Definitions */ static PyObject * FunctionGetattro(PyObject *self, PyObject *nameobj) { FunctionObject *this = (FunctionObject *)(self); GET_ATTR_STRING(name, nameobj); if (strcmp(name, "name") == 0) return PyUnicode_FromString((char *)(this->name)); return PyObject_GenericGetAttr(self, nameobj); } /* External interface */ void python3_buffer_free(buf_T *buf) { if (BUF_PYTHON_REF(buf) != NULL) { BufferObject *bp = BUF_PYTHON_REF(buf); bp->buf = INVALID_BUFFER_VALUE; BUF_PYTHON_REF(buf) = NULL; } } #if defined(FEAT_WINDOWS) || defined(PROTO) void python3_window_free(win_T *win) { if (WIN_PYTHON_REF(win) != NULL) { WindowObject *wp = WIN_PYTHON_REF(win); wp->win = INVALID_WINDOW_VALUE; WIN_PYTHON_REF(win) = NULL; } } void python3_tabpage_free(tabpage_T *tab) { if (TAB_PYTHON_REF(tab) != NULL) { TabPageObject *tp = TAB_PYTHON_REF(tab); tp->tab = INVALID_TABPAGE_VALUE; TAB_PYTHON_REF(tab) = NULL; } } #endif static PyObject * Py3Init_vim(void) { /* The special value is removed from sys.path in Python3_Init(). */ static wchar_t *(argv[2]) = {L"/must>not&exist/foo", NULL}; if (init_types()) return NULL; /* Set sys.argv[] to avoid a crash in warn(). */ PySys_SetArgv(1, argv); if ((vim_module = PyModule_Create(&vimmodule)) == NULL) return NULL; if (populate_module(vim_module)) return NULL; if (init_sys_path()) return NULL; return vim_module; } /************************************************************************* * 4. Utility functions for handling the interface between Vim and Python. */ /* Convert a Vim line into a Python string. * All internal newlines are replaced by null characters. * * On errors, the Python exception data is set, and NULL is returned. */ static PyObject * LineToString(const char *str) { PyObject *result; Py_ssize_t len = strlen(str); char *tmp,*p; tmp = (char *)alloc((unsigned)(len+1)); p = tmp; if (p == NULL) { PyErr_NoMemory(); return NULL; } while (*str) { if (*str == '\n') *p = '\0'; else *p = *str; ++p; ++str; } *p = '\0'; result = PyUnicode_Decode(tmp, len, (char *)ENC_OPT, CODEC_ERROR_HANDLER); vim_free(tmp); return result; } void do_py3eval (char_u *str, typval_T *rettv) { DoPyCommand((char *) str, (rangeinitializer) init_range_eval, (runner) run_eval, (void *) rettv); switch(rettv->v_type) { case VAR_DICT: ++rettv->vval.v_dict->dv_refcount; break; case VAR_LIST: ++rettv->vval.v_list->lv_refcount; break; case VAR_FUNC: func_ref(rettv->vval.v_string); break; case VAR_UNKNOWN: rettv->v_type = VAR_NUMBER; rettv->vval.v_number = 0; break; } } int set_ref_in_python3 (int copyID) { return set_ref_in_py(copyID); }