Merge remote branch 'upstream/master'
[cython.git] / Cython / Compiler / CythonScope.py
1 from Symtab import ModuleScope
2 from PyrexTypes import *
3
4 shape_func_type = CFuncType(
5     c_ptr_type(c_py_ssize_t_type),
6     [CFuncTypeArg("buffer", py_object_type, None)])
7
8 class CythonScope(ModuleScope):
9     def __init__(self, context):
10         ModuleScope.__init__(self, u'cython', None, context)
11         self.pxd_file_loaded = True
12
13         self.shape_entry = self.declare_cfunction('shape',
14                                                   shape_func_type,
15                                                   pos=None,
16                                                   defining = 1,
17                                                   cname='<error>')
18
19     def lookup_type(self, name):
20         # This function should go away when types are all first-level objects.
21         type = parse_basic_type(name)
22         if type:
23             return type
24
25 def create_cython_scope(context):
26     create_utility_scope(context)
27     return CythonScope(context)
28
29
30 def create_utility_scope(context):
31     global utility_scope
32     utility_scope = ModuleScope(u'utility', None, context)
33
34     # These are used to optimize isinstance in FinalOptimizePhase
35     type_object = utility_scope.declare_typedef('PyTypeObject',
36                                                 base_type = c_void_type,
37                                                 pos = None,
38                                                 cname = 'PyTypeObject')
39     type_object.is_void = True
40
41     utility_scope.declare_cfunction(
42                 'PyObject_TypeCheck',
43                 CFuncType(c_bint_type, [CFuncTypeArg("o", py_object_type, None),
44                                         CFuncTypeArg("t", c_ptr_type(type_object), None)]),
45                 pos = None,
46                 defining = 1,
47                 cname = 'PyObject_TypeCheck')
48
49     return utility_scope