Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions kiva/agg/src/graphics_context.i
Original file line number Diff line number Diff line change
Expand Up @@ -722,11 +722,34 @@ namespace kiva {

void clear_clip_path();
void clear(agg24::rgba& value=_clear_color);

// Exception handler for cell block overflow
%exception {
try
{
$action
}
catch (const std::overflow_error& exn)
{
PyErr_SetString(PyExc_OverflowError, exn.what());

// XXX: This is a hacky, but `arg1` is the graphics context
// object in the SWIG-generated code.
// The path needs to be cleared by drawing calls and this
// guarantees that it is even if an exception is thrown.
(arg1)->begin_path();

return NULL;
}
}

void stroke_path();
void fill_path();
void eof_fill_path();
void draw_path(draw_mode_e mode=FILL_STROKE);
Comment thread
jwiggins marked this conversation as resolved.

%exception; // clear exception handlers

void draw_rect(double rect[4],
draw_mode_e mode=FILL_STROKE);

Expand Down
4 changes: 2 additions & 2 deletions kiva/tests/drawing_tester.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,8 @@ def save_and_return_dpi(self):

filename = "{0}.png".format(self.filename)
self.gc.save(filename)
image = Image.open(filename)
dpi = image.info['dpi']
with Image.open(filename) as image:
dpi = image.info['dpi']
Comment on lines +187 to +188
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Drive-by fix. This caused a ResourceWarning when I was running the tests (which I hadn't seen before).

return dpi[0]

@contextlib.contextmanager
Expand Down
22 changes: 22 additions & 0 deletions kiva/tests/test_agg_drawing.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,25 @@ def test_ipython_repr_png(self):
with open(filename, 'wb') as fp:
fp.write(stream)
self.assertImageSavedWithContent(filename)

def test_rasterizer_cell_overflow(self):
def genpoints(num):
arr = np.empty((num, 2))
arr[::2, 0] = np.linspace(0, 99, num=arr[::2, 0].shape[0])
arr[::2, 1] = 0.0
arr[1::2, 0] = np.linspace(1, 100, num=arr[1::2, 0].shape[0])
arr[1::2, 1] = 100.0
return arr

# This many points definitely generates more than 2^22 cells
count = 10000
points = genpoints(count)
self.gc.lines(points)
with self.assertRaises(OverflowError):
self.gc.stroke_path()

# This many points is OK
count = 9000
points = genpoints(count)
self.gc.lines(points)
self.gc.stroke_path()