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
00821040
Commit
00821040
authored
Dec 21, 2020
by
Jason Madden
Committed by
GitHub
Dec 21, 2020
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #1726 from gevent/issue1724
Move socket objects fully to __slots__.
parents
5a603ade
5cf57b16
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
75 additions
and
38 deletions
+75
-38
docs/changes/1724.bugfix
docs/changes/1724.bugfix
+4
-0
src/gevent/_socket2.py
src/gevent/_socket2.py
+8
-17
src/gevent/_socket3.py
src/gevent/_socket3.py
+9
-19
src/gevent/_socketcommon.py
src/gevent/_socketcommon.py
+32
-0
src/gevent/tests/test__socket.py
src/gevent/tests/test__socket.py
+22
-2
No files found.
docs/changes/1724.bugfix
0 → 100644
View file @
00821040
Remove the ``__dict__`` attribute from `gevent.socket.socket` objects. The
standard library socket do not have a ``__dict__``.
Noticed by Carson Ip.
src/gevent/_socket2.py
View file @
00821040
...
@@ -105,7 +105,9 @@ gtype = type
...
@@ -105,7 +105,9 @@ gtype = type
from
gevent._hub_primitives
import
wait_on_socket
as
_wait_on_socket
from
gevent._hub_primitives
import
wait_on_socket
as
_wait_on_socket
class
socket
(
_socketcommon
.
SocketMixin
):
_Base
=
_socketcommon
.
SocketMixin
class
socket
(
_Base
):
"""
"""
gevent `socket.socket <https://docs.python.org/2/library/socket.html#socket-objects>`_
gevent `socket.socket <https://docs.python.org/2/library/socket.html#socket-objects>`_
for Python 2.
for Python 2.
...
@@ -120,9 +122,12 @@ class socket(_socketcommon.SocketMixin):
...
@@ -120,9 +122,12 @@ class socket(_socketcommon.SocketMixin):
# pylint:disable=too-many-public-methods
# pylint:disable=too-many-public-methods
# TODO: Define __slots__.
__slots__
=
(
)
def
__init__
(
self
,
family
=
AF_INET
,
type
=
SOCK_STREAM
,
proto
=
0
,
_sock
=
None
):
def
__init__
(
self
,
family
=
AF_INET
,
type
=
SOCK_STREAM
,
proto
=
0
,
_sock
=
None
):
_Base
.
__init__
(
self
)
timeout
=
_socket
.
getdefaulttimeout
()
timeout
=
_socket
.
getdefaulttimeout
()
if
_sock
is
None
:
if
_sock
is
None
:
self
.
_sock
=
_realsocket
(
family
,
type
,
proto
)
self
.
_sock
=
_realsocket
(
family
,
type
,
proto
)
...
@@ -403,20 +408,6 @@ class socket(_socketcommon.SocketMixin):
...
@@ -403,20 +408,6 @@ class socket(_socketcommon.SocketMixin):
else
:
else
:
self
.
timeout
=
0.0
self
.
timeout
=
0.0
def
settimeout
(
self
,
howlong
):
if
howlong
is
not
None
:
try
:
f
=
howlong
.
__float__
except
AttributeError
:
raise
TypeError
(
'a float is required'
)
howlong
=
f
()
if
howlong
<
0.0
:
raise
ValueError
(
'Timeout value out of range'
)
self
.
__dict__
[
'timeout'
]
=
howlong
# avoid recursion with any property on self.timeout
def
gettimeout
(
self
):
return
self
.
__dict__
[
'timeout'
]
# avoid recursion with any property on self.timeout
def
shutdown
(
self
,
how
):
def
shutdown
(
self
,
how
):
if
how
==
0
:
# SHUT_RD
if
how
==
0
:
# SHUT_RD
self
.
hub
.
cancel_wait
(
self
.
_read_event
,
cancel_wait_ex
)
self
.
hub
.
cancel_wait
(
self
.
_read_event
,
cancel_wait_ex
)
...
@@ -444,7 +435,7 @@ class socket(_socketcommon.SocketMixin):
...
@@ -444,7 +435,7 @@ class socket(_socketcommon.SocketMixin):
_s
=
"def %s(self, *args): return self._sock.%s(*args)
\
n
\
n
"
_s
=
"def %s(self, *args): return self._sock.%s(*args)
\
n
\
n
"
_m
=
None
_m
=
None
for
_m
in
set
(
_socketmethods
)
-
set
(
locals
()):
for
_m
in
set
(
_socketmethods
)
-
set
(
locals
())
-
{
'settimeout'
,
'gettimeout'
}
:
exec
(
_s
%
(
_m
,
_m
,))
exec
(
_s
%
(
_m
,
_m
,))
del
_m
,
_s
del
_m
,
_s
...
...
src/gevent/_socket3.py
View file @
00821040
...
@@ -113,11 +113,10 @@ class socket(_socketcommon.SocketMixin):
...
@@ -113,11 +113,10 @@ class socket(_socketcommon.SocketMixin):
# of _wrefsocket. (gevent internal usage only)
# of _wrefsocket. (gevent internal usage only)
_gevent_sock_class
=
_wrefsocket
_gevent_sock_class
=
_wrefsocket
_io_refs
=
0
__slots__
=
(
_closed
=
False
'_io_refs'
,
_read_event
=
None
'_closed'
,
_write_event
=
None
)
# Take the same approach as socket2: wrap a real socket object,
# Take the same approach as socket2: wrap a real socket object,
# don't subclass it. This lets code that needs the raw _sock (not tied to the hub)
# don't subclass it. This lets code that needs the raw _sock (not tied to the hub)
...
@@ -125,6 +124,8 @@ class socket(_socketcommon.SocketMixin):
...
@@ -125,6 +124,8 @@ class socket(_socketcommon.SocketMixin):
if
sys
.
version_info
[:
2
]
<
(
3
,
7
):
if
sys
.
version_info
[:
2
]
<
(
3
,
7
):
def
__init__
(
self
,
family
=
AF_INET
,
type
=
SOCK_STREAM
,
proto
=
0
,
fileno
=
None
):
def
__init__
(
self
,
family
=
AF_INET
,
type
=
SOCK_STREAM
,
proto
=
0
,
fileno
=
None
):
super
().
__init__
()
self
.
_closed
=
False
self
.
_sock
=
self
.
_gevent_sock_class
(
family
,
type
,
proto
,
fileno
)
self
.
_sock
=
self
.
_gevent_sock_class
(
family
,
type
,
proto
,
fileno
)
self
.
timeout
=
None
self
.
timeout
=
None
self
.
__init_common
()
self
.
__init_common
()
...
@@ -132,6 +133,8 @@ class socket(_socketcommon.SocketMixin):
...
@@ -132,6 +133,8 @@ class socket(_socketcommon.SocketMixin):
# In 3.7, socket changed to auto-detecting family, type, and proto
# In 3.7, socket changed to auto-detecting family, type, and proto
# when given a fileno.
# when given a fileno.
def
__init__
(
self
,
family
=-
1
,
type
=-
1
,
proto
=-
1
,
fileno
=
None
):
def
__init__
(
self
,
family
=-
1
,
type
=-
1
,
proto
=-
1
,
fileno
=
None
):
super
().
__init__
()
self
.
_closed
=
False
if
fileno
is
None
:
if
fileno
is
None
:
if
family
==
-
1
:
if
family
==
-
1
:
family
=
AF_INET
family
=
AF_INET
...
@@ -144,6 +147,7 @@ class socket(_socketcommon.SocketMixin):
...
@@ -144,6 +147,7 @@ class socket(_socketcommon.SocketMixin):
self
.
__init_common
()
self
.
__init_common
()
def
__init_common
(
self
):
def
__init_common
(
self
):
self
.
_io_refs
=
0
_socket
.
socket
.
setblocking
(
self
.
_sock
,
False
)
_socket
.
socket
.
setblocking
(
self
.
_sock
,
False
)
fileno
=
_socket
.
socket
.
fileno
(
self
.
_sock
)
fileno
=
_socket
.
socket
.
fileno
(
self
.
_sock
)
self
.
hub
=
get_hub
()
self
.
hub
=
get_hub
()
...
@@ -579,20 +583,6 @@ class socket(_socketcommon.SocketMixin):
...
@@ -579,20 +583,6 @@ class socket(_socketcommon.SocketMixin):
else
:
else
:
self
.
timeout
=
0.0
self
.
timeout
=
0.0
def
settimeout
(
self
,
howlong
):
if
howlong
is
not
None
:
try
:
f
=
howlong
.
__float__
except
AttributeError
:
raise
TypeError
(
'a float is required'
)
howlong
=
f
()
if
howlong
<
0.0
:
raise
ValueError
(
'Timeout value out of range'
)
self
.
__dict__
[
'timeout'
]
=
howlong
def
gettimeout
(
self
):
return
self
.
__dict__
[
'timeout'
]
def
shutdown
(
self
,
how
):
def
shutdown
(
self
,
how
):
if
how
==
0
:
# SHUT_RD
if
how
==
0
:
# SHUT_RD
self
.
hub
.
cancel_wait
(
self
.
_read_event
,
cancel_wait_ex
)
self
.
hub
.
cancel_wait
(
self
.
_read_event
,
cancel_wait_ex
)
...
...
src/gevent/_socketcommon.py
View file @
00821040
...
@@ -434,11 +434,27 @@ def _resolve_addr(sock, address):
...
@@ -434,11 +434,27 @@ def _resolve_addr(sock, address):
class
SocketMixin
(
object
):
class
SocketMixin
(
object
):
__slots__
=
(
__slots__
=
(
'hub'
,
'timeout'
,
'_read_event'
,
'_read_event'
,
'_write_event'
,
'_write_event'
,
'_sock'
,
'_sock'
,
'__weakref__'
,
)
)
def
__init__
(
self
):
# Writing:
# (self.a, self.b) = (None,) * 2
# generates the fastest bytecode. But At least on PyPy,
# where the SSLSocket subclass has a timeout property,
# it results in the settimeout() method getting the tuple
# as the value, not the unpacked None.
self
.
_read_event
=
None
self
.
_write_event
=
None
self
.
_sock
=
None
self
.
hub
=
None
self
.
timeout
=
None
def
_drop_events_and_close
(
self
,
closefd
=
True
,
_cancel_wait_ex
=
cancel_wait_ex
):
def
_drop_events_and_close
(
self
,
closefd
=
True
,
_cancel_wait_ex
=
cancel_wait_ex
):
hub
=
self
.
hub
hub
=
self
.
hub
read_event
=
self
.
_read_event
read_event
=
self
.
_read_event
...
@@ -455,3 +471,19 @@ class SocketMixin(object):
...
@@ -455,3 +471,19 @@ class SocketMixin(object):
def
_drop_ref_on_close
(
self
,
sock
):
def
_drop_ref_on_close
(
self
,
sock
):
raise
NotImplementedError
raise
NotImplementedError
def
settimeout
(
self
,
howlong
):
if
howlong
is
not
None
:
try
:
f
=
howlong
.
__float__
except
AttributeError
:
raise
TypeError
(
'a float is required'
,
howlong
,
type
(
howlong
))
howlong
=
f
()
if
howlong
<
0.0
:
raise
ValueError
(
'Timeout value out of range'
)
# avoid recursion with any property on self.timeout
SocketMixin
.
timeout
.
__set__
(
self
,
howlong
)
def
gettimeout
(
self
):
# avoid recursion with any property on self.timeout
return
SocketMixin
.
timeout
.
__get__
(
self
,
type
(
self
))
src/gevent/tests/test__socket.py
View file @
00821040
# This line can be commented out so that most tests run with the
# system socket for comparison.
from
__future__
import
print_function
from
__future__
import
print_function
from
__future__
import
absolute_import
from
__future__
import
absolute_import
from
gevent
import
monkey
from
gevent
import
monkey
# This line can be commented out so that most tests run with the
# system socket for comparison.
monkey
.
patch_all
()
monkey
.
patch_all
()
import
sys
import
sys
...
@@ -599,5 +599,25 @@ class TestSocket(greentest.TestCase):
...
@@ -599,5 +599,25 @@ class TestSocket(greentest.TestCase):
with
self
.
assertRaises
(
socket
.
error
):
with
self
.
assertRaises
(
socket
.
error
):
s
.
shutdown
(
socket
.
SHUT_RDWR
)
s
.
shutdown
(
socket
.
SHUT_RDWR
)
def
test_can_be_weak_ref
(
self
):
# stdlib socket can be weak reffed.
import
weakref
s
=
socket
.
socket
()
try
:
w
=
weakref
.
ref
(
s
)
self
.
assertIsNotNone
(
w
)
finally
:
s
.
close
()
def
test_has_no_dict
(
self
):
# stdlib socket has no dict
s
=
socket
.
socket
()
try
:
with
self
.
assertRaises
(
AttributeError
):
getattr
(
s
,
'__dict__'
)
finally
:
s
.
close
()
if
__name__
==
'__main__'
:
if
__name__
==
'__main__'
:
greentest
.
main
()
greentest
.
main
()
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