@@ -3675,27 +3675,45 @@ static PyObject *
36753675count_repr (PyObject * op )
36763676{
36773677 countobject * lz = countobject_CAST (op );
3678- if (lz -> long_cnt == NULL ) {
3678+ PyObject * long_cnt , * long_step , * result ;
3679+
3680+ Py_BEGIN_CRITICAL_SECTION (lz );
3681+ long_cnt = Py_XNewRef (lz -> long_cnt );
3682+ long_step = Py_XNewRef (lz -> long_step );
3683+ Py_END_CRITICAL_SECTION ();
3684+
3685+ if (long_cnt == NULL ) {
3686+ /* Fast mode: cnt is advanced by count_next()'s lock-free atomic CAS,
3687+ which never takes this critical section, so read it atomically. */
36793688 Py_ssize_t cnt = FT_ATOMIC_LOAD_SSIZE_RELAXED (lz -> cnt );
3680- return PyUnicode_FromFormat ("%s(%zd)" ,
3681- _PyType_Name (Py_TYPE (lz )), cnt );
3689+ result = PyUnicode_FromFormat ("%s(%zd)" ,
3690+ _PyType_Name (Py_TYPE (lz )), cnt );
36823691 }
3683-
3684- if ( PyLong_Check ( lz -> long_step )) {
3685- long step = PyLong_AsLong ( lz -> long_step );
3686- if ( step == -1 && PyErr_Occurred ()) {
3687- PyErr_Clear ();
3688- }
3689- if ( step == 1 ) {
3692+ else {
3693+ int hide_step = 0 ;
3694+ if ( PyLong_Check ( long_step )) {
3695+ long step = PyLong_AsLong ( long_step );
3696+ if ( step == -1 && PyErr_Occurred ()) {
3697+ PyErr_Clear ();
3698+ }
36903699 /* Don't display step when it is an integer equal to 1 */
3691- return PyUnicode_FromFormat ("%s(%R)" ,
3692- _PyType_Name (Py_TYPE (lz )),
3693- lz -> long_cnt );
3700+ hide_step = (step == 1 );
3701+ }
3702+ if (hide_step ) {
3703+ result = PyUnicode_FromFormat ("%s(%R)" ,
3704+ _PyType_Name (Py_TYPE (lz )),
3705+ long_cnt );
3706+ }
3707+ else {
3708+ result = PyUnicode_FromFormat ("%s(%R, %R)" ,
3709+ _PyType_Name (Py_TYPE (lz )),
3710+ long_cnt , long_step );
36943711 }
36953712 }
3696- return PyUnicode_FromFormat ("%s(%R, %R)" ,
3697- _PyType_Name (Py_TYPE (lz )),
3698- lz -> long_cnt , lz -> long_step );
3713+
3714+ Py_XDECREF (long_cnt );
3715+ Py_XDECREF (long_step );
3716+ return result ;
36993717}
37003718
37013719static PyType_Slot count_slots [] = {
0 commit comments