Nel codice sorgente Python, il metodo di creazione dell'oggetto int PyInt_FromLong
, python crea un nuovo oggetto PyIntObject nella posizione del primo elemento di free_list che punta a. Ecco il codice:Come funziona il codice sorgente Python "free_list = (PyIntObject *) Py_TYPE (v);" sposta il puntatore free_list sull'oggetto successivo?
PyObject *
PyInt_FromLong(long ival)
{
register PyIntObject *v;
#if NSMALLNEGINTS + NSMALLPOSINTS > 0
if (-NSMALLNEGINTS <= ival && ival < NSMALLPOSINTS) {
v = small_ints[ival + NSMALLNEGINTS];
Py_INCREF(v);
return (PyObject *) v;
}
#endif
if (free_list == NULL) {
if ((free_list = fill_free_list()) == NULL)
return NULL;
}
/* Inline PyObject_New */
v = free_list;
free_list = (PyIntObject *)Py_TYPE(v);
PyObject_INIT(v, &PyInt_Type);
v->ob_ival = ival;
return (PyObject *) v;
}
e Py_TYPE è:
#define Py_TYPE(ob) (((PyObject*)(ob))->ob_type)
Come funziona free_list = (PyIntObject *)Py_TYPE(v);
lavoro?
Sposta free_list per puntare all'oggetto successivo nell'elenco.
Penso che Py_TYPE(v)
restituirà PyInt_Type
, quindi (PyIntObject *)PyInt_Type
non sarà l'oggetto successivo.
thx, haha, hanno bisogno di più attenzione da leggere il codice, grazie ancora. – weidwonder
Sei sempre il benvenuto! – soon