5757 stream_eval /1 ,
5858 stream_eval /2 ,
5959 % % Module import caching
60- import /1 ,
61- import /2 ,
60+ ensure_imported /1 ,
61+ ensure_imported /2 ,
62+ is_imported /1 ,
63+ is_imported /2 ,
6264 import_stats /0 ,
6365 import_list /0 ,
6466 % % Import registry (global list applied to all interpreters)
6567 init_import_registry /0 ,
66- get_imports /0 ,
68+ all_imports /0 ,
6769 clear_imports /0 ,
6870 version /0 ,
6971 memory_stats /0 ,
@@ -371,14 +373,14 @@ init_import_registry() ->
371373% %
372374% % Example:
373375% % ```
374- % % ok = py:import (json),
376+ % % ok = py:ensure_imported (json),
375377% % {ok, Result} = py:call(json, dumps, [Data]). %% Module imported on first use
376378% % '''
377379% %
378380% % @param Module Python module name
379381% % @returns ok | {error, Reason}
380- -spec import (py_module ()) -> ok | {error , term ()}.
381- import (Module ) ->
382+ -spec ensure_imported (py_module ()) -> ok | {error , term ()}.
383+ ensure_imported (Module ) ->
382384 ModuleBin = ensure_binary (Module ),
383385 % % Reject __main__
384386 case ModuleBin of
@@ -403,15 +405,15 @@ import(Module) ->
403405% %
404406% % Example:
405407% % ```
406- % % ok = py:import (json, dumps),
408+ % % ok = py:ensure_imported (json, dumps),
407409% % {ok, Result} = py:call(json, dumps, [Data]). %% Module imported on first use
408410% % '''
409411% %
410412% % @param Module Python module name
411413% % @param Func Function name to register
412414% % @returns ok | {error, Reason}
413- -spec import (py_module (), py_func ()) -> ok | {error , term ()}.
414- import (Module , Func ) ->
415+ -spec ensure_imported (py_module (), py_func ()) -> ok | {error , term ()}.
416+ ensure_imported (Module , Func ) ->
415417 ModuleBin = ensure_binary (Module ),
416418 FuncBin = ensure_binary (Func ),
417419 % % Reject __main__
@@ -427,21 +429,52 @@ import(Module, Func) ->
427429 ok
428430 end .
429431
432+ % % @doc Check if a module is registered in the import registry.
433+ % %
434+ % % @param Module Python module name
435+ % % @returns true if module is registered, false otherwise
436+ -spec is_imported (py_module ()) -> boolean ().
437+ is_imported (Module ) ->
438+ ModuleBin = ensure_binary (Module ),
439+ case ets :info (? IMPORT_REGISTRY ) of
440+ undefined -> false ;
441+ _ -> ets :member (? IMPORT_REGISTRY , ModuleBin )
442+ end .
443+
444+ % % @doc Check if a module/function is registered in the import registry.
445+ % %
446+ % % @param Module Python module name
447+ % % @param Func Function name
448+ % % @returns true if module/function is registered, false otherwise
449+ -spec is_imported (py_module (), py_func ()) -> boolean ().
450+ is_imported (Module , Func ) ->
451+ ModuleBin = ensure_binary (Module ),
452+ FuncBin = ensure_binary (Func ),
453+ case ets :info (? IMPORT_REGISTRY ) of
454+ undefined -> false ;
455+ _ ->
456+ case ets :lookup (? IMPORT_REGISTRY , ModuleBin ) of
457+ [{_ , all }] -> true ;
458+ [{_ , FuncBin }] -> true ;
459+ _ -> false
460+ end
461+ end .
462+
430463% % @doc Get all registered imports from the global registry.
431464% %
432465% % Returns a list of {Module, Func | all} tuples representing all
433466% % modules/functions registered for automatic import.
434467% %
435468% % Example:
436469% % ```
437- % % ok = py:import (json),
438- % % ok = py:import (math, sqrt),
439- % % [{<<"json">>, all}, {<<"math">>, <<"sqrt">>}] = py:get_imports ().
470+ % % ok = py:ensure_imported (json),
471+ % % ok = py:ensure_imported (math, sqrt),
472+ % % [{<<"json">>, all}, {<<"math">>, <<"sqrt">>}] = py:all_imports ().
440473% % '''
441474% %
442475% % @returns List of {Module, Func | all} tuples
443- -spec get_imports () -> [{binary (), binary () | all }].
444- get_imports () ->
476+ -spec all_imports () -> [{binary (), binary () | all }].
477+ all_imports () ->
445478 case ets :info (? IMPORT_REGISTRY ) of
446479 undefined -> [];
447480 _ -> ets :tab2list (? IMPORT_REGISTRY )
@@ -498,7 +531,7 @@ import_stats() ->
498531% % @returns {ok, #{Module => [Func]}} map of modules to functions
499532-spec import_list () -> {ok , #{binary () => [binary ()]}} | {error , term ()}.
500533import_list () ->
501- Imports = get_imports (),
534+ Imports = all_imports (),
502535 % % Group by module
503536 Map = lists :foldl (fun ({Module , FuncOrAll }, Acc ) ->
504537 Existing = maps :get (Module , Acc , []),
0 commit comments