|
1 | 1 | from __future__ import unicode_literals |
2 | 2 |
|
| 3 | +from codeop import Compile |
3 | 4 | import os |
4 | 5 | import sys |
5 | 6 | import json |
|
10 | 11 | NODE_CHANNEL_FD = int(os.environ['NODE_CHANNEL_FD']) |
11 | 12 | UNICODE_TYPE = unicode if sys.version_info[0] == 2 else str |
12 | 13 |
|
| 14 | +if sys.version_info[0] <= 2: |
| 15 | + # print('PY2') |
| 16 | + def _exec(_code_, _globs_): |
| 17 | + exec('exec _code_ in _globs_') |
| 18 | +else: |
| 19 | + _exec = getattr(__builtins__, 'exec') |
| 20 | + |
| 21 | +_locals = {'__name__': '__console__', '__doc__': None} |
| 22 | +_compile = Compile() |
| 23 | + |
13 | 24 |
|
14 | 25 | if platform.system() == 'Windows': |
15 | 26 | # hacky reimplementation of https://github.com/nodejs/node/blob/master/deps/uv/src/win/pipe.c |
@@ -77,10 +88,11 @@ def format_exception(t=None, e=None, tb=None): |
77 | 88 |
|
78 | 89 | # Run Python code |
79 | 90 | if data['type'] == 'execute': |
80 | | - exec(data['code']) |
| 91 | + _exec(_compile(data['code'], '<input>', 'exec'), _locals) |
81 | 92 | response = dict(type='success') |
82 | 93 | else: |
83 | | - response = dict(type='success', value=eval(data['code'])) |
| 94 | + value = eval(_compile(data['code'], '<input>', 'eval'), _locals) |
| 95 | + response = dict(type='success', value=value) |
84 | 96 | except: |
85 | 97 | t, e, tb = sys.exc_info() |
86 | 98 | response = dict(type='exception', value=format_exception(t, e, tb)) |
|
0 commit comments