Skip to content

Commit 1f97377

Browse files
bbc2theskumar
authored andcommitted
Fix parsing of variable without a value (theskumar#158)
In Python, `\s` also matches newlines. This would cause newlines to be consumed by the equal sign matcher, resulting in the next line being considered as a value. For instance, the following would be parsed as `[("FOO", "BAR=b")]` instead of [("FOO", ""), ("BAR", "b")]. ```bash FOO= BAR=b ``` Fixes theskumar#157.
1 parent 3daef30 commit 1f97377

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

dotenv/main.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@
2121
r"""
2222
(
2323
\s* # leading whitespace
24-
(?:export\s+)? # export
24+
(?:export{0}+)? # export
2525
2626
( '[^']+' # single-quoted key
2727
| [^=\#\s]+ # or unquoted key
2828
)?
2929
3030
(?:
31-
(?:\s*=\s*) # equal sign
31+
(?:{0}*={0}*) # equal sign
3232
3333
( '(?:\\'|[^'])*' # single-quoted value
3434
| "(?:\\"|[^"])*" # or double-quoted value
@@ -40,7 +40,7 @@
4040
(?:\#[^\r\n]*)? # comment
4141
(?:\r|\n|\r\n)? # newline
4242
)
43-
""",
43+
""".format(r'[^\S\r\n]'),
4444
re.MULTILINE | re.VERBOSE,
4545
)
4646

tests/test_core.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,13 @@ def restore_os_environ():
6363
Binding(key="c", value="d", original="c=d"),
6464
],
6565
),
66+
(
67+
'a=\nb=c',
68+
[
69+
Binding(key="a", value='', original='a=\n'),
70+
Binding(key="b", value='c', original="b=c"),
71+
]
72+
),
6673
(
6774
'a="\nb=c',
6875
[

0 commit comments

Comments
 (0)