• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1
2NAME = 'PyYAML'
3VERSION = '6.0.2'
4DESCRIPTION = "YAML parser and emitter for Python"
5LONG_DESCRIPTION = """\
6YAML is a data serialization format designed for human readability
7and interaction with scripting languages.  PyYAML is a YAML parser
8and emitter for Python.
9
10PyYAML features a complete YAML 1.1 parser, Unicode support, pickle
11support, capable extension API, and sensible error messages.  PyYAML
12supports standard YAML tags and provides Python-specific tags that
13allow to represent an arbitrary Python object.
14
15PyYAML is applicable for a broad range of tasks from complex
16configuration files to object serialization and persistence."""
17AUTHOR = "Kirill Simonov"
18AUTHOR_EMAIL = 'xi@resolvent.net'
19LICENSE = "MIT"
20PLATFORMS = "Any"
21URL = "https://pyyaml.org/"
22DOWNLOAD_URL = "https://pypi.org/project/PyYAML/"
23CLASSIFIERS = [
24    "Development Status :: 5 - Production/Stable",
25    "Intended Audience :: Developers",
26    "License :: OSI Approved :: MIT License",
27    "Operating System :: OS Independent",
28    "Programming Language :: Cython",
29    "Programming Language :: Python",
30    "Programming Language :: Python :: 3",
31    "Programming Language :: Python :: 3.8",
32    "Programming Language :: Python :: 3.9",
33    "Programming Language :: Python :: 3.10",
34    "Programming Language :: Python :: 3.11",
35    "Programming Language :: Python :: 3.12",
36    "Programming Language :: Python :: 3.13",
37    "Programming Language :: Python :: Implementation :: CPython",
38    "Programming Language :: Python :: Implementation :: PyPy",
39    "Topic :: Software Development :: Libraries :: Python Modules",
40    "Topic :: Text Processing :: Markup",
41]
42PROJECT_URLS = {
43   'Bug Tracker': 'https://github.com/yaml/pyyaml/issues',
44   'CI': 'https://github.com/yaml/pyyaml/actions',
45   'Documentation': 'https://pyyaml.org/wiki/PyYAMLDocumentation',
46   'Mailing lists': 'http://lists.sourceforge.net/lists/listinfo/yaml-core',
47   'Source Code': 'https://github.com/yaml/pyyaml',
48}
49
50LIBYAML_CHECK = """
51#include <yaml.h>
52
53int main(void) {
54    yaml_parser_t parser;
55    yaml_emitter_t emitter;
56
57    yaml_parser_initialize(&parser);
58    yaml_parser_delete(&parser);
59
60    yaml_emitter_initialize(&emitter);
61    yaml_emitter_delete(&emitter);
62
63    return 0;
64}
65"""
66
67
68import sys, os, os.path, pathlib, platform, shutil, tempfile, warnings
69
70# for newer setuptools, enable the embedded distutils before importing setuptools/distutils to avoid warnings
71os.environ['SETUPTOOLS_USE_DISTUTILS'] = 'local'
72
73from setuptools import setup, Command, Distribution as _Distribution, Extension as _Extension
74from setuptools.command.build_ext import build_ext as _build_ext
75# NB: distutils imports must remain below setuptools to ensure we use the embedded version
76from distutils import log
77from distutils.errors import DistutilsError, CompileError, LinkError, DistutilsPlatformError
78
79with_cython = False
80if 'sdist' in sys.argv or os.environ.get('PYYAML_FORCE_CYTHON') == '1':
81    # we need cython here
82    with_cython = True
83try:
84    from Cython.Distutils.extension import Extension as _Extension
85    try:
86        # try old_build_ext from Cython > 3 first, until we can dump it entirely
87        from Cython.Distutils.old_build_ext import old_build_ext as _build_ext
88    except ImportError:
89        # Cython < 3
90        from Cython.Distutils import build_ext as _build_ext
91    with_cython = True
92except ImportError:
93    if with_cython:
94        raise
95
96try:
97    from wheel.bdist_wheel import bdist_wheel
98except ImportError:
99    bdist_wheel = None
100
101
102try:
103    from _pyyaml_pep517 import ActiveConfigSettings
104except ImportError:
105    class ActiveConfigSettings:
106        @staticmethod
107        def current():
108            return {}
109
110# on Windows, disable wheel generation warning noise
111windows_ignore_warnings = [
112"Unknown distribution option: 'python_requires'",
113"Config variable 'Py_DEBUG' is unset",
114"Config variable 'WITH_PYMALLOC' is unset",
115"Config variable 'Py_UNICODE_SIZE' is unset",
116"Cython directive 'language_level' not set"
117]
118
119if platform.system() == 'Windows':
120    for w in windows_ignore_warnings:
121        warnings.filterwarnings('ignore', w)
122
123
124class Distribution(_Distribution):
125    def __init__(self, attrs=None):
126        _Distribution.__init__(self, attrs)
127        if not self.ext_modules:
128            return
129        for idx in range(len(self.ext_modules)-1, -1, -1):
130            ext = self.ext_modules[idx]
131            if not isinstance(ext, Extension):
132                continue
133            setattr(self, ext.attr_name, None)
134            self.global_options = [
135                    (ext.option_name, None,
136                        "include %s (default if %s is available)"
137                        % (ext.feature_description, ext.feature_name)),
138                    (ext.neg_option_name, None,
139                        "exclude %s" % ext.feature_description),
140            ] + self.global_options
141            self.negative_opt = self.negative_opt.copy()
142            self.negative_opt[ext.neg_option_name] = ext.option_name
143
144    def has_ext_modules(self):
145        if not self.ext_modules:
146            return False
147        for ext in self.ext_modules:
148            with_ext = self.ext_status(ext)
149            if with_ext is None or with_ext:
150                return True
151        return False
152
153    def ext_status(self, ext):
154        implementation = platform.python_implementation()
155        if implementation not in ['CPython', 'PyPy']:
156            return False
157        if isinstance(ext, Extension):
158            # the "build by default" behavior is implemented by this returning None
159            with_ext = getattr(self, ext.attr_name) or os.environ.get('PYYAML_FORCE_{0}'.format(ext.feature_name.upper()))
160            try:
161                with_ext = int(with_ext)  # attempt coerce envvar to int
162            except TypeError:
163                pass
164            return with_ext
165        else:
166            return True
167
168
169class Extension(_Extension):
170
171    def __init__(self, name, sources, feature_name, feature_description,
172            feature_check, **kwds):
173        if not with_cython:
174            for filename in sources[:]:
175                base, ext = os.path.splitext(filename)
176                if ext == '.pyx':
177                    sources.remove(filename)
178                    sources.append('%s.c' % base)
179        _Extension.__init__(self, name, sources, **kwds)
180        self.feature_name = feature_name
181        self.feature_description = feature_description
182        self.feature_check = feature_check
183        self.attr_name = 'with_' + feature_name.replace('-', '_')
184        self.option_name = 'with-' + feature_name
185        self.neg_option_name = 'without-' + feature_name
186
187
188class build_ext(_build_ext):
189    def finalize_options(self):
190        super().finalize_options()
191        pep517_config = ActiveConfigSettings.current()
192
193        build_config = pep517_config.get('pyyaml_build_config')
194
195        if build_config:
196            import json
197            build_config = json.loads(build_config)
198            print(f"`pyyaml_build_config`: {build_config}")
199        else:
200            build_config = {}
201            print("No `pyyaml_build_config` setting found.")
202
203        for key, value in build_config.items():
204            existing_value = getattr(self, key, ...)
205            if existing_value is ...:
206                print(f"ignoring unknown config key {key!r}")
207                continue
208
209            if existing_value:
210                print(f"combining {key!r} {existing_value!r} and {value!r}")
211                value = existing_value + value  # FIXME: handle type diff
212
213            setattr(self, key, value)
214
215    def run(self):
216        optional = True
217        disabled = True
218        for ext in self.extensions:
219            with_ext = self.distribution.ext_status(ext)
220            if with_ext is None:
221                disabled = False
222            elif with_ext:
223                optional = False
224                disabled = False
225                break
226        if disabled:
227            return
228        try:
229            _build_ext.run(self)
230        except DistutilsPlatformError:
231            exc = sys.exc_info()[1]
232            if optional:
233                log.warn(str(exc))
234                log.warn("skipping build_ext")
235            else:
236                raise
237
238    def get_source_files(self):
239        self.check_extensions_list(self.extensions)
240        filenames = []
241        for ext in self.extensions:
242            if with_cython:
243                self.cython_sources(ext.sources, ext)
244            for filename in ext.sources:
245                filenames.append(filename)
246                base = os.path.splitext(filename)[0]
247                for ext in ['c', 'h', 'pyx', 'pxd']:
248                    filename = '%s.%s' % (base, ext)
249                    if filename not in filenames and os.path.isfile(filename):
250                        filenames.append(filename)
251        return filenames
252
253    def get_outputs(self):
254        self.check_extensions_list(self.extensions)
255        outputs = []
256        for ext in self.extensions:
257            fullname = self.get_ext_fullname(ext.name)
258            filename = os.path.join(self.build_lib,
259                                    self.get_ext_filename(fullname))
260            if os.path.isfile(filename):
261                outputs.append(filename)
262        return outputs
263
264    def build_extensions(self):
265        self.check_extensions_list(self.extensions)
266        for ext in self.extensions:
267            with_ext = self.distribution.ext_status(ext)
268            if with_ext is not None and not with_ext:
269                continue
270            if with_cython:
271                print(f"BUILDING CYTHON EXT; {self.include_dirs=} {self.library_dirs=} {self.define=}")
272                ext.sources = self.cython_sources(ext.sources, ext)
273            try:
274                self.build_extension(ext)
275            except (CompileError, LinkError):
276                if with_ext is not None:
277                    raise
278                log.warn("Error compiling module, falling back to pure Python")
279
280
281class test(Command):
282
283    user_options = []
284
285    def initialize_options(self):
286        pass
287
288    def finalize_options(self):
289        pass
290
291    def run(self):
292        build_cmd = self.get_finalized_command('build')
293        build_cmd.run()
294
295        # running the tests this way can pollute the post-MANIFEST build sources
296        # (see https://github.com/yaml/pyyaml/issues/527#issuecomment-921058344)
297        # until we remove the test command, run tests from an ephemeral copy of the intermediate build sources
298        tempdir = tempfile.TemporaryDirectory(prefix='test_pyyaml')
299
300        try:
301            warnings.warn(
302                "Direct invocation of `setup.py` is deprecated by `setuptools` and will be removed in a future release. PyYAML tests should be run via `pytest`.",
303                DeprecationWarning,
304            )
305
306            # have to create a subdir since we don't get dir_exists_ok on copytree until 3.8
307            temp_test_path = pathlib.Path(tempdir.name) / 'pyyaml'
308            shutil.copytree(build_cmd.build_lib, temp_test_path)
309            sys.path.insert(0, str(temp_test_path))
310            sys.path.insert(0, 'tests/lib')
311
312            import test_all
313            if not test_all.main([]):
314                raise DistutilsError("Tests failed")
315        finally:
316            try:
317                # this can fail under Windows; best-effort cleanup
318                tempdir.cleanup()
319            except Exception:
320                pass
321
322
323cmdclass = {
324    'build_ext': build_ext,
325    'test': test,
326}
327if bdist_wheel:
328    cmdclass['bdist_wheel'] = bdist_wheel
329
330
331if __name__ == '__main__':
332
333    setup(
334        name=NAME,
335        version=VERSION,
336        description=DESCRIPTION,
337        long_description=LONG_DESCRIPTION,
338        author=AUTHOR,
339        author_email=AUTHOR_EMAIL,
340        license=LICENSE,
341        platforms=PLATFORMS,
342        url=URL,
343        download_url=DOWNLOAD_URL,
344        classifiers=CLASSIFIERS,
345        project_urls=PROJECT_URLS,
346
347        package_dir={'': 'lib'},
348        packages=['yaml', '_yaml'],
349        ext_modules=[
350            Extension('yaml._yaml', ['yaml/_yaml.pyx'],
351                'libyaml', "LibYAML bindings", LIBYAML_CHECK,
352                libraries=['yaml']),
353        ],
354
355        distclass=Distribution,
356        cmdclass=cmdclass,
357        python_requires='>=3.8',
358    )
359