Lua, Python

Started by
8 comments, last by Jnz86 18 years, 7 months ago
Hi, last few days i was reading Python documentation and learning Python, and when time came to embbed(right word?) it into my app i find it very anoying with all these DECREF,INCREF . So i want to ask for advice, someone who tried both Lua and Python, IS LUA RELLY EASIER TO INTEGRATE INTO APP THAN PYTHON ?(if it is i think i will learn Lua, and drop Python...). Do i need to keep track of objects in Lua like in Python with DECREF and so on... Thanks...
Advertisement
If you chose not to use an integration library than Lua is hands-down easier to embed than Python. If you choose to use an integration library like toLua or boost::python then they're about the same.
Quote:and when time came to embbed(right word?) it into my app i find it very anoying with all these DECREF,INCREF


Yes, that's embedding. No, it's not trivial. Using boost::python, SWIG or even Elmer make it much simpler.

Quote:Do i need to keep track of objects in Lua like in Python with DECREF and so on...


Skimming the manual, apparently not. The price you pay is that Lua is not aware of references that exist on the C side of your application, so you may end up with dangling pointers.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Quote:
If you chose not to use an integration library than Lua is hands-down easier to embed than Python. If you choose to use an integration library like toLua or boost::python then they're about the same.

if i use boost::python, do i have to deal with reference counting ?
Quote:Original post by Jnz86
if i use boost::python, do i have to deal with reference counting ?


Not directly, no. The object constructor and destructor take care of incrementing and decrementing the reference count. A wrapper exist to take care of "borrowed references". You just have to make sure you do capture any stray PyObject*

From the tutorial:

object main_module((    handle<>(borrowed(PyImport_AddModule("__main__")))));object main_namespace = main_module.attr("__dict__");handle<> ignored((PyRun_String(    "hello = file('hello.txt', 'w')\n"    "hello.write('Hello world!')\n"    "hello.close()"  , Py_file_input  , main_namespace.ptr()  , main_namespace.ptr())));


PyImport_AddModule returns a borrowed reference that we need, so we wrap it with borrowed. PyRun_String returns a new reference that we don't need, so we store it in an object variable and just do nothing with it.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Well i look into boost::python, it's a life saver i guess :)
Thanks guys!
If you want to use boost::python, you might want to check out my draft article on setting up projects with boost::python. There are a few details that can screw you up when setting up boost::python like forgetting to enable RTTI.
Quote:Original post by Jnz86
Well i look into boost::python, it's a life saver i guess :)
Thanks guys!


It's not perfect but it helps a lot. It's definitely not as simple as if there were no reference counting at all.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Do you recall if the Python spec requires reference counting or is it a C/Python implementation detail?
Quote:Original post by SiCrane
If you want to use boost::python, you might want to check out my draft article on setting up projects with boost::python. There are a few details that can screw you up when setting up boost::python like forgetting to enable RTTI.

I will look at that. Thanks again.

This topic is closed to new replies.

Advertisement