Skip to content
Projeler
Gruplar
Parçacıklar
Yardım
Yükleniyor...
Oturum aç / Kaydol
Gezinmeyi değiştir
C
cpython
Proje
Proje
Ayrıntılar
Etkinlik
Cycle Analytics
Depo (repository)
Depo (repository)
Dosyalar
Kayıtlar (commit)
Dallar (branch)
Etiketler
Katkıda bulunanlar
Grafik
Karşılaştır
Grafikler
Konular (issue)
0
Konular (issue)
0
Liste
Pano
Etiketler
Kilometre Taşları
Birleştirme (merge) Talepleri
0
Birleştirme (merge) Talepleri
0
CI / CD
CI / CD
İş akışları (pipeline)
İşler
Zamanlamalar
Grafikler
Paketler
Paketler
Wiki
Wiki
Parçacıklar
Parçacıklar
Üyeler
Üyeler
Collapse sidebar
Close sidebar
Etkinlik
Grafik
Grafikler
Yeni bir konu (issue) oluştur
İşler
Kayıtlar (commit)
Konu (issue) Panoları
Kenar çubuğunu aç
Batuhan Osman TASKAYA
cpython
Commits
21ebbb7d
Kaydet (Commit)
21ebbb7d
authored
Kas 02, 2010
tarafından
Brian Curtin
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
Clean up ResourceWarnings due to unclosed sockets.
üst
be647e28
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
20 additions
and
0 deletions
+20
-0
test_socket.py
Lib/test/test_socket.py
+20
-0
No files found.
Lib/test/test_socket.py
Dosyayı görüntüle @
21ebbb7d
...
@@ -259,6 +259,7 @@ class GeneralModuleTests(unittest.TestCase):
...
@@ -259,6 +259,7 @@ class GeneralModuleTests(unittest.TestCase):
def
test_repr
(
self
):
def
test_repr
(
self
):
s
=
socket
.
socket
(
socket
.
AF_INET
,
socket
.
SOCK_STREAM
)
s
=
socket
.
socket
(
socket
.
AF_INET
,
socket
.
SOCK_STREAM
)
self
.
addCleanup
(
s
.
close
)
self
.
assertTrue
(
repr
(
s
)
.
startswith
(
"<socket.socket object"
))
self
.
assertTrue
(
repr
(
s
)
.
startswith
(
"<socket.socket object"
))
def
test_weakref
(
self
):
def
test_weakref
(
self
):
...
@@ -532,6 +533,7 @@ class GeneralModuleTests(unittest.TestCase):
...
@@ -532,6 +533,7 @@ class GeneralModuleTests(unittest.TestCase):
# Testing getsockname()
# Testing getsockname()
port
=
self
.
_get_unused_port
()
port
=
self
.
_get_unused_port
()
sock
=
socket
.
socket
(
socket
.
AF_INET
,
socket
.
SOCK_STREAM
)
sock
=
socket
.
socket
(
socket
.
AF_INET
,
socket
.
SOCK_STREAM
)
self
.
addCleanup
(
sock
.
close
)
sock
.
bind
((
"0.0.0.0"
,
port
))
sock
.
bind
((
"0.0.0.0"
,
port
))
name
=
sock
.
getsockname
()
name
=
sock
.
getsockname
()
# XXX(nnorwitz): http://tinyurl.com/os5jz seems to indicate
# XXX(nnorwitz): http://tinyurl.com/os5jz seems to indicate
...
@@ -545,12 +547,14 @@ class GeneralModuleTests(unittest.TestCase):
...
@@ -545,12 +547,14 @@ class GeneralModuleTests(unittest.TestCase):
# Testing getsockopt()
# Testing getsockopt()
# We know a socket should start without reuse==0
# We know a socket should start without reuse==0
sock
=
socket
.
socket
(
socket
.
AF_INET
,
socket
.
SOCK_STREAM
)
sock
=
socket
.
socket
(
socket
.
AF_INET
,
socket
.
SOCK_STREAM
)
self
.
addCleanup
(
sock
.
close
)
reuse
=
sock
.
getsockopt
(
socket
.
SOL_SOCKET
,
socket
.
SO_REUSEADDR
)
reuse
=
sock
.
getsockopt
(
socket
.
SOL_SOCKET
,
socket
.
SO_REUSEADDR
)
self
.
assertFalse
(
reuse
!=
0
,
"initial mode is reuse"
)
self
.
assertFalse
(
reuse
!=
0
,
"initial mode is reuse"
)
def
testSetSockOpt
(
self
):
def
testSetSockOpt
(
self
):
# Testing setsockopt()
# Testing setsockopt()
sock
=
socket
.
socket
(
socket
.
AF_INET
,
socket
.
SOCK_STREAM
)
sock
=
socket
.
socket
(
socket
.
AF_INET
,
socket
.
SOCK_STREAM
)
self
.
addCleanup
(
sock
.
close
)
sock
.
setsockopt
(
socket
.
SOL_SOCKET
,
socket
.
SO_REUSEADDR
,
1
)
sock
.
setsockopt
(
socket
.
SOL_SOCKET
,
socket
.
SO_REUSEADDR
,
1
)
reuse
=
sock
.
getsockopt
(
socket
.
SOL_SOCKET
,
socket
.
SO_REUSEADDR
)
reuse
=
sock
.
getsockopt
(
socket
.
SOL_SOCKET
,
socket
.
SO_REUSEADDR
)
self
.
assertFalse
(
reuse
==
0
,
"failed to set reuse mode"
)
self
.
assertFalse
(
reuse
==
0
,
"failed to set reuse mode"
)
...
@@ -784,6 +788,7 @@ class BasicTCPTest(SocketConnectedTest):
...
@@ -784,6 +788,7 @@ class BasicTCPTest(SocketConnectedTest):
# Testing fromfd()
# Testing fromfd()
fd
=
self
.
cli_conn
.
fileno
()
fd
=
self
.
cli_conn
.
fileno
()
sock
=
socket
.
fromfd
(
fd
,
socket
.
AF_INET
,
socket
.
SOCK_STREAM
)
sock
=
socket
.
fromfd
(
fd
,
socket
.
AF_INET
,
socket
.
SOCK_STREAM
)
self
.
addCleanup
(
sock
.
close
)
self
.
assertIsInstance
(
sock
,
socket
.
socket
)
self
.
assertIsInstance
(
sock
,
socket
.
socket
)
msg
=
sock
.
recv
(
1024
)
msg
=
sock
.
recv
(
1024
)
self
.
assertEqual
(
msg
,
MSG
)
self
.
assertEqual
(
msg
,
MSG
)
...
@@ -794,6 +799,7 @@ class BasicTCPTest(SocketConnectedTest):
...
@@ -794,6 +799,7 @@ class BasicTCPTest(SocketConnectedTest):
def
testDup
(
self
):
def
testDup
(
self
):
# Testing dup()
# Testing dup()
sock
=
self
.
cli_conn
.
dup
()
sock
=
self
.
cli_conn
.
dup
()
self
.
addCleanup
(
sock
.
close
)
msg
=
sock
.
recv
(
1024
)
msg
=
sock
.
recv
(
1024
)
self
.
assertEqual
(
msg
,
MSG
)
self
.
assertEqual
(
msg
,
MSG
)
...
@@ -824,6 +830,7 @@ class BasicTCPTest(SocketConnectedTest):
...
@@ -824,6 +830,7 @@ class BasicTCPTest(SocketConnectedTest):
# ...but we can create another socket using the (still open)
# ...but we can create another socket using the (still open)
# file descriptor
# file descriptor
sock
=
socket
.
socket
(
socket
.
AF_INET
,
socket
.
SOCK_STREAM
,
fileno
=
f
)
sock
=
socket
.
socket
(
socket
.
AF_INET
,
socket
.
SOCK_STREAM
,
fileno
=
f
)
self
.
addCleanup
(
sock
.
close
)
msg
=
sock
.
recv
(
1024
)
msg
=
sock
.
recv
(
1024
)
self
.
assertEqual
(
msg
,
MSG
)
self
.
assertEqual
(
msg
,
MSG
)
...
@@ -970,6 +977,7 @@ class NonBlockingTCPTests(ThreadedTCPSocketTest):
...
@@ -970,6 +977,7 @@ class NonBlockingTCPTests(ThreadedTCPSocketTest):
read
,
write
,
err
=
select
.
select
([
self
.
serv
],
[],
[])
read
,
write
,
err
=
select
.
select
([
self
.
serv
],
[],
[])
if
self
.
serv
in
read
:
if
self
.
serv
in
read
:
conn
,
addr
=
self
.
serv
.
accept
()
conn
,
addr
=
self
.
serv
.
accept
()
conn
.
close
()
else
:
else
:
self
.
fail
(
"Error trying to do accept after select."
)
self
.
fail
(
"Error trying to do accept after select."
)
...
@@ -980,6 +988,7 @@ class NonBlockingTCPTests(ThreadedTCPSocketTest):
...
@@ -980,6 +988,7 @@ class NonBlockingTCPTests(ThreadedTCPSocketTest):
def
testConnect
(
self
):
def
testConnect
(
self
):
# Testing non-blocking connect
# Testing non-blocking connect
conn
,
addr
=
self
.
serv
.
accept
()
conn
,
addr
=
self
.
serv
.
accept
()
conn
.
close
()
def
_testConnect
(
self
):
def
_testConnect
(
self
):
self
.
cli
.
settimeout
(
10
)
self
.
cli
.
settimeout
(
10
)
...
@@ -998,6 +1007,7 @@ class NonBlockingTCPTests(ThreadedTCPSocketTest):
...
@@ -998,6 +1007,7 @@ class NonBlockingTCPTests(ThreadedTCPSocketTest):
read
,
write
,
err
=
select
.
select
([
conn
],
[],
[])
read
,
write
,
err
=
select
.
select
([
conn
],
[],
[])
if
conn
in
read
:
if
conn
in
read
:
msg
=
conn
.
recv
(
len
(
MSG
))
msg
=
conn
.
recv
(
len
(
MSG
))
conn
.
close
()
self
.
assertEqual
(
msg
,
MSG
)
self
.
assertEqual
(
msg
,
MSG
)
else
:
else
:
self
.
fail
(
"Error during select call to non-blocking socket."
)
self
.
fail
(
"Error during select call to non-blocking socket."
)
...
@@ -1441,6 +1451,7 @@ class NetworkConnectionNoServer(unittest.TestCase):
...
@@ -1441,6 +1451,7 @@ class NetworkConnectionNoServer(unittest.TestCase):
def
test_connect
(
self
):
def
test_connect
(
self
):
port
=
support
.
find_unused_port
()
port
=
support
.
find_unused_port
()
cli
=
socket
.
socket
(
socket
.
AF_INET
,
socket
.
SOCK_STREAM
)
cli
=
socket
.
socket
(
socket
.
AF_INET
,
socket
.
SOCK_STREAM
)
self
.
addCleanup
(
cli
.
close
)
with
self
.
assertRaises
(
socket
.
error
)
as
cm
:
with
self
.
assertRaises
(
socket
.
error
)
as
cm
:
cli
.
connect
((
HOST
,
port
))
cli
.
connect
((
HOST
,
port
))
self
.
assertEqual
(
cm
.
exception
.
errno
,
errno
.
ECONNREFUSED
)
self
.
assertEqual
(
cm
.
exception
.
errno
,
errno
.
ECONNREFUSED
)
...
@@ -1478,16 +1489,19 @@ class NetworkConnectionAttributesTest(SocketTCPTest, ThreadableTest):
...
@@ -1478,16 +1489,19 @@ class NetworkConnectionAttributesTest(SocketTCPTest, ThreadableTest):
def
_justAccept
(
self
):
def
_justAccept
(
self
):
conn
,
addr
=
self
.
serv
.
accept
()
conn
,
addr
=
self
.
serv
.
accept
()
conn
.
close
()
testFamily
=
_justAccept
testFamily
=
_justAccept
def
_testFamily
(
self
):
def
_testFamily
(
self
):
self
.
cli
=
socket
.
create_connection
((
HOST
,
self
.
port
),
timeout
=
30
)
self
.
cli
=
socket
.
create_connection
((
HOST
,
self
.
port
),
timeout
=
30
)
self
.
addCleanup
(
self
.
cli
.
close
)
self
.
assertEqual
(
self
.
cli
.
family
,
2
)
self
.
assertEqual
(
self
.
cli
.
family
,
2
)
testSourceAddress
=
_justAccept
testSourceAddress
=
_justAccept
def
_testSourceAddress
(
self
):
def
_testSourceAddress
(
self
):
self
.
cli
=
socket
.
create_connection
((
HOST
,
self
.
port
),
timeout
=
30
,
self
.
cli
=
socket
.
create_connection
((
HOST
,
self
.
port
),
timeout
=
30
,
source_address
=
(
''
,
self
.
source_port
))
source_address
=
(
''
,
self
.
source_port
))
self
.
addCleanup
(
self
.
cli
.
close
)
self
.
assertEqual
(
self
.
cli
.
getsockname
()[
1
],
self
.
source_port
)
self
.
assertEqual
(
self
.
cli
.
getsockname
()[
1
],
self
.
source_port
)
# The port number being used is sufficient to show that the bind()
# The port number being used is sufficient to show that the bind()
# call happened.
# call happened.
...
@@ -1499,6 +1513,7 @@ class NetworkConnectionAttributesTest(SocketTCPTest, ThreadableTest):
...
@@ -1499,6 +1513,7 @@ class NetworkConnectionAttributesTest(SocketTCPTest, ThreadableTest):
socket
.
setdefaulttimeout
(
42
)
socket
.
setdefaulttimeout
(
42
)
try
:
try
:
self
.
cli
=
socket
.
create_connection
((
HOST
,
self
.
port
))
self
.
cli
=
socket
.
create_connection
((
HOST
,
self
.
port
))
self
.
addCleanup
(
self
.
cli
.
close
)
finally
:
finally
:
socket
.
setdefaulttimeout
(
None
)
socket
.
setdefaulttimeout
(
None
)
self
.
assertEquals
(
self
.
cli
.
gettimeout
(),
42
)
self
.
assertEquals
(
self
.
cli
.
gettimeout
(),
42
)
...
@@ -1510,6 +1525,7 @@ class NetworkConnectionAttributesTest(SocketTCPTest, ThreadableTest):
...
@@ -1510,6 +1525,7 @@ class NetworkConnectionAttributesTest(SocketTCPTest, ThreadableTest):
socket
.
setdefaulttimeout
(
30
)
socket
.
setdefaulttimeout
(
30
)
try
:
try
:
self
.
cli
=
socket
.
create_connection
((
HOST
,
self
.
port
),
timeout
=
None
)
self
.
cli
=
socket
.
create_connection
((
HOST
,
self
.
port
),
timeout
=
None
)
self
.
addCleanup
(
self
.
cli
.
close
)
finally
:
finally
:
socket
.
setdefaulttimeout
(
None
)
socket
.
setdefaulttimeout
(
None
)
self
.
assertEqual
(
self
.
cli
.
gettimeout
(),
None
)
self
.
assertEqual
(
self
.
cli
.
gettimeout
(),
None
)
...
@@ -1522,6 +1538,7 @@ class NetworkConnectionAttributesTest(SocketTCPTest, ThreadableTest):
...
@@ -1522,6 +1538,7 @@ class NetworkConnectionAttributesTest(SocketTCPTest, ThreadableTest):
testTimeoutValueNonamed
=
_justAccept
testTimeoutValueNonamed
=
_justAccept
def
_testTimeoutValueNonamed
(
self
):
def
_testTimeoutValueNonamed
(
self
):
self
.
cli
=
socket
.
create_connection
((
HOST
,
self
.
port
),
30
)
self
.
cli
=
socket
.
create_connection
((
HOST
,
self
.
port
),
30
)
self
.
addCleanup
(
self
.
cli
.
close
)
self
.
assertEqual
(
self
.
cli
.
gettimeout
(),
30
)
self
.
assertEqual
(
self
.
cli
.
gettimeout
(),
30
)
@unittest.skipUnless
(
thread
,
'Threading required for this test.'
)
@unittest.skipUnless
(
thread
,
'Threading required for this test.'
)
...
@@ -1541,6 +1558,7 @@ class NetworkConnectionBehaviourTest(SocketTCPTest, ThreadableTest):
...
@@ -1541,6 +1558,7 @@ class NetworkConnectionBehaviourTest(SocketTCPTest, ThreadableTest):
def
testInsideTimeout
(
self
):
def
testInsideTimeout
(
self
):
conn
,
addr
=
self
.
serv
.
accept
()
conn
,
addr
=
self
.
serv
.
accept
()
self
.
addCleanup
(
conn
.
close
)
time
.
sleep
(
3
)
time
.
sleep
(
3
)
conn
.
send
(
b
"done!"
)
conn
.
send
(
b
"done!"
)
testOutsideTimeout
=
testInsideTimeout
testOutsideTimeout
=
testInsideTimeout
...
@@ -1834,6 +1852,7 @@ class ContextManagersTest(ThreadedTCPSocketTest):
...
@@ -1834,6 +1852,7 @@ class ContextManagersTest(ThreadedTCPSocketTest):
def
testCreateConnectionBase
(
self
):
def
testCreateConnectionBase
(
self
):
conn
,
addr
=
self
.
serv
.
accept
()
conn
,
addr
=
self
.
serv
.
accept
()
self
.
addCleanup
(
conn
.
close
)
data
=
conn
.
recv
(
1024
)
data
=
conn
.
recv
(
1024
)
conn
.
sendall
(
data
)
conn
.
sendall
(
data
)
...
@@ -1847,6 +1866,7 @@ class ContextManagersTest(ThreadedTCPSocketTest):
...
@@ -1847,6 +1866,7 @@ class ContextManagersTest(ThreadedTCPSocketTest):
def
testCreateConnectionClose
(
self
):
def
testCreateConnectionClose
(
self
):
conn
,
addr
=
self
.
serv
.
accept
()
conn
,
addr
=
self
.
serv
.
accept
()
self
.
addCleanup
(
conn
.
close
)
data
=
conn
.
recv
(
1024
)
data
=
conn
.
recv
(
1024
)
conn
.
sendall
(
data
)
conn
.
sendall
(
data
)
...
...
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