forked from chrisdickinson/node-python
-
Notifications
You must be signed in to change notification settings - Fork 17
/
index.js
66 lines (53 loc) · 1.76 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
var binding = require('bindings')('binding.node');
var debug = require('debug')('node-python');
var warn = require('debug')('node-python:warn');
var util = require('util');
function PythonError (message, value) {
if (arguments[0] instanceof Error) {
var error = arguments[0];
this.message = error.message;
this.stack = error.stack;
this.value = error.value;
Error.apply(this, arguments);
} else {
Error.call(this, message, value);
}
}
util.inherits(PythonError, Error);
module.exports.PythonError = PythonError;
var finalized = false;
function pythonFinalized () {
if (finalized) {
warn('node-python\'s python interpreter has already been finalized. python code cannot be executed.');
}
return finalized;
}
module.exports.eval = function (string) {
if (pythonFinalized()) throw new PythonError('node-python\'s python interpreter has already been finalized. python code cannot be executed.');
return binding.eval(string);
}
module.exports.finalize = function () {
if ( ! pythonFinalized()) {
binding.finalize();
finalized = true;
return finalized;
}
return false;
}
var _import = module.exports.import = function (string) {
if (pythonFinalized()) throw new PythonError('node-python\'s python interpreter has already been finalized. python code cannot be executed.');
var result = null;
try {
result = binding.import(string);
} catch (e) {
e = new PythonError(e);
debug(e);
throw e;
}
return result;
}
var os = _import('os');
var pythonPath = os.environ.get('PYTHONPATH');
if (pythonPath === undefined) {
debug('WARNING: PYTHONPATH environment variable is undefined. This may cause problems finding python modules. see: https://docs.python.org/2/tutorial/modules.html#the-module-search-path');
}