Skip to content

Commit 3d44035

Browse files
committed
Avoid urllib.parse.splitport DeprecationWarning
“DeprecationWarning: urllib.parse.splitport() is deprecated as of 3.8, use urllib.parse.urlparse() instead” Signed-off-by: Anders Kaseorg <andersk@mit.edu>
1 parent 801f344 commit 3d44035

File tree

2 files changed

+6
-13
lines changed

2 files changed

+6
-13
lines changed

bmemcached/protocol.py

+4-10
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
from datetime import datetime, timedelta
22
import logging
3-
import re
43
import socket
54
import struct
65
import threading
76
try:
8-
from urllib import splitport # type: ignore
7+
from urlparse import SplitResult # type: ignore[import-not-found]
98
except ImportError:
10-
from urllib.parse import splitport # type: ignore
9+
from urllib.parse import SplitResult # type: ignore[import-not-found]
1110

1211
import zlib
1312
from io import BytesIO
@@ -180,13 +179,8 @@ def split_host_port(cls, server):
180179
>>> split_host_port('127.0.0.1')
181180
('127.0.0.1', 11211)
182181
"""
183-
host, port = splitport(server)
184-
if port is None:
185-
port = 11211
186-
port = int(port)
187-
if re.search(':.*$', host):
188-
host = re.sub(':.*$', '', host)
189-
return host, port
182+
u = SplitResult("", server, "", "", "")
183+
return u.hostname, 11211 if u.port is None else u.port
190184

191185
def _read_socket(self, size):
192186
"""

test/test_server_parsing.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,8 @@ def testNoPortGiven(self):
2727
self.assertEqual(server.port, 11211)
2828

2929
def testInvalidPort(self):
30-
server = bmemcached.protocol.Protocol('{}:blah'.format(os.environ['MEMCACHED_HOST']))
31-
self.assertEqual(server.host, os.environ['MEMCACHED_HOST'])
32-
self.assertEqual(server.port, 11211)
30+
with self.assertRaises(ValueError):
31+
bmemcached.protocol.Protocol('{}:blah'.format(os.environ['MEMCACHED_HOST']))
3332

3433
def testNonStandardPort(self):
3534
server = bmemcached.protocol.Protocol('{}:5000'.format(os.environ['MEMCACHED_HOST']))

0 commit comments

Comments
 (0)