diff -r aeb3faaf4754 Lib/tempfile.py --- a/Lib/tempfile.py Mon Sep 02 17:01:10 2013 -0700 +++ b/Lib/tempfile.py Thu Sep 05 09:36:05 2013 +0100 @@ -27,6 +27,7 @@ # Imports. +import functools as _functools import warnings as _warnings import sys as _sys import io as _io @@ -344,7 +345,16 @@ a = getattr(file, name) if not isinstance(a, int): setattr(self, name, a) - return a + + if hasattr(a, '__call__'): + @_functools.wraps(a) + def func_wrapper(*args, **kwargs): + # This is to prevent self getting out of scope too early, see bug 18879 + local_self_copy = self + return a(*args, **kwargs) + + return func_wrapper + else: + return a # The underlying __enter__ method returns the wrong object # (self.file) so override it to return the wrapper diff -r aeb3faaf4754 Lib/test/test_tempfile.py --- a/Lib/test/test_tempfile.py Mon Sep 02 17:01:10 2013 -0700 +++ b/Lib/test/test_tempfile.py Thu Sep 05 09:36:05 2013 +0100 @@ -610,6 +610,9 @@ self.do_create(pre="a", suf="b") self.do_create(pre="aa", suf=".txt") + def test_regression_18879(self): + self.do_create().write(b"xxx") + def test_creates_named(self): # NamedTemporaryFile creates files with names f = tempfile.NamedTemporaryFile()