components/python/decorator/test/results-3.4-32.master
changeset 3840 39959a8e7a76
child 5028 db8ff415ba49
equal deleted inserted replaced
3839:dece34b8bbeb 3840:39959a8e7a76
       
     1 Trying:
       
     2     @memoize_uw
       
     3     def f1(x):
       
     4         time.sleep(1) # simulate some long computation
       
     5         return x
       
     6 Expecting nothing
       
     7 ok
       
     8 Trying:
       
     9     from inspect import getargspec 
       
    10 Expecting nothing
       
    11 ok
       
    12 Trying:
       
    13     print(getargspec(f1))
       
    14 Expecting:
       
    15     ArgSpec(args=[], varargs='args', keywords='kw', defaults=None)
       
    16 ok
       
    17 Trying:
       
    18     f1(0, 1) # doctest: +IGNORE_EXCEPTION_DETAIL
       
    19 Expecting:
       
    20     Traceback (most recent call last):
       
    21        ...
       
    22     TypeError: f1() takes exactly 1 positional argument (2 given)
       
    23 ok
       
    24 Trying:
       
    25     from decorator import decorator
       
    26 Expecting nothing
       
    27 ok
       
    28 Trying:
       
    29     @memoize
       
    30     def heavy_computation():
       
    31         time.sleep(2)
       
    32         return "done"
       
    33 Expecting nothing
       
    34 ok
       
    35 Trying:
       
    36     print(heavy_computation()) # the first time it will take 2 seconds
       
    37 Expecting:
       
    38     done
       
    39 ok
       
    40 Trying:
       
    41     print(heavy_computation()) # the second time it will be instantaneous
       
    42 Expecting:
       
    43     done
       
    44 ok
       
    45 Trying:
       
    46     print(getargspec(heavy_computation)) 
       
    47 Expecting:
       
    48     ArgSpec(args=[], varargs=None, keywords=None, defaults=None)
       
    49 ok
       
    50 Trying:
       
    51     @trace
       
    52     def f1(x):
       
    53         pass
       
    54 Expecting nothing
       
    55 ok
       
    56 Trying:
       
    57     f1(0)
       
    58 Expecting:
       
    59     calling f1 with args (0,), {}
       
    60 ok
       
    61 Trying:
       
    62     print(getargspec(f1)) 
       
    63 Expecting:
       
    64     ArgSpec(args=['x'], varargs=None, keywords=None, defaults=None)
       
    65 ok
       
    66 Trying:
       
    67     @trace
       
    68     def f(x, y=1, z=2, *args, **kw):
       
    69         pass
       
    70 Expecting nothing
       
    71 ok
       
    72 Trying:
       
    73     f(0, 3)
       
    74 Expecting:
       
    75     calling f with args (0, 3, 2), {}
       
    76 ok
       
    77 Trying:
       
    78     print(getargspec(f)) 
       
    79 Expecting:
       
    80     ArgSpec(args=['x', 'y', 'z'], varargs='args', keywords='kw', defaults=(1, 2))
       
    81 ok
       
    82 Trying:
       
    83     @trace
       
    84     def f(x: 'the first argument', y: 'default argument'=1, z=2,
       
    85           *args: 'varargs', **kw: 'kwargs'):
       
    86         pass
       
    87 Expecting nothing
       
    88 ok
       
    89 Trying:
       
    90     from inspect import getfullargspec 
       
    91 Expecting nothing
       
    92 ok
       
    93 Trying:
       
    94     argspec = getfullargspec(f)
       
    95 Expecting nothing
       
    96 ok
       
    97 Trying:
       
    98     argspec.args
       
    99 Expecting:
       
   100     ['x', 'y', 'z']
       
   101 ok
       
   102 Trying:
       
   103     argspec.varargs
       
   104 Expecting:
       
   105     'args'
       
   106 ok
       
   107 Trying:
       
   108     argspec.varkw
       
   109 Expecting:
       
   110     'kw'
       
   111 ok
       
   112 Trying:
       
   113     argspec.defaults
       
   114 Expecting:
       
   115     (1, 2)
       
   116 ok
       
   117 Trying:
       
   118     argspec.kwonlyargs
       
   119 Expecting:
       
   120     []
       
   121 ok
       
   122 Trying:
       
   123     argspec.kwonlydefaults
       
   124 Expecting nothing
       
   125 ok
       
   126 Trying:
       
   127     f.__annotations__ == f.__wrapped__.__annotations__
       
   128 Expecting:
       
   129     True
       
   130 ok
       
   131 Trying:
       
   132     @decorator
       
   133     def trace(f, *args, **kw):
       
   134         kwstr = ', '.join('%r: %r' % (k, kw[k]) for k in sorted(kw))
       
   135         print("calling %s with args %s, {%s}" % (f.__name__, args, kwstr))
       
   136         return f(*args, **kw)
       
   137 Expecting nothing
       
   138 ok
       
   139 Trying:
       
   140     trace # doctest: +ELLIPSIS
       
   141 Expecting:
       
   142     <function trace at 0x...>
       
   143 ok
       
   144 Trying:
       
   145     @trace
       
   146     def func(): pass
       
   147 Expecting nothing
       
   148 ok
       
   149 Trying:
       
   150     func()
       
   151 Expecting:
       
   152     calling func with args (), {}
       
   153 ok
       
   154 Trying:
       
   155     @blocking("Please wait ...")
       
   156     def read_data():
       
   157         time.sleep(3) # simulate a blocking resource
       
   158         return "some data"
       
   159 Expecting nothing
       
   160 ok
       
   161 Trying:
       
   162     print(read_data()) # data is not available yet
       
   163 Expecting:
       
   164     Please wait ...
       
   165 ok
       
   166 Trying:
       
   167     time.sleep(1)  
       
   168 Expecting nothing
       
   169 ok
       
   170 Trying:
       
   171     print(read_data()) # data is not available yet
       
   172 Expecting:
       
   173     Please wait ...
       
   174 ok
       
   175 Trying:
       
   176     time.sleep(1)
       
   177 Expecting nothing
       
   178 ok
       
   179 Trying:
       
   180     print(read_data()) # data is not available yet
       
   181 Expecting:
       
   182     Please wait ...
       
   183 ok
       
   184 Trying:
       
   185     time.sleep(1.1) # after 3.1 seconds, data is available
       
   186 Expecting nothing
       
   187 ok
       
   188 Trying:
       
   189     print(read_data())
       
   190 Expecting:
       
   191     some data
       
   192 ok
       
   193 Trying:
       
   194     async = decorator(Async(threading.Thread))
       
   195 Expecting nothing
       
   196 ok
       
   197 Trying:
       
   198     datalist = [] # for simplicity the written data are stored into a list.
       
   199 Expecting nothing
       
   200 ok
       
   201 Trying:
       
   202     @async
       
   203     def write(data):
       
   204         # append data to the datalist by locking
       
   205         with threading.Lock():
       
   206             time.sleep(1) # emulate some long running operation
       
   207             datalist.append(data)
       
   208         # other operations not requiring a lock here
       
   209 Expecting nothing
       
   210 ok
       
   211 Trying:
       
   212     write("data1") # doctest: +ELLIPSIS
       
   213 Expecting:
       
   214     <Thread(write-1, started...)>
       
   215 ok
       
   216 Trying:
       
   217     time.sleep(.1) # wait a bit, so we are sure data2 is written after data1
       
   218 Expecting nothing
       
   219 ok
       
   220 Trying:
       
   221     write("data2") # doctest: +ELLIPSIS
       
   222 Expecting:
       
   223     <Thread(write-2, started...)>
       
   224 ok
       
   225 Trying:
       
   226     time.sleep(2) # wait for the writers to complete
       
   227 Expecting nothing
       
   228 ok
       
   229 Trying:
       
   230     print(datalist)
       
   231 Expecting:
       
   232     ['data1', 'data2']
       
   233 ok
       
   234 Trying:
       
   235     from contextlib import contextmanager
       
   236 Expecting nothing
       
   237 ok
       
   238 Trying:
       
   239     @contextmanager
       
   240     def before_after(before, after):
       
   241         print(before)
       
   242         yield
       
   243         print(after)
       
   244 Expecting nothing
       
   245 ok
       
   246 Trying:
       
   247     ba = before_after('BEFORE', 'AFTER')
       
   248 Expecting nothing
       
   249 ok
       
   250 Trying:
       
   251     type(ba)
       
   252 Expecting:
       
   253     <class 'contextlib._GeneratorContextManager'>
       
   254 ok
       
   255 Trying:
       
   256     with ba:
       
   257         print('hello')
       
   258 Expecting:
       
   259     BEFORE
       
   260     hello
       
   261     AFTER
       
   262 ok
       
   263 Trying:
       
   264     def f(*args, **kw): # a function with a generic signature
       
   265         print(args, kw)
       
   266 Expecting nothing
       
   267 ok
       
   268 Trying:
       
   269     f1 = FunctionMaker.create('f1(a, b)', 'f(a, b)', dict(f=f))
       
   270 Expecting nothing
       
   271 ok
       
   272 Trying:
       
   273     f1(1,2)
       
   274 Expecting:
       
   275     (1, 2) {}
       
   276 ok
       
   277 Trying:
       
   278     f1 = FunctionMaker.create(
       
   279         'f1(a, b)', 'f(a, b)', dict(f=f), addsource=True)
       
   280 Expecting nothing
       
   281 ok
       
   282 Trying:
       
   283     print(f1.__source__)
       
   284 Expecting:
       
   285     def f1(a, b):
       
   286         f(a, b)
       
   287     <BLANKLINE>
       
   288 ok
       
   289 Trying:
       
   290     print(inspect.getsource(example))
       
   291 Expecting:
       
   292         def wrapper(*args, **kw):
       
   293             return func(*args, **kw)
       
   294     <BLANKLINE>
       
   295 ok
       
   296 Trying:
       
   297     print(inspect.getsource(factorial.__wrapped__))
       
   298 Expecting:
       
   299     @tail_recursive
       
   300     def factorial(n, acc=1):
       
   301         "The good old factorial"
       
   302         if n == 0: return acc
       
   303         return factorial(n-1, n*acc)
       
   304     <BLANKLINE>
       
   305 ok
       
   306 Trying:
       
   307     print(factorial(4))
       
   308 Expecting:
       
   309     24
       
   310 ok
       
   311 Trying:
       
   312     @trace
       
   313     def f():
       
   314         1/0
       
   315 Expecting nothing
       
   316 ok
       
   317 Trying:
       
   318     f() # doctest: +ELLIPSIS
       
   319 Expecting:
       
   320     Traceback (most recent call last):
       
   321       ...
       
   322          File "<string>", line 2, in f
       
   323          File "<doctest __main__[22]>", line 4, in trace
       
   324            return f(*args, **kw)
       
   325          File "<doctest __main__[51]>", line 3, in f
       
   326            1/0
       
   327     ZeroDivisionError: ...
       
   328 ok
       
   329 Trying:
       
   330     @trace
       
   331     def f(_func_): print(f)
       
   332 Expecting:
       
   333     Traceback (most recent call last):
       
   334       ...
       
   335     NameError: _func_ is overridden in
       
   336     def f(_func_):
       
   337         return _call_(_func_, _func_)
       
   338 ok
       
   339 Trying:
       
   340     def f(): pass # the original function
       
   341 Expecting nothing
       
   342 ok
       
   343 Trying:
       
   344     f.attr1 = "something" # setting an attribute
       
   345 Expecting nothing
       
   346 ok
       
   347 Trying:
       
   348     f.attr2 = "something else" # setting another attribute
       
   349 Expecting nothing
       
   350 ok
       
   351 Trying:
       
   352     traced_f = trace(f) # the decorated function
       
   353 Expecting nothing
       
   354 ok
       
   355 Trying:
       
   356     traced_f.attr1
       
   357 Expecting:
       
   358     'something'
       
   359 ok
       
   360 Trying:
       
   361     traced_f.attr2 = "something different" # setting attr
       
   362 Expecting nothing
       
   363 ok
       
   364 Trying:
       
   365     f.attr2 # the original attribute did not change
       
   366 Expecting:
       
   367     'something else'
       
   368 ok
       
   369 Trying:
       
   370     a = Action()
       
   371 Expecting nothing
       
   372 ok
       
   373 Trying:
       
   374     a.view() # ok
       
   375 Expecting nothing
       
   376 ok
       
   377 Trying:
       
   378     a.insert() # err
       
   379 Expecting:
       
   380     Traceback (most recent call last):
       
   381        ...
       
   382     PermissionError: User does not have the permission to run insert!
       
   383 ok
       
   384 Trying:
       
   385     decorator(_memoize).__name__
       
   386 Expecting:
       
   387     '_memoize'
       
   388 ok
       
   389 Trying:
       
   390     factorial.__doc__
       
   391 Expecting:
       
   392     'The good old factorial'
       
   393 ok
       
   394 Trying:
       
   395     ba.__class__.__name__
       
   396 Expecting:
       
   397     'ContextManager'
       
   398 ok
       
   399 Trying:
       
   400     hello('michele')
       
   401 Expecting:
       
   402     BEFORE
       
   403     hello michele
       
   404     AFTER
       
   405 ok
       
   406 Trying:
       
   407     @trace
       
   408     def f(**kw): pass
       
   409 Expecting nothing
       
   410 ok
       
   411 Trying:
       
   412     f()
       
   413 Expecting:
       
   414     calling f with args (), {}
       
   415 ok
       
   416 Trying:
       
   417     @trace
       
   418     def f(*, a=1, **kw): pass
       
   419 Expecting nothing
       
   420 ok
       
   421 Trying:
       
   422     inspect.getfullargspec(f)
       
   423 Expecting:
       
   424     FullArgSpec(args=[], varargs=None, varkw='kw', defaults=None, kwonlyargs=['a'], kwonlydefaults={'a': 1}, annotations={})
       
   425 ok
       
   426 Trying:
       
   427     @trace
       
   428     def func(a, b, *args, y=2, z=3, **kwargs):
       
   429         return y, z
       
   430 Expecting nothing
       
   431 ok
       
   432 Trying:
       
   433     func('a', 'b', 'c', 'd', 'e', y='y', z='z', cat='dog')
       
   434 Expecting:
       
   435     calling func with args ('a', 'b', 'c', 'd', 'e'), {'cat': 'dog', 'y': 'y', 'z': 'z'}
       
   436     ('y', 'z')
       
   437 ok
       
   438 Trying:
       
   439     @trace
       
   440     def f(arg, defarg=1, *args, kwonly=2): pass
       
   441 Expecting nothing
       
   442 ok
       
   443 Trying:
       
   444     f.__kwdefaults__ 
       
   445 Expecting:
       
   446     {'kwonly': 2}
       
   447 ok
       
   448 31 items had no tests:
       
   449     __main__.Action.delete
       
   450     __main__.Action.insert
       
   451     __main__.Action.view
       
   452     __main__.Admin
       
   453     __main__.Async
       
   454     __main__.Async.__call__
       
   455     __main__.Async.__init__
       
   456     __main__.PermissionError
       
   457     __main__.PowerUser
       
   458     __main__.TailRecursive
       
   459     __main__.TailRecursive.__call__
       
   460     __main__.TailRecursive.__init__
       
   461     __main__.User
       
   462     __main__._memoize
       
   463     __main__._trace
       
   464     __main__.before_after
       
   465     __main__.blocking
       
   466     __main__.decorator_apply
       
   467     __main__.example
       
   468     __main__.fact
       
   469     __main__.factorial
       
   470     __main__.get_userclass
       
   471     __main__.identity_dec
       
   472     __main__.memoize
       
   473     __main__.memoize_uw
       
   474     __main__.on_closing
       
   475     __main__.on_failure
       
   476     __main__.on_success
       
   477     __main__.restricted
       
   478     __main__.tail_recursive
       
   479     __main__.trace
       
   480 8 items passed all tests:
       
   481   68 tests in __main__
       
   482    3 tests in __main__.Action
       
   483    2 tests in __main__.a_test_for_pylons
       
   484    2 tests in __main__.hello
       
   485    2 tests in __main__.test_kwonly_no_args
       
   486    2 tests in __main__.test_kwonly_star_notation
       
   487    2 tests in __main__.test_kwonlyargs
       
   488    2 tests in __main__.test_kwonlydefaults
       
   489 83 tests in 39 items.
       
   490 83 passed and 0 failed.
       
   491 Test passed.
       
   492 [?1034h