使用 tcl 脚本调用带参数的 python 函数

calling python function with arguments using tcl script

我有一个 python 脚本 (sample.py),它有一个带 3 个参数的函数。其中第一个两个参数是十六进制,第三个是字符串。

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

    output = "some string"
    return output

我必须使用 tcl 脚本来调用 sample.py 中存在的函数 sampleFun。

怎么做? 我试过这个 tcl 命令:

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

}

但是我得到的错误是名称“linkup”没有定义。

那么,如何将字符串参数从 tcl 传递给 python 函数?

您需要为 Python 引用它。

对于这样一个近乎任意的值,您可以试试这个:

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

这利用了以下事实:Python 中的 ''' 引号字符串可以包含换行符和单个 ' 字符,而 r 字符串可以包含反斜杠。剩下的可能导致问题的情况很少见。

您知道您的示例 Python 代码表示 sampleFun 但您的测试代码表示 sample.print_file 吗?我想你可以解决这个问题……