Browse Source

Warn against reused context managers, and fix broken tests

tags/nilmdb-1.6.0
Jim Paris 10 years ago
parent
commit
ea838d05ae
4 changed files with 14 additions and 2 deletions
  1. +10
    -0
      nilmdb/client/client.py
  2. +1
    -0
      nilmdb/client/numpyclient.py
  3. +1
    -1
      tests/test_client.py
  4. +2
    -1
      tests/test_numpyclient.py

+ 10
- 0
nilmdb/client/client.py View File

@@ -144,6 +144,7 @@ class Client(object):
ctx = StreamInserter(self, path, start, end)
yield ctx
ctx.finalize()
ctx.destroy()

def stream_insert(self, path, data, start = None, end = None):
"""Insert rows of data into a stream. data should be a string
@@ -293,6 +294,15 @@ class StreamInserter(object):
self._block_data = []
self._block_len = 0

self.destroyed = False

def destroy(self):
"""Ensure this object can't be used again without raising
an error"""
def error(*args, **kwargs):
raise Exception("don't reuse this context object")
self._send_block = self.insert = self.finalize = self.send = error

def insert(self, data):
"""Insert a chunk of ASCII formatted data in string form. The
overall data must consist of lines terminated by '\\n'."""


+ 1
- 0
nilmdb/client/numpyclient.py View File

@@ -98,6 +98,7 @@ class NumpyClient(nilmdb.client.client.Client):
ctx = StreamInserterNumpy(self, path, start, end, dtype)
yield ctx
ctx.finalize()
ctx.destroy()

def stream_insert_numpy(self, path, data, start = None, end = None,
layout = None):


+ 1
- 1
tests/test_client.py View File

@@ -621,7 +621,7 @@ class TestClient(object):
pass

# Try various things that might cause problems
with client.stream_insert_context("/empty/test", 1000, 1050):
with client.stream_insert_context("/empty/test", 1000, 1050) as ctx:
ctx.finalize() # inserts [1000, 1050]
ctx.finalize() # nothing
ctx.finalize() # nothing


+ 2
- 1
tests/test_numpyclient.py View File

@@ -310,7 +310,8 @@ class TestNumpyClient(object):
pass

# Try various things that might cause problems
with client.stream_insert_numpy_context("/empty/test", 1000, 1050):
with client.stream_insert_numpy_context("/empty/test",
1000, 1050) as ctx:
ctx.finalize() # inserts [1000, 1050]
ctx.finalize() # nothing
ctx.finalize() # nothing


Loading…
Cancel
Save