diff options
author | shadchin <shadchin@yandex-team.com> | 2023-12-08 17:43:34 +0300 |
---|---|---|
committer | shadchin <shadchin@yandex-team.com> | 2023-12-08 20:15:01 +0300 |
commit | 213cda016d72c7d8d0e55ac7a7351959fa6ca2b8 (patch) | |
tree | a0a9170141dd91d1ff0727c0faa1af58d7b81133 /contrib/python/kiwisolver/py3/py/variable.cpp | |
parent | 914f57e3243f53dd89dd3adb4d8b6d35c47f46ce (diff) | |
download | ydb-213cda016d72c7d8d0e55ac7a7351959fa6ca2b8.tar.gz |
Update kiwisolver to 1.2.0
Diffstat (limited to 'contrib/python/kiwisolver/py3/py/variable.cpp')
-rw-r--r-- | contrib/python/kiwisolver/py3/py/variable.cpp | 235 |
1 files changed, 74 insertions, 161 deletions
diff --git a/contrib/python/kiwisolver/py3/py/variable.cpp b/contrib/python/kiwisolver/py3/py/variable.cpp index a622e8529a..1a9c69f8f6 100644 --- a/contrib/python/kiwisolver/py3/py/variable.cpp +++ b/contrib/python/kiwisolver/py3/py/variable.cpp @@ -1,22 +1,26 @@ /*----------------------------------------------------------------------------- -| Copyright (c) 2013-2017, Nucleic Development Team. +| Copyright (c) 2013-2019, Nucleic Development Team. | | Distributed under the terms of the Modified BSD License. | -| The full license is in the file COPYING.txt, distributed with this software. +| The full license is in the file LICENSE, distributed with this software. |----------------------------------------------------------------------------*/ -#include <Python.h> +#include <cppy/cppy.h> #include <kiwi/kiwi.h> -#include "pythonhelpers.h" #include "symbolics.h" #include "types.h" #include "util.h" -using namespace PythonHelpers; +namespace kiwisolver +{ + + +namespace +{ -static PyObject* +PyObject* Variable_new( PyTypeObject* type, PyObject* args, PyObject* kwargs ) { static const char *kwlist[] = { "name", "context", 0 }; @@ -28,24 +32,17 @@ Variable_new( PyTypeObject* type, PyObject* args, PyObject* kwargs ) &name, &context ) ) return 0; - PyObjectPtr pyvar( PyType_GenericNew( type, args, kwargs ) ); + cppy::ptr pyvar( PyType_GenericNew( type, args, kwargs ) ); if( !pyvar ) return 0; Variable* self = reinterpret_cast<Variable*>( pyvar.get() ); - self->context = xnewref( context ); + self->context = cppy::xincref( context ); if( name != 0 ) { -#if PY_MAJOR_VERSION >= 3 if( !PyUnicode_Check( name ) ) - return py_expected_type_fail( name, "unicode" ); -#else - if( !( PyString_Check( name ) | PyUnicode_Check( name ) ) ) - { - return py_expected_type_fail( name, "str or unicode" ); - } -#endif + return cppy::type_error( name, "str" ); std::string c_name; if( !convert_pystr_to_str(name, c_name) ) return 0; // LCOV_EXCL_LINE @@ -60,14 +57,14 @@ Variable_new( PyTypeObject* type, PyObject* args, PyObject* kwargs ) } -static void +void Variable_clear( Variable* self ) { Py_CLEAR( self->context ); } -static int +int Variable_traverse( Variable* self, visitproc visit, void* arg ) { Py_VISIT( self->context ); @@ -75,7 +72,7 @@ Variable_traverse( Variable* self, visitproc visit, void* arg ) } -static void +void Variable_dealloc( Variable* self ) { PyObject_GC_UnTrack( self ); @@ -85,32 +82,25 @@ Variable_dealloc( Variable* self ) } -static PyObject* +PyObject* Variable_repr( Variable* self ) { - return FROM_STRING( self->variable.name().c_str() ); + return PyUnicode_FromString( self->variable.name().c_str() ); } -static PyObject* +PyObject* Variable_name( Variable* self ) { - return FROM_STRING( self->variable.name().c_str() ); + return PyUnicode_FromString( self->variable.name().c_str() ); } -static PyObject* +PyObject* Variable_setName( Variable* self, PyObject* pystr ) { -#if PY_MAJOR_VERSION >= 3 if( !PyUnicode_Check( pystr ) ) - return py_expected_type_fail( pystr, "unicode" ); -#else - if( !(PyString_Check( pystr ) | PyUnicode_Check( pystr ) ) ) - { - return py_expected_type_fail( pystr, "str or unicode" ); - } -#endif + return cppy::type_error( pystr, "str" ); std::string str; if( !convert_pystr_to_str( pystr, str ) ) return 0; @@ -119,71 +109,71 @@ Variable_setName( Variable* self, PyObject* pystr ) } -static PyObject* +PyObject* Variable_context( Variable* self ) { if( self->context ) - return newref( self->context ); + return cppy::incref( self->context ); Py_RETURN_NONE; } -static PyObject* +PyObject* Variable_setContext( Variable* self, PyObject* value ) { if( value != self->context ) { PyObject* temp = self->context; - self->context = newref( value ); + self->context = cppy::incref( value ); Py_XDECREF( temp ); } Py_RETURN_NONE; } -static PyObject* +PyObject* Variable_value( Variable* self ) { return PyFloat_FromDouble( self->variable.value() ); } -static PyObject* +PyObject* Variable_add( PyObject* first, PyObject* second ) { return BinaryInvoke<BinaryAdd, Variable>()( first, second ); } -static PyObject* +PyObject* Variable_sub( PyObject* first, PyObject* second ) { return BinaryInvoke<BinarySub, Variable>()( first, second ); } -static PyObject* +PyObject* Variable_mul( PyObject* first, PyObject* second ) { return BinaryInvoke<BinaryMul, Variable>()( first, second ); } -static PyObject* +PyObject* Variable_div( PyObject* first, PyObject* second ) { return BinaryInvoke<BinaryDiv, Variable>()( first, second ); } -static PyObject* +PyObject* Variable_neg( PyObject* value ) { return UnaryInvoke<UnaryNeg, Variable>()( value ); } -static PyObject* +PyObject* Variable_richcmp( PyObject* first, PyObject* second, int op ) { switch( op ) @@ -225,129 +215,52 @@ Variable_methods[] = { }; -static PyNumberMethods -Variable_as_number = { - (binaryfunc)Variable_add, /* nb_add */ - (binaryfunc)Variable_sub, /* nb_subtract */ - (binaryfunc)Variable_mul, /* nb_multiply */ -#if PY_MAJOR_VERSION < 3 - (binaryfunc)Variable_div, /* nb_divide */ -#endif - 0, /* nb_remainder */ - 0, /* nb_divmod */ - 0, /* nb_power */ - (unaryfunc)Variable_neg, /* nb_negative */ - 0, /* nb_positive */ - 0, /* nb_absolute */ -#if PY_MAJOR_VERSION >= 3 - 0, /* nb_bool */ -#else - 0, /* nb_nonzero */ -#endif - 0, /* nb_invert */ - 0, /* nb_lshift */ - 0, /* nb_rshift */ - 0, /* nb_and */ - 0, /* nb_xor */ - (binaryfunc)0, /* nb_or */ -#if PY_MAJOR_VERSION < 3 - 0, /* nb_coerce */ -#endif - 0, /* nb_int */ - 0, /* nb_long */ - 0, /* nb_float */ -#if PY_MAJOR_VERSION < 3 - 0, /* nb_oct */ - 0, /* nb_hex */ -#endif - 0, /* nb_inplace_add */ - 0, /* nb_inplace_subtract */ - 0, /* nb_inplace_multiply */ -#if PY_MAJOR_VERSION < 3 - 0, /* nb_inplace_divide */ -#endif - 0, /* nb_inplace_remainder */ - 0, /* nb_inplace_power */ - 0, /* nb_inplace_lshift */ - 0, /* nb_inplace_rshift */ - 0, /* nb_inplace_and */ - 0, /* nb_inplace_xor */ - 0, /* nb_inplace_or */ - (binaryfunc)0, /* nb_floor_divide */ - (binaryfunc)Variable_div, /* nb_true_divide */ - 0, /* nb_inplace_floor_divide */ - 0, /* nb_inplace_true_divide */ -#if PY_VERSION_HEX >= 0x02050000 - (unaryfunc)0, /* nb_index */ -#endif -#if PY_VERSION_HEX >= 0x03050000 - (binaryfunc)0, /* nb_matrix_multiply */ - (binaryfunc)0, /* nb_inplace_matrix_multiply */ -#endif +static PyType_Slot Variable_Type_slots[] = { + { Py_tp_dealloc, void_cast( Variable_dealloc ) }, /* tp_dealloc */ + { Py_tp_traverse, void_cast( Variable_traverse ) }, /* tp_traverse */ + { Py_tp_clear, void_cast( Variable_clear ) }, /* tp_clear */ + { Py_tp_repr, void_cast( Variable_repr ) }, /* tp_repr */ + { Py_tp_richcompare, void_cast( Variable_richcmp ) }, /* tp_richcompare */ + { Py_tp_methods, void_cast( Variable_methods ) }, /* tp_methods */ + { Py_tp_new, void_cast( Variable_new ) }, /* tp_new */ + { Py_tp_alloc, void_cast( PyType_GenericAlloc ) }, /* tp_alloc */ + { Py_tp_free, void_cast( PyObject_GC_Del ) }, /* tp_free */ + { Py_nb_add, void_cast( Variable_add ) }, /* nb_add */ + { Py_nb_subtract, void_cast( Variable_sub ) }, /* nb_subtract */ + { Py_nb_multiply, void_cast( Variable_mul ) }, /* nb_multiply */ + { Py_nb_negative, void_cast( Variable_neg ) }, /* nb_negative */ + { Py_nb_true_divide, void_cast( Variable_div ) }, /* nb_true_divide */ + { 0, 0 }, }; -PyTypeObject Variable_Type = { - PyVarObject_HEAD_INIT( &PyType_Type, 0 ) - "kiwisolver.Variable", /* tp_name */ - sizeof( Variable ), /* tp_basicsize */ - 0, /* tp_itemsize */ - (destructor)Variable_dealloc, /* tp_dealloc */ - (printfunc)0, /* tp_print */ - (getattrfunc)0, /* tp_getattr */ - (setattrfunc)0, /* tp_setattr */ -#if PY_VERSION_HEX >= 0x03050000 - ( PyAsyncMethods* )0, /* tp_as_async */ -#elif PY_VERSION_HEX >= 0x03000000 - ( void* ) 0, /* tp_reserved */ -#else - ( cmpfunc )0, /* tp_compare */ -#endif - (reprfunc)Variable_repr, /* tp_repr */ - (PyNumberMethods*)&Variable_as_number, /* tp_as_number */ - (PySequenceMethods*)0, /* tp_as_sequence */ - (PyMappingMethods*)0, /* tp_as_mapping */ - (hashfunc)0, /* tp_hash */ - (ternaryfunc)0, /* tp_call */ - (reprfunc)0, /* tp_str */ - (getattrofunc)0, /* tp_getattro */ - (setattrofunc)0, /* tp_setattro */ - (PyBufferProcs*)0, /* tp_as_buffer */ -#if PY_MAJOR_VERSION >= 3 - Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC|Py_TPFLAGS_BASETYPE, /* tp_flags */ -#else - Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC|Py_TPFLAGS_BASETYPE|Py_TPFLAGS_CHECKTYPES, /* tp_flags */ -#endif - 0, /* Documentation string */ - (traverseproc)Variable_traverse, /* tp_traverse */ - (inquiry)Variable_clear, /* tp_clear */ - (richcmpfunc)Variable_richcmp, /* tp_richcompare */ - 0, /* tp_weaklistoffset */ - (getiterfunc)0, /* tp_iter */ - (iternextfunc)0, /* tp_iternext */ - (struct PyMethodDef*)Variable_methods, /* tp_methods */ - (struct PyMemberDef*)0, /* tp_members */ - 0, /* tp_getset */ - 0, /* tp_base */ - 0, /* tp_dict */ - (descrgetfunc)0, /* tp_descr_get */ - (descrsetfunc)0, /* tp_descr_set */ - 0, /* tp_dictoffset */ - (initproc)0, /* tp_init */ - (allocfunc)PyType_GenericAlloc, /* tp_alloc */ - (newfunc)Variable_new, /* tp_new */ - (freefunc)PyObject_GC_Del, /* tp_free */ - (inquiry)0, /* tp_is_gc */ - 0, /* tp_bases */ - 0, /* tp_mro */ - 0, /* tp_cache */ - 0, /* tp_subclasses */ - 0, /* tp_weaklist */ - (destructor)0 /* tp_del */ +} // namespace + + +// Initialize static variables (otherwise the compiler eliminates them) +PyTypeObject* Variable::TypeObject = NULL; + + +PyType_Spec Variable::TypeObject_Spec = { + "kiwisolver.Variable", /* tp_name */ + sizeof( Variable ), /* tp_basicsize */ + 0, /* tp_itemsize */ + Py_TPFLAGS_DEFAULT| + Py_TPFLAGS_HAVE_GC| + Py_TPFLAGS_BASETYPE, /* tp_flags */ + Variable_Type_slots /* slots */ }; -int import_variable() +bool Variable::Ready() { - return PyType_Ready( &Variable_Type ); + // The reference will be handled by the module to which we will add the type + TypeObject = pytype_cast( PyType_FromSpec( &TypeObject_Spec ) ); + if( !TypeObject ) + { + return false; + } + return true; } + +} // namespace kiwisolver |