Ignore:
Timestamp:
Mar 19, 2014, 11:31:01 PM (11 years ago)
Author:
dmik
Message:

python: Merge vendor 2.7.6 to trunk.

Location:
python/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • python/trunk

  • python/trunk/Lib/test/test_funcattrs.py

    r2 r391  
    1414        self.b = b
    1515
    16     def cannot_set_attr(self,obj, name, value, exceptions):
    17         # This method is not called as a test (name doesn't start with 'test'),
    18         # but may be used by other tests.
    19         try: setattr(obj, name, value)
    20         except exceptions: pass
    21         else: self.fail("shouldn't be able to set %s to %r" % (name, value))
    22         try: delattr(obj, name)
    23         except exceptions: pass
    24         else: self.fail("shouldn't be able to del %s" % name)
     16    def cannot_set_attr(self, obj, name, value, exceptions):
     17        # Helper method for other tests.
     18        try:
     19            setattr(obj, name, value)
     20        except exceptions:
     21            pass
     22        else:
     23            self.fail("shouldn't be able to set %s to %r" % (name, value))
     24        try:
     25            delattr(obj, name)
     26        except exceptions:
     27            pass
     28        else:
     29            self.fail("shouldn't be able to del %s" % name)
    2530
    2631
     
    3237    def test_dir_includes_correct_attrs(self):
    3338        self.b.known_attr = 7
    34         self.assert_('known_attr' in dir(self.b),
    35             "set attributes not in dir listing of method")
     39        self.assertIn('known_attr', dir(self.b),
     40                        "set attributes not in dir listing of method")
    3641        # Test on underlying function object of method
    3742        self.f.a.im_func.known_attr = 7
    38         self.assert_('known_attr' in dir(self.f.a),
    39             "set attribute on unbound method implementation in class not in "
    40                      "dir")
    41         self.assert_('known_attr' in dir(self.fi.a),
    42             "set attribute on unbound method implementations, should show up"
    43                      " in next dir")
     43        self.assertIn('known_attr', dir(self.f.a),
     44                        "set attribute on unbound method implementation in "
     45                        "class not in dir")
     46        self.assertIn('known_attr', dir(self.fi.a),
     47                        "set attribute on unbound method implementations, "
     48                        "should show up in next dir")
    4449
    4550    def test_duplicate_function_equality(self):
     
    5762
    5863    def test_func_globals(self):
    59         self.assertEqual(self.b.func_globals, globals())
     64        self.assertIs(self.b.func_globals, globals())
    6065        self.cannot_set_attr(self.b, 'func_globals', 2, TypeError)
     66
     67    def test_func_closure(self):
     68        a = 12
     69        def f(): print a
     70        c = f.func_closure
     71        self.assertIsInstance(c, tuple)
     72        self.assertEqual(len(c), 1)
     73        # don't have a type object handy
     74        self.assertEqual(c[0].__class__.__name__, "cell")
     75        self.cannot_set_attr(f, "func_closure", c, TypeError)
     76
     77    def test_empty_cell(self):
     78        def f(): print a
     79        try:
     80            f.func_closure[0].cell_contents
     81        except ValueError:
     82            pass
     83        else:
     84            self.fail("shouldn't be able to read an empty cell")
     85        a = 12
    6186
    6287    def test_func_name(self):
     
    97122        self.assertEqual(c(), 7)
    98123        # self.assertEqual(d(), 7)
    99         try: b.func_code = c.func_code
    100         except ValueError: pass
    101         else: self.fail(
    102             "func_code with different numbers of free vars should not be "
    103             "possible")
    104         try: e.func_code = d.func_code
    105         except ValueError: pass
    106         else: self.fail(
    107             "func_code with different numbers of free vars should not be "
    108             "possible")
     124        try:
     125            b.func_code = c.func_code
     126        except ValueError:
     127            pass
     128        else:
     129            self.fail("func_code with different numbers of free vars should "
     130                      "not be possible")
     131        try:
     132            e.func_code = d.func_code
     133        except ValueError:
     134            pass
     135        else:
     136            self.fail("func_code with different numbers of free vars should "
     137                      "not be possible")
    109138
    110139    def test_blank_func_defaults(self):
     
    127156        del second_func.func_defaults
    128157        self.assertEqual(second_func.func_defaults, None)
    129         try: second_func()
    130         except TypeError: pass
    131         else: self.fail(
    132             "func_defaults does not update; deleting it does not remove "
    133             "requirement")
    134 
    135 class ImplicitReferencesTest(FuncAttrsTest):
     158        try:
     159            second_func()
     160        except TypeError:
     161            pass
     162        else:
     163            self.fail("func_defaults does not update; deleting it does not "
     164                      "remove requirement")
     165
     166
     167class InstancemethodAttrTest(FuncAttrsTest):
    136168    def test_im_class(self):
    137169        self.assertEqual(self.f.a.im_class, self.f)
     
    160192        self.assertNotEqual(self.fi.id(), id(self.f))
    161193        # Test usage
    162         try: self.f.id.unknown_attr
    163         except AttributeError: pass
    164         else: self.fail("using unknown attributes should raise AttributeError")
     194        try:
     195            self.f.id.unknown_attr
     196        except AttributeError:
     197            pass
     198        else:
     199            self.fail("using unknown attributes should raise AttributeError")
    165200        # Test assignment and deletion
    166201        self.cannot_set_attr(self.f.id, 'unknown_attr', 2, AttributeError)
     
    172207        self.assertEqual(self.fi.a.known_attr, 7)
    173208
     209
    174210class ArbitraryFunctionAttrTest(FuncAttrsTest):
    175211    def test_set_attr(self):
     212        # setting attributes only works on function objects
    176213        self.b.known_attr = 7
    177214        self.assertEqual(self.b.known_attr, 7)
    178215        for func in [self.f.a, self.fi.a]:
    179             try: func.known_attr = 7
    180             except AttributeError: pass
    181             else: self.fail("setting attributes on methods should raise error")
     216            try:
     217                func.known_attr = 7
     218            except AttributeError:
     219                pass
     220            else:
     221                self.fail("setting attributes on methods should raise error")
    182222
    183223    def test_delete_unknown_attr(self):
    184         try: del self.b.unknown_attr
    185         except AttributeError: pass
    186         else: self.fail("deleting unknown attribute should raise TypeError")
     224        try:
     225            del self.b.unknown_attr
     226        except AttributeError:
     227            pass
     228        else:
     229            self.fail("deleting unknown attribute should raise TypeError")
    187230
    188231    def test_setting_attrs_duplicates(self):
    189         try: self.f.a.klass = self.f
    190         except AttributeError: pass
    191         else: self.fail("setting arbitrary attribute in unbound function "
    192                         " should raise AttributeError")
     232        try:
     233            self.f.a.klass = self.f
     234        except AttributeError:
     235            pass
     236        else:
     237            self.fail("setting arbitrary attribute in unbound function "
     238                      " should raise AttributeError")
    193239        self.f.a.im_func.klass = self.f
    194240        for method in [self.f.a, self.fi.a, self.fi.a.im_func]:
     
    197243    def test_unset_attr(self):
    198244        for func in [self.b, self.f.a, self.fi.a]:
    199             try:  func.non_existent_attr
    200             except AttributeError: pass
    201             else: self.fail("using unknown attributes should raise "
    202                             "AttributeError")
     245            try:
     246                func.non_existent_attr
     247            except AttributeError:
     248                pass
     249            else:
     250                self.fail("using unknown attributes should raise "
     251                          "AttributeError")
     252
    203253
    204254class FunctionDictsTest(FuncAttrsTest):
     
    217267        self.f.a.im_func.__dict__ = d
    218268        # Test assignment
    219         self.assertEqual(d, self.b.__dict__)
    220         self.assertEqual(d, self.b.func_dict)
     269        self.assertIs(d, self.b.__dict__)
     270        self.assertIs(d, self.b.func_dict)
    221271        # ... and on all the different ways of referencing the method's func
    222         self.assertEqual(d, self.f.a.im_func.__dict__)
    223         self.assertEqual(d, self.f.a.__dict__)
    224         self.assertEqual(d, self.fi.a.im_func.__dict__)
    225         self.assertEqual(d, self.fi.a.__dict__)
     272        self.assertIs(d, self.f.a.im_func.__dict__)
     273        self.assertIs(d, self.f.a.__dict__)
     274        self.assertIs(d, self.fi.a.im_func.__dict__)
     275        self.assertIs(d, self.fi.a.__dict__)
    226276        # Test value
    227277        self.assertEqual(self.b.known_attr, 7)
     
    235285
    236286    def test_delete_func_dict(self):
    237         try: del self.b.__dict__
    238         except TypeError: pass
    239         else: self.fail("deleting function dictionary should raise TypeError")
    240         try: del self.b.func_dict
    241         except TypeError: pass
    242         else: self.fail("deleting function dictionary should raise TypeError")
     287        try:
     288            del self.b.__dict__
     289        except TypeError:
     290            pass
     291        else:
     292            self.fail("deleting function dictionary should raise TypeError")
     293        try:
     294            del self.b.func_dict
     295        except TypeError:
     296            pass
     297        else:
     298            self.fail("deleting function dictionary should raise TypeError")
    243299
    244300    def test_unassigned_dict(self):
     
    250306        d[self.b] = value
    251307        self.assertEqual(d[self.b], value)
     308
    252309
    253310class FunctionDocstringTest(FuncAttrsTest):
     
    274331        self.assertEqual(self.b.func_doc, None)
    275332
     333
     334class StaticMethodAttrsTest(unittest.TestCase):
     335    def test_func_attribute(self):
     336        def f():
     337            pass
     338
     339        c = classmethod(f)
     340        self.assertTrue(c.__func__ is f)
     341
     342        s = staticmethod(f)
     343        self.assertTrue(s.__func__ is f)
     344
     345
    276346def test_main():
    277     test_support.run_unittest(FunctionPropertiesTest, ImplicitReferencesTest,
     347    test_support.run_unittest(FunctionPropertiesTest, InstancemethodAttrTest,
    278348                              ArbitraryFunctionAttrTest, FunctionDictsTest,
    279                               FunctionDocstringTest)
     349                              FunctionDocstringTest,
     350                              StaticMethodAttrsTest)
    280351
    281352if __name__ == "__main__":
Note: See TracChangeset for help on using the changeset viewer.