Swipl used for unit tests (test_saved_states.pl)

(sent this too early, some edits below)

In the unit tests for the saved states, I have seen that the program searches for a few alternatives of the current executable.

me(Exe) :-
    current_prolog_flag(executable, WinExeOS),
    (windows stuff).

me(MeSH) :-
    absolute_file_name('swipl.sh', MeSH,
                       [ access(execute),
                         file_errors(fail)
                       ]),
    !.
me(Exe) :-
    current_prolog_flag(executable, Exe).

Is the above thing with swipl.sh some legacy code that should be removed anyway? If it is being used, I’d like to change the behavior so that the two swipls for saving the state and running the state can be set by environment variables, like this:

create_state(File, Output, Args) :-
    me_run(Run),
    me_save(Save),
    format(atom(Emulator), '--emulator=~w', [Run]),
    append(Args, ['-o', Output, Emulator, '-c', File, '-f', none], AllArgs),
    test_dir(TestDir),
    debug(save, 'Creating state in ~q using ~q ~q', [TestDir, Save, AllArgs]),
    process_create(Save, AllArgs,
                   [ cwd(TestDir),
                     stderr(pipe(Err))
                   ]),
    ...

me_save(Exe) :-
    getenv('SWIPL_SAVE', Exe0),
    !, Exe = Exe0.

me_save(Exe) :-
    me_run(Exe).

me_run(Exe) :-
    getenv('SWIPL_RUN', Exe0),
    !, Exe = Exe0.

me_run(Exe) :-
    current_prolog_flag(executable, Exe).

Any objections? The name of the environment variables is arbitrary, of course.

This solves a tiny problem with for the “save state”-mode, where swipl refuses the option --sigalert=0.

Not entirely clear. The commit messages says it is to deal with building Debian PPA version. But I think this was superseded by cmake logic that ensures that the system runs in the build environment.

That makes we wonder what is going on. The --sigalert option is only present on Unix-like systems. On Windows we use other tricks to work around blocking system calls (with varying success). Why would you need this option in the first place? And why to you need something else than the running executable as returned by the executable Prolog flag?

(just noticed that I can simplify a bit)

This refers to a linux system, not Windows. It’s again in “embedded” mode, so that something like swipl -g goal is replaced by a longer, a bit cumbersome command line

Rscript -e "rswipl::swipl()" -g goal

I created a script swipl.sh with this content

#!/bin/bash
Rscript -e "rswipl::swipl()" $*

If this happens to be in the correct folder, test_save_state.pl passes the test. Well, not entirely. For test_saved_state.pl, I need a swipl.sh with Rscript -e "rswipl::swipl()" $*. For the other tests, I need a swipl0.sh with Rscript -e "rswipl::swipl()" --sigalert=0 $*. Note that swipl does not like sigalert=0 when it is invoked to create a state.

Therefore, and given the little PPA hack above, I was wondering if the swipl executable could be customized in the unit tests (using environment variables).

Sorry for backing up a little. I’d like to keep the mess minimal … Why do we need --sigalert? Is R using SIGUSR2 for something else? Even then, it is often not a problem as SWI-Prolog installs a handler that does nothing and, if there is already a handler installed, it will call the existing handler. I think there are only issues if SWI-Prolog sends SIGUSR2 internally and R does something unwanted with it.

Even if that was the case, I don’t think the user should be requested to add --sigalert to the commandline. Is some Prolog code loaded by rswipl::swipl()? In that case, this could call prolog_alert_signal/2 to disable. That means Prolog temporarily installs a signal handler. If all is right though, that should have no impact. Alternatively, if needed, rswipl::swipl() could add the --sigalert=0 to the commandline. We can patch swipl -o out -c file ... to allow for --sigalert. Probably we should, although I do not see many sensible use cases (possibly except for this).

No problem, ideas are always welcome.

That’s indeed the case.

That’s indeed what I am doing right now.

I guess that is the easiest solution. I can try a PR.

1 Like