-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
[stdlib] Add os.path.expandvars
and respective tests
#3735
Conversation
os.path.expandvars
and respective testsos.path.expandvars
and respective tests
@@ -119,6 +119,7 @@ environments: | |||
- conda: https://conda.anaconda.org/conda-forge/linux-64/libxcrypt-4.4.36-hd590300_1.conda | |||
- conda: https://conda.anaconda.org/conda-forge/linux-64/libxml2-2.13.4-h064dc61_2.conda | |||
- conda: https://conda.anaconda.org/conda-forge/linux-64/libzlib-1.3.1-hb9d3cd8_2.conda | |||
- conda: https://conda.anaconda.org/conda-forge/noarch/lit-19.1.3-pyhd8ed1ab_0.conda |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Ahajha can you comment here how we're managing the magic lock files here internally wrt this? Given the migration to magic, I agree with @thatstoasty that we could just specify this as part of the dependencies in the pixi.toml
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this should be fine. 2 points:
1: This would allow a follow-up cleanup where we could remove the section of CI that installs lit
through llvm.sh.
2: Internally, the lockfiles are wiped and then recreated each night (rather than updated in-place), so the only reason to update them here is to satisfy the --frozen
flag, as pixi.toml
is essentially ignored in CI here. (There might be a different flag that might be better for this use case, where it will check both?) After a night this should sync internally + regenerate, and then everything should be fine.
Verbal approval, from the CI side of things this should be a-ok.
@thatstoasty Thanks for improving the state of the world :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great! I handle a lot of the CI for my teams at work, so I know how fickle internal processes can be 🙂. Glad to see that the change might make its way downstream to make contributing a little easier!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great addition, @toiletsandpaper. LGTM, appreciate the easy-to-read test cases 👍
!sync |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM! Just a minor remaining question/comment and then we can land this!
stdlib/src/os/env.mojo
Outdated
"""Unsets an environment variable. | ||
|
||
Constraints: | ||
The function only works on macOS or Linux and returns False otherwise. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Question Any particular reason not to just use constrained
instead (hard error in other words) instead of just gracefully returning False
on Windows?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No concerns here, I was following the same pattern as getenv
and setenv
. I'll open up a separate PR to make them consistent with using constrained
and c_int
. For now I've updated only unsetenv
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @thatstoasty nice work. I left some ideas in case you want to apply them though some might take time and it's fine to leave them as is or do a followup (they're a bit of an overkill).
var b = int(byte) | ||
return ( | ||
b == ord("_") | ||
or ord("0") <= b | ||
and b <= ord("9") | ||
or ord("a") <= b | ||
and b <= ord("z") | ||
or ord("A") <= b | ||
and b <= ord("Z") | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
var b = int(byte) | |
return ( | |
b == ord("_") | |
or ord("0") <= b | |
and b <= ord("9") | |
or ord("a") <= b | |
and b <= ord("z") | |
or ord("A") <= b | |
and b <= ord("Z") | |
) | |
alias `_` = Byte(ord("_")) | |
alias `0` = Byte(ord("0")) | |
alias `9` = Byte(ord("9")) | |
alias `a` = Byte(ord("a")) | |
alias `z` = Byte(ord("z")) | |
alias `A` = Byte(ord("A")) | |
alias `Z` = Byte(ord("Z")) | |
var h_num = `0` <= byte <= `z` and not (`9` < byte < `A` or `Z` < byte < `a`) | |
return byte == `_` or h_num |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Whenever I see backtick variables, it gets a bit difficult to differentiate between a variable name and a value. So I'm going to leave it as is for now. Perhaps it can be cleaned up as part of a future PR if backtick variables become the norm in the stdlib style guide
stdlib/src/os/path/path.mojo
Outdated
if i == 1: | ||
return String("${}"), 2 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Suggestion that might also beak some code or require some time
This is already handled by the logic bellow.
if i == 1: | |
return String("${}"), 2 |
The one after the while can also just return bytes[1:i]
return String("${"), 1
You can then change this:
# Invalid syntax (`${}` or `${`); write as is.
if name.startswith("$") and length > 0:
buf.write(name)
# $ was not followed by a name, write the $.
elif name == "":
buf.write_bytes(bytes[j : j + 1])
to
# incomplete (`$` or `${}` or `${`); write as is.
if length <= 2:
buf.write_bytes(bytes[j:j + length + 1])
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was able to squash the cases into one:
# Invalid syntax (`${}` or `${`) or $ was not followed by a name; write as is.
if name.startswith("{") or name == "":
buf.write_bytes(bytes[j : j + length + 1])
if length <= 2:
did not work for one letter environment variables like $a
.
alias shell_variables = InlineArray[Int, 17]( | ||
ord("*"), | ||
ord("#"), | ||
ord("$"), | ||
ord("@"), | ||
ord("!"), | ||
ord("?"), | ||
ord("-"), | ||
ord("0"), | ||
ord("1"), | ||
ord("2"), | ||
ord("3"), | ||
ord("4"), | ||
ord("5"), | ||
ord("6"), | ||
ord("7"), | ||
ord("8"), | ||
ord("9"), | ||
) | ||
return int(byte) in shell_variables |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
alias shell_variables = InlineArray[Int, 17]( | |
ord("*"), | |
ord("#"), | |
ord("$"), | |
ord("@"), | |
ord("!"), | |
ord("?"), | |
ord("-"), | |
ord("0"), | |
ord("1"), | |
ord("2"), | |
ord("3"), | |
ord("4"), | |
ord("5"), | |
ord("6"), | |
ord("7"), | |
ord("8"), | |
ord("9"), | |
) | |
return int(byte) in shell_variables | |
alias `0` = Byte(ord("0")) | |
alias `9` = Byte(ord("9")) | |
alias shell_variables = SIMD[DType.uint8, 8]( | |
Byte(ord("!")), | |
Byte(ord("!")), | |
Byte(ord("#")), | |
Byte(ord("$")), | |
Byte(ord("*"), | |
Byte(ord("-")), | |
Byte(ord("?")), | |
Byte(ord("@")), | |
) | |
return `0` <= byte <= `9` or byte in shell_variables |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm seeing some compilation issues using SIMD, so I'm going to leave it as it for now. Byte(ord(...))
was adding visual noise IMO.
…agic run` to pre-commit configuration. squash code a bit by using write add tests Cleanup with formatting and sign add missing newline Signed-off-by: Mikhail Tavarez <[email protected]> updated changelog
…agic run` to pre-commit configuration. squash code a bit by using write add tests Cleanup with formatting and sign add missing newline Signed-off-by: Mikhail Tavarez <[email protected]> updated changelog updated changelog Add unsetenv and use a context manager to unset test variables. Add renamed test_env.mojo Fix unsetenv result in EnvVar context manager. Signed-off-by: Mikhail Tavarez <[email protected]> use constrained for unsetenv use immutable stringslice
@JoeLoser I think I'm good on additional changes to the code, unless you have some more suggestions! |
!sync |
✅🟣 This contribution has been merged 🟣✅ Your pull request has been merged to the internal upstream Mojo sources. It will be reflected here in the Mojo repository on the nightly branch during the next Mojo nightly release, typically within the next 24-48 hours. We use Copybara to merge external contributions, click here to learn more. |
…0283) [External] [stdlib] Add `os.path.expandvars` and respective tests This PR adds the `expandvars` function to the `os.path` module. The documentation for contributing references installing `lit` into our local Python environment, but with `magic` we have a Python environment already defined, so I added it as a dependency in the `pixi.toml` file. On a similar note, I added `magic run` to pre-commit hooks so `magic` is used for the Python and Mojo environments. Co-authored-by: Mikhail Tavarez <[email protected]> Closes #3735 MODULAR_ORIG_COMMIT_REV_ID: 2d8c7e8c2b2cadf773fcb36cdc9a33d27306c488
Landed in dca55fd! Thank you for your contribution 🎉 |
…0283) [External] [stdlib] Add `os.path.expandvars` and respective tests This PR adds the `expandvars` function to the `os.path` module. The documentation for contributing references installing `lit` into our local Python environment, but with `magic` we have a Python environment already defined, so I added it as a dependency in the `pixi.toml` file. On a similar note, I added `magic run` to pre-commit hooks so `magic` is used for the Python and Mojo environments. Co-authored-by: Mikhail Tavarez <[email protected]> Closes #3735 MODULAR_ORIG_COMMIT_REV_ID: 2d8c7e8c2b2cadf773fcb36cdc9a33d27306c488
This PR adds the
expandvars
function to theos.path
module.The documentation for contributing references installing
lit
into our local Python environment, but withmagic
we have a Python environment already defined, so I added it as a dependency in thepixi.toml
file. On a similar note, I addedmagic run
to pre-commit hooks somagic
is used for the Python and Mojo environments.Any feedback on the changes or on the implementation would be welcome!