This repository has been archived by the owner on Oct 23, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 658
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Handle unicode strings with import_string (refs GH-627)
- Loading branch information
Showing
2 changed files
with
29 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |