From cb701bd77e52a22c2975de396f1442eb742ae886 Mon Sep 17 00:00:00 2001 From: Robin Dunn Date: Fri, 4 Mar 2016 21:05:46 -0800 Subject: [PATCH] Reinstate the size_t MappedType with compile-time bit size checking Replace the use of size_t in custom code with ulong so SIP doesn't try to use the MappedType there (it only uses pointers for them.) --- etg/_core.py | 2 +- etg/defs.py | 3 +- etg/graphics.py | 2 +- etg/sizer.py | 4 +- etg/stream.py | 12 +++--- etgtools/tweaker_tools.py | 6 +-- src/size_t.sip | 85 --------------------------------------- src/wacky_ints.sip | 78 +++++++++++++++++++++++++++++++++++ src/wxpy_api.h | 1 + 9 files changed, 93 insertions(+), 100 deletions(-) delete mode 100644 src/size_t.sip create mode 100644 src/wacky_ints.sip diff --git a/etg/_core.py b/etg/_core.py index 908a2a3d..4a570dfe 100644 --- a/etg/_core.py +++ b/etg/_core.py @@ -31,7 +31,7 @@ ITEMS = [ ] # promoted script to be the same as its NAME. INCLUDES = [ # base and core stuff - 'size_t', + 'wacky_ints', 'defs', 'debug', 'object', diff --git a/etg/defs.py b/etg/defs.py index f311cab7..6ab16db8 100644 --- a/etg/defs.py +++ b/etg/defs.py @@ -49,11 +49,9 @@ def run(): if sys.maxsize > 2**32: module.find('wxIntPtr').type = 'long long' module.find('wxUIntPtr').type = 'unsigned long long' - #module.insertItemAfter(td, etgtools.TypedefDef(type='unsigned long long', name='size_t')) else: module.find('wxIntPtr').type = 'long' module.find('wxUIntPtr').type = 'unsigned long' - #module.insertItemAfter(td, etgtools.TypedefDef(type='unsigned long', name='size_t')) # Correct the types for these as their values are outside the range of int @@ -82,6 +80,7 @@ def run(): module.insertItemAfter(td, etgtools.TypedefDef(type='long long', name='wxFileOffset')) module.insertItemAfter(td, etgtools.TypedefDef(type='SIP_SSIZE_T', name='ssize_t')) module.insertItemAfter(td, etgtools.TypedefDef(type='unsigned char', name='byte', pyInt=True)) + module.insertItemAfter(td, etgtools.TypedefDef(type='unsigned long', name='ulong')) diff --git a/etg/graphics.py b/etg/graphics.py index b18acd84..973c9f0c 100644 --- a/etg/graphics.py +++ b/etg/graphics.py @@ -212,7 +212,7 @@ def run(): #--------------------------------------------- c = module.find('wxGraphicsGradientStops') c.addCppMethod('SIP_SSIZE_T', '__len__', '()', body="return (SIP_SSIZE_T)self->GetCount();") - c.addCppMethod('wxGraphicsGradientStop*', '__getitem__', '(size_t n)', + c.addCppMethod('wxGraphicsGradientStop*', '__getitem__', '(ulong n)', pyArgsString='(n)', body="return new wxGraphicsGradientStop(self->Item(n));", factory=True) diff --git a/etg/sizer.py b/etg/sizer.py index 54b35fe9..73d7bd6b 100644 --- a/etg/sizer.py +++ b/etg/sizer.py @@ -120,7 +120,7 @@ def run(): body="return self->Prepend(size->x, size->y, proportion, flag, border, userData);") c.addCppMethod('wxSizerItem*', 'Insert', - '(size_t index, const wxSize& size, int proportion=0, int flag=0, ' + '(ulong index, const wxSize& size, int proportion=0, int flag=0, ' 'int border=0, wxPyUserData* userData /Transfer/ = NULL)', doc="Insert a spacer using a :class:`Size` object.", body="return self->Insert(index, size->x, size->y, proportion, flag, border, userData);") @@ -137,7 +137,7 @@ def run(): body="return self->Prepend(size->x, size->y, *flags);") c.addCppMethod('wxSizerItem*', 'Insert', - '(size_t index, const wxSize& size, const wxSizerFlags& flags)', + '(ulong index, const wxSize& size, const wxSizerFlags& flags)', doc="Insert a spacer using a :class:`Size` object.", body="return self->Insert(index, size->x, size->y, *flags);") diff --git a/etg/stream.py b/etg/stream.py index d79940b8..cc7852c8 100644 --- a/etg/stream.py +++ b/etg/stream.py @@ -127,7 +127,7 @@ def run(): c.addCppMethod('PyObject*', 'read', '()', """\ wxMemoryBuffer buf; - const size_t BUFSIZE = 1024; + const ulong BUFSIZE = 1024; // read while bytes are available on the stream while ( self->CanRead() ) { @@ -137,7 +137,7 @@ def run(): return _makeReadBufObj(self, buf); """) - c.addCppMethod('PyObject*', 'read', '(size_t size)', """\ + c.addCppMethod('PyObject*', 'read', '(ulong size)', """\ wxMemoryBuffer buf; // Read only size number of characters @@ -158,7 +158,7 @@ def run(): return _makeReadBufObj(self, buf); """) - c.addCppMethod('PyObject*', 'readline', '(size_t size)', """\ + c.addCppMethod('PyObject*', 'readline', '(ulong size)', """\ wxMemoryBuffer buf; int i; char ch; @@ -177,7 +177,7 @@ def run(): // This does the real work of the readlines methods static PyObject* _readlinesHelper(wxInputStream* self, - bool useSizeHint=false, size_t sizehint=0) { + bool useSizeHint=false, ulong sizehint=0) { PyObject* pylist; // init list @@ -193,7 +193,7 @@ def run(): } // read sizehint bytes or until EOF - size_t i; + ulong i; for (i=0; (self->CanRead()) && (useSizeHint || (i < sizehint));) { PyObject* s = _wxInputStream_readline(self); if (s == NULL) { @@ -224,7 +224,7 @@ def run(): c.addCppMethod('PyObject*', 'readlines', '()', """\ return _readlinesHelper(self); """) - c.addCppMethod('PyObject*', 'readlines', '(size_t sizehint)', """\ + c.addCppMethod('PyObject*', 'readlines', '(ulong sizehint)', """\ return _readlinesHelper(self, true, sizehint); """) diff --git a/etgtools/tweaker_tools.py b/etgtools/tweaker_tools.py index c91d0440..cba0f783 100644 --- a/etgtools/tweaker_tools.py +++ b/etgtools/tweaker_tools.py @@ -831,7 +831,7 @@ public: sipRes = sipCpp->size(); %End - {ItemClass}* __getitem__(size_t index); + {ItemClass}* __getitem__(ulong index); %MethodCode if (index < sipCpp->size()) {{ {ListClass}::compatibility_iterator node = sipCpp->Item(index); @@ -963,7 +963,7 @@ public: sipRes = sipCpp->GetCount(); %End - {ItemClass}& __getitem__(size_t index); + {ItemClass}& __getitem__(ulong index); %MethodCode if (index < sipCpp->GetCount()) {{ sipRes = &sipCpp->Item(index); @@ -1026,7 +1026,7 @@ public: sipRes = sipCpp->GetCount(); %End - {ItemClass}* __getitem__(size_t index); + {ItemClass}* __getitem__(ulong index); %MethodCode if (index < sipCpp->GetCount()) {{ sipRes = sipCpp->Item(index); diff --git a/src/size_t.sip b/src/size_t.sip deleted file mode 100644 index 0f64d162..00000000 --- a/src/size_t.sip +++ /dev/null @@ -1,85 +0,0 @@ -//-------------------------------------------------------------------------- -// Name: size_t.sip - -// Purpose: Implements a %MappedType for size_t in order to -// deal with different sizeof's on different platforms. It's 32bit -// on some, 64bit on others. -// -// Author: Robin Dunn -// -// Created: 4-March-2016 -// Copyright: (c) 2016 by Total Control Software -// Licence: wxWindows license -//-------------------------------------------------------------------------- - - -// These types can have difference sizes based on the platform and -// architecture. Have SIP always treat them as long longs. - -typedef unsigned long long size_t; - - -//%MappedType size_t { - - //%TypeHeaderCode - //#include - //typedef size_t foosize_t; - //%End - - //%ConvertToTypeCode - //// Code to test an incoming PyObject for compatibility - //if (!sipIsErr) { - //if (PyNumber_Check(sipPy)) - //return TRUE; - //return FALSE; - //} - - //// Code to convert from a compatible PyObject - //#if wxSIZE_T_IS_ULONG - //*sipCppPtr = new size_t(PyLong_AsUnsignedLong(sipPy)); - //#else - //*sipCppPtr = new size_t(PyLong_AsUnsignedLongLong(sipPy)); - //#endif - //return sipGetState(sipTransferObj); - //%End - - //%ConvertFromTypeCode - //#if wxSIZE_T_IS_ULONG - //return PyLong_FromUnsignedLong(*sipCpp); - //#else - //return PyLong_FromUnsignedLongLong(*sipCpp); - //#endif - //%End -//}; - - -// Used just for testing the MappedType code, it can be removed later -%ModuleCode -size_t test_size_t(size_t value) -{ - size_t local = value; - return local; -} - -void printIntSizes() -{ - printf("**** sizeof(int): %d\n", sizeof(int)); - printf("**** sizeof(long): %d\n", sizeof(long)); - printf("**** sizeof(long long): %d\n", sizeof(long long)); - - printf("**** sizeof(size_t): %d\n", sizeof(size_t)); - printf("**** sizeof(time_t): %d\n", sizeof(time_t)); - - printf("**** sizeof(wxIntPtr): %d\n", sizeof(wxIntPtr)); - printf("**** sizeof(wxUIntPtr): %d\n", sizeof(wxUIntPtr)); - printf("**** sizeof(wxFileOffset): %d\n", sizeof(wxFileOffset)); - -} -%End - -size_t test_size_t(size_t value); -void printIntSizes(); - - - - diff --git a/src/wacky_ints.sip b/src/wacky_ints.sip new file mode 100644 index 00000000..8f014453 --- /dev/null +++ b/src/wacky_ints.sip @@ -0,0 +1,78 @@ +//-------------------------------------------------------------------------- +// Name: wacky_ints.sip +// +// Purpose: Implements a %MappedType for size_t and others in order to +// deal with different sizeof's on different platforms. They're +// 32bit on some, 64bit on others. +// +// Author: Robin Dunn +// +// Created: 4-March-2016 +// Copyright: (c) 2016 by Total Control Software +// Licence: wxWindows license +//-------------------------------------------------------------------------- + + +%MappedType size_t { + + %TypeHeaderCode + #include + %End + + %ConvertToTypeCode + // Code to test an incoming PyObject for compatibility + if (!sipIsErr) { + if (PyNumber_Check(sipPy)) + return TRUE; + return FALSE; + } + + // Code to convert from a compatible PyObject + #if wxSIZE_T_IS_ULONG + *sipCppPtr = new size_t(PyLong_AsUnsignedLong(sipPy)); + #else + *sipCppPtr = new size_t(PyLong_AsUnsignedLongLong(sipPy)); + #endif + return sipGetState(sipTransferObj); + %End + + %ConvertFromTypeCode + #if wxSIZE_T_IS_ULONG + return PyLong_FromUnsignedLong(*sipCpp); + #else + return PyLong_FromUnsignedLongLong(*sipCpp); + #endif + %End +}; + + +// Used just for testing the MappedType code, it can be removed later +%ModuleCode +size_t test_size_t(size_t value) +{ + size_t local = value; + return local; +} + +void printIntSizes() +{ + printf("**** sizeof(int): %d\n", sizeof(int)); + printf("**** sizeof(long): %d\n", sizeof(long)); + printf("**** sizeof(long long): %d\n", sizeof(long long)); + + printf("**** sizeof(size_t): %d\n", sizeof(size_t)); + printf("**** sizeof(time_t): %d\n", sizeof(time_t)); + + printf("**** sizeof(wxIntPtr): %d\n", sizeof(wxIntPtr)); + printf("**** sizeof(wxUIntPtr): %d\n", sizeof(wxUIntPtr)); + printf("**** sizeof(wxFileOffset): %d\n", sizeof(wxFileOffset)); + +} +%End + +size_t test_size_t(size_t value); +void printIntSizes(); + + + + diff --git a/src/wxpy_api.h b/src/wxpy_api.h index 30b46fcc..5a6ddc96 100644 --- a/src/wxpy_api.h +++ b/src/wxpy_api.h @@ -50,6 +50,7 @@ typedef PyGILState_STATE wxPyBlock_t; typedef unsigned char byte; typedef unsigned char* buffer; +typedef unsigned long ulong; // Convert a wxString to a Python string (actually a PyUnicode object).