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
b098197a
Commit
b098197a
authored
Mar 10, 2016
by
Jason Madden
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
cleanup monkey.
parent
2ce1fd3c
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
31 additions
and
26 deletions
+31
-26
gevent/monkey.py
gevent/monkey.py
+28
-26
gevent/socket.py
gevent/socket.py
+3
-0
No files found.
gevent/monkey.py
View file @
b098197a
...
@@ -80,14 +80,11 @@ if sys.version_info[0] >= 3:
...
@@ -80,14 +80,11 @@ if sys.version_info[0] >= 3:
string_types
=
str
,
string_types
=
str
,
PY3
=
True
PY3
=
True
else
:
else
:
import
__builtin__
import
__builtin__
# pylint:disable=import-error
string_types
=
__builtin__
.
basestring
string_types
=
__builtin__
.
basestring
PY3
=
False
PY3
=
False
if
sys
.
platform
.
startswith
(
"win"
):
WIN
=
sys
.
platform
.
startswith
(
"win"
)
WIN
=
True
else
:
WIN
=
False
# maps module name -> {attribute name: original item}
# maps module name -> {attribute name: original item}
# e.g. "time" -> {"sleep": built-in function sleep}
# e.g. "time" -> {"sleep": built-in function sleep}
...
@@ -133,19 +130,19 @@ def get_original(mod_name, item_name):
...
@@ -133,19 +130,19 @@ def get_original(mod_name, item_name):
else
:
else
:
return
_get_original
(
mod_name
,
item_name
)
return
_get_original
(
mod_name
,
item_name
)
_NONE
=
object
()
def
patch_item
(
module
,
attr
,
newitem
):
def
patch_item
(
module
,
attr
,
newitem
):
NONE
=
object
()
olditem
=
getattr
(
module
,
attr
,
_NONE
)
olditem
=
getattr
(
module
,
attr
,
NONE
)
if
olditem
is
not
_NONE
:
if
olditem
is
not
NONE
:
saved
.
setdefault
(
module
.
__name__
,
{}).
setdefault
(
attr
,
olditem
)
saved
.
setdefault
(
module
.
__name__
,
{}).
setdefault
(
attr
,
olditem
)
setattr
(
module
,
attr
,
newitem
)
setattr
(
module
,
attr
,
newitem
)
def
remove_item
(
module
,
attr
):
def
remove_item
(
module
,
attr
):
NONE
=
object
()
olditem
=
getattr
(
module
,
attr
,
_NONE
)
olditem
=
getattr
(
module
,
attr
,
NONE
)
if
olditem
is
_NONE
:
if
olditem
is
NONE
:
return
return
saved
.
setdefault
(
module
.
__name__
,
{}).
setdefault
(
attr
,
olditem
)
saved
.
setdefault
(
module
.
__name__
,
{}).
setdefault
(
attr
,
olditem
)
delattr
(
module
,
attr
)
delattr
(
module
,
attr
)
...
@@ -294,6 +291,9 @@ def patch_thread(threading=True, _threading_local=True, Event=False, logging=Tru
...
@@ -294,6 +291,9 @@ def patch_thread(threading=True, _threading_local=True, Event=False, logging=Tru
.. versionchanged:: 1.1b1
.. versionchanged:: 1.1b1
Add *logging* and *existing_locks* params.
Add *logging* and *existing_locks* params.
"""
"""
# XXX: Simplify
# pylint:disable=too-many-branches
# Description of the hang:
# Description of the hang:
# There is an incompatibility with patching 'thread' and the 'multiprocessing' module:
# There is an incompatibility with patching 'thread' and the 'multiprocessing' module:
# The problem is that multiprocessing.queues.Queue uses a half-duplex multiprocessing.Pipe,
# The problem is that multiprocessing.queues.Queue uses a half-duplex multiprocessing.Pipe,
...
@@ -326,18 +326,18 @@ def patch_thread(threading=True, _threading_local=True, Event=False, logging=Tru
...
@@ -326,18 +326,18 @@ def patch_thread(threading=True, _threading_local=True, Event=False, logging=Tru
patch_module
(
'thread'
)
patch_module
(
'thread'
)
if
threading
:
if
threading
:
threading
=
patch_module
(
'threading'
)
threading
_mod
=
patch_module
(
'threading'
)
if
Event
:
if
Event
:
from
gevent.event
import
Event
from
gevent.event
import
Event
patch_item
(
threading
,
'Event'
,
Event
)
patch_item
(
threading
_mod
,
'Event'
,
Event
)
if
existing_locks
:
if
existing_locks
:
_patch_existing_locks
(
threading
)
_patch_existing_locks
(
threading
_mod
)
if
logging
and
'logging'
in
sys
.
modules
:
if
logging
and
'logging'
in
sys
.
modules
:
logging
=
__import__
(
'logging'
)
logging
=
__import__
(
'logging'
)
patch_item
(
logging
,
'_lock'
,
threading
.
RLock
())
patch_item
(
logging
,
'_lock'
,
threading
_mod
.
RLock
())
for
wr
in
logging
.
_handlerList
:
for
wr
in
logging
.
_handlerList
:
# In py26, these are actual handlers, not weakrefs
# In py26, these are actual handlers, not weakrefs
handler
=
wr
()
if
callable
(
wr
)
else
wr
handler
=
wr
()
if
callable
(
wr
)
else
wr
...
@@ -345,7 +345,7 @@ def patch_thread(threading=True, _threading_local=True, Event=False, logging=Tru
...
@@ -345,7 +345,7 @@ def patch_thread(threading=True, _threading_local=True, Event=False, logging=Tru
continue
continue
if
not
hasattr
(
handler
,
'lock'
):
if
not
hasattr
(
handler
,
'lock'
):
raise
TypeError
(
"Unknown/unsupported handler %r"
%
handler
)
raise
TypeError
(
"Unknown/unsupported handler %r"
%
handler
)
handler
.
lock
=
threading
.
RLock
()
handler
.
lock
=
threading
_mod
.
RLock
()
if
_threading_local
:
if
_threading_local
:
_threading_local
=
__import__
(
'_threading_local'
)
_threading_local
=
__import__
(
'_threading_local'
)
...
@@ -359,15 +359,15 @@ def patch_thread(threading=True, _threading_local=True, Event=False, logging=Tru
...
@@ -359,15 +359,15 @@ def patch_thread(threading=True, _threading_local=True, Event=False, logging=Tru
# (which is already running) cannot wait for the main thread---it
# (which is already running) cannot wait for the main thread---it
# hangs forever. We patch around this if possible. See also
# hangs forever. We patch around this if possible. See also
# gevent.threading.
# gevent.threading.
threading
=
__import__
(
'threading'
)
threading
_mod
=
__import__
(
'threading'
)
greenlet
=
__import__
(
'greenlet'
)
greenlet
=
__import__
(
'greenlet'
)
if
threading
.
current_thread
()
==
threading
.
main_thread
():
if
threading
_mod
.
current_thread
()
==
threading_mod
.
main_thread
():
main_thread
=
threading
.
main_thread
()
main_thread
=
threading
_mod
.
main_thread
()
_greenlet
=
main_thread
.
_greenlet
=
greenlet
.
getcurrent
()
_greenlet
=
main_thread
.
_greenlet
=
greenlet
.
getcurrent
()
from
gevent.hub
import
sleep
from
gevent.hub
import
sleep
def
join
(
timeout
=
None
):
def
join
(
timeout
=
None
):
if
threading
.
current_thread
()
is
main_thread
:
if
threading
_mod
.
current_thread
()
is
main_thread
:
raise
RuntimeError
(
"Cannot join current thread"
)
raise
RuntimeError
(
"Cannot join current thread"
)
if
_greenlet
.
dead
or
not
main_thread
.
is_alive
():
if
_greenlet
.
dead
or
not
main_thread
.
is_alive
():
return
return
...
@@ -384,11 +384,11 @@ def patch_thread(threading=True, _threading_local=True, Event=False, logging=Tru
...
@@ -384,11 +384,11 @@ def patch_thread(threading=True, _threading_local=True, Event=False, logging=Tru
# matters if threading was imported before monkey-patching
# matters if threading was imported before monkey-patching
# thread
# thread
oldid
=
main_thread
.
ident
oldid
=
main_thread
.
ident
main_thread
.
_ident
=
threading
.
get_ident
()
main_thread
.
_ident
=
threading
_mod
.
get_ident
()
if
oldid
in
threading
.
_active
:
if
oldid
in
threading
_mod
.
_active
:
threading
.
_active
[
main_thread
.
ident
]
=
threading
.
_active
[
oldid
]
threading
_mod
.
_active
[
main_thread
.
ident
]
=
threading_mod
.
_active
[
oldid
]
if
oldid
!=
main_thread
.
ident
:
if
oldid
!=
main_thread
.
ident
:
del
threading
.
_active
[
oldid
]
del
threading
_mod
.
_active
[
oldid
]
else
:
else
:
_queue_warning
(
"Monkey-patching not on the main thread; "
_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"
,
...
@@ -471,7 +471,7 @@ def patch_select(aggressive=True):
...
@@ -471,7 +471,7 @@ def patch_select(aggressive=True):
select
=
__import__
(
'select'
)
select
=
__import__
(
'select'
)
selectors
=
__import__
(
'selectors'
)
selectors
=
__import__
(
'selectors'
)
if
selectors
.
SelectSelector
.
_select
is
select
.
select
:
if
selectors
.
SelectSelector
.
_select
is
select
.
select
:
def
_select
(
self
,
*
args
,
**
kwargs
):
def
_select
(
self
,
*
args
,
**
kwargs
):
# pylint:disable=unused-argument
return
select
.
select
(
*
args
,
**
kwargs
)
return
select
.
select
(
*
args
,
**
kwargs
)
selectors
.
SelectSelector
.
_select
=
_select
selectors
.
SelectSelector
.
_select
=
_select
...
@@ -555,6 +555,8 @@ def patch_all(socket=True, dns=True, time=True, select=True, thread=True, os=Tru
...
@@ -555,6 +555,8 @@ def patch_all(socket=True, dns=True, time=True, select=True, thread=True, os=Tru
and ``signal=True``. This will cause SIGCHLD handlers to not be called. This may
and ``signal=True``. This will cause SIGCHLD handlers to not be called. This may
be an error in the future.
be an error in the future.
"""
"""
# pylint:disable=too-many-locals,too-many-branches
# Check to see if they're changing the patched list
# Check to see if they're changing the patched list
_warnings
,
first_time
=
_check_repatching
(
**
locals
())
_warnings
,
first_time
=
_check_repatching
(
**
locals
())
if
not
_warnings
and
not
first_time
:
if
not
_warnings
and
not
first_time
:
...
@@ -640,7 +642,7 @@ def main():
...
@@ -640,7 +642,7 @@ def main():
def
_get_script_help
():
def
_get_script_help
():
from
inspect
import
getargspec
from
inspect
import
getargspec
patch_all_args
=
getargspec
(
patch_all
)[
0
]
patch_all_args
=
getargspec
(
patch_all
)[
0
]
# pylint:disable=deprecated-method
modules
=
[
x
for
x
in
patch_all_args
if
'patch_'
+
x
in
globals
()]
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.
script_help
=
"""gevent.monkey - monkey patch the standard modules to use gevent.
...
...
gevent/socket.py
View file @
b098197a
...
@@ -22,6 +22,9 @@ if PY3:
...
@@ -22,6 +22,9 @@ if PY3:
else
:
else
:
from
gevent
import
_socket2
as
_source
from
gevent
import
_socket2
as
_source
# define some things we're expecting to overwrite; each module
# needs to define these
__implements__
=
__dns__
=
__all__
=
__extensions__
=
__imports__
=
()
for
key
in
_source
.
__dict__
:
for
key
in
_source
.
__dict__
:
if
key
.
startswith
(
'__'
)
and
key
not
in
'__implements__ __dns__ __all__ __extensions__ __imports__ __socket__'
.
split
():
if
key
.
startswith
(
'__'
)
and
key
not
in
'__implements__ __dns__ __all__ __extensions__ __imports__ __socket__'
.
split
():
...
...
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