internlm/SWE-Fixer-Train-110K huggingface dataset:
Repository: cython/cython

Issue:
const fails when used as the argument to a template of a template
This works:

```
from libcpp.vector cimport vector

cdef extern from "Test.hpp":
    cdef vector[char](const) f()
```

As does this:

```
from libcpp.vector cimport vector

cdef extern from "Test.hpp":
    cdef vector[f()
```

But this causes a Cython parse error on Cython 0.18:

```
from libcpp.vector cimport vector

cdef extern from "Test.hpp":
    cdef vector[vector[const char](vector[char]])] f()
```

Migrated from http://trac.cython.org/ticket/798



Discussion:
**garrison** changed **cc** to `garrison`
commented

The obvious work-around is a `ctypedef`, but it seems worth fixing.

The parser calls `p_buffer_or_template()` to parse the `[…]` part, which then calls `p_positional_and_keyword_args()` to parse what's inside of the brackets. I guess that simply doesn't allow template type declarations. It could, though. That would probably also help with template type parsing in function signatures.

Also needs a test, probably in one of the tests/run/cpp_*.pyx modules.
Taking a look

Resolving patch:
diff --git a/Cython/Compiler/Parsing.py b/Cython/Compiler/Parsing.py
--- a/Cython/Compiler/Parsing.py
+++ b/Cython/Compiler/Parsing.py
@@ -2698,7 +2698,7 @@ def looking_at_expr(s):
             s.put_back(*saved)
         elif s.sy == '[':
             s.next()
-            is_type = s.sy == ']'
+            is_type = s.sy == ']' or not looking_at_expr(s)  # could be a nested template type
             s.put_back(*saved)
 
         dotted_path.reverse()


Test patch:
diff --git a/tests/compile/cpp_templates_nested.pyx b/tests/compile/cpp_templates_nested.pyx
new file mode 100644
--- /dev/null
+++ b/tests/compile/cpp_templates_nested.pyx
@@ -0,0 +1,18 @@
+# tag: cpp
+# mode: compile
+
+from libcpp.vector cimport vector
+
+cdef extern from *:
+    cdef cppclass Foo[T]:
+        pass
+
+    cdef cppclass Bar:
+        pass
+
+cdef vector[vector[int]] a
+cdef vector[vector[const int]] b
+cdef vector[vector[vector[int]]] c
+cdef vector[vector[vector[const int]]] d
+cdef Foo[Foo[Bar]] e
+cdef Foo[Foo[const Bar]] f
Repository: cython/cython

Issue:
Invalid C code for pxd-inline function that returns memory view
```
# foo.pxd:

from cython cimport numeric
from libc.stdlib cimport malloc
cdef inline numeric[mview(size_t size, numeric* null):
    return <numeric[:size:1](::1])>malloc(size * sizeof(null))

# bar.pyx:

from foo cimport mview
def main():
    return mview(1, <double*>NULL)
```

C compiler error is:

```
bar.c: In function ‘__pyx_fuse_0__pyx_f_3foo_mview’:
bar.c:1571:13: error: incompatible types when assigning to type ‘__Pyx_memviewslice’ from type ‘int’
   __pyx_t_5 = __Pyx_PyObject_to_MemoryviewSlice_dc_short(((PyObject *)__pyx_t_2));
             ^
```

Originally reported here:

http://thread.gmane.org/gmane.comp.python.cython.user/13946

Migrated from http://trac.cython.org/ticket/861



Discussion:

Resolving patch:
diff --git a/Cython/Compiler/ExprNodes.py b/Cython/Compiler/ExprNodes.py
--- a/Cython/Compiler/ExprNodes.py
+++ b/Cython/Compiler/ExprNodes.py
@@ -13115,12 +13115,11 @@ def __init__(self, arg, dst_type, env):
         CoercionNode.__init__(self, arg)
         self.type = dst_type
         self.is_temp = 1
-        self.env = env
         self.use_managed_ref = True
         self.arg = arg
+        self.type.create_from_py_utility_code(env)
 
     def generate_result_code(self, code):
-        self.type.create_from_py_utility_code(self.env)
         code.putln(self.type.from_py_call_code(
             self.arg.py_result(),
             self.result(),


Test patch:
diff --git a/tests/memoryview/memoryview_inline_pxd.srctree b/tests/memoryview/memoryview_inline_pxd.srctree
new file mode 100644
--- /dev/null
+++ b/tests/memoryview/memoryview_inline_pxd.srctree
@@ -0,0 +1,35 @@
+# ticket: 1415
+
+# Utility code from an inline function in a pxd file was not
+# correctly included in a pyx file that cimported it.
+# Do not add more to this test - it is intentionally minimal
+# to avoid including the utility code through other means
+
+PYTHON setup.py build_ext --inplace
+PYTHON -c "import uses_inline; uses_inline.main()"
+
+######## setup.py ########
+
+from distutils.core import setup
+from Cython.Distutils import build_ext
+from Cython.Distutils.extension import Extension
+
+setup(
+    ext_modules = [
+        Extension("uses_inline", ["uses_inline.pyx"]),
+    ],
+    cmdclass={'build_ext': build_ext},
+)
+
+######## has_inline.pxd ########
+
+from libc.stdlib cimport malloc
+cdef inline double[::1] mview(size_t size):
+    return <double[:size:1]>malloc(size * sizeof(double))
+    
+######## uses_inline.pyx ########
+
+from has_inline cimport mview
+def main():
+    return mview(1)
+
Repository: cython/cython

Issue:
[BUG] Compilation fails when class attribute is annotated using `cython.pointer()`
**Describe the bug**
Compiler fails to compile when class attribute annotation is using `cython.pointer()`.

**To Reproduce**
Code to reproduce the behaviour:
```cython
import cython

A = cython.declare(cython.int, 3)

@cython.cclass
class Test:
    test: cython.pointer(cython.int)

    def __init__(self):
        self.test = cython.address(A)
```
Compilation fails with:
```
$ cython -3a test.py

Error compiling Cython file:
------------------------------------------------------------
...
@cython.cclass
class Test:
    test: cython.pointer(cython.int)

    def __init__(self):
        self.test = cython.address(A)
                         ^
------------------------------------------------------------

test.py:11:26: Cannot convert 'int *' to Python object
```
**Expected behavior**

Compilation should be successful.

**Environment (please complete the following information):**
 - OS: macOS
 - Python version: 3.9.7
 - Cython version: master

**Additional context**
All following code snippets compiles succesfully:
* cython version
```cython
import cython

cdef int A = 3

cdef class Test:
    cdef int *test

    def __init__(self):
        self.test = &A
```
* pure python version using `cython.p_int` annotation:
```python
import cython

A = cython.declare(cython.int, 3)

@cython.cclass
class Test:
    test: cython.p_int

    def __init__(self):
        self.test = cython.address(A)
```



Discussion:

Resolving patch:
diff --git a/Cython/Compiler/ParseTreeTransforms.py b/Cython/Compiler/ParseTreeTransforms.py
--- a/Cython/Compiler/ParseTreeTransforms.py
+++ b/Cython/Compiler/ParseTreeTransforms.py
@@ -913,6 +913,8 @@ def visit_SingleAssignmentNode(self, node):
         return node
 
     def visit_NameNode(self, node):
+        if node.annotation:
+            self.visit(node.annotation)
         if node.name in self.cython_module_names:
             node.is_cython_module = True
         else:


Test patch:
diff --git a/tests/run/annotation_typing.pyx b/tests/run/annotation_typing.pyx
--- a/tests/run/annotation_typing.pyx
+++ b/tests/run/annotation_typing.pyx
@@ -278,6 +278,21 @@ def call_take_ptr():
     python_dict = {"abc": 123}
     take_ptr(cython.cast(cython.pointer(PyObject), python_dict))
 
+@cython.cclass
+class HasPtr:
+    """
+    >>> HasPtr()
+    HasPtr(1, 1)
+    """
+    a: cython.pointer(cython.int)
+    b: cython.int
+
+    def __init__(self):
+        self.b = 1
+        self.a = cython.address(self.b)
+    def __repr__(self):
+        return f"HasPtr({self.a[0]}, {self.b})"
+
 
 _WARNINGS = """
 9:32: Strings should no longer be used for type declarations. Use 'cython.int' etc. directly.
Repository: cython/cython

Issue:
[BUG] Cannot pass fused type by reference
It seems that passing fused types by reference is not currently possible. Consider the following snippet

```cython
# distutils: language=c++

ctypedef fused element_t:
        int
    
cdef funcr(element_t &a):
    pass

def test():
    cdef int a = 5
    funcr(a)
```

which fails with the error message:

```

Error compiling Cython file:
------------------------------------------------------------
...
cdef funcr(element_t &a):
    pass

def test():
    cdef int a = 5
    funcr(a)
         ^
------------------------------------------------------------

...pyx:11:10: Cannot coerce to a type that is not specialized
```

I tried explicitly indexing to specialize the type by changing the call from `funcr(a)` to `funcr[int](a)` but got the following message.

```
Error compiling Cython file:
------------------------------------------------------------
...
cdef funcr(element_t &a):
    pass

def test():
    cdef int a = 5
    funcr[int](a)
        ^
------------------------------------------------------------

...pyx:11:9: Can only parameterize template functions.
```

The code compiles fine after changing the type definition to `ctypedef int element_t`. Similarly, passing `a` by value or by pointer works as expected.

My environment is
- OS: macOS Monterey 12.3 (M1 chip)
- Python version: 3.9.9 (pyenv via brew)
- Cython version: 0.29.28 (have also tried `master`)

Happy to support if I can, but not quite sure where to get started.


Discussion:

Resolving patch:
diff --git a/Cython/Compiler/PyrexTypes.py b/Cython/Compiler/PyrexTypes.py
--- a/Cython/Compiler/PyrexTypes.py
+++ b/Cython/Compiler/PyrexTypes.py
@@ -2769,6 +2769,8 @@ class CReferenceBaseType(BaseType):
 
     # Common base type for C reference and C++ rvalue reference types.
 
+    subtypes = ['ref_base_type']
+
     def __init__(self, base_type):
         self.ref_base_type = base_type
 


Test patch:
diff --git a/tests/run/fused_cpp.pyx b/tests/run/fused_cpp.pyx
--- a/tests/run/fused_cpp.pyx
+++ b/tests/run/fused_cpp.pyx
@@ -41,3 +41,13 @@ def typeid_call2(cython.integral x):
     """
     cdef const type_info* a = &typeid(cython.integral)
     return a[0] == tidint[0]
+
+cdef fused_ref(cython.integral& x):
+    return x*2
+
+def test_fused_ref(int x):
+    """
+    >>> test_fused_ref(5)
+    (10, 10)
+    """
+    return fused_ref(x), fused_ref[int](x)
Repository: cython/cython

Issue:
[BUG] Fused type not subscriptable in uncompiled pure python
**Describe the bug**
Fused type can't be subscribed in pure python syntax when `cython.compiled == False`

**To Reproduce**
Code to reproduce the behaviour:
```python
import cython

int_or_float = cython.fused_type(cython.int, cython.float)

def func(num: int_or_float[:]):
    ...
```

Gives error:
```
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Input In [302], in <module>
      1 import cython
      3 int_or_float = cython.fused_type(cython.int, cython.float)
----> 5 def func(num: int_or_float[:]):
      6     ...

TypeError: '_FusedType' object is not subscriptable
```

**Expected behavior**
`Cython.Shadow` should implement this so that it doesn't raise an error (?).

**Environment (please complete the following information):**
 - OS: Linux
 - Python version: '3.8.12 | packaged by conda-forge | (default, Oct 12 2021, 21:59:51) \n[GCC 9.4.0]'
 - Cython version: 3.0.0a10



Discussion:
PR welcome

Resolving patch:
diff --git a/Cython/Shadow.py b/Cython/Shadow.py
--- a/Cython/Shadow.py
+++ b/Cython/Shadow.py
@@ -385,7 +385,7 @@ def __repr__(self):
     __getitem__ = index_type
 
 class _FusedType(CythonType):
-    pass
+    __getitem__ = index_type
 
 
 def fused_type(*args):


Test patch:
Repository: cython/cython

Issue:
[BUG] Broken tuple multiplication behavior
<!--
**PLEASE READ THIS FIRST:**
- DO NOT use the bug and feature tracker for general questions and support requests.
  Use the `cython-users` mailing list instead.
  It has a wider audience, so you get more and better answers.
- Did you search for similar issues already?
  Please do, it helps to save us precious time that we otherwise could not invest into development.
- Did you try the latest master branch or pre-release?
  It might already have what you want to report.
  Also see the [Changelog](https://github.com/cython/cython/blob/master/CHANGES.rst) regarding recent changes.
-->

**Describe the bug**
Tuple multiplication doesn't work correctly while it is unpacked into list. It produces list with only one copy of initial tuple and also doesn't throw exception when multiplier is of float type.

**To Reproduce**
Code to reproduce the behaviour:
```cython
[*(1,) * 1]  # [1,]
[*(1,) * 2]  # [1,]
[*(1,) * 3]  # [1,]
[*(1,) * 1.5]  # [1,]
```

**Expected behavior**
It is expected to work identically to multiplication of lists which works fine in cython:
```cython
[*[1,] * 1]  # [1,]
[*[1,] * 2]  # [1, 1]
[*[1,] * 3]  # [1, 1, 1]
[*[1,] * 1.5]  # TypeError: can't multiply sequence by non-int of type 'float'
```

**Environment (please complete the following information):**
 - OS: Linux, Ubuntu 20.04.3 LTS
 - Python version 3.9.5
 - Cython version 0.29.30


Discussion:

Resolving patch:
diff --git a/Cython/Compiler/ExprNodes.py b/Cython/Compiler/ExprNodes.py
--- a/Cython/Compiler/ExprNodes.py
+++ b/Cython/Compiler/ExprNodes.py
@@ -8482,7 +8482,7 @@ def __init__(self, pos, args, type):
         if type in (list_type, tuple_type) and args and args[0].is_sequence_constructor:
             # construct a list directly from the first argument that we can then extend
             if args[0].type is not list_type:
-                args[0] = ListNode(args[0].pos, args=args[0].args, is_temp=True)
+                args[0] = ListNode(args[0].pos, args=args[0].args, is_temp=True, mult_factor=args[0].mult_factor)
         ExprNode.__init__(self, pos, args=args, type=type)
 
     def calculate_constant_result(self):


Test patch:
diff --git a/tests/run/pep448_extended_unpacking.pyx b/tests/run/pep448_extended_unpacking.pyx
--- a/tests/run/pep448_extended_unpacking.pyx
+++ b/tests/run/pep448_extended_unpacking.pyx
@@ -185,6 +185,24 @@ def unpack_list_literal_mult():
     return [*([1, 2, *([4, 5] * 2)] * 3)]
 
 
+def unpack_list_tuple_mult():
+    """
+    >>> unpack_list_tuple_mult()
+    [1, 1]
+    """
+    return [*(1,) * 2]
+
+
+def unpack_list_tuple_bad_mult():
+    """
+    >>> unpack_list_tuple_bad_mult()
+    Traceback (most recent call last):
+    ...
+    TypeError: can't multiply sequence by non-int of type 'float'
+    """
+    return [*(1,) * 1.5]
+
+
 @cython.test_fail_if_path_exists(
     "//ListNode//ListNode",
     "//MergedSequenceNode",
Repository: cython/cython

Issue:
[BUG]  IpythonMagic.py uses deprecated `imp` module
Should be replaced with importlib with fallback to `imp` for python version < 3.5

Note: Module uses function `imp.load_dynamic()` function. The same function is used in Inline.py file and is already ported to importlib machinery. Maybe it is enough just to move the `load_dynamic()` code to another module and use it in both modules.


Discussion:

Resolving patch:
diff --git a/Cython/Build/Inline.py b/Cython/Build/Inline.py
--- a/Cython/Build/Inline.py
+++ b/Cython/Build/Inline.py
@@ -41,11 +41,12 @@ def to_unicode(s):
     def load_dynamic(name, module_path):
         return imp.load_dynamic(name, module_path)
 else:
-    import importlib.util as _importlib_util
-    def load_dynamic(name, module_path):
-        spec = _importlib_util.spec_from_file_location(name, module_path)
-        module = _importlib_util.module_from_spec(spec)
-        # sys.modules[name] = module
+    import importlib.util
+    from importlib.machinery import ExtensionFileLoader
+
+    def load_dynamic(name, path):
+        spec = importlib.util.spec_from_file_location(name, loader=ExtensionFileLoader(name, path))
+        module = importlib.util.module_from_spec(spec)
         spec.loader.exec_module(module)
         return module
 
diff --git a/Cython/Build/IpythonMagic.py b/Cython/Build/IpythonMagic.py
--- a/Cython/Build/IpythonMagic.py
+++ b/Cython/Build/IpythonMagic.py
@@ -46,7 +46,6 @@
 
 from __future__ import absolute_import, print_function
 
-import imp
 import io
 import os
 import re
@@ -75,7 +74,7 @@
 
 from ..Shadow import __version__ as cython_version
 from ..Compiler.Errors import CompileError
-from .Inline import cython_inline
+from .Inline import cython_inline, load_dynamic
 from .Dependencies import cythonize
 from ..Utils import captured_fd, print_captured
 
@@ -357,7 +356,7 @@ def print_compiler_output(stdout, stderr, where):
         # Build seems ok, but we might still want to show any warnings that occurred
         print_compiler_output(get_stdout(), get_stderr(), sys.stdout)
 
-        module = imp.load_dynamic(module_name, module_path)
+        module = load_dynamic(module_name, module_path)
         self._import_all(module)
 
         if args.annotate:
@@ -420,7 +419,7 @@ def _profile_pgo_wrapper(self, extension, lib_dir):
 
         # import and execute module code to generate profile
         so_module_path = os.path.join(lib_dir, pgo_module_name + self.so_ext)
-        imp.load_dynamic(pgo_module_name, so_module_path)
+        load_dynamic(pgo_module_name, so_module_path)
 
     def _cythonize(self, module_name, code, lib_dir, args, quiet=True):
         pyx_file = os.path.join(lib_dir, module_name + '.pyx')


Test patch:
Repository: cython/cython

Issue:
Make the cythonize command line more useful
A couple of more common build options, such as `--numpy`, `--include_path`, `--libraries`, `--cflags`, `--ldflags` should be added to `cythonize` command line so that it can be used easily for the majority of building tasks. 

Setting up a `setup.py` script for every `pyx` file I need to write is really, really tedious, which put me off from cython for years before I finally tried it for the first time (and that was in IPython notebook with cython magic, so I still don't know how to write `setup.py`). I am sure that many beginners to cython are just scared of the complicated build process like me, and never have the chance to actually try it. Please make it more accessible.

The `%%cython` magic in IPython has much better sane defaults and useful command line options than the `cythonize` script.


Discussion:
I extracted the cython magic into a separate script at https://github.com/netheril96/utilities/blob/master/mycython.py.
Just noting here something I noticed, cythonize CLI interface actually honors environment variables such as CC, CXX, LDSHARED, CFLAGS, CPPFLAGS, LDFLAGS... I assume this isn't documented under cythonize since this feature is provided by distutils.

Example:
```
~/tmp$ CFLAGS="-O3 -g0" CPPFLAGS="-DMYDEF1 -DMYDEF2" LDFLAGS="-lz" cythonize -3 -i test.pyx 
running build_ext
building 'test' extension
creating /home/omer/tmp/tmpvkvr9kqr/home
creating /home/omer/tmp/tmpvkvr9kqr/home/omer
creating /home/omer/tmp/tmpvkvr9kqr/home/omer/tmp
x86_64-linux-gnu-gcc -pthread -DNDEBUG -g -fwrapv -O2 -Wall -O3 -g0 -DMYDEF1 -DMYDEF2 -fPIC -I/usr/include/python3.7m -c /home/omer/tmp/test.c -o /home/omer/tmp/tmpvkvr9kqr/home/omer/tmp/test.o
x86_64-linux-gnu-gcc -pthread -shared -Wl,-O1 -Wl,-Bsymbolic-functions -Wl,-z,relro -lz -O3 -g0 -DMYDEF1 -DMYDEF2 /home/omer/tmp/tmpvkvr9kqr/home/omer/tmp/test.o -o /home/omer/tmp/test.cpython-37m-x86_64-linux-gnu.so
```

Additionally, setting DISTUTILS_DEBUG environment variable enables printing extra debug info.

Resolving patch:
diff --git a/Cython/Build/Cythonize.py b/Cython/Build/Cythonize.py
--- a/Cython/Build/Cythonize.py
+++ b/Cython/Build/Cythonize.py
@@ -121,10 +121,18 @@ def run_distutils(args):
 
 
 def create_args_parser():
-    from argparse import ArgumentParser
+    from argparse import ArgumentParser, RawDescriptionHelpFormatter
     from ..Compiler.CmdLine import ParseDirectivesAction, ParseOptionsAction, ParseCompileTimeEnvAction
 
-    parser = ArgumentParser()
+    parser = ArgumentParser(
+        formatter_class=RawDescriptionHelpFormatter,
+        epilog="""\
+Environment variables:
+  CYTHON_FORCE_REGEN: if set to 1, forces cythonize to regenerate the output files regardless
+        of modification times and changes.
+  Environment variables accepted by setuptools are supported to configure the C compiler and build:
+  https://setuptools.pypa.io/en/latest/userguide/ext_modules.html#compiler-and-linker-options"""
+    )
 
     parser.add_argument('-X', '--directive', metavar='NAME=VALUE,...',
                       dest='directives', default={}, type=str,


Test patch:
Repository: cython/cython

Issue:
[ENH] Optimise bound C methods
Follow-up to https://github.com/cython/cython/pull/4988 and https://github.com/cython/cython/issues/4890

The current implementation just calls `__import__("functools").partial(cfunc)`. Cython could generate more efficient code. Specifically, it could use a CPython method object, which would also make use of the vectorcall protocol. Assuming that Cython supports that for its generated C function wrapper.


Discussion:
That is significantly simpler than what I did.

Resolving patch:
diff --git a/Cython/Compiler/ExprNodes.py b/Cython/Compiler/ExprNodes.py
--- a/Cython/Compiler/ExprNodes.py
+++ b/Cython/Compiler/ExprNodes.py
@@ -7230,22 +7230,23 @@ def coerce_to(self, dst_type, env):
                 obj_node.result_ctype = self.obj.result_ctype
                 self.obj = obj_node
                 unbound_node = ExprNode.coerce_to(self, dst_type, env)
-                functools = SimpleCallNode(
-                    self.pos,
-                    function=NameNode(self.pos, name=StringEncoding.EncodedString("__import__")),
-                    args=[StringNode(self.pos, value=StringEncoding.EncodedString("functools"))],
+                utility_code=UtilityCode.load_cached(
+                    "PyMethodNew2Arg", "ObjectHandling.c"
                 )
-                partial = AttributeNode(
-                    self.pos,
-                    obj=functools,
-                    attribute=StringEncoding.EncodedString("partial"),
+                func_type = PyrexTypes.CFuncType(
+                    PyrexTypes.py_object_type, [
+                        PyrexTypes.CFuncTypeArg("func", PyrexTypes.py_object_type, None),
+                        PyrexTypes.CFuncTypeArg("self", PyrexTypes.py_object_type, None)
+                    ],
                 )
-                partial_call = SimpleCallNode(
+                binding_call = PythonCapiCallNode(
                     self.pos,
-                    function=partial,
+                    function_name="__Pyx_PyMethod_New2Arg",
+                    func_type=func_type,
                     args=[unbound_node, obj_node],
+                    utility_code=utility_code,
                 )
-                complete_call = EvalWithTempExprNode(obj_node, partial_call)
+                complete_call = EvalWithTempExprNode(obj_node, binding_call)
                 return complete_call.analyse_types(env)
         return ExprNode.coerce_to(self, dst_type, env)
 


Test patch: