public StringWrapperLineReader(StringWrapper strw) { Strw = strw; en = strw.Lines().GetEnumerator(); has_next = en.MoveNext(); }
public StringWrapper GetNext() { var res = new StringWrapper() { str = en.Current }; has_next = en.MoveNext(); return(res); }
/// <summary> /// exec given code through a temp file /// </summary> public StringWrapper Exec(StringWrapper code, bool remove_tmp_file = true) { string tmp_pathfilename = null; if (TempFolder == null) { tmp_pathfilename = System.IO.Path.GetTempFileName() + ".py"; } else { tmp_pathfilename = System.IO.Path.Combine(TempFolder, "_" + Guid.NewGuid().ToString() + ".py"); } guid = Guid.NewGuid().ToString(); using (var sw0 = new StreamWriter(tmp_pathfilename)) { switch (Environment.OSVersion.Platform) { case PlatformID.Unix: case PlatformID.MacOSX: { sw0.WriteLine(code.str.Replace("\r\n", "\n")); } break; default: { sw0.WriteLine(code.str); } break; } sw0.WriteLine($"print('{guid}')"); } sberr.Clear(); sbout.Clear(); var sw = new Stopwatch(); sw.Start(); string res = ""; while (!initialized) { if (startup_error) { throw new Exception($"startup error [{sberr.ToString()}]"); } Thread.Sleep(25); } lock (wrapper_initialized) { finished = false; hasErr = false; //process.BeginErrorReadLine(); //process.BeginOutputReadLine(); var cmd = $"exec(open('{tmp_pathfilename.Replace('\\', '/')}').read())\n"; debug?.Invoke($"using process id = [{process.Id}]"); process.StandardInput.WriteLine(cmd); process.StandardInput.Flush(); while (!finished) { //process.StandardInput.Flush(); Thread.Sleep(25); if (hasErr) { Thread.Sleep(25); // gather other errors break; } } //process.CancelErrorRead(); //process.CancelOutputRead(); if (hasErr) { throw new PythonException($"pyhton[{PythonExePathfilename}] script[{tmp_pathfilename}] : {sberr.ToString()}", sbout.ToString()); } res = sbout.ToString(); } sw.Stop(); debug?.Invoke($"python took [{sw.Elapsed}]"); if (remove_tmp_file) { File.Delete(tmp_pathfilename); } return(new StringWrapper() { str = res }); }