[Python-Dev] Re: PEP 292, Simpler String Substitutions
Christian Tismer
tismer@tismer.com
Sun, 23 Jun 2002 04:04:59 +0200
Barry A. Warsaw wrote:
>>>>>>"CT" == Christian Tismer <tismer@tismer.com> writes:
>>>>>
>
> CT> If there are program variables directly accessible inside
> CT> strings to be interpolated, then I see possible abuse, if
> CT> abusers manage to supply such a string in an unforeseen way.
>
> For literal strings in .py files, the only way that's going to happen
> is if someone you don't trust is hacking your source code, /or/ if you
> have evil translators sneaking in bogus translation strings. The
> latter can be solved with a verification step over your message
> catalogs, while the former I leave as an exercise for the reader. :)
>
> So still, I trust automatic interpolation of program vars for literal
> strings, but for strings coming from some other source (e.g. a web
> form), then yes, you obviously want to be explicit about the
> interpolation dictionary.
From another reply:
>
> def whereBorn(name):
> country = countryOfOrigin(name)
> return _('$name was born in $country')
Ok, I'm all with it.
Since a couple of hours, I'm riding the following horse:
- $name, $(name), $(any expr) is just fine
- all of this is compile-time stuff
The idea is:
Resolve the variables at compile time.
Don't provide the feature at runtime.
Here a simple approach. (I'm working on a complicated, too):
(assuming the "e" character triggering expression extraction)
def whereBorn(name):
country = countryOfOrigin(name)
return _(e'$name was born in $country')
is accepted by the grammar, but turned into the
equivalent of:
def whereBorn(name):
country = countryOfOrigin(name)
return _('%(x1)s was born in %(x2)s') % {
"x1": name, "x2": country}
That is: The $ stuff is extracted, turning the fmt
string into something anonymous. Your _() processes
it, then the variables are formatted in.
This turns the $ stuff completely into syntactic
sugar. Any Python expression inside $() is allowed,
it is compiled as if it were sitting inside the dict.
I also believe it is a good idea to do the _() on
the unexpanded string (as shown), since the submitted
values are most probably hard to translate at all.
cheers - chris
--
Christian Tismer :^) <mailto:tismer@tismer.com>
Mission Impossible 5oftware : Have a break! Take a ride on Python's
Johannes-Niemeyer-Weg 9a : *Starship* http://starship.python.net/
14109 Berlin : PGP key -> http://wwwkeys.pgp.net/
work +49 30 89 09 53 34 home +49 30 802 86 56 pager +49 173 24 18 776
PGP 0x57F3BF04 9064 F4E1 D754 C2FF 1619 305B C09C 5A3B 57F3 BF04
whom do you want to sponsor today? http://www.stackless.com/