Commit cc786153 authored by Stefan Behnel's avatar Stefan Behnel

prevent utility code C-#include externalisation when it contains PYIDENT() place holders

parent 785da0f1
...@@ -607,7 +607,7 @@ class GetAndReleaseBufferUtilityCode(object): ...@@ -607,7 +607,7 @@ class GetAndReleaseBufferUtilityCode(object):
proto = util_code.format_code(util_code.proto) proto = util_code.format_code(util_code.proto)
impl = util_code.format_code( impl = util_code.format_code(
util_code.inject_string_constants(util_code.impl, output)) util_code.inject_string_constants(util_code.impl, output)[1])
proto_code.putln(proto) proto_code.putln(proto)
code.putln(impl) code.putln(impl)
......
...@@ -385,11 +385,13 @@ class UtilityCode(UtilityCodeBase): ...@@ -385,11 +385,13 @@ class UtilityCode(UtilityCodeBase):
"""Replace 'PYIDENT("xyz")' by a constant Python identifier cname. """Replace 'PYIDENT("xyz")' by a constant Python identifier cname.
""" """
pystrings = re.findall('(PYIDENT\("([^"]+)"\))', impl) pystrings = re.findall('(PYIDENT\("([^"]+)"\))', impl)
any_replacements = False
for ref, name in pystrings: for ref, name in pystrings:
py_const = output.get_interned_identifier( py_const = output.get_interned_identifier(
StringEncoding.EncodedString(name)) StringEncoding.EncodedString(name))
any_replacements = True
impl = impl.replace(ref, py_const.cname) impl = impl.replace(ref, py_const.cname)
return impl return any_replacements, impl
def put_code(self, output): def put_code(self, output):
if self.requires: if self.requires:
...@@ -400,10 +402,14 @@ class UtilityCode(UtilityCodeBase): ...@@ -400,10 +402,14 @@ class UtilityCode(UtilityCodeBase):
self.format_code(self.proto), self.format_code(self.proto),
'%s_proto' % self.name) '%s_proto' % self.name)
if self.impl: if self.impl:
output['utility_code_def'].put_or_include( impl = self.format_code(self.impl)
self.format_code( is_specialised, impl = self.inject_string_constants(impl, output)
self.inject_string_constants(self.impl, output)), if not is_specialised:
'%s_impl' % self.name) # no module specific adaptations => can be reused
output['utility_code_def'].put_or_include(
impl, '%s_impl' % self.name)
else:
output['utility_code_def'].put(impl)
if self.init: if self.init:
writer = output['init_globals'] writer = output['init_globals']
writer.putln("/* %s.init */" % self.name) writer.putln("/* %s.init */" % self.name)
...@@ -1530,12 +1536,19 @@ class CCodeWriter(object): ...@@ -1530,12 +1536,19 @@ class CCodeWriter(object):
def put_or_include(self, code, name): def put_or_include(self, code, name):
if code: if code:
if self.globalstate.common_utility_include_dir and len(code) > 1042: include_dir = self.globalstate.common_utility_include_dir
include_file = "%s_%s.h" % (name, hashlib.md5(code).hexdigest()) if include_dir and len(code) > 1042:
path = os.path.join(self.globalstate.common_utility_include_dir, include_file) include_file = "%s_%s.h" % (
name, hashlib.md5(code).hexdigest())
path = os.path.join(
include_dir, include_file)
if not os.path.exists(path): if not os.path.exists(path):
tmp_path = '%s.tmp%s' % (path, os.getpid()) tmp_path = '%s.tmp%s' % (path, os.getpid())
open(tmp_path, 'w').write(code) f = Utils.open_new_file(tmp_path)
try:
f.write(code)
finally:
f.close()
os.rename(tmp_path, path) os.rename(tmp_path, path)
code = '#include "%s"\n' % path code = '#include "%s"\n' % path
self.put(code) self.put(code)
......
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