Skip to content

Commit

Permalink
Modified "pretend_to_initialize" for c++
Browse files Browse the repository at this point in the history
In this case we actually have to initialize rather than just
do nothing. Fixes cython#5278.

Supercedes cython#5296 (I think this is better since it limits the
amount of work Cython itself has to do)
  • Loading branch information
da-woods committed Dec 22, 2023
1 parent 2f93a5f commit 3ce26d2
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions Cython/Compiler/ModuleNode.py
Expand Up @@ -866,7 +866,24 @@ def generate_module_preamble(self, env, options, cimported_modules, metadata, co
PyrexTypes.c_int_type.create_from_py_utility_code(env)

code.put(Nodes.branch_prediction_macros)

# For C, taking an address of a variable is enough to make returning it
# defined behaviour. For C++ this isn't true, and we genuinely have to
# make sure a variable is initialized.
code.putln('#if __cplusplus')
code.putln('#include <type_traits>')
code.putln('template <typename T> void __Pyx_pretend_to_initialize(T* ptr) {')
# In C++11 we have enough introspection to work out which types it's actually
# necessary to apply this to (non-trivial types will have been initialized by
# the definition). Below that just apply it to eveything.
code.putln('#if __cplusplus > 201103L')
code.putln('if ((std::is_trivially_default_constructible<T>::value))')
code.putln('#endif')
code.putln('*ptr = T();')
code.putln('}')
code.putln('#else')
code.putln('static CYTHON_INLINE void __Pyx_pretend_to_initialize(void* ptr) { (void)ptr; }')
code.putln('#endif')
code.putln('')
code.putln('#if !CYTHON_USE_MODULE_STATE')
code.putln('static PyObject *%s = NULL;' % env.module_cname)
Expand Down

0 comments on commit 3ce26d2

Please sign in to comment.