Browse Source

move KeywordHandler functions to Reflect directly

frabbit 11 years ago
parent
commit
b255276f69
3 changed files with 46 additions and 17 deletions
  1. 1 1
      std/haxe/Log.hx
  2. 9 9
      std/python/Lib.hx
  3. 36 7
      std/python/_std/Reflect.hx

+ 1 - 1
std/haxe/Log.hx

@@ -105,7 +105,7 @@ class Log {
 			} else {
 				str = v;
 			}
-			Sys.println(str);
+			python.Lib.println([str]);
 		#end
 	}
 

+ 9 - 9
std/python/Lib.hx

@@ -4,18 +4,18 @@ import python.lib.Types;
 
 
 
-class HaxeIterable<T> 
+class HaxeIterable<T>
 {
   var x :NativeIterable<T>;
   public inline function new (x:NativeIterable<T>) {
     this.x = x;
-    
+
   }
 
   public inline function iterator ():HaxeIterator<T> return new HaxeIterator(x.__iter__());
 }
 
-class HaxeIterator<T> 
+class HaxeIterator<T>
 {
   var it :NativeIterator<T>;
   var x:Null<T> = null;
@@ -45,9 +45,9 @@ class HaxeIterator<T>
         x = null;
       }
       checked = true;
-      return has;  
+      return has;
     }
-    
+
   }
 }
 
@@ -79,7 +79,7 @@ class Lib
        }
     }
 
-    public static function toPythonIterable <T>(it:Iterable<T>):python.lib.Types.NativeIterable<T> 
+    public static function toPythonIterable <T>(it:Iterable<T>):python.lib.Types.NativeIterable<T>
     {
       return {
         __iter__ : function () {
@@ -100,16 +100,16 @@ class Lib
       }
     }
 
-    public static inline function toHaxeIterable <T>(it:NativeIterable<T>):HaxeIterable<T> 
+    public static inline function toHaxeIterable <T>(it:NativeIterable<T>):HaxeIterable<T>
     {
       return new HaxeIterable(it);
     }
 
-    public static inline function toHaxeIterator <T>(it:NativeIterator<T>):HaxeIterator<T> 
+    public static inline function toHaxeIterator <T>(it:NativeIterator<T>):HaxeIterator<T>
     {
       return new HaxeIterator(it);
     }
 
 
-    
+
 }

+ 36 - 7
std/python/_std/Reflect.hx

@@ -20,7 +20,7 @@
  * DEALINGS IN THE SOFTWARE.
  */
 
-import python.internal.KeywordHandler;
+
 import python.lib.Builtin;
 import python.lib.Inspect;
 import python.lib.Types;
@@ -28,28 +28,57 @@ import python.lib.Types;
 @:coreApi
 class Reflect {
 
+	static var keywords:Set<String> = new Set(
+    [
+        "and",       "del",       "from",      "not",       "while",
+        "as",        "elif",      "global",    "or",        "with",
+        "assert",    "else",      "if",        "pass",      "yield",
+        "break",     "except",    "import",    "print",     "float",
+        "class",     "exec",      "in",        "raise",
+        "continue",  "finally",   "is",        "return",
+        "def",       "for",       "lambda",    "try",
+        "None",      "list"
+    ]);
+
+	static inline function handleKeywords(name:String):String
+    {
+        if (keywords.has(name)) {
+            return "_hx_" + name;
+        }
+        return name;
+    }
+
+    static function unhandleKeywords(name:String):String
+    {
+    	if (name.substr(0,4) == "_hx_") {
+    		var real = name.substr(4);
+    		if (keywords.has(real)) return real;
+    	}
+    	return name;
+    }
+
 	public static function hasField( o : Dynamic, field : String ) : Bool
 	{
-		var field = KeywordHandler.handleKeywords(field);
+		var field = handleKeywords(field);
 		return Builtin.hasattr(o, field);
 	}
 
 	@:keep public static function field( o : Dynamic, field : String ) : Dynamic
 	{
 		if (field == null) return null;
-		var field = KeywordHandler.handleKeywords(field);
+		var field = handleKeywords(field);
 		return if (Builtin.hasattr(o, field)) Builtin.getattr(o, field) else null;
 	}
 
 	@:keep public static function setField( o : Dynamic, field : String, value : Dynamic ) : Void untyped
 	{
-		var field = KeywordHandler.handleKeywords(field);
+		var field = handleKeywords(field);
 		return __define_feature__("Reflect.setField",Builtin.setattr(o,field,value));
 	}
 
 	public static function getProperty( o : Dynamic, field : String ) : Dynamic
 	{
-		var field = KeywordHandler.handleKeywords(field);
+		var field = handleKeywords(field);
 		var tmp = null;
 		if (o == null) {
 			return null;
@@ -65,7 +94,7 @@ class Reflect {
 
 	public static function setProperty( o : Dynamic, field : String, value : Dynamic ) : Void {
 
-		var field = KeywordHandler.handleKeywords(field);
+		var field = handleKeywords(field);
 
 		return if (Builtin.hasattr(o,"set_"+field)) {
 			var tmp = Builtin.getattr(o,"set_"+field);
@@ -96,7 +125,7 @@ class Reflect {
 
 				var d:Dict<String, Dynamic> = Builtin.getattr(o, "__dict__");
 				var keys  = d.keys();
-				var handler = python.internal.KeywordHandler.unhandleKeywords;
+				var handler = unhandleKeywords;
 				untyped __python__("for k in keys:");
 				untyped __python__("	a.append(handler(k))");