Skip to content

Commit fee6675

Browse files
authored
CLN: clean setup.py (#37732)
1 parent b80691c commit fee6675

File tree

1 file changed

+17
-53
lines changed

1 file changed

+17
-53
lines changed

setup.py

+17-53
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"""
88

99
import argparse
10+
from distutils.command.build import build
1011
from distutils.sysconfig import get_config_vars
1112
from distutils.version import LooseVersion
1213
import multiprocessing
@@ -16,10 +17,10 @@
1617
import shutil
1718
import sys
1819

19-
import pkg_resources
20-
from setuptools import Command, find_packages, setup
20+
import numpy
21+
from setuptools import Command, Extension, find_packages, setup
22+
from setuptools.command.build_ext import build_ext as _build_ext
2123

22-
# versioning
2324
import versioneer
2425

2526
cmdclass = versioneer.get_cmdclass()
@@ -37,9 +38,7 @@ def is_platform_mac():
3738
min_cython_ver = "0.29.21" # note: sync with pyproject.toml
3839

3940
try:
40-
import Cython
41-
42-
_CYTHON_VERSION = Cython.__version__
41+
from Cython import Tempita, __version__ as _CYTHON_VERSION
4342
from Cython.Build import cythonize
4443

4544
_CYTHON_INSTALLED = _CYTHON_VERSION >= LooseVersion(min_cython_ver)
@@ -48,22 +47,6 @@ def is_platform_mac():
4847
_CYTHON_INSTALLED = False
4948
cythonize = lambda x, *args, **kwargs: x # dummy func
5049

51-
# The import of Extension must be after the import of Cython, otherwise
52-
# we do not get the appropriately patched class.
53-
# See https://cython.readthedocs.io/en/latest/src/userguide/source_files_and_compilation.html # noqa
54-
from distutils.extension import Extension # isort:skip
55-
from distutils.command.build import build # isort:skip
56-
57-
if _CYTHON_INSTALLED:
58-
from Cython.Distutils.old_build_ext import old_build_ext as _build_ext
59-
60-
cython = True
61-
from Cython import Tempita as tempita
62-
else:
63-
from distutils.command.build_ext import build_ext as _build_ext
64-
65-
cython = False
66-
6750

6851
_pxi_dep_template = {
6952
"algos": ["_libs/algos_common_helper.pxi.in", "_libs/algos_take_helper.pxi.in"],
@@ -101,15 +84,15 @@ def render_templates(cls, pxifiles):
10184

10285
with open(pxifile) as f:
10386
tmpl = f.read()
104-
pyxcontent = tempita.sub(tmpl)
87+
pyxcontent = Tempita.sub(tmpl)
10588

10689
with open(outfile, "w") as f:
10790
f.write(pyxcontent)
10891

10992
def build_extensions(self):
11093
# if building from c files, don't need to
11194
# generate template output
112-
if cython:
95+
if _CYTHON_INSTALLED:
11396
self.render_templates(_pxifiles)
11497

11598
super().build_extensions()
@@ -404,7 +387,7 @@ def run(self):
404387
cmdclass.update({"clean": CleanCommand, "build": build})
405388
cmdclass["build_ext"] = CheckingBuildExt
406389

407-
if cython:
390+
if _CYTHON_INSTALLED:
408391
suffix = ".pyx"
409392
cmdclass["cython"] = CythonCommand
410393
else:
@@ -477,18 +460,10 @@ def run(self):
477460
directives["linetrace"] = True
478461
macros = [("CYTHON_TRACE", "1"), ("CYTHON_TRACE_NOGIL", "1")]
479462

480-
# in numpy>=1.16.0, silence build warnings about deprecated API usage
481-
# we can't do anything about these warnings because they stem from
482-
# cython+numpy version mismatches.
463+
# silence build warnings about deprecated API usage
464+
# we can't do anything about these warnings because they stem from
465+
# cython+numpy version mismatches.
483466
macros.append(("NPY_NO_DEPRECATED_API", "0"))
484-
if "-Werror" in extra_compile_args:
485-
try:
486-
import numpy as np
487-
except ImportError:
488-
pass
489-
else:
490-
if np.__version__ < LooseVersion("1.16.0"):
491-
extra_compile_args.remove("-Werror")
492467

493468

494469
# ----------------------------------------------------------------------
@@ -507,7 +482,7 @@ def maybe_cythonize(extensions, *args, **kwargs):
507482
# See https://github.com/cython/cython/issues/1495
508483
return extensions
509484

510-
elif not cython:
485+
elif not _CYTHON_INSTALLED:
511486
# GH#28836 raise a helfpul error message
512487
if _CYTHON_VERSION:
513488
raise RuntimeError(
@@ -516,25 +491,12 @@ def maybe_cythonize(extensions, *args, **kwargs):
516491
)
517492
raise RuntimeError("Cannot cythonize without Cython installed.")
518493

519-
numpy_incl = pkg_resources.resource_filename("numpy", "core/include")
520-
# TODO: Is this really necessary here?
521-
for ext in extensions:
522-
if hasattr(ext, "include_dirs") and numpy_incl not in ext.include_dirs:
523-
ext.include_dirs.append(numpy_incl)
524-
525494
# reuse any parallel arguments provided for compilation to cythonize
526495
parser = argparse.ArgumentParser()
527-
parser.add_argument("-j", type=int)
528-
parser.add_argument("--parallel", type=int)
496+
parser.add_argument("--parallel", "-j", type=int, default=1)
529497
parsed, _ = parser.parse_known_args()
530498

531-
nthreads = 0
532-
if parsed.parallel:
533-
nthreads = parsed.parallel
534-
elif parsed.j:
535-
nthreads = parsed.j
536-
537-
kwargs["nthreads"] = nthreads
499+
kwargs["nthreads"] = parsed.parallel
538500
build_ext.render_templates(_pxifiles)
539501
return cythonize(extensions, *args, **kwargs)
540502

@@ -679,7 +641,8 @@ def srcpath(name=None, suffix=".pyx", subdir="src"):
679641

680642
sources.extend(data.get("sources", []))
681643

682-
include = data.get("include")
644+
include = data.get("include", [])
645+
include.append(numpy.get_include())
683646

684647
obj = Extension(
685648
f"pandas.{name}",
@@ -728,6 +691,7 @@ def srcpath(name=None, suffix=".pyx", subdir="src"):
728691
"pandas/_libs/src/ujson/python",
729692
"pandas/_libs/src/ujson/lib",
730693
"pandas/_libs/src/datetime",
694+
numpy.get_include(),
731695
],
732696
extra_compile_args=(["-D_GNU_SOURCE"] + extra_compile_args),
733697
extra_link_args=extra_link_args,

0 commit comments

Comments
 (0)