fix py/io classes and tests to pass 3.1
introduce py.builtin._totext helper to make a 2k=unicode / 3k=str object, allow a string as data --HG-- branch : trunk
This commit is contained in:
+122
-93
@@ -1,6 +1,34 @@
|
||||
import os, sys
|
||||
import py
|
||||
|
||||
from py.builtin import print_
|
||||
|
||||
if sys.version_info >= (3,0):
|
||||
def tobytes(obj):
|
||||
if isinstance(obj, str):
|
||||
obj = obj.encode('UTF-8')
|
||||
assert isinstance(obj, bytes)
|
||||
return obj
|
||||
def totext(obj):
|
||||
if isinstance(obj, bytes):
|
||||
obj = str(obj, 'UTF-8')
|
||||
assert isinstance(obj, str)
|
||||
return obj
|
||||
else:
|
||||
def tobytes(obj):
|
||||
if isinstance(obj, unicode):
|
||||
obj = obj.encode('UTF-8')
|
||||
assert isinstance(obj, str)
|
||||
return obj
|
||||
def totext(obj):
|
||||
if isinstance(obj, str):
|
||||
obj = unicode(obj, 'UTF-8')
|
||||
assert isinstance(obj, unicode)
|
||||
return obj
|
||||
|
||||
def oswritebytes(fd, obj):
|
||||
os.write(fd, tobytes(obj))
|
||||
|
||||
class TestTextIO:
|
||||
def test_text(self):
|
||||
f = py.io.TextIO()
|
||||
@@ -11,18 +39,23 @@ class TestTextIO:
|
||||
|
||||
def test_unicode_and_str_mixture(self):
|
||||
f = py.io.TextIO()
|
||||
f.write(u"\u00f6")
|
||||
f.write(str("hello"))
|
||||
s = f.getvalue()
|
||||
f.close()
|
||||
assert isinstance(s, unicode)
|
||||
if sys.version_info >= (3,0):
|
||||
f.write("\u00f6")
|
||||
py.test.skip("3k IO beahviour?")
|
||||
f.write(bytes("hello", 'UTF-8'))
|
||||
else:
|
||||
f.write(unicode("\u00f6", 'UTF-8'))
|
||||
f.write("hello") # bytes
|
||||
s = f.getvalue()
|
||||
f.close()
|
||||
assert isinstance(s, unicode)
|
||||
|
||||
def test_bytes_io():
|
||||
f = py.io.BytesIO()
|
||||
f.write("hello")
|
||||
py.test.raises(TypeError, "f.write(u'hello')")
|
||||
f.write(tobytes("hello"))
|
||||
py.test.raises(TypeError, "f.write(totext('hello'))")
|
||||
s = f.getvalue()
|
||||
assert s == "hello"
|
||||
assert s == tobytes("hello")
|
||||
|
||||
def test_dontreadfrominput():
|
||||
from py.__.io.capture import DontReadFromInput
|
||||
@@ -33,30 +66,36 @@ def test_dontreadfrominput():
|
||||
py.test.raises(IOError, iter, f)
|
||||
py.test.raises(ValueError, f.fileno)
|
||||
|
||||
def test_dupfile():
|
||||
somefile = py.std.os.tmpfile()
|
||||
def pytest_funcarg__tmpfile(request):
|
||||
testdir = request.getfuncargvalue("testdir")
|
||||
f = testdir.makepyfile("").open('wb+')
|
||||
request.addfinalizer(f.close)
|
||||
return f
|
||||
|
||||
def test_dupfile(tmpfile):
|
||||
somefile = tmpfile
|
||||
flist = []
|
||||
for i in range(5):
|
||||
nf = py.io.dupfile(somefile)
|
||||
assert nf != somefile
|
||||
assert nf.fileno() != somefile.fileno()
|
||||
assert nf not in flist
|
||||
print >>nf, i,
|
||||
print_(i, end="", file=nf)
|
||||
flist.append(nf)
|
||||
for i in range(5):
|
||||
f = flist[i]
|
||||
f.close()
|
||||
somefile.seek(0)
|
||||
s = somefile.read()
|
||||
assert s.startswith("01234")
|
||||
assert "01234" in repr(s)
|
||||
somefile.close()
|
||||
|
||||
class TestFDCapture:
|
||||
def test_basic(self):
|
||||
tmpfile = py.std.os.tmpfile()
|
||||
def test_stdout(self, tmpfile):
|
||||
fd = tmpfile.fileno()
|
||||
cap = py.io.FDCapture(fd)
|
||||
os.write(fd, "hello")
|
||||
data = tobytes("hello")
|
||||
os.write(fd, data)
|
||||
f = cap.done()
|
||||
s = f.read()
|
||||
assert s == "hello"
|
||||
@@ -64,48 +103,34 @@ class TestFDCapture:
|
||||
def test_stderr(self):
|
||||
cap = py.io.FDCapture(2)
|
||||
cap.setasfile('stderr')
|
||||
print >>sys.stderr, "hello"
|
||||
print_("hello", file=sys.stderr)
|
||||
f = cap.done()
|
||||
s = f.read()
|
||||
assert s == "hello\n"
|
||||
|
||||
def test_stdin(self):
|
||||
f = os.tmpfile()
|
||||
print >>f, "3"
|
||||
f.seek(0)
|
||||
cap = py.io.FDCapture(0, tmpfile=f)
|
||||
def test_stdin(self, tmpfile):
|
||||
tmpfile.write(tobytes("3"))
|
||||
tmpfile.seek(0)
|
||||
cap = py.io.FDCapture(0, tmpfile=tmpfile)
|
||||
# check with os.read() directly instead of raw_input(), because
|
||||
# sys.stdin itself may be redirected (as py.test now does by default)
|
||||
x = os.read(0, 100).strip()
|
||||
f = cap.done()
|
||||
assert x == "3"
|
||||
assert x == tobytes("3")
|
||||
|
||||
def test_writeorg(self):
|
||||
tmppath = py.test.ensuretemp('test_writeorg').ensure('stderr',
|
||||
file=True)
|
||||
tmpfp = tmppath.open('w+b')
|
||||
def test_writeorg(self, tmpfile):
|
||||
data1, data2 = tobytes("foo"), tobytes("bar")
|
||||
try:
|
||||
cap = py.io.FDCapture(tmpfp.fileno())
|
||||
print >>tmpfp, 'foo'
|
||||
cap.writeorg('bar\n')
|
||||
cap = py.io.FDCapture(tmpfile.fileno())
|
||||
tmpfile.write(data1)
|
||||
cap.writeorg(data2)
|
||||
finally:
|
||||
tmpfp.close()
|
||||
tmpfile.close()
|
||||
f = cap.done()
|
||||
scap = f.read()
|
||||
assert scap == 'foo\n'
|
||||
stmp = tmppath.read()
|
||||
assert stmp == "bar\n"
|
||||
|
||||
def test_writeorg_wrongtype(self):
|
||||
tmppath = py.test.ensuretemp('test_writeorg').ensure('stdout',
|
||||
file=True)
|
||||
tmpfp = tmppath.open('r')
|
||||
try:
|
||||
cap = py.io.FDCapture(tmpfp.fileno())
|
||||
py.test.raises(IOError, "cap.writeorg('bar\\n')")
|
||||
finally:
|
||||
tmpfp.close()
|
||||
f = cap.done()
|
||||
assert scap == totext(data1)
|
||||
stmp = open(tmpfile.name, 'rb').read()
|
||||
assert stmp == data2
|
||||
|
||||
|
||||
class TestStdCapture:
|
||||
@@ -114,16 +139,18 @@ class TestStdCapture:
|
||||
|
||||
def test_capturing_done_simple(self):
|
||||
cap = self.getcapture()
|
||||
print "hello world"
|
||||
print >>sys.stderr, "hello error"
|
||||
sys.stdout.write("hello")
|
||||
sys.stderr.write("world")
|
||||
outfile, errfile = cap.done()
|
||||
assert outfile.read() == "hello world\n"
|
||||
assert errfile.read() == "hello error\n"
|
||||
s = outfile.read()
|
||||
assert s == "hello"
|
||||
s = errfile.read()
|
||||
assert s == "world"
|
||||
|
||||
def test_capturing_reset_simple(self):
|
||||
cap = self.getcapture()
|
||||
print "hello world"
|
||||
print >>sys.stderr, "hello error"
|
||||
print("hello world")
|
||||
sys.stderr.write("hello error\n")
|
||||
out, err = cap.reset()
|
||||
assert out == "hello world\n"
|
||||
assert err == "hello error\n"
|
||||
@@ -131,28 +158,28 @@ class TestStdCapture:
|
||||
def test_capturing_readouterr(self):
|
||||
cap = self.getcapture()
|
||||
try:
|
||||
print "hello world"
|
||||
print >>sys.stderr, "hello error"
|
||||
print ("hello world")
|
||||
sys.stderr.write("hello error\n")
|
||||
out, err = cap.readouterr()
|
||||
assert out == "hello world\n"
|
||||
assert err == "hello error\n"
|
||||
print >>sys.stderr, "error2"
|
||||
sys.stderr.write("error2")
|
||||
finally:
|
||||
out, err = cap.reset()
|
||||
assert err == "error2\n"
|
||||
assert err == "error2"
|
||||
|
||||
def test_capturing_mixed(self):
|
||||
cap = self.getcapture(mixed=True)
|
||||
print "hello",
|
||||
print >>sys.stderr, "world",
|
||||
print >>sys.stdout, ".",
|
||||
sys.stdout.write("hello ")
|
||||
sys.stderr.write("world")
|
||||
sys.stdout.write(".")
|
||||
out, err = cap.reset()
|
||||
assert out.strip() == "hello world ."
|
||||
assert out.strip() == "hello world."
|
||||
assert not err
|
||||
|
||||
def test_capturing_twice_error(self):
|
||||
cap = self.getcapture()
|
||||
print "hello"
|
||||
print ("hello")
|
||||
cap.reset()
|
||||
py.test.raises(Exception, "cap.reset()")
|
||||
|
||||
@@ -160,12 +187,12 @@ class TestStdCapture:
|
||||
oldout = sys.stdout
|
||||
olderr = sys.stderr
|
||||
cap = self.getcapture()
|
||||
print "hello",
|
||||
print >>sys.stderr, "world",
|
||||
sys.stdout.write("hello")
|
||||
sys.stderr.write("world")
|
||||
sys.stdout = py.io.TextIO()
|
||||
sys.stderr = py.io.TextIO()
|
||||
print "not seen"
|
||||
print >>sys.stderr, "not seen"
|
||||
print ("not seen" )
|
||||
sys.stderr.write("not seen\n")
|
||||
out, err = cap.reset()
|
||||
assert out == "hello"
|
||||
assert err == "world"
|
||||
@@ -174,9 +201,9 @@ class TestStdCapture:
|
||||
|
||||
def test_capturing_error_recursive(self):
|
||||
cap1 = self.getcapture()
|
||||
print "cap1"
|
||||
print ("cap1")
|
||||
cap2 = self.getcapture()
|
||||
print "cap2"
|
||||
print ("cap2")
|
||||
out2, err2 = cap2.reset()
|
||||
py.test.raises(Exception, "cap2.reset()")
|
||||
out1, err1 = cap1.reset()
|
||||
@@ -185,18 +212,18 @@ class TestStdCapture:
|
||||
|
||||
def test_just_out_capture(self):
|
||||
cap = self.getcapture(out=True, err=False)
|
||||
print >>sys.stdout, "hello"
|
||||
print >>sys.stderr, "world"
|
||||
sys.stdout.write("hello")
|
||||
sys.stderr.write("world")
|
||||
out, err = cap.reset()
|
||||
assert out == "hello\n"
|
||||
assert out == "hello"
|
||||
assert not err
|
||||
|
||||
def test_just_err_capture(self):
|
||||
cap = self.getcapture(out=False, err=True)
|
||||
print >>sys.stdout, "hello"
|
||||
print >>sys.stderr, "world"
|
||||
sys.stdout.write("hello")
|
||||
sys.stderr.write("world")
|
||||
out, err = cap.reset()
|
||||
assert err == "world\n"
|
||||
assert err == "world"
|
||||
assert not out
|
||||
|
||||
def test_stdin_restored(self):
|
||||
@@ -208,9 +235,9 @@ class TestStdCapture:
|
||||
assert sys.stdin is old
|
||||
|
||||
def test_stdin_nulled_by_default(self):
|
||||
print "XXX this test may well hang instead of crashing"
|
||||
print "XXX which indicates an error in the underlying capturing"
|
||||
print "XXX mechanisms"
|
||||
print ("XXX this test may well hang instead of crashing")
|
||||
print ("XXX which indicates an error in the underlying capturing")
|
||||
print ("XXX mechanisms" )
|
||||
cap = self.getcapture()
|
||||
py.test.raises(IOError, "sys.stdin.read()")
|
||||
out, err = cap.reset()
|
||||
@@ -218,15 +245,15 @@ class TestStdCapture:
|
||||
def test_suspend_resume(self):
|
||||
cap = self.getcapture(out=True, err=False, in_=False)
|
||||
try:
|
||||
print "hello"
|
||||
print ("hello")
|
||||
sys.stderr.write("error\n")
|
||||
out, err = cap.suspend()
|
||||
assert out == "hello\n"
|
||||
assert not err
|
||||
print "in between"
|
||||
print ("in between")
|
||||
sys.stderr.write("in between\n")
|
||||
cap.resume()
|
||||
print "after"
|
||||
print ("after")
|
||||
sys.stderr.write("error_after\n")
|
||||
finally:
|
||||
out, err = cap.reset()
|
||||
@@ -239,20 +266,22 @@ class TestStdCaptureFD(TestStdCapture):
|
||||
|
||||
def test_intermingling(self):
|
||||
cap = self.getcapture()
|
||||
os.write(1, "1")
|
||||
print >>sys.stdout, 2,
|
||||
os.write(1, "3")
|
||||
os.write(2, "a")
|
||||
print >>sys.stderr, "b",
|
||||
os.write(2, "c")
|
||||
oswritebytes(1, "1")
|
||||
sys.stdout.write(str(2))
|
||||
sys.stdout.flush()
|
||||
oswritebytes(1, "3")
|
||||
oswritebytes(2, "a")
|
||||
sys.stderr.write("b")
|
||||
sys.stderr.flush()
|
||||
oswritebytes(2, "c")
|
||||
out, err = cap.reset()
|
||||
assert out == "123"
|
||||
assert err == "abc"
|
||||
|
||||
def test_callcapture(self):
|
||||
def func(x, y):
|
||||
print x
|
||||
print >>py.std.sys.stderr, y
|
||||
print (x)
|
||||
py.std.sys.stderr.write(str(y))
|
||||
return 42
|
||||
|
||||
res, out, err = py.io.StdCaptureFD.call(func, 3, y=4)
|
||||
@@ -264,10 +293,10 @@ def test_capture_no_sys():
|
||||
capsys = py.io.StdCapture()
|
||||
try:
|
||||
cap = py.io.StdCaptureFD(patchsys=False)
|
||||
print >>sys.stdout, "hello"
|
||||
print >>sys.stderr, "world"
|
||||
os.write(1, "1")
|
||||
os.write(2, "2")
|
||||
sys.stdout.write("hello")
|
||||
sys.stderr.write("world")
|
||||
oswritebytes(1, "1")
|
||||
oswritebytes(2, "2")
|
||||
out, err = cap.reset()
|
||||
assert out == "1"
|
||||
assert err == "2"
|
||||
@@ -276,10 +305,10 @@ def test_capture_no_sys():
|
||||
|
||||
def test_callcapture_nofd():
|
||||
def func(x, y):
|
||||
os.write(1, "hello")
|
||||
os.write(2, "hello")
|
||||
print x
|
||||
print >>sys.stderr, y
|
||||
oswritebytes(1, "hello")
|
||||
oswritebytes(2, "hello")
|
||||
print (x)
|
||||
sys.stderr.write(str(y))
|
||||
return 42
|
||||
|
||||
capfd = py.io.StdCaptureFD(patchsys=False)
|
||||
|
||||
@@ -44,11 +44,10 @@ def test_terminalwriter_dumb_term_no_markup(monkeypatch):
|
||||
assert not tw.hasmarkup
|
||||
|
||||
def test_unicode_encoding():
|
||||
l = []
|
||||
msg = unicode('b\u00f6y', 'utf8')
|
||||
msg = py.builtin._totext('b\u00f6y', 'utf8')
|
||||
for encoding in 'utf8', 'latin1':
|
||||
tw = py.io.TerminalWriter(l.append)
|
||||
tw._encoding = encoding
|
||||
l = []
|
||||
tw = py.io.TerminalWriter(l.append, encoding=encoding)
|
||||
tw.line(msg)
|
||||
assert l[0] == msg.encode(encoding)
|
||||
|
||||
@@ -64,7 +63,7 @@ class BaseTests:
|
||||
tw = self.getwriter()
|
||||
for encoding in 'utf8', 'latin1':
|
||||
tw._encoding = encoding
|
||||
msg = unicode('b\u00f6y', 'utf8')
|
||||
msg = py.builtin._totext('b\u00f6y', 'utf8')
|
||||
tw.line(msg)
|
||||
l = self.getlines()
|
||||
assert l[0] == msg + "\n"
|
||||
|
||||
Reference in New Issue
Block a user