Back to index

python3.2  3.2.2
Public Member Functions
test.test_dynamic.RebindBuiltinsTests Class Reference

List of all members.

Public Member Functions

def configure_func
def test_globals_shadow_builtins
def test_modify_builtins
def test_modify_builtins_while_generator_active
def test_modify_builtins_from_leaf_function
def test_cannot_change_globals_or_builtins_with_eval
def test_cannot_change_globals_or_builtins_with_exec
def test_cannot_replace_builtins_dict_while_active
def test_cannot_replace_builtins_dict_between_calls
def test_eval_gives_lambda_custom_globals

Detailed Description

Test all the ways that we can change/shadow globals/builtins.

Definition at line 10 of file test_dynamic.py.


Member Function Documentation

def test.test_dynamic.RebindBuiltinsTests.configure_func (   self,
  func,
  args 
)
Perform TestCase-specific configuration on a function before testing.

By default, this does nothing. Example usage: spinning a function so
that a JIT will optimize it. Subclasses should override this as needed.

Args:
    func: function to configure.
    *args: any arguments that should be passed to func, if calling it.

Returns:
    Nothing. Work will be performed on func in-place.

Definition at line 14 of file test_dynamic.py.

00014 
00015     def configure_func(self, func, *args):
00016         """Perform TestCase-specific configuration on a function before testing.
00017 
00018         By default, this does nothing. Example usage: spinning a function so
00019         that a JIT will optimize it. Subclasses should override this as needed.
00020 
00021         Args:
00022             func: function to configure.
00023             *args: any arguments that should be passed to func, if calling it.
00024 
00025         Returns:
00026             Nothing. Work will be performed on func in-place.
00027         """
00028         pass

Here is the caller graph for this function:

Definition at line 79 of file test_dynamic.py.

00079 
00080     def test_cannot_change_globals_or_builtins_with_eval(self):
00081         def foo():
00082             return len([1, 2, 3])
00083         self.configure_func(foo)
00084 
00085         # Note that this *doesn't* change the definition of len() seen by foo().
00086         builtins_dict = {"len": lambda x: 7}
00087         globals_dict = {"foo": foo, "__builtins__": builtins_dict,
00088                         "len": lambda x: 8}
00089         self.assertEqual(eval("foo()", globals_dict), 3)
00090 
00091         self.assertEqual(eval("foo()", {"foo": foo}), 3)

Here is the call graph for this function:

Definition at line 92 of file test_dynamic.py.

00092 
00093     def test_cannot_change_globals_or_builtins_with_exec(self):
00094         def foo():
00095             return len([1, 2, 3])
00096         self.configure_func(foo)
00097 
00098         globals_dict = {"foo": foo}
00099         exec("x = foo()", globals_dict)
00100         self.assertEqual(globals_dict["x"], 3)
00101 
00102         # Note that this *doesn't* change the definition of len() seen by foo().
00103         builtins_dict = {"len": lambda x: 7}
00104         globals_dict = {"foo": foo, "__builtins__": builtins_dict,
00105                         "len": lambda x: 8}
00106 
00107         exec("x = foo()", globals_dict)
00108         self.assertEqual(globals_dict["x"], 3)

Here is the call graph for this function:

Definition at line 121 of file test_dynamic.py.

00121 
00122     def test_cannot_replace_builtins_dict_between_calls(self):
00123         def foo():
00124             return len([1, 2, 3])
00125         self.configure_func(foo)
00126 
00127         self.assertEqual(foo(), 3)
00128         with swap_item(globals(), "__builtins__", {"len": lambda x: 7}):
00129             self.assertEqual(foo(), 3)

Here is the call graph for this function:

Definition at line 109 of file test_dynamic.py.

00109 
00110     def test_cannot_replace_builtins_dict_while_active(self):
00111         def foo():
00112             x = range(3)
00113             yield len(x)
00114             yield len(x)
00115         self.configure_func(foo)
00116 
00117         g = foo()
00118         self.assertEqual(next(g), 3)
00119         with swap_item(globals(), "__builtins__", {"len": lambda x: 7}):
00120             self.assertEqual(next(g), 3)

Here is the call graph for this function:

Definition at line 130 of file test_dynamic.py.

00130 
00131     def test_eval_gives_lambda_custom_globals(self):
00132         globals_dict = {"len": lambda x: 7}
00133         foo = eval("lambda: len([])", globals_dict)
00134         self.configure_func(foo)
00135 
00136         self.assertEqual(foo(), 7)
00137 

Here is the call graph for this function:

Definition at line 29 of file test_dynamic.py.

00029 
00030     def test_globals_shadow_builtins(self):
00031         # Modify globals() to shadow an entry in builtins.
00032         def foo():
00033             return len([1, 2, 3])
00034         self.configure_func(foo)
00035 
00036         self.assertEqual(foo(), 3)
00037         with swap_item(globals(), "len", lambda x: 7):
00038             self.assertEqual(foo(), 7)

Here is the call graph for this function:

Definition at line 39 of file test_dynamic.py.

00039 
00040     def test_modify_builtins(self):
00041         # Modify the builtins module directly.
00042         def foo():
00043             return len([1, 2, 3])
00044         self.configure_func(foo)
00045 
00046         self.assertEqual(foo(), 3)
00047         with swap_attr(builtins, "len", lambda x: 7):
00048             self.assertEqual(foo(), 7)

Here is the call graph for this function:

Definition at line 62 of file test_dynamic.py.

00062 
00063     def test_modify_builtins_from_leaf_function(self):
00064         # Verify that modifications made by leaf functions percolate up the
00065         # callstack.
00066         with swap_attr(builtins, "len", len):
00067             def bar():
00068                 builtins.len = lambda x: 4
00069 
00070             def foo(modifier):
00071                 l = []
00072                 l.append(len(range(7)))
00073                 modifier()
00074                 l.append(len(range(7)))
00075                 return l
00076             self.configure_func(foo, lambda: None)
00077 
00078             self.assertEqual(foo(bar), [7, 4])

Here is the call graph for this function:

Definition at line 49 of file test_dynamic.py.

00049 
00050     def test_modify_builtins_while_generator_active(self):
00051         # Modify the builtins out from under a live generator.
00052         def foo():
00053             x = range(3)
00054             yield len(x)
00055             yield len(x)
00056         self.configure_func(foo)
00057 
00058         g = foo()
00059         self.assertEqual(next(g), 3)
00060         with swap_attr(builtins, "len", lambda x: 7):
00061             self.assertEqual(next(g), 7)

Here is the call graph for this function:


The documentation for this class was generated from the following file: