You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+66-42Lines changed: 66 additions & 42 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -36,7 +36,7 @@ Examples
36
36
Lua inside Python
37
37
A basic example.
38
38
39
-
```
39
+
```lua
40
40
>>>importlua
41
41
>>>lg=lua.globals()
42
42
>>>lg.string
@@ -49,7 +49,7 @@ A basic example.
49
49
50
50
Now, let's put a local object into Lua space.
51
51
52
-
```
52
+
```lua
53
53
>>>d= {}
54
54
>>>lg.d=d
55
55
>>>lua.execute("d['key'] = 'value'")
@@ -65,14 +65,14 @@ Good!
65
65
66
66
Is the python interface available inside the Lua interpreter?
67
67
68
-
```
68
+
```lua
69
69
>>>lua.eval("python")
70
70
<Luatableat0x81c7540>
71
71
```
72
72
73
73
Yes, it looks so. Let's nest some evaluations and see a local reference passing through.
74
74
75
-
```
75
+
```python
76
76
>>>classMyClass: pass
77
77
...
78
78
>>> obj = MyClass()
@@ -84,15 +84,15 @@ Yes, it looks so. Let's nest some evaluations and see a local reference passing
84
84
85
85
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.
86
86
87
-
```
87
+
```python
88
88
>>> lua.execute("pg = python.globals()")
89
89
>>> lua.eval("pg.obj")
90
90
<__main__.MyClass instance at 0x403ccb4c>
91
91
```
92
92
93
93
Things get more interesting when we start to really mix Lua and Python code.
94
94
95
-
```
95
+
```python
96
96
>>> table = lua.eval("table")
97
97
>>>defshow(key, value):
98
98
...print"key is %s and value is %s"% (`key`, `value`)
@@ -107,7 +107,7 @@ key is 'b' and value is 2
107
107
108
108
Of course, in this case the same could be achieved easily with Python.
109
109
110
-
```
110
+
```python
111
111
>>>defshow(key, value):
112
112
...print"key is %s and value is %s"% (`key`, `value`)
113
113
...
@@ -125,7 +125,7 @@ Python inside Lua
125
125
126
126
Now, let's have a look from another perspective. The basic idea is exactly the same.
127
127
128
-
```
128
+
```python
129
129
> require("python")
130
130
> python.execute("import string")
131
131
> pg = python.globals()
@@ -137,7 +137,7 @@ hello world!
137
137
138
138
As Lua is mainly an embedding language, getting access to the batteries included in Python may be interesting.
139
139
140
-
```
140
+
```python
141
141
> re = python.import("re")
142
142
> pattern = re.compile("^Hel(lo) world!")
143
143
> match = pattern.match("Hello world!")
@@ -147,7 +147,7 @@ lo
147
147
148
148
Just like in the Python example, let's put a local object in Python space.
149
149
150
-
```
150
+
```python
151
151
> d = {}
152
152
> pg.d = d
153
153
> python.execute("d['key'] = 'value'")
@@ -157,22 +157,22 @@ key value
157
157
158
158
Again, let's grab back the reference from Python space.
159
159
160
-
```
160
+
```python
161
161
> d2 = python.eval("d")
162
162
>print(d == d2)
163
163
true
164
164
```
165
165
166
166
Is the Lua interface available to Python?
167
167
168
-
```
168
+
```pythom
169
169
> =python.eval("lua")
170
170
<module 'lua' (built-in)>
171
171
```
172
172
173
173
Good. So let's do the nested trick in Lua as well.
174
174
175
-
```
175
+
```python
176
176
> t = {}
177
177
>=t
178
178
table: 0x80fbdb8
@@ -183,15 +183,15 @@ table: 0x80fbdb8
183
183
184
184
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.
185
185
186
-
```
186
+
```lua
187
187
>python.execute("lg = lua.globals()")
188
188
>=python.eval("lg.t")
189
189
table: 0x80fbdb8
190
190
```
191
191
192
192
Now for the mixing example.
193
193
194
-
```
194
+
```lua
195
195
>functionnotthree(num)
196
196
>>return (num~=3)
197
197
>>end
@@ -218,7 +218,7 @@ Attribute vs. Subscript object access
218
218
219
219
Accessing 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:
220
220
221
-
```
221
+
```python
222
222
>>> string = lua.eval("string")
223
223
>>> string.lower
224
224
<Lua function at 0x81c6bf8>
@@ -228,7 +228,7 @@ Accessing an attribute or using the subscript operator in Lua give access to the
228
228
229
229
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: `asindx()` and `asattr()`. These functions receive a single Python object as parameter, and return the same object with the given access discipline. Notice that dictionaries and lists use the index discipline by default, while other objects use the attribute discipline. For example:
230
230
231
-
```
231
+
```python
232
232
>dict= python.eval("{}")
233
233
>=dict.keys
234
234
nil
@@ -251,20 +251,22 @@ Lua inside Python
251
251
When 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.
252
252
Below is a description of the functions available in the lua module.
253
253
254
-
`lua.execute(statement)`
254
+
```python
255
+
lua.execute(statement)
256
+
```
255
257
256
258
This function will execute the given statement inside the Lua interpreter state.
257
259
Examples:
258
260
259
-
```
261
+
```lua
260
262
>>>lua.execute("foo = 'bar'")
261
263
lua.eval(expression)
262
264
```
263
265
264
266
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.
265
267
Examples:
266
268
267
-
```
269
+
```lua
268
270
>>>lua.eval("'foo'..2")
269
271
'foo2'
270
272
>>>lua.eval('string')
@@ -274,13 +276,15 @@ Examples:
274
276
'hello world!'
275
277
```
276
278
277
-
`lua.globals()`
279
+
```lua
280
+
lua.globals()
281
+
```
278
282
279
283
Return the Lua global scope from the interpreter state.
280
284
281
285
Examples:
282
286
283
-
```
287
+
```lua
284
288
>>>lg=lua.globals()
285
289
>>>lg.string.lower("Hello world!")
286
290
'hello world!'
@@ -292,12 +296,14 @@ Examples:
292
296
Helloworld!
293
297
```
294
298
295
-
`lua.require(name)`
299
+
```lua
300
+
lua.require(name)
301
+
```
296
302
297
303
Executes the require() Lua function, importing the given module.
298
304
299
305
Examples:
300
-
```
306
+
```lua
301
307
>>>lua.require("testmod")
302
308
True
303
309
>>>lua.execute("func()")
@@ -312,22 +318,26 @@ Unlike Python, Lua has no default path to its modules. Thus, the default path of
312
318
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.
313
319
Below is a description of the functions available in the python module.
314
320
315
-
`python.execute(statement)`
321
+
```python
322
+
python.execute(statement)
323
+
```
316
324
317
325
This function will execute the given statement inside the Python interpreter state.
318
326
Examples:
319
327
320
-
```
328
+
```python
321
329
> python.execute("foo = 'bar'")
322
330
```
323
331
324
-
`python.eval(expression)`
332
+
```python
333
+
python.eval(expression)
334
+
```
325
335
326
336
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.
Return the Python global scope dictionary from the interpreter state.
342
354
Examples:
343
355
344
-
```
356
+
```python
345
357
> python.execute("import string")
346
358
> pg = python.globals()
347
359
>=pg.string.lower("Hello world!")
@@ -350,12 +362,14 @@ hello world!
350
362
hello world!
351
363
```
352
364
353
-
`python.locals()`
365
+
```python
366
+
python.locals()
367
+
```
354
368
355
369
Return the Python local scope dictionary from the interpreter state.
356
370
Examples:
357
371
358
-
```
372
+
```python
359
373
> function luafunc()
360
374
>>print(python.globals().var)
361
375
>>print(python.locals().var)
@@ -366,34 +380,40 @@ nil
366
380
value
367
381
```
368
382
369
-
`python.builtins()`
383
+
```python
384
+
python.builtins()
385
+
```
370
386
371
387
Return the Python builtins module dictionary from the interpreter state.
372
388
Examples:
373
389
374
-
```
390
+
```python
375
391
> pb = python.builtins()
376
392
>=pb.len("Hello world!")
377
393
12
378
394
```
379
395
380
-
`python.import(name)`
396
+
```python
397
+
python.import(name)
398
+
```
381
399
382
400
Imports and returns the given Python module.
383
401
Examples:
384
402
385
-
```
403
+
```python
386
404
> os = python.import("os")
387
405
>=os.getcwd()
388
406
/home/niemeyer/src/lunatic-python
389
407
```
390
408
391
-
`python.asattr(pyobj)`
409
+
```python
410
+
python.asattr(pyobj)
411
+
```
392
412
393
413
Return a copy of the given Python object with an attribute access discipline.
394
414
Examples:
395
415
396
-
```
416
+
```python
397
417
>dict= python.eval("{}")
398
418
>=dict.keys
399
419
nil
@@ -410,12 +430,14 @@ function: 0x80fa9b8
410
430
['keys']
411
431
```
412
432
413
-
`python.asindx(pyobj)`
433
+
```python
434
+
python.asindx(pyobj)
435
+
```
414
436
415
437
Return a copy of the given Python object with an index access discipline.
416
438
Examples:
417
439
418
-
```
440
+
```python
419
441
> buffer = python.eval("buffer('foobar')")
420
442
>=buffer[0]
421
443
stdin:1: unknown attribute in python object
@@ -428,12 +450,14 @@ stack traceback:
428
450
f
429
451
```
430
452
431
-
`python.asfunc(pyobj)`
453
+
```python
454
+
python.asfunc(pyobj)
455
+
```
432
456
433
457
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.
0 commit comments