Skip to content

Commit

Permalink
[fixes #27] remove bluebird
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryan Munro committed Apr 24, 2019
1 parent 9b13b40 commit 356e4c8
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 50 deletions.
20 changes: 9 additions & 11 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
interface pythonBridge extends Function {
(options?: PythonBridgeOptions): PythonBridge;
}
type pythonBridge = (options?: PythonBridgeOptions) => PythonBridge;

export const pythonBridge: pythonBridge
export const pythonBridge: pythonBridge;

export interface PythonBridgeOptions {
python?: string;
Expand All @@ -14,9 +12,9 @@ export interface PythonBridgeOptions {
}

export interface PythonBridge {
(literals: TemplateStringsArray | string, ...placeholders: any[]): Bluebird.Promise<any>;
ex(literals: TemplateStringsArray | string, ...placeholders: any[]): Bluebird.Promise<void>;
lock<T>(withLock: (python: PythonBridge) => Promise<T>): Bluebird.Promise<T>
(literals: TemplateStringsArray | string, ...placeholders: any[]): Promise<any>;
ex(literals: TemplateStringsArray | string, ...placeholders: any[]): Promise<void>;
lock<T>(withLock: (python: PythonBridge) => Promise<T>): Promise<T>
pid: number;
end(): Promise<void>;
disconnect(): Promise<void>;
Expand All @@ -31,21 +29,21 @@ export function isPythonException(name: string): (e: any) => boolean;
export function isPythonException(name: string, e: any): boolean;

export class PythonException extends Error {
exception: {
public exception: {
message: string;
args: any[];
type: { name: string; module: string; }
format: string[];
};
traceback: {
public traceback: {
lineno: number;
strack: string[];
format: string[]
};
format: string[]
public format: string[]
}

export type Pipe = "pipe" | "ignore" | "inherit";
export type Pipe = 'pipe' | 'ignore' | 'inherit';
export type PipeStdin = Pipe | NodeJS.ReadableStream;
export type PipeStdout = Pipe | NodeJS.WritableStream;
export type PipeStderr = Pipe | NodeJS.WritableStream;
Expand Down
27 changes: 21 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
'use strict';

let Promise = require('bluebird');
let path = require('path');
let child_process = Promise.promisifyAll(require('child_process'));
let child_process = require('child_process');

const PYTHON_BRIDGE_SCRIPT = path.join(__dirname, 'node_python_bridge.py');

Expand Down Expand Up @@ -142,11 +141,11 @@ function singleQueue() {
last = new Promise(resolve => {
done = resolve;
});
return new Promise((resolve, reject) => {
wait.finally(() => {
Promise.try(f).then(resolve, reject);
return promiseFinally(new Promise((resolve, reject) => {
promiseFinally(wait,() => {
promiseTry(f).then(resolve, reject);
});
}).finally(() => done());
}), () => done());
};
}

Expand Down Expand Up @@ -209,6 +208,22 @@ function serializePython(value) {
}
}

function promiseTry(f) {
// return Promise.try(f);
return new Promise((resolve, reject) => {
try {
resolve(f());
} catch (e) {
reject(e)
}
});
}

function promiseFinally(promise, cb) {
promise.then(cb, cb);
return promise;
}

pythonBridge.pythonBridge = pythonBridge;
pythonBridge.PythonException = PythonException;
pythonBridge.PythonBridgeNotConnected = PythonBridgeNotConnected;
Expand Down
4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,9 @@
],
"author": "Ryan Munro <ryan@submersible.io>",
"license": "MIT",
"dependencies": {
"bluebird": "^3.5.0"
},
"devDependencies": {
"@types/node": "^8.0.14",
"es6-promisify": "^6.0.1",
"tap": "^10.7.0",
"temp": "^0.8.3",
"tslint": "^5.5.0",
Expand Down
110 changes: 80 additions & 30 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ let pythonBridge = require('./');
let PythonException = pythonBridge.PythonException;
let isPythonException = pythonBridge.isPythonException;
let test = require('tap').test;
let Promise = require('bluebird');
let mkdirTemp = Promise.promisify(require('temp').mkdir);
let path = require('path');
const promisify = require('es6-promisify').promisify;
const mkdirTemp = promisify(require('temp').mkdir);

test('leave __future__ alone!', t => {
t.plan(2);
Expand All @@ -21,7 +21,9 @@ test('leave __future__ alone!', t => {
} else {
python`type('').__name__`.then(x => t.equal(x, 'unicode'));
}
}).finally(() => {
}).then(() => {
python.end();
}, () => {
python.end();
});
});
Expand Down Expand Up @@ -81,14 +83,17 @@ test('readme', t => {
}, 100));
}).then(x => t.equal(x, 444));

python`hello + 321`.catch(isPythonException('NameError'), () => t.ok(true));
python`hello + 321`.catch(e => {
if (isPythonException('NameError', e)) {
t.ok(true);
}
});
python.ex`hello = 123`;
python`hello + 321`.then(x => t.equal(x, 444));

python.disconnect();
});


t.test('lock recommended', t => {
t.plan(1);

Expand All @@ -111,9 +116,8 @@ test('readme', t => {
mkdirTemp('node-python-bridge-test').then(tempdir => {
const OUTPUT = path.join(tempdir, 'output.txt');

let Promise = require('bluebird');
let fs = Promise.promisifyAll(require('fs'));

const fs = require('fs');
const readFileAsync = promisify(fs.readFile);
let fileWriter = fs.createWriteStream(OUTPUT);

python.stdout.pipe(fileWriter);
Expand All @@ -126,7 +130,7 @@ test('readme', t => {
sys.stdout.flush()
`.then(function () {
fileWriter.end();
fs.readFileAsync(OUTPUT, {encoding: 'utf8'}).then(x => {
readFileAsync(OUTPUT, {encoding: 'utf8'}).then(x => {
t.equal(x.replace(/\r/g, ''), 'hello\nworld\n')
});
});
Expand All @@ -147,24 +151,22 @@ test('readme', t => {

let python = pythonBridge();

let Promise = require('bluebird');

python.ex`
promiseTimeout(python.ex`
from time import sleep
sleep(9000)
`.timeout(100).then(x => {
`, 100).then(x => {
t.ok(false);
}).catch(Promise.TimeoutError, exit_code => {
python.kill('SIGKILL');
t.ok(true);
python = pythonBridge();
}).catch(e => {
if (e instanceof PromiseTimeout) {
python.kill('SIGKILL');
t.ok(true);
python = pythonBridge();
}
});
setTimeout(() => {
python`1 + 2`.then(x => t.equal(x, 3));
python.disconnect();
}, 200);

// python.disconnect();
});

t.test('exceptions', t => {
Expand All @@ -176,17 +178,29 @@ test('readme', t => {
hello = 123
print(hello + world)
world = 321
`.catch(python.Exception, () => t.ok(true));
`.catch(e => {
if (e instanceof python.Exception) {
t.ok(true);
}
});

python.ex`
hello = 123
print(hello + world)
world = 321
`.catch(pythonBridge.PythonException, () => t.ok(true));
`.catch(e => {
if (e instanceof pythonBridge.PythonException) {
t.ok(true);
}
});

function pyDivide(numerator, denominator) {
return python`${numerator} / ${denominator}`
.catch(python.isException('ZeroDivisionError'), () => Promise.resolve(Infinity));
.catch(e => {
if (python.isException('ZeroDivisionError', e)) {
return Promise.resolve(Infinity);
}
});
}
pyDivide(1, 0).then(x => {
t.equal(x, Infinity);
Expand All @@ -195,7 +209,11 @@ test('readme', t => {
pyDivide(6, 2).then(x => t.equal(x, 3));

python`1 / 0`
.catch(pythonBridge.isPythonException('ZeroDivisionError'), () => Promise.resolve(Infinity))
.catch(e => {
if (pythonBridge.isPythonException('ZeroDivisionError', e)) {
return Promise.resolve(Infinity);
}
})
.then(x => t.equal(x, 1 / 0));

python.disconnect();
Expand All @@ -218,14 +236,18 @@ test('nested locks', t => {
});
return new Promise(resolve => setTimeout(() => {
python.ex`del hello`.then(() => {
return Promise.all([$value1, $value2]).spread((value1, value2) => {
resolve(value1 + value2);
return Promise.all([$value1, $value2]).then((args) => {
resolve(args[0] + args[1]);
})
});
}, 100));
}).then(x => t.equal(x, 1443));

python`hello + 808`.catch(isPythonException('NameError'), () => t.ok(true));
python`hello + 808`.catch(e => {
if (isPythonException('NameError', e)) {
t.ok(true);
}
});
python.ex`hello = 123`;
python`hello + 321`.then(x => t.equal(x, 444));

Expand All @@ -238,11 +260,11 @@ test('exceptions', t => {
let python = pythonBridge();
python`1 / 0`.catch(() => t.ok(true));
python`1 / 0`
.catch(ReferenceError, () => t.ok(false))
.catch(PythonException, () => t.ok(true));
.catch(e => { if (e instanceof ReferenceError) { t.ok(false); } else { return Promise.reject(e); }})
.catch(e => { if (e instanceof PythonException) { t.ok(true); }});
python`1 / 0`
.catch(isPythonException('IOError'), () => t.ok(false))
.catch(isPythonException('ZeroDivisionError'), () => t.ok(true));
.catch(e => { if (isPythonException('IOError', e)) { t.ok(false) } else { return Promise.reject(e); }})
.catch(e => { if (isPythonException('ZeroDivisionError', e)) { t.ok(true) }});
python.end();
});

Expand Down Expand Up @@ -274,3 +296,31 @@ test('bug #24 support more than just numbers and strings', t => {
python`(lambda x: x)(${s})`.then(x => t.deepEqual(x, s));
python.end();
});

function promiseTimeout(promise, delay) {
return new Promise((resolve, reject) => {
let timer = setTimeout(() => {
timer = null;
reject(new PromiseTimeout());
}, delay);

promise.then(function () {
if (timer === null) {
return;
}
clearTimeout(timer);
timer = null;
resolve.apply(this, arguments);
}, function () {
if (timer === null) {
return;
}
clearTimeout(timer);
timer = null;
reject.apply(this, arguments);
})
})
}

class PromiseTimeout extends Error {
}

0 comments on commit 356e4c8

Please sign in to comment.