resolve NameError: name 'a1' is not defined in modeltests.str

Create issue
Issue #16 resolved
c4nhukha created an issue

current error message:

{{{ #!pycon

====================================================================== FAIL: Doctest: modeltests.str.models.test.API_TESTS


Traceback (most recent call last): File "/usr/local/lib/python3.1/dist-packages/django/test/_doctest.py", line 2187, in runTest raise self.failureException(self.format_failure(new.getvalue())) AssertionError: Failed doctest test for modeltests.str.models.test.API_TESTS File "/home/andrew/django-3k/build/tests/modeltests/str/models.py", line unknown line number, in API_TESTS


File "/home/andrew/django-3k/build/tests/modeltests/str/models.py", line ?, in modeltests.str.models.test.API_TESTS Failed example: a1 = InternationalArticle(headline=u'Girl wins €12.500 in lottery', pub_date=datetime(2005, 7, 28)) Exception raised: 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.str.models.test__.API_TESTS[5]>", line 1 a1 = InternationalArticle(headline=u'Girl wins €12.500 in lottery', pub_date=datetime(2005, 7, 28)) ^ SyntaxError: invalid syntax


File "/home/andrew/django-3k/build/tests/modeltests/str/models.py", line ?, in modeltests.str.models.test.API_TESTS Failed example: str(a1) Exception raised: 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.str.models.test__.API_TESTS[6]>", line 1, in <module> str(a1) NameError: name 'a1' is not defined

}}}

Comments (7)

  1. Johan Harjono

    tip: if you use the Code and Syntax highlight that bitbucket provides, the error trace would look more legible

  2. Johan Harjono

    Also, just from the console error message I would guess that the only problem is the SyntaxError there

    a1 = InternationalArticle(headline=u'Girl wins €12.500 in lottery', pub_date=datetime(2005, 7, 28))
    

    that would fail because the u'*' syntax is not supported in Python 3, consequently a1 does not get initialized and then you have NameError

    However I'm just guessing, so you should verify it

  3. Martin von Löwis

    I agree that the likely cause is the syntax error. This is something that 2to3 could have fixed (removing the u prefix).

  4. c4nhukha reporter

    running 2to3 with -d did fix the "u" in a1, but I'm now seeing an error with the expected value. The expected value is a unicode string for the euro sign but the actual value is the euro sign.

    Failed example:

    str(a1)

    Expected:

    'Girl wins \xe2\x82\xac12.500 in lottery'

    Got:

    'Girl wins €12.500 in lottery'

  5. c4nhukha reporter

    Adding the code below to setup.py will fix the problem, but there might be better ways to do it using the 2to3 library.

    os.system(2to3 -d modeltest.py)
    
    input = open("models.py").read()
    input = input.replace('\\xe2\\x82\\xac', '€')
    output = open("models.py", 'w')
    output.write(input)
    output.close()
    
  6. Log in to comment