diff --git a/src/CXX/Config.hxx b/src/CXX/Config.hxx index 536e7c8820..951d22c9ac 100644 --- a/src/CXX/Config.hxx +++ b/src/CXX/Config.hxx @@ -36,6 +36,12 @@ //----------------------------------------------------------------------------- #include "CXX/WrapPython.h" +#if defined( PYCXX_6_2_COMPATIBILITY ) + typedef int PyCxx_ssize_t; +#else + typedef Py_ssize_t PyCxx_ssize_t; +#endif + #if PY_MAJOR_VERSION == 2 #include "CXX/Python2/Config.hxx" #else diff --git a/src/CXX/CxxDebug.hxx b/src/CXX/CxxDebug.hxx new file mode 100644 index 0000000000..508395f7a2 --- /dev/null +++ b/src/CXX/CxxDebug.hxx @@ -0,0 +1,41 @@ +//----------------------------------------------------------------------------- +// +// Copyright (c) 1998 - 2007, The Regents of the University of California +// Produced at the Lawrence Livermore National Laboratory +// All rights reserved. +// +// This file is part of PyCXX. For details,see http://cxx.sourceforge.net/. The +// full copyright notice is contained in the file COPYRIGHT located at the root +// of the PyCXX distribution. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are met: +// +// - Redistributions of source code must retain the above copyright notice, +// this list of conditions and the disclaimer below. +// - Redistributions in binary form must reproduce the above copyright notice, +// this list of conditions and the disclaimer (as noted below) in the +// documentation and/or materials provided with the distribution. +// - Neither the name of the UC/LLNL nor the names of its contributors may be +// used to endorse or promote products derived from this software without +// specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +// ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OF THE UNIVERSITY OF +// CALIFORNIA, THE U.S. DEPARTMENT OF ENERGY OR CONTRIBUTORS BE LIABLE FOR +// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY +// OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH +// DAMAGE. +// +//----------------------------------------------------------------------------- +#if PY_MAJOR_VERSION == 2 +#include "CXX/Python2/CxxDebug.hxx" +#else +#include "CXX/Python3/CxxDebug.hxx" +#endif diff --git a/src/CXX/IndirectPythonInterface.cxx b/src/CXX/IndirectPythonInterface.cxx index c04c202383..20bd11e068 100644 --- a/src/CXX/IndirectPythonInterface.cxx +++ b/src/CXX/IndirectPythonInterface.cxx @@ -72,46 +72,17 @@ PYCXX_EXPORT bool _Bytes_Check( PyObject *op ) { return op->ob_type == _By static HMODULE python_dll; -static PyObject *ptr__Exc_ArithmeticError = NULL; -static PyObject *ptr__Exc_AssertionError = NULL; -static PyObject *ptr__Exc_AttributeError = NULL; -static PyObject *ptr__Exc_EnvironmentError = NULL; -static PyObject *ptr__Exc_EOFError = NULL; -static PyObject *ptr__Exc_Exception = NULL; -static PyObject *ptr__Exc_FloatingPointError = NULL; -static PyObject *ptr__Exc_ImportError = NULL; -static PyObject *ptr__Exc_IndexError = NULL; -static PyObject *ptr__Exc_IOError = NULL; -static PyObject *ptr__Exc_KeyboardInterrupt = NULL; -static PyObject *ptr__Exc_KeyError = NULL; -static PyObject *ptr__Exc_LookupError = NULL; -static PyObject *ptr__Exc_MemoryError = NULL; -static PyObject *ptr__Exc_NameError = NULL; -static PyObject *ptr__Exc_NotImplementedError = NULL; -static PyObject *ptr__Exc_OSError = NULL; -static PyObject *ptr__Exc_OverflowError = NULL; -static PyObject *ptr__Exc_RuntimeError = NULL; +#define PYCXX_STANDARD_EXCEPTION( eclass, bclass ) \ + static PyObject *ptr_Exc_##eclass = 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; -static PyObject *ptr__Exc_TypeError = NULL; -static PyObject *ptr__Exc_ValueError = NULL; -static PyObject *ptr__Exc_ZeroDivisionError = NULL; - -#ifdef MS_WINDOWS -static PyObject *ptr__Exc_WindowsError = NULL; +#include "CXX/Python2/cxx_standard_exceptions.hxx" +#else +#include "CXX/Python3/cxx_standard_exceptions.hxx" #endif -static PyObject *ptr__Exc_IndentationError = NULL; -static PyObject *ptr__Exc_TabError = NULL; -static PyObject *ptr__Exc_UnboundLocalError = NULL; -static PyObject *ptr__Exc_UnicodeError = NULL; -static PyObject *ptr__PyNone = NULL; -static PyObject *ptr__PyFalse = NULL; -static PyObject *ptr__PyTrue = NULL; +#undef PYCXX_STANDARD_EXCEPTION + static PyTypeObject *ptr__CFunction_Type = NULL; static PyTypeObject *ptr__Complex_Type = NULL; static PyTypeObject *ptr__Dict_Type = NULL; @@ -248,41 +219,17 @@ bool InitialisePythonIndirectInterface() ptr_Py_VerboseFlag = GetInt_as_IntPointer( "Py_VerboseFlag" ); ptr__Py_PackageContext = GetCharPointer_as_CharPointerPointer( "_Py_PackageContext" ); - ptr__Exc_ArithmeticError = GetPyObjectPointer_As_PyObjectPointer( "PyExc_ArithmeticError" ); - ptr__Exc_AssertionError = GetPyObjectPointer_As_PyObjectPointer( "PyExc_AssertionError" ); - ptr__Exc_AttributeError = GetPyObjectPointer_As_PyObjectPointer( "PyExc_AttributeError" ); - ptr__Exc_EnvironmentError = GetPyObjectPointer_As_PyObjectPointer( "PyExc_EnvironmentError" ); - ptr__Exc_EOFError = GetPyObjectPointer_As_PyObjectPointer( "PyExc_EOFError" ); - ptr__Exc_Exception = GetPyObjectPointer_As_PyObjectPointer( "PyExc_Exception" ); - ptr__Exc_FloatingPointError = GetPyObjectPointer_As_PyObjectPointer( "PyExc_FloatingPointError" ); - ptr__Exc_ImportError = GetPyObjectPointer_As_PyObjectPointer( "PyExc_ImportError" ); - ptr__Exc_IndexError = GetPyObjectPointer_As_PyObjectPointer( "PyExc_IndexError" ); - ptr__Exc_IOError = GetPyObjectPointer_As_PyObjectPointer( "PyExc_IOError" ); - ptr__Exc_KeyboardInterrupt = GetPyObjectPointer_As_PyObjectPointer( "PyExc_KeyboardInterrupt" ); - ptr__Exc_KeyError = GetPyObjectPointer_As_PyObjectPointer( "PyExc_KeyError" ); - ptr__Exc_LookupError = GetPyObjectPointer_As_PyObjectPointer( "PyExc_LookupError" ); - ptr__Exc_MemoryError = GetPyObjectPointer_As_PyObjectPointer( "PyExc_MemoryError" ); - ptr__Exc_NameError = GetPyObjectPointer_As_PyObjectPointer( "PyExc_NameError" ); - ptr__Exc_NotImplementedError= GetPyObjectPointer_As_PyObjectPointer( "PyExc_NotImplementedError" ); - ptr__Exc_OSError = GetPyObjectPointer_As_PyObjectPointer( "PyExc_OSError" ); - ptr__Exc_OverflowError = GetPyObjectPointer_As_PyObjectPointer( "PyExc_OverflowError" ); - ptr__Exc_RuntimeError = GetPyObjectPointer_As_PyObjectPointer( "PyExc_RuntimeError" ); +#define PYCXX_STANDARD_EXCEPTION( eclass, bclass ) + ptr_Exc_#eclass = GetPyTypeObject_As_PyTypeObjectPointer( "PyExc_" #eclass ); + #if PY_MAJOR_VERSION == 2 - ptr__Exc_StandardError = GetPyObjectPointer_As_PyObjectPointer( "PyExc_StandardError" ); +#include "CXX/Python2/cxx_standard_exceptions.hxx" +#else +#include "CXX/Python3/cxx_standard_exceptions.hxx" #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" ); - ptr__Exc_TypeError = GetPyObjectPointer_As_PyObjectPointer( "PyExc_TypeError" ); - ptr__Exc_ValueError = GetPyObjectPointer_As_PyObjectPointer( "PyExc_ValueError" ); -#ifdef MS_WINDOWS - ptr__Exc_WindowsError = GetPyObjectPointer_As_PyObjectPointer( "PyExc_WindowsError" ); -#endif - ptr__Exc_ZeroDivisionError = GetPyObjectPointer_As_PyObjectPointer( "PyExc_ZeroDivisionError" ); - ptr__Exc_IndentationError = GetPyObjectPointer_As_PyObjectPointer( "PyExc_IndentationError" ); - ptr__Exc_TabError = GetPyObjectPointer_As_PyObjectPointer( "PyExc_TabError" ); - ptr__Exc_UnboundLocalError = GetPyObjectPointer_As_PyObjectPointer( "PyExc_UnboundLocalError" ); - ptr__Exc_UnicodeError = GetPyObjectPointer_As_PyObjectPointer( "PyExc_UnicodeError" ); + +#undef PYCXX_STANDARD_EXCEPTION + ptr__PyNone = GetPyObject_As_PyObjectPointer( "_Py_NoneStruct" ); # if PY_MAJOR_VERSION == 2 @@ -469,43 +416,16 @@ void _XDECREF( PyObject *op ) // // Wrap variables as function calls // +#define PYCXX_STANDARD_EXCEPTION( eclass, bclass ) \ + PYCXX_EXPORT PyObject *_Exc_##eclass() { return ::PyExc_##eclass; } -PYCXX_EXPORT PyObject *_Exc_ArithmeticError() { return ::PyExc_ArithmeticError; } -PYCXX_EXPORT PyObject *_Exc_AssertionError() { return ::PyExc_AssertionError; } -PYCXX_EXPORT PyObject *_Exc_AttributeError() { return ::PyExc_AttributeError; } -PYCXX_EXPORT PyObject *_Exc_EnvironmentError() { return ::PyExc_EnvironmentError; } -PYCXX_EXPORT PyObject *_Exc_EOFError() { return ::PyExc_EOFError; } -PYCXX_EXPORT PyObject *_Exc_Exception() { return ::PyExc_Exception; } -PYCXX_EXPORT PyObject *_Exc_FloatingPointError() { return ::PyExc_FloatingPointError; } -PYCXX_EXPORT PyObject *_Exc_ImportError() { return ::PyExc_ImportError; } -PYCXX_EXPORT PyObject *_Exc_IndexError() { return ::PyExc_IndexError; } -PYCXX_EXPORT PyObject *_Exc_IOError() { return ::PyExc_IOError; } -PYCXX_EXPORT PyObject *_Exc_KeyboardInterrupt() { return ::PyExc_KeyboardInterrupt; } -PYCXX_EXPORT PyObject *_Exc_KeyError() { return ::PyExc_KeyError; } -PYCXX_EXPORT PyObject *_Exc_LookupError() { return ::PyExc_LookupError; } -PYCXX_EXPORT PyObject *_Exc_MemoryError() { return ::PyExc_MemoryError; } -PYCXX_EXPORT PyObject *_Exc_NameError() { return ::PyExc_NameError; } -PYCXX_EXPORT PyObject *_Exc_NotImplementedError() { return ::PyExc_NotImplementedError; } -PYCXX_EXPORT PyObject *_Exc_OSError() { return ::PyExc_OSError; } -PYCXX_EXPORT PyObject *_Exc_OverflowError() { return ::PyExc_OverflowError; } -PYCXX_EXPORT PyObject *_Exc_RuntimeError() { return ::PyExc_RuntimeError; } -PYCXX_EXPORT PyObject *_Exc_SyntaxError() { return ::PyExc_SyntaxError; } -PYCXX_EXPORT PyObject *_Exc_SystemError() { return ::PyExc_SystemError; } -PYCXX_EXPORT PyObject *_Exc_SystemExit() { return ::PyExc_SystemExit; } -PYCXX_EXPORT PyObject *_Exc_TypeError() { return ::PyExc_TypeError; } -PYCXX_EXPORT PyObject *_Exc_ValueError() { return ::PyExc_ValueError; } -PYCXX_EXPORT PyObject *_Exc_ZeroDivisionError() { return ::PyExc_ZeroDivisionError; } -PYCXX_EXPORT PyObject *_Exc_IndentationError() { return ::PyExc_IndentationError; } -PYCXX_EXPORT PyObject *_Exc_TabError() { return ::PyExc_TabError; } -PYCXX_EXPORT PyObject *_Exc_UnboundLocalError() { return ::PyExc_UnboundLocalError; } -PYCXX_EXPORT PyObject *_Exc_UnicodeError() { return ::PyExc_UnicodeError; } - -#ifdef MS_WINDOWS -PYCXX_EXPORT PyObject *_Exc_WindowsError() { return ::PyExc_WindowsError; } +#if PY_MAJOR_VERSION == 2 +#include "CXX/Python2/cxx_standard_exceptions.hxx" +#else +#include "CXX/Python3/cxx_standard_exceptions.hxx" #endif - - +#undef PYCXX_STANDARD_EXCEPTION // // wrap items in Object.h diff --git a/src/CXX/Python2/ExtensionModule.hxx b/src/CXX/Python2/ExtensionModule.hxx index bf2e49c8a6..26306b71a6 100644 --- a/src/CXX/Python2/ExtensionModule.hxx +++ b/src/CXX/Python2/ExtensionModule.hxx @@ -139,10 +139,12 @@ namespace Py args[0] = Object( self, true ); args[1] = Object( PyCObject_FromVoidPtr( method_def, do_not_dealloc ), true ); - PyObject *func = PyCFunction_New + assert( m_module != NULL ); + PyObject *func = PyCFunction_NewEx ( &method_def->ext_meth_def, - new_reference_to( args ) + new_reference_to( args ), + m_module ); method_def->py_method = Object( func, true ); diff --git a/src/CXX/Python2/ExtensionOldType.hxx b/src/CXX/Python2/ExtensionOldType.hxx index cfd2fbe6a5..570e31f612 100644 --- a/src/CXX/Python2/ExtensionOldType.hxx +++ b/src/CXX/Python2/ExtensionOldType.hxx @@ -180,7 +180,7 @@ namespace Py self[0] = Object( this ); self[1] = Object( PyCObject_FromVoidPtr( method_def, do_not_dealloc ), true ); - PyObject *func = PyCFunction_New( &method_def->ext_meth_def, self.ptr() ); + PyObject *func = PyCFunction_NewEx( &method_def->ext_meth_def, self.ptr(), NULL ); return Object(func, true); } @@ -256,7 +256,7 @@ namespace Py return new_reference_to( result.ptr() ); } - catch( Exception & ) + catch( BaseException & ) { return 0; } @@ -293,7 +293,7 @@ namespace Py return new_reference_to( result.ptr() ); } - catch( Exception & ) + catch( BaseException & ) { return 0; } @@ -322,7 +322,7 @@ namespace Py return new_reference_to( result.ptr() ); } - catch( Exception & ) + catch( BaseException & ) { return 0; } diff --git a/src/CXX/Python3/Exception.hxx b/src/CXX/Python3/Exception.hxx index c337fc3018..95ecd37a1d 100644 --- a/src/CXX/Python3/Exception.hxx +++ b/src/CXX/Python3/Exception.hxx @@ -54,33 +54,16 @@ namespace Py class Object; - class PYCXX_EXPORT Exception + class PYCXX_EXPORT BaseException { public: - Exception( ExtensionExceptionType &exception, const std::string &reason ); - Exception( ExtensionExceptionType &exception, Object &reason ); + BaseException( ExtensionExceptionType &exception, const std::string &reason ); + BaseException( ExtensionExceptionType &exception, Object &reason ); + BaseException( PyObject *exception, Object &reason ); + BaseException( PyObject *exception, const std::string &reason ); + explicit BaseException(); - explicit Exception () - {} - - // This overloaded constructor will be removed in future PyCXX versions - //Exception (const std::string &reason) - //{ - // PyErr_SetString( Py::_Exc_RuntimeError(), reason.c_str() ); - //} - - Exception( PyObject *exception, const std::string &reason ) - { - PyErr_SetString( exception, reason.c_str() ); - } - - Exception( PyObject *exception, Object &reason ); - - void clear() // clear the error - // technically but not philosophically const - { - PyErr_Clear(); - } + void clear(); // clear the error // is the exception this specific exception 'exc' bool matches( ExtensionExceptionType &exc ); @@ -90,13 +73,27 @@ namespace Py typedef void (*throw_exception_func_t)( void ); void addPythonException( ExtensionExceptionType &py_exc_type, throw_exception_func_t throw_func ); - // Abstract - class PYCXX_EXPORT StandardError: public Exception +#if defined( PYCXX_6_2_COMPATIBILITY ) + class PYCXX_EXPORT Exception : public BaseException { - protected: - explicit StandardError() + public: + Exception( ExtensionExceptionType &exception, const std::string &reason ) + : BaseException( exception, reason ) + {} + + Exception( ExtensionExceptionType &exception, Object &reason ) + : BaseException( exception, reason ) + {} + + Exception( PyObject *exception, const std::string &reason ) + : BaseException( exception, reason ) + {} + + explicit Exception() + : BaseException() {} }; +#endif #define PYCXX_STANDARD_EXCEPTION( eclass, bclass ) \ class PYCXX_EXPORT eclass : public bclass \ @@ -114,6 +111,56 @@ namespace Py #include #undef PYCXX_STANDARD_EXCEPTION + +#define PYCXX_USER_EXCEPTION_STR_ARG( uclass ) \ +class PYCXX_EXPORT uclass : public Py::BaseException \ +{ \ +public: \ + uclass( const std::string &reason ) \ + : Py::BaseException( m_error, reason ) \ + { } \ + ~uclass() {} \ + static void init( Py::ExtensionModuleBase &module ) \ + { \ + m_error.init( module, #uclass ); \ + Py::addPythonException( m_error, throwFunc ); \ + Py::Dict d( module.moduleDictionary() ); \ + d[#uclass] = m_error; \ + } \ +private: \ + uclass() : Py::BaseException() {} \ + static void throwFunc() \ + { \ + throw uclass(); \ + } \ + static Py::ExtensionExceptionType m_error; \ +}; \ +Py::ExtensionExceptionType uclass::m_error; + +#define PYCXX_USER_EXCEPTION_NO_ARG( uclass ) \ +class PYCXX_EXPORT uclass : public Py::BaseException \ +{ \ +public: \ + uclass() \ + : Py::BaseException() \ + { } \ + ~uclass() {} \ + static void init( Py::ExtensionModuleBase &module ) \ + { \ + m_error.init( module, #uclass ); \ + Py::addPythonException( m_error, throwFunc ); \ + Py::Dict d( module.moduleDictionary() ); \ + d[#uclass] = m_error; \ + } \ +private: \ + static void throwFunc() \ + { \ + throw uclass(); \ + } \ + static Py::ExtensionExceptionType m_error; \ +}; \ +Py::ExtensionExceptionType uclass::m_error; + }// Py #endif diff --git a/src/CXX/Python3/ExtensionOldType.hxx b/src/CXX/Python3/ExtensionOldType.hxx index 5c65e8400e..96b68e66d7 100644 --- a/src/CXX/Python3/ExtensionOldType.hxx +++ b/src/CXX/Python3/ExtensionOldType.hxx @@ -270,7 +270,7 @@ namespace Py return new_reference_to( result.ptr() ); } - catch( Exception & ) + catch( BaseException & ) { return 0; } @@ -307,7 +307,7 @@ namespace Py return new_reference_to( result.ptr() ); } - catch( Exception & ) + catch( BaseException & ) { return 0; } @@ -335,7 +335,7 @@ namespace Py return new_reference_to( result.ptr() ); } - catch( Exception & ) + catch( BaseException & ) { return 0; } diff --git a/src/CXX/Python3/ExtensionType.hxx b/src/CXX/Python3/ExtensionType.hxx index ad7395f4ac..0bb036c6c8 100644 --- a/src/CXX/Python3/ExtensionType.hxx +++ b/src/CXX/Python3/ExtensionType.hxx @@ -52,7 +52,7 @@ Py::Object r( (self->NAME)() ); \ return Py::new_reference_to( r.ptr() ); \ } \ - catch( Py::Exception & ) \ + catch( Py::BaseException & ) \ { \ return 0; \ } \ @@ -68,7 +68,7 @@ Py::Object r( (self->NAME)( a ) ); \ return Py::new_reference_to( r.ptr() ); \ } \ - catch( Py::Exception & ) \ + catch( Py::BaseException & ) \ { \ return 0; \ } \ @@ -87,7 +87,7 @@ Py::Object r( (self->NAME)( a, k ) ); \ return Py::new_reference_to( r.ptr() ); \ } \ - catch( Py::Exception & ) \ + catch( Py::BaseException & ) \ { \ return 0; \ } \ @@ -119,6 +119,13 @@ namespace Py , m_methods_used( 0 ) , m_methods_size( METHOD_TABLE_SIZE_INCREMENT ) { + // add the sentinel marking the table end + PyMethodDef *p = &m_methods_table[ 0 ]; + + p->ml_name = NULL; + p->ml_meth = NULL; + p->ml_flags = 0; + p->ml_doc = NULL; } ~ExtensionClassMethodsTable() @@ -247,7 +254,7 @@ namespace Py PyObject *self = reinterpret_cast( o ); #ifdef PYCXX_DEBUG - std::cout << "extension_object_new() => self=0x" << std::hex << reinterpret_cast< unsigned int >( self ) << std::dec << std::endl; + std::cout << "extension_object_new() => self=0x" << std::hex << reinterpret_cast< unsigned long >( self ) << std::dec << std::endl; #endif return self; } @@ -263,26 +270,26 @@ namespace Py PythonClassInstance *self = reinterpret_cast( _self ); #ifdef PYCXX_DEBUG - std::cout << "extension_object_init( self=0x" << std::hex << reinterpret_cast< unsigned int >( self ) << std::dec << " )" << std::endl; - std::cout << " self->m_pycxx_object=0x" << std::hex << reinterpret_cast< unsigned int >( self->m_pycxx_object ) << std::dec << std::endl; + std::cout << "extension_object_init( self=0x" << std::hex << reinterpret_cast< unsigned long >( self ) << std::dec << " )" << std::endl; + std::cout << " self->m_pycxx_object=0x" << std::hex << reinterpret_cast< unsigned long >( self->m_pycxx_object ) << std::dec << std::endl; #endif if( self->m_pycxx_object == NULL ) { self->m_pycxx_object = new T( self, args, kwds ); #ifdef PYCXX_DEBUG - std::cout << " self->m_pycxx_object=0x" << std::hex << reinterpret_cast< unsigned int >( self->m_pycxx_object ) << std::dec << std::endl; + std::cout << " self->m_pycxx_object=0x" << std::hex << reinterpret_cast< unsigned long >( self->m_pycxx_object ) << std::dec << std::endl; #endif } else { #ifdef PYCXX_DEBUG - std::cout << " reinit - self->m_pycxx_object=0x" << std::hex << reinterpret_cast< unsigned int >( self->m_pycxx_object ) << std::dec << std::endl; + std::cout << " reinit - self->m_pycxx_object=0x" << std::hex << reinterpret_cast< unsigned long >( self->m_pycxx_object ) << std::dec << std::endl; #endif self->m_pycxx_object->reinit( args, kwds ); } } - catch( Exception & ) + catch( BaseException & ) { return -1; } @@ -293,8 +300,8 @@ namespace Py { PythonClassInstance *self = reinterpret_cast< PythonClassInstance * >( _self ); #ifdef PYCXX_DEBUG - std::cout << "extension_object_deallocator( self=0x" << std::hex << reinterpret_cast< unsigned int >( self ) << std::dec << " )" << std::endl; - std::cout << " self->m_pycxx_object=0x" << std::hex << reinterpret_cast< unsigned int >( self->m_pycxx_object ) << std::dec << std::endl; + std::cout << "extension_object_deallocator( self=0x" << std::hex << reinterpret_cast< unsigned long >( self ) << std::dec << " )" << std::endl; + std::cout << " self->m_pycxx_object=0x" << std::hex << reinterpret_cast< unsigned long >( self->m_pycxx_object ) << std::dec << std::endl; #endif delete self->m_pycxx_object; _self->ob_type->tp_free( _self ); @@ -313,8 +320,17 @@ namespace Py static bool check( PyObject *p ) { - // is p like me? - return p->ob_type == type_object(); + // is p a me or a derived me + switch( PyObject_IsInstance( p, reinterpret_cast( type_object() ) ) ) + { + default: + case -1: + throw Exception(); + case 0: + return false; + case 1: + return true; + } } static bool check( const Object &ob ) diff --git a/src/CXX/Python3/ExtensionTypeBase.hxx b/src/CXX/Python3/ExtensionTypeBase.hxx index a585ef2c55..ddf6d0ffc6 100644 --- a/src/CXX/Python3/ExtensionTypeBase.hxx +++ b/src/CXX/Python3/ExtensionTypeBase.hxx @@ -89,14 +89,20 @@ namespace Py virtual PyObject *iternext(); // Sequence methods - virtual int sequence_length(); + virtual PyCxx_ssize_t sequence_length(); virtual Object sequence_concat( const Object & ); virtual Object sequence_repeat( Py_ssize_t ); virtual Object sequence_item( Py_ssize_t ); + virtual int sequence_ass_item( Py_ssize_t, const Object & ); + virtual Object sequence_inplace_concat( const Object & ); + virtual Object sequence_inplace_repeat( Py_ssize_t ); + + virtual int sequence_contains( const Object & ); + // Mapping - virtual int mapping_length(); + virtual PyCxx_ssize_t mapping_length(); virtual Object mapping_subscript( const Object & ); virtual int mapping_ass_subscript( const Object &, const Object & ); diff --git a/src/CXX/Python3/Extensions.hxx b/src/CXX/Python3/Extensions.hxx index 0a181d9f07..5fcc3e3c1d 100644 --- a/src/CXX/Python3/Extensions.hxx +++ b/src/CXX/Python3/Extensions.hxx @@ -104,7 +104,7 @@ namespace Py extern "C" typedef PyObject *(*method_keyword_call_handler_t)( PyObject *_self, PyObject *_args, PyObject *_dict ); template - class MethodDefExt : public PyMethodDef + class MethodDefExt { public: typedef Object (T::*method_noargs_function_t)(); diff --git a/src/CXX/Python3/IndirectPythonInterface.hxx b/src/CXX/Python3/IndirectPythonInterface.hxx index 9f4e899b44..8f3b198e6a 100644 --- a/src/CXX/Python3/IndirectPythonInterface.hxx +++ b/src/CXX/Python3/IndirectPythonInterface.hxx @@ -49,40 +49,12 @@ bool InitialisePythonIndirectInterface(); // Wrap Exception variables as function calls // PYCXX_EXPORT PyObject * _Exc_Exception(); -PYCXX_EXPORT PyObject * _Exc_StandardError(); -PYCXX_EXPORT PyObject * _Exc_ArithmeticError(); -PYCXX_EXPORT PyObject * _Exc_LookupError(); -PYCXX_EXPORT PyObject * _Exc_AssertionError(); -PYCXX_EXPORT PyObject * _Exc_AttributeError(); -PYCXX_EXPORT PyObject * _Exc_EOFError(); -PYCXX_EXPORT PyObject * _Exc_FloatingPointError(); -PYCXX_EXPORT PyObject * _Exc_EnvironmentError(); -PYCXX_EXPORT PyObject * _Exc_IOError(); -PYCXX_EXPORT PyObject * _Exc_OSError(); -PYCXX_EXPORT PyObject * _Exc_ImportError(); -PYCXX_EXPORT PyObject * _Exc_IndexError(); -PYCXX_EXPORT PyObject * _Exc_KeyError(); -PYCXX_EXPORT PyObject * _Exc_KeyboardInterrupt(); -PYCXX_EXPORT PyObject * _Exc_MemoryError(); -PYCXX_EXPORT PyObject * _Exc_NameError(); -PYCXX_EXPORT PyObject * _Exc_OverflowError(); -PYCXX_EXPORT PyObject * _Exc_RuntimeError(); -PYCXX_EXPORT PyObject * _Exc_NotImplementedError(); -PYCXX_EXPORT PyObject * _Exc_SyntaxError(); -PYCXX_EXPORT PyObject * _Exc_SystemError(); -PYCXX_EXPORT PyObject * _Exc_SystemExit(); -PYCXX_EXPORT PyObject * _Exc_TypeError(); -PYCXX_EXPORT PyObject * _Exc_ValueError(); -PYCXX_EXPORT PyObject * _Exc_ZeroDivisionError(); -#ifdef MS_WINDOWS -PYCXX_EXPORT PyObject * _Exc_WindowsError(); -#endif +#define PYCXX_STANDARD_EXCEPTION( eclass, bclass ) \ + PYCXX_EXPORT PyObject * _Exc_##eclass(); -PYCXX_EXPORT PyObject * _Exc_IndentationError(); -PYCXX_EXPORT PyObject * _Exc_TabError(); -PYCXX_EXPORT PyObject * _Exc_UnboundLocalError(); -PYCXX_EXPORT PyObject * _Exc_UnicodeError(); +#include "CXX/Python3/cxx_standard_exceptions.hxx" +#undef PYCXX_STANDARD_EXCEPTION // // Wrap Object variables as function calls diff --git a/src/CXX/Python3/Objects.hxx b/src/CXX/Python3/Objects.hxx index aae03fc00a..1126115e64 100644 --- a/src/CXX/Python3/Objects.hxx +++ b/src/CXX/Python3/Objects.hxx @@ -375,7 +375,7 @@ namespace Py { if( PyObject_SetAttrString( p, const_cast( s.c_str() ), *value ) == -1 ) { - throw AttributeError( "setAttr failed." ); + ifPyErrorThrowCxxException(); } } @@ -383,7 +383,7 @@ namespace Py { if( PyObject_DelAttrString( p, const_cast( s.c_str() ) ) == -1 ) { - throw AttributeError( "delAttr failed." ); + ifPyErrorThrowCxxException(); } } @@ -394,8 +394,7 @@ namespace Py { if( PyObject_DelItem( p, *key ) == -1 ) { - // failed to link on Windows? - throw KeyError( "delItem failed." ); + ifPyErrorThrowCxxException(); } } // Equality and comparison use PyObject_Compare @@ -1378,7 +1377,7 @@ namespace Py { if( PySequence_SetItem( ptr(), i, *ob ) == -1 ) { - throw Exception(); + ifPyErrorThrowCxxException(); } } @@ -1442,7 +1441,7 @@ namespace Py protected: friend class SeqBase; SeqBase *seq; - size_type count; + sequence_index_type count; public: ~iterator() @@ -1453,7 +1452,7 @@ namespace Py , count( 0 ) {} - iterator( SeqBase *s, size_type where ) + iterator( SeqBase *s, Py_ssize_t where ) : seq( s ) , count( where ) {} @@ -1513,29 +1512,29 @@ namespace Py return *this; } - iterator operator+( int n ) const + iterator operator+( sequence_index_type n ) const { return iterator( seq, count + n ); } - iterator operator-( int n ) const + iterator operator-( sequence_index_type n ) const { return iterator( seq, count - n ); } - iterator &operator+=( int n ) + iterator &operator+=( sequence_index_type n ) { count = count + n; return *this; } - iterator &operator-=( int n ) + iterator &operator-=( sequence_index_type n ) { count = count - n; return *this; } - int operator-( const iterator &other ) const + sequence_index_type operator-( const iterator &other ) const { if( seq->ptr() != other.seq->ptr() ) { @@ -1612,7 +1611,7 @@ namespace Py , count( 0 ) {} - const_iterator( const SeqBase *s, size_type where ) + const_iterator( const SeqBase *s, sequence_index_type where ) : seq( s ) , count( where ) {} @@ -1642,7 +1641,7 @@ namespace Py return *this; } - const_iterator operator+( int n ) const + const_iterator operator+( sequence_index_type n ) const { return const_iterator( seq, count + n ); } @@ -1677,18 +1676,18 @@ namespace Py return count >= other.count; } - const_iterator operator-( int n ) + const_iterator operator-( sequence_index_type n ) { return const_iterator( seq, count - n ); } - const_iterator &operator+=( int n ) + const_iterator &operator+=( sequence_index_type n ) { count = count + n; return *this; } - const_iterator &operator-=( int n ) + const_iterator &operator-=( sequence_index_type n ) { count = count - n; return *this; @@ -1789,6 +1788,7 @@ namespace Py virtual bool accepts( PyObject *pyob ) const { return pyob != NULL +// QQQ _Unicode_Check for a Byte??? && Py::_Unicode_Check( pyob ) && PySequence_Length( pyob ) == 1; } @@ -2176,7 +2176,7 @@ namespace Py // note PyTuple_SetItem is a thief... if( PyTuple_SetItem( ptr(), offset, new_reference_to( ob ) ) == -1 ) { - throw Exception(); + ifPyErrorThrowCxxException(); } } @@ -2202,7 +2202,7 @@ namespace Py { if( PyTuple_SetItem( ptr(), i, new_reference_to( Py::_None() ) ) == -1 ) { - throw Exception(); + ifPyErrorThrowCxxException(); } } } @@ -2218,7 +2218,7 @@ namespace Py { if( PyTuple_SetItem( ptr(), i, new_reference_to( s[i] ) ) == -1 ) { - throw Exception(); + ifPyErrorThrowCxxException(); } } } @@ -2385,7 +2385,7 @@ namespace Py { if( PyList_SetItem( ptr(), i, new_reference_to( Py::_None() ) ) == -1 ) { - throw Exception(); + ifPyErrorThrowCxxException(); } } } @@ -2401,7 +2401,7 @@ namespace Py { if( PyList_SetItem( ptr(), i, new_reference_to( s[i] ) ) == -1 ) { - throw Exception(); + ifPyErrorThrowCxxException(); } } } @@ -2439,7 +2439,7 @@ namespace Py { if( PyList_SetSlice( ptr(), i, j, *v ) == -1 ) { - throw Exception(); + ifPyErrorThrowCxxException(); } } @@ -2447,7 +2447,7 @@ namespace Py { if( PyList_Append( ptr(), *ob ) == -1 ) { - throw Exception(); + ifPyErrorThrowCxxException(); } } @@ -2460,7 +2460,7 @@ namespace Py { if( PyList_Insert( ptr(), i, *ob ) == -1 ) { - throw Exception(); + ifPyErrorThrowCxxException(); } } @@ -2468,7 +2468,7 @@ namespace Py { if( PyList_Sort( ptr() ) == -1 ) { - throw Exception(); + ifPyErrorThrowCxxException(); } } @@ -2476,7 +2476,7 @@ namespace Py { if( PyList_Reverse( ptr() ) == -1 ) { - throw Exception(); + ifPyErrorThrowCxxException(); } } }; @@ -2674,7 +2674,7 @@ namespace Py // If you assume that Python mapping is a hash_map... // hash_map::value_type is not assignable, but //( *it ).second = data must be a valid expression - typedef Py_ssize_t size_type; + typedef PyCxx_ssize_t size_type; typedef Object key_type; typedef mapref data_type; typedef std::pair< const T, T > value_type; @@ -2726,7 +2726,7 @@ namespace Py } } - virtual size_type size() const + virtual Py_ssize_t size() const { return PyMapping_Length( ptr() ); } @@ -2757,7 +2757,7 @@ namespace Py return mapref( *this, key ); } - size_type length() const + Py_ssize_t length() const { return PyMapping_Length( ptr() ); } @@ -2786,7 +2786,7 @@ namespace Py { if( PyMapping_SetItemString( ptr(), const_cast( s ), *ob ) == -1 ) { - throw Exception(); + ifPyErrorThrowCxxException(); } } @@ -2794,7 +2794,7 @@ namespace Py { if( PyMapping_SetItemString( ptr(), const_cast( s.c_str() ), *ob ) == -1 ) { - throw Exception(); + ifPyErrorThrowCxxException(); } } @@ -2802,7 +2802,7 @@ namespace Py { if( PyObject_SetItem( ptr(), s.ptr(), ob.ptr() ) == -1 ) { - throw Exception(); + ifPyErrorThrowCxxException(); } } @@ -2810,7 +2810,7 @@ namespace Py { if( PyMapping_DelItemString( ptr(), const_cast( s.c_str() ) ) == -1 ) { - throw Exception(); + ifPyErrorThrowCxxException(); } } @@ -2818,7 +2818,7 @@ namespace Py { if( PyMapping_DelItem( ptr(), *s ) == -1 ) { - throw Exception(); + ifPyErrorThrowCxxException(); } } @@ -2853,7 +2853,7 @@ namespace Py // MapBase *map; List keys; // for iterating over the map - size_type pos; // index into the keys + sequence_index_type pos; // index into the keys public: ~iterator() @@ -2970,7 +2970,7 @@ namespace Py friend class MapBase; const MapBase *map; List keys; // for iterating over the map - size_type pos; // index into the keys + Py_ssize_t pos; // index into the keys public: ~const_iterator() @@ -2982,7 +2982,7 @@ namespace Py , pos() {} - const_iterator( const MapBase *m, List k, size_type p ) + const_iterator( const MapBase *m, List k, Py_ssize_t p ) : map( m ) , keys( k ) , pos( p ) @@ -3449,7 +3449,7 @@ namespace Py //------------------------------------------------------------ // type - inline Object type( const Exception &) // return the type of the error + inline Object type( const BaseException & ) // return the type of the error { PyObject *ptype, *pvalue, *ptrace; PyErr_Fetch( &ptype, &pvalue, &ptrace ); @@ -3460,7 +3460,7 @@ namespace Py return result; } - inline Object value( const Exception &) // return the value of the error + inline Object value( const BaseException & ) // return the value of the error { PyObject *ptype, *pvalue, *ptrace; PyErr_Fetch( &ptype, &pvalue, &ptrace ); @@ -3471,7 +3471,7 @@ namespace Py return result; } - inline Object trace( const Exception &) // return the traceback of the error + inline Object trace( const BaseException & ) // return the traceback of the error { PyObject *ptype, *pvalue, *ptrace; PyErr_Fetch( &ptype, &pvalue, &ptrace ); diff --git a/src/CXX/Python3/PythonType.hxx b/src/CXX/Python3/PythonType.hxx index 362d09256b..1596f5aeb1 100644 --- a/src/CXX/Python3/PythonType.hxx +++ b/src/CXX/Python3/PythonType.hxx @@ -72,12 +72,94 @@ namespace Py PythonType &supportStr( void ); PythonType &supportHash( void ); PythonType &supportCall( void ); - PythonType &supportIter( void ); - PythonType &supportSequenceType( void ); - PythonType &supportMappingType( void ); - PythonType &supportNumberType( void ); - PythonType &supportBufferType( void ); +#define B( n ) (1<<(n)) + enum { + support_iter_iter = B(0), + support_iter_iternext = B(1) + }; + PythonType &supportIter( int methods_to_support= + support_iter_iter | + support_iter_iternext ); + + enum { + support_sequence_length = B(0), + support_sequence_repeat = B(1), + support_sequence_item = B(2), + support_sequence_slice = B(3), + support_sequence_concat = B(4), + support_sequence_ass_item = B(5), + support_sequence_ass_slice = B(6), + support_sequence_inplace_concat = B(7), + support_sequence_inplace_repeat = B(8), + support_sequence_contains = B(9) + }; + PythonType &supportSequenceType( int methods_to_support= + support_sequence_length | + support_sequence_repeat | + support_sequence_item | + support_sequence_slice | + support_sequence_concat + ); + + enum { + support_mapping_length = B(0), + support_mapping_subscript = B(1), + support_mapping_ass_subscript = B(2) + }; + PythonType &supportMappingType( int methods_to_support= + support_mapping_length | + support_mapping_subscript + ); + + enum { + support_number_add = B(0), + support_number_subtract = B(1), + support_number_multiply = B(2), + support_number_remainder = B(3), + support_number_divmod = B(4), + support_number_power = B(5), + support_number_negative = B(6), + support_number_positive = B(7), + support_number_absolute = B(8), + support_number_invert = B(9), + support_number_lshift = B(10), + support_number_rshift = B(11), + support_number_and = B(12), + support_number_xor = B(13), + support_number_or = B(14), + support_number_int = B(15), + support_number_float= B(16) + }; + PythonType &supportNumberType( int methods_to_support= + support_number_add | + support_number_subtract | + support_number_multiply | + support_number_remainder | + support_number_divmod | + support_number_power | + support_number_negative | + support_number_positive | + support_number_absolute | + support_number_invert | + support_number_lshift | + support_number_rshift | + support_number_and | + support_number_xor | + support_number_or | + support_number_int | + support_number_float + ); + + enum { + support_buffer_getbuffer = B(0), + support_buffer_releasebuffer = B(1) + }; + PythonType &supportBufferType( int methods_to_support= + support_buffer_getbuffer | + support_buffer_releasebuffer + ); +#undef B PythonType &set_tp_dealloc( void (*tp_dealloc)( PyObject * ) ); PythonType &set_tp_init( int (*tp_init)( PyObject *self, PyObject *args, PyObject *kwds ) ); diff --git a/src/CXX/Python3/cxx_extensions.cxx b/src/CXX/Python3/cxx_extensions.cxx index 34e57fb8ba..e55593a5ac 100644 --- a/src/CXX/Python3/cxx_extensions.cxx +++ b/src/CXX/Python3/cxx_extensions.cxx @@ -50,7 +50,7 @@ void bpt( void ) void printRefCount( PyObject *obj ) { - std::cout << "RefCount of 0x" << std::hex << reinterpret_cast< unsigned int >( obj ) << std::dec << " is " << Py_REFCNT( obj ) << std::endl; + std::cout << "RefCount of 0x" << std::hex << reinterpret_cast< unsigned long >( obj ) << std::dec << " is " << Py_REFCNT( obj ) << std::endl; } #endif @@ -201,12 +201,12 @@ void ExtensionModuleBase::initialize( const char *module_doc ) m_module = PyModule_Create( &m_module_def ); } -Py::Module ExtensionModuleBase::module( void ) const +Module ExtensionModuleBase::module( void ) const { return Module( m_module ); } -Py::Dict ExtensionModuleBase::moduleDictionary( void ) const +Dict ExtensionModuleBase::moduleDictionary( void ) const { return module().getDict(); } @@ -250,6 +250,11 @@ extern "C" static PyObject *sequence_item_handler( PyObject *, Py_ssize_t ); static int sequence_ass_item_handler( PyObject *, Py_ssize_t, PyObject * ); + static PyObject *sequence_inplace_concat_handler( PyObject *, PyObject * ); + static PyObject *sequence_inplace_repeat_handler( PyObject *, Py_ssize_t ); + + static int sequence_contains_handler( PyObject *, PyObject * ); + // Mapping static Py_ssize_t mapping_length_handler( PyObject * ); static PyObject *mapping_subscript_handler( PyObject *, PyObject * ); @@ -289,78 +294,171 @@ bool PythonType::readyType() return PyType_Ready( table ) >= 0; } -PythonType &PythonType::supportSequenceType() +PythonType &PythonType::supportSequenceType( int methods_to_support ) { if( !sequence_table ) { sequence_table = new PySequenceMethods; memset( sequence_table, 0, sizeof( PySequenceMethods ) ); // ensure new fields are 0 table->tp_as_sequence = sequence_table; - sequence_table->sq_length = sequence_length_handler; - sequence_table->sq_concat = sequence_concat_handler; - sequence_table->sq_repeat = sequence_repeat_handler; - sequence_table->sq_item = sequence_item_handler; - - sequence_table->sq_ass_item = sequence_ass_item_handler; // BAS setup separately? - // QQQ sq_inplace_concat - // QQQ sq_inplace_repeat + if( methods_to_support&support_sequence_length ) + { + sequence_table->sq_length = sequence_length_handler; + } + if( methods_to_support&support_sequence_concat ) + { + sequence_table->sq_concat = sequence_concat_handler; + } + if( methods_to_support&support_sequence_repeat ) + { + sequence_table->sq_repeat = sequence_repeat_handler; + } + if( methods_to_support&support_sequence_item ) + { + sequence_table->sq_item = sequence_item_handler; + } + if( methods_to_support&support_sequence_ass_item ) + { + sequence_table->sq_ass_item = sequence_ass_item_handler; + } + if( methods_to_support&support_sequence_inplace_concat ) + { + sequence_table->sq_inplace_concat = sequence_inplace_concat_handler; + } + if( methods_to_support&support_sequence_inplace_repeat ) + { + sequence_table->sq_inplace_repeat = sequence_inplace_repeat_handler; + } + if( methods_to_support&support_sequence_contains ) + { + sequence_table->sq_contains = sequence_contains_handler; + } } return *this; } -PythonType &PythonType::supportMappingType() +PythonType &PythonType::supportMappingType( int methods_to_support ) { if( !mapping_table ) { mapping_table = new PyMappingMethods; memset( mapping_table, 0, sizeof( PyMappingMethods ) ); // ensure new fields are 0 table->tp_as_mapping = mapping_table; - mapping_table->mp_length = mapping_length_handler; - mapping_table->mp_subscript = mapping_subscript_handler; - mapping_table->mp_ass_subscript = mapping_ass_subscript_handler; // BAS setup separately? + + if( methods_to_support&support_mapping_length ) + { + mapping_table->mp_length = mapping_length_handler; + } + if( methods_to_support&support_mapping_subscript ) + { + mapping_table->mp_subscript = mapping_subscript_handler; + } + if( methods_to_support&support_mapping_ass_subscript ) + { + mapping_table->mp_ass_subscript = mapping_ass_subscript_handler; + } } return *this; } -PythonType &PythonType::supportNumberType() +PythonType &PythonType::supportNumberType( int methods_to_support ) { if( !number_table ) { number_table = new PyNumberMethods; memset( number_table, 0, sizeof( PyNumberMethods ) ); // ensure new fields are 0 table->tp_as_number = number_table; - number_table->nb_add = number_add_handler; - number_table->nb_subtract = number_subtract_handler; - number_table->nb_multiply = number_multiply_handler; - number_table->nb_remainder = number_remainder_handler; - number_table->nb_divmod = number_divmod_handler; - number_table->nb_power = number_power_handler; - number_table->nb_negative = number_negative_handler; - number_table->nb_positive = number_positive_handler; - number_table->nb_absolute = number_absolute_handler; - number_table->nb_invert = number_invert_handler; - number_table->nb_lshift = number_lshift_handler; - number_table->nb_rshift = number_rshift_handler; - number_table->nb_and = number_and_handler; - number_table->nb_xor = number_xor_handler; - number_table->nb_or = number_or_handler; - number_table->nb_int = number_int_handler; - number_table->nb_float = number_float_handler; + + if( methods_to_support&support_number_add ) + { + number_table->nb_add = number_add_handler; + } + if( methods_to_support&support_number_subtract ) + { + number_table->nb_subtract = number_subtract_handler; + } + if( methods_to_support&support_number_multiply ) + { + number_table->nb_multiply = number_multiply_handler; + } + if( methods_to_support&support_number_remainder ) + { + number_table->nb_remainder = number_remainder_handler; + } + if( methods_to_support&support_number_divmod ) + { + number_table->nb_divmod = number_divmod_handler; + } + if( methods_to_support&support_number_power ) + { + number_table->nb_power = number_power_handler; + } + if( methods_to_support&support_number_negative ) + { + number_table->nb_negative = number_negative_handler; + } + if( methods_to_support&support_number_positive ) + { + number_table->nb_positive = number_positive_handler; + } + if( methods_to_support&support_number_absolute ) + { + number_table->nb_absolute = number_absolute_handler; + } + if( methods_to_support&support_number_invert ) + { + number_table->nb_invert = number_invert_handler; + } + if( methods_to_support&support_number_lshift ) + { + number_table->nb_lshift = number_lshift_handler; + } + if( methods_to_support&support_number_rshift ) + { + number_table->nb_rshift = number_rshift_handler; + } + if( methods_to_support&support_number_and ) + { + number_table->nb_and = number_and_handler; + } + if( methods_to_support&support_number_xor ) + { + number_table->nb_xor = number_xor_handler; + } + if( methods_to_support&support_number_or ) + { + number_table->nb_or = number_or_handler; + } + if( methods_to_support&support_number_int ) + { + number_table->nb_int = number_int_handler; + } + if( methods_to_support&support_number_float ) + { + number_table->nb_float = number_float_handler; + } // QQQ lots of new methods to add } return *this; } -PythonType &PythonType::supportBufferType() +PythonType &PythonType::supportBufferType( int methods_to_support ) { if( !buffer_table ) { buffer_table = new PyBufferProcs; memset( buffer_table, 0, sizeof( PyBufferProcs ) ); // ensure new fields are 0 table->tp_as_buffer = buffer_table; - buffer_table->bf_getbuffer = buffer_get_handler; - buffer_table->bf_releasebuffer = buffer_release_handler; + + if( methods_to_support&support_buffer_getbuffer ) + { + buffer_table->bf_getbuffer = buffer_get_handler; + } + if( methods_to_support&support_buffer_releasebuffer ) + { + buffer_table->bf_releasebuffer = buffer_release_handler; + } } return *this; } @@ -603,10 +701,16 @@ PythonType &PythonType::supportCall() return *this; } -PythonType &PythonType::supportIter() +PythonType &PythonType::supportIter( int methods_to_support ) { - table->tp_iter = iter_handler; - table->tp_iternext = iternext_handler; + if( methods_to_support&support_iter_iter ) + { + table->tp_iter = iter_handler; + } + if( methods_to_support&support_iter_iternext ) + { + table->tp_iternext = iternext_handler; + } return *this; } @@ -636,7 +740,7 @@ extern "C" int print_handler( PyObject *self, FILE *fp, int flags ) PythonExtensionBase *p = getPythonExtensionBase( self ); return p->print( fp, flags ); } - catch( Py::Exception & ) + catch( BaseException & ) { return -1; // indicate error } @@ -650,7 +754,7 @@ extern "C" PyObject *getattr_handler( PyObject *self, char *name ) PythonExtensionBase *p = getPythonExtensionBase( self ); return new_reference_to( p->getattr( name ) ); } - catch( Py::Exception & ) + catch( BaseException & ) { return NULL; // indicate error } @@ -661,9 +765,9 @@ extern "C" int setattr_handler( PyObject *self, char *name, PyObject *value ) try { PythonExtensionBase *p = getPythonExtensionBase( self ); - return p->setattr( name, Py::Object( value ) ); + return p->setattr( name, Object( value ) ); } - catch( Py::Exception & ) + catch( BaseException & ) { return -1; // indicate error } @@ -674,9 +778,9 @@ extern "C" PyObject *getattro_handler( PyObject *self, PyObject *name ) try { PythonExtensionBase *p = getPythonExtensionBase( self ); - return new_reference_to( p->getattro( Py::String( name ) ) ); + return new_reference_to( p->getattro( String( name ) ) ); } - catch( Py::Exception & ) + catch( BaseException & ) { return NULL; // indicate error } @@ -687,9 +791,9 @@ extern "C" int setattro_handler( PyObject *self, PyObject *name, PyObject *value try { PythonExtensionBase *p = getPythonExtensionBase( self ); - return p->setattro( Py::String( name ), Py::Object( value ) ); + return p->setattro( String( name ), Object( value ) ); } - catch( Py::Exception & ) + catch( BaseException & ) { return -1; // indicate error } @@ -700,9 +804,9 @@ extern "C" PyObject *rich_compare_handler( PyObject *self, PyObject *other, int try { PythonExtensionBase *p = getPythonExtensionBase( self ); - return new_reference_to( p->rich_compare( Py::Object( other ), op ) ); + return new_reference_to( p->rich_compare( Object( other ), op ) ); } - catch( Py::Exception & ) + catch( BaseException & ) { return NULL; // indicate error } @@ -715,7 +819,7 @@ extern "C" PyObject *repr_handler( PyObject *self ) PythonExtensionBase *p = getPythonExtensionBase( self ); return new_reference_to( p->repr() ); } - catch( Py::Exception & ) + catch( BaseException & ) { return NULL; // indicate error } @@ -728,7 +832,7 @@ extern "C" PyObject *str_handler( PyObject *self ) PythonExtensionBase *p = getPythonExtensionBase( self ); return new_reference_to( p->str() ); } - catch( Py::Exception & ) + catch( BaseException & ) { return NULL; // indicate error } @@ -741,7 +845,7 @@ extern "C" Py_hash_t hash_handler( PyObject *self ) PythonExtensionBase *p = getPythonExtensionBase( self ); return p->hash(); } - catch( Py::Exception & ) + catch( BaseException & ) { return -1; // indicate error } @@ -753,11 +857,11 @@ extern "C" PyObject *call_handler( PyObject *self, PyObject *args, PyObject *kw { PythonExtensionBase *p = getPythonExtensionBase( self ); if( kw != NULL ) - return new_reference_to( p->call( Py::Object( args ), Py::Object( kw ) ) ); + return new_reference_to( p->call( Object( args ), Object( kw ) ) ); else - return new_reference_to( p->call( Py::Object( args ), Py::Object() ) ); + return new_reference_to( p->call( Object( args ), Object() ) ); } - catch( Py::Exception & ) + catch( BaseException & ) { return NULL; // indicate error } @@ -770,7 +874,7 @@ extern "C" PyObject *iter_handler( PyObject *self ) PythonExtensionBase *p = getPythonExtensionBase( self ); return new_reference_to( p->iter() ); } - catch( Py::Exception & ) + catch( BaseException & ) { return NULL; // indicate error } @@ -783,7 +887,7 @@ extern "C" PyObject *iternext_handler( PyObject *self ) PythonExtensionBase *p = getPythonExtensionBase( self ); return p->iternext(); // might be a NULL ptr on end of iteration } - catch( Py::Exception & ) + catch( BaseException & ) { return NULL; // indicate error } @@ -798,7 +902,7 @@ extern "C" Py_ssize_t sequence_length_handler( PyObject *self ) PythonExtensionBase *p = getPythonExtensionBase( self ); return p->sequence_length(); } - catch( Py::Exception & ) + catch( BaseException & ) { return -1; // indicate error } @@ -809,9 +913,9 @@ extern "C" PyObject *sequence_concat_handler( PyObject *self, PyObject *other ) try { PythonExtensionBase *p = getPythonExtensionBase( self ); - return new_reference_to( p->sequence_concat( Py::Object( other ) ) ); + return new_reference_to( p->sequence_concat( Object( other ) ) ); } - catch( Py::Exception & ) + catch( BaseException & ) { return NULL; // indicate error } @@ -824,7 +928,7 @@ extern "C" PyObject *sequence_repeat_handler( PyObject *self, Py_ssize_t count ) PythonExtensionBase *p = getPythonExtensionBase( self ); return new_reference_to( p->sequence_repeat( count ) ); } - catch( Py::Exception & ) + catch( BaseException & ) { return NULL; // indicate error } @@ -837,7 +941,7 @@ extern "C" PyObject *sequence_item_handler( PyObject *self, Py_ssize_t index ) PythonExtensionBase *p = getPythonExtensionBase( self ); return new_reference_to( p->sequence_item( index ) ); } - catch( Py::Exception & ) + catch( BaseException & ) { return NULL; // indicate error } @@ -848,9 +952,48 @@ extern "C" int sequence_ass_item_handler( PyObject *self, Py_ssize_t index, PyOb try { PythonExtensionBase *p = getPythonExtensionBase( self ); - return p->sequence_ass_item( index, Py::Object( value ) ); + return p->sequence_ass_item( index, Object( value ) ); } - catch( Py::Exception & ) + catch( BaseException & ) + { + return -1; // indicate error + } +} + +extern "C" PyObject *sequence_inplace_concat_handler( PyObject *self, PyObject *o2 ) +{ + try + { + PythonExtensionBase *p = getPythonExtensionBase( self ); + return new_reference_to( p->sequence_inplace_concat( Object( o2 ) ) ); + } + catch( BaseException & ) + { + return NULL; // indicate error + } +} + +extern "C" PyObject *sequence_inplace_repeat_handler( PyObject *self, Py_ssize_t count ) +{ + try + { + PythonExtensionBase *p = getPythonExtensionBase( self ); + return new_reference_to( p->sequence_inplace_repeat( count ) ); + } + catch( BaseException & ) + { + return NULL; // indicate error + } +} + +extern "C" int sequence_contains_handler( PyObject *self, PyObject *value ) +{ + try + { + PythonExtensionBase *p = getPythonExtensionBase( self ); + return p->sequence_contains( Object( value ) ); + } + catch( BaseException & ) { return -1; // indicate error } @@ -864,7 +1007,7 @@ extern "C" Py_ssize_t mapping_length_handler( PyObject *self ) PythonExtensionBase *p = getPythonExtensionBase( self ); return p->mapping_length(); } - catch( Py::Exception & ) + catch( BaseException & ) { return -1; // indicate error } @@ -875,9 +1018,9 @@ extern "C" PyObject *mapping_subscript_handler( PyObject *self, PyObject *key ) try { PythonExtensionBase *p = getPythonExtensionBase( self ); - return new_reference_to( p->mapping_subscript( Py::Object( key ) ) ); + return new_reference_to( p->mapping_subscript( Object( key ) ) ); } - catch( Py::Exception & ) + catch( BaseException & ) { return NULL; // indicate error } @@ -888,9 +1031,9 @@ extern "C" int mapping_ass_subscript_handler( PyObject *self, PyObject *key, PyO try { PythonExtensionBase *p = getPythonExtensionBase( self ); - return p->mapping_ass_subscript( Py::Object( key ), Py::Object( value ) ); + return p->mapping_ass_subscript( Object( key ), Object( value ) ); } - catch( Py::Exception & ) + catch( BaseException & ) { return -1; // indicate error } @@ -904,7 +1047,7 @@ extern "C" PyObject *number_negative_handler( PyObject *self ) PythonExtensionBase *p = getPythonExtensionBase( self ); return new_reference_to( p->number_negative() ); } - catch( Py::Exception & ) + catch( BaseException & ) { return NULL; // indicate error } @@ -917,7 +1060,7 @@ extern "C" PyObject *number_positive_handler( PyObject *self ) PythonExtensionBase *p = getPythonExtensionBase( self ); return new_reference_to( p->number_positive() ); } - catch( Py::Exception & ) + catch( BaseException & ) { return NULL; // indicate error } @@ -930,7 +1073,7 @@ extern "C" PyObject *number_absolute_handler( PyObject *self ) PythonExtensionBase *p = getPythonExtensionBase( self ); return new_reference_to( p->number_absolute() ); } - catch( Py::Exception & ) + catch( BaseException & ) { return NULL; // indicate error } @@ -943,7 +1086,7 @@ extern "C" PyObject *number_invert_handler( PyObject *self ) PythonExtensionBase *p = getPythonExtensionBase( self ); return new_reference_to( p->number_invert() ); } - catch( Py::Exception & ) + catch( BaseException & ) { return NULL; // indicate error } @@ -956,7 +1099,7 @@ extern "C" PyObject *number_int_handler( PyObject *self ) PythonExtensionBase *p = getPythonExtensionBase( self ); return new_reference_to( p->number_int() ); } - catch( Py::Exception & ) + catch( BaseException & ) { return NULL; // indicate error } @@ -969,7 +1112,7 @@ extern "C" PyObject *number_float_handler( PyObject *self ) PythonExtensionBase *p = getPythonExtensionBase( self ); return new_reference_to( p->number_float() ); } - catch( Py::Exception & ) + catch( BaseException & ) { return NULL; // indicate error } @@ -980,9 +1123,9 @@ extern "C" PyObject *number_add_handler( PyObject *self, PyObject *other ) try { PythonExtensionBase *p = getPythonExtensionBase( self ); - return new_reference_to( p->number_add( Py::Object( other ) ) ); + return new_reference_to( p->number_add( Object( other ) ) ); } - catch( Py::Exception & ) + catch( BaseException & ) { return NULL; // indicate error } @@ -993,9 +1136,9 @@ extern "C" PyObject *number_subtract_handler( PyObject *self, PyObject *other ) try { PythonExtensionBase *p = getPythonExtensionBase( self ); - return new_reference_to( p->number_subtract( Py::Object( other ) ) ); + return new_reference_to( p->number_subtract( Object( other ) ) ); } - catch( Py::Exception & ) + catch( BaseException & ) { return NULL; // indicate error } @@ -1006,9 +1149,9 @@ extern "C" PyObject *number_multiply_handler( PyObject *self, PyObject *other ) try { PythonExtensionBase *p = getPythonExtensionBase( self ); - return new_reference_to( p->number_multiply( Py::Object( other ) ) ); + return new_reference_to( p->number_multiply( Object( other ) ) ); } - catch( Py::Exception & ) + catch( BaseException & ) { return NULL; // indicate error } @@ -1019,9 +1162,9 @@ extern "C" PyObject *number_remainder_handler( PyObject *self, PyObject *other ) try { PythonExtensionBase *p = getPythonExtensionBase( self ); - return new_reference_to( p->number_remainder( Py::Object( other ) ) ); + return new_reference_to( p->number_remainder( Object( other ) ) ); } - catch( Py::Exception & ) + catch( BaseException & ) { return NULL; // indicate error } @@ -1032,9 +1175,9 @@ extern "C" PyObject *number_divmod_handler( PyObject *self, PyObject *other ) try { PythonExtensionBase *p = getPythonExtensionBase( self ); - return new_reference_to( p->number_divmod( Py::Object( other ) ) ); + return new_reference_to( p->number_divmod( Object( other ) ) ); } - catch( Py::Exception & ) + catch( BaseException & ) { return NULL; // indicate error } @@ -1045,9 +1188,9 @@ extern "C" PyObject *number_lshift_handler( PyObject *self, PyObject *other ) try { PythonExtensionBase *p = getPythonExtensionBase( self ); - return new_reference_to( p->number_lshift( Py::Object( other ) ) ); + return new_reference_to( p->number_lshift( Object( other ) ) ); } - catch( Py::Exception & ) + catch( BaseException & ) { return NULL; // indicate error } @@ -1058,9 +1201,9 @@ extern "C" PyObject *number_rshift_handler( PyObject *self, PyObject *other ) try { PythonExtensionBase *p = getPythonExtensionBase( self ); - return new_reference_to( p->number_rshift( Py::Object( other ) ) ); + return new_reference_to( p->number_rshift( Object( other ) ) ); } - catch( Py::Exception & ) + catch( BaseException & ) { return NULL; // indicate error } @@ -1071,9 +1214,9 @@ extern "C" PyObject *number_and_handler( PyObject *self, PyObject *other ) try { PythonExtensionBase *p = getPythonExtensionBase( self ); - return new_reference_to( p->number_and( Py::Object( other ) ) ); + return new_reference_to( p->number_and( Object( other ) ) ); } - catch( Py::Exception & ) + catch( BaseException & ) { return NULL; // indicate error } @@ -1084,9 +1227,9 @@ extern "C" PyObject *number_xor_handler( PyObject *self, PyObject *other ) try { PythonExtensionBase *p = getPythonExtensionBase( self ); - return new_reference_to( p->number_xor( Py::Object( other ) ) ); + return new_reference_to( p->number_xor( Object( other ) ) ); } - catch( Py::Exception & ) + catch( BaseException & ) { return NULL; // indicate error } @@ -1097,9 +1240,9 @@ extern "C" PyObject *number_or_handler( PyObject *self, PyObject *other ) try { PythonExtensionBase *p = getPythonExtensionBase( self ); - return new_reference_to( p->number_or( Py::Object( other ) ) ); + return new_reference_to( p->number_or( Object( other ) ) ); } - catch( Py::Exception & ) + catch( BaseException & ) { return NULL; // indicate error } @@ -1110,9 +1253,9 @@ extern "C" PyObject *number_power_handler( PyObject *self, PyObject *x1, PyObjec try { PythonExtensionBase *p = getPythonExtensionBase( self ); - return new_reference_to( p->number_power( Py::Object( x1 ), Py::Object( x2 ) ) ); + return new_reference_to( p->number_power( Object( x1 ), Object( x2 ) ) ); } - catch( Py::Exception & ) + catch( BaseException & ) { return NULL; // indicate error } @@ -1126,7 +1269,7 @@ extern "C" int buffer_get_handler( PyObject *self, Py_buffer *buf, int flags ) PythonExtensionBase *p = getPythonExtensionBase( self ); return p->buffer_get( buf, flags ); } - catch( Py::Exception & ) + catch( BaseException & ) { return -1; // indicate error } @@ -1157,81 +1300,81 @@ PythonExtensionBase::~PythonExtensionBase() assert( ob_refcnt == 0 ); } -Py::Object PythonExtensionBase::callOnSelf( const std::string &fn_name ) +Object PythonExtensionBase::callOnSelf( const std::string &fn_name ) { - Py::TupleN args; + TupleN args; return self().callMemberFunction( fn_name, args ); } -Py::Object PythonExtensionBase::callOnSelf( const std::string &fn_name, - const Py::Object &arg1 ) +Object PythonExtensionBase::callOnSelf( const std::string &fn_name, + const Object &arg1 ) { - Py::TupleN args( arg1 ); + TupleN args( arg1 ); return self().callMemberFunction( fn_name, args ); } -Py::Object PythonExtensionBase::callOnSelf( const std::string &fn_name, - const Py::Object &arg1, const Py::Object &arg2 ) +Object PythonExtensionBase::callOnSelf( const std::string &fn_name, + const Object &arg1, const Object &arg2 ) { - Py::TupleN args( arg1, arg2 ); + TupleN args( arg1, arg2 ); return self().callMemberFunction( fn_name, args ); } -Py::Object PythonExtensionBase::callOnSelf( const std::string &fn_name, - const Py::Object &arg1, const Py::Object &arg2, const Py::Object &arg3 ) +Object PythonExtensionBase::callOnSelf( const std::string &fn_name, + const Object &arg1, const Object &arg2, const Object &arg3 ) { - Py::TupleN args( arg1, arg2, arg3 ); + TupleN args( arg1, arg2, arg3 ); return self().callMemberFunction( fn_name, args ); } -Py::Object PythonExtensionBase::callOnSelf( const std::string &fn_name, - const Py::Object &arg1, const Py::Object &arg2, const Py::Object &arg3, - const Py::Object &arg4 ) +Object PythonExtensionBase::callOnSelf( const std::string &fn_name, + const Object &arg1, const Object &arg2, const Object &arg3, + const Object &arg4 ) { - Py::TupleN args( arg1, arg2, arg3, arg4 ); + TupleN args( arg1, arg2, arg3, arg4 ); return self().callMemberFunction( fn_name, args ); } -Py::Object PythonExtensionBase::callOnSelf( const std::string &fn_name, - const Py::Object &arg1, const Py::Object &arg2, const Py::Object &arg3, - const Py::Object &arg4, const Py::Object &arg5 ) +Object PythonExtensionBase::callOnSelf( const std::string &fn_name, + const Object &arg1, const Object &arg2, const Object &arg3, + const Object &arg4, const Object &arg5 ) { - Py::TupleN args( arg1, arg2, arg3, arg4, arg5 ); + TupleN args( arg1, arg2, arg3, arg4, arg5 ); return self().callMemberFunction( fn_name, args ); } -Py::Object PythonExtensionBase::callOnSelf( const std::string &fn_name, - const Py::Object &arg1, const Py::Object &arg2, const Py::Object &arg3, - const Py::Object &arg4, const Py::Object &arg5, const Py::Object &arg6 ) +Object PythonExtensionBase::callOnSelf( const std::string &fn_name, + const Object &arg1, const Object &arg2, const Object &arg3, + const Object &arg4, const Object &arg5, const Object &arg6 ) { - Py::TupleN args( arg1, arg2, arg3, arg4, arg5, arg6 ); + TupleN args( arg1, arg2, arg3, arg4, arg5, arg6 ); return self().callMemberFunction( fn_name, args ); } -Py::Object PythonExtensionBase::callOnSelf( const std::string &fn_name, - const Py::Object &arg1, const Py::Object &arg2, const Py::Object &arg3, - const Py::Object &arg4, const Py::Object &arg5, const Py::Object &arg6, - const Py::Object &arg7 ) +Object PythonExtensionBase::callOnSelf( const std::string &fn_name, + const Object &arg1, const Object &arg2, const Object &arg3, + const Object &arg4, const Object &arg5, const Object &arg6, + const Object &arg7 ) { - Py::TupleN args( arg1, arg2, arg3, arg4, arg5, arg6, arg7 ); + TupleN args( arg1, arg2, arg3, arg4, arg5, arg6, arg7 ); return self().callMemberFunction( fn_name, args ); } -Py::Object PythonExtensionBase::callOnSelf( const std::string &fn_name, - const Py::Object &arg1, const Py::Object &arg2, const Py::Object &arg3, - const Py::Object &arg4, const Py::Object &arg5, const Py::Object &arg6, - const Py::Object &arg7, const Py::Object &arg8 ) +Object PythonExtensionBase::callOnSelf( const std::string &fn_name, + const Object &arg1, const Object &arg2, const Object &arg3, + const Object &arg4, const Object &arg5, const Object &arg6, + const Object &arg7, const Object &arg8 ) { - Py::TupleN args( arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8 ); + TupleN args( arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8 ); return self().callMemberFunction( fn_name, args ); } -Py::Object PythonExtensionBase::callOnSelf( const std::string &fn_name, - const Py::Object &arg1, const Py::Object &arg2, const Py::Object &arg3, - const Py::Object &arg4, const Py::Object &arg5, const Py::Object &arg6, - const Py::Object &arg7, const Py::Object &arg8, const Py::Object &arg9 ) +Object PythonExtensionBase::callOnSelf( const std::string &fn_name, + const Object &arg1, const Object &arg2, const Object &arg3, + const Object &arg4, const Object &arg5, const Object &arg6, + const Object &arg7, const Object &arg8, const Object &arg9 ) { - Py::TupleN args( arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9 ); + TupleN args( arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9 ); return self().callMemberFunction( fn_name, args ); } @@ -1241,12 +1384,12 @@ void PythonExtensionBase::reinit( Tuple & /* args */, Dict & /* kwds */) } -Py::Object PythonExtensionBase::genericGetAttro( const Py::String &name ) +Object PythonExtensionBase::genericGetAttro( const String &name ) { return asObject( PyObject_GenericGetAttr( selfPtr(), name.ptr() ) ); } -int PythonExtensionBase::genericSetAttro( const Py::String &name, const Py::Object &value ) +int PythonExtensionBase::genericSetAttro( const String &name, const Object &value ) { return PyObject_GenericSetAttr( selfPtr(), name.ptr(), value.ptr() ); } @@ -1255,240 +1398,215 @@ int PythonExtensionBase::genericSetAttro( const Py::String &name, const Py::Obje int PythonExtensionBase::print( FILE *, int ) { missing_method( print ); - return -1; } #endif -Py::Object PythonExtensionBase::getattr( const char * ) +Object PythonExtensionBase::getattr( const char * ) { missing_method( getattr ); - return Py::None(); } -int PythonExtensionBase::setattr( const char *, const Py::Object & ) +int PythonExtensionBase::setattr( const char *, const Object & ) { missing_method( setattr ); - return -1; } -Py::Object PythonExtensionBase::getattro( const Py::String &name ) +Object PythonExtensionBase::getattro( const String &name ) { return asObject( PyObject_GenericGetAttr( selfPtr(), name.ptr() ) ); } -int PythonExtensionBase::setattro( const Py::String &name, const Py::Object &value ) +int PythonExtensionBase::setattro( const String &name, const Object &value ) { return PyObject_GenericSetAttr( selfPtr(), name.ptr(), value.ptr() ); } -int PythonExtensionBase::compare( const Py::Object & ) +int PythonExtensionBase::compare( const Object & ) { missing_method( compare ); - return -1; } -Py::Object PythonExtensionBase::rich_compare( const Py::Object &, int ) +Object PythonExtensionBase::rich_compare( const Object &, int ) { missing_method( rich_compare ); - return Py::None(); } -Py::Object PythonExtensionBase::repr() +Object PythonExtensionBase::repr() { missing_method( repr ); - return Py::None(); } -Py::Object PythonExtensionBase::str() +Object PythonExtensionBase::str() { missing_method( str ); - return Py::None(); } long PythonExtensionBase::hash() { missing_method( hash ); - return -1; } - - -Py::Object PythonExtensionBase::call( const Py::Object &, const Py::Object & ) -{ - missing_method( call ); - return Py::None(); } -Py::Object PythonExtensionBase::iter() +Object PythonExtensionBase::call( const Object &, const Object & ) +{ + missing_method( call ); +} + +Object PythonExtensionBase::iter() { missing_method( iter ); - return Py::None(); } PyObject *PythonExtensionBase::iternext() { missing_method( iternext ); - return NULL; } // Sequence methods -int PythonExtensionBase::sequence_length() +PyCxx_ssize_t PythonExtensionBase::sequence_length() { missing_method( sequence_length ); - return -1; } - -Py::Object PythonExtensionBase::sequence_concat( const Py::Object & ) +Object PythonExtensionBase::sequence_concat( const Object & ) { missing_method( sequence_concat ); - return Py::None(); } -Py::Object PythonExtensionBase::sequence_repeat( Py_ssize_t ) +Object PythonExtensionBase::sequence_repeat( Py_ssize_t ) { missing_method( sequence_repeat ); - return Py::None(); } -Py::Object PythonExtensionBase::sequence_item( Py_ssize_t ) +Object PythonExtensionBase::sequence_item( Py_ssize_t ) { missing_method( sequence_item ); - return Py::None(); } -int PythonExtensionBase::sequence_ass_item( Py_ssize_t, const Py::Object & ) +int PythonExtensionBase::sequence_ass_item( Py_ssize_t, const Object & ) { missing_method( sequence_ass_item ); - return -1; } +Object PythonExtensionBase::sequence_inplace_concat( const Object & ) +{ + missing_method( sequence_inplace_concat ); +} + +Object PythonExtensionBase::sequence_inplace_repeat( Py_ssize_t ) +{ + missing_method( sequence_inplace_repeat ); +} + +int PythonExtensionBase::sequence_contains( const Object & ) +{ + missing_method( sequence_contains ); +} // Mapping -int PythonExtensionBase::mapping_length() +PyCxx_ssize_t PythonExtensionBase::mapping_length() { missing_method( mapping_length ); - return -1; } - -Py::Object PythonExtensionBase::mapping_subscript( const Py::Object & ) +Object PythonExtensionBase::mapping_subscript( const Object & ) { missing_method( mapping_subscript ); - return Py::None(); } -int PythonExtensionBase::mapping_ass_subscript( const Py::Object &, const Py::Object & ) +int PythonExtensionBase::mapping_ass_subscript( const Object &, const Object & ) { missing_method( mapping_ass_subscript ); - return -1; } -Py::Object PythonExtensionBase::number_negative() +Object PythonExtensionBase::number_negative() { missing_method( number_negative ); - return Py::None(); } -Py::Object PythonExtensionBase::number_positive() +Object PythonExtensionBase::number_positive() { missing_method( number_positive ); - return Py::None(); } -Py::Object PythonExtensionBase::number_absolute() +Object PythonExtensionBase::number_absolute() { missing_method( number_absolute ); - return Py::None(); } -Py::Object PythonExtensionBase::number_invert() +Object PythonExtensionBase::number_invert() { missing_method( number_invert ); - return Py::None(); } -Py::Object PythonExtensionBase::number_int() +Object PythonExtensionBase::number_int() { missing_method( number_int ); - return Py::None(); } -Py::Object PythonExtensionBase::number_float() +Object PythonExtensionBase::number_float() { missing_method( number_float ); - return Py::None(); } -Py::Object PythonExtensionBase::number_long() +Object PythonExtensionBase::number_long() { missing_method( number_long ); - return Py::None(); } -Py::Object PythonExtensionBase::number_add( const Py::Object & ) +Object PythonExtensionBase::number_add( const Object & ) { missing_method( number_add ); - return Py::None(); } -Py::Object PythonExtensionBase::number_subtract( const Py::Object & ) +Object PythonExtensionBase::number_subtract( const Object & ) { missing_method( number_subtract ); - return Py::None(); } -Py::Object PythonExtensionBase::number_multiply( const Py::Object & ) +Object PythonExtensionBase::number_multiply( const Object & ) { missing_method( number_multiply ); - return Py::None(); } -Py::Object PythonExtensionBase::number_remainder( const Py::Object & ) +Object PythonExtensionBase::number_remainder( const Object & ) { missing_method( number_remainder ); - return Py::None(); } -Py::Object PythonExtensionBase::number_divmod( const Py::Object & ) +Object PythonExtensionBase::number_divmod( const Object & ) { missing_method( number_divmod ); - return Py::None(); } -Py::Object PythonExtensionBase::number_lshift( const Py::Object & ) +Object PythonExtensionBase::number_lshift( const Object & ) { missing_method( number_lshift ); - return Py::None(); } -Py::Object PythonExtensionBase::number_rshift( const Py::Object & ) +Object PythonExtensionBase::number_rshift( const Object & ) { missing_method( number_rshift ); - return Py::None(); } -Py::Object PythonExtensionBase::number_and( const Py::Object & ) +Object PythonExtensionBase::number_and( const Object & ) { missing_method( number_and ); - return Py::None(); } -Py::Object PythonExtensionBase::number_xor( const Py::Object & ) +Object PythonExtensionBase::number_xor( const Object & ) { missing_method( number_xor ); - return Py::None(); } -Py::Object PythonExtensionBase::number_or( const Py::Object & ) +Object PythonExtensionBase::number_or( const Object & ) { missing_method( number_or ); - return Py::None(); } -Py::Object PythonExtensionBase::number_power( const Py::Object &, const Py::Object & ) +Object PythonExtensionBase::number_power( const Object &, const Object & ) { missing_method( number_power ); - return Py::None(); } @@ -1496,7 +1614,6 @@ Py::Object PythonExtensionBase::number_power( const Py::Object &, const Py::Obje int PythonExtensionBase::buffer_get( Py_buffer * /*buf*/, int /*flags*/ ) { missing_method( buffer_get ); - return -1; } int PythonExtensionBase::buffer_release( Py_buffer * /*buf*/ ) @@ -1531,7 +1648,7 @@ extern "C" PyObject *method_noargs_call_handler( PyObject *_self_and_name_tuple, return new_reference_to( result.ptr() ); } - catch( Exception & ) + catch( BaseException & ) { return 0; } @@ -1561,7 +1678,7 @@ extern "C" PyObject *method_varargs_call_handler( PyObject *_self_and_name_tuple return new_reference_to( result.ptr() ); } - catch( Exception & ) + catch( BaseException & ) { return 0; } @@ -1615,7 +1732,7 @@ extern "C" PyObject *method_keyword_call_handler( PyObject *_self_and_name_tuple return new_reference_to( result.ptr() ); } } - catch( Exception & ) + catch( BaseException & ) { return 0; } @@ -1628,7 +1745,7 @@ extern "C" PyObject *method_keyword_call_handler( PyObject *_self_and_name_tuple // //-------------------------------------------------------------------------------- ExtensionExceptionType::ExtensionExceptionType() - : Py::Object() +: Object() { } @@ -1650,31 +1767,47 @@ void ExtensionExceptionType::init( ExtensionModuleBase &module, const std::strin set( PyErr_NewException( const_cast( module_name.c_str() ), parent.ptr(), NULL ), true ); } -// is the exception this specific exception 'exc' -bool Exception::matches( ExtensionExceptionType &exc ) -{ - return PyErr_ExceptionMatches( exc.ptr() ) != 0; -} - ExtensionExceptionType::~ExtensionExceptionType() { } -Exception::Exception( ExtensionExceptionType &exception, const std::string& reason ) +BaseException::BaseException( ExtensionExceptionType &exception, const std::string& reason ) { PyErr_SetString( exception.ptr(), reason.c_str() ); } -Exception::Exception( ExtensionExceptionType &exception, Object &reason ) +BaseException::BaseException( ExtensionExceptionType &exception, Object &reason ) { PyErr_SetObject( exception.ptr(), reason.ptr() ); } -Exception::Exception( PyObject *exception, Object &reason ) +BaseException::BaseException( PyObject *exception, Object &reason ) { PyErr_SetObject( exception, reason.ptr() ); } +BaseException::BaseException( PyObject *exception, const std::string &reason ) +{ + PyErr_SetString( exception, reason.c_str() ); +} + +BaseException::BaseException() +{ +} + +void BaseException::clear() +{ + PyErr_Clear(); +} + +// is the exception this specific exception 'exc' +bool BaseException::matches( ExtensionExceptionType &exc ) +{ + return PyErr_ExceptionMatches( exc.ptr() ) != 0; +} + +//------------------------------------------------------------ + #if 1 //------------------------------------------------------------ // compare operators diff --git a/src/CXX/Python3/cxx_standard_exceptions.hxx b/src/CXX/Python3/cxx_standard_exceptions.hxx index 4bf2ca0bd9..4c53ad967d 100644 --- a/src/CXX/Python3/cxx_standard_exceptions.hxx +++ b/src/CXX/Python3/cxx_standard_exceptions.hxx @@ -2,20 +2,67 @@ #pragma error( "define PYCXX_STANDARD_EXCEPTION before including" ) #endif -PYCXX_STANDARD_EXCEPTION( LookupError, StandardError ) -PYCXX_STANDARD_EXCEPTION( ArithmeticError, StandardError ) -PYCXX_STANDARD_EXCEPTION( EnvironmentError, StandardError ) -PYCXX_STANDARD_EXCEPTION( TypeError, StandardError ) -PYCXX_STANDARD_EXCEPTION( IndexError, LookupError ) -PYCXX_STANDARD_EXCEPTION( AttributeError, StandardError ) -PYCXX_STANDARD_EXCEPTION( NameError, StandardError ) -PYCXX_STANDARD_EXCEPTION( RuntimeError, StandardError ) -PYCXX_STANDARD_EXCEPTION( NotImplementedError, StandardError ) -PYCXX_STANDARD_EXCEPTION( SystemError, StandardError ) -PYCXX_STANDARD_EXCEPTION( KeyError, LookupError ) -PYCXX_STANDARD_EXCEPTION( ValueError, StandardError ) -PYCXX_STANDARD_EXCEPTION( OverflowError, ArithmeticError ) -PYCXX_STANDARD_EXCEPTION( ZeroDivisionError, ArithmeticError ) -PYCXX_STANDARD_EXCEPTION( FloatingPointError, ArithmeticError ) -PYCXX_STANDARD_EXCEPTION( MemoryError, StandardError ) -PYCXX_STANDARD_EXCEPTION( SystemExit, StandardError ) +// taken for python3.5 documentation + +// EnvironmentError and IOError are not used in Python3 +//PYCXX_STANDARD_EXCEPTION( EnvironmentError, QQQ ) +//PYCXX_STANDARD_EXCEPTION( IOError, QQQ ) + +// 5.4 Exception hierarchy +PYCXX_STANDARD_EXCEPTION( SystemExit, BaseException ) +PYCXX_STANDARD_EXCEPTION( KeyboardInterrupt, BaseException ) +PYCXX_STANDARD_EXCEPTION( GeneratorExit, BaseException ) +#if !defined( PYCXX_6_2_COMPATIBILITY ) +PYCXX_STANDARD_EXCEPTION( Exception, BaseException ) +#endif +PYCXX_STANDARD_EXCEPTION( StopIteration, Exception ) +#if PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION >= 5 +PYCXX_STANDARD_EXCEPTION( StopAsyncIteration, Exception ) +#endif +PYCXX_STANDARD_EXCEPTION( ArithmeticError, Exception ) +PYCXX_STANDARD_EXCEPTION( FloatingPointError, ArithmeticError ) +PYCXX_STANDARD_EXCEPTION( OverflowError, ArithmeticError ) +PYCXX_STANDARD_EXCEPTION( ZeroDivisionError, ArithmeticError ) +PYCXX_STANDARD_EXCEPTION( AssertionError, Exception ) +PYCXX_STANDARD_EXCEPTION( AttributeError, Exception ) +PYCXX_STANDARD_EXCEPTION( BufferError, Exception ) +PYCXX_STANDARD_EXCEPTION( EOFError, Exception ) +PYCXX_STANDARD_EXCEPTION( ImportError, Exception ) +PYCXX_STANDARD_EXCEPTION( LookupError, Exception ) +PYCXX_STANDARD_EXCEPTION( IndexError, LookupError ) +PYCXX_STANDARD_EXCEPTION( KeyError, LookupError ) +PYCXX_STANDARD_EXCEPTION( MemoryError, Exception ) +PYCXX_STANDARD_EXCEPTION( NameError, Exception ) +PYCXX_STANDARD_EXCEPTION( UnboundLocalError, NameError ) +PYCXX_STANDARD_EXCEPTION( OSError, Exception ) +PYCXX_STANDARD_EXCEPTION( BlockingIOError, OSError ) +PYCXX_STANDARD_EXCEPTION( ChildProcessError,OSError ) +PYCXX_STANDARD_EXCEPTION( ConnectionError, OSError ) +PYCXX_STANDARD_EXCEPTION( BrokenPipeError, ConnectionError ) +PYCXX_STANDARD_EXCEPTION( ConnectionAbortedError, ConnectionError ) +PYCXX_STANDARD_EXCEPTION( ConnectionRefusedError, ConnectionError ) +PYCXX_STANDARD_EXCEPTION( ConnectionResetError, ConnectionError ) +PYCXX_STANDARD_EXCEPTION( FileExistsError, OSError ) +PYCXX_STANDARD_EXCEPTION( FileNotFoundError, OSError ) +PYCXX_STANDARD_EXCEPTION( InterruptedError, OSError ) +PYCXX_STANDARD_EXCEPTION( IsADirectoryError, OSError ) +PYCXX_STANDARD_EXCEPTION( NotADirectoryError, OSError ) +PYCXX_STANDARD_EXCEPTION( PermissionError, OSError ) +PYCXX_STANDARD_EXCEPTION( ProcessLookupError, OSError ) +PYCXX_STANDARD_EXCEPTION( TimeoutError, OSError ) +PYCXX_STANDARD_EXCEPTION( ReferenceError, Exception ) +PYCXX_STANDARD_EXCEPTION( RuntimeError, Exception ) +PYCXX_STANDARD_EXCEPTION( NotImplementedError, RuntimeError ) +#if PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION >= 5 +PYCXX_STANDARD_EXCEPTION( RecursionError, RuntimeError ) +#endif +PYCXX_STANDARD_EXCEPTION( SyntaxError, Exception ) +PYCXX_STANDARD_EXCEPTION( IndentationError, SyntaxError ) +PYCXX_STANDARD_EXCEPTION( TabError, IndentationError ) +PYCXX_STANDARD_EXCEPTION( SystemError, Exception ) +PYCXX_STANDARD_EXCEPTION( TypeError, Exception ) +PYCXX_STANDARD_EXCEPTION( ValueError, Exception ) +PYCXX_STANDARD_EXCEPTION( UnicodeError, ValueError ) +PYCXX_STANDARD_EXCEPTION( UnicodeDecodeError, UnicodeError ) +PYCXX_STANDARD_EXCEPTION( UnicodeEncodeError, UnicodeError ) +PYCXX_STANDARD_EXCEPTION( UnicodeTranslateError,UnicodeError ) diff --git a/src/CXX/Version.hxx b/src/CXX/Version.hxx index 67f2ca9454..8247a731fa 100644 --- a/src/CXX/Version.hxx +++ b/src/CXX/Version.hxx @@ -38,8 +38,8 @@ #ifndef __PyCXX_version_hxx__ #define __PyCXX_version_hxx__ -#define PYCXX_VERSION_MAJOR 6 -#define PYCXX_VERSION_MINOR 3 +#define PYCXX_VERSION_MAJOR 7 +#define PYCXX_VERSION_MINOR 0 #define PYCXX_VERSION_PATCH 0 #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 )