[Python-Dev] The role of NotImplemented: What is it for and when should it be used?

Nick Coghlan ncoghlan at gmail.com
Tue Nov 4 12:15:02 CET 2014


It's worth noting that as far as I am aware, all the cases where CPython
currently raises TypeError directly rather than returning NotImplemented
are due to a longstanding bug in the handling concatenation and repetition
of sequences implemented entirely in C (which includes the builtins):
http://bugs.python.org/issue11477

The coercion dance in abstract.c currently gets the operand precedence
wrong for sq_concat and sq_repeat, and also doesn't check their return
values for NotImplemented.

Types implemented in Python work correctly (including respecting
NotImplemented return values), as those fill in both the nb_* and the sq_*
slots. That's why this bug is in the "annoying implementation quirk"
category rather than the "major correctness flaw" category.

I tried to fix it ages ago directly in abstract.c, but the result was an
unmaintainable mess. (The test changes in the draft patch will hopefully
still prove useful some day).

There's another possible implementation strategy, which is to change type
creation to populate nb_add and nb_multiply when sq_concat and sq_repeat
are defined, and then never call those two methods directly from abstract.c.

I've never found the time myself to go back and try that version of the
fix, but it's definitely an issue I'd love to see fixed at some point.

Regards,
Nick.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20141104/fac44871/attachment.html>


More information about the Python-Dev mailing list