|
9 | 9 | import sys |
10 | 10 | import time |
11 | 11 | import unittest |
| 12 | +import textwrap |
12 | 13 | from test import support |
13 | 14 | try: |
14 | 15 | import _posixsubprocess |
@@ -218,36 +219,81 @@ def test(self): |
218 | 219 | self.assertEqual(_testcapi.argparsing("Hello", "World"), 1) |
219 | 220 |
|
220 | 221 |
|
221 | | -class EmbeddingTest(unittest.TestCase): |
| 222 | +@unittest.skipIf( |
| 223 | + sys.platform.startswith('win'), |
| 224 | + "interpreter embedding tests aren't built under Windows") |
| 225 | +class EmbeddingTests(unittest.TestCase): |
| 226 | + # XXX only tested under Unix checkouts |
222 | 227 |
|
223 | | - @unittest.skipIf( |
224 | | - sys.platform.startswith('win'), |
225 | | - "test doesn't work under Windows") |
226 | | - def test_subinterps(self): |
227 | | - # XXX only tested under Unix checkouts |
| 228 | + def setUp(self): |
228 | 229 | basepath = os.path.dirname(os.path.dirname(os.path.dirname(__file__))) |
229 | | - oldcwd = os.getcwd() |
| 230 | + self.test_exe = exe = os.path.join(basepath, "Modules", "_testembed") |
| 231 | + if not os.path.exists(exe): |
| 232 | + self.skipTest("%r doesn't exist" % exe) |
230 | 233 | # This is needed otherwise we get a fatal error: |
231 | 234 | # "Py_Initialize: Unable to get the locale encoding |
232 | 235 | # LookupError: no codec search functions registered: can't find encoding" |
| 236 | + self.oldcwd = os.getcwd() |
233 | 237 | os.chdir(basepath) |
234 | | - try: |
235 | | - exe = os.path.join(basepath, "Modules", "_testembed") |
236 | | - if not os.path.exists(exe): |
237 | | - self.skipTest("%r doesn't exist" % exe) |
238 | | - p = subprocess.Popen([exe], |
239 | | - stdout=subprocess.PIPE, |
240 | | - stderr=subprocess.PIPE) |
241 | | - (out, err) = p.communicate() |
242 | | - self.assertEqual(p.returncode, 0, |
243 | | - "bad returncode %d, stderr is %r" % |
244 | | - (p.returncode, err)) |
245 | | - if support.verbose: |
246 | | - print() |
247 | | - print(out.decode('latin1')) |
248 | | - print(err.decode('latin1')) |
249 | | - finally: |
250 | | - os.chdir(oldcwd) |
| 238 | + |
| 239 | + def tearDown(self): |
| 240 | + os.chdir(self.oldcwd) |
| 241 | + |
| 242 | + def run_embedded_interpreter(self, *args): |
| 243 | + """Runs a test in the embedded interpreter""" |
| 244 | + cmd = [self.test_exe] |
| 245 | + cmd.extend(args) |
| 246 | + p = subprocess.Popen(cmd, |
| 247 | + stdout=subprocess.PIPE, |
| 248 | + stderr=subprocess.PIPE) |
| 249 | + (out, err) = p.communicate() |
| 250 | + self.assertEqual(p.returncode, 0, |
| 251 | + "bad returncode %d, stderr is %r" % |
| 252 | + (p.returncode, err)) |
| 253 | + return out.decode("latin1"), err.decode("latin1") |
| 254 | + |
| 255 | + def test_subinterps(self): |
| 256 | + # This is just a "don't crash" test |
| 257 | + out, err = self.run_embedded_interpreter() |
| 258 | + if support.verbose: |
| 259 | + print() |
| 260 | + print(out) |
| 261 | + print(err) |
| 262 | + |
| 263 | + def test_forced_io_encoding(self): |
| 264 | + # Checks forced configuration of embedded interpreter IO streams |
| 265 | + out, err = self.run_embedded_interpreter("forced_io_encoding") |
| 266 | + if support.verbose: |
| 267 | + print() |
| 268 | + print(out) |
| 269 | + print(err) |
| 270 | + expected_output = textwrap.dedent("""\ |
| 271 | + --- Use defaults --- |
| 272 | + Expected encoding: default |
| 273 | + Expected errors: default |
| 274 | + stdin: {0.stdin.encoding}:strict |
| 275 | + stdout: {0.stdout.encoding}:strict |
| 276 | + stderr: {0.stderr.encoding}:backslashreplace |
| 277 | + --- Set errors only --- |
| 278 | + Expected encoding: default |
| 279 | + Expected errors: surrogateescape |
| 280 | + stdin: {0.stdin.encoding}:surrogateescape |
| 281 | + stdout: {0.stdout.encoding}:surrogateescape |
| 282 | + stderr: {0.stderr.encoding}:backslashreplace |
| 283 | + --- Set encoding only --- |
| 284 | + Expected encoding: latin-1 |
| 285 | + Expected errors: default |
| 286 | + stdin: latin-1:strict |
| 287 | + stdout: latin-1:strict |
| 288 | + stderr: latin-1:backslashreplace |
| 289 | + --- Set encoding and errors --- |
| 290 | + Expected encoding: latin-1 |
| 291 | + Expected errors: surrogateescape |
| 292 | + stdin: latin-1:surrogateescape |
| 293 | + stdout: latin-1:surrogateescape |
| 294 | + stderr: latin-1:backslashreplace""").format(sys) |
| 295 | + |
| 296 | + self.assertEqual(out.strip(), expected_output) |
251 | 297 |
|
252 | 298 | class SkipitemTest(unittest.TestCase): |
253 | 299 |
|
@@ -358,7 +404,7 @@ def callback(): |
358 | 404 |
|
359 | 405 | def test_main(): |
360 | 406 | support.run_unittest(CAPITest, TestPendingCalls, Test6012, |
361 | | - EmbeddingTest, SkipitemTest, TestThreadState, |
| 407 | + EmbeddingTests, SkipitemTest, TestThreadState, |
362 | 408 | SubinterpreterTest) |
363 | 409 |
|
364 | 410 | for name in dir(_testcapi): |
|
0 commit comments