Handle unicode strings with import_string (refs GH-627)

This commit is contained in:
David Cramer 2015-07-13 02:38:18 -07:00
parent 88f1dfd3ea
commit 87b2034197
2 changed files with 29 additions and 1 deletions

View File

@ -1,10 +1,16 @@
from __future__ import absolute_import
from . import six
def import_string(key):
# HACK(dcramer): Ensure a unicode key is still importable
if not six.PY3:
key = str(key)
if '.' not in key:
return __import__(key)
module_name, class_name = key.rsplit('.', 1)
module = __import__(module_name, {}, {}, [class_name])
module = __import__(module_name, {}, {}, [class_name], 0)
return getattr(module, class_name)

View File

@ -0,0 +1,22 @@
from __future__ import absolute_import
import raven
from raven.utils import six
from raven.utils.imports import import_string
def test_import_string():
new_raven = import_string('raven')
assert new_raven is raven
# this will test unicode on python2
new_raven = import_string(six.text_type('raven'))
assert new_raven is raven
new_client = import_string('raven.Client')
assert new_client is raven.Client
# this will test unicode on python2
new_client = import_string(six.text_type('raven.Client'))
assert new_client is raven.Client