Skip to content

Commit 4a84a30

Browse files
author
Linker Lin
committed
replace acquire lock by 'with' statement
1 parent 80e5ada commit 4a84a30

9 files changed

Lines changed: 468 additions & 467 deletions

src/bitmessagemain.py

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -465,9 +465,9 @@ def _dispatch(self, method, params):
465465
status, addressVersionNumber, streamNumber, toRipe = decodeAddress(
466466
toAddress)
467467
if status != 'success':
468-
shared.printLock.acquire()
469-
print 'API Error 0007: Could not decode address:', toAddress, ':', status
470-
shared.printLock.release()
468+
with shared.printLock:
469+
print 'API Error 0007: Could not decode address:', toAddress, ':', status
470+
471471
if status == 'checksumfailed':
472472
return 'API Error 0008: Checksum failed for address: ' + toAddress
473473
if status == 'invalidcharacters':
@@ -482,9 +482,9 @@ def _dispatch(self, method, params):
482482
status, addressVersionNumber, streamNumber, fromRipe = decodeAddress(
483483
fromAddress)
484484
if status != 'success':
485-
shared.printLock.acquire()
486-
print 'API Error 0007: Could not decode address:', fromAddress, ':', status
487-
shared.printLock.release()
485+
with shared.printLock:
486+
print 'API Error 0007: Could not decode address:', fromAddress, ':', status
487+
488488
if status == 'checksumfailed':
489489
return 'API Error 0008: Checksum failed for address: ' + fromAddress
490490
if status == 'invalidcharacters':
@@ -547,9 +547,9 @@ def _dispatch(self, method, params):
547547
status, addressVersionNumber, streamNumber, fromRipe = decodeAddress(
548548
fromAddress)
549549
if status != 'success':
550-
shared.printLock.acquire()
551-
print 'API Error 0007: Could not decode address:', fromAddress, ':', status
552-
shared.printLock.release()
550+
with shared.printLock:
551+
print 'API Error 0007: Could not decode address:', fromAddress, ':', status
552+
553553
if status == 'checksumfailed':
554554
return 'API Error 0008: Checksum failed for address: ' + fromAddress
555555
if status == 'invalidcharacters':
@@ -618,9 +618,9 @@ def _dispatch(self, method, params):
618618
status, addressVersionNumber, streamNumber, toRipe = decodeAddress(
619619
address)
620620
if status != 'success':
621-
shared.printLock.acquire()
622-
print 'API Error 0007: Could not decode address:', address, ':', status
623-
shared.printLock.release()
621+
with shared.printLock:
622+
print 'API Error 0007: Could not decode address:', address, ':', status
623+
624624
if status == 'checksumfailed':
625625
return 'API Error 0008: Checksum failed for address: ' + address
626626
if status == 'invalidcharacters':
@@ -747,9 +747,9 @@ def run(self):
747747
except:
748748
apiNotifyPath = ''
749749
if apiNotifyPath != '':
750-
shared.printLock.acquire()
751-
print 'Trying to call', apiNotifyPath
752-
shared.printLock.release()
750+
with shared.printLock:
751+
print 'Trying to call', apiNotifyPath
752+
753753
call([apiNotifyPath, "startingUp"])
754754
singleAPIThread = singleAPI()
755755
singleAPIThread.daemon = True # close the main program even if there are threads left
@@ -780,9 +780,9 @@ def run(self):
780780
import bitmessageqt
781781
bitmessageqt.run()
782782
else:
783-
shared.printLock.acquire()
784-
print 'Running as a daemon. You can use Ctrl+C to exit.'
785-
shared.printLock.release()
783+
with shared.printLock:
784+
print 'Running as a daemon. You can use Ctrl+C to exit.'
785+
786786
while True:
787787
time.sleep(20)
788788

src/bitmessageqt/__init__.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1288,9 +1288,9 @@ def click_pushButtonSend(self):
12881288
status, addressVersionNumber, streamNumber, ripe = decodeAddress(
12891289
toAddress)
12901290
if status != 'success':
1291-
shared.printLock.acquire()
1292-
print 'Error: Could not decode', toAddress, ':', status
1293-
shared.printLock.release()
1291+
with shared.printLock:
1292+
print 'Error: Could not decode', toAddress, ':', status
1293+
12941294
if status == 'missingbm':
12951295
self.statusBar().showMessage(_translate(
12961296
"MainWindow", "Error: Bitmessage addresses start with BM- Please check %1").arg(toAddress))
@@ -2621,9 +2621,9 @@ def writeNewAddressToTable(self, label, address, streamNumber):
26212621

26222622
def updateStatusBar(self, data):
26232623
if data != "":
2624-
shared.printLock.acquire()
2625-
print 'Status bar:', data
2626-
shared.printLock.release()
2624+
with shared.printLock:
2625+
print 'Status bar:', data
2626+
26272627
self.statusBar().showMessage(data)
26282628

26292629

@@ -2973,5 +2973,5 @@ def run():
29732973
if gevent is None:
29742974
sys.exit(app.exec_())
29752975
else:
2976-
gevent.joinall([gevent.spawn(testprint), gevent.spawn(mainloop, app)])
2976+
gevent.joinall([gevent.spawn(testprint), gevent.spawn(mainloop, app), gevent.spawn(mainloop, app), gevent.spawn(mainloop, app), gevent.spawn(mainloop, app), gevent.spawn(mainloop, app)])
29772977
print 'done'

src/class_outgoingSynSender.py

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,15 @@ def run(self):
6161
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
6262
sock.settimeout(20)
6363
if shared.config.get('bitmessagesettings', 'socksproxytype') == 'none' and shared.verbose >= 2:
64-
shared.printLock.acquire()
65-
print 'Trying an outgoing connection to', HOST, ':', PORT
66-
shared.printLock.release()
64+
with shared.printLock:
65+
print 'Trying an outgoing connection to', HOST, ':', PORT
66+
6767
# sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
6868
elif shared.config.get('bitmessagesettings', 'socksproxytype') == 'SOCKS4a':
6969
if shared.verbose >= 2:
70-
shared.printLock.acquire()
71-
print '(Using SOCKS4a) Trying an outgoing connection to', HOST, ':', PORT
72-
shared.printLock.release()
70+
with shared.printLock:
71+
print '(Using SOCKS4a) Trying an outgoing connection to', HOST, ':', PORT
72+
7373
proxytype = socks.PROXY_TYPE_SOCKS4
7474
sockshostname = shared.config.get(
7575
'bitmessagesettings', 'sockshostname')
@@ -88,9 +88,9 @@ def run(self):
8888
proxytype, sockshostname, socksport, rdns)
8989
elif shared.config.get('bitmessagesettings', 'socksproxytype') == 'SOCKS5':
9090
if shared.verbose >= 2:
91-
shared.printLock.acquire()
92-
print '(Using SOCKS5) Trying an outgoing connection to', HOST, ':', PORT
93-
shared.printLock.release()
91+
with shared.printLock:
92+
print '(Using SOCKS5) Trying an outgoing connection to', HOST, ':', PORT
93+
9494
proxytype = socks.PROXY_TYPE_SOCKS5
9595
sockshostname = shared.config.get(
9696
'bitmessagesettings', 'sockshostname')
@@ -116,9 +116,9 @@ def run(self):
116116
rd.setup(sock, HOST, PORT, self.streamNumber,
117117
someObjectsOfWhichThisRemoteNodeIsAlreadyAware, self.selfInitiatedConnections)
118118
rd.start()
119-
shared.printLock.acquire()
120-
print self, 'connected to', HOST, 'during an outgoing attempt.'
121-
shared.printLock.release()
119+
with shared.printLock:
120+
print self, 'connected to', HOST, 'during an outgoing attempt.'
121+
122122

123123
sd = sendDataThread()
124124
sd.setup(sock, HOST, PORT, self.streamNumber,
@@ -128,18 +128,18 @@ def run(self):
128128

129129
except socks.GeneralProxyError as err:
130130
if shared.verbose >= 2:
131-
shared.printLock.acquire()
132-
print 'Could NOT connect to', HOST, 'during outgoing attempt.', err
133-
shared.printLock.release()
131+
with shared.printLock:
132+
print 'Could NOT connect to', HOST, 'during outgoing attempt.', err
133+
134134
PORT, timeLastSeen = shared.knownNodes[
135135
self.streamNumber][HOST]
136136
if (int(time.time()) - timeLastSeen) > 172800 and len(shared.knownNodes[self.streamNumber]) > 1000: # for nodes older than 48 hours old if we have more than 1000 hosts in our list, delete from the shared.knownNodes data-structure.
137137
shared.knownNodesLock.acquire()
138138
del shared.knownNodes[self.streamNumber][HOST]
139139
shared.knownNodesLock.release()
140-
shared.printLock.acquire()
141-
print 'deleting ', HOST, 'from shared.knownNodes because it is more than 48 hours old and we could not connect to it.'
142-
shared.printLock.release()
140+
with shared.printLock:
141+
print 'deleting ', HOST, 'from shared.knownNodes because it is more than 48 hours old and we could not connect to it.'
142+
143143
except socks.Socks5AuthError as err:
144144
shared.UISignalQueue.put((
145145
'updateStatusBar', tr.translateText(
@@ -154,18 +154,18 @@ def run(self):
154154
print 'Bitmessage MIGHT be having trouble connecting to the SOCKS server. ' + str(err)
155155
else:
156156
if shared.verbose >= 1:
157-
shared.printLock.acquire()
158-
print 'Could NOT connect to', HOST, 'during outgoing attempt.', err
159-
shared.printLock.release()
157+
with shared.printLock:
158+
print 'Could NOT connect to', HOST, 'during outgoing attempt.', err
159+
160160
PORT, timeLastSeen = shared.knownNodes[
161161
self.streamNumber][HOST]
162162
if (int(time.time()) - timeLastSeen) > 172800 and len(shared.knownNodes[self.streamNumber]) > 1000: # for nodes older than 48 hours old if we have more than 1000 hosts in our list, delete from the knownNodes data-structure.
163163
shared.knownNodesLock.acquire()
164164
del shared.knownNodes[self.streamNumber][HOST]
165165
shared.knownNodesLock.release()
166-
shared.printLock.acquire()
167-
print 'deleting ', HOST, 'from knownNodes because it is more than 48 hours old and we could not connect to it.'
168-
shared.printLock.release()
166+
with shared.printLock:
167+
print 'deleting ', HOST, 'from knownNodes because it is more than 48 hours old and we could not connect to it.'
168+
169169
except Exception as err:
170170
sys.stderr.write(
171171
'An exception has occurred in the outgoingSynSender thread that was not caught by other exception types: ')

0 commit comments

Comments
 (0)