File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -464,6 +464,14 @@ def test_bytes_within_range(self):
464464 with self .assertRaises (ValueError ):
465465 b2 = bytes ([254 , 255 , 256 ])
466466
467+ def test_bytes_hasattr_encode (self ):
468+ """
469+ This test tests whether hasattr(b, 'encode') is False, like it is on Py3.
470+ """
471+ b = bytes (b'abcd' )
472+ self .assertFalse (hasattr (b , 'encode' ))
473+ self .assertTrue (hasattr (b , 'decode' ))
474+
467475
468476if __name__ == '__main__' :
469477 unittest .main ()
Original file line number Diff line number Diff line change @@ -78,6 +78,19 @@ def test_str_is_str(self):
7878 def test_str_fromhex (self ):
7979 self .assertFalse (hasattr (str , 'fromhex' ))
8080
81+ def test_str_hasattr_decode (self ):
82+ """
83+ This test tests whether hasattr(s, 'decode') is False, like it is on Py3.
84+
85+ Sometimes code (such as http.client in Py3.3) checks hasattr(mystring,
86+ 'decode') to determine if a string-like thing needs encoding. It would
87+ be nice to have this return False so the string can be treated on Py2
88+ like a Py3 string.
89+ """
90+ s = str (u'abcd' )
91+ self .assertFalse (hasattr (s , 'decode' ))
92+ self .assertTrue (hasattr (s , 'encode' ))
93+
8194 def test_isinstance_str (self ):
8295 self .assertTrue (isinstance (str ('blah' ), str ))
8396
You can’t perform that action at this time.
0 commit comments