summaryrefslogtreecommitdiff
path: root/test/ref_count_test.py
diff options
context:
space:
mode:
authorSteve Chaplin <>2012-04-25 12:25:28 +0800
committerSteve Chaplin <>2012-04-25 12:25:28 +0800
commitbeb3e1cb641ef4f5461b0e360d7d8d3c9fbcf4aa (patch)
treef73c9ec164effd43eed5793be08e3ea17392b504 /test/ref_count_test.py
parenta2d54c73e2d50159fef593d112e1c1dccb8b4222 (diff)
downloadpycairo-beb3e1cb641ef4f5461b0e360d7d8d3c9fbcf4aa.tar.gz
Update tests.
Diffstat (limited to 'test/ref_count_test.py')
-rw-r--r--test/ref_count_test.py74
1 files changed, 74 insertions, 0 deletions
diff --git a/test/ref_count_test.py b/test/ref_count_test.py
new file mode 100644
index 0000000..a16be75
--- /dev/null
+++ b/test/ref_count_test.py
@@ -0,0 +1,74 @@
+'''test for reference counting problems.
+
+If a Python object is garbage collected while another object is using its
+data, you will get a segmentation fault.
+'''
+
+import array
+import gc
+import tempfile as tfi
+
+import cairo
+import py.test as test
+
+width, height = 256, 256
+
+def draw(ctx, width, height):
+ "example draw code"
+ ctx.scale(width/1.0, height/1.0)
+
+ pat = cairo.LinearGradient(0.0, 0.0, 0.0, 1.0)
+ pat.add_color_stop_rgba(1, 0, 0, 0, 1)
+ pat.add_color_stop_rgba(0, 1, 1, 1, 1)
+ ctx.rectangle(0,0,1,1)
+ ctx.set_source(pat)
+ ctx.fill()
+
+
+def test_create_for_stream():
+ def run_test(surface_method, suffix):
+ _, fo = tfi.mkstemp(prefix='pycairo_', suffix=suffix)
+ surface = surface_method(fo, width, height)
+ ctx = cairo.Context(surface)
+
+ del fo # test that 'fo' is referenced to keep it alive
+ gc.collect()
+
+ draw(ctx, width, height)
+ ctx.show_page()
+ surface.finish()
+
+ if cairo.HAS_PDF_SURFACE:
+ run_test(cairo.PDFSurface, '.pdf')
+ if cairo.HAS_PS_SURFACE:
+ run_test(cairo.PSSurface, '.ps')
+ if cairo.HAS_SVG_SURFACE:
+ run_test(cairo.SVGSurface, '.svg')
+
+
+def test_get_data():
+ surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, width, height)
+
+ memView = surface.get_data()
+
+ del surface # test that 'surface' is referenced to keep it alive
+ gc.collect()
+
+ memView[0] = b'\xFF'
+ data = memView.tobytes()
+
+
+def test_create_for_data():
+ data = array.array('B', [0] * width * height * 4)
+
+ surface = cairo.ImageSurface.create_for_data(data, cairo.FORMAT_ARGB32,
+ width, height)
+ ctx = cairo.Context(surface)
+
+ del data # test that 'data' is referenced to keep it alive
+ gc.collect()
+
+ draw(ctx, width, height)
+
+ _, fo = tfi.mkstemp(prefix='pycairo_', suffix='.png')
+ surface.write_to_png(fo)