<p>If it doesn&#39;t pass PyDict_CheckExact you won&#39;t be able to use it as the globals to eval or exec. That&#39;s assuming you hack the module type so you can change its __dict__.</p>
<div class="gmail_quote">On Jan 19, 2012 2:01 AM, &quot;Robert Bradshaw&quot; &lt;<a href="mailto:robertwb@math.washington.edu">robertwb@math.washington.edu</a>&gt; wrote:<br type="attribution"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
On Wed, Jan 18, 2012 at 11:53 PM, Vitja Makarov &lt;<a href="mailto:vitja.makarov@gmail.com">vitja.makarov@gmail.com</a>&gt; wrote:<br>
&gt; 2012/1/19 Vitja Makarov &lt;<a href="mailto:vitja.makarov@gmail.com">vitja.makarov@gmail.com</a>&gt;:<br>
&gt;&gt; 2012/1/19 Robert Bradshaw &lt;<a href="mailto:robertwb@math.washington.edu">robertwb@math.washington.edu</a>&gt;:<br>
&gt;&gt;&gt; I think the right thing to do here is make all module-level globals<br>
&gt;&gt;&gt; into &quot;cdef public&quot; attributes, i.e. C globals with getters and setters<br>
&gt;&gt;&gt; for Python space. I&#39;m not sure whether this would best be done by<br>
&gt;&gt;&gt; creating a custom dict or module subclass, but it would probably be<br>
&gt;&gt;&gt; cleaner and afford much more than a 1.6x speedup.<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt; - Robert<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt; On Wed, Jan 18, 2012 at 12:30 PM, Vitja Makarov &lt;<a href="mailto:vitja.makarov@gmail.com">vitja.makarov@gmail.com</a>&gt; wrote:<br>
&gt;&gt;&gt;&gt; I tried to optimize module lookups (__pyx_m) by caching internal PyDict state.<br>
&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt; In this example bar() is 1.6 time faster (500us against 842us):<br>
&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt; C = 123<br>
&gt;&gt;&gt;&gt; def foo(a):<br>
&gt;&gt;&gt;&gt;     return C * adef bar():<br>
&gt;&gt;&gt;&gt;     for i in range(10000):        foo(i)<br>
&gt;&gt;&gt;&gt; Here is proof of<br>
&gt;&gt;&gt;&gt; concept:<a href="https://github.com/vitek/cython/commit/1d134fe54a74e6fc6d39d09973db499680b2a8d9" target="_blank">https://github.com/vitek/cython/commit/1d134fe54a74e6fc6d39d09973db499680b2a8d9</a><br>
&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt; So the question is: does it worth it?<br>
&gt;&gt;&gt;&gt;<br>
&gt;&gt;<br>
&gt;&gt; Yes, nice idea.<br>
&gt;&gt; It&#39;s possible to subclass PyModuleObject and I didn&#39;t find any use of<br>
&gt;&gt; PyModule_CheckExact() in CPython&#39;s sources:<br>
&gt;&gt;<br>
&gt;&gt; import types<br>
&gt;&gt; import sys<br>
&gt;&gt;<br>
&gt;&gt; global_foo = 1234<br>
&gt;&gt;<br>
&gt;&gt; class CustomModule(types.ModuleType):<br>
&gt;&gt;    def __init__(self, name):<br>
&gt;&gt;        types.ModuleType.__init__(self, name)<br>
&gt;&gt;        sys.modules[name] = self<br>
&gt;&gt;<br>
&gt;&gt;    @property<br>
&gt;&gt;    def foo(self):<br>
&gt;&gt;        return global_foo<br>
&gt;&gt;<br>
&gt;&gt;    @foo.setter<br>
&gt;&gt;    def foo(self, value):<br>
&gt;&gt;        global global_foo<br>
&gt;&gt;        global_foo = value<br>
&gt;&gt;<br>
&gt;&gt; CustomModule(&#39;foo&#39;)<br>
&gt;&gt;<br>
&gt;&gt; import foo<br>
&gt;&gt; print foo.foo<br>
&gt;&gt;<br>
&gt;<br>
&gt; But this seems to break globals().<br>
<br>
How so? We have to hack globals() to get it to work for a Cython<br>
module anyways. (I wonder if this must return a dict, or would any<br>
mapping (or subclass of dict) be sufficient...)<br>
<br>
- Robert<br>
_______________________________________________<br>
cython-devel mailing list<br>
<a href="mailto:cython-devel@python.org">cython-devel@python.org</a><br>
<a href="http://mail.python.org/mailman/listinfo/cython-devel" target="_blank">http://mail.python.org/mailman/listinfo/cython-devel</a><br>
</blockquote></div>