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

WSL Method Implementation. #190

Closed
wants to merge 2 commits into from
Closed

WSL Method Implementation. #190

wants to merge 2 commits into from

Conversation

ccrutchf
Copy link

This is an initial pass to fix #36, #161. It is missing unit tests. As it calls out to PowerShell, I don't know how to best write unit tests testing the functionality.

I figured that as I try to come up with unit tests, I could get your feedback on this PR.

This could also be adapted to fix #167.


from wakepy.core import DbusAdapter, Method, ModeName, PlatformName

class Flags(enum.StrEnum):
Copy link
Owner

Choose a reason for hiding this comment

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

The enum.StrEnum is unfortunately available only for python 3.11 and above. I've created my own backport which can be imported from wakepy.core. Let's use that instead so we can keep wakepy supported from python 3.7 onwards.



class WslSetThreadExecutionState(Method, ABC):
"""This is a method which calls the SetThreadExecutionState function from
Copy link
Owner

Choose a reason for hiding this comment

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

Thanks for keeping a good level of docstrings / comments!

I've been using a strict line limits of 79 for comments and dosctrings, and 88 (from black) for formatting the code. I guess I should write those down to some contribution guide, and probably add some tests for the comments line length.

If you're using vs code, you could add this to your workspace settings:

  "editor.rulers": [
    {
      "column": 79,
      "color": "#664766"
    },
    {
      "column": 88,
      "color": "#7e4343"
    },
  ],

I have a laptop with very small screen and these line lengths make it possible to see two tabs of code side-by-side :)

self._powershell = Popen(["powershell.exe"], stdout=PIPE, stderr=None, stdin=PIPE, universal_newlines=True)

# C# Code to allow PowerShell to call SetThreadExecutionState
code = """$code = @'
Copy link
Owner

Choose a reason for hiding this comment

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

I wonder if we could use textwrap.dedent to make this indented, too?

We could also include the explanation for the non-obvious SetLastError. Perhaps

SetLastError makes it possible to look up the result of the function (currently not used, though).

# Write a blank line to complete the code block.
self._write("")

# Add the code above to the PowerShell Context.
Copy link
Owner

Choose a reason for hiding this comment

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

This was the line I had question in the initial version, so I suggest we add your answer as comment :D something like:

Some C# code to add the `code` above to the PowerShell Context. Add-Type call sets it up as something we can call within PowerShell.

?


# Tests the result of the SetThreadExecutionState.
# Close PowerShell if we failed. This is easy to test for.
self._write("([System.Runtime.InteropServices.Marshal]::GetLastWin32Error() -eq 0) | Where-Object { $_ } | Foreach-Object { exit -1 }")
Copy link
Owner

Choose a reason for hiding this comment

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

This goes a bit above my head but nice that there's a check for the success and RuntimeError if it did not succeed!

@fohrloop
Copy link
Owner

Hi @ccrutchf , thanks for the PR! Very nice work! :)

I commented on the code about few small details.

re: Unit tests

Yeah this is the same pain in all of the wakepy.Methods, as they all need to call some other software to work. I think it's enough that the automatic tests test that the external software gets the calls we expect. I've been just mocking or faking the required parts and checking that they're called in correct order and with correct arguments.

Then, in manual tests (done once per Method when creating it), I check that the mode is actually activated and it works as it should.

I just ran tox on my machine and it passed on python 3.12 but failed on 3.7 and 3.10, most likely due to the missing enum.StrEnum. It also failed the formatting check. You may autoformat the code with isort + black. Usually that settles it.

Manual tests

I could boot to Windows to test this manually. Did you already test this on WSL?

Windows support?

I was wondering if this would actually work also on Windows? If so, this could supersede the old windows.py (perhaps then rename to kernel32.py or something..? This already should fix the #167 as is, so it is quite attractive also in that sense. And it's nice if there's less code to be maintained :) I can also test this once I have some time.

@fohrloop
Copy link
Owner

Hi @ccrutchf , have you had time to think about unit tests? Would you still like to provide them? If not, I could take this as is, merge to dev (after a rebase), and write the unit tests :)

@fohrloop fohrloop deleted the branch fohrloop:dev May 26, 2024 08:18
@fohrloop fohrloop closed this May 26, 2024
@fohrloop
Copy link
Owner

Hi, I removed the dev branch to close #305 (that closed this PR automatically). The changes could be merged to main. How it would be done in practice is to sync the fork (or remove the old one and create a new one), and (1) copy the wsl.py and (2) edit the 3 other files (1+1+6 lines), or save the modifications as a patch file and apply the patch.

@fohrloop fohrloop mentioned this pull request Jun 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants