diff --git a/setup.py b/setup.py index 9a9d12ce4d2ba..78a789c808efb 100755 --- a/setup.py +++ b/setup.py @@ -7,6 +7,7 @@ """ import argparse +from distutils.command.build import build from distutils.sysconfig import get_config_vars from distutils.version import LooseVersion import multiprocessing @@ -16,10 +17,10 @@ import shutil import sys -import pkg_resources -from setuptools import Command, find_packages, setup +import numpy +from setuptools import Command, Extension, find_packages, setup +from setuptools.command.build_ext import build_ext as _build_ext -# versioning import versioneer cmdclass = versioneer.get_cmdclass() @@ -37,9 +38,7 @@ def is_platform_mac(): min_cython_ver = "0.29.21" # note: sync with pyproject.toml try: - import Cython - - _CYTHON_VERSION = Cython.__version__ + from Cython import Tempita, __version__ as _CYTHON_VERSION from Cython.Build import cythonize _CYTHON_INSTALLED = _CYTHON_VERSION >= LooseVersion(min_cython_ver) @@ -48,22 +47,6 @@ def is_platform_mac(): _CYTHON_INSTALLED = False cythonize = lambda x, *args, **kwargs: x # dummy func -# The import of Extension must be after the import of Cython, otherwise -# we do not get the appropriately patched class. -# See https://cython.readthedocs.io/en/latest/src/userguide/source_files_and_compilation.html # noqa -from distutils.extension import Extension # isort:skip -from distutils.command.build import build # isort:skip - -if _CYTHON_INSTALLED: - from Cython.Distutils.old_build_ext import old_build_ext as _build_ext - - cython = True - from Cython import Tempita as tempita -else: - from distutils.command.build_ext import build_ext as _build_ext - - cython = False - _pxi_dep_template = { "algos": ["_libs/algos_common_helper.pxi.in", "_libs/algos_take_helper.pxi.in"], @@ -101,7 +84,7 @@ def render_templates(cls, pxifiles): with open(pxifile) as f: tmpl = f.read() - pyxcontent = tempita.sub(tmpl) + pyxcontent = Tempita.sub(tmpl) with open(outfile, "w") as f: f.write(pyxcontent) @@ -109,7 +92,7 @@ def render_templates(cls, pxifiles): def build_extensions(self): # if building from c files, don't need to # generate template output - if cython: + if _CYTHON_INSTALLED: self.render_templates(_pxifiles) super().build_extensions() @@ -404,7 +387,7 @@ def run(self): cmdclass.update({"clean": CleanCommand, "build": build}) cmdclass["build_ext"] = CheckingBuildExt -if cython: +if _CYTHON_INSTALLED: suffix = ".pyx" cmdclass["cython"] = CythonCommand else: @@ -477,18 +460,10 @@ def run(self): directives["linetrace"] = True macros = [("CYTHON_TRACE", "1"), ("CYTHON_TRACE_NOGIL", "1")] -# in numpy>=1.16.0, silence build warnings about deprecated API usage -# we can't do anything about these warnings because they stem from -# cython+numpy version mismatches. +# silence build warnings about deprecated API usage +# we can't do anything about these warnings because they stem from +# cython+numpy version mismatches. macros.append(("NPY_NO_DEPRECATED_API", "0")) -if "-Werror" in extra_compile_args: - try: - import numpy as np - except ImportError: - pass - else: - if np.__version__ < LooseVersion("1.16.0"): - extra_compile_args.remove("-Werror") # ---------------------------------------------------------------------- @@ -507,7 +482,7 @@ def maybe_cythonize(extensions, *args, **kwargs): # See https://github.com/cython/cython/issues/1495 return extensions - elif not cython: + elif not _CYTHON_INSTALLED: # GH#28836 raise a helfpul error message if _CYTHON_VERSION: raise RuntimeError( @@ -516,25 +491,12 @@ def maybe_cythonize(extensions, *args, **kwargs): ) raise RuntimeError("Cannot cythonize without Cython installed.") - numpy_incl = pkg_resources.resource_filename("numpy", "core/include") - # TODO: Is this really necessary here? - for ext in extensions: - if hasattr(ext, "include_dirs") and numpy_incl not in ext.include_dirs: - ext.include_dirs.append(numpy_incl) - # reuse any parallel arguments provided for compilation to cythonize parser = argparse.ArgumentParser() - parser.add_argument("-j", type=int) - parser.add_argument("--parallel", type=int) + parser.add_argument("--parallel", "-j", type=int, default=1) parsed, _ = parser.parse_known_args() - nthreads = 0 - if parsed.parallel: - nthreads = parsed.parallel - elif parsed.j: - nthreads = parsed.j - - kwargs["nthreads"] = nthreads + kwargs["nthreads"] = parsed.parallel build_ext.render_templates(_pxifiles) return cythonize(extensions, *args, **kwargs) @@ -679,7 +641,8 @@ def srcpath(name=None, suffix=".pyx", subdir="src"): sources.extend(data.get("sources", [])) - include = data.get("include") + include = data.get("include", []) + include.append(numpy.get_include()) obj = Extension( f"pandas.{name}", @@ -728,6 +691,7 @@ def srcpath(name=None, suffix=".pyx", subdir="src"): "pandas/_libs/src/ujson/python", "pandas/_libs/src/ujson/lib", "pandas/_libs/src/datetime", + numpy.get_include(), ], extra_compile_args=(["-D_GNU_SOURCE"] + extra_compile_args), extra_link_args=extra_link_args,