summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJason R. Coombs <jaraco@jaraco.com>2021-11-22 10:03:31 -0500
committerGitHub <noreply@github.com>2021-11-22 10:03:31 -0500
commit85db7a412425756f90a9c43bac86b8c9ecdeb812 (patch)
treef987d660704454397f487a125354db4711344d39
parent514e9d03acb51ea93205c1fd0d336174c9481de7 (diff)
parent405c150c1d5393af2232526f77f39985d37b11fc (diff)
downloadpython-setuptools-git-85db7a412425756f90a9c43bac86b8c9ecdeb812.tar.gz
Merge pull request #71 from abravalheri/use-tokenize
Update `distutils.core.setup/run_setup` to better match `setuptools.build_meta`.
-rw-r--r--distutils/core.py55
-rw-r--r--distutils/tests/test_core.py25
2 files changed, 60 insertions, 20 deletions
diff --git a/distutils/core.py b/distutils/core.py
index d603d4a4..f43888ea 100644
--- a/distutils/core.py
+++ b/distutils/core.py
@@ -8,6 +8,7 @@ really defined in distutils.dist and distutils.cmd.
import os
import sys
+import tokenize
from distutils.debug import DEBUG
from distutils.errors import *
@@ -144,29 +145,41 @@ def setup (**attrs):
# And finally, run all the commands found on the command line.
if ok:
- try:
- dist.run_commands()
- except KeyboardInterrupt:
- raise SystemExit("interrupted")
- except OSError as exc:
- if DEBUG:
- sys.stderr.write("error: %s\n" % (exc,))
- raise
- else:
- raise SystemExit("error: %s" % (exc,))
-
- except (DistutilsError,
- CCompilerError) as msg:
- if DEBUG:
- raise
- else:
- raise SystemExit("error: " + str(msg))
+ return run_commands(dist)
return dist
# setup ()
+def run_commands (dist):
+ """Given a Distribution object run all the commands,
+ raising ``SystemExit`` errors in the case of failure.
+
+ This function assumes that either ``sys.argv`` or ``dist.script_args``
+ is already set accordingly.
+ """
+ try:
+ dist.run_commands()
+ except KeyboardInterrupt:
+ raise SystemExit("interrupted")
+ except OSError as exc:
+ if DEBUG:
+ sys.stderr.write("error: %s\n" % (exc,))
+ raise
+ else:
+ raise SystemExit("error: %s" % (exc,))
+
+ except (DistutilsError,
+ CCompilerError) as msg:
+ if DEBUG:
+ raise
+ else:
+ raise SystemExit("error: " + str(msg))
+
+ return dist
+
+
def run_setup (script_name, script_args=None, stop_after="run"):
"""Run a setup script in a somewhat controlled environment, and
return the Distribution instance that drives things. This is useful
@@ -205,14 +218,16 @@ def run_setup (script_name, script_args=None, stop_after="run"):
_setup_stop_after = stop_after
save_argv = sys.argv.copy()
- g = {'__file__': script_name}
+ g = {'__file__': script_name, '__name__': '__main__'}
try:
try:
sys.argv[0] = script_name
if script_args is not None:
sys.argv[1:] = script_args
- with open(script_name, 'rb') as f:
- exec(f.read(), g)
+ # tokenize.open supports automatic encoding detection
+ with tokenize.open(script_name) as f:
+ code = f.read().replace(r'\r\n', r'\n')
+ exec(code, g)
finally:
sys.argv = save_argv
_setup_stop_after = None
diff --git a/distutils/tests/test_core.py b/distutils/tests/test_core.py
index 666ff4a3..d99cfd26 100644
--- a/distutils/tests/test_core.py
+++ b/distutils/tests/test_core.py
@@ -10,6 +10,7 @@ from . import py38compat as os_helper
import unittest
from distutils.tests import support
from distutils import log
+from distutils.dist import Distribution
# setup script that uses __file__
setup_using___file__ = """\
@@ -45,6 +46,16 @@ class install(_install):
setup(cmdclass={'install': install})
"""
+setup_within_if_main = """\
+from distutils.core import setup
+
+def main():
+ return setup(name="setup_within_if_main")
+
+if __name__ == "__main__":
+ main()
+"""
+
class CoreTestCase(support.EnvironGuard, unittest.TestCase):
def setUp(self):
@@ -115,6 +126,20 @@ class CoreTestCase(support.EnvironGuard, unittest.TestCase):
output = output[:-1]
self.assertEqual(cwd, output)
+ def test_run_setup_within_if_main(self):
+ dist = distutils.core.run_setup(
+ self.write_setup(setup_within_if_main), stop_after="config")
+ self.assertIsInstance(dist, Distribution)
+ self.assertEqual(dist.get_name(), "setup_within_if_main")
+
+ def test_run_commands(self):
+ sys.argv = ['setup.py', 'build']
+ dist = distutils.core.run_setup(
+ self.write_setup(setup_within_if_main), stop_after="commandline")
+ self.assertNotIn('build', dist.have_run)
+ distutils.core.run_commands(dist)
+ self.assertIn('build', dist.have_run)
+
def test_debug_mode(self):
# this covers the code called when DEBUG is set
sys.argv = ['setup.py', '--name']