          old_stdout, old_stderr = sys.stdout, sys.stderr
          sys.stdout, sys.stderr = StringIO(), StringIO()
          try:
              management.execute_from_command_line(['django-admin', 'optparse_cmd'])
          finally:
              output = sys.stdout.getvalue()
              sys.stdout, sys.stderr = old_stdout, old_stderr
          self.assertEqual(output, "All right, let's dance Rock'n'Roll.\n")
  
      def test_calling_a_command_with_only_empty_parameter_should_ends_gracefully(self):
          out = StringIO()
          management.call_command('hal', "--empty", stdout=out)
          self.assertIn("Dave, I can't do that.\n", out.getvalue())
 
</s> remove         old_stdout, old_stderr = sys.stdout, sys.stderr
        sys.stdout, sys.stderr = StringIO(), StringIO()
        try:
 </s> add         with captured_stdout() as stdout, captured_stderr(): </s> remove         old_stdout, old_stderr = sys.stdout, sys.stderr
        sys.stdout, sys.stderr = StringIO(), StringIO()
        try:
 </s> add         with captured_stdout(), captured_stderr(): </s> remove         old_stdout, old_stderr = sys.stdout, sys.stderr
        sys.stdout, sys.stderr = StringIO(), StringIO()
        try:
 </s> add         with captured_stdout(), captured_stderr(): </s> remove         old_stderr = sys.stderr
        sys.stderr = err = StringIO()
        try:
            with self.assertRaises(SystemExit):
                management.ManagementUtility(['manage.py', 'dance', '--example=raise']).execute()
        finally:
            sys.stderr = old_stderr
        self.assertIn("CommandError", err.getvalue())
 </s> add         with captured_stderr() as stderr, self.assertRaises(SystemExit):
            management.ManagementUtility(['manage.py', 'dance', '--example=raise']).execute()
        self.assertIn("CommandError", stderr.getvalue()) </s> remove         finally:
            sys.stdout, sys.stderr = old_stdout, old_stderr
 </s> add 
        self.assertEqual(stdout.getvalue(), "All right, let's dance Rock'n'Roll.\n")
          old_stdout, old_stderr = sys.stdout, sys.stderr
          sys.stdout, sys.stderr = StringIO(), StringIO()
          try:
              management.execute_from_command_line(['django-admin', 'optparse_cmd'])
        self.assertEqual(stdout.getvalue(), "All right, let's dance Rock'n'Roll.\n")
        self.assertEqual(stdout.getvalue(), "All right, let's dance Rock'n'Roll.\n")
        self.assertEqual(stdout.getvalue(), "All right, let's dance Rock'n'Roll.\n")
        self.assertEqual(stdout.getvalue(), "All right, let's dance Rock'n'Roll.\n")
  
      def test_calling_a_command_with_only_empty_parameter_should_ends_gracefully(self):
          out = StringIO()
          management.call_command('hal', "--empty", stdout=out)
          self.assertIn("Dave, I can't do that.\n", out.getvalue())
keep keep keep keep replace replace replace replace keep keep keep keep keep
 Fixed #23930 -- Added copies of captured_std* managers from CPython's test.support.

StringIO import was adapted for compatibility with Python 2. 
tests/user_commands/tests.py