forked from JeanSebTr/node-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinding.cc
More file actions
50 lines (38 loc) · 1.12 KB
/
Copy pathbinding.cc
File metadata and controls
50 lines (38 loc) · 1.12 KB
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
#include <node.h>
#include <Python.h>
#include "py_object_wrapper.h"
#include "utils.h"
using namespace v8;
using namespace node;
using std::string;
Handle<Value> import(const Arguments& args) {
HandleScope scope;
if(args.Length() < 1 || !args[0]->IsString()) {
return ThrowException(
Exception::Error(String::New("I don't know how to import that."))
);
}
PyObject* module_name = PyString_FromString(*String::Utf8Value(args[0]->ToString()));
PyObject* module = PyImport_Import(module_name);
if(!module) {
return ThrowPythonException();
}
Py_XDECREF(module_name);
return scope.Close(PyObjectWrapper::New(module));
}
void init (Handle<Object> exports) {
HandleScope scope;
Py_Initialize();
PyObjectWrapper::Initialize();
// module.exports.import
exports->Set(
String::NewSymbol("import"),
FunctionTemplate::New(import)->GetFunction()
);
// module.exports.PyObject
exports->Set(
String::NewSymbol("PyObject"),
PyObjectWrapper::py_function_template->GetFunction()
);
}
NODE_MODULE(binding, init)