Skip to content

Misleading error information during exceptions in __fill_content #447

Open

Description

@gelbi123

Bug report

I found that in a really complex Zope application with a lot of
pagetemplates...

So I tried to break it down.

Exemplary:

<div metal-fill-slot="aslot" tal:condition="ErrorThrowingExpr">
</div>

What I did

I did two experiments:

First experiment

def test_fill_slot_error(self):

    def value():
        return 7/0

    template = self.from_string(
        '''
        <div metal:use-macro="macro">
            <div metal:fill-slot="content"
                  tal:condition="value()">
            </div>
        </div>
        ''', filename='templatefile_test1.pt')

    macro = self.from_string(
        '''
        <div metal:define-macro="macro">
            <div metal:define-slot="content"></div>
        </div>''', filename='macrofile_test1.pt')

    template(macro=macro, value=value)

This leads to following Error:

ZeroDivisionError: division by zero

 - Expression: "macro"
 - Filename:   templatefile_test1.pt
 - Location:   (line 2: col 34)
 - Arguments:  macro: <PageTemplate ('None',) at 0x7f28e830a870>
               value: <function ('value',) at 0x7f28e78bf9c0>
               target_language: <NoneType ('None',) at 0xa408a0>
               repeat: <RepeatDict ('None',) at 0x7f28e8222080>
               macroname: macro

I expected something like (or so):

   ZeroDivisionError: division by zero

    - Expression: "value()"
                    ^^^^^^^^^
    [...]

Second Experiment

Adding an other expression before define slot

def test_fill_slot_error2(self):

    value = lambda: 7/0
    value2 = 'Hi'

    template = self.from_string(
        '''
        <div metal:use-macro="macro">
            <div metal:fill-slot="content"
                tal:condition="value()">
            </div>
        </div>
        ''', filename='templatefile_test2.pt')

    macro = self.from_string(
        '''
        <div metal:define-macro="macro">
            <p tal:content="value2"></p>
            <div metal:define-slot="content"></div>
        </div>''', filename='macrofilename_test2.pt')

    template(macro=macro, value=value, value2=value2)

leads to

ZeroDivisionError: division by zero

 - Expression: "value2"
 - Filename:   macrofilename_test2.pt
 - Location:   (line 3: col 32)
 - Expression: "macro"
 - Filename:   templatefile_test2.pt
 - Location:   (line 2: col 34)
 - Arguments:  macro: <PageTemplate ('None',) at 0x7f28e80f2ea0>
               value: <function ('<lambda>',) at 0x7f28e784cc20>
               value2: Hi
               target_language: <NoneType ('None',) at 0xa408a0>
               repeat: <RepeatDict ('None',) at 0x7f28e7f8a7a0>
               macroname: macro

so even the Expression value points to the wrong expression

Analysis

Trying to understand what happens here...

in the compiled python script there is the generated method for
"fill-content"

compiled file from "templatefile_test2.pt"

I found that the whole "__fill_content" is in a try-except block

try:
    def __fill_content(__stream, econtext, rcontext, __i18n_domain=__i18n_domain, __i18n_context=__i18n_context, target_language=target_language):
        [...]
        __condition = getname('value')()
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        [...]
except:
    if __token is not None:
      rcontext.setdefault('__error__', []).append(__tokens[__token] + (__filename, _exc_info()[1]))
    raise

So when the error is raised the last __token is on eg. 35.:

35: ('macro', ...)

Possible solution

I only tried to fixed the compiled file by wrapping the logic in __fill_content in a try-except block like

def __fill_content(__stream, econtext, rcontext, __i18n_domain=__i18n_domain, __i18n_context=__i18n_context, target_language=target_language):
    try:
        [...]
    except:
        if __token is not None:
            rcontext.setdefault('__error__', []).append(__tokens[__token] + (__filename, _exc_info()[1]))
        raise

this leads to

ZeroDivisionError: division by zero

 - Expression: "value()"
 - Filename:   templatefile_test2.pt
 - Location:   (line 4: col 35)
 - Expression: "value2"
 - Filename:   macrofilename_test2.pt
 - Location:   (line 3: col 32)
 - Expression: "macro"
 - Filename:   templatefile_test2.pt
 - Location:   (line 2: col 34)
 - Arguments:  macro: <PageTemplate ('None',) at 0x74f8140da8d0>
               value: <function ('<lambda>',) at 0x74f8140e4360>
               value2: Hi
               target_language: <NoneType ('None',) at 0xa408a0>
               repeat: <RepeatDict ('None',) at 0x74f8140db610>
               macroname: macro

in case of the second experiment code.

Activity

  1. malthe commented on Sep 1, 2026

    @malthe
    Owner

    This seems about right. The idea is definitely that we can have nested errors.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions