在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):bastibe/lunatic-python开源软件地址(OpenSource Url):https://github.com/bastibe/lunatic-python开源编程语言(OpenSource Language):C 83.1%开源软件介绍(OpenSource Introduction):Lunatic-PythonDetailsThis is a fork of Lunatic Python, which can be found on the 'net at http://labix.org/lunatic-python. Sadly, Lunatic Python is very much outdated and won't work with either a current Python or Lua. This is an updated version of lunatic-python that works with Python 2.7-3.x and Lua 5.1-5.3. I tried contacting the original author of Lunatic Python, but got no response. InstallingTo install, you will need to have the Python and Lua development libraries on your system. If you
do, use the recommended methods ( This version has been modified to compile under Ubuntu. I haven't tested it under other distributions, your mileage may vary. IntroductionLunatic Python is a two-way bridge between Python and Lua, allowing these languages to intercommunicate. Being two-way means that it allows Lua inside Python, Python inside Lua, Lua inside Python inside Lua, Python inside Lua inside Python, and so on. Why? Even though the project was born as an experiment, it's already being used in real world projects to integrate features from both languages. Please, let me know if you use it in real world projects. Examples Lua inside Python A basic example.
Now, let's put a local object into Lua space.
Good! Is the python interface available inside the Lua interpreter?
Yes, it looks so. Let's nest some evaluations and see a local reference passing through.
Are you still following me? Good. Then you've probably noticed that the Python interpreter state inside the Lua interpreter state is the same as the outside Python we're running. Let's see that in a more comfortable way.
Things get more interesting when we start to really mix Lua and Python code.
Of course, in this case the same could be achieved easily with Python.
Python inside LuaNow, let's have a look from another perspective. The basic idea is exactly the same.
As Lua is mainly an embedding language, getting access to the batteries included in Python may be interesting.
Just like in the Python example, let's put a local object in Python space.
Again, let's grab back the reference from Python space.
Is the Lua interface available to Python?
Good. So let's do the nested trick in Lua as well.
It means that the Lua interpreter state inside the Python interpreter is the same as the outside Lua interpreter state. Let's show that in a more obvious way.
Now for the mixing example.
DocumentationTheoryThe bridging mechanism consists of creating the missing interpreter state inside the host interpreter. That is, when you run the bridging system inside Python, a Lua interpreter is created; when you run the system inside Lua, a Python interpreter is created. Once both interpreter states are available, these interpreters are provided with the necessary tools to interact freely with each other. The given tools offer not only the ability of executing statements inside the alien interpreter, but also to acquire individual objects and interact with them inside the native state. This magic is done by two special object types, which act bridging native object access to the alien interpreter state. Almost every object which is passed between Python and Lua is encapsulated in the language specific bridging object type. The only types which are not encapsulated are strings and numbers, which are converted to the native equivalent objects.
Besides that, the Lua side also has special treatment for encapsulated Python functions and methods. The most obvious way to implement calling of Python objects inside the Lua interpreter is to implement a Attribute vs. Subscript object accessAccessing an attribute or using the subscript operator in Lua give access to the same information. This behavior is reflected in the Python special object that encapsulates Lua objects, allowing Lua tables to be accessed in a more comfortable way, and also giving access to objects which use protected Python keywords (such as the print function). For example:
Using Python from the Lua side requires a little bit more attention, since Python has a more strict syntax than Lua. The later makes no distinction between attribute and subscript access, so we need some way to know what kind of access is desired at a given moment. This control is provided using two functions:
Lua inside PythonWhen executing Python as the host language, the Lua functionality is accessed by importing the lua module. When Lua is the host language, the lua module will already be available in the global Python scope. Below is a description of the functions available in the lua module.
This function will execute the given statement inside the Lua interpreter state. Examples:
This function will evaluate the given expression inside the Lua interpreter state, and return the result. It may be used to acquire any object from the Lua interpreter state. Examples:
Return the Lua global scope from the interpreter state. Examples:
Executes the require() Lua function, importing the given module. Examples:
Python inside LuaUnlike Python, Lua has no default path to its modules. Thus, the default path of the real Lua module of Lunatic Python is together with the Python module, and a python.lua stub is provided. This stub must be placed in a path accessible by the Lua require() mechanism, and once imported it will locate the real module and load it. Unfortunately, there's a minor inconvenience for our purposes regarding the Lua system which imports external shared objects. The hardcoded behavior of the loadlib() function is to load shared objects without exporting their symbols. This is usually not a problem in the Lua world, but we're going a little beyond their usual requirements here. We're loading the Python interpreter as a shared object, and the Python interpreter may load its own external modules which are compiled as shared objects as well, and these will want to link back to the symbols in the Python interpreter. Luckily, fixing this problem is easier than explaining the problem. It's just a matter of replacing the flag RTLD_NOW in the loadlib.c file of the Lua distribution by the or'ed version RTLD_NOW|RTLD_GLOBAL. This will avoid "undefined symbol" errors which could eventually happen. Below is a description of the functions available in the python module.
This function will execute the given statement inside the Python interpreter state. Examples:
This function will evaluate the given expression inside the Python interpreter state, and return the result. It may be used to acquire any object from the Python interpreter state. Examples:
Return the Python global scope dictionary from the interpreter state. Examples:
Return the Python local scope dictionary from the interpreter state. Examples:
Return the Python builtins module dictionary from the interpreter state. Examples:
Imports and returns the given Python module. Examples:
Return a copy of the given Python object with an attribute access discipline. Examples:
Return a copy of the given Python object with an index access discipline. Examples:
Return a copy of the given Python object enclosed in a Lua function closure. This is useful to use Python callable instances in places that require a Lua function. Python methods and functions are automatically converted to Lua functions, and don't require to be explicitly converted. Examples:
LicenseLunatic Python is available under the LGPL license. DownloadAvailable files: • lunatic-python-1.0.tar.bz2 Author Gustavo Niemeyer [email protected] |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论