From 7b705481f840b7e83a7d79b305bb12eb773032d2 Mon Sep 17 00:00:00 2001
From: Kazuhiko SHIOZAKI <kazuhiko@nexedi.com>
Date: Mon, 6 Feb 2023 13:12:33 +0000
Subject: [PATCH] Zope2: use func_code if __code__ is missing, that is the case
 for Script (Python).

---
 product/ERP5/Document/Alarm.py     | 2 ++
 product/ERP5Type/Core/Predicate.py | 5 ++++-
 product/ZSQLCatalog/SQLCatalog.py  | 7 ++++++-
 3 files changed, 12 insertions(+), 2 deletions(-)

diff --git a/product/ERP5/Document/Alarm.py b/product/ERP5/Document/Alarm.py
index cdcc50bb112..cb415cf04a4 100644
--- a/product/ERP5/Document/Alarm.py
+++ b/product/ERP5/Document/Alarm.py
@@ -165,6 +165,8 @@ class Alarm(XMLObject, PeriodicityMixin):
         tag = activate_kw['tag']
         method = getattr(self, method_id)
         func_code = method.__code__
+        if func_code is None: # BBB Zope2
+          func_code = method.func_code
         try:
           has_kw = func_code.co_flags & CO_VARKEYWORDS
         except AttributeError:
diff --git a/product/ERP5Type/Core/Predicate.py b/product/ERP5Type/Core/Predicate.py
index 3085b891f14..6e0baf4c99b 100644
--- a/product/ERP5Type/Core/Predicate.py
+++ b/product/ERP5Type/Core/Predicate.py
@@ -181,7 +181,10 @@ class Predicate(XMLObject):
           try:
             result = method(self)
           except TypeError:
-            if method.__code__.co_argcount != isinstance(method, MethodType):
+            func_code = method.__code__
+            if func_code is None: # BBB Zope2
+              func_code = method.func_code
+            if func_code.co_argcount != isinstance(method, MethodType):
               raise
             # backward compatibilty with script that takes no argument
             warn('Predicate %s uses an old-style method (%s) that does not'
diff --git a/product/ZSQLCatalog/SQLCatalog.py b/product/ZSQLCatalog/SQLCatalog.py
index 9adfab64fc2..355a0a641ff 100644
--- a/product/ZSQLCatalog/SQLCatalog.py
+++ b/product/ZSQLCatalog/SQLCatalog.py
@@ -1451,7 +1451,10 @@ 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]
+      func_code = method.__code__
+      if func_code is None: # BBB Zope2
+        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,6 +1841,8 @@ class Catalog(Folder,
         search_key = self.getSearchKey(key, 'RelatedKey')
     else:
       func_code = script.__code__
+      if func_code is None: # BBB Zope2
+        func_code = script.func_code
       search_key = (
         AdvancedSearchKeyWrapperForScriptableKey if (
           # 5: search_value (under any name), "search_key", "group",
-- 
2.30.9