bpo-43916: Apply Py_TPFLAGS_DISALLOW_INSTANTIATION to selected types (GH-25748)

Apply Py_TPFLAGS_DISALLOW_INSTANTIATION to the following types:

* _dbm.dbm
* _gdbm.gdbm
* _multibytecodec.MultibyteCodec
* _sre..SRE_Scanner
* _thread._localdummy
* _thread.lock
* _winapi.Overlapped
* array.arrayiterator
* functools.KeyWrapper
* functools._lru_list_elem
* pyexpat.xmlparser
* re.Match
* re.Pattern
* unicodedata.UCD
* zlib.Compress
* zlib.Decompress
diff --git a/Lib/test/test_array.py b/Lib/test/test_array.py
index 11184ad..b18467f 100644
--- a/Lib/test/test_array.py
+++ b/Lib/test/test_array.py
@@ -41,6 +41,12 @@ def test_bad_constructor(self):
         self.assertRaises(ValueError, array.array, 'x')
 
     @support.cpython_only
+    def test_disallow_instantiation(self):
+        # Ensure that the type disallows instantiation (bpo-43916)
+        tp = type(iter(array.array('I')))
+        self.assertRaises(TypeError, tp)
+
+    @support.cpython_only
     def test_immutable(self):
         # bpo-43908: check that array.array is immutable
         with self.assertRaises(TypeError):
diff --git a/Lib/test/test_curses.py b/Lib/test/test_curses.py
index 280a9dc..8bf48a6 100644
--- a/Lib/test/test_curses.py
+++ b/Lib/test/test_curses.py
@@ -6,7 +6,7 @@
 import tempfile
 import unittest
 
-from test.support import requires, verbose, SaveSignals
+from test.support import requires, verbose, SaveSignals, cpython_only
 from test.support.import_helper import import_module
 
 # Optionally test curses module.  This currently requires that the
@@ -1046,8 +1046,10 @@ def __del__(self):
         panel.set_userptr(A())
         panel.set_userptr(None)
 
+    @cpython_only
     @requires_curses_func('panel')
-    def test_new_curses_panel(self):
+    def test_disallow_instantiation(self):
+        # Ensure that the type disallows instantiation (bpo-43916)
         w = curses.newwin(10, 10)
         panel = curses.panel.new_panel(w)
         self.assertRaises(TypeError, type(panel))
diff --git a/Lib/test/test_dbm_gnu.py b/Lib/test/test_dbm_gnu.py
index 017d0ff..b3e5572 100644
--- a/Lib/test/test_dbm_gnu.py
+++ b/Lib/test/test_dbm_gnu.py
@@ -1,5 +1,5 @@
 from test import support
-from test.support import import_helper
+from test.support import import_helper, cpython_only
 gdbm = import_helper.import_module("dbm.gnu") #skip if not supported
 import unittest
 import os
@@ -27,6 +27,13 @@ def tearDown(self):
             self.g.close()
         unlink(filename)
 
+    @cpython_only
+    def test_disallow_instantiation(self):
+        # Ensure that the type disallows instantiation (bpo-43916)
+        self.g = gdbm.open(filename, 'c')
+        tp = type(self.g)
+        self.assertRaises(TypeError, tp)
+
     def test_key_methods(self):
         self.g = gdbm.open(filename, 'c')
         self.assertEqual(self.g.keys(), [])
diff --git a/Lib/test/test_functools.py b/Lib/test/test_functools.py
index fba9281..3320ab7 100644
--- a/Lib/test/test_functools.py
+++ b/Lib/test/test_functools.py
@@ -948,6 +948,12 @@ class TestCmpToKeyC(TestCmpToKey, unittest.TestCase):
     if c_functools:
         cmp_to_key = c_functools.cmp_to_key
 
+    @support.cpython_only
+    def test_disallow_instantiation(self):
+        # Ensure that the type disallows instantiation (bpo-43916)
+        tp = type(c_functools.cmp_to_key(None))
+        self.assertRaises(TypeError, tp)
+
 
 class TestCmpToKeyPy(TestCmpToKey, unittest.TestCase):
     cmp_to_key = staticmethod(py_functools.cmp_to_key)
diff --git a/Lib/test/test_re.py b/Lib/test/test_re.py
index 96d0cdb..e1b2c79 100644
--- a/Lib/test/test_re.py
+++ b/Lib/test/test_re.py
@@ -2215,6 +2215,15 @@ def test_signedness(self):
         self.assertGreaterEqual(sre_compile.MAXREPEAT, 0)
         self.assertGreaterEqual(sre_compile.MAXGROUPS, 0)
 
+    @cpython_only
+    def test_disallow_instantiation(self):
+        # Ensure that the type disallows instantiation (bpo-43916)
+        self.assertRaises(TypeError, re.Match)
+        self.assertRaises(TypeError, re.Pattern)
+        pat = re.compile("")
+        tp = type(pat.scanner(""))
+        self.assertRaises(TypeError, tp)
+
 
 class ExternalTests(unittest.TestCase):
 
diff --git a/Lib/test/test_tcl.py b/Lib/test/test_tcl.py
index ee7344c..cd3aacf6 100644
--- a/Lib/test/test_tcl.py
+++ b/Lib/test/test_tcl.py
@@ -736,8 +736,11 @@ def check(value):
         check('{\n')
         check('}\n')
 
+    @support.cpython_only
     def test_new_tcl_obj(self):
         self.assertRaises(TypeError, _tkinter.Tcl_Obj)
+        self.assertRaises(TypeError, _tkinter.TkttType)
+        self.assertRaises(TypeError, _tkinter.TkappType)
 
 class BigmemTclTest(unittest.TestCase):
 
diff --git a/Lib/test/test_threading.py b/Lib/test/test_threading.py
index f44f17f..546773e 100644
--- a/Lib/test/test_threading.py
+++ b/Lib/test/test_threading.py
@@ -119,6 +119,13 @@ def func(): pass
             thread = threading.Thread(target=func)
             self.assertEqual(thread.name, "Thread-5 (func)")
 
+    @cpython_only
+    def test_disallow_instantiation(self):
+        # Ensure that the type disallows instantiation (bpo-43916)
+        lock = threading.Lock()
+        tp = type(lock)
+        self.assertRaises(TypeError, tp)
+
     # Create a bunch of threads, let each do some work, wait until all are
     # done.
     def test_various_ops(self):
diff --git a/Lib/test/test_unicodedata.py b/Lib/test/test_unicodedata.py
index edfd860..c6bbe3f 100644
--- a/Lib/test/test_unicodedata.py
+++ b/Lib/test/test_unicodedata.py
@@ -11,7 +11,8 @@
 import sys
 import unicodedata
 import unittest
-from test.support import open_urlresource, requires_resource, script_helper
+from test.support import (open_urlresource, requires_resource, script_helper,
+                          cpython_only)
 
 
 class UnicodeMethodsTest(unittest.TestCase):
@@ -225,6 +226,11 @@ def test_east_asian_width_9_0_changes(self):
 
 class UnicodeMiscTest(UnicodeDatabaseTest):
 
+    @cpython_only
+    def test_disallow_instantiation(self):
+        # Ensure that the type disallows instantiation (bpo-43916)
+        self.assertRaises(TypeError, unicodedata.UCD)
+
     def test_failed_import_during_compiling(self):
         # Issue 4367
         # Decoding \N escapes requires the unicodedata module. If it can't be
diff --git a/Lib/test/test_zlib.py b/Lib/test/test_zlib.py
index 7f30cac..694ef6e 100644
--- a/Lib/test/test_zlib.py
+++ b/Lib/test/test_zlib.py
@@ -129,6 +129,14 @@ def test_overflow(self):
         with self.assertRaisesRegex(OverflowError, 'int too large'):
             zlib.decompressobj().flush(sys.maxsize + 1)
 
+    @support.cpython_only
+    def test_disallow_instantiation(self):
+        # Ensure that the type disallows instantiation (bpo-43916)
+        comp_type = type(zlib.compressobj())
+        decomp_type = type(zlib.decompressobj())
+        self.assertRaises(TypeError, comp_type)
+        self.assertRaises(TypeError, decomp_type)
+
 
 class BaseCompressTestCase(object):
     def check_big_compress_buffer(self, size, compress_func):