Problem with mixing doctest with gettext _()

Pierre Rouleau prouleau at
Fri Feb 27 05:12:46 CET 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 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: would contain some code like this:

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


     >>> onOffStr(True)
     >>> onOffStr(False)
     if isOn:
         return _(u"ON")  # notice the underscore
         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\", line 442, in _run_examples_inner
     compileflags, 1) in globs
   File "<string>", line 1, in ?
   File "C:\dev\python\", 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 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



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

Thanks in advance.


More information about the Python-list mailing list