diff mbox series

python3: use __spec__.cached when looking for cached bytecode

Message ID 20261110153103.3124649-1-ross.burton@arm.com
State New
Headers show
Series python3: use __spec__.cached when looking for cached bytecode | expand

Commit Message

Ross Burton Nov. 10, 2026, 3:31 p.m. UTC
The __cached__ attribute has been deprecated since Python 3.13 and has
been removed in 3.15:

  The __cached__ attribute on modules, which was deprecated since
  version 3.13, is no longer set or taken into consideration by the
  import system or standard library. Use __spec__.cached instead.[1]

Replace __cached__ with __spec__.cached, and remove obsolete exception
handling that doesn't get triggered with the current standard library.

[1] https://docs.python.org/3/whatsnew/3.15.html

Signed-off-by: Ross Burton <ross.burton@arm.com>
---
 .../python/python3/get_module_deps3.py        | 21 +++----------------
 1 file changed, 3 insertions(+), 18 deletions(-)
diff mbox series

Patch

diff --git a/meta/recipes-devtools/python/python3/get_module_deps3.py b/meta/recipes-devtools/python/python3/get_module_deps3.py
index 8e432b49af8..28b5d22c458 100644
--- a/meta/recipes-devtools/python/python3/get_module_deps3.py
+++ b/meta/recipes-devtools/python/python3/get_module_deps3.py
@@ -143,24 +143,9 @@  for item in dif:
 
     cpython_tag = sys.implementation.cache_tag
     cached = ''
-    # Theres no naive way to find *.pyc files on python3
-    try:
-        if debug == True:
-            log.write('\nCalling: sys.modules[' + '%s' % item + '].__cached__\n')
-        cached = sys.modules['%s' % item].__cached__
-    except AttributeError as e:
-        # Deals with thread (builtin module) not having __cached__ attribute
-        if debug == True:
-            log.write(item + ' ')
-            log.write(str(e))
-            log.write('\n')
-        pass
-    except NameError as e:
-        # Deals with NameError: name 'cached' is not defined
-        if debug == True:
-            log.write(item+' ') 
-            log.write(str(e))                                              
-        pass
+    if debug == True:
+        log.write('\nGetting cached attribute for %s\n' % item)
+    cached = sys.modules['%s' % item].__spec__.cached
     if cached is not None:
         if debug == True:
             log.write(cached + '\n')