From ca8991d7ded684262f162d990a77320670a2e7b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9rome=20Perrin?= <jerome@nexedi.com> Date: Fri, 9 Nov 2007 18:18:14 +0000 Subject: [PATCH] Check that we don't store persistent object in caching methods. The cache will only be enabled in unit tests. It uses pickle to check if the object is persistent or not, I'm too lazy to do this by iterating in gc.get_referents graphs. git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@17504 20353a03-c40f-0410-a6d1-a30d3c3de9de --- product/ERP5Type/tests/ERP5TypeTestCase.py | 18 +++++++++++++++++- product/ERP5Type/tests/testCacheTool.py | 17 +++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/product/ERP5Type/tests/ERP5TypeTestCase.py b/product/ERP5Type/tests/ERP5TypeTestCase.py index 40e4def05e..e9628e3d89 100644 --- a/product/ERP5Type/tests/ERP5TypeTestCase.py +++ b/product/ERP5Type/tests/ERP5TypeTestCase.py @@ -779,5 +779,21 @@ def optimize(): from Products.CMFCore.FSPythonScript import FSPythonScript FSPythonScript._compile = PythonScript_compile - optimize() + + +def fortify(): + '''Add some extra checks that we don't have at runtime, not to slow down the + system. + ''' + # check that we don't store persistent objects in cache + from pickle import dumps + from Products.ERP5Type.CachePlugins.BaseCache import CacheEntry + CacheEntry__init__ = CacheEntry.__init__ + def __init__(self, value, *args, **kw): + # this will raise TypeError if you try to cache a persistent object + dumps(value) + CacheEntry__init__(self, value, *args, **kw) + CacheEntry.__init__ = __init__ + +fortify() diff --git a/product/ERP5Type/tests/testCacheTool.py b/product/ERP5Type/tests/testCacheTool.py index 12caa41cd1..4e2ecd56a7 100644 --- a/product/ERP5Type/tests/testCacheTool.py +++ b/product/ERP5Type/tests/testCacheTool.py @@ -277,6 +277,23 @@ return result ## Cache cleared shouldn't be previously cached self.assert_(1.0 < calculation_time) + def test_CachePersistentObjects(self): + # storing persistent objects in cache is not allowed, but this check is + # only performed in unit tests. + from Products.ERP5Type.Cache import CachingMethod + def func(): + # return a persistent object + return self.portal + cached_func = CachingMethod(func, 'cache_persistent_obj') + self.assertRaises(TypeError, cached_func) + + def func(): + # return a method bound on a persistent object + return self.portal.getTitle + cached_func = CachingMethod(func, 'cache_bound_method') + self.assertRaises(TypeError, cached_func) + + def test_suite(): suite = unittest.TestSuite() suite.addTest(unittest.makeSuite(TestCacheTool)) -- 2.30.9