Commits

Jason R. Coombs  committed 66c41b2

Fixed issue with nto? not failing on unexpected types.

  • Participants
  • Parent commits 54b89ba
  • Branches cherrypy-3.2.x

Comments (0)

Files changed (1)

File cherrypy/_cpcompat.py

     basestring = (bytes, str)
     def ntob(n, encoding='ISO-8859-1'):
         """Return the given native string as a byte string in the given encoding."""
+        assert_native(n)
         # In Python 3, the native string type is unicode
         return n.encode(encoding)
     def ntou(n, encoding='ISO-8859-1'):
         """Return the given native string as a unicode string with the given encoding."""
+        assert_native(n)
         # In Python 3, the native string type is unicode
         return n
     def tonative(n, encoding='ISO-8859-1'):
     basestring = basestring
     def ntob(n, encoding='ISO-8859-1'):
         """Return the given native string as a byte string in the given encoding."""
+        assert_native(n)
         # In Python 2, the native string type is bytes. Assume it's already
         # in the given encoding, which for ISO-8859-1 is almost always what
         # was intended.
         return n
     def ntou(n, encoding='ISO-8859-1'):
         """Return the given native string as a unicode string with the given encoding."""
+        assert_native(n)
         # In Python 2, the native string type is bytes.
         # First, check for the special encoding 'escape'. The test suite uses this
         # to signal that it wants to pass a string with embedded \uXXXX escapes,
     # bytes:
     BytesIO = StringIO
 
+def assert_native(n):
+    if not isinstance(n, nativestr):
+        raise TypeError("n must be a native str (got %s)" % type(n).__name__)
+
 try:
     set = set
 except NameError: