commit-gnue
[Top][All Lists]
Advanced

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

r5604 - trunk/gnue-common/src/logic/adapters


From: johannes
Subject: r5604 - trunk/gnue-common/src/logic/adapters
Date: Thu, 1 Apr 2004 03:06:01 -0600 (CST)

Author: johannes
Date: 2004-04-01 03:06:00 -0600 (Thu, 01 Apr 2004)
New Revision: 5604

Modified:
   trunk/gnue-common/src/logic/adapters/python.py
Log:
Fixed namespace-transformation.


Modified: trunk/gnue-common/src/logic/adapters/python.py
===================================================================
--- trunk/gnue-common/src/logic/adapters/python.py      2004-04-01 01:27:29 UTC 
(rev 5603)
+++ trunk/gnue-common/src/logic/adapters/python.py      2004-04-01 09:06:00 UTC 
(rev 5604)
@@ -20,9 +20,10 @@
 #
 # $Id: $
 
-from copy import copy
 
 import string
+import re
+import copy
 
 from gnue.common.logic import language
 from gnue.common.logic.adapters import Base
@@ -136,15 +137,10 @@
     the code.
     """
     # The function name may only contain ascii characters and underscores.
-    self.funcName = ''
-    for c in self._name:
-      # if c in string.ascii_letters:  -- needs Python 2.2 or later
-      if c in 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz':
-        self.funcName += c
-      else:
-        self.funcName += '_'
+    self.funcName = self.__identifier (self._name)
 
-    paramlist = string.join (['__namespace'] + self._parameters.keys (), ", ")
+    paramlist = string.join (['__namespace'] + \
+           [self.__identifier (key) for key in self._parameters.keys ()], ", ")
     text      = self._code.rstrip ()
 
     try:
@@ -180,7 +176,6 @@
 
       self._compiled = compile (revisedCode.encode ('utf-8'),
                      '<%s>' % self._context.shortname.encode ('utf-8'), 'exec')
-
     except:
       raise language.CompileError, self._traceback (1)
           
@@ -196,10 +191,13 @@
     executes the code.
     """
     try:
-      localNS = copy (self._context._localNS)
+      localNS = copy.copy (self._context._localNS)
       localNS.update (params)
       localNS ['__namespace'] = localNS
 
+      # make sure we only use safe identifiers in our namespace
+      self.__makeSafeNamespace (localNS)
+
       exec self._compiled in self._context._globalNS, localNS
 
       if localNS.has_key ('__result'):
@@ -210,3 +208,34 @@
 
     except:
       raise language.RuntimeError, self._traceback (2)
+
+
+
+  # ---------------------------------------------------------------------------
+  # Make sure we use proper identifiers
+  # ---------------------------------------------------------------------------
+
+  def __identifier (self, name):
+    """
+    This function translates @name to a valid python identifier, which means
+    all non-letter characters are replaced by an underscore.
+    """
+    return re.sub ('[^a-zA-Z]', '_', name)
+
+
+  # ---------------------------------------------------------------------------
+  # Make sure the given Namespace has no invalid identifiers
+  # ---------------------------------------------------------------------------
+
+  def __makeSafeNamespace (self, namespace):
+    """
+    This function replaces all invalid keys in the dict. @namespace by
+    appropriate identifiers.
+    """
+    for key in namespace.keys ():
+      safeId = self.__identifier (key)
+      if safeId != key:
+        namespace [safeId] = namespace [key]
+        del namespace [key]
+
+





reply via email to

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