From d747394234817b283ed767001b7c4951f21497c9 Mon Sep 17 00:00:00 2001 From: Chuck Date: Sat, 6 Jul 2013 18:45:51 +0700 Subject: [PATCH] changing syntax for print lock --- src/bitmessageqt/settings.py | 2 +- src/class_asyncoreThread.py | 5 +-- src/class_pop3Server.py | 45 ++++++++++------------ src/class_smtpServer.py | 73 ++++++++++++++++-------------------- 4 files changed, 56 insertions(+), 69 deletions(-) diff --git a/src/bitmessageqt/settings.py b/src/bitmessageqt/settings.py index 7eb5e663..f66e3037 100644 --- a/src/bitmessageqt/settings.py +++ b/src/bitmessageqt/settings.py @@ -2,7 +2,7 @@ # Form implementation generated from reading ui file 'settings.ui' # -# Created: Sat Jul 06 18:17:40 2013 +# Created: Sat Jul 06 18:24:20 2013 # by: PyQt4 UI code generator 4.10.1 # # WARNING! All changes made in this file will be lost! diff --git a/src/class_asyncoreThread.py b/src/class_asyncoreThread.py index 75225d1c..d6679fa9 100644 --- a/src/class_asyncoreThread.py +++ b/src/class_asyncoreThread.py @@ -9,9 +9,8 @@ class asyncoreThread(threading.Thread): threading.Thread.__init__(self) def run(self): - shared.printLock.acquire() - print "Asyncore thread started" - shared.printLock.release() + with shared.printLock: + print "Asyncore thread started" while True: asyncore.loop(timeout=1) # Despite the horrible parameter name, this function will not timeout until all channels are closed. diff --git a/src/class_pop3Server.py b/src/class_pop3Server.py index 3e03a9c5..a13df705 100644 --- a/src/class_pop3Server.py +++ b/src/class_pop3Server.py @@ -104,11 +104,10 @@ class bitmessagePOP3Connection(asyncore.dispatcher): def sendline(self, data, END=END): if self.debug: - shared.printLock.acquire() - sys.stdout.write("sending ") - sys.stdout.write(data) - sys.stdout.write("\n") - shared.printLock.release() + with shared.printLock: + sys.stdout.write("sending ") + sys.stdout.write(data) + sys.stdout.write("\n") data = data + END while len(data) > 4096: self.send(data[:4096]) @@ -130,11 +129,10 @@ class bitmessagePOP3Connection(asyncore.dispatcher): self.data_buffer.append(chunk) if self.debug: - shared.printLock.acquire() - print('data_buffer', self.data_buffer) - print('commands', self.commands) - print('-') - shared.printLock.release() + with shared.printLock: + print('data_buffer', self.data_buffer) + print('commands', self.commands) + print('-') while len(self.commands): line = self.commands.popleft() @@ -170,15 +168,14 @@ class bitmessagePOP3Connection(asyncore.dispatcher): status, addressVersionNumber, streamNumber, ripe = decodeAddress(self.address) if status != 'success': - shared.printLock.acquire() - print 'Error: Could not decode address: ' + self.address + ' : ' + status - if status == 'checksumfailed': - print 'Error: Checksum failed for address: ' + self.address - if status == 'invalidcharacters': - print 'Error: Invalid characters in address: ' + self.address - if status == 'versiontoohigh': - print 'Error: Address version number too high (or zero) in address: ' + self.address - shared.printLock.release() + with shared.printLock: + print 'Error: Could not decode address: ' + self.address + ' : ' + status + if status == 'checksumfailed': + print 'Error: Checksum failed for address: ' + self.address + if status == 'invalidcharacters': + print 'Error: Invalid characters in address: ' + self.address + if status == 'versiontoohigh': + print 'Error: Address version number too high (or zero) in address: ' + self.address raise Exception("Invalid Bitmessage address: {}".format(self.address)) username = '{}@{}'.format(getBase58Capitaliation(self.address), self.address) @@ -237,9 +234,8 @@ class bitmessagePOP3Connection(asyncore.dispatcher): msg = self.messages[index] content = self.getMessageContent(msg['msgid']) if self.debug: - shared.printLock.acquire() - sys.stdout.write(str(msg) + ": " + str(content)) - shared.printLock.release() + with shared.printLock: + sys.stdout.write(str(msg) + ": " + str(content)) yield "+OK {} octets".format(msg['size']) yield content['message'] yield '.' @@ -281,9 +277,8 @@ class bitmessagePOP3Server(asyncore.dispatcher): self.bind((bindAddress, pop3port)) self.listen(10) - shared.printLock.acquire() - print "POP3 server started: SSL enabled={}".format(str(self.ssl)) - shared.printLock.release() + with shared.printLock: + print "POP3 server started: SSL enabled={}".format(str(self.ssl)) def handle_accept(self): sock, peer_address = self.accept() diff --git a/src/class_smtpServer.py b/src/class_smtpServer.py index 4e4fb43e..15b2466f 100644 --- a/src/class_smtpServer.py +++ b/src/class_smtpServer.py @@ -179,16 +179,15 @@ class bitmessageSMTPChannel(asynchat.async_chat): status, addressVersionNumber, streamNumber, ripe = decodeAddress(self.address) if status != 'success': - shared.printLock.acquire() - print 'Error: Could not decode address: ' + self.address + ' : ' + status - if status == 'checksumfailed': - print 'Error: Checksum failed for address: ' + self.address - if status == 'invalidcharacters': - print 'Error: Invalid characters in address: ' + self.address - if status == 'versiontoohigh': - print 'Error: Address version number too high (or zero) in address: ' + self.address - shared.printLock.release() - raise Exception("Invalid Bitmessage address: {}".format(self.address)) + with shared.printLock: + print 'Error: Could not decode address: ' + self.address + ' : ' + status + if status == 'checksumfailed': + print 'Error: Checksum failed for address: ' + self.address + if status == 'invalidcharacters': + print 'Error: Invalid characters in address: ' + self.address + if status == 'versiontoohigh': + print 'Error: Address version number too high (or zero) in address: ' + self.address + raise Exception("Invalid Bitmessage address: {}".format(self.address)) self.fullUsername = '{}@{}'.format(getBase58Capitaliation(self.address), address) @@ -324,9 +323,8 @@ class bitmessageSMTPServer(smtpd.SMTPServer): bindAddress = '127.0.0.1' smtpd.SMTPServer.__init__(self, (bindAddress, smtpport), None) - shared.printLock.acquire() - print "SMTP server started: SSL enabled={}".format(str(self.ssl)) - shared.printLock.release() + with shared.printLock: + print "SMTP server started: SSL enabled={}".format(str(self.ssl)) def handle_accept(self): # Override SMTPServer's handle_accept so that we can start an SSL connection. @@ -363,29 +361,26 @@ class bitmessageSMTPServer(smtpd.SMTPServer): else: status, addressVersionNumber, streamNumber, fromRipe = decodeAddress(fromAddress) if status != 'success': - shared.printLock.acquire() - print 'Error: Could not decode address: ' + fromAddress + ' : ' + status - if status == 'checksumfailed': - print 'Error: Checksum failed for address: ' + fromAddress - if status == 'invalidcharacters': - print 'Error: Invalid characters in address: ' + fromAddress - if status == 'versiontoohigh': - print 'Error: Address version number too high (or zero) in address: ' + fromAddress - shared.printLock.release() + with shared.printLock: + print 'Error: Could not decode address: ' + fromAddress + ' : ' + status + if status == 'checksumfailed': + print 'Error: Checksum failed for address: ' + fromAddress + if status == 'invalidcharacters': + print 'Error: Invalid characters in address: ' + fromAddress + if status == 'versiontoohigh': + print 'Error: Address version number too high (or zero) in address: ' + fromAddress raise Exception("Invalid Bitmessage address: {}".format(fromAddress)) #fromAddress = addBMIfNotPresent(fromAddress) # I know there's a BM-, because it's required when using SMTP try: fromAddressEnabled = shared.config.getboolean(fromAddress, 'enabled') except: - shared.printLock.acquire() - print 'Error: Could not find your fromAddress in the keys.dat file.' - shared.printLock.release() + with shared.printLock: + print 'Error: Could not find your fromAddress in the keys.dat file.' raise Exception("Could not find address in keys.dat: {}".format(fromAddress)) if not fromAddressEnabled: - shared.printLock.acquire() - print 'Error: Your fromAddress is disabled. Cannot send.' - shared.printLock.release() + with shared.printLock: + print 'Error: Your fromAddress is disabled. Cannot send.' raise Exception("The fromAddress is disabled: {}".format(fromAddress)) for recipient in rcpttos: @@ -402,15 +397,14 @@ class bitmessageSMTPServer(smtpd.SMTPServer): # into a utility func! status, addressVersionNumber, streamNumber, toRipe = decodeAddress(toAddress) if status != 'success': - shared.printLock.acquire() - print 'Error: Could not decode address: ' + toAddress + ' : ' + status - if status == 'checksumfailed': - print 'Error: Checksum failed for address: ' + toAddress - if status == 'invalidcharacters': - print 'Error: Invalid characters in address: ' + toAddress - if status == 'versiontoohigh': - print 'Error: Address version number too high (or zero) in address: ' + toAddress - shared.printLock.release() + with shared.printLock: + print 'Error: Could not decode address: ' + toAddress + ' : ' + status + if status == 'checksumfailed': + print 'Error: Checksum failed for address: ' + toAddress + if status == 'invalidcharacters': + print 'Error: Invalid characters in address: ' + toAddress + if status == 'versiontoohigh': + print 'Error: Address version number too high (or zero) in address: ' + toAddress raise Exception("Invalid Bitmessage address: {}".format(toAddress)) toAddressIsOK = False @@ -423,9 +417,8 @@ class bitmessageSMTPServer(smtpd.SMTPServer): # The toAddress is one owned by me. We cannot send # messages to ourselves without significant changes # to the codebase. - shared.printLock.acquire() - print "Error: One of the addresses to which you are sending a message, {}, is yours. Unfortunately the Bitmessage client cannot process its own messages. Please try running a second client on a different computer or within a VM.".format(toAddress) - shared.printLock.release() + with shared.printLock: + print "Error: One of the addresses to which you are sending a message, {}, is yours. Unfortunately the Bitmessage client cannot process its own messages. Please try running a second client on a different computer or within a VM.".format(toAddress) raise Exception("An address that you are sending a message to, {}, is yours. Unfortunately the Bitmessage client cannot process its own messages. Please try running a second client on a different computer or within a VM.".format(toAddress)) # The subject is specially formatted to identify it from non-E-mail messages.