@@ -1904,7 +1904,8 @@ sys_get_int_max_str_digits_impl(PyObject *module)
19041904/*[clinic end generated code: output=0042f5e8ae0e8631 input=77fb74e987ba7ecb]*/
19051905{
19061906 PyInterpreterState * interp = _PyInterpreterState_GET ();
1907- return PyLong_FromLong (interp -> long_state .max_str_digits );
1907+ int maxdigits = _Py_atomic_load_int (& interp -> long_state .max_str_digits );
1908+ return PyLong_FromLong (maxdigits );
19081909}
19091910
19101911
@@ -3528,14 +3529,39 @@ sys_set_flag(PyObject *flags, Py_ssize_t pos, PyObject *value)
35283529int
35293530_PySys_SetFlagObj (Py_ssize_t pos , PyObject * value )
35303531{
3531- PyObject * flags = PySys_GetAttrString ("flags" );
3532- if (flags == NULL ) {
3533- return -1 ;
3532+ PyObject * new_flags = NULL ;
3533+ PyObject * flags_str = & _Py_ID (flags ); // immortal ref
3534+
3535+ PyObject * old_flags = PySys_GetAttr (flags_str );
3536+ if (old_flags == NULL ) {
3537+ goto error ;
35343538 }
35353539
3536- sys_set_flag (flags , pos , value );
3537- Py_DECREF (flags );
3538- return 0 ;
3540+ new_flags = PyStructSequence_New (& FlagsType );
3541+ if (new_flags == NULL ) {
3542+ goto error ;
3543+ }
3544+
3545+ for (Py_ssize_t i = 0 ; i < (Py_ssize_t )(Py_ARRAY_LENGTH (flags_fields ) - 1 ); i ++ ) {
3546+ if (i != pos ) {
3547+ PyObject * old_value ;
3548+ old_value = PyStructSequence_GET_ITEM (old_flags , i ); // borrowed ref
3549+ sys_set_flag (new_flags , i , old_value );
3550+ }
3551+ else {
3552+ sys_set_flag (new_flags , pos , value );
3553+ }
3554+ }
3555+
3556+ int res = _PySys_SetAttr (flags_str , new_flags );
3557+ Py_DECREF (old_flags );
3558+ Py_DECREF (new_flags );
3559+ return res ;
3560+
3561+ error :
3562+ Py_XDECREF (old_flags );
3563+ Py_XDECREF (new_flags );
3564+ return -1 ;
35393565}
35403566
35413567
@@ -3559,8 +3585,6 @@ set_flags_from_config(PyInterpreterState *interp, PyObject *flags)
35593585 const PyPreConfig * preconfig = & interp -> runtime -> preconfig ;
35603586 const PyConfig * config = _PyInterpreterState_GetConfig (interp );
35613587
3562- // _PySys_UpdateConfig() modifies sys.flags in-place:
3563- // Py_XDECREF() is needed in this case.
35643588 Py_ssize_t pos = 0 ;
35653589#define SetFlagObj (expr ) \
35663590 do { \
@@ -4071,7 +4095,7 @@ _PySys_InitCore(PyThreadState *tstate, PyObject *sysdict)
40714095 /* implementation */
40724096 SET_SYS ("implementation" , make_impl_info (version_info ));
40734097
4074- // sys.flags: updated in-place later by _PySys_UpdateConfig()
4098+ // sys.flags: updated later by _PySys_UpdateConfig()
40754099 ENSURE_INFO_TYPE (FlagsType , flags_desc );
40764100 SET_SYS ("flags" , make_flags (tstate -> interp ));
40774101
@@ -4191,16 +4215,21 @@ _PySys_UpdateConfig(PyThreadState *tstate)
41914215#undef COPY_LIST
41924216#undef COPY_WSTR
41934217
4194- // sys.flags
4195- PyObject * flags = PySys_GetAttrString ( "flags" );
4196- if (flags == NULL ) {
4218+ // replace sys.flags
4219+ PyObject * new_flags = PyStructSequence_New ( & FlagsType );
4220+ if (new_flags == NULL ) {
41974221 return -1 ;
41984222 }
4199- if (set_flags_from_config (interp , flags ) < 0 ) {
4200- Py_DECREF (flags );
4223+ if (set_flags_from_config (interp , new_flags ) < 0 ) {
4224+ Py_DECREF (new_flags );
4225+ return -1 ;
4226+ }
4227+
4228+ res = _PySys_SetAttr (& _Py_ID (flags ), new_flags );
4229+ Py_DECREF (new_flags );
4230+ if (res < 0 ) {
42014231 return -1 ;
42024232 }
4203- Py_DECREF (flags );
42044233
42054234 SET_SYS ("dont_write_bytecode" , PyBool_FromLong (!config -> write_bytecode ));
42064235
@@ -4713,7 +4742,7 @@ _PySys_SetIntMaxStrDigits(int maxdigits)
47134742 // Set PyInterpreterState.long_state.max_str_digits
47144743 // and PyInterpreterState.config.int_max_str_digits.
47154744 PyInterpreterState * interp = _PyInterpreterState_GET ();
4716- interp -> long_state .max_str_digits = maxdigits ;
4717- interp -> config .int_max_str_digits = maxdigits ;
4745+ _Py_atomic_store_int ( & interp -> long_state .max_str_digits , maxdigits ) ;
4746+ _Py_atomic_store_int ( & interp -> config .int_max_str_digits , maxdigits ) ;
47184747 return 0 ;
47194748}
0 commit comments