def generate_assignment_code(self, rhs, code):
self.generate_subexpr_evaluation_code(code)
if self.type.is_pyobject:
- code.putln(
- "if (PyObject_SetItem(%s, %s, %s) < 0) %s" % (
- self.base.py_result(),
- self.index.py_result(),
- rhs.py_result(),
- code.error_goto(self.pos)))
+ if self.index.type.is_int:
+ code.putln("if (PyList_CheckExact(%s) && 0 <= %s && %s < PyList_GET_SIZE(%s)) {" % (
+ self.base.py_result(),
+ self.index.result_code,
+ self.index.result_code,
+ self.base.py_result()))
+ code.putln("Py_DECREF(PyList_GET_ITEM(%s, %s)); Py_INCREF(%s);" % (
+ self.base.py_result(),
+ self.index.result_code,
+ rhs.py_result()))
+ code.putln("PyList_SET_ITEM(%s, %s, %s);" % (
+ self.base.py_result(),
+ self.index.result_code,
+ rhs.py_result()))
+ code.putln("} else {")
+ self.generate_generic_assignment_code(rhs, code)
+ code.putln("}")
+ else:
+ self.generate_generic_assignment_code(rhs, code)
- self.generate_subexpr_disposal_code(code)
else:
code.putln(
"%s = %s;" % (
self.result_code, rhs.result_code))
+ self.generate_subexpr_disposal_code(code)
rhs.generate_disposal_code(code)
+ def generate_generic_assignment_code(self, rhs, code):
+ self.py_index.generate_result_code(code)
+ code.putln(
+ "if (PyObject_SetItem(%s, %s, %s) < 0) %s" % (
+ self.base.py_result(),
+ self.py_index.py_result(),
+ rhs.py_result(),
+ code.error_goto(self.pos)))
+ if self.is_temp:
+ self.py_index.generate_disposal_code(code)
+
def generate_deletion_code(self, code):
self.generate_subexpr_evaluation_code(code)
+ self.py_index.generate_evaluation_code(code)
code.putln(
"if (PyObject_DelItem(%s, %s) < 0) %s" % (
self.base.py_result(),
PyErr_SetString(PyExc_ValueError, "unpack sequence of wrong size");
}
- PyObject *item;
- if (!(item = PySequence_GetItem(seq, i))) {
- if (PyErr_ExceptionMatches(PyExc_IndexError))
- __Pyx_UnpackError();
- }
- return item;
- }
+static PyObject *__Pyx_UnpackItem(PyObject *seq, Py_ssize_t i) {
-}
+ static PyObject *__Pyx_UnpackItem(PyObject *iter) {
+ PyObject *item;
+ if (!(item = PyIter_Next(iter))) {
+ if (!PyErr_Occurred())
+ __Pyx_UnpackError();
+ }
+ return item;
- PyObject *item;
- if (item = PySequence_GetItem(seq, i)) {
- Py_DECREF(item);
- __Pyx_UnpackError();
- return -1;
- }
- PyErr_Clear();
- return 0;
- }
- """
+static int __Pyx_EndUnpack(PyObject *seq, Py_ssize_t i) {
-}
+ static int __Pyx_EndUnpack(PyObject *iter) {
+ PyObject *item;
+ if ((item = PyIter_Next(iter))) {
+ Py_DECREF(item);
+ __Pyx_UnpackError();
+ return -1;
+ }
+ else if (!PyErr_Occurred())
+ return 0;
+ else
+ return -1;
+ """]
#------------------------------------------------------------------------------------
# Pyrex - Parse tree nodes
#
- import os, string, sys, time
+ import string, sys
import Code
-from Errors import error, InternalError
+from Errors import error, warning, InternalError
import Naming
import PyrexTypes
from PyrexTypes import py_object_type, error_type, CTypedefType
ExprNodes.CloneNode(c_loopvar_node).coerce_to_pyobject(env)
self.bound1.allocate_temps(env)
self.bound2.allocate_temps(env)
- if self.py_loopvar_node:
+ if self.step is not None:
+ self.step.allocate_temps(env)
+ if self.is_py_target:
self.py_loopvar_node.allocate_temps(env)
- self.target.allocate_target_temps(env)
- self.target.release_target_temp(env)
- if self.py_loopvar_node:
- self.py_loopvar_node.release_temp(env)
+ self.target.allocate_target_temps(env, self.py_loopvar_node)
+ #self.target.release_target_temp(env)
+ #self.py_loopvar_node.release_temp(env)
self.body.analyse_expressions(env)
- if self.py_loopvar_node:
+ if self.is_py_target:
c_loopvar_node.release_temp(env)
if self.else_clause:
self.else_clause.analyse_expressions(env)
self.bound1.release_temp(env)
self.bound2.release_temp(env)
- #env.recycle_pending_temps() # TEMPORARY
+ if self.step is not None:
+ self.step.release_temp(env)
def generate_execution_code(self, code):
old_loop_labels = code.new_loop_labels()
"""
typedef struct {PyObject **p; char *s;} __Pyx_InternTabEntry; /*proto*/
typedef struct {PyObject **p; char *s; long n;} __Pyx_StringTabEntry; /*proto*/
- static PyObject *__Pyx_UnpackItem(PyObject *, Py_ssize_t); /*proto*/
- static int __Pyx_EndUnpack(PyObject *, Py_ssize_t); /*proto*/
- static int __Pyx_PrintItem(PyObject *); /*proto*/
- static int __Pyx_PrintNewline(void); /*proto*/
- static void __Pyx_Raise(PyObject *type, PyObject *value, PyObject *tb); /*proto*/
- static void __Pyx_ReRaise(void); /*proto*/
- static PyObject *__Pyx_Import(PyObject *name, PyObject *from_list); /*proto*/
- static PyObject *__Pyx_GetExcValue(void); /*proto*/
- static int __Pyx_ArgTypeTest(PyObject *obj, PyTypeObject *type, int none_allowed, char *name); /*proto*/
- static int __Pyx_TypeTest(PyObject *obj, PyTypeObject *type); /*proto*/
- static int __Pyx_GetStarArgs(PyObject **args, PyObject **kwds,\
- char *kwd_list[], Py_ssize_t nargs, PyObject **args2, PyObject **kwds2); /*proto*/
- static void __Pyx_WriteUnraisable(char *name); /*proto*/
- static void __Pyx_AddTraceback(char *funcname); /*proto*/
- static PyTypeObject *__Pyx_ImportType(char *module_name, char *class_name, long size); /*proto*/
- static int __Pyx_SetVtable(PyObject *dict, void *vtable); /*proto*/
- static int __Pyx_GetVtable(PyObject *dict, void *vtabptr); /*proto*/
- static PyObject *__Pyx_CreateClass(PyObject *bases, PyObject *dict, PyObject *name, char *modname); /*proto*/
- static int __Pyx_InternStrings(__Pyx_InternTabEntry *t); /*proto*/
- static int __Pyx_InitStrings(__Pyx_StringTabEntry *t); /*proto*/
+
+#DEFINE __Pyx_PyBool_FromLong(b) ((b) ? (Py_INCREF(Py_True), Py_True) : (Py_INCREF(Py_False), Py_False))
+#DEFINE __Pyx_PyObject_IsTrue(x) ({PyObject *_x = (x); _x == Py_True ? 1 : (_x) == Py_False ? 0 : PyObject_IsTrue(_x)})
"""
- get_name_predeclaration = \
- "static PyObject *__Pyx_GetName(PyObject *dict, char *name); /*proto*/"
+ #get_name_predeclaration = \
+ #"static PyObject *__Pyx_GetName(PyObject *dict, char *name); /*proto*/"
- get_name_interned_predeclaration = \
- "static PyObject *__Pyx_GetName(PyObject *dict, PyObject *name); /*proto*/"
+ #get_name_interned_predeclaration = \
+ #"static PyObject *__Pyx_GetName(PyObject *dict, PyObject *name); /*proto*/"
#------------------------------------------------------------------------------------
Py_INCREF(type);
Py_DECREF(tmp);
}
- if (PyString_Check(type)) {
- if (PyErr_Warn(PyExc_DeprecationWarning,
- "raising a string exception is deprecated"))
- goto raise_error;
- }
+ if (PyString_Check(type))
+ ;
+/* else if (PyClass_Check(type)) */
else if (PyType_Check(type) || PyClass_Check(type))
; /*PyErr_NormalizeException(&type, &value, &tb);*/
- else if (PyInstance_Check(type)) {
+ else {
/* Raising an instance. The value should be a dummy. */
if (value != Py_None) {
PyErr_SetString(PyExc_TypeError,
- "instance exception may not have a separate value");
+ "instance exception may not have a separate value");
goto raise_error;
}
- else {
- /* Normalize to raise <class>, <instance> */
- Py_DECREF(value);
- value = type;
+ /* Normalize to raise <class>, <instance> */
+ Py_DECREF(value);
+ value = type;
+ if (PyInstance_Check(type))
type = (PyObject*) ((PyInstanceObject*)type)->in_class;
- Py_INCREF(type);
- }
+ else
+ type = (PyObject*) type->ob_type;
+ Py_INCREF(type);
}
- else {
- /* Not something you can raise. You get an exception
- anyway, just not what you specified :-) */
- PyErr_Format(PyExc_TypeError,
- "exceptions must be strings, classes, or "
- "instances, not %s", type->ob_type->tp_name);
- goto raise_error;
- PyErr_Restore(type, value, tb);
- return;
-raise_error:
- Py_XDECREF(value);
++ if (PyString_Check(type)) {
++ if (PyErr_Warn(PyExc_DeprecationWarning,
++ "raising a string exception is deprecated"))
++ goto raise_error;
+ }
- PyErr_Restore(type, value, tb);
- return;
- raise_error:
- Py_XDECREF(value);
++ else if (PyType_Check(type) || PyClass_Check(type))
Py_XDECREF(type);
Py_XDECREF(tb);
return;
bad:
Py_XDECREF(args1);
Py_XDECREF(kwds1);
- if (*args2) {
- Py_XDECREF(*args2);
- }
- if (*kwds2) {
- Py_XDECREF(*kwds2);
- }
+ Py_XDECREF(*args2);
+ Py_XDECREF(*kwds2);
return -1;
}
- """
+ """]
#------------------------------------------------------------------------------------
if (!ctx)
ctx = Py_None;
PyErr_WriteUnraisable(ctx);
--}
- """
-"""]
++ if (*args2) {
++ Py_XDECREF(*args2);
++ }
++ if (*kwds2) {
++ Py_XDECREF(*kwds2);
++ }
#------------------------------------------------------------------------------------
def __init__(self, rank, signed, pymemberdef_typecode = None, is_returncode = 0):
CNumericType.__init__(self, rank, signed, pymemberdef_typecode)
self.is_returncode = is_returncode
+
+ def assignable_from_resolved_type(self, src_type):
+ return src_type.is_int or src_type.is_enum or src_type is error_type
+
+
+class CBIntType(CIntType):
+
+ # TODO: this should be a macro "(__ ? Py_True : Py_False)"
+ # and no error checking should be needed (just an incref).
+ to_py_function = "__Pyx_PyBool_FromLong"
+ from_py_function = "__Pyx_PyObject_IsTrue"
+
+
+class CPySSizeTType(CIntType):
+
+ to_py_function = "PyInt_FromSsize_t"
+ from_py_function = "PyInt_AsSsize_t"
class CUIntType(CIntType):
c_int_type = CIntType(2, 1, "T_INT")
c_long_type = CIntType(3, 1, "T_LONG")
c_longlong_type = CLongLongType(4, 1, "T_LONGLONG")
-
+c_py_ssize_t_type = CPySSizeTType(5, 1)
+c_bint_type = CBIntType(2, 1, "T_INT")
c_uchar_type = CIntType(0, 0, "T_UBYTE")
c_ushort_type = CIntType(1, 0, "T_USHORT")