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.)
This commit is contained in:
Robin Dunn
2016-03-04 21:05:46 -08:00
parent 2c0ba84396
commit cb701bd77e
9 changed files with 93 additions and 100 deletions

View File

@@ -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',

View File

@@ -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'))

View File

@@ -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)

View File

@@ -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);")

View File

@@ -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);
""")

View File

@@ -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);

View File

@@ -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 <wx/setup.h>
//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();

78
src/wacky_ints.sip Normal file
View File

@@ -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 <wx/setup.h>
%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();

View File

@@ -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).