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

Syntax error with ${{'foo':'bar'}} #20

Closed
sqlalchemy-bot opened this issue Jan 31, 2007 · 14 comments
Closed

Syntax error with ${{'foo':'bar'}} #20

sqlalchemy-bot opened this issue Jan 31, 2007 · 14 comments
Labels
bug Something isn't working compiler low priority

Comments

@sqlalchemy-bot
Copy link

Migrated issue, originally created by Anonymous

This works fine:

<li>${dict(method="get")}</li>

This crashes:

<li>${{'method':'get'}}</li>

mako.exceptions.SyntaxException: (SyntaxError) unexpected EOF while parsing (line 1) ("{'method':'get'") in file [...]


Attachments: mako_braces.diff | mako_ticket20.diff | mako-lexer-fix.diff

@sqlalchemy-bot
Copy link
Author

Michael Bayer (@zzzeek) wrote:

yeah unfortunately there will always be some issues like this one, it should be pretty obvious why this one is occurring (you know, its looking for a bracket). there is some logic to handle things like this which is already accounting for strings like ${"}") but thats because its counting quotes...i can look into it further but unless i rewrote the entire python parser there will always be things like this which can slip through (also, a typical MVC setup wouldnt really be declaring dictionaries inside of templates/expressions anyway, though thats not a 100% excuse.....)

id like to see how other template languages handle phrases like that one.

@sqlalchemy-bot
Copy link
Author

Changes by Michael Bayer (@zzzeek):

  • changed assignee from "zzzzeek" to "zzzeek"

@sqlalchemy-bot
Copy link
Author

Anonymous wrote:

I've simplfied the example for the demonstration, but following this example, this is not an excuse, not even 1%. :)

From a Pylons point of view, I'd be tempted to do the following:

${h.link_to_remote("Home", options={"url":h.url_for("home"), "method":"get"})}

How about extracting what's contained inside ${} as a string and do an eval() on the string ?

extracted = """h.link_to_remote("Home", options={"url":h.url_for("home"), "method":"get"})"""
print eval(extracted)
Home

Just an idea...

@sqlalchemy-bot
Copy link
Author

P C (@pc) wrote:

Rather than fixing every such bug, may be a good idea to let user to increment number of braces if he needs. So that examples above writen as:

  ${{ h.link_to_remote("Home", options={"url":h.url_for("home"), "method":"get"}) }}
  ${{ {'method':'get'} }}
# So this would be also written as:
  ${{ " some } text " }}
# And maybe sometimes you will need:
  $` {'a': b, c: { 'e': 'f' }} `
# but this is better writen as (with space between braces "}"):
  ${{ {'a': b, c: { 'e': 'f' } } }}

I consider this is better than escaping braces someway:

  ${ h.link_to_remote("Home", options=\{"url":h.url_for("home"), "method":"get"\}) }
# or maybe
  ${ h.link_to_remote("Home", options={{"url":h.url_for("home"), "method":"get"}}) }

@sqlalchemy-bot
Copy link
Author

Anonymous wrote:

Attached patch exploits codeop module to overcome this problem. This module is one that is used for interactive python sessions.

@sqlalchemy-bot
Copy link
Author

Michael Bayer (@zzzeek) wrote:

unfortunately, many tests fail with the latest patch. If the patch can be repaired such that all tests pass, we can put it in. Mako is due for a release so this one might have to wait for another go-around....

@sqlalchemy-bot
Copy link
Author

Anonymous wrote:

Tavis has the following suggestion from Cheetah. -MO

"I maintain a stack of enclosures (,[,{ to balance things out. See
getExpressionParts at line 1052 in Parser.py."

@sqlalchemy-bot
Copy link
Author

Michael Bayer (@zzzeek) wrote:

yeah, i think an expansion of the current "quote counting" idea to just be a stack of enclosures is the most practical approach here. I had hopes for the "python compiler" approaches previously patched here but this would be more straightforward.

@sqlalchemy-bot
Copy link
Author

Anonymous wrote:

Werkzeug ships a minimal template engine for some small scripts and it solved that problem the same way Ka-Ping Yee's old interpolation code did with a small modification for newer Python versions:

http://dev.pocoo.org/projects/werkzeug/browser/werkzeug/templates.py

see token_re and match_or_fail (and the code that calls it) around line 312.

@sqlalchemy-bot
Copy link
Author

Michael Bayer (@zzzeek) wrote:

k, thats hopeful. i'm at energy level zero for mako these days so if someone wants to adapt that into a patch, that's a good path towards a fix.

@sqlalchemy-bot
Copy link
Author

Anonymous wrote:

I took a crack at this; it's not the most beautiful solution, but it should be correct. Fixes #86 as well. Added simple tests, all the tests pass, and the suite is even a whopping 4ms faster!

Trac griped when I tried to attach a file, so patch here: https://gist.github.com/1713508

~ @eevee

@sqlalchemy-bot
Copy link
Author

Michael Bayer (@zzzeek) wrote:

wow, that is really impressive ! as you can see, this bug has existed since day one and...ah someone else took a crack five years ago. I'll try running this across a huge pile of templates here and see what I get.

@sqlalchemy-bot
Copy link
Author

Michael Bayer (@zzzeek) wrote:

awesome works great !!

1efbc03

@sqlalchemy-bot
Copy link
Author

Changes by Michael Bayer (@zzzeek):

  • changed status to closed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working compiler low priority
Projects
None yet
Development

No branches or pull requests

1 participant