calling python function with arguments using tcl script

0

I have a python script (sample.py) that has a function with 3 arguments. where 1st two arguments are in hex and 3rd is the string.

def sampleFun(gen_lane,sw_state,test_name):`
    
    ### code segment 

    output = "some string"
    return output

I have to use tcl script to call this function sampleFun present in sample.py.

How to do that? I have tried this tcl command :

proc call_python { } {
    set gen_lane 0x10500000
    set sw_state 0x0000000B
    set test_case "linkup"
    set result [exec python -c "import sample; print sample.sampleFun($gen_lane,$sw_state,$test_case)"]
    puts $result

}

But I am getting the error that the name "linkup" is not defined.

So, how to pass a string argument to the python function from tcl?

python
python-3.x
parameter-passing
tcl
asked on Stack Overflow Apr 29, 2021 by Rishabh Gupta • edited Apr 29, 2021 by Rishabh Gupta

1 Answer

0

You need to quote it for Python.

For an almost-arbitrary value like that, you'd try this:

set result [exec python -c "import sample; print sample.print_file($gen_lane,$sw_state,r'''$test_case''')"]

This uses the fact that '''-quoted strings in Python can contain newlines and single ' characters, and r strings can contain backslashes. The cases remaining that could possibly cause problems are rare.

You are aware that your example Python code said sampleFun but your test code said sample.print_file? I assume you can fix that up…

answered on Stack Overflow Apr 29, 2021 by Donal Fellows

User contributions licensed under CC BY-SA 3.0