5151#include <deemon/dex.h>
5252#endif /* !CONFIG_NO_DEX */
5353
54+ #include "../runtime/kwlist.h"
5455#include "../runtime/runtime_error.h"
5556#include "../runtime/strings.h"
5657
@@ -2433,6 +2434,72 @@ INTERN struct type_attr module_attr = {
24332434
24342435#ifdef CONFIG_EXPERIMENTAL_MODULE_DIRECTORIES
24352436#define module_get_code DeeModule_GetRootCode
2437+
2438+ PRIVATE WUNUSED NONNULL ((1 )) DREF DeeObject * DCALL
2439+ module_get_directory (DeeModuleObject * __restrict self ) {
2440+ DREF DeeObject * result = DeeModule_GetDirectory (self );
2441+ Dee_XIncref (result );
2442+ return result ;
2443+ }
2444+
2445+ PRIVATE WUNUSED NONNULL ((1 )) DREF DeeObject * DCALL
2446+ module_get_filename (DeeModuleObject * __restrict self ) {
2447+ DREF DeeObject * result = DeeModule_GetFileName (self );
2448+ if (result == ITER_DONE )
2449+ result = DeeRT_ErrUnboundAttrCStr (self , "__filename__" );
2450+ return result ;
2451+ }
2452+
2453+ PRIVATE WUNUSED NONNULL ((1 )) int DCALL
2454+ module_bound_filename (DeeModuleObject * __restrict self ) {
2455+ return Dee_BOUND_FROMBOOL (self -> mo_absname != NULL );
2456+ }
2457+
2458+ PRIVATE WUNUSED NONNULL ((1 )) DREF DeeObject * DCALL
2459+ module_get_path (DeeModuleObject * __restrict self ) {
2460+ DREF DeeObject * result = DeeModule_GetFileName (self );
2461+ if (result == ITER_DONE )
2462+ result = DeeNone_NewRef ();
2463+ return result ;
2464+ }
2465+
2466+ PRIVATE WUNUSED NONNULL ((1 )) DREF DeeObject * DCALL
2467+ module_relname (DeeModuleObject * __restrict self , size_t argc ,
2468+ DeeObject * const * argv , DeeObject * kw ) {
2469+ DREF DeeObject * result ;
2470+ unsigned int flags ;
2471+ /*[[[deemon (print_DeeArg_UnpackKw from rt.gen.unpack)("__relname__", params: """
2472+ DeeObject *to:?X4?DModule?Dstring?DType?N=!N;
2473+ bool libname = false;
2474+ bool todir = false;
2475+ """, docStringPrefix: "module");]]]*/
2476+ #define module___relname___params "to:?X4?DModule?Dstring?DType?N=!N,libname=!f,todir=!f"
2477+ struct {
2478+ DeeObject * to ;
2479+ bool libname ;
2480+ bool todir ;
2481+ } args ;
2482+ args .to = Dee_None ;
2483+ args .libname = false;
2484+ args .todir = false;
2485+ if (DeeArg_UnpackStructKw (argc , argv , kw , kwlist__to_libname_todir , "|obb:__relname__" , & args ))
2486+ goto err ;
2487+ /*[[[end]]]*/
2488+ flags = DeeModule_RELNAME_F_NORMAL ;
2489+ if (args .libname )
2490+ flags |= DeeModule_RELNAME_F_LIBNAM ;
2491+ if (args .todir )
2492+ flags |= DeeModule_RELNAME_F_CTXDIR ;
2493+ result = DeeModule_GetRelName (self , args .to , flags );
2494+ if unlikely (result == ITER_DONE )
2495+ goto err_noname ;
2496+ return result ;
2497+ err_noname :
2498+ DeeError_Throwf (& DeeError_ValueError , "Cannot form relative name for module %k" , self );
2499+ err :
2500+ return NULL ;
2501+ }
2502+
24362503#else /* CONFIG_EXPERIMENTAL_MODULE_DIRECTORIES */
24372504PRIVATE NONNULL ((1 )) int DCALL
24382505err_module_not_fully_loaded (DeeModuleObject * __restrict self ) {
@@ -2483,6 +2550,8 @@ PRIVATE struct type_member tpconst module_members[] = {
24832550 TYPE_MEMBER_FIELD_DOC ("__absname__" , STRUCT_STRING , offsetof(DeeModuleObject , mo_absname ),
24842551 "The absolute name of this module (as can be passed to "
24852552 /**/ "$import to access this module from any context)" ),
2553+ TYPE_MEMBER_FIELD_DOC ("__haspath__" , STRUCT_CONST | STRUCT_BOOLPTR , offsetof(DeeModuleObject , mo_absname ),
2554+ "Deprecated alias for ${this.__filename__ is bound}" ),
24862555#else /* CONFIG_EXPERIMENTAL_MODULE_DIRECTORIES */
24872556 TYPE_MEMBER_FIELD_DOC (STR___name__ , STRUCT_OBJECT , offsetof(DeeModuleObject , mo_name ),
24882557 "->?Dstring\n"
@@ -2496,6 +2565,23 @@ DeeModule_ViewExports(DeeObject *__restrict self);
24962565INTDEF WUNUSED NONNULL ((1 )) DREF DeeObject * DCALL
24972566DeeModule_ViewGlobals (DeeObject * __restrict self );
24982567
2568+ #ifdef CONFIG_EXPERIMENTAL_MODULE_DIRECTORIES
2569+ PRIVATE struct type_method tpconst module_methods [] = {
2570+ TYPE_KWMETHOD_F ("__relname__" , & module_relname , METHOD_FNOREFESCAPE ,
2571+ "(to:?X4?DModule?Dstring?DType?N=!N,libname=!f,todir=!f)->?Dstring\n"
2572+ "#tValueError{?#__filename__ is unbound, or @to is an empty string and @libname is false, or @this has no libnames}"
2573+ "Form the import name of @this module relative to @to. When @to is a string, it "
2574+ /**/ "should be the filename (like ?#__filename__) of a deemon script relative to "
2575+ /**/ "which the string should be generated. However, when @todir is $true and @to "
2576+ /**/ "is a string, then @to is the directory of the reference file instead of the "
2577+ /**/ "reference file itself\n"
2578+ "When @libname is true, try to return ${__libnames__.first} instead (s.a. ?#__libnames__)" ),
2579+ TYPE_METHOD_END
2580+ };
2581+ #else /* CONFIG_EXPERIMENTAL_MODULE_DIRECTORIES */
2582+ #define module_methods NULL
2583+ #endif /* !CONFIG_EXPERIMENTAL_MODULE_DIRECTORIES */
2584+
24992585PRIVATE struct type_getset tpconst module_getsets [] = {
25002586 TYPE_GETTER_AB ("__exports__" , & DeeModule_ViewExports ,
25012587 "->?M?X2?Dstring?Dint?O\n"
@@ -2517,9 +2603,25 @@ PRIVATE struct type_getset tpconst module_getsets[] = {
25172603 TYPE_GETTER_AB ("__globals__" , & DeeModule_ViewGlobals ,
25182604 "->?S?O\n"
25192605 "Similar to ?#__exports__, however global variables are addressed using their "
2520- /**/ "internal index. Using this, anonymous global variables (such as property callbacks) "
2521- /**/ "can be accessed and modified" ),
2522- #ifndef CONFIG_EXPERIMENTAL_MODULE_DIRECTORIES
2606+ /**/ "internal index. Using this, anonymous global variables (such as property "
2607+ /**/ "callbacks) can be accessed and modified" ),
2608+ #ifdef CONFIG_EXPERIMENTAL_MODULE_DIRECTORIES
2609+ TYPE_GETTER_AB_F ("__directory__" , & module_get_directory , METHOD_FNOREFESCAPE ,
2610+ "->?S?Dstring\n"
2611+ "Returns the names of all child-modules located "
2612+ /**/ "within the directory specified by ?#__absname__\n"
2613+ "These strings can be used as attribute names with "
2614+ /**/ "?#{op:getattr} to retrieve the relevant module" ),
2615+ TYPE_GETTER_BOUND_F ("__filename__" , & module_get_filename , & module_bound_filename , METHOD_FNOREFESCAPE ,
2616+ "->?Dstring\n"
2617+ "#tUnboundAttribute{Module does not originate from the filesystem}\n"
2618+ "Returns the actual filename (as opposed to the unique / "
2619+ /**/ "directory-name returned by ?#__absname__) of this module" ),
2620+ TYPE_GETTER_AB_F ("__path__" , & module_get_path , METHOD_FNOREFESCAPE ,
2621+ "->?X2?Dstring?N\n"
2622+ "Deprecated alias for ${this.__filename__ is bound ? this.__filename__ : none}" ),
2623+ /* TODO: __libnames__->?S?Dstring (to expose DeeModule_GetLibName() and DeeModule_GetLibNameCount()) */
2624+ #else /* CONFIG_EXPERIMENTAL_MODULE_DIRECTORIES */
25232625 TYPE_GETTER_F ("__code__" , & module_get_code , METHOD_FNOREFESCAPE ,
25242626 "->?Ert:Code\n"
25252627 "#tValueError{The Module hasn't been fully loaded}"
@@ -2593,6 +2695,7 @@ module_class_gethome(DeeObject *__restrict UNUSED(self)) {
25932695}
25942696
25952697PRIVATE struct type_getset tpconst module_class_getsets [] = {
2698+
25962699 TYPE_GETTER_AB ("home" , & module_class_gethome ,
25972700 "->?Dstring\n"
25982701 "The deemon home path (usually the path where the deemon executable resides)" ),
@@ -2602,6 +2705,9 @@ PRIVATE struct type_getset tpconst module_class_getsets[] = {
26022705 "A tuple of strings describing the search path for system libraries" ),
26032706 /* TODO: User-code access to "DeeModule_AddLibPath" */
26042707 /* TODO: User-code access to "DeeModule_RemoveLibPath" */
2708+ /* TODO: User-code access to "DeeModule_NextAbsTree" */
2709+ /* TODO: User-code access to "DeeModule_NextAdrTree" */
2710+ /* TODO: User-code access to "DeeModule_NextLibTree" */
26052711#else /* CONFIG_EXPERIMENTAL_MODULE_DIRECTORIES */
26062712 TYPE_GETSET_AB ("path" , & module_class_getpath , NULL , & module_class_setpath ,
26072713 "->?DList\n"
@@ -3338,7 +3444,7 @@ PUBLIC DeeTypeObject DeeModule_Type = {
33383444 /* .tp_attr = */ & module_attr ,
33393445 /* .tp_with = */ NULL ,
33403446 /* .tp_buffer = */ NULL ,
3341- /* .tp_methods = */ NULL ,
3447+ /* .tp_methods = */ module_methods ,
33423448 /* .tp_getsets = */ module_getsets ,
33433449 /* .tp_members = */ module_members ,
33443450 /* .tp_class_methods = */ module_class_methods ,
0 commit comments