@@ -1899,7 +1899,8 @@ sys_get_int_max_str_digits_impl(PyObject *module)
18991899/*[clinic end generated code: output=0042f5e8ae0e8631 input=61bf9f99bc8b112d]*/
19001900{
19011901 PyInterpreterState * interp = _PyInterpreterState_GET ();
1902- return PyLong_FromLong (interp -> long_state .max_str_digits );
1902+ int maxdigits = _Py_atomic_load_int (& interp -> long_state .max_str_digits );
1903+ return PyLong_FromLong (maxdigits );
19031904}
19041905
19051906
@@ -3434,14 +3435,39 @@ sys_set_flag(PyObject *flags, Py_ssize_t pos, PyObject *value)
34343435int
34353436_PySys_SetFlagObj (Py_ssize_t pos , PyObject * value )
34363437{
3437- PyObject * flags = _PySys_GetRequiredAttrString ("flags" );
3438- if (flags == NULL ) {
3439- return -1 ;
3438+ PyObject * new_flags = NULL ;
3439+ PyObject * flags_str = & _Py_ID (flags ); // immortal ref
3440+
3441+ PyObject * old_flags = _PySys_GetRequiredAttr (flags_str );
3442+ if (old_flags == NULL ) {
3443+ goto error ;
34403444 }
34413445
3442- sys_set_flag (flags , pos , value );
3443- Py_DECREF (flags );
3444- return 0 ;
3446+ new_flags = PyStructSequence_New (& FlagsType );
3447+ if (new_flags == NULL ) {
3448+ goto error ;
3449+ }
3450+
3451+ for (Py_ssize_t i = 0 ; i < (Py_ssize_t )(Py_ARRAY_LENGTH (flags_fields ) - 1 ); i ++ ) {
3452+ if (i != pos ) {
3453+ PyObject * old_value ;
3454+ old_value = PyStructSequence_GET_ITEM (old_flags , i ); // borrowed ref
3455+ sys_set_flag (new_flags , i , old_value );
3456+ }
3457+ else {
3458+ sys_set_flag (new_flags , pos , value );
3459+ }
3460+ }
3461+
3462+ int res = _PySys_SetAttr (flags_str , new_flags );
3463+ Py_DECREF (old_flags );
3464+ Py_DECREF (new_flags );
3465+ return res ;
3466+
3467+ error :
3468+ Py_XDECREF (old_flags );
3469+ Py_XDECREF (new_flags );
3470+ return -1 ;
34453471}
34463472
34473473
@@ -3465,8 +3491,6 @@ set_flags_from_config(PyInterpreterState *interp, PyObject *flags)
34653491 const PyPreConfig * preconfig = & interp -> runtime -> preconfig ;
34663492 const PyConfig * config = _PyInterpreterState_GetConfig (interp );
34673493
3468- // _PySys_UpdateConfig() modifies sys.flags in-place:
3469- // Py_XDECREF() is needed in this case.
34703494 Py_ssize_t pos = 0 ;
34713495#define SetFlagObj (expr ) \
34723496 do { \
@@ -3889,7 +3913,7 @@ _PySys_InitCore(PyThreadState *tstate, PyObject *sysdict)
38893913 /* implementation */
38903914 SET_SYS ("implementation" , make_impl_info (version_info ));
38913915
3892- // sys.flags: updated in-place later by _PySys_UpdateConfig()
3916+ // sys.flags: updated later by _PySys_UpdateConfig()
38933917 ENSURE_INFO_TYPE (FlagsType , flags_desc );
38943918 SET_SYS ("flags" , make_flags (tstate -> interp ));
38953919
@@ -4007,16 +4031,21 @@ _PySys_UpdateConfig(PyThreadState *tstate)
40074031#undef COPY_LIST
40084032#undef COPY_WSTR
40094033
4010- // sys.flags
4011- PyObject * flags = _PySys_GetRequiredAttrString ( "flags" );
4012- if (flags == NULL ) {
4034+ // replace sys.flags
4035+ PyObject * new_flags = PyStructSequence_New ( & FlagsType );
4036+ if (new_flags == NULL ) {
40134037 return -1 ;
40144038 }
4015- if (set_flags_from_config (interp , flags ) < 0 ) {
4016- Py_DECREF (flags );
4039+ if (set_flags_from_config (interp , new_flags ) < 0 ) {
4040+ Py_DECREF (new_flags );
4041+ return -1 ;
4042+ }
4043+
4044+ res = _PySys_SetAttr (& _Py_ID (flags ), new_flags );
4045+ Py_DECREF (new_flags );
4046+ if (res < 0 ) {
40174047 return -1 ;
40184048 }
4019- Py_DECREF (flags );
40204049
40214050 SET_SYS ("dont_write_bytecode" , PyBool_FromLong (!config -> write_bytecode ));
40224051
@@ -4517,7 +4546,7 @@ _PySys_SetIntMaxStrDigits(int maxdigits)
45174546 // Set PyInterpreterState.long_state.max_str_digits
45184547 // and PyInterpreterState.config.int_max_str_digits.
45194548 PyInterpreterState * interp = _PyInterpreterState_GET ();
4520- interp -> long_state .max_str_digits = maxdigits ;
4521- interp -> config .int_max_str_digits = maxdigits ;
4549+ _Py_atomic_store_int ( & interp -> long_state .max_str_digits , maxdigits ) ;
4550+ _Py_atomic_store_int ( & interp -> config .int_max_str_digits , maxdigits ) ;
45224551 return 0 ;
45234552}
0 commit comments