Commit 9257aee9 authored by Stefan Behnel's avatar Stefan Behnel

fixes: generate constant declarations before we access them, write cleanup code as before

parent c4ba39e1
...@@ -221,6 +221,7 @@ class ModuleNode(Nodes.Node, Nodes.BlockNode): ...@@ -221,6 +221,7 @@ class ModuleNode(Nodes.Node, Nodes.BlockNode):
code.putln("/* Implementation of %s */" % env.qualified_name) code.putln("/* Implementation of %s */" % env.qualified_name)
self.generate_const_definitions(env, code) self.generate_const_definitions(env, code)
self.generate_interned_num_decls(env, code) self.generate_interned_num_decls(env, code)
self.generate_interned_string_decls(env, code)
self.generate_py_string_decls(env, code) self.generate_py_string_decls(env, code)
self.generate_cached_builtins_decls(env, code) self.generate_cached_builtins_decls(env, code)
self.body.generate_function_definitions(env, code, options.transforms) self.body.generate_function_definitions(env, code, options.transforms)
...@@ -1365,11 +1366,6 @@ class ModuleNode(Nodes.Node, Nodes.BlockNode): ...@@ -1365,11 +1366,6 @@ class ModuleNode(Nodes.Node, Nodes.BlockNode):
entries = env.all_pystring_entries entries = env.all_pystring_entries
if entries: if entries:
code.putln("") code.putln("")
for entry in entries:
if entry.is_interned:
code.putln('static char %s[] = "%s";' % (
entry.cname, entry.init))
code.putln("")
code.putln( code.putln(
"static __Pyx_StringTabEntry %s[] = {" % "static __Pyx_StringTabEntry %s[] = {" %
Naming.stringtab_cname) Naming.stringtab_cname)
...@@ -1476,9 +1472,11 @@ class ModuleNode(Nodes.Node, Nodes.BlockNode): ...@@ -1476,9 +1472,11 @@ class ModuleNode(Nodes.Node, Nodes.BlockNode):
code.putln("/*--- Intern cleanup code ---*/") code.putln("/*--- Intern cleanup code ---*/")
for entry in env.pynum_entries: for entry in env.pynum_entries:
code.put_var_decref_clear(entry) code.put_var_decref_clear(entry)
if env.intern_map: if env.all_pystring_entries:
for name, cname in env.intern_map.items(): for entry in env.all_pystring_entries:
code.put_decref_clear(cname, PyrexTypes.py_object_type) if entry.is_interned:
code.put_decref_clear(
entry.pystring_cname, PyrexTypes.py_object_type)
code.putln("Py_INCREF(Py_None); return Py_None;") code.putln("Py_INCREF(Py_None); return Py_None;")
code.putln('}') code.putln('}')
......
...@@ -230,7 +230,18 @@ class BlockNode: ...@@ -230,7 +230,18 @@ class BlockNode:
for entry in env.const_entries: for entry in env.const_entries:
if not entry.is_interned: if not entry.is_interned:
code.put_var_declaration(entry, static = 1) code.put_var_declaration(entry, static = 1)
def generate_interned_string_decls(self, env, code):
entries = env.global_scope().new_interned_string_entries
if entries:
code.putln("")
for entry in entries:
code.put_var_declaration(entry, static = 1)
for entry in entries:
code.putln(
"static PyObject *%s;" % entry.pystring_cname)
del entries[:]
def generate_py_string_decls(self, env, code): def generate_py_string_decls(self, env, code):
entries = env.pystring_entries entries = env.pystring_entries
if entries: if entries:
...@@ -238,7 +249,7 @@ class BlockNode: ...@@ -238,7 +249,7 @@ class BlockNode:
for entry in entries: for entry in entries:
code.putln( code.putln(
"static PyObject *%s;" % entry.pystring_cname) "static PyObject *%s;" % entry.pystring_cname)
def generate_interned_num_decls(self, env, code): def generate_interned_num_decls(self, env, code):
# Flush accumulated interned nums from the global scope # Flush accumulated interned nums from the global scope
# and generate declarations for them. # and generate declarations for them.
...@@ -865,6 +876,7 @@ class FuncDefNode(StatNode, BlockNode): ...@@ -865,6 +876,7 @@ class FuncDefNode(StatNode, BlockNode):
# if we supported them, which we probably won't. # if we supported them, which we probably won't.
# ----- Top-level constants used by this function # ----- Top-level constants used by this function
self.generate_interned_num_decls(lenv, code) self.generate_interned_num_decls(lenv, code)
self.generate_interned_string_decls(lenv, code)
self.generate_py_string_decls(lenv, code) self.generate_py_string_decls(lenv, code)
self.generate_cached_builtins_decls(lenv, code) self.generate_cached_builtins_decls(lenv, code)
#code.putln("") #code.putln("")
......
...@@ -461,13 +461,15 @@ class Scope: ...@@ -461,13 +461,15 @@ class Scope:
# a string literal, and add it to the list of Python strings to # a string literal, and add it to the list of Python strings to
# be created at module init time. If the string resembles a # be created at module init time. If the string resembles a
# Python identifier, it will be interned. # Python identifier, it will be interned.
if not entry.pystring_cname: if entry.pystring_cname:
value = entry.init return
if possible_identifier(value): value = entry.init
entry.is_interned = 1 entry.pystring_cname = entry.cname + "p"
entry.pystring_cname = entry.cname + "p" self.pystring_entries.append(entry)
self.pystring_entries.append(entry) self.global_scope().all_pystring_entries.append(entry)
self.global_scope().all_pystring_entries.append(entry) if possible_identifier(value):
entry.is_interned = 1
self.global_scope().new_interned_string_entries.append(entry)
def add_py_num(self, value): def add_py_num(self, value):
# Add an entry for an int constant. # Add an entry for an int constant.
...@@ -673,7 +675,7 @@ class ModuleScope(Scope): ...@@ -673,7 +675,7 @@ class ModuleScope(Scope):
# type_names {string : 1} Set of type names (used during parsing) # type_names {string : 1} Set of type names (used during parsing)
# pxd_file_loaded boolean Corresponding .pxd file has been processed # pxd_file_loaded boolean Corresponding .pxd file has been processed
# cimported_modules [ModuleScope] Modules imported with cimport # cimported_modules [ModuleScope] Modules imported with cimport
# intern_map {string : string} Mapping from Python names to interned strs # new_interned_string_entries [Entry] New interned strings waiting to be declared
# interned_nums [int/long] Interned numeric constants # interned_nums [int/long] Interned numeric constants
# all_pystring_entries [Entry] Python string consts from all scopes # all_pystring_entries [Entry] Python string consts from all scopes
# types_imported {PyrexType : 1} Set of types for which import code generated # types_imported {PyrexType : 1} Set of types for which import code generated
...@@ -700,7 +702,7 @@ class ModuleScope(Scope): ...@@ -700,7 +702,7 @@ class ModuleScope(Scope):
self.type_names = dict(outer_scope.type_names) self.type_names = dict(outer_scope.type_names)
self.pxd_file_loaded = 0 self.pxd_file_loaded = 0
self.cimported_modules = [] self.cimported_modules = []
self.intern_map = {} self.new_interned_string_entries = []
self.interned_nums = [] self.interned_nums = []
self.interned_objs = [] self.interned_objs = []
self.all_pystring_entries = [] self.all_pystring_entries = []
...@@ -739,7 +741,7 @@ class ModuleScope(Scope): ...@@ -739,7 +741,7 @@ class ModuleScope(Scope):
return entry return entry
def intern(self, name): def intern(self, name):
string_entry = self.add_string_const(name) string_entry = self.get_string_const(name)
self.add_py_string(string_entry) self.add_py_string(string_entry)
return string_entry.pystring_cname return string_entry.pystring_cname
......
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