@@ -1024,6 +1024,7 @@ PyDoc_STRVAR(update_doc,
1024
1024
static PyObject *
1025
1025
make_new_set (PyTypeObject * type , PyObject * iterable )
1026
1026
{
1027
+ assert (PyType_Check (type ));
1027
1028
PySetObject * so ;
1028
1029
1029
1030
so = (PySetObject * )type -> tp_alloc (type , 0 );
@@ -1064,37 +1065,67 @@ make_new_set_basetype(PyTypeObject *type, PyObject *iterable)
1064
1065
static PyObject * emptyfrozenset = NULL ;
1065
1066
1066
1067
static PyObject *
1067
- frozenset_new (PyTypeObject * type , PyObject * args , PyObject * kwds )
1068
+ make_new_frozenset (PyTypeObject * type , PyObject * iterable )
1068
1069
{
1069
- PyObject * iterable = NULL , * result ;
1070
-
1071
- if (type == & PyFrozenSet_Type && !_PyArg_NoKeywords ("frozenset" , kwds ))
1072
- return NULL ;
1073
-
1074
- if (!PyArg_UnpackTuple (args , type -> tp_name , 0 , 1 , & iterable ))
1075
- return NULL ;
1076
-
1077
- if (type != & PyFrozenSet_Type )
1070
+ if (type != & PyFrozenSet_Type ) {
1078
1071
return make_new_set (type , iterable );
1072
+ }
1079
1073
1080
1074
if (iterable != NULL ) {
1081
- /* frozenset(f) is idempotent */
1082
1075
if (PyFrozenSet_CheckExact (iterable )) {
1076
+ /* frozenset(f) is idempotent */
1083
1077
Py_INCREF (iterable );
1084
1078
return iterable ;
1085
1079
}
1086
- result = make_new_set (type , iterable );
1087
- if (result == NULL || PySet_GET_SIZE (result ))
1088
- return result ;
1089
- Py_DECREF (result );
1080
+ PyObject * res = make_new_set ((PyTypeObject * )type , iterable );
1081
+ if (res == NULL || PySet_GET_SIZE (res ) != 0 ) {
1082
+ return res ;
1083
+ }
1084
+ /* If the created frozenset is empty, return the empty frozenset singleton instead */
1085
+ Py_DECREF (res );
1086
+ }
1087
+
1088
+ // The empty frozenset is a singleton
1089
+ if (emptyfrozenset == NULL ) {
1090
+ emptyfrozenset = make_new_set ((PyTypeObject * )type , NULL );
1090
1091
}
1091
- /* The empty frozenset is a singleton */
1092
- if (emptyfrozenset == NULL )
1093
- emptyfrozenset = make_new_set (type , NULL );
1094
1092
Py_XINCREF (emptyfrozenset );
1095
1093
return emptyfrozenset ;
1096
1094
}
1097
1095
1096
+ static PyObject *
1097
+ frozenset_new (PyTypeObject * type , PyObject * args , PyObject * kwds )
1098
+ {
1099
+ PyObject * iterable = NULL ;
1100
+
1101
+ if (type == & PyFrozenSet_Type && !_PyArg_NoKeywords ("frozenset" , kwds )) {
1102
+ return NULL ;
1103
+ }
1104
+
1105
+ if (!PyArg_UnpackTuple (args , type -> tp_name , 0 , 1 , & iterable )) {
1106
+ return NULL ;
1107
+ }
1108
+
1109
+ return make_new_frozenset (type , iterable );
1110
+ }
1111
+
1112
+ static PyObject *
1113
+ frozenset_vectorcall (PyObject * type , PyObject * const * args ,
1114
+ size_t nargsf , PyObject * kwnames )
1115
+ {
1116
+ if (!_PyArg_NoKwnames ("frozenset" , kwnames )) {
1117
+ return NULL ;
1118
+ }
1119
+
1120
+ Py_ssize_t nargs = PyVectorcall_NARGS (nargsf );
1121
+ if (!_PyArg_CheckPositional ("frozenset" , nargs , 0 , 1 )) {
1122
+ return NULL ;
1123
+ }
1124
+
1125
+ PyObject * iterable = (nargs ? args [0 ] : NULL );
1126
+ return make_new_frozenset ((PyTypeObject * )type , iterable );
1127
+ }
1128
+
1098
1129
static PyObject *
1099
1130
set_new (PyTypeObject * type , PyObject * args , PyObject * kwds )
1100
1131
{
@@ -2283,6 +2314,7 @@ PyTypeObject PyFrozenSet_Type = {
2283
2314
PyType_GenericAlloc , /* tp_alloc */
2284
2315
frozenset_new , /* tp_new */
2285
2316
PyObject_GC_Del , /* tp_free */
2317
+ .tp_vectorcall = frozenset_vectorcall ,
2286
2318
};
2287
2319
2288
2320
0 commit comments