@@ -165,7 +165,6 @@ typedef struct {
165165 PyTypeObject * UuidType ;
166166
167167 PyObject * safe_uuid ;
168- PyObject * safe_uuid_unknown ;
169168
170169 PyObject * uint128_max ;
171170
@@ -968,12 +967,7 @@ make_uuid(PyTypeObject *type)
968967 }
969968 }
970969
971- // During module initialization, safe_uuid_unknown might not be set yet
972- if (state -> safe_uuid_unknown != NULL ) {
973- self -> is_safe = Py_NewRef (state -> safe_uuid_unknown );
974- } else {
975- self -> is_safe = Py_NewRef (Py_None );
976- }
970+ self -> is_safe = NULL ;
977971
978972 self -> weakreflist = NULL ;
979973 self -> cached_hash = -1 ;
@@ -1033,8 +1027,9 @@ static PyObject *
10331027Uuid_get_is_safe (PyObject * o , void * closure )
10341028{
10351029 uuidobject * self = (uuidobject * )o ;
1030+ uuid_state * state = get_uuid_state_by_cls (Py_TYPE (self ));
10361031 if (self -> is_safe == NULL ) {
1037- Py_RETURN_NONE ;
1032+ return PyObject_GetAttrString ( state -> safe_uuid , "unknown" ) ;
10381033 }
10391034 return Py_NewRef (self -> is_safe );
10401035}
@@ -1579,8 +1574,6 @@ static PyObject *
15791574_uuid_UUID___setstate___impl (uuidobject * self , PyObject * state )
15801575/*[clinic end generated code: output=cdf6bd4a2a680b3f input=b1ec0744788a73a0]*/
15811576{
1582- uuid_state * module_state = get_uuid_state_by_cls (Py_TYPE (self ));
1583-
15841577 if (!PyDict_Check (state )) {
15851578 PyErr_SetString (PyExc_TypeError , "state must be a dictionary" );
15861579 return NULL ;
@@ -1597,25 +1590,52 @@ _uuid_UUID___setstate___impl(uuidobject *self, PyObject *state)
15971590 return NULL ;
15981591 }
15991592
1600- // Get and set 'is_safe' if present
1601- PyObject * is_safe = PyDict_GetItem (state , & _Py_ID (is_safe ));
1602- if (is_safe != NULL ) {
1603- // is_safe is the integer value, we need to call SafeUUID(value)
1604- PyObject * safe_uuid_member = PyObject_CallOneArg (module_state -> safe_uuid , is_safe );
1605- if (safe_uuid_member == NULL ) {
1606- return NULL ;
1607- }
1608- Py_XDECREF (self -> is_safe );
1609- self -> is_safe = safe_uuid_member ;
1610- } else {
1611- // No is_safe in state, set to SafeUUID.unknown
1612- Py_XDECREF (self -> is_safe );
1613- self -> is_safe = Py_NewRef (module_state -> safe_uuid_unknown );
1593+ Py_CLEAR (self -> is_safe );
1594+ if (PyDict_GetItemRef (state , & _Py_ID (is_safe ), & self -> is_safe ) < 0 ) {
1595+ return NULL ;
16141596 }
16151597
16161598 Py_RETURN_NONE ;
16171599}
16181600
1601+ static PyObject *
1602+ compute_uuid_max (void )
1603+ {
1604+ // Compute `(1 << 128) - 1`
1605+
1606+ PyObject * one = NULL ;
1607+ PyObject * shift = NULL ;
1608+ PyObject * shifted = NULL ;
1609+
1610+ one = PyLong_FromLong (1 );
1611+ if (one == NULL ) {
1612+ goto err ;
1613+ }
1614+
1615+ shift = PyLong_FromLong (128 );
1616+ if (shift == NULL ) {
1617+ goto err ;
1618+ }
1619+
1620+ shifted = PyNumber_Lshift (one , shift );
1621+ if (shifted == NULL ) {
1622+ goto err ;
1623+ }
1624+ Py_DECREF (shift );
1625+
1626+ PyObject * result = PyNumber_Subtract (shifted , one );
1627+ Py_DECREF (shifted );
1628+ Py_DECREF (one );
1629+
1630+ return result ;
1631+
1632+ err :
1633+ Py_XDECREF (one );
1634+ Py_XDECREF (shift );
1635+ Py_XDECREF (shifted );
1636+ return NULL ;
1637+ }
1638+
16191639static PyMethodDef Uuid_methods [] = {
16201640 _UUID_UUID__FROM_INT_METHODDEF
16211641 _UUID_UUID___GETSTATE___METHODDEF
@@ -1672,7 +1692,6 @@ module_traverse(PyObject *mod, visitproc visit, void *arg)
16721692 uuid_state * state = get_uuid_state (mod );
16731693 Py_VISIT (state -> UuidType );
16741694 Py_VISIT (state -> safe_uuid );
1675- Py_VISIT (state -> safe_uuid_unknown );
16761695 Py_VISIT (state -> uint128_max );
16771696 Py_VISIT (state -> reserved_ncs );
16781697 Py_VISIT (state -> rfc_4122 );
@@ -1691,7 +1710,6 @@ module_clear(PyObject *mod)
16911710
16921711 Py_CLEAR (state -> UuidType );
16931712 Py_CLEAR (state -> safe_uuid );
1694- Py_CLEAR (state -> safe_uuid_unknown );
16951713 Py_CLEAR (state -> uint128_max );
16961714 Py_CLEAR (state -> reserved_ncs );
16971715 Py_CLEAR (state -> rfc_4122 );
@@ -1775,12 +1793,8 @@ uuid_exec(PyObject *module)
17751793 if (safe_uuid == NULL ) {
17761794 goto fail ;
17771795 }
1778- state -> safe_uuid_unknown = PyObject_GetAttrString (safe_uuid , "unknown" );
1779- if (state -> safe_uuid_unknown == NULL ) {
1780- goto fail ;
1781- }
17821796
1783- state -> uint128_max = PyObject_GetAttrString ( uuid_mod , "_UINT_128_MAX" );
1797+ state -> uint128_max = compute_uuid_max ( );
17841798 if (state -> uint128_max == NULL ) {
17851799 goto fail ;
17861800 }
0 commit comments