Menu

#449 Segfault when trying to pass string to convert_state, set_scale, set_state

next release
closed-fixed
None
5
2021-03-23
2021-03-22
No

Hi Paul,

Following a discussion with Willem I am turning to you with a bug in PSRCHIVE Python interface. When trying to convert state of an archive from Coherence to Stokes with pam -s I get the correct behaviour, i.e. file is ingested, converted and written out. With the same process in python I am getting the following error on my Mac (Big Sur, Python (Homebrew version) 3.9.2, swig 4.0.2 (Homebrew), gcc 10.2.0 (Homebrew)):

m.serylak@Mac [22 Mar 2021 10:04] ~/> pam -S -e ar.S j0955.ar
j0955.ar.S written to disk
m.serylak@Mac [22 Mar 2021 10:04] ~/> ipython
Python 3.9.2 (default, Mar 15 2021, 17:37:51)
Type 'copyright', 'credits' or 'license' for more information
IPython 7.21.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: import psrchive

In [2]: f = psrchive.Archive_load('j0955.ar')

In [3]: f.convert_state('Stokes')
terminate called after throwing an instance of 'std::logic_error'
  what():  basic_string::_M_construct null not valid
Abort trap: 6

I have also tried it on my linux box (Ubuntu 18.04, python 3.6.9, swig 3.0.12-1, gcc 7.5.0):
~~~
m.serylak@Box [22 Mar 2021 10:02] ~> pam -S -e ar.S j0955.ar
j0955.ar.S written to disk
m.serylak@Box [22 Mar 2021 10:02] ~> ipython
Python 3.6.9 (default, Jan 26 2021, 15:33:00)
Type 'copyright', 'credits' or 'license' for more information
IPython 7.16.1 -- An enhanced Interactive Python. Type '?' for help.

In [1]: import psrchive

In [2]: f = psrchive.Archive_load('j0955.ar')

In [3]: f.convert_state('Stokes')
terminate called after throwing an instance of 'std::logic_error'
what(): basic_string::_M_construct null not valid
Aborted (core dumped)

Googling around did not give me clear answers. There was something about char* returning NULLs etc. (e.g. here https://stackoverflow.com/questions/56552148/how-to-fix-terminate-called-after-throwing-an-instance-of-stdlogic-error-wha). But I could not find such behaviour in the code... I am not sure what the problem could it be.

This must have changed recently as I have scripts which use convert_state and they did work well before... Not sure if this is compiler related.

Also, double checking now, it turns out that the same behaviour will happen if one tries to convert back from Stokes to Coherency. As a matter of fact it affects those as well:
`set_scale()` and `set_state()`. Looks like something to do with passing strings back to C++. But `psrchive.i` has those definitions set correctly I think:

// Convert various enums to/from string
%define %map_enum(TYPE)
%typemap(out) Signal:: ## TYPE {
$result = PyString_FromString( TYPE ## 2string($1).c_str());
}
%typemap(in) Signal:: ## TYPE {
try {
$1 = Signal::string2 ## TYPE (PyString_AsString($input));
} catch (Error &error) {
SWIG_exception(SWIG_RuntimeError,error.get_message().c_str());
}
}
%enddef
%map_enum(State)
%map_enum(Basis)
%map_enum(Scale)
%map_enum(Source)
~~~

I attach the file I used for the above examples. Hope this will give you enough info to get to the bottom of this!

Best regards,
Maciej

1 Attachments

Discussion

  • Paul Demorest

    Paul Demorest - 2021-03-22

    Hi Maciej, I can reproduce this on my system. You're right that it used to work, I'll see if I can track it down. In the meantime, if you need a workaround, you can access psrsh commands from python using execute() ie f.execute('state Stokes').

     
  • Paul Demorest

    Paul Demorest - 2021-03-22

    OK, this is a python3 "string vs bytes" issue. If you do f.convert_state(b'Stokes') that also appears to work. I'll see if I can update the wrapper to handle this automatically.

     
  • Paul Demorest

    Paul Demorest - 2021-03-22

    I think this is fixed in commit 29373ef4d. Can you give it a try?

     
  • Maciej Serylak

    Maciej Serylak - 2021-03-23

    Hi Paul, thank you for prompt reply. I tested it on both systems. Functionality restored! Let's close this one.

     
  • Maciej Serylak

    Maciej Serylak - 2021-03-23
    • Description has changed:

    Diff:

    --- old
    +++ new
    @@ -2,7 +2,6 @@
    
     Following a discussion with Willem I am turning to you with a bug in PSRCHIVE Python interface. When trying to convert state of an archive from Coherence to Stokes with `pam -s` I get the correct behaviour, i.e. file is ingested, converted and written out. With the same process in python I am getting the following error on my Mac (Big Sur, Python (Homebrew version) 3.9.2, swig 4.0.2 (Homebrew), gcc 10.2.0 (Homebrew)):
     ~~~
    -
     m.serylak@Mac [22 Mar 2021 10:04] ~/> pam -S -e ar.S j0955.ar
     j0955.ar.S written to disk
     m.serylak@Mac [22 Mar 2021 10:04] ~/> ipython
    @@ -21,9 +20,8 @@
     ~~~
    
      I have also tried it on my linux box (Ubuntu 18.04, python 3.6.9, swig 3.0.12-1, gcc 7.5.0):
    
    - 
      ~~~
    - m.serylak@Box [22 Mar 2021 10:02] ~> pam -S -e ar.S j0955.ar
    +m.serylak@Box [22 Mar 2021 10:02] ~> pam -S -e ar.S j0955.ar
     j0955.ar.S written to disk
     m.serylak@Box [22 Mar 2021 10:02] ~> ipython
     Python 3.6.9 (default, Jan 26 2021, 15:33:00)
    @@ -36,9 +34,10 @@
    
     In [3]: f.convert_state('Stokes')
     terminate called after throwing an instance of 'std::logic_error'
    
    -  what():  basic_string::_M_construct null not valid
    +  what():  basic_string::\_M\_construct null not valid
     Aborted (core dumped)
     ~~~
    +
    
     Googling around did not give me clear answers. There was something about char* returning NULLs etc. (e.g. here https://stackoverflow.com/questions/56552148/how-to-fix-terminate-called-after-throwing-an-instance-of-stdlogic-error-wha). But I could not find such behaviour in the code... I am not sure what the problem could it be.
    
    • status: open --> closed-fixed
     

Log in to post a comment.

MongoDB Logo MongoDB