[New-bugs-announce] [issue34974] bytes and bytearray constructors replace unexpected exceptions

Serhiy Storchaka report at bugs.python.org
Sat Oct 13 15:18:56 EDT 2018


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

bytes() and bytearray() replace some unexpected exceptions (including MemoryError and KeyboardInterrupt) with TypeError.

1) Exception in __index__.

class X:
    def __index__(self):
        raise MemoryError

`bytes(X())` results in:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: cannot convert 'X' object to bytes

`bytearray(X())` results in:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'X' object is not iterable

This is related to issue29159.

2) Exception in __iter__.

class X:
    def __iter__(self):
        raise MemoryError

`bytes(X())` results in:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: cannot convert 'X' object to bytes

`bytearray(X())` works correctly (raises a MemoryError).

The proper solution is to replace just a TypeError and allow other exceptions to emerge.

----------
assignee: serhiy.storchaka
components: Interpreter Core
messages: 327661
nosy: belopolsky, inada.naoki, serhiy.storchaka
priority: normal
severity: normal
status: open
title: bytes and bytearray constructors replace unexpected exceptions
type: behavior
versions: Python 3.6, Python 3.7, Python 3.8

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


More information about the New-bugs-announce mailing list