Removed the Requirement to Install Python and NodeJS (Now Bundled with Borealis)

This commit is contained in:
2025-04-24 00:42:19 -06:00
parent 785265d3e7
commit 9c68cdea84
7786 changed files with 2386458 additions and 217 deletions

View File

@ -0,0 +1,108 @@
# gh-91321: Build a basic C++ test extension to check that the Python C API is
# compatible with C++ and does not emit C++ compiler warnings.
import os.path
import shlex
import shutil
import subprocess
import unittest
from test import support
SOURCE = os.path.join(os.path.dirname(__file__), 'extension.cpp')
SETUP = os.path.join(os.path.dirname(__file__), 'setup.py')
# With MSVC on a debug build, the linker fails with: cannot open file
# 'python311.lib', it should look 'python311_d.lib'.
@unittest.skipIf(support.MS_WINDOWS and support.Py_DEBUG,
'test fails on Windows debug build')
# Building and running an extension in clang sanitizing mode is not
# straightforward
@support.skip_if_sanitizer('test does not work with analyzing builds',
address=True, memory=True, ub=True, thread=True)
# the test uses venv+pip: skip if it's not available
@support.requires_venv_with_pip()
@support.requires_subprocess()
@support.requires_resource('cpu')
class TestCPPExt(unittest.TestCase):
def test_build(self):
self.check_build('_testcppext')
def test_build_cpp03(self):
self.check_build('_testcpp03ext', std='c++03')
@unittest.skipIf(support.MS_WINDOWS, "MSVC doesn't support /std:c++11")
def test_build_cpp11(self):
self.check_build('_testcpp11ext', std='c++11')
# Only test C++14 on MSVC.
# On s390x RHEL7, GCC 4.8.5 doesn't support C++14.
@unittest.skipIf(not support.MS_WINDOWS, "need Windows")
def test_build_cpp14(self):
self.check_build('_testcpp14ext', std='c++14')
@support.requires_gil_enabled('incompatible with Free Threading')
def test_build_limited(self):
self.check_build('_testcppext_limited', limited=True)
def check_build(self, extension_name, std=None, limited=False):
venv_dir = 'env'
with support.setup_venv_with_pip_setuptools_wheel(venv_dir) as python_exe:
self._check_build(extension_name, python_exe,
std=std, limited=limited)
def _check_build(self, extension_name, python_exe, std, limited):
pkg_dir = 'pkg'
os.mkdir(pkg_dir)
shutil.copy(SETUP, os.path.join(pkg_dir, os.path.basename(SETUP)))
shutil.copy(SOURCE, os.path.join(pkg_dir, os.path.basename(SOURCE)))
def run_cmd(operation, cmd):
env = os.environ.copy()
if std:
env['CPYTHON_TEST_CPP_STD'] = std
if limited:
env['CPYTHON_TEST_LIMITED'] = '1'
env['CPYTHON_TEST_EXT_NAME'] = extension_name
if support.verbose:
print('Run:', ' '.join(map(shlex.quote, cmd)))
subprocess.run(cmd, check=True, env=env)
else:
proc = subprocess.run(cmd,
env=env,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
text=True)
if proc.returncode:
print('Run:', ' '.join(map(shlex.quote, cmd)))
print(proc.stdout, end='')
self.fail(
f"{operation} failed with exit code {proc.returncode}")
# Build and install the C++ extension
cmd = [python_exe, '-X', 'dev',
'-m', 'pip', 'install', '--no-build-isolation',
os.path.abspath(pkg_dir)]
if support.verbose:
cmd.append('-v')
run_cmd('Install', cmd)
# Do a reference run. Until we test that running python
# doesn't leak references (gh-94755), run it so one can manually check
# -X showrefcount results against this baseline.
cmd = [python_exe,
'-X', 'dev',
'-X', 'showrefcount',
'-c', 'pass']
run_cmd('Reference run', cmd)
# Import the C++ extension
cmd = [python_exe,
'-X', 'dev',
'-X', 'showrefcount',
'-c', f"import {extension_name}"]
run_cmd('Import', cmd)
if __name__ == "__main__":
unittest.main()

View File

@ -0,0 +1,271 @@
// gh-91321: Very basic C++ test extension to check that the Python C API is
// compatible with C++ and does not emit C++ compiler warnings.
//
// The code is only built, not executed.
// Always enable assertions
#undef NDEBUG
#include "Python.h"
#ifndef MODULE_NAME
# error "MODULE_NAME macro must be defined"
#endif
#define _STR(NAME) #NAME
#define STR(NAME) _STR(NAME)
PyDoc_STRVAR(_testcppext_add_doc,
"add(x, y)\n"
"\n"
"Return the sum of two integers: x + y.");
static PyObject *
_testcppext_add(PyObject *Py_UNUSED(module), PyObject *args)
{
long i, j;
if (!PyArg_ParseTuple(args, "ll:foo", &i, &j)) {
return _Py_NULL;
}
long res = i + j;
return PyLong_FromLong(res);
}
// Class to test operator casting an object to PyObject*
class StrongRef
{
public:
StrongRef(PyObject *obj) : m_obj(obj) {
Py_INCREF(this->m_obj);
}
~StrongRef() {
Py_DECREF(this->m_obj);
}
// Cast to PyObject*: get a borrowed reference
inline operator PyObject*() const { return this->m_obj; }
private:
PyObject *m_obj; // Strong reference
};
static PyObject *
test_api_casts(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(args))
{
PyObject *obj = Py_BuildValue("(ii)", 1, 2);
if (obj == _Py_NULL) {
return _Py_NULL;
}
Py_ssize_t refcnt = Py_REFCNT(obj);
assert(refcnt >= 1);
#ifndef Py_LIMITED_API
// gh-92138: For backward compatibility, functions of Python C API accepts
// "const PyObject*". Check that using it does not emit C++ compiler
// warnings.
const PyObject *const_obj = obj;
Py_INCREF(const_obj);
Py_DECREF(const_obj);
PyTypeObject *type = Py_TYPE(const_obj);
assert(Py_REFCNT(const_obj) == refcnt);
assert(type == &PyTuple_Type);
assert(PyTuple_GET_SIZE(const_obj) == 2);
PyObject *one = PyTuple_GET_ITEM(const_obj, 0);
assert(PyLong_AsLong(one) == 1);
#endif
// gh-92898: StrongRef doesn't inherit from PyObject but has an operator to
// cast to PyObject*.
StrongRef strong_ref(obj);
assert(Py_TYPE(strong_ref) == &PyTuple_Type);
assert(Py_REFCNT(strong_ref) == (refcnt + 1));
Py_INCREF(strong_ref);
Py_DECREF(strong_ref);
// gh-93442: Pass 0 as NULL for PyObject*
Py_XINCREF(0);
Py_XDECREF(0);
#if __cplusplus >= 201103
// Test nullptr passed as PyObject*
Py_XINCREF(nullptr);
Py_XDECREF(nullptr);
#endif
Py_DECREF(obj);
Py_RETURN_NONE;
}
static PyObject *
test_unicode(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(args))
{
PyObject *str = PyUnicode_FromString("abc");
if (str == _Py_NULL) {
return _Py_NULL;
}
assert(PyUnicode_Check(str));
assert(PyUnicode_GetLength(str) == 3);
assert(PyUnicode_ReadChar(str, 0) == 'a');
assert(PyUnicode_ReadChar(str, 1) == 'b');
#ifndef Py_LIMITED_API
assert(PyUnicode_GET_LENGTH(str) == 3);
// gh-92800: test PyUnicode_READ()
const void* data = PyUnicode_DATA(str);
assert(data != _Py_NULL);
int kind = PyUnicode_KIND(str);
assert(kind == PyUnicode_1BYTE_KIND);
assert(PyUnicode_READ(kind, data, 0) == 'a');
// gh-92800: test PyUnicode_READ() casts
const void* const_data = PyUnicode_DATA(str);
unsigned int ukind = static_cast<unsigned int>(kind);
assert(PyUnicode_READ(ukind, const_data, 2) == 'c');
assert(PyUnicode_READ_CHAR(str, 1) == 'b');
#endif
Py_DECREF(str);
Py_RETURN_NONE;
}
/* Test a `new`-allocated object with a virtual method.
* (https://github.com/python/cpython/issues/94731) */
class VirtualPyObject : public PyObject {
public:
VirtualPyObject();
virtual ~VirtualPyObject() {
delete [] internal_data;
--instance_count;
}
virtual void set_internal_data() {
internal_data[0] = 1;
}
static void dealloc(PyObject* o) {
delete static_cast<VirtualPyObject*>(o);
}
// Number of "living" instances
static int instance_count;
private:
// buffer that can get corrupted
int* internal_data;
};
int VirtualPyObject::instance_count = 0;
PyType_Slot VirtualPyObject_Slots[] = {
{Py_tp_free, (void*)VirtualPyObject::dealloc},
{0, _Py_NULL},
};
PyType_Spec VirtualPyObject_Spec = {
/* .name */ STR(MODULE_NAME) ".VirtualPyObject",
/* .basicsize */ sizeof(VirtualPyObject),
/* .itemsize */ 0,
/* .flags */ Py_TPFLAGS_DEFAULT,
/* .slots */ VirtualPyObject_Slots,
};
VirtualPyObject::VirtualPyObject() {
// Create a temporary type (just so we don't need to store it)
PyObject *type = PyType_FromSpec(&VirtualPyObject_Spec);
// no good way to signal failure from a C++ constructor, so use assert
// for error handling
assert(type);
assert(PyObject_Init(this, (PyTypeObject *)type));
Py_DECREF(type);
internal_data = new int[50];
++instance_count;
}
static PyObject *
test_virtual_object(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(args))
{
VirtualPyObject* obj = new VirtualPyObject();
obj->set_internal_data();
Py_DECREF(obj);
if (VirtualPyObject::instance_count != 0) {
return PyErr_Format(
PyExc_AssertionError,
"instance_count should be 0, got %d",
VirtualPyObject::instance_count);
}
Py_RETURN_NONE;
}
static PyMethodDef _testcppext_methods[] = {
{"add", _testcppext_add, METH_VARARGS, _testcppext_add_doc},
{"test_api_casts", test_api_casts, METH_NOARGS, _Py_NULL},
{"test_unicode", test_unicode, METH_NOARGS, _Py_NULL},
{"test_virtual_object", test_virtual_object, METH_NOARGS, _Py_NULL},
// Note: _testcppext_exec currently runs all test functions directly.
// When adding a new one, add a call there.
{_Py_NULL, _Py_NULL, 0, _Py_NULL} /* sentinel */
};
static int
_testcppext_exec(PyObject *module)
{
if (PyModule_AddIntMacro(module, __cplusplus) < 0) {
return -1;
}
PyObject *result;
result = PyObject_CallMethod(module, "test_api_casts", "");
if (!result) return -1;
Py_DECREF(result);
result = PyObject_CallMethod(module, "test_unicode", "");
if (!result) return -1;
Py_DECREF(result);
result = PyObject_CallMethod(module, "test_virtual_object", "");
if (!result) return -1;
Py_DECREF(result);
// test Py_BUILD_ASSERT() and Py_BUILD_ASSERT_EXPR()
Py_BUILD_ASSERT(sizeof(int) == sizeof(unsigned int));
assert(Py_BUILD_ASSERT_EXPR(sizeof(int) == sizeof(unsigned int)) == 0);
return 0;
}
static PyModuleDef_Slot _testcppext_slots[] = {
{Py_mod_exec, reinterpret_cast<void*>(_testcppext_exec)},
{0, _Py_NULL}
};
PyDoc_STRVAR(_testcppext_doc, "C++ test extension.");
static struct PyModuleDef _testcppext_module = {
PyModuleDef_HEAD_INIT, // m_base
STR(MODULE_NAME), // m_name
_testcppext_doc, // m_doc
0, // m_size
_testcppext_methods, // m_methods
_testcppext_slots, // m_slots
_Py_NULL, // m_traverse
_Py_NULL, // m_clear
_Py_NULL, // m_free
};
#define _FUNC_NAME(NAME) PyInit_ ## NAME
#define FUNC_NAME(NAME) _FUNC_NAME(NAME)
PyMODINIT_FUNC
FUNC_NAME(MODULE_NAME)(void)
{
return PyModuleDef_Init(&_testcppext_module);
}

View File

@ -0,0 +1,103 @@
# gh-91321: Build a basic C++ test extension to check that the Python C API is
# compatible with C++ and does not emit C++ compiler warnings.
import os
import platform
import shlex
import sysconfig
from test import support
from setuptools import setup, Extension
SOURCE = 'extension.cpp'
if not support.MS_WINDOWS:
# C++ compiler flags for GCC and clang
CPPFLAGS = [
# gh-91321: The purpose of _testcppext extension is to check that building
# a C++ extension using the Python C API does not emit C++ compiler
# warnings
'-Werror',
]
else:
# MSVC compiler flags
CPPFLAGS = [
# Display warnings level 1 to 4
'/W4',
# Treat all compiler warnings as compiler errors
'/WX',
]
def main():
cppflags = list(CPPFLAGS)
std = os.environ.get("CPYTHON_TEST_CPP_STD", "")
module_name = os.environ["CPYTHON_TEST_EXT_NAME"]
limited = bool(os.environ.get("CPYTHON_TEST_LIMITED", ""))
cppflags = list(CPPFLAGS)
cppflags.append(f'-DMODULE_NAME={module_name}')
# Add -std=STD or /std:STD (MSVC) compiler flag
if std:
if support.MS_WINDOWS:
cppflags.append(f'/std:{std}')
else:
cppflags.append(f'-std={std}')
# gh-105776: When "gcc -std=11" is used as the C++ compiler, -std=c11
# option emits a C++ compiler warning. Remove "-std11" option from the
# CC command.
cmd = (sysconfig.get_config_var('CC') or '')
if cmd is not None:
if support.MS_WINDOWS:
std_prefix = '/std'
else:
std_prefix = '-std'
cmd = shlex.split(cmd)
cmd = [arg for arg in cmd if not arg.startswith(std_prefix)]
cmd = shlex.join(cmd)
# CC env var overrides sysconfig CC variable in setuptools
os.environ['CC'] = cmd
# Define Py_LIMITED_API macro
if limited:
version = sys.hexversion
cppflags.append(f'-DPy_LIMITED_API={version:#x}')
# On Windows, add PCbuild\amd64\ to include and library directories
include_dirs = []
library_dirs = []
if support.MS_WINDOWS:
srcdir = sysconfig.get_config_var('srcdir')
machine = platform.uname().machine
pcbuild = os.path.join(srcdir, 'PCbuild', machine)
if os.path.exists(pcbuild):
# pyconfig.h is generated in PCbuild\amd64\
include_dirs.append(pcbuild)
# python313.lib is generated in PCbuild\amd64\
library_dirs.append(pcbuild)
print(f"Add PCbuild directory: {pcbuild}")
# Display information to help debugging
for env_name in ('CC', 'CFLAGS', 'CPPFLAGS'):
if env_name in os.environ:
print(f"{env_name} env var: {os.environ[env_name]!r}")
else:
print(f"{env_name} env var: <missing>")
print(f"extra_compile_args: {cppflags!r}")
ext = Extension(
module_name,
sources=[SOURCE],
language='c++',
extra_compile_args=cppflags,
include_dirs=include_dirs,
library_dirs=library_dirs)
setup(name=f'internal_{module_name}',
version='0.0',
ext_modules=[ext])
if __name__ == "__main__":
main()