the Gravity of Python 2

Chris Angelico rosuav at gmail.com
Thu Jan 9 16:12:27 EST 2014


On Fri, Jan 10, 2014 at 7:54 AM, Roy Smith <roy at panix.com> wrote:
> On Thursday, January 9, 2014 3:35:05 PM UTC-5, Chris Angelico wrote:
>> In fact, I've given end users the ability to enter strftime strings (eg
>> to construct a filename), and it's worked just fine.
>
> I assume you realize that "../../../../../../../../../../../../../../../../etc/passwd" is a valid strftime() format specifier? :-)

Yes, and since this was for the creation of a log file by an
unprivileged process, that would simply fail :) Though the specific
case I'm thinking of here was on Windows, so you could probably find
an equivalent filename (it didn't prevent absolute names, so you could
just stuff whatever you want in) and shoot yourself in the foot
big-time. It's the user's own system, let him make a mess of it if he
wants :)

> But, to answer your question, no, I have nothing against small languages, per-se (and I've done plenty of regex work).  But, if my goal is to print a time in some human-readable form:
>
>>>> print t
>
> is a lot easier than anything involving strftime().

Sure, it's easier. But there are plenty of types that don't provide a
particularly useful repr - regexes being one that only recently
changed:

2.7 and 3.3:
>>> re.compile(r"(.)\1\1\1")
<_sre.SRE_Pattern object at 0x012464F0>
>>> _.search("This is a test string with a quadrrrruple letter in it!")
<_sre.SRE_Match object at 0x012C3EE0>

3.4:
>>> re.compile(r"(.)\1\1\1")
re.compile('(.)\\1\\1\\1')
>>> _.search("This is a test string with a quadrrrruple letter in it!")
<_sre.SRE_Match object; span=(33, 37), match='rrrr'>

Would you avoid using regexes in anything less than 3.4 simply because
of this lack of repr? It's a convenience, not a deal-breaker. (Or if
you disagree with me on that point, you're cutting out a lot of very
useful types.) It's not hard to call time.ctime(ts) or strftime(...)
for display; the big loser is the interactive interpreter, where a
good repr is everything.

ChrisA



More information about the Python-list mailing list