This commit is contained in:
Kovid Goyal 2025-07-22 16:16:49 +05:30
parent e38f7f2346
commit 4e66d2fff6
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C

View File

@ -234,6 +234,7 @@ class FormatterFunction:
arg_count = 0 arg_count = 0
aliases = [] aliases = []
object_type = StoredObjectType.PythonFunction object_type = StoredObjectType.PythonFunction
_cached_program_text = None
def __doc__getter__(self) -> str: def __doc__getter__(self) -> str:
return _('No documentation provided') return _('No documentation provided')
@ -246,6 +247,18 @@ class FormatterFunction:
def __doc__(self): def __doc__(self):
return self.__doc__getter__() return self.__doc__getter__()
@property
def program_text(self) -> str:
if self._cached_program_text is None:
eval_func = inspect.getmembers(self.__class__,
lambda x: inspect.isfunction(x) and x.__name__ == 'evaluate')
try:
lines = [l[4:] for l in inspect.getsourcelines(eval_func[0][1])[0]]
except Exception:
lines = []
self._cached_program_text = ''.join(lines)
return self._cached_program_text
def evaluate(self, formatter, kwargs, mi, locals, *args): def evaluate(self, formatter, kwargs, mi, locals, *args):
raise NotImplementedError() raise NotImplementedError()
@ -281,21 +294,8 @@ class FormatterFunction:
class BuiltinFormatterFunction(FormatterFunction): class BuiltinFormatterFunction(FormatterFunction):
def __init__(self): def __init__(self):
self._cached_program_text = None
formatter_functions().register_builtin(self) formatter_functions().register_builtin(self)
@property
def program_text(self) -> str:
if self._cached_program_text is None:
eval_func = inspect.getmembers(self.__class__,
lambda x: inspect.isfunction(x) and x.__name__ == 'evaluate')
try:
lines = [l[4:] for l in inspect.getsourcelines(eval_func[0][1])[0]]
except Exception:
lines = []
self._cached_program_text = ''.join(lines)
return self._cached_program_text
class BuiltinStrcmp(BuiltinFormatterFunction): class BuiltinStrcmp(BuiltinFormatterFunction):
name = 'strcmp' name = 'strcmp'
@ -3691,7 +3691,7 @@ class FormatterUserFunction(FormatterFunction):
self.name = name self.name = name
self.user_doc = doc self.user_doc = doc
self.arg_count = arg_count self.arg_count = arg_count
self._cached_program_text = program_text self._cached_program_text = program_text or ''
self.cached_compiled_text = None self.cached_compiled_text = None
# Keep this for external code compatibility. Set it to True if we have a # Keep this for external code compatibility. Set it to True if we have a
# python template function, otherwise false. This might break something # python template function, otherwise false. This might break something