Skip to content

Commit

Permalink
Merge pull request #333 from mehdy/develop
Browse files Browse the repository at this point in the history
Handle escaped dollar sign in values
  • Loading branch information
sergeyklay authored Oct 7, 2021
2 parents 7dc1c1b + 1b6c4ba commit ef15435
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ Fixed
+++++
- Keep newline/tab escapes in quoted strings
`#296 <https://github.com/joke2k/django-environ/pull/296>`_.
- Handle escaped dolloar sign in values
`#271 <https://github.com/joke2k/django-environ/issues/271>`_.


`v0.7.0`_ - 11-September-2021
Expand Down
17 changes: 17 additions & 0 deletions docs/tips.rst
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,23 @@ Values that being with a ``$`` may be interpolated. Pass ``interpolate=True`` to
FOO
Escape Proxy
============

If you're having trouble with values starting with dollar sign ($) without the intention of proxying the value to
another, You should enbale the ``escape_proxy`` and prepend a backslash to it.

.. code-block:: python
import environ
env = environ.Env()
env.escape_proxy = True
# ESCAPED_VAR=\$baz
env.str('ESCAPED_VAR') # $baz
Reading env files
=================

Expand Down
5 changes: 5 additions & 0 deletions environ/environ.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ class Env:

def __init__(self, **scheme):
self.smart_cast = True
self.escape_proxy = False
self.scheme = scheme

def __call__(self, var, cast=None, default=NOTSET, parse_default=False):
Expand Down Expand Up @@ -365,10 +366,14 @@ def get_value(self, var, cast=None, default=NOTSET, parse_default=False):

# Resolve any proxied values
prefix = b'$' if isinstance(value, bytes) else '$'
escape = rb'\$' if isinstance(value, bytes) else r'\$'
if hasattr(value, 'startswith') and value.startswith(prefix):
value = value.lstrip(prefix)
value = self.get_value(value, cast=cast, default=default)

if self.escape_proxy and hasattr(value, 'replace'):
value = value.replace(escape, prefix)

# Smart casting
if self.smart_cast:
if cast is None and default is not None and \
Expand Down
1 change: 1 addition & 0 deletions tests/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ def generate_data(cls):
BOOL_FALSE_STRING_LIKE_BOOL='False',
BOOL_FALSE_BOOL=False,
PROXIED_VAR='$STR_VAR',
ESCAPED_VAR=r'\$baz',
INT_LIST='42,33',
INT_TUPLE='(42,33)',
STR_LIST_WITH_SPACES=' foo, bar',
Expand Down
8 changes: 8 additions & 0 deletions tests/test_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,14 @@ def test_bool_true(self, value, variable):
def test_proxied_value(self):
assert self.env('PROXIED_VAR') == 'bar'

def test_escaped_dollar_sign(self):
self.env.escape_proxy = True
assert self.env('ESCAPED_VAR') == '$baz'

def test_escaped_dollar_sign_disabled(self):
self.env.escape_proxy = False
assert self.env('ESCAPED_VAR') == r'\$baz'

def test_int_list(self):
assert_type_and_value(list, [42, 33], self.env('INT_LIST', cast=[int]))
assert_type_and_value(list, [42, 33], self.env.list('INT_LIST', int))
Expand Down
1 change: 1 addition & 0 deletions tests/test_env.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ FLOAT_STRANGE_VAR1=123,420,333.3
FLOAT_STRANGE_VAR2=123.420.333,3
FLOAT_NEGATIVE_VAR=-1.0
PROXIED_VAR=$STR_VAR
ESCAPED_VAR=\$baz
EMPTY_LIST=
EMPTY_INT_VAR=
INT_VAR=42
Expand Down

0 comments on commit ef15435

Please sign in to comment.