PythonCharmers/python-future

newbytes is unable to handle a generator as a parameter

Open

#288 建立於 2017年6月21日

在 GitHub 查看
 (0 留言) (1 反應) (0 負責人)Python (327 fork)batch import
0.20bughelp wanted

倉庫指標

Star
 (1,172 star)
PR 合併指標
 (30 天內沒有已合併 PR)

描述

After only checking that the parameter is Iterable, newbytes then checks the parameters len(). https://github.com/PythonCharmers/python-future/blob/39a066ed8c29a0b3a32adac2ffd407119fe9ea6d/src/future/types/newbytes.py#L106

https://repl.it/IwSF/2

print(repr(bytes.newbytes(x for x in range(0))))
Traceback (most recent call last):
  File "main.py", line 4, in <module>
    print(repr(bytes.newbytes(x for x in range(0))))
  File "/home/runner/bytes.py", line 103, in __new__
    if len(args[0]) == 0:
TypeError: object of type 'generator' has no len()

Including a check for Sized as well avoids this.

if isinstance(args[0], Sized) and len(args[0]) == 0:

https://repl.it/IwSF/1

貢獻者指南