Ver Fonte

improve types of io hierarchy, fix return type of Builtin.open, improve io wrappers

frabbit há 10 anos atrás
pai
commit
ee40199b35

+ 7 - 4
std/python/_std/sys/io/Process.hx

@@ -1,6 +1,10 @@
 
 package sys.io;
 
+import python.io.IoTools;
+import python.lib.io.BufferedReader;
+import python.lib.io.BufferedWriter;
+import python.lib.io.TextIOWrapper;
 import python.lib.Subprocess;
 import python.lib.subprocess.Popen;
 
@@ -16,10 +20,9 @@ class Process {
 
 		p = Popen.create([cmd].concat(args), { stdin : Subprocess.PIPE, stdout: Subprocess.PIPE, stderr : Subprocess.PIPE });
 
-
-		this.stdout = new FileInput (cast p.stdout);
-		this.stderr = new FileInput (cast p.stderr);
-		this.stdin =  new FileOutput(cast p.stdin);
+		this.stdout = IoTools.createFileInputFromText(new TextIOWrapper(new BufferedReader(p.stdout)));
+		this.stderr = IoTools.createFileInputFromText(new TextIOWrapper(new BufferedReader(p.stderr)));
+		this.stdin =  IoTools.createFileOutputFromText(new TextIOWrapper(new BufferedWriter(p.stdin)));
 	}
 
 	public function getPid() : Int {

+ 1 - 0
std/python/io/IoTools.hx

@@ -44,6 +44,7 @@ class IoTools {
  				p = tell() + p;
  				SeekSet;
  		}
+
  		stream.seek(p, pos);
 	}
 

+ 1 - 1
std/python/io/NativeBytesInput.hx

@@ -30,7 +30,7 @@ class NativeBytesInput extends NativeInput<RawIOBase> implements IInput {
 		return ret.get(0);
 	}
 
-	public function seek( p : Int, pos : sys.io.FileSeek ) : Void
+	override public function seek( p : Int, pos : sys.io.FileSeek ) : Void
 	{
 		wasEof = false;
 		return IoTools.seekInBinaryMode(stream, p, pos);

+ 5 - 1
std/python/io/NativeInput.hx

@@ -49,12 +49,16 @@ class NativeInput<T:IOBase> extends Input{
 		throw "abstract method, should be overriden";
 	}
 
+	function seek (p:Int, mode:sys.io.FileSeek) {
+		throw "abstract method, should be overriden";
+	}
+
 	override public function readBytes(s:haxe.io.Bytes, pos:Int, len:Int):Int
 	{
 		if( pos < 0 || len < 0 || pos + len > s.length )
 			throw haxe.io.Error.OutsideBounds;
 
-		stream.seek(pos, python.lib.io.IOBase.SeekSet.SeekCur);
+		seek(pos, SeekBegin);
 		var ba = new Bytearray(len);
 		var ret = readinto(ba);
 		s.blit(pos, haxe.io.Bytes.ofData(ba) ,0,len);

+ 1 - 1
std/python/io/NativeTextInput.hx

@@ -29,7 +29,7 @@ class NativeTextInput extends NativeInput<TextIOBase> implements IInput {
 		return ret.charCodeAt(0);
 	}
 
-	public function seek( p : Int, pos : sys.io.FileSeek ) : Void
+	override public function seek( p : Int, pos : sys.io.FileSeek ) : Void
 	{
 		wasEof = false;
 		IoTools.seekInTextMode(stream, tell, p, pos);

+ 2 - 1
std/python/lib/Builtin.hx

@@ -4,6 +4,7 @@ package python.lib;
 
 import python.lib.io.FileIO;
 import python.lib.Dict;
+import python.lib.io.IOBase;
 import python.NativeIterable;
 import python.NativeIterator;
 
@@ -41,7 +42,7 @@ extern class Builtin {
 	@:overload(function (f:Tuple<Dynamic>):Int {})
 	public static function len(x:String):Int;
 
-	public static function open(file:String, mode:String, ?buffering:Int = -1, ?encoding:String = null, ?errors : String, ?newline:String, ?closefd:Bool, ?opener:String->Int->FileDescriptor):FileIO;
+	public static function open(file:String, mode:String, ?buffering:Int = -1, ?encoding:String = null, ?errors : String, ?newline:String, ?closefd:Bool, ?opener:String->Int->FileDescriptor):IOBase;
 
 	//public static function divmod():Void;
 	//public static function input():Void;

+ 12 - 0
std/python/lib/Io.hx

@@ -0,0 +1,12 @@
+
+package python.lib;
+
+import python.lib.io.IOBase;
+
+@:pythonImport("io")
+extern class Io {
+
+	public static var DEFAULT_BUFFER_SIZE:Int;
+
+	public static function open(file:String, mode:String, ?buffering:Int = -1, ?encoding:String = null, ?errors : String, ?newline:String, ?closefd:Bool, ?opener:String->Int->FileDescriptor):IOBase;
+}

+ 9 - 0
std/python/lib/io/BlockingIOError.hx

@@ -0,0 +1,9 @@
+package python.lib.io;
+
+import python.lib.Exceptions.IOError;
+
+class BlockingIOError extends IOError {
+
+	var characters_written:Int;
+
+}

+ 18 - 2
std/python/lib/io/BufferedIOBase.hx

@@ -1,14 +1,30 @@
-
 package python.lib.io;
 
+import python.lib.io.IOBase;
+
 import python.lib.io.RawIOBase;
 import python.lib.Bytearray;
 
-extern class BufferedIOBase extends IOBase {
+@:pythonImport("io", "BufferedIOBase")
+extern class BufferedIOBase extends IOBase implements IBufferedIOBase {
 
 	/* not always available */
 	public var raw:RawIOBase;
 
 	public function write (b:Bytearray):Int;
 	public function readinto (b:Bytearray):Int;
+	public function detach ():RawIOBase;
+	public function read(n:Int = -1):Null<Bytes>;
+	public function read1(n:Int = -1):Null<Bytes>;
+}
+
+
+@:remove extern interface IBufferedIOBase extends IIOBase {
+	public var raw:RawIOBase;
+
+	public function write (b:Bytearray):Int;
+	public function readinto (b:Bytearray):Int;
+	public function detach ():RawIOBase;
+	public function read(n:Int = -1):Null<Bytes>;
+	public function read1(n:Int = -1):Null<Bytes>;
 }

+ 3 - 2
std/python/lib/io/BufferedRWPair.hx

@@ -1,8 +1,9 @@
 
 package python.lib.io;
 
-extern class BufferedRWPair {
+@:pythonImport("io", "BufferedRWPair")
+extern class BufferedRWPair extends BufferedIOBase {
+
 
-	
 
 }

+ 6 - 2
std/python/lib/io/BufferedRandom.hx

@@ -1,8 +1,12 @@
 
 package python.lib.io;
 
-extern class BufferedRandom {
+import python.lib.io.BufferedReader;
+import python.lib.io.BufferedWriter;
+
+@:pythonImport("io", "BufferedRandom")
+extern class BufferedRandom extends BufferedReader implements IBufferedWriter {
+
 
-	
 
 }

+ 11 - 1
std/python/lib/io/BufferedReader.hx

@@ -1,10 +1,20 @@
 
 package python.lib.io;
 
+import python.lib.Bytes;
 import python.lib.io.BufferedIOBase;
 
-extern class BufferedReader extends BufferedIOBase {
+@:pythonImport("io", "BufferedReader")
+extern class BufferedReader extends BufferedIOBase implements IBufferedReader {
 
+	public function new (raw:RawIOBase):Void;
 
+	public function peek(?n:Int):Null<Bytes>;
+
+
+}
+
+@:remove extern interface IBufferedReader extends IBufferedIOBase {
+	public function peek(?n:Int):Null<Bytes>;
 
 }

+ 9 - 2
std/python/lib/io/BufferedWriter.hx

@@ -1,8 +1,15 @@
 
 package python.lib.io;
 
-extern class BufferedWriter {
+import python.lib.Bytearray;
+import python.lib.io.BufferedIOBase;
 
-	
+@:pythonImport("io", "BufferedWriter")
+extern class BufferedWriter extends BufferedIOBase implements IBufferedWriter {
+	public function new (raw:RawIOBase):Void;
+}
 
+
+@:remove extern interface IBufferedWriter extends IBufferedIOBase {
+	public function flush():Void;
 }

+ 12 - 2
std/python/lib/io/FileIO.hx

@@ -3,8 +3,18 @@ package python.lib.io;
 
 import python.lib.io.RawIOBase;
 
-extern class FileIO extends RawIOBase{
+@:pythonImport("io", "FileIO")
+extern class FileIO extends RawIOBase {
+
+	/**
+		The mode as given in the constructor.
+	**/
+	public var mode:String;
+	/**
+		The file name. This is the file descriptor of the file when no name is given in the constructor.
+	**/
+	public var name:String;
+
 
-	
 
 }

+ 16 - 1
std/python/lib/io/IOBase.hx

@@ -6,9 +6,24 @@ package python.lib.io;
 	var SeekCur = 1;
 	var SeekEnd = 2;
 }
+@:pythonImport("io", "IOBase")
+extern class IOBase implements IIOBase {
 
-extern class IOBase {
+	public function close():Void;
+	public function flush():Void;
+	public function readline(limit:Int = -1):String;
+	public function readable():Bool;
+	public var closed(default, null):Bool;
+	public function readlines(hint:Int=-1):Array<String>;
+	public function tell():Int;
+	public function writable():Bool;
+	public function seekable():Bool;
+	public function fileno():Int;
+	public function seek(offset:Int, whence:SeekSet):Int;
+	public function truncate (size:Int):Int;
+}
 
+@:remove extern interface IIOBase {
 	public function close():Void;
 	public function flush():Void;
 	public function readline(limit:Int = -1):String;

+ 11 - 2
std/python/lib/io/RawIOBase.hx

@@ -4,10 +4,19 @@ package python.lib.io;
 import python.lib.io.FileIO;
 import python.lib.io.IOBase;
 
-extern class RawIOBase extends IOBase{
+@:pythonImport("io", "RawIOBase")
+extern class RawIOBase extends IOBase implements IRawIOBase {
 
 	public function readall():Bytes;
 	public function read(n:Int=-1):Null<Bytes>;
 	public function write(b:Bytearray):Null<Int>;
 	public function readinto(b:Bytearray):Null<Int>;
-}
+}
+
+@:remove extern interface IRawIOBase extends IIOBase {
+
+	public function readall():Bytes;
+	public function read(n:Int=-1):Null<Bytes>;
+	public function write(b:Bytearray):Null<Int>;
+	public function readinto(b:Bytearray):Null<Int>;
+}

+ 27 - 2
std/python/lib/io/TextIOBase.hx

@@ -4,13 +4,38 @@ package python.lib.io;
 import python.lib.io.BufferedIOBase;
 import python.lib.io.IOBase;
 
-extern class TextIOBase extends IOBase {
+@:pythonImport("io", "TextIOBase")
+extern class TextIOBase extends IOBase implements ITextIOBase {
 
 	public var encoding:String;
+	public var error:String;
+	public var newlines:Null<haxe.EitherType<String, Tuple<String>>>;
+
+	public function detach ():BufferedIOBase;
 
 	public function write (s:String):Int;
 
 	public function read (n:Int):String;
 
 	public var buffer:BufferedIOBase;
-}
+
+}
+
+@:remove extern interface ITextIOBase extends IIOBase {
+
+	public var encoding:String;
+	public var error:String;
+	public var newlines:Null<haxe.EitherType<String, Tuple<String>>>;
+
+	public var buffer:BufferedIOBase;
+
+	public function detach ():BufferedIOBase;
+
+	public function write (s:String):Int;
+
+	public function read (n:Int):String;
+
+
+
+
+}

+ 22 - 0
std/python/lib/io/TextIOWrapper.hx

@@ -0,0 +1,22 @@
+package python.lib.io;
+
+import python.lib.io.BufferedIOBase;
+import python.lib.io.TextIOBase;
+import python.Syntax;
+import python.KwArgs;
+
+typedef TextIOWrapperOptions = {
+	?encoding : String,
+	?errors : String,
+	?newline : String,
+	?line_buffering : Bool,
+	?write_through : Bool
+};
+
+@:pythonImport("io", "TextIOWrapper")
+extern class TextIOWrapper extends TextIOBase
+{
+	public function new (buffer:BufferedIOBase, ?options:KwArgs<TextIOWrapperOptions>):Void;
+
+	public var line_buffering : Bool;
+}

+ 9 - 0
std/python/lib/io/UnsupportedOperation.hx

@@ -0,0 +1,9 @@
+package python.lib.io;
+
+import python.lib.Exceptions.IOError;
+
+class UnsupportedOperation extends IOError {
+
+
+
+}

+ 4 - 3
std/python/lib/subprocess/Popen.hx

@@ -2,6 +2,7 @@
 package python.lib.subprocess;
 
 import python.lib.io.BufferedReader;
+import python.lib.io.FileIO;
 import python.lib.io.TextIOBase;
 import python.lib.Subprocess.StartupInfo;
 import python.lib.Tuple;
@@ -65,9 +66,9 @@ extern class Popen {
 	public function poll ():Null<Int>;
 	public function terminate ():Void;
 
-	public var stdout : BufferedReader;
-	public var stderr : BufferedReader;
-	public var stdin : BufferedReader;
+	public var stdout : FileIO;
+	public var stderr : FileIO;
+	public var stdin : FileIO;
 	public var returncode : Int;
 	public var pid:Int;