HNHacker News
TopNewBestAskShowJobs

jimbaker

1 karma · joined June 5, 2012

submissionscomments
jimbaker··on PEP 750: Tag Strings for Writing Domain-Specific Languages
Walking the call stack implies using dynamic scope, which has hard edges, vs lexical scope. See my answer earlier in this thread https://news.ycombinator.com/item?id=41272285

It's been nearly 23 years, but Python 2.2 fixed this issue (https://docs.python.org/3/whatsnew/2.2.html#pep-227-nested-s...), and it's also why JavaScript added let (and const). f-string support also uses lexical scope, and it's an important part of its success.

jimbaker··on PEP 750: Tag Strings for Writing Domain-Specific Languages
Thanks to the feedback in the Discourse thread, we will be changing the signature such that a tag function will be passed an object implementing a Template protocol, with attributes source, for the original source string as written; and attribute args for what was *args in the starting version of the PEP.

Template.source can act as a memoization key.

jimbaker··on PEP 750: Tag Strings for Writing Domain-Specific Languages
The intent here is to support the following approach for tag function authors:

1. Parse to an AST, generally using an off-the-shelf parser. In practice, it's possible to rewrite interpolations with a placeholder suitable for a given language, eg x$Nx for HTML. Of course if that doesn't actually work, you might have to write/modify an existing parser. Hopefully we can cleverly avoid this extra work.

2. Walk/compile the AST, filling interpolations, but taking in account the context. This can for example take the form of building appropriate query strings that avoid Bobby Tables SQL injection, whether by mapping to SQL placeholders or with appropriate quoting (such as for a column or table name).

3. Memoize these steps, much as we see with builtin DSLs in Python, like the re module; see https://github.com/python/cpython/blob/3.12/Lib/re/__init__.... We do plan to make this easier/faster by supporting getting the original source of the template string (Template.source), vs the *args approach we show in the PEP at the start of this discussion (this will become Template.args instead; Template here is the proposed protocol of the object passed in).

Related is my post here: https://discuss.python.org/t/pep-750-tag-strings-for-writing...

jimbaker··on PEP 750: Tag Strings for Writing Domain-Specific Languages
Frame hacks with sys._getframe necessarily imply dynamic scope not lexical scope. Dynamic scope does not work with nested functions, including comprehensions. See this issue with the htm library, https://github.com/jviide/htm.py/issues/11
jimbaker··on Why I'm Making Python 2.8
Yes, that would be true on all counts :) as one of those Jython maintainers. Jython 2.7.1 is currently bottlenecked on finalizing support on Windows for pip/setuptools. It should be the last fix.