Skip to content

[BUG] __PYX_STD_MOVE_IF_SUPPORTED doesn't work well with MSVC #3789

@Zabolekar

Description

@Zabolekar

Windows 10, Python 3.8.3, Cython 3.0a5, Visual Studio 2017 version 15.9.20.

Let's consider the following example:

a.h:

#pragma once
#include <memory>

std::unique_ptr<int> f() {
    return std::make_unique<int>(123456);
}

b.pyx:

from libcpp.memory cimport unique_ptr

cdef extern from "a.h":
    unique_ptr[int] f() except +

def g():
    cdef unique_ptr[int] p = f()

setup.py:

from setuptools import setup, Extension
from Cython.Build import cythonize

extensions = [Extension(
    "b",
    ["b.pyx"],
    language='c++'
)]
setup(ext_modules=cythonize(extensions, compiler_directives={"language_level" : "3"}))

Expected behavior
python setup.py build builds a .pyd-file.

Observed behavior
python setup.py build produces a message referencing error C2280, doesn't build a .pyd-file.

Workaround
Adding extra_compile_args=["/Zc:__cplusplus"] to the arguments of Extension fixes the problem.

Likely reason
MSVC defines __cplusplus as 199711L by default (details)

Cython defines __PYX_STD_MOVE_IF_SUPPORTED like this:

(source)

#if __cplusplus >= 201103L
  #include <utility>
  #define __PYX_STD_MOVE_IF_SUPPORTED(x) std::move(x)
#else
  #define __PYX_STD_MOVE_IF_SUPPORTED(x) x
#endif

The generated .cpp file tries to copy a unique_ptr, and thus doesn't compile.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions