From 50ca24fd55910b055d3f4552aa2795ea7242ff31 Mon Sep 17 00:00:00 2001 From: wmayer Date: Wed, 10 Aug 2016 11:55:23 +0200 Subject: [PATCH] upgrade PyCXX from v6.2.5 to v6.2.8 --- src/CXX/IndirectPythonInterface.cxx | 11 +++++ src/CXX/Python2/ExtensionType.hxx | 2 +- src/CXX/Python2/Objects.hxx | 74 +++++++++++++++++++---------- src/CXX/Python2/cxx_extensions.cxx | 50 ++++++++++--------- src/CXX/Python3/ExtensionType.hxx | 4 +- src/CXX/Python3/Objects.hxx | 55 +++++++++++++++++---- src/CXX/Python3/cxx_extensions.cxx | 62 ++---------------------- src/CXX/Version.hxx | 2 +- 8 files changed, 142 insertions(+), 118 deletions(-) diff --git a/src/CXX/IndirectPythonInterface.cxx b/src/CXX/IndirectPythonInterface.cxx index 661423a41..dd63bf356 100644 --- a/src/CXX/IndirectPythonInterface.cxx +++ b/src/CXX/IndirectPythonInterface.cxx @@ -91,7 +91,9 @@ static PyObject *ptr__Exc_NotImplementedError = NULL; static PyObject *ptr__Exc_OSError = NULL; static PyObject *ptr__Exc_OverflowError = NULL; static PyObject *ptr__Exc_RuntimeError = NULL; +#if PY_MAJOR_VERSION == 2 static PyObject *ptr__Exc_StandardError = NULL; +#endif static PyObject *ptr__Exc_SyntaxError = NULL; static PyObject *ptr__Exc_SystemError = NULL; static PyObject *ptr__Exc_SystemExit = NULL; @@ -125,6 +127,7 @@ static PyTypeObject *ptr__Slice_Type = NULL; static PyTypeObject *ptr__TraceBack_Type = NULL; static PyTypeObject *ptr__Tuple_Type = NULL; static PyTypeObject *ptr__Type_Type = NULL; +static PyTypeObject *ptr__Unicode_Type = NULL; #if PY_MAJOR_VERSION == 2 static PyTypeObject *ptr__Int_Type = NULL; static PyTypeObject *ptr__String_Type = NULL; @@ -264,7 +267,9 @@ bool InitialisePythonIndirectInterface() ptr__Exc_OSError = GetPyObjectPointer_As_PyObjectPointer( "PyExc_OSError" ); ptr__Exc_OverflowError = GetPyObjectPointer_As_PyObjectPointer( "PyExc_OverflowError" ); ptr__Exc_RuntimeError = GetPyObjectPointer_As_PyObjectPointer( "PyExc_RuntimeError" ); +#if PY_MAJOR_VERSION == 2 ptr__Exc_StandardError = GetPyObjectPointer_As_PyObjectPointer( "PyExc_StandardError" ); +#endif ptr__Exc_SyntaxError = GetPyObjectPointer_As_PyObjectPointer( "PyExc_SyntaxError" ); ptr__Exc_SystemError = GetPyObjectPointer_As_PyObjectPointer( "PyExc_SystemError" ); ptr__Exc_SystemExit = GetPyObjectPointer_As_PyObjectPointer( "PyExc_SystemExit" ); @@ -280,7 +285,11 @@ bool InitialisePythonIndirectInterface() ptr__Exc_UnicodeError = GetPyObjectPointer_As_PyObjectPointer( "PyExc_UnicodeError" ); ptr__PyNone = GetPyObject_As_PyObjectPointer( "_Py_NoneStruct" ); +#if PY_MAJOR_VERSION == 2 ptr__PyFalse = GetPyObject_As_PyObjectPointer( "_Py_ZeroStruct" ); +#else + ptr__PyFalse = GetPyObject_As_PyObjectPointer( "_Py_FalseStruct" ); +#endif ptr__PyTrue = GetPyObject_As_PyObjectPointer( "_Py_TrueStruct" ); ptr__CFunction_Type = GetPyTypeObject_As_PyTypeObjectPointer( "PyCFunction_Type" ); @@ -343,7 +352,9 @@ PYCXX_EXPORT PyObject *_Exc_NotImplementedError() { return ptr__Exc_NotImplem PYCXX_EXPORT PyObject *_Exc_OSError() { return ptr__Exc_OSError; } PYCXX_EXPORT PyObject *_Exc_OverflowError() { return ptr__Exc_OverflowError; } PYCXX_EXPORT PyObject *_Exc_RuntimeError() { return ptr__Exc_RuntimeError; } +#if PY_MAJOR_VERSION == 2 PYCXX_EXPORT PyObject *_Exc_StandardError() { return ptr__Exc_StandardError; } +#endif PYCXX_EXPORT PyObject *_Exc_SyntaxError() { return ptr__Exc_SyntaxError; } PYCXX_EXPORT PyObject *_Exc_SystemError() { return ptr__Exc_SystemError; } PYCXX_EXPORT PyObject *_Exc_SystemExit() { return ptr__Exc_SystemExit; } diff --git a/src/CXX/Python2/ExtensionType.hxx b/src/CXX/Python2/ExtensionType.hxx index 24480b6ed..84ca5534c 100644 --- a/src/CXX/Python2/ExtensionType.hxx +++ b/src/CXX/Python2/ExtensionType.hxx @@ -220,7 +220,7 @@ namespace Py #else const char *default_name = "unknown"; #endif - p = new PythonType( sizeof( T ), 0, default_name ); + p = new PythonType( sizeof( PythonClassInstance ), 0, default_name ); p->set_tp_new( extension_object_new ); p->set_tp_init( extension_object_init ); p->set_tp_dealloc( extension_object_deallocator ); diff --git a/src/CXX/Python2/Objects.hxx b/src/CXX/Python2/Objects.hxx index 0d6b57f51..5311314b7 100644 --- a/src/CXX/Python2/Objects.hxx +++ b/src/CXX/Python2/Objects.hxx @@ -49,6 +49,8 @@ #include #include #include +#include + namespace Py { @@ -101,19 +103,20 @@ namespace Py // /* explicit MyType (PyObject *pyob): Object(pyob) { - validate(); -} + validate(); + } MyType(const Object& other): Object(other.ptr()) { - validate(); -} + validate(); + } */ // Alernate version for the constructor to allow for construction from owned pointers: /* - explicit MyType (PyObject *pyob): Object(pyob) { - validate(); -} + explicit MyType (PyObject *pyob) + : Object(pyob) { + validate(); + } */ // You may wish to add other constructors; see the classes below for examples. @@ -123,14 +126,14 @@ namespace Py // (4) Each class needs at least these two assignment operators: /* MyType& operator= (const Object& rhs) { - return (*this = *rhs); -} + return (*this = *rhs); + } Mytype& operator= (PyObject* rhsp) { - if(ptr() == rhsp) return *this; - set(rhsp); - return *this; -} + if(ptr() == rhsp) return *this; + set(rhsp); + return *this; + } */ // Note on accepts: constructors call the base class // version of a virtual when calling the base class constructor, @@ -174,7 +177,8 @@ namespace Py public: // Constructor acquires new ownership of pointer unless explicitly told not to. - explicit Object (PyObject* pyob=Py::_None(), bool owned = false): p (pyob) + explicit Object (PyObject* pyob=Py::_None(), bool owned = false) + : p(pyob) { if(!owned) { @@ -184,7 +188,8 @@ namespace Py } // Copy constructor acquires new ownership of pointer - Object (const Object& ob): p(ob.p) + Object (const Object& ob) + : p(ob.p) { Py::_XINCREF (p); validate(); @@ -226,9 +231,10 @@ namespace Py { // not allowed to commit suicide, however if(reference_count() == 1) - throw RuntimeError("Object::decrement_reference_count error."); + throw RuntimeError("Object::decrement_reference_count error."); Py::_XDECREF(p); } + // Would like to call this pointer() but messes up STL in SeqBase PyObject* ptr () const { @@ -371,13 +377,13 @@ namespace Py void setAttr (const std::string& s, const Object& value) { if(PyObject_SetAttrString (p, const_cast(s.c_str()), *value) == -1) - throw AttributeError ("getAttr failed."); + throw AttributeError ("setAttr failed."); } void delAttr (const std::string& s) { if(PyObject_DelAttrString (p, const_cast(s.c_str())) == -1) - throw AttributeError ("delAttr failed."); + throw AttributeError ("delAttr failed."); } // PyObject_SetItem is too weird to be using from C++ @@ -385,9 +391,9 @@ namespace Py void delItem (const Object& key) { - //if(PyObject_DelItem(p, *key) == -1) - // failed to link on Windows? - throw KeyError("delItem failed."); + if(PyObject_DelItem(p, *key) == -1) + // failed to link on Windows? + throw KeyError("delItem failed."); } // Equality and comparison use PyObject_RichCompareBool @@ -2909,7 +2915,8 @@ namespace Py // Queries List keys () const { - return List(PyMapping_Keys(ptr()), true); + static char keys[] = {'k', 'e', 'y', 's', 0}; + return List(PyObject_CallMethod( ptr(), keys, NULL ), true ); } List values () const @@ -3212,18 +3219,35 @@ namespace Py // Call Object apply(const Tuple& args) const { - return asObject(PyObject_CallObject(ptr(), args.ptr())); + PyObject *result = PyObject_CallObject( ptr(), args.ptr() ); + if( result == NULL ) + { + throw Exception(); + } + return asObject( result ); } // Call with keywords Object apply(const Tuple& args, const Dict& kw) const { - return asObject( PyEval_CallObjectWithKeywords( ptr(), args.ptr(), kw.ptr() ) ); + PyObject *result = PyEval_CallObjectWithKeywords( ptr(), args.ptr(), kw.ptr() ); + if( result == NULL ) + { + throw Exception(); + } + return asObject( result ); } Object apply(PyObject* pargs = 0) const { - return apply (Tuple(pargs)); + if( pargs == 0 ) + { + return apply( Tuple() ); + } + else + { + return apply( Tuple( pargs ) ); + } } }; diff --git a/src/CXX/Python2/cxx_extensions.cxx b/src/CXX/Python2/cxx_extensions.cxx index 1e10e5167..165f14d35 100644 --- a/src/CXX/Python2/cxx_extensions.cxx +++ b/src/CXX/Python2/cxx_extensions.cxx @@ -191,6 +191,7 @@ ExtensionModuleBase::ExtensionModuleBase( const char *name ) : m_module_name( name ) , m_full_module_name( __Py_PackageContext() != NULL ? std::string( __Py_PackageContext() ) : m_module_name ) , m_method_table() +, m_module( NULL ) {} ExtensionModuleBase::~ExtensionModuleBase() @@ -222,7 +223,7 @@ public: void ExtensionModuleBase::initialize( const char *module_doc ) { PyObject *module_ptr = new ExtensionModuleBasePtr( this ); - Py_InitModule4 + m_module = Py_InitModule4 ( const_cast( m_module_name.c_str() ), // name m_method_table.table(), // methods @@ -242,6 +243,11 @@ Py::Dict ExtensionModuleBase::moduleDictionary( void ) const return module().getDict(); } +Object ExtensionModuleBase::moduleObject( void ) const +{ + return Object( m_module ); +} + //================================================================================ // // Implementation of PythonType @@ -1787,27 +1793,27 @@ extern "C" PyObject *method_noargs_call_handler( PyObject *_self_and_name_tuple, return 0; } #if 0 - try - { - Tuple self_and_name_tuple( _self_and_name_tuple ); - - PyObject *self_in_cobject = self_and_name_tuple[0].ptr(); - void *self_as_void = PyCObject_AsVoidPtr( self_in_cobject ); - if( self_as_void == NULL ) - return NULL; - - ExtensionModuleBase *self = static_cast( self_as_void ); - - String py_name( self_and_name_tuple[1] ); - std::string name( py_name.as_std_string( NULL ) ); - - Object result( self->invoke_method_noargs( name ) ); - - return new_reference_to( result.ptr() ); - } - catch( Exception & ) - { - return 0; + try + { + Tuple self_and_name_tuple( _self_and_name_tuple ); + + PyObject *self_in_cobject = self_and_name_tuple[0].ptr(); + void *self_as_void = PyCObject_AsVoidPtr( self_in_cobject ); + if( self_as_void == NULL ) + return NULL; + + ExtensionModuleBase *self = static_cast( self_as_void ); + + String py_name( self_and_name_tuple[1] ); + std::string name( py_name.as_std_string( NULL ) ); + + Object result( self->invoke_method_noargs( name ) ); + + return new_reference_to( result.ptr() ); + } + catch( Exception & ) + { + return 0; } #else return 0; diff --git a/src/CXX/Python3/ExtensionType.hxx b/src/CXX/Python3/ExtensionType.hxx index 1524a8afb..90829e21c 100644 --- a/src/CXX/Python3/ExtensionType.hxx +++ b/src/CXX/Python3/ExtensionType.hxx @@ -124,7 +124,7 @@ namespace Py ~ExtensionClassMethodsTable() { - delete m_methods_table; + delete[] m_methods_table; } // check that all methods added are unique @@ -219,7 +219,7 @@ namespace Py #else const char *default_name = "unknown"; #endif - p = new PythonType( sizeof( T ), 0, default_name ); + p = new PythonType( sizeof( PythonClassInstance ), 0, default_name ); p->set_tp_new( extension_object_new ); p->set_tp_init( extension_object_init ); p->set_tp_dealloc( extension_object_deallocator ); diff --git a/src/CXX/Python3/Objects.hxx b/src/CXX/Python3/Objects.hxx index 9e6b3044d..21cd89170 100644 --- a/src/CXX/Python3/Objects.hxx +++ b/src/CXX/Python3/Objects.hxx @@ -50,6 +50,7 @@ #include #include #include +#include namespace Py { @@ -221,7 +222,9 @@ namespace Py { // not allowed to commit suicide, however if( reference_count() == 1 ) - throw RuntimeError( "Object::decrement_reference_count error." ); + { + throw RuntimeError( "Object::decrement_reference_count error." ); + } Py::_XDECREF( p ); } @@ -378,13 +381,17 @@ namespace Py void setAttr( const std::string &s, const Object &value ) { if( PyObject_SetAttrString( p, const_cast( s.c_str() ), *value ) == -1 ) - throw AttributeError( "getAttr failed." ); + { + throw AttributeError( "setAttr failed." ); + } } void delAttr( const std::string &s ) { if( PyObject_DelAttrString( p, const_cast( s.c_str() ) ) == -1 ) + { throw AttributeError( "delAttr failed." ); + } } // PyObject_SetItem is too weird to be using from C++ @@ -392,9 +399,11 @@ namespace Py void delItem( const Object &key ) { - //if( PyObject_DelItem( p, *key ) == -1 ) - // failed to link on Windows? - throw KeyError( "delItem failed." ); + if( PyObject_DelItem( p, *key ) == -1 ) + { + // failed to link on Windows? + throw KeyError( "delItem failed." ); + } } // Equality and comparison use PyObject_Compare @@ -413,7 +422,7 @@ namespace Py { } - virtual bool accepts( PyObject *pyob ) + virtual bool accepts( PyObject *pyob ) const { return pyob == NULL; } @@ -1408,14 +1417,18 @@ namespace Py void verify_length( size_type required_size ) const { if( size() != required_size ) + { throw IndexError( "Unexpected SeqBase length." ); + } } void verify_length( size_type min_size, size_type max_size ) const { size_type n = size(); if( n < min_size || n > max_size ) + { throw IndexError( "Unexpected SeqBase length." ); + } } class PYCXX_EXPORT iterator @@ -1520,8 +1533,9 @@ namespace Py int operator-( const iterator &other ) const { if( seq->ptr() != other.seq->ptr() ) + { throw RuntimeError( "SeqBase::iterator comparison error" ); - + } return count - other.count; } @@ -1672,7 +1686,9 @@ namespace Py int operator-( const const_iterator &other ) const { if( *seq != *other.seq ) + { throw RuntimeError( "SeqBase::const_iterator::- error" ); + } return count - other.count; } @@ -3122,7 +3138,9 @@ namespace Py Callable &operator=( PyObject *rhsp ) { if( ptr() != rhsp ) + { set( rhsp ); + } return *this; } @@ -3135,18 +3153,35 @@ namespace Py // Call Object apply( const Tuple &args ) const { - return asObject( PyObject_CallObject( ptr(), args.ptr() ) ); + PyObject *result = PyObject_CallObject( ptr(), args.ptr() ); + if( result == NULL ) + { + throw Exception(); + } + return asObject( result ); } // Call with keywords Object apply( const Tuple &args, const Dict &kw ) const { - return asObject( PyEval_CallObjectWithKeywords( ptr(), args.ptr(), kw.ptr() ) ); + PyObject *result = PyEval_CallObjectWithKeywords( ptr(), args.ptr(), kw.ptr() ); + if( result == NULL ) + { + throw Exception(); + } + return asObject( result ); } Object apply( PyObject *pargs = 0 ) const { - return apply( Tuple( pargs ) ); + if( pargs == 0 ) + { + return apply( Tuple() ); + } + else + { + return apply( Tuple( pargs ) ); + } } }; diff --git a/src/CXX/Python3/cxx_extensions.cxx b/src/CXX/Python3/cxx_extensions.cxx index 0daa4c8b5..22f804274 100644 --- a/src/CXX/Python3/cxx_extensions.cxx +++ b/src/CXX/Python3/cxx_extensions.cxx @@ -205,6 +205,11 @@ Py::Dict ExtensionModuleBase::moduleDictionary( void ) const return module().getDict(); } +Object ExtensionModuleBase::moduleObject( void ) const +{ + return Object( m_module ); +} + //================================================================================ // // Implementation of PythonType @@ -1518,63 +1523,6 @@ extern "C" PyObject *method_noargs_call_handler( PyObject *_self_and_name_tuple, } } -#if 0 -extern "C" PyObject *method_noargs_call_handler( PyObject *_self_and_name_tuple, PyObject * ) -{ - try - { - Tuple self_and_name_tuple( _self_and_name_tuple ); - - PyObject *self_in_cobject = self_and_name_tuple[0].ptr(); - void *self_as_void = PyCObject_AsVoidPtr( self_in_cobject ); - if( self_as_void == NULL ) - return NULL; - - ExtensionModuleBase *self = static_cast( self_as_void ); - - Object result - ( - self->invoke_method_noargs - ( - PyCObject_AsVoidPtr( self_and_name_tuple[1].ptr() ) - ) - ); - - return new_reference_to( result.ptr() ); - } - catch( Exception & ) - { - return 0; - } -// #if 0 - try - { - Tuple self_and_name_tuple( _self_and_name_tuple ); - - PyObject *self_in_cobject = self_and_name_tuple[0].ptr(); - void *self_as_void = PyCObject_AsVoidPtr( self_in_cobject ); - if( self_as_void == NULL ) - return NULL; - - ExtensionModuleBase *self = static_cast( self_as_void ); - - String py_name( self_and_name_tuple[1] ); - std::string name( py_name.as_std_string( NULL ) ); - - Object result( self->invoke_method_noargs( name ) ); - - return new_reference_to( result.ptr() ); - } - catch( Exception & ) - { - return 0; - } -// #else - return 0; -// #endif -} -#endif - extern "C" PyObject *method_varargs_call_handler( PyObject *_self_and_name_tuple, PyObject *_args ) { try diff --git a/src/CXX/Version.hxx b/src/CXX/Version.hxx index f5a5d6536..cdca51932 100644 --- a/src/CXX/Version.hxx +++ b/src/CXX/Version.hxx @@ -40,7 +40,7 @@ #define PYCXX_VERSION_MAJOR 6 #define PYCXX_VERSION_MINOR 2 -#define PYCXX_VERSION_PATCH 5 +#define PYCXX_VERSION_PATCH 8 #define PYCXX_MAKEVERSION( major, minor, patch ) ((major<<16)|(minor<<8)|(patch)) #define PYCXX_VERSION PYCXX_MAKEVERSION( PYCXX_VERSION_MAJOR, PYCXX_VERSION_MINOR, PYCXX_VERSION_PATCH ) #endif