test_winconsoleio.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. '''Tests for WindowsConsoleIO
  2. '''
  3. import io
  4. import os
  5. import sys
  6. import tempfile
  7. import unittest
  8. from test.support import os_helper
  9. if sys.platform != 'win32':
  10. raise unittest.SkipTest("test only relevant on win32")
  11. from _testconsole import write_input
  12. ConIO = io._WindowsConsoleIO
  13. class WindowsConsoleIOTests(unittest.TestCase):
  14. def test_abc(self):
  15. self.assertTrue(issubclass(ConIO, io.RawIOBase))
  16. self.assertFalse(issubclass(ConIO, io.BufferedIOBase))
  17. self.assertFalse(issubclass(ConIO, io.TextIOBase))
  18. def test_open_fd(self):
  19. self.assertRaisesRegex(ValueError,
  20. "negative file descriptor", ConIO, -1)
  21. with tempfile.TemporaryFile() as tmpfile:
  22. fd = tmpfile.fileno()
  23. # Windows 10: "Cannot open non-console file"
  24. # Earlier: "Cannot open console output buffer for reading"
  25. self.assertRaisesRegex(ValueError,
  26. "Cannot open (console|non-console file)", ConIO, fd)
  27. try:
  28. f = ConIO(0)
  29. except ValueError:
  30. # cannot open console because it's not a real console
  31. pass
  32. else:
  33. self.assertTrue(f.readable())
  34. self.assertFalse(f.writable())
  35. self.assertEqual(0, f.fileno())
  36. f.close() # multiple close should not crash
  37. f.close()
  38. try:
  39. f = ConIO(1, 'w')
  40. except ValueError:
  41. # cannot open console because it's not a real console
  42. pass
  43. else:
  44. self.assertFalse(f.readable())
  45. self.assertTrue(f.writable())
  46. self.assertEqual(1, f.fileno())
  47. f.close()
  48. f.close()
  49. try:
  50. f = ConIO(2, 'w')
  51. except ValueError:
  52. # cannot open console because it's not a real console
  53. pass
  54. else:
  55. self.assertFalse(f.readable())
  56. self.assertTrue(f.writable())
  57. self.assertEqual(2, f.fileno())
  58. f.close()
  59. f.close()
  60. def test_open_name(self):
  61. self.assertRaises(ValueError, ConIO, sys.executable)
  62. f = ConIO("CON")
  63. self.assertTrue(f.readable())
  64. self.assertFalse(f.writable())
  65. self.assertIsNotNone(f.fileno())
  66. f.close() # multiple close should not crash
  67. f.close()
  68. f = ConIO('CONIN$')
  69. self.assertTrue(f.readable())
  70. self.assertFalse(f.writable())
  71. self.assertIsNotNone(f.fileno())
  72. f.close()
  73. f.close()
  74. f = ConIO('CONOUT$', 'w')
  75. self.assertFalse(f.readable())
  76. self.assertTrue(f.writable())
  77. self.assertIsNotNone(f.fileno())
  78. f.close()
  79. f.close()
  80. # bpo-45354: Windows 11 changed MS-DOS device name handling
  81. if sys.getwindowsversion()[:3] < (10, 0, 22000):
  82. f = open('C:/con', 'rb', buffering=0)
  83. self.assertIsInstance(f, ConIO)
  84. f.close()
  85. @unittest.skipIf(sys.getwindowsversion()[:2] <= (6, 1),
  86. "test does not work on Windows 7 and earlier")
  87. def test_conin_conout_names(self):
  88. f = open(r'\\.\conin$', 'rb', buffering=0)
  89. self.assertIsInstance(f, ConIO)
  90. f.close()
  91. f = open('//?/conout$', 'wb', buffering=0)
  92. self.assertIsInstance(f, ConIO)
  93. f.close()
  94. def test_conout_path(self):
  95. temp_path = tempfile.mkdtemp()
  96. self.addCleanup(os_helper.rmtree, temp_path)
  97. conout_path = os.path.join(temp_path, 'CONOUT$')
  98. with open(conout_path, 'wb', buffering=0) as f:
  99. # bpo-45354: Windows 11 changed MS-DOS device name handling
  100. if (6, 1) < sys.getwindowsversion()[:3] < (10, 0, 22000):
  101. self.assertIsInstance(f, ConIO)
  102. else:
  103. self.assertNotIsInstance(f, ConIO)
  104. def test_write_empty_data(self):
  105. with ConIO('CONOUT$', 'w') as f:
  106. self.assertEqual(f.write(b''), 0)
  107. def assertStdinRoundTrip(self, text):
  108. stdin = open('CONIN$', 'r')
  109. old_stdin = sys.stdin
  110. try:
  111. sys.stdin = stdin
  112. write_input(
  113. stdin.buffer.raw,
  114. (text + '\r\n').encode('utf-16-le', 'surrogatepass')
  115. )
  116. actual = input()
  117. finally:
  118. sys.stdin = old_stdin
  119. self.assertEqual(actual, text)
  120. def test_input(self):
  121. # ASCII
  122. self.assertStdinRoundTrip('abc123')
  123. # Non-ASCII
  124. self.assertStdinRoundTrip('ϼўТλФЙ')
  125. # Combining characters
  126. self.assertStdinRoundTrip('A͏B ﬖ̳AA̝')
  127. # bpo-38325
  128. @unittest.skipIf(True, "Handling Non-BMP characters is broken")
  129. def test_input_nonbmp(self):
  130. # Non-BMP
  131. self.assertStdinRoundTrip('\U00100000\U0010ffff\U0010fffd')
  132. def test_partial_reads(self):
  133. # Test that reading less than 1 full character works when stdin
  134. # contains multibyte UTF-8 sequences
  135. source = 'ϼўТλФЙ\r\n'.encode('utf-16-le')
  136. expected = 'ϼўТλФЙ\r\n'.encode('utf-8')
  137. for read_count in range(1, 16):
  138. with open('CONIN$', 'rb', buffering=0) as stdin:
  139. write_input(stdin, source)
  140. actual = b''
  141. while not actual.endswith(b'\n'):
  142. b = stdin.read(read_count)
  143. actual += b
  144. self.assertEqual(actual, expected, 'stdin.read({})'.format(read_count))
  145. # bpo-38325
  146. @unittest.skipIf(True, "Handling Non-BMP characters is broken")
  147. def test_partial_surrogate_reads(self):
  148. # Test that reading less than 1 full character works when stdin
  149. # contains surrogate pairs that cannot be decoded to UTF-8 without
  150. # reading an extra character.
  151. source = '\U00101FFF\U00101001\r\n'.encode('utf-16-le')
  152. expected = '\U00101FFF\U00101001\r\n'.encode('utf-8')
  153. for read_count in range(1, 16):
  154. with open('CONIN$', 'rb', buffering=0) as stdin:
  155. write_input(stdin, source)
  156. actual = b''
  157. while not actual.endswith(b'\n'):
  158. b = stdin.read(read_count)
  159. actual += b
  160. self.assertEqual(actual, expected, 'stdin.read({})'.format(read_count))
  161. def test_ctrl_z(self):
  162. with open('CONIN$', 'rb', buffering=0) as stdin:
  163. source = '\xC4\x1A\r\n'.encode('utf-16-le')
  164. expected = '\xC4'.encode('utf-8')
  165. write_input(stdin, source)
  166. a, b = stdin.read(1), stdin.readall()
  167. self.assertEqual(expected[0:1], a)
  168. self.assertEqual(expected[1:], b)
  169. if __name__ == "__main__":
  170. unittest.main()