Code

Opened 17 months ago

Closed 17 months ago

Last modified 17 months ago

#19833 closed Bug (fixed)

Django's test framework fails when using unicode strings in settings.py

Reported by: dbrgn Owned by: nobody
Component: Testing framework Version: 1.4
Severity: Release blocker Keywords: unicode, test runner, py3k
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: yes UI/UX: no

Description

When specifying a non-standard test runner using an unicode string in settings.py (TEST_RUNNER = u'disccover_runner.DiscoverRunner') or when using from __future__ import unicode_literals, the test management command breaks.

This is the "faulty" code snippet (test/utils.py):

def get_runner(settings, test_runner_class=None):                                                   
    if not test_runner_class:                                                                       
        test_runner_class = settings.TEST_RUNNER                                                    
                                                                                                    
    test_path = test_runner_class.split('.')                                                        
    # Allow for Python 2.5 relative paths                                                           
    if len(test_path) > 1:                                                                          
        test_module_name = '.'.join(test_path[:-1])                                                 
    else:                                                                                           
        test_module_name = '.'                                                                      
    test_module = __import__(test_module_name, {}, {}, test_path[-1])                                                                                 
    test_runner = getattr(test_module, test_path[-1])                                               
    return test_runner                                                                              

The problem is that a unicode string gets passed to __import__s fromlist argument. Apparently that won't work.

I'm not sure what the right solution is. Is it enough to just convert test_path[-1] to a str type? Are decoding errors possible in that case? Are there any helper functions in Django to do a conversion that works both in py2 and py3 (str() vs bytes())?

Bug tested on 1.4 and 1.5.

Attachments (0)

Change History (7)

comment:1 Changed 17 months ago by claudep

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Severity changed from Normal to Release blocker
  • Triage Stage changed from Unreviewed to Accepted

Are there any helper functions in Django to do a conversion that works both in py2 and py3 (str() vs bytes())?

Yes, see django.utils.encoding.force_str

comment:2 Changed 17 months ago by dbrgn

So django.utils.encoding.force_str(test_path[-1]) would be an acceptable solution? If yes, I'll provide a pull request on Github.

comment:3 Changed 17 months ago by dbrgn

Sorry for forgetting about the traceback, here it is:

$ cat config/settings.py | grep TEST_
TEST_RUNNER = 'discover_runner.DiscoverRunner'
TEST_DISCOVER_TOP_LEVEL = PROJECT_ROOT
TEST_DISCOVER_ROOT = PROJECT_ROOT
TEST_DISCOVER_PATTERN = 'test*.py'

$ ./manage.py test
Traceback (most recent call last):
  File "./manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/home/danilo/.virtualenvs/pcf/lib/python2.7/site-packages/django/core/management/__init__.py", line 453, in execute_from_command_line
    utility.execute()
  File "/home/danilo/.virtualenvs/pcf/lib/python2.7/site-packages/django/core/management/__init__.py", line 392, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/danilo/.virtualenvs/pcf/lib/python2.7/site-packages/django/core/management/commands/test.py", line 49, in run_from_argv
    super(Command, self).run_from_argv(argv)
  File "/home/danilo/.virtualenvs/pcf/lib/python2.7/site-packages/django/core/management/base.py", line 218, in run_from_argv
    parser = self.create_parser(argv[0], argv[1])
  File "/home/danilo/.virtualenvs/pcf/lib/python2.7/site-packages/django/core/management/commands/test.py", line 52, in create_parser
    test_runner_class = get_runner(settings, self.test_runner)
  File "/home/danilo/.virtualenvs/pcf/lib/python2.7/site-packages/django/test/utils.py", line 129, in get_runner
    test_module = __import__(test_module_name, {}, {}, test_path[-1])
TypeError: Item in ``from list'' not a string

comment:4 Changed 17 months ago by claudep

I wonder if your test runner path is correct. Try to use force_str(test_path[-1]) and see if you are not getting an AttributeError. In any case, I admit the AttributeError is more friendly than the TypeError, so let's go for this change (waiting for your tests, still).

comment:5 Changed 17 months ago by dbrgn

Yes, force_str solves the issue in this case.

comment:6 Changed 17 months ago by Claude Paroz <claude@…>

  • Resolution set to fixed
  • Status changed from new to closed

In 632361611c6386696dc525ad3aecf065e6ed98ee:

Fixed #19833 -- Fixed import parameter encoding in get_runner

Thanks Danilo Bargen for the report.

comment:7 Changed 17 months ago by Claude Paroz <claude@…>

In 41848b078a2f0402b23043da7cfb517d0a194aa3:

[1.5.x] Fixed #19833 -- Fixed import parameter encoding in get_runner

Thanks Danilo Bargen for the report.
Backport of 63236161 from master.

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.