summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_capi.py
diff options
context:
space:
mode:
Diffstat (limited to 'Lib/test/test_capi.py')
-rw-r--r--Lib/test/test_capi.py71
1 files changed, 57 insertions, 14 deletions
diff --git a/Lib/test/test_capi.py b/Lib/test/test_capi.py
index 3f1d63c..327ac66 100644
--- a/Lib/test/test_capi.py
+++ b/Lib/test/test_capi.py
@@ -2,12 +2,17 @@
# these are all functions _testcapi exports whose name begins with 'test_'.
from __future__ import with_statement
+import os
+import random
+import subprocess
import sys
import time
-import random
import unittest
-import threading
from test import support
+try:
+ import threading
+except ImportError:
+ threading = None
import _testcapi
@@ -32,7 +37,24 @@ class CAPITest(unittest.TestCase):
self.assertEqual(testfunction.attribute, "test")
self.assertRaises(AttributeError, setattr, inst.testfunction, "attribute", "test")
-
+ @unittest.skipUnless(threading, 'Threading required for this test.')
+ def test_no_FatalError_infinite_loop(self):
+ p = subprocess.Popen([sys.executable, "-c",
+ 'import _testcapi;'
+ '_testcapi.crash_no_current_thread()'],
+ stdout=subprocess.PIPE,
+ stderr=subprocess.PIPE)
+ (out, err) = p.communicate()
+ self.assertEqual(out, b'')
+ # This used to cause an infinite loop.
+ self.assertEqual(err.rstrip(),
+ b'Fatal Python error:'
+ b' PyThreadState_Get: no current thread')
+
+ def test_memoryview_from_NULL_pointer(self):
+ self.assertRaises(ValueError, _testcapi.make_memoryview_from_NULL_pointer)
+
+@unittest.skipUnless(threading, 'Threading required for this test.')
class TestPendingCalls(unittest.TestCase):
def pendingcalls_submit(self, l, n):
@@ -120,8 +142,38 @@ class Test6012(unittest.TestCase):
def test(self):
self.assertEqual(_testcapi.argparsing("Hello", "World"), 1)
+
+class EmbeddingTest(unittest.TestCase):
+
+ def test_subinterps(self):
+ # XXX only tested under Unix checkouts
+ basepath = os.path.dirname(os.path.dirname(os.path.dirname(__file__)))
+ oldcwd = os.getcwd()
+ # This is needed otherwise we get a fatal error:
+ # "Py_Initialize: Unable to get the locale encoding
+ # LookupError: no codec search functions registered: can't find encoding"
+ os.chdir(basepath)
+ try:
+ exe = os.path.join(basepath, "Modules", "_testembed")
+ if not os.path.exists(exe):
+ self.skipTest("%r doesn't exist" % exe)
+ p = subprocess.Popen([exe],
+ stdout=subprocess.PIPE,
+ stderr=subprocess.PIPE)
+ (out, err) = p.communicate()
+ self.assertEqual(p.returncode, 0,
+ "bad returncode %d, stderr is %r" %
+ (p.returncode, err))
+ if support.verbose:
+ print()
+ print(out.decode('latin1'))
+ print(err.decode('latin1'))
+ finally:
+ os.chdir(oldcwd)
+
+
def test_main():
- support.run_unittest(CAPITest)
+ support.run_unittest(CAPITest, TestPendingCalls, Test6012, EmbeddingTest)
for name in dir(_testcapi):
if name.startswith('test_'):
@@ -148,23 +200,14 @@ def test_main():
raise support.TestFailed(
"Couldn't find main thread correctly in the list")
- try:
- _testcapi._test_thread_state
- have_thread_state = True
- except AttributeError:
- have_thread_state = False
-
- if have_thread_state:
+ if threading:
import _thread
import time
TestThreadState()
- import threading
t = threading.Thread(target=TestThreadState)
t.start()
t.join()
- support.run_unittest(TestPendingCalls, Test6012)
-
if __name__ == "__main__":
test_main()