Skip to content

Commit

Permalink
pass reloader tests
Browse files Browse the repository at this point in the history
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
  • Loading branch information
lmmarsano committed Mar 26, 2021
1 parent 5bfcb4d commit 47a55ac
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions bottle.py
Expand Up @@ -3633,6 +3633,14 @@ def load_app(target):

_debug = debug

def _main_module_name():
package = vars(sys.modules['__main__'])['__package__']
if package is None:
return None
else:
file_path = sys.argv[0]
base_name, _ = os.path.splitext(os.path.basename(file_path))
return package if base_name == '__main__' else '.'.join((package, base_name))

def run(app=None,
server='wsgiref',
Expand Down Expand Up @@ -3667,11 +3675,13 @@ def run(app=None,
try:
fd, lockfile = tempfile.mkstemp(prefix='bottle.', suffix='.lock')
os.close(fd) # We only need this file to exist. We never write to it
module_name = _main_module_name()
options = [sys.argv[0]] if module_name is None else ['-m', module_name]
args = [sys.executable] + options + sys.argv[1:]
environ = os.environ.copy()
environ['BOTTLE_CHILD'] = 'true'
environ['BOTTLE_LOCKFILE'] = lockfile
while os.path.exists(lockfile):
args = [sys.executable] + sys.argv
environ = os.environ.copy()
environ['BOTTLE_CHILD'] = 'true'
environ['BOTTLE_LOCKFILE'] = lockfile
p = subprocess.Popen(args, env=environ)
while p.poll() is None: # Busy wait...
os.utime(lockfile, None) # I am alive!
Expand Down

0 comments on commit 47a55ac

Please sign in to comment.