Skip to content

Commit d4e6880

Browse files
authored
Add addImportPath method (hmenyus#22)
Allow adding folders to import path to simplify deployment of python dependencies.
1 parent 1eb16f1 commit d4e6880

4 files changed

Lines changed: 61 additions & 2 deletions

File tree

index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,11 @@ class Interpreter
8989
{
9090
return this.py.fixlink(filename);
9191
}
92+
93+
addImportPath(path)
94+
{
95+
return this.py.addImportPath(path);
96+
}
9297
}
9398

9499
let py = new Interpreter();

src/addon.cpp

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,11 +217,12 @@ namespace nodecallspython
217217
DECLARE_NAPI_METHOD("callSync", callSync),
218218
DECLARE_NAPI_METHOD("create", newclass),
219219
DECLARE_NAPI_METHOD("createSync", newclassSync),
220-
DECLARE_NAPI_METHOD("fixlink", fixlink)
220+
DECLARE_NAPI_METHOD("fixlink", fixlink),
221+
DECLARE_NAPI_METHOD("addImportPath", addImportPath)
221222
};
222223

223224
napi_value cons;
224-
CHECKNULL(napi_define_class(env, "PyInterpreter", NAPI_AUTO_LENGTH, create, nullptr, 7, properties, &cons));
225+
CHECKNULL(napi_define_class(env, "PyInterpreter", NAPI_AUTO_LENGTH, create, nullptr, 8, properties, &cons));
225226

226227
CHECKNULL(napi_create_reference(env, cons, 1, &constructor));
227228

@@ -582,6 +583,50 @@ namespace nodecallspython
582583
return nullptr;
583584
#endif
584585
}
586+
587+
static napi_value addImportPath(napi_env env, napi_callback_info info)
588+
{
589+
napi_value jsthis;
590+
size_t argc = 1;
591+
napi_value args[1];
592+
CHECKNULL(napi_get_cb_info(env, info, &argc, &args[0], &jsthis, nullptr));
593+
594+
if (argc != 1)
595+
{
596+
napi_throw_error(env, "args", "Must have 1 arguments");
597+
return nullptr;
598+
}
599+
600+
Python* obj;
601+
CHECKNULL(napi_unwrap(env, jsthis, reinterpret_cast<void**>(&obj)));
602+
603+
napi_valuetype valuetype;
604+
CHECKNULL(napi_typeof(env, args[0], &valuetype));
605+
606+
if (valuetype == napi_string)
607+
{
608+
auto path = convertString(env, args[0]);
609+
auto& py = obj->getInterpreter();
610+
611+
std::string handler;
612+
try
613+
{
614+
GIL gil;
615+
py.addImportPath(path);
616+
return nullptr;
617+
}
618+
catch(const std::exception& e)
619+
{
620+
napi_throw_error(env, "py", e.what());
621+
}
622+
}
623+
else
624+
{
625+
napi_throw_error(env, "args", "Wrong type of arguments");
626+
}
627+
628+
return nullptr;
629+
}
585630
};
586631

587632
napi_ref Python::constructor;

src/pyinterpreter.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -440,3 +440,10 @@ void PyInterpreter::release(const std::string& handler)
440440
{
441441
m_objs.erase(handler);
442442
}
443+
444+
void PyInterpreter::addImportPath(const std::string& path)
445+
{
446+
auto sysPath = PySys_GetObject((char*)"path");
447+
CPyObject dirName = PyUnicode_FromString(path.c_str());
448+
PyList_Insert(sysPath, 0, *dirName);
449+
}

src/pyinterpreter.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,5 +48,7 @@ namespace nodecallspython
4848
void release(const std::string& handler);
4949

5050
CPyObject call(const std::string& handler, const std::string& func, CPyObject& args);
51+
52+
void addImportPath(const std::string& path);
5153
};
5254
}

0 commit comments

Comments
 (0)