Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

pynerl:exec(FilePath), runs a file as in "python FilePath" (the name …

…of the module is '__main__')
  • Loading branch information...
commit 84a689008917bff8b6c093a060ce5da6ffb9b968 1 parent bb430da
@marianoguerra authored
Showing with 43 additions and 2 deletions.
  1. +6 −0 .gitignore
  2. +33 −1 pynerl.c
  3. +4 −1 pynerl.erl
View
6 .gitignore
@@ -0,0 +1,6 @@
+*.beam
+lexer.erl
+parser.erl
+src/*.fn
+erl_crash.dump
+.*.swp
View
34 pynerl.c
@@ -155,6 +155,37 @@ static ERL_NIF_TERM pynerl_eval(ErlNifEnv* env, int argc, const ERL_NIF_TERM arg
return eResult;
}
+static ERL_NIF_TERM pynerl_exec(ErlNifEnv* env, int arc, const ERL_NIF_TERM argv[]) {
+ Py_Initialize();
+ char buff[BUFF_SIZE];
+ ERL_NIF_TERM eResult;
+
+ PyObject *pdict, *pResult;
+
+ // TODO: error checking
+ enif_get_string(env, argv[0], buff, BUFF_SIZE, ERL_NIF_LATIN1);
+
+ pdict = PyDict_New();
+ PyDict_SetItemString(pdict, "__builtins__", PyEval_GetBuiltins());
+ PyDict_SetItemString(pdict, "__name__", PyUnicode_FromString("__main__"));
+
+ pResult = PyRun_FileEx(fopen(buff, "r"), buff, Py_file_input, pdict, pdict, 1);
+
+ if (pResult == NULL) {
+ eResult = pynerl_make_error(env, "exception", "Exception while running file");
+ }
+ else {
+ eResult = enif_make_atom(env, "ok");
+ Py_DECREF(pResult);
+ }
+
+ Py_DECREF(pdict);
+
+ Py_Finalize();
+
+ return eResult;
+}
+
static ERL_NIF_TERM pynerl_call(ErlNifEnv* env, int arc, const ERL_NIF_TERM argv[]) {
Py_Initialize();
@@ -208,7 +239,8 @@ static ERL_NIF_TERM pynerl_call(ErlNifEnv* env, int arc, const ERL_NIF_TERM argv
static ErlNifFunc nif_funcs[] = {
{"eval", 2, pynerl_eval},
- {"call", 3, pynerl_call}
+ {"call", 3, pynerl_call},
+ {"exec", 1, pynerl_exec}
};
ERL_NIF_INIT(pynerl,nif_funcs,NULL,NULL,NULL,NULL)
View
5 pynerl.erl
@@ -1,5 +1,5 @@
-module(pynerl).
--export([init/0, eval/2, call/3]).
+-export([init/0, eval/2, call/3, exec/1]).
-on_load(init/0).
@@ -12,3 +12,6 @@ eval(_Str, _Var) ->
call(_Module, _Function, _Args) ->
"NIF library not loaded".
+exec(_FilePath) ->
+ "NIF library not loaded".
+
Please sign in to comment.
Something went wrong with that request. Please try again.