details:   https://code.tryton.org/tryton/commit/599fb838e8d1
branch:    default
user:      Nicolas Évrard <[email protected]>
date:      Fri May 15 22:23:22 2026 +0200
description:
        Do not import module to find the path of one of its file

        Closes #14892
diffstat:

 trytond/trytond/tests/test_tools.py |   6 ++++--
 trytond/trytond/tools/misc.py       |  25 ++++++++++++++++++++-----
 2 files changed, 24 insertions(+), 7 deletions(-)

diffs (63 lines):

diff -r 2335dba4dc44 -r 599fb838e8d1 trytond/trytond/tests/test_tools.py
--- a/trytond/trytond/tests/test_tools.py       Fri Jun 26 15:41:27 2026 +0200
+++ b/trytond/trytond/tests/test_tools.py       Fri May 15 22:23:22 2026 +0200
@@ -125,11 +125,13 @@
             with file_open('ir/noexist'):
                 pass
 
-        with self.assertRaisesRegex(IOError, "Permission denied:"):
+        with self.assertRaisesRegex(
+                FileNotFoundError, "No such file or directory:"):
             with file_open('/etc/passwd'):
                 pass
 
-        with self.assertRaisesRegex(IOError, "Permission denied:"):
+        with self.assertRaisesRegex(
+                FileNotFoundError, "No such file or directory:"):
             with file_open('../../foo'):
                 pass
 
diff -r 2335dba4dc44 -r 599fb838e8d1 trytond/trytond/tools/misc.py
--- a/trytond/trytond/tools/misc.py     Fri Jun 26 15:41:27 2026 +0200
+++ b/trytond/trytond/tools/misc.py     Fri May 15 22:23:22 2026 +0200
@@ -5,6 +5,7 @@
 Miscelleanous tools used by tryton
 """
 import importlib
+import importlib.resources
 import io
 import math
 import os
@@ -71,13 +72,27 @@
                 path = secure_join(_root_path, module_name, module_path)
             else:
                 try:
-                    module = import_module(module_name)
+                    ep, = entry_points().select(
+                        group=MODULES_GROUP, name=module_name)
+                except ValueError:
+                    ep = None
+                try:
+                    if ep is None:
+                        spec = importlib.util.find_spec(
+                            f'{MODULES_GROUP}.{module_name}')
+                    else:
+                        spec = importlib.util.find_spec(ep.module)
                 except ModuleNotFoundError:
-                    path = secure_join(_root_path, subdir, name)
+                    raise FileNotFoundError(
+                        f"No such file or directory: {name!r}")
                 else:
-                    path = os.path.dirname(module.__file__)
-                    if module_path:
-                        path = secure_join(path, module_path)
+                    if spec and spec.has_location:
+                        path = os.path.dirname(spec.origin)
+                        if module_path:
+                            path = secure_join(path, module_path)
+                    else:
+                        raise FileNotFoundError(
+                            f"No such file or directory: {name!r}")
         else:
             path = secure_join(_root_path, subdir, name)
     else:

Reply via email to