150 likes | 337 Views
AspectJ-style advice for Python functions. Zhenkuang He Tolga Cerrah. Overview. Motivation/Goals Advised functions/Pointcuts Advice Exceptions Demo Future Work. Motivation/Goals. To use the functionality of the decorators to make Python look more like AspectJ.
E N D
AspectJ-style advice for Python functions Zhenkuang He Tolga Cerrah
Overview • Motivation/Goals • Advised functions/Pointcuts • Advice • Exceptions • Demo • Future Work
Motivation/Goals To use the functionality of the decoratorsto make Python look more like AspectJ. The main goal is to come up with a way to implement pointcuts and advices in Python to make it similar to AspectJ.
Advised functions • @Aspect() • Setup the function path dictionary • pathDict{} # Function's name : Actual function • @Aspect( “PointCut_Name” ) • If there is given a pointcut name, setup the point cut dictionary • pointcutDict{} # PointCut name : Functions in PointCut
Pointcuts • @Pointcut(“..”,”..”, ...) • Parameters can be function names • Parameters can be PointCut names @Pointcut(“a”,”b”) @Pointcut(“pc1”,”Dec”,”a”)
Pointcuts • If there is a pointcut, get all the advised functions of that pointcut • If not, just get the advised functions • Initialize/Update the pointcut dictionary
Advice • @After( “..”,”..”, … ) • AfterDict{} • @Before( “..”,”..”, … ) • BeforeDict{} • @Around( “..” ) • AroundDict{} • __call__ @Aspect • Proceed()
@After • AfterDict{} ( BeforeDict{}, AroundDict{} ) • Key: Function to be advised • Value: set of advised functions • If there is a pointcut, get all the advised functions of that pointcut. • If not, just get the advised functions
@After • Setup the function path dictionary of advice function • Update the "After" advice dictionary • Every step is the same for @Before and @Around except this step
__call__ @Aspect • Check the "Before" advice, • If we do have, run those before the function • Check the "Around" advice • If we do have, run it instead the function • Store the advised function and its arguments • If we do not have, just run the original function • Check the "After" advice • If we do have, run those after the function
Proceed() • If no argument is given, run the advised function with its own arguments • If not, run it with the given arguments
Exceptions • Advice Error • raised when the function decorated with @Before, @After or @Around doesn’t advice any other function • Aspect Error • raised when a function being advised hasn’t beenannotated with @Aspect or defined
Demo • Test case 1, 2, 3, 4, 5
Future Work Functions themselves can be passed as parameters instead of just their name string. More exceptions can be covered such as one that checks if Proceed() is called inside a function decorated with @Around.