Skip to content

Commit

Permalink
PyCXX: update to version 7.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
wwmayer committed Aug 1, 2022
1 parent cfc8fb9 commit 114b5be
Show file tree
Hide file tree
Showing 13 changed files with 408 additions and 178 deletions.
71 changes: 49 additions & 22 deletions src/CXX/IndirectPythonInterface.cxx
Expand Up @@ -43,11 +43,15 @@ PYCXX_EXPORT bool _CFunction_Check( PyObject *op ) { return op->ob_type == _CF
PYCXX_EXPORT bool _Complex_Check( PyObject *op ) { return op->ob_type == _Complex_Type(); }
PYCXX_EXPORT bool _Dict_Check( PyObject *op ) { return op->ob_type == _Dict_Type(); }
PYCXX_EXPORT bool _Float_Check( PyObject *op ) { return op->ob_type == _Float_Type(); }
#if PY_MAJOR_VERSION == 2 || !defined( Py_LIMITED_API )
PYCXX_EXPORT bool _Function_Check( PyObject *op ) { return op->ob_type == _Function_Type(); }
#endif
PYCXX_EXPORT bool _Boolean_Check( PyObject *op ) { return op->ob_type == _Bool_Type(); }
PYCXX_EXPORT bool _List_Check( PyObject *op ) { return op->ob_type == _List_Type(); }
PYCXX_EXPORT bool _Long_Check( PyObject *op ) { return op->ob_type == _Long_Type(); }
#if PY_MAJOR_VERSION == 2 || !defined( Py_LIMITED_API )
PYCXX_EXPORT bool _Method_Check( PyObject *op ) { return op->ob_type == _Method_Type(); }
#endif
PYCXX_EXPORT bool _Module_Check( PyObject *op ) { return op->ob_type == _Module_Type(); }
PYCXX_EXPORT bool _Range_Check( PyObject *op ) { return op->ob_type == _Range_Type(); }
PYCXX_EXPORT bool _Slice_Check( PyObject *op ) { return op->ob_type == _Slice_Type(); }
Expand All @@ -72,26 +76,30 @@ PYCXX_EXPORT bool _Bytes_Check( PyObject *op ) { return op->ob_type == _By

static HMODULE python_dll;

#define PYCXX_STANDARD_EXCEPTION( eclass, bclass ) \
# define PYCXX_STANDARD_EXCEPTION( eclass, bclass ) \
static PyObject *ptr_Exc_##eclass = NULL;

#if PY_MAJOR_VERSION == 2
#include "CXX/Python2/cxx_standard_exceptions.hxx"
#else
#include "CXX/Python3/cxx_standard_exceptions.hxx"
#endif
# 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
# undef PYCXX_STANDARD_EXCEPTION

static PyTypeObject *ptr__CFunction_Type = NULL;
static PyTypeObject *ptr__Complex_Type = NULL;
static PyTypeObject *ptr__Dict_Type = NULL;
static PyTypeObject *ptr__Float_Type = NULL;
# if PY_MAJOR_VERSION == 2 || !defined( Py_LIMITED_API )
static PyTypeObject *ptr__Function_Type = NULL;
# endif
static PyTypeObject *ptr__Bool_Type = NULL;
static PyTypeObject *ptr__List_Type = NULL;
static PyTypeObject *ptr__Long_Type = NULL;
# if PY_MAJOR_VERSION == 2 || !defined( Py_LIMITED_API )
static PyTypeObject *ptr__Method_Type = NULL;
# endif
static PyTypeObject *ptr__Module_Type = NULL;
static PyTypeObject *ptr__Range_Type = NULL;
static PyTypeObject *ptr__Slice_Type = NULL;
Expand All @@ -108,14 +116,15 @@ static PyTypeObject *ptr__CObject_Type = NULL;
static PyTypeObject *ptr__Bytes_Type = NULL;
# endif


# if PY_MAJOR_VERSION == 2 || !defined( Py_LIMITED_API )
static int *ptr_Py_DebugFlag = NULL;
static int *ptr_Py_InteractiveFlag = NULL;
static int *ptr_Py_OptimizeFlag = NULL;
static int *ptr_Py_NoSiteFlag = NULL;
static int *ptr_Py_VerboseFlag = NULL;

static char **ptr__Py_PackageContext = NULL;
# endif

# ifdef Py_REF_DEBUG
int *ptr_Py_RefTotal;
Expand Down Expand Up @@ -212,23 +221,25 @@ bool InitialisePythonIndirectInterface()
# ifdef Py_REF_DEBUG
ptr_Py_RefTotal = GetInt_as_IntPointer( "_Py_RefTotal" );
# endif
# if PY_MAJOR_VERSION == 2 || !defined( Py_LIMITED_API )
ptr_Py_DebugFlag = GetInt_as_IntPointer( "Py_DebugFlag" );
ptr_Py_InteractiveFlag = GetInt_as_IntPointer( "Py_InteractiveFlag" );
ptr_Py_OptimizeFlag = GetInt_as_IntPointer( "Py_OptimizeFlag" );
ptr_Py_NoSiteFlag = GetInt_as_IntPointer( "Py_NoSiteFlag" );
ptr_Py_VerboseFlag = GetInt_as_IntPointer( "Py_VerboseFlag" );
ptr__Py_PackageContext = GetCharPointer_as_CharPointerPointer( "_Py_PackageContext" );
# endif

#define PYCXX_STANDARD_EXCEPTION( eclass, bclass )
# define PYCXX_STANDARD_EXCEPTION( eclass, bclass )
ptr_Exc_#eclass = GetPyTypeObject_As_PyTypeObjectPointer( "PyExc_" #eclass );

#if PY_MAJOR_VERSION == 2
#include "CXX/Python2/cxx_standard_exceptions.hxx"
#else
#include "CXX/Python3/cxx_standard_exceptions.hxx"
#endif
# 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
# undef PYCXX_STANDARD_EXCEPTION

ptr__PyNone = GetPyObject_As_PyObjectPointer( "_Py_NoneStruct" );

Expand All @@ -243,11 +254,15 @@ bool InitialisePythonIndirectInterface()
ptr__Complex_Type = GetPyTypeObject_As_PyTypeObjectPointer( "PyComplex_Type" );
ptr__Dict_Type = GetPyTypeObject_As_PyTypeObjectPointer( "PyDict_Type" );
ptr__Float_Type = GetPyTypeObject_As_PyTypeObjectPointer( "PyFloat_Type" );
# if PY_MAJOR_VERSION == 2 || !defined( Py_LIMITED_API )
ptr__Function_Type = GetPyTypeObject_As_PyTypeObjectPointer( "PyFunction_Type" );
# endif
ptr__Bool_Type = GetPyTypeObject_As_PyTypeObjectPointer( "PyBool_Type" );
ptr__List_Type = GetPyTypeObject_As_PyTypeObjectPointer( "PyList_Type" );
ptr__Long_Type = GetPyTypeObject_As_PyTypeObjectPointer( "PyLong_Type" );
# if PY_MAJOR_VERSION == 2 || !defined( Py_LIMITED_API )
ptr__Method_Type = GetPyTypeObject_As_PyTypeObjectPointer( "PyMethod_Type" );
# endif
ptr__Module_Type = GetPyTypeObject_As_PyTypeObjectPointer( "PyModule_Type" );
ptr__Range_Type = GetPyTypeObject_As_PyTypeObjectPointer( "PyRange_Type" );
ptr__Slice_Type = GetPyTypeObject_As_PyTypeObjectPointer( "PySlice_Type" );
Expand Down Expand Up @@ -328,11 +343,15 @@ PYCXX_EXPORT PyTypeObject *_CFunction_Type() { return ptr__CFunction_Typ
PYCXX_EXPORT PyTypeObject *_Complex_Type() { return ptr__Complex_Type; }
PYCXX_EXPORT PyTypeObject *_Dict_Type() { return ptr__Dict_Type; }
PYCXX_EXPORT PyTypeObject *_Float_Type() { return ptr__Float_Type; }
# if PY_MAJOR_VERSION == 2 || !defined( Py_LIMITED_API )
PYCXX_EXPORT PyTypeObject *_Function_Type() { return ptr__Function_Type; }
# endif
PYCXX_EXPORT PyTypeObject *_Bool_Type() { return ptr__Bool_Type; }
PYCXX_EXPORT PyTypeObject *_List_Type() { return ptr__List_Type; }
PYCXX_EXPORT PyTypeObject *_Long_Type() { return ptr__Long_Type; }
# if PY_MAJOR_VERSION == 2 || !defined( Py_LIMITED_API )
PYCXX_EXPORT PyTypeObject *_Method_Type() { return ptr__Method_Type; }
# endif
PYCXX_EXPORT PyTypeObject *_Module_Type() { return ptr__Module_Type; }
PYCXX_EXPORT PyTypeObject *_Range_Type() { return ptr__Range_Type; }
PYCXX_EXPORT PyTypeObject *_Slice_Type() { return ptr__Slice_Type; }
Expand All @@ -352,11 +371,13 @@ PYCXX_EXPORT PyTypeObject *_Bytes_Type() { return ptr__Bytes_Type; }
//
// wrap the Python Flag variables
//
# if PY_MAJOR_VERSION == 2 || !defined( Py_LIMITED_API )
PYCXX_EXPORT int &_Py_DebugFlag() { return *ptr_Py_DebugFlag; }
PYCXX_EXPORT int &_Py_InteractiveFlag() { return *ptr_Py_InteractiveFlag; }
PYCXX_EXPORT int &_Py_OptimizeFlag() { return *ptr_Py_OptimizeFlag; }
PYCXX_EXPORT int &_Py_NoSiteFlag() { return *ptr_Py_NoSiteFlag; }
PYCXX_EXPORT int &_Py_VerboseFlag() { return *ptr_Py_VerboseFlag; }
# endif

PYCXX_EXPORT char *__Py_PackageContext() { return *ptr__Py_PackageContext; }

Expand Down Expand Up @@ -416,16 +437,16 @@ void _XDECREF( PyObject *op )
//
// Wrap variables as function calls
//
#define PYCXX_STANDARD_EXCEPTION( eclass, bclass ) \
# define PYCXX_STANDARD_EXCEPTION( eclass, bclass ) \
PYCXX_EXPORT PyObject *_Exc_##eclass() { return ::PyExc_##eclass; }

#if PY_MAJOR_VERSION == 2
#include "CXX/Python2/cxx_standard_exceptions.hxx"
#else
#include "CXX/Python3/cxx_standard_exceptions.hxx"
#endif
# 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
# undef PYCXX_STANDARD_EXCEPTION

//
// wrap items in Object.h
Expand All @@ -439,11 +460,15 @@ PYCXX_EXPORT PyTypeObject *_CFunction_Type() { return &PyCFunction_Type;
PYCXX_EXPORT PyTypeObject *_Complex_Type() { return &PyComplex_Type; }
PYCXX_EXPORT PyTypeObject *_Dict_Type() { return &PyDict_Type; }
PYCXX_EXPORT PyTypeObject *_Float_Type() { return &PyFloat_Type; }
# if PY_MAJOR_VERSION == 2 || !defined( Py_LIMITED_API )
PYCXX_EXPORT PyTypeObject *_Function_Type() { return &PyFunction_Type; }
# endif
PYCXX_EXPORT PyTypeObject *_Bool_Type() { return &PyBool_Type; }
PYCXX_EXPORT PyTypeObject *_List_Type() { return &PyList_Type; }
PYCXX_EXPORT PyTypeObject *_Long_Type() { return &PyLong_Type; }
# if PY_MAJOR_VERSION == 2 || !defined( Py_LIMITED_API )
PYCXX_EXPORT PyTypeObject *_Method_Type() { return &PyMethod_Type; }
# endif
PYCXX_EXPORT PyTypeObject *_Module_Type() { return &PyModule_Type; }
PYCXX_EXPORT PyTypeObject *_Range_Type() { return &PyRange_Type; }
PYCXX_EXPORT PyTypeObject *_Slice_Type() { return &PySlice_Type; }
Expand All @@ -463,12 +488,14 @@ PYCXX_EXPORT PyTypeObject *_Bytes_Type() { return &PyBytes_Type; }
//
// wrap flags
//
# if PY_MAJOR_VERSION == 2 || !defined( Py_LIMITED_API )
PYCXX_EXPORT int &_Py_DebugFlag() { return Py_DebugFlag; }
PYCXX_EXPORT int &_Py_InteractiveFlag() { return Py_InteractiveFlag; }
PYCXX_EXPORT int &_Py_OptimizeFlag() { return Py_OptimizeFlag; }
PYCXX_EXPORT int &_Py_NoSiteFlag() { return Py_NoSiteFlag; }
PYCXX_EXPORT int &_Py_VerboseFlag() { return Py_VerboseFlag; }
PYCXX_EXPORT const char *__Py_PackageContext() { return _Py_PackageContext; }
# endif

//
// Needed to keep the abstractions for delayload interface
Expand Down
4 changes: 4 additions & 0 deletions src/CXX/Python3/Config.hxx
Expand Up @@ -38,6 +38,10 @@
#ifndef __PyCXX_config_hh__
#define __PyCXX_config_hh__

#if defined( Py_LIMITED_API ) && Py_LIMITED_API+0 < 0x03040000
#error "PyCXX support for Python limited API requires version 3.4 or newer. Py_LIMITED_API=0x03040000"
#endif

//
// Microsoft VC++ 6.0 has no traits
//
Expand Down
4 changes: 4 additions & 0 deletions src/CXX/Python3/Exception.hxx
Expand Up @@ -85,6 +85,10 @@ namespace Py
: BaseException( exception, reason )
{}

Exception( PyObject *exception, Object &reason )
: BaseException( exception, reason )
{}

Exception( PyObject *exception, const std::string &reason )
: BaseException( exception, reason )
{}
Expand Down
6 changes: 5 additions & 1 deletion src/CXX/Python3/ExtensionOldType.hxx
Expand Up @@ -119,15 +119,19 @@ namespace Py
{
std::string name( _name );

#if !defined( Py_LIMITED_API )
if( name == "__name__" && type_object()->tp_name != NULL )
{
return Py::String( type_object()->tp_name );
}
#endif

#if !defined( Py_LIMITED_API )
if( name == "__doc__" && type_object()->tp_doc != NULL )
{
return Py::String( type_object()->tp_doc );
}
#endif

// trying to fake out being a class for help()
// else if( name == "__bases__" )
Expand Down Expand Up @@ -192,7 +196,7 @@ namespace Py
self[0] = Object( this );
self[1] = Object( PyCapsule_New( method_def, NULL, NULL ), 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);
}
Expand Down
14 changes: 12 additions & 2 deletions src/CXX/Python3/ExtensionType.hxx
Expand Up @@ -246,10 +246,15 @@ namespace Py
#ifdef PYCXX_DEBUG
std::cout << "extension_object_new()" << std::endl;
#endif
PythonClassInstance *o = reinterpret_cast<PythonClassInstance *>( subtype->tp_alloc( subtype, 0 ) );
if( o == NULL )
#if defined( Py_LIMITED_API )
PyObject *object = reinterpret_cast<allocfunc>( PyType_GetSlot( subtype, Py_tp_alloc ) )( subtype, 0 );
#else
PyObject *object = subtype->tp_alloc( subtype, 0 );
#endif
if( object == NULL )
return NULL;

PythonClassInstance *o = reinterpret_cast<PythonClassInstance *>( object );
o->m_pycxx_object = NULL;

PyObject *self = reinterpret_cast<PyObject *>( o );
Expand Down Expand Up @@ -304,7 +309,12 @@ namespace Py
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;
#ifdef Py_LIMITED_API
freefunc fn = reinterpret_cast<freefunc>( PyType_GetSlot( _self->ob_type, Py_tp_free ) );
fn( _self );
#else
_self->ob_type->tp_free( _self );
#endif
}

public:
Expand Down
2 changes: 2 additions & 0 deletions src/CXX/Python3/ExtensionTypeBase.hxx
Expand Up @@ -127,9 +127,11 @@ namespace Py
virtual Object number_or( const Object & );
virtual Object number_power( const Object &, const Object & );

#if !defined( Py_LIMITED_API )
// Buffer
virtual int buffer_get( Py_buffer *, int flags );
virtual int buffer_release( Py_buffer *buf );
#endif

public:
// helper functions to call function fn_name with 0 to 9 args
Expand Down
13 changes: 8 additions & 5 deletions src/CXX/Python3/IndirectPythonInterface.hxx
Expand Up @@ -48,13 +48,13 @@ bool InitialisePythonIndirectInterface();
//
// Wrap Exception variables as function calls
//
PYCXX_EXPORT PyObject * _Exc_Exception();
PYCXX_EXPORT PyObject * _Exc_BaseException();

#define PYCXX_STANDARD_EXCEPTION( eclass, bclass ) \
# define PYCXX_STANDARD_EXCEPTION( eclass, bclass ) \
PYCXX_EXPORT PyObject * _Exc_##eclass();

#include "CXX/Python3/cxx_standard_exceptions.hxx"
#undef PYCXX_STANDARD_EXCEPTION
# include "CXX/Python3/cxx_standard_exceptions.hxx"
# undef PYCXX_STANDARD_EXCEPTION

//
// Wrap Object variables as function calls
Expand All @@ -79,11 +79,13 @@ PYCXX_EXPORT bool _Class_Check( PyObject *op );
PYCXX_EXPORT PyTypeObject * _Instance_Type();
PYCXX_EXPORT bool _Instance_Check( PyObject *op );

# if !defined( Py_LIMITED_API )
PYCXX_EXPORT PyTypeObject * _Method_Type();
PYCXX_EXPORT bool _Method_Check( PyObject *op );

PYCXX_EXPORT PyTypeObject * _Function_Type();
PYCXX_EXPORT bool _Function_Check( PyObject *op );
# endif

PYCXX_EXPORT PyTypeObject * _Complex_Type();
PYCXX_EXPORT bool _Complex_Check( PyObject *op );
Expand Down Expand Up @@ -139,15 +141,16 @@ PYCXX_EXPORT bool _TraceBack_Check( PyObject *v );
PYCXX_EXPORT PyTypeObject * _Tuple_Type();
PYCXX_EXPORT bool _Tuple_Check( PyObject *op );

# if PY_MAJOR_VERSION == 2 || !defined( Py_LIMITED_API )
PYCXX_EXPORT int &_Py_DebugFlag();
PYCXX_EXPORT int &_Py_InteractiveFlag();
PYCXX_EXPORT int &_Py_OptimizeFlag();
PYCXX_EXPORT int &_Py_NoSiteFlag();
PYCXX_EXPORT int &_Py_TabcheckFlag();
PYCXX_EXPORT int &_Py_VerboseFlag();
PYCXX_EXPORT int &_Py_UnicodeFlag();

PYCXX_EXPORT const char *__Py_PackageContext();
# endif

PYCXX_EXPORT void _XINCREF( PyObject *op );
PYCXX_EXPORT void _XDECREF( PyObject *op );
Expand Down

0 comments on commit 114b5be

Please sign in to comment.