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
436f6557
Commit
436f6557
authored
Mar 04, 2016
by
Jason Madden
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Remove a duplicate warning on re-patching and don't use a global variable to track the state.
parent
4eed30f2
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
34 additions
and
23 deletions
+34
-23
gevent/monkey.py
gevent/monkey.py
+30
-18
greentest/test__monkey.py
greentest/test__monkey.py
+4
-5
No files found.
gevent/monkey.py
View file @
436f6557
...
...
@@ -164,21 +164,19 @@ def patch_module(name, items=None):
return
module
_warnings
=
list
()
def
_queue_warning
(
message
):
def
_queue_warning
(
message
,
_warnings
):
# Queues a warning to show after the monkey-patching process is all done.
# Done this way to avoid extra imports during the process itself, just
# in case
_warnings
.
append
(
message
)
# in case. If we're calling a function one-off (unusual) go ahead and do it
if
_warnings
is
None
:
_process_warnings
([
message
])
else
:
_warnings
.
append
(
message
)
def
_process_warnings
():
def
_process_warnings
(
_warnings
):
import
warnings
_w
=
list
(
_warnings
)
del
_warnings
[:]
for
warning
in
_w
:
for
warning
in
_warnings
:
warnings
.
warn
(
warning
,
RuntimeWarning
,
stacklevel
=
3
)
...
...
@@ -271,7 +269,8 @@ def _patch_existing_locks(threading):
def
patch_thread
(
threading
=
True
,
_threading_local
=
True
,
Event
=
False
,
logging
=
True
,
existing_locks
=
True
):
existing_locks
=
True
,
_warnings
=
None
):
"""
Replace the standard :mod:`thread` module to make it greenlet-based.
...
...
@@ -389,7 +388,8 @@ def patch_thread(threading=True, _threading_local=True, Event=False, logging=Tru
del
threading
.
_active
[
oldid
]
else
:
_queue_warning
(
"Monkey-patching not on the main thread; "
"threading.main_thread().join() will hang from a greenlet"
)
"threading.main_thread().join() will hang from a greenlet"
,
_warnings
)
def
patch_socket
(
dns
=
True
,
aggressive
=
True
):
...
...
@@ -519,19 +519,30 @@ def patch_signal():
"""
patch_module
(
"signal"
)
def
_check_repatching
(
**
module_settings
):
if
saved
.
get
(
'_gevent_saved_patch_all'
,
module_settings
)
!=
module_settings
:
_warnings
=
[]
key
=
'_gevent_saved_patch_all'
if
saved
.
get
(
key
,
module_settings
)
!=
module_settings
:
_queue_warning
(
"Patching more than once will result in the union of all True"
" parameters being patched"
)
" parameters being patched"
,
_warnings
)
first_time
=
key
not
in
saved
saved
[
key
]
=
module_settings
return
_warnings
,
first_time
saved
[
'_gevent_saved_patch_all'
]
=
module_settings
def
patch_all
(
socket
=
True
,
dns
=
True
,
time
=
True
,
select
=
True
,
thread
=
True
,
os
=
True
,
ssl
=
True
,
httplib
=
False
,
subprocess
=
True
,
sys
=
False
,
aggressive
=
True
,
Event
=
False
,
builtins
=
True
,
signal
=
True
):
"""Do all of the default monkey patching (calls every other applicable function in this module)."""
# Check to see if they're changing the patched list
_check_repatching
(
**
locals
())
_warnings
,
first_time
=
_check_repatching
(
**
locals
())
if
not
_warnings
and
not
first_time
:
# Nothing to do, identical args to what we just
# did
return
# order is important
if
os
:
...
...
@@ -561,10 +572,11 @@ def patch_all(socket=True, dns=True, time=True, select=True, thread=True, os=Tru
_queue_warning
(
'Patching signal but not os will result in SIGCHLD handlers'
' installed after this not being called and os.waitpid may not'
' function correctly if gevent.subprocess is used. This may raise an'
' error in the future.'
)
' error in the future.'
,
_warnings
)
patch_signal
()
_process_warnings
()
_process_warnings
(
_warnings
)
def
main
():
...
...
greentest/test__monkey.py
View file @
436f6557
...
...
@@ -54,17 +54,16 @@ with warnings.catch_warnings(record=True) as issued_warnings:
assert
'more than once'
in
str
(
issued_warnings
[
0
].
message
),
issued_warnings
[
0
]
# Patching with the exact same argument doesn't issue a second warning.
#
(just repeats the signal warning)
#
in fact, it doesn't do anything
del
issued_warnings
[:]
monkey
.
patch_all
(
os
=
False
)
orig_saved
[
'_gevent_saved_patch_all'
]
=
monkey
.
saved
[
'_gevent_saved_patch_all'
]
assert
len
(
issued_warnings
)
==
1
,
len
(
issued_warnings
)
assert
'SIGCHLD'
in
str
(
issued_warnings
[
-
1
].
message
),
issued_warnings
[
-
1
]
assert
len
(
issued_warnings
)
==
0
,
len
(
issued_warnings
)
# Make sure that re-patching did not change the monkey.saved
# attribute, overwriting the original functions
assert
orig_saved
==
monkey
.
saved
# attribute, overwriting the original functions
.
assert
orig_saved
==
monkey
.
saved
,
(
orig_saved
,
monkey
.
saved
)
# Make sure some problematic attributes stayed correct.
# NOTE: This was only a problem if threading was not previously imported.
...
...
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