Author: oracle
Date: Thu Jun 19 23:00:21 2008
New Revision: 27328

URL: http://svn.gna.org/viewcvs/wesnoth?rev=27328&view=rev
Log:
Extended safe.py environment to expand AI's language capabilities; including:
collections, hotshot, psyco, Queue, sets, time, and the upcoming wail module
Use of chr, hash, lambda, ord, and super (new style classes) are now allowed
Control of safe_exec can now be toggled from the wesnoth binary
(not implemented) by passing a True/False value when the script is invoked.
Various exceptions are now allowed.

Modified:
    trunk/changelog
    trunk/data/ais/parse.py
    trunk/data/ais/safe.py

Modified: trunk/changelog
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/changelog?rev=27328&r1=27327&r2=27328&view=diff
==============================================================================
--- trunk/changelog (original)
+++ trunk/changelog Thu Jun 19 23:00:21 2008
@@ -46,6 +46,10 @@
  * Python AI:
    * Implemented a function which detects if a location is on the map border.
    * Implemented a function which gives the game's gold parameters.
+   * Extended safe.py environment to expand AI's language capabilities; 
including:
+       collections, hotshot, psyco, Queue, sets, time, and the upcoming wail 
module
+       Use of chr, hash, lambda, ord, and super (new style classes) are now 
allowed
+       Control of safe_exec can now be toggled from the wesnoth binary (not 
implemented)
  * terrains:
    * Fixed city village not being alias of the village terrain type; this was
      causing a duplicate "Village" terrain being displayed in the defense 
ratios

Modified: trunk/data/ais/parse.py
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/data/ais/parse.py?rev=27328&r1=27327&r2=27328&view=diff
==============================================================================
--- trunk/data/ais/parse.py (original)
+++ trunk/data/ais/parse.py Thu Jun 19 23:00:21 2008
@@ -1,6 +1,6 @@
 import re, os, safe
 
-whitelisted = ["wesnoth", "heapq", "random", "math", "string", "re", 
"threading"]
+whitelisted = ["collections", "heapq", "hotshot", "math", "psyco", "Queue", 
"random", "re", "sets", "string", "threading", "time", "wail", "wesnoth"]
 rex = re.compile(r"^import\s+(.*)", re.M)
 modules = {}
 
@@ -24,8 +24,9 @@
                 pass
         else:
             raise safe.SafeException("Could not include %s." % name)
-            return None
+
         r += code
+
     return r
 
 def parse_file(name):

Modified: trunk/data/ais/safe.py
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/data/ais/safe.py?rev=27328&r1=27327&r2=27328&view=diff
==============================================================================
--- trunk/data/ais/safe.py (original)
+++ trunk/data/ais/safe.py Thu Jun 19 23:00:21 2008
@@ -71,10 +71,12 @@
     '__debug__','quit','exit',
     'Warning',
     'None','True','False',
-    'abs', 'bool', 'callable', 'cmp', 'complex', 'dict', 'divmod', 'filter',
-    'float', 'frozenset', 'hex', 'int', 'isinstance', 'issubclass', 'len',
-    'list', 'long', 'map', 'max', 'min', 'object', 'oct', 'pow', 'range',
-    'repr', 'round', 'set', 'slice', 'str', 'sum', 'tuple',  'xrange', 'zip',
+    'abs', 'bool', 'callable', 'chr', 'cmp', 'complex', 'dict', 'divmod', 
'filter',
+    'float', 'frozenset', 'hash', 'hex', 'int', 'isinstance', 'issubclass', 
'len',
+    'list', 'long', 'map', 'max', 'min', 'object', 'oct', 'ord', 'pow', 
'range',
+    'repr', 'round', 'set', 'slice', 'str', 'sum', 'super', 'tuple',  
'xrange', 'zip',
+    'ArithmeticError', 'AssertionError', 'AttributeError', 'BaseException', 
'StopIteration',
+    'IndexError', 'KeyError', 'KeyboardInterrupt', 'RuntimeError', 
'RuntimeWarning'
     ]
 
 _BUILTIN_STR = [
@@ -124,11 +126,20 @@
         _builtin_restore()
         raise
 
-# If you want to disable safe python, use this instead:
-#
-##def safe_exec(code, context = None): exec code in context
-def safe_exec(code, context = None):
-    """Check the code to be safe, then run it with only safe builtins on."""
-    safe_check(code)
-    safe_run(code,context)
-    
+def safe_exec_noop( code, context=None ):
+    exec code in context
+
+def safe_exec_op( code, context=None ):
+    safe_check( code )
+    safe_run( code, context )
+
+# Wrapper allowing safe_exec to be dynamically controlled
+# from wesnoth binary.
+def safe_exec( code, context=None, runSafe=True ):
+    if runSafe:
+        safe_exec_op( code, context )
+
+    else:
+        safe_exec_noop( code, context )
+
+


_______________________________________________
Wesnoth-commits mailing list
[email protected]
https://mail.gna.org/listinfo/wesnoth-commits

Reply via email to