Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
M
mitogen
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
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Commits
Open sidebar
nexedi
mitogen
Commits
79dd00db
Commit
79dd00db
authored
Sep 30, 2017
by
David Wilson
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
master: hack to avoid executing __main__.
parent
236d8341
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
31 additions
and
0 deletions
+31
-0
docs/howitworks.rst
docs/howitworks.rst
+18
-0
mitogen/master.py
mitogen/master.py
+13
-0
No files found.
docs/howitworks.rst
View file @
79dd00db
...
...
@@ -456,6 +456,24 @@ source is fetched, the method builds a new module object using the best
practice documented in PEP-302.
Neutralizing ``__main__``
#########################
To avoid accidental execution of the ``__main__`` module'
s
code
in
a
slave
context
,
when
serving
the
source
of
the
main
module
,
Mitogen
removes
any
code
occurring
after
the
first
conditional
that
looks
like
a
standard
``
__main__
``
execution
guard
:
..
code
-
block
::
python
#
Code
that
looks
like
this
is
stripped
from
__main__
.
if
__name__
==
'__main__'
:
run_some_code
()
This
is
a
hack
,
but
it
's the least annoying hack I'
ve
found
for
the
problem
yet
.
Avoiding
Negative
Imports
#########################
...
...
mitogen/master.py
View file @
79dd00db
...
...
@@ -566,6 +566,17 @@ class ModuleResponder(object):
def
__repr__
(
self
):
return
'ModuleResponder(%r)'
%
(
self
.
_router
,)
MAIN_RE
=
re
.
compile
(
r'^if\
s+__
name__\
s*==
\s*.__main__.\
s*:
', re.M)
def neutralize_main(self, src):
"""Given the source for the __main__ module, try to find where it
begins conditional execution based on a "if __name__ == '
__main__
'"
guard, and remove any code after that point."""
match = self.MAIN_RE.search(src)
if match:
return src[:match.start()]
return src
def _on_get_module(self, msg):
LOG.debug('
%
r
.
get_module
(
%
r
)
', self, msg)
if msg == mitogen.core._DEAD:
...
...
@@ -584,6 +595,8 @@ class ModuleResponder(object):
else:
pkg_present = None
if fullname == '
__main__
':
source = self.neutralize_main(source)
compressed = zlib.compress(source)
related = list(self._finder.find_related(fullname))
self._router.route(
...
...
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