Skip to content

Invalid comparison between string and number for viewbox determination in eggbot.py #11

@oskay

Description

@oskay

Reported by Matthew Beckler as https://code.google.com/p/eggbotcode/issues/detail?id=74

Here is the code section:

# Viewbox handling
# Also ignores the preserveAspectRatio attribute
viewbox = self.svg.get( 'viewBox' )
if viewbox:
    vinfo = viewbox.strip().replace( ',', ' ' ).split( ' ' )
    if ( vinfo[2] != 0 ) and ( vinfo[3] != 0 ):   # <-------------------------- this line here ------------<<
        sx = self.svgWidth / float( vinfo[2] )
        sy = self.svgHeight / float( vinfo[3] )
        self.svgTransform = parseTransform( 'scale(%f,%f) translate(%f,%f)' % (sx, sy, -float( vinfo[0] ), -float( vinfo[1] ) ) )

The issue lies with the "if ( vinfo[2] != 0 ) and ( vinfo[3] != 0 ):" line. vinfo is a list of strings, and a comparison is being done to see if two of these strings are not equal to the numeric value 0. Even if the string value in vinfo[2] or vinfo[3] is "0", that is never equal to the numeric value 0 in an inequality check like this. That is, (vinfo[2] != 0) always evaluates to True, because every string is not equal to 0.

I think we should probably wrap vinfo[2] and vinfo[3] in calls to float, making that line instead be:
if ( float(vinfo[2]) != 0 ) and ( float(vinfo[3]) != 0 ):

Again, this probably never causes trouble but is an easy fix. Here's a very small patch for it:

--- eggbot.py   2014-03-21 10:05:49.222832100 -0500
+++ eggbot_fix.py       2014-03-21 10:06:27.758685300 -0500
@@ -526,7 +526,7 @@
                viewbox = self.svg.get( 'viewBox' )
                if viewbox:
                        vinfo = viewbox.strip().replace( ',', ' ' ).split( ' ' )
-                       if ( vinfo[2] != 0 ) and ( vinfo[3] != 0 ):
+                       if ( float(vinfo[2]) != 0 ) and ( float(vinfo[3]) != 0 ):
                                sx = self.svgWidth / float( vinfo[2] )
                                sy = self.svgHeight / float( vinfo[3] )
                                self.svgTransform = parseTransform( 'scale(%f,%f) translate(%f,%f)' % (sx, sy, -float( vinfo[0] ), -float( vinfo[1] ) ) )

Alternatively, we could just use map to cast all the entries of vinfo into floats right away, and remove the float calls from the lines afterward:

--- eggbot.py   2014-03-21 10:05:49.222832100 -0500
+++ eggbot_fix_2.py     2014-03-21 10:14:22.526157300 -0500
@@ -525,11 +525,11 @@
                # Also ignores the preserveAspectRatio attribute
                viewbox = self.svg.get( 'viewBox' )
                if viewbox:
-                       vinfo = viewbox.strip().replace( ',', ' ' ).split( ' ' )
+                       vinfo = map(float, viewbox.strip().replace( ',', ' ' ).split( ' ' )) # convert all items to float
                        if ( vinfo[2] != 0 ) and ( vinfo[3] != 0 ):
-                               sx = self.svgWidth / float( vinfo[2] )
-                               sy = self.svgHeight / float( vinfo[3] )
-                               self.svgTransform = parseTransform( 'scale(%f,%f) translate(%f,%f)' % (sx, sy, -float( vinfo[0] ), -float( vinfo[1] ) ) )
+                               sx = self.svgWidth / vinfo[2]
+                               sy = self.svgHeight / vinfo[3]
+                               self.svgTransform = parseTransform( 'scale(%f,%f) translate(%f,%f)' % (sx, sy, -vinfo[0], -vinfo[1]) )

                self.ServoSetup()

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions