-
-
Notifications
You must be signed in to change notification settings - Fork 440
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Twisted concurrency model #816
Open
exarkun
wants to merge
3
commits into
nedbat:master
Choose a base branch
from
exarkun:twisted-concurrency-model
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
# Licensed under the Apache License: http://www.apache.org/licenses/LICENSE-2.0 | ||
# For details: https://github.com/nedbat/coveragepy/blob/master/NOTICE.txt | ||
|
||
"""Monkey-patching to add Twisted support for coverage.py | ||
""" | ||
|
||
from functools import partial | ||
from tempfile import mkdtemp | ||
from os.path import join | ||
|
||
# XXX do it without installing the default reactor | ||
import twisted.internet.reactor | ||
|
||
from coverage.misc import contract | ||
|
||
# An attribute that will be set on the module to indicate that it has been | ||
# monkey-patched. Value copied from multiproc.py. | ||
PATCHED_MARKER = "_coverage$patched" | ||
|
||
@contract(rcfile=str) | ||
def patch_twisted(rcfile): | ||
""" | ||
The twisted.internet.interfaces.IReactorProcess.spawnProcess | ||
implementation of the Twisted reactor is patched to enable coverage | ||
collection in spawned processes. | ||
|
||
This works by clobbering sitecustomize. | ||
""" | ||
if getattr(twisted.internet, PATCHED_MARKER, False): | ||
return | ||
|
||
origSpawnProcess = twisted.internet.reactor.spawnProcess | ||
twisted.internet.reactor.spawnProcess = partial( | ||
_coverageSpawnProcess, | ||
origSpawnProcess, | ||
rcfile, | ||
) | ||
setattr(twisted.internet, PATCHED_MARKER, True) | ||
|
||
|
||
def _coverageSpawnProcess( | ||
origSpawnProcess, | ||
rcfile, | ||
processProtocol, | ||
executable, | ||
args=(), | ||
env=None, | ||
*a, | ||
**kw | ||
): | ||
""" | ||
Spawn a process using ``origSpawnProcess``. Set up its environment so | ||
that coverage its collected, if it is a Python process. | ||
""" | ||
if env is None: | ||
env = os.environ.copy() | ||
pythonpath = env.get(u"PYTHONPATH", u"").split(u":") | ||
dtemp = mkdtemp() | ||
pythonpath.insert(0, dtemp) | ||
sitecustomize = join(dtemp, u"sitecustomize.py") | ||
with open(sitecustomize, "wt") as f: | ||
f.write("""\ | ||
import sys, os.path | ||
sys.path.remove({dtemp!r}) | ||
os.remove({sitecustomize!r}) | ||
if os.path.exists({sitecustomizec!r}): | ||
os.remove({sitecustomizec!r}) | ||
os.rmdir({dtemp!r}) | ||
import coverage | ||
coverage.process_startup() | ||
""".format( | ||
sitecustomize=sitecustomize, | ||
sitecustomizec=sitecustomize + u"c", | ||
dtemp=dtemp, | ||
)) | ||
env[u"PYTHONPATH"] = u":".join(pythonpath) | ||
env[u"COVERAGE_PROCESS_START"] = rcfile | ||
return origSpawnProcess( | ||
processProtocol, | ||
executable, | ||
args, | ||
env, | ||
*a, | ||
**kw | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(Will probably not be very useful reviewing much of this, but it'd be a bit nicer UX-wise if rather than saying "A or B" here you used
.format
and inserted inoptions.concurrency
, i.e. the actual one of the two of those that the user really did use, instead of including ones they didn't)