From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Thomas White Newsgroups: gmane.lisp.guile.bugs Subject: bug#55568: Error in string->number when calling (backtrace) without COLUMNS envionment variable Date: Sun, 22 May 2022 01:04:44 +0200 Message-ID: <20220522010444.6bea02a1@dorian> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="28320"; mail-complaints-to="usenet@ciao.gmane.io" To: 55568@debbugs.gnu.org Original-X-From: bug-guile-bounces+guile-bugs=m.gmane-mx.org@gnu.org Sun May 22 01:36:38 2022 Return-path: Envelope-to: guile-bugs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nsYeE-0007Bu-9E for guile-bugs@m.gmane-mx.org; Sun, 22 May 2022 01:36:38 +0200 Original-Received: from localhost ([::1]:49960 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nsYeC-0003cZ-Tl for guile-bugs@m.gmane-mx.org; Sat, 21 May 2022 19:36:36 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:47030) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nsYde-0003bo-P4 for bug-guile@gnu.org; Sat, 21 May 2022 19:36:02 -0400 Original-Received: from debbugs.gnu.org ([209.51.188.43]:49267) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1nsYde-0006Yr-GG for bug-guile@gnu.org; Sat, 21 May 2022 19:36:02 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1nsYde-0000uf-EH for bug-guile@gnu.org; Sat, 21 May 2022 19:36:02 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Thomas White Original-Sender: "Debbugs-submit" Resent-CC: bug-guile@gnu.org Resent-Date: Sat, 21 May 2022 23:36:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: report 55568 X-GNU-PR-Package: guile X-Debbugs-Original-To: bug-guile@gnu.org Original-Received: via spool by submit@debbugs.gnu.org id=B.16531761243440 (code B ref -1); Sat, 21 May 2022 23:36:02 +0000 Original-Received: (at submit) by debbugs.gnu.org; 21 May 2022 23:35:24 +0000 Original-Received: from localhost ([127.0.0.1]:43160 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1nsYd0-0000tN-Di for submit@debbugs.gnu.org; Sat, 21 May 2022 19:35:24 -0400 Original-Received: from lists.gnu.org ([209.51.188.17]:43758) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1nsY9g-0000Ai-3L for submit@debbugs.gnu.org; Sat, 21 May 2022 19:05:05 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:44490) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nsY9f-0005Ae-Se for bug-guile@gnu.org; Sat, 21 May 2022 19:05:03 -0400 Original-Received: from mx2.mythic-beasts.com ([2a00:1098:0:82:1000:0:2:1]:35461) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nsY9d-0002FW-NF for bug-guile@gnu.org; Sat, 21 May 2022 19:05:03 -0400 Original-Received: from [84.62.107.143] (port=40964 helo=dorian) by balrog.mythic-beasts.com with esmtpsa (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92.3) (envelope-from ) id 1nsY9U-0006mh-RS for bug-guile@gnu.org; Sun, 22 May 2022 00:04:57 +0100 X-Mailer: Claws Mail 4.0.0git382 (GTK+ 3.24.31; x86_64-pc-linux-gnu) X-BlackCat-Spam-Score: 4 Received-SPF: pass client-ip=2a00:1098:0:82:1000:0:2:1; envelope-from=taw-gl@bitwiz.me.uk; helo=mx2.mythic-beasts.com X-Spam_score_int: -41 X-Spam_score: -4.2 X-Spam_bar: ---- X-Spam_report: (-4.2 / 5.0 requ) BAYES_00=-1.9, RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-Mailman-Approved-At: Sat, 21 May 2022 19:35:21 -0400 X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: bug-guile@gnu.org List-Id: "Bug reports for GUILE, GNU's Ubiquitous Extension Language" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-guile-bounces+guile-bugs=m.gmane-mx.org@gnu.org Original-Sender: "bug-guile" Xref: news.gmane.io gmane.lisp.guile.bugs:10282 Archived-At: Summary: This appears to be a combination of two separate problems: 1. Procedure terminal-width in system/repl/debug.scm does not gracefully handle a #f return value from (getenv "COLUMNS"). 2. The COLUMNS environment variable is only set when readline is active, which never happens if a program is run directly via a command line argument. Relates to https://debbugs.gnu.org/cgi/bugreport.cgi?bug=3D36677 ("Don't truncate backtraces"). Detailed report: I want to set up an exception handler to get a backtrace when something goes wrong in my program. The simple test code below is based on the example code found in https://www.gnu.org/software/guile/manual/html_node/Pre_002dUnwind-Debuggin= g.html The output on Guile 3.0.7 is below. Instead of a backtrace, the call to the backtrace routine produces the following error: system/repl/debug.scm:72:40: In procedure string->number: Wrong type argument in position 1 (expecting string): #f I've also tested on the latest Git version (3.0.8.13-cc455), with the same result. The line referred to in debug.scm (shown below) is all about determining the width of the terminal based on the environment variable COLUMNS. When run as shown in the example below, this variable is not set, causing string->number to fail. This is the first problem, which probably has a simple fix for someone who properly understands this routine. I might have time to look at this myself in a few weeks' time. (define terminal-width (let ((set-width (make-fluid))) (case-lambda (() (or (fluid-ref set-width) (let ((w (false-if-exception (string->number (getenv "COLUMNS"))= ))) (and (integer? w) (exact? w) (> w 0) w)) 72)) ((w) (if (or (not w) (and (integer? w) (exact? w) (> w 0))) (fluid-set! set-width w) (error "Expected a column number (a positive integer)" w)))))) The second problem is the question of why COLUMNS is not set in the first place. The problem goes away after uncommenting the (setenv .. ) lin= e in the test case code, forcing it to be set when the backtrace is generated. I think the problem is that COLUMNS is a shell variable, which is not the same thing as an envionment variable. Read here: https://stackoverflow.com/questions/1780483/lines-and-columns-environmental= -variables-lost-in-a-script In a shell, COLUMNS is usually (not always!) available. But because it's not an environment variable, it doesn't get exported to Guile. Within Guile, COLUMNS is only set after activate-readline is called. Therefore, we get proper backtraces when the program is run from an already-running REPL, like this: $ guile scheme@(guile-user)> (load "testcase.scm") ---> correct backtrace But not when running the program directly, like this: $ guile testcase.scm ---> string->number error instead of backtrace This appears to be confirmed by testing with various combinations of "guile -l" and adding/removing readline references from my ~/.guile file. Fixing the first problem will work around the overall problem in a reasonably acceptable way. We would only have backtraces with the correct width if readline is activated. To get proper width backtraces in all circumstances would require a different way of getting the terminal width. With thanks to stis on IRC this evening, who first realised what was going on. Test case code: (use-modules (ice-9 threads) (ice-9 exceptions) (ice-9 control)) ;; Uncomment the following line to work round the problem ;; (setenv "COLUMNS" "110")=20 (define (do-test) (raise-exception (make-exception (make-exception-with-message "Test exception") (make-exception-with-irritants (list 1 2 3 4 5))))) (define (call-with-backtrace thunk) (let/ec cancel (start-stack 'stack-with-backtrace (with-exception-handler (lambda (exn) (display "Error occurred\n") (backtrace) (cancel #f)) thunk)))) (define (exception-test) (call-with-backtrace (lambda () (sleep 1) (do-test)))) (exception-test) Test case output: [23:06] ~ $ guile exception-test.scm=20 ;;; note: auto-compilation is enabled, set GUILE_AUTO_COMPILE=3D0 ;;; or pass the --no-auto-compile argument to disable. ;;; compiling /home/taw/exception-test.scm ;;; compiled /home/taw/.cache/guile/ccache/3.0-LE-8-4.5/home/taw/exception-= test.scm.go Error occurred Backtrace: Backtrace: In ice-9/boot-9.scm: 1752:10 7 (with-exception-handler _ _ #:unwind? _ # _) 1685:16 6 (raise-exception _ #:continuable? _) In /home/taw/exception-test.scm: 18:27 5 (_ _) In unknown file: 4 (backtrace #) In system/repl/debug.scm: 148:36 3 (print-frames #(# # = =E2=80=A6) =E2=80=A6) 72:20 2 (_) In ice-9/boot-9.scm: 1747:15 1 (with-exception-handler # =E2=80=A6) In system/repl/debug.scm: 72:40 0 (_) system/repl/debug.scm:72:40: In procedure string->number: Wrong type argume= nt in position 1 (expecting string): #f [23:07] ~ $