|
35 | 35 |
|
36 | 36 | -export([ |
37 | 37 | start_link/2, |
| 38 | + new/1, |
38 | 39 | stop/1, |
| 40 | + destroy/1, |
| 41 | + call/4, |
39 | 42 | call/5, |
40 | 43 | call/6, |
41 | 44 | call/7, |
| 45 | + eval/2, |
42 | 46 | eval/3, |
43 | 47 | eval/4, |
44 | 48 | eval/5, |
@@ -121,6 +125,38 @@ stop(Ctx) when is_pid(Ctx) -> |
121 | 125 | ok |
122 | 126 | end. |
123 | 127 |
|
| 128 | +%% @doc Create a new context with options map. |
| 129 | +%% |
| 130 | +%% Options: |
| 131 | +%% - `mode' - Context mode (auto | subinterp | worker | owngil), default: auto |
| 132 | +%% |
| 133 | +%% @param Opts Options map |
| 134 | +%% @returns {ok, Pid} | {error, Reason} |
| 135 | +-spec new(map()) -> {ok, context()} | {error, term()}. |
| 136 | +new(Opts) when is_map(Opts) -> |
| 137 | + Mode = maps:get(mode, Opts, auto), |
| 138 | + Id = erlang:unique_integer([positive]), |
| 139 | + start_link(Id, Mode). |
| 140 | + |
| 141 | +%% @doc Alias for stop/1 for API consistency. |
| 142 | +-spec destroy(context()) -> ok. |
| 143 | +destroy(Ctx) -> |
| 144 | + stop(Ctx). |
| 145 | + |
| 146 | +%% @doc Call a Python function with empty kwargs. |
| 147 | +%% |
| 148 | +%% This is a convenience wrapper for call/5 that defaults Kwargs to #{}. |
| 149 | +%% |
| 150 | +%% @param Ctx Context process |
| 151 | +%% @param Module Python module name |
| 152 | +%% @param Func Function name |
| 153 | +%% @param Args List of arguments |
| 154 | +%% @returns {ok, Result} | {error, Reason} |
| 155 | +-spec call(context(), atom() | binary(), atom() | binary(), list()) -> |
| 156 | + {ok, term()} | {error, term()}. |
| 157 | +call(Ctx, Module, Func, Args) -> |
| 158 | + call(Ctx, Module, Func, Args, #{}). |
| 159 | + |
124 | 160 | %% @doc Call a Python function. |
125 | 161 | %% |
126 | 162 | %% @param Ctx Context process |
@@ -181,6 +217,18 @@ call(Ctx, Module, Func, Args, Kwargs, Timeout, EnvRef) when is_pid(Ctx), is_refe |
181 | 217 | {error, timeout} |
182 | 218 | end. |
183 | 219 |
|
| 220 | +%% @doc Evaluate a Python expression with empty locals. |
| 221 | +%% |
| 222 | +%% This is a convenience wrapper for eval/3 that defaults Locals to #{}. |
| 223 | +%% |
| 224 | +%% @param Ctx Context process |
| 225 | +%% @param Code Python code to evaluate |
| 226 | +%% @returns {ok, Result} | {error, Reason} |
| 227 | +-spec eval(context(), binary() | string()) -> |
| 228 | + {ok, term()} | {error, term()}. |
| 229 | +eval(Ctx, Code) -> |
| 230 | + eval(Ctx, Code, #{}). |
| 231 | + |
184 | 232 | %% @doc Evaluate a Python expression. |
185 | 233 | %% |
186 | 234 | %% @param Ctx Context process |
|
0 commit comments