2
0
Эх сурвалжийг харах

try to use __file__, fallback to output filename (cython does not define __file__). This works when running through python interpreter regardless of the cwd. For compiled cython binaries this only works when we execute the binary in the same directory, for a cwd independent fix we need something better.

frabbit 10 жил өмнө
parent
commit
811e8adab0

+ 13 - 5
genpy.ml

@@ -2128,7 +2128,18 @@ module Generator = struct
 
 	let gen_resources ctx =
 		if Hashtbl.length ctx.com.resources > 0 then begin
-			spr ctx "def _hx_resources__():\n\treturn {";
+			let slash_index = try (String.rindex ctx.com.file '/')+1 with Not_found -> 0 in
+			let len = String.length ctx.com.file - slash_index in
+			let file_name = String.sub ctx.com.file slash_index len in
+			spr ctx "def _hx_resources__():";
+			spr ctx "\n\timport inspect";
+			spr ctx "\n\timport sys";
+			spr ctx "\n\tif not hasattr(sys.modules[__name__], '__file__'):";
+			print ctx "\n\t\t_file = '%s'" file_name;
+			spr ctx "\n\telse:";
+			spr ctx "\n\t\t_file = __file__";
+
+			spr ctx "\n\treturn {";
 			let first = ref true in
 			Hashtbl.iter (fun k v ->
 				let prefix = if !first then begin
@@ -2138,10 +2149,7 @@ module Generator = struct
 					","
 				in
 				let k_enc = Codegen.escape_res_name k false in
-				let slash_index = try (String.rindex ctx.com.file '/')+1 with Not_found -> 0 in
-				let len = String.length ctx.com.file - slash_index in
-				let file_name = String.sub ctx.com.file slash_index len in
-				print ctx "%s\"%s\": open('%%s.%%s'%%('%s','%s'),'rb').read()" prefix (Ast.s_escape k) file_name k_enc;
+				print ctx "%s\"%s\": open('%%s.%%s'%%(_file,'%s'),'rb').read()" prefix (Ast.s_escape k) k_enc;
 				Std.output_file (ctx.com.file ^ "." ^ k_enc) v
 			) ctx.com.resources;
 			spr ctx "}"

+ 13 - 8
std/python/_std/haxe/Resource.hx

@@ -26,36 +26,41 @@ import haxe.io.BytesData;
 
 @:coreApi class Resource {
 
-	static var content : python.lib.Dict<String, BytesData> = untyped _hx_resources__();
+	static var content : python.lib.Dict<String, BytesData> = null;
+
+	static function getContent ():python.lib.Dict<String, BytesData> {
+		if (content == null) content = untyped _hx_resources__();
+		return content;
+	}
 
 	public static inline function listNames() : Array<String> {
-		return python.lib.Builtin.list(content.keys());
+		return python.lib.Builtin.list(getContent().keys());
 	}
 
 	public static function getString( name : String ) : String {
         #if embed_resources
-		for (k in content.keys().iter()) {
+		for (k in getContent().keys().iter()) {
 			if (k == name) {
-				var b : haxe.io.Bytes = haxe.crypto.Base64.decode(content.get(k, null));
+				var b : haxe.io.Bytes = haxe.crypto.Base64.decode(getContent().get(k, null));
 				return b.toString();
 			}
 		}
 		return null;
         #else
-        return content.hasKey(name) ? Bytes.ofData(content.get(name, null)).toString() : null;
+        return getContent().hasKey(name) ? Bytes.ofData(getContent().get(name, null)).toString() : null;
         #end
 	}
 
 	public static function getBytes( name : String ) : haxe.io.Bytes {
         #if embed_resources
-		for( k in content.keys().iter() )
+		for( k in getContent().keys().iter() )
 			if( k == name ) {
-				var b : haxe.io.Bytes = haxe.crypto.Base64.decode(content.get(k, null));
+				var b : haxe.io.Bytes = haxe.crypto.Base64.decode(getContent().get(k, null));
 				return b;
 
 			}
         #else
-        return Bytes.ofData(content.get(name,null));
+        return Bytes.ofData(getContent().get(name,null));
         #end
 	}
 }