[issue34676] Guarantie that divmod() and PyNumber_Divmod() return a 2-tuple

Serhiy Storchaka report at bugs.python.org
Fri Sep 14 06:13:50 EDT 2018


New submission from Serhiy Storchaka <storchaka+cpython at gmail.com>:

It is documented that divmod() returns a pair of numbers, but the implementation doesn't enforce this. Actually divmod() just returns the result of __divmod__() or __rdivmod__() without checking it. PyNumber_Divmod() is documented as the C equivalent of divmod(). But since it returns a 2-tuple in all implemented cases, the user code can expect that it always return a 2-tuple. This can lead to hidden bugs similar to issue31577.

I think there are no reasons of returning anything except a 2-tuple from divmod(). Forcing this conditions can save third-party code from crashes. The following PR make divmod() and PyNumber_Divmod() always returning a 2-tuple.

----------
components: Interpreter Core
messages: 325341
nosy: mark.dickinson, serhiy.storchaka
priority: normal
severity: normal
status: open
title: Guarantie that divmod() and PyNumber_Divmod() return a 2-tuple
type: enhancement
versions: Python 3.8

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue34676>
_______________________________________


More information about the Python-bugs-list mailing list