Issue #18878: sunau.open now supports the context manager protocol.  Based on
patches by Claudiu Popa and R. David Murray.
diff --git a/Lib/test/test_sunau.py b/Lib/test/test_sunau.py
index c381d07..1b0baee 100644
--- a/Lib/test/test_sunau.py
+++ b/Lib/test/test_sunau.py
@@ -1,4 +1,4 @@
-from test.support import run_unittest, TESTFN
+from test.support import TESTFN, unlink
 import unittest
 import pickle
 import os
@@ -18,10 +18,7 @@
     def tearDown(self):
         if self.f is not None:
             self.f.close()
-        try:
-            os.remove(TESTFN)
-        except OSError:
-            pass
+        unlink(TESTFN)
 
     def test_lin(self):
         self.f = sunau.open(TESTFN, 'w')
@@ -84,9 +81,49 @@
         dump = pickle.dumps(params)
         self.assertEqual(pickle.loads(dump), params)
 
+    def test_write_context_manager_calls_close(self):
+        # Close checks for a minimum header and will raise an error
+        # if it is not set, so this proves that close is called.
+        with self.assertRaises(sunau.Error):
+            with sunau.open(TESTFN, 'wb') as f:
+                pass
+        with self.assertRaises(sunau.Error):
+            with open(TESTFN, 'wb') as testfile:
+                with sunau.open(testfile):
+                    pass
 
-def test_main():
-    run_unittest(SunAUTest)
+    def test_context_manager_with_open_file(self):
+        with open(TESTFN, 'wb') as testfile:
+            with sunau.open(testfile) as f:
+                f.setnchannels(nchannels)
+                f.setsampwidth(sampwidth)
+                f.setframerate(framerate)
+            self.assertFalse(testfile.closed)
+        with open(TESTFN, 'rb') as testfile:
+            with sunau.open(testfile) as f:
+                self.assertFalse(f.getfp().closed)
+                params = f.getparams()
+                self.assertEqual(params[0], nchannels)
+                self.assertEqual(params[1], sampwidth)
+                self.assertEqual(params[2], framerate)
+            self.assertIsNone(f.getfp())
+            self.assertFalse(testfile.closed)
+
+    def test_context_manager_with_filename(self):
+        # If the file doesn't get closed, this test won't fail, but it will
+        # produce a resource leak warning.
+        with sunau.open(TESTFN, 'wb') as f:
+            f.setnchannels(nchannels)
+            f.setsampwidth(sampwidth)
+            f.setframerate(framerate)
+        with sunau.open(TESTFN) as f:
+            self.assertFalse(f.getfp().closed)
+            params = f.getparams()
+            self.assertEqual(params[0], nchannels)
+            self.assertEqual(params[1], sampwidth)
+            self.assertEqual(params[2], framerate)
+        self.assertIsNone(f.getfp())
+
 
 if __name__ == "__main__":
     unittest.main()