commit-gnue
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[gnue] r7204 - trunk/gnue-common/src/logic/adapters


From: johannes
Subject: [gnue] r7204 - trunk/gnue-common/src/logic/adapters
Date: Mon, 14 Mar 2005 13:16:04 -0600 (CST)

Author: johannes
Date: 2005-03-14 13:16:03 -0600 (Mon, 14 Mar 2005)
New Revision: 7204

Modified:
   trunk/gnue-common/src/logic/adapters/Base.py
   trunk/gnue-common/src/logic/adapters/python.py
Log:
Introduced a context-method 'bindBuiltin'


Modified: trunk/gnue-common/src/logic/adapters/Base.py
===================================================================
--- trunk/gnue-common/src/logic/adapters/Base.py        2005-03-14 18:20:27 UTC 
(rev 7203)
+++ trunk/gnue-common/src/logic/adapters/Base.py        2005-03-14 19:16:03 UTC 
(rev 7204)
@@ -114,6 +114,19 @@
 
 
   # ---------------------------------------------------------------------------
+  # Bind an element into the contexts builtin-namespace
+  # ---------------------------------------------------------------------------
+
+  def bindBuiltin (self, name, anElement):
+    """
+    Abstract: A descendant overrides this function to bind a given element into
+    the builtin-namespace of the context.
+    """
+
+    raise ImplementationError, (self.__class__, 'bindBuiltin ()')
+
+
+  # ---------------------------------------------------------------------------
   # Create a new function instance 
   # ---------------------------------------------------------------------------
 

Modified: trunk/gnue-common/src/logic/adapters/python.py
===================================================================
--- trunk/gnue-common/src/logic/adapters/python.py      2005-03-14 18:20:27 UTC 
(rev 7203)
+++ trunk/gnue-common/src/logic/adapters/python.py      2005-03-14 19:16:03 UTC 
(rev 7204)
@@ -67,6 +67,7 @@
 
     self._globalNS = {}
     self._localNS  = {}
+    self._builtins = {}
 
 
   # ---------------------------------------------------------------------------
@@ -112,6 +113,20 @@
 
 
   # ---------------------------------------------------------------------------
+  # Bind an element into the contexts builtin-namespace
+  # ---------------------------------------------------------------------------
+
+  def bindBuiltin (self, name, anElement):
+    """
+    Bind the given element into the builtin-namespace.
+    @param name: name of the element within the builtin-namespace
+    @param anElement: element to be bound into the builtin-namespace
+    """
+
+    self._builtins [name] = anElement
+
+
+  # ---------------------------------------------------------------------------
   # Create a function
   # ---------------------------------------------------------------------------
 
@@ -129,13 +144,11 @@
     context.
     """
 
-    for key in self._globalNS.keys ():
-      del self._globalNS [key]
+    self._globalNS.clear ()
+    self._localNS.clear ()
+    self._builtins.clear ()
 
-    for key in self._localNS.keys ():
-      del self._localNS [key]
 
-
 # =============================================================================
 # This class implements a virtual function using python
 # =============================================================================
@@ -159,7 +172,7 @@
     if not self.funcName:
       self.funcName = 'unnamed'
 
-    paramlist = string.join (['__namespace'] + \
+    paramlist = string.join (['__namespace', '__builtins'] + \
            [self.__identifier (key) for key in self._parameters.keys ()], ", ")
     text      = self._code.rstrip ()
 
@@ -179,12 +192,15 @@
 
       # add the functions header
       revisedCode  = "# -*- coding: utf-8 -*-\n"
+      revisedCode += "import __builtin__\n"
       revisedCode += "def %s (%s):\n" % (self.funcName, paramlist)
 
       # add the namespace transformation loop
       revisedCode += "  for __add in __namespace.keys ():\n"
       revisedCode += "    exec '%s = __namespace [\"%s\"]' % (__add, __add) in 
globals(), locals()\n"
-      revisedCode += "  del __add, __namespace\n\n"
+      revisedCode += "  for (name, item) in __builtins.items ():\n"
+      revisedCode += "    __builtin__.__dict__ [name] = item\n"
+      revisedCode += "  del __add, __namespace, __builtins\n\n"
 
       # add the original code
       for line in text.splitlines ():
@@ -218,7 +234,10 @@
       localNS = copy.copy (self._context._localNS)
       localNS.update (params)
       localNS ['__namespace'] = localNS
-      localNS ['abort'] = self.requestAbort
+      localNS ['__builtins'] = copy.copy (self._context._builtins)
+      # We do not use the context's bindBuiltin () method here to avoid another
+      # non-collectable reference
+      localNS ['__builtins'] ['abort'] = self.requestAbort
 
       try:
 
@@ -227,16 +246,12 @@
 
         exec self._compiled in self._context._globalNS, localNS
 
-        if localNS.has_key ('__result'):
-          return localNS ['__result']
-        else:
-          return None
+        return localNS.get ('__result')
 
       finally:
         # It's very importaint to release all references from the cloned
         # namespace here, otherwise garbage collection won't work very well !
-        for k in localNS.keys ():
-          del localNS [k]
+        localNS.clear ()
 
     except language.AbortRequest:
       # Pass through AbortRequests unchanged





reply via email to

[Prev in Thread] Current Thread [Next in Thread]