Skip to content

Commit

Permalink
converting python lists to js arrays and python dicts to js objects
Browse files Browse the repository at this point in the history
  • Loading branch information
Matt Walters committed Oct 29, 2014
1 parent ba2bafd commit 8a9342a
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 0 deletions.
42 changes: 42 additions & 0 deletions npm-debug.log
@@ -0,0 +1,42 @@
0 info it worked if it ends with ok
1 verbose cli [ '/usr/local/bin/node', '/usr/local/bin/npm', 'install' ]
2 info using npm@1.4.28
3 info using node@v0.10.32
4 verbose readDependencies using package.json deps
5 verbose install where, deps [ '/Users/matt/development/electronifie/node-python',
5 verbose install [ 'bindings', 'debug', 'mocha', 'should', 'sinon' ] ]
6 info preinstall node-python@0.0.5-rc3
7 verbose readDependencies using package.json deps
8 verbose already installed skipping bindings@~1.1.1 /Users/matt/development/electronifie/node-python
9 verbose already installed skipping debug@^1.0.4 /Users/matt/development/electronifie/node-python
10 verbose already installed skipping mocha@^1.21.3 /Users/matt/development/electronifie/node-python
11 verbose already installed skipping should@^4.0.4 /Users/matt/development/electronifie/node-python
12 verbose already installed skipping sinon@^1.10.3 /Users/matt/development/electronifie/node-python
13 silly resolved []
14 info build /Users/matt/development/electronifie/node-python
15 verbose linkStuff [ false, false, false, '/Users/matt/development/electronifie' ]
16 info linkStuff node-python@0.0.5-rc3
17 verbose linkBins node-python@0.0.5-rc3
18 verbose linkMans node-python@0.0.5-rc3
19 verbose rebuildBundles node-python@0.0.5-rc3
20 verbose rebuildBundles [ '.bin', 'bindings', 'debug', 'mocha', 'should', 'sinon' ]
21 info install node-python@0.0.5-rc3
22 verbose unsafe-perm in lifecycle true
23 info node-python@0.0.5-rc3 Failed to exec install script
24 error node-python@0.0.5-rc3 install: `node-gyp rebuild`
24 error Exit status 1
25 error Failed at the node-python@0.0.5-rc3 install script.
25 error This is most likely a problem with the node-python package,
25 error not with npm itself.
25 error Tell the author that this fails on your system:
25 error node-gyp rebuild
25 error You can get their info via:
25 error npm owner ls node-python
25 error There is likely additional logging output above.
26 error System Darwin 13.3.0
27 error command "/usr/local/bin/node" "/usr/local/bin/npm" "install"
28 error cwd /Users/matt/development/electronifie/node-python
29 error node -v v0.10.32
30 error npm -v 1.4.28
31 error code ELIFECYCLE
32 verbose exit [ 1, true ]
22 changes: 22 additions & 0 deletions src/py_object_wrapper.cc
Expand Up @@ -39,6 +39,28 @@ Handle<Value> PyObjectWrapper::New(PyObject* obj) {
if(obj == Py_None) {
jsVal = Local<Value>::New(Undefined());
}
else if(PyDict_Check(obj)) {
Local<Object> dict = v8::Object::New();
PyObject *key, *value;
Py_ssize_t pos = 0;
while (PyDict_Next(obj, &pos, &key, &value)) {
Handle<Value> jsKey = PyObjectWrapper::New(key);
Handle<Value> jsValue = PyObjectWrapper::New(value);
dict->Set(jsKey, jsValue);
}
jsVal = dict;
}
else if(PyList_CheckExact(obj)) {
int size = PyList_Size(obj);
Local<Array> array = v8::Array::New(size);
PyObject* value;
for(int i = 0; i < size; i++ ){
value = PyList_GetItem(obj, i);
Handle<Value> jsValue = PyObjectWrapper::New(value);
array->Set(i, jsValue);
}
jsVal = array;
}
// double
else if(PyFloat_CheckExact(obj)) {
double d = PyFloat_AsDouble(obj);
Expand Down
14 changes: 14 additions & 0 deletions test/index.js
Expand Up @@ -95,4 +95,18 @@ describe('node-python', function () {
var type = test.getPythonTypeName(arr);
type.should.equal('list');
});
it('should convert python dicts to javascript objects', function () {
test = python.import('test2');
var value = test.getPythonValue({
value: 1
});
value.should.have.property('value', 1);
});
it('should convert python lists to javascript arrays', function () {
test = python.import('test2');
var value = test.getPythonValue([ 1, 2, 3]);
value.should.containEql(1);
value.should.containEql(2);
value.should.containEql(3);
});
});

0 comments on commit 8a9342a

Please sign in to comment.