Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] Extension module build files are written to source directory #4669

Open
adang1345 opened this issue Oct 7, 2024 · 0 comments · May be fixed by pypa/distutils#302
Open

[BUG] Extension module build files are written to source directory #4669

adang1345 opened this issue Oct 7, 2024 · 0 comments · May be fixed by pypa/distutils#302
Labels
bug Needs Triage Issues that need to be evaluated for severity and status.

Comments

@adang1345
Copy link
Contributor

setuptools version

75.1.0

Python version

Python 3.13rc3

OS

Windows

Additional environment information

No response

Description

When the absolute path to an extension module source file is given to setuptools.Extension(), the build files are written to the same directory as the module source files. This clutters the source directory. In my project, I am giving an absolute path because my extension module source files are outside of the directory where setup.py is located.

In Python 3.12, this does not occur. The build files are written to build/temp.win-amd64-cpython-312 instead.

The difference in behavior is caused by a change to os.path.isabs() for Python 3.13 on Windows, where a path starting with a single slash is no longer considered an absolute path. Suppose that the source file is located at C:\projects\simpleext\simpleext.c. When this path is given to

return no_drive[os.path.isabs(no_drive) :]
, the function returns \projects\simpleext\simpleext.c instead of projects\simpleext\simpleext.c.

I will submit a PR for this.

Expected behavior

Build files are written to the build/ directory.

How to Reproduce

On Windows, install Python 3.12 and Python 3.13, and ensure that Visual Studio is installed. Create a file setup.py with the following contents.

from setuptools import setup, Extension
import os

setup(
    name='simpleext',
    version='0.0.1',
    description='Simple extension module',
    zip_safe=False,
    ext_modules=[Extension(
        'simpleext', [os.path.abspath('simpleext.c')],
    )]
)

In the same directory, create a file simpleext.c with the following contents.

#define PY_SSIZE_T_CLEAN
#include <Python.h>

static PyObject *simpleext_donothing(PyObject *self, PyObject *args)
{
   Py_RETURN_NONE;
}

static PyMethodDef SimpleExtMethods[] = {
    {"donothing", simpleext_donothing, METH_NOARGS, NULL},
    {NULL, NULL, 0, NULL}
};

static struct PyModuleDef simpleextmodule = {
    PyModuleDef_HEAD_INIT,
    "simpleext",
    NULL,
    -1,
    SimpleExtMethods
};

PyMODINIT_FUNC PyInit_simpleext(void)
{
    return PyModule_Create(&simpleextmodule);
}

Execute py -3.12 -m pip wheel .. Note that the build files are written to build/temp.win-amd64-cpython-312.

Execute py -3.13 -m pip wheel .. Note that the build files simpleext.cp313-win_amd64.exp, simpleext.cp313-win_amd64.lib, and simpleext.obj are now in the current directory.

Output

Output doesn't show the issue, but here it is.

For Python 3.13

Processing c:\users\adang\desktop\simpleext
  Installing build dependencies ... done
  Getting requirements to build wheel ... done
  Preparing metadata (pyproject.toml) ... done
Building wheels for collected packages: simpleext
  Building wheel for simpleext (pyproject.toml) ... done
  Created wheel for simpleext: filename=simpleext-0.0.1-cp313-cp313-win_amd64.whl size=5979 sha256=341e3d3e17f0c1ca17b27669320fa6c1c4650e14bde8e398ab9a2d36ab41a798
  Stored in directory: C:\Users\adang\AppData\Local\Temp\pip-ephem-wheel-cache-xtr732o6\wheels\27\4c\a5\0e20b9c70908c623502130d90e6b09da62188f03c1f769fa03
Successfully built simpleext

For Python 3.12

Processing c:\users\adang\desktop\simpleext
  Installing build dependencies ... done
  Getting requirements to build wheel ... done
  Preparing metadata (pyproject.toml) ... done
Building wheels for collected packages: simpleext
  Building wheel for simpleext (pyproject.toml) ... done
  Created wheel for simpleext: filename=simpleext-0.0.1-cp312-cp312-win_amd64.whl size=5980 sha256=03d7fcfca73e476b535fb2cf0db6e28a15e3d7903b8b49dd0e1277c9659d0a52
  Stored in directory: C:\Users\adang\AppData\Local\Temp\pip-ephem-wheel-cache-_kwgidxz\wheels\11\8a\f0\d510945bc3d119d0d87da3bbd42b3d54dc616cf09200d05ae0
Successfully built simpleext
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Needs Triage Issues that need to be evaluated for severity and status.
Projects
None yet
1 participant