Beispiel #1
0
PyObject *LuaConvert(lua_State *L, int n)
{
	PyObject *ret = NULL;

	switch (lua_type(L, n)) {

		case LUA_TNIL:
			Py_INCREF(Py_None);
			ret = Py_None;
			break;

		case LUA_TSTRING: {
			size_t len;
			const char *s = lua_tolstring(L, n, &len);
			ret = PyString_FromStringAndSize(s, len);
			break;
		}

		case LUA_TNUMBER: {
			lua_Number num = lua_tonumber(L, n);
#ifdef LUA_NUMBER_DOUBLE
			if (num != (long)num) {
				ret = PyFloat_FromDouble(n);
			} else
#endif
			{
				ret = PyInt_FromLong((long)num);
			}
			break;
		}

		case LUA_TBOOLEAN:
			if (lua_toboolean(L, n)) {
				Py_INCREF(Py_True);
				ret = Py_True;
			} else {
				Py_INCREF(Py_False);
				ret = Py_False;
			}
			break;

		case LUA_TUSERDATA: {
			py_object *obj = check_py_object(L, n);

			if (obj) {
				Py_INCREF(obj->o);
				ret = obj->o;
				break;
			}

			/* Otherwise go on and handle as custom. */
		}

		default:
			ret = LuaObject_New(n);
			break;
	}

	return ret;
}
Beispiel #2
0
PyObject *LuaConvert(lua_State *L, int n)
{
    
    PyObject *ret = NULL;

    switch (lua_type(L, n)) {

        case LUA_TNIL:
            Py_INCREF(Py_None);
            ret = Py_None;
            break;

        case LUA_TSTRING: {
            const char *s = lua_tostring(L, n);
            int len = lua_strlen(L, n);
            ret = PyUnicode_FromStringAndSize(s, len);
            break;
        }

        case LUA_TNUMBER: {
            lua_Number num = lua_tonumber(L, n);
            if (num != (long)num) {
                ret = PyFloat_FromDouble(num);
            } else {
                ret = PyLong_FromLong((long)num);
            }
            break;
        }

        case LUA_TBOOLEAN:
            if (lua_toboolean(L, n)) {
                Py_INCREF(Py_True);
                ret = Py_True;
            } else {
                Py_INCREF(Py_False);
                ret = Py_False;
            }
            break;

        case LUA_TUSERDATA: {

            // Handle torch tensor and convert them to numpy array
            #define TH_GENERIC_FILE "generic/tensorToArray.c"
            #include "THGenerateAllTypes.h"

            // If not a tensor, is it a pobject that we created in pythoninlua
            py_object *obj = luaPy_to_pobject(L, n);
            if (obj) {
                Py_INCREF(obj->o);
                ret = obj->o;
                break;
            }

            /* Otherwise go on and handle as custom. */
        }

        default:
            ret = LuaObject_New(n);
            break;
    }

    return ret;
}