@@ -1990,6 +1990,13 @@ defdict_missing(defdictobject *dd, PyObject *key)
19901990 return value ;
19911991}
19921992
1993+ static inline PyObject *
1994+ new_defdict (defdictobject * dd , PyObject * arg )
1995+ {
1996+ return PyObject_CallFunctionObjArgs ((PyObject * )Py_TYPE (dd ),
1997+ dd -> default_factory ? dd -> default_factory : Py_None , arg , NULL );
1998+ }
1999+
19932000PyDoc_STRVAR (defdict_copy_doc , "D.copy() -> a shallow copy of D." );
19942001
19952002static PyObject *
@@ -1999,11 +2006,7 @@ defdict_copy(defdictobject *dd, PyObject *Py_UNUSED(ignored))
19992006 whose class constructor has the same signature. Subclasses that
20002007 define a different constructor signature must override copy().
20012008 */
2002-
2003- if (dd -> default_factory == NULL )
2004- return PyObject_CallFunctionObjArgs ((PyObject * )Py_TYPE (dd ), Py_None , dd , NULL );
2005- return PyObject_CallFunctionObjArgs ((PyObject * )Py_TYPE (dd ),
2006- dd -> default_factory , dd , NULL );
2009+ return new_defdict (dd , (PyObject * )dd );
20072010}
20082011
20092012static PyObject *
@@ -2127,6 +2130,42 @@ defdict_repr(defdictobject *dd)
21272130 return result ;
21282131}
21292132
2133+ static PyObject *
2134+ defdict_or (PyObject * left , PyObject * right )
2135+ {
2136+ int left_is_self = PyObject_IsInstance (left , (PyObject * )& defdict_type );
2137+ if (left_is_self < 0 ) {
2138+ return NULL ;
2139+ }
2140+ PyObject * self , * other ;
2141+ if (left_is_self ) {
2142+ self = left ;
2143+ other = right ;
2144+ }
2145+ else {
2146+ self = right ;
2147+ other = left ;
2148+ }
2149+ if (!PyDict_Check (other )) {
2150+ Py_RETURN_NOTIMPLEMENTED ;
2151+ }
2152+ // Like copy(), this calls the object's class.
2153+ // Override __or__/__ror__ for subclasses with different constructors.
2154+ PyObject * new = new_defdict ((defdictobject * )self , left );
2155+ if (!new ) {
2156+ return NULL ;
2157+ }
2158+ if (PyDict_Update (new , right )) {
2159+ Py_DECREF (new );
2160+ return NULL ;
2161+ }
2162+ return new ;
2163+ }
2164+
2165+ static PyNumberMethods defdict_as_number = {
2166+ .nb_or = defdict_or ,
2167+ };
2168+
21302169static int
21312170defdict_traverse (PyObject * self , visitproc visit , void * arg )
21322171{
@@ -2198,7 +2237,7 @@ static PyTypeObject defdict_type = {
21982237 0 , /* tp_setattr */
21992238 0 , /* tp_as_async */
22002239 (reprfunc )defdict_repr , /* tp_repr */
2201- 0 , /* tp_as_number */
2240+ & defdict_as_number , /* tp_as_number */
22022241 0 , /* tp_as_sequence */
22032242 0 , /* tp_as_mapping */
22042243 0 , /* tp_hash */
0 commit comments