From 25892210824d619c019e5bfa2365e52087bf3941 Mon Sep 17 00:00:00 2001 From: Serghei Iakovlev Date: Fri, 8 Sep 2023 22:14:57 +0200 Subject: [PATCH] Make inline comments handling optional and disabled by default --- environ/environ.py | 32 +++++++++++++++++++++++++------- 1 file changed, 25 insertions(+), 7 deletions(-) diff --git a/environ/environ.py b/environ/environ.py index f74884be..051063a5 100644 --- a/environ/environ.py +++ b/environ/environ.py @@ -862,8 +862,8 @@ def search_url_config(cls, url, engine=None): return config @classmethod - def read_env(cls, env_file=None, overwrite=False, encoding='utf8', - **overrides): + def read_env(cls, env_file=None, overwrite=False, parse_comments=False, + encoding='utf8', **overrides): r"""Read a .env file into os.environ. If not given a path to a dotenv path, does filthy magic stack @@ -883,6 +883,8 @@ def read_env(cls, env_file=None, overwrite=False, encoding='utf8', the Django settings module from the Django project root. :param overwrite: ``overwrite=True`` will force an overwrite of existing environment variables. + :param parse_comments: Determines whether to recognize and ignore inline + comments in the .env file. Default is False. :param encoding: The encoding to use when reading the environment file. :param \**overrides: Any additional keyword arguments provided directly to read_env will be added to the environment. If the key matches an @@ -927,13 +929,29 @@ def _keep_escaped_format_characters(match): for line in content.splitlines(): m1 = re.match(r'\A(?:export )?([A-Za-z_0-9]+)=(.*)\Z', line) if m1: + + # Example: + # + # line: KEY_499=abc#def + # key: KEY_499 + # val: abc#def key, val = m1.group(1), m1.group(2) - # Look for value in quotes, ignore post-# comments - # (outside quotes) - m2 = re.match(r"\A\s*'(?