self.func_type, self.sig = func_type, sig
self.utility_code = utility_code
+class BuiltinAttribute(object):
+ def __init__(self, py_name, cname=None, field_type=None, field_type_name=None):
+ self.py_name = py_name
+ self.cname = cname or py_name
+ self.field_type_name = field_type_name # can't do the lookup before the type is declared!
+ self.field_type = field_type
+
+ def declare_in_type(self, self_type):
+ if self.field_type_name is not None:
+ # lazy type lookup
+ field_type = builtin_scope.lookup(self.field_type_name).type
+ else:
+ field_type = self.field_type or PyrexTypes.py_object_type
+ entry = self_type.scope.declare(self.py_name, self.cname, field_type, None, 'private')
+ entry.is_variable = True
+
class BuiltinFunction(_BuiltinOverride):
def declare_in_scope(self, scope):
func_type, sig = self.func_type, self.sig
("long", "PyLong_Type", []),
("float", "PyFloat_Type", []),
-# Until we have a way to access attributes of a type,
-# we don't want to make this one builtin.
-# ("complex", "PyComplex_Type", []),
+ ("complex", "PyComplex_Type", [BuiltinAttribute('cval', field_type_name = 'Py_complex'),
+ BuiltinAttribute('real', 'cval.real', field_type = PyrexTypes.c_double_type),
+ BuiltinAttribute('imag', 'cval.imag', field_type = PyrexTypes.c_double_type),
+ ]),
("bytes", "PyBytes_Type", []),
("str", "PyString_Type", []),
BuiltinMethod("values","T", "O", "PyDict_Values"), # FIXME: Py3 mode?
BuiltinMethod("copy", "T", "T", "PyDict_Copy")]),
- ("slice", "PySlice_Type", []),
+ ("slice", "PySlice_Type", [BuiltinAttribute('start'),
+ BuiltinAttribute('stop'),
+ BuiltinAttribute('step'),
+ ]),
# ("file", "PyFile_Type", []), # not in Py3
("set", "PySet_Type", [BuiltinMethod("clear", "T", "i", "PySet_Clear"),
("strides", PyrexTypes.c_py_ssize_t_ptr_type),
("suboffsets", PyrexTypes.c_py_ssize_t_ptr_type),
("internal", PyrexTypes.c_void_ptr_type),
+ ]),
+ ('Py_complex', 'Py_complex',
+ [('real', PyrexTypes.c_double_type),
+ ('imag', PyrexTypes.c_double_type),
])
]
global builtin_types
for name, cname, methods in builtin_types_table:
utility = builtin_utility_code.get(name)
- the_type = builtin_scope.declare_builtin_type(name, cname, utility)
+ if name == 'frozenset':
+ objstruct_cname = 'PySetObject'
+ elif name == 'bool':
+ objstruct_cname = None
+ else:
+ objstruct_cname = 'Py%sObject' % name.capitalize()
+ the_type = builtin_scope.declare_builtin_type(name, cname, utility, objstruct_cname)
builtin_types[name] = the_type
for method in methods:
method.declare_in_type(the_type)
name, "struct", scope, 1, None, cname = cname)
def init_builtins():
+ init_builtin_structs()
init_builtin_funcs()
init_builtin_types()
- init_builtin_structs()
global list_type, tuple_type, dict_type, set_type, frozenset_type
global bytes_type, str_type, unicode_type
global float_type, bool_type, type_type, complex_type