From 410f0c998cf4e9d6abe2546e65a01c8403a1f7a4 Mon Sep 17 00:00:00 2001 From: Donald Stufft Date: Fri, 5 Dec 2014 20:25:15 -0500 Subject: [PATCH] Switch to using six --- bcrypt/__init__.py | 14 +++----------- setup.py | 5 +++++ tests/test_bcrypt.py | 5 +++-- 3 files changed, 11 insertions(+), 13 deletions(-) diff --git a/bcrypt/__init__.py b/bcrypt/__init__.py index 2f41e33..4c4ee0f 100644 --- a/bcrypt/__init__.py +++ b/bcrypt/__init__.py @@ -19,10 +19,11 @@ from __future__ import unicode_literals import hashlib import os -import sys from cffi import FFI +import six + from . import __about__ from .__about__ import * @@ -30,15 +31,6 @@ from .__about__ import * __all__ = ["gensalt", "hashpw"] + __about__.__all__ -# True if we are running on Python 3. -PY3 = sys.version_info[0] == 3 - -if PY3: - text_type = str -else: - text_type = unicode - - _crypt_blowfish_dir = "crypt_blowfish-1.2" _bundled_dir = os.path.join(os.path.dirname(__file__), _crypt_blowfish_dir) @@ -87,7 +79,7 @@ def gensalt(rounds=12): def hashpw(password, salt): - if isinstance(password, text_type) or isinstance(salt, text_type): + if isinstance(password, six.text_type) or isinstance(salt, six.text_type): raise TypeError("Unicode-objects must be encoded before hashing") hashed = _ffi.new("unsigned char[]", 128) diff --git a/setup.py b/setup.py index 8ff8ca8..17d7340 100644 --- a/setup.py +++ b/setup.py @@ -6,6 +6,9 @@ from setuptools import setup from setuptools.command.test import test as TestCommand +SIX_DEPENDENCY = "six>=1.4.1" + + class _AttrDict(dict): def __getattr__(self, key): @@ -65,9 +68,11 @@ setup( setup_requires=[ "cffi", + SIX_DEPENDENCY, ], install_requires=[ "cffi", + SIX_DEPENDENCY, ], extras_require={ "tests": [ diff --git a/tests/test_bcrypt.py b/tests/test_bcrypt.py index 920facf..7f589fb 100644 --- a/tests/test_bcrypt.py +++ b/tests/test_bcrypt.py @@ -2,6 +2,7 @@ import os import mock import pytest +import six import bcrypt @@ -109,9 +110,9 @@ def test_hashpw_invalid(): def test_hashpw_str_password(): with pytest.raises(TypeError): - bcrypt.hashpw(bcrypt.text_type("password"), b"$2a$04$cVWp4XaNU8a4v1uMRum2SO") + bcrypt.hashpw(six.text_type("password"), b"$2a$04$cVWp4XaNU8a4v1uMRum2SO") def test_hashpw_str_salt(): with pytest.raises(TypeError): - bcrypt.hashpw(b"password", bcrypt.text_type("$2a$04$cVWp4XaNU8a4v1uMRum2SO")) + bcrypt.hashpw(b"password", six.text_type("$2a$04$cVWp4XaNU8a4v1uMRum2SO"))