Skip to content
This repository has been archived by the owner on Oct 23, 2023. It is now read-only.

Commit

Permalink
Handle unicode strings with import_string (refs GH-627)
Browse files Browse the repository at this point in the history
  • Loading branch information
dcramer committed Jul 13, 2015
1 parent 88f1dfd commit 87b2034
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
8 changes: 7 additions & 1 deletion raven/utils/imports.py
Original file line number Diff line number Diff line change
@@ -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)
22 changes: 22 additions & 0 deletions tests/utils/test_imports.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 87b2034

Please sign in to comment.