Skip to content

Commit 9107927

Browse files
committed
Allow spaces before = in dotenv
1 parent 18cee2e commit 9107927

File tree

4 files changed

+63
-68
lines changed

4 files changed

+63
-68
lines changed

shared-module/dotenv/__init__.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ STATIC bool key_matches(file_arg *active_file, const char *key) {
137137
if (character == '=' || character == '\n' || character == '#' || character == 0) {
138138
// Rewind one so the value, if any, can be found.
139139
seek_minus_one(active_file);
140-
} else {
140+
} else if (!unichar_isspace(character)) {
141141
// We're followed by something else that is invalid syntax.
142142
matches = false;
143143
}

tests/circuitpython/dotenv_test.env

Lines changed: 21 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,23 @@
1-
# No e0 value
1+
# Key "notpresent" is not present
22
# comment preceded by spaces
3-
e1=e1value
4-
e2=e2value # value followed by a comment
5-
e3='e3value'
6-
e4='e4value' # quoted value followed by a comment
7-
# e5 should be None
8-
e5
9-
# e6 should be the empty string
10-
e6=
11-
# e7 should be '#' (bash-like syntax processing)
12-
e7=#
13-
# e8 should be the empty string
14-
e8=''
15-
# e9 should be the empty string
16-
e9= #
17-
e10=e10_first
18-
e10=e10_last
19-
e11='abc#def'
20-
# e12 should be 'abc#def'
21-
e12=abc#def
22-
e12='multi
3+
plain_value=value
4+
value_with_comment=value # value followed by a comment
5+
quoted_value='value'
6+
quoted_value_with_comment='value' # quoted value followed by a comment
7+
should_be_none
8+
should_be_empty_string=
9+
should_be_hash=#
10+
quoted_should_be_empty_string=''
11+
duplicate_key=wrong
12+
duplicate_key=right
13+
value_with_hash=value#value
14+
quoted_value_with_hash='value#value'
15+
multi_line_value='multi
2316
line'
24-
e13=e13value
25-
e14 #comment
26-
e15 = e15value
27-
# e16 should be '#'
28-
e16=# #
29-
# e17 should be 'def#hi'
30-
e17='def'#hi
31-
# e18 should be '#has a hash'
32-
e18=#has a hash
17+
space_before_key=value
18+
space_before_value= value
19+
space_before_hash_value= #value
20+
space_after_key =value
21+
space_after_key_before_value = value
22+
quoted_then_comment='value'#comment
23+
hash_with_spaces=#value value

tests/circuitpython/dotenv_test.py

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,24 @@
22

33
FILE = __file__.rsplit(".", 1)[0] + ".env"
44

5-
print("e0", dotenv.get_key(FILE, "e0"))
6-
print("e1", dotenv.get_key(FILE, "e1"))
7-
print("e2", dotenv.get_key(FILE, "e2"))
8-
print("e3", dotenv.get_key(FILE, "e3"))
9-
print("e4", dotenv.get_key(FILE, "e4"))
10-
print("e5", dotenv.get_key(FILE, "e5"))
11-
print("e6", dotenv.get_key(FILE, "e6"))
12-
print("e7", dotenv.get_key(FILE, "e7"))
13-
print("e8", dotenv.get_key(FILE, "e8"))
14-
print("e9", dotenv.get_key(FILE, "e9"))
15-
print("e10", dotenv.get_key(FILE, "e10"))
16-
print("e11", dotenv.get_key(FILE, "e11"))
17-
print("e12", dotenv.get_key(FILE, "e12"))
18-
print("e13", dotenv.get_key(FILE, "e13"))
19-
print("e14", dotenv.get_key(FILE, "e14"))
20-
# print("e15", dotenv.get_key(FILE, "e15"))
21-
print("e16", dotenv.get_key(FILE, "e16"))
22-
print("e17", dotenv.get_key(FILE, "e17"))
23-
print("e18", dotenv.get_key(FILE, "e18"))
5+
print(f"notpresent={dotenv.get_key(FILE, 'notpresent')}")
6+
print(f"plain_value={dotenv.get_key(FILE, 'plain_value')}")
7+
print(f"value_with_comment={dotenv.get_key(FILE, 'value_with_comment')}")
8+
print(f"quoted_value={dotenv.get_key(FILE, 'quoted_value')}")
9+
print(f"quoted_value_with_comment={dotenv.get_key(FILE, 'quoted_value_with_comment')}")
10+
print(f"should_be_none={dotenv.get_key(FILE, 'should_be_none')}")
11+
print(f"should_be_empty_string={dotenv.get_key(FILE, 'should_be_empty_string')}")
12+
print(f"should_be_hash={dotenv.get_key(FILE, 'should_be_hash')}")
13+
print(f"quoted_should_be_empty_string={dotenv.get_key(FILE, 'quoted_should_be_empty_string')}")
14+
print(f"duplicate_key={dotenv.get_key(FILE, 'duplicate_key')}")
15+
### This is the a difference from CPython dotenv. The trailing #value is taken as a comment.
16+
print(f"value_with_hash={dotenv.get_key(FILE, 'value_with_hash')}")
17+
print(f"quoted_value_with_hash={dotenv.get_key(FILE, 'quoted_value_with_hash')}")
18+
print(f"multi_line_value={dotenv.get_key(FILE, 'multi_line_value')}")
19+
print(f"space_before_key={dotenv.get_key(FILE, 'space_before_key')}")
20+
print(f"space_before_value={dotenv.get_key(FILE, 'space_before_value')}")
21+
print(f"space_before_hash_value={dotenv.get_key(FILE, 'space_before_hash_value')}")
22+
print(f"space_after_key={dotenv.get_key(FILE, 'space_after_key')}")
23+
print(f"space_after_key_before_value={dotenv.get_key(FILE, 'space_after_key_before_value')}")
24+
print(f"quoted_then_comment={dotenv.get_key(FILE, 'quoted_then_comment')}")
25+
print(f"hash_with_spaces={dotenv.get_key(FILE, 'hash_with_spaces')}")
Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
1-
e0 None
2-
e1 e1value
3-
e2 e2value
4-
e3 e3value
5-
e4 e4value
6-
e5 None
7-
e6
8-
e7 #
9-
e8
10-
e9 #
11-
e10 e10_last
12-
e11 abc#def
13-
e12 multi
1+
notpresent=None
2+
plain_value=value
3+
value_with_comment=value
4+
quoted_value=value
5+
quoted_value_with_comment=value
6+
should_be_none=None
7+
should_be_empty_string=
8+
should_be_hash=#
9+
quoted_should_be_empty_string=
10+
duplicate_key=right
11+
value_with_hash=value
12+
quoted_value_with_hash=value#value
13+
multi_line_value=multi
1414
line
15-
e13 e13value
16-
e14 None
17-
e16 #
18-
e17 def
19-
e18 #has a hash
15+
space_before_key=value
16+
space_before_value=value
17+
space_before_hash_value=#value
18+
space_after_key=value
19+
space_after_key_before_value=value
20+
quoted_then_comment=value
21+
hash_with_spaces=#value value

0 commit comments

Comments
 (0)