Rev 76: Test that passing a callable works. in http://bazaar.launchpad.net/~meliae-dev/meliae/trunk

John Arbash Meinel john at arbash-meinel.com
Fri Sep 11 17:16:17 BST 2009


At http://bazaar.launchpad.net/~meliae-dev/meliae/trunk

------------------------------------------------------------
revno: 76
revision-id: john at arbash-meinel.com-20090911161600-7fhsncg08p3kxoov
parent: john at arbash-meinel.com-20090911152035-twwbnvbndmunotpq
committer: John Arbash Meinel <john at arbash-meinel.com>
branch nick: trunk
timestamp: Fri 2009-09-11 11:16:00 -0500
message:
  Test that passing a callable works.
-------------- next part --------------
=== modified file 'meliae/_scanner.pyx'
--- a/meliae/_scanner.pyx	2009-09-11 15:09:34 +0000
+++ b/meliae/_scanner.pyx	2009-09-11 16:16:00 +0000
@@ -29,6 +29,8 @@
     int Py_UNICODE_SIZE
     ctypedef struct PyGC_Head:
         pass
+    object PyString_FromStringAndSize(char *, Py_ssize_t)
+
 
 cdef extern from "_scanner_core.h":
     Py_ssize_t _size_of(object c_obj)
@@ -63,6 +65,12 @@
     file_cb = <FILE *>callee_data
     fwrite(bytes, 1, len, file_cb)
 
+cdef void _callable_callback(void *callee_data, char *bytes, size_t len):
+    callable = <object>callee_data
+
+    s = PyString_FromStringAndSize(bytes, len)
+    callable(s)
+
 
 def dump_object_info(object out, object obj, object nodump=None,
                      int recurse_depth=1):
@@ -72,7 +80,7 @@
         If a File object, we will write bytes to the underlying FILE*
         Otherwise, we will call(str) with bytes as we build up the state of the
         object. Note that a single call will not be a complete description, but
-        potentially a single character for formatting.
+        potentially a single character of the final formatted string.
     :param obj: The object to inspect
     :param nodump: If supplied, this is a set() of objects that we want to
         exclude from the dump file.
@@ -88,7 +96,7 @@
         # This must be a callable
         _dump_object_info(_file_io_callback, fp_out, obj, nodump, recurse_depth)
     else:
-        raise TypeError('not a file')
+        _dump_object_info(_callable_callback, <void *>out, obj, nodump, recurse_depth)
 
 
 def get_referents(object obj):

=== modified file 'meliae/tests/test__scanner.py'
--- a/meliae/tests/test__scanner.py	2009-09-10 21:42:06 +0000
+++ b/meliae/tests/test__scanner.py	2009-09-11 16:16:00 +0000
@@ -340,7 +340,11 @@
         t_file = getattr(t, 'file', t)
         _scanner.dump_object_info(t_file, obj, nodump=nodump)
         t.seek(0)
-        self.assertEqual(py_dump_object_info(obj, nodump=nodump), t.read())
+        as_bytes = t.read()
+        self.assertEqual(py_dump_object_info(obj, nodump=nodump), as_bytes)
+        as_list = []
+        _scanner.dump_object_info(as_list.append, obj, nodump=nodump)
+        self.assertEqual(as_bytes, ''.join(as_list))
 
     def test_dump_int(self):
         self.assertDumpInfo(1)



More information about the bazaar-commits mailing list