Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Labels
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Commits
Open sidebar
nexedi
cython
Commits
594c1d3b
Commit
594c1d3b
authored
Aug 18, 2016
by
Stefan Behnel
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add script "bin/pcython" to run Cython with a Python-like command line interface
parent
9cc4da5a
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
107 additions
and
0 deletions
+107
-0
bin/pcython
bin/pcython
+107
-0
No files found.
bin/pcython
0 → 100755
View file @
594c1d3b
#!/usr/bin/env python
"""
Script to run Cython with a Python command line.
Executes Python code by compiling it in Cython and running the compiled code.
"""
import
sys
import
subprocess
def
parse_args
(
args
=
None
):
import
argparse
parser
=
argparse
.
ArgumentParser
(
description
=
'Run a Python command line with Cython'
)
parser
.
add_argument
(
'-c'
,
metavar
=
'CODE'
,
dest
=
'command'
,
help
=
'program passed in as string'
)
parser
.
add_argument
(
'--python'
,
metavar
=
'PYTHON'
,
dest
=
'python'
,
default
=
sys
.
executable
,
help
=
'Python interpreter to use'
)
parser
.
add_argument
(
'-X'
,
metavar
=
'NAME=VALUE'
,
dest
=
'directives'
,
action
=
'append'
,
help
=
'Compiler directives to set during compilation'
)
parser
.
add_argument
(
'-V'
,
'--version'
,
action
=
'store_true'
,
help
=
'print the Python and Cython version numbers and exit'
)
parser
.
add_argument
(
'file_args'
,
nargs
=
'*'
,
help
=
'program read from script file'
)
return
parser
.
parse_args
(
args
)
def
run_python
(
python
,
command
):
subprocess
.
check_call
([
python
,
'-c'
,
command
])
def
print_versions
(
python
):
"""Print version numbers of Python and Cython.
"""
command
=
(
"import sys, Cython; "
"print('Python {}'.format('.'.join(map(str, sys.version_info[:3])))); "
"print('[Cython {}]'.format(Cython.__version__))"
)
run_python
(
python
,
command
)
def
run_cython_command
(
python
,
command
,
args
=
None
):
"""Compile and run a Python command string.
"""
command
=
(
"import sys; "
"from Cython.Build.Inline import cython_inline; "
"sys.argv[1:] = {args!r}; "
"(lambda: cython_inline({code!r}, quiet=True))()"
).
format
(
code
=
command
,
args
=
list
(
args
)
if
args
else
[])
run_python
(
python
,
command
)
def
run_python_stdin
(
python
,
file_args
=
None
,
directives
=
None
):
"""Compile and run a Python program from stdin.
"""
import
shutil
import
tempfile
with
tempfile
.
NamedTemporaryFile
(
suffix
=
'.py'
)
as
f
:
shutil
.
copyfileobj
(
sys
.
stdin
,
f
)
f
.
flush
()
file_args
=
[
f
.
name
]
+
list
(
file_args
or
())
run_python_file
(
python
,
file_args
,
directives
)
def
run_python_file
(
python
,
file_args
,
directives
=
None
):
"""Compile and run a Python program from a file.
"""
args
=
[]
if
directives
:
for
directive
in
directives
:
args
.
extend
((
'-X'
,
directive
))
args
.
extend
(
file_args
)
command
=
(
"import Cython.Build.BuildExecutable as bex; "
"bex.DEBUG = False; "
"bex.build_and_run({args!r})"
).
format
(
args
=
args
)
run_python
(
python
,
command
)
def
main
():
args
=
parse_args
()
python
=
args
.
python
if
args
.
version
:
print_versions
(
python
)
return
if
args
.
command
:
run_cython_command
(
python
,
args
.
command
,
args
.
file_args
)
if
args
.
file_args
:
if
args
.
file_args
[
0
]
==
'-'
:
run_python_stdin
(
python
,
args
.
file_args
[
1
:],
args
.
directives
)
else
:
run_python_file
(
python
,
args
.
file_args
,
args
.
directives
)
if
__name__
==
'__main__'
:
main
()
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment