bastibe / lunatic-python

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

Suggestion to change addressing object by number to force asindx access #45

Closed marcelvanherk closed 7 years ago

marcelvanherk commented 7 years ago

Hi,

in this sample:

require('python')
np = python.import('numpy')
a = np.arange(1, 10)

a[0] is not defined. It only becomes defined after you say

a = python.asindx(a)

I would there like to suggest the following change:

if (obj->asindx || lua_isnumber(L, 2))
    return _p_object_newindex_set(L, obj, 2, 3);

and

if (obj->asindx || lua_isnumber(L, 2))
    return _p_object_index_get(L, obj, 2);

This would force every numerical access to be seen as an index access, which simplifies numpy data access from Lua a lot. Does that sound like a good idea?

Marcel

greatwolf commented 7 years ago

The index vs attrib access could definitely use some refinement. You can take this further and just check that lua_type(L, 2) != LUA_TSTRING. Since attributes are always strings in python, trying to do attribute access on a non-string key is nonsensical.

marcelvanherk commented 7 years ago

Hi, not sure that lua_type(L, 2) != LUA_TSTRING would help much over lua_isnumber; since apart from string and number, we do have some valid table indexes in lua such as function or userdata, but they are very rarely used I believe. But either modification would do.

Marcel

greatwolf commented 7 years ago

Yes, this change is just to make it a little more general. Someone else down the line might want to use esoteric lua types as keys and since we're changing this anyway might as well support it.

marcelvanherk commented 7 years ago

Like this does not work:

if (obj->asindx || !lua_isstring(L, 2))

Kept it as

if (obj->asindx || lua_isnumber(L, 2))
greatwolf commented 7 years ago

lua_isstring(L, 2) does implicit conversion from a number to a string before performing the check. This is why I used lua_type instead so this doesn't happen.

marcelvanherk commented 7 years ago

OK, fixed