Commits

Marek Kubica committed 1244460

Changed handler code so that the handlers are acutally methods of the class

Comments (0)

Files changed (1)

 import libgs
 import gsu
 
-def std_stream_handler(f):
-    """A decorator that wraps the stream handler functions into the apropriate
-    ctypes function callback types"""
-    return libgs.c_stdstream_t(f)
-
 class GhostScriptInterpreter(object):
     """A high level interface for Ghostscript. To push words, you only have
     to call methods on the object"""
                     # quiet out copyright messages and stuff
                     '-q'
                 ])
-        #libgs.gsapi_set_stdio(gs, self._stdin_handler, self._stdout_handler,
-        #        self._stderr_handler)
+
+        # create references to C functions, we have to keep these references
+        # along, otherwise the GC might discard them and the callback fails
+        self._stdout_handler_c = libgs.c_stdstream_t(self._stdout_handler)
+        self._stderr_handler_c = libgs.c_stdstream_t(self._stderr_handler)
+        self._stdin_handler_c = libgs.c_stdstream_t(self._stdin_handler)
+
+        # set up stdio
+        libgs.gsapi_set_stdio(gs, self._stdin_handler_c,
+                self._stdout_handler_c,
+                self._stderr_handler_c)
         # save for later use
         self._instance = gs
+        self._stdout = ''
 
-    @std_stream_handler
-    def _stdout_handler(caller_handle, buf, length):
-        print("I got called")
-        print(caller_handle, buf, length)
-        return 0
+    def _stdout_handler(self, caller_handle, buf, length):
+        """Handles the stdout of the Ghostscript interpreter"""
+        self._stdout += str(buf, 'utf-8')
+        # return length to signalize we handled the data
+        return length
 
-    _stdin_handler = _stdout_handler
-    _stderr_handler = _stdout_handler
+    def _stdin_handler(self, caller_handle, buf, length):
+        # not yet known how to proceed with stdin
+        return length
+
+    _stderr_handler = _stdin_handler
 
     def number(self, value):
         """Pushes a number on the stack by converting into a string"""