Giter Club home page Giter Club logo

lupa's Introduction

Hi there, I'm Stefan πŸ‘‹

  • πŸ§‘πŸ½β€πŸ’» I am the maintainer of Cython, lxml and a few other Python data processing tools that you can find below.
  • πŸ§‘πŸ½β€πŸ’» I am also a core developer of CPython and contributor to many other open source software projects.
  • 🀝 In my city there's an old house from the 1600s with an inscription: "May God give to all those who know me, twice what they feel that they owe me" (*). With a donation, YOU can help me live the life that you think I deserve for the work that I do on Cython and on lxml, or my other projects.
  • πŸ€” You can contact me at [email protected]
  • πŸ˜„ My pronouns are he/him
  • πŸ—£οΈ I speak and πŸ–‹οΈ write in Deutsch, English, FranΓ§ais

(*) original German inscription: "Gott gebe allen, die mich kennen, doppelt so viel, als sie mir gΓΆnnen"

lupa's People

Contributors

aaiyer avatar amotl avatar campadrenalin avatar chekunkov avatar fried avatar gdude2002 avatar guidanoli avatar jayvdb avatar jbinary avatar jmarianer avatar kmike avatar le0developer avatar moreati avatar oddstr13 avatar rakiru avatar riconnon avatar ruihe774 avatar russelldavis avatar scoder avatar siberianfox avatar synodriver avatar wheybags avatar xxyzz avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

lupa's Issues

Lupa running against wrong version of lua

I have lua5.1 and lua5.3 installed on my system. I believe I have installed lupa against lua5.1. Running setup.py install outputs pkg-config found lua5.1 version 5.1.5. However, when I try to use lupa, it appears to be using lua5.3 still: lua.globals()._VERSION outputs 'Lua 5.3'. Any ideas on how to get it to use 5.1?

LuaJIT 5.1 crashed(Segmentation fault) during out-of-range indexing on a _LuaObject

import lupa
lua = lupa.LuaRuntime()
lua.require('torch')
lg = lua.globals()
a = lg.torch.Tensor(2)
print(a[5]) # any out of range indexing will cause the core to crash.

the output is:

Segmentation fault (core dumped)

And then it quit the entire python process immediately, but in torch itself, the error can be captured:

bad argument #2 to '?' (out of range at /tmp/luarocks_torch-scm-1-6180/torch7/generic/Tensor.c:888)                                
stack traceback:                                                                                                                   
        [C]: at 0x7f67699b60b0                                                                                                     
        [C]: in function '__index'                                                                                                 
        [string "_RESULT={a[0]}"]:1: in main chunk                                                                                 
        [C]: in function 'xpcall'                                                                                                  
        /home/xxx/torch/install/share/lua/5.1/trepl/init.lua:651: in function 'repl'                                               
        .../wei/torch/install/lib/luarocks/rocks/trepl/scm-1/bin/th:199: in main chunk                                             
        [C]: at 0x00406670 

Any idea?

lua.table keyword arguments are misleading when runtime encoding is None

When runtime encoding is None, lua.table(foo=132) creates a Lua table with userdata as a key. There is a workaround (use lua.table_from which supports dicts with binary keys). But I think it makes more sense to use Lua strings for keys for tables created by lua.table, even if the encoding is None. I expect this to work even if encoding is None:

from lupa import LuaRuntime
lua = LuaRuntime(encoding=None)
tbl = lua.table(x=1)

# this should print 1, but it prints 'nil'
lua.eval("function(tbl) print(type(tbl.x)) end")(tbl) 

# this prints
# userdata  number
lua.eval("function(tbl) for k,v in pairs(tbl) do print(type(k),type(v)) end end")(tbl)

Interesting bit in readme

you may also happily assume it's just an amalgamation of the phonetic sounds that start the words "Lua" and "Python", two from each to keep the balance

What's the 2nd letter coming from "Python"? L, u and a from Lupa are all only present in the word Lua. Anyhow, nice name and project!

Compilation and installation works but importing from Python seems to be broken

Hi there,

I've tried to build and install lupa on my Windows 7 64 repeatedly. The process seems to be successful, but at the time of using the installed library, Python claims:

Python 2.7.2 (default, Jun 12 2011, 14:24:46) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.

import lupa
Traceback (most recent call last):
File "", line 1, in
File "lupa__init__.py", line 31, in
from lupa._lupa import *
ImportError: No module named _lupa

The folder C:\Python27\Lib\site-packages\lupa seems to contain the correct stuff:

28/03/2013 15:25

.
28/03/2013 15:25 ..
28/03/2013 15:25 22 version.py
28/03/2013 15:25 163 version.pyc
28/03/2013 15:25 119,808 _lupa.pyd
28/03/2013 15:24 837 init.py
28/03/2013 15:25 820 init.pyc
5 File(s) 121,650 bytes

The building process seems to be successful, although with a few warnings:

C:\temp\lupa-0.20>setup.py install
found LuaJIT build in C:\temp\lupa-0.20\luajit-2.0\src
building statically
generated sources not available, need Cython to build
building with Cython 0.18
Compiling lupa/lupa.pyx because it changed.
Cythonizing lupa/lupa.pyx
running install
running build
running build_py
creating build
creating build\lib.win-amd64-2.7
creating build\lib.win-amd64-2.7\lupa
copying lupa\version.py -> build\lib.win-amd64-2.7\lupa
copying lupa__init
.py -> build\lib.win-amd64-2.7\lupa
running build_ext
building 'lupa._lupa' extension
creating build\temp.win-amd64-2.7
creating build\temp.win-amd64-2.7\Release
creating build\temp.win-amd64-2.7\Release\lupa
C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\BIN\amd64\cl.exe /c /nologo /Ox /MD /W3 /GS- /DNDEBUG -IC:\temp\lu
pa-0.20\luajit-2.0\src -IC:\Python27\include -IC:\Python27\PC /Tclupa/lupa.c /Fobuild\temp.win-amd64-2.7\Release\lupa/
lupa.obj
_lupa.c
lupa/_lupa.c(812) : warning C4244: '=' : conversion from 'Py_ssize_t' to 'int', possible loss of data
lupa/_lupa.c(825) : warning C4244: 'return' : conversion from 'Py_ssize_t' to 'int', possible loss of data
lupa/_lupa.c(846) : warning C4244: 'return' : conversion from 'Py_ssize_t' to 'int', possible loss of data
lupa/_lupa.c(3730) : warning C4244: 'function' : conversion from 'Py_ssize_t' to 'int', possible loss of data
lupa/_lupa.c(3730) : warning C4244: 'function' : conversion from 'Py_ssize_t' to 'int', possible loss of data
lupa/_lupa.c(8671) : warning C4244: '=' : conversion from 'Py_ssize_t' to 'int', possible loss of data
lupa/lupa.c(12090) : warning C4244: 'function' : conversion from 'Py_ssize_t' to 'int', possible loss of data
lupa/lupa.c(15984) : warning C4244: '=' : conversion from 'Py_ssize_t' to 'int', possible loss of data
C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\BIN\amd64\link.exe /DLL /nologo /INCREMENTAL:NO /LIBPATH:C:\Python
27\libs /LIBPATH:C:\Python27\PCbuild\amd64 /EXPORT:init_lupa build\temp.win-amd64-2.7\Release\lupa/lupa.obj C:\temp\lup
a-0.20\luajit-2.0\src\lua51.lib /OUT:build\lib.win-amd64-2.7\lupa_lupa.pyd /IMPLIB:build\temp.win-amd64-2.7\Release\lup
a_lupa.lib /MANIFESTFILE:build\temp.win-amd64-2.7\Release\lupa_lupa.pyd.manifest
lupa.obj : warning LNK4197: export 'init_lupa' specified multiple times; using first specification
Creating library build\temp.win-amd64-2.7\Release\lupa_lupa.lib and object build\temp.win-amd64-2.7\Release\lupa_lu
pa.exp
C:\Program Files\Microsoft SDKs\Windows\v7.0\bin\x64\mt.exe -nologo -manifest build\temp.win-amd64-2.7\Release\lupa_lup
a.pyd.manifest -outputresource:build\lib.win-amd64-2.7\lupa_lupa.pyd;2
running install_lib
copying build\lib.win-amd64-2.7\lupa\version.py -> C:\Python27\Lib\site-packages\lupa
copying build\lib.win-amd64-2.7\lupa_lupa.pyd -> C:\Python27\Lib\site-packages\lupa
copying build\lib.win-amd64-2.7\lupa__init
.py -> C:\Python27\Lib\site-packages\lupa
byte-compiling C:\Python27\Lib\site-packages\lupa\version.py to version.pyc
byte-compiling C:\Python27\Lib\site-packages\lupa__init
.py to init.pyc
running install_egg_info
Removing C:\Python27\Lib\site-packages\lupa-0.20-py2.7.egg-info
Writing C:\Python27\Lib\site-packages\lupa-0.20-py2.7.egg-info

Could this be an issue? I've taken the latest LuaJIT and lupa from the git repositories.

Experimental branch that fetches and builds luajit

The biggest struggle with actually using Lupa in any kind of production environment, in my experience, has been platform-specific breakage due to the variable nature of the install process. We do probably always want flexibility in the mainline branch, but for the sake of stability, predictability and testing validity, I'm gonna make a less-flexible branch that handles its own acquisition of luajit.

provide a way to do insinstance() checks

Hey,

Is there a good way to check if a result of some Lua computation is _LuaTable / _LuaFunction / _LuaObject / ...?

Currently I'm writing code like this:

import lupa
lua = lupa.LuaRuntime()
_LuaTable = lua.eval("{}").__class__ 

# ...
obj = lua.globals()["myobj"]
if insinstance(obj, _LuaTable):
    # ...

What do you think about removing cython.internal declarations around these classes?

Socket.core Linking Error

Hi,

I am attempting to add load the socket library in a Lupa runtime.

I installed socket with Luarocks, so I needed to append the necessary paths.

>>> import lupa
>>> sys.setdlopenflags(orig_dlflags)
>>> lua = lupa.LuaRuntime()
>>> lua.execute("package.cpath = package.cpath .. ';/root/.luarocks/lib/lua/5.3/?.so;/usr/local/lib/lua/5.3/?.so;/usr/local/lib/lua/5.3/loadall.so;./?.so'")
>>> lua.execute("  package.path = package.path .. ';/root/.luarocks/share/lua/5.3/?.lua;/root/.luarocks/share/lua/5.3/?/init.lua;/usr/local/share/lua/5.3/?.lua;/usr/local/share/lua/5.3/?/init.lua;/usr/local/lib/lua/5.3/?.lua;/usr/local/lib/lua/5.3/?/init.lua;./?.lua;./?/init.lua'")
>>> lua.require("socket")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "lupa/_lupa.pyx", line 268, in lupa._lupa.LuaRuntime.require (lupa/_lupa.c:4683)
  File "lupa/_lupa.pyx", line 1245, in lupa._lupa.call_lua (lupa/_lupa.c:17287)
  File "lupa/_lupa.pyx", line 1254, in lupa._lupa.execute_lua_call (lupa/_lupa.c:17400)
  File "lupa/_lupa.pyx", line 1207, in lupa._lupa.raise_lua_error (lupa/_lupa.c:16746)
lupa._lupa.LuaError: error loading module 'socket.core' from file '/usr/local/lib/lua/5.3/socket/core.so':
    /usr/local/lib/lua/5.3/socket/core.so: undefined symbol: lua_rotate

A similar issue is mentioned here but the proposed solution does not seem to work for me (I am running Ubuntu):

http://stackoverflow.com/questions/8361437/linker-error-lunatic-python-lua-requiresocket-undefined-symbol-lua-getme

From my understanding, there is a linking problem to liblua5.3.so?

Any help would be greatly appreciated.

Thanks,
Alex

Error when importing lupa

Hi, I installed lupa as explained in INSTALL.rst and it seemed to work. The output of python setup.py install is the following:

found LuaJIT build in /afs/inf.ed.ac.uk/user/s13/s1333293/lupa-1.2/LuaJIT-2.0.4/src
building statically
building without Cython
running install
running bdist_egg
running egg_info
writing lupa.egg-info/PKG-INFO
writing top-level names to lupa.egg-info/top_level.txt
writing dependency_links to lupa.egg-info/dependency_links.txt
reading manifest file 'lupa.egg-info/SOURCES.txt'
reading manifest template 'MANIFEST.in'
writing manifest file 'lupa.egg-info/SOURCES.txt'
installing library code to build/bdist.linux-x86_64/egg
running install_lib
running build_py
creating build
creating build/lib.linux-x86_64-2.6
creating build/lib.linux-x86_64-2.6/lupa
copying lupa/version.py -> build/lib.linux-x86_64-2.6/lupa
copying lupa/__init__.py -> build/lib.linux-x86_64-2.6/lupa
running build_ext
building 'lupa._lupa' extension
creating build/temp.linux-x86_64-2.6
creating build/temp.linux-x86_64-2.6/lupa
gcc -pthread -fno-strict-aliasing -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic -D_GNU_SOURCE -fPIC -fwrapv -DNDEBUG -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic -D_GNU_SOURCE -fPIC -fwrapv -fPIC -DLUA_COMPAT_ALL -I/afs/inf.ed.ac.uk/user/s13/s1333293/lupa-1.2/LuaJIT-2.0.4/src -I/usr/include/python2.6 -c lupa/_lupa.c -o build/temp.linux-x86_64-2.6/lupa/_lupa.o
gcc -pthread -shared build/temp.linux-x86_64-2.6/lupa/_lupa.o /afs/inf.ed.ac.uk/user/s13/s1333293/lupa-1.2/LuaJIT-2.0.4/src/libluajit.a -L/usr/lib64 -lpython2.6 -o build/lib.linux-x86_64-2.6/lupa/_lupa.so
creating build/bdist.linux-x86_64
creating build/bdist.linux-x86_64/egg
creating build/bdist.linux-x86_64/egg/lupa
copying build/lib.linux-x86_64-2.6/lupa/version.py -> build/bdist.linux-x86_64/egg/lupa
copying build/lib.linux-x86_64-2.6/lupa/__init__.py -> build/bdist.linux-x86_64/egg/lupa
copying build/lib.linux-x86_64-2.6/lupa/_lupa.so -> build/bdist.linux-x86_64/egg/lupa
byte-compiling build/bdist.linux-x86_64/egg/lupa/version.py to version.pyc
byte-compiling build/bdist.linux-x86_64/egg/lupa/__init__.py to __init__.pyc
creating stub loader for lupa/_lupa.so
byte-compiling build/bdist.linux-x86_64/egg/lupa/_lupa.py to _lupa.pyc
creating build/bdist.linux-x86_64/egg/EGG-INFO
copying lupa.egg-info/PKG-INFO -> build/bdist.linux-x86_64/egg/EGG-INFO
copying lupa.egg-info/SOURCES.txt -> build/bdist.linux-x86_64/egg/EGG-INFO
copying lupa.egg-info/dependency_links.txt -> build/bdist.linux-x86_64/egg/EGG-INFO
copying lupa.egg-info/not-zip-safe -> build/bdist.linux-x86_64/egg/EGG-INFO
copying lupa.egg-info/top_level.txt -> build/bdist.linux-x86_64/egg/EGG-INFO
writing build/bdist.linux-x86_64/egg/EGG-INFO/native_libs.txt
creating dist
creating 'dist/lupa-1.2-py2.6-linux-x86_64.egg' and adding 'build/bdist.linux-x86_64/egg' to it
removing 'build/bdist.linux-x86_64/egg' (and everything under it)
Processing lupa-1.2-py2.6-linux-x86_64.egg
creating /afs/inf.ed.ac.uk/user/s13/s1333293/virt_env/virt1/lib/python2.6/site-packages/lupa-1.2-py2.6-linux-x86_64.egg
Extracting lupa-1.2-py2.6-linux-x86_64.egg to /afs/inf.ed.ac.uk/user/s13/s1333293/virt_env/virt1/lib/python2.6/site-packages
Adding lupa 1.2 to easy-install.pth file

Installed /afs/inf.ed.ac.uk/user/s13/s1333293/virt_env/virt1/lib/python2.6/site-packages/lupa-1.2-py2.6-linux-x86_64.egg
Processing dependencies for lupa==1.2
Finished processing dependencies for lupa==1.2

However, when I tried to import lupa I got the error:

>>> import lupa
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "lupa/__init__.py", line 31, in <module>
    from lupa._lupa import *
ImportError: No module named _lupa

I've seen two similar issues on this but they didn't help. The funny thing is that I actually succeeded in installing using lupa a few days ago but I uninstalled it by mistake and now it doesn't seem to work anymore. Any ideas?

Marco

Bug with hexchat.

So I was trying to use this with hexchat and this happened:

 >>> import lupa; lua = lupa.LuaRuntime(unpack_returned_tuples=False, encoding=None)
 >>> exec("def x(*args):\n hexchat.prnt(*([x.decode(\"utf-8\") for x in args]))\n\nlua.globals().print = x",globals(),locals())
 >>> lua.eval("print('\\xC2\\xA7')")
 Β§
 >>> lua.eval("print('\\xC2\\xA7')")
 Traceback (most recent call last):
   File "<string>", line 1, in <module>
   File "lupa/_lupa.pyx", line 203, in lupa._lupa.LuaRuntime.eval (lupa/_lupa.c:3789)
   File "lupa/_lupa.pyx", line 1060, in lupa._lupa.run_lua (lupa/_lupa.c:14933)
   File "lupa/_lupa.pyx", line 1079, in lupa._lupa.execute_lua_call (lupa/_lupa.c:15193)
   File "lupa/_lupa.pyx", line 189, in lupa._lupa.LuaRuntime.reraise_on_exception (lupa/_lupa.c:3579)
   File "lupa/_lupa.pyx", line 1214, in lupa._lupa.py_call_with_gil (lupa/_lupa.c:16528)
   File "lupa/_lupa.pyx", line 1206, in lupa._lupa.call_python (lupa/_lupa.c:16408)
   File "<string>", line 2, in x
   File "<string>", line 2, in <listcomp>
 RuntimeError: lost sys.__plugin__
 >>> lua.eval("print('\\xC2\\xA7')")
 Β§

Wat?

Install instruction problem / install problem

Hi,

So one thing I noticed in the INSTALL file is that there's no mention of running python setup.py install after running build. That's fairly obvious to me, but maybe not everyone.

On to my main problem. I installed LuaJIT from a sibling directory as lupa (both in /usr/local/src/) and when I went to run lupa, it said it couldn't find the so object it needed (output below.) Which is weird because it seemed to build OK, using pkg-config to find the LuaJIT package.

So then, I moved the LuaJIT directory that I'd installed from into the lupa directory, re-built/installed and everything seems to work now that it's statically linked.

Any idea why it wouldn't have worked originally? Thanks.


BUILDVM ../lib/vmdef.lua
DYNLINK libluajit.so
LINK luajit
OK Successfully built LuaJIT
make[1]: Leaving directory `/usr/local/src/LuaJIT-2.0.0-beta10/src'
==== Successfully built LuaJIT 2.0.0-beta10 ====
root@localhost:/usr/local/src/LuaJIT-2.0.0-beta10# make install
==== Installing LuaJIT 2.0.0-beta10 to /usr/local ====
mkdir -p /usr/local/bin /usr/local/lib /usr/local/include/luajit-2.0 /usr/local/share/man/man1 /usr/local/lib/pkgconfig /usr/local/share/luajit-2.0.0-beta10/jit /usr/local/share/lua/5.1 /usr/local/lib/lua/5.1
cd src && install -m 0755 luajit /usr/local/bin/luajit-2.0.0-beta10
cd src && test -f libluajit.a && install -m 0644 libluajit.a /usr/local/lib/libluajit-5.1.a || :
rm -f /usr/local/lib/libluajit-5.1.so.2.0.0 /usr/local/lib/libluajit-5.1.so /usr/local/lib/libluajit-5.1.so
cd src && test -f libluajit.so &&
install -m 0755 libluajit.so /usr/local/lib/libluajit-5.1.so.2.0.0 &&
ldconfig -n /usr/local/lib &&
ln -sf libluajit-5.1.so.2.0.0 /usr/local/lib/libluajit-5.1.so &&
ln -sf libluajit-5.1.so.2.0.0 /usr/local/lib/libluajit-5.1.so || :
cd etc && install -m 0644 luajit.1 /usr/local/share/man/man1
cd etc && sed -e "s|^prefix=.*|prefix=/usr/local|" luajit.pc > luajit.pc.tmp &&
install -m 0644 luajit.pc.tmp /usr/local/lib/pkgconfig/luajit.pc &&
rm -f luajit.pc.tmp
cd src && install -m 0644 lua.h lualib.h lauxlib.h luaconf.h lua.hpp luajit.h /usr/local/include/luajit-2.0
cd lib && install -m 0644 bc.lua v.lua dump.lua dis_x86.lua dis_x64.lua dis_arm.lua dis_ppc.lua dis_mips.lua dis_mipsel.lua bcsave.lua vmdef.lua /usr/local/share/luajit-2.0.0-beta10/jit
==== Successfully installed LuaJIT 2.0.0-beta10 to /usr/local ====

Note: the beta releases deliberately do NOT install a symlink for luajit
You can do this now by running this command (with sudo):

ln -sf luajit-2.0.0-beta10 /usr/local/bin/luajit

root@localhost:/usr/local/src/LuaJIT-2.0.0-beta10# ln -sf luajit-2.0.0-beta10 /usr/local/bin/luajit
root@localhost:/usr/local/src/LuaJIT-2.0.0-beta10# cd ..
root@localhost:/usr/local/src# ls
demoinfo2 demoinfo2.zip LuaJIT-2.0.0-beta10 LuaJIT-2.0.0-beta10.tar.gz lupa-0.20.tar.gz riak_1.2.0-1_i386.deb
root@localhost:/usr/local/src# tar zxvf lupa-0.20.tar.gz
lupa-0.20/
lupa-0.20/LICENSE.txt
lupa-0.20/setup.py
lupa-0.20/MANIFEST.in
lupa-0.20/README.rst
lupa-0.20/lupa/
lupa-0.20/lupa/init.py
lupa-0.20/lupa/lua.pxd
lupa-0.20/lupa/_lupa.c
lupa-0.20/lupa/lock.pxi
lupa-0.20/lupa/tests/
lupa-0.20/lupa/tests/init.py
lupa-0.20/lupa/tests/test.py
lupa-0.20/lupa/_lupa.pyx
lupa-0.20/lupa/version.py
lupa-0.20/CHANGES.rst
lupa-0.20/PKG-INFO
lupa-0.20/INSTALL.rst
lupa-0.20/MANIFEST
root@localhost:/usr/local/src# cd lupa-0.20/
root@localhost:/usr/local/src/lupa-0.20# python setup.py build
building without Cython
No local build of LuaJIT2 found in lupa directory, checking for installed library using pkg-config
Did not find LuaJIT2 using pkg-config: /bin/sh: pkg-config: not found

Traceback (most recent call last):
File "setup.py", line 111, in
ext_args = find_luajit_build()
File "setup.py", line 99, in find_luajit_build
raise RuntimeError("LuaJIT2 not found, please install the library and its development packages"
RuntimeError: LuaJIT2 not found, please install the library and its development packages, or put a local build into the lupa main directory (or pass '--no-luajit' option)
root@localhost:/usr/local/src/lupa-0.20# pkg-config
The program 'pkg-config' is currently not installed. You can install it by typing:
apt-get install pkg-config
root@localhost:/usr/local/src/lupa-0.20# aptitude install pkg-config
The following NEW packages will be installed:
pkg-config
0 packages upgraded, 1 newly installed, 0 to remove and 0 not upgraded.
Need to get 40.3 kB of archives. After unpacking 156 kB will be used.
Get: 1 http://us.archive.ubuntu.com/ubuntu/ oneiric/main pkg-config i386 0.26-1ubuntu1 [40.3 kB]
Fetched 40.3 kB in 0s (119 kB/s)
Selecting previously deselected package pkg-config.
(Reading database ... 35010 files and directories currently installed.)
Unpacking pkg-config (from .../pkg-config_0.26-1ubuntu1_i386.deb) ...
Processing triggers for man-db ...
Setting up pkg-config (0.26-1ubuntu1) ...

root@localhost:/usr/local/src/lupa-0.20# python setup.py build
building without Cython
No local build of LuaJIT2 found in lupa directory, checking for installed library using pkg-config
pkg-config found LuaJIT version 2.0.0-beta10

running build
running build_py
creating build
creating build/lib.linux-i686-2.7
creating build/lib.linux-i686-2.7/lupa
copying lupa/version.py -> build/lib.linux-i686-2.7/lupa
copying lupa/init.py -> build/lib.linux-i686-2.7/lupa
running build_ext
building 'lupa._lupa' extension
creating build/temp.linux-i686-2.7
creating build/temp.linux-i686-2.7/lupa
gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fPIC -I/usr/local/include/luajit-2.0 -I/usr/include/python2.7 -c lupa/_lupa.c -o build/temp.linux-i686-2.7/lupa/_lupa.o
lupa/_lupa.c: In function Γ’__pyx_pf_4lupa_5_lupa_9FastRLock_5__exit__Γ’:
lupa/_lupa.c:1561:13: warning: variable Γ’__pyx_v_tbΓ’ set but not used [-Wunused-but-set-variable]
lupa/_lupa.c:1560:13: warning: variable Γ’__pyx_v_vΓ’ set but not used [-Wunused-but-set-variable]
lupa/_lupa.c:1559:13: warning: variable Γ’__pyx_v_tΓ’ set but not used [-Wunused-but-set-variable]
lupa/_lupa.c: In function Γ’__pyx_f_4lupa_5_lupa_call_pythonΓ’:
lupa/_lupa.c:11630:7: warning: variable Γ’__pyx_v_retΓ’ set but not used [-Wunused-but-set-variable]
lupa/_lupa.c: In function Γ’__pyx_f_4lupa_5_lupa_py_as_functionΓ’:
lupa/_lupa.c:13839:42: warning: variable Γ’__pyx_v_py_objΓ’ set but not used [-Wunused-but-set-variable]
lupa/_lupa.c: In function Γ’__pyx_f_4lupa_5_lupa_lua_object_reprΓ’:
lupa/_lupa.c:6202:18: warning: Γ’__pyx_v_ptrΓ’ may be used uninitialized in this function [-Wuninitialized]
lupa/_lupa.c: In function Γ’__pyx_pf_4lupa_5_lupa_10_LuaObject_3__len__Γ’:
lupa/_lupa.c:4455:3: warning: Γ’__pyx_rΓ’ may be used uninitialized in this function [-Wuninitialized]
gcc -pthread -shared -Wl,-O1 -Wl,-Bsymbolic-functions -Wl,-Bsymbolic-functions build/temp.linux-i686-2.7/lupa/_lupa.o -L/usr/local/lib -lluajit-5.1 -o build/lib.linux-i686-2.7/lupa/_lupa.so
root@localhost:/usr/local/src/lupa-0.20# python
Python 2.7.2+ (default, Oct 4 2011, 20:03:08)
[GCC 4.6.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.

import lupa
Traceback (most recent call last):
File "", line 1, in
File "lupa/init.py", line 31, in
from lupa._lupa import *
ImportError: No module named _lupa
quit()
root@localhost:/usr/local/src/lupa-0.20# cd ..
root@localhost:/usr/local/src# python
Python 2.7.2+ (default, Oct 4 2011, 20:03:08)
[GCC 4.6.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
import lupa
Traceback (most recent call last):
File "", line 1, in
ImportError: No module named lupa
quit()
root@localhost:/usr/local/src# cd lupa-0.20/
root@localhost:/usr/local/src/lupa-0.20# python setup.py install
building without Cython
No local build of LuaJIT2 found in lupa directory, checking for installed library using pkg-config
pkg-config found LuaJIT version 2.0.0-beta10

running install
running build
running build_py
copying lupa/version.py -> build/lib.linux-i686-2.7/lupa
running build_ext
running install_lib
creating /usr/local/lib/python2.7/dist-packages/lupa
copying build/lib.linux-i686-2.7/lupa/version.py -> /usr/local/lib/python2.7/dist-packages/lupa
copying build/lib.linux-i686-2.7/lupa/_lupa.so -> /usr/local/lib/python2.7/dist-packages/lupa
copying build/lib.linux-i686-2.7/lupa/init.py -> /usr/local/lib/python2.7/dist-packages/lupa
byte-compiling /usr/local/lib/python2.7/dist-packages/lupa/version.py to version.pyc
byte-compiling /usr/local/lib/python2.7/dist-packages/lupa/init.py to init.pyc
running install_egg_info
Writing /usr/local/lib/python2.7/dist-packages/lupa-0.20.egg-info
root@localhost:/usr/local/src/lupa-0.20# cd ..
root@localhost:/usr/local/src# python
Python 2.7.2+ (default, Oct 4 2011, 20:03:08)
[GCC 4.6.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.

import lupa
Traceback (most recent call last):
File "", line 1, in
File "/usr/local/lib/python2.7/dist-packages/lupa/init.py", line 31, in
from lupa._lupa import *
ImportError: libluajit-5.1.so.2: cannot open shared object file: No such file or directory
quit()
root@localhost:/usr/local/src# find .. -iname 'libluajit'
../lib/libluajit-5.1.so.2.0.0
../lib/libluajit-5.1.a
../lib/libluajit-5.1.so
../lib/libluajit-5.1.so.2
../src/LuaJIT-2.0.0-beta10/src/libluajit.so
../src/LuaJIT-2.0.0-beta10/src/libluajit.a
root@localhost:/usr/local/src# ls lupa-0.20
build CHANGES.rst INSTALL.rst LICENSE.txt lupa MANIFEST MANIFEST.in PKG-INFO README.rst setup.py
root@localhost:/usr/local/src# ls lupa-0.20/build/
lib.linux-i686-2.7 temp.linux-i686-2.7
root@localhost:/usr/local/src# ls lupa-0.20/build/lib.linux-i686-2.7/
lupa
root@localhost:/usr/local/src# ls lupa-0.20/build/lib.linux-i686-2.7/lupa/
init.py _lupa.so version.py
root@localhost:/usr/local/src# ls /usr/local/include/luajit-2.0
lauxlib.h luaconf.h lua.h lua.hpp luajit.h lualib.h

Implementing an execution timeout.

Would it be possible to implement a timeout for running a function? I'm running untrusted, sandboxed code, and I need the execution of functions to be limited. Do you have some pointers as to how this could be achieved?

Update pypi version to use LuaJIT 2.0.4

LuaJIT 2.0.4 was released recently, it would be nice to have the pypi binary updated to use this version, plus reflect the newest changes (though there arent that many)

Thank you for your work! Keep it up πŸ‘

setup.py checks for libluajit.a; Windows incompatible?

I have built LuaJIT 2.0.0 on my Windows box, using the instructions found here: http://luajit.org/install.html

However, at the end of this build process (using msvcbuild.bat), there is no libluajit.a anywhere, so it seems impossible to build LuaJIT statically based on the checks setup.py does in find_luajit_build. It seems to me on Windows it should actually be checking for lua51.dll as well?

I don't really know much about pkg-config on Windows, quite frankly, but is that the only way to get lupa to install on Windows in this scenario? (No Cygwin or anything, I used the Windows SDK to build LuaJIT)

If that is the case, there's really no indication of that on the lupa page.

Surprising table equality behavior

import lupa
r = lupa.LuaRuntime()
r.globals().test == r.globals().test
False

I can supply a patch that adds richcmp and hash to _LuaTable.

Cannot compile with local LuaJIT

I ran into this in my Fetchy branch, but I can reproduce in master, so I know it's not me.

git clone [email protected]:scoder/lupa.git
cd lupa
wget http://luajit.org/download/LuaJIT-2.0.2.tar.gz
tar xvf LuaJIT-2.0.2.tar.gz
cd LuaJIT-2.0.2/
make
cd ..
virtualenv venv
source venv/bin/activate
python setup.py install

If I've got my reproduction steps right, this should grab the latest Lupa, then the latest LuaJIT, then builds LuaJIT, creates a virtualenv, and attempts to install Lupa into that virtualenv.

The error goes like this:

found LuaJIT build in /home/philip/projects/lupa/LuaJIT-2.0.2/src
building statically
building without Cython
running install
running build
running build_py
copying lupa/version.py -> build/lib.linux-x86_64-2.7/lupa
running build_ext
building 'lupa._lupa' extension
gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fPIC -I/home/philip/projects/lupa/LuaJIT-2.0.2/src -I/usr/include/python2.7 -c lupa/_lupa.c -o build/temp.linux-x86_64-2.7/lupa/_lupa.o
gcc -pthread -shared -Wl,-O1 -Wl,-Bsymbolic-functions -Wl,-z,relro build/temp.linux-x86_64-2.7/lupa/_lupa.o /home/philip/projects/lupa/LuaJIT-2.0.2/src/libluajit.a -o build/lib.linux-x86_64-2.7/lupa/_lupa.so
/usr/bin/ld: /home/philip/projects/lupa/LuaJIT-2.0.2/src/libluajit.a(lj_err.o): relocation R_X86_64_32S against `.rodata' can not be used when making a shared object; recompile with -fPIC
/home/philip/projects/lupa/LuaJIT-2.0.2/src/libluajit.a: could not read symbols: Bad value
collect2: error: ld returned 1 exit status
error: command 'gcc' failed with exit status 1

I'm hoping you'll have more insight into this issue than I do.

Implement Lua operator overloading for wrapped Python objects

Lua objects support operator overloading in their metatable:

http://www.lua.org/manual/5.3/manual.html#2.4

Mapping the Lua arithmetic etc. operators for wrapped Python objects to the equivalent Python operators should provide a much better user experience.

This also needs some kind of security support. Either through the normal attribute access intercept (which would have to check for special methods anyway), or through a separate set of explicitly allowed operators. Maybe both: explicitly allowed operators are fast, the rest goes through the special attribute lookup.

new release

Hey @scoder,

Would you mind cutting a pypi release with the current changes?

installation fails with luajit from homebrew

Hi,

Thanks for an awesome project!

I'm installing lupa into 2.7 virtualenv on OS X 10.9.5. The following seems to work fine:

brew install lua
pip install lupa

It detects lua 5.2.3, builds fine and examples from readme work.

With

brew install luajit
pip install lupa

it correctly detects luajit 2.0.3 and builds without errors, but lupa doesn't work:

>>> import lupa
>>> lupa.LuaRuntime()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "lupa/_lupa.pyx", line 147, in lupa._lupa.LuaRuntime.__cinit__ (lupa/_lupa.c:2906)
lupa._lupa.LuaError: Failed to initialise Lua runtime
>>> 

From the install instructions it is unclear if it should ever work, but since non-jit lua seems to work maybe there is a way to make lupa work with homebrew-installed luajit too?

Strange access behavior.

This is PROBABLY in the docs and I just misunderstood this, but what is the explanation for this behavior?

from lupa import LuaRuntime
lua = LuaRuntime()

fone = lua.eval('function (a) return a.thing == nil end')
ftwo = lua.eval('function (a) return a.thing() == nil end')

class Thing(object):
... def init(self):
... self.thing = None
...
class ThingTwo(object):
... def thing(self):
... return None
...
fone(Thing())
False
ftwo(ThingTwo())
True

Surprising `is` behaviour

Currently, a new _LuaTable python object is created every time a Lua table is access from Python. This causes a case where the table appears to not be itself:

>>> import lupa
>>> lua = lupa.LuaRuntime()
>>> lua.execute("foo = {}")
>>> f = lua.globals().foo
>>> g = lua.globals().foo
>>> f is g
False
>>> f
<Lua table at 0x0000000000327C18>
>>> g
<Lua table at 0x0000000000327C18>

The repr makes it especially confusing, since it shows the address of the underlying Lua object.

My suggestion would be to use a weakref cache when creating the objects, either in py_from_lua or the new_lua_* functions. I don't imagine this would greatly affect performance for cache misses, and in addition to fixing the identity issue, should cut down on some garbage.

I'm not actually able to compile lupa on my current machine, and I don't have much experience with Cython, but I can attempt a patch for this if I get the go-ahead.

Crashing Python by iterating Lua table [double post from lupa-dev mailing list]

Hi lupa-dev,

I am using lupa 0.19 with python 2.6.2, and I consistently get "Fatal Python error: deallocating None" error. I suspect python is trying to free a reference inside Lua that is already freed by Lua.

Here is how to reproduce it. First, create two files test.lua and test.py as shown below:

test.lua:

function test()
  local t = {}
  t.foo = 'bar'
  t.hello = 'world'
  return t
end

END of test.lua

test.py:

import lupa
from lupa import LuaRuntime


def func():
  py_obj = dict()
  py_table = dict()
  table = lua.globals().test()
  for k, v in table.items():
    py_table[k] = v
  py_obj['table'] = py_table

def func2():
  py_obj = dict()
  py_table = dict()
  table = lua.globals().test()
  py_table['foo'] = table['foo']
  py_table['hello'] = table['hello']
  py_obj['table'] = py_table
    
lua = LuaRuntime()

f = open("./test.lua", "r")
lines = "".join(f.readlines())
lua.execute(lines)

for i in range(1, 100000):
  func() # this will crash
  #func2() # this will not crash

END of test.py

Run test.py, and by switching between func() and func2(), you will see func() crashes, but func2() doesn't. The only difference between func and func2 is funct() calls the table.items() iterator to extract the content, and func2() directly indexes into table.

pthread_cond_wait: Undefined error: 0

Hi,

Programs that use lupa sometimes start printing pthread_cond_wait: Undefined error: 0 messages and go to infinite cycle. I don't know how to reduce it to a small example; it happened with a console script and with IPython notebook.

It could be related to exceptions because a console script is a bunch of unit tests which went crazy when I started calling Python functions from Lua code with invalid number of arguments. As I recall IPython notebook went crazy after calling a coroutine incorrectly, but I'm not 100% sure.

Tests create (and destroy?) lots of LuaRuntime instances. After making them use a single LuaRuntime instance errors gone. Another created but unused LuaRuntime instance was present when tests were executed, if it matters.

I'm using Python 2.7.5, Lua 5.2 and lupa 1.0.1 on OS X 10.9.5.

Binary instalation for windows python 2.7 64 or anaconda

I have tryed to build the lupa package for anaconda P2.7 64 and pyhon 2.7 64, but without success. I got this error when I use setup.py:

build\temp.win-amd64-2.7\Release\lupa\_lupa.o:_lupa.c:(.text+0x89): undefined reference to `_imp___Py_NoneStruct'
...
...
...
c:/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/bin/ld.exe: build\temp.win-amd64-2.7\Release\lupa\_lupa.o: bad reloc address 0x0 in section `.data'
collect2.exe: error: ld returned 1 exit status
error: command 'gcc' failed with exit status 1

When I try to compile using mingw environment I got this one:

F:\python64\lupa>gcc.exe -DMS_WIN64 -shared -s build\_lupa.o "C:\Program Files (x86)\Lua\5.1\lib\lua51.dll" build\_lupa.def -LC:\Users\emilio\AppData\Local\Continuum\Anaconda\libs -LC:\Users\emilio\AppData\Local\Continuum\Anaconda\PCbuild\amd64 -lpython27 -lmsvcr90 -o build\_lupa.pyd collect2.exe: error: ld returned 5 exit status and an access violation window.

Do you know how to fix it ? I tryed 3 different versions of mingw without success, or do you know where can I found the windows binary installation ?

Best regards,
secauvr

How to specify the lua version

Hi, My lua version is 5.2.But when I execute lua.require('socket'), out :

LuaError: module 'socket' not found:
    no field package.preload['socket']
    no file './socket.lua'
    no file '/usr/share/luajit-2.0.0-beta9/socket.lua'
    no file '/usr/local/share/lua/5.1/socket.lua'
    no file '/usr/local/share/lua/5.1/socket/init.lua'
    no file '/usr/share/lua/5.1/socket.lua'
    no file '/usr/share/lua/5.1/socket/init.lua'
    no file './socket.so'
    no file '/usr/local/lib/lua/5.1/socket.so'
    no file '/usr/lib/x86_64-linux-gnu/lua/5.1/socket.so'
    no file '/usr/lib/lua/5.1/socket.so'
    no file '/usr/local/lib/lua/5.1/loadall.so'

And lua.globals()._VERSION is Lua 5.1

Python functions called from Lua do not unpack returned tuples.

def pyfun():
    return "one", "two", "three"

lua.globals()['pyfun'] = pyfun
lua.execute("a, b = pyfun() print(a) print(b)")

prints:

('one', 'two', 'three')
nil

where one might expect:

one
two

This behavior makes it difficult (or impossible) to write Python libraries that implement the Lua convention:

result, message = somePossiblyFailingFunction(...)

I'm wondering if there is a reason I am overlooking for not unpacking returned tuples. Maybe this is just an oversight? It seems pretty easy to alter this behavior. I can offer you a patch (and unit tests) if you are interested.

lupa+coroutines segfaults in IPython

I'm using lupa 1.0, ipython 2.3 (both installed using pip) and lua 5.2.3 (installed using homebrew). The issue is quite strange: lupa segfaults in IPython but not in python.

Python:

>>> import lupa; lua = lupa.LuaRuntime()
>>> f = lua.eval("function(N) coroutine.yield(N) end")
>>> list(f.coroutine(1))
[1]
>>> f.coroutine(1)
<Lua thread at 0x7ff198f00b30>

IPython:

In [1]: import lupa; lua = lupa.LuaRuntime()
In [2]: f = lua.eval("function(N) coroutine.yield(N) end")
In [3]: list(f.coroutine(1))  # f.coroutine always seems to work fine when wrapped in list()
Out[3]: [1]
In [4]: f.coroutine(1)
Segmentation fault: 11

It is likely a manifistation of a problem unrelated to python vs ipython, it could segfault in IPython e.g. because memory layout of python and ipython processes are different. I've had a similar issue in one of my wrappers once (works in python, doesn't work in ipython); the cause was improper handling of null-terminated strings. But I'm sure you know all this stuff way better :)

Use luajit from a different location?

Hi there, I'm trying to use lupa with lua installed in a non-standard location. Is it somehow possible to point lupa to that location instead of using the default location? Concretely, when I attempt to load a file that uses torch, I get the following error:

Type "help", "copyright", "credits" or "license" for more information.
>>> from lupa import LuaRuntime
>>> lua = LuaRuntime(unpack_returned_tuples=True)
>>> lua.require('train_agent')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "lupa/_lupa.pyx", line 268, in lupa._lupa.LuaRuntime.require (lupa/_lupa.c:4683)
  File "lupa/_lupa.pyx", line 1245, in lupa._lupa.call_lua (lupa/_lupa.c:17287)
  File "lupa/_lupa.pyx", line 1254, in lupa._lupa.execute_lua_call (lupa/_lupa.c:17400)
  File "lupa/_lupa.pyx", line 1207, in lupa._lupa.raise_lua_error (lupa/_lupa.c:16746)
lupa._lupa.LuaError: ./initenv.lua:8: module 'torch' not found:
    no field package.preload['torch']
    no file '/usr/local/share/lua/5.2/torch.lua'
    no file '/usr/local/share/lua/5.2/torch/init.lua'
    no file '/usr/local/lib/lua/5.2/torch.lua'
    no file '/usr/local/lib/lua/5.2/torch/init.lua'
    no file './torch.lua'
    no file '/usr/local/lib/lua/5.2/torch.so'
    no file '/usr/local/lib/lua/5.2/loadall.so'
    no file './torch.so'
>>>

Everything works fine if I load the file using luajit from the torch installation using ~/torch/install/bin/luajit train_agent.lua.

Is it possible to somehow make this work? Any help is greatly appreciated!

Named functions

Hi !

I am using lupa to validate some LUA expressions persisted to a data store from a python webapp. The thing is my expressions contains some custom functions defined elsewhere. I am wondering is there is a way to define them before running the expression I am trying to validate ?

EG:

expr = "field('brand') == 'audi'"

Here field is a custom function, I am just trying to enforce that the LUA syntax is valid, not execute exactly the whole function so I figured I had to do something like that :

lua = LuaRuntime(unpack_returned_tuples=True)
lua.eval('function field(x) return x end')
print lua.eval(expr)

But I keep having this error :

Traceback (most recent call last):
  File "test-lua.py", line 8, in <module>
    lua.eval('function field((x) return x end')
  File "_lupa.pyx", line 168, in lupa._lupa.LuaRuntime.eval (lupa/_lupa.c:3399)
  File "_lupa.pyx", line 950, in lupa._lupa.run_lua (lupa/_lupa.c:13350)
lupa._lupa.LuaSyntaxError: error loading code: [string "<python>"]:1: '(' expected near 'field'

Am I doing something wrong or is this just not possible ?

Thanks a lot

PyPy support

The package compiles and installs 'correctly' for the "pypy" and "pypy3" target on travis ( see build ).

However when it is used the following exception occurs on "pypy":

Traceback (most recent call last):
...
  File "/home/travis/build/jayvdb/lupa/lupa/__init__.py", line 31, in <module>
    from lupa._lupa import *
ImportError: unable to load extension module '/home/travis/build/jayvdb/lupa/lupa/_lupa.pypy-25.so': /home/travis/build/jayvdb/lupa/lupa/_lupa.pypy-25.so: undefined symbol: PyByteArray_Check

This appears to have a hack which works around the problem, but maybe it only pushes the problem further along
https://mail.python.org/pipermail/pypy-dev/2014-August/012701.html

And on PyPy3:

Traceback (most recent call last):
...
  File "/home/travis/build/jayvdb/lupa/lupa/__init__.py", line 31, in <module>
    from lupa._lupa import *
ImportError: unable to load extension module '/home/travis/build/jayvdb/lupa/lupa/_lupa.pypy3-24.so': /home/travis/build/jayvdb/lupa/lupa/_lupa.pypy3-24.so: undefined symbol: PyBytes_FromFormat

which is https://bitbucket.org/pypy/pypy/issues/2022

If it isnt possible to workaround these issues easily, the setup.py could detect pypy and emit a useful error. README.rst currently does mention Cpython a few times prominently , but it could be more clear and say that it definitely doesnt work on PyPy and probably wont compile on other implementations of Python .

Numpy support?

Hey,

I didn't found any information regarding Numpy (PyArrayObject), so I guess it's not supported.
A translation between Numpy and maybe Torch's Lua Tensor (THTensor) would be nice.

Do you know if there is any project or code out there which does that?

THTensor, saved via LuaT.

PyArrayObject.

__pairs metamethod is ignored when iterating Lua tables from Python

Here's a simple example:

In [16]: copy, proxy = LuaRuntime().execute("""
obj = {foo=123, bar=345}

proxy = {}
setmetatable(proxy, {
  __index = function(self, key)
    return obj[key]
  end,

  __newindex = function(self, key, value)
    obj[key] = value
  end,

  __pairs = function(self)
    return pairs(obj)
  end,
})

copy = {}
for k,v in pairs(proxy) do
  copy[k] = v
end

return copy, proxy
""")

In [17]: copy['foo']
Out[17]: 123

In [18]: proxy['foo']
Out[18]: 123

In [19]: list(copy.items())
Out[19]: [(u'foo', 123), (u'bar', 345)]

In [20]: list(proxy.items())
Out[20]: []

convert a python function in py_to_lua

Hi,

I am trying to pass a python function as argument into a function defined in lua code, it complaining that: LuaError: XXXXXX.lua:44: attempt to call local 'opfunc' (a table value).

def tuple_fun():
    return "one", "two", "three", "four"
lua.globals()['fun'] = tuple_fun

lua.eval('type(fun)')
# output userdata

lua.eval('type(python.as_function(fun))')
# output function

I know that by using python.as_function in lua code can make lupa recognize the object as a function, but in my case, the lua code is not possible to change because it's in a third party library, I try to call the function in lupa code.

I checked the code in _lupa.pyx, in the function named py_to_lua, why not just convert the python function into a lua object? or add the python.as_function automatically?

Can anyone help?

From Lua to Python

I have this piece of Lua-Torch code and try to put into Python code
. I have difficulty to understand the meaning result/process of:
= nn.Linear(size1
t2h_d.data.module:share(

I would like to find equivalent of those 2 functions in numpy Python.
Here, remaining code :

   import 'nn'
   import 'nngraph'

   nh_t= nn.identity(d)
   h_d= nn.identity(d)
size1= 5
t2h_d = nn.Linear(size1, 4 * size1)(h_t):annotate{name='ih_'..L}  
d2h_d = nn.Linear(size1, 4 * size1)(h_d):annotate{name='hh_'..L}  


t2h_d.data.module:share(shared_weights[1], 'weight', 'bias', 'grdWeight', 'grdBias')
d2h_d.data.module:share(shared_weights[2], 'weight', 'bias', 'grdWeight', 'grdBias')

tables as kwargs for Python functions

I'm using decorators to make it possible for Python functions to be called from Lua with both "named arguments" and "positional arguments": code, tests.

The decorator has caveats, but if you think it is OK to provide it in lupa I can submit a PR (if we fix #26).

lupa coroutines choke on nested pcalls & yield

I have been running after a weird error in error handling error lately when hacking splash and I seem to have pinned the issue down. Here's an example that looks admittedly contrived when all the unnecessary details are omitted:

from lupa import LuaRuntime
lua = LuaRuntime()

py_coro = lua.execute("""
coro = require("coroutine")

function main()
  print('pcall result:', pcall(pcall, coro.yield))
end

lua_coro = coro.create(main)

step = 1
while coro.status(lua_coro) ~= 'dead' do
   print('lua step '.. step .. ':', coro.resume(lua_coro, i))
   step = step + 1
end

return coro.create(main)
""")

for i, r in enumerate(py_coro(), 1):
    print 'py step %d:' % i, r

It produces the following output for me:

lua step 1: true
pcall result:   true    true    nil
lua step 2: true
py step 1: None
Traceback (most recent call last):
  File "test_lupa.py", line 22, in <module>
    for i, r in enumerate(py_coro(), 1):
  File "lupa/_lupa.pyx", line 810, in lupa._lupa._LuaThread.__next__ (lupa/_lupa.c:12213)
  File "lupa/_lupa.pyx", line 886, in lupa._lupa.resume_lua_thread (lupa/_lupa.c:13091)
  File "lupa/_lupa.pyx", line 1207, in lupa._lupa.raise_lua_error (lupa/_lupa.c:16746)
lupa._lupa.LuaError: error in error handling

As you can see, the coroutine is easily traversed inside Lua code but not in Python code. The issue is reproduced when all three following conditions are true:

  • there are nested pcalls in the coroutine
  • there is coro.yield somewhere inside those pcalls
  • outermost pcall invocation is not a functioncall statement, but an expression whose value is used afterwards and is not optimized away

Can not used in centos

After I install lupa 1.1 by "pip install lupa" in CentOS, I found this error:

[vagrant@localhost download]$ python
Python 2.7.5 (default, Jun 24 2015, 00:41:19) 
[GCC 4.8.3 20140911 (Red Hat 4.8.3-9)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import lupa
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/site-packages/lupa/__init__.py", line 31, in <module>
    from lupa._lupa import *
ImportError: /usr/lib/python2.7/site-packages/lupa/_lupa.so: undefined symbol: lua_gettop
>>> 

Can anyone help me?

skip certain libraries (e.g. io and os) to sandbox semi-trusted code

Dear contributers, dear community,

I'd like to build a web-application in python that can be extended using lua scripts. These extensions are contributed by affiliated developers, and in general, their code is trustworthy. But I'd sleep a bit better, if I could skip some of the standard libraries when creating a new LuaRuntime.

The io and os libraries are most critical as they provide access to the file system and allow to run any programs within the privileges of the user who is running the web server.

The problem comes with the call to the luaL_openlibs function. I'd be glad, if I could state in a list, which module should be loaded when creating a new LuaRuntime. This could help: http://stackoverflow.com/questions/4551101/lual-openlibs-and-sandboxing-scripts

Thanks a lot for the great library,
Timo

Cannot install, getting ImportError

Python 2.6.6 (r266:84292, Dec 27 2010, 00:02:40) 
[GCC 4.4.5] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import lupa
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.6/dist-packages/lupa/__init__.py", line 31, in <module>
    from lupa._lupa import *
ImportError: libluajit-5.1.so.2: cannot open shared object file: No such file or directory

Lupa built and installed with no errors, so I really don't get what the deal is. It may have something to do with me screwing up during the first install and installing LuaJIT separately from Lupa, but I rebuilt LuaJIT in a subfolder and rebuilt/reinstalled Lupa after that, so I'm not sure.

Python exceptions can't be handled from Lua scripts using pcall

>>> import lupa; lua = lupa.LuaRuntime()
>>> pcall = lua.eval("pcall")
>>> def py_func(): raise Exception("python exception")
... 
>>> lua_func = lua.eval("function () error('lua error') end")
>>> pcall(lua_func)
(False, u'[string "<python>"]:1: lua error')
>>> pcall(py_func)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "lupa/_lupa.pyx", line 507, in lupa._lupa._LuaObject.__call__ (lupa/_lupa.c:7743)
  File "lupa/_lupa.pyx", line 1245, in lupa._lupa.call_lua (lupa/_lupa.c:17287)
  File "lupa/_lupa.pyx", line 1252, in lupa._lupa.execute_lua_call (lupa/_lupa.c:17381)
  File "lupa/_lupa.pyx", line 231, in lupa._lupa.LuaRuntime.reraise_on_exception (lupa/_lupa.c:4144)
  File "lupa/_lupa.pyx", line 1386, in lupa._lupa.py_call_with_gil (lupa/_lupa.c:18673)
  File "lupa/_lupa.pyx", line 1352, in lupa._lupa.call_python (lupa/_lupa.c:18407)
  File "<stdin>", line 1, in py_func
Exception: python exception

SEGFAULT calling lua

I am getting segfaults from time to time. It seems to happen when python objects are wrapped and sent to the lua runtime.
Lupa version 1.4
Lua: tested version 5.1.5, 5.2.3, and luajit 2.0.4

Backtraces of the crash:
Lua:

#0  0x00007f639189c261 in lua_type () from /usr/lib64/liblua5.2.so.0
#1  0x00007f6391ae1b21 in __pyx_f_4lupa_5_lupa_lua_object_repr ()
   from /home/micke/Documents/dev/telldus/tellstick-server/build/env/lib/python2.7/site-packages/lupa/_lupa.so
#2  0x00007f6391adf4ac in __pyx_pf_4lupa_5_lupa_10_LuaObject_14__str__ ()
   from /home/micke/Documents/dev/telldus/tellstick-server/build/env/lib/python2.7/site-packages/lupa/_lupa.so
#3  0x00007f6391ade33d in __pyx_pw_4lupa_5_lupa_10_LuaObject_15__str__ ()
   from /home/micke/Documents/dev/telldus/tellstick-server/build/env/lib/python2.7/site-packages/lupa/_lupa.so
#4  0x00007f63994c974a in _PyObject_Str () from /usr/lib64/libpython2.7.so.1.0
#5  0x00007f63994dad98 in PyString_Format () from /usr/lib64/libpython2.7.so.1.0
#6  0x00007f639952735f in PyEval_EvalFrameEx () from /usr/lib64/libpython2.7.so.1.0
#7  0x00007f63995265cb in PyEval_EvalFrameEx () from /usr/lib64/libpython2.7.so.1.0
#8  0x00007f63995265cb in PyEval_EvalFrameEx () from /usr/lib64/libpython2.7.so.1.0
#9  0x00007f63995265cb in PyEval_EvalFrameEx () from /usr/lib64/libpython2.7.so.1.0
#10 0x00007f63995265cb in PyEval_EvalFrameEx () from /usr/lib64/libpython2.7.so.1.0
#11 0x00007f63995265cb in PyEval_EvalFrameEx () from /usr/lib64/libpython2.7.so.1.0
#12 0x00007f63995265cb in PyEval_EvalFrameEx () from /usr/lib64/libpython2.7.so.1.0
#13 0x00007f63995265cb in PyEval_EvalFrameEx () from /usr/lib64/libpython2.7.so.1.0
#14 0x00007f6399529ad0 in PyEval_EvalCodeEx () from /usr/lib64/libpython2.7.so.1.0
#15 0x00007f63994b30cd in ?? () from /usr/lib64/libpython2.7.so.1.0
#16 0x00007f639948d333 in PyObject_Call () from /usr/lib64/libpython2.7.so.1.0
#17 0x00007f639952365e in PyEval_EvalFrameEx () from /usr/lib64/libpython2.7.so.1.0
#18 0x00007f6399529ad0 in PyEval_EvalCodeEx () from /usr/lib64/libpython2.7.so.1.0
#19 0x00007f63994b30cd in ?? () from /usr/lib64/libpython2.7.so.1.0
#20 0x00007f639948d333 in PyObject_Call () from /usr/lib64/libpython2.7.so.1.0
#21 0x00007f639952365e in PyEval_EvalFrameEx () from /usr/lib64/libpython2.7.so.1.0
#22 0x00007f6399529ad0 in PyEval_EvalCodeEx () from /usr/lib64/libpython2.7.so.1.0
#23 0x00007f63995264ae in PyEval_EvalFrameEx () from /usr/lib64/libpython2.7.so.1.0
#24 0x00007f6399529ad0 in PyEval_EvalCodeEx () from /usr/lib64/libpython2.7.so.1.0
#25 0x00007f63994b30cd in ?? () from /usr/lib64/libpython2.7.so.1.0
#26 0x00007f639948d333 in PyObject_Call () from /usr/lib64/libpython2.7.so.1.0
#27 0x00007f639952365e in PyEval_EvalFrameEx () from /usr/lib64/libpython2.7.so.1.0
#28 0x00007f63995265cb in PyEval_EvalFrameEx () from /usr/lib64/libpython2.7.so.1.0
#29 0x00007f63995265cb in PyEval_EvalFrameEx () from /usr/lib64/libpython2.7.so.1.0
#30 0x00007f6399529ad0 in PyEval_EvalCodeEx () from /usr/lib64/libpython2.7.so.1.0
#31 0x00007f63994b2fec in ?? () from /usr/lib64/libpython2.7.so.1.0
#32 0x00007f639948d333 in PyObject_Call () from /usr/lib64/libpython2.7.so.1.0
#33 0x00007f639949c275 in ?? () from /usr/lib64/libpython2.7.so.1.0
#34 0x00007f639948d333 in PyObject_Call () from /usr/lib64/libpython2.7.so.1.0
#35 0x00007f639951fd77 in PyEval_CallObjectWithKeywords () from /usr/lib64/libpython2.7.so.1.0
#36 0x00007f6399558ab2 in ?? () from /usr/lib64/libpython2.7.so.1.0
#37 0x00007f639922c444 in start_thread () from /lib64/libpthread.so.0
#38 0x00007f6398f735ed in clone () from /lib64/libc.so.6

Luajit:

#0  0x00007f555d8a3c78 in lua_rawgeti () from /usr/lib64/libluajit-5.1.so.2
#1  0x00007f555db13b3e in __pyx_f_4lupa_5_lupa_10_LuaObject_push_lua_object ()
   from /home/micke/Documents/dev/telldus/tellstick-server/build/env/lib/python2.7/site-packages/lupa/_lupa.so
#2  0x00007f555db15736 in __pyx_pf_4lupa_5_lupa_10_LuaObject_14__str__ ()
   from /home/micke/Documents/dev/telldus/tellstick-server/build/env/lib/python2.7/site-packages/lupa/_lupa.so
#3  0x00007f555db152f7 in __pyx_pw_4lupa_5_lupa_10_LuaObject_15__str__ ()
   from /home/micke/Documents/dev/telldus/tellstick-server/build/env/lib/python2.7/site-packages/lupa/_lupa.so
#4  0x00007f556550074a in _PyObject_Str () from /usr/lib64/libpython2.7.so.1.0
#5  0x00007f5565511d98 in PyString_Format () from /usr/lib64/libpython2.7.so.1.0
#6  0x00007f556555e35f in PyEval_EvalFrameEx () from /usr/lib64/libpython2.7.so.1.0
#7  0x00007f556555d5cb in PyEval_EvalFrameEx () from /usr/lib64/libpython2.7.so.1.0
#8  0x00007f556555d5cb in PyEval_EvalFrameEx () from /usr/lib64/libpython2.7.so.1.0
#9  0x00007f556555d5cb in PyEval_EvalFrameEx () from /usr/lib64/libpython2.7.so.1.0
#10 0x00007f556555d5cb in PyEval_EvalFrameEx () from /usr/lib64/libpython2.7.so.1.0
#11 0x00007f556555d5cb in PyEval_EvalFrameEx () from /usr/lib64/libpython2.7.so.1.0
#12 0x00007f556555d5cb in PyEval_EvalFrameEx () from /usr/lib64/libpython2.7.so.1.0
#13 0x00007f556555d5cb in PyEval_EvalFrameEx () from /usr/lib64/libpython2.7.so.1.0
#14 0x00007f5565560ad0 in PyEval_EvalCodeEx () from /usr/lib64/libpython2.7.so.1.0
#15 0x00007f55654ea0cd in ?? () from /usr/lib64/libpython2.7.so.1.0
#16 0x00007f55654c4333 in PyObject_Call () from /usr/lib64/libpython2.7.so.1.0
#17 0x00007f556555a65e in PyEval_EvalFrameEx () from /usr/lib64/libpython2.7.so.1.0
#18 0x00007f5565560ad0 in PyEval_EvalCodeEx () from /usr/lib64/libpython2.7.so.1.0
#19 0x00007f55654ea0cd in ?? () from /usr/lib64/libpython2.7.so.1.0
#20 0x00007f55654c4333 in PyObject_Call () from /usr/lib64/libpython2.7.so.1.0
#21 0x00007f556555a65e in PyEval_EvalFrameEx () from /usr/lib64/libpython2.7.so.1.0
#22 0x00007f5565560ad0 in PyEval_EvalCodeEx () from /usr/lib64/libpython2.7.so.1.0
#23 0x00007f556555d4ae in PyEval_EvalFrameEx () from /usr/lib64/libpython2.7.so.1.0
#24 0x00007f5565560ad0 in PyEval_EvalCodeEx () from /usr/lib64/libpython2.7.so.1.0
#25 0x00007f55654ea0cd in ?? () from /usr/lib64/libpython2.7.so.1.0
#26 0x00007f55654c4333 in PyObject_Call () from /usr/lib64/libpython2.7.so.1.0
#27 0x00007f556555a65e in PyEval_EvalFrameEx () from /usr/lib64/libpython2.7.so.1.0
#28 0x00007f556555d5cb in PyEval_EvalFrameEx () from /usr/lib64/libpython2.7.so.1.0
#29 0x00007f556555d5cb in PyEval_EvalFrameEx () from /usr/lib64/libpython2.7.so.1.0
#30 0x00007f5565560ad0 in PyEval_EvalCodeEx () from /usr/lib64/libpython2.7.so.1.0
#31 0x00007f55654e9fec in ?? () from /usr/lib64/libpython2.7.so.1.0
#32 0x00007f55654c4333 in PyObject_Call () from /usr/lib64/libpython2.7.so.1.0
#33 0x00007f55654d3275 in ?? () from /usr/lib64/libpython2.7.so.1.0
#34 0x00007f55654c4333 in PyObject_Call () from /usr/lib64/libpython2.7.so.1.0
#35 0x00007f5565556d77 in PyEval_CallObjectWithKeywords () from /usr/lib64/libpython2.7.so.1.0
#36 0x00007f556558fab2 in ?? () from /usr/lib64/libpython2.7.so.1.0
#37 0x00007f5565263444 in start_thread () from /lib64/libpthread.so.0
#38 0x00007f5564faa5ed in clone () from /lib64/libc.so.6

no attribute 'lua_type' in module lupa

i am install lupa by command: sudo pip install lupa==1.0.1; luajit version is 2.0.2. python: 2.7
it's error when execute: import lupa; lupa.lua_type();
error is: AttributeError: module object has no attribute lua_type;
how can i fix it ?

`lupa._lupa.LuaError: Failed to initialise Lua runtime`

$ python
Python 2.7.3 (default, Nov  1 2012, 14:21:55) 
[GCC 4.2.1 Compatible Apple Clang 4.0 ((tags/Apple/clang-421.0.57))] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from lupa import LuaRuntime
>>> lua = LuaRuntime()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "_lupa.pyx", line 116, in lupa._lupa.LuaRuntime.__cinit__ (lupa/_lupa.c:2237)
lupa._lupa.LuaError: Failed to initialise Lua runtime

lupa was installed with the following command.

$ sudo pip-2.7 install lupa
Downloading/unpacking lupa
  Downloading lupa-0.20.tar.gz (119kB): 119kB downloaded
  Running setup.py egg_info for package lupa
    building with Cython 0.18
    No local build of LuaJIT2 found in lupa directory, checking for installed library using pkg-config
    pkg-config found LuaJIT version 2.0.0-beta10


Installing collected packages: lupa
  Running setup.py install for lupa
    building with Cython 0.18
    No local build of LuaJIT2 found in lupa directory, checking for installed library using pkg-config
    pkg-config found LuaJIT version 2.0.0-beta10

    skipping 'lupa/_lupa.c' Cython extension (up-to-date)
    building 'lupa._lupa' extension
    /usr/bin/clang -fno-strict-aliasing -fno-common -dynamic -pipe -O2 -fwrapv -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -I/opt/local/include/luajit-2.0 -I/opt/local/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7 -c lupa/_lupa.c -o build/temp.macosx-10.8-x86_64-2.7/lupa/_lupa.o
    lupa/_lupa.c:9253:14: warning: explicitly assigning a variable of type 'PyObject *' (aka 'struct _object *') to itself [-Wself-assign]
      __pyx_self = __pyx_self;
      ~~~~~~~~~~ ^ ~~~~~~~~~~
    lupa/_lupa.c:9331:14: warning: explicitly assigning a variable of type 'PyObject *' (aka 'struct _object *') to itself [-Wself-assign]
      __pyx_self = __pyx_self;
      ~~~~~~~~~~ ^ ~~~~~~~~~~
    2 warnings generated.
    /usr/bin/clang -bundle -undefined dynamic_lookup -isysroot / -L/opt/local/lib build/temp.macosx-10.8-x86_64-2.7/lupa/_lupa.o -L/opt/local/lib -lluajit-5.1 -o build/lib.macosx-10.8-x86_64-2.7/lupa/_lupa.so

Successfully installed lupa
Cleaning up...

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.