Skip to content

Commit

Permalink
:Constant hash for _PyNone_Type
Browse files Browse the repository at this point in the history
  • Loading branch information
yonillasky committed Dec 10, 2022
1 parent e37744f commit 961b41f
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
2 changes: 2 additions & 0 deletions Include/object.h
Original file line number Diff line number Diff line change
Expand Up @@ -691,6 +691,8 @@ PyAPI_DATA(PyObject) _Py_NotImplementedStruct; /* Don't use this directly */
/* Macro for returning Py_NotImplemented from a function */
#define Py_RETURN_NOTIMPLEMENTED return Py_NewRef(Py_NotImplemented)

extern Py_ssize_t _PyHASH_NONE;

/* Rich comparison opcodes */
#define Py_LT 0
#define Py_LE 1
Expand Down
12 changes: 11 additions & 1 deletion Objects/object.c
Original file line number Diff line number Diff line change
Expand Up @@ -1638,6 +1638,16 @@ none_bool(PyObject *v)
return 0;
}

Py_ssize_t _PyHASH_NONE;

static Py_hash_t none_hash(PyObject *v)
{
#ifdef Py_DEBUG
assert(_Py_HashSecret_Initialized);
#endif
return _PyHASH_NONE;
}

static PyNumberMethods none_as_number = {
0, /* nb_add */
0, /* nb_subtract */
Expand Down Expand Up @@ -1689,7 +1699,7 @@ PyTypeObject _PyNone_Type = {
&none_as_number, /*tp_as_number*/
0, /*tp_as_sequence*/
0, /*tp_as_mapping*/
0, /*tp_hash */
(hashfunc)none_hash,/* tp_hash */
0, /*tp_call */
0, /*tp_str */
0, /*tp_getattro */
Expand Down
8 changes: 7 additions & 1 deletion Python/bootstrap_hash.c
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,11 @@ _PyOS_URandomNonblock(void *buffer, Py_ssize_t size)
return pyurandom(buffer, size, 0, 1);
}

void
_Py_InitNoneHash() {
const unsigned char data[] = {0xFC, 0xA8, 0x64, 0x20};
_PyHASH_NONE = _Py_HashBytes(data, sizeof(data));
}

PyStatus
_Py_HashRandomization_Init(const PyConfig *config)
Expand Down Expand Up @@ -576,14 +581,15 @@ _Py_HashRandomization_Init(const PyConfig *config)
pyurandom() is non-blocking mode (blocking=0): see the PEP 524. */
res = pyurandom(secret, secret_size, 0, 0);
if (res < 0) {
_Py_HashSecret_Initialized = 0;
return _PyStatus_ERR("failed to get random numbers "
"to initialize Python");
}
}
_Py_InitNoneHash();
return _PyStatus_OK();
}


void
_Py_HashRandomization_Fini(void)
{
Expand Down

0 comments on commit 961b41f

Please sign in to comment.