classification
Title: Assertion failure in timedelta() in case of bad __divmod__
Type: crash Stage: patch review
Components: Extension Modules Versions: Python 3.7, Python 3.6, Python 2.7
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: Oren Milman, belopolsky, louielu, serhiy.storchaka, terry.reedy
Priority: normal Keywords: patch

Created on 2017-10-10 21:29 by serhiy.storchaka, last changed 2017-10-13 19:32 by terry.reedy.

Pull Requests
URL Status Linked Edit
PR 3947 open serhiy.storchaka, 2017-10-10 21:49
Messages (3)
msg304083 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-10-10 21:29
The following code causes an assertion error in timedelta constructor.

from datetime import timedelta

class BadInt(int):
    def __mul__(self, other):
        return Prod()

class Prod:
    def __radd__(self, other):
        return Sum()

class Sum:
    def __divmod__(self, other):
        return (0, -1)

timedelta(hours=BadInt(1))


Result:

python: /home/serhiy/py/cpython/Modules/_datetimemodule.c:1573: microseconds_to_delta_ex: Assertion `0 <= temp && temp < 1000000' failed.
Aborted (core dumped)
msg304131 - (view) Author: Louie Lu (louielu) * Date: 2017-10-11 13:22
I got a different result in latest commit: eeb5ffd54e5

➜  cpython git:(master) ✗ ./python test.py
Traceback (most recent call last):
  File "test.py", line 15, in <module>
    timedelta(hours=BadInt(1))
SystemError: <class 'datetime.timedelta'> returned NULL without setting an error

Do I miss some configure, I'm using ./configure --with-debug
msg304351 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2017-10-13 19:32
On Win10, installed 3.5.4, 3.6.3, 3.7.1a1 all raise SystemError.
3.6 and 3.7 repository debug builds raise AssertionError and Windows crash box.  After the patch, a silent crash.
History
Date User Action Args
2017-10-13 19:32:13terry.reedysetnosy: + terry.reedy
messages: + msg304351
2017-10-11 13:22:30louielusetnosy: + louielu
messages: + msg304131
2017-10-10 21:49:17serhiy.storchakasetkeywords: + patch
pull_requests: + pull_request3922
2017-10-10 21:29:28serhiy.storchakacreate