changeset 265:91e5058437e9

Merge
author Sigurd Meldgaard <stm@daimi.au.dk>
date Wed, 24 Feb 2010 15:49:06 +0100
parents 74b8ba12cc18 (current diff) 71ebbf22dd78 (diff)
children ed93aa3681e9
files
diffstat 1 files changed, 32 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/pysmcl/defer_wrapper.py	Wed Feb 24 15:49:06 2010 +0100
@@ -0,0 +1,32 @@
+# -*- test-case-name: twisted.test.test_defer -*-
+#
+# Copyright (c) 2001-2007 Twisted Matrix Laboratories.
+# See LICENSE for details.
+
+"""
+Support for results that aren't immediately available.
+
+Maintainer: Glyph Lefkowitz
+"""
+
+from __future__ import nested_scopes, generators
+
+from twisted.python.util import unsignedID, mergeFunctionMetadata
+from twisted.internet.defer import *
+from twisted.internet.defer import _inlineCallbacks
+import types
+
+
+def inlineCallbacks(f):
+    def unwindGenerator(*args, **kwargs):
+        deferred = Deferred()
+        try:
+            result = f(*args, **kwargs)
+        except Exception, e:
+            deferred.errback(e)
+            return deferred
+        if isinstance(result, types.GeneratorType):
+            return _inlineCallbacks(None, result, deferred)
+        deferred.callback(result)
+        return deferred
+    return mergeFunctionMetadata(f, unwindGenerator)