Debian Codegraph @

bin/pcython

raw
1 #!/usr/bin/env python
2
3 """
4 Script to run Cython with a Python command line.
5
6 Executes Python code by compiling it in Cython and running the compiled code.
7 """
8
9
10 import sys
11 import subprocess
12
13
14 def parse_args(args=None):
15 import optparse # tried argparse, but it doesn't stop at the first (interspersed) positional argument
16 parser = optparse.OptionParser(description='Run a Python command line with Cython')
17 parser.disable_interspersed_args()
18 parser.add_option('-c', metavar='CODE', dest='command',
19 help='program passed in as string')
20 parser.add_option('--python', metavar='PYTHON', dest='python', default=sys.executable,
21 help='Python interpreter to use')
22 parser.add_option('-X', metavar='NAME=VALUE', dest='directives', action='append',
23 help='Compiler directives to set during compilation')
24 parser.add_option('-V', '--version', action='store_true',
25 help='print the Python and Cython version numbers and exit')
26
27 return parser.parse_args(args)
28
29
30 def run_python(python, command):
31 subprocess.check_call([python, '-c', command])
32
33
34 def print_versions(python):
35 """Print version numbers of Python and Cython.
36 """
37 command = (
38 "import sys, Cython; "
39 "print('Python {}'.format('.'.join(map(str, sys.version_info[:3])))); "
40 "print('[Cython {}]'.format(Cython.__version__))"
41 )
42 run_python(python, command)
43
44
45 def run_cython_command(python, command, args=None):
46 """Compile and run a Python command string.
47 """
48 command = (
49 "import sys; "
50 "from Cython.Build.Inline import cython_inline; "
51 "sys.argv[1:] = {args!r}; "
52 "(lambda: cython_inline({code!r}, quiet=True))()"
53 ).format(code=command, args=list(args) if args else [])
54 run_python(python, command)
55
56
57 def run_python_stdin(python, file_args=None, directives=None):
58 """Compile and run a Python program from stdin.
59 """
60 import shutil
61 import tempfile
62
63 with tempfile.NamedTemporaryFile(suffix='.py') as f:
64 shutil.copyfileobj(sys.stdin, f)
65 f.flush()
66
67 file_args = [f.name] + list(file_args or ())
68 run_python_file(python, file_args, directives)
69
70
71 def run_python_file(python, file_args, directives=None):
72 """Compile and run a Python program from a file.
73 """
74 args = []
75 if directives:
76 for directive in directives:
77 args.extend(('-X', directive))
78 args.extend(file_args)
79 command = (
80 "import Cython.Build.BuildExecutable as bex; "
81 "bex.DEBUG = False; "
82 "bex.build_and_run({args!r})"
83 ).format(args=args)
84 run_python(python, command)
85
86
87 def main():
88 options, args = parse_args()
89 python = options.python
90
91 if options.version:
92 print_versions(python)
93 return
94
95 if options.command:
96 run_cython_command(python, options.command, args)
97
98 if args:
99 if args[0] == '-':
100 run_python_stdin(python, args[1:], options.directives)
101 else:
102 run_python_file(python, args, options.directives)
103
104
105 if __name__ == '__main__':
106 main()