Opened 16 years ago

Closed 10 months ago

#7430 closed Bug (wontfix)

Recursively iterating through a template's nodes

Reported by: miracle2k Owned by: Anders Hovmöller
Component: Template system Version: dev
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Right now, for node in Template() will only yield the top-level nodes. Is this the way it is intended to work? I was expecting to get all nodes, recursively.

The Template.__iter__ code looks like this:

    def __iter__(self):
        for node in self.nodelist:
            for subnode in node:
                yield subnode

And Node.__iter__ does:

    def __iter__(self):
        yield self

This looks like a precipe to allow nodes to yield their childnodes, without relying on the existence of the nodelist attribute.

However, nodes like BlockNode and ExtendsNode do not implement __iter__ - only ForNode and IfNode seem to do, and not in the way I would have expected (they don't yield self).

Is this a bug, or per design?

Change History (12)

comment:1 Changed 16 years ago by miracle2k

comment:2 Changed 16 years ago by miracle2k

I'm noticing that both ForNode and IfNode do not provide the nodelist attribute. Is the idea that you should use Node.nodelist if it exists, and fall back to the iterator otherwise? Wouldn't it make sense then to have the Node class implement a generic iterator that will yield the items in self.nodelist?

comment:3 Changed 16 years ago by Adrian Holovaty

I don't recall whether this was by design. Does any Django code actually *use* this iter behavior?

comment:4 Changed 16 years ago by miracle2k

Any of Django's code itself? I quick grep didn't reveal anything obvious. Rendering works via recursive render() calls, so that part doesn't need it.

comment:5 Changed 15 years ago by Eric Holscher

Needs tests: set
Triage Stage: UnreviewedDesign decision needed

comment:6 Changed 13 years ago by Luke Plant

Severity: Normal
Type: Bug

comment:7 Changed 12 years ago by Carl Meyer

Easy pickings: unset
Resolution: needsinfo
Status: newclosed
UI/UX: unset

Code changes require more practical justification than this. Feel free to reopen with demonstration of a specific use case, the code that is currently required to achieve it, and the code that would be possible with a change to Django.

comment:8 Changed 10 months ago by Carlton Gibson

Needs tests: unset
Resolution: needsinfo
Status: closednew
Triage Stage: Design decision neededReady for checkin

As per a much later report via a PR, Template.__iter__() looks broken:

>>> from django.template import Template
>>> t = Template('Hello, world!')
>>> next(iter(t))
...
TypeError: 'TextNode' object is not iterable

I don't recall whether this was by design. Does any Django code actually *use* this iter behavior?

According to the test suite, it doesn't.

I think this can just be removed (as per the PR).

comment:9 Changed 10 months ago by Carlton Gibson

Owner: changed from nobody to Anders Hovmöller
Status: newassigned

As per Tim's comment, should be wontfix once merged.

Version 1, edited 10 months ago by Carlton Gibson (previous) (next) (diff)

comment:10 Changed 10 months ago by Anders Hovmöller

Just FYI I have working code for walking the syntax tree: https://github.com/boxed/okrand/blob/main/okrand/__init__.py#L262

It's... not ideal as it requires two monkey patches that I've found so far. But I personally don't think having __iter__ on stuff is very useful. There are multiple child nodelists, so iter would be a little weird honestly.

comment:11 Changed 10 months ago by GitHub <noreply@…>

In 69069a4:

Refs #7430 -- Removed broken Template.iter().

Co-authored-by: Anders Hovmöller <anders.hovmoller@…>

comment:12 Changed 10 months ago by Carlton Gibson

Resolution: wontfix
Status: assignedclosed
Triage Stage: Ready for checkinUnreviewed

As per Tim's suggestion on the PR, resolving this as wontfix, since directly iterating a template's nodes isn't supported.
(Short of a rationale and proof-of-concept, it doesn't seem likely to be added.)

Note: See TracTickets for help on using tickets.
Back to Top