Skip to content

Commit 8c4acd4

Browse files
committed
pass reloader tests
python doesn't have an obvious way to reveal the original command and module name, so we check the __main__ module for __package__ and infer it's a module running as a script unless the value is None, which implies a file path was provided modules running as scripts are invoked with -m flags packages running with scripts have base filename __main__.py from this we recreate an invocation
1 parent c1a4cb9 commit 8c4acd4

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

bottle.py

+14-4
Original file line numberDiff line numberDiff line change
@@ -3633,6 +3633,14 @@ def load_app(target):
36333633

36343634
_debug = debug
36353635

3636+
def _main_module_name():
3637+
package = vars(sys.modules['__main__'])['__package__']
3638+
if package is None:
3639+
return None
3640+
else:
3641+
file_path = sys.argv[0]
3642+
base_name, _ = os.path.splitext(os.path.basename(file_path))
3643+
return package if base_name == '__main__' else '.'.join((package, base_name))
36363644

36373645
def run(app=None,
36383646
server='wsgiref',
@@ -3667,11 +3675,13 @@ def run(app=None,
36673675
try:
36683676
fd, lockfile = tempfile.mkstemp(prefix='bottle.', suffix='.lock')
36693677
os.close(fd) # We only need this file to exist. We never write to it
3678+
module_name = _main_module_name()
3679+
options = [sys.argv[0]] if module_name is None else ['-m', module_name]
3680+
args = [sys.executable] + options + sys.argv[1:]
3681+
environ = os.environ.copy()
3682+
environ['BOTTLE_CHILD'] = 'true'
3683+
environ['BOTTLE_LOCKFILE'] = lockfile
36703684
while os.path.exists(lockfile):
3671-
args = [sys.executable] + sys.argv
3672-
environ = os.environ.copy()
3673-
environ['BOTTLE_CHILD'] = 'true'
3674-
environ['BOTTLE_LOCKFILE'] = lockfile
36753685
p = subprocess.Popen(args, env=environ)
36763686
while p.poll() is None: # Busy wait...
36773687
os.utime(lockfile, None) # I am alive!

0 commit comments

Comments
 (0)