Enable wxArray wrappers to return copy from __getitem__.

This solves problems where an array that is the return value of some method call is indexed immediately and a reference to the array is not held, which could result in garbage values for the indexed item. Currently this is turned on for just GridCellCoordsArray, but others can be switched in the future if needed.
This commit is contained in:
Robin Dunn
2017-10-12 14:33:15 -07:00
parent dc013475cc
commit 66b4a7966d
4 changed files with 64 additions and 13 deletions

View File

@@ -988,17 +988,45 @@ del _{ListClass_pyName}___repr__
def wxArrayWrapperTemplate(ArrayClass, ItemClass, module, itemIsPtr=False):
def wxArrayWrapperTemplate(ArrayClass, ItemClass, module, itemIsPtr=False, getItemCopy=False):
moduleName = module.module
ArrayClass_pyName = removeWxPrefix(ArrayClass)
itemRef = '*' if itemIsPtr else '&'
itemDeref = '' if itemIsPtr else '*'
addrOf = '' if itemIsPtr else '&'
# *** TODO: This can probably be done in a way that is not SIP-specfic.
# *** TODO: This can probably be done in a way that is not SIP-specific.
# Try creating extractor objects from scratch and attach cppMethods to
# them as needed, etc..
if not getItemCopy:
getitemMeth = '''\
{ItemClass}{itemRef} __getitem__(ulong index);
%MethodCode
if (index < sipCpp->GetCount()) {{
sipRes = {addrOf}sipCpp->Item(index);
}}
else {{
wxPyErr_SetString(PyExc_IndexError, "sequence index out of range");
sipError = sipErrorFail;
}}
%End
'''.format(**locals())
else:
getitemMeth = '''\
{ItemClass}* __getitem__(ulong index) /Factory/;
%MethodCode
if (index < sipCpp->GetCount()) {{
sipRes = new {ItemClass}(sipCpp->Item(index));
}}
else {{
wxPyErr_SetString(PyExc_IndexError, "sequence index out of range");
sipError = sipErrorFail;
}}
%End
'''.format(**locals())
return extractors.WigCode('''\
class {ArrayClass}
{{
@@ -1008,16 +1036,7 @@ public:
sipRes = sipCpp->GetCount();
%End
{ItemClass}{itemRef} __getitem__(ulong index);
%MethodCode
if (index < sipCpp->GetCount()) {{
sipRes = {addrOf}sipCpp->Item(index);
}}
else {{
wxPyErr_SetString(PyExc_IndexError, "sequence index out of range");
sipError = sipErrorFail;
}}
%End
{getitemMeth}
int __contains__({ItemClass}{itemRef} obj);
%MethodCode