diff options
author | Stefan Behnel <stefan_ml@behnel.de> | 2021-06-09 11:48:11 +0200 |
---|---|---|
committer | Stefan Behnel <stefan_ml@behnel.de> | 2021-06-09 11:49:08 +0200 |
commit | b0b207db71600d480fd7ae6fe43e058b3a0cd51b (patch) | |
tree | 4a36c81a9059ed43db4772a7bb4cb3b675be8444 | |
parent | 40758c6b5f853f42d1ea167f05db74c33a358ed5 (diff) | |
download | cython-b0b207db71600d480fd7ae6fe43e058b3a0cd51b.tar.gz |
Split the interface of "cythonrun" and "BuildExecutable" so that the latter no longer automatically runs the program.
-rw-r--r-- | CHANGES.rst | 6 | ||||
-rw-r--r-- | Cython/Build/BuildExecutable.py | 61 | ||||
-rwxr-xr-x | bin/cythonrun | 2 |
3 files changed, 47 insertions, 22 deletions
diff --git a/CHANGES.rst b/CHANGES.rst index 233a2096e..b1ef97dc6 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -26,6 +26,12 @@ Bugs fixed to match the signature in Py3. It still has an automatic fallback for Py2. (Github issue :issue:`3909`) +Other changes +------------- + +* The ``Cython.Build.BuildExecutable`` tool no longer executes the program automatically. + Use ``cythonrun`` for that. + 3.0.0 alpha 7 (2021-05-24) ========================== diff --git a/Cython/Build/BuildExecutable.py b/Cython/Build/BuildExecutable.py index 57dd10ac6..334fbf069 100644 --- a/Cython/Build/BuildExecutable.py +++ b/Cython/Build/BuildExecutable.py @@ -1,10 +1,10 @@ """ -Compile a Python script into an executable that embeds CPython and run it. +Compile a Python script into an executable that embeds CPython. Requires CPython to be built as a shared library ('libpythonX.Y'). Basic usage: - python cythonrun somefile.py [ARGS] + python -m Cython.Build.BuildExecutable [ARGS] somefile.py """ from __future__ import absolute_import @@ -38,12 +38,14 @@ LIBS = get_config_var('LIBS') SYSLIBS = get_config_var('SYSLIBS') EXE_EXT = sysconfig.get_config_var('EXE') + def _debug(msg, *args): if DEBUG: if args: msg = msg % args sys.stderr.write(msg + '\n') + def dump_config(): _debug('INCDIR: %s', INCDIR) _debug('LIBDIR1: %s', LIBDIR1) @@ -58,6 +60,26 @@ def dump_config(): _debug('SYSLIBS: %s', SYSLIBS) _debug('EXE_EXT: %s', EXE_EXT) + +def _parse_args(args): + cy_args = [] + last_arg = None + for i, arg in enumerate(args): + if arg.startswith('-'): + cy_args.append(arg) + elif last_arg in ('-X', '--directive'): + cy_args.append(arg) + else: + input_file = arg + args = args[i+1:] + break + last_arg = arg + else: + raise ValueError('no input file provided') + + return input_file, cy_args, args + + def runcmd(cmd, shell=True): if shell: cmd = ' '.join(cmd) @@ -71,14 +93,17 @@ def runcmd(cmd, shell=True): if returncode: sys.exit(returncode) + def clink(basename): runcmd([LINKCC, '-o', basename + EXE_EXT, basename+'.o', '-L'+LIBDIR1, '-L'+LIBDIR2] + [PYLIB_DYN and ('-l'+PYLIB_DYN) or os.path.join(LIBDIR1, PYLIB)] + LIBS.split() + SYSLIBS.split() + LINKFORSHARED.split()) + def ccompile(basename): runcmd([CC, '-c', '-o', basename+'.o', basename+'.c', '-I' + INCDIR] + CFLAGS.split()) + def cycompile(input_file, options=()): from ..Compiler import Version, CmdLine, Main options, sources = CmdLine.parse_command_line(list(options or ()) + ['--embed', input_file]) @@ -87,9 +112,11 @@ def cycompile(input_file, options=()): if result.num_errors > 0: sys.exit(1) + def exec_file(program_name, args=()): runcmd([os.path.abspath(program_name)] + list(args), shell=False) + def build(input_file, compiler_args=(), force=False): """ Build an executable program from a Cython module. @@ -109,30 +136,22 @@ def build(input_file, compiler_args=(), force=False): clink(basename) return exe_file + def build_and_run(args): """ - Build an executable program from a Cython module and runs it. + Build an executable program from a Cython module and run it. - Arguments after the module name will be passed verbatimely to the - program. + Arguments after the module name will be passed verbatimly to the program. """ - cy_args = [] - last_arg = None - for i, arg in enumerate(args): - if arg.startswith('-'): - cy_args.append(arg) - elif last_arg in ('-X', '--directive'): - cy_args.append(arg) - else: - input_file = arg - args = args[i+1:] - break - last_arg = arg - else: - raise ValueError('no input file provided') + program_name, args = _build(args) + exec_file(program_name, args) + +def _build(args): + input_file, cy_args, args = _parse_args(args) program_name = build(input_file, cy_args) - exec_file(program_name, args) + return program_name, args + if __name__ == '__main__': - build_and_run(sys.argv[1:]) + _build(sys.argv[1:]) diff --git a/bin/cythonrun b/bin/cythonrun index 1c6195492..f09e1d58d 100755 --- a/bin/cythonrun +++ b/bin/cythonrun @@ -9,7 +9,7 @@ Basic usage: python cythonrun somefile.py [ARGS] """ -from Cython.Build.BuildExecutable import build, build_and_run +from Cython.Build.BuildExecutable import build_and_run if __name__ == '__main__': import sys |