Skip to content

Commit 9809dab

Browse files
A. Jesse Jiryu Davisbehackett
authored andcommitted
Python 2.4 compatibility
1 parent 4152622 commit 9809dab

File tree

2 files changed

+19
-16
lines changed

2 files changed

+19
-16
lines changed

pymongo/connection.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -835,15 +835,18 @@ def __receive_data_on_socket(self, length, sock):
835835
while len(message) < length:
836836
try:
837837
chunk = sock.recv(length - len(message))
838-
except BaseException, e:
838+
except:
839+
# Store the exception from sock.recv() in case we throw a new
840+
# exception in this exception-handler.
841+
exc_type, exc_value, exc_traceback = sys.exc_info()
839842
# PYTHON-294: must close socket here, because if it remains open
840843
# after this then the next time we read from it we may get stale
841844
# data from a previous operation.
842845
try:
843846
self.__pool.discard_socket()
844847
except:
845848
pass
846-
raise e
849+
raise exc_value
847850

848851
if chunk == "":
849852
raise ConnectionFailure("connection closed")

test/test_connection.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
import datetime
1818
import os
19+
import signal
1920
import sys
2021
import time
2122
import unittest
@@ -481,38 +482,35 @@ def test_interrupt_signal(self):
481482
Test fix for PYTHON-294 -- make sure Connection closes its socket if it
482483
gets an interrupt while waiting to recv() from it.
483484
"""
484-
import pymongo
485-
import signal
486-
487485
c = get_connection()
488486
db = c.pymongo_test
489487

490-
# A $where clause which takes 1 sec to execute
488+
# A $where clause which takes 1.5 sec to execute
491489
where = '''function() {
492-
var d = new Date((new Date()).getTime() + 1*1000);
490+
var d = new Date((new Date()).getTime() + 1.5 * 1000);
493491
while (d > (new Date())) { }; return true;
494492
}'''
495493

496494
# Need exactly 1 document so find() will execute its $where clause once
497495
db.drop_collection('foo')
498496
db.foo.insert({'_id': 1}, safe=True)
499497

500-
# Convert SIGALRM to SIGINT -- it's hard to schedule a SIGINT for 1/2 a
498+
# Convert SIGALRM to SIGINT -- it's hard to schedule a SIGINT for one
501499
# second in the future, but easy to schedule SIGALRM.
502500
def sigalarm(num, frame):
503-
import sys
504501
raise KeyboardInterrupt
505502

506503
old_signal_handler = signal.signal(signal.SIGALRM, sigalarm)
507-
signal.setitimer(signal.ITIMER_REAL, 0.5, 0)
504+
signal.alarm(1)
508505
raised = False
509506
try:
510-
# Need list() to actually iterate the cursor and create the
511-
# cursor on the server; find is lazily evaluated. The find() will
512-
# be interrupted by sigalarm() raising a KeyboardInterrupt.
513-
list(db.foo.find({'$where': where}))
514-
except KeyboardInterrupt:
515-
raised = True
507+
try:
508+
# Need list() to actually iterate the cursor and create the
509+
# cursor on the server; find is lazily evaluated. The find() will
510+
# be interrupted by sigalarm() raising a KeyboardInterrupt.
511+
list(db.foo.find({'$where': where}))
512+
except KeyboardInterrupt:
513+
raised = True
516514
finally:
517515
signal.signal(signal.SIGALRM, old_signal_handler)
518516

@@ -528,5 +526,7 @@ def sigalarm(num, frame):
528526
list(db.foo.find())
529527
)
530528

529+
# TODO: test raising an error in pool.discard_socket()
530+
531531
if __name__ == "__main__":
532532
unittest.main()

0 commit comments

Comments
 (0)