cpython: eafe4007c999 (original) (raw)
--- a/Lib/test/test_tools.py +++ b/Lib/test/test_tools.py @@ -16,7 +16,7 @@ import sysconfig import tempfile import textwrap from test import support -from test.script_helper import assert_python_ok, temp_dir +from test.script_helper import assert_python_ok, assert_python_failure if not sysconfig.is_python_build(): # XXX some installers do contain the tools, should we detect that @@ -61,7 +61,7 @@ class PindentTests(unittest.TestCase): def test_selftest(self): self.maxDiff = None
with temp_dir() as directory:[](#l1.16)
with support.temp_dir() as directory:[](#l1.17) data_path = os.path.join(directory, '_test.py')[](#l1.18) with open(self.script) as f:[](#l1.19) closed = f.read()[](#l1.20)
@@ -367,7 +367,7 @@ class TestSundryScripts(unittest.TestCas # added for a script it should be added to the whitelist below. # scripts that have independent tests.
- whitelist = ['reindent.py', 'pdeps.py', 'gprof2html', 'md5sum.py']
blacklist = ['make_ctype.py'] scripts that can't be imported without running
scripts that use windows-only modules
@@ -450,16 +450,74 @@ class Gprof2htmlTests(unittest.TestCase) self.assertTrue(wmock.open.called) +class MD5SumTests(unittest.TestCase): +
- @classmethod
- def setUpClass(cls):
cls.script = os.path.join(scriptsdir, 'md5sum.py')[](#l1.38)
os.mkdir(support.TESTFN)[](#l1.39)
cls.fodder = os.path.join(support.TESTFN, 'md5sum.fodder')[](#l1.40)
with open(cls.fodder, 'wb') as f:[](#l1.41)
f.write(b'md5sum\r\ntest file\r\n')[](#l1.42)
cls.fodder_md5 = b'd38dae2eb1ab346a292ef6850f9e1a0d'[](#l1.43)
cls.fodder_textmode_md5 = b'a8b07894e2ca3f2a4c3094065fa6e0a5'[](#l1.44)
- def test_noargs(self):
rc, out, err = assert_python_ok(self.script)[](#l1.51)
self.assertEqual(rc, 0)[](#l1.52)
self.assertTrue([](#l1.53)
out.startswith(b'd41d8cd98f00b204e9800998ecf8427e <stdin>'))[](#l1.54)
self.assertFalse(err)[](#l1.55)
- def test_checksum_fodder(self):
rc, out, err = assert_python_ok(self.script, self.fodder)[](#l1.58)
self.assertEqual(rc, 0)[](#l1.59)
self.assertTrue(out.startswith(self.fodder_md5))[](#l1.60)
for part in self.fodder.split(os.path.sep):[](#l1.61)
self.assertIn(part.encode(), out)[](#l1.62)
self.assertFalse(err)[](#l1.63)
- def test_dash_l(self):
rc, out, err = assert_python_ok(self.script, '-l', self.fodder)[](#l1.66)
self.assertEqual(rc, 0)[](#l1.67)
self.assertIn(self.fodder_md5, out)[](#l1.68)
parts = self.fodder.split(os.path.sep)[](#l1.69)
self.assertIn(parts[-1].encode(), out)[](#l1.70)
self.assertNotIn(parts[-2].encode(), out)[](#l1.71)
- def test_dash_t(self):
rc, out, err = assert_python_ok(self.script, '-t', self.fodder)[](#l1.74)
self.assertEqual(rc, 0)[](#l1.75)
self.assertTrue(out.startswith(self.fodder_textmode_md5))[](#l1.76)
self.assertNotIn(self.fodder_md5, out)[](#l1.77)
- def test_dash_s(self):
rc, out, err = assert_python_ok(self.script, '-s', '512', self.fodder)[](#l1.80)
self.assertEqual(rc, 0)[](#l1.81)
self.assertIn(self.fodder_md5, out)[](#l1.82)
- def test_multiple_files(self):
rc, out, err = assert_python_ok(self.script, self.fodder, self.fodder)[](#l1.85)
self.assertEqual(rc, 0)[](#l1.86)
lines = out.splitlines()[](#l1.87)
self.assertEqual(len(lines), 2)[](#l1.88)
self.assertEqual(*lines)[](#l1.89)
- def test_usage(self):
rc, out, err = assert_python_failure(self.script, '-h')[](#l1.92)
self.assertEqual(rc, 2)[](#l1.93)
self.assertEqual(out, b'')[](#l1.94)
self.assertGreater(err, b'')[](#l1.95)
Run the tests in Tools/parser/test_unparse.py
with support.DirsOnSysPath(os.path.join(basepath, 'parser')): from test_unparse import UnparseTestCase from test_unparse import DirectoryTestCase - -def test_main():
- - if name == 'main': unittest.main()
--- a/Misc/NEWS +++ b/Misc/NEWS @@ -1,4 +1,4 @@ -+++++++++++ ++++++++++++ Python News +++++++++++ @@ -227,6 +227,9 @@ Windows Tools/Demos ----------- +- Issue #21906: Make Tools/scripts/md5sum.py work in Python 3.
--- a/Tools/scripts/md5sum.py +++ b/Tools/scripts/md5sum.py @@ -9,7 +9,7 @@ fnfilter = None rmode = 'rb' usage = """ -usage: sum5 [-b] [-t] [-l] [-s bufsize] [file ...] +usage: md5sum.py [-b] [-t] [-l] [-s bufsize] [file ...] -b : read files in binary mode (default) -t : read files in text mode (you almost certainly don't want this!) -l : print last pathname component only @@ -17,6 +17,7 @@ usage: sum5 [-b] [-t] [-l] [-s bufsize] file ... : files to sum; '-' or no files means stdin """ % bufsize +import io import sys import os import getopt @@ -24,7 +25,7 @@ from hashlib import md5 def sum(*files): sts = 0
- if files and isinstance(files[-1], io.IOBase): out, files = files[-1], files[:-1] else: out = sys.stdout
@@ -53,12 +54,14 @@ def printsum(filename, out=sys.stdout): return sts def printsumfp(fp, filename, out=sys.stdout):