Opened 17 years ago

Closed 23 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 by miracle2k, 17 years ago

comment:2 by miracle2k, 17 years ago

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 by Adrian Holovaty, 17 years ago

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

comment:4 by miracle2k, 17 years ago

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 by Eric Holscher, 16 years ago

Needs tests: set
Triage Stage: UnreviewedDesign decision needed

comment:6 by Luke Plant, 14 years ago

Severity: Normal
Type: Bug

comment:7 by Carl Meyer, 13 years ago

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 by Carlton Gibson, 23 months ago

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 by Carlton Gibson, 23 months ago

Owner: changed from nobody to Anders Hovmöller
Status: newassigned
Version 0, edited 23 months ago by Carlton Gibson (next)

comment:10 by Anders Hovmöller, 23 months ago

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 by GitHub <noreply@…>, 23 months ago

In 69069a4:

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

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

comment:12 by Carlton Gibson, 23 months ago

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