@@ -1524,18 +1524,21 @@ encoder_listencode_obj(PyEncoderObject *s, PyUnicodeWriter *writer,
15241524 else {
15251525 PyObject * ident = NULL ;
15261526 if (s -> markers != Py_None ) {
1527- int has_key ;
1527+ Py_ssize_t len ;
1528+
15281529 ident = PyLong_FromVoidPtr (obj );
15291530 if (ident == NULL )
15301531 return -1 ;
1531- has_key = PyDict_Contains ( s -> markers , ident );
1532- if ( has_key ) {
1533- if ( has_key != -1 )
1534- PyErr_SetString ( PyExc_ValueError , "Circular reference detected" );
1532+
1533+ len = PyDict_GET_SIZE ( s -> markers );
1534+
1535+ if ( PyDict_SetItem ( s -> markers , ident , obj )) {
15351536 Py_DECREF (ident );
15361537 return -1 ;
15371538 }
1538- if (PyDict_SetItem (s -> markers , ident , obj )) {
1539+
1540+ if (PyDict_GET_SIZE (s -> markers ) == len ) {
1541+ PyErr_SetString (PyExc_ValueError , "Circular reference detected" );
15391542 Py_DECREF (ident );
15401543 return -1 ;
15411544 }
@@ -1660,17 +1663,20 @@ encoder_listencode_dict(PyEncoderObject *s, PyUnicodeWriter *writer,
16601663 }
16611664
16621665 if (s -> markers != Py_None ) {
1663- int has_key ;
1666+ Py_ssize_t len ;
1667+
16641668 ident = PyLong_FromVoidPtr (dct );
16651669 if (ident == NULL )
1666- goto bail ;
1667- has_key = PyDict_Contains ( s -> markers , ident );
1668- if ( has_key ) {
1669- if ( has_key != -1 )
1670- PyErr_SetString ( PyExc_ValueError , "Circular reference detected" );
1670+ return -1 ;
1671+
1672+ len = PyDict_GET_SIZE ( s -> markers );
1673+
1674+ if ( PyDict_SetItem ( s -> markers , ident , dct )) {
16711675 goto bail ;
16721676 }
1673- if (PyDict_SetItem (s -> markers , ident , dct )) {
1677+
1678+ if (PyDict_GET_SIZE (s -> markers ) == len ) {
1679+ PyErr_SetString (PyExc_ValueError , "Circular reference detected" );
16741680 goto bail ;
16751681 }
16761682 }
@@ -1761,17 +1767,20 @@ encoder_listencode_list(PyEncoderObject *s, PyUnicodeWriter *writer,
17611767 }
17621768
17631769 if (s -> markers != Py_None ) {
1764- int has_key ;
1770+ Py_ssize_t len ;
1771+
17651772 ident = PyLong_FromVoidPtr (seq );
17661773 if (ident == NULL )
1767- goto bail ;
1768- has_key = PyDict_Contains ( s -> markers , ident );
1769- if ( has_key ) {
1770- if ( has_key != -1 )
1771- PyErr_SetString ( PyExc_ValueError , "Circular reference detected" );
1774+ return -1 ;
1775+
1776+ len = PyDict_GET_SIZE ( s -> markers );
1777+
1778+ if ( PyDict_SetItem ( s -> markers , ident , seq )) {
17721779 goto bail ;
17731780 }
1774- if (PyDict_SetItem (s -> markers , ident , seq )) {
1781+
1782+ if (PyDict_GET_SIZE (s -> markers ) == len ) {
1783+ PyErr_SetString (PyExc_ValueError , "Circular reference detected" );
17751784 goto bail ;
17761785 }
17771786 }
0 commit comments