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 | |
parent | 914f57e3243f53dd89dd3adb4d8b6d35c47f46ce (diff) | |
download | ydb-213cda016d72c7d8d0e55ac7a7351959fa6ca2b8.tar.gz |
Update kiwisolver to 1.2.0
Diffstat (limited to 'contrib/python/kiwisolver/py3/py')
-rw-r--r-- | contrib/python/kiwisolver/py3/py/constraint.cpp | 348 | ||||
-rw-r--r-- | contrib/python/kiwisolver/py3/py/expression.cpp | 211 | ||||
-rw-r--r-- | contrib/python/kiwisolver/py3/py/kiwisolver.cpp | 235 | ||||
-rw-r--r-- | contrib/python/kiwisolver/py3/py/pythonhelpers.h | 771 | ||||
-rw-r--r-- | contrib/python/kiwisolver/py3/py/solver.cpp | 185 | ||||
-rw-r--r-- | contrib/python/kiwisolver/py3/py/strength.cpp | 125 | ||||
-rw-r--r-- | contrib/python/kiwisolver/py3/py/symbolics.h | 88 | ||||
-rw-r--r-- | contrib/python/kiwisolver/py3/py/term.cpp | 209 | ||||
-rw-r--r-- | contrib/python/kiwisolver/py3/py/types.h | 88 | ||||
-rw-r--r-- | contrib/python/kiwisolver/py3/py/util.h | 59 | ||||
-rw-r--r-- | contrib/python/kiwisolver/py3/py/variable.cpp | 235 |
11 files changed, 768 insertions, 1786 deletions
diff --git a/contrib/python/kiwisolver/py3/py/constraint.cpp b/contrib/python/kiwisolver/py3/py/constraint.cpp index 2fbdd0aeb8..e5b516919e 100644 --- a/contrib/python/kiwisolver/py3/py/constraint.cpp +++ b/contrib/python/kiwisolver/py3/py/constraint.cpp @@ -1,299 +1,201 @@ /*----------------------------------------------------------------------------- -| 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 <algorithm> #include <sstream> -#include <Python.h> +#include <cppy/cppy.h> #include <kiwi/kiwi.h> -#include "pythonhelpers.h" #include "types.h" #include "util.h" +namespace kiwisolver +{ -using namespace PythonHelpers; - +namespace +{ -static PyObject* -Constraint_new( PyTypeObject* type, PyObject* args, PyObject* kwargs ) +PyObject * +Constraint_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) { - static const char *kwlist[] = { "expression", "op", "strength", 0 }; - PyObject* pyexpr; - PyObject* pyop; - PyObject* pystrength = 0; - if( !PyArg_ParseTupleAndKeywords( - args, kwargs, "OO|O:__new__", const_cast<char**>( kwlist ), - &pyexpr, &pyop, &pystrength ) ) + static const char *kwlist[] = {"expression", "op", "strength", 0}; + PyObject *pyexpr; + PyObject *pyop; + PyObject *pystrength = 0; + if (!PyArg_ParseTupleAndKeywords( + args, kwargs, "OO|O:__new__", const_cast<char **>(kwlist), + &pyexpr, &pyop, &pystrength)) return 0; - if( !Expression::TypeCheck( pyexpr ) ) - return py_expected_type_fail( pyexpr, "Expression" ); + if (!Expression::TypeCheck(pyexpr)) + return cppy::type_error(pyexpr, "Expression"); kiwi::RelationalOperator op; - if( !convert_to_relational_op( pyop, op ) ) + if (!convert_to_relational_op(pyop, op)) return 0; double strength = kiwi::strength::required; - if( pystrength && !convert_to_strength( pystrength, strength ) ) + if (pystrength && !convert_to_strength(pystrength, strength)) return 0; - PyObjectPtr pycn( PyType_GenericNew( type, args, kwargs ) ); - if( !pycn ) + cppy::ptr pycn(PyType_GenericNew(type, args, kwargs)); + if (!pycn) return 0; - Constraint* cn = reinterpret_cast<Constraint*>( pycn.get() ); - cn->expression = reduce_expression( pyexpr ); - if( !cn->expression ) + Constraint *cn = reinterpret_cast<Constraint *>(pycn.get()); + cn->expression = reduce_expression(pyexpr); + if (!cn->expression) return 0; - kiwi::Expression expr( convert_to_kiwi_expression( cn->expression ) ); - new( &cn->constraint ) kiwi::Constraint( expr, op, strength ); + kiwi::Expression expr(convert_to_kiwi_expression(cn->expression)); + new (&cn->constraint) kiwi::Constraint(expr, op, strength); return pycn.release(); } - -static void -Constraint_clear( Constraint* self ) +void Constraint_clear(Constraint *self) { - Py_CLEAR( self->expression ); + Py_CLEAR(self->expression); } - -static int -Constraint_traverse( Constraint* self, visitproc visit, void* arg ) +int Constraint_traverse(Constraint *self, visitproc visit, void *arg) { - Py_VISIT( self->expression ); + Py_VISIT(self->expression); return 0; } - -static void -Constraint_dealloc( Constraint* self ) +void Constraint_dealloc(Constraint *self) { - PyObject_GC_UnTrack( self ); - Constraint_clear( self ); + PyObject_GC_UnTrack(self); + Constraint_clear(self); self->constraint.~Constraint(); - Py_TYPE( self )->tp_free( pyobject_cast( self ) ); + Py_TYPE(self)->tp_free(pyobject_cast(self)); } - -static PyObject* -Constraint_repr( Constraint* self ) +PyObject * +Constraint_repr(Constraint *self) { std::stringstream stream; - Expression* expr = reinterpret_cast<Expression*>( self->expression ); - Py_ssize_t size = PyTuple_GET_SIZE( expr->terms ); - for( Py_ssize_t i = 0; i < size; ++i ) + Expression *expr = reinterpret_cast<Expression *>(self->expression); + Py_ssize_t size = PyTuple_GET_SIZE(expr->terms); + for (Py_ssize_t i = 0; i < size; ++i) { - PyObject* item = PyTuple_GET_ITEM( expr->terms, i ); - Term* term = reinterpret_cast<Term*>( item ); + PyObject *item = PyTuple_GET_ITEM(expr->terms, i); + Term *term = reinterpret_cast<Term *>(item); stream << term->coefficient << " * "; - stream << reinterpret_cast<Variable*>( term->variable )->variable.name(); + stream << reinterpret_cast<Variable *>(term->variable)->variable.name(); stream << " + "; } stream << expr->constant; - switch( self->constraint.op() ) + switch (self->constraint.op()) { - case kiwi::OP_EQ: - stream << " == 0"; - break; - case kiwi::OP_LE: - stream << " <= 0"; - break; - case kiwi::OP_GE: - stream << " >= 0"; - break; + case kiwi::OP_EQ: + stream << " == 0"; + break; + case kiwi::OP_LE: + stream << " <= 0"; + break; + case kiwi::OP_GE: + stream << " >= 0"; + break; } stream << " | strength = " << self->constraint.strength(); - return FROM_STRING( stream.str().c_str() ); + return PyUnicode_FromString(stream.str().c_str()); } - -static PyObject* -Constraint_expression( Constraint* self ) +PyObject * +Constraint_expression(Constraint *self) { - return newref( self->expression ); + return cppy::incref(self->expression); } - -static PyObject* -Constraint_op( Constraint* self ) +PyObject * +Constraint_op(Constraint *self) { - PyObject* res = 0; - switch( self->constraint.op() ) + PyObject *res = 0; + switch (self->constraint.op()) { - case kiwi::OP_EQ: - res = FROM_STRING( "==" ); - break; - case kiwi::OP_LE: - res = FROM_STRING( "<=" ); - break; - case kiwi::OP_GE: - res = FROM_STRING( ">=" ); - break; + case kiwi::OP_EQ: + res = PyUnicode_FromString("=="); + break; + case kiwi::OP_LE: + res = PyUnicode_FromString("<="); + break; + case kiwi::OP_GE: + res = PyUnicode_FromString(">="); + break; } return res; } - -static PyObject* -Constraint_strength( Constraint* self ) +PyObject * +Constraint_strength(Constraint *self) { - return PyFloat_FromDouble( self->constraint.strength() ); + return PyFloat_FromDouble(self->constraint.strength()); } - -static PyObject* -Constraint_or( PyObject* pyoldcn, PyObject* value ) +PyObject * +Constraint_or(PyObject *pyoldcn, PyObject *value) { - if( !Constraint::TypeCheck( pyoldcn ) ) - std::swap( pyoldcn, value ); + if (!Constraint::TypeCheck(pyoldcn)) + std::swap(pyoldcn, value); double strength; - if( !convert_to_strength( value, strength ) ) + if (!convert_to_strength(value, strength)) return 0; - PyObject* pynewcn = PyType_GenericNew( &Constraint_Type, 0, 0 ); - if( !pynewcn ) + PyObject *pynewcn = PyType_GenericNew(Constraint::TypeObject, 0, 0); + if (!pynewcn) return 0; - Constraint* oldcn = reinterpret_cast<Constraint*>( pyoldcn ); - Constraint* newcn = reinterpret_cast<Constraint*>( pynewcn ); - newcn->expression = newref( oldcn->expression ); - new( &newcn->constraint ) kiwi::Constraint( oldcn->constraint, strength ); + Constraint *oldcn = reinterpret_cast<Constraint *>(pyoldcn); + Constraint *newcn = reinterpret_cast<Constraint *>(pynewcn); + newcn->expression = cppy::incref(oldcn->expression); + new (&newcn->constraint) kiwi::Constraint(oldcn->constraint, strength); return pynewcn; } - static PyMethodDef -Constraint_methods[] = { - { "expression", ( PyCFunction )Constraint_expression, METH_NOARGS, - "Get the expression object for the constraint." }, - { "op", ( PyCFunction )Constraint_op, METH_NOARGS, - "Get the relational operator for the constraint." }, - { "strength", ( PyCFunction )Constraint_strength, METH_NOARGS, - "Get the strength for the constraint." }, - { 0 } // sentinel + Constraint_methods[] = { + {"expression", (PyCFunction)Constraint_expression, METH_NOARGS, + "Get the expression object for the constraint."}, + {"op", (PyCFunction)Constraint_op, METH_NOARGS, + "Get the relational operator for the constraint."}, + {"strength", (PyCFunction)Constraint_strength, METH_NOARGS, + "Get the strength for the constraint."}, + {0} // sentinel }; - -static PyNumberMethods -Constraint_as_number = { - 0, /* nb_add */ - 0, /* nb_subtract */ - 0, /* nb_multiply */ -#if PY_MAJOR_VERSION < 3 - 0, /* nb_divide */ -#endif - 0, /* nb_remainder */ - 0, /* nb_divmod */ - 0, /* nb_power */ - 0, /* 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)Constraint_or, /* 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)0, /* 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 Constraint_Type_slots[] = { + {Py_tp_dealloc, void_cast(Constraint_dealloc)}, /* tp_dealloc */ + {Py_tp_traverse, void_cast(Constraint_traverse)}, /* tp_traverse */ + {Py_tp_clear, void_cast(Constraint_clear)}, /* tp_clear */ + {Py_tp_repr, void_cast(Constraint_repr)}, /* tp_repr */ + {Py_tp_methods, void_cast(Constraint_methods)}, /* tp_methods */ + {Py_tp_new, void_cast(Constraint_new)}, /* tp_new */ + {Py_tp_alloc, void_cast(PyType_GenericAlloc)}, /* tp_alloc */ + {Py_tp_free, void_cast(PyObject_GC_Del)}, /* tp_free */ + {Py_nb_or, void_cast(Constraint_or)}, /* nb_or */ + {0, 0}, }; +} // namespace -PyTypeObject Constraint_Type = { - PyVarObject_HEAD_INIT( &PyType_Type, 0 ) - "kiwisolver.Constraint", /* tp_name */ - sizeof( Constraint ), /* tp_basicsize */ - 0, /* tp_itemsize */ - (destructor)Constraint_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)Constraint_repr, /* tp_repr */ - (PyNumberMethods*)&Constraint_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)Constraint_traverse, /* tp_traverse */ - (inquiry)Constraint_clear, /* tp_clear */ - (richcmpfunc)0, /* tp_richcompare */ - 0, /* tp_weaklistoffset */ - (getiterfunc)0, /* tp_iter */ - (iternextfunc)0, /* tp_iternext */ - (struct PyMethodDef*)Constraint_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)Constraint_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 */ -}; +// Initialize static variables (otherwise the compiler eliminates them) +PyTypeObject *Constraint::TypeObject = NULL; +PyType_Spec Constraint::TypeObject_Spec = { + "kiwisolver.Constraint", /* tp_name */ + sizeof(Constraint), /* tp_basicsize */ + 0, /* tp_itemsize */ + Py_TPFLAGS_DEFAULT | + Py_TPFLAGS_HAVE_GC | + Py_TPFLAGS_BASETYPE, /* tp_flags */ + Constraint_Type_slots /* slots */ +}; -int import_constraint() +bool Constraint::Ready() { - return PyType_Ready( &Constraint_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 diff --git a/contrib/python/kiwisolver/py3/py/expression.cpp b/contrib/python/kiwisolver/py3/py/expression.cpp index 5cd34e4c12..235e463f12 100644 --- a/contrib/python/kiwisolver/py3/py/expression.cpp +++ b/contrib/python/kiwisolver/py3/py/expression.cpp @@ -1,22 +1,24 @@ /*----------------------------------------------------------------------------- -| 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 <sstream> -#include <Python.h> -#include "pythonhelpers.h" +#include <cppy/cppy.h> #include "symbolics.h" #include "types.h" #include "util.h" -using namespace PythonHelpers; +namespace kiwisolver +{ +namespace +{ -static PyObject* +PyObject* Expression_new( PyTypeObject* type, PyObject* args, PyObject* kwargs ) { static const char *kwlist[] = { "terms", "constant", 0 }; @@ -26,7 +28,7 @@ Expression_new( PyTypeObject* type, PyObject* args, PyObject* kwargs ) args, kwargs, "O|O:__new__", const_cast<char**>( kwlist ), &pyterms, &pyconstant ) ) return 0; - PyObjectPtr terms( PySequence_Tuple( pyterms ) ); + cppy::ptr terms( PySequence_Tuple( pyterms ) ); if( !terms ) return 0; Py_ssize_t end = PyTuple_GET_SIZE( terms.get() ); @@ -34,7 +36,7 @@ Expression_new( PyTypeObject* type, PyObject* args, PyObject* kwargs ) { PyObject* item = PyTuple_GET_ITEM( terms.get(), i ); if( !Term::TypeCheck( item ) ) - return py_expected_type_fail( item, "Term" ); + return cppy::type_error( item, "Term" ); } double constant = 0.0; if( pyconstant && !convert_to_double( pyconstant, constant ) ) @@ -49,14 +51,14 @@ Expression_new( PyTypeObject* type, PyObject* args, PyObject* kwargs ) } -static void +void Expression_clear( Expression* self ) { Py_CLEAR( self->terms ); } -static int +int Expression_traverse( Expression* self, visitproc visit, void* arg ) { Py_VISIT( self->terms ); @@ -64,7 +66,7 @@ Expression_traverse( Expression* self, visitproc visit, void* arg ) } -static void +void Expression_dealloc( Expression* self ) { PyObject_GC_UnTrack( self ); @@ -73,7 +75,7 @@ Expression_dealloc( Expression* self ) } -static PyObject* +PyObject* Expression_repr( Expression* self ) { std::stringstream stream; @@ -87,25 +89,25 @@ Expression_repr( Expression* self ) stream << " + "; } stream << self->constant; - return FROM_STRING( stream.str().c_str() ); + return PyUnicode_FromString( stream.str().c_str() ); } -static PyObject* +PyObject* Expression_terms( Expression* self ) { - return newref( self->terms ); + return cppy::incref( self->terms ); } -static PyObject* +PyObject* Expression_constant( Expression* self ) { return PyFloat_FromDouble( self->constant ); } -static PyObject* +PyObject* Expression_value( Expression* self ) { double result = self->constant; @@ -121,42 +123,42 @@ Expression_value( Expression* self ) } -static PyObject* +PyObject* Expression_add( PyObject* first, PyObject* second ) { return BinaryInvoke<BinaryAdd, Expression>()( first, second ); } -static PyObject* +PyObject* Expression_sub( PyObject* first, PyObject* second ) { return BinaryInvoke<BinarySub, Expression>()( first, second ); } -static PyObject* +PyObject* Expression_mul( PyObject* first, PyObject* second ) { return BinaryInvoke<BinaryMul, Expression>()( first, second ); } -static PyObject* +PyObject* Expression_div( PyObject* first, PyObject* second ) { return BinaryInvoke<BinaryDiv, Expression>()( first, second ); } -static PyObject* +PyObject* Expression_neg( PyObject* value ) { return UnaryInvoke<UnaryNeg, Expression>()( value ); } -static PyObject* +PyObject* Expression_richcmp( PyObject* first, PyObject* second, int op ) { switch( op ) @@ -194,133 +196,52 @@ Expression_methods[] = { }; -static PyNumberMethods -Expression_as_number = { - (binaryfunc)Expression_add, /* nb_add */ - (binaryfunc)Expression_sub, /* nb_subtract */ - (binaryfunc)Expression_mul, /* nb_multiply */ -#if PY_MAJOR_VERSION < 3 - (binaryfunc)Expression_div, /* nb_divide */ -#endif - 0, /* nb_remainder */ - 0, /* nb_divmod */ - 0, /* nb_power */ - (unaryfunc)Expression_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 */ -#if PY_MAJOR_VERSION >= 3 - (void *)0, /* nb_reserved */ -#else - 0, /* nb_long */ -#endif - 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)Expression_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 Expression_Type_slots[] = { + { Py_tp_dealloc, void_cast( Expression_dealloc ) }, /* tp_dealloc */ + { Py_tp_traverse, void_cast( Expression_traverse ) }, /* tp_traverse */ + { Py_tp_clear, void_cast( Expression_clear ) }, /* tp_clear */ + { Py_tp_repr, void_cast( Expression_repr ) }, /* tp_repr */ + { Py_tp_richcompare, void_cast( Expression_richcmp ) }, /* tp_richcompare */ + { Py_tp_methods, void_cast( Expression_methods ) }, /* tp_methods */ + { Py_tp_new, void_cast( Expression_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( Expression_add ) }, /* nb_add */ + { Py_nb_subtract, void_cast( Expression_sub ) }, /* nb_sub */ + { Py_nb_multiply, void_cast( Expression_mul ) }, /* nb_mul */ + { Py_nb_negative, void_cast( Expression_neg ) }, /* nb_neg */ + { Py_nb_true_divide, void_cast( Expression_div ) }, /* nb_div */ + { 0, 0 }, }; -PyTypeObject Expression_Type = { - PyVarObject_HEAD_INIT( &PyType_Type, 0 ) - "kiwisolver.Expression", /* tp_name */ - sizeof( Expression ), /* tp_basicsize */ - 0, /* tp_itemsize */ - (destructor)Expression_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)Expression_repr, /* tp_repr */ - (PyNumberMethods*)&Expression_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, -#else - Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC|Py_TPFLAGS_BASETYPE|Py_TPFLAGS_CHECKTYPES, /* tp_flags */ -#endif - 0, /* Documentation string */ - (traverseproc)Expression_traverse, /* tp_traverse */ - (inquiry)Expression_clear, /* tp_clear */ - (richcmpfunc)Expression_richcmp, /* tp_richcompare */ - 0, /* tp_weaklistoffset */ - (getiterfunc)0, /* tp_iter */ - (iternextfunc)0, /* tp_iternext */ - (struct PyMethodDef*)Expression_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)Expression_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* Expression::TypeObject = NULL; + + +PyType_Spec Expression::TypeObject_Spec = { + "kiwisolver.Expression", /* tp_name */ + sizeof( Expression ), /* tp_basicsize */ + 0, /* tp_itemsize */ + Py_TPFLAGS_DEFAULT| + Py_TPFLAGS_HAVE_GC| + Py_TPFLAGS_BASETYPE, /* tp_flags */ + Expression_Type_slots /* slots */ }; -int import_expression() +bool Expression::Ready() { - return PyType_Ready( &Expression_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; } + +} // namesapce kiwisolver diff --git a/contrib/python/kiwisolver/py3/py/kiwisolver.cpp b/contrib/python/kiwisolver/py3/py/kiwisolver.cpp index 54b333a2c8..d82e67fde2 100644 --- a/contrib/python/kiwisolver/py3/py/kiwisolver.cpp +++ b/contrib/python/kiwisolver/py3/py/kiwisolver.cpp @@ -1,86 +1,187 @@ /*----------------------------------------------------------------------------- -| 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 "types.h" -#define PY_KIWI_VERSION "1.1.0" +#define PY_KIWI_VERSION "1.2.0" + +namespace +{ + + +bool ready_types() +{ + using namespace kiwisolver; + if( !Variable::Ready() ) + { + return false; + } + if( !Term::Ready() ) + { + return false; + } + if( !Expression::Ready() ) + { + return false; + } + if( !Constraint::Ready() ) + { + return false; + } + if( !strength::Ready() ) + { + return false; + } + if( !Solver::Ready() ) + { + return false; + } + return true; +} + +bool add_objects( PyObject* mod ) +{ + using namespace kiwisolver; + + cppy::ptr kiwiversion( PyUnicode_FromString( KIWI_VERSION ) ); + if( !kiwiversion ) + { + return false; + } + cppy::ptr pyversion( PyUnicode_FromString( PY_KIWI_VERSION ) ); + if( !pyversion ) + { + return false; + } + cppy::ptr pystrength( PyType_GenericNew( strength::TypeObject, 0, 0 ) ); + if( !pystrength ) + { + return false; + } + + if( PyModule_AddObject( mod, "__version__", pyversion.get() ) < 0 ) + { + return false; + } + pyversion.release(); + + if( PyModule_AddObject( mod, "__kiwi_version__", kiwiversion.get() ) < 0 ) + { + return false; + } + kiwiversion.release(); + + if( PyModule_AddObject( mod, "strength", pystrength.get() ) < 0 ) + { + return false; + } + pystrength.release(); + + // Variable + cppy::ptr var( pyobject_cast( Variable::TypeObject ) ); + if( PyModule_AddObject( mod, "Variable", var.get() ) < 0 ) + { + return false; + } + var.release(); + + // Term + cppy::ptr term( pyobject_cast( Term::TypeObject ) ); + if( PyModule_AddObject( mod, "Term", term.get() ) < 0 ) + { + return false; + } + term.release(); + + // Expression + cppy::ptr expr( pyobject_cast( Expression::TypeObject ) ); + if( PyModule_AddObject( mod, "Expression", expr.get() ) < 0 ) + { + return false; + } + expr.release(); + + // Constraint + cppy::ptr cons( pyobject_cast( Constraint::TypeObject ) ); + if( PyModule_AddObject( mod, "Constraint", cons.get() ) < 0 ) + { + return false; + } + cons.release(); + + cppy::ptr solver( pyobject_cast( Solver::TypeObject ) ); + if( PyModule_AddObject( mod, "Solver", solver.get() ) < 0 ) + { + return false; + } + solver.release(); + + PyModule_AddObject( mod, "DuplicateConstraint", DuplicateConstraint ); + PyModule_AddObject( mod, "UnsatisfiableConstraint", UnsatisfiableConstraint ); + PyModule_AddObject( mod, "UnknownConstraint", UnknownConstraint ); + PyModule_AddObject( mod, "DuplicateEditVariable", DuplicateEditVariable ); + PyModule_AddObject( mod, "UnknownEditVariable", UnknownEditVariable ); + PyModule_AddObject( mod, "BadRequiredStrength", BadRequiredStrength ); + + return true; +} + + +int +catom_modexec( PyObject *mod ) +{ + if( !ready_types() ) + { + return -1; + } + if( !kiwisolver::init_exceptions() ) + { + return -1; + } + if( !add_objects( mod ) ) + { + return -1; + } + + + return 0; +} -using namespace PythonHelpers; static PyMethodDef kiwisolver_methods[] = { { 0 } // Sentinel }; -#if PY_MAJOR_VERSION >= 3 -static struct PyModuleDef kiwisolver_moduledef = { - PyModuleDef_HEAD_INIT, - "kiwisolver", - NULL, - sizeof( struct module_state ), - kiwisolver_methods, - NULL + +PyModuleDef_Slot kiwisolver_slots[] = { + {Py_mod_exec, reinterpret_cast<void*>( catom_modexec ) }, + {0, NULL} }; -PyMODINIT_FUNC -PyInit_kiwisolver( void ) -#else -PyMODINIT_FUNC -initkiwisolver( void ) -#endif + +struct PyModuleDef moduledef = { + PyModuleDef_HEAD_INIT, + "kiwisolver", + "kiwisolver extension module", + 0, + kiwisolver_methods, + kiwisolver_slots, + NULL, + NULL, + NULL +}; + +} // namespace + + +PyMODINIT_FUNC PyInit_kiwisolver( void ) { -#if PY_MAJOR_VERSION >= 3 - PyObject *mod = PyModule_Create( &kiwisolver_moduledef ); -#else - PyObject* mod = Py_InitModule( "kiwisolver", kiwisolver_methods ); -#endif - if( !mod ) - INITERROR; - if( import_variable() < 0 ) - INITERROR; - if( import_term() < 0 ) - INITERROR; - if( import_expression() < 0 ) - INITERROR; - if( import_constraint() < 0 ) - INITERROR; - if( import_solver() < 0 ) - INITERROR; - if( import_strength() < 0 ) - INITERROR; - PyObject* kiwiversion = FROM_STRING( KIWI_VERSION ); - if( !kiwiversion ) - INITERROR; - PyObject* pyversion = FROM_STRING( PY_KIWI_VERSION ); - if( !pyversion ) - INITERROR; - PyObject* pystrength = PyType_GenericNew( &strength_Type, 0, 0 ); - if( !pystrength ) - INITERROR; - - PyModule_AddObject( mod, "__version__", pyversion ); - PyModule_AddObject( mod, "__kiwi_version__", kiwiversion ); - PyModule_AddObject( mod, "strength", pystrength ); - PyModule_AddObject( mod, "Variable", newref( pyobject_cast( &Variable_Type ) ) ); - PyModule_AddObject( mod, "Term", newref( pyobject_cast( &Term_Type ) ) ); - PyModule_AddObject( mod, "Expression", newref( pyobject_cast( &Expression_Type ) ) ); - PyModule_AddObject( mod, "Constraint", newref( pyobject_cast( &Constraint_Type ) ) ); - PyModule_AddObject( mod, "Solver", newref( pyobject_cast( &Solver_Type ) ) ); - PyModule_AddObject( mod, "DuplicateConstraint", newref( DuplicateConstraint ) ); - PyModule_AddObject( mod, "UnsatisfiableConstraint", newref( UnsatisfiableConstraint ) ); - PyModule_AddObject( mod, "UnknownConstraint", newref( UnknownConstraint ) ); - PyModule_AddObject( mod, "DuplicateEditVariable", newref( DuplicateEditVariable ) ); - PyModule_AddObject( mod, "UnknownEditVariable", newref( UnknownEditVariable ) ); - PyModule_AddObject( mod, "BadRequiredStrength", newref( BadRequiredStrength ) ); - -#if PY_MAJOR_VERSION >= 3 - return mod; -#endif + return PyModuleDef_Init( &moduledef ); } diff --git a/contrib/python/kiwisolver/py3/py/pythonhelpers.h b/contrib/python/kiwisolver/py3/py/pythonhelpers.h deleted file mode 100644 index a9e0db634d..0000000000 --- a/contrib/python/kiwisolver/py3/py/pythonhelpers.h +++ /dev/null @@ -1,771 +0,0 @@ -/*----------------------------------------------------------------------------- -| Copyright (c) 2013-2017, 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. -|----------------------------------------------------------------------------*/ -#pragma once -#include <Python.h> -#include <structmember.h> -#include <string> - -#if PY_MAJOR_VERSION >= 3 -#define FROM_STRING PyUnicode_FromString -#define INITERROR return NULL -#define MOD_INIT_FUNC(name) PyMODINIT_FUNC PyInit_##name(void) -#else -#define FROM_STRING PyString_FromString -#define INITERROR return -#define MOD_INIT_FUNC(name) PyMODINIT_FUNC init##name(void) -#endif - -#ifndef Py_RETURN_NOTIMPLEMENTED -#define Py_RETURN_NOTIMPLEMENTED \ - return Py_INCREF(Py_NotImplemented), Py_NotImplemented -#endif - -#define pyobject_cast( o ) ( reinterpret_cast<PyObject*>( o ) ) -#define pytype_cast( o ) ( reinterpret_cast<PyTypeObject*>( o ) ) - -struct module_state { - PyObject *error; -}; - - -namespace PythonHelpers -{ - - -/*----------------------------------------------------------------------------- -| Exception Handling -|----------------------------------------------------------------------------*/ -inline PyObject* -py_bad_internal_call( const char* message ) -{ - PyErr_SetString( PyExc_SystemError, message ); - return 0; -} - - -inline PyObject* -py_type_fail( const char* message ) -{ - PyErr_SetString( PyExc_TypeError, message ); - return 0; -} - - -inline PyObject* -py_expected_type_fail( PyObject* pyobj, const char* expected_type ) -{ - PyErr_Format( - PyExc_TypeError, - "Expected object of type `%s`. Got object of type `%s` instead.", - expected_type, pyobj->ob_type->tp_name - ); - return 0; -} - - -inline PyObject* -py_value_fail( const char* message ) -{ - PyErr_SetString( PyExc_ValueError, message ); - return 0; -} - - -inline PyObject* -py_runtime_fail( const char* message ) -{ - PyErr_SetString( PyExc_RuntimeError, message ); - return 0; -} - - -inline PyObject* -py_attr_fail( const char* message ) -{ - PyErr_SetString( PyExc_AttributeError, message ); - return 0; -} - - -inline PyObject* -py_no_attr_fail( PyObject* pyobj, const char* attr ) -{ - PyErr_Format( - PyExc_AttributeError, - "'%s' object has no attribute '%s'", - pyobj->ob_type->tp_name, attr - ); - return 0; -} - - -/*----------------------------------------------------------------------------- -| Utilities -|----------------------------------------------------------------------------*/ -inline PyObject* -newref( PyObject* pyobj ) -{ - Py_INCREF( pyobj ); - return pyobj; -} - - -inline PyObject* -xnewref( PyObject* pyobj ) -{ - Py_XINCREF( pyobj ); - return pyobj; -} - - -inline PyObject* -py_bool( bool val ) -{ - return newref( val ? Py_True : Py_False ); -} - - -inline PyCFunction -lookup_method( PyTypeObject* type, const char* name ) -{ - PyMethodDef* method = type->tp_methods; - for( ; method->ml_name != 0; ++method ) - { - if( strcmp( method->ml_name, name ) == 0 ) - return method->ml_meth; - } - return 0; -} - - -/*----------------------------------------------------------------------------- -| Object Ptr -|----------------------------------------------------------------------------*/ -class PyObjectPtr { - -public: - - PyObjectPtr() : m_pyobj( 0 ) {} - - PyObjectPtr( const PyObjectPtr& objptr ) : - m_pyobj( PythonHelpers::xnewref( objptr.m_pyobj ) ) {} - - PyObjectPtr( PyObject* pyobj ) : m_pyobj( pyobj ) {} - - ~PyObjectPtr() - { - xdecref_release(); - } - - PyObject* get() const - { - return m_pyobj; - } - - void set( PyObject* pyobj ) - { - PyObject* old = m_pyobj; - m_pyobj = pyobj; - Py_XDECREF( old ); - } - - PyObject* release() - { - PyObject* pyobj = m_pyobj; - m_pyobj = 0; - return pyobj; - } - - PyObject* decref_release() - { - PyObject* pyobj = m_pyobj; - m_pyobj = 0; - Py_DECREF( pyobj ); - return pyobj; - } - - PyObject* xdecref_release() - { - PyObject* pyobj = m_pyobj; - m_pyobj = 0; - Py_XDECREF( pyobj ); - return pyobj; - } - - PyObject* incref_release() - { - PyObject* pyobj = m_pyobj; - m_pyobj = 0; - Py_INCREF( pyobj ); - return pyobj; - } - - PyObject* xincref_release() - { - PyObject* pyobj = m_pyobj; - m_pyobj = 0; - Py_XINCREF( pyobj ); - return pyobj; - } - - void incref() const - { - Py_INCREF( m_pyobj ); - } - - void decref() const - { - Py_DECREF( m_pyobj ); - } - - void xincref() const - { - Py_XINCREF( m_pyobj ); - } - - void xdecref() const - { - Py_XDECREF( m_pyobj ); - } - - PyObject* newref() const - { - Py_INCREF( m_pyobj ); - return m_pyobj; - } - - PyObject* xnewref() const - { - Py_XINCREF( m_pyobj ); - return m_pyobj; - } - - size_t refcount() const - { - if( m_pyobj ) - return m_pyobj->ob_refcnt; - return 0; - } - - bool is_true( bool clear_err=true ) const - { - int truth = PyObject_IsTrue( m_pyobj ); - if( truth == 1 ) - return true; - if( truth == 0 ) - return false; - if( clear_err ) - PyErr_Clear(); - return false; - } - - bool is_None() const - { - return m_pyobj == Py_None; - } - - bool is_True() const - { - return m_pyobj == Py_True; - } - - bool is_False() const - { - return m_pyobj == Py_False; - } - - bool load_dict( PyObjectPtr& out, bool forcecreate=false ) - { - PyObject** dict = _PyObject_GetDictPtr( m_pyobj ); - if( !dict ) - return false; - if( forcecreate && !*dict ) - *dict = PyDict_New(); - out = PythonHelpers::xnewref( *dict ); - return true; - } - - bool richcompare( PyObject* other, int opid, bool clear_err=true ) - { - int r = PyObject_RichCompareBool( m_pyobj, other, opid ); - if( r == 1 ) - return true; - if( r == 0 ) - return false; - if( clear_err && PyErr_Occurred() ) - PyErr_Clear(); - return false; - } - - bool richcompare( PyObjectPtr& other, int opid, bool clear_err=true ) - { - return richcompare( other.m_pyobj, opid, clear_err ); - } - - bool hasattr( PyObject* attr ) - { - return PyObject_HasAttr( m_pyobj, attr ) == 1; - } - - bool hasattr( PyObjectPtr& attr ) - { - return PyObject_HasAttr( m_pyobj, attr.get() ) == 1; - } - - bool hasattr( const char* attr ) - { - return PyObject_HasAttrString( m_pyobj, attr ) == 1; - } - - bool hasattr( std::string& attr ) - { - return hasattr( attr.c_str() ); - } - - PyObjectPtr getattr( PyObject* attr ) - { - return PyObjectPtr( PyObject_GetAttr( m_pyobj, attr ) ); - } - - PyObjectPtr getattr( PyObjectPtr& attr ) - { - return PyObjectPtr( PyObject_GetAttr( m_pyobj, attr.get() ) ); - } - - PyObjectPtr getattr( const char* attr ) - { - return PyObjectPtr( PyObject_GetAttrString( m_pyobj, attr ) ); - } - - PyObjectPtr getattr( std::string& attr ) - { - return getattr( attr.c_str() ); - } - - bool setattr( PyObject* attr, PyObject* value ) - { - return PyObject_SetAttr( m_pyobj, attr, value ) == 0; - } - - bool setattr( PyObjectPtr& attr, PyObjectPtr& value ) - { - return PyObject_SetAttr( m_pyobj, attr.get(), value.get() ) == 0; - } - - PyObjectPtr operator()( PyObjectPtr& args ) const - { - return PyObjectPtr( PyObject_Call( m_pyobj, args.get(), 0 ) ); - } - - PyObjectPtr operator()( PyObjectPtr& args, PyObjectPtr& kwargs ) const - { - return PyObjectPtr( PyObject_Call( m_pyobj, args.get(), kwargs.get() ) ); - } - - operator void*() const - { - return static_cast<void*>( m_pyobj ); - } - - PyObjectPtr& operator=( const PyObjectPtr& rhs ) - { - PyObject* old = m_pyobj; - m_pyobj = rhs.m_pyobj; - Py_XINCREF( m_pyobj ); - Py_XDECREF( old ); - return *this; - } - - PyObjectPtr& operator=( PyObject* rhs ) - { - PyObject* old = m_pyobj; - m_pyobj = rhs; - Py_XDECREF( old ); - return *this; - } - -protected: - - PyObject* m_pyobj; - -}; - - -inline bool -operator!=( const PyObjectPtr& lhs, const PyObjectPtr& rhs ) -{ - return lhs.get() != rhs.get(); -} - - -inline bool -operator!=( const PyObject* lhs, const PyObjectPtr& rhs ) -{ - return lhs != rhs.get(); -} - - -inline bool -operator!=( const PyObjectPtr& lhs, const PyObject* rhs ) -{ - return lhs.get() != rhs; -} - - -inline bool -operator==( const PyObjectPtr& lhs, const PyObjectPtr& rhs ) -{ - return lhs.get() == rhs.get(); -} - - -inline bool -operator==( const PyObject* lhs, const PyObjectPtr& rhs ) -{ - return lhs == rhs.get(); -} - - -inline bool -operator==( const PyObjectPtr& lhs, const PyObject* rhs ) -{ - return lhs.get() == rhs; -} - - -/*----------------------------------------------------------------------------- -| Tuple Ptr -|----------------------------------------------------------------------------*/ -class PyTuplePtr : public PyObjectPtr { - -public: - - PyTuplePtr() : PyObjectPtr() {} - - PyTuplePtr( const PyObjectPtr& objptr ) : PyObjectPtr( objptr ) {} - - PyTuplePtr( PyObject* pytuple ) : PyObjectPtr( pytuple ) {} - - bool check() - { - return PyTuple_Check( m_pyobj ); - } - - bool check_exact() - { - return PyTuple_CheckExact( m_pyobj ); - } - - Py_ssize_t size() const - { - return PyTuple_GET_SIZE( m_pyobj ); - } - - PyObjectPtr get_item( Py_ssize_t index ) const - { - return PyObjectPtr( PythonHelpers::newref( PyTuple_GET_ITEM( m_pyobj, index ) ) ); - } - - void set_item( Py_ssize_t index, PyObject* pyobj ) - { - PyObject* old_item = PyTuple_GET_ITEM( m_pyobj, index ); - PyTuple_SET_ITEM( m_pyobj, index, pyobj ); - Py_XDECREF( old_item ); - } - - void set_item( Py_ssize_t index, PyObjectPtr& item ) - { - PyObject* old_item = PyTuple_GET_ITEM( m_pyobj, index ); - PyTuple_SET_ITEM( m_pyobj, index, item.get() ); - Py_XINCREF( item.get() ); - Py_XDECREF( old_item ); - } - - // pyobj must not be null, only use to fill a new empty tuple - void initialize( Py_ssize_t index, PyObject* pyobj ) - { - PyTuple_SET_ITEM( m_pyobj, index, pyobj ); - } - - // ptr must not be empty, only use to fill a new empty tuple - void initialize( Py_ssize_t index, PyObjectPtr& item ) - { - PyTuple_SET_ITEM( m_pyobj, index, item.get() ); - Py_INCREF( item.get() ); - } - -}; - - -/*----------------------------------------------------------------------------- -| List Ptr -|----------------------------------------------------------------------------*/ -class PyListPtr : public PyObjectPtr { - -public: - - PyListPtr() : PyObjectPtr() {} - - PyListPtr( const PyObjectPtr& objptr ) : PyObjectPtr( objptr ) {} - - PyListPtr( PyObject* pylist ) : PyObjectPtr( pylist ) {} - - bool check() const - { - return PyList_Check( m_pyobj ); - } - - bool check_exact() const - { - return PyList_CheckExact( m_pyobj ); - } - - Py_ssize_t size() const - { - return PyList_GET_SIZE( m_pyobj ); - } - - PyObject* borrow_item( Py_ssize_t index ) const - { - return PyList_GET_ITEM( m_pyobj, index ); - } - - PyObjectPtr get_item( Py_ssize_t index ) const - { - return PyObjectPtr( PythonHelpers::newref( PyList_GET_ITEM( m_pyobj, index ) ) ); - } - - void set_item( Py_ssize_t index, PyObject* pyobj ) const - { - PyObject* old_item = PyList_GET_ITEM( m_pyobj, index ); - PyList_SET_ITEM( m_pyobj, index, pyobj ); - Py_XDECREF( old_item ); - } - - void set_item( Py_ssize_t index, PyObjectPtr& item ) const - { - PyObject* old_item = PyList_GET_ITEM( m_pyobj, index ); - PyList_SET_ITEM( m_pyobj, index, item.get() ); - Py_XINCREF( item.get() ); - Py_XDECREF( old_item ); - } - - bool del_item( Py_ssize_t index ) const - { - if( PySequence_DelItem( m_pyobj, index ) == -1 ) - return false; - return true; - } - - bool append( PyObjectPtr& pyobj ) const - { - if( PyList_Append( m_pyobj, pyobj.get() ) == 0 ) - return true; - return false; - } - - Py_ssize_t index( PyObjectPtr& item ) const - { - Py_ssize_t maxidx = size(); - for( Py_ssize_t idx = 0; idx < maxidx; idx++ ) - { - PyObjectPtr other( get_item( idx ) ); - if( item.richcompare( other, Py_EQ ) ) - return idx; - } - return -1; - } - -}; - - -/*----------------------------------------------------------------------------- -| Dict Ptr -|----------------------------------------------------------------------------*/ -class PyDictPtr : public PyObjectPtr { - -public: - - PyDictPtr() : PyObjectPtr() {} - - PyDictPtr( const PyObjectPtr& objptr ) : PyObjectPtr( objptr ) {} - - PyDictPtr( PyObject* pydict ) : PyObjectPtr( pydict ) {} - - bool check() - { - return PyDict_Check( m_pyobj ); - } - - bool check_exact() - { - return PyDict_CheckExact( m_pyobj ); - } - - Py_ssize_t size() const - { - return PyDict_Size( m_pyobj ); - } - - PyObjectPtr get_item( PyObject* key ) const - { - return PyObjectPtr( PythonHelpers::xnewref( PyDict_GetItem( m_pyobj, key ) ) ) ; - } - - PyObjectPtr get_item( PyObjectPtr& key ) const - { - return PyObjectPtr( PythonHelpers::xnewref( PyDict_GetItem( m_pyobj, key.get() ) ) ); - } - - PyObjectPtr get_item( const char* key ) const - { - return PyObjectPtr( PythonHelpers::xnewref( PyDict_GetItemString( m_pyobj, key ) ) ); - } - - PyObjectPtr get_item( std::string& key ) const - { - return get_item( key.c_str() ); - } - - bool set_item( PyObject* key, PyObject* value ) const - { - if( PyDict_SetItem( m_pyobj, key, value ) == 0 ) - return true; - return false; - } - - bool set_item( PyObject* key, PyObjectPtr& value ) const - { - if( PyDict_SetItem( m_pyobj, key, value.get() ) == 0 ) - return true; - return false; - } - - bool set_item( PyObjectPtr& key, PyObject* value ) const - { - if( PyDict_SetItem( m_pyobj, key.get(), value ) == 0 ) - return true; - return false; - } - - bool set_item( PyObjectPtr& key, PyObjectPtr& value ) const - { - if( PyDict_SetItem( m_pyobj, key.get(), value.get() ) == 0 ) - return true; - return false; - } - - bool set_item( const char* key, PyObjectPtr& value ) const - { - if( PyDict_SetItemString( m_pyobj, key, value.get() ) == 0 ) - return true; - return false; - } - - bool set_item( const char* key, PyObject* value ) const - { - if( PyDict_SetItemString( m_pyobj, key, value ) == 0 ) - return true; - return false; - } - - bool set_item( std::string& key, PyObjectPtr& value ) const - { - return set_item( key.c_str(), value ); - } - - bool del_item( PyObjectPtr& key ) const - { - if( PyDict_DelItem( m_pyobj, key.get() ) == 0 ) - return true; - return false; - } - - bool del_item( const char* key ) const - { - if( PyDict_DelItemString( m_pyobj, key ) == 0 ) - return true; - return false; - } - - bool del_item( std::string& key ) const - { - return del_item( key.c_str() ); - } - -}; - - -/*----------------------------------------------------------------------------- -| Method Ptr -|----------------------------------------------------------------------------*/ -class PyMethodPtr : public PyObjectPtr { - -public: - - PyMethodPtr() : PyObjectPtr() {} - - PyMethodPtr( const PyObjectPtr& objptr ) : PyObjectPtr( objptr ) {} - - PyMethodPtr( PyObject* pymethod ) : PyObjectPtr( pymethod ) {} - - bool check() - { - return PyMethod_Check( m_pyobj ); - } - - PyObjectPtr get_self() const - { - return PyObjectPtr( PythonHelpers::xnewref( PyMethod_GET_SELF( m_pyobj ) ) ); - } - - PyObjectPtr get_function() const - { - return PyObjectPtr( PythonHelpers::xnewref( PyMethod_GET_FUNCTION( m_pyobj ) ) ); - } - -#if PY_MAJOR_VERSION < 3 - PyObjectPtr get_class() const - { - return PyObjectPtr( PythonHelpers::xnewref( PyMethod_GET_CLASS( m_pyobj ) ) ); - } -#endif -}; - - -/*----------------------------------------------------------------------------- -| Weakref Ptr -|----------------------------------------------------------------------------*/ -class PyWeakrefPtr : public PyObjectPtr { - -public: - - PyWeakrefPtr() : PyObjectPtr() {} - - PyWeakrefPtr( const PyObjectPtr& objptr ) : PyObjectPtr( objptr ) {} - - PyWeakrefPtr( PyObject* pyweakref ) : PyObjectPtr( pyweakref ) {} - - bool check() - { - return PyWeakref_CheckRef( m_pyobj ); - } - - bool check_exact() - { - return PyWeakref_CheckRefExact( m_pyobj ); - } - - PyObjectPtr get_object() const - { - return PyObjectPtr( PythonHelpers::newref( PyWeakref_GET_OBJECT( m_pyobj ) ) ); - } - -}; - -} // namespace PythonHelpers diff --git a/contrib/python/kiwisolver/py3/py/solver.cpp b/contrib/python/kiwisolver/py3/py/solver.cpp index 7a2ef23ca3..cd6af8c724 100644 --- a/contrib/python/kiwisolver/py3/py/solver.cpp +++ b/contrib/python/kiwisolver/py3/py/solver.cpp @@ -1,25 +1,27 @@ /*----------------------------------------------------------------------------- -| 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 "types.h" #include "util.h" -using namespace PythonHelpers; +namespace kiwisolver +{ +namespace +{ -static PyObject* +PyObject* Solver_new( PyTypeObject* type, PyObject* args, PyObject* kwargs ) { if( PyTuple_GET_SIZE( args ) != 0 || ( kwargs && PyDict_Size( kwargs ) != 0 ) ) - return py_type_fail( "Solver.__new__ takes no arguments" ); + return cppy::type_error( "Solver.__new__ takes no arguments" ); PyObject* pysolver = PyType_GenericNew( type, args, kwargs ); if( !pysolver ) return 0; @@ -29,7 +31,7 @@ Solver_new( PyTypeObject* type, PyObject* args, PyObject* kwargs ) } -static void +void Solver_dealloc( Solver* self ) { self->solver.~Solver(); @@ -37,11 +39,11 @@ Solver_dealloc( Solver* self ) } -static PyObject* +PyObject* Solver_addConstraint( Solver* self, PyObject* other ) { if( !Constraint::TypeCheck( other ) ) - return py_expected_type_fail( other, "Constraint" ); + return cppy::type_error( other, "Constraint" ); Constraint* cn = reinterpret_cast<Constraint*>( other ); try { @@ -61,11 +63,11 @@ Solver_addConstraint( Solver* self, PyObject* other ) } -static PyObject* +PyObject* Solver_removeConstraint( Solver* self, PyObject* other ) { if( !Constraint::TypeCheck( other ) ) - return py_expected_type_fail( other, "Constraint" ); + return cppy::type_error( other, "Constraint" ); Constraint* cn = reinterpret_cast<Constraint*>( other ); try { @@ -80,17 +82,17 @@ Solver_removeConstraint( Solver* self, PyObject* other ) } -static PyObject* +PyObject* Solver_hasConstraint( Solver* self, PyObject* other ) { if( !Constraint::TypeCheck( other ) ) - return py_expected_type_fail( other, "Constraint" ); + return cppy::type_error( other, "Constraint" ); Constraint* cn = reinterpret_cast<Constraint*>( other ); - return newref( self->solver.hasConstraint( cn->constraint ) ? Py_True : Py_False ); + return cppy::incref( self->solver.hasConstraint( cn->constraint ) ? Py_True : Py_False ); } -static PyObject* +PyObject* Solver_addEditVariable( Solver* self, PyObject* args ) { PyObject* pyvar; @@ -98,7 +100,7 @@ Solver_addEditVariable( Solver* self, PyObject* args ) if( !PyArg_ParseTuple( args, "OO", &pyvar, &pystrength ) ) return 0; if( !Variable::TypeCheck( pyvar ) ) - return py_expected_type_fail( pyvar, "Variable" ); + return cppy::type_error( pyvar, "Variable" ); double strength; if( !convert_to_strength( pystrength, strength ) ) return 0; @@ -121,11 +123,11 @@ Solver_addEditVariable( Solver* self, PyObject* args ) } -static PyObject* +PyObject* Solver_removeEditVariable( Solver* self, PyObject* other ) { if( !Variable::TypeCheck( other ) ) - return py_expected_type_fail( other, "Variable" ); + return cppy::type_error( other, "Variable" ); Variable* var = reinterpret_cast<Variable*>( other ); try { @@ -140,17 +142,17 @@ Solver_removeEditVariable( Solver* self, PyObject* other ) } -static PyObject* +PyObject* Solver_hasEditVariable( Solver* self, PyObject* other ) { if( !Variable::TypeCheck( other ) ) - return py_expected_type_fail( other, "Variable" ); + return cppy::type_error( other, "Variable" ); Variable* var = reinterpret_cast<Variable*>( other ); - return newref( self->solver.hasEditVariable( var->variable ) ? Py_True : Py_False ); + return cppy::incref( self->solver.hasEditVariable( var->variable ) ? Py_True : Py_False ); } -static PyObject* +PyObject* Solver_suggestValue( Solver* self, PyObject* args ) { PyObject* pyvar; @@ -158,7 +160,7 @@ Solver_suggestValue( Solver* self, PyObject* args ) if( !PyArg_ParseTuple( args, "OO", &pyvar, &pyvalue ) ) return 0; if( !Variable::TypeCheck( pyvar ) ) - return py_expected_type_fail( pyvar, "Variable" ); + return cppy::type_error( pyvar, "Variable" ); double value; if( !convert_to_double( pyvalue, value ) ) return 0; @@ -176,7 +178,7 @@ Solver_suggestValue( Solver* self, PyObject* args ) } -static PyObject* +PyObject* Solver_updateVariables( Solver* self ) { self->solver.updateVariables(); @@ -184,7 +186,7 @@ Solver_updateVariables( Solver* self ) } -static PyObject* +PyObject* Solver_reset( Solver* self ) { self->solver.reset(); @@ -192,15 +194,15 @@ Solver_reset( Solver* self ) } -static PyObject* +PyObject* Solver_dump( Solver* self ) { - PyObjectPtr dump_str( PyUnicode_FromString( self->solver.dumps().c_str() ) ); + cppy::ptr dump_str( PyUnicode_FromString( self->solver.dumps().c_str() ) ); PyObject_Print( dump_str.get(), stdout, 0 ); Py_RETURN_NONE; } -static PyObject* +PyObject* Solver_dumps( Solver* self ) { return PyUnicode_FromString( self->solver.dumps().c_str() ); @@ -234,62 +236,45 @@ Solver_methods[] = { }; -PyTypeObject Solver_Type = { - PyVarObject_HEAD_INIT( &PyType_Type, 0 ) - "kiwisolver.Solver", /* tp_name */ - sizeof( Solver ), /* tp_basicsize */ - 0, /* tp_itemsize */ - (destructor)Solver_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)0, /* tp_repr */ - (PyNumberMethods*)0, /* 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 */ - Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, /* tp_flags */ - 0, /* Documentation string */ - (traverseproc)0, /* tp_traverse */ - (inquiry)0, /* tp_clear */ - (richcmpfunc)0, /* tp_richcompare */ - 0, /* tp_weaklistoffset */ - (getiterfunc)0, /* tp_iter */ - (iternextfunc)0, /* tp_iternext */ - (struct PyMethodDef*)Solver_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)Solver_new, /* tp_new */ - (freefunc)PyObject_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 */ +static PyType_Slot Solver_Type_slots[] = { + { Py_tp_dealloc, void_cast( Solver_dealloc ) }, /* tp_dealloc */ + { Py_tp_methods, void_cast( Solver_methods ) }, /* tp_methods */ + { Py_tp_new, void_cast( Solver_new ) }, /* tp_new */ + { Py_tp_alloc, void_cast( PyType_GenericAlloc ) }, /* tp_alloc */ + { Py_tp_free, void_cast( PyObject_Del ) }, /* tp_free */ + { 0, 0 }, +}; + + +} // namespace + + +// Initialize static variables (otherwise the compiler eliminates them) +PyTypeObject* Solver::TypeObject = NULL; + + +PyType_Spec Solver::TypeObject_Spec = { + "kiwisolver.Solver", /* tp_name */ + sizeof( Solver ), /* tp_basicsize */ + 0, /* tp_itemsize */ + Py_TPFLAGS_DEFAULT| + Py_TPFLAGS_BASETYPE, /* tp_flags */ + Solver_Type_slots /* slots */ }; +bool Solver::Ready() +{ + // 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; +} + + PyObject* DuplicateConstraint; PyObject* UnsatisfiableConstraint; @@ -303,31 +288,51 @@ PyObject* UnknownEditVariable; PyObject* BadRequiredStrength; -int import_solver() +bool init_exceptions() { DuplicateConstraint = PyErr_NewException( const_cast<char*>( "kiwisolver.DuplicateConstraint" ), 0, 0 ); if( !DuplicateConstraint ) - return -1; + { + return false; + } + UnsatisfiableConstraint = PyErr_NewException( const_cast<char*>( "kiwisolver.UnsatisfiableConstraint" ), 0, 0 ); if( !UnsatisfiableConstraint ) - return -1; + { + return false; + } + UnknownConstraint = PyErr_NewException( const_cast<char*>( "kiwisolver.UnknownConstraint" ), 0, 0 ); if( !UnknownConstraint ) - return -1; + { + return false; + } + DuplicateEditVariable = PyErr_NewException( const_cast<char*>( "kiwisolver.DuplicateEditVariable" ), 0, 0 ); if( !DuplicateEditVariable ) - return -1; + { + return false; + } + UnknownEditVariable = PyErr_NewException( const_cast<char*>( "kiwisolver.UnknownEditVariable" ), 0, 0 ); if( !UnknownEditVariable ) - return -1; + { + return false; + } + BadRequiredStrength = PyErr_NewException( const_cast<char*>( "kiwisolver.BadRequiredStrength" ), 0, 0 ); if( !BadRequiredStrength ) - return -1; - return PyType_Ready( &Solver_Type ); + { + return false; + } + + return true; } + +} // namespace diff --git a/contrib/python/kiwisolver/py3/py/strength.cpp b/contrib/python/kiwisolver/py3/py/strength.cpp index df1552d194..03611311f9 100644 --- a/contrib/python/kiwisolver/py3/py/strength.cpp +++ b/contrib/python/kiwisolver/py3/py/strength.cpp @@ -1,61 +1,68 @@ /*----------------------------------------------------------------------------- -| 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 "util.h" -using namespace PythonHelpers; +#ifdef __clang__ +#pragma clang diagnostic ignored "-Wdeprecated-writable-strings" +#endif + +#ifdef __GNUC__ +#pragma GCC diagnostic ignored "-Wwrite-strings" +#endif -struct strength +namespace kiwisolver { - PyObject_HEAD; -}; -static void +namespace +{ + + +void strength_dealloc( PyObject* self ) { Py_TYPE( self )->tp_free( self ); } -static PyObject* +PyObject* strength_weak( strength* self ) { return PyFloat_FromDouble( kiwi::strength::weak ); } -static PyObject* +PyObject* strength_medium( strength* self ) { return PyFloat_FromDouble( kiwi::strength::medium ); } -static PyObject* +PyObject* strength_strong( strength* self ) { return PyFloat_FromDouble( kiwi::strength::strong ); } -static PyObject* +PyObject* strength_required( strength* self ) { return PyFloat_FromDouble( kiwi::strength::required ); } -static PyObject* +PyObject* strength_create( strength* self, PyObject* args ) { PyObject* pya; @@ -100,63 +107,43 @@ strength_methods[] = { }; -PyTypeObject strength_Type = { - PyVarObject_HEAD_INIT( &PyType_Type, 0 ) - "kiwisolver.strength", /* tp_name */ - sizeof( strength ), /* tp_basicsize */ - 0, /* tp_itemsize */ - (destructor)strength_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)0, /* tp_repr */ - (PyNumberMethods*)0, /* 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 */ - Py_TPFLAGS_DEFAULT, /* tp_flags */ - 0, /* Documentation string */ - (traverseproc)0, /* tp_traverse */ - (inquiry)0, /* tp_clear */ - (richcmpfunc)0, /* tp_richcompare */ - 0, /* tp_weaklistoffset */ - (getiterfunc)0, /* tp_iter */ - (iternextfunc)0, /* tp_iternext */ - (struct PyMethodDef*)strength_methods, /* tp_methods */ - (struct PyMemberDef*)0, /* tp_members */ - strength_getset, /* 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)0, /* tp_new */ - (freefunc)PyObject_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 */ + +static PyType_Slot strength_Type_slots[] = { + { Py_tp_dealloc, void_cast( strength_dealloc ) }, /* tp_dealloc */ + { Py_tp_getset, void_cast( strength_getset ) }, /* tp_getset */ + { Py_tp_methods, void_cast( strength_methods ) }, /* tp_methods */ + { Py_tp_alloc, void_cast( PyType_GenericAlloc ) }, /* tp_alloc */ + { Py_tp_free, void_cast( PyObject_Del ) }, /* tp_free */ + { 0, 0 }, }; -int import_strength() +} // namespace + + +// Initialize static variables (otherwise the compiler eliminates them) +PyTypeObject* strength::TypeObject = NULL; + + +PyType_Spec strength::TypeObject_Spec = { + "kiwisolver.strength", /* tp_name */ + sizeof( strength ), /* tp_basicsize */ + 0, /* tp_itemsize */ + Py_TPFLAGS_DEFAULT, /* tp_flags */ + strength_Type_slots /* slots */ +}; + + +bool strength::Ready() { - return PyType_Ready( &strength_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 diff --git a/contrib/python/kiwisolver/py3/py/symbolics.h b/contrib/python/kiwisolver/py3/py/symbolics.h index ac57553740..69ea5404c7 100644 --- a/contrib/python/kiwisolver/py3/py/symbolics.h +++ b/contrib/python/kiwisolver/py3/py/symbolics.h @@ -1,17 +1,19 @@ /*----------------------------------------------------------------------------- -| 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. |----------------------------------------------------------------------------*/ #pragma once -#include <Python.h> -#include "pythonhelpers.h" +#include <cppy/cppy.h> #include "types.h" #include "util.h" +namespace kiwisolver +{ + template<typename Op, typename T> struct UnaryInvoke { @@ -61,10 +63,6 @@ struct BinaryInvoke return Invk()( primary, reinterpret_cast<Variable*>( secondary ) ); if( PyFloat_Check( secondary ) ) return Invk()( primary, PyFloat_AS_DOUBLE( secondary ) ); -#if PY_MAJOR_VERSION < 3 - if( PyInt_Check( secondary ) ) - return Invk()( primary, double( PyInt_AS_LONG( secondary ) ) ); -#endif if( PyLong_Check( secondary ) ) { double v = PyLong_AsDouble( secondary ); @@ -90,11 +88,11 @@ struct BinaryMul template<> inline PyObject* BinaryMul::operator()( Variable* first, double second ) { - PyObject* pyterm = PyType_GenericNew( &Term_Type, 0, 0 ); + PyObject* pyterm = PyType_GenericNew( Term::TypeObject, 0, 0 ); if( !pyterm ) return 0; Term* term = reinterpret_cast<Term*>( pyterm ); - term->variable = PythonHelpers::newref( pyobject_cast( first ) ); + term->variable = cppy::incref( pyobject_cast( first ) ); term->coefficient = second; return pyterm; } @@ -103,11 +101,11 @@ PyObject* BinaryMul::operator()( Variable* first, double second ) template<> inline PyObject* BinaryMul::operator()( Term* first, double second ) { - PyObject* pyterm = PyType_GenericNew( &Term_Type, 0, 0 ); + PyObject* pyterm = PyType_GenericNew( Term::TypeObject, 0, 0 ); if( !pyterm ) return 0; Term* term = reinterpret_cast<Term*>( pyterm ); - term->variable = PythonHelpers::newref( first->variable ); + term->variable = cppy::incref( first->variable ); term->coefficient = first->coefficient * second; return pyterm; } @@ -116,12 +114,11 @@ PyObject* BinaryMul::operator()( Term* first, double second ) template<> inline PyObject* BinaryMul::operator()( Expression* first, double second ) { - using namespace PythonHelpers; - PyObjectPtr pyexpr( PyType_GenericNew( &Expression_Type, 0, 0 ) ); + cppy::ptr pyexpr( PyType_GenericNew( Expression::TypeObject, 0, 0 ) ); if( !pyexpr ) return 0; Expression* expr = reinterpret_cast<Expression*>( pyexpr.get() ); - PyObjectPtr terms( PyTuple_New( PyTuple_GET_SIZE( first->terms ) ) ); + cppy::ptr terms( PyTuple_New( PyTuple_GET_SIZE( first->terms ) ) ); if( !terms ) return 0; Py_ssize_t end = PyTuple_GET_SIZE( first->terms ); @@ -252,7 +249,7 @@ struct BinaryAdd template<> inline PyObject* BinaryAdd::operator()( Expression* first, Expression* second ) { - PythonHelpers::PyObjectPtr pyexpr( PyType_GenericNew( &Expression_Type, 0, 0 ) ); + cppy::ptr pyexpr( PyType_GenericNew( Expression::TypeObject, 0, 0 ) ); if( !pyexpr ) return 0; Expression* expr = reinterpret_cast<Expression*>( pyexpr.get() ); @@ -267,8 +264,7 @@ PyObject* BinaryAdd::operator()( Expression* first, Expression* second ) template<> inline PyObject* BinaryAdd::operator()( Expression* first, Term* second ) { - using namespace PythonHelpers; - PyObjectPtr pyexpr( PyType_GenericNew( &Expression_Type, 0, 0 ) ); + cppy::ptr pyexpr( PyType_GenericNew( Expression::TypeObject, 0, 0 ) ); if( !pyexpr ) return 0; PyObject* terms = PyTuple_New( PyTuple_GET_SIZE( first->terms ) + 1 ); @@ -278,9 +274,9 @@ PyObject* BinaryAdd::operator()( Expression* first, Term* second ) for( Py_ssize_t i = 0; i < end; ++i ) { PyObject* item = PyTuple_GET_ITEM( first->terms, i ); - PyTuple_SET_ITEM( terms, i, newref( item ) ); + PyTuple_SET_ITEM( terms, i, cppy::incref( item ) ); } - PyTuple_SET_ITEM( terms, end, newref( pyobject_cast( second ) ) ); + PyTuple_SET_ITEM( terms, end, cppy::incref( pyobject_cast( second ) ) ); Expression* expr = reinterpret_cast<Expression*>( pyexpr.get() ); expr->terms = terms; expr->constant = first->constant; @@ -291,7 +287,7 @@ PyObject* BinaryAdd::operator()( Expression* first, Term* second ) template<> inline PyObject* BinaryAdd::operator()( Expression* first, Variable* second ) { - PythonHelpers::PyObjectPtr temp( BinaryMul()( second, 1.0 ) ); + cppy::ptr temp( BinaryMul()( second, 1.0 ) ); if( !temp ) return 0; return operator()( first, reinterpret_cast<Term*>( temp.get() ) ); @@ -301,12 +297,11 @@ PyObject* BinaryAdd::operator()( Expression* first, Variable* second ) template<> inline PyObject* BinaryAdd::operator()( Expression* first, double second ) { - using namespace PythonHelpers; - PyObjectPtr pyexpr( PyType_GenericNew( &Expression_Type, 0, 0 ) ); + cppy::ptr pyexpr( PyType_GenericNew( Expression::TypeObject, 0, 0 ) ); if( !pyexpr ) return 0; Expression* expr = reinterpret_cast<Expression*>( pyexpr.get() ); - expr->terms = newref( first->terms ); + expr->terms = cppy::incref( first->terms ); expr->constant = first->constant + second; return pyexpr.release(); } @@ -315,7 +310,7 @@ PyObject* BinaryAdd::operator()( Expression* first, double second ) template<> inline PyObject* BinaryAdd::operator()( Term* first, double second ) { - PythonHelpers::PyObjectPtr pyexpr( PyType_GenericNew( &Expression_Type, 0, 0 ) ); + cppy::ptr pyexpr( PyType_GenericNew( Expression::TypeObject, 0, 0 ) ); if( !pyexpr ) return 0; Expression* expr = reinterpret_cast<Expression*>( pyexpr.get() ); @@ -337,7 +332,7 @@ PyObject* BinaryAdd::operator()( Term* first, Expression* second ) template<> inline PyObject* BinaryAdd::operator()( Term* first, Term* second ) { - PythonHelpers::PyObjectPtr pyexpr( PyType_GenericNew( &Expression_Type, 0, 0 ) ); + cppy::ptr pyexpr( PyType_GenericNew( Expression::TypeObject, 0, 0 ) ); if( !pyexpr ) return 0; Expression* expr = reinterpret_cast<Expression*>( pyexpr.get() ); @@ -352,7 +347,7 @@ PyObject* BinaryAdd::operator()( Term* first, Term* second ) template<> inline PyObject* BinaryAdd::operator()( Term* first, Variable* second ) { - PythonHelpers::PyObjectPtr temp( BinaryMul()( second, 1.0 ) ); + cppy::ptr temp( BinaryMul()( second, 1.0 ) ); if( !temp ) return 0; return BinaryAdd()( first, reinterpret_cast<Term*>( temp.get() ) ); @@ -362,7 +357,7 @@ PyObject* BinaryAdd::operator()( Term* first, Variable* second ) template<> inline PyObject* BinaryAdd::operator()( Variable* first, double second ) { - PythonHelpers::PyObjectPtr temp( BinaryMul()( first, 1.0 ) ); + cppy::ptr temp( BinaryMul()( first, 1.0 ) ); if( !temp ) return 0; return operator()( reinterpret_cast<Term*>( temp.get() ), second ); @@ -372,7 +367,7 @@ PyObject* BinaryAdd::operator()( Variable* first, double second ) template<> inline PyObject* BinaryAdd::operator()( Variable* first, Variable* second ) { - PythonHelpers::PyObjectPtr temp( BinaryMul()( first, 1.0 ) ); + cppy::ptr temp( BinaryMul()( first, 1.0 ) ); if( !temp ) return 0; return operator()( reinterpret_cast<Term*>( temp.get() ), second ); @@ -382,7 +377,7 @@ PyObject* BinaryAdd::operator()( Variable* first, Variable* second ) template<> inline PyObject* BinaryAdd::operator()( Variable* first, Term* second ) { - PythonHelpers::PyObjectPtr temp( BinaryMul()( first, 1.0 ) ); + cppy::ptr temp( BinaryMul()( first, 1.0 ) ); if( !temp ) return 0; return operator()( reinterpret_cast<Term*>( temp.get() ), second ); @@ -392,7 +387,7 @@ PyObject* BinaryAdd::operator()( Variable* first, Term* second ) template<> inline PyObject* BinaryAdd::operator()( Variable* first, Expression* second ) { - PythonHelpers::PyObjectPtr temp( BinaryMul()( first, 1.0 ) ); + cppy::ptr temp( BinaryMul()( first, 1.0 ) ); if( !temp ) return 0; return operator()( reinterpret_cast<Term*>( temp.get() ), second ); @@ -440,7 +435,7 @@ PyObject* BinarySub::operator()( Variable* first, double second ) template<> inline PyObject* BinarySub::operator()( Variable* first, Variable* second ) { - PythonHelpers::PyObjectPtr temp( UnaryNeg()( second ) ); + cppy::ptr temp( UnaryNeg()( second ) ); if( !temp ) return 0; return BinaryAdd()( first, reinterpret_cast<Term*>( temp.get() ) ); @@ -450,7 +445,7 @@ PyObject* BinarySub::operator()( Variable* first, Variable* second ) template<> inline PyObject* BinarySub::operator()( Variable* first, Term* second ) { - PythonHelpers::PyObjectPtr temp( UnaryNeg()( second ) ); + cppy::ptr temp( UnaryNeg()( second ) ); if( !temp ) return 0; return BinaryAdd()( first, reinterpret_cast<Term*>( temp.get() ) ); @@ -460,7 +455,7 @@ PyObject* BinarySub::operator()( Variable* first, Term* second ) template<> inline PyObject* BinarySub::operator()( Variable* first, Expression* second ) { - PythonHelpers::PyObjectPtr temp( UnaryNeg()( second ) ); + cppy::ptr temp( UnaryNeg()( second ) ); if( !temp ) return 0; return BinaryAdd()( first, reinterpret_cast<Expression*>( temp.get() ) ); @@ -477,7 +472,7 @@ PyObject* BinarySub::operator()( Term* first, double second ) template<> inline PyObject* BinarySub::operator()( Term* first, Variable* second ) { - PythonHelpers::PyObjectPtr temp( UnaryNeg()( second ) ); + cppy::ptr temp( UnaryNeg()( second ) ); if( !temp ) return 0; return BinaryAdd()( first, reinterpret_cast<Term*>( temp.get() ) ); @@ -487,7 +482,7 @@ PyObject* BinarySub::operator()( Term* first, Variable* second ) template<> inline PyObject* BinarySub::operator()( Term* first, Term* second ) { - PythonHelpers::PyObjectPtr temp( UnaryNeg()( second ) ); + cppy::ptr temp( UnaryNeg()( second ) ); if( !temp ) return 0; return BinaryAdd()( first, reinterpret_cast<Term*>( temp.get() ) ); @@ -497,7 +492,7 @@ PyObject* BinarySub::operator()( Term* first, Term* second ) template<> inline PyObject* BinarySub::operator()( Term* first, Expression* second ) { - PythonHelpers::PyObjectPtr temp( UnaryNeg()( second ) ); + cppy::ptr temp( UnaryNeg()( second ) ); if( !temp ) return 0; return BinaryAdd()( first, reinterpret_cast<Expression*>( temp.get() ) ); @@ -514,7 +509,7 @@ PyObject* BinarySub::operator()( Expression* first, double second ) template<> inline PyObject* BinarySub::operator()( Expression* first, Variable* second ) { - PythonHelpers::PyObjectPtr temp( UnaryNeg()( second ) ); + cppy::ptr temp( UnaryNeg()( second ) ); if( !temp ) return 0; return BinaryAdd()( first, reinterpret_cast<Term*>( temp.get() ) ); @@ -524,7 +519,7 @@ PyObject* BinarySub::operator()( Expression* first, Variable* second ) template<> inline PyObject* BinarySub::operator()( Expression* first, Term* second ) { - PythonHelpers::PyObjectPtr temp( UnaryNeg()( second ) ); + cppy::ptr temp( UnaryNeg()( second ) ); if( !temp ) return 0; return BinaryAdd()( first, reinterpret_cast<Term*>( temp.get() ) ); @@ -534,7 +529,7 @@ PyObject* BinarySub::operator()( Expression* first, Term* second ) template<> inline PyObject* BinarySub::operator()( Expression* first, Expression* second ) { - PythonHelpers::PyObjectPtr temp( UnaryNeg()( second ) ); + cppy::ptr temp( UnaryNeg()( second ) ); if( !temp ) return 0; return BinaryAdd()( first, reinterpret_cast<Expression*>( temp.get() ) ); @@ -544,7 +539,7 @@ PyObject* BinarySub::operator()( Expression* first, Expression* second ) template<> inline PyObject* BinarySub::operator()( double first, Variable* second ) { - PythonHelpers::PyObjectPtr temp( UnaryNeg()( second ) ); + cppy::ptr temp( UnaryNeg()( second ) ); if( !temp ) return 0; return BinaryAdd()( first, reinterpret_cast<Term*>( temp.get() ) ); @@ -554,7 +549,7 @@ PyObject* BinarySub::operator()( double first, Variable* second ) template<> inline PyObject* BinarySub::operator()( double first, Term* second ) { - PythonHelpers::PyObjectPtr temp( UnaryNeg()( second ) ); + cppy::ptr temp( UnaryNeg()( second ) ); if( !temp ) return 0; return BinaryAdd()( first, reinterpret_cast<Term*>( temp.get() ) ); @@ -564,7 +559,7 @@ PyObject* BinarySub::operator()( double first, Term* second ) template<> inline PyObject* BinarySub::operator()( double first, Expression* second ) { - PythonHelpers::PyObjectPtr temp( UnaryNeg()( second ) ); + cppy::ptr temp( UnaryNeg()( second ) ); if( !temp ) return 0; return BinaryAdd()( first, reinterpret_cast<Expression*>( temp.get() ) ); @@ -574,10 +569,10 @@ PyObject* BinarySub::operator()( double first, Expression* second ) template<typename T, typename U> PyObject* makecn( T first, U second, kiwi::RelationalOperator op ) { - PythonHelpers::PyObjectPtr pyexpr( BinarySub()( first, second ) ); + cppy::ptr pyexpr( BinarySub()( first, second ) ); if( !pyexpr ) return 0; - PythonHelpers::PyObjectPtr pycn( PyType_GenericNew( &Constraint_Type, 0, 0 ) ); + cppy::ptr pycn( PyType_GenericNew( Constraint::TypeObject, 0, 0 ) ); if( !pycn ) return 0; Constraint* cn = reinterpret_cast<Constraint*>( pycn.get() ); @@ -618,3 +613,6 @@ struct CmpGE return makecn( first, second, kiwi::OP_GE ); } }; + + +} // namespace kiwisolver diff --git a/contrib/python/kiwisolver/py3/py/term.cpp b/contrib/python/kiwisolver/py3/py/term.cpp index 4be64a1eea..923e0cf858 100644 --- a/contrib/python/kiwisolver/py3/py/term.cpp +++ b/contrib/python/kiwisolver/py3/py/term.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 <sstream> -#include <Python.h> -#include "pythonhelpers.h" +#include <cppy/cppy.h> #include "symbolics.h" #include "types.h" #include "util.h" -using namespace PythonHelpers; +namespace kiwisolver +{ + +namespace +{ -static PyObject* + +PyObject* Term_new( PyTypeObject* type, PyObject* args, PyObject* kwargs ) { static const char *kwlist[] = { "variable", "coefficient", 0 }; @@ -27,7 +31,7 @@ Term_new( PyTypeObject* type, PyObject* args, PyObject* kwargs ) &pyvar, &pycoeff ) ) return 0; if( !Variable::TypeCheck( pyvar ) ) - return py_expected_type_fail( pyvar, "Variable" ); + return cppy::type_error( pyvar, "Variable" ); double coefficient = 1.0; if( pycoeff && !convert_to_double( pycoeff, coefficient ) ) return 0; @@ -35,20 +39,20 @@ Term_new( PyTypeObject* type, PyObject* args, PyObject* kwargs ) if( !pyterm ) return 0; Term* self = reinterpret_cast<Term*>( pyterm ); - self->variable = newref( pyvar ); + self->variable = cppy::incref( pyvar ); self->coefficient = coefficient; return pyterm; } -static void +void Term_clear( Term* self ) { Py_CLEAR( self->variable ); } -static int +int Term_traverse( Term* self, visitproc visit, void* arg ) { Py_VISIT( self->variable ); @@ -56,7 +60,7 @@ Term_traverse( Term* self, visitproc visit, void* arg ) } -static void +void Term_dealloc( Term* self ) { PyObject_GC_UnTrack( self ); @@ -65,31 +69,31 @@ Term_dealloc( Term* self ) } -static PyObject* +PyObject* Term_repr( Term* self ) { std::stringstream stream; stream << self->coefficient << " * "; stream << reinterpret_cast<Variable*>( self->variable )->variable.name(); - return FROM_STRING( stream.str().c_str() ); + return PyUnicode_FromString( stream.str().c_str() ); } -static PyObject* +PyObject* Term_variable( Term* self ) { - return newref( self->variable ); + return cppy::incref( self->variable ); } -static PyObject* +PyObject* Term_coefficient( Term* self ) { return PyFloat_FromDouble( self->coefficient ); } -static PyObject* +PyObject* Term_value( Term* self ) { Variable* pyvar = reinterpret_cast<Variable*>( self->variable ); @@ -97,42 +101,42 @@ Term_value( Term* self ) } -static PyObject* +PyObject* Term_add( PyObject* first, PyObject* second ) { return BinaryInvoke<BinaryAdd, Term>()( first, second ); } -static PyObject* +PyObject* Term_sub( PyObject* first, PyObject* second ) { return BinaryInvoke<BinarySub, Term>()( first, second ); } -static PyObject* +PyObject* Term_mul( PyObject* first, PyObject* second ) { return BinaryInvoke<BinaryMul, Term>()( first, second ); } -static PyObject* +PyObject* Term_div( PyObject* first, PyObject* second ) { return BinaryInvoke<BinaryDiv, Term>()( first, second ); } -static PyObject* +PyObject* Term_neg( PyObject* value ) { return UnaryInvoke<UnaryNeg, Term>()( value ); } -static PyObject* +PyObject* Term_richcmp( PyObject* first, PyObject* second, int op ) { switch( op ) @@ -170,129 +174,52 @@ Term_methods[] = { }; -static PyNumberMethods -Term_as_number = { - (binaryfunc)Term_add, /* nb_add */ - (binaryfunc)Term_sub, /* nb_subtract */ - (binaryfunc)Term_mul, /* nb_multiply */ -#if PY_MAJOR_VERSION < 3 - (binaryfunc)Term_div, /* nb_divide */ -#endif - 0, /* nb_remainder */ - 0, /* nb_divmod */ - 0, /* nb_power */ - (unaryfunc)Term_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)Term_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 Term_Type_slots[] = { + { Py_tp_dealloc, void_cast( Term_dealloc ) }, /* tp_dealloc */ + { Py_tp_traverse, void_cast( Term_traverse ) }, /* tp_traverse */ + { Py_tp_clear, void_cast( Term_clear ) }, /* tp_clear */ + { Py_tp_repr, void_cast( Term_repr ) }, /* tp_repr */ + { Py_tp_richcompare, void_cast( Term_richcmp ) }, /* tp_richcompare */ + { Py_tp_methods, void_cast( Term_methods ) }, /* tp_methods */ + { Py_tp_new, void_cast( Term_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( Term_add ) }, /* nb_add */ + { Py_nb_subtract, void_cast( Term_sub ) }, /* nb_subatract */ + { Py_nb_multiply, void_cast( Term_mul ) }, /* nb_multiply */ + { Py_nb_negative, void_cast( Term_neg ) }, /* nb_negative */ + { Py_nb_true_divide, void_cast( Term_div ) }, /* nb_true_divide */ + { 0, 0 }, }; -PyTypeObject Term_Type = { - PyVarObject_HEAD_INIT( &PyType_Type, 0 ) - "kiwisolver.Term", /* tp_name */ - sizeof( Term ), /* tp_basicsize */ - 0, /* tp_itemsize */ - (destructor)Term_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)Term_repr, /* tp_repr */ - (PyNumberMethods*)&Term_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)Term_traverse, /* tp_traverse */ - (inquiry)Term_clear, /* tp_clear */ - (richcmpfunc)Term_richcmp, /* tp_richcompare */ - 0, /* tp_weaklistoffset */ - (getiterfunc)0, /* tp_iter */ - (iternextfunc)0, /* tp_iternext */ - (struct PyMethodDef*)Term_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)Term_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* Term::TypeObject = NULL; + + +PyType_Spec Term::TypeObject_Spec = { + "kiwisolver.Term", /* tp_name */ + sizeof( Term ), /* tp_basicsize */ + 0, /* tp_itemsize */ + Py_TPFLAGS_DEFAULT| + Py_TPFLAGS_HAVE_GC| + Py_TPFLAGS_BASETYPE, /* tp_flags */ + Term_Type_slots /* slots */ }; -int import_term() +bool Term::Ready() { - return PyType_Ready( &Term_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 diff --git a/contrib/python/kiwisolver/py3/py/types.h b/contrib/python/kiwisolver/py3/py/types.h index 628efafbca..afd5b8d79e 100644 --- a/contrib/python/kiwisolver/py3/py/types.h +++ b/contrib/python/kiwisolver/py3/py/types.h @@ -1,39 +1,17 @@ /*----------------------------------------------------------------------------- -| 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. |----------------------------------------------------------------------------*/ #pragma once #include <Python.h> #include <kiwi/kiwi.h> -int import_variable(); - -int import_term(); - -int import_expression(); - -int import_constraint(); - -int import_solver(); - -int import_strength(); - - -extern PyTypeObject Variable_Type; - -extern PyTypeObject Term_Type; - -extern PyTypeObject Expression_Type; - -extern PyTypeObject Constraint_Type; - -extern PyTypeObject Solver_Type; - -extern PyTypeObject strength_Type; +namespace kiwisolver +{ extern PyObject* DuplicateConstraint; @@ -48,15 +26,33 @@ extern PyObject* UnknownEditVariable; extern PyObject* BadRequiredStrength; +struct strength +{ + PyObject_HEAD; + + static PyType_Spec TypeObject_Spec; + + static PyTypeObject* TypeObject; + + static bool Ready(); +}; + + struct Variable { PyObject_HEAD PyObject* context; kiwi::Variable variable; + static PyType_Spec TypeObject_Spec; + + static PyTypeObject* TypeObject; + + static bool Ready(); + static bool TypeCheck( PyObject* obj ) { - return PyObject_TypeCheck( obj, &Variable_Type ) != 0; + return PyObject_TypeCheck( obj, TypeObject ) != 0; } }; @@ -67,9 +63,15 @@ struct Term PyObject* variable; double coefficient; + static PyType_Spec TypeObject_Spec; + + static PyTypeObject* TypeObject; + + static bool Ready(); + static bool TypeCheck( PyObject* obj ) { - return PyObject_TypeCheck( obj, &Term_Type ) != 0; + return PyObject_TypeCheck( obj, TypeObject ) != 0; } }; @@ -80,9 +82,15 @@ struct Expression PyObject* terms; double constant; + static PyType_Spec TypeObject_Spec; + + static PyTypeObject* TypeObject; + + static bool Ready(); + static bool TypeCheck( PyObject* obj ) { - return PyObject_TypeCheck( obj, &Expression_Type ) != 0; + return PyObject_TypeCheck( obj, TypeObject ) != 0; } }; @@ -93,9 +101,15 @@ struct Constraint PyObject* expression; kiwi::Constraint constraint; + static PyType_Spec TypeObject_Spec; + + static PyTypeObject* TypeObject; + + static bool Ready(); + static bool TypeCheck( PyObject* obj ) { - return PyObject_TypeCheck( obj, &Constraint_Type ) != 0; + return PyObject_TypeCheck( obj, TypeObject ) != 0; } }; @@ -105,8 +119,20 @@ struct Solver PyObject_HEAD kiwi::Solver solver; + static PyType_Spec TypeObject_Spec; + + static PyTypeObject* TypeObject; + + static bool Ready(); + static bool TypeCheck( PyObject* obj ) { - return PyObject_TypeCheck( obj, &Solver_Type ) != 0; + return PyObject_TypeCheck( obj, TypeObject ) != 0; } }; + + +bool init_exceptions(); + + +} // namespace kiwisolver diff --git a/contrib/python/kiwisolver/py3/py/util.h b/contrib/python/kiwisolver/py3/py/util.h index 78e9cbd0b8..4b00fa76e6 100644 --- a/contrib/python/kiwisolver/py3/py/util.h +++ b/contrib/python/kiwisolver/py3/py/util.h @@ -1,19 +1,21 @@ /*----------------------------------------------------------------------------- -| 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. |----------------------------------------------------------------------------*/ #pragma once #include <map> #include <string> -#include <Python.h> +#include <cppy/cppy.h> #include <kiwi/kiwi.h> -#include "pythonhelpers.h" #include "types.h" +namespace kiwisolver +{ + inline bool convert_to_double( PyObject* obj, double& out ) { @@ -22,13 +24,6 @@ convert_to_double( PyObject* obj, double& out ) out = PyFloat_AS_DOUBLE( obj ); return true; } -#if PY_MAJOR_VERSION < 3 - if( PyInt_Check( obj ) ) - { - out = double( PyInt_AsLong( obj ) ); - return true; - } -#endif if( PyLong_Check( obj ) ) { out = PyLong_AsDouble( obj ); @@ -36,7 +31,7 @@ convert_to_double( PyObject* obj, double& out ) return false; return true; } - PythonHelpers::py_expected_type_fail( obj, "float, int, or long" ); + cppy::type_error( obj, "float, int, or long" ); return false; } @@ -44,19 +39,7 @@ convert_to_double( PyObject* obj, double& out ) inline bool convert_pystr_to_str( PyObject* value, std::string& out ) { -#if PY_MAJOR_VERSION >= 3 out = PyUnicode_AsUTF8( value ); -#else - if( PyUnicode_Check( value ) ) - { - PythonHelpers::PyObjectPtr py_str( PyUnicode_AsUTF8String( value ) ); - if( !py_str ) - return false; // LCOV_EXCL_LINE - out = PyString_AS_STRING( py_str.get() ); - } - else - out = PyString_AS_STRING( value ); -#endif return true; } @@ -64,13 +47,8 @@ convert_pystr_to_str( PyObject* value, std::string& out ) inline bool convert_to_strength( PyObject* value, double& out ) { -#if PY_MAJOR_VERSION >= 3 if( PyUnicode_Check( value ) ) { -#else - if( PyString_Check( value ) | PyUnicode_Check( value )) - { -#endif std::string str; if( !convert_pystr_to_str( value, str ) ) return false; @@ -103,19 +81,11 @@ convert_to_strength( PyObject* value, double& out ) inline bool convert_to_relational_op( PyObject* value, kiwi::RelationalOperator& out ) { -#if PY_MAJOR_VERSION >= 3 if( !PyUnicode_Check( value ) ) { - PythonHelpers::py_expected_type_fail( value, "unicode" ); + cppy::type_error( value, "str" ); return false; } -#else - if( !(PyString_Check( value ) | PyUnicode_Check( value ) ) ) - { - PythonHelpers::py_expected_type_fail( value, "str or unicode" ); - return false; - } -#endif std::string str; if( !convert_pystr_to_str( value, str ) ) return false; @@ -142,7 +112,7 @@ inline PyObject* make_terms( const std::map<PyObject*, double>& coeffs ) { typedef std::map<PyObject*, double>::const_iterator iter_t; - PythonHelpers::PyObjectPtr terms( PyTuple_New( coeffs.size() ) ); + cppy::ptr terms( PyTuple_New( coeffs.size() ) ); if( !terms ) return 0; Py_ssize_t size = PyTuple_GET_SIZE( terms.get() ); @@ -153,11 +123,11 @@ make_terms( const std::map<PyObject*, double>& coeffs ) iter_t end = coeffs.end(); for( ; it != end; ++it, ++i ) { - PyObject* pyterm = PyType_GenericNew( &Term_Type, 0, 0 ); + PyObject* pyterm = PyType_GenericNew( Term::TypeObject, 0, 0 ); if( !pyterm ) return 0; Term* term = reinterpret_cast<Term*>( pyterm ); - term->variable = PythonHelpers::newref( it->first ); + term->variable = cppy::incref( it->first ); term->coefficient = it->second; PyTuple_SET_ITEM( terms.get(), i, pyterm ); } @@ -177,10 +147,10 @@ reduce_expression( PyObject* pyexpr ) // pyexpr must be an Expression Term* term = reinterpret_cast<Term*>( item ); coeffs[ term->variable ] += term->coefficient; } - PythonHelpers::PyObjectPtr terms( make_terms( coeffs ) ); + cppy::ptr terms( make_terms( coeffs ) ); if( !terms ) return 0; - PyObject* pynewexpr = PyType_GenericNew( &Expression_Type, 0, 0 ); + PyObject* pynewexpr = PyType_GenericNew( Expression::TypeObject, 0, 0 ); if( !pynewexpr ) return 0; Expression* newexpr = reinterpret_cast<Expression*>( pynewexpr ); @@ -228,3 +198,6 @@ pyop_str( int op ) return ""; } } + + +} // namespace kiwisolver 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 |