Skip to content

Commit 56e407f

Browse files
committed
Support both python2 and python3.
1 parent 1bb1368 commit 56e407f

11 files changed

+33
-14
lines changed

test/examples/autocommit_transaction_example.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626
class AutocommitTransactionExample(BaseApplication):
2727
def __init__(self, uri, user, password):
28-
super().__init__(uri, user, password)
28+
super(AutocommitTransactionExample, self).__init__(uri, user, password)
2929

3030
# tag::autocommit-transaction[]
3131
def add_person(self, name):

test/examples/base_application.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
from neo4j.v1 import GraphDatabase
2222

23-
class BaseApplication:
23+
class BaseApplication(object):
2424
def __init__(self, uri, user, password):
2525
self._driver = GraphDatabase.driver( uri, auth=( user, password ) )
2626

test/examples/cypher_error_example.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626
class CypherErrorExample(BaseApplication):
2727
def __init__(self, uri, user, password):
28-
super().__init__(uri, user, password)
28+
super(CypherErrorExample, self).__init__(uri, user, password)
2929

3030
# FIXME: this doesn't work because read_transaction behaves
3131
# differently than in Java, so this throws a ClientError

test/examples/hello_world_example.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
# tag::hello-world[]
2727
class HelloWorldExample(BaseApplication):
2828
def __init__(self, uri, user, password):
29-
super().__init__(uri, user, password)
29+
super(HelloWorldExample, self).__init__(uri, user, password)
3030

3131
def _create_and_return_greeting(self, tx, message):
3232
record_list = list(tx.run("CREATE (a:Greeting) " +

test/examples/read_write_transaction_example.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626
class ReadWriteTransactionExample(BaseApplication):
2727
def __init__(self, uri, user, password):
28-
super().__init__(uri, user, password)
28+
super(ReadWriteTransactionExample, self).__init__(uri, user, password)
2929

3030
# tag::read-write-transaction[]
3131
def add_person(self, name):

test/examples/result_consume_example.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626
class ResultConsumeExample(BaseApplication):
2727
def __init__(self, uri, user, password):
28-
super().__init__(uri, user, password)
28+
super(ResultConsumeExample, self).__init__(uri, user, password)
2929

3030
# tag::result-consume[]
3131
def get_people(self):

test/examples/result_retain_example.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626
class ResultRetainExample(BaseApplication):
2727
def __init__(self, uri, user, password):
28-
super().__init__(uri, user, password)
28+
super(ResultRetainExample, self).__init__(uri, user, password)
2929

3030
# tag::result-retain[]
3131
def add_employees(self, company_name):

test/examples/service_unavailable_example.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626
class ServiceUnavailableExample(BaseApplication):
2727
def __init__(self, uri, user, password):
28-
super().__init__(uri, user, password)
28+
super(ServiceUnavailableExample, self).__init__(uri, user, password)
2929

3030
# tag::service-unavailable[]
3131
def addItem(self):

test/examples/session_example.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
class SessionExample(BaseApplication):
2626
def __init__(self, uri, user, password):
27-
super().__init__(uri, user, password)
27+
super(SessionExample, self).__init__(uri, user, password)
2828

2929
# tag::session[]
3030
def do_work(self):

test/examples/test_examples.py

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,27 @@
2020

2121
from test.integration.tools import IntegrationTestCase
2222

23+
24+
## Python2 doesn't have contextlib.redirect_stdout()
25+
# http://eli.thegreenplace.net/2015/redirecting-all-kinds-of-stdout-in-python/
26+
import sys
27+
from contextlib import contextmanager
28+
@contextmanager
29+
def stdout_redirector(stream):
30+
old_stdout = sys.stdout
31+
sys.stdout = stream
32+
try:
33+
yield
34+
finally:
35+
sys.stdout = old_stdout
36+
37+
def get_string_io():
38+
if sys.version_info[0] < 3:
39+
from StringIO import StringIO
40+
else:
41+
from io import StringIO
42+
return StringIO()
43+
2344
class ExamplesTest(IntegrationTestCase):
2445

2546
def setUp(self):
@@ -65,12 +86,10 @@ def test_driver_lifecycle_example(self):
6586
self.assertIsInstance(example, DriverLifecycleExample)
6687

6788
def test_hello_world_example(self):
68-
import io
69-
from contextlib import redirect_stdout
7089
from hello_world_example import HelloWorldExample
7190

72-
f = io.StringIO()
73-
with redirect_stdout(f):
91+
f = get_string_io()
92+
with stdout_redirector(f):
7493
example = HelloWorldExample(self.bolt_uri, self.user, self.password)
7594
example.print_greeting("hello, world")
7695
example.close()

0 commit comments

Comments
 (0)