Skip to content

Commit

Permalink
Make skip_filter work for built-in functions (#69)
Browse files Browse the repository at this point in the history
  • Loading branch information
shalabhc authored and awbraunstein committed Jan 19, 2017
1 parent 4e7b95d commit 4e974e9
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
11 changes: 11 additions & 0 deletions spitfire/runtime/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

# a few helpful filter functions

import functools
import types
from spitfire import runtime
from spitfire.runtime import udn

Expand All @@ -14,6 +16,15 @@
# create data that could be double-escaped and you don't wnat to constantly
# inform spitfire to us raw mode.
def skip_filter(function):
if isinstance(function, types.BuiltinFunctionType):
# Built-in functions don't allow abitrary attributes, so we use a
# wrapper function that just passes through
@functools.wraps(function)
def skip_filter_wrapper(*args, **kwargs):
return function(*args, **kwargs)
skip_filter_wrapper.skip_filter = True
return skip_filter_wrapper

function.skip_filter = True
return function

Expand Down
16 changes: 12 additions & 4 deletions spitfire/runtime/template_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
# Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file.

import sys
import unittest

from spitfire.runtime import filters
from spitfire.runtime import template
from spitfire.runtime import baked
try:
Expand All @@ -13,17 +15,14 @@
_template = None


@filters.skip_filter
def is_skip():
pass


is_skip.skip_filter = True


def no_skip():
pass


# Do not inherit from unittest.TestCase to ensure that these tests don't run.
# Add tests here and they will be run for the C and Python implementations. This
# should make sure that both implementations are equivalent.
Expand All @@ -38,6 +37,15 @@ def setUp(self):
def test_skip_filter(self):
self.assertEqual(self.template.filter_function('foo', is_skip), 'foo')

def test_skip_filter_builtin(self):
# Ensure skip_filter works for built-in functions as well,
# choice of getdefaultencoding is abitrary - it's a built-in function
is_skip_builtin = filters.skip_filter(sys.getdefaultencoding)
self.assertEqual(self.template.filter_function('foo', is_skip_builtin),
'foo')
# Ensure wrapped function works as expected
self.assertEqual(is_skip_builtin(), sys.getdefaultencoding())

def test_no_skip(self):
self.assertEqual(
self.template.filter_function('foo', no_skip), 'FILTERED')
Expand Down

0 comments on commit 4e974e9

Please sign in to comment.