Recently I had an issue where a signal I was using from flask-security was not behaving as expected in python 3.3. In looking at the source code for flask-security I noticed that the signal I was importing from a module in the flask-security package was also imported in __init__.py
. By importing the signal from the top level of the package I was able to solve my problem (since the signal gets imported when package is initialized).
If I run the following code:
from flask.ext.security import user_registered
from flask.ext.security.signals import user_registered as user_reg_sig
user_registered==user_reg_sig
I'll get True
in python 2.7 and I'll get False
for python 3.3+.
What is different in python 3.3+ that causes this difference in import behaviour?
EDIT:
I'm still stumped by the python 2.7 vs 3.3+ issue, but have managed to narrow down that the issue is occurring when the __init__.py
of flask.ext is called and uses the ExtensionImporter class from exthook.py to import flask-security.
Running the following under python 3.4 returns True
when flask-security is directly imported avoiding the extension hook:
from flask_security.signals import user_registered as user_reg_sig
from flask_security import user_registered
user_registered==user_reg_sig
Here is repr() for signals for flask.ext.security and flask_security examples:
from flask_security.signals import user_registered as user_reg_sig
from flask_security import user_registered
repr(user_registered)
>>> "<blinker.base.NamedSignal object at 0x7fb38e258400; 'user-registered'>"
repr(user_reg_sig)
>>> "<blinker.base.NamedSignal object at 0x7fb38e258400; 'user-registered'>"
from flask.ext.security import user_registered
from flask.ext.security.signals import user_registered as user_reg_sig
repr(user_registered)
>>> "<blinker.base.NamedSignal object at 0x7fb38e258400; 'user-registered'>"
repr(user_reg_sig)
>>> "<blinker.base.NamedSignal object at 0x7fb38dd030b8; 'user-registered'>"