self.assertRaises(TypeError, array.array, 'xx')
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
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
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))
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
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(), [])
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)
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):
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):
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):
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):
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
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):
// dbmtype_spec does not have Py_TPFLAGS_BASETYPE flag
// which prevents to create a subclass.
// So calling PyType_GetModuleState() in this file is always safe.
- .flags = Py_TPFLAGS_DEFAULT,
+ .flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_DISALLOW_INSTANTIATION,
.slots = dbmtype_spec_slots,
};
static PyType_Spec keyobject_type_spec = {
.name = "functools.KeyWrapper",
.basicsize = sizeof(keyobject),
- .flags = Py_TPFLAGS_DEFAULT,
+ .flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_DISALLOW_INSTANTIATION,
.slots = keyobject_type_slots
};
static PyType_Spec lru_list_elem_type_spec = {
.name = "functools._lru_list_elem",
.basicsize = sizeof(lru_list_elem),
- .flags = Py_TPFLAGS_DEFAULT,
+ .flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_DISALLOW_INSTANTIATION,
.slots = lru_list_elem_type_slots
};
// dbmtype_spec does not have Py_TPFLAGS_BASETYPE flag
// which prevents to create a subclass.
// So calling PyType_GetModuleState() in this file is always safe.
- .flags = Py_TPFLAGS_DEFAULT,
+ .flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_DISALLOW_INSTANTIATION,
.slots = gdbmtype_spec_slots,
};
.name = "re.Pattern",
.basicsize = sizeof(PatternObject),
.itemsize = sizeof(SRE_CODE),
- .flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_IMMUTABLETYPE,
+ .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_IMMUTABLETYPE |
+ Py_TPFLAGS_DISALLOW_INSTANTIATION),
.slots = pattern_slots,
};
.name = "re.Match",
.basicsize = sizeof(MatchObject),
.itemsize = sizeof(Py_ssize_t),
- .flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_IMMUTABLETYPE,
+ .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_IMMUTABLETYPE |
+ Py_TPFLAGS_DISALLOW_INSTANTIATION),
.slots = match_slots,
};
static PyType_Spec scanner_spec = {
.name = "_" SRE_MODULE ".SRE_Scanner",
.basicsize = sizeof(ScannerObject),
- .flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_IMMUTABLETYPE,
+ .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_IMMUTABLETYPE |
+ Py_TPFLAGS_DISALLOW_INSTANTIATION),
.slots = scanner_slots,
};
static PyType_Spec lock_type_spec = {
.name = "_thread.lock",
.basicsize = sizeof(lockobject),
- .flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,
+ .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
+ Py_TPFLAGS_DISALLOW_INSTANTIATION),
.slots = lock_type_slots,
};
static PyType_Spec local_dummy_type_spec = {
.name = "_thread._localdummy",
.basicsize = sizeof(localdummyobject),
- .flags = Py_TPFLAGS_DEFAULT,
+ .flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_DISALLOW_INSTANTIATION,
.slots = local_dummy_type_slots,
};
static PyType_Spec winapi_overlapped_type_spec = {
.name = "_winapi.Overlapped",
.basicsize = sizeof(OverlappedObject),
- .flags = Py_TPFLAGS_DEFAULT,
+ .flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_DISALLOW_INSTANTIATION,
.slots = winapi_overlapped_type_slots,
};
static PyType_Spec arrayiter_spec = {
.name = "array.arrayiterator",
.basicsize = sizeof(arrayiterobject),
- .flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,
+ .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
+ Py_TPFLAGS_DISALLOW_INSTANTIATION),
.slots = arrayiter_slots,
};
static PyType_Spec multibytecodec_spec = {
.name = MODULE_NAME ".MultibyteCodec",
.basicsize = sizeof(MultibyteCodecObject),
- .flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,
+ .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
+ Py_TPFLAGS_DISALLOW_INSTANTIATION),
.slots = multibytecodec_slots,
};
static PyType_Spec _xml_parse_type_spec = {
.name = "pyexpat.xmlparser",
.basicsize = sizeof(xmlparseobject),
- .flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,
+ .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
+ Py_TPFLAGS_DISALLOW_INSTANTIATION),
.slots = _xml_parse_type_spec_slots,
};
static PyType_Spec ucd_type_spec = {
.name = "unicodedata.UCD",
.basicsize = sizeof(PreviousDBVersion),
- .flags = Py_TPFLAGS_DEFAULT,
+ .flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_DISALLOW_INSTANTIATION,
.slots = ucd_type_slots
};
};
static PyType_Spec Comptype_spec = {
- "zlib.Compress",
- sizeof(compobject),
- 0,
- Py_TPFLAGS_DEFAULT,
- Comptype_slots
+ .name = "zlib.Compress",
+ .basicsize = sizeof(compobject),
+ .flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_DISALLOW_INSTANTIATION,
+ .slots= Comptype_slots,
};
static PyType_Slot Decomptype_slots[] = {
};
static PyType_Spec Decomptype_spec = {
- "zlib.Decompress",
- sizeof(compobject),
- 0,
- Py_TPFLAGS_DEFAULT,
- Decomptype_slots
+ .name = "zlib.Decompress",
+ .basicsize = sizeof(compobject),
+ .flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_DISALLOW_INSTANTIATION,
+ .slots = Decomptype_slots,
};
PyDoc_STRVAR(zlib_module_documentation,