From dd64a7b507351a37aa7f89e1c1bcb16ae283e4a0 Mon Sep 17 00:00:00 2001 From: anand k Date: Tue, 27 Feb 2024 16:30:06 +0530 Subject: [PATCH] Code Quality --- src/depends.py | 2 +- src/helper_startup.py | 4 +++- src/network/bmproto.py | 2 +- src/plugins/sound_playfile.py | 4 ++-- src/proofofwork.py | 9 +++++---- src/shared.py | 2 +- src/singleinstance.py | 4 ++-- src/upnp.py | 8 ++++---- 8 files changed, 19 insertions(+), 16 deletions(-) diff --git a/src/depends.py b/src/depends.py index 8bfbd313..b386f468 100755 --- a/src/depends.py +++ b/src/depends.py @@ -361,7 +361,7 @@ def check_curses(): return False try: - subprocess.check_call(['which', 'dialog']) + subprocess.check_call(['which', 'dialog']) # nosec:B603, B607 except subprocess.CalledProcessError: logger.error( 'Curses requires the `dialog` command to be installed as well as' diff --git a/src/helper_startup.py b/src/helper_startup.py index bd66b1dc..d9002440 100644 --- a/src/helper_startup.py +++ b/src/helper_startup.py @@ -12,6 +12,7 @@ import sys import time from distutils.version import StrictVersion from struct import pack +from six.moves import configparser try: import defaults @@ -218,7 +219,8 @@ def updateConfig(): config.set( addressInKeysFile, 'payloadlengthextrabytes', str(int(previousSmallMessageDifficulty * 1000))) - except Exception: + except (ValueError, TypeError, configparser.NoSectionError, + configparser.NoSectionError): continue config.set('bitmessagesettings', 'maxdownloadrate', '0') config.set('bitmessagesettings', 'maxuploadrate', '0') diff --git a/src/network/bmproto.py b/src/network/bmproto.py index e23dfe8d..99e66965 100644 --- a/src/network/bmproto.py +++ b/src/network/bmproto.py @@ -610,7 +610,7 @@ class BMProto(AdvancedDispatcher, ObjectTracker): 'Closed connection to %s because we are already' ' connected to that IP.', self.destination) return False - except Exception: # TODO: exception types + except Exception: # nosec:B110 pylint:disable=broad-exception-caught pass if not self.isOutbound: # incoming from a peer we're connected to as outbound, diff --git a/src/plugins/sound_playfile.py b/src/plugins/sound_playfile.py index e36d9922..7962c3d3 100644 --- a/src/plugins/sound_playfile.py +++ b/src/plugins/sound_playfile.py @@ -11,14 +11,14 @@ try: winsound.PlaySound(sound_file, winsound.SND_FILENAME) except ImportError: import os - import subprocess + import subprocess # nosec:B404 play_cmd = {} def _subprocess(*args): FNULL = open(os.devnull, 'wb') subprocess.call( - args, stdout=FNULL, stderr=subprocess.STDOUT, close_fds=True) + args, stdout=FNULL, stderr=subprocess.STDOUT, close_fds=True) # nosec:B603 def connect_plugin(sound_file): """This function implements the entry point.""" diff --git a/src/proofofwork.py b/src/proofofwork.py index 73a15e0a..110baf22 100644 --- a/src/proofofwork.py +++ b/src/proofofwork.py @@ -10,7 +10,7 @@ import sys import tempfile import time from struct import pack, unpack -from subprocess import call +from subprocess import call # nosec:B404 import openclpow import paths @@ -135,7 +135,7 @@ def _doFastPoW(target, initialHash): try: pool.terminate() pool.join() - except: # noqa:E722 + except: # nosec:B110 noqa:E722 pylint:disable=bare-except pass raise StopIteration("Interrupted") for i in range(pool_size): @@ -272,10 +272,11 @@ def buildCPoW(): try: if "bsd" in sys.platform: # BSD make - call(["make", "-C", os.path.join(paths.codePath(), "bitmsghash"), '-f', 'Makefile.bsd']) + call(["make", "-C", os.path.join(paths.codePath(), "bitmsghash"), + '-f', 'Makefile.bsd']) # nosec:B607, B603 else: # GNU make - call(["make", "-C", os.path.join(paths.codePath(), "bitmsghash")]) + call(["make", "-C", os.path.join(paths.codePath(), "bitmsghash")]) # nosec:B607, B603 if os.path.exists(os.path.join(paths.codePath(), "bitmsghash", "bitmsghash.so")): init() notifyBuild(True) diff --git a/src/shared.py b/src/shared.py index d9c1ca13..b1493a44 100644 --- a/src/shared.py +++ b/src/shared.py @@ -11,7 +11,7 @@ from __future__ import division import hashlib import os import stat -import subprocess +import subprocess # nosec:B404 import sys from binascii import hexlify diff --git a/src/singleinstance.py b/src/singleinstance.py index 660dcf54..cff9d794 100644 --- a/src/singleinstance.py +++ b/src/singleinstance.py @@ -93,7 +93,7 @@ class singleinstance(object): os.close(self.fd) else: fcntl.lockf(self.fp, fcntl.LOCK_UN) - except Exception: + except (IOError, OSError): pass return @@ -107,5 +107,5 @@ class singleinstance(object): fcntl.lockf(self.fp, fcntl.LOCK_UN) if os.path.isfile(self.lockfile): os.unlink(self.lockfile) - except Exception: + except (IOError, OSError): pass diff --git a/src/upnp.py b/src/upnp.py index dc1334e2..aed83d8f 100644 --- a/src/upnp.py +++ b/src/upnp.py @@ -1,4 +1,4 @@ -# pylint: disable=too-many-statements,too-many-branches,protected-access,no-self-use +# pylint: disable=too-many-statements,too-many-branches,protected-access,no-self-use """ Complete UPnP port forwarding implementation in separate thread. Reference: http://mattscodecave.com/posts/using-python-and-upnp-to-forward-a-port @@ -239,7 +239,7 @@ class uPnPThread(StoppableThread): if time.time() - lastSent > self.sendSleep and not self.routers: try: self.sendSearchRouter() - except: # noqa:E722 + except: # nosec:B110 noqa:E722 pylint:disable=bare-except pass lastSent = time.time() try: @@ -279,11 +279,11 @@ class uPnPThread(StoppableThread): self.createPortMapping(router) try: self.sock.shutdown(socket.SHUT_RDWR) - except: # noqa:E722 + except (IOError, OSError): # noqa:E722 pass try: self.sock.close() - except: # noqa:E722 + except (IOError, OSError): # noqa:E722 pass deleted = False for router in self.routers: