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
4fbe5c71
Commit
4fbe5c71
authored
Jul 10, 2023
by
Jason Madden
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'issue-1932'
parents
c3fb1211
9ef38bb9
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
75 additions
and
25 deletions
+75
-25
docs/changes/1932.bugfix
docs/changes/1932.bugfix
+6
-0
src/gevent/_socketcommon.py
src/gevent/_socketcommon.py
+43
-24
src/gevent/tests/test__backdoor.py
src/gevent/tests/test__backdoor.py
+1
-1
src/gevent/tests/test__socket.py
src/gevent/tests/test__socket.py
+25
-0
No files found.
docs/changes/1932.bugfix
0 → 100644
View file @
4fbe5c71
Fix an edge case connecting a non-blocking ``SSLSocket`` that could result
in an AttributeError. In a change to match the standard library,
calling ``sock.connect_ex()`` on a subclass of ``socket`` no longer
calls the subclass's ``connect`` method.
Initial fix by Priyankar Jain.
src/gevent/_socketcommon.py
View file @
4fbe5c71
...
@@ -581,6 +581,49 @@ class SocketMixin(object):
...
@@ -581,6 +581,49 @@ class SocketMixin(object):
it will be used instead of ignored, if the platform supplies
it will be used instead of ignored, if the platform supplies
:func:`socket.inet_pton`.
:func:`socket.inet_pton`.
"""
"""
# In the standard library, ``connect`` and ``connect_ex`` are implemented
# in C, and they both call a C function ``internal_connect`` to do the real
# work. This means that it is a visible behaviour difference to have our
# Python implementation of ``connect_ex`` simply call ``connect``:
# it could be overridden in a subclass or at runtime! Because of our exception handling,
# this can make a difference for known subclasses like SSLSocket.
self
.
_internal_connect
(
address
)
def
connect_ex
(
self
,
address
):
"""
Connect to *address*, returning a result code.
.. versionchanged:: NEXT
No longer uses an overridden ``connect`` method on
this object. Instead, like the standard library, this method always
uses a non-replacable internal connection function.
"""
try
:
return
self
.
_internal_connect
(
address
)
or
0
except
__socket__
.
timeout
:
return
EAGAIN
except
__socket__
.
gaierror
:
# pylint:disable=try-except-raise
# gaierror/overflowerror/typerror is not silenced by connect_ex;
# gaierror extends error so catch it first
raise
except
_SocketError
as
ex
:
# Python 3: error is now OSError and it has various subclasses.
# Only those that apply to actually connecting are silenced by
# connect_ex.
# On Python 3, we want to check ex.errno; on Python 2
# there is no such attribute, we need to look at the first
# argument.
try
:
err
=
ex
.
errno
except
AttributeError
:
err
=
ex
.
args
[
0
]
if
err
:
return
err
raise
def
_internal_connect
(
self
,
address
):
# Like the C function ``internal_connect``, not meant to be overridden,
# but exposed for testing.
if
self
.
timeout
==
0.0
:
if
self
.
timeout
==
0.0
:
return
self
.
_sock
.
connect
(
address
)
return
self
.
_sock
.
connect
(
address
)
address
=
_resolve_addr
(
self
.
_sock
,
address
)
address
=
_resolve_addr
(
self
.
_sock
,
address
)
...
@@ -611,30 +654,6 @@ class SocketMixin(object):
...
@@ -611,30 +654,6 @@ class SocketMixin(object):
result
=
ECONNREFUSED
result
=
ECONNREFUSED
raise
_SocketError
(
result
,
strerror
(
result
))
raise
_SocketError
(
result
,
strerror
(
result
))
def
connect_ex
(
self
,
address
):
try
:
return
self
.
connect
(
address
)
or
0
except
__socket__
.
timeout
:
return
EAGAIN
except
__socket__
.
gaierror
:
# pylint:disable=try-except-raise
# gaierror/overflowerror/typerror is not silenced by connect_ex;
# gaierror extends error so catch it first
raise
except
_SocketError
as
ex
:
# Python 3: error is now OSError and it has various subclasses.
# Only those that apply to actually connecting are silenced by
# connect_ex.
# On Python 3, we want to check ex.errno; on Python 2
# there is no such attribute, we need to look at the first
# argument.
try
:
err
=
ex
.
errno
except
AttributeError
:
err
=
ex
.
args
[
0
]
if
err
:
return
err
raise
def
recv
(
self
,
*
args
):
def
recv
(
self
,
*
args
):
while
1
:
while
1
:
try
:
try
:
...
...
src/gevent/tests/test__backdoor.py
View file @
4fbe5c71
...
@@ -67,7 +67,7 @@ class Test(greentest.TestCase):
...
@@ -67,7 +67,7 @@ class Test(greentest.TestCase):
def
_create_connection
(
self
,
server
):
def
_create_connection
(
self
,
server
):
conn
=
SocketWithBanner
()
conn
=
SocketWithBanner
()
conn
.
connect
((
DEFAULT_CONNECT
,
server
.
server_port
))
conn
.
connect
((
DEFAULT_CONNECT
,
server
.
server_port
))
# pylint:disable=not-callable
try
:
try
:
banner
=
self
.
_wait_for_prompt
(
conn
)
banner
=
self
.
_wait_for_prompt
(
conn
)
except
:
except
:
...
...
src/gevent/tests/test__socket.py
View file @
4fbe5c71
...
@@ -434,6 +434,31 @@ class TestTCP(greentest.TestCase):
...
@@ -434,6 +434,31 @@ class TestTCP(greentest.TestCase):
finally
:
finally
:
s
.
close
()
s
.
close
()
@
skipWithoutExternalNetwork
(
"Tries to resolve hostname"
)
def
test_connect_ex_not_call_connect
(
self
):
# Issue 1931
def
do_it
(
sock
):
try
:
with
self
.
assertRaises
(
socket
.
gaierror
):
sock
.
connect_ex
((
'foo.bar.fizzbuzz'
,
support
.
find_unused_port
()))
finally
:
sock
.
close
()
# An instance attribute doesn't matter because we can't set it
s
=
socket
.
socket
(
socket
.
AF_INET
,
socket
.
SOCK_STREAM
)
with
self
.
assertRaises
(
AttributeError
):
s
.
connect
=
None
s
.
close
()
# A subclass
class
S
(
socket
.
socket
):
def
connect
(
self
,
*
args
):
raise
AssertionError
(
'Should not be called'
)
s
=
S
(
socket
.
AF_INET
,
socket
.
SOCK_STREAM
)
do_it
(
s
)
def
test_connect_ex_nonblocking_overflow
(
self
):
def
test_connect_ex_nonblocking_overflow
(
self
):
# Issue 841
# Issue 841
s
=
socket
.
socket
(
socket
.
AF_INET
,
socket
.
SOCK_STREAM
)
s
=
socket
.
socket
(
socket
.
AF_INET
,
socket
.
SOCK_STREAM
)
...
...
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