{"id":445570,"date":"2025-01-21T15:11:33","date_gmt":"2025-01-21T15:11:33","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=445570"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=445570","title":{"rendered":"<span>tuple \u0432 CPython \u2013 \u043c\u0443\u0442\u0430\u0431\u0435\u043b\u044c\u043d\u044b\u0439! \u0418 \u0434\u0440\u0443\u0433\u0438\u0435 \u0448\u043e\u043a\u0438\u0440\u0443\u044e\u0449\u0438\u0435 \u0434\u0435\u0442\u0430\u043b\u0438 \u0438\u0437 C-\u0448\u043d\u043e\u0439 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438<\/span>"},"content":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w780q1\/getpro\/habr\/upload_files\/99e\/56f\/809\/99e56f8098a36638fa8b31bed0468992.jpg\" width=\"1920\" height=\"1080\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/99e\/56f\/809\/99e56f8098a36638fa8b31bed0468992.jpg\" data-blurred=\"true\"\/><\/figure>\n<p>\u041f\u0440\u0438\u0432\u0435\u0442! \u041c\u0435\u043d\u044f \u0437\u043e\u0432\u0443\u0442 \u041d\u0438\u043a\u0438\u0442\u0430 \u0421\u043e\u0431\u043e\u043b\u0435\u0432, \u044f core-\u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a \u044f\u0437\u044b\u043a\u0430 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f CPython, \u0430 \u0442\u0430\u043a \u0436\u0435 \u0430\u0432\u0442\u043e\u0440 \u0441\u0435\u0440\u0438\u0438 \u0432\u0438\u0434\u0435\u043e \u043f\u0440\u043e \u0435\u0433\u043e \u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432\u043e.<\/p>\n<p>\u042f \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0430\u044e \u0441\u0432\u043e\u0439 \u0446\u0438\u043a\u043b \u0441\u0442\u0430\u0442\u0435\u0439 \u043d\u0430 \u0445\u0430\u0431\u0440\u0435 \u043f\u0440\u043e \u0434\u0435\u0442\u0430\u043b\u0438 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u043f\u0438\u0442\u043e\u043d\u0430. \u0421\u0435\u0433\u043e\u0434\u043d\u044f \u044f \u0445\u043e\u0447\u0443 \u0440\u0430\u0441\u0441\u043a\u0430\u0437\u0430\u0442\u044c, \u043a\u0430\u043a <code>tuple<\/code> \u0443\u0441\u0442\u0440\u043e\u0435\u043d \u0432\u043d\u0443\u0442\u0440\u0438.<\/p>\n<p>\u041f\u043e\u0434 \u043a\u0430\u0442\u043e\u043c \u0431\u0443\u0434\u0435\u0442 \u043f\u0440\u043e: \u043c\u0443\u0442\u0430\u0431\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u043a\u043e\u0440\u0442\u0435\u0436\u0435\u0439 (\u0438 \u043f\u043b\u0430\u043d\u044b \u043f\u043e \u043e\u0442\u043c\u0435\u043d\u0435 \u0442\u0430\u043a\u043e\u0433\u043e \u043f\u043e\u0432\u0435\u0434\u0435\u043d\u0438\u044f), \u0434\u0438\u0447\u044c \u0441 <code>ctypes<\/code>, \u043f\u0435\u0440\u0435\u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u043f\u0430\u043c\u044f\u0442\u0438 \u0438 \u0434\u0440\u0443\u0433\u0438\u0435 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0438.<\/p>\n<p>\u041d\u0443 \u0438 \u043a\u043e\u043d\u0435\u0447\u043d\u043e \u2013 \u043c\u043e\u0435 \u043f\u043e\u043b\u043d\u043e\u0435 \u0432\u0438\u0434\u0435\u043e \u043d\u0430 \u043f\u043e\u043b\u0442\u043e\u0440\u0430 \u0447\u0430\u0441\u0430, \u0433\u0434\u0435 \u0431\u0443\u0434\u0435\u0442 \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0439 \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u0438 \u043e \u0434\u0435\u0442\u0430\u043b\u044f\u0445 \u0440\u0430\u0431\u043e\u0442\u044b VM.<\/p>\n<p>\u0415\u0441\u043b\u0438 \u0432\u0430\u043c \u0442\u0430\u043a\u043e\u0435 \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u043e \u0438\u043b\u0438 \u0446\u0435\u043b\u0438\u043a\u043e\u043c \u043d\u0435\u0437\u043d\u0430\u043a\u043e\u043c\u043e \u2013 \u0434\u043e\u0431\u0440\u043e \u043f\u043e\u0436\u0430\u043b\u043e\u0432\u0430\u0442\u044c!<\/p>\n<hr\/>\n<p>\u0414\u043b\u044f \u043d\u0430\u0447\u0430\u043b\u0430 \u2013 \u0432\u043e\u0442 \u0432\u0430\u043c \u0432\u0438\u0434\u0435\u043e; \u0432 \u043d\u0435\u043c \u044f \u043f\u043e\u0441\u0442\u0430\u0440\u0430\u043b\u0441\u044f \u0440\u0430\u0441\u0441\u043a\u0430\u0437\u0430\u0442\u044c \u0432\u043e\u043e\u0431\u0449\u0435 \u0432\u0441\u0435, \u0447\u0442\u043e \u044f \u0437\u043d\u0430\u044e \u043f\u0440\u043e <code>tuple<\/code> \u0432 <code>CPython<\/code>. \u041e\u0442 \u0430\u043d\u043d\u043e\u0442\u0430\u0446\u0438\u0439 (<code>tuple[int, ...]<\/code>, <code>tuple[()]<\/code>, <code>*tuple[int, int]<\/code>) \u0438 <code>TypeVarTuple<\/code> \u0434\u043e \u0441\u0438\u0448\u043d\u044b\u0445 \u0434\u0435\u0442\u0430\u043b\u0435\u0439: <code>_PyFreeList_<\/code> C-API \u0438 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0439 AST, VM \u0438 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440\u0430.<\/p>\n<p>\u0412\u0438\u0434\u0435\u043e \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u043e\u0441\u044c \u043e\u0447\u0435\u043d\u044c \u043d\u0430\u0441\u044b\u0449\u0435\u043d\u043d\u044b\u043c, \u0442\u0430\u043a \u0447\u0442\u043e \u0440\u0435\u043a\u043e\u043c\u043c\u0435\u043d\u0434\u0443\u044e \u043a \u043f\u0440\u043e\u0441\u043c\u043e\u0442\u0440\u0443.<\/p>\n<div class=\"tm-iframe_temp\" data-src=\"https:\/\/embedd.srv.habr.com\/iframe\/678b881cbfebf17680867fbe\" data-style=\"\" id=\"678b881cbfebf17680867fbe\" width=\"\"><\/div>\n<h2>\u0423\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432\u043e tuple<\/h2>\n<p>\u041d\u0430\u0447\u043d\u0435\u043c \u0441\u043e \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b \u0434\u0430\u043d\u043d\u044b\u0445 <code>PyTupleObject<\/code>, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u0441\u043e\u0431\u043e\u0439 \u0432\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u0435\u0435 \u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432\u043e <code>tuple<\/code> \u0432 CPython. \u0421\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u043f\u0440\u043e\u0441\u0442\u0430\u044f:<\/p>\n<pre><code class=\"cpp\">typedef struct {     PyObject_VAR_HEAD     \/* ob_item contains space for 'ob_size' elements.        Items must normally not be NULL, except during construction when        the tuple is not yet visible outside the function that builds it. *\/     PyObject *ob_item[1]; } PyTupleObject;<\/code><\/pre>\n<p>\u041f\u043e \u0441\u0443\u0442\u0438 \u0442\u0443\u0442 \u0443 \u043d\u0430\u0441 \u043f\u0440\u043e\u0441\u0442\u043e \u043c\u0430\u0441\u0441\u0438\u0432 \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0445\u0440\u0430\u043d\u0438\u0442 <code>tuple<\/code> \u0432 <code>ob_item<\/code>. \u041d\u043e \u0433\u0434\u0435 \u0436\u0435 \u0441\u0430\u043c \u0440\u0430\u0437\u043c\u0435\u0440 \u043a\u043e\u0440\u0442\u0435\u0436\u0430? \u0410 \u0432\u043e\u0442 \u043e\u043d \u0445\u0440\u0430\u043d\u0438\u0442\u0441\u044f \u0432 <code>PyObject_VAR_HEAD<\/code>. \u0412 CPython \u0435\u0441\u0442\u044c \u0434\u0432\u0430 \u0442\u0438\u043f\u0430 C-\u0448\u043d\u044b\u0445 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440: \u0441 \u043f\u043e\u0441\u0442\u043e\u044f\u043d\u043d\u044b\u043c \u0440\u0430\u0437\u043c\u0435\u0440\u043e\u043c (\u043a\u0430\u043a <code>int<\/code> \u0441 <code>python3.11+<\/code>) \u0438 \u0441 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u043c (variable) \u0440\u0430\u0437\u043c\u0435\u0440\u043e\u043c. <a href=\"https:\/\/docs.python.org\/3\/c-api\/structures.html#c.PyObject_HEAD\" rel=\"noopener noreferrer nofollow\">PyObject_HEAD<\/a> \u0438 <a href=\"https:\/\/docs.python.org\/3\/c-api\/structures.html#c.PyObject_VAR_HEAD\" rel=\"noopener noreferrer nofollow\">PyObject_VAR_HEAD<\/a> \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u043e. \u0421\u0440\u0430\u0432\u043d\u0438\u043c \u0438\u0445!<\/p>\n<pre><code class=\"cpp\">#define PyObject_HEAD PyObject ob_base; #define PyObject_VAR_HEAD PyVarObject ob_base;<\/code><\/pre>\n<p>\u0413\u0434\u0435 <code>PyObject ob_base<\/code> \u0431\u0443\u0434\u0435\u0442 \u0441\u0440\u0430\u0437\u0443 \u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0442\u0438\u043f \u043e\u0431\u044a\u0435\u043a\u0442\u0430, \u0430 <code>PyVarObject ob_base<\/code> \u0431\u0443\u0434\u0435\u0442 \u0434\u0435\u043b\u0430\u0442\u044c \u0442\u0430\u043a:<\/p>\n<pre><code class=\"cpp\">typedef struct {     PyObject ob_base;     Py_ssize_t ob_size; \/* Number of items in variable part *\/ } PyVarObject;<\/code><\/pre>\n<p>\u0420\u044f\u0434\u043e\u043c \u0435\u0449\u0435 \u0431\u0443\u0434\u0435\u0442 \u043b\u0435\u0436\u0430\u0442\u044c \u0438 \u0434\u043b\u0438\u043d\u0430 \u043e\u0431\u044a\u0435\u043a\u0442\u0430. \u0421\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e, \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0434\u043b\u0438\u043d\u0443 \u043c\u044b \u0432\u0441\u0435\u0433\u0434\u0430 \u043c\u043e\u0436\u0435\u043c \u0437\u0430 <code>O(1)<\/code>, \u0432\u043e\u0442 \u043a\u0430\u043a \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 <code>__len__<\/code> \u043a\u043e\u0440\u0442\u0435\u0436\u0430:<\/p>\n<pre><code class=\"cpp\">static Py_ssize_t tuple_length(PyObject *self) {     PyTupleObject *a = _PyTuple_CAST(self);     return Py_SIZE(a); }<\/code><\/pre>\n<p>\u0413\u0434\u0435 <a href=\"https:\/\/docs.python.org\/3\/c-api\/structures.html#c.Py_SIZE\" rel=\"noopener noreferrer nofollow\">Py_SIZE<\/a> \u2013 \u043f\u0440\u043e\u0441\u0442\u043e \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0438\u0437 <code>ob_size<\/code><\/p>\n<pre><code class=\"cpp\">static inline Py_ssize_t Py_SIZE(PyObject *ob) {     return _PyVarObject_CAST(ob)-&gt;ob_size; }<\/code><\/pre>\n<h2>\u0410\u043b\u043b\u043e\u043a\u0430\u0446\u0438\u044f \u0438 \u0434\u0435\u0430\u043b\u043b\u043e\u043a\u0430\u0446\u0438\u044f<\/h2>\n<p>\u0412\u0441\u0435 \u043c\u044b \u0437\u043d\u0430\u0435\u043c, \u0447\u0442\u043e <code>tuple<\/code> \u2013 \u043e\u0431\u044a\u0435\u043a\u0442 \u0438\u043c\u043c\u0443\u0442\u0430\u0431\u0435\u043b\u044c\u043d\u044b\u0439 \ud83c\udf1a\ufe0f\ufe0f. \u0410 \u0437\u043d\u0430\u0447\u0438\u0442 \u2013 \u043f\u0440\u0438 \u043b\u044e\u0431\u043e\u043c \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0438 \u043f\u0440\u0438\u0434\u0435\u0442\u0441\u044f \u043f\u0435\u0440\u0435\u0441\u043e\u0437\u0434\u0430\u0432\u0430\u0442\u044c \u043d\u043e\u0432\u044b\u0439 <code>tuple<\/code>. \u041a\u0430\u0436\u0435\u0442\u0441\u044f, \u0447\u0442\u043e \u0442\u0443\u0442 \u043c\u043e\u0436\u043d\u043e \u0431\u044b\u043b\u043e \u0431\u044b \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043f\u0440\u043e\u0446\u0435\u0441\u0441 \u043f\u0435\u0440\u0435\u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f. \u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u0440\u0430\u0437\u0431\u0438\u0440\u0430\u0442\u044c\u0441\u044f, \u043a\u0430\u043a \u043d\u0430 \u0441\u0430\u043c\u043e\u043c \u0434\u0435\u043b\u0435 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u0438 \u0443\u043d\u0438\u0447\u0442\u043e\u0436\u0435\u043d\u0438\u0435 \u043a\u043e\u0440\u0442\u0435\u0436\u0435\u0439. \u0410 \u043f\u0440\u043e \u0438\u043c\u043c\u0443\u0442\u0430\u0431\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u2013 \u0440\u0430\u0437\u0431\u0435\u0440\u0435\u043c \u0432 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0439 \u0433\u043b\u0430\u0432\u0435.<\/p>\n<p>\u041d\u0430\u0447\u043d\u0435\u043c \u0441 <a href=\"https:\/\/docs.python.org\/3\/c-api\/typeobj.html#c.PyTypeObject.tp_alloc\" rel=\"noopener noreferrer nofollow\">tp_alloc<\/a>: \u0432\u044b\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u043f\u0430\u043c\u044f\u0442\u0438 \u043f\u043e\u0434 \u043a\u043e\u0440\u0442\u0435\u0436 \u0438 \u0435\u0433\u043e \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435. \u0414\u043b\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u043a\u043e\u0440\u0442\u0435\u0436\u0430 \u043d\u0430\u043c \u043f\u043e\u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u0442\u043e\u043b\u044c\u043a\u043e \u0443\u043a\u0430\u0437\u0430\u043d\u0438\u0435 \u0434\u043b\u0438\u043d\u044b:<\/p>\n<pre><code class=\"cpp\">static PyTupleObject * tuple_alloc(Py_ssize_t size) {     if (size &lt; 0) {         PyErr_BadInternalCall();         return NULL;     }     assert(size != 0);    \/\/ The empty tuple is statically allocated.     Py_ssize_t index = size - 1;     if (index &lt; PyTuple_MAXSAVESIZE) {  \/\/ 20         PyTupleObject *op = _Py_FREELIST_POP(PyTupleObject, tuples[index]);         if (op != NULL) {             return op;         }     }     \/* Check for overflow *\/     if ((size_t)size &gt; ((size_t)PY_SSIZE_T_MAX - (sizeof(PyTupleObject) -                 sizeof(PyObject *))) \/ sizeof(PyObject *)) {         return (PyTupleObject *)PyErr_NoMemory();     }     return PyObject_GC_NewVar(PyTupleObject, &amp;PyTuple_Type, size); }<\/code><\/pre>\n<p>\u0417\u0434\u0435\u0441\u044c \u043c\u043d\u043e\u0433\u043e \u0432\u0441\u0435\u0433\u043e \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u043e\u0433\u043e:<\/p>\n<ol>\n<li>\n<p>\u0421\u043e\u0437\u0434\u0430\u0442\u044c \u043a\u043e\u0440\u0442\u0435\u0436 \u0441 \u0434\u043b\u0438\u043d\u043e\u0439 <code>&lt;0<\/code> \u2013 \u043e\u0447\u0435\u0432\u0438\u0434\u043d\u043e \u043d\u0435\u043b\u044c\u0437\u044f<\/p>\n<\/li>\n<li>\n<p>\u0421\u043e\u0437\u0434\u0430\u0432\u0430\u0442\u044c \u043a\u043e\u0440\u0442\u0435\u0436 \u0441 \u0434\u043b\u0438\u043d\u043e\u0439 <code>0<\/code>  \u2013 \u0442\u043e\u0436\u0435 \u043d\u0435\u043b\u044c\u0437\u044f, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e <code>()<\/code> \u2013 \u0441\u043f\u0435\u0446\u0438\u0430\u043b\u044c\u043d\u044b\u0439 <a href=\"https:\/\/peps.python.org\/pep-0683\" rel=\"noopener noreferrer nofollow\">immortal<\/a> \u043e\u0431\u044a\u0435\u043a\u0442, \u0435\u0433\u043e \u0441\u043e\u0437\u0434\u0430\u044e\u0442 \u0432 <a href=\"https:\/\/github.com\/python\/cpython\/blob\/f4afaa6f1190fbbea3e27c590096951d8ffdfb71\/Include\/internal\/pycore_global_objects.h#L55\" rel=\"noopener noreferrer nofollow\">\u0440\u0430\u043c\u043a\u0430\u0445 \u0441\u0442\u0430\u0440\u0442\u0430<\/a> \u0438\u043d\u0442\u0435\u0440\u043f\u0440\u0435\u0442\u0430\u0442\u043e\u0440\u0430 <a href=\"https:\/\/github.com\/python\/cpython\/blob\/f4afaa6f1190fbbea3e27c590096951d8ffdfb71\/Include\/internal\/pycore_runtime_init.h#L103-L105\" rel=\"noopener noreferrer nofollow\">\u043e\u0434\u0438\u043d \u0440\u0430\u0437<\/a> \u0432 <code>pycore_runtime_init<\/code> \u0432\u043e\u0442 \u0442\u0430\u043a: <code>.tuple_empty = { .ob_base = _PyVarObject_HEAD_INIT(&amp;PyTuple_Type, 0), }<\/code> <\/p>\n<\/li>\n<li>\n<p>\u0415\u0441\u043b\u0438 \u0440\u0430\u0437\u043c\u0435\u0440 \u043a\u043e\u0440\u0442\u0435\u0436\u0430 \u043d\u0435 \u043e\u0447\u0435\u043d\u044c \u0431\u043e\u043b\u044c\u0448\u043e\u0439 (<code>&lt; 20<\/code>), \u0442\u043e \u0435\u0449\u0435 \u043f\u0440\u043e\u0431\u0443\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u0443\u044e \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u044e \u0441 <code>_Py_FREELIST_POP<\/code>, \u043e \u043d\u0435\u0439 \u0431\u0443\u0434\u0435\u0442 \u0447\u0443\u0442\u044c \u043f\u043e\u0437\u0436\u0435<\/p>\n<\/li>\n<li>\n<p>\u0414\u0430\u043b\u0435\u0435 \u0441\u043c\u043e\u0442\u0440\u0438\u043c, \u0447\u0442\u043e \u0435\u0441\u043b\u0438 \u0440\u0430\u0437\u043c\u0435\u0440 \u043a\u043e\u0440\u0442\u0435\u0436\u0430 \u043e\u0447\u0435\u043d\u044c \u0431\u043e\u043b\u044c\u0448\u043e\u0439 \u2013 \u0441\u0440\u0430\u0437\u0443 \u043a\u0438\u0434\u0430\u0435\u043c \u043e\u0448\u0438\u0431\u043a\u0443 \u043f\u0430\u043c\u044f\u0442\u0438, \u0447\u0442\u043e\u0431 \u043d\u0435 \u0430\u043b\u043b\u043e\u0446\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0441\u043b\u0438\u0448\u043a\u043e\u043c \u043c\u043d\u043e\u0433\u043e<\/p>\n<\/li>\n<li>\n<p>\u0412 \u0441\u0430\u043c\u043e\u043c \u043a\u043e\u043d\u0446\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c <a href=\"https:\/\/docs.python.org\/3\/c-api\/gcsupport.html#c.PyObject_GC_NewVar\" rel=\"noopener noreferrer nofollow\">PyObject_GC_NewVar<\/a> \u0434\u043b\u044f \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 <code>PyTupleObject<\/code>, \u0432\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u0435\u0433\u043e <code>PyVarObject<\/code> \u0438 \u0444\u0438\u0437\u0438\u0447\u0435\u0441\u043a\u043e\u0433\u043e \u0432\u044b\u0434\u0435\u043b\u0435\u043d\u0438\u044f \u043f\u0430\u043c\u044f\u0442\u0438<\/p>\n<\/li>\n<\/ol>\n<p>\u0412\u043e\u0442 \u043a\u0430\u043a \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 <code>PyObject_GC_NewVar<\/code> (\u043a\u0441\u0442\u0430\u0442\u0438, \u043f\u0440\u044f\u043c\u043e \u0441\u0435\u0439\u0447\u0430\u0441 \u0438\u0445 \u0434\u0432\u0430: <a href=\"https:\/\/github.com\/python\/cpython\/blob\/f4afaa6f1190fbbea3e27c590096951d8ffdfb71\/Python\/gc.c#L2286-L2303\" rel=\"noopener noreferrer nofollow\">\u0434\u043b\u044f \u043e\u0431\u044b\u0447\u043d\u043e\u0433\u043e \u0431\u0438\u043b\u0434\u0430<\/a> \u0438 <a href=\"https:\/\/github.com\/python\/cpython\/blob\/f4afaa6f1190fbbea3e27c590096951d8ffdfb71\/Python\/gc_free_threading.c#L2160-L2177\" rel=\"noopener noreferrer nofollow\">\u0434\u043b\u044f nogil<\/a>, \u043d\u043e \u043e\u0442\u043b\u0438\u0447\u0438\u0439 \u0432\u0440\u043e\u0434\u0435 \u0431\u044b \u043d\u0435\u0442):<\/p>\n<pre><code class=\"cpp\">PyVarObject * _PyObject_GC_NewVar(PyTypeObject *tp, Py_ssize_t nitems) {     PyVarObject *op;      if (nitems &lt; 0) {         PyErr_BadInternalCall();         return NULL;     }     size_t presize = _PyType_PreHeaderSize(tp);     size_t size = _PyObject_VAR_SIZE(tp, nitems);     op = (PyVarObject *)gc_alloc(tp, size, presize);     if (op == NULL) {         return NULL;     }     _PyObject_InitVar(op, tp, nitems);     return op; }<\/code><\/pre>\n<p>\u0422\u0443\u0442 \u0435\u0441\u0442\u044c \u0434\u0432\u0430 \u0432\u0430\u0436\u043d\u044b\u0445 \u043c\u043e\u043c\u0435\u043d\u0442\u0430: <code>gc_alloc<\/code> \u0434\u043b\u044f \u0432\u044b\u0434\u0435\u043b\u0435\u043d\u0438\u044f \u043f\u0430\u043c\u044f\u0442\u0438, \u043f\u0440\u043e \u043a\u043e\u0442\u043e\u0440\u0443\u044e \u0437\u043d\u0430\u0435\u0442 GC. \u0418 <code>_PyObject_InitVar<\/code>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0443\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0435\u0442 <code>ob_size<\/code> \u0438 <code>ob_base<\/code> \u0447\u0435\u0440\u0435\u0437 <a href=\"https:\/\/docs.python.org\/3\/c-api\/structures.html#c.Py_SET_SIZE\" rel=\"noopener noreferrer nofollow\">Py_SET_SIZE<\/a> \u0438 <a href=\"https:\/\/docs.python.org\/3\/c-api\/structures.html#c.Py_SET_TYPE\" rel=\"noopener noreferrer nofollow\">Py_SET_TYPE<\/a>.<\/p>\n<p>\u041a\u0430\u043a GC \u0432\u044b\u0434\u0435\u043b\u044f\u0435\u0442 \u043f\u0430\u043c\u044f\u0442\u044c? \u0413\u043b\u0443\u0431\u043e\u043a\u0430 \u0436\u0435 \u043a\u0440\u043e\u043b\u0438\u0447\u044c\u044f \u043d\u043e\u0440\u0430!<\/p>\n<pre><code class=\"cpp\">static PyObject * gc_alloc(PyTypeObject *tp, size_t basicsize, size_t presize) {     PyThreadState *tstate = _PyThreadState_GET();     if (basicsize &gt; PY_SSIZE_T_MAX - presize) {         return _PyErr_NoMemory(tstate);     }     size_t size = presize + basicsize;     \/\/ \u0421\u043c\u043e\u0442\u0440\u0438 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435 `_PyObject_MallocWithType` \u043d\u0438\u0436\u0435:     char *mem = _PyObject_MallocWithType(tp, size);     if (mem == NULL) {         return _PyErr_NoMemory(tstate);     }     ((PyObject **)mem)[0] = NULL;     ((PyObject **)mem)[1] = NULL;     PyObject *op = (PyObject *)(mem + presize);     _PyObject_GC_Link(op);     return op; }  \/\/ Sets the heap used for PyObject_Malloc(), PyObject_Realloc(), etc. calls in \/\/ Py_GIL_DISABLED builds. We use different heaps depending on if the object \/\/ supports GC and if it has a pre-header. We smuggle the choice of heap \/\/ through the _mimalloc_thread_state. In the default build, this simply \/\/ calls PyObject_Malloc(). static inline void * _PyObject_MallocWithType(PyTypeObject *tp, size_t size) { #ifdef Py_GIL_DISABLED     _PyThreadStateImpl *tstate = (_PyThreadStateImpl *)_PyThreadState_GET();     struct _mimalloc_thread_state *m = &amp;tstate-&gt;mimalloc;     m-&gt;current_object_heap = _PyObject_GetAllocationHeap(tstate, tp); #endif     void *mem = PyObject_Malloc(size); #ifdef Py_GIL_DISABLED     m-&gt;current_object_heap = &amp;m-&gt;heaps[_Py_MIMALLOC_HEAP_OBJECT]; #endif     return mem; }<\/code><\/pre>\n<p>\u041f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 \u043c\u043d\u043e\u0433\u043e \u0432\u0441\u0435\u0433\u043e. \u041e\u0431\u0440\u0430\u0449\u0443 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435 \u043d\u0430 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043a\u043b\u044e\u0447\u0435\u0432\u044b\u0445 \u043c\u043e\u043c\u0435\u043d\u0442\u043e\u0432:<\/p>\n<ul>\n<li>\n<p>\u0412\u044b\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u043f\u0430\u043c\u044f\u0442\u0438 \u0432\u0441\u0435\u0433\u0434\u0430 \u043c\u043e\u0436\u0435\u0442 \u0437\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u044c\u0441\u044f \u043e\u0448\u0438\u0431\u043a\u043e\u0439, \u0447\u0442\u043e \u043c\u044b \u0434\u043e\u043b\u0436\u043d\u044b \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u0438\u0440\u043e\u0432\u0430\u0442\u044c<\/p>\n<\/li>\n<li>\n<p><code>_PyObject_GC_Link<\/code> \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u0442 \u043e\u0431\u044a\u0435\u043a\u0442 \u0432 GC \u0432 \u043c\u043e\u043b\u043e\u0434\u043e\u0435 \u043f\u043e\u043a\u043e\u043b\u0435\u043d\u0438\u0435 \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432<\/p>\n<\/li>\n<li>\n<p>\u0412\u044b\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u043f\u0430\u043c\u044f\u0442\u0438 \u0432 GIL \u0438 noGIL \u0440\u0435\u0436\u0438\u043c\u0430\u0445 \u2013 \u0440\u0430\u0437\u043d\u043e\u0435, \u043f\u0440\u043e mimalloc \u0432 nogil \u043c\u043e\u0436\u043d\u043e <a href=\"https:\/\/github.com\/python\/cpython\/issues\/90815\" rel=\"noopener noreferrer nofollow\">\u043f\u043e\u0447\u0438\u0442\u0430\u0442\u044c \u0442\u0443\u0442<\/a>, \u0432 \u0440\u0430\u043c\u043a\u0430\u0445 \u0434\u0430\u043d\u043d\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0438 \u2013 \u043f\u0435\u0440\u0435\u0431\u043e\u0440 \u0436\u0435\u0441\u0442\u0438 \ud83d\ude42<\/p>\n<\/li>\n<li>\n<p>\u0418 \u0432\u043e\u0442 \u0443\u0436\u0435 \u0432\u043d\u0443\u0442\u0440\u0438 <a href=\"https:\/\/docs.python.org\/3\/c-api\/memory.html#c.PyObject_Malloc\" rel=\"noopener noreferrer nofollow\">PyObject_Malloc<\/a> \u043c\u044b \u0444\u0438\u0437\u0438\u0447\u0435\u0441\u043a\u0438 \u0432\u044b\u0434\u0435\u043b\u0438\u043c \u043f\u0430\u043c\u044f\u0442\u044c \u0434\u043b\u044f \u043e\u0431\u044a\u0435\u043a\u0442\u0430 \u0432 \u043d\u0443\u0436\u043d\u043e\u043c \u0440\u0430\u0437\u043c\u0435\u0440\u0435<\/p>\n<\/li>\n<\/ul>\n<p>\u0424\u0443\u0445! \u041e\u0431\u044a\u0435\u043a\u0442 \u043d\u0443\u0436\u043d\u043e\u0433\u043e \u0440\u0430\u0437\u043c\u0435\u0440\u0430 \u043c\u044b \u0441\u043e\u0437\u0434\u0430\u043b\u0438. \u041e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435, \u0447\u0442\u043e \u043d\u0438\u0433\u0434\u0435 \u043d\u0435 \u0431\u044b\u043b\u043e \u043c\u043e\u043c\u0435\u043d\u0442\u0430 \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0438 \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u044b\u0445 \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432 \u0432 \u043a\u043e\u0440\u0442\u0435\u0436. \u041c\u044b \u043f\u0440\u043e\u0441\u0442\u043e \u0432\u044b\u0434\u0435\u043b\u0438\u043b\u0438 \u043f\u0430\u043c\u044f\u0442\u044c. <\/p>\n<p>\u0410 \u0442\u0435\u043f\u0435\u0440\u044c \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u0435\u0433\u043e \u0441\u043a\u043e\u0440\u0435\u0435 \u0443\u043d\u0438\u0447\u0442\u043e\u0436\u0438\u043c! \u0427\u0442\u043e\u0431\u044b \u0438\u043c\u0435\u0442\u044c \u043f\u043e\u0432\u043e\u0434 \u043f\u043e\u0433\u043e\u0432\u043e\u0440\u0438\u0442\u044c \u043f\u0440\u043e \u0434\u0435\u0430\u043b\u043b\u043e\u043a\u0430\u0446\u0438\u044e.<\/p>\n<p>\u0421\u043c\u043e\u0442\u0440\u0438\u043c \u043d\u0430 <a href=\"https:\/\/docs.python.org\/3\/c-api\/typeobj.html#c.PyTypeObject.tp_dealloc\" rel=\"noopener noreferrer nofollow\">tp_dealloc<\/a>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0441\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0435\u0442, \u043f\u0435\u0440\u0435\u0434 \u0432\u044b\u0447\u0438\u0449\u0435\u043d\u0438\u0435\u043c \u043e\u0431\u044a\u0435\u043a\u0442\u0430 \u0438\u0437 \u043f\u0430\u043c\u044f\u0442\u0438: <\/p>\n<p>\u0427\u0442\u043e \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 \u043f\u043e \u0448\u0430\u0433\u0430\u043c:<\/p>\n<ul>\n<li>\n<p>\u041f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u043c, \u0447\u0442\u043e \u043c\u044b \u043d\u0435 \u0434\u0435\u0430\u043b\u043b\u043e\u0446\u0438\u0440\u0443\u0435\u043c <code>()<\/code>, \u043d\u0443\u0436\u043d\u043e \u0434\u043b\u044f \u0434\u0435\u0431\u0430\u0433 \u0441\u0431\u043e\u0440\u043a\u0438<\/p>\n<\/li>\n<li>\n<p>\u0423\u0431\u0438\u0440\u0430\u0435\u043c \u0442\u0435\u043a\u0443\u0449\u0438\u0439 \u043e\u0431\u044a\u0435\u043a\u0442 \u0438\u0437 GC \u0441 <a href=\"https:\/\/docs.python.org\/3\/c-api\/gcsupport.html#c.PyObject_GC_UnTrack\" rel=\"noopener noreferrer nofollow\">PyObject_GC_UnTrack<\/a><\/p>\n<\/li>\n<li>\n<p>\u041d\u0430\u0447\u0438\u043d\u0430\u0435\u043c \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u044e (\u0438 \u0437\u0430\u0449\u0438\u0442\u0443 \u0441\u0442\u0435\u043a\u0430) \u043f\u0440\u0438 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u0438 \u0431\u043e\u043b\u044c\u0448\u0438\u0445 \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432 \u0441 <code>Py_TRASHCAN_BEGIN<\/code>, \u043e \u0447\u0435\u043c <a href=\"https:\/\/t.me\/opensource_findings\/881\" rel=\"noopener noreferrer nofollow\">\u044f \u043f\u0438\u0441\u0430\u043b \u043f\u043e\u0434\u0440\u043e\u0431\u043d\u043e \u0443 \u0441\u0435\u0431\u044f \u0432 \u0442\u0433 \u043a\u0430\u043d\u0430\u043b\u0435<\/a><\/p>\n<\/li>\n<li>\n<p>\u041f\u0440\u043e\u0445\u043e\u0434\u0438\u043c\u0441\u044f \u043f\u043e \u0432\u0441\u0435\u043c \u043e\u0431\u044a\u0435\u043a\u0442\u0430\u043c \u0432 <code>ob_item<\/code> \u0438 \u043d\u0435\u0449\u0430\u0434\u043d\u043e \u0434\u0435\u043a\u0440\u0435\u0444\u0438\u043c \u0438\u0445, \u0447\u0442\u043e\u0431\u044b \u0443\u043c\u0435\u043d\u044c\u0448\u0438\u0442\u044c <code>ob_refcnt<\/code> \u0438 \u043e\u0441\u0432\u043e\u0431\u043e\u0434\u0438\u0442\u044c \u0438\u0445, \u0435\u0441\u043b\u0438 \u043e\u043d\u0438 \u0431\u043e\u043b\u044c\u0448\u0435 \u043d\u0438\u0433\u0434\u0435 \u043d\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442\u0441\u044f<\/p>\n<\/li>\n<li>\n<p>\u0410 \u0434\u0430\u043b\u044c\u0448\u0435 \u2013 \u043c\u044b \u043f\u044b\u0442\u0430\u0435\u043c\u0441\u044f \u0441\u0434\u0435\u043b\u0430\u0442\u044c <code>maybe_freelist_push<\/code>, \u0438 \u0435\u0441\u043b\u0438 \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u043e\u0441\u044c, <strong>\u0442\u043e \u043c\u044b \u043d\u0435 \u0432\u044b\u0441\u0432\u043e\u0431\u043e\u0436\u0434\u0430\u0435\u043c \u043f\u0430\u043c\u044f\u0442\u044c<\/strong> \u0438 \u043d\u0435 \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u043c <code>tp_free<\/code>. \u0410 \u043e\u0441\u0432\u043e\u0431\u043e\u0436\u0434\u0430\u0435\u043c \u043f\u0430\u043c\u044f\u0442\u044c, \u0442\u043e\u043b\u044c\u043a\u043e \u043a\u043e\u0433\u0434\u0430 \u043d\u0435 \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u043e\u0441\u044c \u0437\u0430\u043f\u0443\u0448\u0438\u0442\u044c \u043d\u043e\u0432\u044b\u0439 <code>freelist<\/code>. \u0422\u0443\u0442 \u043d\u0430\u0441\u0442\u0430\u043b\u043e \u0432\u0440\u0435\u043c\u044f \u043f\u0440\u043e \u043d\u0438\u0445 \u0440\u0430\u0441\u0441\u043a\u0430\u0437\u0430\u0442\u044c!<\/p>\n<\/li>\n<\/ul>\n<p>\u0412\u043e\u043e\u0431\u0449\u0435, \u0438\u0434\u0435\u044f \u043f\u0435\u0440\u0435\u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043f\u0430\u043c\u044f\u0442\u044c \u043d\u0443\u0436\u043d\u043e\u0433\u043e \u0440\u0430\u0437\u043c\u0435\u0440\u0430 \u043f\u043e\u0434 \u043e\u0431\u044a\u0435\u043a\u0442\u044b \u043e\u0434\u0438\u043d\u0430\u043a\u043e\u0432\u043e\u0433\u043e \u0440\u0430\u0437\u043c\u0435\u0440\u0430 \u2013 \u0441\u043e\u0432\u0441\u0435\u043c \u043d\u0435 \u043d\u043e\u0432\u0430. \u0422\u0443\u0442 \u0440\u043e\u0432\u043d\u043e \u0442\u0430\u043a\u043e\u0435 \u0438 \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442. \u041f\u0440\u0438 \u0434\u0435\u0430\u043b\u043b\u043e\u043a\u0430\u0446\u0438\u0438 \u043c\u044b \u0434\u043b\u044f \u043a\u043e\u0440\u0442\u0435\u0436\u0435\u0439 \u0441 \u0440\u0430\u0437\u043c\u0435\u0440\u043e\u043c <code>&lt;20<\/code> \u043d\u0435 \u0432\u044b\u0441\u0432\u043e\u0431\u043e\u0436\u0434\u0430\u0435\u043c \u043f\u0430\u043c\u044f\u0442\u044c \u043f\u0440\u043e\u0441\u0442\u043e \u0442\u0430\u043a. \u041c\u044b \u043f\u043e\u043d\u0438\u043c\u0430\u0435\u043c, \u0447\u0442\u043e \u043a\u0442\u043e-\u0442\u043e \u0441\u043a\u043e\u0440\u043e \u0441\u043d\u043e\u0432\u0430 \u0437\u0430\u0445\u043e\u0447\u0435\u0442 \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u043a\u043e\u0440\u0442\u0435\u0436 \u0442\u0430\u043a\u043e\u0433\u043e \u0440\u0430\u0437\u043c\u0435\u0440\u0430. \u0418 \u043d\u0430\u043c \u043d\u0435 \u043d\u0443\u0436\u043d\u043e \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0434\u0435\u043b\u044f\u0442\u044c \u043f\u0430\u043c\u044f\u0442\u044c (\u0447\u0442\u043e \u0434\u043e\u043b\u0433\u043e), \u043c\u044b \u0441\u043c\u043e\u0436\u0435\u043c \u043f\u0440\u043e\u0441\u0442\u043e \u043f\u0435\u0440\u0435\u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0435 &#171;\u0436\u0434\u0443\u0449\u0438\u0435&#187; \u0443\u0447\u0430\u0441\u0442\u043a\u0438 \u043f\u0430\u043c\u044f\u0442\u0438.<\/p>\n<p>\u0421\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u2013 \u0432\u043e \u0432\u0440\u0435\u043c\u044f \u0434\u0435\u0430\u043b\u043b\u043e\u043a\u0430\u0446\u0438\u0438 \u043c\u044b \u0441\u043a\u043b\u0430\u0434\u044b\u0432\u0430\u0435\u043c \u043e\u0431\u044a\u0435\u043a\u0442\u044b \u043a\u043e\u0440\u0442\u0435\u0436\u0435\u0439 \u043d\u0443\u0436\u043d\u043e\u0433\u043e \u0440\u0430\u0437\u043c\u0435\u0440\u0430 \u0432\u043e <code>freelist<\/code>:<\/p>\n<pre><code class=\"cpp\">static inline int maybe_freelist_push(PyTupleObject *op) {     if (!Py_IS_TYPE(op, &amp;PyTuple_Type)) {         return 0;     }     Py_ssize_t index = Py_SIZE(op) - 1;     if (index &lt; PyTuple_MAXSAVESIZE) {         return _Py_FREELIST_PUSH(tuples[index], op, Py_tuple_MAXFREELIST);     }     return 0; }<\/code><\/pre>\n<p>\u0410 \u0432\u043e \u0432\u0440\u0435\u043c\u044f \u0430\u043b\u043b\u043e\u043a\u0430\u0446\u0438\u0438 \u043f\u0440\u043e\u0431\u0443\u0435\u043c \u043d\u0430\u0439\u0442\u0438 \u043d\u0443\u0436\u043d\u044b\u0439 \u043d\u0443\u0436\u043d\u043e\u0433\u043e \u0440\u0430\u0437\u043c\u0435\u0440\u0430 \u0438 \u043f\u0435\u0440\u0435\u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c. \u041f\u043e\u043c\u043d\u0438\u0442\u0435, \u044f \u043e\u0431\u0435\u0449\u0430\u043b \u043f\u043e\u0434\u0440\u043e\u0431\u043d\u0435\u0435 \u0440\u0430\u0441\u0441\u043a\u0430\u0437\u0430\u0442\u044c \u043f\u0440\u043e <code>_Py_FREELIST_POP<\/code>? \u0422\u0435\u043f\u0435\u0440\u044c \u043c\u044b \u043f\u043e\u043d\u0438\u043c\u0430\u0435\u043c, \u0437\u0430\u0447\u0435\u043c \u043e\u043d \u043d\u0443\u0436\u0435\u043d \u0432 <code>tp_alloc<\/code>:<\/p>\n<pre><code class=\"cpp\">static PyTupleObject * tuple_alloc(Py_ssize_t size) {     \/\/ ...     Py_ssize_t index = size - 1;     if (index &lt; PyTuple_MAXSAVESIZE) {  \/\/ 20         PyTupleObject *op = _Py_FREELIST_POP(PyTupleObject, tuples[index]);         if (op != NULL) {             return op;         }     }     \/\/ ... }<\/code><\/pre>\n<p>\u0415\u0441\u043b\u0438 \u043a\u043e\u0440\u0442\u0435\u0436 \u043d\u0443\u0436\u043d\u043e\u0433\u043e \u0440\u0430\u0437\u043c\u0435\u0440\u0430 \u0435\u0441\u0442\u044c \u0432 <code>_Py_FREELIST<\/code>, \u0442\u043e \u043c\u044b \u043f\u0440\u043e\u0441\u0442\u043e \u0432\u043e\u0437\u044c\u043c\u0435\u043c \u0435\u0433\u043e \u0438 \u0432\u0435\u0440\u043d\u0435\u043c. \u0411\u044b\u0441\u0442\u0440\u0435\u0435 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u043d\u043e\u0432\u044b\u0445 \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432. \u0411\u044b\u0441\u0442\u0440\u0435\u0435 \u0441\u0431\u043e\u0440\u043a\u0430 \u043d\u0435\u043d\u0443\u0436\u043d\u044b\u0445. \u041f\u0435\u0440\u0435\u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u043f\u0430\u043c\u044f\u0442\u0438.<\/p>\n<p>\u041c\u044b \u043c\u043e\u0436\u0435\u043c \u043b\u0435\u0433\u043a\u043e \u043f\u043e\u0434\u0442\u0432\u0435\u0440\u0434\u0438\u0442\u044c \u043c\u043e\u0438 \u0441\u043b\u043e\u0432\u0430 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u043f\u0440\u0438\u043c\u0435\u0440\u043e\u043c (\u0441\u043f\u0435\u0446\u0438\u0430\u043b\u044c\u043d\u043e \u0431\u0435\u0440\u0435\u043c \u0431\u043e\u043b\u044c\u0448\u043e\u0439 \u0440\u0430\u0437\u043c\u0435\u0440, \u0447\u0442\u043e\u0431\u044b \u043d\u0438\u0433\u0434\u0435 \u0432\u043d\u0443\u0442\u0440\u0438 \u0438\u043d\u0442\u0435\u0440\u043f\u0440\u0435\u0442\u0430\u0442\u043e\u0440\u0430 \u043d\u0430 \u0441\u043e\u0437\u0434\u0430\u0432\u0430\u043b\u0438\u0441\u044c &#171;\u0443\u0442\u0438\u043b\u0438\u0442\u0430\u0440\u043d\u044b\u0435&#187; \u043a\u043e\u0440\u0442\u0435\u0436\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043c\u044b \u043d\u0435 \u0443\u0432\u0438\u0434\u0438\u043c, \u043c\u0430\u043b\u043e\u0433\u043e \u0440\u0430\u0437\u043c\u0435\u0440\u0430):<\/p>\n<pre><code class=\"python\">&gt;&gt;&gt; a = tuple(range(18)) &gt;&gt;&gt; id(a) 4352063168 &gt;&gt;&gt; del a  &gt;&gt;&gt; b = tuple(range(18)) &gt;&gt;&gt; id(b)  # should be equal to `id(a)` 4352063168<\/code><\/pre>\n<p>\u0421\u043c\u043e\u0442\u0440\u0438\u0442\u0435! \u0410\u0434\u0440\u0435\u0441 \u0432 \u043f\u0430\u043c\u044f\u0442\u0438 \u0438\u043b\u0438 <code>id(b)<\/code> \u0442\u0430\u043a\u043e\u0439 \u0436\u0435 \u043a\u0430\u043a <code>id(a)<\/code>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043c\u044b \u0442\u043e\u043b\u044c\u043a\u043e \u0447\u0442\u043e \u0443\u0434\u0430\u043b\u0438\u043b\u0438. \u0422\u0435\u043f\u0435\u0440\u044c \u043c\u044b \u0437\u043d\u0430\u0435\u043c, \u043f\u043e\u0447\u0435\u043c\u0443. \u041f\u0440\u0438\u043a\u043e\u043b\u044c\u043d\u0430\u044f \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u044f?<\/p>\n<h2>\u041c\u0443\u0442\u0430\u0431\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c tuple<\/h2>\n<p>\u0412 \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043a\u0435 \u0431\u044b\u043b\u043e \u043f\u0440\u043e \u043c\u0443\u0442\u0430\u0431\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u2013 \u0433\u0434\u0435 \u043c\u0443\u0442\u0430\u0431\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c? \u041e\u0431\u044b\u0447\u043d\u043e \u043c\u044b \u0432\u0441\u0435 \u043f\u0440\u0438\u0432\u044b\u043a\u043b\u0438 \u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u043d\u0430 <code>tuple<\/code> \u043a\u0430\u043a \u043d\u0430 \u0438\u043c\u043c\u0443\u0442\u0430\u0431\u0435\u043b\u044c\u043d\u044b\u0439 \u043e\u0431\u044a\u0435\u043a\u0442; \u043d\u043e \u0432 C-API \u0432\u0441\u0435 \u043f\u043e-\u0434\u0440\u0443\u0433\u043e\u043c\u0443. \u0421\u0435\u0439\u0447\u0430\u0441 \u043f\u043e\u043a\u0430\u0436\u0443. \u0421\u043e\u0437\u0434\u0430\u0432\u0430\u0442\u044c tupl\u044b \u043c\u044b \u0443\u0436\u0435 \u043d\u0430\u0443\u0447\u0438\u043b\u0438\u0441\u044c, \u043d\u043e \u0432\u043e\u0442 \u043d\u0430\u043f\u043e\u043b\u043d\u044f\u0442\u044c \u0438\u0445 \u043e\u0431\u044a\u0435\u043a\u0442\u0430\u043c\u0438 \u2013 \u0435\u0449\u0435 \u043d\u0435\u0442. \u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u0438\u0441\u043f\u0440\u0430\u0432\u043b\u044f\u0442\u044c \u0438 \u043d\u0430\u043f\u043e\u043b\u043d\u044f\u0442\u044c!<\/p>\n<p>\u0412\u043e-\u043f\u0435\u0440\u0432\u044b\u0445, \u0432\u0430\u0436\u043d\u043e \u0441\u043a\u0430\u0437\u0430\u0442\u044c: \u0432\u043d\u0435\u0448\u043d\u0435 \u0438\u0437 Python API \u2013 <code>tuple<\/code> \u043d\u0435 \u043e\u0447\u0435\u043d\u044c-\u0442\u043e \u0438 \u043c\u0443\u0442\u0430\u0431\u0435\u043b\u044c\u043d\u044b\u0439. \u0415\u0433\u043e \u043c\u043e\u0436\u043d\u043e (\u043d\u043e \u043d\u0435 \u0432\u0441\u0435\u0433\u0434\u0430 \u043d\u0443\u0436\u043d\u043e) \u043c\u0443\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u0438\u0437 C-API. \u0414\u043b\u044f C-API \u043a\u0430\u043a\u043e\u0439-\u0442\u043e \u0442\u0430\u043a\u043e\u0439 \u043a\u043e\u0434 \u0431\u0443\u0434\u0435\u0442 <em>\u0430\u0431\u0441\u043e\u043b\u044e\u0442\u043d\u043e<\/em> \u043d\u043e\u0440\u043c\u0430\u043b\u044c\u043d\u044b\u043c:<\/p>\n<pre><code class=\"cpp\">PyObject *tup = PyTuple_New(2); if (tup == NULL) {     return NULL; }  PyTuple_SET_ITEM(tup, 0, first_obj); PyTuple_SET_ITEM(tup, 1, second_obj);<\/code><\/pre>\n<p>\u0427\u0442\u043e \u043c\u044b \u0442\u0443\u0442 \u0432\u0438\u0434\u0438\u043c? \u041c\u0443\u0442\u0430\u0446\u0438\u044e <code>tuple<\/code> \ud83d\ude31<\/p>\n<p>\u0421\u043d\u0430\u0447\u0430\u043b\u0430 \u043c\u044b \u0443\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0435\u043c 0 \u0438\u043d\u0434\u0435\u043a\u0441 \u0438 \u043d\u0443\u0436\u043d\u044b\u0439 \u043e\u0431\u044a\u0435\u043a\u0442, \u043f\u043e\u0442\u043e\u043c 1 \u0438\u043d\u0434\u0435\u043a\u0441 \u0438 \u043d\u0443\u0436\u043d\u044b\u0439 \u043e\u0431\u044a\u0435\u043a\u0442. \u0414\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u044f \u043f\u0440\u043e\u0441\u0438\u0442 \u0434\u0435\u043b\u0430\u0442\u044c \u0442\u0430\u043a \u0442\u043e\u043b\u044c\u043a\u043e \u0434\u043b\u044f \u043d\u043e\u0432\u044b\u0445 \u043a\u043e\u0440\u0442\u0435\u0436\u0435\u0439, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043c\u044b \u0441\u043e\u0437\u0434\u0430\u0435\u043c \u0432\u043d\u0443\u0442\u0440\u0438 C \u043a\u043e\u0434\u0430. \u041d\u043e \u043a\u0442\u043e \u043c\u044b \u0442\u0430\u043a\u0438\u0435, \u0447\u0442\u043e\u0431\u044b \u0447\u0438\u0442\u0430\u0442\u044c \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u044e? \ud83c\udf1a\ufe0f\ufe0f<\/p>\n<p>\u0412\u043e-\u0432\u0442\u043e\u0440\u044b\u0445, \u0441\u043f\u0440\u0430\u0432\u0435\u0434\u043b\u0438\u0432\u043e\u0441\u0442\u0438 \u0440\u0430\u0434\u0438: <a href=\"https:\/\/docs.python.org\/3\/c-api\/tuple.html#c.PyTuple_SetItem\" rel=\"noopener noreferrer nofollow\">PyTuple_SetItem<\/a> \u0447\u0443\u0442\u044c \u043c\u0435\u043d\u0435\u0435 \u043c\u0443\u0442\u0430\u0431\u0435\u043b\u044c\u043d\u0430\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u044f, \u0435\u0441\u043b\u0438 \u043c\u043e\u0436\u043d\u043e \u0442\u0430\u043a \u0441\u043a\u0430\u0437\u0430\u0442\u044c. \u041f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u043e\u043d\u0430 \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u0442, \u0447\u0442\u043e \u0443 \u043a\u043e\u0440\u0442\u0435\u0436\u0430 <code>ob_refcnt<\/code> \u0440\u0430\u0432\u0435\u043d <code>1<\/code>. \u0427\u0442\u043e \u043e\u0442\u0441\u0435\u043a\u0430\u0435\u0442 \u043b\u044e\u0431\u044b\u0435 \u043e\u0431\u044a\u0435\u043a\u0442\u044b, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0443\u0436\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442\u0441\u044f:<\/p>\n<pre><code class=\"cpp\">int PyTuple_SetItem(PyObject *op, Py_ssize_t i, PyObject *newitem) {     PyObject **p;     if (!PyTuple_Check(op) || Py_REFCNT(op) != 1) {         Py_XDECREF(newitem);         PyErr_BadInternalCall();         return -1;     }     if (i &lt; 0 || i &gt;= Py_SIZE(op)) {         Py_XDECREF(newitem);         PyErr_SetString(PyExc_IndexError,                         \"tuple assignment index out of range\");         return -1;     }     p = ((PyTupleObject *)op) -&gt; ob_item + i;     Py_XSETREF(*p, newitem);     return 0; }<\/code><\/pre>\n<p>\u0412-\u0442\u0440\u0435\u0442\u044c\u0438\u0445, \u0435\u0441\u0442\u044c \u043d\u043e\u0440\u043c\u0430\u043b\u044c\u043d\u044b\u0435 \u0410\u041f\u0418 \u0434\u043b\u044f \u0441\u0431\u043e\u0440\u0430 <code>tuple<\/code> \u0438\u0437 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0445 \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432: <a href=\"https:\/\/docs.python.org\/3\/c-api\/tuple.html#c.PyTuple_Pack\" rel=\"noopener noreferrer nofollow\">PyTuple_Pack<\/a> \u0438 <a href=\"https:\/\/docs.python.org\/3\/c-api\/arg.html#c.Py_BuildValue\" rel=\"noopener noreferrer nofollow\">Py_BuildValue<\/a>, \u043f\u0440\u043e \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u044f \u0440\u0430\u0441\u0441\u043a\u0430\u0437\u044b\u0432\u0430\u044e \u0432 \u043b\u0435\u043a\u0446\u0438\u0438 \u043d\u0430 \u0432\u0438\u0434\u0435\u043e.<\/p>\n<p>\u041d\u0443 \u0438 \u0444\u0438\u043d\u0430\u043b\u044c\u043d\u043e: \u043a\u043e\u043c\u0430\u043d\u0434\u0430 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0438 CPython \u0437\u0430\u0434\u0443\u043c\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u0441\u0434\u0435\u043b\u0430\u0442\u044c <code>tuple<\/code> \u0438\u043c\u043c\u0443\u0442\u0430\u0431\u0435\u043b\u044c\u043d\u044b\u043c \u0438 \u0438\u0437 C-API \u0442\u043e\u0436\u0435. \u041d\u043e \u0435\u0441\u0442\u044c \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u044b \u043e\u0431\u0440\u0430\u0442\u043d\u043e\u0439 \u0441\u043e\u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u0438. \u0421\u0442\u0430\u0440\u043e\u0435 \u0410\u041f\u0418 \u0443\u0436\u0435 \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e \u043b\u0435\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0438 \u043f\u0440\u0438\u043c\u0435\u0440\u043d\u043e \u0432\u0435\u0437\u0434\u0435. \u0421\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0431\u0441\u0443\u0436\u0434\u0435\u043d\u0438\u0435: <a href=\"https:\/\/github.com\/python\/cpython\/issues\/127058\" rel=\"noopener noreferrer nofollow\">https:\/\/github.com\/python\/cpython\/issues\/127058<\/a><\/p>\n<h2>\u041f\u043e\u0445\u0443\u043b\u0438\u0433\u0430\u043d\u0438\u043c?<\/h2>\n<p>\u0412 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u043f\u0440\u0438\u043a\u043e\u043b\u0430 \u044f \u0441\u0434\u0435\u043b\u0430\u043b \u0441\u0432\u043e\u0439 \u043c\u043e\u0434\u0443\u043b\u044c \u043d\u0430 C, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043c\u0443\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043b\u044e\u0431\u044b\u0435 \u043a\u043e\u0440\u0442\u0435\u0436\u0438:<\/p>\n<pre><code class=\"python\">&gt;&gt;&gt; import mutable_tuple  &gt;&gt;&gt; x = (1, 2) &gt;&gt;&gt; mutable_tuple.set_item(x, 0, 5)  &gt;&gt;&gt; x (5, 2)<\/code><\/pre>\n<p>\u041d\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0439\u0442\u0435 \u043d\u0435 \u0434\u043b\u044f \u0447\u0435\u0433\u043e \u0441\u0435\u0440\u044c\u0435\u0437\u043d\u043e\u0433\u043e. \u041d\u043e \u043c\u043e\u0436\u043d\u043e <a href=\"https:\/\/github.com\/sobolevn\/mutable-tuple\/blob\/master\/mutable_tuple\/mod.c\" rel=\"noopener noreferrer nofollow\">\u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u0438\u0441\u0445\u043e\u0434\u043d\u0438\u043a\u0438<\/a> \u0438 \u0447\u0435\u043c\u0443-\u0442\u043e \u043d\u0430\u0443\u0447\u0438\u0442\u044c\u0441\u044f (\u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0442\u0430\u043a \u043d\u0435 \u0434\u0435\u043b\u0430\u0442\u044c).<\/p>\n<p>\u0418 \u0441\u0430\u043c\u044b\u0439 \u0444\u0438\u043d\u0430\u043b: \u043d\u0430\u043c \u0432\u043e\u043e\u0431\u0449\u0435 \u043d\u0435 \u043d\u0443\u0436\u043d\u044b \u043d\u0438\u043a\u0430\u043a\u0438\u0435 \u0432\u043d\u0435\u0448\u043d\u0438\u0435 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438, \u0447\u0442\u043e\u0431\u044b \u043c\u0443\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c <code>tuple<\/code> \u0432 \u043f\u0438\u0442\u043e\u043d\u0435. \u0421\u043c\u043e\u0442\u0440\u0438\u043c \u043d\u0430 \u0442\u0430\u043a\u043e\u0439 \u043f\u0440\u0438\u043c\u0435\u0440 (\u043f\u043e\u0436\u0430\u043b\u0443\u0439\u0441\u0442\u0430, \u0443\u0431\u0435\u0440\u0438\u0442\u0435 \u043e\u0442 \u043f\u0440\u043e\u0441\u043c\u043e\u0442\u0440\u0430 \u0432\u043f\u0435\u0447\u0430\u0442\u043b\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0445 \u043b\u044e\u0434\u0435\u0439 \u0438 \u0434\u0435\u0442\u0435\u0439):<\/p>\n<pre><code class=\"python\">&gt;&gt;&gt; tup1 = (1, 2) &gt;&gt;&gt; tup1_2 = tup1 &gt;&gt;&gt; tup2 = (3, 4)  &gt;&gt;&gt; import ctypes &gt;&gt;&gt; offset = ( ...     ctypes.sizeof(ctypes.c_ssize_t)    # skip ob_refcnt ...     + ctypes.sizeof(ctypes.c_void_p)   # skip ob_base ...     + ctypes.sizeof(ctypes.c_ssize_t)  # skip ob_item ... ) &gt;&gt;&gt; size = ctypes.sizeof(ctypes.c_void_p) * len(tup1) &gt;&gt;&gt; ctypes.memmove(id(tup1) + offset, id(tup2) + offset, size)  &gt;&gt;&gt; tup1, tup1_2, tup2 ((3, 4), (3, 4), (3, 4))<\/code><\/pre>\n<p>\u0422\u0443\u0442 \u043c\u044b \u043f\u0440\u043e\u0441\u0442\u043e \u0434\u0432\u0438\u0433\u0430\u0435\u043c \u043f\u0430\u043c\u044f\u0442\u044c \u0438\u0437 \u043e\u0434\u043d\u043e\u0433\u043e \u043e\u0431\u044a\u0435\u043a\u0442\u0430 \u0432 \u0434\u0440\u0443\u0433\u043e\u0439. \u041c\u043e\u0436\u043d\u043e \u043b\u0438 \u0441\u0447\u0438\u0442\u0430\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0439 \u043f\u043e\u0434\u0445\u043e\u0434 \u043c\u0443\u0442\u0430\u0446\u0438\u0435\u0439? \u041d\u0430\u043f\u0438\u0448\u0438\u0442\u0435 \u0432 \u043a\u043e\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u044f\u0445. \u0412\u043e\u043f\u0440\u043e\u0441 \u0441\u043f\u043e\u0440\u043d\u044b\u0439. \u041d\u043e \u0442\u0430\u043a 100% \u043b\u0443\u0447\u0448\u0435 \u043d\u0435 \u0434\u0435\u043b\u0430\u0442\u044c. &#171;\u0418\u043b\u0438 \u0436\u0438\u0432\u0438\u0442\u0435 \u0434\u0430\u043b\u044c\u0448\u0435 \u0432 \u043f\u0440\u043e\u043a\u043b\u044f\u0442\u043e\u043c \u043c\u0438\u0440\u0435, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0441\u0430\u043c\u0438 \u0438 \u0441\u043e\u0437\u0434\u0430\u043b\u0438&#187;. <\/p>\n<p>\u0428\u0430\u043b\u043e\u0441\u0442\u044c \u0443\u0434\u0430\u043b\u0430\u0441\u044c! \u0411\u043e\u043b\u044c\u0448\u0435 \u043c\u044b \u0442\u0430\u043a\u043e\u0439 \u043a\u043e\u0434 \u043e\u0431\u0435\u0449\u0430\u0435\u043c \u043d\u0435 \u043f\u0438\u0441\u0430\u0442\u044c!<\/p>\n<h2>\u0417\u0430\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435<\/h2>\n<p>\u0412\u0441\u0435\u043c \u0431\u043e\u043b\u044c\u0448\u043e\u0435 \u0441\u043f\u0430\u0441\u0438\u0431\u043e \u0437\u0430 \u0438\u043d\u0442\u0435\u0440\u0435\u0441 \u043a \u0434\u0435\u0442\u0430\u043b\u044f\u043c \u043f\u0438\u0442\u043e\u043d\u0430, \u043f\u0440\u043e\u0447\u0438\u0442\u0430\u0442\u044c \u0442\u0430\u043a\u0443\u044e \u0441\u0442\u0430\u0442\u044c\u044e \u0431\u044b\u043b\u043e \u043d\u0435 \u043f\u0440\u043e\u0441\u0442\u043e! \u0412\u044b \u043a\u0440\u0443\u0442\u044b\u0435!<\/p>\n<p>\u0415\u0441\u043b\u0438 \u0432\u0430\u043c \u0445\u043e\u0447\u0435\u0442\u0441\u044f \u0431\u043e\u043b\u044c\u0448\u0435 \u0436\u0435\u0441\u0442\u0438:<\/p>\n<ul>\n<li>\n<p>\u041c\u043e\u0436\u043d\u043e \u043f\u043e\u0434\u043f\u0438\u0441\u0430\u0442\u044c \u043d\u0430 \u043c\u043e\u0439 \u0422\u0413 \u043a\u0430\u043d\u0430\u043b, \u0433\u0434\u0435 \u0442\u0430\u043a\u043e\u0433\u043e \u043e\u0447\u0435\u043d\u044c \u043c\u043d\u043e\u0433\u043e: <a href=\"https:\/\/t.me\/opensource_findings\" rel=\"noopener noreferrer nofollow\">https:\/\/t.me\/opensource_findings<\/a><\/p>\n<\/li>\n<li>\n<p>\u041f\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u0432\u0438\u0434\u0435\u043e \u043f\u0440\u043e \u0433\u043b\u0443\u0431\u043e\u043a\u0438\u0439 \u043f\u0438\u0442\u043e\u043d \u043d\u0430 \u043c\u043e\u0435\u043c \u043a\u0430\u043d\u0430\u043b\u0435: <a href=\"https:\/\/www.youtube.com\/@sobolevn\" rel=\"noopener noreferrer nofollow\">https:\/\/www.youtube.com\/@sobolevn<\/a><\/p>\n<\/li>\n<li>\n<p>\u041f\u043e\u0434\u0434\u0435\u0440\u0436\u0430\u0442\u044c \u043c\u043e\u044e \u0440\u0430\u0431\u043e\u0442\u0443 \u043d\u0430\u0434 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u043e\u0439 \u044f\u0434\u0440\u0430 CPython, \u0432\u0438\u0434\u0435\u043e \u0438 \u0441\u0442\u0430\u0442\u044c\u044f\u043c\u0438. \u0415\u0441\u043b\u0438 \u0432\u044b \u0445\u043e\u0442\u0438\u0442\u0435 \u0431\u043e\u043b\u044c\u0448\u0435 \u0445\u043e\u0440\u043e\u0448\u0435\u0433\u043e \u0442\u0435\u0445\u043d\u0438\u0447\u0435\u0441\u043a\u043e\u0433\u043e \u043a\u043e\u043d\u0442\u0435\u043d\u0442\u0430: <a href=\"https:\/\/boosty.to\/sobolevn\" rel=\"noopener noreferrer nofollow\">https:\/\/boosty.to\/sobolevn<\/a><\/p>\n<\/li>\n<\/ul>\n<p>\u0414\u043e \u043d\u043e\u0432\u044b\u0445 \u0432\u0441\u0442\u0440\u0435\u0447 \u0432 \u043a\u0438\u0448\u043a\u0430\u0445 \u043f\u0438\u0442\u043e\u043d\u0430!<\/p>\n<\/p>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/874608\/\"> https:\/\/habr.com\/ru\/articles\/874608\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<figure class=\"full-width\"><\/figure>\n<p>\u041f\u0440\u0438\u0432\u0435\u0442! \u041c\u0435\u043d\u044f \u0437\u043e\u0432\u0443\u0442 \u041d\u0438\u043a\u0438\u0442\u0430 \u0421\u043e\u0431\u043e\u043b\u0435\u0432, \u044f core-\u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a \u044f\u0437\u044b\u043a\u0430 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f CPython, \u0430 \u0442\u0430\u043a \u0436\u0435 \u0430\u0432\u0442\u043e\u0440 \u0441\u0435\u0440\u0438\u0438 \u0432\u0438\u0434\u0435\u043e \u043f\u0440\u043e \u0435\u0433\u043e \u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432\u043e.<\/p>\n<p>\u042f \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0430\u044e \u0441\u0432\u043e\u0439 \u0446\u0438\u043a\u043b \u0441\u0442\u0430\u0442\u0435\u0439 \u043d\u0430 \u0445\u0430\u0431\u0440\u0435 \u043f\u0440\u043e \u0434\u0435\u0442\u0430\u043b\u0438 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u043f\u0438\u0442\u043e\u043d\u0430. \u0421\u0435\u0433\u043e\u0434\u043d\u044f \u044f \u0445\u043e\u0447\u0443 \u0440\u0430\u0441\u0441\u043a\u0430\u0437\u0430\u0442\u044c, \u043a\u0430\u043a <code>tuple<\/code> \u0443\u0441\u0442\u0440\u043e\u0435\u043d \u0432\u043d\u0443\u0442\u0440\u0438.<\/p>\n<p>\u041f\u043e\u0434 \u043a\u0430\u0442\u043e\u043c \u0431\u0443\u0434\u0435\u0442 \u043f\u0440\u043e: \u043c\u0443\u0442\u0430\u0431\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u043a\u043e\u0440\u0442\u0435\u0436\u0435\u0439 (\u0438 \u043f\u043b\u0430\u043d\u044b \u043f\u043e \u043e\u0442\u043c\u0435\u043d\u0435 \u0442\u0430\u043a\u043e\u0433\u043e \u043f\u043e\u0432\u0435\u0434\u0435\u043d\u0438\u044f), \u0434\u0438\u0447\u044c \u0441 <code>ctypes<\/code>, \u043f\u0435\u0440\u0435\u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u043f\u0430\u043c\u044f\u0442\u0438 \u0438 \u0434\u0440\u0443\u0433\u0438\u0435 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0438.<\/p>\n<p>\u041d\u0443 \u0438 \u043a\u043e\u043d\u0435\u0447\u043d\u043e \u2013 \u043c\u043e\u0435 \u043f\u043e\u043b\u043d\u043e\u0435 \u0432\u0438\u0434\u0435\u043e \u043d\u0430 \u043f\u043e\u043b\u0442\u043e\u0440\u0430 \u0447\u0430\u0441\u0430, \u0433\u0434\u0435 \u0431\u0443\u0434\u0435\u0442 \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0439 \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u0438 \u043e \u0434\u0435\u0442\u0430\u043b\u044f\u0445 \u0440\u0430\u0431\u043e\u0442\u044b VM.<\/p>\n<p>\u0415\u0441\u043b\u0438 \u0432\u0430\u043c \u0442\u0430\u043a\u043e\u0435 \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u043e \u0438\u043b\u0438 \u0446\u0435\u043b\u0438\u043a\u043e\u043c \u043d\u0435\u0437\u043d\u0430\u043a\u043e\u043c\u043e \u2013 \u0434\u043e\u0431\u0440\u043e \u043f\u043e\u0436\u0430\u043b\u043e\u0432\u0430\u0442\u044c!<\/p>\n<hr\/>\n<p>\u0414\u043b\u044f \u043d\u0430\u0447\u0430\u043b\u0430 \u2013 \u0432\u043e\u0442 \u0432\u0430\u043c \u0432\u0438\u0434\u0435\u043e; \u0432 \u043d\u0435\u043c \u044f \u043f\u043e\u0441\u0442\u0430\u0440\u0430\u043b\u0441\u044f \u0440\u0430\u0441\u0441\u043a\u0430\u0437\u0430\u0442\u044c \u0432\u043e\u043e\u0431\u0449\u0435 \u0432\u0441\u0435, \u0447\u0442\u043e \u044f \u0437\u043d\u0430\u044e \u043f\u0440\u043e <code>tuple<\/code> \u0432 <code>CPython<\/code>. \u041e\u0442 \u0430\u043d\u043d\u043e\u0442\u0430\u0446\u0438\u0439 (<code>tuple[int, ...]<\/code>, <code>tuple[()]<\/code>, <code>*tuple[int, int]<\/code>) \u0438 <code>TypeVarTuple<\/code> \u0434\u043e \u0441\u0438\u0448\u043d\u044b\u0445 \u0434\u0435\u0442\u0430\u043b\u0435\u0439: <code>_PyFreeList_<\/code> C-API \u0438 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0439 AST, VM \u0438 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440\u0430.<\/p>\n<p>\u0412\u0438\u0434\u0435\u043e \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u043e\u0441\u044c \u043e\u0447\u0435\u043d\u044c \u043d\u0430\u0441\u044b\u0449\u0435\u043d\u043d\u044b\u043c, \u0442\u0430\u043a \u0447\u0442\u043e \u0440\u0435\u043a\u043e\u043c\u043c\u0435\u043d\u0434\u0443\u044e \u043a \u043f\u0440\u043e\u0441\u043c\u043e\u0442\u0440\u0443.<\/p>\n<div class=\"tm-iframe_temp\" data-src=\"https:\/\/embedd.srv.habr.com\/iframe\/678b881cbfebf17680867fbe\" data-style=\"\" id=\"678b881cbfebf17680867fbe\" width=\"\"><\/div>\n<h2>\u0423\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432\u043e tuple<\/h2>\n<p>\u041d\u0430\u0447\u043d\u0435\u043c \u0441\u043e \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b \u0434\u0430\u043d\u043d\u044b\u0445 <code>PyTupleObject<\/code>, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u0441\u043e\u0431\u043e\u0439 \u0432\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u0435\u0435 \u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432\u043e <code>tuple<\/code> \u0432 CPython. \u0421\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u043f\u0440\u043e\u0441\u0442\u0430\u044f:<\/p>\n<pre><code class=\"cpp\">typedef struct {     PyObject_VAR_HEAD     \/* ob_item contains space for 'ob_size' elements.        Items must normally not be NULL, except during construction when        the tuple is not yet visible outside the function that builds it. *\/     PyObject *ob_item[1]; } PyTupleObject;<\/code><\/pre>\n<p>\u041f\u043e \u0441\u0443\u0442\u0438 \u0442\u0443\u0442 \u0443 \u043d\u0430\u0441 \u043f\u0440\u043e\u0441\u0442\u043e \u043c\u0430\u0441\u0441\u0438\u0432 \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0445\u0440\u0430\u043d\u0438\u0442 <code>tuple<\/code> \u0432 <code>ob_item<\/code>. \u041d\u043e \u0433\u0434\u0435 \u0436\u0435 \u0441\u0430\u043c \u0440\u0430\u0437\u043c\u0435\u0440 \u043a\u043e\u0440\u0442\u0435\u0436\u0430? \u0410 \u0432\u043e\u0442 \u043e\u043d \u0445\u0440\u0430\u043d\u0438\u0442\u0441\u044f \u0432 <code>PyObject_VAR_HEAD<\/code>. \u0412 CPython \u0435\u0441\u0442\u044c \u0434\u0432\u0430 \u0442\u0438\u043f\u0430 C-\u0448\u043d\u044b\u0445 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440: \u0441 \u043f\u043e\u0441\u0442\u043e\u044f\u043d\u043d\u044b\u043c \u0440\u0430\u0437\u043c\u0435\u0440\u043e\u043c (\u043a\u0430\u043a <code>int<\/code> \u0441 <code>python3.11+<\/code>) \u0438 \u0441 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u043c (variable) \u0440\u0430\u0437\u043c\u0435\u0440\u043e\u043c. <a href=\"https:\/\/docs.python.org\/3\/c-api\/structures.html#c.PyObject_HEAD\" rel=\"noopener noreferrer nofollow\">PyObject_HEAD<\/a> \u0438 <a href=\"https:\/\/docs.python.org\/3\/c-api\/structures.html#c.PyObject_VAR_HEAD\" rel=\"noopener noreferrer nofollow\">PyObject_VAR_HEAD<\/a> \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u043e. \u0421\u0440\u0430\u0432\u043d\u0438\u043c \u0438\u0445!<\/p>\n<pre><code class=\"cpp\">#define PyObject_HEAD PyObject ob_base; #define PyObject_VAR_HEAD PyVarObject ob_base;<\/code><\/pre>\n<p>\u0413\u0434\u0435 <code>PyObject ob_base<\/code> \u0431\u0443\u0434\u0435\u0442 \u0441\u0440\u0430\u0437\u0443 \u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0442\u0438\u043f \u043e\u0431\u044a\u0435\u043a\u0442\u0430, \u0430 <code>PyVarObject ob_base<\/code> \u0431\u0443\u0434\u0435\u0442 \u0434\u0435\u043b\u0430\u0442\u044c \u0442\u0430\u043a:<\/p>\n<pre><code class=\"cpp\">typedef struct {     PyObject ob_base;     Py_ssize_t ob_size; \/* Number of items in variable part *\/ } PyVarObject;<\/code><\/pre>\n<p>\u0420\u044f\u0434\u043e\u043c \u0435\u0449\u0435 \u0431\u0443\u0434\u0435\u0442 \u043b\u0435\u0436\u0430\u0442\u044c \u0438 \u0434\u043b\u0438\u043d\u0430 \u043e\u0431\u044a\u0435\u043a\u0442\u0430. \u0421\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e, \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0434\u043b\u0438\u043d\u0443 \u043c\u044b \u0432\u0441\u0435\u0433\u0434\u0430 \u043c\u043e\u0436\u0435\u043c \u0437\u0430 <code>O(1)<\/code>, \u0432\u043e\u0442 \u043a\u0430\u043a \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 <code>__len__<\/code> \u043a\u043e\u0440\u0442\u0435\u0436\u0430:<\/p>\n<pre><code class=\"cpp\">static Py_ssize_t tuple_length(PyObject *self) {     PyTupleObject *a = _PyTuple_CAST(self);     return Py_SIZE(a); }<\/code><\/pre>\n<p>\u0413\u0434\u0435 <a href=\"https:\/\/docs.python.org\/3\/c-api\/structures.html#c.Py_SIZE\" rel=\"noopener noreferrer nofollow\">Py_SIZE<\/a> \u2013 \u043f\u0440\u043e\u0441\u0442\u043e \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0438\u0437 <code>ob_size<\/code><\/p>\n<pre><code class=\"cpp\">static inline Py_ssize_t Py_SIZE(PyObject *ob) {     return _PyVarObject_CAST(ob)-&gt;ob_size; }<\/code><\/pre>\n<h2>\u0410\u043b\u043b\u043e\u043a\u0430\u0446\u0438\u044f \u0438 \u0434\u0435\u0430\u043b\u043b\u043e\u043a\u0430\u0446\u0438\u044f<\/h2>\n<p>\u0412\u0441\u0435 \u043c\u044b \u0437\u043d\u0430\u0435\u043c, \u0447\u0442\u043e <code>tuple<\/code> \u2013 \u043e\u0431\u044a\u0435\u043a\u0442 \u0438\u043c\u043c\u0443\u0442\u0430\u0431\u0435\u043b\u044c\u043d\u044b\u0439 \ud83c\udf1a\ufe0f\ufe0f. \u0410 \u0437\u043d\u0430\u0447\u0438\u0442 \u2013 \u043f\u0440\u0438 \u043b\u044e\u0431\u043e\u043c \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0438 \u043f\u0440\u0438\u0434\u0435\u0442\u0441\u044f \u043f\u0435\u0440\u0435\u0441\u043e\u0437\u0434\u0430\u0432\u0430\u0442\u044c \u043d\u043e\u0432\u044b\u0439 <code>tuple<\/code>. \u041a\u0430\u0436\u0435\u0442\u0441\u044f, \u0447\u0442\u043e \u0442\u0443\u0442 \u043c\u043e\u0436\u043d\u043e \u0431\u044b\u043b\u043e \u0431\u044b \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043f\u0440\u043e\u0446\u0435\u0441\u0441 \u043f\u0435\u0440\u0435\u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f. \u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u0440\u0430\u0437\u0431\u0438\u0440\u0430\u0442\u044c\u0441\u044f, \u043a\u0430\u043a \u043d\u0430 \u0441\u0430\u043c\u043e\u043c \u0434\u0435\u043b\u0435 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u0438 \u0443\u043d\u0438\u0447\u0442\u043e\u0436\u0435\u043d\u0438\u0435 \u043a\u043e\u0440\u0442\u0435\u0436\u0435\u0439. \u0410 \u043f\u0440\u043e \u0438\u043c\u043c\u0443\u0442\u0430\u0431\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u2013 \u0440\u0430\u0437\u0431\u0435\u0440\u0435\u043c \u0432 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0439 \u0433\u043b\u0430\u0432\u0435.<\/p>\n<p>\u041d\u0430\u0447\u043d\u0435\u043c \u0441 <a href=\"https:\/\/docs.python.org\/3\/c-api\/typeobj.html#c.PyTypeObject.tp_alloc\" rel=\"noopener noreferrer nofollow\">tp_alloc<\/a>: \u0432\u044b\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u043f\u0430\u043c\u044f\u0442\u0438 \u043f\u043e\u0434 \u043a\u043e\u0440\u0442\u0435\u0436 \u0438 \u0435\u0433\u043e \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435. \u0414\u043b\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u043a\u043e\u0440\u0442\u0435\u0436\u0430 \u043d\u0430\u043c \u043f\u043e\u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u0442\u043e\u043b\u044c\u043a\u043e \u0443\u043a\u0430\u0437\u0430\u043d\u0438\u0435 \u0434\u043b\u0438\u043d\u044b:<\/p>\n<pre><code class=\"cpp\">static PyTupleObject * tuple_alloc(Py_ssize_t size) {     if (size &lt; 0) {         PyErr_BadInternalCall();         return NULL;     }     assert(size != 0);    \/\/ The empty tuple is statically allocated.     Py_ssize_t index = size - 1;     if (index &lt; PyTuple_MAXSAVESIZE) {  \/\/ 20         PyTupleObject *op = _Py_FREELIST_POP(PyTupleObject, tuples[index]);         if (op != NULL) {             return op;         }     }     \/* Check for overflow *\/     if ((size_t)size &gt; ((size_t)PY_SSIZE_T_MAX - (sizeof(PyTupleObject) -                 sizeof(PyObject *))) \/ sizeof(PyObject *)) {         return (PyTupleObject *)PyErr_NoMemory();     }     return PyObject_GC_NewVar(PyTupleObject, &amp;PyTuple_Type, size); }<\/code><\/pre>\n<p>\u0417\u0434\u0435\u0441\u044c \u043c\u043d\u043e\u0433\u043e \u0432\u0441\u0435\u0433\u043e \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u043e\u0433\u043e:<\/p>\n<ol>\n<li>\n<p>\u0421\u043e\u0437\u0434\u0430\u0442\u044c \u043a\u043e\u0440\u0442\u0435\u0436 \u0441 \u0434\u043b\u0438\u043d\u043e\u0439 <code>&lt;0<\/code> \u2013 \u043e\u0447\u0435\u0432\u0438\u0434\u043d\u043e \u043d\u0435\u043b\u044c\u0437\u044f<\/p>\n<\/li>\n<li>\n<p>\u0421\u043e\u0437\u0434\u0430\u0432\u0430\u0442\u044c \u043a\u043e\u0440\u0442\u0435\u0436 \u0441 \u0434\u043b\u0438\u043d\u043e\u0439 <code>0<\/code>  \u2013 \u0442\u043e\u0436\u0435 \u043d\u0435\u043b\u044c\u0437\u044f, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e <code>()<\/code> \u2013 \u0441\u043f\u0435\u0446\u0438\u0430\u043b\u044c\u043d\u044b\u0439 <a href=\"https:\/\/peps.python.org\/pep-0683\" rel=\"noopener noreferrer nofollow\">immortal<\/a> \u043e\u0431\u044a\u0435\u043a\u0442, \u0435\u0433\u043e \u0441\u043e\u0437\u0434\u0430\u044e\u0442 \u0432 <a href=\"https:\/\/github.com\/python\/cpython\/blob\/f4afaa6f1190fbbea3e27c590096951d8ffdfb71\/Include\/internal\/pycore_global_objects.h#L55\" rel=\"noopener noreferrer nofollow\">\u0440\u0430\u043c\u043a\u0430\u0445 \u0441\u0442\u0430\u0440\u0442\u0430<\/a> \u0438\u043d\u0442\u0435\u0440\u043f\u0440\u0435\u0442\u0430\u0442\u043e\u0440\u0430 <a href=\"https:\/\/github.com\/python\/cpython\/blob\/f4afaa6f1190fbbea3e27c590096951d8ffdfb71\/Include\/internal\/pycore_runtime_init.h#L103-L105\" rel=\"noopener noreferrer nofollow\">\u043e\u0434\u0438\u043d \u0440\u0430\u0437<\/a> \u0432 <code>pycore_runtime_init<\/code> \u0432\u043e\u0442 \u0442\u0430\u043a: <code>.tuple_empty = { .ob_base = _PyVarObject_HEAD_INIT(&amp;PyTuple_Type, 0), }<\/code> <\/p>\n<\/li>\n<li>\n<p>\u0415\u0441\u043b\u0438 \u0440\u0430\u0437\u043c\u0435\u0440 \u043a\u043e\u0440\u0442\u0435\u0436\u0430 \u043d\u0435 \u043e\u0447\u0435\u043d\u044c \u0431\u043e\u043b\u044c\u0448\u043e\u0439 (<code>&lt; 20<\/code>), \u0442\u043e \u0435\u0449\u0435 \u043f\u0440\u043e\u0431\u0443\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u0443\u044e \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u044e \u0441 <code>_Py_FREELIST_POP<\/code>, \u043e \u043d\u0435\u0439 \u0431\u0443\u0434\u0435\u0442 \u0447\u0443\u0442\u044c \u043f\u043e\u0437\u0436\u0435<\/p>\n<\/li>\n<li>\n<p>\u0414\u0430\u043b\u0435\u0435 \u0441\u043c\u043e\u0442\u0440\u0438\u043c, \u0447\u0442\u043e \u0435\u0441\u043b\u0438 \u0440\u0430\u0437\u043c\u0435\u0440 \u043a\u043e\u0440\u0442\u0435\u0436\u0430 \u043e\u0447\u0435\u043d\u044c \u0431\u043e\u043b\u044c\u0448\u043e\u0439 \u2013 \u0441\u0440\u0430\u0437\u0443 \u043a\u0438\u0434\u0430\u0435\u043c \u043e\u0448\u0438\u0431\u043a\u0443 \u043f\u0430\u043c\u044f\u0442\u0438, \u0447\u0442\u043e\u0431 \u043d\u0435 \u0430\u043b\u043b\u043e\u0446\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0441\u043b\u0438\u0448\u043a\u043e\u043c \u043c\u043d\u043e\u0433\u043e<\/p>\n<\/li>\n<li>\n<p>\u0412 \u0441\u0430\u043c\u043e\u043c \u043a\u043e\u043d\u0446\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c <a href=\"https:\/\/docs.python.org\/3\/c-api\/gcsupport.html#c.PyObject_GC_NewVar\" rel=\"noopener noreferrer nofollow\">PyObject_GC_NewVar<\/a> \u0434\u043b\u044f \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 <code>PyTupleObject<\/code>, \u0432\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u0435\u0433\u043e <code>PyVarObject<\/code> \u0438 \u0444\u0438\u0437\u0438\u0447\u0435\u0441\u043a\u043e\u0433\u043e \u0432\u044b\u0434\u0435\u043b\u0435\u043d\u0438\u044f \u043f\u0430\u043c\u044f\u0442\u0438<\/p>\n<\/li>\n<\/ol>\n<p>\u0412\u043e\u0442 \u043a\u0430\u043a \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 <code>PyObject_GC_NewVar<\/code> (\u043a\u0441\u0442\u0430\u0442\u0438, \u043f\u0440\u044f\u043c\u043e \u0441\u0435\u0439\u0447\u0430\u0441 \u0438\u0445 \u0434\u0432\u0430: <a href=\"https:\/\/github.com\/python\/cpython\/blob\/f4afaa6f1190fbbea3e27c590096951d8ffdfb71\/Python\/gc.c#L2286-L2303\" rel=\"noopener noreferrer nofollow\">\u0434\u043b\u044f \u043e\u0431\u044b\u0447\u043d\u043e\u0433\u043e \u0431\u0438\u043b\u0434\u0430<\/a> \u0438 <a href=\"https:\/\/github.com\/python\/cpython\/blob\/f4afaa6f1190fbbea3e27c590096951d8ffdfb71\/Python\/gc_free_threading.c#L2160-L2177\" rel=\"noopener noreferrer nofollow\">\u0434\u043b\u044f nogil<\/a>, \u043d\u043e \u043e\u0442\u043b\u0438\u0447\u0438\u0439 \u0432\u0440\u043e\u0434\u0435 \u0431\u044b \u043d\u0435\u0442):<\/p>\n<pre><code class=\"cpp\">PyVarObject * _PyObject_GC_NewVar(PyTypeObject *tp, Py_ssize_t nitems) {     PyVarObject *op;      if (nitems &lt; 0) {         PyErr_BadInternalCall();         return NULL;     }     size_t presize = _PyType_PreHeaderSize(tp);     size_t size = _PyObject_VAR_SIZE(tp, nitems);     op = (PyVarObject *)gc_alloc(tp, size, presize);     if (op == NULL) {         return NULL;     }     _PyObject_InitVar(op, tp, nitems);     return op; }<\/code><\/pre>\n<p>\u0422\u0443\u0442 \u0435\u0441\u0442\u044c \u0434\u0432\u0430 \u0432\u0430\u0436\u043d\u044b\u0445 \u043c\u043e\u043c\u0435\u043d\u0442\u0430: <code>gc_alloc<\/code> \u0434\u043b\u044f \u0432\u044b\u0434\u0435\u043b\u0435\u043d\u0438\u044f \u043f\u0430\u043c\u044f\u0442\u0438, \u043f\u0440\u043e \u043a\u043e\u0442\u043e\u0440\u0443\u044e \u0437\u043d\u0430\u0435\u0442 GC. \u0418 <code>_PyObject_InitVar<\/code>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0443\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0435\u0442 <code>ob_size<\/code> \u0438 <code>ob_base<\/code> \u0447\u0435\u0440\u0435\u0437 <a href=\"https:\/\/docs.python.org\/3\/c-api\/structures.html#c.Py_SET_SIZE\" rel=\"noopener noreferrer nofollow\">Py_SET_SIZE<\/a> \u0438 <a href=\"https:\/\/docs.python.org\/3\/c-api\/structures.html#c.Py_SET_TYPE\" rel=\"noopener noreferrer nofollow\">Py_SET_TYPE<\/a>.<\/p>\n<p>\u041a\u0430\u043a GC \u0432\u044b\u0434\u0435\u043b\u044f\u0435\u0442 \u043f\u0430\u043c\u044f\u0442\u044c? \u0413\u043b\u0443\u0431\u043e\u043a\u0430 \u0436\u0435 \u043a\u0440\u043e\u043b\u0438\u0447\u044c\u044f \u043d\u043e\u0440\u0430!<\/p>\n<pre><code class=\"cpp\">static PyObject * gc_alloc(PyTypeObject *tp, size_t basicsize, size_t presize) {     PyThreadState *tstate = _PyThreadState_GET();     if (basicsize &gt; PY_SSIZE_T_MAX - presize) {         return _PyErr_NoMemory(tstate);     }     size_t size = presize + basicsize;     \/\/ \u0421\u043c\u043e\u0442\u0440\u0438 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435 `_PyObject_MallocWithType` \u043d\u0438\u0436\u0435:     char *mem = _PyObject_MallocWithType(tp, size);     if (mem == NULL) {         return _PyErr_NoMemory(tstate);     }     ((PyObject **)mem)[0] = NULL;     ((PyObject **)mem)[1] = NULL;     PyObject *op = (PyObject *)(mem + presize);     _PyObject_GC_Link(op);     return op; }  \/\/ Sets the heap used for PyObject_Malloc(), PyObject_Realloc(), etc. calls in \/\/ Py_GIL_DISABLED builds. We use different heaps depending on if the object \/\/ supports GC and if it has a pre-header. We smuggle the choice of heap \/\/ through the _mimalloc_thread_state. In the default build, this simply \/\/ calls PyObject_Malloc(). static inline void * _PyObject_MallocWithType(PyTypeObject *tp, size_t size) { #ifdef Py_GIL_DISABLED     _PyThreadStateImpl *tstate = (_PyThreadStateImpl *)_PyThreadState_GET();     struct _mimalloc_thread_state *m = &amp;tstate-&gt;mimalloc;     m-&gt;current_object_heap = _PyObject_GetAllocationHeap(tstate, tp); #endif     void *mem = PyObject_Malloc(size); #ifdef Py_GIL_DISABLED     m-&gt;current_object_heap = &amp;m-&gt;heaps[_Py_MIMALLOC_HEAP_OBJECT]; #endif     return mem; }<\/code><\/pre>\n<p>\u041f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 \u043c\u043d\u043e\u0433\u043e \u0432\u0441\u0435\u0433\u043e. \u041e\u0431\u0440\u0430\u0449\u0443 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435 \u043d\u0430 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043a\u043b\u044e\u0447\u0435\u0432\u044b\u0445 \u043c\u043e\u043c\u0435\u043d\u0442\u043e\u0432:<\/p>\n<ul>\n<li>\n<p>\u0412\u044b\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u043f\u0430\u043c\u044f\u0442\u0438 \u0432\u0441\u0435\u0433\u0434\u0430 \u043c\u043e\u0436\u0435\u0442 \u0437\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u044c\u0441\u044f \u043e\u0448\u0438\u0431\u043a\u043e\u0439, \u0447\u0442\u043e \u043c\u044b \u0434\u043e\u043b\u0436\u043d\u044b \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u0438\u0440\u043e\u0432\u0430\u0442\u044c<\/p>\n<\/li>\n<li>\n<p><code>_PyObject_GC_Link<\/code> \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u0442 \u043e\u0431\u044a\u0435\u043a\u0442 \u0432 GC \u0432 \u043c\u043e\u043b\u043e\u0434\u043e\u0435 \u043f\u043e\u043a\u043e\u043b\u0435\u043d\u0438\u0435 \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432<\/p>\n<\/li>\n<li>\n<p>\u0412\u044b\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u043f\u0430\u043c\u044f\u0442\u0438 \u0432 GIL \u0438 noGIL \u0440\u0435\u0436\u0438\u043c\u0430\u0445 \u2013 \u0440\u0430\u0437\u043d\u043e\u0435, \u043f\u0440\u043e mimalloc \u0432 nogil \u043c\u043e\u0436\u043d\u043e <a href=\"https:\/\/github.com\/python\/cpython\/issues\/90815\" rel=\"noopener noreferrer nofollow\">\u043f\u043e\u0447\u0438\u0442\u0430\u0442\u044c \u0442\u0443\u0442<\/a>, \u0432 \u0440\u0430\u043c\u043a\u0430\u0445 \u0434\u0430\u043d\u043d\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0438 \u2013 \u043f\u0435\u0440\u0435\u0431\u043e\u0440 \u0436\u0435\u0441\u0442\u0438 \ud83d\ude42<\/p>\n<\/li>\n<li>\n<p>\u0418 \u0432\u043e\u0442 \u0443\u0436\u0435 \u0432\u043d\u0443\u0442\u0440\u0438 <a href=\"https:\/\/docs.python.org\/3\/c-api\/memory.html#c.PyObject_Malloc\" rel=\"noopener noreferrer nofollow\">PyObject_Malloc<\/a> \u043c\u044b \u0444\u0438\u0437\u0438\u0447\u0435\u0441\u043a\u0438 \u0432\u044b\u0434\u0435\u043b\u0438\u043c \u043f\u0430\u043c\u044f\u0442\u044c \u0434\u043b\u044f \u043e\u0431\u044a\u0435\u043a\u0442\u0430 \u0432 \u043d\u0443\u0436\u043d\u043e\u043c \u0440\u0430\u0437\u043c\u0435\u0440\u0435<\/p>\n<\/li>\n<\/ul>\n<p>\u0424\u0443\u0445! \u041e\u0431\u044a\u0435\u043a\u0442 \u043d\u0443\u0436\u043d\u043e\u0433\u043e \u0440\u0430\u0437\u043c\u0435\u0440\u0430 \u043c\u044b \u0441\u043e\u0437\u0434\u0430\u043b\u0438. \u041e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435, \u0447\u0442\u043e \u043d\u0438\u0433\u0434\u0435 \u043d\u0435 \u0431\u044b\u043b\u043e \u043c\u043e\u043c\u0435\u043d\u0442\u0430 \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0438 \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u044b\u0445 \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432 \u0432 \u043a\u043e\u0440\u0442\u0435\u0436. \u041c\u044b \u043f\u0440\u043e\u0441\u0442\u043e \u0432\u044b\u0434\u0435\u043b\u0438\u043b\u0438 \u043f\u0430\u043c\u044f\u0442\u044c. <\/p>\n<p>\u0410 \u0442\u0435\u043f\u0435\u0440\u044c \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u0435\u0433\u043e \u0441\u043a\u043e\u0440\u0435\u0435 \u0443\u043d\u0438\u0447\u0442\u043e\u0436\u0438\u043c! \u0427\u0442\u043e\u0431\u044b \u0438\u043c\u0435\u0442\u044c \u043f\u043e\u0432\u043e\u0434 \u043f\u043e\u0433\u043e\u0432\u043e\u0440\u0438\u0442\u044c \u043f\u0440\u043e \u0434\u0435\u0430\u043b\u043b\u043e\u043a\u0430\u0446\u0438\u044e.<\/p>\n<p>\u0421\u043c\u043e\u0442\u0440\u0438\u043c \u043d\u0430 <a href=\"https:\/\/docs.python.org\/3\/c-api\/typeobj.html#c.PyTypeObject.tp_dealloc\" rel=\"noopener noreferrer nofollow\">tp_dealloc<\/a>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0441\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0435\u0442, \u043f\u0435\u0440\u0435\u0434 \u0432\u044b\u0447\u0438\u0449\u0435\u043d\u0438\u0435\u043c \u043e\u0431\u044a\u0435\u043a\u0442\u0430 \u0438\u0437 \u043f\u0430\u043c\u044f\u0442\u0438: <\/p>\n<p>\u0427\u0442\u043e \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 \u043f\u043e \u0448\u0430\u0433\u0430\u043c:<\/p>\n<ul>\n<li>\n<p>\u041f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u043c, \u0447\u0442\u043e \u043c\u044b \u043d\u0435 \u0434\u0435\u0430\u043b\u043b\u043e\u0446\u0438\u0440\u0443\u0435\u043c <code>()<\/code>, \u043d\u0443\u0436\u043d\u043e \u0434\u043b\u044f \u0434\u0435\u0431\u0430\u0433 \u0441\u0431\u043e\u0440\u043a\u0438<\/p>\n<\/li>\n<li>\n<p>\u0423\u0431\u0438\u0440\u0430\u0435\u043c \u0442\u0435\u043a\u0443\u0449\u0438\u0439 \u043e\u0431\u044a\u0435\u043a\u0442 \u0438\u0437 GC \u0441 <a href=\"https:\/\/docs.python.org\/3\/c-api\/gcsupport.html#c.PyObject_GC_UnTrack\" rel=\"noopener noreferrer nofollow\">PyObject_GC_UnTrack<\/a><\/p>\n<\/li>\n<li>\n<p>\u041d\u0430\u0447\u0438\u043d\u0430\u0435\u043c \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u044e (\u0438 \u0437\u0430\u0449\u0438\u0442\u0443 \u0441\u0442\u0435\u043a\u0430) \u043f\u0440\u0438 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u0438 \u0431\u043e\u043b\u044c\u0448\u0438\u0445 \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432 \u0441 <code>Py_TRASHCAN_BEGIN<\/code>, \u043e \u0447\u0435\u043c <a href=\"https:\/\/t.me\/opensource_findings\/881\" rel=\"noopener noreferrer nofollow\">\u044f \u043f\u0438\u0441\u0430\u043b \u043f\u043e\u0434\u0440\u043e\u0431\u043d\u043e \u0443 \u0441\u0435\u0431\u044f \u0432 \u0442\u0433 \u043a\u0430\u043d\u0430\u043b\u0435<\/a><\/p>\n<\/li>\n<li>\n<p>\u041f\u0440\u043e\u0445\u043e\u0434\u0438\u043c\u0441\u044f \u043f\u043e \u0432\u0441\u0435\u043c \u043e\u0431\u044a\u0435\u043a\u0442\u0430\u043c \u0432 <code>ob_item<\/code> \u0438 \u043d\u0435\u0449\u0430\u0434\u043d\u043e \u0434\u0435\u043a\u0440\u0435\u0444\u0438\u043c \u0438\u0445, \u0447\u0442\u043e\u0431\u044b \u0443\u043c\u0435\u043d\u044c\u0448\u0438\u0442\u044c <code>ob_refcnt<\/code> \u0438 \u043e\u0441\u0432\u043e\u0431\u043e\u0434\u0438\u0442\u044c \u0438\u0445, \u0435\u0441\u043b\u0438 \u043e\u043d\u0438 \u0431\u043e\u043b\u044c\u0448\u0435 \u043d\u0438\u0433\u0434\u0435 \u043d\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442\u0441\u044f<\/p>\n<\/li>\n<li>\n<p>\u0410 \u0434\u0430\u043b\u044c\u0448\u0435 \u2013 \u043c\u044b \u043f\u044b\u0442\u0430\u0435\u043c\u0441\u044f \u0441\u0434\u0435\u043b\u0430\u0442\u044c <code>maybe_freelist_push<\/code>, \u0438 \u0435\u0441\u043b\u0438 \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u043e\u0441\u044c, <strong>\u0442\u043e \u043c\u044b \u043d\u0435 \u0432\u044b\u0441\u0432\u043e\u0431\u043e\u0436\u0434\u0430\u0435\u043c \u043f\u0430\u043c\u044f\u0442\u044c<\/strong> \u0438 \u043d\u0435 \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u043c <code>tp_free<\/code>. \u0410 \u043e\u0441\u0432\u043e\u0431\u043e\u0436\u0434\u0430\u0435\u043c \u043f\u0430\u043c\u044f\u0442\u044c, \u0442\u043e\u043b\u044c\u043a\u043e \u043a\u043e\u0433\u0434\u0430 \u043d\u0435 \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u043e\u0441\u044c \u0437\u0430\u043f\u0443\u0448\u0438\u0442\u044c \u043d\u043e\u0432\u044b\u0439 <code>freelist<\/code>. \u0422\u0443\u0442 \u043d\u0430\u0441\u0442\u0430\u043b\u043e \u0432\u0440\u0435\u043c\u044f \u043f\u0440\u043e \u043d\u0438\u0445 \u0440\u0430\u0441\u0441\u043a\u0430\u0437\u0430\u0442\u044c!<\/p>\n<\/li>\n<\/ul>\n<p>\u0412\u043e\u043e\u0431\u0449\u0435, \u0438\u0434\u0435\u044f \u043f\u0435\u0440\u0435\u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043f\u0430\u043c\u044f\u0442\u044c \u043d\u0443\u0436\u043d\u043e\u0433\u043e \u0440\u0430\u0437\u043c\u0435\u0440\u0430 \u043f\u043e\u0434 \u043e\u0431\u044a\u0435\u043a\u0442\u044b \u043e\u0434\u0438\u043d\u0430\u043a\u043e\u0432\u043e\u0433\u043e \u0440\u0430\u0437\u043c\u0435\u0440\u0430 \u2013 \u0441\u043e\u0432\u0441\u0435\u043c \u043d\u0435 \u043d\u043e\u0432\u0430. \u0422\u0443\u0442 \u0440\u043e\u0432\u043d\u043e \u0442\u0430\u043a\u043e\u0435 \u0438 \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442. \u041f\u0440\u0438 \u0434\u0435\u0430\u043b\u043b\u043e\u043a\u0430\u0446\u0438\u0438 \u043c\u044b \u0434\u043b\u044f \u043a\u043e\u0440\u0442\u0435\u0436\u0435\u0439 \u0441 \u0440\u0430\u0437\u043c\u0435\u0440\u043e\u043c <code>&lt;20<\/code> \u043d\u0435 \u0432\u044b\u0441\u0432\u043e\u0431\u043e\u0436\u0434\u0430\u0435\u043c \u043f\u0430\u043c\u044f\u0442\u044c \u043f\u0440\u043e\u0441\u0442\u043e \u0442\u0430\u043a. \u041c\u044b \u043f\u043e\u043d\u0438\u043c\u0430\u0435\u043c, \u0447\u0442\u043e \u043a\u0442\u043e-\u0442\u043e \u0441\u043a\u043e\u0440\u043e \u0441\u043d\u043e\u0432\u0430 \u0437\u0430\u0445\u043e\u0447\u0435\u0442 \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u043a\u043e\u0440\u0442\u0435\u0436 \u0442\u0430\u043a\u043e\u0433\u043e \u0440\u0430\u0437\u043c\u0435\u0440\u0430. \u0418 \u043d\u0430\u043c \u043d\u0435 \u043d\u0443\u0436\u043d\u043e \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0434\u0435\u043b\u044f\u0442\u044c \u043f\u0430\u043c\u044f\u0442\u044c (\u0447\u0442\u043e \u0434\u043e\u043b\u0433\u043e), \u043c\u044b \u0441\u043c\u043e\u0436\u0435\u043c \u043f\u0440\u043e\u0441\u0442\u043e \u043f\u0435\u0440\u0435\u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0435 &#171;\u0436\u0434\u0443\u0449\u0438\u0435&#187; \u0443\u0447\u0430\u0441\u0442\u043a\u0438 \u043f\u0430\u043c\u044f\u0442\u0438.<\/p>\n<p>\u0421\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u2013 \u0432\u043e \u0432\u0440\u0435\u043c\u044f \u0434\u0435\u0430\u043b\u043b\u043e\u043a\u0430\u0446\u0438\u0438 \u043c\u044b \u0441\u043a\u043b\u0430\u0434\u044b\u0432\u0430\u0435\u043c \u043e\u0431\u044a\u0435\u043a\u0442\u044b \u043a\u043e\u0440\u0442\u0435\u0436\u0435\u0439 \u043d\u0443\u0436\u043d\u043e\u0433\u043e \u0440\u0430\u0437\u043c\u0435\u0440\u0430 \u0432\u043e <code>freelist<\/code>:<\/p>\n<pre><code class=\"cpp\">static inline int maybe_freelist_push(PyTupleObject *op) {     if (!Py_IS_TYPE(op, &amp;PyTuple_Type)) {         return 0;     }     Py_ssize_t index = Py_SIZE(op) - 1;     if (index &lt; PyTuple_MAXSAVESIZE) {         return _Py_FREELIST_PUSH(tuples[index], op, Py_tuple_MAXFREELIST);     }     return 0; }<\/code><\/pre>\n<p>\u0410 \u0432\u043e \u0432\u0440\u0435\u043c\u044f \u0430\u043b\u043b\u043e\u043a\u0430\u0446\u0438\u0438 \u043f\u0440\u043e\u0431\u0443\u0435\u043c \u043d\u0430\u0439\u0442\u0438 \u043d\u0443\u0436\u043d\u044b\u0439 \u043d\u0443\u0436\u043d\u043e\u0433\u043e \u0440\u0430\u0437\u043c\u0435\u0440\u0430 \u0438 \u043f\u0435\u0440\u0435\u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c. \u041f\u043e\u043c\u043d\u0438\u0442\u0435, \u044f \u043e\u0431\u0435\u0449\u0430\u043b \u043f\u043e\u0434\u0440\u043e\u0431\u043d\u0435\u0435 \u0440\u0430\u0441\u0441\u043a\u0430\u0437\u0430\u0442\u044c \u043f\u0440\u043e <code>_Py_FREELIST_POP<\/code>? \u0422\u0435\u043f\u0435\u0440\u044c \u043c\u044b \u043f\u043e\u043d\u0438\u043c\u0430\u0435\u043c, \u0437\u0430\u0447\u0435\u043c \u043e\u043d \u043d\u0443\u0436\u0435\u043d \u0432 <code>tp_alloc<\/code>:<\/p>\n<pre><code class=\"cpp\">static PyTupleObject * tuple_alloc(Py_ssize_t size) {     \/\/ ...     Py_ssize_t index = size - 1;     if (index &lt; PyTuple_MAXSAVESIZE) {  \/\/ 20         PyTupleObject *op = _Py_FREELIST_POP(PyTupleObject, tuples[index]);         if (op != NULL) {             return op;         }     }     \/\/ ... }<\/code><\/pre>\n<p>\u0415\u0441\u043b\u0438 \u043a\u043e\u0440\u0442\u0435\u0436 \u043d\u0443\u0436\u043d\u043e\u0433\u043e \u0440\u0430\u0437\u043c\u0435\u0440\u0430 \u0435\u0441\u0442\u044c \u0432 <code>_Py_FREELIST<\/code>, \u0442\u043e \u043c\u044b \u043f\u0440\u043e\u0441\u0442\u043e \u0432\u043e\u0437\u044c\u043c\u0435\u043c \u0435\u0433\u043e \u0438 \u0432\u0435\u0440\u043d\u0435\u043c. \u0411\u044b\u0441\u0442\u0440\u0435\u0435 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u043d\u043e\u0432\u044b\u0445 \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432. \u0411\u044b\u0441\u0442\u0440\u0435\u0435 \u0441\u0431\u043e\u0440\u043a\u0430 \u043d\u0435\u043d\u0443\u0436\u043d\u044b\u0445. \u041f\u0435\u0440\u0435\u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u043f\u0430\u043c\u044f\u0442\u0438.<\/p>\n<p>\u041c\u044b \u043c\u043e\u0436\u0435\u043c \u043b\u0435\u0433\u043a\u043e \u043f\u043e\u0434\u0442\u0432\u0435\u0440\u0434\u0438\u0442\u044c \u043c\u043e\u0438 \u0441\u043b\u043e\u0432\u0430 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u043f\u0440\u0438\u043c\u0435\u0440\u043e\u043c (\u0441\u043f\u0435\u0446\u0438\u0430\u043b\u044c\u043d\u043e \u0431\u0435\u0440\u0435\u043c \u0431\u043e\u043b\u044c\u0448\u043e\u0439 \u0440\u0430\u0437\u043c\u0435\u0440, \u0447\u0442\u043e\u0431\u044b \u043d\u0438\u0433\u0434\u0435 \u0432\u043d\u0443\u0442\u0440\u0438 \u0438\u043d\u0442\u0435\u0440\u043f\u0440\u0435\u0442\u0430\u0442\u043e\u0440\u0430 \u043d\u0430 \u0441\u043e\u0437\u0434\u0430\u0432\u0430\u043b\u0438\u0441\u044c &#171;\u0443\u0442\u0438\u043b\u0438\u0442\u0430\u0440\u043d\u044b\u0435&#187; \u043a\u043e\u0440\u0442\u0435\u0436\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043c\u044b \u043d\u0435 \u0443\u0432\u0438\u0434\u0438\u043c, \u043c\u0430\u043b\u043e\u0433\u043e \u0440\u0430\u0437\u043c\u0435\u0440\u0430):<\/p>\n<pre><code class=\"python\">&gt;&gt;&gt; a = tuple(range(18)) &gt;&gt;&gt; id(a) 4352063168 &gt;&gt;&gt; del a  &gt;&gt;&gt; b = tuple(range(18)) &gt;&gt;&gt; id(b)  # should be equal to `id(a)` 4352063168<\/code><\/pre>\n<p>\u0421\u043c\u043e\u0442\u0440\u0438\u0442\u0435! \u0410\u0434\u0440\u0435\u0441 \u0432 \u043f\u0430\u043c\u044f\u0442\u0438 \u0438\u043b\u0438 <code>id(b)<\/code> \u0442\u0430\u043a\u043e\u0439 \u0436\u0435 \u043a\u0430\u043a <code>id(a)<\/code>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043c\u044b \u0442\u043e\u043b\u044c\u043a\u043e \u0447\u0442\u043e \u0443\u0434\u0430\u043b\u0438\u043b\u0438. \u0422\u0435\u043f\u0435\u0440\u044c \u043c\u044b \u0437\u043d\u0430\u0435\u043c, \u043f\u043e\u0447\u0435\u043c\u0443. \u041f\u0440\u0438\u043a\u043e\u043b\u044c\u043d\u0430\u044f \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u044f?<\/p>\n<h2>\u041c\u0443\u0442\u0430\u0431\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c tuple<\/h2>\n<p>\u0412 \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043a\u0435 \u0431\u044b\u043b\u043e \u043f\u0440\u043e \u043c\u0443\u0442\u0430\u0431\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u2013 \u0433\u0434\u0435 \u043c\u0443\u0442\u0430\u0431\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c? \u041e\u0431\u044b\u0447\u043d\u043e \u043c\u044b \u0432\u0441\u0435 \u043f\u0440\u0438\u0432\u044b\u043a\u043b\u0438 \u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u043d\u0430 <code>tuple<\/code> \u043a\u0430\u043a \u043d\u0430 \u0438\u043c\u043c\u0443\u0442\u0430\u0431\u0435\u043b\u044c\u043d\u044b\u0439 \u043e\u0431\u044a\u0435\u043a\u0442; \u043d\u043e \u0432 C-API \u0432\u0441\u0435 \u043f\u043e-\u0434\u0440\u0443\u0433\u043e\u043c\u0443. \u0421\u0435\u0439\u0447\u0430\u0441 \u043f\u043e\u043a\u0430\u0436\u0443.<\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-445570","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/445570","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=445570"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/445570\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=445570"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=445570"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=445570"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}