bastibe / lunatic-python

A two-way bridge between Python and Lua
http://labix.org/lunatic-python
GNU Lesser General Public License v2.1
303 stars 83 forks source link

Bug fix: Set 'python' global to newly created lib after loading #63

Closed hosford42 closed 7 years ago

hosford42 commented 7 years ago

This resolves the issue with the python global being undefined after the lib is required.

hosford42 commented 7 years ago

I'll try to revisit this tomorrow to see what is causing the tests to fail. Would you prefer I leave this open or open a new one once it's good to go?

greatwolf commented 7 years ago

This is probably caused by wrong items on the lua virtual stack. lua_setglobal pops one item off when called, in this case, the python 'table' got popped which normally gets returned at the end.

That said, it's probably better to not make this global by default. The suggested lua way for requiring modules is to typically assign the returned table to a variable, eg. local python = require 'python' or python = require 'python' if you really want it global. I think in lua 5.2+ they did away with globals and replaced it with the notion of environment space (_ENV) instead.

hosford42 commented 7 years ago

Ah, that makes sense. I'm brand new to lua, and I guess I was looking at some older examples. I'll close this out, then. Thanks for the explanation!