Slide 19
Slide 19 text
/* CPython 2.7, Python/ceval.c */
PyObject *
PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
{
/* ... */
for (;;) {
/* ... */
switch (opcode) {
/* ... */
case BINARY_ADD:
w = POP(); v = TOP();
if (PyInt_CheckExact(v)
&& PyInt_CheckExact(w)) {
/* INLINE: int + int */
register long a, b, i;
a = PyInt_AS_LONG(v);
b = PyInt_AS_LONG(w);
i = (long)((unsigned long)a + b);
if ((i^a) < 0 && (i^b) < 0)
goto slow_add;
x = PyInt_FromLong(i);
}
else if (PyString_CheckExact(v) &&
PyString_CheckExact(w)) {
x = string_concatenate(v, w, f,
next_instr);
goto skip_decref_vx;
}
else {
slow_add:
x = PyNumber_Add(v, w);
}
/* Objects/abstract.c */
PyObject *
PyNumber_Add(PyObject *v, PyObject *w)
{
PyObject *result = binary_op1(v, w,
NB_SLOT(nb_add));
if (result == Py_NotImplemented) {
PySequenceMethods *m =
v->ob_type->tp_as_sequence;
Py_DECREF(result);
if (m && m->sq_concat) {
return (*m->sq_concat)(v, w);
}
result = binop_type_error(v, w, "+");
}
return result;
}
15 / 59