X Tutup
Skip to content

Commit c0d8973

Browse files
committed
IN PROGRESS - issue SESPRINGPYTHONPY-34: AOP: AOP doesn't seem able to proxy __str__ methods
http://jira.springframework.org/browse/SESPRINGPYTHONPY-34 Merged r46:47 from sandbox/gregturnquist/se-springpython-py-34 into trunk. Ran 230 tests with 100% success. git-svn-id: https://src.springframework.org/svn/se-springpython-py/trunk/springpython@48 ce8fead1-4192-4296-8608-a705134b927f
1 parent d928ad8 commit c0d8973

File tree

3 files changed

+28
-4
lines changed

3 files changed

+28
-4
lines changed

src/springpython/aop/__init__.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ def dispatch(self, *args, **kwargs):
210210
target object. This allow this object to serve as a proxying agent for the target object."""
211211
self.logger.debug("Calling AopProxy.%s(%s)" % (self.methodName, args))
212212
invocation = MethodInvocation(self.target, self.methodName, args, kwargs, self.interceptors)
213-
return getattr(invocation, self.methodName)(*args, **kwargs)
213+
return invocation.__getattr__(self.methodName)(*args, **kwargs)
214214

215215
def __getattr__(self, name):
216216
"""If any of the parameters are local objects, they are immediately retrieved. Callables cause the dispatch method
@@ -224,7 +224,7 @@ def __getattr__(self, name):
224224
return attr
225225
self.methodName = name
226226
return self.dispatch
227-
227+
228228
class ProxyFactory(object):
229229
"""This object helps to build AopProxy objects programmatically. It allows configuring advice and target objects.
230230
Then it will produce an AopProxy when needed. To use similar behavior in an IoC environment, see ProxyFactoryComponent."""
@@ -257,8 +257,11 @@ class ProxyFactoryComponent(ProxyFactory, AopProxy):
257257
"""This class acts as both a ProxyFactory to build and an AopProxy. It makes itself look like the target object.
258258
Any changes to the target and list of interceptors is immediately seen when using this as a proxy."""
259259
def __init__(self, target = None, interceptors = None):
260-
self.logger = logging.getLogger("springpython.aop.ProxyFactoryComponent")
261260
ProxyFactory.__init__(self, target, interceptors)
261+
self.logger = logging.getLogger("springpython.aop.ProxyFactoryComponent")
262+
263+
def __str__(self):
264+
return self.__getattr__("__str__")()
262265

263266
class PerformanceMonitorInterceptor(MethodInterceptor):
264267
def __init__(self, prefix = None, level = logging.DEBUG):

test/springpythontest/aopTestCases.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
See the License for the specific language governing permissions and
1414
limitations under the License.
1515
"""
16+
import logging
1617
import unittest
1718
from springpython.aop import MethodInterceptor
1819
from springpython.aop import MethodMatcher
@@ -53,14 +54,23 @@ def testCreatingAProxyFactoryAndAddingAnInterceptorProgrammatically(self):
5354
self.assertEquals("<Wrapped>Alright!</Wrapped>", service.doSomething())
5455
self.assertEquals("<Wrapped>You made it!</Wrapped>", service.method("test"))
5556
self.assertEquals("sample", service.attribute)
56-
57+
5758
def testCreatingAProxyFactoryAndAddingAnInterceptorIoC(self):
5859
factory = self.appContext.getComponent("factory")
5960
service = factory.getProxy()
6061
self.assertEquals("<Wrapped>Alright!</Wrapped>", service.doSomething())
6162
self.assertEquals("<Wrapped>You made it!</Wrapped>", service.method("test"))
6263
self.assertEquals("sample", service.attribute)
6364

65+
def testWrappingStringFunctionWithInterceptor(self):
66+
service = ProxyFactoryComponent()
67+
service.target = SampleService()
68+
service.interceptors = [WrappingInterceptor()]
69+
self.assertEquals("This is a sample service.", service.target.__str__())
70+
self.assertEquals("This is a sample service.", str(service.target))
71+
self.assertEquals("<Wrapped>This is a sample service.</Wrapped>", str(service))
72+
self.assertEquals("<Wrapped>This is a sample service.</Wrapped>", service.__str__())
73+
6474
def testCreatingAProxyFactoryComponentAndAddingAnInterceptorProgrammatically(self):
6575
service = ProxyFactoryComponent()
6676
service.target = SampleService()
@@ -141,4 +151,13 @@ def testWrappingPyroProxyOnClientSideIoC(self):
141151
# self.appContext.dispose()
142152

143153
if __name__ == "__main__":
154+
logger = logging.getLogger("springpython")
155+
loggingLevel = logging.INFO
156+
logger.setLevel(loggingLevel)
157+
ch = logging.StreamHandler()
158+
ch.setLevel(loggingLevel)
159+
formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
160+
ch.setFormatter(formatter)
161+
logger.addHandler(ch)
162+
144163
unittest.main()

test/springpythontest/support/testSupportClasses.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,8 @@ def method(self, data):
142142
return "You made it!"
143143
def doSomething(self):
144144
return "Alright!"
145+
def __str__(self):
146+
return "This is a sample service."
145147

146148
class RemoteService1(object):
147149
def getData(self, param):

0 commit comments

Comments
 (0)
X Tutup