Allow both unicode and bytes dotted string in utils.import_attr
This commit is contained in:
parent
75b3fe4db0
commit
37c975eaf7
@ -13,6 +13,10 @@ Added
|
|||||||
-----
|
-----
|
||||||
* Add a test for login with missing parameter (username or password or both)
|
* Add a test for login with missing parameter (username or password or both)
|
||||||
|
|
||||||
|
Fixed
|
||||||
|
-----
|
||||||
|
* Allow both unicode and bytes dotted string in utils.import_attr
|
||||||
|
|
||||||
|
|
||||||
v0.7.4 - 2016-09-07
|
v0.7.4 - 2016-09-07
|
||||||
===================
|
===================
|
||||||
|
@ -117,14 +117,18 @@ def import_attr(path):
|
|||||||
transform a python dotted path to the attr
|
transform a python dotted path to the attr
|
||||||
|
|
||||||
:param path: A dotted path to a python object or a python object
|
:param path: A dotted path to a python object or a python object
|
||||||
:type path: :obj:`unicode` or anything
|
:type path: :obj:`unicode` or :obj:`str` or anything
|
||||||
:return: The python object pointed by the dotted path or the python object unchanged
|
:return: The python object pointed by the dotted path or the python object unchanged
|
||||||
"""
|
"""
|
||||||
if not isinstance(path, str):
|
# if we got a str, decode it to unicode (normally it should only contain ascii)
|
||||||
|
if isinstance(path, six.binary_type):
|
||||||
|
path = path.decode("utf-8")
|
||||||
|
# if path is not an unicode, return it unchanged (may be it is already the attribute to import)
|
||||||
|
if not isinstance(path, six.text_type):
|
||||||
return path
|
return path
|
||||||
if "." not in path:
|
if u"." not in path:
|
||||||
ValueError("%r should be of the form `module.attr` and we just got `attr`" % path)
|
ValueError("%r should be of the form `module.attr` and we just got `attr`" % path)
|
||||||
module, attr = path.rsplit('.', 1)
|
module, attr = path.rsplit(u'.', 1)
|
||||||
try:
|
try:
|
||||||
return getattr(import_module(module), attr)
|
return getattr(import_module(module), attr)
|
||||||
except ImportError:
|
except ImportError:
|
||||||
|
Loading…
Reference in New Issue
Block a user