[Zope-CMF] Re: monkey patching CMFCore.utils isn't working?!?

Tres Seaver tseaver at palladion.com
Fri Jun 2 22:01:28 EDT 2006


Florent Guillaume wrote:
> 
> 
> Tres Seaver wrote:
>> Matt Hahnfeld wrote:
>>> I am using Plone 2.1.2, w/ CMF 1.5.5.  I'm having trouble using
>>> the overridden _checkPermission function in 1.5.5 to handle proxy 
>>> roles on
>>> a FSPythonScript.  After hacking around, I've determined the best method
>>> to repair this issue is to "backport" CMF 1.6.0's function and make
>>> _checkPermission call SecurityManager's _checkPermission.  I don't 
>>> really
>>> want to change the core libraries, so my idea was to do it as a monkey
>>> patch.
>>>
>>> So, I added this monkey patch to one of my custom zope products:
>>>
>>> -----
>>> from AccessControl import getSecurityManager
>>>
>>> def _checkPermission(permission, obj):
>>>     return getSecurityManager().checkPermission(permission, obj)
>>>
>>> import Products.CMFCore.utils
>>> Products.CMFCore.utils._checkPermission = _checkPermission
>>> -----
>>>
>>> But every time I use _checkPermission from Products.CMFCore.utils I'm
>>> still getting the old one...  Is it possible to monkey patch 
>>> functions (as
>>> opposed to methods)?  Any idea why this monkey patch wouldn't be 
>>> working?
>>
>> Other modules may have already imported it, using:
>>
>>   from Products.CMFCore.utils import _checkPermission
>>
>> Those modules *also* need monkey patching, because they are holding 
>> onto a reference to the original object, even though you have bound a 
>> new reference to that name with your monkey.
> 
> It might be possible to monkey patch only _checkPermission.func_code, 
> which seems to work on simple tests, but I've never tried it in real life:
> 
>  >>> def f(a):
> ...     print a+1
> 
>  >>> def g(b):
> ...     print b+2
> 
>  >>> f(0)
> 1
>  >>> f.func_code = g.func_code
>  >>> f(0)
> 2

Heh, I thought about that, and then decided that tha voodoo factor was 
too high to suggest.


Tres.
-- 
===================================================================
Tres Seaver          +1 202-558-7113          tseaver at palladion.com
Palladion Software   "Excellence by Design"    http://palladion.com


More information about the Zope-CMF mailing list