|
1 | | -""" |
2 | | ->>> lg = lua.globals() |
3 | | ->>> lg == lg._G |
4 | | -True |
5 | | ->>> lg._G == lg._G |
6 | | -True |
7 | | ->>> lg._G == lg['_G'] |
8 | | -True |
9 | | -
|
10 | | ->>> lg.foo = 'bar' |
11 | | ->>> lg.foo == u'bar' |
12 | | -True |
13 | | -
|
14 | | ->>> lg.tmp = [] |
15 | | ->>> lg.tmp |
16 | | -[] |
17 | | -
|
18 | | ->>> lua.execute("x = {1, 2, 3, foo = {4, 5}}") |
19 | | ->>> lg.x[1], lg.x[2], lg.x[3] |
20 | | -(1..., 2..., 3...) |
21 | | ->>> lg.x['foo'][1], lg.x['foo'][2] |
22 | | -(4..., 5...) |
23 | | -
|
24 | | ->>> lua.require |
25 | | -<built-in function require> |
26 | | -
|
27 | | ->>> lg.string |
28 | | -<Lua table at 0x...> |
29 | | ->>> lg.string.lower |
30 | | -<Lua function at 0x...> |
31 | | ->>> lg.string.lower("Hello world!") == u'hello world!' |
32 | | -True |
33 | | -
|
34 | | ->>> d = {} |
35 | | ->>> lg.d = d |
36 | | ->>> lua.execute("d['key'] = 'value'") |
37 | | ->>> d |
38 | | -{...'key': ...'value'} |
39 | | -
|
40 | | ->>> d2 = lua.eval("d") |
41 | | ->>> d is d2 |
42 | | -True |
43 | | -
|
44 | | ->>> lua.execute("python = require 'python'") |
45 | | ->>> lua.eval("python") |
46 | | -<Lua table at 0x...> |
47 | | -
|
48 | | ->>> obj |
49 | | -<MyClass> |
50 | | -
|
51 | | ->>> lua.eval("python.eval 'obj'") |
52 | | -<MyClass> |
53 | | -
|
54 | | ->>> lua.eval(\"\"\"python.eval([[lua.eval('python.eval("obj")')]])\"\"\") |
55 | | -<MyClass> |
56 | | -
|
57 | | ->>> lua.execute("pg = python.globals()") |
58 | | ->>> lua.eval("pg.obj") |
59 | | -<MyClass> |
60 | | -
|
61 | | ->>> def show(key, value): |
62 | | -... print("key is %s and value is %s" % (repr(key), repr(value))) |
63 | | -... |
64 | | ->>> asfunc = lua.eval("python.asfunc") |
65 | | ->>> asfunc |
66 | | -<Lua function at 0x...> |
67 | | -
|
68 | | ->>> l = ['a', 'b', 'c'] |
69 | | ->>> t = lua.eval("{a=1, b=2, c=3}") |
70 | | ->>> for k in l: |
71 | | -... show(k, t[k]) |
72 | | -key is 'a' and value is 1... |
73 | | -key is 'b' and value is 2... |
74 | | -key is 'c' and value is 3... |
75 | | -
|
76 | | -""" |
77 | | - |
78 | | -import sys, os |
79 | | -sys.path.append(os.getcwd()) |
80 | | - |
81 | | - |
82 | 1 | class MyClass: |
83 | | - def __repr__(self): return '<MyClass>' |
| 2 | + """ |
| 3 | + >>> import lua |
| 4 | + >>> lg = lua.globals() |
| 5 | + >>> lg == lg._G |
| 6 | + True |
| 7 | + >>> lg._G == lg._G |
| 8 | + True |
| 9 | + >>> lg._G == lg['_G'] |
| 10 | + True |
| 11 | +
|
| 12 | + >>> lg.foo = 'bar' |
| 13 | + >>> lg.foo == u'bar' |
| 14 | + True |
| 15 | +
|
| 16 | + >>> lg.tmp = [] |
| 17 | + >>> lg.tmp |
| 18 | + [] |
| 19 | +
|
| 20 | + >>> lua.execute("x = {1, 2, 3, foo = {4, 5}}") |
| 21 | + >>> lg.x[1], lg.x[2], lg.x[3] |
| 22 | + (1, 2, 3) |
| 23 | + >>> lg.x['foo'][1], lg.x['foo'][2] |
| 24 | + (4, 5) |
| 25 | +
|
| 26 | + >>> lua.require |
| 27 | + <built-in function require> |
| 28 | +
|
| 29 | + >>> lg.string # doctest: +ELLIPSIS |
| 30 | + <Lua table at 0x...> |
| 31 | + >>> lg.string.lower # doctest: +ELLIPSIS |
| 32 | + <Lua function at 0x...> |
| 33 | + >>> lg.string.lower("Hello world!") == u'hello world!' |
| 34 | + True |
| 35 | +
|
| 36 | + >>> d = {} |
| 37 | + >>> lg.d = d |
| 38 | + >>> lua.execute("d['key'] = 'value'") |
| 39 | + >>> d |
| 40 | + {'key': 'value'} |
| 41 | +
|
| 42 | + >>> d2 = lua.eval("d") |
| 43 | + >>> d is d2 |
| 44 | + True |
| 45 | +
|
| 46 | + # TODO: This fails without lua --local make |
| 47 | + # >>> lua.execute("python = require 'python'") |
| 48 | + # >>> lua.eval("python") # doctest: +ELLIPSIS |
| 49 | + # <Lua table at 0x...> |
| 50 | +
|
| 51 | + >>> obj |
| 52 | + <MyClass> |
| 53 | +
|
| 54 | + # TODO: This fails without lua --local make |
| 55 | + # >>> lua.eval("python.eval 'obj'") |
| 56 | + # <MyClass> |
| 57 | +
|
| 58 | + # TODO: This fails without lua --local make |
| 59 | + # >>> lua.eval(\"\"\"python.eval([[lua.eval('python.eval("obj")')]])\"\"\") |
| 60 | + # <MyClass> |
| 61 | +
|
| 62 | + # TODO: This fails without lua --local make |
| 63 | + # >>> lua.execute("pg = python.globals()") |
| 64 | + # >>> lua.eval("pg.obj") |
| 65 | + # <MyClass> |
| 66 | +
|
| 67 | + >>> def show(key, value): |
| 68 | + ... print("key is %s and value is %s" % (repr(key), repr(value))) |
| 69 | + ... |
| 70 | +
|
| 71 | + # TODO: This fails without lua --local make |
| 72 | + # >>> asfunc = lua.eval("python.asfunc") |
| 73 | + # >>> asfunc |
| 74 | + # <Lua function at 0x...> |
| 75 | +
|
| 76 | + >>> l = ['a', 'b', 'c'] |
| 77 | + >>> t = lua.eval("{a=1, b=2, c=3}") |
| 78 | + >>> for k in l: |
| 79 | + ... show(k, t[k]) |
| 80 | + key is 'a' and value is 1 |
| 81 | + key is 'b' and value is 2 |
| 82 | + key is 'c' and value is 3 |
| 83 | + """ |
| 84 | + |
| 85 | + def __repr__(self): |
| 86 | + return "<MyClass>" |
| 87 | + |
84 | 88 |
|
85 | 89 | obj = MyClass() |
86 | 90 |
|
87 | 91 |
|
88 | | -if __name__ == '__main__': |
89 | | - import lua |
| 92 | +if __name__ == "__main__": |
90 | 93 | import doctest |
91 | | - doctest.testmod(optionflags=doctest.ELLIPSIS) |
| 94 | + |
| 95 | + doctest.testmod() |
0 commit comments