#586: exceptions on invalid element access

pull/13383/head
James Bowman 14 years ago
parent 3a53d8667a
commit 6b4047eb46
  1. 13
      modules/python/cv.cpp
  2. 37
      tests/python/test.py

@ -151,7 +151,7 @@ static void translate_error_to_exception(void)
} }
#define ERRCHK do { if (cvGetErrStatus() != 0) { translate_error_to_exception(); return NULL; } } while (0) #define ERRCHK do { if (cvGetErrStatus() != 0) { translate_error_to_exception(); return NULL; } } while (0)
#define ERRWRAP(F) \ #define ERRWRAPN(F, N) \
do { \ do { \
try \ try \
{ \ { \
@ -160,10 +160,11 @@ static void translate_error_to_exception(void)
catch (const cv::Exception &e) \ catch (const cv::Exception &e) \
{ \ { \
PyErr_SetString(opencv_error, e.err.c_str()); \ PyErr_SetString(opencv_error, e.err.c_str()); \
return NULL; \ return N; \
} \ } \
ERRCHK; \ ERRCHK; \
} while(0) } while(0)
#define ERRWRAP(F) ERRWRAPN(F, NULL) // for most functions, exception -> NULL return
/************************************************************************/ /************************************************************************/
@ -3139,16 +3140,16 @@ static int cvarr_SetItem(PyObject *o, PyObject *key, PyObject *v)
} }
switch (dd.count) { switch (dd.count) {
case 1: case 1:
cvSet1D(cva, dd.i[0], s); ERRWRAPN(cvSet1D(cva, dd.i[0], s), -1);
break; break;
case 2: case 2:
cvSet2D(cva, dd.i[0], dd.i[1], s); ERRWRAPN(cvSet2D(cva, dd.i[0], dd.i[1], s), -1);
break; break;
case 3: case 3:
cvSet3D(cva, dd.i[0], dd.i[1], dd.i[2], s); ERRWRAPN(cvSet3D(cva, dd.i[0], dd.i[1], dd.i[2], s), -1);
break; break;
default: default:
cvSetND(cva, dd.i, s); ERRWRAPN(cvSetND(cva, dd.i, s), -1);
// XXX - OpenCV bug? - seems as if an error in cvSetND does not set error status? // XXX - OpenCV bug? - seems as if an error in cvSetND does not set error status?
break; break;
} }

@ -968,6 +968,43 @@ class AreaTests(OpenCVTests):
else: else:
print "SKIPPING test_numpy - numpy support not built" print "SKIPPING test_numpy - numpy support not built"
def test_boundscatch(self):
l2 = cv.CreateMat(256, 1, cv.CV_8U)
l2[0,0] # should be OK
self.assertRaises(cv.error, lambda: l2[1,1])
l2[0] # should be OK
self.assertRaises(cv.error, lambda: l2[299])
for n in range(1, 8):
l = cv.CreateMatND([2] * n, cv.CV_8U)
l[0] # should be OK
self.assertRaises(cv.error, lambda: l[999])
tup0 = (0,) * n
l[tup0] # should be OK
tup2 = (2,) * n
self.assertRaises(cv.error, lambda: l[tup2])
def test_stereo(self):
bm = cv.CreateStereoBMState()
def illegal_delete():
bm = cv.CreateStereoBMState()
del bm.preFilterType
def illegal_assign():
bm = cv.CreateStereoBMState()
bm.preFilterType = "foo"
self.assertRaises(TypeError, illegal_delete)
self.assertRaises(TypeError, illegal_assign)
left = self.get_sample("samples/c/lena.jpg", 0)
right = self.get_sample("samples/c/lena.jpg", 0)
disparity = cv.CreateMat(512, 512, cv.CV_16SC1)
cv.FindStereoCorrespondenceBM(left, right, disparity, bm)
gc = cv.CreateStereoGCState(16, 2)
left_disparity = cv.CreateMat(512, 512, cv.CV_16SC1)
right_disparity = cv.CreateMat(512, 512, cv.CV_16SC1)
def test_stereo(self): def test_stereo(self):
bm = cv.CreateStereoBMState() bm = cv.CreateStereoBMState()
def illegal_delete(): def illegal_delete():

Loading…
Cancel
Save