mirror of
https://github.com/python/cpython.git
synced 2025-08-04 00:48:58 +00:00
SF patch# 1770008 by Christian Heimes (plus some extras).
Completely get rid of StringIO.py and cStringIO.c. I had to fix a few tests and modules beyond what Christian did, and invent a few conventions. E.g. in elementtree, I chose to write/return Unicode strings whe no encoding is given, but bytes when an explicit encoding is given. Also mimetools was made to always assume binary files.
This commit is contained in:
parent
918f49e645
commit
34d1928766
78 changed files with 312 additions and 657 deletions
|
@ -6,47 +6,50 @@ Nick Mathewson
|
|||
import unittest
|
||||
from test import test_support
|
||||
|
||||
import sys, os, uu, cStringIO
|
||||
import sys, os
|
||||
import uu
|
||||
from StringIO import StringIO
|
||||
from io import BytesIO
|
||||
import io
|
||||
|
||||
plaintext = "The smooth-scaled python crept over the sleeping dog\n"
|
||||
plaintext = b"The smooth-scaled python crept over the sleeping dog\n"
|
||||
|
||||
encodedtext = """\
|
||||
encodedtext = b"""\
|
||||
M5&AE('-M;V]T:\"US8V%L960@<'ET:&]N(&-R97!T(&]V97(@=&AE('-L965P
|
||||
(:6YG(&1O9PH """
|
||||
|
||||
encodedtextwrapped = "begin %03o %s\n" + encodedtext.replace("%", "%%") + "\n \nend\n"
|
||||
def encodedtextwrapped(mode, filename):
|
||||
return (bytes("begin %03o %s\n" % (mode, filename), "ascii") +
|
||||
encodedtext + b"\n \nend\n")
|
||||
|
||||
class UUTest(unittest.TestCase):
|
||||
|
||||
def test_encode(self):
|
||||
inp = cStringIO.StringIO(plaintext)
|
||||
out = cStringIO.StringIO()
|
||||
inp = io.BytesIO(plaintext)
|
||||
out = io.BytesIO()
|
||||
uu.encode(inp, out, "t1")
|
||||
self.assertEqual(out.getvalue(), encodedtextwrapped % (0o666, "t1"))
|
||||
inp = cStringIO.StringIO(plaintext)
|
||||
out = cStringIO.StringIO()
|
||||
self.assertEqual(out.getvalue(), encodedtextwrapped(0o666, "t1"))
|
||||
inp = io.BytesIO(plaintext)
|
||||
out = io.BytesIO()
|
||||
uu.encode(inp, out, "t1", 0o644)
|
||||
self.assertEqual(out.getvalue(), encodedtextwrapped % (0o644, "t1"))
|
||||
self.assertEqual(out.getvalue(), encodedtextwrapped(0o644, "t1"))
|
||||
|
||||
def test_decode(self):
|
||||
inp = cStringIO.StringIO(encodedtextwrapped % (0o666, "t1"))
|
||||
out = cStringIO.StringIO()
|
||||
inp = io.BytesIO(encodedtextwrapped(0o666, "t1"))
|
||||
out = io.BytesIO()
|
||||
uu.decode(inp, out)
|
||||
self.assertEqual(out.getvalue(), plaintext)
|
||||
inp = cStringIO.StringIO(
|
||||
"UUencoded files may contain many lines,\n" +
|
||||
"even some that have 'begin' in them.\n" +
|
||||
encodedtextwrapped % (0o666, "t1")
|
||||
inp = io.BytesIO(
|
||||
b"UUencoded files may contain many lines,\n" +
|
||||
b"even some that have 'begin' in them.\n" +
|
||||
encodedtextwrapped(0o666, "t1")
|
||||
)
|
||||
out = cStringIO.StringIO()
|
||||
out = io.BytesIO()
|
||||
uu.decode(inp, out)
|
||||
self.assertEqual(out.getvalue(), plaintext)
|
||||
|
||||
def test_truncatedinput(self):
|
||||
inp = cStringIO.StringIO("begin 644 t1\n" + encodedtext)
|
||||
out = cStringIO.StringIO()
|
||||
inp = io.BytesIO(b"begin 644 t1\n" + encodedtext)
|
||||
out = io.BytesIO()
|
||||
try:
|
||||
uu.decode(inp, out)
|
||||
self.fail("No exception thrown")
|
||||
|
@ -54,8 +57,8 @@ class UUTest(unittest.TestCase):
|
|||
self.assertEqual(str(e), "Truncated input file")
|
||||
|
||||
def test_missingbegin(self):
|
||||
inp = cStringIO.StringIO("")
|
||||
out = cStringIO.StringIO()
|
||||
inp = io.BytesIO(b"")
|
||||
out = io.BytesIO()
|
||||
try:
|
||||
uu.decode(inp, out)
|
||||
self.fail("No exception thrown")
|
||||
|
@ -73,24 +76,27 @@ class UUStdIOTest(unittest.TestCase):
|
|||
sys.stdout = self.stdout
|
||||
|
||||
def test_encode(self):
|
||||
sys.stdin = cStringIO.StringIO(plaintext)
|
||||
sys.stdout = cStringIO.StringIO()
|
||||
sys.stdin = io.StringIO(plaintext.decode("ascii"))
|
||||
sys.stdout = io.StringIO()
|
||||
uu.encode("-", "-", "t1", 0o666)
|
||||
self.assertEqual(
|
||||
sys.stdout.getvalue(),
|
||||
encodedtextwrapped % (0o666, "t1")
|
||||
)
|
||||
self.assertEqual(sys.stdout.getvalue(),
|
||||
encodedtextwrapped(0o666, "t1").decode("ascii"))
|
||||
|
||||
def test_decode(self):
|
||||
sys.stdin = cStringIO.StringIO(encodedtextwrapped % (0o666, "t1"))
|
||||
sys.stdout = cStringIO.StringIO()
|
||||
sys.stdin = io.StringIO(encodedtextwrapped(0o666, "t1").decode("ascii"))
|
||||
sys.stdout = io.StringIO()
|
||||
uu.decode("-", "-")
|
||||
self.assertEqual(sys.stdout.getvalue(), plaintext)
|
||||
stdout = sys.stdout
|
||||
sys.stdout = self.stdout
|
||||
sys.stdin = self.stdin
|
||||
self.assertEqual(stdout.getvalue(), plaintext.decode("ascii"))
|
||||
|
||||
class UUFileTest(unittest.TestCase):
|
||||
|
||||
def _kill(self, f):
|
||||
# close and remove file
|
||||
if f is None:
|
||||
return
|
||||
try:
|
||||
f.close()
|
||||
except (SystemExit, KeyboardInterrupt):
|
||||
|
@ -113,44 +119,46 @@ class UUFileTest(unittest.TestCase):
|
|||
del self.tmpout
|
||||
|
||||
def test_encode(self):
|
||||
fin = fout = None
|
||||
try:
|
||||
fin = open(self.tmpin, 'wb')
|
||||
fin.write(plaintext)
|
||||
fin.close()
|
||||
|
||||
fin = open(self.tmpin, 'rb')
|
||||
fout = open(self.tmpout, 'w')
|
||||
fout = open(self.tmpout, 'wb')
|
||||
uu.encode(fin, fout, self.tmpin, mode=0o644)
|
||||
fin.close()
|
||||
fout.close()
|
||||
|
||||
fout = open(self.tmpout, 'r')
|
||||
fout = open(self.tmpout, 'rb')
|
||||
s = fout.read()
|
||||
fout.close()
|
||||
self.assertEqual(s, encodedtextwrapped % (0o644, self.tmpin))
|
||||
self.assertEqual(s, encodedtextwrapped(0o644, self.tmpin))
|
||||
|
||||
# in_file and out_file as filenames
|
||||
uu.encode(self.tmpin, self.tmpout, self.tmpin, mode=0o644)
|
||||
fout = open(self.tmpout, 'r')
|
||||
fout = open(self.tmpout, 'rb')
|
||||
s = fout.read()
|
||||
fout.close()
|
||||
self.assertEqual(s, encodedtextwrapped % (0o644, self.tmpin))
|
||||
self.assertEqual(s, encodedtextwrapped(0o644, self.tmpin))
|
||||
|
||||
finally:
|
||||
self._kill(fin)
|
||||
self._kill(fout)
|
||||
|
||||
def test_decode(self):
|
||||
f = None
|
||||
try:
|
||||
f = open(self.tmpin, 'w')
|
||||
f.write(encodedtextwrapped % (0o644, self.tmpout))
|
||||
f = open(self.tmpin, 'wb')
|
||||
f.write(encodedtextwrapped(0o644, self.tmpout))
|
||||
f.close()
|
||||
|
||||
f = open(self.tmpin, 'r')
|
||||
f = open(self.tmpin, 'rb')
|
||||
uu.decode(f)
|
||||
f.close()
|
||||
|
||||
f = open(self.tmpout, 'r')
|
||||
f = open(self.tmpout, 'rb')
|
||||
s = f.read()
|
||||
f.close()
|
||||
self.assertEqual(s, plaintext)
|
||||
|
@ -160,21 +168,25 @@ class UUFileTest(unittest.TestCase):
|
|||
|
||||
def test_decodetwice(self):
|
||||
# Verify that decode() will refuse to overwrite an existing file
|
||||
f = None
|
||||
try:
|
||||
f = cStringIO.StringIO(encodedtextwrapped % (0o644, self.tmpout))
|
||||
f = io.BytesIO(encodedtextwrapped(0o644, self.tmpout))
|
||||
|
||||
f = open(self.tmpin, 'r')
|
||||
f = open(self.tmpin, 'rb')
|
||||
uu.decode(f)
|
||||
f.close()
|
||||
|
||||
f = open(self.tmpin, 'r')
|
||||
f = open(self.tmpin, 'rb')
|
||||
self.assertRaises(uu.Error, uu.decode, f)
|
||||
f.close()
|
||||
finally:
|
||||
self._kill(f)
|
||||
|
||||
def test_main():
|
||||
test_support.run_unittest(UUTest, UUStdIOTest, UUFileTest)
|
||||
test_support.run_unittest(UUTest,
|
||||
UUStdIOTest,
|
||||
UUFileTest,
|
||||
)
|
||||
|
||||
if __name__=="__main__":
|
||||
test_main()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue