Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

map() filter returns string "<generator object do_map at 0x10bd730>" #288

Closed
l1k opened this issue Jan 3, 2014 · 7 comments
Closed

map() filter returns string "<generator object do_map at 0x10bd730>" #288

l1k opened this issue Jan 3, 2014 · 7 comments

Comments

@l1k
Copy link

l1k commented Jan 3, 2014

Let zonelist be a data structure like this:

[{'zones': ['pe.com', 'ps.de', 'p-s.de'], 'file': 'gm'},
 {'zones': ['p-e.net', 'ps.net', 'p-s.net'], 'file': 'grpc'}]

When using zonelist|map(attribute='file'), a string like this is returned:

<generator object do_map at 0x10bd730>

Expected behaviour would be to return a list containing the 'file' attributes of zonelist.

As it turns out there's a workaround: Using e.g. zonelist|map(attribute='file')|list or zonelist|map(attribute='file')|sort will result in the expected list:

[gm, grpc]

I'm using Jinja 2.7.1 with Ansible 1.4.3.

@untitaker
Copy link
Contributor

This is not a bug. Are you trying to output JSON?

@l1k
Copy link
Author

l1k commented Jan 4, 2014

Yes, my expectation was that map() returns a list. And that is not a bug? So that means map() shall only be used as an intermediate function with some other filter like |join or |list appended to it? Hmmm. Maybe that should be stated explicitly in the documentation.

@untitaker
Copy link
Contributor

You shouldn't just print out the repr of arbitrary Python objects to feed directly into Javascript. In your case you seemed to have luck, depending on the content, however, you might actually get invalid JSON, and it also might be a huge security flaw. Append the list and tojson filters to get valid JSON, or an exception when the object is not encodable to JSON.

@untitaker
Copy link
Contributor

The fact that the repr of most Python builtins is JSON-ish is not a coincidence, but the repr is never something one should rely on for actual program logic.

@untitaker
Copy link
Contributor

Also, i don't see anywhere in the Jinja docs which implies that map would return anything more specific than a sequence.

@mitsuhiko
Copy link
Contributor

I don't want to change this as this would cause performance of these functions to do down which normally are only used with iteration. |list fixes it.

@jaytaylor
Copy link

I didn't know about the {{ ... | list }} solution for the ansbile <generator object do_map at 0xdeadbeef> issue.

Thanks for filing this!

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 13, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants