Commit e5a30318 authored by da-woods's avatar da-woods Committed by Stefan Behnel

Avoid empty frozenset singleton on Python 3.10 (GH-4049)

See https://github.com/cython/cython/issues/3919
parent 62c1802a
...@@ -496,9 +496,9 @@ static CYTHON_INLINE PyObject* __Pyx_PyFrozenSet_New(PyObject* it) { ...@@ -496,9 +496,9 @@ static CYTHON_INLINE PyObject* __Pyx_PyFrozenSet_New(PyObject* it) {
result = PyFrozenSet_New(it); result = PyFrozenSet_New(it);
if (unlikely(!result)) if (unlikely(!result))
return NULL; return NULL;
if (likely(PySet_GET_SIZE(result))) if ((PY_VERSION_HEX >= 0x031000A1) || likely(PySet_GET_SIZE(result)))
return result; return result;
// empty frozenset is a singleton // empty frozenset is a singleton (on Python <3.10)
// seems wasteful, but CPython does the same // seems wasteful, but CPython does the same
Py_DECREF(result); Py_DECREF(result);
#endif #endif
......
...@@ -417,7 +417,8 @@ def test_empty_frozenset(): ...@@ -417,7 +417,8 @@ def test_empty_frozenset():
True True
>>> len(s) >>> len(s)
0 0
>>> s is frozenset() # singleton! >>> import sys
>>> sys.version_info >= (3, 10) or s is frozenset() # singleton (in Python < 3.10)!
True True
""" """
return frozenset() return frozenset()
...@@ -430,7 +431,8 @@ def test_empty_frozenset(): ...@@ -430,7 +431,8 @@ def test_empty_frozenset():
) )
def test_singleton_empty_frozenset(): def test_singleton_empty_frozenset():
""" """
>>> test_singleton_empty_frozenset() # from CPython's test_set.py >>> import sys
>>> test_singleton_empty_frozenset() if sys.version_info < (3, 10) else 1 # from CPython's test_set.py
1 1
""" """
f = frozenset() f = frozenset()
...@@ -438,7 +440,7 @@ def test_singleton_empty_frozenset(): ...@@ -438,7 +440,7 @@ def test_singleton_empty_frozenset():
frozenset(), frozenset([]), frozenset(()), frozenset(''), frozenset(), frozenset([]), frozenset(()), frozenset(''),
frozenset(range(0)), frozenset(frozenset()), frozenset(range(0)), frozenset(frozenset()),
frozenset(f), f] frozenset(f), f]
return len(set(map(id, efs))) return len(set(map(id, efs))) # note, only a singleton in Python <3.10
def sorted(it): def sorted(it):
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment