Merge pull request #357 from NIC619/minor_fix_replace_del

Replace (check and) del pattern with pop method
This commit is contained in:
NIC Lin 2019-11-26 19:39:41 +08:00 committed by GitHub
commit cac7e3909a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 12 additions and 24 deletions

View File

@ -248,7 +248,7 @@ class Swarm(INetwork):
# TODO: Should be changed to close multisple connections, # TODO: Should be changed to close multisple connections,
# if we have several connections per peer in the future. # if we have several connections per peer in the future.
connection = self.connections[peer_id] connection = self.connections[peer_id]
# NOTE: `connection.close` will perform `del self.connections[peer_id]` # NOTE: `connection.close` will delete `peer_id` from `self.connections`
# and `notify_disconnected` for us. # and `notify_disconnected` for us.
await connection.close() await connection.close()

View File

@ -144,8 +144,7 @@ class GossipSub(IPubsubRouter):
elif peer_id in self.peers_floodsub: elif peer_id in self.peers_floodsub:
self.peers_floodsub.remove(peer_id) self.peers_floodsub.remove(peer_id)
if peer_id in self.peers_to_protocol: self.peers_to_protocol.pop(peer_id, None)
del self.peers_to_protocol[peer_id]
async def handle_rpc(self, rpc: rpc_pb2.RPC, sender_peer_id: ID) -> None: async def handle_rpc(self, rpc: rpc_pb2.RPC, sender_peer_id: ID) -> None:
""" """
@ -274,8 +273,7 @@ class GossipSub(IPubsubRouter):
self.mesh[topic].append(peer) self.mesh[topic].append(peer)
await self.emit_graft(topic, peer) await self.emit_graft(topic, peer)
if topic_in_fanout: self.fanout.pop(topic, None)
del self.fanout[topic]
async def leave(self, topic: str) -> None: async def leave(self, topic: str) -> None:
# Note: the comments here are the near-exact algorithm description from the spec # Note: the comments here are the near-exact algorithm description from the spec

View File

@ -96,8 +96,7 @@ class MessageCache:
last_entries: List[CacheEntry] = self.history[len(self.history) - 1] last_entries: List[CacheEntry] = self.history[len(self.history) - 1]
for entry in last_entries: for entry in last_entries:
if entry.mid in self.msgs: self.msgs.pop(entry.mid)
del self.msgs[entry.mid]
i: int = len(self.history) - 2 i: int = len(self.history) - 2

View File

@ -232,8 +232,7 @@ class Pubsub:
:param topic: the topic to remove validator from :param topic: the topic to remove validator from
""" """
if topic in self.topic_validators: self.topic_validators.pop(topic, None)
del self.topic_validators[topic]
def get_msg_validators(self, msg: rpc_pb2.Message) -> Tuple[TopicValidator, ...]: def get_msg_validators(self, msg: rpc_pb2.Message) -> Tuple[TopicValidator, ...]:
""" """

View File

@ -50,8 +50,7 @@ class SecurityMultistream(ABC):
:param transport: the corresponding transportation to the ``protocol``. :param transport: the corresponding transportation to the ``protocol``.
""" """
# If protocol is already added before, remove it and add it again. # If protocol is already added before, remove it and add it again.
if protocol in self.transports: self.transports.pop(protocol, None)
del self.transports[protocol]
self.transports[protocol] = transport self.transports[protocol] = transport
# Note: None is added as the handler for the given protocol since # Note: None is added as the handler for the given protocol since
# we only care about selecting the protocol, not any handler function # we only care about selecting the protocol, not any handler function

View File

@ -297,8 +297,7 @@ class Mplex(IMuxedConn):
# the entry of this stream, to avoid others from accessing it. # the entry of this stream, to avoid others from accessing it.
if is_local_closed: if is_local_closed:
async with self.streams_lock: async with self.streams_lock:
if stream_id in self.streams: self.streams.pop(stream_id, None)
del self.streams[stream_id]
async def _handle_reset(self, stream_id: StreamID) -> None: async def _handle_reset(self, stream_id: StreamID) -> None:
async with self.streams_lock: async with self.streams_lock:
@ -316,8 +315,7 @@ class Mplex(IMuxedConn):
if not stream.event_local_closed.is_set(): if not stream.event_local_closed.is_set():
stream.event_local_closed.set() stream.event_local_closed.set()
async with self.streams_lock: async with self.streams_lock:
if stream_id in self.streams: self.streams.pop(stream_id, None)
del self.streams[stream_id]
async def _cleanup(self) -> None: async def _cleanup(self) -> None:
if not self.event_shutting_down.is_set(): if not self.event_shutting_down.is_set():

View File

@ -180,8 +180,7 @@ class MplexStream(IMuxedStream):
if _is_remote_closed: if _is_remote_closed:
# Both sides are closed, we can safely remove the buffer from the dict. # Both sides are closed, we can safely remove the buffer from the dict.
async with self.muxed_conn.streams_lock: async with self.muxed_conn.streams_lock:
if self.stream_id in self.muxed_conn.streams: self.muxed_conn.streams.pop(self.stream_id, None)
del self.muxed_conn.streams[self.stream_id]
async def reset(self) -> None: async def reset(self) -> None:
"""closes both ends of the stream tells this remote side to hang up.""" """closes both ends of the stream tells this remote side to hang up."""
@ -208,11 +207,8 @@ class MplexStream(IMuxedStream):
self.event_remote_closed.set() self.event_remote_closed.set()
async with self.muxed_conn.streams_lock: async with self.muxed_conn.streams_lock:
if ( if self.muxed_conn.streams is not None:
self.muxed_conn.streams is not None self.muxed_conn.streams.pop(self.stream_id, None)
and self.stream_id in self.muxed_conn.streams
):
del self.muxed_conn.streams[self.stream_id]
# TODO deadline not in use # TODO deadline not in use
def set_deadline(self, ttl: int) -> bool: def set_deadline(self, ttl: int) -> bool:

View File

@ -44,8 +44,7 @@ class MuxerMultistream:
:param transport: the corresponding transportation to the ``protocol``. :param transport: the corresponding transportation to the ``protocol``.
""" """
# If protocol is already added before, remove it and add it again. # If protocol is already added before, remove it and add it again.
if protocol in self.transports: self.transports.pop(protocol, None)
del self.transports[protocol]
self.transports[protocol] = transport self.transports[protocol] = transport
self.multiselect.add_handler(protocol, None) self.multiselect.add_handler(protocol, None)