通过外部函数或Julia中的宏获取执行文件的路径 [英] get path of the executed file through external function or macro in Julia
问题描述
我正在尝试编写一个没有参数的辅助函数或宏,该函数可以记录文件名和调用它的行.
I'm trying write a helper function or macro with no arguments that can record the filename and line of where it is called.
该帮助器位于另一个模块中,并导入到脚本中,因此@__FILE__
和@__LINE__
不会指向正确的位置.
The helper is located in a different module and imported to a script, so @__FILE__
and @__LINE__
would not point to the right place.
这是我在trace.jl
的帮助程序模块:
Here is my helper module at trace.jl
:
module Trace
export @trace, Location
struct Location
file:: String
line:: Integer
end
macro trace()
return Location(abspath(PROGRAM_FILE), __source__.line)
end
end
这是脚本caller.jl
include("trace.jl")
using .Trace
# putting two statements in one line so that line number is the same
println("I want: ", Location(@__FILE__, @__LINE__)); println(" I get: ", @trace)
运行julia caller.jl
的输出如下:
D:\github\Handout.jl\src>julia caller.jl
I want: Location("D:\\github\\Handout.jl\\src\\caller.jl", 5)
I get: Location("D:\\github\\Handout.jl\\src\\caller.jl", 5)
我不确定PROGRAM_FILE是不是偶然提供了我caller.jl
还是可以提供更多保证?
I'm not sure if PROGRAM_FILE provides me caller.jl
by accident or there can be more guarantee?
我本来会更乐于从__source__.file
提取路径,因为__source__.line
将我指向确切文件中的确切行,但是当我尝试时__source__.file
是nothing
.
I would have been happier to extract path from __source__.file
because __source__.line
points me to the exact line in exact file, but __source__.file
is nothing
when I tried.
这里是文档中的两部分. 第一个:
Here are two pieces in the documentation. The first one:
除了给定的参数列表外,每个宏还传递了名为
__source__
和__module__
的其他参数.
自变量__source__
通过宏调用提供有关@ sign
的解析器位置的信息(以LineNumberNode
对象的形式).
The argument __source__
provides information (in the form of a LineNumberNode
object) about the parser location of the @ sign
from the macro invocation.
第二个:>
源位置信息表示为
(line line_num file_name)
,其中第三部分是可选的(当当前行号(但文件名未更改)时省略).
Source location information is represented as
(line line_num file_name)
where the third component is optional (and omitted when the current line number, but not file name, changes).
这些表达式在Julia中表示为LineNumberNode
.
These expressions are represented as LineNumberNode
s in Julia.
是否有可能攀登LineNumberNode
链以获得文件名而不是nothing
?
Is there possibly a way to climb up LineNumberNode
chain to get a filename and not nothing
?
也许还有一种方法可以将%__FILE__
的计算延迟到运行时,以便我可以在trace
中使用该构造?
Also maybe there is a way to delay computation of %__FILE__
until runtime, so that I can use that construct in trace
?
类似的讨论: Julia:创建一个相对于脚本位置的新文件夹和文件
推荐答案
引用__source__
是Julia手册中建议的内容.这是一个例子
Quoting __source__
is what is recommended in the Julia manual. Here is an example
module Trace
export @trace
macro trace()
return QuoteNode(__source__)
end
end # module
文件f2.jl
include("f1.jl")
using .Trace
println("I want: ", (@__FILE__, @__LINE__)); println("I get: ", @trace)
x = @trace
dump(x)
println("This is not what you want: ", PROGRAM_FILE)
文件f3.jl
include("f2.jl")
运行以上
现在看一下输出:
Running the above
Now have a look at the output:
$ julia f3.jl
I want: ("D:\\f2.jl", 5)
I get: #= D:\f2.jl:5 =#
LineNumberNode
line: Int64 7
file: Symbol D:\f2.jl
This is not what you want: f3.jl
尤其是:
-
@trace
返回具有两个字段的LineNumberNode
对象(但我知道这是您想要的) - 您会看到
PROGRAM_FILE
为您提供了不同的信息:这是从命令行传递给Julia的文件的名称(因此在我们的情况下为f3.jl
,尽管在f2.jl
文件中被调用了是f3.jl
的include
d.)
@trace
returns youLineNumberNode
object that has two fields (but I understand this is what you want)- you can see that
PROGRAM_FILE
gives you a different information: it is a name of the file passed to Julia from the command line (so it isf3.jl
in our case, although it was called inf2.jl
file which wasinclude
d byf3.jl
).
现在更清楚吗?
这篇关于通过外部函数或Julia中的宏获取执行文件的路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!