Revision fde0203b
b/lib/utils.py | ||
---|---|---|
55 | 55 |
import sha |
56 | 56 |
sha1 = sha.new |
57 | 57 |
|
58 |
try: |
|
59 |
import functools |
|
60 |
except ImportError: |
|
61 |
functools = None |
|
62 |
|
|
58 | 63 |
from ganeti import errors |
59 | 64 |
from ganeti import constants |
60 | 65 |
|
... | ... | |
1501 | 1506 |
return False |
1502 | 1507 |
|
1503 | 1508 |
|
1509 |
# Even though we're using Python's built-in "partial" function if available, |
|
1510 |
# this one is always defined for testing. |
|
1511 |
def _partial(func, *args, **keywords): # pylint: disable-msg=W0622 |
|
1512 |
"""Decorator with partial application of arguments and keywords. |
|
1513 |
|
|
1514 |
This function was copied from Python's documentation. |
|
1515 |
|
|
1516 |
""" |
|
1517 |
def newfunc(*fargs, **fkeywords): |
|
1518 |
newkeywords = keywords.copy() |
|
1519 |
newkeywords.update(fkeywords) |
|
1520 |
return func(*(args + fargs), **newkeywords) # pylint: disable-msg=W0142 |
|
1521 |
|
|
1522 |
newfunc.func = func |
|
1523 |
newfunc.args = args |
|
1524 |
newfunc.keywords = keywords |
|
1525 |
return newfunc |
|
1526 |
|
|
1527 |
|
|
1528 |
if functools is None: |
|
1529 |
partial = _partial |
|
1530 |
else: |
|
1531 |
partial = functools.partial |
|
1532 |
|
|
1533 |
|
|
1504 | 1534 |
def SingleWaitForFdCondition(fdobj, event, timeout): |
1505 | 1535 |
"""Waits for a condition to occur on the socket. |
1506 | 1536 |
|
b/test/ganeti.utils_unittest.py | ||
---|---|---|
1592 | 1592 |
"", "x"]) |
1593 | 1593 |
|
1594 | 1594 |
|
1595 |
class TestPartial(testutils.GanetiTestCase): |
|
1596 |
def test(self): |
|
1597 |
self._Test(utils.partial) |
|
1598 |
self._Test(utils._partial) |
|
1599 |
|
|
1600 |
def _Test(self, fn): |
|
1601 |
def _TestFunc1(x, power=2): |
|
1602 |
return x ** power |
|
1603 |
|
|
1604 |
cubic = fn(_TestFunc1, power=3) |
|
1605 |
self.assertEqual(cubic(1), 1) |
|
1606 |
self.assertEqual(cubic(3), 27) |
|
1607 |
self.assertEqual(cubic(4), 64) |
|
1608 |
|
|
1609 |
def _TestFunc2(*args, **kwargs): |
|
1610 |
return (args, kwargs) |
|
1611 |
|
|
1612 |
self.assertEqualValues(fn(_TestFunc2, "Hello", "World")("Foo"), |
|
1613 |
(("Hello", "World", "Foo"), {})) |
|
1614 |
|
|
1615 |
self.assertEqualValues(fn(_TestFunc2, "Hello", xyz=123)("Foo"), |
|
1616 |
(("Hello", "Foo"), {"xyz": 123})) |
|
1617 |
|
|
1618 |
self.assertEqualValues(fn(_TestFunc2, xyz=123)("Foo", xyz=999), |
|
1619 |
(("Foo", ), {"xyz": 999,})) |
|
1620 |
|
|
1621 |
|
|
1595 | 1622 |
if __name__ == '__main__': |
1596 | 1623 |
testutils.GanetiTestProgram() |
Also available in: Unified diff