Skip to content

Commit 0b8eca4

Browse files
Raise exception if invalid iterator iterated
This required an out typemap for __next__ that checks for end of iteration so the normal exception handler can be used to check for invalidated iterator.
1 parent ea7689a commit 0b8eca4

File tree

7 files changed

+115
-46
lines changed

7 files changed

+115
-46
lines changed

src/interface/shared/data_iterator.i

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,13 @@ data it points to."
4040
// Creating a new iterator keeps a reference to the current one
4141
KEEP_REFERENCE(container_type##_iterator*)
4242
// Detect end of iteration
43-
%exception container_type##_iterator::__next__ %{
44-
$action
45-
if (!result) {
43+
%typemap(out) Exiv2::datum_type* __next__ {
44+
if (!$1) {
4645
PyErr_SetNone(PyExc_StopIteration);
4746
SWIG_fail;
4847
}
49-
%}
48+
$typemap(out, Exiv2::datum_type*)
49+
}
5050
%inline %{
5151
// Base class implements all methods except dereferencing
5252
class container_type##_iterator {
@@ -63,6 +63,9 @@ public:
6363
}
6464
container_type##_iterator* __iter__() { return this; }
6565
Exiv2::datum_type* __next__() {
66+
if (invalidated)
67+
throw std::runtime_error(
68+
"container_type changed size during iteration");
6669
if (ptr == end)
6770
return NULL;
6871
Exiv2::datum_type* result = &(*ptr);

src/swig-0_27_7/exif_wrap.cxx

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4395,6 +4395,9 @@ class ExifData_iterator {
43954395
}
43964396
ExifData_iterator* __iter__() { return this; }
43974397
Exiv2::Exifdatum* __next__() {
4398+
if (invalidated)
4399+
throw std::runtime_error(
4400+
"ExifData changed size during iteration");
43984401
if (ptr == end)
43994402
return NULL;
44004403
Exiv2::Exifdatum* result = &(*ptr);
@@ -5679,14 +5682,22 @@ SWIGINTERN PyObject *_wrap_ExifData_iterator___next__(PyObject *self, PyObject *
56795682
SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "ExifData_iterator___next__" "', argument " "1"" of type '" "ExifData_iterator *""'");
56805683
}
56815684
arg1 = reinterpret_cast< ExifData_iterator * >(argp1);
5682-
5683-
result = (Exiv2::Exifdatum *)(arg1)->__next__();
5684-
if (!result) {
5685-
PyErr_SetNone(PyExc_StopIteration);
5686-
SWIG_fail;
5685+
{
5686+
try {
5687+
result = (Exiv2::Exifdatum *)(arg1)->__next__();
5688+
}
5689+
catch(std::exception const& e) {
5690+
_set_python_exception();
5691+
SWIG_fail;
5692+
}
5693+
}
5694+
{
5695+
if (!result) {
5696+
PyErr_SetNone(PyExc_StopIteration);
5697+
SWIG_fail;
5698+
}
5699+
resultobj = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIGTYPE_p_Exiv2__Exifdatum, 0 | 0 );
56875700
}
5688-
5689-
resultobj = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIGTYPE_p_Exiv2__Exifdatum, 0 | 0 );
56905701
return resultobj;
56915702
fail:
56925703
return NULL;

src/swig-0_27_7/iptc_wrap.cxx

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4384,6 +4384,9 @@ class IptcData_iterator {
43844384
}
43854385
IptcData_iterator* __iter__() { return this; }
43864386
Exiv2::Iptcdatum* __next__() {
4387+
if (invalidated)
4388+
throw std::runtime_error(
4389+
"IptcData changed size during iteration");
43874390
if (ptr == end)
43884391
return NULL;
43894392
Exiv2::Iptcdatum* result = &(*ptr);
@@ -5615,14 +5618,22 @@ SWIGINTERN PyObject *_wrap_IptcData_iterator___next__(PyObject *self, PyObject *
56155618
SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "IptcData_iterator___next__" "', argument " "1"" of type '" "IptcData_iterator *""'");
56165619
}
56175620
arg1 = reinterpret_cast< IptcData_iterator * >(argp1);
5618-
5619-
result = (Exiv2::Iptcdatum *)(arg1)->__next__();
5620-
if (!result) {
5621-
PyErr_SetNone(PyExc_StopIteration);
5622-
SWIG_fail;
5621+
{
5622+
try {
5623+
result = (Exiv2::Iptcdatum *)(arg1)->__next__();
5624+
}
5625+
catch(std::exception const& e) {
5626+
_set_python_exception();
5627+
SWIG_fail;
5628+
}
5629+
}
5630+
{
5631+
if (!result) {
5632+
PyErr_SetNone(PyExc_StopIteration);
5633+
SWIG_fail;
5634+
}
5635+
resultobj = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIGTYPE_p_Exiv2__Iptcdatum, 0 | 0 );
56235636
}
5624-
5625-
resultobj = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIGTYPE_p_Exiv2__Iptcdatum, 0 | 0 );
56265637
return resultobj;
56275638
fail:
56285639
return NULL;

src/swig-0_27_7/xmp_wrap.cxx

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4386,6 +4386,9 @@ class XmpData_iterator {
43864386
}
43874387
XmpData_iterator* __iter__() { return this; }
43884388
Exiv2::Xmpdatum* __next__() {
4389+
if (invalidated)
4390+
throw std::runtime_error(
4391+
"XmpData changed size during iteration");
43894392
if (ptr == end)
43904393
return NULL;
43914394
Exiv2::Xmpdatum* result = &(*ptr);
@@ -5615,14 +5618,22 @@ SWIGINTERN PyObject *_wrap_XmpData_iterator___next__(PyObject *self, PyObject *a
56155618
SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "XmpData_iterator___next__" "', argument " "1"" of type '" "XmpData_iterator *""'");
56165619
}
56175620
arg1 = reinterpret_cast< XmpData_iterator * >(argp1);
5618-
5619-
result = (Exiv2::Xmpdatum *)(arg1)->__next__();
5620-
if (!result) {
5621-
PyErr_SetNone(PyExc_StopIteration);
5622-
SWIG_fail;
5621+
{
5622+
try {
5623+
result = (Exiv2::Xmpdatum *)(arg1)->__next__();
5624+
}
5625+
catch(std::exception const& e) {
5626+
_set_python_exception();
5627+
SWIG_fail;
5628+
}
5629+
}
5630+
{
5631+
if (!result) {
5632+
PyErr_SetNone(PyExc_StopIteration);
5633+
SWIG_fail;
5634+
}
5635+
resultobj = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIGTYPE_p_Exiv2__Xmpdatum, 0 | 0 );
56235636
}
5624-
5625-
resultobj = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIGTYPE_p_Exiv2__Xmpdatum, 0 | 0 );
56265637
return resultobj;
56275638
fail:
56285639
return NULL;

src/swig-0_28_5/exif_wrap.cxx

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4395,6 +4395,9 @@ class ExifData_iterator {
43954395
}
43964396
ExifData_iterator* __iter__() { return this; }
43974397
Exiv2::Exifdatum* __next__() {
4398+
if (invalidated)
4399+
throw std::runtime_error(
4400+
"ExifData changed size during iteration");
43984401
if (ptr == end)
43994402
return NULL;
44004403
Exiv2::Exifdatum* result = &(*ptr);
@@ -5715,14 +5718,22 @@ SWIGINTERN PyObject *_wrap_ExifData_iterator___next__(PyObject *self, PyObject *
57155718
SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "ExifData_iterator___next__" "', argument " "1"" of type '" "ExifData_iterator *""'");
57165719
}
57175720
arg1 = reinterpret_cast< ExifData_iterator * >(argp1);
5718-
5719-
result = (Exiv2::Exifdatum *)(arg1)->__next__();
5720-
if (!result) {
5721-
PyErr_SetNone(PyExc_StopIteration);
5722-
SWIG_fail;
5721+
{
5722+
try {
5723+
result = (Exiv2::Exifdatum *)(arg1)->__next__();
5724+
}
5725+
catch(std::exception const& e) {
5726+
_set_python_exception();
5727+
SWIG_fail;
5728+
}
5729+
}
5730+
{
5731+
if (!result) {
5732+
PyErr_SetNone(PyExc_StopIteration);
5733+
SWIG_fail;
5734+
}
5735+
resultobj = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIGTYPE_p_Exiv2__Exifdatum, 0 | 0 );
57235736
}
5724-
5725-
resultobj = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIGTYPE_p_Exiv2__Exifdatum, 0 | 0 );
57265737
return resultobj;
57275738
fail:
57285739
return NULL;

src/swig-0_28_5/iptc_wrap.cxx

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4384,6 +4384,9 @@ class IptcData_iterator {
43844384
}
43854385
IptcData_iterator* __iter__() { return this; }
43864386
Exiv2::Iptcdatum* __next__() {
4387+
if (invalidated)
4388+
throw std::runtime_error(
4389+
"IptcData changed size during iteration");
43874390
if (ptr == end)
43884391
return NULL;
43894392
Exiv2::Iptcdatum* result = &(*ptr);
@@ -5658,14 +5661,22 @@ SWIGINTERN PyObject *_wrap_IptcData_iterator___next__(PyObject *self, PyObject *
56585661
SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "IptcData_iterator___next__" "', argument " "1"" of type '" "IptcData_iterator *""'");
56595662
}
56605663
arg1 = reinterpret_cast< IptcData_iterator * >(argp1);
5661-
5662-
result = (Exiv2::Iptcdatum *)(arg1)->__next__();
5663-
if (!result) {
5664-
PyErr_SetNone(PyExc_StopIteration);
5665-
SWIG_fail;
5664+
{
5665+
try {
5666+
result = (Exiv2::Iptcdatum *)(arg1)->__next__();
5667+
}
5668+
catch(std::exception const& e) {
5669+
_set_python_exception();
5670+
SWIG_fail;
5671+
}
5672+
}
5673+
{
5674+
if (!result) {
5675+
PyErr_SetNone(PyExc_StopIteration);
5676+
SWIG_fail;
5677+
}
5678+
resultobj = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIGTYPE_p_Exiv2__Iptcdatum, 0 | 0 );
56665679
}
5667-
5668-
resultobj = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIGTYPE_p_Exiv2__Iptcdatum, 0 | 0 );
56695680
return resultobj;
56705681
fail:
56715682
return NULL;

src/swig-0_28_5/xmp_wrap.cxx

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4386,6 +4386,9 @@ class XmpData_iterator {
43864386
}
43874387
XmpData_iterator* __iter__() { return this; }
43884388
Exiv2::Xmpdatum* __next__() {
4389+
if (invalidated)
4390+
throw std::runtime_error(
4391+
"XmpData changed size during iteration");
43894392
if (ptr == end)
43904393
return NULL;
43914394
Exiv2::Xmpdatum* result = &(*ptr);
@@ -5658,14 +5661,22 @@ SWIGINTERN PyObject *_wrap_XmpData_iterator___next__(PyObject *self, PyObject *a
56585661
SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "XmpData_iterator___next__" "', argument " "1"" of type '" "XmpData_iterator *""'");
56595662
}
56605663
arg1 = reinterpret_cast< XmpData_iterator * >(argp1);
5661-
5662-
result = (Exiv2::Xmpdatum *)(arg1)->__next__();
5663-
if (!result) {
5664-
PyErr_SetNone(PyExc_StopIteration);
5665-
SWIG_fail;
5664+
{
5665+
try {
5666+
result = (Exiv2::Xmpdatum *)(arg1)->__next__();
5667+
}
5668+
catch(std::exception const& e) {
5669+
_set_python_exception();
5670+
SWIG_fail;
5671+
}
5672+
}
5673+
{
5674+
if (!result) {
5675+
PyErr_SetNone(PyExc_StopIteration);
5676+
SWIG_fail;
5677+
}
5678+
resultobj = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIGTYPE_p_Exiv2__Xmpdatum, 0 | 0 );
56665679
}
5667-
5668-
resultobj = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIGTYPE_p_Exiv2__Xmpdatum, 0 | 0 );
56695680
return resultobj;
56705681
fail:
56715682
return NULL;

0 commit comments

Comments
 (0)