Welcome to mirror list, hosted at ThFree Co, Russian Federation.

git.blender.org/blender-addons.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSergey Sharybin <sergey.vfx@gmail.com>2019-11-22 16:37:49 +0300
committerSergey Sharybin <sergey.vfx@gmail.com>2019-12-03 12:50:28 +0300
commite694c1bdc102daee0719ce407bc24bcf0e038ba3 (patch)
treeb893de6b3d4f65c803edf82725e363595cd2616b
parent64dcfc8dc1c43a77f16c848219cac5e396368093 (diff)
Fix T71774: SVG import error on specific files
Was happening if the software which wrote SVG skipped decimal part.
-rw-r--r--io_curve_svg/svg_util.py8
-rwxr-xr-xio_curve_svg/svg_util_test.py33
2 files changed, 35 insertions, 6 deletions
diff --git a/io_curve_svg/svg_util.py b/io_curve_svg/svg_util.py
index bd744df5..c544f91d 100644
--- a/io_curve_svg/svg_util.py
+++ b/io_curve_svg/svg_util.py
@@ -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])
diff --git a/io_curve_svg/svg_util_test.py b/io_curve_svg/svg_util_test.py
index de1d9823..46540085 100755
--- a/io_curve_svg/svg_util_test.py
+++ b/io_curve_svg/svg_util_test.py
@@ -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):