From 442fe81e3a71ec8cb10fca6a2bad56a622658475 Mon Sep 17 00:00:00 2001 From: Mahendra Date: Fri, 6 Apr 2018 17:50:55 +0530 Subject: [PATCH 1/4] helper_generic.py formating --- src/helper_generic.py | 39 +++++++++++++++++++++++++-------------- 1 file changed, 25 insertions(+), 14 deletions(-) diff --git a/src/helper_generic.py b/src/helper_generic.py index 588ae8f1..300af354 100644 --- a/src/helper_generic.py +++ b/src/helper_generic.py @@ -1,9 +1,8 @@ -import os import socket import sys from binascii import hexlify, unhexlify from multiprocessing import current_process -from threading import current_thread, enumerate +import threading import traceback import shared @@ -11,16 +10,18 @@ from debug import logger import queues import shutdown + def powQueueSize(): curWorkerQueue = queues.workerQueue.qsize() - for thread in enumerate(): + for thread in threading.enumerate(): try: if thread.name == "singleWorker": curWorkerQueue += thread.busy - except: - pass + except Exception: + print "" return curWorkerQueue + def convertIntToString(n): a = __builtins__.hex(n) if a[-1:] == 'L': @@ -30,28 +31,37 @@ def convertIntToString(n): else: return unhexlify('0' + a[2:]) + def convertStringToInt(s): return int(hexlify(s), 16) + def allThreadTraceback(frame): - id2name = dict([(th.ident, th.name) for th in enumerate()]) + id2name = dict([(th.ident, th.name) for th in threading.enumerate()]) code = [] for threadId, stack in sys._current_frames().items(): - code.append("\n# Thread: %s(%d)" % (id2name.get(threadId,""), threadId)) + code.append("\n# Thread: %s(%d)" % ( + id2name.get(threadId, ""), threadId)) for filename, lineno, name, line in traceback.extract_stack(stack): - code.append('File: "%s", line %d, in %s' % (filename, lineno, name)) + code.append('File: "%s", line %d, in %s' % ( + filename, lineno, name)) if line: code.append(" %s" % (line.strip())) print "\n".join(code) + def signal_handler(signal, frame): - logger.error("Got signal %i in %s/%s", signal, current_process().name, current_thread().name) + logger.error("Got signal %i in %s/%s", signal, + current_process().name, + threading.current_thread().name) if current_process().name == "RegExParser": - # on Windows this isn't triggered, but it's fine, it has its own process termination thing + # on Windows this isn't triggered, but it's fine, + # it has its own process termination thing raise SystemExit if "PoolWorker" in current_process().name: raise SystemExit - if current_thread().name not in ("PyBitmessage", "MainThread"): + if threading.current_thread().name not in ( + "PyBitmessage", "MainThread"): return logger.error("Got signal %i", signal) if shared.thisapp.daemon: @@ -60,8 +70,9 @@ def signal_handler(signal, frame): allThreadTraceback(frame) print 'Unfortunately you cannot use Ctrl+C when running the UI because the UI captures the signal.' + def isHostInPrivateIPRange(host): - if ":" in host: #IPv6 + if ":" in host: # IPv6 hostAddr = socket.inet_pton(socket.AF_INET6, host) if hostAddr == ('\x00' * 15) + '\x01': return False @@ -69,7 +80,6 @@ def isHostInPrivateIPRange(host): return False if (ord(hostAddr[0]) & 0xfe) == 0xfc: return False - pass elif ".onion" not in host: if host[:3] == '10.': return True @@ -84,5 +94,6 @@ def isHostInPrivateIPRange(host): return True return False -def addDataPadding(data, desiredMsgLength = 12, paddingChar = '\x00'): + +def addDataPadding(data, desiredMsgLength=12, paddingChar='\x00'): return data + paddingChar * (desiredMsgLength - len(data)) From c0036d8c183c616e16537d0c9e59de2a2e1b8885 Mon Sep 17 00:00:00 2001 From: Mahendra Date: Fri, 6 Apr 2018 18:33:30 +0530 Subject: [PATCH 2/4] Add docstring in helper_generic.py --- src/helper_generic.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/helper_generic.py b/src/helper_generic.py index 300af354..7b99bbdc 100644 --- a/src/helper_generic.py +++ b/src/helper_generic.py @@ -1,3 +1,6 @@ +"""This module is to perform generic oprations for threading.""" +"""and some conversion operations.""" + import socket import sys from binascii import hexlify, unhexlify From 7052c734bd64831006fd7767671d8227a2b16e05 Mon Sep 17 00:00:00 2001 From: Mahendra Date: Fri, 6 Apr 2018 19:00:32 +0530 Subject: [PATCH 3/4] Add log for thread error on helper_generic file - Remove if condition for RegExParser --- src/helper_generic.py | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/src/helper_generic.py b/src/helper_generic.py index 7b99bbdc..adbbc66c 100644 --- a/src/helper_generic.py +++ b/src/helper_generic.py @@ -1,5 +1,5 @@ """This module is to perform generic oprations for threading.""" -"""and some conversion operations.""" +"""And some conversion operations.""" import socket import sys @@ -12,6 +12,7 @@ import shared from debug import logger import queues import shutdown +from debug import logger def powQueueSize(): @@ -20,8 +21,8 @@ def powQueueSize(): try: if thread.name == "singleWorker": curWorkerQueue += thread.busy - except Exception: - print "" + except Exception as err: + logger.info("Thread error %s", err) return curWorkerQueue @@ -57,10 +58,6 @@ def signal_handler(signal, frame): logger.error("Got signal %i in %s/%s", signal, current_process().name, threading.current_thread().name) - if current_process().name == "RegExParser": - # on Windows this isn't triggered, but it's fine, - # it has its own process termination thing - raise SystemExit if "PoolWorker" in current_process().name: raise SystemExit if threading.current_thread().name not in ( From 5a2bac7e8c14d7a6b892190e46d7bd5e850b09bb Mon Sep 17 00:00:00 2001 From: Mahendra Date: Fri, 6 Apr 2018 19:09:56 +0530 Subject: [PATCH 4/4] Remove codacy error for docstring and reimport logger in helper_generic file --- src/helper_generic.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/helper_generic.py b/src/helper_generic.py index adbbc66c..699df8b8 100644 --- a/src/helper_generic.py +++ b/src/helper_generic.py @@ -1,5 +1,8 @@ -"""This module is to perform generic oprations for threading.""" -"""And some conversion operations.""" +""" +Helper Generic perform generic oprations for threading. + +Also perform some conversion operations. +""" import socket import sys @@ -12,7 +15,6 @@ import shared from debug import logger import queues import shutdown -from debug import logger def powQueueSize():