Skip to content

Commit eac6ce1

Browse files
committed
Merge pull request matplotlib#6232 from tacaswell/mnt_raster_context
MNT: use stdlib tools in allow_rasterization
2 parents 6f27f95 + 382fe96 commit eac6ce1

File tree

1 file changed

+16
-16
lines changed

1 file changed

+16
-16
lines changed

lib/matplotlib/artist.py

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
from .transforms import (Bbox, IdentityTransform, TransformedBbox,
1616
TransformedPatchPath, TransformedPath, Transform)
1717
from .path import Path
18-
18+
from functools import wraps
19+
from contextlib import contextmanager
1920
# Note, matplotlib artists use the doc strings for set and get
2021
# methods to enable the introspection methods of setp and getp. Every
2122
# set_* method should have a docstring containing the line
@@ -42,31 +43,30 @@ def allow_rasterization(draw):
4243
other setup function calls, such as starting and flushing a mixed-mode
4344
renderer.
4445
"""
45-
def before(artist, renderer):
46+
@contextmanager
47+
def with_rasterized(artist, renderer):
48+
4649
if artist.get_rasterized():
4750
renderer.start_rasterizing()
4851

4952
if artist.get_agg_filter() is not None:
5053
renderer.start_filter()
5154

52-
def after(artist, renderer):
53-
54-
if artist.get_agg_filter() is not None:
55-
renderer.stop_filter(artist.get_agg_filter())
55+
try:
56+
yield
57+
finally:
58+
if artist.get_agg_filter() is not None:
59+
renderer.stop_filter(artist.get_agg_filter())
5660

57-
if artist.get_rasterized():
58-
renderer.stop_rasterizing()
61+
if artist.get_rasterized():
62+
renderer.stop_rasterizing()
5963

6064
# the axes class has a second argument inframe for its draw method.
65+
@wraps(draw)
6166
def draw_wrapper(artist, renderer, *args, **kwargs):
62-
before(artist, renderer)
63-
draw(artist, renderer, *args, **kwargs)
64-
after(artist, renderer)
65-
66-
# "safe wrapping" to exactly replicate anything we haven't overridden above
67-
draw_wrapper.__name__ = draw.__name__
68-
draw_wrapper.__dict__ = draw.__dict__
69-
draw_wrapper.__doc__ = draw.__doc__
67+
with with_rasterized(artist, renderer):
68+
return draw(artist, renderer, *args, **kwargs)
69+
7070
draw_wrapper._supports_rasterization = True
7171
return draw_wrapper
7272

0 commit comments

Comments
 (0)