public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] gdb/testsuite: check the python module is available before using it
@ 2021-11-29 15:19 Andrew Burgess
  2021-11-30 12:43 ` Simon Marchi
  0 siblings, 1 reply; 3+ messages in thread
From: Andrew Burgess @ 2021-11-29 15:19 UTC (permalink / raw)
  To: gdb-patches; +Cc: Andrew Burgess

The gdb.python/py-inferior-leak.exp test makes use of the tracemalloc
module.  When running the Python tests with a GDB built against Python
2 I ran into a test failure due to the tracemalloc module not being
available.

This commit adds a new helper function to lib/gdb-python.exp that
checks if a named module is available.  Using this we can then skip
the py-inferior-leak.exp test when the tracemalloc module is not
available.
---
 gdb/testsuite/gdb.python/py-inferior-leak.exp |  6 +++++
 gdb/testsuite/lib/gdb-python.exp              | 25 +++++++++++++++++++
 2 files changed, 31 insertions(+)

diff --git a/gdb/testsuite/gdb.python/py-inferior-leak.exp b/gdb/testsuite/gdb.python/py-inferior-leak.exp
index 9cd1ebf2433..15d742391de 100644
--- a/gdb/testsuite/gdb.python/py-inferior-leak.exp
+++ b/gdb/testsuite/gdb.python/py-inferior-leak.exp
@@ -25,6 +25,12 @@ clean_restart
 # Skip all tests if Python scripting is not enabled.
 if { [skip_python_tests] } { continue }
 
+# Skip this test if the tracemalloc module is not available.
+if { ![gdb_py_module_available "tracemalloc"] } {
+    unsupported "tracemalloc module not available"
+    continue
+}
+
 set pyfile [gdb_remote_download host ${srcdir}/${subdir}/${testfile}.py]
 
 # Source the Python script, this runs the test (which is written
diff --git a/gdb/testsuite/lib/gdb-python.exp b/gdb/testsuite/lib/gdb-python.exp
index 13a1ab517f1..60931fec28a 100644
--- a/gdb/testsuite/lib/gdb-python.exp
+++ b/gdb/testsuite/lib/gdb-python.exp
@@ -51,3 +51,28 @@ proc get_python_valueof { exp default {test ""} } {
     }
     return ${val}
 }
+
+# Return true if Python module NAME is available, otherwise, return
+# false.
+
+proc gdb_py_module_available { name } {
+    set available "unknown"
+    gdb_test_multiple "python import ${name}" "" {
+	-re -wrap "ModuleNotFoundError: No module named '${name}'.*" {
+	    set available false
+	}
+	-re -wrap "ImportError: No module named ${name}.*" {
+	    set available false
+	}
+	-re -wrap "python import ${name}" {
+	    set available true
+	}
+    }
+
+    if { $available == "unknown" } {
+	perror "unexpected output from python import"
+	set available false
+    }
+
+    return ${available}
+}
-- 
2.25.4


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

* Re: [PATCH] gdb/testsuite: check the python module is available before using it
  2021-11-29 15:19 [PATCH] gdb/testsuite: check the python module is available before using it Andrew Burgess
@ 2021-11-30 12:43 ` Simon Marchi
  2021-11-30 14:56   ` Andrew Burgess
  0 siblings, 1 reply; 3+ messages in thread
From: Simon Marchi @ 2021-11-30 12:43 UTC (permalink / raw)
  To: Andrew Burgess, gdb-patches



On 2021-11-29 10:19, Andrew Burgess via Gdb-patches wrote:
> The gdb.python/py-inferior-leak.exp test makes use of the tracemalloc
> module.  When running the Python tests with a GDB built against Python
> 2 I ran into a test failure due to the tracemalloc module not being
> available.
> 
> This commit adds a new helper function to lib/gdb-python.exp that
> checks if a named module is available.  Using this we can then skip
> the py-inferior-leak.exp test when the tracemalloc module is not
> available.
> ---
>  gdb/testsuite/gdb.python/py-inferior-leak.exp |  6 +++++
>  gdb/testsuite/lib/gdb-python.exp              | 25 +++++++++++++++++++
>  2 files changed, 31 insertions(+)
> 
> diff --git a/gdb/testsuite/gdb.python/py-inferior-leak.exp b/gdb/testsuite/gdb.python/py-inferior-leak.exp
> index 9cd1ebf2433..15d742391de 100644
> --- a/gdb/testsuite/gdb.python/py-inferior-leak.exp
> +++ b/gdb/testsuite/gdb.python/py-inferior-leak.exp
> @@ -25,6 +25,12 @@ clean_restart
>  # Skip all tests if Python scripting is not enabled.
>  if { [skip_python_tests] } { continue }
>  
> +# Skip this test if the tracemalloc module is not available.
> +if { ![gdb_py_module_available "tracemalloc"] } {
> +    unsupported "tracemalloc module not available"
> +    continue
> +}
> +
>  set pyfile [gdb_remote_download host ${srcdir}/${subdir}/${testfile}.py]
>  
>  # Source the Python script, this runs the test (which is written
> diff --git a/gdb/testsuite/lib/gdb-python.exp b/gdb/testsuite/lib/gdb-python.exp
> index 13a1ab517f1..60931fec28a 100644
> --- a/gdb/testsuite/lib/gdb-python.exp
> +++ b/gdb/testsuite/lib/gdb-python.exp
> @@ -51,3 +51,28 @@ proc get_python_valueof { exp default {test ""} } {
>      }
>      return ${val}
>  }
> +
> +# Return true if Python module NAME is available, otherwise, return
> +# false.
> +
> +proc gdb_py_module_available { name } {
> +    set available "unknown"
> +    gdb_test_multiple "python import ${name}" "" {
> +	-re -wrap "ModuleNotFoundError: No module named '${name}'.*" {
> +	    set available false
> +	}
> +	-re -wrap "ImportError: No module named ${name}.*" {
> +	    set available false
> +	}
> +	-re -wrap "python import ${name}" {
> +	    set available true
> +	}
> +    }
> +
> +    if { $available == "unknown" } {
> +	perror "unexpected output from python import"
> +	set available false
> +    }
> +
> +    return ${available}
> +}
> 

LGTM.

Simon

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

* Re: [PATCH] gdb/testsuite: check the python module is available before using it
  2021-11-30 12:43 ` Simon Marchi
@ 2021-11-30 14:56   ` Andrew Burgess
  0 siblings, 0 replies; 3+ messages in thread
From: Andrew Burgess @ 2021-11-30 14:56 UTC (permalink / raw)
  To: Simon Marchi; +Cc: gdb-patches

* Simon Marchi <simon.marchi@polymtl.ca> [2021-11-30 07:43:50 -0500]:

> 
> 
> On 2021-11-29 10:19, Andrew Burgess via Gdb-patches wrote:
> > The gdb.python/py-inferior-leak.exp test makes use of the tracemalloc
> > module.  When running the Python tests with a GDB built against Python
> > 2 I ran into a test failure due to the tracemalloc module not being
> > available.
> > 
> > This commit adds a new helper function to lib/gdb-python.exp that
> > checks if a named module is available.  Using this we can then skip
> > the py-inferior-leak.exp test when the tracemalloc module is not
> > available.
> > ---
> >  gdb/testsuite/gdb.python/py-inferior-leak.exp |  6 +++++
> >  gdb/testsuite/lib/gdb-python.exp              | 25 +++++++++++++++++++
> >  2 files changed, 31 insertions(+)
> > 
> > diff --git a/gdb/testsuite/gdb.python/py-inferior-leak.exp b/gdb/testsuite/gdb.python/py-inferior-leak.exp
> > index 9cd1ebf2433..15d742391de 100644
> > --- a/gdb/testsuite/gdb.python/py-inferior-leak.exp
> > +++ b/gdb/testsuite/gdb.python/py-inferior-leak.exp
> > @@ -25,6 +25,12 @@ clean_restart
> >  # Skip all tests if Python scripting is not enabled.
> >  if { [skip_python_tests] } { continue }
> >  
> > +# Skip this test if the tracemalloc module is not available.
> > +if { ![gdb_py_module_available "tracemalloc"] } {
> > +    unsupported "tracemalloc module not available"
> > +    continue
> > +}
> > +
> >  set pyfile [gdb_remote_download host ${srcdir}/${subdir}/${testfile}.py]
> >  
> >  # Source the Python script, this runs the test (which is written
> > diff --git a/gdb/testsuite/lib/gdb-python.exp b/gdb/testsuite/lib/gdb-python.exp
> > index 13a1ab517f1..60931fec28a 100644
> > --- a/gdb/testsuite/lib/gdb-python.exp
> > +++ b/gdb/testsuite/lib/gdb-python.exp
> > @@ -51,3 +51,28 @@ proc get_python_valueof { exp default {test ""} } {
> >      }
> >      return ${val}
> >  }
> > +
> > +# Return true if Python module NAME is available, otherwise, return
> > +# false.
> > +
> > +proc gdb_py_module_available { name } {
> > +    set available "unknown"
> > +    gdb_test_multiple "python import ${name}" "" {
> > +	-re -wrap "ModuleNotFoundError: No module named '${name}'.*" {
> > +	    set available false
> > +	}
> > +	-re -wrap "ImportError: No module named ${name}.*" {
> > +	    set available false
> > +	}
> > +	-re -wrap "python import ${name}" {
> > +	    set available true
> > +	}
> > +    }
> > +
> > +    if { $available == "unknown" } {
> > +	perror "unexpected output from python import"
> > +	set available false
> > +    }
> > +
> > +    return ${available}
> > +}
> > 
> 
> LGTM.

Thanks, pushed.

Andrew


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

end of thread, other threads:[~2021-11-30 14:56 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-11-29 15:19 [PATCH] gdb/testsuite: check the python module is available before using it Andrew Burgess
2021-11-30 12:43 ` Simon Marchi
2021-11-30 14:56   ` Andrew Burgess

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).