Opened 13 years ago

Closed 13 years ago

#14902 closed (invalid)

Won't delete files with accents (or other unicode characters)

Reported by: typeshige Owned by: nobody
Component: Core (Other) Version: 1.2
Severity: Keywords: unicode
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 did some searches and didn't find it. I have a model with an ImageField. When I try to delete, it fails whenever the filename has accents. I'm using Linux.

/home/django/.virtualenvs/astrobiology/lib/python2.6/site-packages/django/core/files/storage.pyc in delete(self, name)

190 name = self.path(name)
191 # If the file exists, delete it from the filesystem.

--> 192 if os.path.exists(name):

193 os.remove(name)
194

/home/django/.virtualenvs/astrobiology/lib/python2.6/genericpath.pyc in exists(path)

16 """Test whether a path exists. Returns False for broken symbolic links"""
17 try:

---> 18 st = os.stat(path)

19 except os.error:
20 return False

UnicodeEncodeError: 'ascii' codec can't encode character u'\xf3' in position 103: ordinal not in range(128)

Change History (2)

comment:1 by typeshige, 13 years ago

I've been doing more testing and it seems to fail on 'normal' filenames sometimes too.

It seem to fail on the same filename whenever doing something like:

Image.objects.all().delete()

or

import os
for i in Image.objects.all():
    print i.image.path
    os.stat(i.image.path)

It fails on the same file each time, so if I isolate the offending object like this:

t = Image.objects.all(title='the problem file')
os.stat(t.image.path)

there is no problem. How can this be? Are the loops too fast?

My model Image looks like this:

class Image(models.Model):
    title = models.CharField(max_length=255)
    image = models.ImageField(upload_to=get_file_path)

comment:2 by Karen Tracey, 13 years ago

Resolution: invalid
Status: newclosed

Getting a UnicodeEncodeError from file system function like os.stat means your server's environment is not set up correctly to allow passing unicode strings containing non-ASCII characters to the base file system functions. See: http://docs.djangoproject.com/en/dev/howto/deployment/modpython/#if-you-get-a-unicodeencodeerror (though that is in the mod_python doc it is not mod_python specific).

Note: See TracTickets for help on using tickets.
Back to Top