Skip to content

Commit c8b2402

Browse files
Avoid function-scoping global variables
In 2e6f2e8, we added a main function to the publish_toolstate.py script. Unfortunately, we missed that the Python program implicitly declares global variables in that code, which means that adding a function changes variable scoping and breaks other code. This commit avoids introducing that function and adds a warning to future editors of the code.
1 parent 48717b6 commit c8b2402

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

src/tools/publish_toolstate.py

+9-9
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,12 @@ def update_latest(
275275
return message
276276

277277

278-
def main():
278+
# Warning: Do not try to add a function containing the body of this try block.
279+
# There are variables declared within that are implicitly global; it is unknown
280+
# which ones precisely but at least this is true for `github_token`.
281+
try:
282+
if __name__ != '__main__':
283+
exit(0)
279284
repo = os.environ.get('TOOLSTATE_VALIDATE_MAINTAINERS_REPO')
280285
if repo:
281286
github_token = os.environ.get('TOOLSTATE_REPO_ACCESS_TOKEN')
@@ -342,11 +347,6 @@ def main():
342347
}
343348
))
344349
response.read()
345-
346-
347-
if __name__ == '__main__':
348-
try:
349-
main()
350-
except urllib2.HTTPError as e:
351-
print("HTTPError: %s\n%s" % (e, e.read()))
352-
raise
350+
except urllib2.HTTPError as e:
351+
print("HTTPError: %s\n%s" % (e, e.read()))
352+
raise

0 commit comments

Comments
 (0)