unofficial mirror of bug-guile@gnu.org 
 help / color / mirror / Atom feed
* bug#43364: Possible bug with output redirection
@ 2020-09-12 20:59 pinoaffe
  2020-09-13  7:13 ` tomas
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: pinoaffe @ 2020-09-12 20:59 UTC (permalink / raw)
  To: 43364

Dear guilers,

When using with-output-to-string, the output of external processes
started using system* and the like is not redirected to the temporary
port. As far as I can tell, it just redirects things written/displayed
from within guile.
This seems to be a bug, or if this is intended behaviour it might be
beneficial to document this somewhere.

As an example:
I'd expect snippet [0] to behave like snippet [1] and return "bar",
instead it just returns the empty string.

This probably affects other similar functions.

Pandemically,
pinoaffe

[0]:
(with-output-to-string
 (lambda ()
   (system* "echo" "bar")))

[1]:
(with-output-to-string
 (lambda ()
   (display "bar")))





^ permalink raw reply	[flat|nested] 6+ messages in thread

* bug#43364: Possible bug with output redirection
  2020-09-12 20:59 bug#43364: Possible bug with output redirection pinoaffe
@ 2020-09-13  7:13 ` tomas
  2023-09-08 17:53 ` bug#43364: with-output-to-port works with file ports Felix Lechner via Bug reports for GUILE, GNU's Ubiquitous Extension Language
  2024-04-23 16:32 ` Fabio Natali
  2 siblings, 0 replies; 6+ messages in thread
From: tomas @ 2020-09-13  7:13 UTC (permalink / raw)
  To: 43364

[-- Attachment #1: Type: text/plain, Size: 984 bytes --]

On Sat, Sep 12, 2020 at 10:59:23PM +0200, pinoaffe wrote:
> Dear guilers,
> 
> When using with-output-to-string, the output of external processes
> started using system* and the like is not redirected to the temporary
> port. As far as I can tell, it just redirects things written/displayed
> from within guile.
> This seems to be a bug, or if this is intended behaviour it might be
> beneficial to document this somewhere.

I think this is intentional (or rather: out of Guile's scope). While the
Guile process's output functions are the scope of `with-output-to-string',
the subprocess started with `system' just happens to inherit the standard
output file descriptor, which is an operating system mechanism.

No idea, for example, about what would happen under Windows or other
(more or less) operating systems.

But you are right that it can be a bit confusing. I'm not sure whether a
hint in the doc would be in place.

What do oters think?

Cheers
-- t

[-- Attachment #2: Digital signature --]
[-- Type: application/pgp-signature, Size: 198 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

* bug#43364: with-output-to-port works with file ports
  2020-09-12 20:59 bug#43364: Possible bug with output redirection pinoaffe
  2020-09-13  7:13 ` tomas
@ 2023-09-08 17:53 ` Felix Lechner via Bug reports for GUILE, GNU's Ubiquitous Extension Language
  2023-09-08 19:06   ` Ricardo Wurmus
  2024-04-23 16:32 ` Fabio Natali
  2 siblings, 1 reply; 6+ messages in thread
From: Felix Lechner via Bug reports for GUILE, GNU's Ubiquitous Extension Language @ 2023-09-08 17:53 UTC (permalink / raw)
  To: 43364; +Cc: pinoaffe

Hi,

In an interesting (or perhaps maddening) inconsistency,
'with-output-to-port' captures stdout from system* here

(call-with-output-file "/tmp/test.log"
  (lambda (port)
    (with-output-to-port
      port
      (lambda ()
        (system* "mktemp" "-d")))))

but 'with-output-to-string' does not do so here

(with-output-to-string
  (lambda ()
    (system* "mktemp" "-d")))

According to lloda on #guile, system* handles the redirection only
when the current ports are file ports. Thanks for that pointer!

Kind regards
Felix





^ permalink raw reply	[flat|nested] 6+ messages in thread

* bug#43364: with-output-to-port works with file ports
  2023-09-08 17:53 ` bug#43364: with-output-to-port works with file ports Felix Lechner via Bug reports for GUILE, GNU's Ubiquitous Extension Language
@ 2023-09-08 19:06   ` Ricardo Wurmus
  0 siblings, 0 replies; 6+ messages in thread
From: Ricardo Wurmus @ 2023-09-08 19:06 UTC (permalink / raw)
  To: Felix Lechner; +Cc: 43364, pinoaffe


Felix Lechner via "Bug reports for GUILE, GNU's Ubiquitous Extension Language" <bug-guile@gnu.org> writes:

> Hi,
>
> In an interesting (or perhaps maddening) inconsistency,
> 'with-output-to-port' captures stdout from system* here
>
> (call-with-output-file "/tmp/test.log"
>   (lambda (port)
>     (with-output-to-port
>       port
>       (lambda ()
>         (system* "mktemp" "-d")))))
>
> but 'with-output-to-string' does not do so here
>
> (with-output-to-string
>   (lambda ()
>     (system* "mktemp" "-d")))
>
> According to lloda on #guile, system* handles the redirection only
> when the current ports are file ports. Thanks for that pointer!

That’s correct.  I’ve been using the following monstrosity to capture
and process output.  Perhaps someone finds a prettier way?

--8<---------------cut here---------------start------------->8---
(define* (call-with-output-processor command proc #:optional capture-stderr?)
  "Silently execute COMMAND, a list of strings representing an
executable with its arguments, and apply PROC to every line printed to
standard output and, optionally when CAPTURE-STDERR? is #T, standard
error.  Return the exit status of COMMAND."
  ;; We can only capture a program's standard error by parameterizing
  ;; current-error-port to a *file* port before using system* or
  ;; open-pipe*.  The process will write its standard error stream to
  ;; the provided file descriptor.  Meanwhile we read from the file
  ;; descriptor (blocking) for new lines until the process exits.
  (match (socketpair PF_UNIX SOCK_STREAM 0)
    ((in . out)
     (let ((err (if capture-stderr?
                    (dup out)
                    (%make-void-port "w"))))
       (catch #true
         (lambda ()
           (let ((thread
                  (parameterize ((current-error-port err)
                                 (current-output-port out))
                    (call-with-new-thread
                     (lambda ()
                       (let ((status
                              (status:exit-val
                               (apply system* command))))
                         (close-port err)
                         (close-port out)
                         status))))))
             (let loop ()
               (match (read-line in 'concat)
                 ((? eof-object?)
                  (for-each
                   (lambda (port)
                     (false-if-exception (close-port port)))
                   (list err out in))
                  (join-thread thread))
                 (line
                  (proc line)
                  (loop))))))
         (lambda (key . args)
           (for-each
            (lambda (port)
              (false-if-exception (close-port port)))
            (list err out in))
           (apply throw key args)))))))
--8<---------------cut here---------------end--------------->8---


-- 
Ricardo





^ permalink raw reply	[flat|nested] 6+ messages in thread

* bug#43364: with-output-to-port works with file ports
  2020-09-12 20:59 bug#43364: Possible bug with output redirection pinoaffe
  2020-09-13  7:13 ` tomas
  2023-09-08 17:53 ` bug#43364: with-output-to-port works with file ports Felix Lechner via Bug reports for GUILE, GNU's Ubiquitous Extension Language
@ 2024-04-23 16:32 ` Fabio Natali
  2024-04-24  7:00   ` Tony Garnock-Jones
  2 siblings, 1 reply; 6+ messages in thread
From: Fabio Natali @ 2024-04-23 16:32 UTC (permalink / raw)
  To: 43364

Dear All 👋,

I've been encountering a few times the need for a procedure like
'system' or 'system*' but that might be capable of capturing the output
of the program that's being called.

While this is something that can be solved with a simple 'open-pipe*'
wrapper, the pattern seems relatively frequent in Guix, which is where I
noticed it. I suggested that some variant of such a wrapper could be
added to '(guix build utils)'⁰.

In reply to my post, it was brought to my attention that the problem
might be addressed at the Guile level and pointed me to this bug report.

I thought of sending a quick follow-up here to see if there's any rough
consensus on a possible way of addressing this - or why it might be
difficult or potentially not worth the effort.

Thanks, all best, Fabio.

⁰ https://lists.gnu.org/archive/html/guix-devel/2024-04/msg00199.html


-- 
Fabio Natali
https://fabionatali.com





^ permalink raw reply	[flat|nested] 6+ messages in thread

* bug#43364: with-output-to-port works with file ports
  2024-04-23 16:32 ` Fabio Natali
@ 2024-04-24  7:00   ` Tony Garnock-Jones
  0 siblings, 0 replies; 6+ messages in thread
From: Tony Garnock-Jones @ 2024-04-24  7:00 UTC (permalink / raw)
  To: Fabio Natali, 43364

This is a nice idea. The way Racket does it is to make 
with-output-to-string cooperate with system:

     (with-output-to-string (lambda () (system "date")))

but a quick check shows that Guile does not yet have such cooperation in 
place. Would that be a good place to start looking?

Tony


On 23/04/2024 18:32, Fabio Natali wrote:
> Dear All 👋,
> 
> I've been encountering a few times the need for a procedure like
> 'system' or 'system*' but that might be capable of capturing the output
> of the program that's being called.
> 
> While this is something that can be solved with a simple 'open-pipe*'
> wrapper, the pattern seems relatively frequent in Guix, which is where I
> noticed it. I suggested that some variant of such a wrapper could be
> added to '(guix build utils)'⁰.
> 
> In reply to my post, it was brought to my attention that the problem
> might be addressed at the Guile level and pointed me to this bug report.
> 
> I thought of sending a quick follow-up here to see if there's any rough
> consensus on a possible way of addressing this - or why it might be
> difficult or potentially not worth the effort.
> 
> Thanks, all best, Fabio.
> 
> ⁰ https://lists.gnu.org/archive/html/guix-devel/2024-04/msg00199.html
> 
> 





^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2024-04-24  7:00 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-09-12 20:59 bug#43364: Possible bug with output redirection pinoaffe
2020-09-13  7:13 ` tomas
2023-09-08 17:53 ` bug#43364: with-output-to-port works with file ports Felix Lechner via Bug reports for GUILE, GNU's Ubiquitous Extension Language
2023-09-08 19:06   ` Ricardo Wurmus
2024-04-23 16:32 ` Fabio Natali
2024-04-24  7:00   ` Tony Garnock-Jones

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).