Should django File wrapper support .next()?

120 views
Skip to first unread message

Cristiano Coelho

unread,
Jul 27, 2017, 12:24:13 PM7/27/17
to Django developers (Contributions to Django itself)
Hello,

I have recently found an interesting issue, using a project that relies on different storage backends, when switching from a custom one to django's file system storage, I found that existing code that would iterate files with the next() call would start to fail, since although django's File is iterable, it doesn't define the next method.

Now I'm wondering if this is on purpose, or a bug. It's odd that every stream (or almost) from the python library such as everything from the .io module or simply the object returned by open() (python 2) supports the next call but the django File wrapper doesn't.

This happened with django 1.10 and I believe it wasn't changed with Django 1.11

Adam Johnson

unread,
Aug 15, 2017, 6:21:08 PM8/15/17
to django-d...@googlegroups.com
The next() method is gone from the return value of open() in Python 3 and I don't think it was every necessary. As per the iterator protcool ( https://docs.python.org/2/library/stdtypes.html#iterator-types ), __iter__ is meant to return an iterator over the object, and that is what the next() method - __next__ on Python 3 - should be attached to. Django's File.__iter__ is a generator function, thus it returns a generator, with comes with a next()/__next__().

I think you should just try iterating your files by calling iter() on them first, it's Python 3 compatible too.

--
You received this message because you are subscribed to the Google Groups "Django developers (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-developers+unsubscribe@googlegroups.com.
To post to this group, send email to django-developers@googlegroups.com.
Visit this group at https://groups.google.com/group/django-developers.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/09ffad76-e473-4593-ac84-4bca7f76e92c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Adam

Cristiano Coelho

unread,
Aug 16, 2017, 8:55:43 AM8/16/17
to Django developers (Contributions to Django itself)
I forgot about django dropping support for Python2 so I guess you are right this doesn't make much sense if next() is removed, but for Python2 it would make sense for django's File object to have the same api as the object returned by open().



El martes, 15 de agosto de 2017, 19:21:08 (UTC-3), Adam Johnson escribió:
The next() method is gone from the return value of open() in Python 3 and I don't think it was every necessary. As per the iterator protcool ( https://docs.python.org/2/library/stdtypes.html#iterator-types ), __iter__ is meant to return an iterator over the object, and that is what the next() method - __next__ on Python 3 - should be attached to. Django's File.__iter__ is a generator function, thus it returns a generator, with comes with a next()/__next__().

I think you should just try iterating your files by calling iter() on them first, it's Python 3 compatible too.
On 27 July 2017 at 17:24, Cristiano Coelho <cristia...@gmail.com> wrote:
Hello,

I have recently found an interesting issue, using a project that relies on different storage backends, when switching from a custom one to django's file system storage, I found that existing code that would iterate files with the next() call would start to fail, since although django's File is iterable, it doesn't define the next method.

Now I'm wondering if this is on purpose, or a bug. It's odd that every stream (or almost) from the python library such as everything from the .io module or simply the object returned by open() (python 2) supports the next call but the django File wrapper doesn't.

This happened with django 1.10 and I believe it wasn't changed with Django 1.11

--
You received this message because you are subscribed to the Google Groups "Django developers (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-develop...@googlegroups.com.
To post to this group, send email to django-d...@googlegroups.com.



--
Adam

Adam Johnson

unread,
Aug 18, 2017, 7:21:46 AM8/18/17
to django-d...@googlegroups.com
I looked a bit more and the reason file objects have next() in Py2 is because "A file object is its own iterator" ( https://docs.python.org/2/library/stdtypes.html#file.next ). This is still the case in Py3 - "IOBase (and its subclasses) supports the iterator protocol" ( https://docs.python.org/3/library/io.html#io.IOBase ).

So yes there is a disparity in Django's File. After a quick look at the code I'm not sure if it's possible to modify it to match though because of all the logic embedded in __iter__ but it's worth having a try.

To unsubscribe from this group and stop receiving emails from it, send an email to django-developers+unsubscribe@googlegroups.com.
To post to this group, send email to django-developers@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
Adam
Reply all
Reply to author
Forward
0 new messages