# CRASHES getitem(NULL, 'a')
def test_dict_contains(self):
+ # Test PyDict_Contains()
contains = _testlimitedcapi.dict_contains
dct = {'a': 1, '\U0001f40d': 2}
self.assertTrue(contains(dct, 'a'))
self.assertRaises(TypeError, contains, {}, []) # unhashable
# CRASHES contains({}, NULL)
- # CRASHES contains(UserDict(), 'a')
- # CRASHES contains(42, 'a')
+ self.assertRaises(SystemError, contains, UserDict(), 'a')
+ self.assertRaises(SystemError, contains, 42, 'a')
# CRASHES contains(NULL, 'a')
def test_dict_contains_string(self):
+ # Test PyDict_ContainsString()
contains_string = _testcapi.dict_containsstring
dct = {'a': 1, '\U0001f40d': 2}
self.assertTrue(contains_string(dct, b'a'))
self.assertTrue(contains_string(dct2, b'a'))
self.assertFalse(contains_string(dct2, b'b'))
+ self.assertRaises(SystemError, contains_string, UserDict(), 'a')
+ self.assertRaises(SystemError, contains_string, 42, 'a')
# CRASHES contains({}, NULL)
# CRASHES contains(NULL, b'a')
PyObject *kwds);
static PyObject* dict_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
static int dict_merge(PyObject *a, PyObject *b, int override);
+static int dict_contains(PyObject *op, PyObject *key);
static int dict_merge_from_seq2(PyObject *d, PyObject *seq2, int override);
for (key = PyIter_Next(iter); key; key = PyIter_Next(iter)) {
if (override != 1) {
- status = PyDict_Contains(a, key);
+ status = dict_contains(a, key);
if (status != 0) {
if (status > 0) {
if (override == 0) {
dict___contains___impl(PyDictObject *self, PyObject *key)
/*[clinic end generated code: output=1b314e6da7687dae input=fe1cb42ad831e820]*/
{
- int contains = PyDict_Contains((PyObject *)self, key);
+ int contains = dict_contains((PyObject *)self, key);
if (contains < 0) {
return NULL;
}
{NULL, NULL} /* sentinel */
};
-/* Return 1 if `key` is in dict `op`, 0 if not, and -1 on error. */
-int
-PyDict_Contains(PyObject *op, PyObject *key)
+static int
+dict_contains(PyObject *op, PyObject *key)
{
Py_hash_t hash = _PyObject_HashFast(key);
if (hash == -1) {
return _PyDict_Contains_KnownHash(op, key, hash);
}
+/* Return 1 if `key` is in dict `op`, 0 if not, and -1 on error. */
+int
+PyDict_Contains(PyObject *op, PyObject *key)
+{
+ if (!PyAnyDict_Check(op)) {
+ PyErr_BadInternalCall();
+ return -1;
+ }
+
+ return dict_contains(op, key);
+}
+
int
PyDict_ContainsString(PyObject *op, const char *key)
{
int
_PyDict_Contains_KnownHash(PyObject *op, PyObject *key, Py_hash_t hash)
{
- PyDictObject *mp = (PyDictObject *)op;
+ PyDictObject *mp = _PyAnyDict_CAST(op);
PyObject *value;
Py_ssize_t ix;
0, /* sq_slice */
0, /* sq_ass_item */
0, /* sq_ass_slice */
- PyDict_Contains, /* sq_contains */
+ dict_contains, /* sq_contains */
0, /* sq_inplace_concat */
0, /* sq_inplace_repeat */
};
_PyDictViewObject *dv = (_PyDictViewObject *)self;
if (dv->dv_dict == NULL)
return 0;
- return PyDict_Contains((PyObject *)dv->dv_dict, obj);
+ return dict_contains((PyObject *)dv->dv_dict, obj);
}
static PySequenceMethods dictkeys_as_sequence = {