• How can I enforce a "pure" string tcl object?

    From Georgios Petasis@21:1/5 to All on Tue Apr 5 18:54:29 2022
    Hi all,

    I am facing a problem with the internal representation of objects.

    Lets assume I create some tcl objects through a C extension, using Tcl_NewStringObj().

    This is passed to Tcl, so I do not know what happens regarding type conversions.

    The problem is, that if I access the objects from python's tkinter, and
    the problem is that what I get back in python, depends on the tcl
    object's internal representation. Which means that if the object
    contains a number (as a string), it is converted into an int internal representation, and ends up in python as a python number (and not as a
    string).

    If I use "string cat {} $value" before returning to python, it is always
    a string in python.

    Thus, my question is: is there a way in tcl (script level) to say that
    an object is a string or other type, and convert it to a string if it is
    not?

    I know about tcl::unsupported::representation, but the manual says to
    not use that.

    Is there a tcl command, that will enforce this conversion?

    George

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From [email protected]@21:1/5 to All on Tue Apr 5 17:55:51 2022
    If I use "string cat {} $value" before returning to python, it is always
    a string in python.

    do you need the {}

    string cat $value

    might work just as well.

    I'm not familiar with python or tkinter, but if you might be able to use "$value"
    if the value is parsed by the tcl command line before it is sent back to the python environment.

    Thus, my question is: is there a way in tcl (script level) to say that
    an object is a string or other type, and convert it to a string if it is
    not?

    However Tcl is all about everything is a string except under the covers, and we never look there. It looks like python only looks under the covers though.


    Dave B

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Georgios Petasis@21:1/5 to All on Tue Apr 5 21:20:34 2022
    Στις 5/4/2022 20:55, ο/η [email protected] έγραψε:
    If I use "string cat {} $value" before returning to python, it is always
    a string in python.

    do you need the {}

    string cat $value

    might work just as well.

    No, it does not...


    I'm not familiar with python or tkinter, but if you might be able to use "$value"
    if the value is parsed by the tcl command line before it is sent back to the python environment.

    Thus, my question is: is there a way in tcl (script level) to say that
    an object is a string or other type, and convert it to a string if it is
    not?

    However Tcl is all about everything is a string except under the covers, and we never look there. It looks like python only looks under the covers though.


    Dave B







    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From heinrichmartin@21:1/5 to Georgios Petasis on Wed Apr 6 01:14:17 2022
    On Tuesday, April 5, 2022 at 5:54:33 PM UTC+2, Georgios Petasis wrote:
    Is there a tcl command, that will enforce this conversion?

    Tcl's type system becomes even more funny when it reuses Tcl_Objs.

    I haven't thought that through, but maybe one of the json-lib (tdom, rl_json) can help you. Iirc, they provide objects with type information. There have been several threads about them on clt.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Andreas Leitgeb@21:1/5 to Georgios Petasis on Wed Apr 6 14:17:06 2022
    Georgios Petasis <[email protected]> wrote:
    The problem is, that if I access the objects from python's tkinter, and
    the problem is that what I get back in python, depends on the tcl
    object's internal representation.

    This is really a "bug" wrt "EIAS" inside tkinter.

    There are some "brute-force" ways to turn something into a pure string, like prepending a char, and then extracting the substring from it:
    [string range x$val 1 end]
    though these brute-force ways may cost performance if strings are long.

    Also, it is not guaranteed, that some far future version of tcl might
    not even recognize that pattern and still reproduce the original object.

    Even more brute-force: [join [split $val {}] {}] is likely another big bit slower, as it will create a list of each char, before smashing it back
    together into a string, but it's damn likely, that this will not be
    optimized away, any time thinkable.

    Maybe tkinter itself has some C-functions to coerce internal objects to
    what is then expected in python... if not, that might be the best place
    for them: at the boundary to a not as dynamically typed language.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Georgios Petasis@21:1/5 to All on Thu Apr 7 12:55:15 2022
    Στις 6/4/2022 17:17, ο/η Andreas Leitgeb έγραψε:
    Georgios Petasis <[email protected]> wrote:
    The problem is, that if I access the objects from python's tkinter, and
    the problem is that what I get back in python, depends on the tcl
    object's internal representation.

    This is really a "bug" wrt "EIAS" inside tkinter.

    There are some "brute-force" ways to turn something into a pure string, like prepending a char, and then extracting the substring from it:
    [string range x$val 1 end]
    though these brute-force ways may cost performance if strings are long.

    Also, it is not guaranteed, that some far future version of tcl might
    not even recognize that pattern and still reproduce the original object.

    Even more brute-force: [join [split $val {}] {}] is likely another big bit slower, as it will create a list of each char, before smashing it back together into a string, but it's damn likely, that this will not be
    optimized away, any time thinkable.

    Maybe tkinter itself has some C-functions to coerce internal objects to
    what is then expected in python... if not, that might be the best place
    for them: at the boundary to a not as dynamically typed language.


    The solution I gave was to add a command to my C extension, which
    exposes Tcl_ConvertToType(). This solves the problem, as it enforces a conversion to type "string".

    But I think these commands should be available at the Tcl level. To
    check what type an object is, and convert it to another type.

    George

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From [email protected]@21:1/5 to All on Thu Apr 7 15:54:12 2022
    I'm curious about your use case. I know very little about python. From this discussion it appears the problem is with the python tkinter interface with Tcl/Tk.

    In the first post you mentioned trouble sending a number to python as a string. Why is it important that python get the string value? If you are trying to preserve a formatted value returning the result of [format] would give a Tcl value with a string
    type, and no internal numeric value. If you need the full precision of the Tcl internal value, passing it as a number seems like the correct thing to do.

    Dave B

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Andreas Leitgeb@21:1/5 to Georgios Petasis on Thu Apr 7 17:37:53 2022
    Georgios Petasis <[email protected]> wrote:
    Στις 6/4/2022 17:17, ο/η Andreas Leitgeb έγραψε:
    Georgios Petasis <[email protected]> wrote:
    The problem is, that if I access the objects from python's tkinter, and
    the problem is that what I get back in python, depends on the tcl
    object's internal representation.
    This is really a "bug" wrt "EIAS" inside tkinter.
    ...
    Maybe tkinter itself has some C-functions to coerce internal objects to
    what is then expected in python... if not, that might be the best place
    for them: at the boundary to a not as dynamically typed language.

    The solution I gave was to add a command to my C extension, which
    exposes Tcl_ConvertToType(). This solves the problem, as it enforces a conversion to type "string".

    But I think these commands should be available at the Tcl level. To
    check what type an object is, and convert it to another type.

    This non-EIAS-based approach is very unlikely to go into Tcl itself.

    On the other hand, it seems perfectly right in the context of this language-border, to offer functions for python (or the C code glueing
    parts together) to express: "treat this value as a string (or maybe
    even numeric value, list, dict) no matter what internal object type
    Tcl-side last used for it"

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Georgios Petasis@21:1/5 to All on Fri Apr 8 21:13:20 2022
    Στις 7/4/2022 20:37, ο/η Andreas Leitgeb έγραψε:
    Georgios Petasis <[email protected]> wrote:
    Στις 6/4/2022 17:17, ο/η Andreas Leitgeb έγραψε:
    Georgios Petasis <[email protected]> wrote:
    The problem is, that if I access the objects from python's tkinter, and >>>> the problem is that what I get back in python, depends on the tcl
    object's internal representation.
    This is really a "bug" wrt "EIAS" inside tkinter.
    ...
    Maybe tkinter itself has some C-functions to coerce internal objects to
    what is then expected in python... if not, that might be the best place >>> for them: at the boundary to a not as dynamically typed language.

    The solution I gave was to add a command to my C extension, which
    exposes Tcl_ConvertToType(). This solves the problem, as it enforces a
    conversion to type "string".

    But I think these commands should be available at the Tcl level. To
    check what type an object is, and convert it to another type.

    This non-EIAS-based approach is very unlikely to go into Tcl itself.

    On the other hand, it seems perfectly right in the context of this language-border, to offer functions for python (or the C code glueing
    parts together) to express: "treat this value as a string (or maybe
    even numeric value, list, dict) no matter what internal object type
    Tcl-side last used for it"


    I am not concerned with what python (or any other caller) does. I am
    more concerned that I do not have the tools to deal with the situation,
    and I have to revert to writing a C extension.

    George

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From [email protected]@21:1/5 to Georgios Petasis on Fri Apr 8 16:36:39 2022
    On 4/8/22 2:13 PM, Georgios Petasis wrote:

    I am not concerned with what python (or any other caller) does. I am
    more concerned that I do not have the tools to deal with the situation,
    and I have to revert to writing a C extension.

    George

    Hello,

    Perhaps you can use critcl for that piece.

    So there is no function in Python/tkinter to get a plain string out of a
    Tcl object? I'd find that surprising.

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