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

gh-88402: Add new sysconfig variables on Windows (GH-110049) #110049

Merged
merged 10 commits into from
Oct 4, 2023
22 changes: 4 additions & 18 deletions Lib/sysconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -544,30 +544,16 @@ def _init_posix(vars):
def _init_non_posix(vars):
"""Initialize the module as appropriate for NT"""
# set basic install directories
import _imp
import re
import _winapi
vars['LIBDEST'] = get_path('stdlib')
vars['BINLIBDEST'] = get_path('platstdlib')
vars['INCLUDEPY'] = get_path('include')

extension_suffixes = _imp.extension_suffixes()
if len(extension_suffixes) >= 1:
# GH-99201: _imp.extension_suffixes may be empty when
# HAVE_DYNAMIC_LOADING is not set. In this case, don't set EXT_SUFFIX.
# e.g., "_d.cp313-win_amd64.pyd"
vars['EXT_SUFFIX'] = extension_suffixes[0]

# e.g., "cp313-win_amd64"
_, soabi, _ = vars['EXT_SUFFIX'].split('.')
vars['SOABI'] = soabi

# e.g., check for a "t" (for "threading") in SOABI
if re.match(r'cp\d+t', soabi):
vars['Py_NOGIL'] = 1
# Add EXT_SUFFIX, SOABI, and Py_NOGIL
vars.update(_winapi._sysconfig_vars())

vars['LIBPL'] = _safe_realpath(os.path.join(get_config_var('installed_base'), 'libs'))
vars['LIBDIR'] = _safe_realpath(os.path.join(get_config_var('installed_base'), 'libs'))
if hasattr(sys, 'dllhandle'):
FFY00 marked this conversation as resolved.
Show resolved Hide resolved
import _winapi
dllhandle = _winapi.GetModuleFileName(sys.dllhandle)
vars['LIBRARY'] = os.path.basename(_safe_realpath(dllhandle))
FFY00 marked this conversation as resolved.
Show resolved Hide resolved
vars['LDLIBRARY'] = vars['LIBRARY']
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
Add new variables to :py:meth:`sysconfig.get_config_vars` on Windows: ``LIBRARY``, ``LDLIBRARY``,
``LIBPL``, ``SOABI``, and ``Py_NOGIL``.
Add new variables to :py:meth:`sysconfig.get_config_vars` on Windows:
``LIBRARY``, ``LDLIBRARY``, ``LIBDIR``, ``SOABI``, and ``Py_NOGIL``.
29 changes: 29 additions & 0 deletions Modules/_winapi.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#include "pycore_moduleobject.h" // _PyModule_GetState()
#include "pycore_pylifecycle.h" // _Py_IsInterpreterFinalizing()
#include "pycore_pystate.h" // _PyInterpreterState_GET
#include "importdl.h" // _PyImport_DynLoadFiletab



Expand Down Expand Up @@ -2166,6 +2167,32 @@ _winapi_CopyFile2_impl(PyObject *module, LPCWSTR existing_file_name,
}


/*[clinic input]
_winapi._sysconfig_vars

Returns a dictionary containing variables intended to be exposed by sysconfig.
[clinic start generated code]*/

static PyObject *
_winapi__sysconfig_vars_impl(PyObject *module)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like this being here, I'd rather it be in the sys module. It's not a Windows API

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about putting it in a _sysconfig C module?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we're probably past due having one of those. We already smuggle constants into getbuildinfo.c (git info) and sysmodule.c (VPATH) on Windows, so may as well put them all into a _sysconfig.c that is importable.

Make it a built-in module though, not its own .pyd. PC/config.c lists the modules that are directly linked in, so it'll look like one of those.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can see the smuggling in PCbuild/pythoncore.vcxproj (search for GITVERSION= and VPATH=). Doesn't necessarily have to be done the same way, but copying it is going to be the easiest way to ensure incremental builds keep working normally.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've moved the computation to a _sysconfig C module. Thanks for the pointer to PC/config.c -- that made it easier. I don't think we need to smuggle data from PCbuild/pythoncore.vcxproj like we do for GITVERSION and VPATH here.

/*[clinic end generated code: output=8384e61338037cb1 input=065a8a08627f27bf]*/
{
PyObject *sysconfig = Py_BuildValue("{ssss}",
"EXT_SUFFIX", _PyImport_DynLoadFiletab[0],
"SOABI", _Py_SOABI);
if (sysconfig == NULL) {
return NULL;
}
#ifdef Py_NOGIL
if (PyDict_SetItem(sysconfig, "Py_NOGIL", _PyLong_GetOne()) < 0) {
Py_DECREF(sysconfig);
return NULL;
}
#endif
return sysconfig;
}


static PyMethodDef winapi_functions[] = {
_WINAPI_CLOSEHANDLE_METHODDEF
_WINAPI_CONNECTNAMEDPIPE_METHODDEF
Expand All @@ -2190,6 +2217,7 @@ static PyMethodDef winapi_functions[] = {
_WINAPI_LCMAPSTRINGEX_METHODDEF
_WINAPI_READFILE_METHODDEF
_WINAPI_SETNAMEDPIPEHANDLESTATE_METHODDEF
_WINAPI__SYSCONFIG_VARS_METHODDEF
_WINAPI_TERMINATEPROCESS_METHODDEF
_WINAPI_UNMAPVIEWOFFILE_METHODDEF
_WINAPI_VIRTUALQUERYSIZE_METHODDEF
Expand Down Expand Up @@ -2376,6 +2404,7 @@ static int winapi_exec(PyObject *m)

WINAPI_CONSTANT("i", NULL);


return 0;
}

Expand Down
20 changes: 19 additions & 1 deletion Modules/clinic/_winapi.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions Python/dynload_win.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,16 @@
#endif

#ifdef PYD_PLATFORM_TAG
#define PYD_TAGGED_SUFFIX PYD_DEBUG_SUFFIX ".cp" Py_STRINGIFY(PY_MAJOR_VERSION) Py_STRINGIFY(PY_MINOR_VERSION) PYD_THREADING_TAG "-" PYD_PLATFORM_TAG ".pyd"
# define PYD_SOABI "cp" Py_STRINGIFY(PY_MAJOR_VERSION) Py_STRINGIFY(PY_MINOR_VERSION) PYD_THREADING_TAG "-" PYD_PLATFORM_TAG
#else
#define PYD_TAGGED_SUFFIX PYD_DEBUG_SUFFIX ".cp" Py_STRINGIFY(PY_MAJOR_VERSION) Py_STRINGIFY(PY_MINOR_VERSION) PYD_THREADING_TAG ".pyd"
# define PYD_SOABI "cp" Py_STRINGIFY(PY_MAJOR_VERSION) Py_STRINGIFY(PY_MINOR_VERSION) PYD_THREADING_TAG
#endif

#define PYD_TAGGED_SUFFIX PYD_DEBUG_SUFFIX "." PYD_SOABI ".pyd"
#define PYD_UNTAGGED_SUFFIX PYD_DEBUG_SUFFIX ".pyd"

const char *_Py_SOABI = PYD_SOABI;

const char *_PyImport_DynLoadFiletab[] = {
PYD_TAGGED_SUFFIX,
PYD_UNTAGGED_SUFFIX,
Expand Down
1 change: 1 addition & 0 deletions Python/importdl.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ typedef PyObject *(*PyModInitFunction)(void);
#ifdef MS_WINDOWS
#include <windows.h>
typedef FARPROC dl_funcptr;
extern const char *_Py_SOABI;
#else
typedef void (*dl_funcptr)(void);
#endif
Expand Down