Skip to content

Commit

Permalink
Python 3 compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonathan Girven committed Jan 12, 2015
1 parent 6879e06 commit fce9e33
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 19 deletions.
26 changes: 13 additions & 13 deletions apns.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ def _connect(self):
_logger.debug("%s APNS connection establishing..." % self.__class__.__name__)

# Fallback for socket timeout.
for i in xrange(3):
for i in range(3):
try:
self._socket = socket(AF_INET, SOCK_STREAM)
self._socket.settimeout(self.timeout)
Expand All @@ -215,7 +215,7 @@ def _connect(self):
try:
self._ssl.do_handshake()
break
except ssl.SSLError, err:
except ssl.SSLError as err:
if ssl.SSL_ERROR_WANT_READ == err.args[0]:
select.select([self._ssl], [], [])
elif ssl.SSL_ERROR_WANT_WRITE == err.args[0]:
Expand All @@ -225,11 +225,11 @@ def _connect(self):

else:
# Fallback for 'SSLError: _ssl.c:489: The handshake operation timed out'
for i in xrange(3):
for i in range(3):
try:
self._ssl = wrap_socket(self._socket, self.key_file, self.cert_file)
break
except SSLError, ex:
except SSLError as ex:
if ex.args[0] == SSL_ERROR_WANT_READ:
sys.exc_clear()
elif ex.args[0] == SSL_ERROR_WANT_WRITE:
Expand Down Expand Up @@ -363,36 +363,36 @@ def get_frame(self):
def add_item(self, token_hex, payload, identifier, expiry, priority):
"""Add a notification message to the frame"""
item_len = 0
self.frame_data.extend('\2' + APNs.packed_uint_big_endian(item_len))
self.frame_data.extend(b'\2' + APNs.packed_uint_big_endian(item_len))

token_bin = a2b_hex(token_hex)
token_length_bin = APNs.packed_ushort_big_endian(len(token_bin))
token_item = '\1' + token_length_bin + token_bin
token_item = b'\1' + token_length_bin + token_bin
self.frame_data.extend(token_item)
item_len += len(token_item)

payload_json = payload.json()
payload_length_bin = APNs.packed_ushort_big_endian(len(payload_json))
payload_item = '\2' + payload_length_bin + payload_json
payload_item = b'\2' + payload_length_bin + payload_json
self.frame_data.extend(payload_item)
item_len += len(payload_item)

identifier_bin = APNs.packed_uint_big_endian(identifier)
identifier_length_bin = \
APNs.packed_ushort_big_endian(len(identifier_bin))
identifier_item = '\3' + identifier_length_bin + identifier_bin
identifier_item = b'\3' + identifier_length_bin + identifier_bin
self.frame_data.extend(identifier_item)
item_len += len(identifier_item)

expiry_bin = APNs.packed_uint_big_endian(expiry)
expiry_length_bin = APNs.packed_ushort_big_endian(len(expiry_bin))
expiry_item = '\4' + expiry_length_bin + expiry_bin
expiry_item = b'\4' + expiry_length_bin + expiry_bin
self.frame_data.extend(expiry_item)
item_len += len(expiry_item)

priority_bin = APNs.packed_uchar(priority)
priority_length_bin = APNs.packed_ushort_big_endian(len(priority_bin))
priority_item = '\5' + priority_length_bin + priority_bin
priority_item = b'\5' + priority_length_bin + priority_bin
self.frame_data.extend(priority_item)
item_len += len(priority_item)

Expand Down Expand Up @@ -432,7 +432,7 @@ def items(self):
A generator that yields (token_hex, fail_time) pairs retrieved from
the APNs feedback server
"""
buff = ''
buff = b''
for chunk in self._chunks():
buff += chunk

Expand Down Expand Up @@ -526,7 +526,7 @@ def send_notification(self, token_hex, payload, identifier=0, expiry=0):
message = self._get_enhanced_notification(token_hex, payload,
identifier, expiry)

for i in xrange(WRITE_RETRY):
for i in range(WRITE_RETRY):
try:
with self._send_lock:
self._make_sure_error_response_handler_worker_alive()
Expand All @@ -548,7 +548,7 @@ def _make_sure_error_response_handler_worker_alive(self):
or not self._error_response_handler_worker.is_alive()):
self._init_error_response_handler_worker()
TIMEOUT_SEC = 10
for _ in xrange(TIMEOUT_SEC):
for _ in range(TIMEOUT_SEC):
if self._error_response_handler_worker.is_alive():
_logger.debug("error response handler worker is running")
return
Expand Down
13 changes: 7 additions & 6 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@
NUM_MOCK_TOKENS = 10
mock_tokens = []
for i in range(0, NUM_MOCK_TOKENS):
mock_tokens.append(hashlib.sha256("%.12f" % random()).hexdigest())
mock_tokens.append(bytes(hashlib.sha256(("%.12f" % random()).encode("utf-8")).hexdigest().encode("utf-8")))

def mock_chunks_generator():
BUF_SIZE = 64
# Create fake data feed
data = ''
data = b''

for t in mock_tokens:
token_bin = a2b_hex(t)
Expand Down Expand Up @@ -88,7 +88,7 @@ def testGatewayServer(self):
)

self.assertEqual(len(notification), expected_length)
self.assertEqual(notification[0], '\0')
self.assertEqual(notification[0:1], b'\0')

def testFeedbackServer(self):
pem_file = TEST_CERTIFICATE
Expand All @@ -102,7 +102,7 @@ def testFeedbackServer(self):
feedback_server._chunks = mock_chunks_generator

i = 0;
for (token_hex, fail_time) in feedback_server.items():
for (token_hex, fail_time) in list(feedback_server.items()):
self.assertEqual(token_hex, mock_tokens[i])
i += 1
self.assertEqual(i, NUM_MOCK_TOKENS)
Expand Down Expand Up @@ -188,8 +188,9 @@ def testFrame(self):
frame = Frame()
frame.add_item(token_hex, payload, identifier, expiry, priority)

f = '\x02\x00\x00\x00t\x01\x00 \xb5\xbb\x9d\x80\x14\xa0\xf9\xb1\xd6\x1e!\xe7\x96\xd7\x8d\xcc\xdf\x13R\xf2<\xd3(\x12\xf4\x85\x0b\x87\x8a\xe4\x94L\x02\x00<{"aps":{"sound":"default","badge":4,"alert":"Hello World!"}}\x03\x00\x04\x00\x00\x00\x01\x04\x00\x04\x00\x00\x0e\x10\x05\x00\x01\n'
self.assertEqual(f, str(frame))
f1 = bytearray(b'\x02\x00\x00\x00t\x01\x00 \xb5\xbb\x9d\x80\x14\xa0\xf9\xb1\xd6\x1e!\xe7\x96\xd7\x8d\xcc\xdf\x13R\xf2<\xd3(\x12\xf4\x85\x0b\x87\x8a\xe4\x94L\x02\x00<{"aps":{"sound":"default","badge":4,"alert":"Hello World!"}}\x03\x00\x04\x00\x00\x00\x01\x04\x00\x04\x00\x00\x0e\x10\x05\x00\x01\n')
f2 = bytearray(b'\x02\x00\x00\x00t\x01\x00 \xb5\xbb\x9d\x80\x14\xa0\xf9\xb1\xd6\x1e!\xe7\x96\xd7\x8d\xcc\xdf\x13R\xf2<\xd3(\x12\xf4\x85\x0b\x87\x8a\xe4\x94L\x02\x00<{"aps":{"sound":"default","alert":"Hello World!","badge":4}}\x03\x00\x04\x00\x00\x00\x01\x04\x00\x04\x00\x00\x0e\x10\x05\x00\x01\n')
self.assertTrue(f1 == frame.get_frame() or f2 == frame.get_frame())

def testPayloadTooLargeError(self):
# The maximum size of the JSON payload is MAX_PAYLOAD_LENGTH
Expand Down

0 comments on commit fce9e33

Please sign in to comment.