Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Raise error("interrupted!") when torch-qlua is interrupted by ctrl-C. #109

Closed
wants to merge 3 commits into from

1 participant

@fidlej

I modified torch-qlua to react to ctrl-C like torch-lua.

Example usage:

$ torch-qlua -e 'while true do os.clock() end'
^Ctorch-qlua: interrupted!
stack traceback:
    [C]: ?
    [C]: in function 'clock'
    [string "while true do os.clock() end"]:1: in main chunk
@fidlej

I unintentionally split the change to multiple commits.
I will try to clean up my branch.

@fidlej fidlej closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
Showing with 30 additions and 1 deletion.
  1. +30 −1 exe/qtlua/qlua/qluaapplication.cpp
View
31 exe/qtlua/qlua/qluaapplication.cpp
@@ -11,6 +11,9 @@
#if HAVE_UNISTD_H
# include <unistd.h>
#endif
+#if HAVE_SIGNAL_H
+# include <signal.h>
+#endif
#include <QApplication>
#include <QDateTime>
@@ -42,7 +45,27 @@
#include "qluaconsole.h"
+static lua_State *globalL = NULL;
+#ifdef HAVE_SIGNAL_H
+// Handling SIGINT the same way as in luajit.c.
+static void lstop(lua_State *L, lua_Debug *ar)
+{
+ (void)ar; /* unused arg. */
+ lua_sethook(L, NULL, 0, 0);
+ /* Avoid luaL_error -- a C hook doesn't add an extra frame. */
+ luaL_where(L, 0);
+ lua_pushfstring(L, "%sinterrupted!", lua_tostring(L, -1));
+ lua_error(L);
+}
+
+static void laction(int i)
+{
+ signal(i, SIG_DFL); /* if another SIGINT happens before lstop,
+ terminate process (default action) */
+ lua_sethook(globalL, lstop, LUA_MASKCALL | LUA_MASKRET | LUA_MASKCOUNT, 1);
+}
+#endif // HAVE_SIGNAL_H
// ------- private class
@@ -359,7 +382,6 @@ QLuaApplication::Private::printBadOption(const char *option)
}
-
int
QLuaApplication::Private::doCall(struct lua_State *L, int nargs)
{
@@ -367,7 +389,13 @@ QLuaApplication::Private::doCall(struct lua_State *L, int nargs)
int base = lua_gettop(L) - nargs;
lua_pushcfunction(L, luaQ_traceback);
lua_insert(L, base);
+#if HAVE_SIGNAL_H
+ signal(SIGINT, laction);
+#endif
status = luaQ_pcall(L, nargs, 0, base, theEngine);
+#if HAVE_SIGNAL_H
+ signal(SIGINT, SIG_DFL);
+#endif
lua_remove(L, base);
if (status)
lua_gc(L, LUA_GCCOLLECT, 0);
@@ -467,6 +495,7 @@ QLuaApplication::Private::processArguments(int argc, char **argv)
// Obtain and lock lua
QtLuaLocker lua(theEngine);
+ globalL = lua;
// Good time to limit access to QtLuaConsole
lua_pushcfunction(lua, hook_qluaconsole);
Something went wrong with that request. Please try again.