Example #1
0
static int py_globals(lua_State *L)
{
	PyObject *globals;

	if (lua_gettop(L) != 0) {
		luaL_error(L, "invalid arguments");
		return 0;
	}

	globals = PyEval_GetGlobals();
	if (!globals) {
		PyObject *module = PyImport_AddModule("__main__");
		if (!module) {
			luaL_error(L, "Can't get __main__ module");
			return 0;
		}
		globals = PyModule_GetDict(module);
	}

	if (!globals) {
		PyErr_Print();
		luaL_error(L, "can't get globals");
		return 0;
	}

	return py_convert_custom(L, globals, 1);
}
static void py_asattr(lua_State *L) {
    py_object *pobj = get_py_object(L, 1);
    if (!pobj) {
        luaL_argerror(L, 1, "not a python object");
    }
    Py_DECREF(pobj->o);
    py_convert_custom(L, pobj->o, 0);
}
Example #3
0
static int py_asattr(lua_State *L)
{
    py_object *obj = (py_object*) luaL_checkudata(L, 1, POBJECT);
    if (!obj)
        return luaL_argerror(L, 1, "not a python object");

    return py_convert_custom(L, obj->o, 0);
}
Example #4
0
LUA_API int luaopen_python(lua_State *L)
{
    int rc;

    /* Register module */
    luaL_register(L, "python", py_lib);

    /* Register python object metatable */
    luaL_newmetatable(L, POBJECT);
    luaL_register(L, NULL, py_object_lib);
    lua_pop(L, 1);

    /* Initialize Lua state in Python territory */
    if (!LuaState) LuaState = L;

    /* Initialize Python interpreter */
    if (!Py_IsInitialized()) {
        PyObject *luam, *mainm, *maind;
#if PY_MAJOR_VERSION >= 3
        wchar_t *argv[] = {L"<lua>", 0};
#else
        char *argv[] = {"<lua>", 0};
#endif
        Py_SetProgramName(argv[0]);
        PyImport_AppendInittab("lua", PyInit_lua);
        Py_Initialize();
        PySys_SetArgv(1, argv);
        /* Import 'lua' automatically. */
        luam = PyImport_ImportModule("lua");
        if (!luam) {
            luaL_error(L, "Can't import lua module");
        } else {
            mainm = PyImport_AddModule("__main__");
            if (!mainm) {
                luaL_error(L, "Can't get __main__ module");
            } else {
                maind = PyModule_GetDict(mainm);
                PyDict_SetItemString(maind, "lua", luam);
                Py_DECREF(luam);
            }
        }
    }

    /* Register 'none' */
    lua_pushliteral(L, "Py_None");
    rc = py_convert_custom(L, Py_None, 0);
    if (rc) {
        lua_pushliteral(L, "none");
        lua_pushvalue(L, -2);
        lua_rawset(L, -5); /* python.none */
        lua_rawset(L, LUA_REGISTRYINDEX); /* registry.Py_None */
    } else {
        lua_pop(L, 1);
        luaL_error(L, "failed to convert none object");
    }

    return 0;
}
Example #5
0
static int py_asindx(lua_State *L)
{
	py_object *obj = (py_object*) luaL_checkudata(L, 1, POBJECT);
	if (obj)
		return py_convert_custom(L, obj->o, 1);
	else
		luaL_argerror(L, 1, "not a python object");
	return 0;

}
Example #6
0
int py_convert(lua_State *L, PyObject *o, int withnone)
{
    int ret = 0;
    if (o == Py_None) {
        if (withnone) {
            lua_pushliteral(L, "Py_None");
            lua_rawget(L, LUA_REGISTRYINDEX);
            if (lua_isnil(L, -1)) {
                lua_pop(L, 1);
                luaL_error(L, "lost none from registry");
            }
        } else {
            /* Not really needed, but this way we may check
             * for errors with ret == 0. */
            lua_pushnil(L);
            ret = 1;
        }
    } else if (o == Py_True) {
        lua_pushboolean(L, 1);
        ret = 1;
    } else if (o == Py_False) {
        lua_pushboolean(L, 0);
        ret = 1;
#if PY_MAJOR_VERSION >= 3
    } else if (PyUnicode_Check(o)) {
        Py_ssize_t len;
        char *s = PyUnicode_AsUTF8AndSize(o, &len);
#else
    } else if (PyString_Check(o)) {
        Py_ssize_t len;
        char *s;
        PyString_AsStringAndSize(o, &s, &len);
#endif
        lua_pushlstring(L, s, len);
        ret = 1;
    } else if (PyLong_Check(o)) {
        lua_pushnumber(L, (lua_Number)PyLong_AsLong(o));
        ret = 1;
    } else if (PyFloat_Check(o)) {
        lua_pushnumber(L, (lua_Number)PyFloat_AsDouble(o));
        ret = 1;
    } else if (LuaObject_Check(o)) {
        lua_rawgeti(L, LUA_REGISTRYINDEX, ((LuaObject*)o)->ref);
        ret = 1;
    } else {
        int asindx = 0;
        if (PyDict_Check(o) || PyList_Check(o) || PyTuple_Check(o))
            asindx = 1;
        ret = py_convert_custom(L, o, asindx);
        if (ret && !asindx &&
            (PyFunction_Check(o) || PyCFunction_Check(o)))
            lua_pushcclosure(L, py_asfunc_call, 1);
    }
    return ret;
}
static void py_locals(lua_State *L) {
    PyObject *locals;

    if (lua_gettop(L) != 0) {
        lua_error(L, "invalid arguments");
    }
    locals = PyEval_GetLocals();
    if (!locals) {
        py_globals(L);
        return;
    }
    py_convert_custom(L, locals, 1);
}
static void py_builtins(lua_State *L) {
    PyObject *builtins;

    if (lua_gettop(L) != 0) {
        lua_error(L, "invalid arguments");
    }

    builtins = PyEval_GetBuiltins();
    if (!builtins) {
        PyErr_Print();
        lua_error(L, "failed to get builtins");
    }
    py_convert_custom(L, builtins, 1);
}
static void py_import(lua_State *L) {
    const char *name = luaL_check_string(L, 1);
    PyObject *module;

    if (!name) luaL_argerror(L, 1, "module name expected");

    module = PyImport_ImportModule((char *) name);

    if (!module) {
        PyErr_Print();
        char *error = "failed importing \"%s\"";
        char buff[strlen(error) + strlen(name) + 1];
        sprintf(buff, error, name);
        lua_error(L, &buff[0]);
    }

    py_convert_custom(L, module, 0);
    Py_DECREF(module);
}
Example #10
0
static int py_import(lua_State *L)
{
    const char *name = luaL_checkstring(L, 1);
    PyObject *module;
    int ret;

    if (!name) {
        return luaL_argerror(L, 1, "module name expected");
    }

    module = PyImport_ImportModule((char*)name);

    if (!module) {
        PyErr_Print();
        return luaL_error(L, "failed importing '%s'", name);
    }

    ret = py_convert_custom(L, module, 0);
    Py_DECREF(module);
    return ret;
}
Example #11
0
static int py_convert(lua_State *L, PyObject *o) {
    int ret = 0;
    if (o == Py_None || o == Py_False) {
        lua_pushnil(L);
        ret = 1;
    } else if (o == Py_True) {
        lua_pushnumber(L, 1);
        ret = 1;
#if PY_MAJOR_VERSION >= 3
    } else if (PyUnicode_Check(o)) {
        Py_ssize_t len;
        char *s = PyUnicode_AsUTF8AndSize(o, &len);
#else
    } else if (PyString_Check(o)) {
        lua_pushstring(L, get_pyobject_as_string(L, o));
        ret = 1;
    } else if (PyUnicode_Check(o)) {
        char *s = get_pyobject_as_utf8string(L, o);
#endif
        lua_pushstring(L, s);
        ret = 1;
#if PY_MAJOR_VERSION < 3
    } else if (PyInt_Check(o)) {
        lua_pushnumber(L, PyInt_AsLong(o));
        ret = 1;
#endif
    } else if (PyLong_Check(o)) {
        lua_pushnumber(L, PyLong_AsLong(o));
        ret = 1;
    } else if (PyFloat_Check(o)) {
        lua_pushnumber(L, PyFloat_AsDouble(o));
        ret = 1;
    } else {
        int asindx = 0;
        if (PyList_Check(o) || PyTuple_Check(o) || PyDict_Check(o))
            asindx = 1;
        ret = py_convert_custom(L, o, asindx);
    }
    return ret;
}