Problem with mixing doctest with gettext _()

Pierre Rouleau prouleau at impathnetworks.com
Fri Feb 27 19:04:57 CET 2004


Pierre Rouleau wrote:

> Peter Otten wrote:
> 
>> Pierre Rouleau wrote:
>>
>>
>>> Skip Montanaro wrote:
>>>
>>>
>>>> If you really want a dummy _() you can also stuff your version into
>>>> builtins:
>>>>
>>>>    >>> import __builtin__
>>>>    >>> def foo(s): return s
>>>>    ...
>>>>    >>> __builtin__._ = foo
>>>>    >>> _
>>>>    <function foo at 0x1d6670>
>>>>    >>> _("hi")
>>>>    'hi'
>>>
>>>
>>> I tried that, but it only works for the first call...
>>
>>
>>
>> Setting __builtin__._ to the result of the last calculation is a side 
>> effect
>> of sys.displayhook. Therefore you need to change that too:
>>
>> Python 2.3.3 (#1, Jan  3 2004, 13:57:08)
>> [GCC 3.2] on linux2
>> Type "help", "copyright", "credits" or "license" for more information.
>>
>>>>> import sys
>>>>> def mydisplayhook(a):
>>
>>
>> ...     if a is not None: sys.stdout.write("%r\n" % a)
>> ...
>>
>>>>> def foo(s): return s
>>
>>
>> ...
>>
>>>>> sys.displayhook = mydisplayhook
>>>>> import __builtin__
>>>>> __builtin__._ = foo
>>>>> _("hi")
>>
>>
>> 'hi'
>>
>>>>> _("hello")
>>
>>
>> 'hello'
>>
> 
> Thanks Peter, it does work!


It worked, BUT only for a simple function, it fails if I add a another 
simple function:

My updated teststr.py script:

#--[--------------------------------------
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")
     else:
         return _(u"OFF")


def inList(longString, stringList) :
     """Return True if one of the string in `stringList` is inside 
`longString`.

     Also return the list index.
     **Example**

     >>> L = ["**", "/*"]
     >>> inList("aksdkajshd",L)
     (False, 0)
     >>> inList("aksdkajsh**d",L)
     (True, 0)

     """

     theIndex = 0
     for theStr in stringList:
         if longString.find(theStr) >= 0:
             return (True,theIndex)
         theIndex +=1
     return (False,0)


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

     import doctest, teststr
     return doctest.testmod(teststr)

if __name__ == "__main__":
     import sys

     def test_displayhook(a):
         if a is not None: sys.stdout.write("%r\n" % a)

     def test_translator(aString):
         return aString

     sys.displayhook = test_displayhook

     import __builtin__
     __builtin__._ = test_translator
     _test()

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

Running the test fails:

D:\dev\python>teststr
*****************************************************************
Failure in example: inList("aksdkajshd",L)
from line #6 of teststr.inList
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 "D:\dev\python\teststr.py", line 50, in test_displayhook
     if a is not None: sys.stdout.write("%r\n" % a)
TypeError: not all arguments converted during string formatting
*****************************************************************
Failure in example: inList("aksdkajsh**d",L)
from line #8 of teststr.inList
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 "D:\dev\python\teststr.py", line 50, in test_displayhook
     if a is not None: sys.stdout.write("%r\n" % a)
TypeError: not all arguments converted during string formatting
*****************************************************************
1 items had failures:
    2 of   3 in teststr.inList
***Test Failed*** 2 failures.

#------------------------------------------

So far, I don't have a solution for writing internationalized Python 
that support doctest.  Surely, I am not the first one trying to do that...


Pierre




More information about the Python-list mailing list