Code

Opened 3 years ago

Closed 3 years ago

Last modified 3 years ago

#16569 closed Bug (invalid)

NamedTemporaryFile is missing the delete argument

Reported by: LvanderRee Owned by: nobody
Component: Core (Other) Version: 1.3
Severity: Normal Keywords: NamedTemporaryFile, delete
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

I am developing under Linux, while my test server is running Windows, which made me aware of the following error.

I wrote the following piece of code:
[code]
from django.core.files.temp import NamedTemporaryFile

img_temp = NamedTemporaryFile(delete=True)
code
which works fine under Linux, but fails under Windows.

I looked at the code in the django.core.files.temp module, and see it makes a distinction between Windows and other systems:

[code]
if os.name == 'nt':
code
in which case NamedTemporaryFile = TemporaryFile as defined above it.

However, according to http://docs.python.org/library/tempfile.html#tempfile.NamedTemporaryFile NamedTemporaryFile should be able to handle delete as well!

A solution would be to define NamedTemporaryFile as follows:
[code]

class NamedTemporaryFile(TemporaryFile):

def init(self, kwargs):

kwargs.pop('delete', True) # TODO: handle this delete flag
super(NamedTemporaryFile,self).init(kwargs)

code
But I haven't thought about how to handle the delete argument.

Attachments (0)

Change History (3)

comment:1 Changed 3 years ago by aaugustin

  • Has patch unset
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Resolution set to invalid
  • Status changed from new to closed

NamedTemporaryFile isn't a public API — there isn't any reference to this class in the documentation: https://docs.djangoproject.com/search/?q=NamedTemporaryFile&release=5

Django doesn't guarantee any consistency, usability or stability of the internal APIs. Use them at your own risk.

On a side note, delete=True is the default: http://docs.python.org/library/tempfile#tempfile.NamedTemporaryFile Just don't specify it and you'll be fine.

comment:2 Changed 3 years ago by mtredinnick

In [16694]:

Clarify the documentation around SQLite and case-sensitive string matching.

This was still causing some confusion, so I rewrote the section in the
database notes to encompass both substring matching and non-ASCII
case-insensitive equality checks, as well as putting in a stronger
callout on the "contains" filter.

Refs #16569.

comment:3 Changed 3 years ago by mtredinnick

(Ignore previous comment; should have gone to #15659.)

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.