Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
203 changes: 18 additions & 185 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

NAME = 'PyYAML'
VERSION = '7.0.0.dev0'
VERSION = '6.0.1'
DESCRIPTION = "YAML parser and emitter for Python"
LONG_DESCRIPTION = """\
YAML is a data serialization format designed for human readability
Expand Down Expand Up @@ -28,12 +28,12 @@
"Programming Language :: Cython",
"Programming Language :: Python",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.6",
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
"Programming Language :: Python :: 3.13",
"Programming Language :: Python :: Implementation :: CPython",
"Programming Language :: Python :: Implementation :: PyPy",
"Topic :: Software Development :: Libraries :: Python Modules",
Expand Down Expand Up @@ -70,8 +70,8 @@
# for newer setuptools, enable the embedded distutils before importing setuptools/distutils to avoid warnings
os.environ['SETUPTOOLS_USE_DISTUTILS'] = 'local'

from setuptools import setup, Command, Distribution as _Distribution, Extension as _Extension
from setuptools.command.build_ext import build_ext as _build_ext
from setuptools import setup, Command, Distribution, Extension
from setuptools.command.build_ext import build_ext
# NB: distutils imports must remain below setuptools to ensure we use the embedded version
from distutils import log
from distutils.errors import DistutilsError, CompileError, LinkError, DistutilsPlatformError
Expand All @@ -83,11 +83,10 @@
try:
from Cython.Distutils.extension import Extension as _Extension
try:
# try old_build_ext from Cython > 3 first, until we can dump it entirely
from Cython.Distutils.old_build_ext import old_build_ext as _build_ext
except ImportError:
# Cython < 3
from Cython.Distutils import build_ext as _build_ext

with_cython = True
except ImportError:
if with_cython:
Expand All @@ -99,14 +98,6 @@
bdist_wheel = None


try:
from _pyyaml_pep517 import ActiveConfigSettings
except ImportError:
class ActiveConfigSettings:
@staticmethod
def current():
return {}

# on Windows, disable wheel generation warning noise
windows_ignore_warnings = [
"Unknown distribution option: 'python_requires'",
Expand All @@ -121,163 +112,6 @@ def current():
warnings.filterwarnings('ignore', w)


class Distribution(_Distribution):
def __init__(self, attrs=None):
_Distribution.__init__(self, attrs)
if not self.ext_modules:
return
for idx in range(len(self.ext_modules)-1, -1, -1):
ext = self.ext_modules[idx]
if not isinstance(ext, Extension):
continue
setattr(self, ext.attr_name, None)
self.global_options = [
(ext.option_name, None,
"include %s (default if %s is available)"
% (ext.feature_description, ext.feature_name)),
(ext.neg_option_name, None,
"exclude %s" % ext.feature_description),
] + self.global_options
self.negative_opt = self.negative_opt.copy()
self.negative_opt[ext.neg_option_name] = ext.option_name

def has_ext_modules(self):
if not self.ext_modules:
return False
for ext in self.ext_modules:
with_ext = self.ext_status(ext)
if with_ext is None or with_ext:
return True
return False

def ext_status(self, ext):
implementation = platform.python_implementation()
if implementation not in ['CPython', 'PyPy']:
return False
if isinstance(ext, Extension):
# the "build by default" behavior is implemented by this returning None
with_ext = getattr(self, ext.attr_name) or os.environ.get('PYYAML_FORCE_{0}'.format(ext.feature_name.upper()))
try:
with_ext = int(with_ext) # attempt coerce envvar to int
except TypeError:
pass
return with_ext
else:
return True


class Extension(_Extension):

def __init__(self, name, sources, feature_name, feature_description,
feature_check, **kwds):
if not with_cython:
for filename in sources[:]:
base, ext = os.path.splitext(filename)
if ext == '.pyx':
sources.remove(filename)
sources.append('%s.c' % base)
_Extension.__init__(self, name, sources, **kwds)
self.feature_name = feature_name
self.feature_description = feature_description
self.feature_check = feature_check
self.attr_name = 'with_' + feature_name.replace('-', '_')
self.option_name = 'with-' + feature_name
self.neg_option_name = 'without-' + feature_name


class build_ext(_build_ext):
def finalize_options(self):
super().finalize_options()
pep517_config = ActiveConfigSettings.current()

build_config = pep517_config.get('pyyaml_build_config')

if build_config:
import json
build_config = json.loads(build_config)
print(f"`pyyaml_build_config`: {build_config}")
else:
build_config = {}
print("No `pyyaml_build_config` setting found.")

for key, value in build_config.items():
existing_value = getattr(self, key, ...)
if existing_value is ...:
print(f"ignoring unknown config key {key!r}")
continue

if existing_value:
print(f"combining {key!r} {existing_value!r} and {value!r}")
value = existing_value + value # FIXME: handle type diff

setattr(self, key, value)

def run(self):
optional = True
disabled = True
for ext in self.extensions:
with_ext = self.distribution.ext_status(ext)
if with_ext is None:
disabled = False
elif with_ext:
optional = False
disabled = False
break
if disabled:
return
try:
_build_ext.run(self)
except DistutilsPlatformError:
exc = sys.exc_info()[1]
if optional:
log.warn(str(exc))
log.warn("skipping build_ext")
else:
raise

def get_source_files(self):
self.check_extensions_list(self.extensions)
filenames = []
for ext in self.extensions:
if with_cython:
self.cython_sources(ext.sources, ext)
for filename in ext.sources:
filenames.append(filename)
base = os.path.splitext(filename)[0]
for ext in ['c', 'h', 'pyx', 'pxd']:
filename = '%s.%s' % (base, ext)
if filename not in filenames and os.path.isfile(filename):
filenames.append(filename)
return filenames

def get_outputs(self):
self.check_extensions_list(self.extensions)
outputs = []
for ext in self.extensions:
fullname = self.get_ext_fullname(ext.name)
filename = os.path.join(self.build_lib,
self.get_ext_filename(fullname))
if os.path.isfile(filename):
outputs.append(filename)
return outputs

def build_extensions(self):
self.check_extensions_list(self.extensions)
for ext in self.extensions:
with_ext = self.distribution.ext_status(ext)
if with_ext is not None and not with_ext:
continue
if with_cython:
print(f"BUILDING CYTHON EXT; {self.include_dirs=} {self.library_dirs=} {self.define=}")
ext.sources = self.cython_sources(ext.sources, ext)
try:
self.build_extension(ext)
except (CompileError, LinkError):
if with_ext is not None:
raise
log.warn("Error compiling module, falling back to pure Python")


class test(Command):

user_options = []
Expand All @@ -289,7 +123,6 @@ def finalize_options(self):
pass

def run(self):
warnings.warn('Running tests via `setup.py test` is deprecated and will be removed in a future release. Use `pytest` instead to ensure that the complete test suite is run.', DeprecationWarning)
build_cmd = self.get_finalized_command('build')
build_cmd.run()

Expand All @@ -299,16 +132,11 @@ def run(self):
tempdir = tempfile.TemporaryDirectory(prefix='test_pyyaml')

try:
warnings.warn(
"Direct invocation of `setup.py` is deprecated by `setuptools` and will be removed in a future release. PyYAML tests should be run via `pytest`.",
DeprecationWarning,
)

# have to create a subdir since we don't get dir_exists_ok on copytree until 3.8
temp_test_path = pathlib.Path(tempdir.name) / 'pyyaml'
shutil.copytree(build_cmd.build_lib, temp_test_path)
sys.path.insert(0, str(temp_test_path))
sys.path.insert(0, 'tests/legacy_tests')
sys.path.insert(0, 'tests/lib')

import test_all
if not test_all.main([]):
Expand All @@ -330,6 +158,15 @@ def run(self):


if __name__ == '__main__':
from Cython.Build import cythonize

ext_modules = cythonize([
Extension(
name='yaml._yaml',
sources=['yaml/_yaml.pyx'],
libraries=['yaml'],
)
])

setup(
name=NAME,
Expand All @@ -347,13 +184,9 @@ def run(self):

package_dir={'': 'lib'},
packages=['yaml', '_yaml'],
ext_modules=[
Extension('yaml._yaml', ['yaml/_yaml.pyx'],
'libyaml', "LibYAML bindings", LIBYAML_CHECK,
libraries=['yaml']),
],
ext_modules=ext_modules,

distclass=Distribution,
cmdclass=cmdclass,
python_requires='>=3.8',
python_requires='>=3.6',
)