Fix: bt2: _trim_docstring(): docstring can have 0 or 1 line
authorPhilippe Proulx <eeppeliteloop@gmail.com>
Thu, 10 Sep 2020 20:47:52 +0000 (16:47 -0400)
committerSimon Marchi <simon.marchi@efficios.com>
Wed, 9 Dec 2020 17:10:16 +0000 (12:10 -0500)
It is possible that a user component class's docstring has no lines or a
single one, for example:

    # no line
    class MySink(bt2._UserSinkComponent):
        """"""

        def _user_consume(self):
            pass

    # single line
    class MySink(bt2._UserSinkComponent):
        """The single line"""

        def _user_consume(self):
            pass

The previous version of _trim_docstring() expects this format:

    class MySink(bt2._UserSinkComponent):
        """
        My sink's description

        Dolore officia ex et aliquip eiusmod enim pariatur reprehenderit
        ad adipisicing non occaecat ullamco aliquip laborum duis
        proident ex duis.

        Irure commodo proident esse non pariatur in aute cillum id aute.
        """

        def _user_consume(self):
            pass

In _trim_docstring(), accept no lines or a single one.

Adding new tests to `test_component_class.py` to validate this
behaviour. The

    # fmt: off
    """
    """
    # fmt: on

docstring has off/on formatting markers as Black 20.8b1 transforms this
into the non-equivalent

    """"""

(which is another test now).

In addition, reformat Python files with Black v20.8b1, so that the
pylint CI job passes.

Signed-off-by: Philippe Proulx <eeppeliteloop@gmail.com>
Change-Id: Ia12b0e9bfd4d1e1aaa86f0c8c207c3c1535f5c3e
Reviewed-on: https://review.lttng.org/c/babeltrace/+/4072
Reviewed-on: https://review.lttng.org/c/babeltrace/+/4525
CI-Build: Simon Marchi <simon.marchi@efficios.com>
Tested-by: jenkins <jenkins@lttng.org>
src/bindings/python/bt2/bt2/__init__.py
src/bindings/python/bt2/bt2/component.py
src/bindings/python/bt2/bt2/error.py
src/bindings/python/bt2/bt2/message_iterator.py
src/bindings/python/bt2/bt2/trace_class.py
tests/bindings/python/bt2/test_component_class.py
tests/bindings/python/bt2/test_event.py
tests/bindings/python/bt2/test_message.py

index f1c0954b273b5c135d6695493bc20dc60bf9f738..7068f2409474c02cbf1d62e04de6b160e8f0470a 100644 (file)
@@ -250,10 +250,10 @@ class _MemoryError(_Error):
 
 
 class UnknownObject(Exception):
-    '''
+    """
     Raised when a component class handles a query for an object it doesn't
     know about.
-    '''
+    """
 
     pass
 
index f4aa8e8003104b5448937d7ddcfcc88b064be260..df62ae639bc3022c2d06db25c91f4404794eea0a 100644 (file)
@@ -345,17 +345,22 @@ def _create_component_class_from_const_ptr_and_get_ref(ptr, comp_cls_type):
 
 def _trim_docstring(docstring):
     lines = docstring.expandtabs().splitlines()
+
+    if len(lines) == 0:
+        return ''
+
     indent = sys.maxsize
 
-    for line in lines[1:]:
-        stripped = line.lstrip()
+    if len(lines) > 1:
+        for line in lines[1:]:
+            stripped = line.lstrip()
 
-        if stripped:
-            indent = min(indent, len(line) - len(stripped))
+            if stripped:
+                indent = min(indent, len(line) - len(stripped))
 
     trimmed = [lines[0].strip()]
 
-    if indent < sys.maxsize:
+    if indent < sys.maxsize and len(lines) > 1:
         for line in lines[1:]:
             trimmed.append(line[indent:].rstrip())
 
index 07d24714be423651ed3ab4d1a1205d9fc8ca34ba..86303d47d4d3f2c0fe1e40a1716dc3b7aedfe08a 100644 (file)
@@ -54,11 +54,11 @@ class _ComponentErrorCause(_ErrorCause):
         self._component_name = native_bt.error_cause_component_actor_get_component_name(
             ptr
         )
-        self._component_class_type = native_bt.error_cause_component_actor_get_component_class_type(
-            ptr
+        self._component_class_type = (
+            native_bt.error_cause_component_actor_get_component_class_type(ptr)
         )
-        self._component_class_name = native_bt.error_cause_component_actor_get_component_class_name(
-            ptr
+        self._component_class_name = (
+            native_bt.error_cause_component_actor_get_component_class_name(ptr)
         )
         self._plugin_name = native_bt.error_cause_component_actor_get_plugin_name(ptr)
 
@@ -82,11 +82,11 @@ class _ComponentErrorCause(_ErrorCause):
 class _ComponentClassErrorCause(_ErrorCause):
     def __init__(self, ptr):
         super().__init__(ptr)
-        self._component_class_type = native_bt.error_cause_component_class_actor_get_component_class_type(
-            ptr
+        self._component_class_type = (
+            native_bt.error_cause_component_class_actor_get_component_class_type(ptr)
         )
-        self._component_class_name = native_bt.error_cause_component_class_actor_get_component_class_name(
-            ptr
+        self._component_class_name = (
+            native_bt.error_cause_component_class_actor_get_component_class_name(ptr)
         )
         self._plugin_name = native_bt.error_cause_component_class_actor_get_plugin_name(
             ptr
@@ -108,20 +108,22 @@ class _ComponentClassErrorCause(_ErrorCause):
 class _MessageIteratorErrorCause(_ErrorCause):
     def __init__(self, ptr):
         super().__init__(ptr)
-        self._component_name = native_bt.error_cause_message_iterator_actor_get_component_name(
-            ptr
+        self._component_name = (
+            native_bt.error_cause_message_iterator_actor_get_component_name(ptr)
         )
-        self._component_output_port_name = native_bt.error_cause_message_iterator_actor_get_component_output_port_name(
-            ptr
+        self._component_output_port_name = (
+            native_bt.error_cause_message_iterator_actor_get_component_output_port_name(
+                ptr
+            )
         )
-        self._component_class_type = native_bt.error_cause_message_iterator_actor_get_component_class_type(
-            ptr
+        self._component_class_type = (
+            native_bt.error_cause_message_iterator_actor_get_component_class_type(ptr)
         )
-        self._component_class_name = native_bt.error_cause_message_iterator_actor_get_component_class_name(
-            ptr
+        self._component_class_name = (
+            native_bt.error_cause_message_iterator_actor_get_component_class_name(ptr)
         )
-        self._plugin_name = native_bt.error_cause_message_iterator_actor_get_plugin_name(
-            ptr
+        self._plugin_name = (
+            native_bt.error_cause_message_iterator_actor_get_plugin_name(ptr)
         )
 
     @property
index 2fef7d68dfcb183f6cf15b5d78c5b9a9b008d686..6735bff3968d3e5c1661140c7cd733bd1f451ce6 100644 (file)
@@ -398,8 +398,10 @@ class _UserMessageIterator(_MessageIterator):
 
             utils._check_uint64(beg_clock_snapshot)
             utils._check_uint64(end_clock_snapshot)
-            ptr = native_bt.message_discarded_events_create_with_default_clock_snapshots(
-                self._bt_ptr, stream._ptr, beg_clock_snapshot, end_clock_snapshot
+            ptr = (
+                native_bt.message_discarded_events_create_with_default_clock_snapshots(
+                    self._bt_ptr, stream._ptr, beg_clock_snapshot, end_clock_snapshot
+                )
             )
         else:
             if beg_clock_snapshot is not None or end_clock_snapshot is not None:
@@ -435,8 +437,10 @@ class _UserMessageIterator(_MessageIterator):
 
             utils._check_uint64(beg_clock_snapshot)
             utils._check_uint64(end_clock_snapshot)
-            ptr = native_bt.message_discarded_packets_create_with_default_clock_snapshots(
-                self._bt_ptr, stream._ptr, beg_clock_snapshot, end_clock_snapshot
+            ptr = (
+                native_bt.message_discarded_packets_create_with_default_clock_snapshots(
+                    self._bt_ptr, stream._ptr, beg_clock_snapshot, end_clock_snapshot
+                )
             )
         else:
             if beg_clock_snapshot is not None or end_clock_snapshot is not None:
index 94010d47aa04aac8e2dc48795bf758a7dddf59e6..184de804bf1e614b41313e2d6ae50de588e2aabc 100644 (file)
@@ -501,8 +501,10 @@ class _TraceClass(_TraceClassConst):
             )
         else:
             utils._check_type(ranges, bt2_integer_range_set.SignedIntegerRangeSet)
-            ptr = native_bt.field_class_option_with_selector_field_integer_signed_create(
-                self._ptr, content_fc._ptr, selector_fc._ptr, ranges._ptr
+            ptr = (
+                native_bt.field_class_option_with_selector_field_integer_signed_create(
+                    self._ptr, content_fc._ptr, selector_fc._ptr, ranges._ptr
+                )
             )
 
         self._check_field_class_create_status(ptr, 'option')
index 4f6978e74bbaeeea1203b74daed12491f122815d..acb2e8b260782913d70ab06b97ea6bab3d17d773 100644 (file)
@@ -127,16 +127,38 @@ class UserComponentClassTestCase(unittest.TestCase):
 
         self.assertEqual(MySink.description, 'The description.')
 
-    def test_empty_description(self):
+    def test_empty_description_no_lines(self):
         class MySink(bt2._UserSinkComponent):
+            # fmt: off
+            """"""
+            # fmt: on
+
+            def _user_consume(self):
+                pass
+
+        self.assertIsNone(MySink.description)
+
+    def test_empty_description_no_contents(self):
+        class MySink(bt2._UserSinkComponent):
+            # fmt: off
             """
             """
+            # fmt: on
 
             def _user_consume(self):
                 pass
 
         self.assertIsNone(MySink.description)
 
+    def test_empty_description_single_line(self):
+        class MySink(bt2._UserSinkComponent):
+            """my description"""
+
+            def _user_consume(self):
+                pass
+
+        self.assertEqual(MySink.description, "my description")
+
     def test_help(self):
         class MySink(bt2._UserSinkComponent):
             """
@@ -291,11 +313,11 @@ class UserComponentClassTestCase(unittest.TestCase):
 class ComponentClassTestCase(unittest.TestCase):
     def setUp(self):
         class MySink(bt2._UserSinkComponent):
-            '''
+            """
             The description.
 
             The help.
-            '''
+            """
 
             def _user_consume(self):
                 pass
index e8effef34535ea1cb55953db817a0a2803a1d56b..470ae01d086140edf3dd1b532e50236a752b8364 100644 (file)
@@ -338,7 +338,8 @@ class EventTestCase(unittest.TestCase):
             event.payload_field['mosquito'] = 42
 
         msg = self._create_test_const_event_message(
-            event_fields_config=event_fields_config, with_ep=True,
+            event_fields_config=event_fields_config,
+            with_ep=True,
         )
         ev = msg.event
 
index ff67976dfc5ee47517926526c85847e260aef5fa..c55664980aaaa173087199d87320ea4ab441e815 100644 (file)
@@ -560,7 +560,9 @@ class CreateDiscardedPacketMessageTestCase(unittest.TestCase):
     # Trying to create when the stream does not support discarded packets.
     def test_create_unsupported_raises(self):
         def create_stream_class(tc, cc):
-            return tc.create_stream_class(supports_packets=True,)
+            return tc.create_stream_class(
+                supports_packets=True,
+            )
 
         def msg_iter_next(msg_iter, stream):
             with self.assertRaisesRegex(
This page took 0.030222 seconds and 4 git commands to generate.