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

[stdlib] Add os.path.expandvars and respective tests #3735

Closed
wants to merge 3 commits into from

Conversation

thatstoasty
Copy link
Contributor

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.

Any feedback on the changes or on the implementation would be welcome!

@thatstoasty thatstoasty requested a review from a team as a code owner November 3, 2024 19:55
@thatstoasty thatstoasty changed the title Add os.path.expandvars and respective tests [stdlib] Add os.path.expandvars and respective tests Nov 3, 2024
@thatstoasty thatstoasty marked this pull request as draft November 3, 2024 20:12
@thatstoasty thatstoasty marked this pull request as ready for review November 4, 2024 16:32
@@ -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
Copy link
Collaborator

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.

Copy link
Contributor

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 :)

Copy link
Contributor Author

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!

Copy link
Collaborator

@JoeLoser JoeLoser left a 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 👍

stdlib/src/os/path/path.mojo Outdated Show resolved Hide resolved
stdlib/src/os/path/path.mojo Show resolved Hide resolved
stdlib/test/os/path/test_expandvars.mojo Outdated Show resolved Hide resolved
@JoeLoser
Copy link
Collaborator

JoeLoser commented Nov 4, 2024

!sync

@modularbot modularbot added the imported-internally Signals that a given pull request has been imported internally. label Nov 4, 2024
Copy link
Collaborator

@JoeLoser JoeLoser left a 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!

"""Unsets an environment variable.

Constraints:
The function only works on macOS or Linux and returns False otherwise.
Copy link
Collaborator

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?

Copy link
Contributor Author

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.

stdlib/src/os/env.mojo Outdated Show resolved Hide resolved
stdlib/src/os/path/path.mojo Show resolved Hide resolved
Copy link
Contributor

@martinvuyk martinvuyk left a 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).

Comment on lines +441 to +450
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")
)
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
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

Copy link
Contributor Author

@thatstoasty thatstoasty Nov 5, 2024

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 Show resolved Hide resolved
Comment on lines 475 to 476
if i == 1:
return String("${}"), 2
Copy link
Contributor

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.

Suggested change
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])

Copy link
Contributor Author

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.

Comment on lines +410 to +429
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
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
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

Copy link
Contributor Author

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
@thatstoasty
Copy link
Contributor Author

@JoeLoser I think I'm good on additional changes to the code, unless you have some more suggestions!

@JoeLoser
Copy link
Collaborator

JoeLoser commented Nov 7, 2024

!sync

@modularbot
Copy link
Collaborator

✅🟣 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.

@modularbot modularbot added the merged-internally Indicates that this pull request has been merged internally label Nov 7, 2024
modularbot pushed a commit that referenced this pull request Nov 7, 2024
…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
@modularbot modularbot added the merged-externally Merged externally in public mojo repo label Nov 7, 2024
@modularbot
Copy link
Collaborator

Landed in dca55fd! Thank you for your contribution 🎉

@modularbot modularbot closed this Nov 7, 2024
modularbot pushed a commit that referenced this pull request Dec 17, 2024
…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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
imported-internally Signals that a given pull request has been imported internally. merged-externally Merged externally in public mojo repo merged-internally Indicates that this pull request has been merged internally
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants