Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 103a6d6

Browse files
committed
Issue 11177: asyncore's create_socket() arguments can now be omitted.
1 parent 0bd4deb commit 103a6d6

4 files changed

Lines changed: 18 additions & 14 deletions

File tree

Doc/library/asyncore.rst

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -184,12 +184,14 @@ any that have been added to the map during asynchronous service) is closed.
184184
Most of these are nearly identical to their socket partners.
185185

186186

187-
.. method:: create_socket(family, type)
187+
.. method:: create_socket(family=socket.AF_INET, type=socket.SOCK_STREAM)
188188

189189
This is identical to the creation of a normal socket, and will use the
190190
same options for creation. Refer to the :mod:`socket` documentation for
191191
information on creating sockets.
192192

193+
.. versionchanged:: 3.3 family and type arguments can be omitted.
194+
193195

194196
.. method:: connect(address)
195197

@@ -280,7 +282,7 @@ implement its socket handling::
280282

281283
def __init__(self, host, path):
282284
asyncore.dispatcher.__init__(self)
283-
self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
285+
self.create_socket()
284286
self.connect( (host, 80) )
285287
self.buffer = bytes('GET %s HTTP/1.0\r\n\r\n' % path, 'ascii')
286288

@@ -326,7 +328,7 @@ connections and dispatches the incoming connections to a handler::
326328

327329
def __init__(self, host, port):
328330
asyncore.dispatcher.__init__(self)
329-
self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
331+
self.create_socket()
330332
self.set_reuse_addr()
331333
self.bind((host, port))
332334
self.listen(5)

Lib/asyncore.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ def del_channel(self, map=None):
287287
del map[fd]
288288
self._fileno = None
289289

290-
def create_socket(self, family, type):
290+
def create_socket(self, family=socket.AF_INET, type=socket.SOCK_STREAM):
291291
self.family_and_type = family, type
292292
sock = socket.socket(family, type)
293293
sock.setblocking(0)

Lib/test/test_asyncore.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ def tearDown(self):
352352
@support.reap_threads
353353
def test_send(self):
354354
evt = threading.Event()
355-
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
355+
sock = socket.socket()
356356
sock.settimeout(3)
357357
port = support.bind_port(sock)
358358

@@ -367,7 +367,7 @@ def test_send(self):
367367

368368
data = b"Suppose there isn't a 16-ton weight?"
369369
d = dispatcherwithsend_noread()
370-
d.create_socket(socket.AF_INET, socket.SOCK_STREAM)
370+
d.create_socket()
371371
d.connect((HOST, port))
372372

373373
# give time for socket to connect
@@ -474,7 +474,7 @@ class TCPServer(asyncore.dispatcher):
474474

475475
def __init__(self, handler=BaseTestHandler, host=HOST, port=0):
476476
asyncore.dispatcher.__init__(self)
477-
self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
477+
self.create_socket()
478478
self.set_reuse_addr()
479479
self.bind((host, port))
480480
self.listen(5)
@@ -495,7 +495,7 @@ class BaseClient(BaseTestHandler):
495495

496496
def __init__(self, address):
497497
BaseTestHandler.__init__(self)
498-
self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
498+
self.create_socket()
499499
self.connect(address)
500500

501501
def handle_connect(self):
@@ -536,7 +536,7 @@ class TestListener(BaseTestHandler):
536536

537537
def __init__(self):
538538
BaseTestHandler.__init__(self)
539-
self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
539+
self.create_socket()
540540
self.bind((HOST, 0))
541541
self.listen(5)
542542
self.address = self.socket.getsockname()[:2]
@@ -555,7 +555,7 @@ class TestListener(BaseTestHandler):
555555

556556
def __init__(self):
557557
BaseTestHandler.__init__(self)
558-
self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
558+
self.create_socket()
559559
self.bind((HOST, 0))
560560
self.listen(5)
561561
self.address = self.socket.getsockname()[:2]
@@ -693,20 +693,20 @@ def test_connection_attributes(self):
693693

694694
def test_create_socket(self):
695695
s = asyncore.dispatcher()
696-
s.create_socket(socket.AF_INET, socket.SOCK_STREAM)
696+
s.create_socket()
697697
self.assertEqual(s.socket.family, socket.AF_INET)
698698
SOCK_NONBLOCK = getattr(socket, 'SOCK_NONBLOCK', 0)
699699
self.assertEqual(s.socket.type, socket.SOCK_STREAM | SOCK_NONBLOCK)
700700

701701
def test_bind(self):
702702
s1 = asyncore.dispatcher()
703-
s1.create_socket(socket.AF_INET, socket.SOCK_STREAM)
703+
s1.create_socket()
704704
s1.bind((HOST, 0))
705705
s1.listen(5)
706706
port = s1.socket.getsockname()[1]
707707

708708
s2 = asyncore.dispatcher()
709-
s2.create_socket(socket.AF_INET, socket.SOCK_STREAM)
709+
s2.create_socket()
710710
# EADDRINUSE indicates the socket was correctly bound
711711
self.assertRaises(socket.error, s2.bind, (HOST, port))
712712

@@ -723,7 +723,7 @@ def test_set_reuse_addr(self):
723723
self.assertFalse(s.socket.getsockopt(socket.SOL_SOCKET,
724724
socket.SO_REUSEADDR))
725725
s.socket.close()
726-
s.create_socket(socket.AF_INET, socket.SOCK_STREAM)
726+
s.create_socket()
727727
s.set_reuse_addr()
728728
self.assertTrue(s.socket.getsockopt(socket.SOL_SOCKET,
729729
socket.SO_REUSEADDR))

Misc/NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ Core and Builtins
3535
Library
3636
-------
3737

38+
- Issue 11177: asyncore's create_socket() arguments can now be omitted.
39+
3840
- Issue #6064: Add a ``daemon`` keyword argument to the threading.Thread
3941
and multiprocessing.Process constructors in order to override the
4042
default behaviour of inheriting the daemonic property from the current

0 commit comments

Comments
 (0)