subprocess2_test_script.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #!/usr/bin/env python3
  2. # Copyright (c) 2019 The Chromium Authors. All rights reserved.
  3. # Use of this source code is governed by a BSD-style license that can be
  4. # found in the LICENSE file.
  5. """Script used to test subprocess2."""
  6. import optparse
  7. import os
  8. import sys
  9. import time
  10. if sys.platform == 'win32':
  11. # Annoying, make sure the output is not translated on Windows.
  12. # pylint: disable=no-member,import-error
  13. import msvcrt
  14. msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)
  15. msvcrt.setmode(sys.stderr.fileno(), os.O_BINARY)
  16. parser = optparse.OptionParser()
  17. parser.add_option(
  18. '--fail',
  19. dest='return_value',
  20. action='store_const',
  21. default=0,
  22. const=64)
  23. parser.add_option(
  24. '--crlf', action='store_const', const='\r\n', dest='eol', default='\n')
  25. parser.add_option(
  26. '--cr', action='store_const', const='\r', dest='eol')
  27. parser.add_option('--stdout', action='store_true')
  28. parser.add_option('--stderr', action='store_true')
  29. parser.add_option('--read', action='store_true')
  30. options, args = parser.parse_args()
  31. if args:
  32. parser.error('Internal error')
  33. def do(string):
  34. if options.stdout:
  35. if sys.version_info.major == 2:
  36. sys.stdout.write(string.upper())
  37. sys.stdout.write(options.eol)
  38. else:
  39. sys.stdout.buffer.write(string.upper().encode('utf-8'))
  40. sys.stdout.buffer.write(options.eol.encode('utf-8'))
  41. if options.stderr:
  42. if sys.version_info.major == 2:
  43. sys.stderr.write(string.lower())
  44. sys.stderr.write(options.eol)
  45. else:
  46. sys.stderr.buffer.write(string.lower().encode('utf-8'))
  47. sys.stderr.buffer.write(options.eol.encode('utf-8'))
  48. sys.stderr.flush()
  49. do('A')
  50. do('BB')
  51. do('CCC')
  52. if options.read:
  53. assert options.return_value == 0
  54. try:
  55. while sys.stdin.read(1):
  56. options.return_value += 1
  57. except OSError:
  58. pass
  59. sys.exit(options.return_value)