Skip to content

Commit a3ac462

Browse files
author
Ryan Munro
authored
Windows support (#6)
* Windows support * Too many builds...
1 parent 0143ca6 commit a3ac462

5 files changed

Lines changed: 94 additions & 29 deletions

File tree

.travis.yml

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,29 @@ os:
33
- "linux"
44
env:
55
- NODE_VERSION="7.5"
6-
- NODE_VERSION="7.3"
7-
- NODE_VERSION="7.2"
8-
- NODE_VERSION="7.1"
9-
- NODE_VERSION="7.0"
6+
# - NODE_VERSION="7.3"
7+
# - NODE_VERSION="7.2"
8+
# - NODE_VERSION="7.1"
9+
# - NODE_VERSION="7.0"
1010
- NODE_VERSION="6.1"
11-
- NODE_VERSION="6.0"
11+
# - NODE_VERSION="6.0"
1212
- NODE_VERSION="5.11"
13-
- NODE_VERSION="5.10"
14-
- NODE_VERSION="5.9"
15-
- NODE_VERSION="5.8"
16-
- NODE_VERSION="5.7"
17-
- NODE_VERSION="5.6"
18-
- NODE_VERSION="5.5"
19-
- NODE_VERSION="5.4"
20-
- NODE_VERSION="5.3"
21-
- NODE_VERSION="5.2"
22-
- NODE_VERSION="5.1"
23-
- NODE_VERSION="5.0"
13+
# - NODE_VERSION="5.10"
14+
# - NODE_VERSION="5.9"
15+
# - NODE_VERSION="5.8"
16+
# - NODE_VERSION="5.7"
17+
# - NODE_VERSION="5.6"
18+
# - NODE_VERSION="5.5"
19+
# - NODE_VERSION="5.4"
20+
# - NODE_VERSION="5.3"
21+
# - NODE_VERSION="5.2"
22+
# - NODE_VERSION="5.1"
23+
# - NODE_VERSION="5.0"
2424
- NODE_VERSION="4.4"
25-
- NODE_VERSION="4.3"
26-
- NODE_VERSION="4.2"
27-
- NODE_VERSION="4.1"
28-
- NODE_VERSION="4.0"
25+
# - NODE_VERSION="4.3"
26+
# - NODE_VERSION="4.2"
27+
# - NODE_VERSION="4.1"
28+
# - NODE_VERSION="4.0"
2929
python:
3030
- "nightly"
3131
- "3.5"
@@ -46,3 +46,6 @@ before_script:
4646
- node --version
4747
script:
4848
npm test
49+
cache:
50+
directories:
51+
- node_modules

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
# python-bridge [![Build Status](https://secure.travis-ci.org/Submersible/node-python-bridge.png?branch=master)](http://travis-ci.org/Submersible/node-python-bridge)
1+
# python-bridge [![Build Status](https://secure.travis-ci.org/Submersible/node-python-bridge.png?branch=master)](http://travis-ci.org/Submersible/node-python-bridge) [![Build Status](https://ci.appveyor.com/api/projects/status/8h64yyve684nn900/branch/master?svg=true)](https://ci.appveyor.com/project/munro/node-python-bridge/branch/master)
22

3-
Most robust and simple Python bridge. [Features](#features), and [comparisons](#comparisons) to other Python bridges below.
3+
Most robust and simple Python bridge. [Features](#features), and [comparisons](#comparisons) to other Python bridges below, supports Windows.
44

55
```
66
npm install python-bridge
@@ -198,6 +198,7 @@ _Alias to `python.isException`, this is useful if you want to import the functio
198198
* Does not affect Python's stdin, stdout, or stderr pipes.
199199
* Exception stack traces forwarded to Node for easy debugging.
200200
* Python 2 & 3 support, end-to-end tested.
201+
* Windows support, end-to-end tested.
201202
* Command queueing, with promises.
202203
* Long running Python sessions.
203204
* ES6 template tags for easy interpolation & multiline code.

appveyor.yml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
environment:
2+
matrix:
3+
- PYTHON: "C:\\Python27"
4+
- PYTHON: "C:\\Python27-x64"
5+
- PYTHON: "C:\\Python33"
6+
- PYTHON: "C:\\Python33-x64"
7+
- PYTHON: "C:\\Python34"
8+
- PYTHON: "C:\\Python34-x64"
9+
- PYTHON: "C:\\Python35"
10+
- PYTHON: "C:\\Python35-x64"
11+
12+
- nodejs_version: "7"
13+
- nodejs_version: "6"
14+
- nodejs_version: "5"
15+
- nodejs_version: "4"
16+
17+
install:
18+
- ps: Install-Product node $env:nodejs_version
19+
- "SET PATH=%PYTHON%;%PYTHON%\\Scripts;%PATH%"
20+
- "python --version"
21+
- "node --version"
22+
- "npm --version"
23+
- "npm install"
24+
25+
test_script:
26+
- "npm test"
27+
28+
build: off
29+
30+
cache:
31+
- node_modules

node_python_bridge.py

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,36 @@
44
import sys
55
import json
66
import traceback
7+
import platform
8+
import struct
79

810
NODE_CHANNEL_FD = int(os.environ['NODE_CHANNEL_FD'])
911
UNICODE_TYPE = unicode if sys.version_info[0] == 2 else str
1012

1113

14+
if platform.system() == 'Windows':
15+
# hacky reimplementation of https://github.com/nodejs/node/blob/master/deps/uv/src/win/pipe.c
16+
def read_data(f):
17+
header = f.read(16)
18+
if not header:
19+
return header
20+
try:
21+
msg_length, = struct.unpack('<Q', header[8:])
22+
return f.read(msg_length)
23+
except:
24+
raise ValueError('Error parsing msg with header: {}'.format(repr(header)))
25+
def write_data(f, data):
26+
header = struct.pack('<Q', 1) + struct.pack('<Q', len(data))
27+
f.write(header + data)
28+
f.flush()
29+
else:
30+
def read_data(f):
31+
return reader.readline()
32+
def write_data(f, data):
33+
f.write(data)
34+
f.flush()
35+
36+
1237
def format_exception(t=None, e=None, tb=None):
1338
return dict(
1439
exception=dict(
@@ -30,16 +55,19 @@ def format_exception(t=None, e=None, tb=None):
3055

3156

3257
if __name__ == '__main__':
33-
writer = os.fdopen(NODE_CHANNEL_FD, 'w')
34-
reader = os.fdopen(NODE_CHANNEL_FD, 'r')
58+
writer = os.fdopen(NODE_CHANNEL_FD, 'wb')
59+
reader = os.fdopen(NODE_CHANNEL_FD, 'rb')
3560

3661
while True:
3762
try:
3863
# Read new command
39-
line = reader.readline()
64+
line = read_data(reader)
4065
if not line:
4166
break
42-
data = json.loads(line)
67+
try:
68+
data = json.loads(line.decode('utf-8'))
69+
except ValueError:
70+
raise ValueError('Could not decode IPC data:\n{}'.format(repr(line)))
4371

4472
# Assert data saneness
4573
if data['type'] not in ['execute', 'evaluate']:
@@ -57,8 +85,8 @@ def format_exception(t=None, e=None, tb=None):
5785
t, e, tb = sys.exc_info()
5886
response = dict(type='exception', value=format_exception(t, e, tb))
5987

60-
writer.write(json.dumps(response, separators=(',', ':')) + '\n')
61-
writer.flush()
88+
data = json.dumps(response, separators=(',', ':')).encode('utf-8') + b'\n'
89+
write_data(writer, data)
6290

6391
# Closing is messy
6492
try:

test.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,9 @@ test('readme', t => {
108108
sys.stdout.flush()
109109
`.then(function () {
110110
fileWriter.end();
111-
fs.readFileAsync(OUTPUT, {encoding: 'utf8'}).then(x => t.equal(x, 'hello\nworld\n'));
111+
fs.readFileAsync(OUTPUT, {encoding: 'utf8'}).then(x => {
112+
t.equal(x.replace(/\r/g, ''), 'hello\nworld\n')
113+
});
112114
});
113115

114116
// write to Python process's stdin

0 commit comments

Comments
 (0)