def test_dict_copy(self):
# Test PyDict_Copy()
copy = _testlimitedcapi.dict_copy
- for dict_type in ANYDICT_TYPES:
+ for dict_type in DICT_TYPES:
dct = dict_type({1: 2})
dct_copy = copy(dct)
- if dict_type == frozendict:
- expected_type = frozendict
- self.assertIs(dct_copy, dct)
- else:
- if issubclass(dict_type, frozendict):
- expected_type = frozendict
- else:
- expected_type = dict
- self.assertIs(type(dct_copy), expected_type)
- self.assertEqual(dct_copy, dct)
+ self.assertIs(type(dct_copy), dict)
+ self.assertEqual(dct_copy, dct)
- for test_type in NOT_ANYDICT_TYPES + OTHER_TYPES:
+ for test_type in NOT_DICT_TYPES + OTHER_TYPES:
self.assertRaises(SystemError, copy, test_type())
self.assertRaises(SystemError, copy, NULL)
{
return dict_values_impl((PyDictObject *)self);
}
-/*[clinic end generated code: output=9007b74432217017 input=a9049054013a1b77]*/
+
+PyDoc_STRVAR(frozendict_copy__doc__,
+"copy($self, /)\n"
+"--\n"
+"\n"
+"Return a shallow copy of the frozendict.");
+
+#define FROZENDICT_COPY_METHODDEF \
+ {"copy", (PyCFunction)frozendict_copy, METH_NOARGS, frozendict_copy__doc__},
+
+static PyObject *
+frozendict_copy_impl(PyFrozenDictObject *self);
+
+static PyObject *
+frozendict_copy(PyObject *self, PyObject *Py_UNUSED(ignored))
+{
+ return frozendict_copy_impl((PyFrozenDictObject *)self);
+}
+/*[clinic end generated code: output=f4c88a3464928ae3 input=a9049054013a1b77]*/
/*[clinic input]
class dict "PyDictObject *" "&PyDict_Type"
+class frozendict "PyFrozenDictObject *" "&PyFrozenDict_Type"
[clinic start generated code]*/
-/*[clinic end generated code: output=da39a3ee5e6b4b0d input=f157a5a0ce9589d6]*/
+/*[clinic end generated code: output=da39a3ee5e6b4b0d input=5dfa93bac68e7c54]*/
/*
}
const char *errmsg;
- if (PyObject_IsInstance(op, (PyObject*)&PyFrozenDict_Type)) {
+ if (PyFrozenDict_Check(op)) {
errmsg = "cannot use '%T' as a frozendict key (%S)";
}
else {
return NULL;
}
-// Similar to PyDict_Copy(), but copy also frozendict.
-static PyObject *
-_PyDict_Copy(PyObject *o)
+PyObject *
+PyDict_Copy(PyObject *o)
{
- assert(PyAnyDict_Check(o));
+ if (o == NULL || !PyDict_Check(o)) {
+ PyErr_BadInternalCall();
+ return NULL;
+ }
PyObject *res;
Py_BEGIN_CRITICAL_SECTION(o);
- res = copy_lock_held(o, PyFrozenDict_Check(o));
+ res = copy_lock_held(o, 0);
Py_END_CRITICAL_SECTION();
return res;
}
-PyObject *
-PyDict_Copy(PyObject *o)
+// Similar to PyDict_Copy(), but return a frozendict if the argument
+// is a frozendict.
+static PyObject *
+anydict_copy(PyObject *o)
{
- if (o == NULL || !PyAnyDict_Check(o)) {
- PyErr_BadInternalCall();
- return NULL;
- }
-
- if (PyFrozenDict_CheckExact(o)) {
- return Py_NewRef(o);
- }
+ assert(PyAnyDict_Check(o));
- return _PyDict_Copy(o);
+ PyObject *res;
+ Py_BEGIN_CRITICAL_SECTION(o);
+ res = copy_lock_held(o, PyFrozenDict_Check(o));
+ Py_END_CRITICAL_SECTION();
+ return res;
}
-// Similar to PyDict_Copy(), but return a dict if the argument is a frozendict.
+// Similar to PyDict_Copy(), but accept also frozendict:
+// convert frozendict to a new dict.
PyObject*
_PyDict_CopyAsDict(PyObject *o)
{
if (!PyAnyDict_Check(self) || !PyAnyDict_Check(other)) {
Py_RETURN_NOTIMPLEMENTED;
}
- PyObject *new = _PyDict_Copy(self);
+ PyObject *new = anydict_copy(self);
if (new == NULL) {
return NULL;
}
DICT_ITEMS_METHODDEF
DICT_VALUES_METHODDEF
DICT_FROMKEYS_METHODDEF
- DICT_COPY_METHODDEF
+ FROZENDICT_COPY_METHODDEF
DICT___REVERSED___METHODDEF
{"__class_getitem__", Py_GenericAlias, METH_O|METH_CLASS, PyDoc_STR("See PEP 585")},
{"__getnewargs__", frozendict_getnewargs, METH_NOARGS},
}
}
+/*[clinic input]
+frozendict.copy
+
+Return a shallow copy of the frozendict.
+[clinic start generated code]*/
+
+static PyObject *
+frozendict_copy_impl(PyFrozenDictObject *self)
+/*[clinic end generated code: output=e580fd91d9fc2cf7 input=35f6abeaa08fd4bc]*/
+{
+ assert(PyFrozenDict_Check(self));
+
+ if (PyFrozenDict_CheckExact(self)) {
+ return Py_NewRef(self);
+ }
+
+ return anydict_copy((PyObject*)self);
+}
+
PyTypeObject PyFrozenDict_Type = {
PyVarObject_HEAD_INIT(&PyType_Type, 0)