bobber6467 / python-nose

Automatically exported from code.google.com/p/python-nose
0 stars 0 forks source link

Problems with i18n , gettext and the "_" function #373

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?
1. Download w3af's source code
2. Run nosetests --rednose --with-doctest --doctest-tests -e 'extlib.*' 

What is the expected output? What do you see instead?
I expect nose to run the different modules and show the tests that failed, 
instead I'm getting:

-----------------------------------------------------------------------------
17) ERROR: Failure: NameError (name '_' is not defined)

   Traceback (most recent call last):
    /usr/lib/python2.5/site-packages/nose-0.11.3-py2.5.egg/nose/loader.py line 382 in loadTestsFromName
      addr.filename, addr.module)
    /usr/lib/python2.5/site-packages/nose-0.11.3-py2.5.egg/nose/importer.py line 39 in importFromPath
      return self.importFromDir(dir_path, fqname)
    /usr/lib/python2.5/site-packages/nose-0.11.3-py2.5.egg/nose/importer.py line 86 in importFromDir
      mod = load_module(part_fqname, fh, filename, desc)
    core/ui/gtkUi/reqResViewer.py line 43 in <module>
      from .export_request import export_request
    core/ui/gtkUi/export_request.py line 25 in <module>
      from .encdec import SimpleTextView
    core/ui/gtkUi/encdec.py line 502 in <module>
      (_("URL Encode"),                   urlencode),
   NameError: name '_' is not defined

These errors are generated because my tool has some i18n features. These 
features are added with the gettext module. This module adds the "_" function 
to globals when I run this in the init script:

# Translation stuff
gettext.install('w3af', 'locales/')

The "_" is defined at runtime, and nose is not being able to handle that.

What version of the product are you using? On what operating system?
Latest version, with the rednose plugin installed.
Ubuntu 8.04.
Python 2.5.2

Please provide any additional information below.
Feel free to contact me via email (andres.riancho !at! gmail . com)

Original issue reported on code.google.com by andres.riancho@gmail.com on 9 Nov 2010 at 3:09

GoogleCodeExporter commented 8 years ago
Extra note: We don't need a final solution, a workaround could be enough for 
now :)

Original comment by andres.riancho@gmail.com on 9 Nov 2010 at 3:13

GoogleCodeExporter commented 8 years ago
Any comments about this issue?

Original comment by andres.riancho@gmail.com on 12 Nov 2010 at 11:18

GoogleCodeExporter commented 8 years ago
Fixed by adding this code to the "__init__.py" file of the package that was 
raising all those errors:

"""
# Intended to be called by nose.
def setUpPackage():
    # Hack to init i18n function
    import __builtins__
    __builtins__.__dict__['_'] = lambda x: x
"""

Original comment by andres.riancho@gmail.com on 30 Nov 2010 at 5:13

GoogleCodeExporter commented 8 years ago
Andres, your fix did not work for me. First, there is a typo... it's 
__builtin__, not __builtins__, but even after making that fix, I still get the 
same NameError: name '_' is not defined message when running nosetests.

Any thoughts or ideas?

Original comment by jaypi...@gmail.com on 10 Dec 2010 at 4:24

GoogleCodeExporter commented 8 years ago
Following up, I was able to get nosetests to work using the following in a 
/tests/__init__.py file where tests/ contains all the test case files.

My project is structured like so:

/src
  /project
  /tests

Putting the below code in /src/project/__init__.py did not work, however 
putting it in /src/tests/__init__.py did work...

import __builtin__
setattr(__builtin__, '_', lambda x: x)

If I put the above inside a setup or setUpProject function, it does NOT work.

Just a heads up.

Cheers,
jay

Original comment by jaypi...@gmail.com on 10 Dec 2010 at 4:55