Skip to content

Commit

Permalink
ToBytes/ToUnicode return empty bytes/str for None
Browse files Browse the repository at this point in the history
They used to return u'None'/b'None', which we don't want.
  • Loading branch information
Valloric committed Feb 21, 2016
1 parent 528dbad commit 41b8ce8
Showing 2 changed files with 17 additions and 0 deletions.
12 changes: 12 additions & 0 deletions ycmd/tests/utils_test.py
Original file line number Diff line number Diff line change
@@ -85,6 +85,12 @@ def ToBytes_Int_test():
eq_( type( value ), bytes )


def ToBytes_None_test():
value = utils.ToBytes( None )
eq_( value, bytes( b'' ) )
eq_( type( value ), bytes )


if PY2:
def ToUnicode_Py2Bytes_test():
value = utils.ToUnicode( bytes( 'abc' ) )
@@ -134,6 +140,12 @@ def ToUnicode_Int_test():
ok_( isinstance( value, str ) )


def ToUnicode_None_test():
value = utils.ToUnicode( None )
eq_( value, u'' )
ok_( isinstance( value, str ) )


if PY2:
def ToCppStringCompatible_Py2Str_test():
value = utils.ToCppStringCompatible( 'abc' )
5 changes: 5 additions & 0 deletions ycmd/utils.py
Original file line number Diff line number Diff line change
@@ -79,6 +79,8 @@ def ToCppStringCompatible( value ):
# Returns a unicode type; either the new python-future str type or the real
# unicode type. The difference shouldn't matter.
def ToUnicode( value ):
if not value:
return str()
if isinstance( value, str ):
return value
if isinstance( value, bytes ):
@@ -90,6 +92,9 @@ def ToUnicode( value ):
# Consistently returns the new bytes() type from python-future. Assumes incoming
# strings are either UTF-8 or unicode (which is converted to UTF-8).
def ToBytes( value ):
if not value:
return bytes()

# This is tricky. On py2, the bytes type from builtins (from python-future) is
# a subclass of str. So all of the following are true:
# isinstance(str(), bytes)

0 comments on commit 41b8ce8

Please sign in to comment.