From 3ae8f25f559e3a8d0ec4cf5129ffbfd7a601b3d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Hozza?= Date: Mon, 22 Apr 2024 14:36:21 +0200 Subject: [PATCH] Testutil/importlib: don't write bytecode when importing modules MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Cache files will split the extension, this means that all pyc cache files looks like we get many clashing `org.osbuild.cpython-py311.pyc files. Moreover, the cache bytecode invalidation is based on the timestamp (which is the same after git checkout) and the file size (which may be the same for two different files). This means that we can't rely on the cache files. This issue has been found after the previous commit made the `org.osbuild.systemd` and `org.osbuild.selinux` stages to have exactly the same size, which caused the interpreter to reuse the bytecode for the selinux stage when running unit tests for the systemd stage. This resulted in consistent and weird failures when the systemd stage options were passed to the selinux stage code. The credit for this fix goes to Michael Vogt, who found the cause and fix. Also thanks to Simon de Vlieger for his help with debugging the problem. Co-authored-by: Michael Vogt Signed-off-by: Tomáš Hozza --- osbuild/testutil/imports.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/osbuild/testutil/imports.py b/osbuild/testutil/imports.py index 3fb14aee..12969282 100644 --- a/osbuild/testutil/imports.py +++ b/osbuild/testutil/imports.py @@ -3,8 +3,16 @@ Import related utilities """ import importlib +import sys from types import ModuleType +# Cache files will split the extension, this means that all pyc cache files +# looks like we get many clashing `org.osbuild.cpython-py311.pyc` files. +# Moreover, the cache bytecode invalidation is based on the timestamp (which +# is the same after git checkout) and the file size (which may be the same +# for two different files). This means that we can't rely on the cache files. +sys.dont_write_bytecode = True + def import_module_from_path(fullname, path: str) -> ModuleType: """import_module_from_path imports the given path as a python module