From 74764524d7ff4b21a057878833eeae583783a662 Mon Sep 17 00:00:00 2001
From: Romain Courteaud <romain@nexedi.com>
Date: Tue, 10 May 2022 15:35:04 +0200
Subject: [PATCH] fixup: add compatibility with missing __code__ attribute

In order to apply https://lab.nexedi.com/nexedi/erp5/commit/4115e4658f50a48d3cd4b10e0ae033a3aaa3e273
it is needed to upgrade an existing side which only contains func_code
---
 product/ERP5/Document/Alarm.py    |  8 +++++++-
 product/ZSQLCatalog/SQLCatalog.py | 11 +++++++++--
 2 files changed, 16 insertions(+), 3 deletions(-)

diff --git a/product/ERP5/Document/Alarm.py b/product/ERP5/Document/Alarm.py
index 089d2ab568..1a93ed435a 100644
--- a/product/ERP5/Document/Alarm.py
+++ b/product/ERP5/Document/Alarm.py
@@ -164,7 +164,13 @@ class Alarm(XMLObject, PeriodicityMixin):
           activate_kw['tag'] = '%s_%x' % (self.getRelativeUrl(), getrandbits(32))
         tag = activate_kw['tag']
         method = getattr(self, method_id)
-        func_code = method.__code__
+        try:
+          func_code = method.__code__
+        except AttributeError:
+          # Compatibility with not migrated python script
+          # See https://lab.nexedi.com/nexedi/erp5/commit/4115e4658f50a48d3cd4b10e0ae033a3aaa3e273
+          func_code = method.func_code
+
         try:
           has_kw = func_code.co_flags & CO_VARKEYWORDS
         except AttributeError:
diff --git a/product/ZSQLCatalog/SQLCatalog.py b/product/ZSQLCatalog/SQLCatalog.py
index a03a63ac1c..beb23d82c9 100644
--- a/product/ZSQLCatalog/SQLCatalog.py
+++ b/product/ZSQLCatalog/SQLCatalog.py
@@ -1452,7 +1452,11 @@ class Catalog(Folder,
     if meta_type in self.HAS_ARGUMENT_SRC_METATYPE_SET:
       return method.arguments_src.split()
     elif meta_type in self.HAS_FUNC_CODE_METATYPE_SET:
-      return method.__code__.co_varnames[:method.__code__.co_argcount]
+      try:
+        func_code = method.__code__
+      except AttributeError:
+        func_code = method.func_code
+      return func_code.co_varnames[:func_code.co_argcount]
     # Note: Raising here would completely prevent indexation from working.
     # Instead, let the method actually fail when called, so _catalogObjectList
     # can log the error and carry on.
@@ -1838,7 +1842,10 @@ class Catalog(Folder,
       else:
         search_key = self.getSearchKey(key, 'RelatedKey')
     else:
-      func_code = script.__code__
+      try:
+        func_code = script.__code__
+      except AttributeError:
+        func_code = script.func_code
       search_key = (
         AdvancedSearchKeyWrapperForScriptableKey if (
           # 5: search_value (under any name), "search_key", "group",
-- 
2.30.9