From: Michael Jeanson Date: Mon, 27 Mar 2023 19:25:07 +0000 (-0400) Subject: Tests: python: path-like object introduced in python 3.6 X-Git-Url: https://git.liburcu.org/?p=lttng-tools.git;a=commitdiff_plain;h=2d2198ca03187a1bc1795ddf235c56de8ddea4d1 Tests: python: path-like object introduced in python 3.6 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 Signed-off-by: Jérémie Galarneau --- diff --git a/tests/utils/lttngtest/environment.py b/tests/utils/lttngtest/environment.py index 2e4b48569..d9777810b 100644 --- a/tests/utils/lttngtest/environment.py +++ b/tests/utils/lttngtest/environment.py @@ -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,