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
b2ff3cf0
Kaydet (Commit)
b2ff3cf0
authored
Agu 31, 2013
tarafından
Eli Bendersky
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
Switch the AF_* and SOCK_* constants in the socket module to IntEnum.
Closes #18720.
üst
7e7cf8bc
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
91 additions
and
3 deletions
+91
-3
socket.py
Lib/socket.py
+65
-1
test_socket.py
Lib/test/test_socket.py
+26
-2
No files found.
Lib/socket.py
Dosyayı görüntüle @
b2ff3cf0
...
@@ -48,6 +48,7 @@ import _socket
...
@@ -48,6 +48,7 @@ import _socket
from
_socket
import
*
from
_socket
import
*
import
os
,
sys
,
io
import
os
,
sys
,
io
from
enum
import
IntEnum
try
:
try
:
import
errno
import
errno
...
@@ -60,6 +61,30 @@ EWOULDBLOCK = getattr(errno, 'EWOULDBLOCK', 11)
...
@@ -60,6 +61,30 @@ EWOULDBLOCK = getattr(errno, 'EWOULDBLOCK', 11)
__all__
=
[
"getfqdn"
,
"create_connection"
]
__all__
=
[
"getfqdn"
,
"create_connection"
]
__all__
.
extend
(
os
.
_get_exports_list
(
_socket
))
__all__
.
extend
(
os
.
_get_exports_list
(
_socket
))
# Set up the socket.AF_* socket.SOCK_* constants as members of IntEnums for
# nicer string representations.
# Note that _socket only knows about the integer values. The public interface
# in this module understands the enums and translates them back from integers
# where needed (e.g. .family property of a socket object).
AddressFamily
=
IntEnum
(
'AddressFamily'
,
{
name
:
value
for
name
,
value
in
globals
()
.
items
()
if
name
.
isupper
()
and
name
.
startswith
(
'AF_'
)})
globals
()
.
update
(
AddressFamily
.
__members__
)
SocketType
=
IntEnum
(
'SocketType'
,
{
name
:
value
for
name
,
value
in
globals
()
.
items
()
if
name
.
isupper
()
and
name
.
startswith
(
'SOCK_'
)})
globals
()
.
update
(
SocketType
.
__members__
)
def
_intenum_converter
(
value
,
enum_klass
):
"""Convert a numeric family value to an IntEnum member.
If it's not a known member, return the numeric value itself.
"""
try
:
return
enum_klass
(
value
)
except
ValueError
:
return
value
_realsocket
=
socket
_realsocket
=
socket
...
@@ -91,6 +116,10 @@ class socket(_socket.socket):
...
@@ -91,6 +116,10 @@ class socket(_socket.socket):
__slots__
=
[
"__weakref__"
,
"_io_refs"
,
"_closed"
]
__slots__
=
[
"__weakref__"
,
"_io_refs"
,
"_closed"
]
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
):
# For user code address family and type values are IntEnum members, but
# for the underlying _socket.socket they're just integers. The
# constructor of _socket.socket converts the given argument to an
# integer automatically.
_socket
.
socket
.
__init__
(
self
,
family
,
type
,
proto
,
fileno
)
_socket
.
socket
.
__init__
(
self
,
family
,
type
,
proto
,
fileno
)
self
.
_io_refs
=
0
self
.
_io_refs
=
0
self
.
_closed
=
False
self
.
_closed
=
False
...
@@ -230,6 +259,18 @@ class socket(_socket.socket):
...
@@ -230,6 +259,18 @@ class socket(_socket.socket):
self
.
_closed
=
True
self
.
_closed
=
True
return
super
()
.
detach
()
return
super
()
.
detach
()
@property
def
family
(
self
):
"""Read-only access to the address family for this socket.
"""
return
_intenum_converter
(
super
()
.
family
,
AddressFamily
)
@property
def
type
(
self
):
"""Read-only access to the socket type.
"""
return
_intenum_converter
(
super
()
.
type
,
SocketType
)
if
os
.
name
==
'nt'
:
if
os
.
name
==
'nt'
:
def
get_inheritable
(
self
):
def
get_inheritable
(
self
):
return
os
.
get_handle_inheritable
(
self
.
fileno
())
return
os
.
get_handle_inheritable
(
self
.
fileno
())
...
@@ -243,7 +284,6 @@ class socket(_socket.socket):
...
@@ -243,7 +284,6 @@ class socket(_socket.socket):
get_inheritable
.
__doc__
=
"Get the inheritable flag of the socket"
get_inheritable
.
__doc__
=
"Get the inheritable flag of the socket"
set_inheritable
.
__doc__
=
"Set the inheritable flag of the socket"
set_inheritable
.
__doc__
=
"Set the inheritable flag of the socket"
def
fromfd
(
fd
,
family
,
type
,
proto
=
0
):
def
fromfd
(
fd
,
family
,
type
,
proto
=
0
):
""" fromfd(fd, family, type[, proto]) -> socket object
""" fromfd(fd, family, type[, proto]) -> socket object
...
@@ -469,3 +509,27 @@ def create_connection(address, timeout=_GLOBAL_DEFAULT_TIMEOUT,
...
@@ -469,3 +509,27 @@ def create_connection(address, timeout=_GLOBAL_DEFAULT_TIMEOUT,
raise
err
raise
err
else
:
else
:
raise
error
(
"getaddrinfo returns an empty list"
)
raise
error
(
"getaddrinfo returns an empty list"
)
def
getaddrinfo
(
host
,
port
,
family
=
0
,
type
=
0
,
proto
=
0
,
flags
=
0
):
"""Resolve host and port into list of address info entries.
Translate the host/port argument into a sequence of 5-tuples that contain
all the necessary arguments for creating a socket connected to that service.
host is a domain name, a string representation of an IPv4/v6 address or
None. port is a string service name such as 'http', a numeric port number or
None. By passing None as the value of host and port, you can pass NULL to
the underlying C API.
The family, type and proto arguments can be optionally specified in order to
narrow the list of addresses returned. Passing zero as a value for each of
these arguments selects the full range of results.
"""
# We override this function since we want to translate the numeric family
# and socket type values to enum constants.
addrlist
=
[]
for
res
in
_socket
.
getaddrinfo
(
host
,
port
,
family
,
type
,
proto
,
flags
):
af
,
socktype
,
proto
,
canonname
,
sa
=
res
addrlist
.
append
((
_intenum_converter
(
af
,
AddressFamily
),
_intenum_converter
(
socktype
,
SocketType
),
proto
,
canonname
,
sa
))
return
addrlist
Lib/test/test_socket.py
Dosyayı görüntüle @
b2ff3cf0
...
@@ -1161,9 +1161,12 @@ class GeneralModuleTests(unittest.TestCase):
...
@@ -1161,9 +1161,12 @@ class GeneralModuleTests(unittest.TestCase):
socket
.
getaddrinfo
(
HOST
,
80
)
socket
.
getaddrinfo
(
HOST
,
80
)
socket
.
getaddrinfo
(
HOST
,
None
)
socket
.
getaddrinfo
(
HOST
,
None
)
# test family and socktype filters
# test family and socktype filters
infos
=
socket
.
getaddrinfo
(
HOST
,
None
,
socket
.
AF_INET
)
infos
=
socket
.
getaddrinfo
(
HOST
,
80
,
socket
.
AF_INET
,
socket
.
SOCK_STREAM
)
for
family
,
_
,
_
,
_
,
_
in
infos
:
for
family
,
type
,
_
,
_
,
_
in
infos
:
self
.
assertEqual
(
family
,
socket
.
AF_INET
)
self
.
assertEqual
(
family
,
socket
.
AF_INET
)
self
.
assertEqual
(
str
(
family
),
'AddressFamily.AF_INET'
)
self
.
assertEqual
(
type
,
socket
.
SOCK_STREAM
)
self
.
assertEqual
(
str
(
type
),
'SocketType.SOCK_STREAM'
)
infos
=
socket
.
getaddrinfo
(
HOST
,
None
,
0
,
socket
.
SOCK_STREAM
)
infos
=
socket
.
getaddrinfo
(
HOST
,
None
,
0
,
socket
.
SOCK_STREAM
)
for
_
,
socktype
,
_
,
_
,
_
in
infos
:
for
_
,
socktype
,
_
,
_
,
_
in
infos
:
self
.
assertEqual
(
socktype
,
socket
.
SOCK_STREAM
)
self
.
assertEqual
(
socktype
,
socket
.
SOCK_STREAM
)
...
@@ -1321,6 +1324,27 @@ class GeneralModuleTests(unittest.TestCase):
...
@@ -1321,6 +1324,27 @@ class GeneralModuleTests(unittest.TestCase):
with
socket
.
socket
(
socket
.
AF_INET6
,
socket
.
SOCK_STREAM
)
as
s
:
with
socket
.
socket
(
socket
.
AF_INET6
,
socket
.
SOCK_STREAM
)
as
s
:
self
.
assertRaises
(
OverflowError
,
s
.
bind
,
(
support
.
HOSTv6
,
0
,
-
10
))
self
.
assertRaises
(
OverflowError
,
s
.
bind
,
(
support
.
HOSTv6
,
0
,
-
10
))
def
test_str_for_enums
(
self
):
# Make sure that the AF_* and SOCK_* constants have enum-like string
# reprs.
with
socket
.
socket
(
socket
.
AF_INET
,
socket
.
SOCK_STREAM
)
as
s
:
self
.
assertEqual
(
str
(
s
.
family
),
'AddressFamily.AF_INET'
)
self
.
assertEqual
(
str
(
s
.
type
),
'SocketType.SOCK_STREAM'
)
@unittest.skipIf
(
os
.
name
==
'nt'
,
'Will not work on Windows'
)
def
test_uknown_socket_family_repr
(
self
):
# Test that when created with a family that's not one of the known
# AF_*/SOCK_* constants, socket.family just returns the number.
#
# To do this we fool socket.socket into believing it already has an
# open fd because on this path it doesn't actually verify the family and
# type and populates the socket object.
#
# On Windows this trick won't work, so the test is skipped.
fd
,
_
=
tempfile
.
mkstemp
()
with
socket
.
socket
(
family
=
42424
,
type
=
13331
,
fileno
=
fd
)
as
s
:
self
.
assertEqual
(
s
.
family
,
42424
)
self
.
assertEqual
(
s
.
type
,
13331
)
@unittest.skipUnless
(
HAVE_SOCKET_CAN
,
'SocketCan required for this test.'
)
@unittest.skipUnless
(
HAVE_SOCKET_CAN
,
'SocketCan required for this test.'
)
class
BasicCANTest
(
unittest
.
TestCase
):
class
BasicCANTest
(
unittest
.
TestCase
):
...
...
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