• possible problem with namespace and tcl-C-extension

    From aotto1968@21:1/5 to All on Mon May 23 10:01:02 2022
    Hi, I have a problem that a "package require …" always be done into the global (::) namespace. I have a problem to setup a "namespace" from
    required package to be "::test::tclmsgque::MkKernel" and *not* "::tclmsgque::MkKernel"

    error: namespace import tclmsgque::* =
    unknown namespace in import pattern "tclmsgque::*"

    I don't know if this is a feature or a bug because without fix to global
    "::" ns a package require or a lazy loading would be probably put the
    package into a *wrong* ns. BUT the feature to force an other "namespace current" as global(::) for a package imported would be a benefit.

    tcl code:

    namespace eval test {

    cmd package names
    cmd namespace current

    package require tclmsgque::MkKernel

    cmd namespace current
    cmd namespace import tclmsgque::*



    tcl result:


    package names = zlib TclOO tcl::tommath Tcl namespace current = ::test
    :(tclStubsPtr->tcl_GetCurrentNamespace)(interp)->fullName<::> :ns->fullName<::tclmsgque::MkKernel>
    namespace current = ::test
    namespace import tclmsgque::* = unknown namespace in import
    pattern "tclmsgque::*"



    tcl extension C code

    OT_TCL_EXTERN int

    Tclmkkernel_Init (

    Tcl_Interp * interp

    )

    {

    // check for the right tcl version

    if (Tcl_InitStubs (interp, "8.5", 0) == NULL) {

    return TCL_ERROR;

    }

    if (Tcl_OOInitStubs (interp) == NULL) {

    return TCL_ERROR;

    }



    printC(Tcl_GetCurrentNamespace(interp)->fullName)



    // announce my package

    LngErrorCheck (Tcl_PkgProvide (interp, "tclmsgque::MkKernel", LIBMSGQUE_VERSION ));


    // toplevel namespace

    Tcl_Namespace *ns = Tcl_CreateNamespace (interp,
    "tclmsgque::MkKernel", NULL, NULL);
    check_NULL(ns) return TCL_ERROR;



    printC(ns->fullName)


    mfg ao

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Rich@21:1/5 to aotto1968@t-online.de on Mon May 23 14:06:44 2022
    aotto1968 <aotto1968@t-online.de> wrote:

    Hi, I have a problem that a "package require ?" always be done into
    the global (::) namespace. I have a problem to setup a "namespace"
    from required package to be "::test::tclmsgque::MkKernel" and *not* "::tclmsgque::MkKernel"

    Do you mean you always want your "test" namespace to be created in the
    global top-level namespace?

    tcl code:

    namespace eval test {

    If you want "test" to always be in the global, then ask for that:

    namespace eval ::test {

    Now 'test' will always be in the global namespace, no matter in what
    namespace you run the package require.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From aotto1968@21:1/5 to Rich on Mon May 23 17:54:32 2022
    On 23.05.22 16:06, Rich wrote:
    aotto1968 <aotto1968@t-online.de> wrote:

    Hi, I have a problem that a "package require ?" always be done into
    the global (::) namespace. I have a problem to setup a "namespace"
    from required package to be "::test::tclmsgque::MkKernel" and *not*
    "::tclmsgque::MkKernel"

    Do you mean you always want your "test" namespace to be created in the
    global top-level namespace?

    tcl code:

    namespace eval test {

    If you want "test" to always be in the global, then ask for that:

    namespace eval ::test {

    Now 'test' will always be in the global namespace, no matter in what namespace you run the package require.

    I want to have that the "tclmsgque" namespace be created into the
    namespace the "package require" was called (now "test"). the "test"
    namespace is just an "test-example"

    mfg

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Rich@21:1/5 to aotto1968@t-online.de on Mon May 23 19:01:23 2022
    aotto1968 <aotto1968@t-online.de> wrote:
    On 23.05.22 16:06, Rich wrote:
    aotto1968 <aotto1968@t-online.de> wrote:

    Hi, I have a problem that a "package require ?" always be done into
    the global (::) namespace. I have a problem to setup a "namespace"
    from required package to be "::test::tclmsgque::MkKernel" and *not*
    "::tclmsgque::MkKernel"

    Do you mean you always want your "test" namespace to be created in the
    global top-level namespace?

    tcl code:

    namespace eval test {

    If you want "test" to always be in the global, then ask for that:

    namespace eval ::test {

    Now 'test' will always be in the global namespace, no matter in what
    namespace you run the package require.

    I want to have that the "tclmsgque" namespace be created into the
    namespace the "package require" was called (now "test"). the "test"
    namespace is just an "test-example"

    How does the "tclmsgque" package define it's namespace eval?

    If it does "namespace eval ::tclmsgque {...}" then change it to be
    "namespace eval tclmsgque {...}" and it should create the "tclmsgque"
    namespace inside where it is called from. But you do need to run the
    package require inside the namespace you wish to be the parent, i.e.:

    namespace eval ::test {
    package require tclmsgque
    }

    And it should then create ::test::tclmsgque.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From blacksqr@21:1/5 to aotto1...@gmail.com on Tue May 24 12:40:35 2022
    On Monday, May 23, 2022 at 10:54:36 AM UTC-5, aotto1...@gmail.com wrote:
    I want to have that the "tclmsgque" namespace be created into the
    namespace the "package require" was called (now "test"). the "test" namespace is just an "test-example"

    The fact that some people include double-colons in package names (like you do with "tclmsgque::MkKernel") is simply a convention, it's just a coincidence that the package name looks like a namespace, and the naming of the package plays no role in where
    the package's namespaces are created.

    The package name is just a string, and Tcl's package API checks if that string matches a package name registered with the "package ifneeded" command. The "package ifneeded" command registers a script to be executed when "package require" is called with
    that package name. The script registered with "package ifneeded" is always executed in the global namespace.

    So if you want the script that runs when you call "package require tclmsgque::MkKernel" to create a child of the current namespace, you'll have to find a way to pass the name of the current namespace to the package script when it executes in the global
    namespace. For example, you could create a global variable called "mynamespace" and set it equal to ::test just before calling the "package require" command. Then the package script could include a command like "namespace eval ${mynamespace}::tclmsgque:
    :MkKernel {}"

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)