Drop use of functools.wrap in logging_pool

functools.wrap is only warranted to work with functions and methods but
logging_pool is warranted to work with callable behaviors, so using
functools.wrap has been wrong all along.

The particular incompatibility motivating this correction is that
callable objects do not have a "__name__" attribute.
pull/4923/head
Nathaniel Manista 9 years ago
parent a847f51c94
commit 8fff90d3a9
  1. 7
      src/python/grpcio/grpc/framework/foundation/logging_pool.py
  2. 26
      src/python/grpcio/tests/unit/framework/foundation/_logging_pool_test.py

@ -1,4 +1,4 @@
# Copyright 2015, Google Inc.
# Copyright 2015-2016, Google Inc.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
@ -29,7 +29,6 @@
"""A thread pool that logs exceptions raised by tasks executed within it."""
import functools
import logging
from concurrent import futures
@ -37,12 +36,12 @@ from concurrent import futures
def _wrap(behavior):
"""Wraps an arbitrary callable behavior in exception-logging."""
@functools.wraps(behavior)
def _wrapping(*args, **kwargs):
try:
return behavior(*args, **kwargs)
except Exception as e:
logging.exception('Unexpected exception from task run in logging pool!')
logging.exception(
'Unexpected exception from %s executed in logging pool!', behavior)
raise
return _wrapping

@ -1,4 +1,4 @@
# Copyright 2015, Google Inc.
# Copyright 2015-2016, Google Inc.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
@ -29,6 +29,7 @@
"""Tests for grpc.framework.foundation.logging_pool."""
import threading
import unittest
from grpc.framework.foundation import logging_pool
@ -36,6 +37,21 @@ from grpc.framework.foundation import logging_pool
_POOL_SIZE = 16
class _CallableObject(object):
def __init__(self):
self._lock = threading.Lock()
self._passed_values = []
def __call__(self, value):
with self._lock:
self._passed_values.append(value)
def passed_values(self):
with self._lock:
return tuple(self._passed_values)
class LoggingPoolTest(unittest.TestCase):
def testUpAndDown(self):
@ -59,6 +75,14 @@ class LoggingPoolTest(unittest.TestCase):
self.assertIsNotNone(raised_exception)
def testCallableObjectExecuted(self):
callable_object = _CallableObject()
passed_object = object()
with logging_pool.pool(_POOL_SIZE) as pool:
future = pool.submit(callable_object, passed_object)
self.assertIsNone(future.result())
self.assertSequenceEqual((passed_object,), callable_object.passed_values())
if __name__ == '__main__':
unittest.main(verbosity=2)

Loading…
Cancel
Save