Problem with mixing doctest with gettext _()

Pierre Rouleau prouleau at impathnetworks.com
Fri Feb 27 11:46:02 EST 2004


Pierre Rouleau wrote:

> Skip Montanaro wrote:
> 
>>     Pierre> I tried to define a _() function when testing with the code
>>     Pierre> below but the doctest still fails.  The following code is 
>> at the
>>     Pierre> end of my utstring.py module
>>     ...
>>
>> I suspect it's because your dummy _ is not in builtins.  Why not just 
>> call
>> gettext.install() where you are currently defining _?
>>
> 
> Skip, this looks like the way to go.
> 
> I was trying to avoid it because the translation files for these library 
> modules are constructed at the application level somewhere else.  But I 
> was also considering creating a translation library for these module, so 
> I guess that is one more incentive to do so...
> 

I implemented the dictionary and use it in the code just fine, but the 
doctest still fails :(

My teststr.py module is:

#--[---------------------------------------------------------------
if __name__ == "__main__":
     # install gettext for testing this module because of the string 
translation
     # performed in the code.
     import gettext
     gettext.install('impathpl', './locale', unicode=False)
     presLan_en = gettext.translation('impathpl', "./locale", 
languages=['en'])
     presLan_en.install()

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

     **Example**

     >>> onOffStr(True)
     'ON'
     >>> onOffStr(False)
     'OFF'
     >>>
     """
     if isOn:
         return _("ON")
     else:
         return _("OFF")


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

     import doctest, teststr
     return doctest.testmod(teststr)

if __name__ == "__main__":
     _test()

#--]-------------------------------------------------


Running the following script shows that the module runs OK:

[Shell buffer started: python.exe]
Python 2.3.3 (#51, Dec 18 2003, 20:22:39) [MSC v.1200 32 bit (Intel)] on 
win32
Type "help", "copyright", "credits" or "license" for more information.
 >>> import teststr
 >>> import gettext
 >>> gettext.install('impathpl', './locale', unicode=False)
 >>> presLan_en = gettext.translation('impathpl', "./locale", 
languages=['en'])
 >>> presLan_en.install()
 >>> teststr.onOffStr(True)
'ON'
 >>>


BUT, the doctest fails:

D:\dev\python>teststr
*****************************************************************
Failure in example: onOffStr(False)
from line #6 of teststr.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 "teststr.py", line 23, in onOffStr
     return _("OFF")
TypeError: 'str' object is not callable
*****************************************************************
1 items had failures:
    1 of   2 in teststr.onOffStr
***Test Failed*** 1 failures.


I'm still puzzled...

Pierre



More information about the Python-list mailing list