Tests: python: path-like object introduced in python 3.6
authorMichael Jeanson <mjeanson@efficios.com>
Mon, 27 Mar 2023 19:25:07 +0000 (15:25 -0400)
committerMichael Jeanson <mjeanson@efficios.com>
Tue, 28 Mar 2023 18:54:30 +0000 (14:54 -0400)
Prior to python 3.6 the builtin open() function expected a string or
bytes object for the pathname. Add a compat method to convert the
path-like object to a string on interpreters that lack PEP-519 [1]
support.

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

Change-Id: I23d1da1202e17db621f1fb89f7a79b12694dd4a6
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 2e4b48569909705ceb400a5c05958f8a80a65511..d9777810bdeb90fa7c4b7563598c43289304a8f6 100644 (file)
@@ -82,7 +82,7 @@ class WaitTraceTestApplication:
             tempfile.mktemp(
                 prefix="app_",
                 suffix="_start_tracing",
-                dir=environment.lttng_home_location,
+                dir=self._compat_open_path(environment.lttng_home_location),
             )
         )
         self._has_returned = False
@@ -95,7 +95,9 @@ class WaitTraceTestApplication:
 
         # File that the application will create to indicate it has completed its initialization.
         app_ready_file_path: str = tempfile.mktemp(
-            prefix="app_", suffix="_ready", dir=environment.lttng_home_location
+            prefix="app_",
+            suffix="_ready",
+            dir=self._compat_open_path(environment.lttng_home_location),
         )
 
         test_app_args = [str(binary_path)]
@@ -140,7 +142,7 @@ class WaitTraceTestApplication:
                     return_code=self._process.returncode
                 )
             )
-        open(self._app_start_tracing_file_path, mode="x")
+        open(self._compat_open_path(self._app_start_tracing_file_path), mode="x")
 
     def wait_for_exit(self) -> None:
         if self._process.wait() != 0:
@@ -155,6 +157,19 @@ class WaitTraceTestApplication:
     def vpid(self) -> int:
         return self._process.pid
 
+    @staticmethod
+    def _compat_open_path(path):
+        # type: (pathlib.Path)
+        """
+        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.
+        """
+        if hasattr(path, "__fspath__"):
+            return path
+        else:
+            return str(path)
+
     def __del__(self):
         if not self._has_returned:
             # This is potentially racy if the pid has been recycled. However,
This page took 0.026312 seconds and 4 git commands to generate.