authorgravatar for quae@daurnimator.comdaurnimator <quae@daurnimator.com> 2020-02-07 16:11:57+11:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-03-06 18:49:12-05:00
log8f627593ebb7ce073e43f06235fe1d9b9c1a1579
treeacc6c57ae7720c7109f079ebe4f47d5dc57300cf
parentbcf56c32eb11306613c92128c1b95ff280ba4f68
signaturelock-open Commit is signed but in an unrecognized format.

Use in_stream.readAllAlloc where sensible


4 files changed, 37 insertions(+), 38 deletions(-)

lib/std/build.zig+3-5
......@@ -926,11 +926,9 @@ pub const Builder = struct {
926926
927927 try child.spawn();
928928
929 var stdout = std.Buffer.initNull(self.allocator);
930 defer std.Buffer.deinit(&stdout);
931
932929 var stdout_file_in_stream = child.stdout.?.inStream();
933 try stdout_file_in_stream.stream.readAllBuffer(&stdout, max_output_size);
930 const stdout = try stdout_file_in_stream.stream.readAllAlloc(self.allocator, max_output_size);
931 errdefer self.allocator.free(stdout);
934932
935933 const term = try child.wait();
936934 switch (term) {
......@@ -939,7 +937,7 @@ pub const Builder = struct {
939937 out_code.* = @truncate(u8, code);
940938 return error.ExitCodeFailure;
941939 }
942 return stdout.toOwnedSlice();
940 return stdout;
943941 },
944942 .Signal, .Stopped, .Unknown => |code| {
945943 out_code.* = @truncate(u8, code);
lib/std/build/run.zig+21-14
......@@ -169,26 +169,33 @@ pub const RunStep = struct {
169169 return err;
170170 };
171171
172 var stdout = Buffer.initNull(self.builder.allocator);
173 var stderr = Buffer.initNull(self.builder.allocator);
174
175172 // TODO need to poll to read these streams to prevent a deadlock (or rely on evented I/O).
176173
174 var stdout: []const u8 = undefined;
177175 switch (self.stdout_action) {
178176 .expect_exact, .expect_matches => {
179177 var stdout_file_in_stream = child.stdout.?.inStream();
180 stdout_file_in_stream.stream.readAllBuffer(&stdout, max_stdout_size) catch unreachable;
178 stdout = stdout_file_in_stream.stream.readAllAlloc(self.builder.allocator, max_stdout_size) catch unreachable;
181179 },
182180 .inherit, .ignore => {},
183181 }
182 defer switch (self.stdout_action) {
183 .expect_exact, .expect_matches => self.builder.allocator.free(stdout),
184 .inherit, .ignore => {},
185 };
184186
185 switch (self.stdout_action) {
187 var stderr: []const u8 = undefined;
188 switch (self.stdout_behavior) {
186189 .expect_exact, .expect_matches => {
187190 var stderr_file_in_stream = child.stderr.?.inStream();
188 stderr_file_in_stream.stream.readAllBuffer(&stderr, max_stdout_size) catch unreachable;
191 stderr = stderr_file_in_stream.stream.readAllAlloc(self.builder.allocator, max_stdout_size) catch unreachable;
189192 },
190193 .inherit, .ignore => {},
191194 }
195 defer switch (self.stderr_action) {
196 .expect_exact, .expect_matches => self.builder.allocator.free(stderr),
197 .inherit, .ignore => {},
198 };
192199
193200 const term = child.wait() catch |err| {
194201 warn("Unable to spawn {}: {}\n", .{ argv[0], @errorName(err) });
......@@ -216,7 +223,7 @@ pub const RunStep = struct {
216223 switch (self.stderr_action) {
217224 .inherit, .ignore => {},
218225 .expect_exact => |expected_bytes| {
219 if (!mem.eql(u8, expected_bytes, stderr.toSliceConst())) {
226 if (!mem.eql(u8, expected_bytes, stderr)) {
220227 warn(
221228 \\
222229 \\========= Expected this stderr: =========
......@@ -224,13 +231,13 @@ pub const RunStep = struct {
224231 \\========= But found: ====================
225232 \\{}
226233 \\
227 , .{ expected_bytes, stderr.toSliceConst() });
234 , .{ expected_bytes, stderr });
228235 printCmd(cwd, argv);
229236 return error.TestFailed;
230237 }
231238 },
232239 .expect_matches => |matches| for (matches) |match| {
233 if (mem.indexOf(u8, stderr.toSliceConst(), match) == null) {
240 if (mem.indexOf(u8, stderr, match) == null) {
234241 warn(
235242 \\
236243 \\========= Expected to find in stderr: =========
......@@ -238,7 +245,7 @@ pub const RunStep = struct {
238245 \\========= But stderr does not contain it: =====
239246 \\{}
240247 \\
241 , .{ match, stderr.toSliceConst() });
248 , .{ match, stderr });
242249 printCmd(cwd, argv);
243250 return error.TestFailed;
244251 }
......@@ -248,7 +255,7 @@ pub const RunStep = struct {
248255 switch (self.stdout_action) {
249256 .inherit, .ignore => {},
250257 .expect_exact => |expected_bytes| {
251 if (!mem.eql(u8, expected_bytes, stdout.toSliceConst())) {
258 if (!mem.eql(u8, expected_bytes, stdout)) {
252259 warn(
253260 \\
254261 \\========= Expected this stdout: =========
......@@ -256,13 +263,13 @@ pub const RunStep = struct {
256263 \\========= But found: ====================
257264 \\{}
258265 \\
259 , .{ expected_bytes, stdout.toSliceConst() });
266 , .{ expected_bytes, stdout });
260267 printCmd(cwd, argv);
261268 return error.TestFailed;
262269 }
263270 },
264271 .expect_matches => |matches| for (matches) |match| {
265 if (mem.indexOf(u8, stdout.toSliceConst(), match) == null) {
272 if (mem.indexOf(u8, stdout, match) == null) {
266273 warn(
267274 \\
268275 \\========= Expected to find in stdout: =========
......@@ -270,7 +277,7 @@ pub const RunStep = struct {
270277 \\========= But stdout does not contain it: =====
271278 \\{}
272279 \\
273 , .{ match, stdout.toSliceConst() });
280 , .{ match, stdout });
274281 printCmd(cwd, argv);
275282 return error.TestFailed;
276283 }
lib/std/child_process.zig+7-9
......@@ -217,21 +217,19 @@ pub const ChildProcess = struct {
217217
218218 try child.spawn();
219219
220 var stdout = Buffer.initNull(args.allocator);
221 var stderr = Buffer.initNull(args.allocator);
222 defer Buffer.deinit(&stdout);
223 defer Buffer.deinit(&stderr);
224
225220 var stdout_file_in_stream = child.stdout.?.inStream();
226221 var stderr_file_in_stream = child.stderr.?.inStream();
227222
228 try stdout_file_in_stream.stream.readAllBuffer(&stdout, args.max_output_bytes);
229 try stderr_file_in_stream.stream.readAllBuffer(&stderr, args.max_output_bytes);
223 // TODO need to poll to read these streams to prevent a deadlock (or rely on evented I/O).
224 const stdout = try stdout_file_in_stream.stream.readAllAlloc(args.allocator, args.max_output_bytes);
225 errdefer args.allocator.free(stdout);
226 const stderr = try stderr_file_in_stream.stream.readAllAlloc(args.allocator, args.max_output_bytes);
227 errdefer args.allocator.free(stderr);
230228
231229 return ExecResult{
232230 .term = try child.wait(),
233 .stdout = stdout.toOwnedSlice(),
234 .stderr = stderr.toOwnedSlice(),
231 .stdout = stdout,
232 .stderr = stderr,
235233 };
236234 }
237235
test/tests.zig+6-10
......@@ -566,14 +566,13 @@ pub const StackTracesContext = struct {
566566 }
567567 child.spawn() catch |err| debug.panic("Unable to spawn {}: {}\n", .{ full_exe_path, @errorName(err) });
568568
569 var stdout = Buffer.initNull(b.allocator);
570 var stderr = Buffer.initNull(b.allocator);
571
572569 var stdout_file_in_stream = child.stdout.?.inStream();
573570 var stderr_file_in_stream = child.stderr.?.inStream();
574571
575 stdout_file_in_stream.stream.readAllBuffer(&stdout, max_stdout_size) catch unreachable;
576 stderr_file_in_stream.stream.readAllBuffer(&stderr, max_stdout_size) catch unreachable;
572 const stdout = stdout_file_in_stream.stream.readAllAlloc(b.allocator, max_stdout_size) catch unreachable;
573 defer b.allocator.free(stdout);
574 const stderr = stderr_file_in_stream.stream.readAllAlloc(b.allocator, max_stdout_size) catch unreachable;
575 defer b.allocator.free(stderr);
577576
578577 const term = child.wait() catch |err| {
579578 debug.panic("Unable to spawn {}: {}\n", .{ full_exe_path, @errorName(err) });
......@@ -616,11 +615,8 @@ pub const StackTracesContext = struct {
616615 const got: []const u8 = got_result: {
617616 var buf = try Buffer.initSize(b.allocator, 0);
618617 defer buf.deinit();
619 const bytes = if (stderr.endsWith("\n"))
620 stderr.toSliceConst()[0 .. stderr.len() - 1]
621 else
622 stderr.toSliceConst()[0..stderr.len()];
623 var it = mem.separate(bytes, "\n");
618 if (stderr.len != 0 and stderr[stderr.len - 1] == '\n') stderr = stderr[0 .. stderr.len - 1];
619 var it = mem.separate(stderr, "\n");
624620 process_lines: while (it.next()) |line| {
625621 if (line.len == 0) continue;
626622 const delims = [_][]const u8{ ":", ":", ":", " in " };