Commit 57d24c58 authored by VIGNET Pierre's avatar VIGNET Pierre
Browse files

[lib] C api: last refactor for forward_code

parent 14ea6c1c
......@@ -286,11 +286,10 @@ static PyObject* forward_code(PyObject *self, PyObject *args, PyObject *kwds)
// SET_ITEM steals a reference, so: no Py_DECREF on items !
PyObject *lit;
PyObject *lit_cod;
long lit_val;
PyObject *lit_name;
PyObject *lit_sign;
PyObject *lit_name_last_char;
PyObject *name_prefix;
PyObject *variable_value;
PyObject *multiply_result;
#ifdef IS_PY3K
......@@ -316,6 +315,16 @@ static PyObject* forward_code(PyObject *self, PyObject *args, PyObject *kwds)
#endif
//std::cout << "name size" << name_size << std::endl;
// Get value corresponding to lit_name
// Borrowed reference
variable_value = PyDict_GetItem(var_code_table, lit_name);
if (variable_value == NULL) {
PyErr_SetString(PyExc_KeyError, "lit_name not found in dict");
return NULL;
}
// Because variable_value reference IS borrowed (owned by PyDict_GetItem)
// we don't do Py_DECREF(variable_value);
// Get and test last char of the name
lit_name_last_char = PySequence_ITEM(lit_name, name_size - 1); // new ref
// Returns -1 on error, 0 if the result is false, 1 otherwise
......@@ -323,66 +332,40 @@ static PyObject* forward_code(PyObject *self, PyObject *args, PyObject *kwds)
// If last char is '`'
// t+1 variable
// Get value corresponding to lit_name
// Borrowed reference
variable_value = PyDict_GetItem(var_code_table, lit_name);
if (variable_value == NULL) {
PyErr_SetString(PyExc_KeyError, "lit_name not found in dict");
return NULL;
}
// Add shift_step to the value corresponding to the given name
lit_cod = PyNumber_Add(variable_value, py_shift_step); // new ref
// Because variable_value reference IS borrowed (owned by PyDict_GetItem)
// we don't do Py_DECREF(variable_value);
// Note:
// Add new lit_cod (use SET_ITEM instead of PyList_Append)
// => numeric_clause is a new list there will be no leak of previously inserted items
// Append does not steal a reference, so shifted_lit refcount = 2
// after Py_DECREF, shifted_lit refcount = 1, but stored in the list so the pointer var can be reused
// SET_ITEM steals a reference, so : no Py_DECREF on items !
// Ret int 1 if lit is false, -1 on failure
if (PyObject_Not(lit_sign)) {
multiply_result = PyNumber_Multiply(lit_cod, minus_one); // new ref
PyList_SET_ITEM(numeric_clause, i, multiply_result);
Py_DECREF(lit_cod);
// Because PyList_SET_ITEM steals the reference of multiply_result, we don't do Py_DECREF(multiply_result);
} else {
PyList_SET_ITEM(numeric_clause, i, lit_cod);
// Because lit_cod is built in place (the reference is not borrowed)
// and because PyList_SET_ITEM steals the reference of lit_cod, we don't do Py_DECREF(lit_cod);
}
lit_val = PyLong_AsLong(variable_value) + shift_step;
// Ret of PyObject_Not is: 1 if lit is false, -1 on failure
#ifdef IS_PY3K
multiply_result = (PyObject_Not(lit_sign)) ? PyLong_FromLong(-lit_val) : PyLong_FromLong(lit_val); // new ref
#else
multiply_result = (PyObject_Not(lit_sign)) ? PyInt_FromLong(-lit_val) : PyInt_FromLong(lit_val); // new ref
#endif
} else {
// t variable
// Get value corresponding to lit_name
// Borrowed reference
lit_cod = PyDict_GetItem(var_code_table, lit_name);
if (lit_cod == NULL) {
PyErr_SetString(PyExc_KeyError, "lit_name not found in dict");
return NULL;
}
// Get variable_value
lit_val = PyLong_AsLong(variable_value);
// Ret int 1 if lit is false, -1 on failure
if (PyObject_Not(lit_sign)) {
multiply_result = PyNumber_Multiply(lit_cod, minus_one); // new ref
PyList_SET_ITEM(numeric_clause, i, multiply_result);
// Because PyList_SET_ITEM steals the reference of multiply_result, we don't do Py_DECREF(multiply_result);
} else {
Py_INCREF(lit_cod);
PyList_SET_ITEM(numeric_clause, i, lit_cod);
// Because lit_cod reference IS borrowed (owned by PyDict_GetItem),
// we don't do Py_DECREF(lit_cod) and because PyList_SET_ITEM steals the reference of lit_cod,
// we do an INCREF on it.
}
// Ret of PyObject_Not is: 1 if lit is false, -1 on failure
#ifdef IS_PY3K
multiply_result = (PyObject_Not(lit_sign)) ? PyLong_FromLong(-lit_val) : PyLong_FromLong(lit_val); // new ref
#else
multiply_result = (PyObject_Not(lit_sign)) ? PyInt_FromLong(-lit_val) : PyInt_FromLong(lit_val); // new ref
#endif
}
// Because PyList_SET_ITEM steals the reference of multiply_result, we don't do Py_DECREF(multiply_result);
PyList_SET_ITEM(numeric_clause, i, multiply_result);
// Note:
// Add new lit_cod (use SET_ITEM instead of PyList_Append)
// => numeric_clause is a new list there will be no leak of previously inserted items
// Append does not steal a reference, so shifted_lit refcount = 2
// after Py_DECREF, shifted_lit refcount = 1, but stored in the list so the pointer var can be reused
// SET_ITEM steals a reference, so : no Py_DECREF on items !
i++;
/* release reference when done */
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment