New submission from Serhiy Storchaka <storchaka+cpyt...@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 <rep...@bugs.python.org>
<https://bugs.python.org/issue34974>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to