Tests: python: path-like object introduced in python 3.6
authorMichael Jeanson <mjeanson@efficios.com>
Tue, 27 Jun 2023 17:33:18 +0000 (17:33 +0000)
committerJérémie Galarneau <jeremie.galarneau@efficios.com>
Thu, 27 Jul 2023 17:51:02 +0000 (13:51 -0400)
Prior to python 3.6 the os.path() function expected a string or bytes
object for the pathname. Use a compat method to convert the path-like
object to a string on interpreters that lack PEP-519 [1] support.

Traceback (most recent call last):
  File "tests/regression/tools/context/test_ust.py", line 156, in <module>
    tap, test_env, lttngtest.VpidContextType(), lambda test_app: test_app.vpid
  File "tests/regression/tools/context/test_ust.py", line 114, in test_static_context
    test_app = test_env.launch_wait_trace_test_application(50)
  File "tests/utils/lttngtest/environment.py", line 541, in launch_wait_trace_test_application
    wait_before_exit_file_path,
  File "tests/utils/lttngtest/environment.py", line 163, in __init__
    self._wait_for_file_to_be_created(pathlib.Path(app_ready_file_path))
  File "tests/utils/lttngtest/environment.py", line 168, in _wait_for_file_to_be_created
    if os.path.exists(sync_file_path):
  File "/usr/lib/python3.5/genericpath.py", line 19, in exists
    os.stat(path)
TypeError: argument should be string, bytes or integer, not PosixPath

[1] https://peps.python.org/pep-0519/

Change-Id: I783e36f61223d44667294ccbf4b3aec5bff68701
Signed-off-by: Michael Jeanson <mjeanson@efficios.com>
Signed-off-by: Jérémie Galarneau <jeremie.galarneau@efficios.com>
tests/utils/lttngtest/environment.py

index 4be5fb4faa356add20c59709800d6d1df6ef2541..60c42f51cb472bb89b3aa1953c9a4a0ba65e3007 100644 (file)
@@ -100,7 +100,7 @@ class _WaitTraceTestApplication:
             tempfile.mktemp(
                 prefix="app_",
                 suffix="_start_tracing",
-                dir=self._compat_open_path(environment.lttng_home_location),
+                dir=self._compat_pathlike(environment.lttng_home_location),
             )
         )
         # File that the application will create when all events have been emitted.
@@ -108,7 +108,7 @@ class _WaitTraceTestApplication:
             tempfile.mktemp(
                 prefix="app_",
                 suffix="_done_tracing",
-                dir=self._compat_open_path(environment.lttng_home_location),
+                dir=self._compat_pathlike(environment.lttng_home_location),
             )
         )
 
@@ -117,7 +117,7 @@ class _WaitTraceTestApplication:
                 tempfile.mktemp(
                     prefix="app_",
                     suffix="_exit",
-                    dir=self._compat_open_path(environment.lttng_home_location),
+                    dir=self._compat_pathlike(environment.lttng_home_location),
                 )
             )
 
@@ -133,7 +133,7 @@ class _WaitTraceTestApplication:
         app_ready_file_path = tempfile.mktemp(
             prefix="app_",
             suffix="_ready",
-            dir=self._compat_open_path(environment.lttng_home_location),
+            dir=self._compat_pathlike(environment.lttng_home_location),
         )  # type: str
 
         test_app_args = [str(binary_path)]
@@ -165,7 +165,7 @@ class _WaitTraceTestApplication:
     def _wait_for_file_to_be_created(self, sync_file_path):
         # type: (pathlib.Path) -> None
         while True:
-            if os.path.exists(sync_file_path):
+            if os.path.exists(self._compat_pathlike(sync_file_path)):
                 break
 
             if self._process.poll() is not None:
@@ -187,7 +187,7 @@ class _WaitTraceTestApplication:
                     return_code=self._process.returncode
                 )
             )
-        open(self._compat_open_path(self._app_start_tracing_file_path), mode="x")
+        open(self._compat_pathlike(self._app_start_tracing_file_path), mode="x")
 
     def wait_for_tracing_done(self):
         # type: () -> None
@@ -209,12 +209,13 @@ class _WaitTraceTestApplication:
         return self._process.pid
 
     @staticmethod
-    def _compat_open_path(path):
+    def _compat_pathlike(path):
         # type: (pathlib.Path) -> pathlib.Path | str
         """
-        The builtin open() in python >= 3.6 expects a path-like object while
-        prior versions expect a string or bytes object. Return the correct type
-        based on the presence of the "__fspath__" attribute specified in PEP-519.
+        The builtin open() and many methods of the 'os' library in Python >= 3.6
+        expect a path-like object while prior versions expect a string or
+        bytes object. Return the correct type based on the presence of the
+        "__fspath__" attribute specified in PEP-519.
         """
         if hasattr(path, "__fspath__"):
             return path
@@ -243,7 +244,7 @@ class WaitTraceTestApplicationGroup:
                 tempfile.mktemp(
                     prefix="app_group_",
                     suffix="_exit",
-                    dir=_WaitTraceTestApplication._compat_open_path(
+                    dir=_WaitTraceTestApplication._compat_pathlike(
                         environment.lttng_home_location
                     ),
                 )
@@ -294,7 +295,7 @@ class WaitTraceTestApplicationGroup:
             app.wait_for_tracing_done()
 
         open(
-            _WaitTraceTestApplication._compat_open_path(
+            _WaitTraceTestApplication._compat_pathlike(
                 self._wait_before_exit_file_path
             ),
             mode="x",
This page took 0.028292 seconds and 4 git commands to generate.