Skip to content

Commit e578759

Browse files
committed
Reduced helper_randon dependency from network module
1 parent 205e253 commit e578759

File tree

6 files changed

+18
-19
lines changed

6 files changed

+18
-19
lines changed

src/network/addrthread.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
"""
22
Announce addresses as they are received from other hosts
33
"""
4+
import random
45
from six.moves import queue
56

67
# magic imports!
78
import connectionpool
8-
from helper_random import randomshuffle
99
from protocol import assembleAddrMessage
1010
from queues import addrQueue # FIXME: init with queue
1111

@@ -29,9 +29,9 @@ def run(self):
2929
if chunk:
3030
# Choose peers randomly
3131
connections = connectionpool.pool.establishedConnections()
32-
randomshuffle(connections)
32+
random.shuffle(connections)
3333
for i in connections:
34-
randomshuffle(chunk)
34+
random.shuffle(chunk)
3535
filtered = []
3636
for stream, peer, seen, destination in chunk:
3737
# peer's own address or address received from peer

src/network/asyncore_pollchoose.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import os
1010
import select
1111
import socket
12+
import random
1213
import sys
1314
import time
1415
import warnings
@@ -19,7 +20,6 @@
1920
)
2021
from threading import current_thread
2122

22-
import helper_random
2323

2424
try:
2525
from errno import WSAEWOULDBLOCK
@@ -233,13 +233,13 @@ def select_poller(timeout=0.0, map=None):
233233
if err.args[0] in (WSAENOTSOCK, ):
234234
return
235235

236-
for fd in helper_random.randomsample(r, len(r)):
236+
for fd in random.sample(r, len(r)):
237237
obj = map.get(fd)
238238
if obj is None:
239239
continue
240240
read(obj)
241241

242-
for fd in helper_random.randomsample(w, len(w)):
242+
for fd in random.sample(w, len(w)):
243243
obj = map.get(fd)
244244
if obj is None:
245245
continue
@@ -297,7 +297,7 @@ def poll_poller(timeout=0.0, map=None):
297297
except socket.error as err:
298298
if err.args[0] in (EBADF, WSAENOTSOCK, EINTR):
299299
return
300-
for fd, flags in helper_random.randomsample(r, len(r)):
300+
for fd, flags in random.sample(r, len(r)):
301301
obj = map.get(fd)
302302
if obj is None:
303303
continue
@@ -357,7 +357,7 @@ def epoll_poller(timeout=0.0, map=None):
357357
if err.args[0] != EINTR:
358358
raise
359359
r = []
360-
for fd, flags in helper_random.randomsample(r, len(r)):
360+
for fd, flags in random.sample(r, len(r)):
361361
obj = map.get(fd)
362362
if obj is None:
363363
continue
@@ -420,7 +420,7 @@ def kqueue_poller(timeout=0.0, map=None):
420420

421421
events = kqueue_poller.pollster.control(updates, selectables, timeout)
422422
if len(events) > 1:
423-
events = helper_random.randomsample(events, len(events))
423+
events = random.sample(events, len(events))
424424

425425
for event in events:
426426
fd = event.ident

src/network/connectionpool.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
import socket
88
import sys
99
import time
10+
import random
1011

1112
import asyncore_pollchoose as asyncore
12-
import helper_random
1313
import knownnodes
1414
import protocol
1515
import state
@@ -210,7 +210,7 @@ def startBootstrappers(self):
210210
connection_base = TCPConnection
211211
elif proxy_type == 'SOCKS5':
212212
connection_base = Socks5BMConnection
213-
hostname = helper_random.randomchoice([
213+
hostname = random.choice([ # nosec B311
214214
'quzwelsuziwqgpt2.onion', None
215215
])
216216
elif proxy_type == 'SOCKS4a':
@@ -222,7 +222,7 @@ def startBootstrappers(self):
222222

223223
bootstrapper = bootstrap(connection_base)
224224
if not hostname:
225-
port = helper_random.randomchoice([8080, 8444])
225+
port = random.choice([8080, 8444]) # nosec B311
226226
hostname = 'bootstrap%s.bitmessage.org' % port
227227
else:
228228
port = 8444
@@ -289,7 +289,7 @@ def loop(self): # pylint: disable=too-many-branches,too-many-statements
289289
state.maximumNumberOfHalfOpenConnections - pending):
290290
try:
291291
chosen = self.trustedPeer or chooseConnection(
292-
helper_random.randomchoice(self.streams))
292+
random.choice(self.streams)) # nosec B311
293293
except ValueError:
294294
continue
295295
if chosen in self.outboundConnections:

src/network/downloadthread.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
`DownloadThread` class definition
33
"""
44
import time
5+
import random
56
import state
67
import addresses
7-
import helper_random
88
import protocol
99
import connectionpool
1010
from network import dandelion_ins
@@ -43,7 +43,7 @@ def run(self):
4343
requested = 0
4444
# Choose downloading peers randomly
4545
connections = connectionpool.pool.establishedConnections()
46-
helper_random.randomshuffle(connections)
46+
random.shuffle(connections)
4747
requestChunk = max(int(
4848
min(self.maxRequestChunk, len(missingObjects))
4949
/ len(connections)), 1) if connections else 1

src/network/tcp.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
# magic imports!
1313
import addresses
14-
import helper_random
1514
import l10n
1615
import protocol
1716
import state
@@ -201,7 +200,7 @@ def sendAddr(self):
201200
elemCount = min(
202201
len(filtered),
203202
maxAddrCount / 2 if n else maxAddrCount)
204-
addrs[s] = helper_random.randomsample(filtered, elemCount)
203+
addrs[s] = random.sample(filtered, elemCount)
205204
for substream in addrs:
206205
for peer, params in addrs[substream]:
207206
templist.append((substream, peer, params["lastseen"]))

src/network/uploadthread.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"""
44
import time
55

6-
import helper_random
6+
import random
77
import protocol
88
import state
99
import connectionpool
@@ -24,7 +24,7 @@ def run(self):
2424
uploaded = 0
2525
# Choose uploading peers randomly
2626
connections = connectionpool.pool.establishedConnections()
27-
helper_random.randomshuffle(connections)
27+
random.shuffle(connections)
2828
for i in connections:
2929
now = time.time()
3030
# avoid unnecessary delay

0 commit comments

Comments
 (0)