diff options
author | Stefan Behnel <stefan_ml@behnel.de> | 2021-05-20 20:34:09 +0200 |
---|---|---|
committer | Stefan Behnel <stefan_ml@behnel.de> | 2021-05-20 20:34:09 +0200 |
commit | f7f445cbda02765a0a587e88a96dd2378189a9f6 (patch) | |
tree | 9ad137fb327c7c0792ec036413769485bb17d752 /Cython/Compiler/ModuleNode.py | |
parent | 83020d996733b5376cfa5814ca81c8f823029346 (diff) | |
download | cython-f7f445cbda02765a0a587e88a96dd2378189a9f6.tar.gz |
Include .h and _api.h files in target overwrite check.
See https://github.com/cython/cython/issues/4177
See https://github.com/cython/cython/pull/4178
Diffstat (limited to 'Cython/Compiler/ModuleNode.py')
-rw-r--r-- | Cython/Compiler/ModuleNode.py | 22 |
1 files changed, 15 insertions, 7 deletions
diff --git a/Cython/Compiler/ModuleNode.py b/Cython/Compiler/ModuleNode.py index 3a59c4be5..4ddbe76ce 100644 --- a/Cython/Compiler/ModuleNode.py +++ b/Cython/Compiler/ModuleNode.py @@ -179,6 +179,14 @@ class ModuleNode(Nodes.Node, Nodes.BlockNode): return 1 return 0 + def assure_safe_target(self, path, allow_failed=False): + # Check for a common gotcha for new users: naming your .pyx file after the .c file you want to wrap + if not is_cython_generated_file(path, allow_failed=allow_failed, if_not_found=True): + # Raising a fatal CompileError instead of calling error() to prevent castrating an existing file. + raise CompileError( + self.pos, 'The output file already exists and does not look like it was generated by Cython: "%s"' % + os.path.basename(path)) + def generate_h_code(self, env, options, result): def h_entries(entries, api=0, pxd=0): return [entry for entry in entries @@ -189,8 +197,11 @@ class ModuleNode(Nodes.Node, Nodes.BlockNode): h_vars = h_entries(env.var_entries) h_funcs = h_entries(env.cfunc_entries) h_extension_types = h_entries(env.c_class_entries) + if h_types or h_vars or h_funcs or h_extension_types: result.h_file = replace_suffix_encoded(result.c_file, ".h") + self.assure_safe_target(result.h_file) + h_code_writer = Code.CCodeWriter() c_code_config = generate_c_code_config(env, options) globalstate = Code.GlobalState(h_code_writer, self, c_code_config) @@ -299,8 +310,11 @@ class ModuleNode(Nodes.Node, Nodes.BlockNode): api_vars = api_entries(env.var_entries) api_funcs = api_entries(env.cfunc_entries) api_extension_types = api_entries(env.c_class_entries) + if api_vars or api_funcs or api_extension_types: result.api_file = replace_suffix_encoded(result.c_file, "_api.h") + self.assure_safe_target(result.api_file) + h_code = Code.CCodeWriter() c_code_config = generate_c_code_config(env, options) Code.GlobalState(h_code, self, c_code_config) @@ -401,13 +415,7 @@ class ModuleNode(Nodes.Node, Nodes.BlockNode): i_code.dedent() def generate_c_code(self, env, options, result): - # Check for a common gotcha for new users: naming your .pyx file after the .c file you want to wrap - if not is_cython_generated_file(result.c_file, allow_failed=True, if_not_found=True): - # Raising a fatal CompileError instead of calling error() to prevent castrating an existing file. - raise CompileError( - self.pos, 'The output file already exists and does not look like it was generated by Cython: "%s"' % - os.path.basename(result.c_file)) - + self.assure_safe_target(result.c_file, allow_failed=True) modules = self.referenced_modules if Options.annotate or options.annotate: |