This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: doctest work with Windows PyReadline
Type: enhancement Stage: patch review
Components: Library (Lib), Windows Versions: Python 3.2
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: manuelg_, terry.reedy
Priority: normal Keywords: patch

Created on 2007-04-26 19:02 by manuelg_, last changed 2022-04-11 14:56 by admin.

Files
File name Uploaded Description Edit
doctest.py manuelg_, 2007-04-26 19:02 doctest.py (added 2 lines: "encoding")
doctest-patch.txt akuchling, 2010-02-22 20:50 Second version
Messages (6)
msg31900 - (view) Author: manuelg (manuelg_) Date: 2007-04-26 19:02
doctest crashes when working with Windows PyReadline (PyReadline is a component of Windows IPython)

PyReadline expects "_SpoofOut" to have an "encoding" attribute

E
======================================================================
ERROR: testDocTest (__main__.TestDocTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "test_freecell_solver.py", line 26, in testDocTest
    r = doctest.testmod(freecell_solver)
  File "c:\Python25\Lib\doctest.py", line 1799, in testmod
    runner.run(test)
  File "c:\Python25\Lib\doctest.py", line 1335, in run
    self.debugger = _OutputRedirectingPdb(save_stdout)
  File "c:\Python25\Lib\doctest.py", line 320, in __init__
    pdb.Pdb.__init__(self, stdout=out)
  File "c:\Python25\Lib\pdb.py", line 66, in __init__
    import readline
  File "C:\Python25\Lib\site-packages\readline.py", line 5, in <module>
    from pyreadline import *
  File "C:\Python25\Lib\site-packages\pyreadline\__init__.py", line 10, in <module>
    from rlmain import *
  File "C:\Python25\Lib\site-packages\pyreadline\rlmain.py", line 13, in <module>
    import clipboard,logger,console
  File "C:\Python25\Lib\site-packages\pyreadline\console\__init__.py", line 14,in <module>
    from console import *
  File "C:\Python25\Lib\site-packages\pyreadline\console\console.py", line 118,in <module>
    consolecodepage=sys.stdout.encoding
AttributeError: _SpoofOut instance has no attribute 'encoding'

This is an easy fix with 2 lines of code to doctest.py

right after doctest.py imports "sys", store the "sys.stdout.encoding"

_sys_stdout_encoding = sys.stdout.encoding

Then add this as an attribute "encoding" in the "_SpoofOut" class

# Override some StringIO methods.
class _SpoofOut(StringIO):

    ....
            
    encoding = _sys_stdout_encoding

msg99822 - (view) Author: A.M. Kuchling (akuchling) * (Python committer) Date: 2010-02-22 20:22
Does the attached, slightly simpler patch, also fix it?
The patch just sets the .encoding attribute after 
creating it with _SpoofOut().  

The revised patch doesn't seem to break anything on MacOS; I don't have access to Windows to test whether it still fixes the bug.
msg99829 - (view) Author: A.M. Kuchling (akuchling) * (Python committer) Date: 2010-02-22 20:50
Updated version: use sys.__stdout__ instead of sys.stdout, which might have already been replaced by another object lacking .encoding.
msg99887 - (view) Author: manuelg (manuelg_) Date: 2010-02-23 00:34
The supported version and the trunk of pyreadline launchpad.net/pyreadline no long relies on sys.stdout for the encoding for the Windows console.

Getting .encoding from sys.__stdout__ seems reasonable.  If taken from sys.stdout at very last moment, only referencing sys.__stdout__ if sys.stdout is missing .encoding might be optimal, but since the bug was in pyreadline, and it is no longer trying to get .encoding from sys.stdout (which it shouldn't do anyway, because no guarantee is made that sys.stdout even has an attr .encoding), the whole point may be moot.

I rely on your judgement.  Since I only use the supported version of pyreadline, the error no longer happens in the unpatched code.
msg112754 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2010-08-04 04:32
While it could be argued that this is a bugfix, I think changing test modules in bugfix releases should be avoided.

I would think that the simplest patch would be 
 
+        encoding = sys.__stdout__.encoding

in _Spoofout
msg222707 - (view) Author: Mark Lawrence (BreamoreBoy) * Date: 2014-07-10 22:16
http://mail.scipy.org/pipermail/ipython-user/2007-April/004299.html talks about a patch to work around this problem.  I think this is what the originator is talking about in msg99887 as in "Since I only use the supported version of pyreadline, the error no longer happens in the unpatched code.".  So would we be justified in closing this as "won't fix" or should we patch doctest in any case?
History
Date User Action Args
2022-04-11 14:56:24adminsetgithub: 44902
2019-04-26 17:24:05BreamoreBoysetnosy: - BreamoreBoy
2014-12-31 16:25:57akuchlingsetnosy: - akuchling
2014-07-10 22:16:24BreamoreBoysetnosy: + BreamoreBoy
messages: + msg222707
2010-08-10 11:43:31floxsetcomponents: + Windows
2010-08-04 04:32:09terry.reedysetversions: + Python 3.2, - Python 2.6, Python 2.5
nosy: + terry.reedy

messages: + msg112754

type: enhancement
stage: patch review
2010-02-23 00:34:15manuelg_setmessages: + msg99887
2010-02-22 20:51:00akuchlingsetfiles: - doctest-patch.txt
2010-02-22 20:50:51akuchlingsetfiles: + doctest-patch.txt

messages: + msg99829
2010-02-22 20:22:45akuchlingsetfiles: + doctest-patch.txt
nosy: + akuchling
messages: + msg99822

2008-01-06 12:45:18christian.heimessetkeywords: + patch
versions: + Python 2.6, Python 2.5
2007-04-26 19:02:04manuelg_create