<br><br><div class="gmail_quote">On Mon, Feb 23, 2009 at 04:02, Nick Coghlan <span dir="ltr">&lt;<a href="mailto:ncoghlan@gmail.com">ncoghlan@gmail.com</a>&gt;</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>
&gt; I don&#39;t want to move it because this isn&#39;t some idea for a new feature<br>
&gt; that may or may not be useful; this isn&#39;t an &quot;idea&quot;, it&#39;s needed.<br>
<br>
</div>It is needed, but it&#39;s only really needed in the test suite. The<br>
&quot;sys.modules hackery&quot; needed to get a Python-only version using the<br>
existing idiom really isn&#39;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&#39;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&#39;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&#39;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>&nbsp;</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>
 &nbsp;import sys<br>
 &nbsp;def import_python_only(mod_name, *ext_names):<br>
 &nbsp; &nbsp;if not ext_names:<br>
 &nbsp; &nbsp; &nbsp; &nbsp;ext_names = ((&quot;_&quot; + mod_name),)<br>
 &nbsp; &nbsp;orig_modules = {}<br>
 &nbsp; &nbsp;if name in sys.modules:<br>
 &nbsp; &nbsp; &nbsp;orig_modules[name] = sys.modules[name]<br>
 &nbsp; &nbsp; &nbsp;del sys.modules[name]<br>
 &nbsp; &nbsp;try:<br>
 &nbsp; &nbsp; &nbsp;for name in ext_names:<br>
 &nbsp; &nbsp; &nbsp; &nbsp;orig_modules[name] = sys.modules[name]<br>
 &nbsp; &nbsp; &nbsp; &nbsp;sys.modules[name] = 0<br>
 &nbsp; &nbsp; &nbsp;py_module = importlib.import_module(mod_name)<br>
 &nbsp; &nbsp;finally:<br>
 &nbsp; &nbsp; &nbsp;for name, module in orig_modules.items():<br>
 &nbsp; &nbsp; &nbsp; &nbsp;sys.modules[name] = module<br>
 &nbsp; &nbsp;return py_module<br>
<div><div></div><div class="Wj3C7c"><br>
Cheers,<br>
Nick.<br>
<br>
--<br>
Nick Coghlan &nbsp; | &nbsp; <a href="mailto:ncoghlan@gmail.com">ncoghlan@gmail.com</a> &nbsp; | &nbsp; Brisbane, Australia<br>
---------------------------------------------------------------<br>
</div></div></blockquote></div><br>