Code


Version 2 (modified by Alex, 4 years ago) (diff)

Note another circumstance.

Certain tests in Django's built-in test suite fail if your system isn't set up correctly. Until we have a better system for recording these semi-expected failures, document them here.

File names and locale

Symptom

The file_upload tests fail with something like:

======================================================================
ERROR: test_unicode_file_name (regressiontests.file_uploads.tests.FileUploadTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/buildbot/slave-py2.5/parts/ubuntu9.10-py2.5-sqlite/django-trunk_ubuntu9.10-py2.5-sqlite/build/tests/regressiontests/file_uploads/tests.py", line 69, in test_unicode_file_name
    response = self.client.post('/file_uploads/unicode_name/', post_data)
  File "/home/buildbot/slave-py2.5/parts/ubuntu9.10-py2.5-sqlite/django-trunk_ubuntu9.10-py2.5-sqlite/build/django/test/client.py", line 318, in post
    response = self.request(**r)
  File "/home/buildbot/slave-py2.5/parts/ubuntu9.10-py2.5-sqlite/django-trunk_ubuntu9.10-py2.5-sqlite/build/django/core/handlers/base.py", line 101, in get_response
    response = callback(request, *callback_args, **callback_kwargs)
  File "/home/buildbot/slave-py2.5/parts/ubuntu9.10-py2.5-sqlite/django-trunk_ubuntu9.10-py2.5-sqlite/build/tests/regressiontests/file_uploads/views.py", line 64, in file_upload_unicode_name
    obj = FileModel.objects.create(testfile=uni_named_file)
  File "/home/buildbot/slave-py2.5/parts/ubuntu9.10-py2.5-sqlite/django-trunk_ubuntu9.10-py2.5-sqlite/build/django/db/models/manager.py", line 138, in create
    return self.get_query_set().create(**kwargs)
  File "/home/buildbot/slave-py2.5/parts/ubuntu9.10-py2.5-sqlite/django-trunk_ubuntu9.10-py2.5-sqlite/build/django/db/models/query.py", line 350, in create
    obj.save(force_insert=True, using=self.db)
  File "/home/buildbot/slave-py2.5/parts/ubuntu9.10-py2.5-sqlite/django-trunk_ubuntu9.10-py2.5-sqlite/build/django/db/models/base.py", line 434, in save
    self.save_base(using=using, force_insert=force_insert, force_update=force_update)
  File "/home/buildbot/slave-py2.5/parts/ubuntu9.10-py2.5-sqlite/django-trunk_ubuntu9.10-py2.5-sqlite/build/django/db/models/base.py", line 510, in save_base
    for f in meta.local_fields if not isinstance(f, AutoField)]
  File "/home/buildbot/slave-py2.5/parts/ubuntu9.10-py2.5-sqlite/django-trunk_ubuntu9.10-py2.5-sqlite/build/django/db/models/fields/files.py", line 255, in pre_save
    file.save(file.name, file, save=False)
  File "/home/buildbot/slave-py2.5/parts/ubuntu9.10-py2.5-sqlite/django-trunk_ubuntu9.10-py2.5-sqlite/build/django/db/models/fields/files.py", line 92, in save
    self.name = self.storage.save(name, content)
  File "/home/buildbot/slave-py2.5/parts/ubuntu9.10-py2.5-sqlite/django-trunk_ubuntu9.10-py2.5-sqlite/build/django/core/files/storage.py", line 47, in save
    name = self.get_available_name(name)
  File "/home/buildbot/slave-py2.5/parts/ubuntu9.10-py2.5-sqlite/django-trunk_ubuntu9.10-py2.5-sqlite/build/django/core/files/storage.py", line 73, in get_available_name
    while self.exists(name):
  File "/home/buildbot/slave-py2.5/parts/ubuntu9.10-py2.5-sqlite/django-trunk_ubuntu9.10-py2.5-sqlite/build/django/core/files/storage.py", line 196, in exists
    return os.path.exists(self.path(name))
  File "/usr/lib/python2.5/posixpath.py", line 171, in exists
    st = os.stat(path)
UnicodeEncodeError: 'ascii' codec can't encode characters in position 43-44: ordinal not in range(128)

Solution

You'll see this if your locale is C (or if your locale is unassigned and locale.getpreferredlocale() is ASCII). Fix your locale with something like export LC_ALL=en_US.utf8. On some OSes you'll need to install "language packs" -- on Ubuntu, it's aptitude install language-pack-en-base.