Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RFC: Add support for determining name of script #14114

Merged
merged 1 commit into from
Jan 28, 2016

Conversation

omus
Copy link
Member

@omus omus commented Nov 24, 2015

Fixes #14109.

@@ -174,6 +174,7 @@ export
UnicodeError,

# Global constants and variables
FILE_NAME,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should be alphabetized in each grouping

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good to know. Thanks

@sjkelly
Copy link
Contributor

sjkelly commented Nov 24, 2015

I think n+1 people have wanted this, so docs and a NEWS.md entry might be in order?

(I also wonder if this opens up module as a script functionality?)

@omus
Copy link
Member Author

omus commented Nov 24, 2015

I added/updated documentation and an entry to NEWS.md.

What do people think of the name FILE_NAME to indicate the name of the script? Anyone have any better suggestions? Also, FILE_NAME is currently a non-constant global. Is this a major problem or is there a good way to solve this that I'm not thinking of?

@KristofferC
Copy link
Sponsor Member

Maybe use const FILE_NAME = Ref{UTF8String}()?

@iamed2
Copy link
Contributor

iamed2 commented Nov 24, 2015

@omus:
CLI names for the script file:

Julia: programfile
Ruby: programfile
Python: file
Lua: script
Guile: file
Racket: file
Perl: programfile
Bash: script-file
Fish: help
R: file
PHP: file

Seems like PROGRAMFILE might be the best, in order to be consistent with the CLI help.

@omus
Copy link
Member Author

omus commented Nov 24, 2015

I like that name better. I'll change it to PROGRAM_FILE to make it slightly more readable.

@omus
Copy link
Member Author

omus commented Nov 24, 2015

After messing around with this more ARGS only has the program file removed right before the program file is run. That means that for code executing from using --load, --eval, or --print will not be able to determine the PROGRAM_FILE since it will still be included within ARGS. I was planning to initialize PROGRAM_FILE as a constant by initializing it at the last possible moment via:

global const PROGRAM_FILE = UTF8String(shift!(ARGS))

Unfortunately this won't work as I would also like to support having PROGRAM_FILE defined and usable to any external script passed into Julia.

The repercussions of this is that PROGRAM_FILE cannot be a constant and will only be assigned correctly to a filename from within the PROGRAM_FILE itself and included files.

@@ -151,6 +151,8 @@ New language features

* `@__LINE__` special macro now available to reflect invocation source line number ([#12727]).

* `PROGRAM_FILE` global is now available for determining the name of the running script ([#14114]).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you need to run julia doc/NEWS-update.jl to update the cross-reference links.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok. Is there some Julia contribution documentation that mentions this?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if it isn't mentioned somewhere in CONTRIBUTING.md it probably should be

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey, I just noticed that this was accidentally added to the Julia 0.4 NEWS, not the Julia 0.5 NEWS...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 47c29a9

@omus
Copy link
Member Author

omus commented Dec 3, 2015

I'll squash these commits

@tkelman
Copy link
Contributor

tkelman commented Dec 3, 2015

was the issue with

The repercussions of this is that PROGRAM_FILE cannot be a constant and will only be assigned correctly to a filename from within the PROGRAM_FILE itself and included files.

sorted, or is it a minor concern?

@omus
Copy link
Member Author

omus commented Dec 3, 2015

@tkelman I think it is a minor concern. Basically PROGRAM_FILE will be empty for the ~/.juliarc and the --load file. I could sort this problem out but that the change would involve taking the PROGRAM_FILE out of ARGS prior to running either of these file. Changing that behaviour could break things:

Here's a demonstration of the current behavior:

$ echo 'println("PROGRAM_FILE: $PROGRAM_FILE"); for (i, arg) in enumerate(ARGS); println("$i: $arg"); end' > script.jl

$ julia script.jl foo -bar --baz
PROGRAM_FILE: script.jl
1: foo
2: -bar
3: --baz

$ julia -L script.jl script.jl foo -bar --baz
PROGRAM_FILE: 
1: script.jl
2: foo
3: -bar
4: --baz
PROGRAM_FILE: script.jl
1: foo
2: -bar
3: --baz

What do you think?

@tkelman
Copy link
Contributor

tkelman commented Dec 3, 2015

I guess it not being const means people can modify it which could be confusing or abused?

@@ -14,6 +14,10 @@ Constants
A symbol representing the name of the operating system. Possible values
are ``:Linux``, ``:Darwin`` (OS X), or ``:Windows``.

.. data:: PROGRAM_FILE

A string containing the script name passed to Julia.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be good to clarify here exactly how this differs from @__FILE__

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. I'll add some more details.

@omus
Copy link
Member Author

omus commented Dec 3, 2015

Yes it could be modified. I'll note that ARGS elements can also be modified if someone wants to. Alternatively I could make PROGRAM_FILE constant without changing anything else. This would mean that if someone tried using PROGRAM_FILE in the --load file it would raise an UndefVarError.

@omus
Copy link
Member Author

omus commented Dec 3, 2015

@tkelman I updated the documentation to be clearer. I noticed however that macro's don't seem to support permalinks which means the @__FILE__ link I included in the PROGRAM_FILE documentation doesn't work.

@omus omus changed the title Add support for determining name of script RFC: Add support for determining name of script Dec 3, 2015
@@ -320,7 +320,7 @@

.. Docstring generated from Julia source

``@__FILE__`` expands to a string with the absolute path and file name of the script being run. Returns ``nothing`` if run from a REPL or an empty string if evaluated by ``julia -e <expr>``\ .
``@__FILE__`` expands to a string with the absolute path and file name of the containing the macro. Returns ``nothing`` if run from a REPL or an empty string if evaluated by ``julia -e <expr>``\ . Alternatively see :data:`PROGRAM_FILE`.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is autogenerated so should be changed at the jl docstring source

@omus
Copy link
Member Author

omus commented Dec 4, 2015

I tracked down the permalink issue with @__FILE__. Will be fixed when JuliaLang/JuliaDoc#18 is merged. No changes will be required in this PR.

@omus
Copy link
Member Author

omus commented Dec 16, 2015

Rebased on to latest master. I also separated the PROGRAM_FILE tests from the ARGS tests.

@JeffBezanson
Copy link
Sponsor Member

LGTM. @StefanKarpinski what do you think?

@stevengj
Copy link
Member

LGTM.

Note that PROGRAM_FILE was chosen as the name for this variable as it
matches the help for the Julia CLI.
@omus
Copy link
Member Author

omus commented Jan 27, 2016

Rebased again on to the latest master. Fixed conflict with 7d5def6

@omus
Copy link
Member Author

omus commented Jan 27, 2016

The appveyor 64-bit Windows failure looks like it just got killed at 1 hour.

stevengj added a commit that referenced this pull request Jan 28, 2016
RFC: Add support for determining name of script
@stevengj stevengj merged commit b210456 into JuliaLang:master Jan 28, 2016
@omus
Copy link
Member Author

omus commented Jan 28, 2016

Thanks!

@omus omus deleted the filename branch January 28, 2016 17:00
stevengj added a commit that referenced this pull request Apr 8, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants