mirror of
https://github.com/python/cpython.git
synced 2025-07-28 21:55:21 +00:00
when print() gets unicode arguments, sep and end should be unicode by default #4618
This commit is contained in:
parent
1bf4765369
commit
753d16234f
3 changed files with 107 additions and 25 deletions
|
@ -9,12 +9,7 @@ import unittest
|
|||
from test import test_support
|
||||
|
||||
import sys
|
||||
if sys.version_info[0] == 3:
|
||||
# 3.x
|
||||
from io import StringIO
|
||||
else:
|
||||
# 2.x
|
||||
from StringIO import StringIO
|
||||
from StringIO import StringIO
|
||||
|
||||
NotDefined = object()
|
||||
|
||||
|
@ -112,6 +107,34 @@ class TestPrint(unittest.TestCase):
|
|||
self.assertRaises(TypeError, print, '', end=3)
|
||||
self.assertRaises(AttributeError, print, '', file='')
|
||||
|
||||
def test_mixed_args(self):
|
||||
# If an unicode arg is passed, sep and end should be unicode, too.
|
||||
class Recorder(object):
|
||||
|
||||
def __init__(self, must_be_unicode):
|
||||
self.buf = []
|
||||
self.force_unicode = must_be_unicode
|
||||
|
||||
def write(self, what):
|
||||
if self.force_unicode and not isinstance(what, unicode):
|
||||
raise AssertionError("{0!r} is not unicode".format(what))
|
||||
self.buf.append(what)
|
||||
|
||||
buf = Recorder(True)
|
||||
print(u'hi', file=buf)
|
||||
self.assertEqual(u''.join(buf.buf), 'hi\n')
|
||||
del buf.buf[:]
|
||||
print(u'hi', u'nothing', file=buf)
|
||||
self.assertEqual(u''.join(buf.buf), 'hi nothing\n')
|
||||
buf = Recorder(False)
|
||||
print('hi', 'bye', end=u'\n', file=buf)
|
||||
self.assertTrue(isinstance(buf.buf[1], unicode))
|
||||
self.assertTrue(isinstance(buf.buf[3], unicode))
|
||||
del buf.buf[:]
|
||||
print(sep=u'x', file=buf)
|
||||
self.assertTrue(isinstance(buf.buf[-1], unicode))
|
||||
|
||||
|
||||
def test_main():
|
||||
test_support.run_unittest(TestPrint)
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue