[Python-ideas] Exploiting type-homogeneity in list.sort() (again!)

Chris Angelico rosuav at gmail.com
Mon Mar 6 01:56:12 EST 2017


On Mon, Mar 6, 2017 at 5:52 PM, Tim Peters <tim.peters at gmail.com> wrote:
> [Chris Angelico <rosuav at gmail.com>]
>> Arbitrary comparison functions let you do anything.... but whoa, I
>> cannot imagine any way that this would ever happen outside of "hey
>> look, here's how you can trigger a SystemError"!
>
> CPython is full of defensive code protecting against malicious crap.
> That's why it rarely crashes ;-)
>
> def __lt__(self, other):
>     return self.size < other.size
>
> Looks harmless?  Can't tell!  For all we know, there are proxy
> objects, and other.__getattr__ invokes some elaborate library to open
> a socket in a new thread to fetch the value of `size` over a network.

Exactly. It's always fun to discover some nice tidy exception that
cleanly copes with a ridiculous situation.

def gen():
    yield next(g)
g = gen()
next(g)

Fortunately in this case, the solution isn't to say "SystemError:
cannot create threads while sorting", but even if that were the case,
I can't imagine that much production code would be stopped by it.

ChrisA


More information about the Python-ideas mailing list