New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Strange Exception from copying an iterator #58558
Comments
Running: --------------------------------------------------------------------------- TypeError Traceback (most recent call last)
T:\languages\Python27\Scripts\<ipython-input-2-4b0069a09ded> in <module>()
----> 1 copy.copy(iter([1,2,3])) T:\languages\Python27\lib\copy.pyc in copy(x) T:\languages\Python27\lib\copy.pyc in _reconstruct(x, info, deep, memo) T:\languages\Python27\lib\copy_reg.pyc in __newobj__(cls, *args) TypeError: object.__new__(listiterator) is not safe, use listiterator.__new__() Either this is a bug or not a clear error message in the exception |
I get a normal exception. |
@ramchandra: I think you referring to the traceback format (which is indeed less useful than a normal Python traceback in the context of this tracker). The OP, however, is referring to the exception itself: TypeError: object.__new__(listiterator) is not safe, use listiterator.__new__() This is indeed a bit unexpected, though I don't know that copying iterators is actually supported. |
C:\Users\Jakob>python -c "import copy; copy.copy(iter([1,2,3]))"
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "T:\languages\Python27\lib\copy.py", line 96, in copy
return _reconstruct(x, rv, 0)
File "T:\languages\Python27\lib\copy.py", line 329, in _reconstruct
y = callable(*args)
File "T:\languages\Python27\lib\copy_reg.py", line 93, in __newobj__
return cls.__new__(cls, *args)
TypeError: object.__new__(listiterator) is not safe, use listiterator.__new__()
C:\Users\Jakob>python3 -c "import copy; copy.copy(iter([1,2,3]))"
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "T:\languages\Python32\lib\copy.py", line 97, in copy
return _reconstruct(x, rv, 0)
File "T:\languages\Python32\lib\copy.py", line 285, in _reconstruct
y = callable(*args)
File "T:\languages\Python32\lib\copyreg.py", line 88, in __newobj__
return cls.__new__(cls, *args)
TypeError: object.__new__(list_iterator) is not safe, use list_iterator.__new__() Pure python traceback. Just for clarity. |
BTW, can we add support for copying iterators by using itertools.tee |
Title corrected. Non-iterator iterables are usually easy to copy. I believe the idea of making iterators copyable has been rejected on one of the lists because it is probably not possible in general. So I suspect that raising an error for copy.copy(iter([])) is correct. The error message amounts to saying make a new instance with the class constructor. That is usually correct when copy is not possible, but not in this case. ListIterator.__new__ is the inherited object.__new__. It appears that ListIterators can only be constructed by list.__iter__. But I doubt that the code that raises the error message can know that. |
Before anyone else rushes of to do this, can I? I really want to break into python-dev and this might be my chance. |
Please, give it a try. But also be prepared for it being harder than it looks; the problem is that there may be very limited knowledge available where the error is generated. (I don't know; I haven't looked at the code and am not likely to...I usually leave reviews of C code to our C experts.) |
Actually the tp_new field of list iterator class is NULL. Unpickler raises other error in such case (see bpo-24900 for example).
Backported to 2.7 the part of the patch for bpo-22995 fixes this issue. >>> copy.copy(iter([]))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/serhiy/py/cpython-2.7/Lib/copy.py", line 88, in copy
rv = reductor(2)
TypeError: can't pickle listiterator objects |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: