8000 BLD lazy import of tempita to avoid cython dependency by jeremiedbb · Pull Request #15313 · scikit-learn/scikit-learn · GitHub
[go: up one dir, main page]

Skip to content

BLD lazy import of tempita to avoid cython dependency #15313

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Oct 25, 2019
Merged
Show file tree
Hide file tree
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
27 changes: 27 additions & 0 deletions sklearn/_build_utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,30 @@ def maybe_cythonize_extensions(top_path, config):
nthreads=n_jobs,
compile_time_env={'SKLEARN_OPENMP_SUPPORTED': with_openmp},
compiler_directives={'language_level': 3})


def gen_from_templates(templates, top_path):
"""Generate cython files from a list of templates"""
is_release = os.path.exists(os.path.join(top_path, 'PKG-INFO'))
# Files are already cythonized, nothing to do.
if is_release:
return

# Lazy import because cython is not a dependency when building from
# source distribution.
from Cython import Tempita # noqa

for template in templates:
outfile = template.replace('.tp', '')

# if the template is not updated, no need to output the cython file
if not (os.path.exists(outfile) and
os.stat(template).st_mtime < os.stat(outfile).st_mtime):

with open(template, "r") as f:
tmpl = f.read()

tmpl_ = Tempita.sub(tmpl)

with open(outfile, "w") as f:
f.write(tmpl_)
19 changes: 5 additions & 14 deletions sklearn/linear_model/setup.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import os

import numpy

from sklearn._build_utils import gen_from_templates
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

out of curiosity, can you explain me why an absolute import is required here ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't work if we use a relative import?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, they also did it for the deprecated modules in sklearn/setup.py
Maybe @thomasjpfan knows better about that ?



def configuration(parent_package='', top_path=None):
from numpy.distutils.misc_util import Configuration

Expand All @@ -22,19 +24,8 @@ def configuration(parent_package='', top_path=None):
libraries=libraries)

# generate sag_fast from template
sag_cython_file = 'sklearn/linear_model/_sag_fast.pyx.tp'
sag_file = sag_cython_file.replace('.tp', '')

if not (os.path.exists(sag_file) and
os.stat(sag_cython_file).st_mtime < os.stat(sag_file).st_mtime):

with open(sag_cython_file, "r") as f:
tmpl = f.read()
from Cython import Tempita # noqa
tmpl_ = Tempita.sub(tmpl)

with open(sag_file, "w") as f:
f.write(tmpl_)
templates = ['sklearn/linear_model/_sag_fast.pyx.tp']
gen_from_templates(templates, top_path)

config.add_extension('_sag_fast',
sources=['_sag_fast.pyx'],
Expand Down
24 changes: 6 additions & 18 deletions sklearn/utils/setup.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import os
from os.path import join

from sklearn._build_utils import gen_from_templates


def configuration(parent_package='', top_path=None):
import numpy
from numpy.distutils.misc_util import Configuration
from Cython import Tempita

config = Configuration('utils', parent_package, top_path)

Expand Down Expand Up @@ -45,23 +46,10 @@ def configuration(parent_package='', top_path=None):
sources=['_openmp_helpers.pyx'],
libraries=libraries)

# generate files from a template
pyx_templates = ['sklearn/utils/_seq_dataset.pyx.tp',
'sklearn/utils/_seq_dataset.pxd.tp']

for pyxfiles in pyx_templates:
outfile = pyxfiles.replace('.tp', '')
# if .pyx.tp is not updated, no need to output .pyx
if (os.path.exists(outfile) and
os.stat(pyxfiles).st_mtime < os.stat(outfile).st_mtime):
continue

with open(pyxfiles, "r") as f:
tmpl = f.read()
pyxcontent = Tempita.sub(tmpl)

with open(outfile, "w") as f:
f.write(pyxcontent)
# generate _seq_dataset from template
templates = ['sklearn/utils/_seq_dataset.pyx.tp',
'sklearn/utils/_seq_dataset.pxd.tp']
gen_from_templates(templates, top_path)

config.add_extension('_seq_dataset',
sources=['_seq_dataset.pyx'],
Expand Down
0