Commits

Anonymous committed a2b9362

Fix [ 3606028 ] ``assert`` is skipped with ``python -O``.

Also, raise ValueError with list of valid units if
length_or_percentage_or_unitless() fails.

  • Participants
  • Parent commits 4e1d3ff

Comments (0)

Files changed (3)

docutils/HISTORY.txt

 
   - Fix [ 3601607 ] node.__repr__() must return `str` instance.
 
+* docutils/parsers/rst/directives/__init__.py
+
+  - Fix [ 3606028 ] ``assert`` is skipped with ``python -O``.
+
 * docutils/parsers/rst/directives/images.py
 
   - Apply [ 3599485 ] node source/line information for sphinx translation.

docutils/docutils/parsers/rst/directives/__init__.py

     """
     match = re.match(r'^([0-9.]+) *(%s)$' % '|'.join(units), argument)
     try:
-        assert match is not None
         float(match.group(1))
-    except (AssertionError, ValueError):
+    except (AttributeError, ValueError):
         raise ValueError(
             'not a positive measure of one of the following units:\n%s'
             % ' '.join(['"%s"' % i for i in units]))
     try:
         return get_measure(argument, length_units + ['%'])
     except ValueError:
-        return get_measure(argument, ['']) + default
+        try:
+            return get_measure(argument, ['']) + default
+        except ValueError:
+            # raise ValueError with list of valid units:
+            return get_measure(argument, length_units + ['%'])
 
 def class_option(argument):
     """

docutils/test/test_parsers/test_rst/test_directives/test_images.py

 """],
 ["""\
 .. image:: picture.png
+   :width: 20mc
+""",
+"""\
+<document source="test data">
+    <system_message level="3" line="1" source="test data" type="ERROR">
+        <paragraph>
+            Error in "image" directive:
+            invalid option value: (option: "width"; value: \'20mc\')
+            not a positive measure of one of the following units:
+            "em" "ex" "px" "in" "cm" "mm" "pt" "pc" "%".
+        <literal_block xml:space="preserve">
+            .. image:: picture.png
+               :width: 20mc
+"""],
+["""\
+.. image:: picture.png
    :height: 100
    :width: 200
    :scale: 50