#27777: File object does not consistently open itself in context manager use
-------------------------------------+-------------------------------------
     Reporter:  Raphael Gaschignard  |                    Owner:  nobody
         Type:  Bug                  |                   Status:  new
    Component:  File                 |                  Version:  1.10
  uploads/storage                    |
     Severity:  Normal               |               Resolution:
     Keywords:                       |             Triage Stage:
                                     |  Unreviewed
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------
Changes (by Raphael Gaschignard):

 * status:  closed => new
 * resolution:  needsinfo =>


Comment:

 Sorry,  I chose the wrong property. Here's two examples of behavior I
 found that didn't make sense to me at first.

 {{{ ContentFile}}} does not properly seek back to 0 on context manager re-
 use.

 {{{
 In [8]: from django.core.files.base import ContentFile

 In [9]: my_file = ContentFile("Hi there")

 In [10]: with my_file as f: print(f.read())
 Hi there

 In [11]: with my_file as f: print(f.read())
 # empty string, no content
 }}}

 for {{{File}}}s that use actual files in their backend, multiple context
 manager usages results in a value error (I/O operation on closed file)

 {{{

 In [12]: from django.core.files.base import File

 In [13]: my_file = File(open('hi.txt'))

 In [14]: with my_file as f: print(f.read())
     ...: with my_file as f: print(f.read())
     ...:
 Hi

 ---------------------------------------------------------------------------
 ValueError                                Traceback (most recent call
 last)
 <ipython-input-14-ea725ad0698a> in <module>()
       1 with my_file as f: print(f.read())
 ----> 2 with my_file as f: print(f.read())
       3

 ValueError: I/O operation on closed file.
 }}}

 I figured out that this behaviour *is* consistent with the behavior of
 python's {{{file}}} object. But given that Django's {{{File}}} object
 includes an "idempotent" {{{open}}}, it would seem useful for the context
 manager to call {{{open}}}, so that we open the file and seek back to 0.

--
Ticket URL: <https://code.djangoproject.com/ticket/27777#comment:3>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-updates+unsubscr...@googlegroups.com.
To post to this group, send email to django-updates@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/062.96c5f26726ffc037f49e19570b9e47cc%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to