Skip to content

Commit a771ea3

Browse files
author
Ryan Munro
authored
[fixes #7] Optionally set unicode literals (#8)
1 parent aab3748 commit a771ea3

4 files changed

Lines changed: 35 additions & 2 deletions

File tree

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ env:
2828
# - NODE_VERSION="4.0"
2929
python:
3030
- "nightly"
31+
- "3.6"
3132
- "3.5"
3233
- "3.4"
3334
- "3.3"

appveyor.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ environment:
88
- PYTHON: "C:\\Python34-x64"
99
- PYTHON: "C:\\Python35"
1010
- PYTHON: "C:\\Python35-x64"
11+
- PYTHON: "C:\\Python36"
12+
- PYTHON: "C:\\Python36-x64"
1113

1214
- nodejs_version: "7"
1315
- nodejs_version: "6"

node_python_bridge.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from __future__ import unicode_literals
22

3+
from codeop import Compile
34
import os
45
import sys
56
import json
@@ -10,6 +11,16 @@
1011
NODE_CHANNEL_FD = int(os.environ['NODE_CHANNEL_FD'])
1112
UNICODE_TYPE = unicode if sys.version_info[0] == 2 else str
1213

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+
1324

1425
if platform.system() == 'Windows':
1526
# 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):
7788

7889
# Run Python code
7990
if data['type'] == 'execute':
80-
exec(data['code'])
91+
_exec(_compile(data['code'], '<input>', 'exec'), _locals)
8192
response = dict(type='success')
8293
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)
8496
except:
8597
t, e, tb = sys.exc_info()
8698
response = dict(type='exception', value=format_exception(t, e, tb))

test.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,24 @@ let Promise = require('bluebird');
88
let mkdirTemp = Promise.promisify(require('temp').mkdir);
99
let path = require('path');
1010

11+
test('leave __future__ alone!', t => {
12+
t.plan(2);
13+
14+
let python = pythonBridge();
15+
python.ex`import sys`;
16+
python`sys.version_info[0] > 2`.then(py3 => {
17+
python`type('').__name__`.then(x => t.equal(x, 'str'));
18+
python.ex`from __future__ import unicode_literals`;
19+
if (py3) {
20+
python`type('').__name__`.then(x => t.equal(x, 'str'));
21+
} else {
22+
python`type('').__name__`.then(x => t.equal(x, 'unicode'));
23+
}
24+
}).finally(() => {
25+
python.end();
26+
});
27+
});
28+
1129
test('readme', t => {
1230
t.test('example', t => {
1331
t.plan(2);

0 commit comments

Comments
 (0)