Problem with mixing doctest with gettext _()

Pierre Rouleau prouleau at impathnetworks.com
Thu Feb 26 23:12:46 EST 2004


I have a problem writing self-testable modules using doctest when these 
modules have internationalized strings using gettext _('...').

- The main module of an application (say app.py) calls gettext.install() 
to install the special _ function inside Python builtin.  Other modules, 
taken from a general purpose collection of Python modules, also support 
internationalisation and doctest testing.

For example:

utstring.py would contain some code like this:

def onOffStr(isOn) :
     """Return the "ON" string for True, "OFF" for False.

     **Example**

     >>> onOffStr(True)
     u'ON'
     >>> onOffStr(False)
     u'OFF'
     >>>
     """
     if isOn:
         return _(u"ON")  # notice the underscore
     else:
         return _(u"OFF")



The utstring module does not call any of the gettext calls, because some 
other module does it when the application runs.  So the doctest fails:

*****************************************************************
Failure in example: onOffStr(True)
from line #4 of utstring.onOffStr
Exception raised:
Traceback (most recent call last):
   File "C:\Python23\Lib\doctest.py", line 442, in _run_examples_inner
     compileflags, 1) in globs
   File "<string>", line 1, in ?
   File "C:\dev\python\utstring.py", line 513, in onOffStr
     return _(u"ON")
TypeError: 'tuple' object is not callable
*****************************************************************

I tried to define a _() function when testing with the code below but 
the doctest still fails.  The following code is at the end of my 
utstring.py module

def _test():
     """_test() perform docstring test"""

     import doctest, utstring
     return doctest.testmod(utstring)

if __name__ == "__main__":
     def _(aString):
	# dummy _() attempting to get doctest to pass.
         return aString

     _test()


----------

Does anyone know why the doctest still fails when I define the dummy _() 
function?


Thanks in advance.

Pierre





More information about the Python-list mailing list