Skip to content

Commit

Permalink
Replace dangerous [] defaults with None, and explicitly check for None (
Browse files Browse the repository at this point in the history
#467)

* add failing tests

* use None as default instead of []

* remove some commented out code

* some style fixes
  • Loading branch information
aaronayres35 authored Dec 7, 2020
1 parent 130fb11 commit 550561d
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 12 deletions.
20 changes: 8 additions & 12 deletions enable/savage/svg/backends/kiva/renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,13 +166,15 @@ class LinearGradientBrush(AbstractGradientBrush):
""" A Brush representing a linear gradient.
"""
def __init__(self, x1,y1, x2,y2, stops, spreadMethod='pad',
transforms=[], units='userSpaceOnUse'):
transforms=None, units='userSpaceOnUse'):
self.x1 = x1
self.y1 = y1
self.x2 = x2
self.y2 = y2
self.stops = stops
self.spreadMethod = spreadMethod
if transforms is None:
transforms = []
self.transforms = transforms
self.units = units

Expand Down Expand Up @@ -220,7 +222,7 @@ class RadialGradientBrush(AbstractGradientBrush):
""" A Brush representing a radial gradient.
"""
def __init__(self, cx,cy, r, stops, fx=None,fy=None, spreadMethod='pad',
transforms=[], units='userSpaceOnUse'):
transforms=None, units='userSpaceOnUse'):
self.cx = cx
self.cy = cy
self.r = r
Expand All @@ -232,6 +234,8 @@ def __init__(self, cx,cy, r, stops, fx=None,fy=None, spreadMethod='pad',
fy = self.cy
self.fy = fy
self.spreadMethod = spreadMethod
if transforms is None:
transforms = []
self.transforms = transforms
self.units = units

Expand Down Expand Up @@ -332,7 +336,6 @@ def concatTransform(cls, gc, matrix):
def createAffineMatrix(cls, a,b,c,d,x,y):
# FIXME: should we create a 6x1 or 3x3 matrix???
return (a,b,c,d,x,y)
# return affine.affine_from_values(a,b,c,d,x,y)

@classmethod
def createBrush(cls, color_tuple):
Expand All @@ -341,7 +344,6 @@ def createBrush(cls, color_tuple):
@classmethod
def createNativePen(cls, pen):
# fixme: Not really sure what to do here...
#return wx.GraphicsRenderer_GetDefaultRenderer().CreatePen(pen)
return pen

@classmethod
Expand All @@ -350,13 +352,13 @@ def createPen(cls, color_tuple):

@classmethod
def createLinearGradientBrush(cls, x1,y1,x2,y2, stops, spreadMethod='pad',
transforms=[], units='userSpaceOnUse'):
transforms=None, units='userSpaceOnUse'):
return LinearGradientBrush(x1,y1,x2,y2,stops, spreadMethod, transforms,
units)

@classmethod
def createRadialGradientBrush(cls, cx,cy, r, stops, fx=None,fy=None,
spreadMethod='pad', transforms=[],
spreadMethod='pad', transforms=None,
units='userSpaceOnUse'):
return RadialGradientBrush(cx,cy, r, stops, fx,fy, spreadMethod,
transforms, units)
Expand Down Expand Up @@ -541,12 +543,6 @@ def DrawText(cls, gc, text, x, y, brush, anchor='start'):
# text transform matrix to have y going up so the text is rendered
# upright. But since, +y is now *up*, we need to draw at -y.

# fixme: There is something wrong with the text matrix. The following
# commands don't work and I would expect them to.
#text_matrix = affine.affine_from_values(1,0,0,-1,x,-y)
#gc.set_text_matrix(text_matrix)
#gc.show_text_at_point(text, 0, 0)

if anchor != 'start':
tx, ty, tw, th = gc.get_text_extent(text)
if anchor == 'middle':
Expand Down
Empty file.
38 changes: 38 additions & 0 deletions enable/savage/svg/backends/kiva/tests/test_renderer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import unittest

from enable.savage.svg.backends.kiva.renderer import (
LinearGradientBrush,
RadialGradientBrush,
Renderer,
)


class TestRenderer(unittest.TestCase):

def test_linear_gradient_brush(self):
lgb = LinearGradientBrush(1,1,2,2,3)
lgb.transforms.append('a')
self.assertEqual(LinearGradientBrush(1,1,2,2,3).transforms, [])

def test_radial_gradient_brush(self):
rgb = RadialGradientBrush(1,1,2,2,3)
rgb.transforms.append('a')
self.assertEqual(RadialGradientBrush(1,1,2,2,3).transforms, [])

def test_create_linear_gradient_brush(self):
renderer = Renderer()
lgb = renderer.createLinearGradientBrush(1,1,2,2,3)
lgb.transforms.append('a')
self.assertEqual(
renderer.createLinearGradientBrush(1,1,2,2,3).transforms,
[]
)

def test_create_radial_gradient_brush(self):
renderer = Renderer()
rgb = renderer.createRadialGradientBrush(1,1,2,2,3)
rgb.transforms.append('a')
self.assertEqual(
renderer.createRadialGradientBrush(1,1,2,2,3).transforms,
[]
)

0 comments on commit 550561d

Please sign in to comment.