|
2 | 2 | import threading
|
3 | 3 | from mockredis.exceptions import ResponseError
|
4 | 4 |
|
| 5 | + |
5 | 6 | LuaLock = threading.Lock()
|
6 | 7 |
|
| 8 | +if sys.version_info[0] == 3: |
| 9 | + _string_types = (str, ) |
| 10 | + _integer_types = (int, ) |
| 11 | + _number_types = (int, float) |
| 12 | + _string_or_binary_types = (str, bytes) |
| 13 | + _binary_type = bytes |
| 14 | + _long_type = int |
| 15 | + _iteritems = lambda d, **kw: iter(d.items(**kw)) |
| 16 | +else: |
| 17 | + _string_types = (basestring, ) |
| 18 | + _integer_types = (int, long) |
| 19 | + _number_types = (int, long, float) |
| 20 | + _string_or_binary_types = (basestring, ) |
| 21 | + _binary_type = str |
| 22 | + _long_type = long |
| 23 | + _iteritems = lambda d, **kw: d.iteritems(**kw) |
| 24 | + |
7 | 25 |
|
8 | 26 | class Script(object):
|
9 | 27 | """
|
@@ -47,7 +65,14 @@ def _call(*call_args):
|
47 | 65 | response = client.call(*call_args)
|
48 | 66 | return self._python_to_lua(response)
|
49 | 67 |
|
50 |
| - lua_globals.redis = {"call": _call} |
| 68 | + def _reply_table(field, message): |
| 69 | + return lua.eval("{{{field}='{message}'}}".format(field=field, message=message)) |
| 70 | + |
| 71 | + lua_globals.redis = { |
| 72 | + 'call': _call, |
| 73 | + 'status_reply': lambda status: _reply_table('ok', status), |
| 74 | + 'error_reply': lambda error: _reply_table('err', error), |
| 75 | + } |
51 | 76 | return self._lua_to_python(lua.execute(self.script), return_status=True)
|
52 | 77 |
|
53 | 78 | @staticmethod
|
@@ -117,9 +142,9 @@ def _lua_to_python(lval, return_status=False):
|
117 | 142 | raise ResponseError(lval[i])
|
118 | 143 | pval.append(Script._lua_to_python(lval[i]))
|
119 | 144 | return pval
|
120 |
| - elif isinstance(lval, long): |
| 145 | + elif isinstance(lval, _integer_types): |
121 | 146 | # Lua number --> Python long
|
122 |
| - return long(lval) |
| 147 | + return _long_type(lval) |
123 | 148 | elif isinstance(lval, float):
|
124 | 149 | # Lua number --> Python float
|
125 | 150 | return float(lval)
|
@@ -161,17 +186,17 @@ def _python_to_lua(pval):
|
161 | 186 | # in Lua returns: {k1, v1, k2, v2, k3, v3}
|
162 | 187 | lua_dict = lua.eval("{}")
|
163 | 188 | lua_table = lua.eval("table")
|
164 |
| - for k, v in pval.iteritems(): |
| 189 | + for k, v in _iteritems(pval): |
165 | 190 | lua_table.insert(lua_dict, Script._python_to_lua(k))
|
166 | 191 | lua_table.insert(lua_dict, Script._python_to_lua(v))
|
167 | 192 | return lua_dict
|
168 |
| - elif isinstance(pval, str): |
| 193 | + elif isinstance(pval, _string_or_binary_types): |
169 | 194 | # Python string --> Lua userdata
|
170 | 195 | return pval
|
171 | 196 | elif isinstance(pval, bool):
|
172 | 197 | # Python bool--> Lua boolean
|
173 | 198 | return lua.eval(str(pval).lower())
|
174 |
| - elif isinstance(pval, (int, long, float)): |
| 199 | + elif isinstance(pval, _number_types): |
175 | 200 | # Python int --> Lua number
|
176 | 201 | lua_globals = lua.globals()
|
177 | 202 | return lua_globals.tonumber(str(pval))
|
|
0 commit comments