summaryrefslogtreecommitdiff
path: root/Cython/Compiler/Code.py
Commit message (Collapse)AuthorAgeFilesLines
* Refactor label interception code in for-loop and exception handling nodes to ↵scoder2023-04-021-7/+31
| | | | reduce redundancy. (GH-5350)
* Limit the cname length for generated number constants since some C compilers ↵0dminnimda2023-03-171-9/+19
| | | | | cannot handle long names (GH-5293) Closes https://github.com/cython/cython/issues/5290
* Fix cleanup of non-py-object constants on module close (GH-5265)da-woods2023-02-271-2/+10
| | | | | | Not all `py_constants` are actually `py_constants` so they need to be checked before generating clean-up code. Since this fact is a source of bugs (like this one), it should be cleaned up. `py_constants` should be limited to Python constants (or get a more accurate name, but it seems reasonable to have such a list of things that need cleanup). Issue found in https://github.com/cython/cython/pull/5261
* Write unicode strings instead of str in Py2 to get a more optimised string ↵Stefan Behnel2023-01-171-1/+1
| | | | generation.
* Minor code cleanups.Stefan Behnel2023-01-171-2/+2
|
* Use PyxCodeWriter in the dataclass TemplateCode (GH-5049)da-woods2023-01-161-1/+11
| | | | | Makes insertion points behave as they do elsewhere, adds the ability to track indentation (which I don't use here, but which I think will clear things up)
* Refactor module state to always use struct (#5056)da-woods2022-12-011-47/+14
| | | | | | | | | | | | | | | | | | | | | First part of https://github.com/cython/cython/issues/3689 First part of https://github.com/cython/cython/issues/4495 This tries to put all the global static variables into the module state struct. When CYTHON_USE_MODULE_STATE is false, they're looked up from a single static instance of the struct. This allows a lot of semi-duplicated code to be dropped. It doesn't get rid of the awkward `#defines` to access the struct (that's probably the next step of the cleanup). It moves the stringtab from a global into a function local. It moves the statically defined coroutine types into the module state struct in the same way that CyFunction/FusedFunction already were. It's mostly an exercise in deleting things - it doesn't attempt to do any significant cleanup of the generation code.
* Merge branch '0.29.x'da-woods2022-10-221-1/+1
|\
| * Fix various clang warnings (#5086)Lisandro Dalcin2022-10-171-1/+1
| | | | | | | | | | | | * Fix clang -Wcomma * Fix clang -Wextra-semi-stmt * Fix clang -Wconditional-uninitialized * Fix clang -Wunreachable-code-return
| * Revert "Avoid acquiring the GIL at the end of nogil functions (GH-3556) ↵Stefan Behnel2022-05-171-2/+2
| | | | | | | | | | | | (GH-4749)" This reverts commit 74073417c4f7e616af0a139a55f790f1ec7fe728.
| * Avoid acquiring the GIL at the end of nogil functions (GH-3556) (GH-4749)Oleksandr Pavlyk2022-05-031-2/+2
| | | | | | | | | | | | | | | | | | | | | | | | Closes https://github.com/cython/cython/issues/4637 See See https://github.com/cython/cython/issues/3556 * Acquire the GIL in nogil functions only when strictly needed on function exit, e.g. for cleaning up temp variables from with-gil blocks or adding tracebacks. See https://github.com/cython/cython/issues/3554 * Make the GIL-avoidance in 7d99b0f0 actually work in nogil functions and not just nogil sections. See https://github.com/cython/cython/issues/3558
| * Mark reverse operators with METHOD_COEXIST (GH-4753)da-woods2022-05-031-2/+2
| | | | | | | | | | | | | | | | | | | | This means that reverse operators (e.g. `__radd__`) won't be hidden by the automatic wrapper that `PyType_Ready()` produces if the forward method exists. Although they won't work as in Python, they will be possible to look up and call explicitly. This should make it easier to write code that's compatible with Cython 0.29.x and Cython 3 (where reverse operators will be full supported). Closes https://github.com/cython/cython/issues/4750
| * Revert "Avoid acquiring the GIL at the end of nogil functions (GH-3556) ↵scoder2022-04-161-2/+2
| | | | | | | | | | | | | | | | | | | | (GH-4703)" (GH-4742) PR 4703 was an incomplete backport of the changes needed for #3554 and generates incorrect C code. See https://github.com/cython/cython/issues/3554 Reverts https://github.com/cython/cython/pull/4703 This reverts commit d395a56f8e68ee563f866d0dec5a31a37f77df9e.
| * Avoid acquiring the GIL at the end of nogil functions (GH-3556) (GH-4703)Oleksandr Pavlyk2022-03-311-2/+2
| | | | | | | | | | | | Acquire the GIL in nogil functions only when strictly needed on function exit, e.g. for cleaning up temp variables from with-gil blocks or adding tracebacks. Closes GH-3554 Closes GH-4637
| * Keep utility code names across type specialisations and show it in the C ↵Stefan Behnel2022-01-281-1/+5
| | | | | | | | file instead of "None".
| * Validate and fix temp releasing (GH-3708) (GH-3717)scoder2020-07-011-8/+29
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | * Validate and fix temp releasing (GH-3708) Backports 92147baf11071352ffbfa475d0d21e091753e628. * Fix a temp leak in the type init code. * Fix temp leaks in fused types initialisation code. * Correctly release the buffer index temps allocated for index calculations. * Make tests fails hard if a temp variable is not released at the end of a generated function. * Fix temp leak in switch statement code. * Make end-to-end tests fail on refnanny output. * Fix result temp leak in PyTypeTestNode. * Fix result temp leak in external type/function import code and enable the refnanny check for them. * Fix temp leak when try-return-finally is used in generators. * Make it explicit when an allocated temp is not meant to be reused. * Fix temp leak when assigning to the real/imag attributes of complex numbers. * Fix temp leak when assigning to a memoryview slice. * Clean up "num_threads" result temp in parallel section, not only in prange loop. * Fix temp leak in Pythran buffer setitem code. * Simplify NumPyMethodCallNode since it does not need the Python function anymore. Previously, it generated code that needlessly looked up the Python function without actually using it. * Fix temp leak when deleting C++ objects. * Add a test that actually reusing temps when deleting C++ objects works correctly.
* | Update pyxcodewriter.indenter (#5048)da-woods2022-09-261-15/+7
| | | | | | | | It was written before `with` was universally available. Update the interface to make it harder to get wrong.
* | Fix C++ error from Cython unused (#5029)da-woods2022-09-141-2/+2
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | I was getting a whole lot on errors along the lines of ``` cfunc_convert_with_memoryview.cpp:11097:8: note: in expansion of macro ‘CYTHON_UNUSED’ 11097 | static CYTHON_UNUSED int __pyx_memoryview_getbuffer(PyObject *__pyx_v_self, Py_buffer *__pyx_v_info, int __pyx_v_flags) { | ^~~~~~~~~~~~~ cfunc_convert_with_memoryview.cpp:407:31: note: an attribute that appertains to a type-specifier is ignored 407 | #define CYTHON_UNUSED [[maybe_unused]] ``` This swaps the order of static and CYTHON_UNUSED. I think whether the error appears is probably dependent on the exact compiler version.
* | Fix memoryview iteration in generator expressions (GH-4968)da-woods2022-08-091-8/+8
| | | | | | | | | | As a result of f946fe22fff32077dc58beeb64ec1ebc85d37632 And fix at least one pre-existing reference counting leak for memoryview references in things like `any(i > 0 for i in memview)`
* | Turn some often used CCodeWriter methods into (final) cdef methods to reduce ↵Stefan Behnel2022-08-061-10/+10
| | | | | | | | their call overhead.
* | Fix missing newlines in HTML annotation file, introduced in ↵Stefan Behnel2022-08-041-3/+6
| | | | | | | | | | | | 0b4370678e5b00a020cd990f922964d3aba59884. Closes https://github.com/cython/cython/issues/4945
* | Reduce overhead in the code writer when writing out simple code without ↵Stefan Behnel2022-07-161-7/+12
| | | | | | | | newlines.
* | Avoid raising StopIteration in "__next__" if possible (GH-4844)da-woods2022-07-131-2/+13
| | | | | | Fixes https://github.com/cython/cython/issues/3447
* | Mark reverse operators with METHOD_COEXIST (GH-4754)da-woods2022-05-131-3/+15
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | This means that reverse operators (e.g. `__radd__`) won't be hidden by the automatic wrapper that PyType_Ready() produces if the forward method exists. Although they won't work as in Python, they will be possible to look up and call explicitly. This should make it easier to write code that's compatible with Cython 0.29.x and Cython 3 (where reverse operators will be full supported). Adjusted to work on Cython 3.0. Based on https://github.com/cython/cython/pull/4753 Closes https://github.com/cython/cython/issues/4750
* | Merge branch 0.29.xStefan Behnel2022-05-111-2/+2
| |
* | Remove unused imports (GH-4643)Matus Valo2022-02-151-1/+0
| |
* | Avoid an unnecessary inefficiency in a regex, although it is not relevant in ↵Stefan Behnel2022-02-091-1/+1
| | | | | | | | | | | | practice since the regex is only applied to Cython's only C code. Found by GitHub CodeQL.
* | Always regenerate .c/cpp output files when changing the Cython version. This ↵Stefan Behnel2022-02-021-1/+1
| | | | | | | | is probably what users expect since silently keeping outdated files is error prone.
* | Keep utility code names across type specialisations and show it in the C ↵Stefan Behnel2022-01-221-1/+5
| | | | | | | | file instead of "None".
* | A function that is supposed to return something should return something.Stefan Behnel2022-01-051-1/+1
| |
* | Allow releasing the GIL in nogil functions (GH-4318)da-woods2021-12-181-2/+13
| | | | | | | | | | | | | | Also adds a check whether we have the GIL before doing so. This is important because Py_UNBLOCK_THREADS is documented as unsafe if we don't hold the GIL. Closes https://github.com/cython/cython/issues/4137
* | Avoid AddTraceback() if stringtab isn't set up (GH-4378)da-woods2021-09-271-22/+29
| | | | | | | | | | | | | | This can happen (rarely) with exceptions that occur very early in the module init process. Fixes https://github.com/cython/cython/issues/4377 Uses a fake Numpy module for testing to make a version of "import_array" that always fails.
* | Suppress warnings about constant expressions on MSVC (GH-4317)da-woods2021-08-071-0/+14
| | | | | | Adds utility code sections to toggle C compiler warnings for the duration of the utility code.
* | Resolve some issues with "cpp_locals" (GH-4265)da-woods2021-07-191-2/+2
| | | | | | | | | | | | | | * Fix class attributes access where cpp_locals=False. * Add "no-cpp-locals" tag to mark a test as not suitable for running with "cpp_locals=True". * Add a list of "extra_directives" to runtests as additional test mode. Resolves some issues in https://github.com/cython/cython/issues/4266
* | Fix a few bugs in the new "cpp_locals" directive implementation (GH-4259)da-woods2021-07-021-0/+1
| | | | | | | | | | | | | | 1) Entry utility_code wasn't being imported unless the entry was visibly used. The utility code is needed for the definition even if unused. I think this is what utility_code_definition is for, but I'm not completely sure. I also had to add some calls to ensure it's used. 2) CppOptionalTempCoercions weren't being moved correctly. Tested by printing from the destructor. 3) cdef-class attributes weren't being created or destroyed correctly. Tested by printing from the destructor
* | Add directive "cpp_locals" to handle C++ variables using std::optional (GH-4225)da-woods2021-06-291-5/+26
| | | | | | | | | | This avoids the need for default constructors of stack allocated variables and temps by allowing late initialisation. Closes https://github.com/cython/cython/issues/4160
* | Move some utility code from ExprNodes.py into utility code files.Stefan Behnel2021-06-291-4/+3
| |
* | Split the current "CYTHON_COMPILING_IN_LIMITED_API" macro guard (GH-3611)scoder2021-05-251-16/+16
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | * Split the current "CYTHON_COMPILING_IN_LIMITED_API" macro guard into separate feature guards: 1) using a global module state struct ("CYTHON_USE_MODULE_STATE") 2) using PyType_FromSpec() for extension types ("CYTHON_USE_TYPE_SPECS") 3) actual limited-API special casing ("CYTHON_COMPILING_IN_LIMITED_API") * Start using PyType_FromModuleAndSpec() for creating extension types since that adds the module reference with PEP-573. * Do not pass the module reference into *shared* extension types since we do not want to keep the module alive just because other modules still refer to the type. * Mark the Limited-API and module state struct features as experimental. * Add CI setups for using PyType_FromSpec() for creating extension types. * Avoid overly complicated code in __Pyx_FetchCommonTypeFromSpec() when we are not compiling against the Limited-API. * Correctly set up the bases tuple for PyType_FromSpecWithBases() and stop relying on the PyTypeObject struct being available in several places by using the type pointer instead (and setting it early enough). * Set the "tp_dealloc" slot from the type spec, also for the limited API case. * Generally re-enable the "heaptype bases hack" when creating extension types. * Implement "tp_dictoffset" slot when using type specs in Py3.9+. * Fix and simplify the vtable setup in the limited API config. * Make CyFunction inheritable to "correctly" allow FusedFunction subclassing it. * Enable buffer interface support when using type specs by assigning the "tp_as_buffer" slot explicitly after creating the type. * Set "vectorcalloffset" when using type specs. * Make "__bool__" slot prefer Py3+ name instead of Py2 name to make the correct name ("Py_nb_bool") appear in the type spec. * Validate and fix up all extension types when constructing them from type specs (not only the internal ones). * Only call PyType_Modified() once after changing a type, not once per change. * Turn the "no buffer protocol in the limited API" error into a warning since the user code might still work without it. * Enable the same flags for CyFunction in the static type and type spec case since it's shared across modules and thus, we might otherwise end up with differently configured types in the same runtime. * The names of Cython's internal types (functions, generator, coroutine, etc.) are now prefixed with the shared module name, instead of making them look like homeless builtins. See https://bugs.python.org/issue20204 See https://github.com/python/cpython/commit/490055a1673b524da2ebe2312f072aba2a826036 * Work around a CPython bug that makes it overwrite exttype member definitions for the "__module__" attribute. See https://bugs.python.org/issue40703 * Inherit buffer slots manually since type slots don't support them and thus PyType_Ready() does not see them. * Add METH_METHOD call support (currently unused). * Fix the vectorcall offset used in CyFunction type struct in Py3.9, if it inherits from PyCMethodObject instead of PyCFunctionObject. * Work around the duplicate creation of the "OverrideCheckNode" for fused cpdef functions. See https://github.com/cython/cython/issues/3628 * Decref the type when deallocating instances of heap types in Py3.8+. See https://bugs.python.org/issue35810 * Look up the fused dispatch function in the override check, not the specialisation (which shouldn't be overridden all by itself). Closes https://github.com/cython/cython/issues/3628 * Avoid running the override check for CyFunction, just because it's not a PyCFunction itself.
* | Modernise code: use set literals/comprehensions where possible, frozenset ↵Stefan Behnel2021-04-131-3/+3
| | | | | | | | where appropriate.
* | Code style: Modernise some set() usages (GH-4109)Zhenbo Li2021-04-111-3/+3
| |
* | Fix compiler warning about constant conditions in clang (GH-4053)Max Bachmann2021-03-211-1/+2
| | | | | | clang warns about dead code when an if-statement always evaluates to false. This warning can be silenced using an extra '()'.
* | Support utility code in headers (GH-3779)da-woods2020-09-221-0/+13
| | | | | | Specifically this allows public packed structs but may also allow other public declarations that require small amounts of utility code.
* | Fix cygdb (GH-3542)Volker-Weissmann2020-08-241-4/+7
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | * Cython debugger documentation: Added link to an installation script. * Got a new libpython.py from the cpython source distribution. * Default language level in tests is now 3 instead of 2 * Migrated codefile from python 2 to python 3. * Added testcase for the cy list command in cygdb. * Temporarily removing test case that freezes gdb. * Fixed a bug that broke several Cygdb tests. The cython_debug/cython_debug_info_* files map the names of the C-functions generated by the Cython compiler to the names of the functions in the *.pyx source. If the function was defined using "def" (and not "cpdef" or "cdef") in the *.pyx source file, the C-function named in cython_debug/cython_debug_info_* used to be __pyx_pw_*, which is the name of the wrapper function and now it is __pyx_f_*, which is the name of the actual function. This makes some Cygdb tests pass that did not pass before. * Better error messages: If a cygdb command raises, a traceback will be printed. * Fixed a bug in cygdb. The following now works: 1. Start cygdb 2. Type "cy exec" and hit enter 3. Type some other lines 4. Type "end" and hit enter. -> These "other lines" will get executed * Fixed a bug in cygdb: cy list now works outside of functions. * Added print_hr_allmarkers function for easier debugging. * Fixed a bug that broke cygdb: cy break did not work if you put the breakpoint outside of a function if there was e.g. the following somewhere in your *.pyx file: cdef class SomeClass(): pass * Added a Cygdb test for printing global variables. * Fixing cygdb: Replaced cy print with a simple, working solution. * If an exception in Cygdb occurs, a stacktrace will be printed. * Fixed a bug that broke cy break -p * Bugfix: The compiler now writes out correctly which cython linenumber and path corresponds to which c linenumber. * Set language_level=2 in runtests.py
* | Validate and fix temp releasing (GH-3708)scoder2020-06-281-11/+18
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | * Fix a temp leak in the type init code. * Fix temp leaks in fused types initialisation code. * Correctly release the buffer index temps allocated for index calculations. * Make tests fails hard if a temp variable is not released at the end of a generated function. * Fix temp leak in switch statement code. * Make end-to-end tests fail on refnanny output. * Fix result temp leak in PyTypeTestNode. * Fix result temp leak in external type/function import code and enable the refnanny check for them. * Fix temp leak when try-return-finally is used in generators. * Make it explicit when an allocated temp is not meant to be reused. * Fix temp leak when assigning to the real/imag attributes of complex numbers. * Fix temp leak when assigning to a memoryview slice. * Clean up "num_threads" result temp in parallel section, not only in prange loop. * Fix temp leak in Pythran buffer setitem code. * Simplify NumPyMethodCallNode since it does not need the Python function anymore. Previously, it generated code that needlessly looked up the Python function without actually using it. * Fix temp leak when deleting C++ objects. * Add a test that actually reusing temps when deleting C++ objects works correctly.
* | Validate that all temps were correctly released at the end of a function.Stefan Behnel2020-06-251-0/+15
| |
* | Fix many indentation and whitespace issues throughout the code base (GH-3673)scoder2020-06-101-13/+13
| | | | | | … and enforce them with pycodestyle.
* | Merge branch '0.29.x'Stefan Behnel2020-05-291-5/+2
|\ \ | |/
| * Fix detection of reusable utility code: whenever we do string and/or unbound ↵Stefan Behnel2020-05-291-5/+2
| | | | | | | | method replacements at all, it's not reusable.
| * Allow '# cython: …' module level directives in Cython utility code.Stefan Behnel2020-05-151-1/+1
| |
| * Generate function-local error indicator variables whenever "error_goto()" is ↵Stefan Behnel2020-04-291-12/+6
| | | | | | | | | | | | used. This was previously broken by the introduction of the "__PYX_ERR()" macro.