From 4ceae8c83a3017e505f09b4c125eb5ec5b39f617 Mon Sep 17 00:00:00 2001 From: Alex Miller Date: Thu, 23 Jan 2020 16:09:56 -0800 Subject: [PATCH] Ban generators in 7.0.0 --- bindings/python/fdb/impl.py | 11 ++++++++--- bindings/python/tests/tester.py | 18 +++++++++++++++--- 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/bindings/python/fdb/impl.py b/bindings/python/fdb/impl.py index 490b10cc46..5641832970 100644 --- a/bindings/python/fdb/impl.py +++ b/bindings/python/fdb/impl.py @@ -252,6 +252,12 @@ def transactional(*tr_args, **tr_kwargs): else: @functools.wraps(func) def wrapper(*args, **kwargs): + # We can't throw this from the decorator, as when a user runs + # >>> import fdb ; fdb.api_version(700) + # the code above uses @transactional before the API version is set + if fdb.get_api_version() >= 700 and inspect.isgeneratorfunction(func): + raise ValueError("Generators can not be wrapped with fdb.transactional") + if isinstance(args[index], TransactionRead): return func(*args, **kwargs) @@ -267,14 +273,13 @@ def transactional(*tr_args, **tr_kwargs): ret = None try: ret = func(*largs, **kwargs) + if fdb.get_api_version() >= 700 and inspect.isgenerator(ret): + raise ValueError("Generators can not be wrapped with fdb.transactional") tr.commit().wait() committed = True except FDBError as e: tr.on_error(e.code).wait() - if fdb.get_api_version() >= 620 and isinstance(ret, types.GeneratorType): - raise ValueError("Generators can not be wrapped with fdb.transactional") - # now = datetime.datetime.now() # td = now - last # elapsed = (td.microseconds + (td.seconds + td.days * 24 * 3600) * 10**6) / float(10**6) diff --git a/bindings/python/tests/tester.py b/bindings/python/tests/tester.py index 4376a29766..a32de5114b 100644 --- a/bindings/python/tests/tester.py +++ b/bindings/python/tests/tester.py @@ -132,10 +132,22 @@ def test_fdb_transactional_generator(db): @fdb.transactional def function_that_yields(tr): yield 0 - function_that_yields(db) - assert fdb.get_api_version() < 620, "Generators post-6.2.0 should throw" + assert fdb.get_api_version() < 700, "Pre-7.0, a decorators may wrap a function that yield" except ValueError as e: - pass + assert fdb.get_api_version() >= 700, "Post-7.0, a decorator should throw if wrapped function yields" + + +def test_fdb_transactional_returns_generator(db): + try: + def function_that_yields(tr): + yield 0 + @fdb.transactional + def function_that_returns(tr): + return function_that_yields(tr) + function_that_returns() + assert fdb.get_api_version() < 700, "Pre-7.0, returning a generator is allowed" + except ValueError as e: + assert fdb.get_api_version() >= 700, "Post-7.0, returning a generator should throw" def test_db_options(db):