call sanitizeOptions() in start() to prevent a third-party breakage

This commit is contained in:
Eduard Braun 2016-11-27 18:38:29 +01:00
parent c45f050fe6
commit 26abe21486
2 changed files with 40 additions and 8 deletions

View file

@ -3523,10 +3523,10 @@ def scourString(in_string, options=None):
# input is a filename # input is a filename
# returns the minidom doc representation of the SVG # returns the minidom doc representation of the SVG
def scourXmlFile(filename, options=None): def scourXmlFile(filename, options=None):
# we need to set infilename (otherwise relative references in the SVG won't work) # sanitize options (take missing attributes from defaults, discard unknown attributes)
if options is None: options = sanitizeOptions(options)
options = generateDefaultOptions() # we need to make sure infilename is set correctly (otherwise relative references in the SVG won't work)
options.infilename = filename options.ensure_value("infilename", filename)
# open the file and scour it # open the file and scour it
with open(filename, "rb") as f: with open(filename, "rb") as f:
@ -3783,6 +3783,8 @@ def getReport():
def start(options, input, output): def start(options, input, output):
# sanitize options (take missing attributes from defaults, discard unknown attributes)
options = sanitizeOptions(options)
start = walltime() start = walltime()

View file

@ -30,7 +30,7 @@ import unittest
import six import six
from six.moves import map, range from six.moves import map, range
from scour.scour import makeWellFormed, parse_args, scourString, scourXmlFile, run from scour.scour import makeWellFormed, parse_args, scourString, scourXmlFile, start, run
from scour.svg_regex import svg_parser from scour.svg_regex import svg_parser
from scour import __version__ from scour import __version__
@ -58,15 +58,45 @@ class ScourOptions:
class EmptyOptions(unittest.TestCase): class EmptyOptions(unittest.TestCase):
def runTest(self): MINIMAL_SVG = '<?xml version="1.0" encoding="UTF-8"?>\n' \
'<svg xmlns="http://www.w3.org/2000/svg"/>\n'
def test_scourString(self):
options = ScourOptions options = ScourOptions
try: try:
scourXmlFile('unittests/ids-to-strip.svg', options) scourString(self.MINIMAL_SVG, options)
fail = False fail = False
except: except:
fail = True fail = True
self.assertEqual(fail, False, self.assertEqual(fail, False,
'Exception when calling Scour with empty options object') 'Exception when calling "scourString" with empty options object')
def test_scourXmlFile(self):
options = ScourOptions
try:
scourXmlFile('unittests/minimal.svg', options)
fail = False
except:
fail = True
self.assertEqual(fail, False,
'Exception when calling "scourXmlFile" with empty options object')
def test_start(self):
options = ScourOptions
input = open('unittests/minimal.svg', 'rb')
output = open('testscour_temp.svg', 'wb')
stdout_temp = sys.stdout
sys.stdout = None
try:
start(options, input, output)
fail = False
except:
fail = True
sys.stdout = stdout_temp
self.assertEqual(fail, False,
'Exception when calling "start" with empty options object')
class InvalidOptions(unittest.TestCase): class InvalidOptions(unittest.TestCase):