Fix T71774: SVG import error on specific files

Was happening if the software which wrote SVG skipped decimal part.
This commit is contained in:
Sergey Sharybin 2019-11-22 14:37:49 +01:00
parent 7ff82d87c6
commit ee818b2a28
Notes: blender-bot 2023-02-14 19:05:29 +01:00
Referenced by issue #71774, SVG Import Error
2 changed files with 35 additions and 6 deletions

View File

@ -46,14 +46,14 @@ def check_points_equal(point_a, point_b):
abs(point_a[1] - point_b[1]) < 1e-6)
match_number = r"-?\d+(\.\d+)?([eE][-+]?\d+)?"
match_number_optional_fractional = r"-?\d+(\.\d*)?([eE][-+]?\d+)?"
match_first_comma = r"^\s*(?=,)"
match_comma_pair = r",\s*(?=,)"
match_last_comma = r",\s*$"
re_match_number_optional_fractional = re.compile(match_number_optional_fractional)
match_number_optional_parts = r"(-?\d+(\.\d*)?([eE][-+]?\d+)?)|(-?\.\d+([eE][-+]?\d+)?)"
re_match_number_optional_parts = re.compile(match_number_optional_parts)
array_of_floats_pattern = f"({match_number})|{match_first_comma}|{match_comma_pair}|{match_last_comma}"
array_of_floats_pattern = f"({match_number_optional_parts})|{match_first_comma}|{match_comma_pair}|{match_last_comma}"
re_array_of_floats_pattern = re.compile(array_of_floats_pattern)
def parse_array_of_floats(text):
@ -82,7 +82,7 @@ def read_float(text: str, start_index: int = 0):
return "0", start_index
text_part = text[start_index:]
match = re_match_number_optional_fractional.match(text_part)
match = re_match_number_optional_parts.match(text_part)
if match is None:
raise Exception('Invalid float value near ' + text[start_index:start_index + 10])

View File

@ -29,7 +29,6 @@ else:
from .svg_util import (parse_array_of_floats, read_float, parse_coord,)
import unittest
class ParseArrayOfFloatsTest(unittest.TestCase):
def test_empty(self):
self.assertEqual(parse_array_of_floats(""), [])
@ -78,6 +77,13 @@ class ParseArrayOfFloatsTest(unittest.TestCase):
def test_comma_separated_values_with_decimal_separator(self):
self.assertEqual(parse_array_of_floats("2.75,8.5"), [2.75, 8.5])
def test_missing_decimal(self):
self.assertEqual(parse_array_of_floats(".92"), [0.92])
self.assertEqual(parse_array_of_floats(".92e+1"), [9.2])
self.assertEqual(parse_array_of_floats("-.92"), [-0.92])
self.assertEqual(parse_array_of_floats("-.92e+1"), [-9.2])
class ReadFloatTest(unittest.TestCase):
def test_empty(self):
@ -116,7 +122,7 @@ class ReadFloatTest(unittest.TestCase):
self.assertEqual(endptr, 10)
def test_not_a_number(self):
# TODO(sergey): Make this more concrete.
# TODO(sergey): Make this catch more concrete.
with self.assertRaises(Exception):
read_float("1.2eV", 3)
@ -129,6 +135,29 @@ class ReadFloatTest(unittest.TestCase):
self.assertEqual(value, "2.")
self.assertEqual(endptr, 2)
def test_missing_decimal(self):
value, endptr = read_float(".92", 0)
self.assertEqual(value, ".92")
self.assertEqual(endptr, 3)
value, endptr = read_float("-.92", 0)
self.assertEqual(value, "-.92")
self.assertEqual(endptr, 4)
value, endptr = read_float(".92e+3", 0)
self.assertEqual(value, ".92e+3")
self.assertEqual(endptr, 6)
value, endptr = read_float("-.92e+3", 0)
self.assertEqual(value, "-.92e+3")
self.assertEqual(endptr, 7)
# TODO(sergey): Make these catch more concrete.
with self.assertRaises(Exception):
read_float(".", 0)
with self.assertRaises(Exception):
read_float(".e+1", 0)
class ParseCoordTest(unittest.TestCase):
def test_empty(self):