<p>If it doesn't pass PyDict_CheckExact you won't be able to use it as the globals to eval or exec. That'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, "Robert Bradshaw" <<a href="mailto:robertwb@math.washington.edu">robertwb@math.washington.edu</a>> 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 <<a href="mailto:vitja.makarov@gmail.com">vitja.makarov@gmail.com</a>> wrote:<br>
> 2012/1/19 Vitja Makarov <<a href="mailto:vitja.makarov@gmail.com">vitja.makarov@gmail.com</a>>:<br>
>> 2012/1/19 Robert Bradshaw <<a href="mailto:robertwb@math.washington.edu">robertwb@math.washington.edu</a>>:<br>
>>> I think the right thing to do here is make all module-level globals<br>
>>> into "cdef public" attributes, i.e. C globals with getters and setters<br>
>>> for Python space. I'm not sure whether this would best be done by<br>
>>> creating a custom dict or module subclass, but it would probably be<br>
>>> cleaner and afford much more than a 1.6x speedup.<br>
>>><br>
>>> - Robert<br>
>>><br>
>>> On Wed, Jan 18, 2012 at 12:30 PM, Vitja Makarov <<a href="mailto:vitja.makarov@gmail.com">vitja.makarov@gmail.com</a>> wrote:<br>
>>>> I tried to optimize module lookups (__pyx_m) by caching internal PyDict state.<br>
>>>><br>
>>>> In this example bar() is 1.6 time faster (500us against 842us):<br>
>>>><br>
>>>> C = 123<br>
>>>> def foo(a):<br>
>>>> return C * adef bar():<br>
>>>> for i in range(10000): foo(i)<br>
>>>> Here is proof of<br>
>>>> concept:<a href="https://github.com/vitek/cython/commit/1d134fe54a74e6fc6d39d09973db499680b2a8d9" target="_blank">https://github.com/vitek/cython/commit/1d134fe54a74e6fc6d39d09973db499680b2a8d9</a><br>
>>>><br>
>>>> So the question is: does it worth it?<br>
>>>><br>
>><br>
>> Yes, nice idea.<br>
>> It's possible to subclass PyModuleObject and I didn't find any use of<br>
>> PyModule_CheckExact() in CPython's sources:<br>
>><br>
>> import types<br>
>> import sys<br>
>><br>
>> global_foo = 1234<br>
>><br>
>> class CustomModule(types.ModuleType):<br>
>> def __init__(self, name):<br>
>> types.ModuleType.__init__(self, name)<br>
>> sys.modules[name] = self<br>
>><br>
>> @property<br>
>> def foo(self):<br>
>> return global_foo<br>
>><br>
>> @foo.setter<br>
>> def foo(self, value):<br>
>> global global_foo<br>
>> global_foo = value<br>
>><br>
>> CustomModule('foo')<br>
>><br>
>> import foo<br>
>> print foo.foo<br>
>><br>
><br>
> 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>