99
1010#include "Python.h"
1111#include "structmember.h"
12+ #include "stringlib/eq.h"
1213
1314/* Set a key error with the specified argument, wrapping it in a
1415 * tuple automatically so that tuple keys are not unpacked as the
@@ -55,6 +56,7 @@ _PySet_Dummy(void)
5556static PySetObject * free_sets [MAXFREESETS ];
5657static int num_free_sets = 0 ;
5758
59+
5860/*
5961The basic lookup function used by all operations.
6062This is based on Algorithm D from Knuth Vol. 3, Sec. 6.4.
@@ -144,12 +146,12 @@ set_lookkey(PySetObject *so, PyObject *key, register long hash)
144146}
145147
146148/*
147- * Hacked up version of set_lookkey which can assume keys are always strings ;
148- * This means we can always use _PyString_Eq directly and not have to check to
149+ * Hacked up version of set_lookkey which can assume keys are always unicode ;
150+ * This means we can always use unicode_eq directly and not have to check to
149151 * see if the comparison altered the table.
150152 */
151153static setentry *
152- set_lookkey_string (PySetObject * so , PyObject * key , register long hash )
154+ set_lookkey_unicode (PySetObject * so , PyObject * key , register long hash )
153155{
154156 register Py_ssize_t i ;
155157 register size_t perturb ;
@@ -158,11 +160,11 @@ set_lookkey_string(PySetObject *so, PyObject *key, register long hash)
158160 setentry * table = so -> table ;
159161 register setentry * entry ;
160162
161- /* Make sure this function doesn't have to handle non-string keys,
163+ /* Make sure this function doesn't have to handle non-unicode keys,
162164 including subclasses of str; e.g., one reason to subclass
163165 strings is to override __eq__, and for speed we don't cater to
164166 that here. */
165- if (!PyString_CheckExact (key )) {
167+ if (!PyUnicode_CheckExact (key )) {
166168 so -> lookup = set_lookkey ;
167169 return set_lookkey (so , key , hash );
168170 }
@@ -173,7 +175,7 @@ set_lookkey_string(PySetObject *so, PyObject *key, register long hash)
173175 if (entry -> key == dummy )
174176 freeslot = entry ;
175177 else {
176- if (entry -> hash == hash && _PyString_Eq (entry -> key , key ))
178+ if (entry -> hash == hash && unicode_eq (entry -> key , key ))
177179 return entry ;
178180 freeslot = NULL ;
179181 }
@@ -188,7 +190,7 @@ set_lookkey_string(PySetObject *so, PyObject *key, register long hash)
188190 if (entry -> key == key
189191 || (entry -> hash == hash
190192 && entry -> key != dummy
191- && _PyString_Eq (entry -> key , key )))
193+ && unicode_eq (entry -> key , key )))
192194 return entry ;
193195 if (entry -> key == dummy && freeslot == NULL )
194196 freeslot = entry ;
@@ -375,8 +377,8 @@ set_add_key(register PySetObject *so, PyObject *key)
375377 register long hash ;
376378 register Py_ssize_t n_used ;
377379
378- if (!PyString_CheckExact (key ) ||
379- (hash = ((PyStringObject * ) key )-> ob_shash ) == -1 ) {
380+ if (!PyUnicode_CheckExact (key ) ||
381+ (hash = ((PyUnicodeObject * ) key )-> hash ) == -1 ) {
380382 hash = PyObject_Hash (key );
381383 if (hash == -1 )
382384 return -1 ;
@@ -422,8 +424,9 @@ set_discard_key(PySetObject *so, PyObject *key)
422424 PyObject * old_key ;
423425
424426 assert (PyAnySet_Check (so ));
425- if (!PyString_CheckExact (key ) ||
426- (hash = ((PyStringObject * ) key )-> ob_shash ) == -1 ) {
427+
428+ if (!PyUnicode_CheckExact (key ) ||
429+ (hash = ((PyUnicodeObject * ) key )-> hash ) == -1 ) {
427430 hash = PyObject_Hash (key );
428431 if (hash == -1 )
429432 return -1 ;
@@ -668,8 +671,8 @@ set_contains_key(PySetObject *so, PyObject *key)
668671 long hash ;
669672 setentry * entry ;
670673
671- if (!PyString_CheckExact (key ) ||
672- (hash = ((PyStringObject * ) key )-> ob_shash ) == -1 ) {
674+ if (!PyUnicode_CheckExact (key ) ||
675+ (hash = ((PyUnicodeObject * ) key )-> hash ) == -1 ) {
673676 hash = PyObject_Hash (key );
674677 if (hash == -1 )
675678 return -1 ;
@@ -989,7 +992,7 @@ make_new_set(PyTypeObject *type, PyObject *iterable)
989992 INIT_NONZERO_SET_SLOTS (so );
990993 }
991994
992- so -> lookup = set_lookkey_string ;
995+ so -> lookup = set_lookkey_unicode ;
993996 so -> weakreflist = NULL ;
994997
995998 if (iterable != NULL ) {
@@ -1352,7 +1355,7 @@ set_isdisjoint(PySetObject *so, PyObject *other)
13521355 while ((key = PyIter_Next (it )) != NULL ) {
13531356 int rv ;
13541357 setentry entry ;
1355- long hash = PyObject_Hash (key );
1358+ long hash = PyObject_Hash (key );;
13561359
13571360 if (hash == -1 ) {
13581361 Py_DECREF (key );
0 commit comments