Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions gazelle/std_modules.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ func init() {
cmd := exec.CommandContext(ctx, stdModulesScriptRunfile)

cmd.Stderr = os.Stderr
cmd.Env = []string{}

// All userland site-packages should be ignored.
cmd.Env = []string{"PYTHONNOUSERSITE=1"}
stdin, err := cmd.StdinPipe()
if err != nil {
log.Printf("failed to initialize std_modules: %v\n", err)
Expand Down
28 changes: 13 additions & 15 deletions gazelle/std_modules.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,28 @@
# it evaluates, it outputs true/false for whether the module is part of the
# standard library or not.

import site
import os
import sys


# Don't return any paths, all userland site-packages should be ignored.
def __override_getusersitepackages__():
return ""


site.getusersitepackages = __override_getusersitepackages__
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This code wasn't having the desired effect.

Copy link
Member

Choose a reason for hiding this comment

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

Interesting.

from contextlib import redirect_stdout


def is_std_modules(module):
try:
__import__(module, globals(), locals(), [], 0)
return True
except Exception:
return False
# If for some reason a module (such as pygame, see https://github.com/pygame/pygame/issues/542)
# prints to stdout upon import,
# the output of this script should still be parseable by golang.
# Therefore, redirect stdout while running the import.
with redirect_stdout(os.devnull):
try:
__import__(module, globals(), locals(), [], 0)
return True
except Exception:
return False


def main(stdin, stdout):
for module in stdin:
module = module.strip()
# Don't print the boolean directly as it is captilized in Python.
# Don't print the boolean directly as it is capitalized in Python.
Copy link
Member

Choose a reason for hiding this comment

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

This was the Brazilian spelling. 🤣
Thanks for fixing it.

print(
"true" if is_std_modules(module) else "false",
end="\n",
Expand Down