Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gevent
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
gevent
Commits
1a39110b
Commit
1a39110b
authored
Jul 25, 2019
by
Julien Danjou
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
monkey: allow to execute a module rather than only a script
parent
0283cffe
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
33 additions
and
9 deletions
+33
-9
CHANGES.rst
CHANGES.rst
+3
-0
src/gevent/monkey.py
src/gevent/monkey.py
+14
-6
src/gevent/tests/monkey_package/__init__.py
src/gevent/tests/monkey_package/__init__.py
+0
-0
src/gevent/tests/test__monkey_scope.py
src/gevent/tests/test__monkey_scope.py
+16
-3
No files found.
CHANGES.rst
View file @
1a39110b
...
...
@@ -9,6 +9,9 @@
- Update to Cython 0.29.7 and cffi 1.12.3.
- Add an ``--module`` option to ``gevent.monkey`` allowing to run a Python
module rather than a script.
1.5a1 (2019-05-02)
==================
...
...
src/gevent/monkey.py
View file @
1a39110b
...
...
@@ -1060,11 +1060,14 @@ def main():
args
=
{}
argv
=
sys
.
argv
[
1
:]
verbose
=
False
run_fn
=
"run_path"
script_help
,
patch_all_args
,
modules
=
_get_script_help
()
while
argv
and
argv
[
0
].
startswith
(
'--'
):
option
=
argv
[
0
][
2
:]
if
option
==
'verbose'
:
verbose
=
True
elif
option
==
'module'
:
run_fn
=
"run_module"
elif
option
.
startswith
(
'no-'
)
and
option
.
replace
(
'no-'
,
''
)
in
patch_all_args
:
args
[
option
[
3
:]]
=
False
elif
option
in
patch_all_args
:
...
...
@@ -1087,15 +1090,14 @@ def main():
patch_all
(
**
args
)
if
argv
:
sys
.
argv
=
argv
import
runpy
sys
.
argv
=
argv
# Use runpy.run_path to closely (exactly) match what the
# interpreter does given 'python <path>'. This includes allowing
# passing .pyc/.pyo files and packages with a __main__ and
# potentially even zip files. Previously we used exec, which only
# worked if we directly read a python source file.
runpy
.
run_path
(
sys
.
argv
[
0
],
run_name
=
'__main__'
)
getattr
(
runpy
,
run_fn
)(
sys
.
argv
[
0
],
run_name
=
'__main__'
)
else
:
print
(
script_help
)
...
...
@@ -1111,19 +1113,25 @@ def _get_script_help():
modules
=
[
x
for
x
in
patch_all_args
if
'patch_'
+
x
in
globals
()]
script_help
=
"""gevent.monkey - monkey patch the standard modules to use gevent.
USAGE: ``python -m gevent.monkey [MONKEY OPTIONS]
script
[SCRIPT OPTIONS]``
USAGE: ``python -m gevent.monkey [MONKEY OPTIONS]
[--module] (script|module)
[SCRIPT OPTIONS]``
If no OPTIONS present, monkey patches all the modules it can patch.
You can exclude a module with --no-
module
, e.g. --no-thread. You can
specify a module to patch with --
module
, e.g. --socket. In the latter
You can exclude a module with --no-
<module>
, e.g. --no-thread. You can
specify a module to patch with --
<module>
, e.g. --socket. In the latter
case only the modules specified on the command line will be patched.
The default behavior is to execute the script passed as argument. If you with
to run a module instead, pass the `--module` argument before the module name.
.. versionchanged:: 1.3b1
The *script* argument can now be any argument that can be passed to `runpy.run_path`,
just like the interpreter itself does, for example a package directory containing ``__main__.py``.
Previously it had to be the path to
a .py source file.
.. versionchanged:: 1.5
The `--module` option has been added.
MONKEY OPTIONS: ``--verbose %s``"""
%
', '
.
join
(
'--[no-]%s'
%
m
for
m
in
modules
)
return
script_help
,
patch_all_args
,
modules
...
...
src/gevent/tests/monkey_package/__init__.py
0 → 100644
View file @
1a39110b
src/gevent/tests/test__monkey_scope.py
View file @
1a39110b
...
...
@@ -17,15 +17,21 @@ class TestRun(unittest.TestCase):
def
tearDown
(
self
):
os
.
chdir
(
self
.
cwd
)
def
_run
(
self
,
script
):
def
_run
(
self
,
script
,
module
=
False
):
env
=
os
.
environ
.
copy
()
env
[
'PYTHONWARNINGS'
]
=
'ignore'
args
=
[
sys
.
executable
,
'-m'
,
'gevent.monkey'
,
script
,
'patched'
]
args
=
[
sys
.
executable
,
'-m'
,
'gevent.monkey'
]
if
module
:
args
.
append
(
'--module'
)
args
+=
[
script
,
'patched'
]
p
=
Popen
(
args
,
stdout
=
PIPE
,
stderr
=
PIPE
,
env
=
env
)
gout
,
gerr
=
p
.
communicate
()
self
.
assertEqual
(
0
,
p
.
returncode
,
(
gout
,
gerr
))
args
=
[
sys
.
executable
,
script
,
'stdlib'
]
if
module
:
args
=
[
sys
.
executable
,
"-m"
,
script
,
'stdlib'
]
else
:
args
=
[
sys
.
executable
,
script
,
'stdlib'
]
p
=
Popen
(
args
,
stdout
=
PIPE
,
stderr
=
PIPE
)
pout
,
perr
=
p
.
communicate
()
...
...
@@ -48,6 +54,13 @@ class TestRun(unittest.TestCase):
self
.
assertTrue
(
lines
[
0
].
endswith
(
'__main__.py'
),
lines
[
0
])
self
.
assertEqual
(
lines
[
1
],
'__main__'
)
def
test_run_module
(
self
):
# Run a __main__ inside a module
lines
,
_
=
self
.
_run
(
'monkey_package'
,
module
=
True
)
self
.
assertTrue
(
lines
[
0
].
endswith
(
'__main__.py'
),
lines
[
0
])
self
.
assertEqual
(
lines
[
1
],
'__main__'
)
def
test_issue_302
(
self
):
lines
,
_
=
self
.
_run
(
os
.
path
.
join
(
'monkey_package'
,
'issue302monkey.py'
))
...
...
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