Skip to content

Commit b2c84dc

Browse files
committed
Patch refactored GeneratorContainer to support Python2
The python2 inspect module uses `getcallargs()` instead of `signature()` and `bind()`. Update the refactored GeneratorContainer class to conditionally use the version specific method.
1 parent bc98eb6 commit b2c84dc

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

ciscosparkapi/generator_containers.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
unicode_literals,
2020
)
2121
from builtins import *
22-
from past.builtins import basestring
2322

2423

2524
__author__ = "Chris Lunsford"
@@ -31,6 +30,7 @@
3130
import functools
3231
import inspect
3332
from itertools import islice
33+
import sys
3434

3535

3636
class GeneratorContainer(object):
@@ -54,11 +54,17 @@ def __init__(self, generator_function, *args, **kwargs):
5454
raise TypeError("generator_function must be a generator function.")
5555

5656
self.generator_function = generator_function
57-
self.signature = inspect.signature(self.generator_function)
58-
self.bound_arguments = self.signature.bind(*args, **kwargs)
59-
self.arguments = self.bound_arguments.arguments
60-
self.args = self.bound_arguments.args
61-
self.kwargs = self.bound_arguments.kwargs
57+
58+
if sys.version_info[0] < 3:
59+
self.arguments = inspect.getcallargs(
60+
self.generator_function,
61+
*args,
62+
**kwargs
63+
)
64+
else:
65+
signature = inspect.signature(self.generator_function)
66+
bound_arguments = signature.bind(*args, **kwargs)
67+
self.arguments = bound_arguments.arguments
6268

6369
def __repr__(self):
6470
"""A string representation of this object."""
@@ -76,7 +82,7 @@ def __str__(self):
7682

7783
def new_generator(self):
7884
"""Create a new generator object."""
79-
return self.generator_function(*self.args, **self.kwargs)
85+
return self.generator_function(**self.arguments)
8086

8187
def __iter__(self):
8288
"""Return a fresh iterator."""

0 commit comments

Comments
 (0)