webapp-improved icon indicating copy to clipboard operation
webapp-improved copied to clipboard

Fixes issues with security.create_token() and Unicode problems

Open gorakhargosh opened this issue 14 years ago • 0 comments

bytes, str, unicode, and basestring mean different things to Python 2.5, 2.6, and 3.x.

Python 2.5

  • bytes is not available.
  • str is a byte string.
  • unicode converts to unicode string.
  • basestring exists.

Python 2.6

  • bytes is available and maps to str
  • str is a byte string.
  • unicode converts to unicode string
  • basestring exists.

Python 3.x

  • bytes is available and does not map to str.
  • str maps to the earlier unicode, but unicode has been removed.
  • basestring has been removed.
  • unicode has been removed

This patch adds portable support for all three versions of Python by adding a portable types module. All Unicode handling has been replaced to use the functions from this module and tests pass.

It introduces these portable types that you can use in your code:

  • bytes where you need byte strings.
  • unicode_string where you need unicode strings
  • a few other utility functions that hide all the complications behind type checking therefore cleaning up the code base.
  • The function create_token() can now accept a numerical base argument.
  • All create_token() usages have been updated to return expected hexadecimal string.
  • Tests have been added for decimals.
  • All create_token() tests pass.
  • bytes is a Python 2.6+ type not available in Python 2.5. This patch fixes it with a suitable replacement.

webapp2 is still small, but it doesn't necessarily mean we can't use packages. All the existing tests pass without affecting any existing parent projects.

This patch ensures webapp2 can be easily ported to Python 3.x in the future as and when required.

Signed-off-by: Gora Khargosh [email protected]

gorakhargosh avatar Jul 10 '11 03:07 gorakhargosh