<br><br><div class="gmail_quote">On Mon, Feb 23, 2009 at 04:02, Nick Coghlan <span dir="ltr"><<a href="mailto:ncoghlan@gmail.com">ncoghlan@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<div class="Ih2E3d">Brett Cannon wrote:<br>
> I don't want to move it because this isn't some idea for a new feature<br>
> that may or may not be useful; this isn't an "idea", it's needed.<br>
<br>
</div>It is needed, but it's only really needed in the test suite. The<br>
"sys.modules hackery" needed to get a Python-only version using the<br>
existing idiom really isn't that complicated and the associated import<br>
behaviour is perfectly well defined (putting a 0 in sys.modules may<br>
currently be a bit questionable, but I'd prefer to make sure that is<br>
officially supported with the desired effect rather than trying to<br>
define a new idiom for the actual library code for handling optional<br>
optimised extension modules).<br>
<br>
So, I'm still not seeing any significant problem with providing a<br>
utility function in test.support that hides that hackery and returns the<br>
pure Python version of the module.<br>
</blockquote><div><br>Well, neither do I as your proposed approach below is what I do for warnings. But I think you and I, Nick, are more comfortable with mucking with imports than most people. =) I think some people early on in this thread said they didn't like the idea of screwing around with sys.modules. But doing that along with an import * from the extension module is probably the simplest solution.<br>
<br>-Brett<br> </div><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"><br>
For example, a version that allows any number of extension modules to be<br>
suppressed when importing a module (defaulting to the Foo/_Foo naming):<br>
<br>
import sys<br>
def import_python_only(mod_name, *ext_names):<br>
if not ext_names:<br>
ext_names = (("_" + mod_name),)<br>
orig_modules = {}<br>
if name in sys.modules:<br>
orig_modules[name] = sys.modules[name]<br>
del sys.modules[name]<br>
try:<br>
for name in ext_names:<br>
orig_modules[name] = sys.modules[name]<br>
sys.modules[name] = 0<br>
py_module = importlib.import_module(mod_name)<br>
finally:<br>
for name, module in orig_modules.items():<br>
sys.modules[name] = module<br>
return py_module<br>
<div><div></div><div class="Wj3C7c"><br>
Cheers,<br>
Nick.<br>
<br>
--<br>
Nick Coghlan | <a href="mailto:ncoghlan@gmail.com">ncoghlan@gmail.com</a> | Brisbane, Australia<br>
---------------------------------------------------------------<br>
</div></div></blockquote></div><br>