classification
Title: bytes and bytearray constructors replace unexpected exceptions
Type: behavior Stage: resolved
Components: Interpreter Core Versions: Python 3.8, Python 3.7, Python 3.6, Python 2.7
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: serhiy.storchaka Nosy List: belopolsky, inada.naoki, miss-islington, serhiy.storchaka
Priority: normal Keywords: patch

Created on 2018-10-13 19:18 by serhiy.storchaka, last changed 2018-10-15 05:46 by serhiy.storchaka. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 9852 merged serhiy.storchaka, 2018-10-13 19:28
PR 9878 merged miss-islington, 2018-10-14 21:03
PR 9879 merged miss-islington, 2018-10-14 21:03
PR 9885 merged serhiy.storchaka, 2018-10-15 05:12
Messages (5)
msg327661 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2018-10-13 19:18
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.
msg327721 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2018-10-14 21:03
New changeset e890421e334ccf0c000c6b29c4a521d86cd12f47 by Serhiy Storchaka in branch 'master':
bpo-34974: Do not replace unexpected errors in bytes() and bytearray(). (GH-9852)
https://github.com/python/cpython/commit/e890421e334ccf0c000c6b29c4a521d86cd12f47
msg327722 - (view) Author: miss-islington (miss-islington) Date: 2018-10-14 21:26
New changeset 1370832af24cc6f0f464354b9ec3ecdb343d35ce by Miss Islington (bot) in branch '3.7':
bpo-34974: Do not replace unexpected errors in bytes() and bytearray(). (GH-9852)
https://github.com/python/cpython/commit/1370832af24cc6f0f464354b9ec3ecdb343d35ce
msg327723 - (view) Author: miss-islington (miss-islington) Date: 2018-10-14 21:32
New changeset 08ba7eb89d5353af31ef9e66a5337abea1b676ef by Miss Islington (bot) in branch '3.6':
bpo-34974: Do not replace unexpected errors in bytes() and bytearray(). (GH-9852)
https://github.com/python/cpython/commit/08ba7eb89d5353af31ef9e66a5337abea1b676ef
msg327730 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2018-10-15 05:46
New changeset 43308dfc3335906cfefe9f14a44e468935f3c321 by Serhiy Storchaka in branch '2.7':
[2.7] bpo-34974: Do not replace unexpected errors in bytearray(). (GH-9852) (GH-9885)
https://github.com/python/cpython/commit/43308dfc3335906cfefe9f14a44e468935f3c321
History
Date User Action Args
2018-10-15 05:46:54serhiy.storchakasetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2018-10-15 05:46:22serhiy.storchakasetmessages: + msg327730
2018-10-15 05:14:59serhiy.storchakasetversions: + Python 2.7
2018-10-15 05:12:17serhiy.storchakasetpull_requests: + pull_request9248
2018-10-14 21:32:07miss-islingtonsetmessages: + msg327723
2018-10-14 21:26:32miss-islingtonsetnosy: + miss-islington
messages: + msg327722
2018-10-14 21:03:24miss-islingtonsetpull_requests: + pull_request9242
2018-10-14 21:03:17miss-islingtonsetpull_requests: + pull_request9241
2018-10-14 21:03:01serhiy.storchakasetmessages: + msg327721
2018-10-13 19:28:24serhiy.storchakasetkeywords: + patch
stage: patch review
pull_requests: + pull_request9222
2018-10-13 19:18:56serhiy.storchakacreate