aboutsummaryrefslogtreecommitdiffstats
path: root/lang/python/tests
diff options
context:
space:
mode:
Diffstat (limited to 'lang/python/tests')
-rwxr-xr-xlang/python/tests/t-callbacks.py71
-rwxr-xr-xlang/python/tests/t-data.py41
2 files changed, 112 insertions, 0 deletions
diff --git a/lang/python/tests/t-callbacks.py b/lang/python/tests/t-callbacks.py
index 57975264..3219463c 100755
--- a/lang/python/tests/t-callbacks.py
+++ b/lang/python/tests/t-callbacks.py
@@ -17,6 +17,7 @@
# You should have received a copy of the GNU Lesser General Public
# License along with this program; if not, see <http://www.gnu.org/licenses/>.
+import os
from pyme import core, constants
import support
@@ -181,3 +182,73 @@ except Exception as e:
assert e == myException
else:
assert False, "Expected an error, got none"
+
+
+
+# Test the data callbacks.
+def read_cb(amount, hook=None):
+ assert hook == cookie
+ return 0
+def release_cb(hook=None):
+ assert hook == cookie
+data = core.Data(cbs=(read_cb, None, None, release_cb, cookie))
+try:
+ data.read()
+except Exception as e:
+ assert type(e) == TypeError
+else:
+ assert False, "Expected an error, got none"
+
+def read_cb(amount):
+ raise myException
+data = core.Data(cbs=(read_cb, None, None, lambda: None))
+try:
+ data.read()
+except Exception as e:
+ assert e == myException
+else:
+ assert False, "Expected an error, got none"
+
+
+def write_cb(what, hook=None):
+ assert hook == cookie
+ return "wrong type"
+data = core.Data(cbs=(None, write_cb, None, release_cb, cookie))
+try:
+ data.write(b'stuff')
+except Exception as e:
+ assert type(e) == TypeError
+else:
+ assert False, "Expected an error, got none"
+
+def write_cb(what):
+ raise myException
+data = core.Data(cbs=(None, write_cb, None, lambda: None))
+try:
+ data.write(b'stuff')
+except Exception as e:
+ assert e == myException
+else:
+ assert False, "Expected an error, got none"
+
+
+def seek_cb(offset, whence, hook=None):
+ assert hook == cookie
+ return "wrong type"
+data = core.Data(cbs=(None, None, seek_cb, release_cb, cookie))
+try:
+ data.seek(0, os.SEEK_SET)
+except Exception as e:
+ assert type(e) == TypeError
+else:
+ assert False, "Expected an error, got none"
+
+def seek_cb(offset, whence):
+ raise myException
+data = core.Data(cbs=(None, None, seek_cb, lambda: None))
+try:
+ data.seek(0, os.SEEK_SET)
+except Exception as e:
+ assert e == myException
+else:
+ assert False, "Expected an error, got none"
diff --git a/lang/python/tests/t-data.py b/lang/python/tests/t-data.py
index 6cf10fab..de60c473 100755
--- a/lang/python/tests/t-data.py
+++ b/lang/python/tests/t-data.py
@@ -17,6 +17,7 @@
# You should have received a copy of the GNU Lesser General Public
# License along with this program; if not, see <http://www.gnu.org/licenses/>.
+import io
import os
import tempfile
from pyme import core
@@ -79,3 +80,43 @@ with tempfile.NamedTemporaryFile() as tmp:
# Open using name, offset, and length.
data = core.Data(file=tmp.name, offset=23, length=42)
assert data.read() == binjunk[23:23+42]
+
+# Test callbacks.
+class DataObject(object):
+ def __init__(self):
+ self.buffer = io.BytesIO()
+ self.released = False
+
+ def read(self, amount, hook=None):
+ assert not self.released
+ return self.buffer.read(amount)
+
+ def write(self, data, hook=None):
+ assert not self.released
+ return self.buffer.write(data)
+
+ def seek(self, offset, whence, hook=None):
+ assert not self.released
+ return self.buffer.seek(offset, whence)
+
+ def release(self, hook=None):
+ assert not self.released
+ self.released = True
+
+do = DataObject()
+cookie = object()
+data = core.Data(cbs=(do.read, do.write, do.seek, do.release, cookie))
+data.write('Hello world!')
+data.seek(0, os.SEEK_SET)
+assert data.read() == b'Hello world!'
+del data
+assert do.released
+
+# Again, without the cookie.
+do = DataObject()
+data = core.Data(cbs=(do.read, do.write, do.seek, do.release))
+data.write('Hello world!')
+data.seek(0, os.SEEK_SET)
+assert data.read() == b'Hello world!'
+del data
+assert do.released