From da870f2461349c423a9ed38801680c8525371b65 Mon Sep 17 00:00:00 2001 From: Stefan Behnel Date: Wed, 14 May 2008 18:19:04 +0200 Subject: [PATCH] removed 'intern_names' Option - always intern identifier-like strings --- Cython/Compiler/ExprNodes.py | 130 +++++++++------------------------- Cython/Compiler/ModuleNode.py | 25 +++---- Cython/Compiler/Nodes.py | 37 +++------- Cython/Compiler/Options.py | 1 - Cython/Compiler/Symtab.py | 5 +- 5 files changed, 55 insertions(+), 143 deletions(-) diff --git a/Cython/Compiler/ExprNodes.py b/Cython/Compiler/ExprNodes.py index 109a59c6..d0598559 100644 --- a/Cython/Compiler/ExprNodes.py +++ b/Cython/Compiler/ExprNodes.py @@ -865,10 +865,7 @@ class NameNode(AtomicExprNode): self.is_temp = 0 else: self.is_temp = 1 - if Options.intern_names: - env.use_utility_code(get_name_interned_utility_code) - else: - env.use_utility_code(get_name_utility_code) + env.use_utility_code(get_name_interned_utility_code) def analyse_entry(self, env): #print "NameNode.analyse_entry:", self.name ### @@ -878,8 +875,7 @@ class NameNode(AtomicExprNode): self.type = type if entry.is_pyglobal or entry.is_builtin: assert type.is_pyobject, "Python global or builtin not a Python object" - if Options.intern_names: - self.interned_cname = self.entry.interned_cname = env.intern(self.entry.name) + self.interned_cname = self.entry.interned_cname = env.intern(self.entry.name) def check_identifier_kind(self): #print "NameNode.check_identifier_kind:", self.entry.name ### @@ -947,20 +943,12 @@ class NameNode(AtomicExprNode): namespace = Naming.builtins_cname else: # entry.is_pyglobal namespace = entry.namespace_cname - if Options.intern_names: - code.putln( - '%s = __Pyx_GetName(%s, %s); %s' % ( - self.result_code, - namespace, - self.interned_cname, - code.error_goto_if_null(self.result_code, self.pos))) - else: - code.putln( - '%s = __Pyx_GetName(%s, "%s"); %s' % ( - self.result_code, - namespace, - self.entry.name, - code.error_goto_if_null(self.result_code, self.pos))) + code.putln( + '%s = __Pyx_GetName(%s, %s); %s' % ( + self.result_code, + namespace, + self.interned_cname, + code.error_goto_if_null(self.result_code, self.pos))) elif entry.is_local and False: # control flow not good enough yet assigned = entry.scope.control_flow.get_state((entry.name, 'initalized'), self.pos) @@ -983,32 +971,17 @@ class NameNode(AtomicExprNode): if entry.is_member: # if we entry is a member we have to cheat: SetAttr does not work # on types, so we create a descriptor which is then added to tp_dict - if Options.intern_names: - code.put_error_if_neg(self.pos, - 'PyDict_SetItem(%s->tp_dict, %s, %s)' % ( - namespace, - self.interned_cname, - rhs.py_result())) - else: - code.put_error_if_neg(self.pos, - 'PyDict_SetItemString(%s->tp_dict, %s, %s)' % ( - namespace, - entry.name, - rhs.py_result())) - + code.put_error_if_neg(self.pos, + 'PyDict_SetItem(%s->tp_dict, %s, %s)' % ( + namespace, + self.interned_cname, + rhs.py_result())) else: - if Options.intern_names: - code.put_error_if_neg(self.pos, - 'PyObject_SetAttr(%s, %s, %s)' % ( - namespace, - self.interned_cname, - rhs.py_result())) - else: - code.put_error_if_neg(self.pos, - 'PyObject_SetAttrString(%s, "%s", %s)' % ( - namespace, - entry.name, - rhs.py_result())) + code.put_error_if_neg(self.pos, + 'PyObject_SetAttr(%s, %s, %s)' % ( + namespace, + self.interned_cname, + rhs.py_result())) if debug_disposal_code: print("NameNode.generate_assignment_code:") print("...generating disposal code for %s" % rhs) @@ -2016,8 +1989,7 @@ class AttributeNode(ExprNode): if obj_type.is_pyobject: self.type = py_object_type self.is_py_attr = 1 - if Options.intern_names: - self.interned_attr_cname = env.intern(self.attribute) + self.interned_attr_cname = env.intern(self.attribute) else: if not obj_type.is_error: error(self.pos, @@ -2061,36 +2033,21 @@ class AttributeNode(ExprNode): def generate_result_code(self, code): if self.is_py_attr: - if Options.intern_names: - code.putln( - '%s = PyObject_GetAttr(%s, %s); %s' % ( - self.result_code, - self.obj.py_result(), - self.interned_attr_cname, - code.error_goto_if_null(self.result_code, self.pos))) - else: - code.putln( - '%s = PyObject_GetAttrString(%s, "%s"); %s' % ( - self.result_code, - self.obj.py_result(), - self.attribute, - code.error_goto_if_null(self.result_code, self.pos))) + code.putln( + '%s = PyObject_GetAttr(%s, %s); %s' % ( + self.result_code, + self.obj.py_result(), + self.interned_attr_cname, + code.error_goto_if_null(self.result_code, self.pos))) def generate_assignment_code(self, rhs, code): self.obj.generate_evaluation_code(code) if self.is_py_attr: - if Options.intern_names: - code.put_error_if_neg(self.pos, - 'PyObject_SetAttr(%s, %s, %s)' % ( - self.obj.py_result(), - self.interned_attr_cname, - rhs.py_result())) - else: - code.put_error_if_neg(self.pos, - 'PyObject_SetAttrString(%s, "%s", %s)' % ( - self.obj.py_result(), - self.attribute, - rhs.py_result())) + code.put_error_if_neg(self.pos, + 'PyObject_SetAttr(%s, %s, %s)' % ( + self.obj.py_result(), + self.interned_attr_cname, + rhs.py_result())) rhs.generate_disposal_code(code) else: select_code = self.result_code @@ -2108,16 +2065,10 @@ class AttributeNode(ExprNode): def generate_deletion_code(self, code): self.obj.generate_evaluation_code(code) if self.is_py_attr: - if Options.intern_names: - code.put_error_if_neg(self.pos, - 'PyObject_DelAttr(%s, %s)' % ( - self.obj.py_result(), - self.interned_attr_cname)) - else: - code.put_error_if_neg(self.pos, - 'PyObject_DelAttrString(%s, "%s")' % ( - self.obj.py_result(), - self.attribute)) + code.put_error_if_neg(self.pos, + 'PyObject_DelAttr(%s, %s)' % ( + self.obj.py_result(), + self.interned_attr_cname)) else: error(self.pos, "Cannot delete C attribute of extension type") self.obj.generate_disposal_code(code) @@ -3904,19 +3855,6 @@ class CloneNode(CoercionNode): # #------------------------------------------------------------------------------------ -get_name_utility_code = [ -""" -static PyObject *__Pyx_GetName(PyObject *dict, char *name); /*proto*/ -""",""" -static PyObject *__Pyx_GetName(PyObject *dict, char *name) { - PyObject *result; - result = PyObject_GetAttrString(dict, name); - if (!result) - PyErr_SetString(PyExc_NameError, name); - return result; -} -"""] - get_name_interned_utility_code = [ """ static PyObject *__Pyx_GetName(PyObject *dict, PyObject *name); /*proto*/ diff --git a/Cython/Compiler/ModuleNode.py b/Cython/Compiler/ModuleNode.py index 1ef54be0..5528af40 100644 --- a/Cython/Compiler/ModuleNode.py +++ b/Cython/Compiler/ModuleNode.py @@ -1560,23 +1560,14 @@ class ModuleNode(Nodes.Node, Nodes.BlockNode): # Lookup and cache builtin objects. if Options.cache_builtins: for entry in env.cached_builtins: - if Options.intern_names: - #assert entry.interned_cname is not None - code.putln( - '%s = __Pyx_GetName(%s, %s); if (!%s) %s' % ( - entry.cname, - Naming.builtins_cname, - entry.interned_cname, - entry.cname, - code.error_goto(entry.pos))) - else: - code.putln( - '%s = __Pyx_GetName(%s, "%s"); if (!%s) %s' % ( - entry.cname, - Naming.builtins_cname, - entry.name, - entry.cname, - code.error_goto(entry.pos))) + #assert entry.interned_cname is not None + code.putln( + '%s = __Pyx_GetName(%s, %s); if (!%s) %s' % ( + entry.cname, + Naming.builtins_cname, + entry.interned_cname, + entry.cname, + code.error_goto(entry.pos))) def generate_global_init_code(self, env, code): # Generate code to initialise global PyObject * diff --git a/Cython/Compiler/Nodes.py b/Cython/Compiler/Nodes.py index e509ea49..e08cd219 100644 --- a/Cython/Compiler/Nodes.py +++ b/Cython/Compiler/Nodes.py @@ -1084,8 +1084,7 @@ class CFuncDefNode(FuncDefNode): self.entry.as_variable = self.py_func.entry # Reset scope entry the above cfunction env.entries[name] = self.entry - if Options.intern_names: - self.py_func.interned_attr_cname = env.intern(self.py_func.entry.name) + self.py_func.interned_attr_cname = env.intern(self.py_func.entry.name) if not env.is_module_scope or Options.lookup_module_cpdef: self.override = OverrideCheckNode(self.pos, py_func = self.py_func) self.body = StatListNode(self.pos, stats=[self.override, self.body]) @@ -1925,10 +1924,7 @@ class OverrideCheckNode(StatNode): code.putln("else if (unlikely(Py_TYPE(%s)->tp_dictoffset != 0)) {" % self_arg) err = code.error_goto_if_null(self_arg, self.pos) # need to get attribute manually--scope would return cdef method - if Options.intern_names: - code.putln("%s = PyObject_GetAttr(%s, %s); %s" % (self.func_node.result_code, self_arg, self.py_func.interned_attr_cname, err)) - else: - code.putln('%s = PyObject_GetAttrString(%s, "%s"); %s' % (self.func_node.result_code, self_arg, self.py_func.entry.name, err)) + code.putln("%s = PyObject_GetAttr(%s, %s); %s" % (self.func_node.result_code, self_arg, self.py_func.interned_attr_cname, err)) # It appears that this type is not anywhere exposed in the Python/C API is_builtin_function_or_method = '(strcmp(Py_TYPE(%s)->tp_name, "builtin_function_or_method") == 0)' % self.func_node.result_code is_overridden = '(PyCFunction_GET_FUNCTION(%s) != (void *)&%s)' % (self.func_node.result_code, self.py_func.entry.func_cname) @@ -3683,8 +3679,7 @@ class FromImportStatNode(StatNode): self.item.allocate_temp(env) self.interned_items = [] for name, target in self.items: - if Options.intern_names: - self.interned_items.append((env.intern(name), target)) + self.interned_items.append((env.intern(name), target)) target.analyse_target_expression(env, None) #target.release_target_temp(env) # was release_temp ?!? self.module.release_temp(env) @@ -3692,24 +3687,14 @@ class FromImportStatNode(StatNode): def generate_execution_code(self, code): self.module.generate_evaluation_code(code) - if Options.intern_names: - for cname, target in self.interned_items: - code.putln( - '%s = PyObject_GetAttr(%s, %s); %s' % ( - self.item.result_code, - self.module.py_result(), - cname, - code.error_goto_if_null(self.item.result_code, self.pos))) - target.generate_assignment_code(self.item, code) - else: - for name, target in self.items: - code.putln( - '%s = PyObject_GetAttrString(%s, "%s"); %s' % ( - self.item.result_code, - self.module.py_result(), - name, - code.error_goto_if_null(self.item.result_code, self.pos))) - target.generate_assignment_code(self.item, code) + for cname, target in self.interned_items: + code.putln( + '%s = PyObject_GetAttr(%s, %s); %s' % ( + self.item.result_code, + self.module.py_result(), + cname, + code.error_goto_if_null(self.item.result_code, self.pos))) + target.generate_assignment_code(self.item, code) self.module.generate_disposal_code(code) #------------------------------------------------------------------------------------ diff --git a/Cython/Compiler/Options.py b/Cython/Compiler/Options.py index 2f9a7e43..600864c1 100644 --- a/Cython/Compiler/Options.py +++ b/Cython/Compiler/Options.py @@ -2,7 +2,6 @@ # Pyrex - Compilation-wide options # -intern_names = 1 # Intern global variable and attribute names cache_builtins = 1 # Perform lookups on builtin names only once embed_pos_in_docstring = 0 diff --git a/Cython/Compiler/Symtab.py b/Cython/Compiler/Symtab.py index 0c023ad4..a0beb711 100644 --- a/Cython/Compiler/Symtab.py +++ b/Cython/Compiler/Symtab.py @@ -1238,10 +1238,9 @@ class CClassScope(ClassScope): # I keep it in for now. is_member should be enough # later on entry.namespace_cname = "(PyObject *)%s" % self.parent_type.typeptr_cname - if Options.intern_names: - entry.interned_cname = self.intern(name) + entry.interned_cname = self.intern(name) return entry - + def declare_pyfunction(self, name, pos): # Add an entry for a method. -- 2.26.2