resolve FieldDoesNotExist error in modeltests.basic

Issue #8 resolved
Johan Harjono created an issue

{{{

!py3

File "/home/johan/workspace/fiji/build/tests/modeltests/basic/models.py", line ?, in modeltests.basic.models.test.API_TESTS
Failed example:
Article.objects.dates('invalid_field', 'year')
Expected:
Traceback (most recent call last):
...
FieldDoesNotExist: Article has no field named 'invalid_field'
Got:
Traceback (most recent call last):
File "/usr/local/lib/python3.1/dist-packages/django/test/_doctest.py", line 1275, in run
compileflags, 1), test.globs)
File "<doctest modeltests.basic.models.
test__.API_TESTS[78]>", line 1, in <module>
Article.objects.dates('invalid_field', 'year')
File "/usr/local/lib/python3.1/dist-packages/django/db/models/manager.py", line 123, in dates
return self.get_query_set().dates(args, *kwargs)
File "/usr/local/lib/python3.1/dist-packages/django/db/models/query.py", line 521, in dates
_field_name=field_name, _kind=kind, _order=order)
File "/usr/local/lib/python3.1/dist-packages/django/db/models/query.py", line 744, in _clone
c._setup_query()
File "/usr/local/lib/python3.1/dist-packages/django/db/models/query.py", line 985, in _setup_query
field = self.model._meta.get_field(self._field_name, many_to_many=False)
File "/usr/local/lib/python3.1/dist-packages/django/db/models/options.py", line 277, in get_field
raise FieldDoesNotExist('%s has no field named %r' % (self.object_name, name))
django.db.models.fields.FieldDoesNotExist: Article has no field named 'invalid_field'
}}}

Comments (11)

  1. Martin von Löwis

    Notice that FieldDoesNotExist is indeed the right exception. The question really is why it fails to match the expected output with the actual output - they ought to match.

  2. Johan Harjono reporter

    What I find interesting is the fact that the expected output is:

    FieldDoesNotExist: Article has no field named 'invalid_field'
    

    but instead it gets

    django.db.models.fields.FieldDoesNotExist: Article has no field named 'invalid_field
    

    The only difference is the django.db.models.field prefix. This suggests that the local installation of Django in /usr/local/lib/python3.1 defines a FieldDoesNotExist as well which apparently is distinct from the FieldDoesNotExist in Python. Namespace or scoping problem?

  3. c4nhukha

    Below are my findings for the traceback mismatch found in modeltests. The problem is that python2.6 and python3.1 have a slightly different definition of the function format_exception_only in traceback.py. In 2.6, the exception is defined as DoesNotExist. However in python3.1, the exception is defined as classname concatenated with DoesNotExist". As a result, the exception message in python3.1 is "modeltests.one_to_one.models.DoesNotExist: Restaurant matching query does not exist." while the exception message in python2.6 is "DoesNotExist: Restaurant matching query does not exist."

    python3.1
    exception	tuple: (<class 'modeltests.one_to_one.models.DoesNotExist'>, DoesNotExist('Restaurant matching query does not exist.',), <traceback object at 0x37794d0>)	
    	0	type: <class 'modeltests.one_to_one.models.DoesNotExist'>	
    	1	DoesNotExist: Restaurant matching query does not exist.	
    	2	traceback: <traceback object at 0x37794d0>	
    	__len__	int: 3	
    
    def __run(self, test, compileflags, out):
        exc_msg = traceback.format_exception_only(*exception[:2])[-1]
    
    python3.1/traceback.py
    def format_exception_only(etype, value):
    stype = etype.__name__
        smod = etype.__module__
        if smod not in ("__main__", "builtins"):
            stype = smod + '.' + stype
    
        if not issubclass(etype, SyntaxError):
            return [_format_final_exc_line(stype, value)]
    
    #<stype is> stype	str: modeltests.one_to_one.models.DoesNotExist	
    #<value is> value	DoesNotExist: Restaurant matching query does not exist.	
    
    
    python2.6
    exception	tuple: (<class 'modeltests.one_to_one.models.DoesNotExist'>, DoesNotExist('Restaurant matching query does not exist.',), <traceback object at 0x3500ab8>)	
    	0	type: <class 'modeltests.one_to_one.models.DoesNotExist'>	
    	1	DoesNotExist: Restaurant matching query does not exist.	
    	2	traceback: <traceback object at 0x3500ab8>	
    	__len__	int: 3	
    
    def __run(self, test, compileflags, out):
        exc_msg = traceback.format_exception_only(*exception[:2])[-1]
    
    python2.6/traceback.py
    def format_exception_only(etype, value):
        stype = etype.__name__
    
        if not issubclass(etype, SyntaxError):
            return [_format_final_exc_line(stype, value)]
    
    #<stype is> stype	str: DoesNotExist	
    #<value is> value	DoesNotExist: Restaurant matching query does not exist.	
    
    
    def _format_final_exc_line(etype, value):
        """Return a list of a single line -- normal case for format_exception_only"""
        valuestr = _some_str(value)
        if value is None or not valuestr:
            line = "%s\n" % etype
        else:
            line = "%s: %s\n" % (etype, valuestr)
        return line
    
  4. Log in to comment