├── programs ├── files │ ├── # │ ├── π │ ├── stat3 │ ├── stat │ ├── stat2 │ │ └── .hgempty │ └── FileIO ├── module1.py ├── politz │ ├── test_as │ │ ├── empty.py │ │ └── __init__.py │ ├── generated4test.py │ ├── tuple-truth.py │ ├── address-of-none.py │ ├── test_list_assign.py │ ├── test_list_truth.py │ ├── assertion-error.py │ ├── test_list_identity.py │ ├── tuple-mul.py │ ├── test-dict-aug-assign.py │ ├── tuple-add.py │ ├── bool-callable.py │ ├── bool-str.py │ ├── func_attr.py │ ├── func_attr2.py │ ├── test-builtin-scope.py │ ├── tuple-to-list.py │ ├── simple-append.py │ ├── tuple-length.py │ ├── lambda3.py │ ├── assert-silent.py │ ├── dict-list.py │ ├── gen-arg.py │ ├── dict-clear.py │ ├── dict-bool.py │ ├── except-as.py │ ├── lambda4.py │ ├── tuple-in.py │ ├── lambda1.py │ ├── support_exception.py │ ├── bool-int.py │ ├── elseReturn.py │ ├── assign-to-self.py │ ├── gen-list.py │ ├── test-try-except-no-exception.py │ ├── try-finally-no-exception.py │ ├── dict-update-iterable.py │ ├── lambda2.py │ ├── test-try-except.py │ ├── bool-float.py │ ├── simple-extend.py │ ├── test_in_modules.py │ ├── dict-values.py │ ├── invalid-reraise.py │ ├── test-assign-field.py │ ├── dict-items.py │ ├── nested-classes.py │ ├── continue.py │ ├── test_simple_strings.py │ ├── bitwise_test.py │ ├── gen-in-gen.py │ ├── test_pow.py │ ├── type-dict.py │ ├── dict-keys.py │ ├── dict-set-comprehensions.py │ ├── non_tuple_starag.py │ ├── varargs.py │ ├── nested-reraise.py │ ├── dict-contains.py │ ├── test-builtin-scope-iter.py │ ├── comprehensions-leak.py │ ├── len.py │ ├── support.py │ ├── test-staticmethod-decorator.py │ ├── test_booleans.py │ ├── test-staticmethod-oldstyle.py │ ├── bool-convert.py │ ├── bound-and-free.py │ ├── dict-attribute.py │ ├── simple-gen.py │ ├── simple-nesting.py │ ├── types_truthy2.py │ ├── shadow.py │ ├── try-except-else-no-exception.py │ ├── while-break-finally.py │ ├── test-finally-reraise.py │ ├── reraise.py │ ├── test-try-except-else.py │ ├── try-except-finally-no-exception.py │ ├── unboundlocal-augassign.py │ ├── nonlocal-class.py │ ├── simple_getter_exn.py │ ├── try-except-finally.py │ ├── dict-update.py │ ├── except-reraise.py │ ├── test_exception.py │ ├── simple_getter.py │ ├── destructuring-bind.py │ ├── nearest-enclosing-scope.py │ ├── override_getattr_descr.py │ ├── bool-isinstance.py │ ├── nesting-global-under-local.py │ ├── nested-nonlocal.py │ ├── nesting-through-class.py │ ├── range-list.py │ ├── dict-keys-effects.py │ ├── recursion.py │ ├── locals-function.py │ ├── test_import_name_binding.py │ ├── dict-getitem.py │ ├── test-for-else.py │ ├── send-gen.py │ ├── slice1.py │ ├── simple-and-rebinding.py │ ├── unbound-local.py │ ├── while-continue-finally.py │ ├── test-add-with-funcs.py │ ├── assertRaises.py │ ├── super-external.py │ ├── super-nested-self.py │ ├── test_ex5_from_c3_switch.py │ ├── unboundlocal-after-del.py │ ├── extra-nesting.py │ ├── test-aliasing.py │ ├── nesting-plus-free-ref-to-global.py │ ├── try-except-else-finally-no-exception.py │ ├── bool-compare.py │ ├── dict-get.py │ ├── try-except-else-finally.py │ ├── super-external-self.py │ ├── nonlocal-function.py │ ├── super-nested-noarg.py │ ├── test-classmethod-desugar.py │ ├── test_floats.py │ ├── for-loops.py │ ├── keywords_args1.py │ ├── tuple-constructors.py │ ├── assertion-error-msg.py │ ├── test_file.py │ ├── callable.py │ ├── memofib-function-attributes.py │ ├── metaclass-function.py │ ├── locals-function-renamed.py │ ├── mixed-freevars-and-cellvars.py │ ├── test_comparisons.py │ ├── filter-comprehension.py │ ├── gen-return.py │ ├── nested.py │ ├── nonlocal-method.py │ ├── dict-set-keys.py │ ├── super-nested-not-self.py │ ├── gen-exception.py │ ├── isinstance.py │ ├── metaclass-class1.py │ ├── test_failing_support_sticks.py │ ├── test_simple_string_ops.py │ ├── keywords_args2.py │ ├── basic-gen.py │ ├── metaclass-class2.py │ ├── gen-return-uncatchable.py │ ├── nesting-global-no-free.py │ ├── range-errs.py │ ├── test_basic.py │ ├── dict-values-set.py │ ├── global-in-parallel-nested-functions.py │ ├── isinstance-tuple.py │ ├── keywords_args3.py │ ├── metaclass-class3.py │ ├── simple_property.py │ ├── types_truthy1.py │ ├── all.py │ ├── simple_property_decorator.py │ ├── any.py │ ├── dunder-methods.py │ ├── class-bases-scope.py │ └── immutable-alias.py ├── testpass.py ├── smeding │ ├── imported │ │ ├── __init__.py │ │ ├── imported_module_in_module_B.py │ │ ├── imported_syntax_error.py │ │ ├── imported_module_in_module_A.py │ │ ├── imported_module_override_class.py │ │ ├── imported_instantiation_module.py │ │ ├── imported_re_export_imported_B.py │ │ ├── imported_re_export_imported_A.py │ │ ├── imported_self.py │ │ ├── imported_circular_shared_env_A.py │ │ └── imported_circular_shared_env_B.py │ ├── and-trace-lazy.py │ ├── import-non-existant.py │ ├── exec-basic-trace.py │ ├── int-as-super.py │ ├── bool-as-super.py │ ├── binop-simple-trace.py │ ├── lists-prim-getitem-index-pos.py │ ├── lists-prim-getitem-index-neg.py │ ├── none-as-super.py │ ├── str-as-super.py │ ├── import-syntax-error.py │ ├── exec-type-prog-nostring.py │ ├── lists-prim-getitem-type.py │ ├── class-scope-recursive.py │ ├── exec-type-prog-childofstr.py │ ├── instantiation-lambda.py │ ├── lists-iter-empty.py │ ├── function-scope-del-self.py │ ├── if-else-scope-inh.py │ ├── lists-prim-getitem-index-large.py │ ├── for-type-notiterable.py │ ├── lists-as-super.py │ ├── lists-prim-getitem-index-small.py │ ├── object-new-noinit.py │ ├── try-finally-scope-noblock.py │ ├── class-scope-classattr.py │ ├── function-call-basic-noargs.py │ ├── class-scope-simple.py │ ├── class-scope-recursive-inh.py │ ├── function-call-basic.py │ ├── class-scope-nested-super.py │ ├── import-module-in-module.py │ ├── instantiation-module.py │ ├── instantiation-type-simple.py │ ├── class-delattr-instdef.py │ ├── dictionary-constr-withinst.py │ ├── import-self.py │ ├── op-type-add-noattr.py │ ├── instantiation-type-inh.py │ ├── dictionary-as-super.py │ ├── print-type-nostring.py │ ├── try-finally-break.py │ ├── class-scope-lambda.py │ ├── exception-basic-catch.py │ ├── function-scope-del-inh.py │ ├── if-else-scope-inh-body.py │ ├── lists-iter-simple.py │ ├── attr-assign-trace.py │ ├── class-delattr-classattr.py │ ├── class-delattr-initdef.py │ ├── subscription-trace-argsfirst.py │ ├── try-except-type-catch-nonexcep.py │ ├── dict-no-instantiation.py │ ├── function-call-evalargs-noargs.py │ ├── instantiation-double.py │ ├── try-except-scope-raise.py │ ├── exec-type-env-childofdict.py │ ├── try-except-scope-uncaughtexception.py │ ├── try-finally-return.py │ ├── type-new-type.py │ ├── function-scope-shadow-self.py │ ├── lists-iter-append.py │ ├── binop-operants-before-operator.py │ ├── exec-context-sensitive.py │ ├── try-except-scope-catchtype.py │ ├── try-except-scope-caughtexception.py │ ├── try-except-type-raise-string.py │ ├── subscription-assign-trace-argsfirst.py │ ├── class-scope-funcattr-call.py │ ├── function-scope-double-del.py │ ├── instantiation-objfunc.py │ ├── str-as-super-tostr.py │ ├── try-finally-continue.py │ ├── class-scope-func-recursive.py │ ├── instantiation-type.py │ ├── metaclass-new-copy-dict.py │ ├── try-except-scope-noblock.py │ ├── class-scope-nested.py │ ├── exec-func-no-new-locals.py │ ├── exception-trace-noraise.py │ ├── function-closure-env-notseq-nested.py │ ├── instantiation-classobjects.py │ ├── if-else-scope-shadow.py │ ├── try-finally-scope-caughtexception.py │ ├── instantiation-callable-object.py │ ├── function-scope-refbeforeshadow.py │ ├── print-childof-str.py │ ├── and-trace-nonzero-before-rhs.py │ ├── exec-type-env-simulated.py │ ├── generator-call-while-running.py │ ├── lists-iter-fail-fail.py │ ├── try-except-type-raise-nonexcep.py │ ├── if-else-nonzero-objectmember.py │ ├── import-re-export-imported.py │ ├── metaclass-rename-class.py │ ├── generator-raising-stops.py │ ├── object-new-type.py │ ├── try-finally-yield.py │ ├── function-scope-del-attr.py │ ├── class-scope-function-shadow.py │ ├── exception-basic-nocatch.py │ ├── for-raise-stopiter-in-body.py │ ├── for-type-baditerator-nonext.py │ ├── try-finally-scope-noblock-raise.py │ ├── class-scope-shadow.py │ ├── function-closure-env-notseq.py │ ├── metaclass-inherited-simple.py │ ├── getattribute-class-func-type.py │ ├── instantiation-nopars.py │ ├── try-except-type-catch-subtype.py │ ├── for-type-baditerator-wrongnext.py │ ├── module-override-class.py │ ├── try-except-type-catch-siblingclass.py │ ├── if-else-nonzero-returntype.py │ ├── instantiation-parname.py │ ├── class-scope-func.py │ ├── for-type-iter-in-object.py │ ├── if-else-scope-remove.py │ ├── getattribute-class-func-instantiation.py │ ├── exception-trace-raise.py │ ├── lists-iter-fail-append-fail.py │ ├── function-scope-shadow-callself.py │ ├── getattribute-in-parent.py │ ├── for-type-baditerator-rmnext.py │ ├── metaclass-type-new-return-True.py │ ├── while-type-rmnonzero.py │ ├── instantiation-objasfunc.py │ ├── for-type-stopiter-faulty.py │ ├── setattr-trace.py │ ├── function-object-as-func-trace.py │ ├── for-trace-basic.py │ ├── for-type-stopiter-subtype.py │ ├── metaclass-basic-trace.py │ └── exception-basic-throwingcatch.py ├── testassign1.py ├── testlambda.py ├── testtuples11.py ├── package1 │ ├── module2.py │ ├── module3.py │ └── __init__.py ├── testaugassign1.py ├── testaugassign10.py ├── testaugassign11.py ├── testaugassign2.py ├── testaugassign3.py ├── testaugassign4.py ├── testaugassign5.py ├── testaugassign6.py ├── testaugassign7.py ├── testaugassign8.py ├── testaugassign9.py ├── testbools8.py ├── testin.py ├── testintegers1.py ├── testassign2.py ├── testfunctions10.py ├── testimport3.py ├── testbools3.py ├── testfunctions9.py ├── testlists1.py ├── testlists7.py ├── testord.py ├── testtuples1.py ├── testassign5.py ├── testassign6.py ├── testassign7.py ├── testassign9.py ├── testgetattr4.py ├── testscope6.py ├── testtry15.py ├── testeval4.py ├── testintegers10.py ├── testobject1.py ├── teststrings11.py ├── testtuples2.py ├── testassign3.py ├── testassign4.py ├── testbools11.py ├── testbools5.py ├── testbools9.py ├── testimport4.py ├── testtuples3.py ├── testtuples8.py ├── testclasses4.py ├── testdicts2.py ├── testfunctions12.py ├── testintegers5.py ├── testintegers7.py ├── testlists3.py ├── testscope11.py ├── testtry4.py ├── testtry5.py ├── testtuples6.py ├── testbytes3.py ├── testlists4.py ├── testbools4.py ├── testcompile2.py ├── testlists2.py ├── teststrings2.py ├── testtuples9.py ├── testwhile1.py ├── testbools7.py ├── testfunctions11.py ├── testbytes2.py ├── testfor1.py ├── testlists10.py ├── testscope1.py ├── testfunctions2.py ├── testlists8.py ├── testloopfinally9.py ├── teststaticmethod.py ├── testexec4.py ├── testfunctions14.py ├── testyield6.py ├── testbools10.py ├── testclasses9.py ├── testfunctions13.py ├── testobject2.py ├── teststrings1.py ├── testtuples12.py ├── testclassmethod.py ├── testeval5.py ├── testisinstance2.py ├── testscope9.py ├── testfor3.py ├── testmap.py ├── testcallable.py ├── testintegers4.py ├── testissubclass2.py ├── testassert2.py ├── testclasses7.py ├── testhash.py ├── testtry2.py ├── testlists9.py ├── testsuper2.py ├── testtry7.py ├── testassign8.py ├── testloopfinally7.py ├── testbools2.py ├── testfor4.py ├── testif.py ├── testio8.py ├── testloopfinally6.py ├── teststrings10.py ├── testdecorator.py ├── testlists6.py ├── testloopfinally2.py ├── testrange.py ├── teststrings3.py ├── testwhile3.py ├── testclasses2.py ├── testtry14.py ├── testwith4.py ├── testfor2.py ├── testfunctions5.py ├── testloopfinally3.py ├── testloopfinally4.py ├── testloopfinally8.py ├── teststrings5.py ├── testassert1.py ├── testassign10.py ├── testio6.py ├── testnone.py ├── testobject3.py ├── testtry10.py ├── testfuncfinally4.py ├── testiter2.py ├── testformat1.py ├── testscope8.py ├── testyield14.py ├── testyield5.py ├── testfunctions8.py ├── testintegers3.py ├── testintegers6.py ├── testrepr2.py ├── testtry3.py ├── testfunctions6.py ├── testtuples10.py ├── testyield10.py ├── testfuncfinally2.py ├── testtry12.py ├── testwhile4.py ├── testimport2.py ├── testis.py ├── testiter1.py ├── testsys.py ├── testtry1.py ├── testwhile2.py ├── testfunctions3.py ├── testio7.py ├── testlists5.py ├── testreversed1.py ├── testfuncfinally1.py ├── testfuncraise2.py ├── testimport1.py ├── testsetattr4.py ├── testaugassign12.py ├── testbools6.py ├── testlists11.py ├── teststrings9.py ├── testyield4.py ├── testexceptions1.py ├── testgetattr2.py ├── testloopfinally5.py ├── teststrings7.py ├── testyield3.py ├── testyield8.py ├── testfloat1.py ├── testloopfinally1.py ├── testmodule1.py ├── testposix3.py ├── testsetattr5.py ├── testtry13.py ├── testtry9.py ├── testyield9.py ├── testbytes4.py ├── testfuncfinally3.py ├── testfunctions4.py ├── testsetattr1.py ├── testtuples5.py ├── testisinstance1.py ├── teststrings4.py ├── testbools1.py ├── testtry6.py ├── testtuples4.py ├── testbytes1.py ├── testio3.py ├── testissubclass1.py ├── testmockimport5.py ├── testyield13.py ├── testintegers8.py ├── testtuples7.py ├── testyield12.py ├── testobject4.py ├── testslice.py ├── testyield2.py ├── testbytes5.py ├── testall.py ├── testfunctions1.py ├── testscope5.py ├── testyield11.py ├── testany.py ├── testio5.py ├── testsets.py ├── teststrings8.py ├── testintegers9.py ├── testyield7.py ├── testfuncraise1.py ├── testtry11.py ├── testmockimport3.py ├── testscope10.py ├── testreversed2.py ├── testtry8.py ├── testscope4.py ├── testsetattr6.py ├── testscope3.py ├── testcoercion1.py ├── testclasses8.py ├── testeval3.py ├── testcoercion2.py ├── testexec3.py ├── testweakref.py ├── testwith3.py ├── testyield1.py ├── testdicts1.py ├── testfunctions7.py ├── testgetattr5.py ├── testscope2.py ├── testsetattr3.py ├── testrepr1.py ├── testmockimport4.py ├── testclasses6.py ├── testassert3.py ├── testclasses1.py ├── testscope7.py ├── testformat3.py ├── testio1.py ├── testgetattr3.py ├── testdel.py ├── testclasses5.py ├── testeval1.py ├── testexec1.py ├── testmockimport1.py └── testwith2.py ├── bytes.k ├── complex-numbers.k ├── runparse.maude ├── errno.py ├── fake-importlib.py └── .gitignore /programs/files/#: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /programs/files/π: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /programs/files/stat3: -------------------------------------------------------------------------------- 1 | . -------------------------------------------------------------------------------- /programs/files/stat: -------------------------------------------------------------------------------- 1 | foo 2 | -------------------------------------------------------------------------------- /programs/files/stat2/.hgempty: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /programs/module1.py: -------------------------------------------------------------------------------- 1 | x = 5 2 | -------------------------------------------------------------------------------- /programs/politz/test_as/empty.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /programs/testpass.py: -------------------------------------------------------------------------------- 1 | pass 2 | -------------------------------------------------------------------------------- /programs/politz/test_as/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /programs/smeding/imported/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /programs/politz/generated4test.py: -------------------------------------------------------------------------------- 1 | a = 1 / 0 -------------------------------------------------------------------------------- /programs/files/FileIO: -------------------------------------------------------------------------------- 1 | abc 2 | def 3 | ghi 4 | -------------------------------------------------------------------------------- /programs/testassign1.py: -------------------------------------------------------------------------------- 1 | x = 5 2 | assert x == 5 3 | -------------------------------------------------------------------------------- /programs/smeding/imported/imported_module_in_module_B.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /programs/testlambda.py: -------------------------------------------------------------------------------- 1 | assert (lambda x: x + 1)(5) == 6 2 | -------------------------------------------------------------------------------- /programs/testtuples11.py: -------------------------------------------------------------------------------- 1 | x = (5,) 2 | assert x == tuple(x) 3 | -------------------------------------------------------------------------------- /programs/package1/module2.py: -------------------------------------------------------------------------------- 1 | x = 5 2 | y = 5 3 | __all__ = ["x"] 4 | -------------------------------------------------------------------------------- /programs/testaugassign1.py: -------------------------------------------------------------------------------- 1 | x = 5 2 | x += 6 3 | assert x == 11 4 | -------------------------------------------------------------------------------- /programs/testaugassign10.py: -------------------------------------------------------------------------------- 1 | x = 5 2 | x ^= 6 3 | assert x == 3 4 | -------------------------------------------------------------------------------- /programs/testaugassign11.py: -------------------------------------------------------------------------------- 1 | x = 5 2 | x |= 6 3 | assert x == 7 4 | -------------------------------------------------------------------------------- /programs/testaugassign2.py: -------------------------------------------------------------------------------- 1 | x = 5 2 | x -= 6 3 | assert x == -1 4 | -------------------------------------------------------------------------------- /programs/testaugassign3.py: -------------------------------------------------------------------------------- 1 | x = 5 2 | x *= 6 3 | assert x == 30 4 | -------------------------------------------------------------------------------- /programs/testaugassign4.py: -------------------------------------------------------------------------------- 1 | x = 3 2 | x /= 6 3 | assert x == 0.5 4 | -------------------------------------------------------------------------------- /programs/testaugassign5.py: -------------------------------------------------------------------------------- 1 | x = 17 2 | x //= 6 3 | assert x == 2 4 | -------------------------------------------------------------------------------- /programs/testaugassign6.py: -------------------------------------------------------------------------------- 1 | x = 5 2 | x **= 3 3 | assert x == 125 4 | -------------------------------------------------------------------------------- /programs/testaugassign7.py: -------------------------------------------------------------------------------- 1 | x = 32 2 | x >>= 3 3 | assert x == 4 4 | -------------------------------------------------------------------------------- /programs/testaugassign8.py: -------------------------------------------------------------------------------- 1 | x = 4 2 | x <<= 3 3 | assert x == 32 4 | -------------------------------------------------------------------------------- /programs/testaugassign9.py: -------------------------------------------------------------------------------- 1 | x = 5 2 | x &= 6 3 | assert x == 4 4 | -------------------------------------------------------------------------------- /programs/testbools8.py: -------------------------------------------------------------------------------- 1 | class A: pass 2 | 3 | assert not not A 4 | -------------------------------------------------------------------------------- /programs/testin.py: -------------------------------------------------------------------------------- 1 | assert 5 in (5,6) 2 | assert 7 not in (5,6) 3 | -------------------------------------------------------------------------------- /programs/testintegers1.py: -------------------------------------------------------------------------------- 1 | assert 5 == 5 2 | assert not (5 == 6) 3 | -------------------------------------------------------------------------------- /programs/testassign2.py: -------------------------------------------------------------------------------- 1 | x, y = (6, 5) 2 | assert x == 6 and y == 5 3 | -------------------------------------------------------------------------------- /programs/testfunctions10.py: -------------------------------------------------------------------------------- 1 | def a(*, x=5): assert x == 5 2 | 3 | a() 4 | -------------------------------------------------------------------------------- /programs/testimport3.py: -------------------------------------------------------------------------------- 1 | import package1 2 | assert package1.x == 5 3 | -------------------------------------------------------------------------------- /programs/package1/module3.py: -------------------------------------------------------------------------------- 1 | from . import x 2 | assert x == 5 3 | y = 5 4 | -------------------------------------------------------------------------------- /programs/testbools3.py: -------------------------------------------------------------------------------- 1 | assert not None 2 | assert (5,) 3 | assert not () 4 | -------------------------------------------------------------------------------- /programs/testfunctions9.py: -------------------------------------------------------------------------------- 1 | def a(x): assert x == 5 2 | 3 | a(**{"x": 5}) 4 | -------------------------------------------------------------------------------- /programs/testlists1.py: -------------------------------------------------------------------------------- 1 | assert len([5]) == 1 2 | assert len([5,6]) == 2 3 | -------------------------------------------------------------------------------- /programs/testlists7.py: -------------------------------------------------------------------------------- 1 | assert [1, 2, 3] + [4, 5, 6] == [1, 2, 3, 4, 5, 6] 2 | -------------------------------------------------------------------------------- /programs/testord.py: -------------------------------------------------------------------------------- 1 | assert ord("a") == 97 2 | assert ord("\u2020") == 8224 3 | -------------------------------------------------------------------------------- /programs/testtuples1.py: -------------------------------------------------------------------------------- 1 | assert (5,) == (5,) 2 | assert not ((5,6) == (6,)) 3 | -------------------------------------------------------------------------------- /bytes.k: -------------------------------------------------------------------------------- 1 | module BYTES 2 | 3 | syntax Bytes ::= "b" String 4 | 5 | endmodule 6 | -------------------------------------------------------------------------------- /programs/politz/tuple-truth.py: -------------------------------------------------------------------------------- 1 | ___assertTrue(not ()) 2 | ___assertTrue((42, )) 3 | -------------------------------------------------------------------------------- /programs/testassign5.py: -------------------------------------------------------------------------------- 1 | try: 2 | x, y = 5 3 | except TypeError: 4 | pass 5 | -------------------------------------------------------------------------------- /programs/package1/__init__.py: -------------------------------------------------------------------------------- 1 | x = 5 2 | from .module3 import y 3 | assert y == 5 4 | -------------------------------------------------------------------------------- /programs/politz/address-of-none.py: -------------------------------------------------------------------------------- 1 | ret = None 2 | ret = 3 3 | assert(None != 3) 4 | 5 | -------------------------------------------------------------------------------- /programs/politz/test_list_assign.py: -------------------------------------------------------------------------------- 1 | l = [1,2] 2 | l[0] = 4 3 | assert(l == [4,2]) 4 | -------------------------------------------------------------------------------- /programs/politz/test_list_truth.py: -------------------------------------------------------------------------------- 1 | ___assertTrue(not []) 2 | ___assertTrue([42]) 3 | -------------------------------------------------------------------------------- /programs/smeding/imported/imported_syntax_error.py: -------------------------------------------------------------------------------- 1 | # a simple syntax error 2 | - 3 | -------------------------------------------------------------------------------- /programs/testassign6.py: -------------------------------------------------------------------------------- 1 | try: 2 | x, y, z = 1, 2 3 | except ValueError: 4 | pass 5 | -------------------------------------------------------------------------------- /programs/testassign7.py: -------------------------------------------------------------------------------- 1 | try: 2 | x, y = 1, 2, 3 3 | except ValueError: 4 | pass 5 | -------------------------------------------------------------------------------- /programs/testassign9.py: -------------------------------------------------------------------------------- 1 | x, *y, z = (1, 2) 2 | assert x == 1 and z == 2 and y == [] 3 | -------------------------------------------------------------------------------- /programs/politz/assertion-error.py: -------------------------------------------------------------------------------- 1 | ___assertRaises(AssertionError, ___assertEqual, 1, 2) 2 | -------------------------------------------------------------------------------- /programs/testgetattr4.py: -------------------------------------------------------------------------------- 1 | default = 6 2 | assert getattr(5, "foo", default) is default 3 | -------------------------------------------------------------------------------- /programs/testscope6.py: -------------------------------------------------------------------------------- 1 | def a(x): 2 | pass 3 | 4 | assert "x" in a.__code__.co_varnames 5 | -------------------------------------------------------------------------------- /programs/testtry15.py: -------------------------------------------------------------------------------- 1 | try: 2 | raise TypeError 3 | except TypeError as e: 4 | del e 5 | -------------------------------------------------------------------------------- /programs/politz/test_list_identity.py: -------------------------------------------------------------------------------- 1 | if(not ([] is not [])): raise Exception('list identity') 2 | -------------------------------------------------------------------------------- /programs/politz/tuple-mul.py: -------------------------------------------------------------------------------- 1 | u = (0, 1) 2 | u2 = u 3 | u *= 3 4 | ___assertTrue(u is not u2) 5 | -------------------------------------------------------------------------------- /programs/testeval4.py: -------------------------------------------------------------------------------- 1 | try: 2 | eval("5;6") 3 | assert False 4 | except SyntaxError: pass 5 | -------------------------------------------------------------------------------- /programs/testintegers10.py: -------------------------------------------------------------------------------- 1 | class A(int): pass 2 | 3 | assert A(5).__int__().__class__ is int 4 | -------------------------------------------------------------------------------- /programs/politz/test-dict-aug-assign.py: -------------------------------------------------------------------------------- 1 | a = [1,2] 2 | a[0] += 1 3 | 4 | ___assertEqual(a[0], 2) 5 | -------------------------------------------------------------------------------- /programs/politz/tuple-add.py: -------------------------------------------------------------------------------- 1 | u = (0, 1) 2 | u2 = u 3 | u += (2, 3) 4 | ___assertTrue(u is not u2) 5 | -------------------------------------------------------------------------------- /programs/smeding/and-trace-lazy.py: -------------------------------------------------------------------------------- 1 | # check if the and operator is lazy 2 | 3 | False and fail() 4 | -------------------------------------------------------------------------------- /programs/smeding/import-non-existant.py: -------------------------------------------------------------------------------- 1 | expectException(ImportError) 2 | import non_existant_module 3 | -------------------------------------------------------------------------------- /programs/testobject1.py: -------------------------------------------------------------------------------- 1 | class A: pass 2 | 3 | assert "__iter__" in A.__dict__.__class__.__dict__ 4 | -------------------------------------------------------------------------------- /programs/teststrings11.py: -------------------------------------------------------------------------------- 1 | class A(str): pass 2 | 3 | assert A("foo").__str__().__class__ is str 4 | -------------------------------------------------------------------------------- /programs/testtuples2.py: -------------------------------------------------------------------------------- 1 | assert len((5,)) == 1 2 | assert len((5,6)) == 2 3 | assert len(()) == 0 4 | -------------------------------------------------------------------------------- /complex-numbers.k: -------------------------------------------------------------------------------- 1 | module COMPLEX-NUMBERS 2 | syntax Imaginary ::= Float "+" Float "j" 3 | endmodule 4 | -------------------------------------------------------------------------------- /programs/politz/bool-callable.py: -------------------------------------------------------------------------------- 1 | ___assertIs(callable(len), True) 2 | ___assertIs(callable(1), False) 3 | -------------------------------------------------------------------------------- /programs/politz/bool-str.py: -------------------------------------------------------------------------------- 1 | ___assertEqual(str(False), 'False') 2 | ___assertEqual(str(True), 'True') 3 | -------------------------------------------------------------------------------- /programs/smeding/imported/imported_module_in_module_A.py: -------------------------------------------------------------------------------- 1 | import imported.imported_module_in_module_B 2 | -------------------------------------------------------------------------------- /programs/testassign3.py: -------------------------------------------------------------------------------- 1 | x, y, (z, w) = (1, 2, (3, 4)) 2 | assert x == 1 and y == 2 and z == 3 and w == 4 3 | -------------------------------------------------------------------------------- /programs/testassign4.py: -------------------------------------------------------------------------------- 1 | x, y, [z, w] = [1, 2, [3, 4]] 2 | assert x == 1 and y == 2 and z == 3 and w == 4 3 | -------------------------------------------------------------------------------- /programs/testbools11.py: -------------------------------------------------------------------------------- 1 | try: 2 | class A(bool): pass 3 | assert False 4 | except TypeError: pass 5 | -------------------------------------------------------------------------------- /programs/testbools5.py: -------------------------------------------------------------------------------- 1 | assert bool(0) is False 2 | assert bool(1) is True 3 | assert bool() is False 4 | -------------------------------------------------------------------------------- /programs/testbools9.py: -------------------------------------------------------------------------------- 1 | assert 5 == 5 == 5 2 | assert not ((5 == 5) == 5) 3 | assert not (5 == (5 == 5)) 4 | -------------------------------------------------------------------------------- /programs/testimport4.py: -------------------------------------------------------------------------------- 1 | try: 2 | from . import foo 3 | assert False 4 | except SystemError: pass 5 | -------------------------------------------------------------------------------- /programs/testtuples3.py: -------------------------------------------------------------------------------- 1 | assert (5,).__add__((6,)) == tuple.__add__((5,),(6,)) == (5,) + (6,) == (5,6) 2 | -------------------------------------------------------------------------------- /programs/testtuples8.py: -------------------------------------------------------------------------------- 1 | try: 2 | iter.__class__() 3 | except TypeError: 4 | x = 9 5 | assert x == 9 6 | -------------------------------------------------------------------------------- /programs/testclasses4.py: -------------------------------------------------------------------------------- 1 | exec(""" 2 | class A: pass 3 | 4 | assert A.__module__ == "builtins" 5 | """, {}) 6 | -------------------------------------------------------------------------------- /programs/testdicts2.py: -------------------------------------------------------------------------------- 1 | assert {}.get(5) is None 2 | assert {5: 6}.get(5) == 6 3 | assert {}.get(5, 6) == 6 4 | -------------------------------------------------------------------------------- /runparse.maude: -------------------------------------------------------------------------------- 1 | 2 | load /home/k/k-framework/core/maude/compiler/../lib/k-prelude 3 | load .k/python 4 | 5 | -------------------------------------------------------------------------------- /programs/politz/func_attr.py: -------------------------------------------------------------------------------- 1 | def f(): 2 | return f.x 3 | 4 | f.x = 22 5 | 6 | ___assertEqual(f(), 22) 7 | 8 | -------------------------------------------------------------------------------- /programs/testfunctions12.py: -------------------------------------------------------------------------------- 1 | def a(* x, **y): 2 | assert x == () 3 | assert y == {} 4 | 5 | assert a() is None 6 | -------------------------------------------------------------------------------- /programs/testintegers5.py: -------------------------------------------------------------------------------- 1 | if 5: assert True 2 | else: assert False 3 | if 0: assert False 4 | else: assert True 5 | -------------------------------------------------------------------------------- /programs/testintegers7.py: -------------------------------------------------------------------------------- 1 | try: 2 | 5() 3 | assert False 4 | except TypeError: 5 | x = 5 6 | assert x == 5 7 | -------------------------------------------------------------------------------- /programs/testlists3.py: -------------------------------------------------------------------------------- 1 | try: 2 | [5,6][2] 3 | assert False 4 | except IndexError: 5 | x = 5 6 | assert x == 5 7 | -------------------------------------------------------------------------------- /programs/testscope11.py: -------------------------------------------------------------------------------- 1 | class A: 2 | __class__ = 5 3 | def foo(self): assert __class__ is A 4 | 5 | A().foo() 6 | -------------------------------------------------------------------------------- /programs/testtry4.py: -------------------------------------------------------------------------------- 1 | try: 2 | raise Exception 3 | except Exception as e: 4 | assert e.__class__ == Exception 5 | -------------------------------------------------------------------------------- /programs/testtry5.py: -------------------------------------------------------------------------------- 1 | try: 2 | raise Exception() 3 | except Exception as e: 4 | assert e.__class__ == Exception 5 | -------------------------------------------------------------------------------- /programs/testtuples6.py: -------------------------------------------------------------------------------- 1 | try: 2 | iter(5) 3 | assert False 4 | except TypeError: 5 | x = 7 6 | assert x == 7 7 | -------------------------------------------------------------------------------- /programs/testbytes3.py: -------------------------------------------------------------------------------- 1 | try: 2 | bytes("foo") 3 | assert False 4 | except TypeError: 5 | x = 5 6 | assert x == 5 7 | -------------------------------------------------------------------------------- /programs/testlists4.py: -------------------------------------------------------------------------------- 1 | try: 2 | [5,6][-3] 3 | assert False 4 | except IndexError: 5 | x = 6 6 | assert x == 6 7 | -------------------------------------------------------------------------------- /programs/politz/func_attr2.py: -------------------------------------------------------------------------------- 1 | def f(g): 2 | g.x = 22 3 | return g 4 | 5 | ___assertEqual(f(lambda: None).x, 22) 6 | 7 | -------------------------------------------------------------------------------- /programs/politz/test-builtin-scope.py: -------------------------------------------------------------------------------- 1 | # Simple test of builtin scope (issue #44) 2 | int = bool 3 | ___assertEqual(str(5), "5") -------------------------------------------------------------------------------- /programs/politz/tuple-to-list.py: -------------------------------------------------------------------------------- 1 | l = list((1,2,3)) 2 | ___assertEqual(type(l), list) 3 | ___assertEqual(l, [1,2,3]) 4 | 5 | -------------------------------------------------------------------------------- /programs/testbools4.py: -------------------------------------------------------------------------------- 1 | assert (5 and 6) == 6 2 | assert (0 and 6) == 0 3 | assert (5 or 6) == 5 4 | assert (0 or 6) == 6 5 | -------------------------------------------------------------------------------- /programs/testcompile2.py: -------------------------------------------------------------------------------- 1 | assert eval(compile("5", "foo", "eval")) == 5 2 | assert eval(compile("5", "foo", "exec")) is None 3 | -------------------------------------------------------------------------------- /programs/testlists2.py: -------------------------------------------------------------------------------- 1 | assert [5,6][0] == 5 2 | assert [5,6][1] == 6 3 | assert [5,6][-1] == 6 4 | assert [5,6][-2] == 5 5 | -------------------------------------------------------------------------------- /programs/teststrings2.py: -------------------------------------------------------------------------------- 1 | try: 2 | "5" .__add__(6) 3 | assert False 4 | except TypeError: 5 | x = 5 6 | assert x == 5 7 | -------------------------------------------------------------------------------- /programs/testtuples9.py: -------------------------------------------------------------------------------- 1 | assert (5,6)[0] == 5 2 | assert (5,6)[1] == 6 3 | assert (5,6)[-1] == 6 4 | assert (5,6)[-2] == 5 5 | -------------------------------------------------------------------------------- /programs/testwhile1.py: -------------------------------------------------------------------------------- 1 | x = 3 2 | while x > 0: 3 | x = x - 1 4 | else: 5 | y = 6 6 | assert x == 0 7 | assert y == 6 8 | -------------------------------------------------------------------------------- /programs/politz/simple-append.py: -------------------------------------------------------------------------------- 1 | l = [] 2 | l.append(4) 3 | ___assertEqual(l, [4]) 4 | l.append(5) 5 | ___assertEqual(l, [4,5]) 6 | -------------------------------------------------------------------------------- /programs/politz/tuple-length.py: -------------------------------------------------------------------------------- 1 | ___assertEqual(len(()), 0) 2 | ___assertEqual(len((0,)), 1) 3 | ___assertEqual(len((0, 1, 2)), 3) 4 | -------------------------------------------------------------------------------- /programs/smeding/exec-basic-trace.py: -------------------------------------------------------------------------------- 1 | # check order of execution for exec statement 2 | 3 | exec(trace(0,"pass"), trace(1,{})) 4 | -------------------------------------------------------------------------------- /programs/smeding/imported/imported_module_override_class.py: -------------------------------------------------------------------------------- 1 | # this file is imported by a test in imports 2 | 3 | __class__ = 0 4 | -------------------------------------------------------------------------------- /programs/smeding/int-as-super.py: -------------------------------------------------------------------------------- 1 | # one can inherit from integers 2 | 3 | class A(int) : 4 | pass 5 | 6 | assertTrue(A() == 0) 7 | -------------------------------------------------------------------------------- /programs/testbools7.py: -------------------------------------------------------------------------------- 1 | class A: 2 | def __lt__(self, other): 3 | return True 4 | 5 | assert A() < A() 6 | assert A() > A() 7 | -------------------------------------------------------------------------------- /programs/testfunctions11.py: -------------------------------------------------------------------------------- 1 | def f(a, b, c, d, e, g, h=4, i=5): assert g == 6 and h == 4 and i == 5 2 | 3 | f(1, 2, 3, 4, 5, 6) 4 | -------------------------------------------------------------------------------- /programs/politz/lambda3.py: -------------------------------------------------------------------------------- 1 | f3 = lambda x: lambda y: global_x + y 2 | global_x = 1 3 | inc = f3(None) 4 | ___assertEqual(inc(2), 3) 5 | -------------------------------------------------------------------------------- /programs/smeding/bool-as-super.py: -------------------------------------------------------------------------------- 1 | # bool cannot be a supertype 2 | 3 | expectException(TypeError) 4 | class Spam(bool) : 5 | pass 6 | -------------------------------------------------------------------------------- /programs/testbytes2.py: -------------------------------------------------------------------------------- 1 | assert b"f" * 5 == b"fffff" == b"f" . __mul__(5) == b"f" . __rmul__(5) 2 | assert bytes(5) == b"\0\0\0\0\0" 3 | -------------------------------------------------------------------------------- /programs/testfor1.py: -------------------------------------------------------------------------------- 1 | y = 0 2 | for x in (5,6): 3 | y = y + 1 4 | else: 5 | assert x == 6 and y == 2 6 | z = 1 7 | assert z == 1 8 | -------------------------------------------------------------------------------- /programs/testlists10.py: -------------------------------------------------------------------------------- 1 | x = [1, 2, 3] 2 | x.append(4) 3 | assert x == [1, 2, 3, 4] 4 | x.append([]) 5 | assert x == [1, 2, 3, 4, []] 6 | -------------------------------------------------------------------------------- /programs/testscope1.py: -------------------------------------------------------------------------------- 1 | x = 5 2 | def a(): 3 | x = 6 4 | def b(): 5 | return x 6 | return b 7 | 8 | assert a()() == 6, a()() 9 | -------------------------------------------------------------------------------- /programs/politz/assert-silent.py: -------------------------------------------------------------------------------- 1 | def failing_function(): 2 | assert(False) 3 | 4 | ___assertRaises(AssertionError, failing_function) 5 | -------------------------------------------------------------------------------- /programs/politz/dict-list.py: -------------------------------------------------------------------------------- 1 | d1 = {} 2 | ___assertEqual(list(d1), []) 3 | d2 = {'a': 1, 'b': 2} 4 | ___assertEqual(['a', 'b'], list(d2)) 5 | -------------------------------------------------------------------------------- /programs/politz/gen-arg.py: -------------------------------------------------------------------------------- 1 | def f(): 2 | n = yield 1 3 | yield n 4 | 5 | g = f() 6 | assert next(g) == 1 7 | assert g.send(2) == 2 8 | -------------------------------------------------------------------------------- /programs/smeding/binop-simple-trace.py: -------------------------------------------------------------------------------- 1 | # simple order of execution for a binary operator 2 | 3 | trace(0,1) + trace(1,0) 4 | trace(2,None) 5 | -------------------------------------------------------------------------------- /programs/smeding/lists-prim-getitem-index-pos.py: -------------------------------------------------------------------------------- 1 | # getting an item from a list by its index 2 | 3 | assertTrue(["a","b","c"][1] == "b") 4 | -------------------------------------------------------------------------------- /programs/testfunctions2.py: -------------------------------------------------------------------------------- 1 | def b(* x): 2 | assert x[0] == 5 and x[1] == 6 and len(x) == 2, x[0] + x[1] 3 | 4 | assert b(5, 6) is None 5 | -------------------------------------------------------------------------------- /programs/testlists8.py: -------------------------------------------------------------------------------- 1 | class A: 2 | def __index__(self): 3 | return 1 4 | 5 | assert [1, 2][A()] == 2 6 | assert (1, 2)[A()] == 2 7 | -------------------------------------------------------------------------------- /programs/testloopfinally9.py: -------------------------------------------------------------------------------- 1 | x = 1 2 | while x > 0: 3 | try: 4 | raise 5 | finally: 6 | y = 2 7 | break 8 | assert y == 2 9 | -------------------------------------------------------------------------------- /programs/teststaticmethod.py: -------------------------------------------------------------------------------- 1 | def a(): pass 2 | 3 | class A: 4 | b = staticmethod(a) 5 | 6 | assert a is A.b is A().b, (a, A.b, A().b) 7 | -------------------------------------------------------------------------------- /programs/politz/dict-clear.py: -------------------------------------------------------------------------------- 1 | d = {1:1, 2:2, 3:3} 2 | d.clear() 3 | ___assertEqual(d, {}) 4 | 5 | ___assertRaises(TypeError, d.clear, None) 6 | -------------------------------------------------------------------------------- /programs/smeding/lists-prim-getitem-index-neg.py: -------------------------------------------------------------------------------- 1 | # getting an item from a list by a negative index 2 | 3 | assertTrue(["a","b","c"][-2] == "b") 4 | -------------------------------------------------------------------------------- /programs/smeding/none-as-super.py: -------------------------------------------------------------------------------- 1 | # Check whether we can derive from none 2 | 3 | expectException(TypeError) 4 | class Foo(None) : 5 | pass 6 | -------------------------------------------------------------------------------- /programs/testexec4.py: -------------------------------------------------------------------------------- 1 | assert exec(""" 2 | x = 5 3 | assert locals()["x"] == 5 4 | assert globals().get("x") is None 5 | """, {}, {}) is None 6 | -------------------------------------------------------------------------------- /programs/testfunctions14.py: -------------------------------------------------------------------------------- 1 | import types 2 | 3 | try: 4 | class A(types.FunctionType): pass 5 | assert False 6 | except TypeError: pass 7 | -------------------------------------------------------------------------------- /programs/testyield6.py: -------------------------------------------------------------------------------- 1 | def a(): 2 | try: 3 | yield 5 4 | return 5 | finally: 6 | yield 6 7 | 8 | assert list(a()) == [5, 6] 9 | -------------------------------------------------------------------------------- /programs/politz/dict-bool.py: -------------------------------------------------------------------------------- 1 | ___assertIs(not {}, True) 2 | ___assertTrue({1: 2}) 3 | ___assertIs(bool({}), False) 4 | ___assertIs(bool({1: 2}), True) 5 | -------------------------------------------------------------------------------- /programs/testbools10.py: -------------------------------------------------------------------------------- 1 | class A: 2 | def __bool__(self): return x 3 | 4 | x = False 5 | assert not bool(A()) 6 | x = True 7 | assert bool(A()) 8 | -------------------------------------------------------------------------------- /programs/testclasses9.py: -------------------------------------------------------------------------------- 1 | class A: 2 | def __init__(self): 3 | return 5 4 | 5 | try: 6 | A() 7 | assert False 8 | except TypeError: pass 9 | -------------------------------------------------------------------------------- /programs/testfunctions13.py: -------------------------------------------------------------------------------- 1 | def a(): pass 2 | assert a.__module__ == "__main__" 3 | del __name__ 4 | def b(): pass 5 | assert b.__module__ is None 6 | -------------------------------------------------------------------------------- /programs/testobject2.py: -------------------------------------------------------------------------------- 1 | class A: pass 2 | a = A() 3 | class B(str): pass 4 | b = B() 5 | a.x = 5 6 | assert a.x == 5 7 | b.x = 5 8 | assert b.x == 5 9 | -------------------------------------------------------------------------------- /programs/teststrings1.py: -------------------------------------------------------------------------------- 1 | assert "5" == "5" 2 | assert not ("5" == "6") 3 | assert "5" .__add__("6") == str.__add__("5", "6") == "5" + "6" == "56" 4 | -------------------------------------------------------------------------------- /programs/testtuples12.py: -------------------------------------------------------------------------------- 1 | assert hash(()) == 3527539, hash(()) 2 | assert hash((1,)) == 3430019387558 3 | assert hash((1,2)) == 3713081631934410656 4 | -------------------------------------------------------------------------------- /programs/politz/except-as.py: -------------------------------------------------------------------------------- 1 | try: 2 | raise Exception('just testing scope') 3 | except Exception as e: 4 | ___assertTrue(isinstance(e, Exception)) 5 | -------------------------------------------------------------------------------- /programs/politz/lambda4.py: -------------------------------------------------------------------------------- 1 | f8 = lambda x, y, z: lambda a, b, c: lambda : z * (b + y) 2 | g = f8(1, 2, 3) 3 | h = g(2, 4, 6) 4 | ___assertEqual(h(), 18) 5 | -------------------------------------------------------------------------------- /programs/politz/tuple-in.py: -------------------------------------------------------------------------------- 1 | t = tuple((1,2,3)) 2 | ___assertTrue(1 in t) 3 | ___assertTrue(2 in t) 4 | ___assertTrue(3 in t) 5 | ___assertFalse(4 in t) 6 | -------------------------------------------------------------------------------- /programs/smeding/imported/imported_instantiation_module.py: -------------------------------------------------------------------------------- 1 | # testing instantiation of objects (or lack thereof) 2 | 3 | def foo(self) : 4 | return self 5 | -------------------------------------------------------------------------------- /programs/testclassmethod.py: -------------------------------------------------------------------------------- 1 | @classmethod 2 | def a(x): assert x is int 3 | 4 | a.__get__(5, int)() 5 | a.__get__(5, None)() 6 | a.__get__(None, int)() 7 | -------------------------------------------------------------------------------- /programs/testeval5.py: -------------------------------------------------------------------------------- 1 | a = {"__builtins__": {}} 2 | assert eval("True", a) is True 3 | assert eval("False", a) is False 4 | assert eval("None", a) is None 5 | -------------------------------------------------------------------------------- /programs/testisinstance2.py: -------------------------------------------------------------------------------- 1 | assert isinstance(5, (int, bool, float)) 2 | assert isinstance(5, (((int,), (bool, float)),)) 3 | assert not isinstance(5, ()) 4 | -------------------------------------------------------------------------------- /programs/testscope9.py: -------------------------------------------------------------------------------- 1 | class A: 2 | def a(self): 3 | assert __class__ is A 4 | 5 | A().a() 6 | 7 | assert "__class__" in A.a.__code__.co_freevars 8 | -------------------------------------------------------------------------------- /programs/politz/lambda1.py: -------------------------------------------------------------------------------- 1 | f1 = lambda x: lambda y: x + y 2 | inc = f1(1) 3 | plus10 = f1(10) 4 | ___assertEqual(inc(1), 2) 5 | ___assertEqual(plus10(5), 15) 6 | -------------------------------------------------------------------------------- /programs/politz/support_exception.py: -------------------------------------------------------------------------------- 1 | # file for importing to test whether import statement will 2 | # raise proper exception. 3 | s = 1 4 | raise TypeError 5 | -------------------------------------------------------------------------------- /programs/smeding/str-as-super.py: -------------------------------------------------------------------------------- 1 | # using str as super type 2 | 3 | class A(str) : 4 | pass 5 | 6 | assertTrue(A("spam") == "spam") 7 | assertTrue(A() == "") 8 | -------------------------------------------------------------------------------- /programs/testfor3.py: -------------------------------------------------------------------------------- 1 | for x in (0, 1, 2, 3): 2 | for y in (4, 5, 6, 7): 3 | if y == 5: 4 | break 5 | assert y <= 5 6 | z = x 7 | assert z == 3 8 | -------------------------------------------------------------------------------- /programs/testmap.py: -------------------------------------------------------------------------------- 1 | assert list(map(5 . __add__, [1, 2, 3, 4])) == [6, 7, 8, 9] 2 | assert list(map(int .__add__, [1, 2, 3, 4], [4, 3, 2, 1])) == [5,5,5,5] 3 | -------------------------------------------------------------------------------- /errno.py: -------------------------------------------------------------------------------- 1 | import errno 2 | 3 | for err in dir(errno): 4 | if err[0] == "E": 5 | print('"errno.' + err + '" |-> ' + str(errno.__dict__[err]) + ' ', end="") 6 | -------------------------------------------------------------------------------- /programs/politz/bool-int.py: -------------------------------------------------------------------------------- 1 | ___assertEqual(int(False), 0) 2 | ___assertIsNot(int(False), False) 3 | ___assertEqual(int(True), 1) 4 | ___assertIsNot(int(True), True) 5 | -------------------------------------------------------------------------------- /programs/politz/elseReturn.py: -------------------------------------------------------------------------------- 1 | def f(): 2 | try: 3 | pass 4 | except: 5 | pass 6 | else: 7 | return 4 8 | 9 | assert(f() == 4) 10 | 11 | -------------------------------------------------------------------------------- /programs/smeding/import-syntax-error.py: -------------------------------------------------------------------------------- 1 | # import a module that has a syntax error 2 | 3 | expectException(SyntaxError) 4 | import imported.imported_syntax_error 5 | -------------------------------------------------------------------------------- /programs/smeding/imported/imported_re_export_imported_B.py: -------------------------------------------------------------------------------- 1 | # this is imported by another imported module that is imported by a test in imports :P 2 | 3 | x = 1 4 | -------------------------------------------------------------------------------- /programs/testcallable.py: -------------------------------------------------------------------------------- 1 | assert not callable(5) 2 | def a(): pass 3 | assert callable(a) 4 | class A: 5 | def __call__(self): pass 6 | 7 | assert callable(A()) 8 | -------------------------------------------------------------------------------- /programs/testintegers4.py: -------------------------------------------------------------------------------- 1 | assert 5 < 6 < 7 < 8 2 | assert 8 > 7 > 6 > 5 3 | assert 5 <= 5 <= 6 4 | assert 6 >= 6 >= 5 5 | assert 5 == 5 == 5 6 | assert 5 != 6 != 7 7 | -------------------------------------------------------------------------------- /programs/testissubclass2.py: -------------------------------------------------------------------------------- 1 | assert issubclass(bool, (int, float, dict)) 2 | assert issubclass(bool, (((int,), (float, dict)),)) 3 | assert not issubclass(bool, ()) 4 | -------------------------------------------------------------------------------- /programs/politz/assign-to-self.py: -------------------------------------------------------------------------------- 1 | class O(object): 2 | def __init__(self): 3 | self = ['why am i a list now'] 4 | 5 | o = O() 6 | assert(type(o) != list) 7 | 8 | -------------------------------------------------------------------------------- /programs/politz/gen-list.py: -------------------------------------------------------------------------------- 1 | def f(): 2 | n = 1 3 | while n < 10: 4 | yield n 5 | n += 1 6 | 7 | assert [x for x in f()] == [1,2,3,4,5,6,7,8,9] 8 | -------------------------------------------------------------------------------- /programs/politz/test-try-except-no-exception.py: -------------------------------------------------------------------------------- 1 | hit_except = False 2 | 3 | try: 4 | pass 5 | except: 6 | hit_except = True 7 | 8 | ___assertFalse(hit_except) 9 | -------------------------------------------------------------------------------- /programs/politz/try-finally-no-exception.py: -------------------------------------------------------------------------------- 1 | hit_finally = False 2 | 3 | try: 4 | pass 5 | finally: 6 | hit_finally = True 7 | 8 | ___assertTrue(hit_finally) 9 | -------------------------------------------------------------------------------- /programs/testassert2.py: -------------------------------------------------------------------------------- 1 | import builtins 2 | builtins.__dict__["__debug__"] = False 3 | try: 4 | assert False 5 | raise SystemError 6 | except AssertionError: pass 7 | -------------------------------------------------------------------------------- /programs/testclasses7.py: -------------------------------------------------------------------------------- 1 | class A(int): 2 | def __new__(cls): 3 | return 5.0 4 | def __init__(self): 5 | assert False 6 | 7 | assert A().__class__ is float 8 | -------------------------------------------------------------------------------- /programs/testhash.py: -------------------------------------------------------------------------------- 1 | assert str.__hash__("5").__class__ is int, str.__hash__("5").__class__ 2 | 3 | class A(str): 4 | __eq__ = 5 5 | 6 | assert A.__hash__ is None 7 | -------------------------------------------------------------------------------- /programs/testtry2.py: -------------------------------------------------------------------------------- 1 | try: 2 | raise 3 | assert False 4 | except RuntimeError: 5 | x = 6 6 | finally: 7 | y = 7 8 | assert x == 6 9 | assert y == 7 10 | -------------------------------------------------------------------------------- /programs/politz/dict-update-iterable.py: -------------------------------------------------------------------------------- 1 | d = {} 2 | d.update([[1, 100]]) 3 | d.update(((2, 20),)) 4 | d.update([(1, 1), (2, 2), (3, 3)]) 5 | ___assertEqual(d, {1:1, 2:2, 3:3}) 6 | -------------------------------------------------------------------------------- /programs/politz/lambda2.py: -------------------------------------------------------------------------------- 1 | f2 = lambda x: (lambda : lambda y: x + y)() 2 | inc = f2(1) 3 | plus10 = f2(10) 4 | ___assertEqual(inc(1), 2) 5 | ___assertEqual(plus10(5), 15) 6 | -------------------------------------------------------------------------------- /programs/politz/test-try-except.py: -------------------------------------------------------------------------------- 1 | hit_except = False 2 | 3 | try: 4 | raise Exception('ahoy!') 5 | except: 6 | hit_except = True 7 | 8 | ___assertTrue(hit_except) 9 | -------------------------------------------------------------------------------- /programs/smeding/exec-type-prog-nostring.py: -------------------------------------------------------------------------------- 1 | # type error should be thrown when something other than a string is executed 2 | 3 | expectException(TypeError) 4 | exec(1, {}) 5 | -------------------------------------------------------------------------------- /programs/smeding/lists-prim-getitem-type.py: -------------------------------------------------------------------------------- 1 | # getitem for lists only accepts integers and slices 2 | 3 | expectException(TypeError) 4 | ["x", "foo", "bar"].__getitem__("1") 5 | -------------------------------------------------------------------------------- /programs/testlists9.py: -------------------------------------------------------------------------------- 1 | x = [1, 2, 3] 2 | x.extend((4, 5, 6)) 3 | assert x == [1, 2, 3, 4, 5, 6] 4 | try: 5 | x.extend(5) 6 | except TypeError: 7 | y = 5 8 | assert y == 5 9 | -------------------------------------------------------------------------------- /programs/testsuper2.py: -------------------------------------------------------------------------------- 1 | try: 2 | super(int, "5") 3 | except TypeError: 4 | try: 5 | super("5") 6 | except TypeError: 7 | x = 5 8 | 9 | assert x == 5 10 | -------------------------------------------------------------------------------- /programs/testtry7.py: -------------------------------------------------------------------------------- 1 | try: 2 | try: 3 | raise 4 | assert False 5 | except a: 6 | assert False 7 | except NameError: 8 | x = 9 9 | assert x == 9 10 | -------------------------------------------------------------------------------- /programs/politz/bool-float.py: -------------------------------------------------------------------------------- 1 | ___assertEqual(float(False), 0.0) 2 | ___assertIsNot(float(False), False) 3 | ___assertEqual(float(True), 1.0) 4 | ___assertIsNot(float(True), True) 5 | -------------------------------------------------------------------------------- /programs/politz/simple-extend.py: -------------------------------------------------------------------------------- 1 | l = [] 2 | l.extend([]) 3 | ___assertEqual(l, []) 4 | l.extend([4]) 5 | ___assertEqual(l, [4]) 6 | l.extend([5, 6]) 7 | ___assertEqual(l, [4,5,6]) 8 | -------------------------------------------------------------------------------- /programs/politz/test_in_modules.py: -------------------------------------------------------------------------------- 1 | # When import a module 2 | # the module will be as to sys.modules 3 | import sys 4 | import support 5 | 6 | ___assertIn("support", sys.modules) 7 | -------------------------------------------------------------------------------- /programs/smeding/class-scope-recursive.py: -------------------------------------------------------------------------------- 1 | # Classes cannot refer to themselves in their function body 2 | 3 | expectException(NameError) 4 | 5 | class A(object) : 6 | a = A() 7 | -------------------------------------------------------------------------------- /programs/smeding/exec-type-prog-childofstr.py: -------------------------------------------------------------------------------- 1 | # see if a subtype of string can be used in exec 2 | 3 | class A(str) : 4 | pass 5 | 6 | exec(A("foo(0, None)"), {"foo" : trace}) 7 | -------------------------------------------------------------------------------- /programs/smeding/instantiation-lambda.py: -------------------------------------------------------------------------------- 1 | setDescr("Function with lambda is not instantiated") 2 | 3 | class A(object) : 4 | f = lambda self: True 5 | 6 | assertTrue(A().f()) 7 | -------------------------------------------------------------------------------- /programs/smeding/lists-iter-empty.py: -------------------------------------------------------------------------------- 1 | # check if a valid iterator is returned for an empty list 2 | 3 | it = [].__iter__() 4 | 5 | expectException(StopIteration) 6 | next(it) 7 | -------------------------------------------------------------------------------- /programs/testassign8.py: -------------------------------------------------------------------------------- 1 | a, b, * c, x, y, z = (1, 2, 3, 4, 5, 6, 7, 8, 9) 2 | assert a == 1 and b == 2 and x == 7 and y == 8 and z == 9 and c == [3, 4, 5, 6], (c, [3, 4, 5, 6]) 3 | -------------------------------------------------------------------------------- /programs/politz/dict-values.py: -------------------------------------------------------------------------------- 1 | d = {} 2 | ___assertEqual(set(d.values()), set()) 3 | d = {1:2} 4 | ___assertEqual(set(d.values()), {2}) 5 | ___assertRaises(TypeError, d.values, None) 6 | -------------------------------------------------------------------------------- /programs/politz/invalid-reraise.py: -------------------------------------------------------------------------------- 1 | try: 2 | raise 3 | except RuntimeError as e: 4 | ___assertIn("No active exception", str(e)) 5 | else: 6 | ___fail("No exception raised") 7 | -------------------------------------------------------------------------------- /programs/politz/test-assign-field.py: -------------------------------------------------------------------------------- 1 | class A: pass 2 | a = A() 3 | f = lambda x: x 4 | f(a).x = 1 # this assignment does not work in current implementation 5 | ___assertEqual(a.x, 1) 6 | -------------------------------------------------------------------------------- /programs/smeding/function-scope-del-self.py: -------------------------------------------------------------------------------- 1 | # a function can not delete the reference to itself 2 | 3 | def foo() : 4 | del foo 5 | 6 | expectException(UnboundLocalError) 7 | foo() 8 | -------------------------------------------------------------------------------- /programs/smeding/if-else-scope-inh.py: -------------------------------------------------------------------------------- 1 | setDescr("Refering to a variable defined before the statement should work") 2 | 3 | a = True 4 | 5 | if a : 6 | pass 7 | else : 8 | fail() 9 | -------------------------------------------------------------------------------- /programs/smeding/lists-prim-getitem-index-large.py: -------------------------------------------------------------------------------- 1 | # getting an item from an index with an index that is too large 2 | 3 | expectException(IndexError) 4 | ["a","b","c"].__getitem__(3) 5 | -------------------------------------------------------------------------------- /programs/politz/dict-items.py: -------------------------------------------------------------------------------- 1 | d = {} 2 | ___assertEqual(set(d.items()), set()) 3 | 4 | d = {1:2} 5 | ___assertEqual(set(d.items()), {(1, 2)}) 6 | ___assertRaises(TypeError, d.items, None) 7 | -------------------------------------------------------------------------------- /programs/politz/nested-classes.py: -------------------------------------------------------------------------------- 1 | 2 | try: 3 | class C(object): 4 | x = 4 5 | class D(object): 6 | y = x 7 | except: 8 | assert True 9 | else: 10 | assert False 11 | 12 | -------------------------------------------------------------------------------- /programs/smeding/for-type-notiterable.py: -------------------------------------------------------------------------------- 1 | # check what error is thrown when a for list is used for a non-iterable list 2 | 3 | expectException(TypeError) 4 | 5 | for i in 1 : 6 | pass 7 | -------------------------------------------------------------------------------- /programs/smeding/lists-as-super.py: -------------------------------------------------------------------------------- 1 | # check if lists can be used as base types 2 | 3 | class A(list) : 4 | pass 5 | 6 | a = A() 7 | a.append(1) 8 | 9 | assertTrue(a[0] == 1) 10 | -------------------------------------------------------------------------------- /programs/smeding/lists-prim-getitem-index-small.py: -------------------------------------------------------------------------------- 1 | # getting an item from an index with an index that is too small 2 | 3 | expectException(IndexError) 4 | ["a","b","c"].__getitem__(-5) 5 | -------------------------------------------------------------------------------- /programs/smeding/object-new-noinit.py: -------------------------------------------------------------------------------- 1 | # object's constructor __new__ does not call __init__ 2 | 3 | class A(object) : 4 | def __init__(self) : 5 | fail() 6 | 7 | A.__new__(A) 8 | -------------------------------------------------------------------------------- /programs/smeding/try-finally-scope-noblock.py: -------------------------------------------------------------------------------- 1 | # the statements in the finally clause do not constitute a block 2 | 3 | try : 4 | pass 5 | finally : 6 | x = True 7 | 8 | assertTrue(x) 9 | -------------------------------------------------------------------------------- /programs/testloopfinally7.py: -------------------------------------------------------------------------------- 1 | y = 2 2 | for x in (3, 4, 5): 3 | try: 4 | continue 5 | assert False 6 | finally: 7 | y = y + 1 8 | assert False 9 | assert y == 5 10 | -------------------------------------------------------------------------------- /programs/politz/continue.py: -------------------------------------------------------------------------------- 1 | l = [] 2 | n = 0 3 | while n < 10: 4 | n += 1 5 | if n % 2 == 0: 6 | continue 7 | l.append(n) 8 | 9 | ___assertEqual(l, [1,3,5,7,9]) 10 | -------------------------------------------------------------------------------- /programs/smeding/class-scope-classattr.py: -------------------------------------------------------------------------------- 1 | setDescr(" Couldn't access an internal class after declaration") 2 | 3 | class A(object) : 4 | class B(object) : 5 | x = 1 6 | y = B.x 7 | -------------------------------------------------------------------------------- /programs/smeding/function-call-basic-noargs.py: -------------------------------------------------------------------------------- 1 | # call without arguments 2 | 3 | setDescr("Funcion call without arguments") 4 | 5 | def foo() : 6 | return 1 7 | assertTrue(foo() == 1) 8 | 9 | -------------------------------------------------------------------------------- /programs/testbools2.py: -------------------------------------------------------------------------------- 1 | assert True .__class__ is False .__class__ is bool 2 | assert bool.__class__ is type, bool.__class__ 3 | assert bool.__class__ == type 4 | assert bool.__name__ == "bool" 5 | -------------------------------------------------------------------------------- /programs/testfor4.py: -------------------------------------------------------------------------------- 1 | for x in (1, 2, 3, 4): 2 | for y in (5, 6, 7, 8): 3 | if y == 6: continue 4 | assert y != 6 5 | if y == 7: 6 | z = 4 7 | z = z + 1 8 | assert z == 5 9 | -------------------------------------------------------------------------------- /programs/testif.py: -------------------------------------------------------------------------------- 1 | if True: assert True 2 | else: assert False 3 | 4 | if False: assert False 5 | else: assert True 6 | 7 | assert False if False else True 8 | assert True if True else False 9 | -------------------------------------------------------------------------------- /programs/testio8.py: -------------------------------------------------------------------------------- 1 | import _io 2 | i = _io.FileIO("programs/files/FileIO") 3 | try: 4 | i.readinto(5) 5 | assert False 6 | except TypeError: pass 7 | assert i.readinto(bytearray(4)) == 4 8 | -------------------------------------------------------------------------------- /programs/testloopfinally6.py: -------------------------------------------------------------------------------- 1 | for x in (2, 3, 4): 2 | try: 3 | break 4 | assert False 5 | finally: 6 | y = 6 7 | assert x == 2 8 | assert False 9 | assert y == 6 10 | -------------------------------------------------------------------------------- /programs/teststrings10.py: -------------------------------------------------------------------------------- 1 | class A: 2 | def __str__(self): return "foo" 3 | 4 | assert str(A()) == "foo" 5 | assert str() == "" 6 | assert str(True) == "True" 7 | assert str(False) == "False" 8 | -------------------------------------------------------------------------------- /programs/politz/test_simple_strings.py: -------------------------------------------------------------------------------- 1 | if len('') != 0: raise Exception('len(\'\')') 2 | if len('a') != 1: raise Exception('len(\'a\')') 3 | if len('abcdef') != 6: raise Exception('len(\'abcdef\')') 4 | -------------------------------------------------------------------------------- /programs/smeding/class-scope-simple.py: -------------------------------------------------------------------------------- 1 | setDescr("Could not refer to variable bound outside class definition") 2 | 3 | a = True 4 | class A(object) : 5 | x = a 6 | 7 | assertTrue(A().x) 8 | 9 | -------------------------------------------------------------------------------- /programs/testdecorator.py: -------------------------------------------------------------------------------- 1 | def a(x): 2 | return lambda: x() + x() 3 | 4 | def b(x): 5 | return lambda: x() - 2 6 | 7 | @a 8 | @b 9 | def c(): 10 | return 5 11 | 12 | assert c() == 6 13 | -------------------------------------------------------------------------------- /programs/testlists6.py: -------------------------------------------------------------------------------- 1 | x = iter([5,6]) 2 | assert x.__next__() == 5 3 | assert x.__next__() == 6 4 | try: 5 | assert x.__next__() and False 6 | except StopIteration: 7 | y = 5 8 | assert y == 5 9 | -------------------------------------------------------------------------------- /programs/testloopfinally2.py: -------------------------------------------------------------------------------- 1 | x = 6 2 | while x > 0: 3 | try: 4 | break 5 | assert False 6 | finally: 7 | y = 7 8 | assert x == 6 9 | assert False 10 | assert y == 7 11 | -------------------------------------------------------------------------------- /programs/testrange.py: -------------------------------------------------------------------------------- 1 | assert list(range(10)) == list(range(0,10)) == list(range(0,10,1)) == [0,1,2,3,4,5,6,7,8,9] 2 | assert list(range(10,0)) == [] 3 | assert list(range(10,0,-2)) == [10,8,6,4,2] 4 | -------------------------------------------------------------------------------- /programs/teststrings3.py: -------------------------------------------------------------------------------- 1 | assert "5" .__class__ is str 2 | assert str .__class__ is type 3 | assert str.__name__ == "str" 4 | assert hash("hello world") == 4477797451044935522, hash("hello world") 5 | -------------------------------------------------------------------------------- /programs/testwhile3.py: -------------------------------------------------------------------------------- 1 | x = 3 2 | while x > 0: 3 | x = x - 1 4 | y = 3 5 | while y > 0: 6 | y = y - 1 7 | if y == 2: break 8 | assert y >= 2 9 | z = x 10 | assert z == 0 11 | -------------------------------------------------------------------------------- /programs/politz/bitwise_test.py: -------------------------------------------------------------------------------- 1 | ___assertEqual(~1, -2) 2 | ___assertEqual(3 & 2, 2) 3 | ___assertEqual(1 | 2, 3) 4 | ___assertEqual(1 ^ 2, 3) 5 | ___assertEqual(1 << 3, 8) 6 | ___assertEqual(64 >> 3, 8) 7 | -------------------------------------------------------------------------------- /programs/politz/gen-in-gen.py: -------------------------------------------------------------------------------- 1 | def grange(n): 2 | for i in range(n): 3 | yield i 4 | 5 | def outergen(n): 6 | for i in grange(n): 7 | yield i 8 | 9 | ___assertEqual(list(outergen(10)), list(range(10))) -------------------------------------------------------------------------------- /programs/politz/test_pow.py: -------------------------------------------------------------------------------- 1 | ___assertEqual(0**17, 0) 2 | ___assertEqual(17**0, 1) 3 | ___assertEqual(0**0, 1) 4 | ___assertEqual(17**1, 17) 5 | ___assertEqual(2**10, 1024) 6 | ___assertEqual(2**-2, 0.25) 7 | -------------------------------------------------------------------------------- /programs/politz/type-dict.py: -------------------------------------------------------------------------------- 1 | d = {'a': 1, 'm':lambda self: self} 2 | 3 | C = type("C", (object,), d) 4 | 5 | assert C.a == 1 6 | assert hasattr(C, 'm') 7 | 8 | c=C() 9 | assert c.m() == c 10 | -------------------------------------------------------------------------------- /programs/smeding/class-scope-recursive-inh.py: -------------------------------------------------------------------------------- 1 | setDescr("a class cannot derive from itself because it is not in scope") 2 | 3 | try : 4 | class A(A) : 5 | fail() 6 | except NameError as n : 7 | pass 8 | -------------------------------------------------------------------------------- /programs/smeding/function-call-basic.py: -------------------------------------------------------------------------------- 1 | # call with multiple simple arguments 2 | 3 | setDescr("Funcion call with two arguments") 4 | 5 | def foo(a,b) : 6 | return a+b 7 | assertTrue(3 == foo(1,2)) 8 | -------------------------------------------------------------------------------- /programs/testclasses2.py: -------------------------------------------------------------------------------- 1 | class M(type): pass 2 | 3 | class A(metaclass=M): pass 4 | 5 | assert A.__class__ is M 6 | 7 | class B(A, metaclass=type): pass 8 | 9 | assert B.__class__ is M 10 | 11 | -------------------------------------------------------------------------------- /programs/testtry14.py: -------------------------------------------------------------------------------- 1 | try: 2 | raise TypeError 3 | except TypeError as e: 4 | e 5 | try: 6 | raise TypeError 7 | except TypeError as e: 8 | e 9 | x = 5 10 | assert x == 5 11 | -------------------------------------------------------------------------------- /programs/testwith4.py: -------------------------------------------------------------------------------- 1 | class A: 2 | def __enter__(self): pass 3 | def __exit__(sef, type, value, traceback): 4 | return False 5 | 6 | def a(): 7 | with A(): 8 | pass 9 | 10 | a() 11 | -------------------------------------------------------------------------------- /programs/testfor2.py: -------------------------------------------------------------------------------- 1 | for x in (0, 1, 2, 3, 4): 2 | if x == 2: 3 | continue 4 | assert x != 2 5 | if x == 3: 6 | break 7 | assert x <= 3 8 | else: 9 | assert False 10 | assert x == 3 11 | -------------------------------------------------------------------------------- /programs/testfunctions5.py: -------------------------------------------------------------------------------- 1 | def e(*, x, y): assert x == 5 and y == 6 2 | 3 | try: 4 | e() 5 | assert False 6 | except TypeError: 7 | x = 8 8 | assert x == 8 9 | 10 | assert e(x=5,y=6) is None 11 | -------------------------------------------------------------------------------- /programs/testloopfinally3.py: -------------------------------------------------------------------------------- 1 | x = 3 2 | y = 1 3 | while x > 0: 4 | x = x - 1 5 | try: 6 | continue 7 | assert False 8 | finally: 9 | y = y + 1 10 | assert False 11 | assert y == 4 12 | -------------------------------------------------------------------------------- /programs/testloopfinally4.py: -------------------------------------------------------------------------------- 1 | x = 8 2 | while x > 0: 3 | try: 4 | x = x - 1 5 | finally: 6 | break 7 | assert False 8 | assert False 9 | else: 10 | assert False 11 | assert x == 7 12 | -------------------------------------------------------------------------------- /programs/testloopfinally8.py: -------------------------------------------------------------------------------- 1 | for x in (4, 5, 6): 2 | try: 3 | y = x - 1 4 | finally: 5 | break 6 | assert False 7 | assert False 8 | else: 9 | assert False 10 | assert y == 3 11 | -------------------------------------------------------------------------------- /programs/teststrings5.py: -------------------------------------------------------------------------------- 1 | assert "5,6,7".rsplit(",",0) == ["5,6,7"], "5,6,7".rsplit(",",0) 2 | assert "5,6,7".rsplit(",",1) == ["5,6", "7"] 3 | assert "5,6,7,8,9,0".rsplit(",",-1) == ["5","6","7","8","9","0"] 4 | -------------------------------------------------------------------------------- /programs/politz/dict-keys.py: -------------------------------------------------------------------------------- 1 | d = {} 2 | ___assertEqual(set(d.keys()), set()) 3 | d = {'a': 1, 'b': 2} 4 | k = d.keys() 5 | ___assertIn('a', d) 6 | ___assertIn('b', d) 7 | ___assertRaises(TypeError, d.keys, None) 8 | -------------------------------------------------------------------------------- /programs/politz/dict-set-comprehensions.py: -------------------------------------------------------------------------------- 1 | sc = {x for x in (1, 2, 2, 3)} 2 | s = {1, 2, 3} 3 | ___assertEqual(sc, s) 4 | 5 | dc = {x: x + 1 for x in range(3)} 6 | d = {0:1, 1:2, 2:3} 7 | ___assertEqual(dc, d) 8 | -------------------------------------------------------------------------------- /programs/smeding/class-scope-nested-super.py: -------------------------------------------------------------------------------- 1 | # a nested class cannot refer to the outer class' members 2 | # but the superclass expression can 3 | 4 | class A(object) : 5 | x = object 6 | class B(x) : 7 | pass 8 | -------------------------------------------------------------------------------- /programs/testassert1.py: -------------------------------------------------------------------------------- 1 | assert True # should succeed 2 | try: 3 | assert False # should raise AssertionError 4 | raise TypeError # should not execute 5 | except AssertionError: 6 | x = 5 7 | assert x == 5 8 | -------------------------------------------------------------------------------- /programs/testassign10.py: -------------------------------------------------------------------------------- 1 | class B: pass 2 | b = B() 3 | 4 | a, * b.x, c = (1, 2, 3, 4) 5 | assert b.x == [2, 3] 6 | 7 | b = [1, 2, 3, 4] 8 | a, * b[2], c = (5, 6, 7, 8) 9 | assert b == [1, 2, [6, 7], 4] 10 | -------------------------------------------------------------------------------- /programs/testio6.py: -------------------------------------------------------------------------------- 1 | import _io 2 | i = _io.FileIO("programs/files/FileIO") 3 | _io._IOBase.close(i) 4 | assert _io._IOBase.closed.__get__(i, _io._IOBase) 5 | assert not i.closed 6 | i.close() 7 | assert i.closed 8 | -------------------------------------------------------------------------------- /programs/testnone.py: -------------------------------------------------------------------------------- 1 | assert None.__ne__(5) 2 | assert None != 5 3 | 4 | try: 5 | class A(None.__class__): pass 6 | assert False 7 | except TypeError: pass 8 | 9 | assert None.__class__() is None 10 | -------------------------------------------------------------------------------- /programs/testobject3.py: -------------------------------------------------------------------------------- 1 | a = object() 2 | b = str() 3 | try: 4 | a.x = 5 5 | assert False 6 | except AttributeError: 7 | try: 8 | b.x = 5 9 | assert False 10 | except AttributeError: pass 11 | -------------------------------------------------------------------------------- /programs/testtry10.py: -------------------------------------------------------------------------------- 1 | try: 2 | try: 3 | raise 4 | assert False 5 | finally: 6 | x = 11 7 | assert False 8 | except RuntimeError: 9 | y = 12 10 | assert x == 11 11 | assert y == 12 12 | -------------------------------------------------------------------------------- /programs/politz/non_tuple_starag.py: -------------------------------------------------------------------------------- 1 | def f(a, b, c): 2 | return (a, b, c) 3 | 4 | ___assertEqual(f(*[0, 1, 2]), (0, 1, 2)) 5 | ___assertEqual(f(*"abc"), ('a', 'b', 'c')) 6 | ___assertEqual(f(*range(3)), (0, 1, 2)) 7 | -------------------------------------------------------------------------------- /programs/politz/varargs.py: -------------------------------------------------------------------------------- 1 | 2 | def f(*a): 3 | return 0 4 | ___assertEqual(f(), 0) 5 | ___assertEqual(f(1, 2, 3), 0) 6 | 7 | f = lambda *a: 0 8 | ___assertEqual(f(), 0) 9 | ___assertEqual(f(4, 5, 6), 0) 10 | -------------------------------------------------------------------------------- /programs/smeding/import-module-in-module.py: -------------------------------------------------------------------------------- 1 | # one can not import a module inside another module 2 | 3 | expectException(ImportError) 4 | import imported.imported_module_in_module_A.imported.imported_module_in_module_B 5 | -------------------------------------------------------------------------------- /programs/smeding/imported/imported_re_export_imported_A.py: -------------------------------------------------------------------------------- 1 | # this is imported by a test in imports 2 | 3 | # import another module to show it will still be in schope 4 | import imported.imported_re_export_imported_B 5 | -------------------------------------------------------------------------------- /programs/smeding/instantiation-module.py: -------------------------------------------------------------------------------- 1 | # module members are not instantiated 2 | 3 | import imported.imported_instantiation_module 4 | 5 | expectException(TypeError) 6 | imported.imported_instantiation_module.foo() 7 | -------------------------------------------------------------------------------- /programs/smeding/instantiation-type-simple.py: -------------------------------------------------------------------------------- 1 | # class functions can only be instantiated with an instance of the class 2 | 3 | class A(object) : 4 | def foo(self) : 5 | return 1 6 | 7 | assertTrue(A.foo(1) == 1) 8 | -------------------------------------------------------------------------------- /programs/testfuncfinally4.py: -------------------------------------------------------------------------------- 1 | def f(): 2 | try: 3 | raise ValueError 4 | finally: 5 | return 5 6 | try: 7 | assert f() == 5 8 | raise 9 | except RuntimeError: 10 | x = 5 11 | assert x == 5 12 | -------------------------------------------------------------------------------- /programs/testiter2.py: -------------------------------------------------------------------------------- 1 | class A: 2 | def __iter__(self): 3 | global x 4 | assert x == 0 5 | x += 1 6 | return self 7 | def __next__(self): return 5 8 | 9 | x = 0 10 | a = A() 11 | iter(a) 12 | -------------------------------------------------------------------------------- /programs/politz/nested-reraise.py: -------------------------------------------------------------------------------- 1 | def nested_reraise(): 2 | raise 3 | def reraise(): 4 | try: 5 | raise TypeError("foo") 6 | except: 7 | nested_reraise() 8 | ___assertRaises(TypeError, reraise) 9 | -------------------------------------------------------------------------------- /programs/smeding/class-delattr-instdef.py: -------------------------------------------------------------------------------- 1 | # Deleting an instance attribute 2 | 3 | class A(object) : 4 | pass 5 | 6 | a = A() 7 | a.x = 1 8 | 9 | del a.x 10 | 11 | expectException(AttributeError) 12 | a.x 13 | -------------------------------------------------------------------------------- /programs/smeding/dictionary-constr-withinst.py: -------------------------------------------------------------------------------- 1 | # when constructing a new dictionary with an instance, the instance is not shared 2 | 3 | a = {"x" : 1} 4 | b = dict(a) 5 | 6 | b["x"] = 2 7 | 8 | assertTrue(a["x"] == 1) 9 | -------------------------------------------------------------------------------- /programs/smeding/import-self.py: -------------------------------------------------------------------------------- 1 | # importing oneself does not actually create a loop 2 | 3 | # cant implement this python attribute because it's a test 4 | expectException(AttributeError) 5 | import imported.imported_self 6 | -------------------------------------------------------------------------------- /programs/smeding/op-type-add-noattr.py: -------------------------------------------------------------------------------- 1 | # check wether a TypeError is raised if + is used for an object without 2 | # the __add__ attribute 3 | 4 | class A(object) : 5 | pass 6 | 7 | expectException(TypeError) 8 | A() + 1 9 | -------------------------------------------------------------------------------- /programs/testformat1.py: -------------------------------------------------------------------------------- 1 | class A: 2 | def __format__(self, spec): 3 | assert spec == ":!.[]rsa123", spec 4 | return "" 5 | 6 | assert format(A(), ":!.[]rsa123") == "" 7 | assert "{::!.[]rsa123}".format(A()) == "" 8 | -------------------------------------------------------------------------------- /programs/testscope8.py: -------------------------------------------------------------------------------- 1 | try: 2 | del x 3 | except NameError: 4 | def a(): 5 | x = 5 6 | del x 7 | del x 8 | try: 9 | a() 10 | except UnboundLocalError: 11 | y = 5 12 | 13 | assert y == 5 14 | -------------------------------------------------------------------------------- /programs/politz/dict-contains.py: -------------------------------------------------------------------------------- 1 | d = {} 2 | ___assertNotIn('a', d) 3 | ___assertFalse('a' in d) 4 | ___assertTrue('a' not in d) 5 | d = {'a': 1, 'b': 2} 6 | ___assertIn('a', d) 7 | ___assertIn('b', d) 8 | ___assertNotIn('c', d) 9 | -------------------------------------------------------------------------------- /programs/politz/test-builtin-scope-iter.py: -------------------------------------------------------------------------------- 1 | str = "builtin ids can be overwritten without affecting internal operation" 2 | list = [] 3 | for iter in str: 4 | list.append(iter) 5 | ___assertEqual(list, [iter for iter in str]) 6 | -------------------------------------------------------------------------------- /programs/smeding/instantiation-type-inh.py: -------------------------------------------------------------------------------- 1 | # A class function can be instantiated with a subclass 2 | 3 | class A(object) : 4 | def foo(self) : 5 | return 1 6 | 7 | class B(A) : 8 | bar = A.foo 9 | 10 | B().bar() 11 | -------------------------------------------------------------------------------- /programs/testyield14.py: -------------------------------------------------------------------------------- 1 | def a(): 2 | yield 1 3 | next(foo) 4 | 5 | foo = a() 6 | next(foo) 7 | try: 8 | foo.throw(TypeError) 9 | assert False 10 | except ValueError: assert False 11 | except TypeError: pass 12 | -------------------------------------------------------------------------------- /programs/testyield5.py: -------------------------------------------------------------------------------- 1 | def a(): 2 | try: 3 | try: 4 | yield 5 5 | raise TypeError 6 | except TypeError: 7 | yield 6 8 | finally: 9 | yield 7 10 | 11 | assert list(a()) == [5, 6, 7] 12 | -------------------------------------------------------------------------------- /programs/politz/comprehensions-leak.py: -------------------------------------------------------------------------------- 1 | x = 10 2 | l = [x for x in range(5)] 3 | ___assertEqual(x, 10) 4 | 5 | try: 6 | l = [x/0 for x in range(5)] 7 | except: 8 | ___assertEqual(x, 10) 9 | else: 10 | ___fail() 11 | -------------------------------------------------------------------------------- /programs/testfunctions8.py: -------------------------------------------------------------------------------- 1 | x = False 2 | def h(): 3 | x = 0 4 | def b(): 5 | global x 6 | return x 7 | assert b() == False 8 | return b 9 | c = h() 10 | assert c() == False 11 | x = 5 12 | assert c() == 5 13 | -------------------------------------------------------------------------------- /programs/testintegers3.py: -------------------------------------------------------------------------------- 1 | assert -(5) == int.__neg__(5) == 5 .__neg__() == -5 2 | assert -(-5) == 5 3 | assert +5 == int.__pos__(5) == 5 .__pos__() == 5 4 | assert +-5 == -5 5 | assert ~5 == int.__invert__(5) == 5 .__invert__() == -6 6 | -------------------------------------------------------------------------------- /programs/testintegers6.py: -------------------------------------------------------------------------------- 1 | assert 5 .__add__("5") is NotImplemented 2 | assert 5 .__bool__() is True 3 | assert 0 .__bool__() is False 4 | assert 5 .__class__ is int 5 | assert int.__class__ is type 6 | assert int.__name__ == "int" 7 | -------------------------------------------------------------------------------- /programs/testrepr2.py: -------------------------------------------------------------------------------- 1 | class A: 2 | def __init__(self, x): self.x = x 3 | def __repr__(self): return self.x 4 | 5 | assert repr(A("foo")) == "foo" 6 | try: 7 | repr(A(5)) 8 | assert False 9 | except TypeError: pass 10 | -------------------------------------------------------------------------------- /programs/testtry3.py: -------------------------------------------------------------------------------- 1 | try: 2 | try: 3 | raise 4 | except SyntaxError: 5 | assert False 6 | finally: 7 | x = 7 8 | assert False 9 | except RuntimeError: 10 | y = 8 11 | assert x == 7 12 | assert y == 8 13 | -------------------------------------------------------------------------------- /programs/smeding/dictionary-as-super.py: -------------------------------------------------------------------------------- 1 | # the dictionary can be used as superclass, inheriting all its primitives 2 | 3 | class A(dict) : 4 | pass 5 | 6 | a = A() 7 | a["spam"] = "meat" 8 | 9 | assertTrue(a["spam"] == "meat") 10 | -------------------------------------------------------------------------------- /programs/smeding/print-type-nostring.py: -------------------------------------------------------------------------------- 1 | # check whether print raises a type error when doesn't __str__ return a string 2 | 3 | def A(object) : 4 | def __str__(self) : 5 | return 1 6 | 7 | expectException(TypeError) 8 | print((A())) 9 | -------------------------------------------------------------------------------- /programs/smeding/try-finally-break.py: -------------------------------------------------------------------------------- 1 | # the finally clause is also executed when we break from its body 2 | 3 | while True : 4 | try : 5 | break 6 | finally : 7 | trace(0, None) 8 | 9 | trace(1,None) 10 | -------------------------------------------------------------------------------- /programs/testfunctions6.py: -------------------------------------------------------------------------------- 1 | x = True 2 | def f(y=[]): 3 | if x: assert y == [] 4 | else: assert y == [5] 5 | list.__init__(y, [5]) 6 | 7 | assert f() is None 8 | x = False 9 | assert f() is None 10 | assert f([5]) is None 11 | -------------------------------------------------------------------------------- /programs/testtuples10.py: -------------------------------------------------------------------------------- 1 | try: 2 | (5,6)[-3] 3 | assert False 4 | except IndexError: 5 | x = 10 6 | assert x == 10 7 | 8 | try: 9 | (5,6)[2] 10 | assert False 11 | except IndexError: 12 | x = 11 13 | assert x == 11 14 | -------------------------------------------------------------------------------- /programs/testyield10.py: -------------------------------------------------------------------------------- 1 | def a(): 2 | try: 3 | yield 5 4 | except GeneratorExit: 5 | raise TypeError 6 | 7 | b = a() 8 | b.__next__() 9 | try: 10 | b.close() 11 | except TypeError: 12 | x = 5 13 | assert x == 5 14 | -------------------------------------------------------------------------------- /programs/politz/len.py: -------------------------------------------------------------------------------- 1 | ___assertEqual(len('123'), 3) 2 | ___assertEqual(len(()), 0) 3 | ___assertEqual(len((1, 2, 3, 4)), 4) 4 | ___assertEqual(len([1, 2, 3, 4]), 4) 5 | ___assertEqual(len({}), 0) 6 | ___assertEqual(len({'a':1, 'b': 2}), 2) 7 | -------------------------------------------------------------------------------- /programs/politz/support.py: -------------------------------------------------------------------------------- 1 | # support file for other testing file 2 | TESTFN = "generated4test" 3 | 4 | class Foo: 5 | def __init__(self): 6 | self.x1 = "x" 7 | self.x2 = len(self.x1) 8 | 9 | def bar(): 10 | pass 11 | 12 | -------------------------------------------------------------------------------- /programs/politz/test-staticmethod-decorator.py: -------------------------------------------------------------------------------- 1 | class A: 2 | @staticmethod 3 | def sm1(who): 4 | return "Hello " + who 5 | 6 | ___assertEqual(A.sm1("World"), "Hello World") 7 | ___assertEqual(A().sm1("World"), "Hello World") 8 | -------------------------------------------------------------------------------- /programs/politz/test_booleans.py: -------------------------------------------------------------------------------- 1 | if 0 or 0: raise Exception('0 or 0 is true instead of false') 2 | if 1 and 1: pass 3 | else: raise Exception('1 and 1 is false instead of true') 4 | if not 1: raise Exception('not 1 is true instead of false') 5 | -------------------------------------------------------------------------------- /programs/smeding/class-scope-lambda.py: -------------------------------------------------------------------------------- 1 | # see if lambda's have the same scope restrictions as functions have 2 | 3 | x = True 4 | 5 | class A(object) : 6 | x = False 7 | foo = lambda self : x 8 | 9 | assertTrue(A().foo()) 10 | -------------------------------------------------------------------------------- /programs/smeding/exception-basic-catch.py: -------------------------------------------------------------------------------- 1 | # Try Except statements basic functionality 2 | 3 | class TestException(Exception) : 4 | pass 5 | 6 | try : 7 | raise TestException() 8 | fail() 9 | except TestException as e: 10 | pass 11 | -------------------------------------------------------------------------------- /programs/smeding/function-scope-del-inh.py: -------------------------------------------------------------------------------- 1 | # A function cannot delete a variable it inherited from the surrounding block 2 | 3 | x = 1 4 | 5 | def foo() : 6 | del x 7 | 8 | expectException(UnboundLocalError) 9 | 10 | foo() 11 | -------------------------------------------------------------------------------- /programs/smeding/if-else-scope-inh-body.py: -------------------------------------------------------------------------------- 1 | setDescr("Variable defined before if, can be used in the if body") 2 | 3 | a = True 4 | 5 | if True : 6 | a 7 | else : 8 | fail() 9 | 10 | if False : 11 | fail() 12 | else : 13 | a 14 | -------------------------------------------------------------------------------- /programs/smeding/lists-iter-simple.py: -------------------------------------------------------------------------------- 1 | # check basic behaviour of a lists' iterator 2 | 3 | it = [1,2].__iter__() 4 | 5 | assertTrue(next(it) == 1) 6 | assertTrue(next(it) == 2) 7 | 8 | expectException(StopIteration) 9 | next(it) 10 | -------------------------------------------------------------------------------- /programs/testfuncfinally2.py: -------------------------------------------------------------------------------- 1 | def b(): 2 | global x 3 | try: 4 | x = 9 5 | return 5 6 | assert False 7 | finally: 8 | assert x == 9 9 | x = 10 10 | assert False 11 | 12 | assert b() == 5 13 | assert x == 10 14 | -------------------------------------------------------------------------------- /programs/testtry12.py: -------------------------------------------------------------------------------- 1 | try: 2 | try: 3 | raise SyntaxError 4 | except SyntaxError: 5 | try: 6 | raise TypeError 7 | except TypeError: pass 8 | raise 9 | except SyntaxError: 10 | x = 12 11 | assert x == 12 12 | -------------------------------------------------------------------------------- /programs/testwhile4.py: -------------------------------------------------------------------------------- 1 | x = 3 2 | while x > 0: 3 | x = x - 1 4 | y = 4 5 | while y > 0: 6 | y = y - 1 7 | if y == 3: continue 8 | assert y != 3 9 | if y == 2: 10 | z = 2 11 | z = z + 1 12 | assert z == 3 13 | -------------------------------------------------------------------------------- /programs/politz/test-staticmethod-oldstyle.py: -------------------------------------------------------------------------------- 1 | class A: 2 | def sm2(who): 3 | return "Hello " + who 4 | sm2 = staticmethod(sm2) 5 | 6 | ___assertEqual(A.sm2("World"), "Hello World") 7 | ___assertEqual(A().sm2("World"), "Hello World") 8 | -------------------------------------------------------------------------------- /programs/testimport2.py: -------------------------------------------------------------------------------- 1 | import package1.module2 2 | from package1.module2 import * 3 | 4 | assert package1.module2.x == 5 5 | assert package1.module2.y == 5 6 | assert x == 5 7 | try: 8 | y 9 | assert False 10 | except NameError: pass 11 | -------------------------------------------------------------------------------- /programs/testis.py: -------------------------------------------------------------------------------- 1 | assert True is True 2 | assert False is False 3 | assert True is True is True 4 | assert False is False is False 5 | assert not ((False is False is False) is False) 6 | assert True is not False 7 | assert not (True is not True) 8 | -------------------------------------------------------------------------------- /programs/testiter1.py: -------------------------------------------------------------------------------- 1 | def a(): 2 | yield 5 3 | yield 6 4 | 5 | x = iter(a()) 6 | assert next(x) == 5 7 | assert next(x) == 6 8 | try: 9 | next(x) 10 | assert False 11 | except StopIteration: pass 12 | 13 | assert next(x, 7) == 7 14 | -------------------------------------------------------------------------------- /programs/testsys.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | assert sys.path[0][-9:] == "/programs", sys.path[0][-9:] 4 | assert sys.argv[0] == "programs/testsys.py" 5 | assert sys.argv[1] == "one" 6 | assert sys.argv[2] == "two" 7 | assert sys.argv[3] == "three four" 8 | -------------------------------------------------------------------------------- /programs/testtry1.py: -------------------------------------------------------------------------------- 1 | try: 2 | x = 5 3 | except TypeError: 4 | assert False 5 | except SyntaxError as y: 6 | assert False 7 | else: 8 | y = 6 9 | assert x == 5 10 | finally: 11 | z = 7 12 | assert y == 6 13 | assert z == 7 14 | -------------------------------------------------------------------------------- /programs/testwhile2.py: -------------------------------------------------------------------------------- 1 | x = 4 2 | y = 7 3 | while x > 0: 4 | x = x - 1 5 | if x == 3: 6 | continue 7 | assert x != 3 8 | if x == 2: 9 | break 10 | assert x >= 2 11 | else: 12 | assert False 13 | assert y == 7 and x == 2 14 | -------------------------------------------------------------------------------- /fake-importlib.py: -------------------------------------------------------------------------------- 1 | def _install(sys_module, _imp_module): 2 | global _imp, sys 3 | _imp = _imp_module 4 | sys = sys_module 5 | 6 | def __import__(name, globals=None, locals=None, fromlist=(), level=0): 7 | return _imp.init_builtin(name) 8 | 9 | -------------------------------------------------------------------------------- /programs/smeding/attr-assign-trace.py: -------------------------------------------------------------------------------- 1 | # trace the order of evaluation of attribute assignments 2 | # the rhs should be evaluated before the lhs! 3 | 4 | class A(object) : 5 | pass 6 | 7 | trace(1,A()).x = trace(0,None) 8 | 9 | trace(2,None) 10 | -------------------------------------------------------------------------------- /programs/smeding/class-delattr-classattr.py: -------------------------------------------------------------------------------- 1 | setDescr("Removing the class' attribute should not be allowed") 2 | 3 | class A(object) : 4 | a = 1 5 | 6 | try : 7 | del A().a 8 | fail() 9 | except AttributeError as e : 10 | pass 11 | -------------------------------------------------------------------------------- /programs/smeding/class-delattr-initdef.py: -------------------------------------------------------------------------------- 1 | # object attributes can be deleted as well 2 | 3 | class A(object) : 4 | def __init__(self) : 5 | self.x = 1 6 | 7 | a = A() 8 | del a.x 9 | 10 | expectException(AttributeError) 11 | a.x 12 | -------------------------------------------------------------------------------- /programs/smeding/subscription-trace-argsfirst.py: -------------------------------------------------------------------------------- 1 | # the arguments of a subscription are evaluated before accessing |__getattribute__| 2 | 3 | try : 4 | trace(0,None)[trace(1,None)] 5 | except TypeError as e: 6 | trace(2,None) 7 | 8 | trace(3,None) 9 | -------------------------------------------------------------------------------- /programs/smeding/try-except-type-catch-nonexcep.py: -------------------------------------------------------------------------------- 1 | # only subtypes of BaseException can catch an exception 2 | 3 | expectException(Exception) 4 | 5 | try : 6 | raise Exception() 7 | except object as e : # Exception is an instance of object 8 | fail() 9 | -------------------------------------------------------------------------------- /programs/testfunctions3.py: -------------------------------------------------------------------------------- 1 | def c(): pass 2 | 3 | try: 4 | c(1) 5 | assert False 6 | except TypeError: 7 | x = 5 8 | assert x == 5 9 | 10 | try: 11 | c(x=5) 12 | assert False 13 | except TypeError: 14 | x = 6 15 | assert x == 6 16 | -------------------------------------------------------------------------------- /programs/testio7.py: -------------------------------------------------------------------------------- 1 | import _io 2 | import errno 3 | i = _io.FileIO("programs/files/FileIO") 4 | i2 = _io.FileIO(i.fileno()) 5 | i.close() 6 | try: 7 | i2.close() 8 | assert False 9 | except OSError as e: 10 | assert e.errno == errno.EBADF 11 | -------------------------------------------------------------------------------- /programs/testlists5.py: -------------------------------------------------------------------------------- 1 | assert list((5,)) == [5] 2 | assert list() == [] 3 | assert [True] == [1] 4 | assert [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] == list([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) 5 | assert not [] == [1] 6 | assert not [1] == [] 7 | assert not [5] == [6] 8 | -------------------------------------------------------------------------------- /programs/testreversed1.py: -------------------------------------------------------------------------------- 1 | x = reversed([2, 1, 0]) 2 | assert x.__next__() == 0 3 | assert x.__next__() == 1 4 | assert x.__next__() == 2 5 | try: 6 | x.__next__() 7 | assert False 8 | except StopIteration: 9 | y = 5 10 | assert y == 5 11 | -------------------------------------------------------------------------------- /programs/politz/bool-convert.py: -------------------------------------------------------------------------------- 1 | ___assertIs(bool(10), True) 2 | ___assertIs(bool(1), True) 3 | ___assertIs(bool(-1), True) 4 | ___assertIs(bool(0), False) 5 | ___assertIs(bool("hello"), True) 6 | ___assertIs(bool(""), False) 7 | ___assertIs(bool(), False) 8 | -------------------------------------------------------------------------------- /programs/politz/bound-and-free.py: -------------------------------------------------------------------------------- 1 | # var is bound and free in class 2 | 3 | def f(x): 4 | class C: 5 | def m(self): 6 | return x 7 | a = x 8 | return C 9 | 10 | inst = f(3)() 11 | ___assertEqual(inst.a, inst.m()) 12 | -------------------------------------------------------------------------------- /programs/politz/dict-attribute.py: -------------------------------------------------------------------------------- 1 | class C: 2 | def __init__(self): 3 | self.a = 1 4 | 5 | assert "__init__" in C.__dict__, "Class C has no __init__ attribute" 6 | 7 | assert "a" in C().__dict__, "instance of class C has no 'a' attribute" 8 | 9 | -------------------------------------------------------------------------------- /programs/politz/simple-gen.py: -------------------------------------------------------------------------------- 1 | def f(): 2 | yield 1 3 | 4 | g = f() 5 | 6 | assert g.__next__() == 1 7 | try: 8 | g.__next__() 9 | except StopIteration: 10 | pass 11 | else: 12 | raise "g.__next__() should have raised StopIteration" 13 | -------------------------------------------------------------------------------- /programs/politz/simple-nesting.py: -------------------------------------------------------------------------------- 1 | def make_adder(x): 2 | def adder(y): 3 | return x + y 4 | return adder 5 | 6 | inc = make_adder(1) 7 | plus10 = make_adder(10) 8 | 9 | ___assertEqual(inc(1), 2) 10 | ___assertEqual(plus10(-2), 8) 11 | -------------------------------------------------------------------------------- /programs/politz/types_truthy2.py: -------------------------------------------------------------------------------- 1 | def f(): pass 2 | class C: pass 3 | x = C() 4 | if not f: raise Exception('f is false instead of true') 5 | if not C: raise Exception('C is false instead of true') 6 | #if not x: raise Exception('x is false instead of true') 7 | -------------------------------------------------------------------------------- /programs/smeding/dict-no-instantiation.py: -------------------------------------------------------------------------------- 1 | # a class function is not type/class specific when it is retrieved from the 2 | # class object's dictionary 3 | 4 | class A(object) : 5 | def foo(x) : 6 | return x 7 | 8 | assertTrue(A.__dict__["foo"](1) == 1) 9 | -------------------------------------------------------------------------------- /programs/smeding/function-call-evalargs-noargs.py: -------------------------------------------------------------------------------- 1 | # call with an incorrect number of arguments 2 | 3 | setDescr("Function with no arguments called with an argument") 4 | 5 | def foo() : 6 | return 1 7 | 8 | expectException(TypeError) 9 | foo(1) 10 | -------------------------------------------------------------------------------- /programs/smeding/instantiation-double.py: -------------------------------------------------------------------------------- 1 | setDescr("An instantiated class member cannot be instantiated again") 2 | 3 | class A(object) : 4 | def f(a,b) : 5 | return b 6 | 7 | class B(object) : 8 | f = A().f 9 | 10 | assertTrue(B().f(1) == 1) 11 | -------------------------------------------------------------------------------- /programs/smeding/try-except-scope-raise.py: -------------------------------------------------------------------------------- 1 | # variable defined in try with raise is accessible in the exception and after try 2 | 3 | try : 4 | a = True 5 | raise Exception() 6 | except Exception as e : 7 | assertTrue(a) 8 | 9 | assertTrue(a) 10 | -------------------------------------------------------------------------------- /programs/politz/shadow.py: -------------------------------------------------------------------------------- 1 | shadowed = False 2 | def property(f): 3 | global shadowed 4 | shadowed = True 5 | return f 6 | 7 | class C(object): 8 | @property 9 | def meth(self): 10 | pass 11 | 12 | C() 13 | ___assertTrue(shadowed) 14 | 15 | -------------------------------------------------------------------------------- /programs/politz/try-except-else-no-exception.py: -------------------------------------------------------------------------------- 1 | hit_except = False 2 | hit_else = False 3 | 4 | try: 5 | pass 6 | except: 7 | hit_except = True 8 | else: 9 | hit_else = True 10 | 11 | ___assertFalse(hit_except) 12 | ___assertTrue(hit_else) 13 | -------------------------------------------------------------------------------- /programs/politz/while-break-finally.py: -------------------------------------------------------------------------------- 1 | chk = 'init' 2 | while True: 3 | try: 4 | chk = 'in loop' 5 | break 6 | chk = 'better-not-be-this' 7 | finally: 8 | if chk == 'in loop': 9 | chk = 100 10 | 11 | ___assertEqual(chk, 100) 12 | -------------------------------------------------------------------------------- /programs/smeding/exec-type-env-childofdict.py: -------------------------------------------------------------------------------- 1 | # see if a descendent of dict can be used for the environment of an exec statement 2 | 3 | class MyEnv(dict) : 4 | pass 5 | 6 | env = MyEnv() 7 | env["foo"] = assertTrue 8 | 9 | exec("foo(True)", env) 10 | -------------------------------------------------------------------------------- /programs/smeding/try-except-scope-uncaughtexception.py: -------------------------------------------------------------------------------- 1 | # Uncaught exception binding is still in scope 2 | 3 | try : 4 | try : 5 | raise Exception() 6 | except None as n : 7 | pass 8 | except Exception as e : 9 | expectException(NameError) 10 | n 11 | -------------------------------------------------------------------------------- /programs/smeding/try-finally-return.py: -------------------------------------------------------------------------------- 1 | # the finally clause is also executed when we return from its body 2 | 3 | def foo() : 4 | try : 5 | return trace(0,None) 6 | finally : 7 | trace(1, None) 8 | 9 | foo() 10 | trace(2,None) 11 | -------------------------------------------------------------------------------- /programs/smeding/type-new-type.py: -------------------------------------------------------------------------------- 1 | # type's constructor new can only be called for types, not objects 2 | 3 | class A(object) : 4 | def __new__(self) : 5 | return type.__new__(self) # should've called object.__new__ 6 | 7 | expectException(TypeError) 8 | A() 9 | -------------------------------------------------------------------------------- /programs/testfuncfinally1.py: -------------------------------------------------------------------------------- 1 | x = 5 2 | 3 | def a(): 4 | global x 5 | assert x == 5 6 | x = 6 7 | return 5 8 | 9 | try: 10 | assert a() == 5 11 | assert x == 6 12 | x = 7 13 | finally: 14 | assert x == 7 15 | x = 8 16 | assert x == 8 17 | -------------------------------------------------------------------------------- /programs/testfuncraise2.py: -------------------------------------------------------------------------------- 1 | def a(): 2 | raise ValueError 3 | 4 | try: 5 | try: 6 | raise 7 | except RuntimeError: 8 | a() 9 | except ValueError as e: 10 | assert e.__context__.__class__ == RuntimeError 11 | x = 5 12 | assert x == 5 13 | -------------------------------------------------------------------------------- /programs/testimport1.py: -------------------------------------------------------------------------------- 1 | import module1 2 | import module1 as foo 3 | from module1 import x 4 | 5 | assert module1.x == 5 6 | assert foo.x == 5 7 | assert x == 5 8 | 9 | try: 10 | from module1 import y 11 | assert False 12 | except ImportError: pass 13 | -------------------------------------------------------------------------------- /programs/testsetattr4.py: -------------------------------------------------------------------------------- 1 | class A: 2 | def __set__(self, instance, value): assert self.__class__ is A and instance is b and value == 5, (self, instance, value); instance.y = 6 3 | 4 | class B: 5 | x = A() 6 | 7 | b = B() 8 | b.x = 5 9 | b.y == 6 10 | -------------------------------------------------------------------------------- /programs/politz/test-finally-reraise.py: -------------------------------------------------------------------------------- 1 | def reraise(): 2 | try: 3 | raise TypeError("foo") 4 | except: 5 | try: 6 | raise KeyError("caught") 7 | finally: 8 | raise 9 | ___assertRaises(KeyError, reraise) 10 | -------------------------------------------------------------------------------- /programs/smeding/function-scope-shadow-self.py: -------------------------------------------------------------------------------- 1 | # override the binding for foo inside the function, thus preventing looping 2 | 3 | setDescr("Function name was not shadowed in the function body.") 4 | 5 | def foo(foo) : 6 | return foo 7 | 8 | assertTrue(1 == foo(1)) 9 | -------------------------------------------------------------------------------- /programs/smeding/lists-iter-append.py: -------------------------------------------------------------------------------- 1 | # check what happens if a list is extended after the iterator has been created 2 | 3 | list = [1] 4 | it = list.__iter__() 5 | 6 | assertTrue(next(it) == 1) 7 | 8 | list.append(2) 9 | 10 | assertTrue(next(it) == 2) 11 | -------------------------------------------------------------------------------- /programs/testaugassign12.py: -------------------------------------------------------------------------------- 1 | class A: 2 | def __ior__(self, other): 3 | global x 4 | x = 5 5 | 6 | c = {"foo" : A()} 7 | def b(): 8 | global x 9 | x += 1 10 | assert x == 1 11 | return c 12 | x = 0 13 | b()["foo"] |= A() 14 | assert x == 5 15 | -------------------------------------------------------------------------------- /programs/testbools6.py: -------------------------------------------------------------------------------- 1 | class A: 2 | def __eq__(self, other): 3 | return self.x == other.x 4 | def __init__(self, x): 5 | self.x = x 6 | 7 | assert A(0) == A(0) 8 | assert not(A(0) == A(1)) 9 | assert A(1) != A(0) 10 | assert not(A(1) != A(1)) 11 | -------------------------------------------------------------------------------- /programs/testlists11.py: -------------------------------------------------------------------------------- 1 | x = [1, 2, 3, 4, 5] 2 | x[0] = "1" 3 | x[-2] = "4" 4 | 5 | assert x == ["1", 2, 3, "4", 5] 6 | try: 7 | x[6] = 4 8 | except IndexError: 9 | try: 10 | x[-10] = 4 11 | except IndexError: 12 | y = 5 13 | 14 | assert y == 5 15 | -------------------------------------------------------------------------------- /programs/teststrings9.py: -------------------------------------------------------------------------------- 1 | assert "foo abc abc abc".partition("abc") == ("foo ", "abc", " abc abc") 2 | assert "foo abc abc abc".partition("def") == ("foo abc abc abc", "", "") 3 | try: 4 | "abc abc abc foo".partition(5) 5 | assert False 6 | except TypeError: pass 7 | -------------------------------------------------------------------------------- /programs/testyield4.py: -------------------------------------------------------------------------------- 1 | def f(): 2 | yield 5 3 | raise ValueError 4 | 5 | g = f() 6 | try: 7 | assert g.__next__() == 5 8 | except ValueError: 9 | assert False 10 | 11 | try: 12 | g.__next__() 13 | except ValueError: 14 | x = 6 15 | assert x == 6 16 | -------------------------------------------------------------------------------- /programs/politz/reraise.py: -------------------------------------------------------------------------------- 1 | try: 2 | try: 3 | raise IndexError() 4 | except IndexError as e: 5 | exc1 = e 6 | raise 7 | except IndexError as exc2: 8 | ___assertTrue(exc1 is exc2) 9 | else: 10 | ___fail("No exception raised") 11 | -------------------------------------------------------------------------------- /programs/politz/test-try-except-else.py: -------------------------------------------------------------------------------- 1 | hit_except = False 2 | hit_else = False 3 | 4 | try: 5 | raise Exception('foo!') 6 | except: 7 | hit_except = True 8 | else: 9 | hit_else = True 10 | 11 | ___assertFalse(hit_else) 12 | ___assertTrue(hit_except) 13 | -------------------------------------------------------------------------------- /programs/smeding/binop-operants-before-operator.py: -------------------------------------------------------------------------------- 1 | # the operants are evaluated before the operator 2 | 3 | class A(object) : 4 | pass 5 | 6 | try : 7 | trace(0,A()) + trace(1,A()) 8 | except TypeError as e: 9 | trace(2,None) 10 | 11 | trace(3,None) 12 | -------------------------------------------------------------------------------- /programs/smeding/exec-context-sensitive.py: -------------------------------------------------------------------------------- 1 | # the parser of the exec statement is not context sensitive 2 | # it won't allow returns in functions that are not part of the string 3 | 4 | def foo() : 5 | exec("return 1", {}) 6 | 7 | expectException(SyntaxError) 8 | foo() 9 | -------------------------------------------------------------------------------- /programs/smeding/try-except-scope-catchtype.py: -------------------------------------------------------------------------------- 1 | # variable defined in try is even accessible in except clause 2 | 3 | setDescr("Catching an exception defined in the try body") 4 | 5 | try : 6 | a = Exception 7 | raise Exception() 8 | except a as e: 9 | pass 10 | -------------------------------------------------------------------------------- /programs/smeding/try-except-scope-caughtexception.py: -------------------------------------------------------------------------------- 1 | setDescr("caught exception is in scope in the exception body and after it") 2 | 3 | try : 4 | raise Exception() 5 | except Exception as e: 6 | e 7 | 8 | try: 9 | e 10 | fail() 11 | except NameError: pass 12 | -------------------------------------------------------------------------------- /programs/smeding/try-except-type-raise-string.py: -------------------------------------------------------------------------------- 1 | # in previous versions (python < 2.5 ?) raising strings was allowed 2 | # TODO: find a safe way to test this 3 | 4 | #expectException(TypeError) 5 | # 6 | #try : 7 | # raise "adsf" 8 | #except str, e : 9 | # fail() 10 | -------------------------------------------------------------------------------- /programs/testexceptions1.py: -------------------------------------------------------------------------------- 1 | i = ImportError(1, 2, 3, name=4, path=5) 2 | 3 | assert i.args == (1, 2, 3) 4 | assert i.name == 4 5 | assert i.path == 5 6 | 7 | try: 8 | ImportError(foo=5) 9 | assert False 10 | except TypeError: 11 | x = 5 12 | assert x == 5 13 | -------------------------------------------------------------------------------- /programs/testgetattr2.py: -------------------------------------------------------------------------------- 1 | class A: 2 | def __get__(self, instance, owner): assert self.__class__ is A and instance is None and owner is B; return 5 3 | 4 | class B: 5 | x = A() 6 | 7 | assert B.x == 5 8 | assert getattr(B, "x") == 5 9 | assert hasattr(B, "x") 10 | -------------------------------------------------------------------------------- /programs/testloopfinally5.py: -------------------------------------------------------------------------------- 1 | try: 2 | for x in (1, 2, 3): 3 | try: 4 | raise 5 | finally: 6 | y = 5 7 | assert x == 1 8 | assert False 9 | assert False 10 | except RuntimeError: 11 | z = 6 12 | assert y == 5 13 | assert z == 6 14 | -------------------------------------------------------------------------------- /programs/teststrings7.py: -------------------------------------------------------------------------------- 1 | assert "12345"[0] == "1" 2 | assert "12345"[4] == "5" 3 | assert "12345"[-1] == "5" 4 | assert "12345"[:] == "12345" 5 | assert "12345"[2:] == "345" 6 | assert "12345"[:2] == "12" 7 | assert "12345"[1:4] == "234" 8 | assert "12345"[::2] == "135" 9 | -------------------------------------------------------------------------------- /programs/testyield3.py: -------------------------------------------------------------------------------- 1 | def e(foo): 2 | assert foo == 5 3 | try: 4 | return 5 | yield 6 | except StopIteration: 7 | assert False 8 | 9 | try: 10 | e(5).__next__() 11 | assert False 12 | except StopIteration: 13 | x = 5 14 | assert x == 5 15 | -------------------------------------------------------------------------------- /programs/testyield8.py: -------------------------------------------------------------------------------- 1 | def a(): 2 | try: 3 | yield 5 4 | assert False 5 | except GeneratorExit: 6 | yield 6 7 | assert False 8 | 9 | b = a() 10 | b.__next__() 11 | try: 12 | b.close() 13 | except RuntimeError: 14 | x = 5 15 | assert x == 5 16 | -------------------------------------------------------------------------------- /programs/politz/try-except-finally-no-exception.py: -------------------------------------------------------------------------------- 1 | hit_except = False 2 | hit_finally = False 3 | 4 | try: 5 | pass 6 | except: 7 | hit_except = True 8 | finally: 9 | hit_finally = True 10 | 11 | ___assertFalse(hit_except) 12 | ___assertTrue(hit_finally) 13 | -------------------------------------------------------------------------------- /programs/politz/unboundlocal-augassign.py: -------------------------------------------------------------------------------- 1 | # the variable in f() is lifted as a local and assigned 2 | # unbound, because it is the left side of an assignment. 3 | 4 | global_x = 1 5 | 6 | def f(): 7 | global_x += 1 8 | 9 | ___assertRaises(UnboundLocalError, f) 10 | -------------------------------------------------------------------------------- /programs/smeding/subscription-assign-trace-argsfirst.py: -------------------------------------------------------------------------------- 1 | # the arguments of a subscription assigment are evaluated before __setitem__ is called 2 | 3 | try : 4 | trace(1,None)[trace(2,None)] = trace(0,None) 5 | except TypeError as e : 6 | trace(3,None) 7 | 8 | trace(4,None) 9 | -------------------------------------------------------------------------------- /programs/testfloat1.py: -------------------------------------------------------------------------------- 1 | assert int(5.99) == 5 2 | assert 5.99 < 6 <= 6.0 3 | assert 5.0 <= 5 < 5.01 4 | assert 6.0 >= 6 > 5.99 5 | assert 5.01 > 5 >= 5.0 6 | assert 6.0 >= 6.0 >= 5.99 7 | assert 5.99 <= 6.0 <= 6.0 8 | assert 5 == 5.0 != 5.01 9 | assert 5 != 5.01 == 5.01 10 | -------------------------------------------------------------------------------- /programs/testloopfinally1.py: -------------------------------------------------------------------------------- 1 | x = 5 2 | try: 3 | while x > 0: 4 | try: 5 | raise 6 | finally: 7 | y = 6 8 | assert x == 5 9 | assert False 10 | assert False 11 | except RuntimeError: 12 | z = 7 13 | assert y == 6 14 | assert z == 7 15 | -------------------------------------------------------------------------------- /programs/testmodule1.py: -------------------------------------------------------------------------------- 1 | import _io 2 | module = type(_io) 3 | m = module.__new__(module) 4 | assert m.__dict__ is None 5 | try: 6 | m.__init__() 7 | except TypeError: 8 | m.__init__("foo") 9 | assert m.__dict__.__class__ is dict 10 | assert m.__name__ == "foo" 11 | -------------------------------------------------------------------------------- /programs/testposix3.py: -------------------------------------------------------------------------------- 1 | import posix 2 | 3 | l = posix.listdir("programs/files") 4 | assert "#" in l 5 | assert "\u03c0" in l 6 | assert "stat" in l 7 | l2 = posix.listdir(b"programs/files") 8 | assert b"#" in l2 9 | assert b"\xcf\x80" in l2 10 | assert b"stat" in l2 11 | -------------------------------------------------------------------------------- /programs/testsetattr5.py: -------------------------------------------------------------------------------- 1 | class A: 2 | def __setattr__(self, name, value): 3 | assert name == "x" 4 | assert self.__class__ is A 5 | assert value == 5 6 | 7 | a = A() 8 | a.x = 5 9 | try: 10 | a.x 11 | assert False 12 | except AttributeError: pass 13 | -------------------------------------------------------------------------------- /programs/testtry13.py: -------------------------------------------------------------------------------- 1 | try: 2 | raise None 3 | assert False 4 | except TypeError: 5 | x = 13 6 | assert x == 13 7 | 8 | try: 9 | raise SyntaxError from None 10 | except SyntaxError as e: 11 | assert e.__suppress_context__ 12 | x = 14 13 | assert x == 14 14 | -------------------------------------------------------------------------------- /programs/testtry9.py: -------------------------------------------------------------------------------- 1 | try: 2 | raise Exception from TypeError 3 | except Exception as e: 4 | assert e.__cause__.__class__ == TypeError 5 | 6 | try: 7 | raise Exception from TypeError() 8 | except Exception as e: 9 | assert e.__cause__.__class__ == TypeError 10 | -------------------------------------------------------------------------------- /programs/politz/nonlocal-class.py: -------------------------------------------------------------------------------- 1 | def f(x): 2 | class c: 3 | nonlocal x 4 | x += 1 5 | def get(self): 6 | return x 7 | return c() 8 | 9 | c = f(0) 10 | ___assertEqual(c.get(), 1) 11 | ___assertNotIn("x", c.__class__.__dict__) 12 | -------------------------------------------------------------------------------- /programs/politz/simple_getter_exn.py: -------------------------------------------------------------------------------- 1 | class C(object): 2 | def __init__(self): 3 | self.x = 42 4 | @property 5 | def f(self): 6 | self.x += 1 7 | return self.x 8 | 9 | def assign_to_f(): 10 | C().f = 12 11 | ___assertRaises(AttributeError, assign_to_f) 12 | -------------------------------------------------------------------------------- /programs/politz/try-except-finally.py: -------------------------------------------------------------------------------- 1 | hit_except = False 2 | hit_finally = False 3 | 4 | try: 5 | raise Exception('yarr!') 6 | except: 7 | hit_except = True 8 | finally: 9 | hit_finally = True 10 | 11 | ___assertTrue(hit_except) 12 | ___assertTrue(hit_finally) 13 | -------------------------------------------------------------------------------- /programs/smeding/class-scope-funcattr-call.py: -------------------------------------------------------------------------------- 1 | # class variables can use class functions to be initialized 2 | 3 | setDescr("Could not initialize class attribute using class function") 4 | 5 | class A(object) : 6 | def f() : 7 | return 1 8 | 9 | a = f() 10 | -------------------------------------------------------------------------------- /programs/smeding/function-scope-double-del.py: -------------------------------------------------------------------------------- 1 | # when a variable is deleted twice it should produce an unbound local error, 2 | # even if it used to be bound 3 | 4 | def foo() : 5 | x = 1 6 | del x 7 | del x 8 | 9 | expectException(UnboundLocalError) 10 | 11 | foo() 12 | -------------------------------------------------------------------------------- /programs/smeding/instantiation-objfunc.py: -------------------------------------------------------------------------------- 1 | # Object members that are functions are not instantiated, unlike class functions 2 | 3 | class A(object) : 4 | pass 5 | 6 | def foo(a) : 7 | return a 8 | 9 | a = A() 10 | 11 | a.bar = foo 12 | 13 | assertTrue(a.bar(1) == 1) 14 | -------------------------------------------------------------------------------- /programs/smeding/str-as-super-tostr.py: -------------------------------------------------------------------------------- 1 | # calling __str__ on a subclass of str will return the 'inner' string 2 | # or a copy, not the object itself 3 | 4 | class A(str) : 5 | pass 6 | 7 | a = A() 8 | a.x = 1 9 | 10 | expectException(AttributeError) 11 | a.__str__().x 12 | -------------------------------------------------------------------------------- /programs/smeding/try-finally-continue.py: -------------------------------------------------------------------------------- 1 | # the finally clause is also executed when we continue in its body 2 | 3 | t = True 4 | 5 | while t : 6 | try : 7 | t = False 8 | continue 9 | finally : 10 | trace(0, None) 11 | 12 | trace(1,None) 13 | -------------------------------------------------------------------------------- /programs/testyield9.py: -------------------------------------------------------------------------------- 1 | def a(): 2 | try: 3 | yield 5 4 | assert False 5 | except GeneratorExit: 6 | raise StopIteration 7 | 8 | b = a() 9 | b.__next__() 10 | b.close() 11 | try: 12 | b.__next__() 13 | except StopIteration: 14 | x = 5 15 | assert x == 5 16 | -------------------------------------------------------------------------------- /programs/politz/dict-update.py: -------------------------------------------------------------------------------- 1 | d = {} 2 | d.update({1:100}) 3 | d.update({2:20}) 4 | d.update({1:1, 2:2, 3:3}) 5 | ___assertEqual(d, {1:1, 2:2, 3:3}) 6 | 7 | d.update() 8 | ___assertEqual(d, {1:1, 2:2, 3:3}) 9 | 10 | ___assertRaises((TypeError, AttributeError), d.update, None) 11 | -------------------------------------------------------------------------------- /programs/politz/except-reraise.py: -------------------------------------------------------------------------------- 1 | def reraise(): 2 | try: 3 | raise TypeError("foo") 4 | except: 5 | try: 6 | raise KeyError("caught") 7 | except KeyError: 8 | pass 9 | raise 10 | ___assertRaises(TypeError, reraise) 11 | -------------------------------------------------------------------------------- /programs/politz/test_exception.py: -------------------------------------------------------------------------------- 1 | def foo(): 2 | import support_exception 3 | 4 | ___assertRaises(TypeError, foo) 5 | #try: 6 | # import support_exception 7 | # ___assertTrue(False) 8 | #except TypeError("foo"): 9 | # pass 10 | #else: 11 | # ___assertTrue(False) 12 | -------------------------------------------------------------------------------- /programs/smeding/class-scope-func-recursive.py: -------------------------------------------------------------------------------- 1 | # A class method cannot call itself recursively 2 | 3 | class A(object) : 4 | def f(self, x) : 5 | if x : 6 | return f(self, False) 7 | else : 8 | return None 9 | 10 | expectException(NameError) 11 | 12 | A().f(True) 13 | -------------------------------------------------------------------------------- /programs/smeding/instantiation-type.py: -------------------------------------------------------------------------------- 1 | setDescr("a function's class cannot be changed once it has been set") 2 | 3 | class A(object) : 4 | def foo(self) : 5 | return 1 6 | 7 | class B(object) : 8 | foo = A.foo 9 | 10 | b = B() 11 | b.foo() 12 | assertTrue(b.foo() == 1) 13 | -------------------------------------------------------------------------------- /programs/smeding/metaclass-new-copy-dict.py: -------------------------------------------------------------------------------- 1 | # check if the class-constructor of type copies the dictionary of attributes 2 | 3 | dict = {} 4 | 5 | A = type.__new__(type, "A", (object,), dict) 6 | 7 | dict["spam"] = True 8 | 9 | expectException(AttributeError) 10 | A.spam 11 | -------------------------------------------------------------------------------- /programs/smeding/try-except-scope-noblock.py: -------------------------------------------------------------------------------- 1 | # variables defined in try still accessible after try statement 2 | 3 | setDescr("Variable defined in try body before raise is not in scope") 4 | 5 | try : 6 | a = True 7 | except Exception as e : 8 | pass 9 | 10 | assertTrue(a) 11 | -------------------------------------------------------------------------------- /programs/testbytes4.py: -------------------------------------------------------------------------------- 1 | class A: 2 | def __init__(self): self.x = 5 3 | def __iter__(self): return self 4 | def __next__(self): 5 | self.x -= 1 6 | if self.x == 0: 7 | raise StopIteration 8 | return self.x 9 | 10 | assert bytes(A()) == b"\x04\x03\x02\x01" 11 | -------------------------------------------------------------------------------- /programs/testfuncfinally3.py: -------------------------------------------------------------------------------- 1 | def c(): 2 | global x 3 | try: 4 | x = 11 5 | return 5 6 | assert False 7 | finally: 8 | assert x == 11 9 | x = 12 10 | return 6 11 | assert False 12 | assert False 13 | 14 | assert c() == 6 15 | assert x == 12 16 | -------------------------------------------------------------------------------- /programs/testfunctions4.py: -------------------------------------------------------------------------------- 1 | def d(x, y, ** z): 2 | assert x == 5 and y == 6 and z["w"] == 7 and len(z) == 1, (x, y, z) 3 | return 0 4 | 5 | assert d(y=6,x=5,w=7) == 0 6 | 7 | try: 8 | d(1, 2, x=2) 9 | assert False 10 | except TypeError: 11 | x = 7 12 | assert x == 7 13 | -------------------------------------------------------------------------------- /programs/testsetattr1.py: -------------------------------------------------------------------------------- 1 | class A: pass 2 | 3 | A.x = 5 4 | assert A.x == 5 5 | setattr(A, "x", 6) 6 | assert A.x == 6 7 | 8 | a = A() 9 | assert a.x == 6 10 | a.x = 7 11 | assert a.x == 7 12 | assert A.x == 6 13 | setattr(a, "x", 8) 14 | assert a.x == 8 15 | assert A.x == 6 16 | -------------------------------------------------------------------------------- /programs/testtuples5.py: -------------------------------------------------------------------------------- 1 | try: 2 | x = iter((5,6)) 3 | assert x.__next__() == 5 4 | assert x.__next__() == 6 5 | y = 6 6 | assert x.__next__() and False 7 | except StopIteration as e: 8 | z = 7 9 | assert y == 6 10 | assert e.__context__ is None 11 | assert z == 7 12 | -------------------------------------------------------------------------------- /programs/politz/simple_getter.py: -------------------------------------------------------------------------------- 1 | class C(object): 2 | def __init__(self): 3 | self.x = 42 4 | @property 5 | def f(self): 6 | self.x += 1 7 | return self.x 8 | 9 | c = C() 10 | ___assertEqual(c.f, 43) 11 | ___assertEqual(c.f, 44) 12 | ___assertEqual(c.f, 45) 13 | 14 | -------------------------------------------------------------------------------- /programs/smeding/class-scope-nested.py: -------------------------------------------------------------------------------- 1 | setDescr("A nested class' body cannot refer to the outer class' attributes") 2 | 3 | try : 4 | class A(object) : 5 | x = 1 6 | class B(object) : 7 | y = x 8 | fail() 9 | except NameError as n : 10 | pass 11 | -------------------------------------------------------------------------------- /programs/smeding/exec-func-no-new-locals.py: -------------------------------------------------------------------------------- 1 | # check if an exec statement can introduce new local variables 2 | # and whether it shadows variables in the outside scope 3 | 4 | x = True 5 | 6 | def foo() : 7 | assertTrue(x) 8 | exec("x = False") 9 | 10 | foo() 11 | assertTrue(x) 12 | -------------------------------------------------------------------------------- /programs/testisinstance1.py: -------------------------------------------------------------------------------- 1 | assert isinstance(5, int) 2 | assert isinstance(True, int) 3 | assert not isinstance(5, tuple) 4 | 5 | class M(type): 6 | def __instancecheck__(self, instance): 7 | return True 8 | 9 | class A(metaclass=M): pass 10 | 11 | assert isinstance(5, A) 12 | -------------------------------------------------------------------------------- /programs/teststrings4.py: -------------------------------------------------------------------------------- 1 | assert "abc abc abc foo".rpartition("abc") == ("abc abc ", "abc", " foo") 2 | assert "abc abc abc foo".rpartition("def") == ("", "", "abc abc abc foo") 3 | try: 4 | "abc abc abc foo".rpartition(5) 5 | assert False 6 | except TypeError: 7 | y = 6 8 | assert y == 6 9 | -------------------------------------------------------------------------------- /programs/politz/destructuring-bind.py: -------------------------------------------------------------------------------- 1 | x = 12 2 | y = 13 3 | 4 | def f(): 5 | x, y = (3,4) 6 | f() 7 | 8 | assert x == 12 9 | assert y == 13 10 | 11 | class c: 12 | x, y = (5, 6) 13 | 14 | assert x == 12 15 | assert y == 13 16 | 17 | assert c().x == 5 18 | assert c().y == 6 19 | 20 | -------------------------------------------------------------------------------- /programs/politz/nearest-enclosing-scope.py: -------------------------------------------------------------------------------- 1 | def f(x): 2 | def g(y): 3 | x = 42 # check that this masks binding in f() 4 | def h(z): 5 | return x + z 6 | return h 7 | return g(2) 8 | 9 | test_func = f(10) 10 | ___assertEqual(test_func(5), 47) 11 | -------------------------------------------------------------------------------- /programs/politz/override_getattr_descr.py: -------------------------------------------------------------------------------- 1 | class Descriptor(object): 2 | def __get__(self, obj, objtype): 3 | def f(name): 4 | return 'got ' + name 5 | return f 6 | 7 | class B(object): 8 | __getattr__ = Descriptor() 9 | 10 | assert(getattr(B(), "key") == "got key") 11 | -------------------------------------------------------------------------------- /programs/smeding/exception-trace-noraise.py: -------------------------------------------------------------------------------- 1 | # tests on the order of execution in case no exceptions are raised 2 | 3 | class TestException(Exception) : 4 | pass 5 | 6 | try : 7 | try : 8 | trace(0, None) 9 | except fail() as e: 10 | fail() 11 | finally : 12 | trace(1, None) 13 | -------------------------------------------------------------------------------- /programs/smeding/function-closure-env-notseq-nested.py: -------------------------------------------------------------------------------- 1 | # this is a slightly more complicated version of 2 | # function-closure-env-notseq.py 3 | 4 | x = 1 5 | 6 | def foo() : 7 | def bar() : 8 | return x 9 | x = 2 10 | return bar 11 | 12 | assertTrue(foo()() == 2) 13 | -------------------------------------------------------------------------------- /programs/testbools1.py: -------------------------------------------------------------------------------- 1 | assert False is (not True) 2 | assert True is (not False) 3 | assert (True and True) is True 4 | assert (True and False) is False 5 | assert (False and 5 / 0) is False 6 | assert (True or False) is True 7 | assert (False or False) is False 8 | assert (True or 5 / 0) is True 9 | -------------------------------------------------------------------------------- /programs/testtry6.py: -------------------------------------------------------------------------------- 1 | try: 2 | try: 3 | e 4 | assert False 5 | except NameError as e: 6 | x = 8 7 | raise 8 | assert False 9 | except NameError as f: 10 | try: 11 | e 12 | assert False 13 | except NameError: 14 | y = 9 15 | assert x == 8 and y == 9 16 | -------------------------------------------------------------------------------- /programs/testtuples4.py: -------------------------------------------------------------------------------- 1 | try: 2 | x = (5,6).__iter__() 3 | assert x.__next__() == 5 4 | assert x.__next__() == 6 5 | y = 5 6 | assert x.__next__() and False 7 | except StopIteration as e: 8 | z = 6 9 | assert y == 5 10 | assert e.__context__ is None 11 | assert z == 6, z 12 | -------------------------------------------------------------------------------- /programs/politz/bool-isinstance.py: -------------------------------------------------------------------------------- 1 | ___assertIs(isinstance(True, bool), True) 2 | ___assertIs(isinstance(False, bool), True) 3 | ___assertIs(isinstance(True, int), True) 4 | ___assertIs(isinstance(False, int), True) 5 | ___assertIs(isinstance(1, bool), False) 6 | ___assertIs(isinstance(0, bool), False) 7 | -------------------------------------------------------------------------------- /programs/politz/nesting-global-under-local.py: -------------------------------------------------------------------------------- 1 | x = 7 2 | def f(): 3 | x = 1 4 | def g(): 5 | global x 6 | def i(): 7 | def h(): 8 | return x 9 | return h() 10 | return i() 11 | return g() 12 | 13 | ___assertEqual(f(), 7) 14 | ___assertEqual(x, 7) 15 | 16 | -------------------------------------------------------------------------------- /programs/smeding/instantiation-classobjects.py: -------------------------------------------------------------------------------- 1 | # calling static class functions 2 | 3 | setDescr("Class functions are not instantiated") 4 | 5 | class A(object) : 6 | def foo(self) : 7 | return 1 8 | 9 | assertTrue(A.foo(A()) == 1) 10 | 11 | expectException(TypeError) 12 | 13 | A.foo() 14 | -------------------------------------------------------------------------------- /programs/testbytes1.py: -------------------------------------------------------------------------------- 1 | assert b"foo" > b"fo" >= b"f" 2 | assert not b"foo" > b"foo" 3 | assert b"foo" >= b"foo" 4 | assert b"f" <= b"fo" < b"foo" 5 | assert not b"foo" < b"foo" 6 | assert b"foo" <= b"foo" 7 | assert b"foo" == b"foo" != b"fo" 8 | 9 | assert b"fo" + b"o" == b"foo", b"fo" + b"o" 10 | -------------------------------------------------------------------------------- /programs/testio3.py: -------------------------------------------------------------------------------- 1 | import _io 2 | i = _io._RawIOBase() 3 | i.close() 4 | try: 5 | i.read() 6 | except AttributeError: 7 | try: 8 | i.read(-1) 9 | except AttributeError: 10 | try: 11 | i.readall() 12 | except AttributeError: 13 | x = 5 14 | 15 | assert x == 5 16 | -------------------------------------------------------------------------------- /programs/testissubclass1.py: -------------------------------------------------------------------------------- 1 | assert issubclass(bool, int) 2 | assert issubclass(bool, object) 3 | assert not issubclass(int, tuple) 4 | 5 | class M(type): 6 | def __subclasscheck__(self, instance): 7 | return True 8 | 9 | class A(metaclass=M): pass 10 | 11 | assert issubclass(int, A) 12 | -------------------------------------------------------------------------------- /programs/testmockimport5.py: -------------------------------------------------------------------------------- 1 | def __import__(name, globals=None, locals=None, fromlist=(), level=0): 2 | class A: pass 3 | a = A() 4 | return a 5 | 6 | import builtins 7 | builtins.__import__ = __import__ 8 | try: 9 | from foo import a 10 | assert False 11 | except ImportError: pass 12 | -------------------------------------------------------------------------------- /programs/testyield13.py: -------------------------------------------------------------------------------- 1 | class A(Exception): 2 | def __init__(self): 3 | assert not foo.gi_running 4 | assert next(foo) == 1 5 | 6 | def a(): 7 | try: 8 | yield 1 9 | except A: 10 | assert foo.gi_running 11 | yield 2 12 | 13 | foo = a() 14 | assert foo.throw(A) == 2 15 | -------------------------------------------------------------------------------- /programs/politz/nested-nonlocal.py: -------------------------------------------------------------------------------- 1 | def f(x): 2 | def g(): 3 | nonlocal x 4 | x -= 2 5 | def h(): 6 | nonlocal x 7 | x += 4 8 | return x 9 | return h 10 | return g 11 | 12 | g = f(1) 13 | h = g() 14 | ___assertEqual(h(), 3) 15 | -------------------------------------------------------------------------------- /programs/politz/nesting-through-class.py: -------------------------------------------------------------------------------- 1 | def make_adder5(x): 2 | class Adder: 3 | def __call__(self, y): 4 | return x + y 5 | return Adder() 6 | 7 | inc = make_adder5(1) 8 | plus10 = make_adder5(10) 9 | 10 | ___assertEqual(inc(1), 2) 11 | ___assertEqual(plus10(-2), 8) 12 | -------------------------------------------------------------------------------- /programs/smeding/if-else-scope-shadow.py: -------------------------------------------------------------------------------- 1 | setDescr("Variables can be redefined in the function body, not shadowed") 2 | 3 | a = 1 4 | 5 | if True : 6 | a = 2 7 | else : 8 | pass 9 | 10 | assertTrue(a == 2) 11 | 12 | if False : 13 | pass 14 | else : 15 | a = 3 16 | 17 | assertTrue(a == 3) 18 | -------------------------------------------------------------------------------- /programs/testintegers8.py: -------------------------------------------------------------------------------- 1 | try: 2 | 1/0 3 | except ZeroDivisionError: 4 | try: 5 | 1%0 6 | except ZeroDivisionError: 7 | try: 8 | 1//0 9 | except ZeroDivisionError: 10 | try: 11 | 0 ** -1 12 | except ZeroDivisionError: 13 | x = 1 14 | assert x == 1 15 | -------------------------------------------------------------------------------- /programs/politz/range-list.py: -------------------------------------------------------------------------------- 1 | ___assertEqual(list(range(3)), [0, 1, 2]) 2 | ___assertEqual(list(range(1, 5)), [1, 2, 3, 4]) 3 | ___assertEqual(list(range(0)), []) 4 | ___assertEqual(list(range(-3)), []) 5 | ___assertEqual(list(range(1, 10, 3)), [1, 4, 7]) 6 | ___assertEqual(list(range(5, -5, -3)), [5, 2, -1, -4]) 7 | -------------------------------------------------------------------------------- /programs/smeding/try-finally-scope-caughtexception.py: -------------------------------------------------------------------------------- 1 | # exceptions that have been caught are available in the finally clause 2 | 3 | try : 4 | try : 5 | raise Exception() 6 | except Exception as e : 7 | pass 8 | finally : 9 | try: 10 | e 11 | fail() 12 | except NameError: pass 13 | -------------------------------------------------------------------------------- /programs/testtuples7.py: -------------------------------------------------------------------------------- 1 | try: 2 | x = iter.__class__.__call__(iter, (5,6)) 3 | assert x.__next__() is 5 4 | assert x.__next__() is 6 5 | y = 8 6 | assert x.__next__() and False 7 | except StopIteration as e: 8 | z = 9 9 | assert y == 8 10 | assert e.__context__ is None 11 | assert z == 9 12 | -------------------------------------------------------------------------------- /programs/testyield12.py: -------------------------------------------------------------------------------- 1 | def a(): 2 | yield 5 3 | return 4 | 5 | b=a() 6 | 7 | assert b.__next__() == 5 8 | try: 9 | b.__next__() 10 | assert False 11 | except StopIteration: 12 | try: 13 | b.__next__() 14 | assert False 15 | except StopIteration: 16 | x = 5 17 | assert x == 5 18 | -------------------------------------------------------------------------------- /programs/politz/dict-keys-effects.py: -------------------------------------------------------------------------------- 1 | def f(): 2 | global x 3 | x = 4 4 | return "key1" 5 | 6 | def g(): 7 | global y 8 | y = 5 9 | return "key2" 10 | 11 | o = {f(): "fval", g(): "gval"} 12 | 13 | ___assertEqual(o.keys(), {"key1", "key2"}) 14 | ___assertEqual(x, 4) 15 | ___assertEqual(y, 5) 16 | -------------------------------------------------------------------------------- /programs/politz/recursion.py: -------------------------------------------------------------------------------- 1 | def f(x): 2 | def fact(n): 3 | if n == 0: 4 | return 1 5 | else: 6 | return n * fact(n - 1) 7 | if x >= 0: 8 | return fact(x) 9 | else: 10 | raise ValueError("x must be >= 0") 11 | 12 | ___assertEqual(f(6), 720) 13 | -------------------------------------------------------------------------------- /programs/smeding/instantiation-callable-object.py: -------------------------------------------------------------------------------- 1 | # a callable object will be instantiated only once 2 | 3 | class B(object) : 4 | def __call__(self,x) : 5 | return x 6 | 7 | class A(object) : 8 | __call__ = B() # B() won't be instantiated with an A object 9 | 10 | assertTrue(A()(True)) 11 | -------------------------------------------------------------------------------- /programs/politz/locals-function.py: -------------------------------------------------------------------------------- 1 | def f(x): 2 | def g(y): 3 | def h(z): 4 | return y + z 5 | w = x + y 6 | y += 3 7 | return locals() 8 | return g 9 | 10 | d = f(2)(4) 11 | ___assertIn('h', d) 12 | del d['h'] 13 | ___assertEqual(d, {'x': 2, 'y': 7, 'w': 6}) 14 | -------------------------------------------------------------------------------- /programs/politz/test_import_name_binding.py: -------------------------------------------------------------------------------- 1 | # import x.y.z binds x in the current namespace 2 | import test_as 3 | import test_as as x 4 | 5 | ___assertTrue(x is test_as) 6 | 7 | # import x.y.z as w binds z as w 8 | import test_as.empty 9 | import test_as.empty as y 10 | 11 | ___assertTrue(y is test_as.empty) 12 | -------------------------------------------------------------------------------- /programs/smeding/function-scope-refbeforeshadow.py: -------------------------------------------------------------------------------- 1 | # a variable cannot be referenced before it is defined 2 | # even if the variable was inherited as well 3 | 4 | x = 1 5 | 6 | def foo() : 7 | trace(1, x) 8 | fail() 9 | x = 1 10 | 11 | expectException(UnboundLocalError) 12 | 13 | trace(0,foo()) 14 | -------------------------------------------------------------------------------- /programs/smeding/print-childof-str.py: -------------------------------------------------------------------------------- 1 | # print will call __str__ even for a direct descendant of str 2 | 3 | class StrCalledException(Exception) : 4 | pass 5 | 6 | class A(str) : 7 | def __str__(self) : 8 | raise StrCalledException() 9 | 10 | expectException(StrCalledException) 11 | print((A())) 12 | -------------------------------------------------------------------------------- /programs/testobject4.py: -------------------------------------------------------------------------------- 1 | class A: pass 2 | 3 | a = object.__new__(A) 4 | a.x = 5 5 | assert a.x == 5 6 | 7 | class B(int): pass 8 | 9 | try: 10 | b = object.__new__(B) 11 | assert False 12 | except TypeError: 13 | try: 14 | b = object.__new__(int) 15 | assert False 16 | except TypeError: pass 17 | -------------------------------------------------------------------------------- /programs/testslice.py: -------------------------------------------------------------------------------- 1 | x = [0,1,2,25,4,50,6,100,8,9] 2 | 3 | assert x[:] == x == x[0:] == x[:20] == x[-20:] == x[0:10:1] 4 | assert x[3:9:2] == x[-7:-1:2] == [25,50,100] 5 | assert x[5:4] == x[4:5:-1] == x[0:0] == [] 6 | assert x[7:2:-2] == x[-3:-8:-2] == [100,50,25] 7 | assert x[::-1] == [9, 8, 100, 6, 50, 4, 25, 2, 1, 0] 8 | -------------------------------------------------------------------------------- /programs/testyield2.py: -------------------------------------------------------------------------------- 1 | def c(): 2 | try: 3 | yield 5 4 | assert False 5 | except ValueError: 6 | yield 6 7 | 8 | d = c() 9 | assert d.__next__() == 5 10 | assert d.throw(ValueError) == 6 11 | try: 12 | d.__next__() 13 | assert False 14 | except StopIteration as e: 15 | assert e.args == () 16 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | syntax: glob 2 | tests/* 3 | python-kompiled 4 | **/*.out 5 | **/*.out.tmp 6 | **/*.ref 7 | .k 8 | .*.sw? 9 | kompile_* 10 | all_tokens.tok 11 | run.maude 12 | test.py 13 | *.orig 14 | IN.maude 15 | *.rej 16 | junit-results.xml 17 | **/__pycache__ 18 | nohup.out 19 | bootstrapped.bin 20 | programs/*.bin 21 | -------------------------------------------------------------------------------- /programs/politz/dict-getitem.py: -------------------------------------------------------------------------------- 1 | d = {'a': 1, 'b': 2} 2 | ___assertEqual(d['a'], 1) 3 | ___assertEqual(d['b'], 2) 4 | d['c'] = 3 5 | d['a'] = 4 6 | ___assertEqual(d['c'], 3) 7 | ___assertEqual(d['a'], 4) 8 | del d['b'] 9 | ___assertEqual(d, {'a': 4, 'c': 3}) 10 | 11 | ___assertRaises(TypeError, d.__getitem__) 12 | 13 | -------------------------------------------------------------------------------- /programs/politz/test-for-else.py: -------------------------------------------------------------------------------- 1 | def index(iterable, item): 2 | i = 0 3 | for it in iterable: 4 | if it == item: 5 | break 6 | i += 1 7 | else: 8 | i = None 9 | return i 10 | 11 | ___assertEqual(index('abcde', 'c'), 2) 12 | ___assertEqual(index('abcde', 'C'), None) 13 | -------------------------------------------------------------------------------- /programs/smeding/and-trace-nonzero-before-rhs.py: -------------------------------------------------------------------------------- 1 | # check that the and operator is truly lazy, i.e., whether it calls nonzero on its lhs before evaluating the rhs 2 | 3 | class A(object) : 4 | def __bool__(self) : 5 | trace(0,None) 6 | return True 7 | 8 | assertTrue(1 == ( A() and trace(1,1) )) 9 | trace(2,None) 10 | -------------------------------------------------------------------------------- /programs/smeding/exec-type-env-simulated.py: -------------------------------------------------------------------------------- 1 | # an object that simulates a dictionary by implementing the right methods 2 | # can not be used as the environment of an exec statement 3 | 4 | class A(object) : 5 | def __getitem__(self, key) : 6 | return fail 7 | 8 | expectException(TypeError) 9 | exec("pass", A()) 10 | -------------------------------------------------------------------------------- /programs/testbytes5.py: -------------------------------------------------------------------------------- 1 | class A: 2 | def __index__(self): return 5 3 | 4 | b = b"\x00\x00\x00\x00\x00" 5 | assert bytes(A()) == b 6 | 7 | def a(): 8 | yield 256 9 | 10 | try: 11 | bytes(a()) 12 | except ValueError: 13 | try: 14 | bytearray(a()) 15 | except ValueError: 16 | x = 5 17 | assert x == 5 18 | -------------------------------------------------------------------------------- /programs/politz/send-gen.py: -------------------------------------------------------------------------------- 1 | def coroutine(seq): 2 | count = 0 3 | while count < 200: 4 | count += yield 5 | seq.append(count) 6 | 7 | seq = [] 8 | c = coroutine(seq) 9 | 10 | next(c) 11 | ___assertEqual(seq, []) 12 | 13 | c.send(10) 14 | ___assertEqual(seq, [10]) 15 | 16 | c.send(10) 17 | ___assertEqual(seq, [10, 20]) -------------------------------------------------------------------------------- /programs/politz/slice1.py: -------------------------------------------------------------------------------- 1 | ___assertEqual("foobar"[::], "foobar") 2 | ___assertEqual("foobar"[0:6:], "foobar") 3 | ___assertEqual("foobar"[0:6:1], "foobar") 4 | ___assertEqual("foobar"[0:6:-1], "") 5 | ___assertEqual("foobar"[6:0:-1], "raboo") 6 | ___assertEqual("foobar"[7:0:-1], "raboo") 7 | ___assertEqual("foobar"[::-1], "raboof") 8 | -------------------------------------------------------------------------------- /programs/smeding/generator-call-while-running.py: -------------------------------------------------------------------------------- 1 | # when a generator's next is called while it is already running 2 | # an exception should be thrown 3 | 4 | def gen() : 5 | trace(0,0) # call itself in trace to prevent a loop 6 | next(it) 7 | yield 1 8 | 9 | it = gen() 10 | expectException(ValueError) 11 | next(it) 12 | -------------------------------------------------------------------------------- /programs/smeding/lists-iter-fail-fail.py: -------------------------------------------------------------------------------- 1 | # if next() raises the StopIteration exception, it should continue to do that 2 | # on subsequent calls 3 | 4 | it = [1].__iter__() 5 | 6 | next(it) 7 | 8 | try : 9 | next(it) 10 | except StopIteration as e : 11 | pass 12 | 13 | expectException(StopIteration) 14 | next(it) 15 | -------------------------------------------------------------------------------- /programs/smeding/try-except-type-raise-nonexcep.py: -------------------------------------------------------------------------------- 1 | # raise statements can only raise expressions that are a subclass of BaseException 2 | # catches can however catch any type 3 | 4 | class A(object) : 5 | pass 6 | 7 | expectException(TypeError) 8 | 9 | try : 10 | raise A() 11 | except A as a: 12 | fail() 13 | -------------------------------------------------------------------------------- /programs/testall.py: -------------------------------------------------------------------------------- 1 | assert all(()) is True 2 | assert all((1,)) is True 3 | assert all((0,)) is False 4 | assert all((1,2,3,4)) is True 5 | assert all((1,2,3,4,0)) is False 6 | 7 | class A: 8 | x = 0 9 | def __bool__(self): A.x += 1; return False 10 | 11 | assert not all((A(), A(), A(), A())) 12 | assert A.x == 1 13 | -------------------------------------------------------------------------------- /programs/politz/simple-and-rebinding.py: -------------------------------------------------------------------------------- 1 | def make_adder3(x): 2 | def adder(y): 3 | return x + y 4 | x = x + 1 # check tracking of assignment to x in defining scope 5 | return adder 6 | 7 | inc = make_adder3(0) 8 | plus10 = make_adder3(9) 9 | 10 | ___assertEqual(inc(1), 2) 11 | ___assertEqual(plus10(-2), 8) 12 | -------------------------------------------------------------------------------- /programs/politz/unbound-local.py: -------------------------------------------------------------------------------- 1 | def errorInOuter(): 2 | y 3 | def inner(): 4 | return y 5 | y = 1 6 | 7 | def errorInInner(): 8 | def inner(): 9 | return y 10 | inner() 11 | y = 1 12 | 13 | ___assertRaises(UnboundLocalError, errorInOuter) 14 | ___assertRaises(NameError, errorInInner) 15 | -------------------------------------------------------------------------------- /programs/politz/while-continue-finally.py: -------------------------------------------------------------------------------- 1 | chk = 'init' 2 | while True: 3 | if chk == 'saw while': 4 | chk = 100 5 | break 6 | try: 7 | chk = 'in loop' 8 | continue 9 | chk = 'better-not-be-this' 10 | finally: 11 | if chk == 'in loop': 12 | chk = 'saw while' 13 | 14 | ___assertEqual(chk, 100) 15 | -------------------------------------------------------------------------------- /programs/smeding/if-else-nonzero-objectmember.py: -------------------------------------------------------------------------------- 1 | setDescr("If uses the nonzero function of an object's class") 2 | 3 | class A(object) : 4 | def __bool__(self) : 5 | return False 6 | 7 | def nonzero(self) : 8 | return True 9 | 10 | a = A() 11 | a.__nonzero__ = nonzero 12 | 13 | if a : 14 | fail() 15 | else : 16 | pass 17 | -------------------------------------------------------------------------------- /programs/smeding/import-re-export-imported.py: -------------------------------------------------------------------------------- 1 | # an import statement gets everything that is in scope, also modules imported 2 | # by an imported module 3 | 4 | import imported.imported_re_export_imported_A 5 | 6 | # also note the naming 7 | assertTrue(imported.imported_re_export_imported_A.imported.imported_re_export_imported_B.x == 1) 8 | -------------------------------------------------------------------------------- /programs/smeding/metaclass-rename-class.py: -------------------------------------------------------------------------------- 1 | # check that renaming a class in the metatype's constructor does not change the binding 2 | 3 | class M(type) : 4 | def __new__(self, name, bases, dict) : 5 | return type.__new__(self, "B", bases, dict) 6 | 7 | class A(object, metaclass=M) : 8 | x = True 9 | 10 | assertTrue(A.x) 11 | -------------------------------------------------------------------------------- /programs/testfunctions1.py: -------------------------------------------------------------------------------- 1 | def a(x, y): 2 | assert x == 1 and y == 2 3 | 4 | z = [1, 2] 5 | assert a(* z) is None 6 | 7 | try: 8 | a(* 5) 9 | assert False 10 | except TypeError: 11 | x = 3 12 | assert x == 3 13 | 14 | try: 15 | a(1, 2, 3) 16 | assert False 17 | except TypeError: 18 | x = 4 19 | assert x == 4 20 | -------------------------------------------------------------------------------- /programs/testscope5.py: -------------------------------------------------------------------------------- 1 | x = 5 2 | def a(): 3 | x = 6 4 | assert x == 6 5 | def b(): 6 | global x 7 | assert x == 5 8 | def c(): 9 | assert x == 5 10 | c() 11 | assert "x" in c.__code__.co_names 12 | b() 13 | assert "x" in b.__code__.co_names 14 | a() 15 | assert "x" in a.__code__.co_varnames 16 | -------------------------------------------------------------------------------- /programs/testyield11.py: -------------------------------------------------------------------------------- 1 | def a(): 2 | yield 5 3 | raise TypeError 4 | yield 6 5 | 6 | b=a() 7 | 8 | assert b.__next__() == 5 9 | try: 10 | b.__next__() 11 | assert False 12 | except TypeError: 13 | try: 14 | b.__next__() 15 | assert False 16 | except StopIteration: 17 | x = 5 18 | 19 | assert x == 5 20 | -------------------------------------------------------------------------------- /programs/politz/test-add-with-funcs.py: -------------------------------------------------------------------------------- 1 | 2 | # The first operand of a binary operation are getting evaluated twice. 3 | 4 | x = 0 5 | def f(): 6 | global x 7 | x += 1 8 | return x 9 | 10 | ___assertEqual(f(), 1) 11 | 12 | x = 0 13 | ___assertEqual(f() + 0, 1) 14 | 15 | x = 0 16 | ___assertEqual(f() + 0 + 0, 1) 17 | -------------------------------------------------------------------------------- /programs/smeding/generator-raising-stops.py: -------------------------------------------------------------------------------- 1 | # raising any exception in a generator will stop the generator 2 | 3 | class E(Exception) : 4 | pass 5 | 6 | def foo() : 7 | raise E() 8 | yield 1 9 | 10 | f = foo() 11 | 12 | try : 13 | next(f) 14 | except E as e: 15 | pass 16 | 17 | expectException(StopIteration) 18 | next(f) 19 | -------------------------------------------------------------------------------- /programs/smeding/object-new-type.py: -------------------------------------------------------------------------------- 1 | # object's new function can only produce objects descendents of object 2 | 3 | # this is more illustrating than a direct call to object.__new__ 4 | class A(int) : 5 | def __new__(self) : 6 | return object.__new__(self) # we should call int.__new__ 7 | 8 | expectException(TypeError) 9 | A() 10 | -------------------------------------------------------------------------------- /programs/testany.py: -------------------------------------------------------------------------------- 1 | assert any(()) is False 2 | assert any((1,)) is True, any((1,)) 3 | assert any((0,)) is False 4 | assert any((0, 0, 0, 0)) is False 5 | assert any((0, 0, 0, 1)) is True 6 | 7 | class A: 8 | x = 0 9 | def __bool__(self): A.x += 1; return True 10 | 11 | assert any((A(), A(), A(), A())) 12 | assert A.x == 1 13 | -------------------------------------------------------------------------------- /programs/testio5.py: -------------------------------------------------------------------------------- 1 | import _io 2 | i = _io.FileIO("programs/files/FileIO") 3 | assert i.__enter__() is i 4 | assert i.read() == b"abc\ndef\nghi\n" == _io.FileIO("programs/files/FileIO").readall() 5 | assert i.__exit__() is None 6 | assert i.closed 7 | try: 8 | i.__enter__() 9 | assert False 10 | except ValueError: 11 | pass 12 | -------------------------------------------------------------------------------- /programs/testsets.py: -------------------------------------------------------------------------------- 1 | x = set() 2 | x |= {"5"} 3 | assert x == {"5"} 4 | assert x < {"5", "6"} 5 | assert x <= {"5", "6"} 6 | assert x <= {"5"} 7 | assert x != {"5", "6"} 8 | assert not(x < set()) 9 | assert not(x < {"5"}) 10 | assert not(x <= set()) 11 | assert not(x != {"5"}) 12 | x |= {"6", "7"} 13 | assert x == {"5", "6", "7"} 14 | -------------------------------------------------------------------------------- /programs/teststrings8.py: -------------------------------------------------------------------------------- 1 | assert ",".join([]) == "" 2 | assert ",".join(["foo"]) == "foo" 3 | assert ",".join(["foo", "bar", "baz"]) == "foo,bar,baz", ",".join(["foo", "bar", "baz"]) 4 | 5 | try: 6 | ",".join(5) 7 | except TypeError: 8 | try: 9 | ",".join([5]) 10 | except TypeError: 11 | x = 5 12 | 13 | assert x == 5 14 | -------------------------------------------------------------------------------- /programs/smeding/try-finally-yield.py: -------------------------------------------------------------------------------- 1 | # the finally clause is not executed when we yield from its body 2 | 3 | def foo() : 4 | try : 5 | yield None 6 | finally : 7 | fail() 8 | 9 | # this will print a warning because the finally block 10 | # is executed when the generator is garbage collected 11 | next(foo()) 12 | -------------------------------------------------------------------------------- /programs/testintegers9.py: -------------------------------------------------------------------------------- 1 | assert hash(0) == 0 2 | assert hash(1) == 1 3 | assert hash(-1) == -2 4 | assert hash(-2) == -2 5 | assert hash(2305843009213693951) == 0 6 | assert hash(2305843009213693952) == 1 7 | assert hash(-2305843009213693951) == 0 8 | assert hash(-2305843009213693952) == -2 9 | assert hash(-2305843009213693953) == -2 10 | -------------------------------------------------------------------------------- /programs/testyield7.py: -------------------------------------------------------------------------------- 1 | try: 2 | def a(): 3 | try: 4 | raise ValueError 5 | except ValueError: 6 | yield 5 7 | raise 8 | b=a() 9 | b.__next__() 10 | raise 11 | except RuntimeError: 12 | x = 5 13 | assert x == 5 14 | 15 | try: 16 | b.__next__() 17 | except ValueError: 18 | x = 6 19 | assert x == 6 20 | -------------------------------------------------------------------------------- /programs/politz/assertRaises.py: -------------------------------------------------------------------------------- 1 | def ___assertRaises(e, f, *args): 2 | try: 3 | f(*args) 4 | except e as the_exn: 5 | return 6 | else: 7 | assert(False) 8 | assert(False) 9 | 10 | def check(x): 11 | if not x: 12 | raise KeyError 13 | else: 14 | return x 15 | 16 | ___assertRaises(KeyError, check, 0) 17 | -------------------------------------------------------------------------------- /programs/politz/super-external.py: -------------------------------------------------------------------------------- 1 | class A: 2 | def f(self): 3 | return 'A' 4 | 5 | # super() in an external function 6 | def external(): 7 | super().f() + 'E' 8 | 9 | class EE(A): 10 | def f(self): 11 | return external() 12 | 13 | # SystemError("super(): no arguments") 14 | ___assertRaises(SystemError, EE().f) 15 | -------------------------------------------------------------------------------- /programs/politz/super-nested-self.py: -------------------------------------------------------------------------------- 1 | class A: 2 | def f(self): 3 | return 'A' 4 | 5 | # super() in a nested function with "self" argument 6 | class EE(A): 7 | def f(self): 8 | def nested(this): 9 | return super().f() + 'E' 10 | return nested(self) 11 | 12 | ___assertEqual(EE().f(), 'AE') 13 | -------------------------------------------------------------------------------- /programs/politz/test_ex5_from_c3_switch.py: -------------------------------------------------------------------------------- 1 | # see thread python-dev/2002-October/029035.html 2 | # Testing ex5 from C3 switch discussion... 3 | class A(object): pass 4 | class B(object): pass 5 | class C(object): pass 6 | class X(A): pass 7 | class Y(A): pass 8 | class Z(X,B,Y,C): pass 9 | ___assertEqual(Z.__mro__, (Z, X, B, Y, A, C, object)) 10 | -------------------------------------------------------------------------------- /programs/smeding/imported/imported_self.py: -------------------------------------------------------------------------------- 1 | # this is imported by a test in imports 2 | # importing oneself does not actually create a loop 3 | # this does seem a bit strange though, as the same error does not occur for a circular import 4 | 5 | import imported.imported_self 6 | 7 | imported.imported_self # imported_self is not yet bound at this point 8 | -------------------------------------------------------------------------------- /programs/testfuncraise1.py: -------------------------------------------------------------------------------- 1 | x = 5 2 | def b(): 3 | global x 4 | try: 5 | assert x == 5 6 | x = 6 7 | raise 8 | assert False 9 | finally: 10 | assert x == 6 11 | x = 7 12 | assert False 13 | 14 | try: 15 | assert b() and False 16 | except RuntimeError: 17 | assert x == 7 18 | x = 8 19 | 20 | assert x == 8 21 | -------------------------------------------------------------------------------- /programs/politz/unboundlocal-after-del.py: -------------------------------------------------------------------------------- 1 | def errorInOuter(): 2 | y = 1 3 | del y 4 | y 5 | def inner(): 6 | return y 7 | 8 | def errorInInner(): 9 | def inner(): 10 | return y 11 | y = 1 12 | del y 13 | inner() 14 | 15 | ___assertRaises(UnboundLocalError, errorInOuter) 16 | ___assertRaises(NameError, errorInInner) 17 | 18 | -------------------------------------------------------------------------------- /programs/smeding/function-scope-del-attr.py: -------------------------------------------------------------------------------- 1 | # attributes can be deleted as they are considered locals 2 | 3 | def foo(x) : 4 | del x 5 | 6 | foo(None) 7 | 8 | 9 | # we would expect a local unbound when the variable is used after deletion 10 | 11 | def foo(x) : 12 | del x 13 | x 14 | 15 | expectException(UnboundLocalError) 16 | foo(None) 17 | -------------------------------------------------------------------------------- /programs/testtry11.py: -------------------------------------------------------------------------------- 1 | try: 2 | try: 3 | raise 4 | assert False 5 | except RuntimeError: 6 | raise SyntaxError 7 | assert False 8 | except SyntaxError as e: 9 | assert e.__context__.__class__ == RuntimeError 10 | assert e.__cause__ is None 11 | assert not e.__suppress_context__ 12 | assert e.__traceback__.tb_next is None 13 | -------------------------------------------------------------------------------- /programs/smeding/class-scope-function-shadow.py: -------------------------------------------------------------------------------- 1 | # variables in class functions never refer to a class variable 2 | 3 | setDescr("Variables in class function can not be shadowed by class attributes") 4 | 5 | a = 0 6 | 7 | class A(object) : 8 | a = 1 9 | def f(self) : 10 | return a # returns 0, not 1 11 | 12 | assertTrue(A().f() == 0) 13 | -------------------------------------------------------------------------------- /programs/smeding/exception-basic-nocatch.py: -------------------------------------------------------------------------------- 1 | setDescr("Caught an exception when it shouldn't be caught") 2 | 3 | class TestException(Exception) : 4 | pass 5 | class OtherException(Exception) : 6 | pass 7 | 8 | try : 9 | try : 10 | raise TestException() 11 | except OtherException as e: 12 | fail() 13 | except TestException as e: 14 | pass 15 | -------------------------------------------------------------------------------- /programs/smeding/for-raise-stopiter-in-body.py: -------------------------------------------------------------------------------- 1 | # check how raising the StopIteration exception in the body stops execution 2 | # it should just stop with the raised StopIteration exception, not as if 3 | # the end of the list has been reached 4 | 5 | expectException(StopIteration) 6 | 7 | for i in [1] : 8 | raise StopIteration() 9 | 10 | fail() 11 | -------------------------------------------------------------------------------- /programs/smeding/for-type-baditerator-nonext.py: -------------------------------------------------------------------------------- 1 | # check what happens if the iterator returned for the list has no next func 2 | 3 | class A(object) : 4 | def __iter__(self) : 5 | return BadIterator() 6 | 7 | class BadIterator(object) : 8 | def __iter__(self) : 9 | return self 10 | 11 | expectException(TypeError) 12 | for i in A() : 13 | pass 14 | -------------------------------------------------------------------------------- /programs/testmockimport3.py: -------------------------------------------------------------------------------- 1 | def __import__(name, globals=None, locals=None, fromlist=(), level=0): 2 | assert name == "" 3 | assert fromlist == ("foo",) 4 | assert level == 1 5 | class A: pass 6 | a = A() 7 | a.foo = 5 8 | return a 9 | 10 | import builtins 11 | builtins.__import__ = __import__ 12 | from . import foo 13 | assert foo == 5 14 | -------------------------------------------------------------------------------- /programs/testscope10.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | def test(): 4 | foo = 5 5 | class Test: 6 | foo = 6 7 | def bar(self): return foo 8 | try: 9 | raise 10 | except RuntimeError: 11 | baz = sys.exc_info()[2].tb_frame.f_code 12 | return Test() 13 | a = test() 14 | assert a.baz.co_freevars == ('foo',) 15 | assert a.bar() == 5 16 | -------------------------------------------------------------------------------- /programs/politz/extra-nesting.py: -------------------------------------------------------------------------------- 1 | def make_adder2(x): 2 | def extra(): # check freevars passing through non-use scopes 3 | def adder(y): 4 | return x + y 5 | return adder 6 | return extra() 7 | 8 | inc = make_adder2(1) 9 | plus10 = make_adder2(10) 10 | 11 | ___assertEqual(inc(1), 2) 12 | ___assertEqual(plus10(-2), 8) 13 | -------------------------------------------------------------------------------- /programs/politz/test-aliasing.py: -------------------------------------------------------------------------------- 1 | # aliasing for built-in object 2 | x = [] 3 | y = x 4 | x.append(1) 5 | ___assertEqual(y, [1]) 6 | 7 | # aliasing for user-defined object 8 | class A: 9 | pass 10 | 11 | a = A() 12 | b = a 13 | a.x = 1 14 | ___assertEqual(b.x, 1) 15 | 16 | # aliasing for class object 17 | B = A 18 | A.x = 2 19 | ___assertEqual(B.x, 2) 20 | -------------------------------------------------------------------------------- /programs/smeding/try-finally-scope-noblock-raise.py: -------------------------------------------------------------------------------- 1 | # a variable that is defined in the try block, but before rising the exception 2 | # will still be in scope in the finally clause 3 | 4 | try : 5 | try : 6 | a = True 7 | raise Exception() 8 | except Exception as e: 9 | b = True 10 | pass 11 | finally : 12 | assertTrue(a) 13 | assertTrue(b) 14 | -------------------------------------------------------------------------------- /programs/testreversed2.py: -------------------------------------------------------------------------------- 1 | class A: 2 | def __getitem__(self, idx): return idx 3 | 4 | try: 5 | reversed(5) 6 | assert False, 5 7 | except TypeError: 8 | try: 9 | reversed(A()) 10 | assert False, "A" 11 | except TypeError: 12 | A.__len__ = lambda self: 2 13 | assert list(reversed(A())) == [1, 0] 14 | x = 5 15 | assert x == 5 16 | -------------------------------------------------------------------------------- /programs/testtry8.py: -------------------------------------------------------------------------------- 1 | try: 2 | try: 3 | raise 4 | assert False 5 | finally: 6 | try: 7 | raise TypeError 8 | assert False 9 | except TypeError as e: 10 | assert e.__context__.__class__ == RuntimeError 11 | x = 10 12 | assert False 13 | except RuntimeError: 14 | y = 11 15 | assert x == 10 16 | assert y == 11 17 | -------------------------------------------------------------------------------- /programs/politz/nesting-plus-free-ref-to-global.py: -------------------------------------------------------------------------------- 1 | def make_adder6(x): 2 | global global_nest_x 3 | def adder(y): 4 | return global_nest_x + y 5 | global_nest_x = x 6 | return adder 7 | 8 | inc = make_adder6(1) 9 | plus10 = make_adder6(10) 10 | 11 | ___assertEqual(inc(1), 11) # there's only one global 12 | ___assertEqual(plus10(-2), 8) 13 | -------------------------------------------------------------------------------- /programs/politz/try-except-else-finally-no-exception.py: -------------------------------------------------------------------------------- 1 | hit_except = False 2 | hit_else = False 3 | hit_finally = False 4 | 5 | try: 6 | pass 7 | except: 8 | hit_except = True 9 | else: 10 | hit_else = True 11 | finally: 12 | hit_finally = True 13 | 14 | ___assertFalse(hit_except) 15 | ___assertTrue(hit_finally) 16 | ___assertTrue(hit_else) 17 | -------------------------------------------------------------------------------- /programs/smeding/class-scope-shadow.py: -------------------------------------------------------------------------------- 1 | # variables defined inside a class can use eachother 2 | # and they shadow earlier defined variables 3 | 4 | setDescr("Faulty variable binding in class definition") 5 | 6 | a = 0 7 | class A(object) : 8 | a = 1 9 | b = a # should refer to the last a 10 | 11 | assertTrue(A().a == 1) 12 | assertTrue(A().b == 1) 13 | -------------------------------------------------------------------------------- /programs/smeding/function-closure-env-notseq.py: -------------------------------------------------------------------------------- 1 | # The environment in the body of a function that is inherited from the 2 | # surrounding block, is not a copy, but a reference. As a consequence one can 3 | # use variables defined after the function definition, but before the call 4 | 5 | def foo() : 6 | return x 7 | 8 | x = 1 9 | 10 | assertTrue(foo() == 1) 11 | -------------------------------------------------------------------------------- /programs/smeding/imported/imported_circular_shared_env_A.py: -------------------------------------------------------------------------------- 1 | # start of the circle by import B 2 | import imported.imported_circular_shared_env_B 3 | 4 | # the variable x is defined only after the import, but it will be in scope 5 | # when this module is imported by a 3rd module 6 | x = 1 7 | 8 | # we could call B.foo here, but the test functions aren't in scope here 9 | -------------------------------------------------------------------------------- /programs/smeding/metaclass-inherited-simple.py: -------------------------------------------------------------------------------- 1 | class Meta(type) : 2 | t = 0 3 | 4 | def __new__(self, name, bases, dict) : 5 | trace(self.t, None) 6 | self.t = 1 7 | return type.__new__(self, name, bases, dict) 8 | 9 | class A(object, metaclass=Meta) : 10 | pass 11 | 12 | class B(A) : 13 | pass 14 | 15 | trace(2,None) 16 | -------------------------------------------------------------------------------- /programs/testscope4.py: -------------------------------------------------------------------------------- 1 | x = 5 2 | def a(): 3 | global x 4 | x = 6 5 | a() 6 | assert x == 6, x 7 | assert "x" in a.__code__.co_names 8 | 9 | def b(): 10 | x = 7 11 | def c(): 12 | nonlocal x 13 | x = 8 14 | c() 15 | assert x == 8 16 | assert "x" in c.__code__.co_freevars 17 | b() 18 | assert x == 6 19 | assert "x" in b.__code__.co_cellvars 20 | -------------------------------------------------------------------------------- /programs/testsetattr6.py: -------------------------------------------------------------------------------- 1 | class A: 2 | def __get__(self, instance, owner): 3 | global x 4 | x += 1 5 | assert x > 1 6 | return self 7 | z = 1 8 | 9 | class B: 10 | y = A() 11 | 12 | def c(): 13 | global x 14 | x += 1 15 | assert x == 1 16 | return 2 17 | 18 | x = 0 19 | 20 | b = B() 21 | b.y.z = c() 22 | 23 | assert b.y.z == 2 24 | -------------------------------------------------------------------------------- /programs/politz/bool-compare.py: -------------------------------------------------------------------------------- 1 | ___assertIs(1==1, True) 2 | ___assertIs(1==0, False) 3 | ___assertIs(0<1, True) 4 | ___assertIs(1<0, False) 5 | ___assertIs(0<=0, True) 6 | ___assertIs(1<=0, False) 7 | ___assertIs(1>0, True) 8 | ___assertIs(1>1, False) 9 | ___assertIs(1>=1, True) 10 | ___assertIs(0>=1, False) 11 | ___assertIs(0!=1, True) 12 | ___assertIs(0!=0, False) 13 | -------------------------------------------------------------------------------- /programs/politz/dict-get.py: -------------------------------------------------------------------------------- 1 | d = {} 2 | ___assertIs(d.get('c'), None) 3 | ___assertEqual(d.get('c', 3), 3) 4 | d = {'a': 1, 'b': 2} 5 | ___assertIs(d.get('c'), None) 6 | ___assertEqual(d.get('c', 3), 3) 7 | ___assertEqual(d.get('a'), 1) 8 | ___assertEqual(d.get('a', 3), 1) 9 | ___assertRaises(TypeError, d.get) 10 | #___assertRaises(TypeError, d.get, None, None, None) 11 | -------------------------------------------------------------------------------- /programs/politz/try-except-else-finally.py: -------------------------------------------------------------------------------- 1 | hit_except = False 2 | hit_else = False 3 | hit_finally = False 4 | 5 | try: 6 | raise Exception('nyaa!') 7 | except: 8 | hit_except = True 9 | else: 10 | hit_else = True 11 | finally: 12 | hit_finally = True 13 | 14 | ___assertTrue(hit_except) 15 | ___assertTrue(hit_finally) 16 | ___assertFalse(hit_else) 17 | -------------------------------------------------------------------------------- /programs/smeding/getattribute-class-func-type.py: -------------------------------------------------------------------------------- 1 | # using a metaclass to override getattribute of a type shows that 2 | # type.__getattribute__ performs the type check for class functions 3 | 4 | class Meta(type) : 5 | def __getattribute__(self, attr) : 6 | return True 7 | 8 | class A(object, metaclass=Meta) : 9 | x = False 10 | 11 | assertTrue(A.x) 12 | -------------------------------------------------------------------------------- /programs/smeding/instantiation-nopars.py: -------------------------------------------------------------------------------- 1 | # class functions without parameters (and therefore no self) can be created, 2 | # but not called as attributes 3 | 4 | setDescr("Calling a non-instantiatable function should throw an exception") 5 | 6 | class A(object) : 7 | def f() : 8 | return None 9 | 10 | try : 11 | A().f() 12 | except TypeError as e: 13 | pass 14 | -------------------------------------------------------------------------------- /programs/smeding/try-except-type-catch-subtype.py: -------------------------------------------------------------------------------- 1 | # an except clause only catches exceptions that are (sub)types of the catch 2 | # classes sharing a common base will not catch oneanother 3 | 4 | class A(Exception) : 5 | pass 6 | class B(A) : 7 | pass 8 | 9 | try : 10 | try : 11 | raise A() 12 | except B as e : 13 | fail() 14 | except A as e : 15 | pass 16 | -------------------------------------------------------------------------------- /programs/testscope3.py: -------------------------------------------------------------------------------- 1 | def a(): 2 | x = 5 3 | def b(): 4 | def c(): 5 | assert x == 5 6 | c() 7 | assert "x" in b.__code__.co_freevars 8 | b() 9 | def d(): 10 | def e(): 11 | x = 6 12 | assert x == 6 13 | e() 14 | assert "x" not in d.__code__.co_freevars 15 | assert "x" not in d.__code__.co_cellvars 16 | d() 17 | a() 18 | -------------------------------------------------------------------------------- /programs/smeding/for-type-baditerator-wrongnext.py: -------------------------------------------------------------------------------- 1 | # check what happens if the iterator's next has the wrong type 2 | 3 | class A(object) : 4 | def __iter__(self) : 5 | return BadIterator() 6 | 7 | class BadIterator(object) : 8 | def __iter__(self) : 9 | return self 10 | 11 | next = 1 12 | 13 | expectException(TypeError) 14 | 15 | for i in A() : 16 | pass 17 | -------------------------------------------------------------------------------- /programs/smeding/module-override-class.py: -------------------------------------------------------------------------------- 1 | # an imported module can define a variable __class__ but it will never be 2 | # exported, because it is overridden 3 | 4 | import imported.imported_module_override_class 5 | 6 | # access __class__ variable in imported module because it is overridden 7 | assertTrue(not (imported.imported_module_override_class.__class__.__class__ is int)) 8 | -------------------------------------------------------------------------------- /programs/smeding/try-except-type-catch-siblingclass.py: -------------------------------------------------------------------------------- 1 | # an except clause only catches exceptions that are (sub)types of the catch 2 | # classes sharing a common base will not catch oneanother 3 | class A(Exception) : 4 | pass 5 | class B(Exception) : 6 | pass 7 | 8 | try : 9 | try : 10 | raise A() 11 | except B as e : 12 | fail() 13 | except A as e : 14 | pass 15 | -------------------------------------------------------------------------------- /programs/politz/super-external-self.py: -------------------------------------------------------------------------------- 1 | class A: 2 | def f(self): 3 | return 'A' 4 | 5 | # super() in an external function with "self" argument 6 | def external(self): 7 | super().f() + 'E' 8 | 9 | class EE(A): 10 | def f(self): 11 | return external(self) 12 | 13 | # SystemError("super(): no arguments") 14 | ___assertRaises(SystemError, EE().f) 15 | -------------------------------------------------------------------------------- /programs/smeding/if-else-nonzero-returntype.py: -------------------------------------------------------------------------------- 1 | # if __nonzero__ does not return a boolean, a type error should be raised 2 | 3 | class A(object) : 4 | def __bool__(self) : 5 | assertTrue(self.cnt < 1) 6 | self.cnt = self.cnt + 1 7 | return self 8 | 9 | a = A() 10 | a.cnt = 0 11 | 12 | expectException(TypeError) 13 | 14 | if a : 15 | fail() 16 | else : 17 | fail() 18 | -------------------------------------------------------------------------------- /programs/politz/nonlocal-function.py: -------------------------------------------------------------------------------- 1 | def f(x): 2 | def inc(): 3 | nonlocal x 4 | x += 1 5 | return x 6 | def dec(): 7 | nonlocal x 8 | x -= 1 9 | return x 10 | return inc, dec 11 | 12 | inc, dec = f(0) 13 | ___assertEqual(inc(), 1) 14 | ___assertEqual(inc(), 2) 15 | ___assertEqual(dec(), 1) 16 | ___assertEqual(dec(), 0) 17 | -------------------------------------------------------------------------------- /programs/politz/super-nested-noarg.py: -------------------------------------------------------------------------------- 1 | class A: 2 | def f(self): 3 | return 'A' 4 | 5 | # super() in a nested function without arguments 6 | class EE(A): 7 | def f(self): 8 | def nested(): 9 | return super().f() + 'E' 10 | return nested() 11 | 12 | # SystemError("super(): no arguments") 13 | ___assertRaises(SystemError, EE().f) 14 | -------------------------------------------------------------------------------- /programs/smeding/instantiation-parname.py: -------------------------------------------------------------------------------- 1 | # instantiation doesn't care about the attribute name 2 | # using 'self' is only a convention 3 | 4 | setDescr("Selfs seems to be implicitly instantiated") 5 | 6 | self = True 7 | 8 | class A(object) : 9 | def f(a) : 10 | return self 11 | def g(a,self) : 12 | return self 13 | 14 | assertTrue(A().f()) 15 | assertTrue(A().g(1) == 1) 16 | -------------------------------------------------------------------------------- /programs/testcoercion1.py: -------------------------------------------------------------------------------- 1 | class A: 2 | def __lt__(self, other): return NotImplemented 3 | def __le__(self, other): return None 4 | 5 | a = A() 6 | 7 | try: 8 | a < a 9 | assert False 10 | except TypeError: pass 11 | 12 | assert (a <= a) is None 13 | 14 | class B(A): 15 | def __gt__(self, other): return True 16 | 17 | b = B() 18 | 19 | assert b > a 20 | assert a < b 21 | -------------------------------------------------------------------------------- /programs/politz/test-classmethod-desugar.py: -------------------------------------------------------------------------------- 1 | class A: 2 | # this 3 | @classmethod 4 | def cm1(cls): 5 | return cls 6 | # is just syntactic sugar for: 7 | def cm2(cls): 8 | return cls 9 | cm2 = classmethod(cm2) 10 | 11 | ___assertEqual(A.cm1(), A) 12 | ___assertEqual(A().cm1(), A) 13 | ___assertEqual(A.cm2(), A) 14 | ___assertEqual(A().cm2(), A) 15 | -------------------------------------------------------------------------------- /programs/politz/test_floats.py: -------------------------------------------------------------------------------- 1 | if 12.0 + 24.0 != 36.0: raise Exception('float op +') 2 | if 12.0 + (-24.0) != -12.0: raise Exception('float op +/-') 3 | if (-12.0) + 24.0 != 12.0: raise Exception('float op -/+') 4 | if (-12.0) + (-24.0) != -36.0: raise Exception('float op -/+/-') 5 | if not 12.0 < 24.0: raise Exception('float op <') 6 | if not -24.0 < -12.0: raise Exception('float op < negative') 7 | -------------------------------------------------------------------------------- /programs/smeding/class-scope-func.py: -------------------------------------------------------------------------------- 1 | # try to access a locally defined class variable in a function 2 | 3 | setDescr("Class function scope should not include other class attributes") 4 | 5 | class A(object) : 6 | a = 3 7 | def f(self) : 8 | return a # a shouldn't be in scope! 9 | 10 | try : 11 | A().f() 12 | fail() 13 | except NameError as n : 14 | pass 15 | -------------------------------------------------------------------------------- /programs/smeding/for-type-iter-in-object.py: -------------------------------------------------------------------------------- 1 | # the __iter__ function for the list has to be in the class, not the object 2 | 3 | class A(object) : 4 | pass 5 | 6 | a = A() 7 | 8 | def __iter__() : 9 | return [1].__iter__() # note that this is a perfectly valid iterator 10 | 11 | a.__iter__ = __iter__ 12 | 13 | 14 | expectException(TypeError) 15 | 16 | for i in a : 17 | pass 18 | -------------------------------------------------------------------------------- /programs/testclasses8.py: -------------------------------------------------------------------------------- 1 | class A(int): 2 | def __new__(cls, foo, bar, baz): 3 | return 5 4 | assert foo == "foo" 5 | assert bar == "bar" 6 | assert baz == "baz" 7 | def __init__(self, foo, bar, baz): 8 | assert self.__class__ is A 9 | assert foo == "foo" 10 | assert bar == "bar" 11 | assert baz == "baz" 12 | 13 | assert A("foo", "bar", "baz") == 5 14 | -------------------------------------------------------------------------------- /programs/politz/for-loops.py: -------------------------------------------------------------------------------- 1 | x = 12 2 | y = 13 3 | 4 | def f(): 5 | for x in [3,4]: 6 | for y in [5, 6]: 7 | pass 8 | 9 | assert x == 12 10 | assert y == 13 11 | 12 | class c: 13 | for x in [7,8]: 14 | for y in [9, 10]: 15 | pass 16 | 17 | 18 | assert x == 12 19 | assert y == 13 20 | 21 | assert c().x == 8 22 | assert c().y == 10 23 | 24 | -------------------------------------------------------------------------------- /programs/politz/keywords_args1.py: -------------------------------------------------------------------------------- 1 | def f(a, b=2, c=3): 2 | return (a, b, c) 3 | 4 | # mix keywords and defaults 5 | ___assertEqual(f(1, b=22, c=33), (1, 22, 33)) 6 | ___assertEqual(f(a=1, b=22, c=33), (1, 22, 33)) 7 | ___assertEqual(f(1, b=22), (1, 22, 3)) 8 | ___assertEqual(f(a=1, b=22), (1, 22, 3)) 9 | ___assertEqual(f(1, c=33), (1, 2, 33)) 10 | ___assertEqual(f(a=1, c=33), (1, 2, 33)) 11 | -------------------------------------------------------------------------------- /programs/politz/tuple-constructors.py: -------------------------------------------------------------------------------- 1 | # calling built-in types without argument must return empty 2 | ___assertEqual(tuple(), ()) 3 | t0_3 = (0, 1, 2, 3) 4 | t0_3_bis = tuple(t0_3) 5 | ___assertTrue(t0_3 is t0_3_bis) 6 | ___assertEqual(tuple([]), ()) 7 | ___assertEqual(tuple([0, 1, 2, 3]), (0, 1, 2, 3)) 8 | ___assertEqual(tuple(''), ()) 9 | ___assertEqual(tuple('spam'), ('s', 'p', 'a', 'm')) 10 | -------------------------------------------------------------------------------- /programs/smeding/if-else-scope-remove.py: -------------------------------------------------------------------------------- 1 | setDescr("Variables can be removed in the block") 2 | 3 | a = 1 4 | 5 | if True : 6 | del a 7 | else : 8 | pass 9 | 10 | try : 11 | a 12 | fail() 13 | except NameError as e : 14 | pass 15 | 16 | 17 | a = 1 18 | 19 | if False : 20 | pass 21 | else : 22 | del a 23 | 24 | try : 25 | a 26 | fail() 27 | except NameError as e : 28 | pass 29 | -------------------------------------------------------------------------------- /programs/testeval3.py: -------------------------------------------------------------------------------- 1 | def a(*x): assert x == (); return 5 2 | def b(**x): assert x == {}; return 5 3 | def c(*x, **y): 4 | assert x == () 5 | assert y == {} 6 | return 5 7 | 8 | assert eval(a.__code__) == 5 9 | assert eval(b.__code__) == 5 10 | assert eval(c.__code__) == 5 11 | assert exec(a.__code__) is None 12 | assert exec(b.__code__) is None 13 | assert exec(c.__code__) is None 14 | -------------------------------------------------------------------------------- /programs/politz/assertion-error-msg.py: -------------------------------------------------------------------------------- 1 | # test assertion error msg 2 | def assertion_test(test, good, bad): 3 | try: 4 | assert test, bad 5 | result = good 6 | except AssertionError as e: 7 | result = e.args[0] 8 | return result 9 | 10 | ___assertEqual(assertion_test(True, "Good", "Bad"), "Good") 11 | ___assertEqual(assertion_test(False, "Good", "Bad"), "Bad") 12 | -------------------------------------------------------------------------------- /programs/politz/test_file.py: -------------------------------------------------------------------------------- 1 | source = "a.txt" 2 | f = open(source, "w") 3 | f.write("hello\n") 4 | f.write("world\n") 5 | f.close() 6 | 7 | f = open(source, "r") 8 | ___assertEqual(f.read(), "hello\nworld\n") 9 | ___assertEqual(f.read(), "") 10 | f.close() 11 | 12 | f = open(source, "r") 13 | ___assertEqual(f.readline(), "hello\n") 14 | ___assertEqual(f.readline(), "world\n") 15 | f.close() 16 | -------------------------------------------------------------------------------- /programs/smeding/getattribute-class-func-instantiation.py: -------------------------------------------------------------------------------- 1 | # overloading object.__getattribute__ to disable instantiation of foo 2 | 3 | class A(object) : 4 | def __getattribute__(self, attr) : 5 | if attr in A.__dict__ : 6 | return A.__dict__[attr] 7 | else : 8 | return object.__getattribute__(self, attr) 9 | 10 | def foo(x) : 11 | return x 12 | 13 | assertTrue(A().foo(1) == 1) 14 | -------------------------------------------------------------------------------- /programs/politz/callable.py: -------------------------------------------------------------------------------- 1 | ___assertTrue(callable(len)) 2 | ___assertFalse(callable("a")) 3 | ___assertTrue(callable(callable)) 4 | ___assertTrue(callable(lambda x, y: x + y)) 5 | 6 | def f(): pass 7 | ___assertTrue(callable(f)) 8 | 9 | class C1: 10 | def meth(self): pass 11 | ___assertTrue(callable(C1)) 12 | c = C1() 13 | ___assertTrue(callable(c.meth)) 14 | ___assertFalse(callable(c)) 15 | 16 | -------------------------------------------------------------------------------- /programs/smeding/exception-trace-raise.py: -------------------------------------------------------------------------------- 1 | # tests on the order of execution when raising, catch and finally clauses 2 | 3 | class TestException(Exception) : 4 | pass 5 | 6 | try : 7 | try : 8 | try : 9 | raise trace(0, TestException()) 10 | except trace(1, ValueError) as e: 11 | fail() 12 | except trace(2, TestException) as e : 13 | trace(3, None) 14 | finally : 15 | trace(4, None) 16 | -------------------------------------------------------------------------------- /programs/smeding/lists-iter-fail-append-fail.py: -------------------------------------------------------------------------------- 1 | # if next() raises the StopIteration exception, it should continue to do that 2 | # on subsequent calls, even if the list is extended 3 | 4 | list = [1] 5 | it = list.__iter__() 6 | 7 | next(it) 8 | 9 | try : 10 | next(it) 11 | except StopIteration as e : 12 | pass 13 | 14 | list.append(2) 15 | 16 | expectException(StopIteration) 17 | next(it) 18 | -------------------------------------------------------------------------------- /programs/politz/memofib-function-attributes.py: -------------------------------------------------------------------------------- 1 | # test use of function atributes, in this case to memoize fib. 2 | def memofib(n): 3 | try: 4 | if n in memofib.cache: return memofib.cache[n] 5 | except AttributeError: 6 | memofib.cache = {} 7 | memofib.cache[n] = n if n < 2 else memofib(n-1) + memofib(n-2) 8 | return memofib.cache[n] 9 | 10 | ___assertEqual(memofib(12), 144) 11 | -------------------------------------------------------------------------------- /programs/politz/metaclass-function.py: -------------------------------------------------------------------------------- 1 | def f(name, bases, dict): 2 | dict.__delitem__("x") 3 | return type(name, bases, dict) 4 | 5 | x = "global x" 6 | class C(metaclass=f): 7 | x = "class var" 8 | y = x 9 | def m(self): 10 | return x 11 | 12 | ___assertFalse(hasattr(C, x)) 13 | ___assertEqual(C.y, "class var") 14 | ___assertEqual(C().m(), "global x") 15 | ___assertIs(C.__class__, type) 16 | -------------------------------------------------------------------------------- /programs/smeding/function-scope-shadow-callself.py: -------------------------------------------------------------------------------- 1 | # override the binding and call it even if it's not a function 2 | # to make sure it doesn't infer which binding to use 3 | 4 | setDescr("Function name was not shadowed in the function body when called as function.") 5 | 6 | def foo(foo, i) : 7 | if 0 < i : 8 | foo(foo, i-1) 9 | else : 10 | pass 11 | 12 | expectException(TypeError) 13 | 14 | foo(1,1) 15 | -------------------------------------------------------------------------------- /programs/smeding/getattribute-in-parent.py: -------------------------------------------------------------------------------- 1 | # one can inherit from a class that has custom attribute retrieval 2 | # but the child will use the inherited __getattribute__ 3 | 4 | class A(object) : 5 | def __getattribute__(self, attr) : 6 | return 1 7 | 8 | class B(A) : 9 | x = 2 10 | 11 | assertTrue(B().y == 1) 12 | assertTrue(B().x == 1) 13 | 14 | b = B() 15 | b.z = 2 16 | 17 | assertTrue(b.z == 1) 18 | -------------------------------------------------------------------------------- /programs/testcoercion2.py: -------------------------------------------------------------------------------- 1 | class A: 2 | def __lt__(self, other): return True 3 | 4 | class B(A): 5 | def __init__(self, x): self.x = x 6 | def __gt__(self, other): 7 | if self.x == 5: 8 | return NotImplemented 9 | else: 10 | return False 11 | 12 | a = A() 13 | b1 = B(1) 14 | b5 = B(5) 15 | assert a < a 16 | assert a < b5 17 | assert b5 > a 18 | assert not a < b1 19 | assert not b1 > a 20 | -------------------------------------------------------------------------------- /programs/politz/locals-function-renamed.py: -------------------------------------------------------------------------------- 1 | # locals called from another identifier 2 | def f(x): 3 | mylocals = locals 4 | def g(y): 5 | def h(z): 6 | return y + z 7 | w = x + y 8 | y += 3 9 | return mylocals() 10 | return g 11 | 12 | d = f(2)(4) 13 | ___assertIn('h', d) 14 | del d['h'] 15 | del d['mylocals'] 16 | ___assertEqual(d, {'x': 2, 'y': 7, 'w': 6}) 17 | -------------------------------------------------------------------------------- /programs/politz/mixed-freevars-and-cellvars.py: -------------------------------------------------------------------------------- 1 | def identity(x): 2 | return x 3 | 4 | def f(x, y, z): 5 | def g(a, b, c): 6 | a = a + x # 3 7 | def h(): 8 | # z * (4 + 9) 9 | # 3 * 13 10 | return identity(z * (b + y)) 11 | y = c + z # 9 12 | return h 13 | return g 14 | 15 | g = f(1, 2, 3) 16 | h = g(2, 4, 6) 17 | ___assertEqual(h(), 39) 18 | -------------------------------------------------------------------------------- /programs/politz/test_comparisons.py: -------------------------------------------------------------------------------- 1 | if 0 < 1 <= 1 == 1 >= 1 > 0 != 1: pass 2 | else: raise Exception('int comparisons failed') 3 | if 0.0 < 1.0 <= 1.0 == 1.0 >= 1.0 > 0.0 != 1.0: pass 4 | else: raise Exception('float comparisons failed') 5 | if '' < 'a' <= 'a' == 'a' < 'abc' < 'abd' < 'b': pass 6 | else: raise Exception('string comparisons failed') 7 | if None is None: pass 8 | else: raise Exception('identity test failed') 9 | -------------------------------------------------------------------------------- /programs/smeding/imported/imported_circular_shared_env_B.py: -------------------------------------------------------------------------------- 1 | # linking back to A, thus completing the circle 2 | import imported.imported_circular_shared_env_A 3 | 4 | # at this point A.x is not yet defined 5 | 6 | # foo uses A.x in its body, which is not executed yet 7 | def foo() : 8 | return imported.imported_circular_shared_env_A.x 9 | 10 | 11 | # A.x still isn't defined, so a call to foo() would fail here 12 | -------------------------------------------------------------------------------- /programs/testexec3.py: -------------------------------------------------------------------------------- 1 | assert exec("""try: 2 | assert False 3 | raise SystemError 4 | except AssertionError: pass""") is None 5 | try: 6 | exec("assert False") 7 | raise SystemError 8 | except AssertionError: pass 9 | 10 | try: 11 | exec('exec("assert False")') 12 | raise SystemError 13 | except AssertionError: pass 14 | 15 | try: 16 | exec("return 5") 17 | assert False 18 | except SyntaxError: pass 19 | -------------------------------------------------------------------------------- /programs/testweakref.py: -------------------------------------------------------------------------------- 1 | import _weakref 2 | import gc 3 | 4 | class A: 5 | pass 6 | 7 | def p(weakref): 8 | global x 9 | assert weakref() is None 10 | x += 1 11 | 12 | x = 0 13 | w1 = _weakref.ref(A()) 14 | gc.collect() 15 | assert x == 0 16 | assert w1() is None 17 | a = A() 18 | w2 = _weakref.ref(a) 19 | w3 = _weakref.ref(a, p) 20 | w4 = _weakref.ref(a, p) 21 | del a 22 | gc.collect() 23 | assert x == 2 24 | -------------------------------------------------------------------------------- /programs/testwith3.py: -------------------------------------------------------------------------------- 1 | class A: 2 | def __enter__(self): 3 | self.x = 0 4 | def __exit__(self, type, value, traceback): 5 | assert self.x == 0 6 | self.x = 1 7 | assert type is value.__class__ is RuntimeError 8 | assert traceback.tb_next is None 9 | return False 10 | 11 | try: 12 | with A(): y = 5; raise 13 | except RuntimeError: 14 | assert y == 5 15 | z = 6 16 | assert z == 6 17 | -------------------------------------------------------------------------------- /programs/politz/filter-comprehension.py: -------------------------------------------------------------------------------- 1 | # test filter predicates in list comprehensions 2 | ___assertEqual([x for x in [1, 'hello', [], [3], '', None, 9, 0] if x], [1, 'hello', [3], 9]) 3 | ___assertEqual([x for x in [1, -3, 9, 0, 2] if x > 0], [1, 9, 2]) 4 | ___assertEqual([c for c in 'Hello World' if 'a' <= c <= 'z'], list('elloorld')) 5 | ___assertEqual([c for c in 'Hello World' if 'a' <= c <= 'z' and c != 'l'], list('eoord')) 6 | -------------------------------------------------------------------------------- /programs/smeding/for-type-baditerator-rmnext.py: -------------------------------------------------------------------------------- 1 | # see what happens to an iterator that removes its own next function 2 | 3 | class A(object) : 4 | def __iter__(self) : 5 | return BadIterator() 6 | 7 | class BadIterator(object) : 8 | def __iter__(self) : 9 | return self 10 | def __next__(self) : 11 | del BadIterator.__next__ 12 | return 1 13 | 14 | expectException(TypeError) 15 | for i in A() : 16 | pass 17 | -------------------------------------------------------------------------------- /programs/smeding/metaclass-type-new-return-True.py: -------------------------------------------------------------------------------- 1 | 2 | class M(type) : 3 | def __init__(self, name, bases, dict) : 4 | fail() 5 | 6 | def __new__(self, name, bases, dict) : 7 | trace(0, None) 8 | return N(name, bases, dict) 9 | 10 | 11 | class N(type) : 12 | def __init__(self, name, bases, dict) : 13 | trace(1, None) 14 | 15 | 16 | class A(object, metaclass=M) : 17 | pass 18 | 19 | 20 | trace(2, None) 21 | -------------------------------------------------------------------------------- /programs/smeding/while-type-rmnonzero.py: -------------------------------------------------------------------------------- 1 | # see if we can remove the __nonzero__ function of the while's test 2 | # while the loop is running 3 | 4 | class A(object) : 5 | def __bool__(self) : 6 | return trace(2,False) 7 | 8 | class B(A) : 9 | def __bool__(self) : 10 | del B.__bool__ 11 | return trace(0,True) 12 | 13 | b = B() 14 | 15 | while b : 16 | trace(1,None) # this trace ensures we don't loop endlessly 17 | -------------------------------------------------------------------------------- /programs/politz/gen-return.py: -------------------------------------------------------------------------------- 1 | def f(): 2 | yield 1 3 | return 4 | yield 3 5 | 6 | g = f() 7 | assert next(g) == 1 8 | try: 9 | next(g) 10 | except StopIteration: 11 | pass 12 | else: 13 | raise Exception("return inside of generator raises StopIteration") 14 | 15 | try: 16 | next(g) 17 | except StopIteration: 18 | pass 19 | else: 20 | raise Exception("should have gotten StopIteration") 21 | -------------------------------------------------------------------------------- /programs/politz/nested.py: -------------------------------------------------------------------------------- 1 | hit_finally = False 2 | hit_inner_except = False 3 | hit_inner_finally = False 4 | 5 | try: 6 | try: 7 | raise Exception('inner exception') 8 | except: 9 | hit_inner_except = True 10 | finally: 11 | hit_inner_finally = True 12 | finally: 13 | hit_finally = True 14 | 15 | ___assertTrue(hit_inner_except) 16 | ___assertTrue(hit_inner_finally) 17 | ___assertTrue(hit_finally) 18 | -------------------------------------------------------------------------------- /programs/politz/nonlocal-method.py: -------------------------------------------------------------------------------- 1 | def f(x): 2 | class c: 3 | def inc(self): 4 | nonlocal x 5 | x += 1 6 | return x 7 | def dec(self): 8 | nonlocal x 9 | x -= 1 10 | return x 11 | return c() 12 | c = f(0) 13 | ___assertEqual(c.inc(), 1) 14 | ___assertEqual(c.inc(), 2) 15 | ___assertEqual(c.dec(), 1) 16 | ___assertEqual(c.dec(), 0) 17 | -------------------------------------------------------------------------------- /programs/testyield1.py: -------------------------------------------------------------------------------- 1 | def a(): 2 | assert (yield 5) == 5 3 | assert (yield 6) == 6 4 | return 7 5 | 6 | b = a() 7 | assert b.__next__() == 5 8 | assert b.send(5) == 6 9 | try: 10 | b.send(6) 11 | assert False 12 | except StopIteration as e: 13 | assert e.args == (7,) 14 | 15 | b = a() 16 | b.close() 17 | try: 18 | b.__next__() 19 | assert False 20 | except StopIteration: 21 | x = 5 22 | assert x == 5 23 | -------------------------------------------------------------------------------- /programs/politz/dict-set-keys.py: -------------------------------------------------------------------------------- 1 | k1 = {1:1, 2:2}.keys() 2 | k2 = {1:1, 2:2, 3:3}.keys() 3 | k3 = {4:4}.keys() 4 | 5 | ___assertEqual(k1 - k2, set()) 6 | ___assertEqual(k1 - k3, {1,2}) 7 | ___assertEqual(k2 - k1, {3}) 8 | ___assertEqual(k3 - k1, {4}) 9 | ___assertEqual(k1 & k2, {1,2}) 10 | ___assertEqual(k1 & k3, set()) 11 | ___assertEqual(k1 | k2, {1,2,3}) 12 | ___assertEqual(k1 ^ k2, {3}) 13 | ___assertEqual(k1 ^ k3, {1,2,4}) 14 | -------------------------------------------------------------------------------- /programs/politz/super-nested-not-self.py: -------------------------------------------------------------------------------- 1 | class A: 2 | def f(self): 3 | return 'A' 4 | 5 | # super() in a nested function with non-self argument 6 | class EE(A): 7 | def f(self): 8 | def nested(self): 9 | return super().f() + 'E' 10 | return nested(5) 11 | 12 | # TypeError("super(type, obj): obj must be an instance or subtype of type") 13 | ___assertRaises(TypeError, EE().f) 14 | -------------------------------------------------------------------------------- /programs/testdicts1.py: -------------------------------------------------------------------------------- 1 | x = {"5": "6", "6": "7"} 2 | assert x["5"] == "6" and x["6"] == "7" 3 | assert x == {"5": "6", "6": "7"} 4 | x["7"] = "8" 5 | assert x == {"5": "6", "6": "7", "7": "8"} 6 | assert x["7"] == "8" 7 | del x["5"] 8 | assert x == {"6": "7", "7": "8"} 9 | 10 | try: 11 | x["5"] 12 | assert False 13 | except KeyError: 14 | pass 15 | 16 | assert dict(x)["6"] == "7" 17 | assert dict(x) == {"6": "7", "7": "8"} 18 | -------------------------------------------------------------------------------- /programs/politz/gen-exception.py: -------------------------------------------------------------------------------- 1 | def f(): 2 | raise Exception 3 | while True: 4 | yield 1 5 | 6 | g = f() 7 | try: 8 | g.__next__() 9 | except Exception: 10 | pass 11 | else: 12 | raise Exception("g.__next__() should have raised 'Exception'") 13 | 14 | try: 15 | g.__next__() 16 | except StopIteration: 17 | pass 18 | else: 19 | raise Exception("g.__next__() should have raised StopIteration") 20 | -------------------------------------------------------------------------------- /programs/politz/isinstance.py: -------------------------------------------------------------------------------- 1 | class C: 2 | pass 3 | class D(C): 4 | pass 5 | class E: 6 | pass 7 | c = C() 8 | d = D() 9 | e = E() 10 | ___assertTrue(isinstance(c, C)) 11 | ___assertTrue(isinstance(d, C)) 12 | ___assertTrue(not isinstance(e, C)) 13 | ___assertTrue(not isinstance(c, D)) 14 | ___assertTrue(not isinstance('foo', E)) 15 | ___assertRaises(TypeError, isinstance, E, 'foo') 16 | ___assertRaises(TypeError, isinstance) 17 | -------------------------------------------------------------------------------- /programs/testfunctions7.py: -------------------------------------------------------------------------------- 1 | x = False 2 | def g(): 3 | try: 4 | a 5 | assert False 6 | except UnboundLocalError: 7 | x = 5 8 | assert x == 5 9 | def a(): 10 | nonlocal x 11 | x = x + 1 12 | assert a() is None 13 | assert x == 6 14 | try: 15 | e 16 | assert False 17 | except UnboundLocalError as e: 18 | x = 7 19 | assert x == 7 20 | return a 21 | assert g()() is None 22 | assert x == False 23 | -------------------------------------------------------------------------------- /programs/politz/metaclass-class1.py: -------------------------------------------------------------------------------- 1 | class Meta(type): 2 | def __new__(self, name, bases, dict): 3 | dict.__delitem__("x") 4 | return type(name, bases, dict) 5 | 6 | x = "global x" 7 | class C(metaclass=Meta): 8 | x = "class var" 9 | y = x 10 | def m(self): 11 | return x 12 | 13 | ___assertFalse(hasattr(C, x)) 14 | ___assertEqual(C.y, "class var") 15 | ___assertEqual(C().m(), "global x") 16 | ___assertIs(C.__class__, type) 17 | -------------------------------------------------------------------------------- /programs/politz/test_failing_support_sticks.py: -------------------------------------------------------------------------------- 1 | # If an error occurs during importing the module, 2 | # the module will not be added to sys.modules 3 | import sys 4 | import support 5 | 6 | source = support.TESTFN + ".py" 7 | 8 | # import the new file, which contains a error 9 | ___assertRaises(ZeroDivisionError, __import__, support.TESTFN); 10 | # the sys.modules shouldn't contain the TESTFN 11 | ___assertNotIn(support.TESTFN, sys.modules) 12 | -------------------------------------------------------------------------------- /programs/politz/test_simple_string_ops.py: -------------------------------------------------------------------------------- 1 | if 'xyz' + 'abcde' != 'xyzabcde': raise Exception('string concatenation') 2 | if 'xyz'*3 != 'xyzxyzxyz': raise Exception('string repetition *3') 3 | if 0*'abcde' != '': raise Exception('string repetition 0*') 4 | if min('abc') != 'a' or max('abc') != 'c': raise Exception('min/max string') 5 | if 'a' in 'abc' and 'b' in 'abc' and 'c' in 'abc' and 'd' not in 'abc': pass 6 | else: raise Exception('in/not in string') 7 | -------------------------------------------------------------------------------- /programs/testgetattr5.py: -------------------------------------------------------------------------------- 1 | class A: 2 | def __getattribute__(self, key): assert False 3 | 4 | try: 5 | getattr(A(), 5) 6 | except TypeError: 7 | try: 8 | object.__getattribute__(5, 5) 9 | except TypeError: 10 | try: 11 | type.__getattribute__(int, 5) 12 | except TypeError: 13 | try: 14 | super.__getattribute__(super(bool, True), 5) 15 | except TypeError: 16 | x = 5 17 | 18 | assert x == 5 19 | -------------------------------------------------------------------------------- /programs/testscope2.py: -------------------------------------------------------------------------------- 1 | x = 5 2 | def a(): 3 | x = 6 4 | assert x == 6 5 | a() 6 | assert "x" in a.__code__.co_varnames 7 | 8 | def b(): 9 | x = 6 10 | def c(): 11 | assert x == 6 12 | assert x == 6 13 | c() 14 | assert "x" in c.__code__.co_freevars 15 | b() 16 | assert "x" in b.__code__.co_cellvars 17 | 18 | a = 7 19 | def c(): 20 | def d(): 21 | assert a == 7 22 | d() 23 | assert "a" in d.__code__.co_names 24 | c() 25 | -------------------------------------------------------------------------------- /programs/testsetattr3.py: -------------------------------------------------------------------------------- 1 | def a(): pass 2 | 3 | x = 5 4 | y = "x" 5 | a.__module__ = x 6 | a.__name__ = y 7 | assert a.__module__ is x == 5 8 | assert a.__name__ == y == "x" 9 | 10 | x = 6 11 | y = "y" 12 | setattr(a, "__module__", x) 13 | setattr(a, "__name__", y) 14 | assert a.__module__ is x == 6 15 | assert a.__name__ == y == "y" 16 | 17 | try: 18 | a.__name__ = 5 19 | assert False 20 | except TypeError: 21 | z = 1 22 | assert z == 1 23 | -------------------------------------------------------------------------------- /programs/politz/keywords_args2.py: -------------------------------------------------------------------------------- 1 | def f(a, b=2, c=3): 2 | return (a, b, c) 3 | 4 | # mix keywords, **expression and defaults 5 | ___assertEqual(f(1, **{'b':22, 'c':33}), (1, 22, 33)) 6 | ___assertEqual(f(a=1, **{'b':22, 'c':33}), (1, 22, 33)) 7 | ___assertEqual(f(1, **{'b':22}), (1, 22, 3)) 8 | ___assertEqual(f(**{'a':1, 'b':22}), (1, 22, 3)) 9 | ___assertEqual(f(1, **{'c':33}), (1, 2, 33)) 10 | ___assertEqual(f(**{'a':1, 'c':33}), (1, 2, 33)) 11 | -------------------------------------------------------------------------------- /programs/testrepr1.py: -------------------------------------------------------------------------------- 1 | assert repr("\x00\x1f\x7f") == "'\\x00\\x1f\\x7f'", repr("\x00\x1f\x7f") 2 | assert repr("foo") == "'foo'" 3 | assert repr("foo ' bar") == "\"foo ' bar\"" 4 | assert repr("foo \" bar") == "'foo \" bar'" 5 | assert repr("foo ' \" bar") == "'foo \\' \" bar'" 6 | assert repr("foo\\bar") == "'foo\\\\bar'" 7 | assert repr("foo\nbar") == "'foo\\nbar'" 8 | assert repr("foo\tbar") == "'foo\\tbar'" 9 | assert repr("foo\rbar") == "'foo\\rbar'" 10 | -------------------------------------------------------------------------------- /programs/politz/basic-gen.py: -------------------------------------------------------------------------------- 1 | def basic(): 2 | yield 1 3 | yield 2 4 | 5 | g = basic() 6 | ___assertEqual(1, next(g)) 7 | ___assertEqual(2, next(g)) 8 | ___assertRaises(StopIteration, next, g) 9 | 10 | def f(): 11 | x = 0 12 | while x < 5: 13 | yield x 14 | x += 1 15 | 16 | g = f() 17 | # generator object should return itself when calling iter 18 | ___assertIs(g, iter(g)) 19 | ___assertEqual(list(g), list(range(5))) 20 | ___assertEqual(list(g), []) 21 | -------------------------------------------------------------------------------- /programs/politz/metaclass-class2.py: -------------------------------------------------------------------------------- 1 | class Meta(type): 2 | def __new__(self, name, bases, dict): 3 | dict.__delitem__("x") 4 | return type.__new__(self, name, bases, dict) 5 | 6 | x = "global x" 7 | class C(metaclass=Meta): 8 | x = "class var" 9 | y = x 10 | def m(self): 11 | return x 12 | 13 | ___assertFalse(hasattr(C, x)) 14 | ___assertEqual(C.y, "class var") 15 | ___assertEqual(C().m(), "global x") 16 | ___assertIs(C.__class__, Meta) 17 | -------------------------------------------------------------------------------- /programs/smeding/instantiation-objasfunc.py: -------------------------------------------------------------------------------- 1 | # object-functions i.e., objects with the __call__ attribute pointing to a 2 | # function are always instantiated with the object that contains the function 3 | 4 | class A(object) : 5 | test = True 6 | 7 | def __call__(self) : 8 | return self.test 9 | 10 | class B(object) : 11 | test = False 12 | 13 | foo = A() # this creates an instance of A, it doesn't call A.__call__ ! 14 | 15 | assertTrue(B().foo()) 16 | -------------------------------------------------------------------------------- /programs/politz/gen-return-uncatchable.py: -------------------------------------------------------------------------------- 1 | def f(): 2 | try: 3 | raise StopIteration 4 | except StopIteration: 5 | yield 1 6 | 7 | try: 8 | return 9 | except StopIteration: 10 | yield 2 11 | 12 | yield 3 13 | 14 | g = f() 15 | 16 | assert next(g) == 1 17 | 18 | try: 19 | next(g) 20 | except StopIteration: 21 | pass 22 | else: 23 | raise Exception("return should not be catchable within generator") 24 | -------------------------------------------------------------------------------- /programs/politz/nesting-global-no-free.py: -------------------------------------------------------------------------------- 1 | def make_adder4(): # XXX add exta level of indirection 2 | def nest(): 3 | def nest(): 4 | def adder(y): 5 | return global_x + y # check that plain old globals work 6 | return adder 7 | return nest() 8 | return nest() 9 | 10 | global_x = 1 11 | adder = make_adder4() 12 | ___assertEqual(adder(1), 2) 13 | 14 | global_x = 10 15 | ___assertEqual(adder(-2), 8) 16 | -------------------------------------------------------------------------------- /programs/testmockimport4.py: -------------------------------------------------------------------------------- 1 | def __import__(name, globals=None, locals=None, fromlist=(), level=0): 2 | assert name == "foo.bar.baz" 3 | assert fromlist is None 4 | assert level == 0 5 | class A: pass 6 | a = A() 7 | a.foo = 1 8 | a.bar = A() 9 | a.bar.baz = 2 10 | return a 11 | 12 | x = 0 13 | import builtins 14 | builtins.__import__ = __import__ 15 | import foo.bar.baz 16 | assert foo.foo == 1 17 | import foo.bar.baz as bar 18 | assert bar == 2 19 | -------------------------------------------------------------------------------- /programs/smeding/for-type-stopiter-faulty.py: -------------------------------------------------------------------------------- 1 | # if next raises anything other than a StopIteration 2 | # the exception is passed on 3 | 4 | class A(object) : 5 | def __iter__(self) : 6 | return MyIterator() 7 | 8 | class MyIterator(object) : 9 | def __iter__(self) : 10 | return self 11 | 12 | def __next__(self) : 13 | raise MyException() 14 | 15 | class MyException(Exception) : 16 | pass 17 | 18 | expectException(MyException) 19 | for i in A() : 20 | pass 21 | -------------------------------------------------------------------------------- /programs/smeding/setattr-trace.py: -------------------------------------------------------------------------------- 1 | # check inheritance of setattr: only the class attribute __setattr__ should 2 | # be called, the object's own setattr is off limits 3 | 4 | class A(object) : 5 | def __setattr__(self, attr, val) : 6 | trace(0, None) 7 | 8 | def __setattr__(self, attr, val) : 9 | pass 10 | 11 | a = A() 12 | object.__setattr__(a, "__setattr__", __setattr__) 13 | 14 | a.x = 1 # should increace the trace by one through A.__setattr__ 15 | trace(1, None) 16 | -------------------------------------------------------------------------------- /programs/politz/range-errs.py: -------------------------------------------------------------------------------- 1 | ___assertRaises(TypeError, range) 2 | ___assertRaises(TypeError, range, 1, 2, 3, 4) 3 | ___assertRaises(ValueError, range, 1, 2, 0) 4 | 5 | ___assertRaises(TypeError, range, 0.0, 2, 1) 6 | ___assertRaises(TypeError, range, 1, 2.0, 1) 7 | ___assertRaises(TypeError, range, 1, 2, 1.0) 8 | ___assertRaises(TypeError, range, 1e100, 1e101, 1e101) 9 | 10 | ___assertRaises(TypeError, range, 0, "spam") 11 | ___assertRaises(TypeError, range, 0, 42, "spam") 12 | -------------------------------------------------------------------------------- /programs/politz/test_basic.py: -------------------------------------------------------------------------------- 1 | # should be able to import sys module 2 | # and it should be present in sys.modules 3 | 4 | import sys 5 | ___assertIn("sys", sys.modules) 6 | 7 | import sys as sys2 8 | ___assertIs(sys, sys2) 9 | ___assertIs(sys.path, sys2.path) 10 | 11 | from sys import path 12 | from sys import path as path2 13 | ___assertIs(path, sys.path) 14 | ___assertIs(path2, sys.path) 15 | 16 | ___assertEqual(sys.__name__, "sys") 17 | ___assertEqual(sys2.__name__, "sys") 18 | -------------------------------------------------------------------------------- /programs/smeding/function-object-as-func-trace.py: -------------------------------------------------------------------------------- 1 | # check what the exact order of evaluation is when executing an object with 2 | # the call attribute set 3 | 4 | class CallableObject(object) : 5 | def __call__(self, arg) : 6 | return trace(2, None) 7 | 8 | # the attribute is not actually obtained using this 9 | #TODO: perhaps it does use the metatype's getattribute? 10 | def __getattribute__(self, attr) : 11 | fail() 12 | 13 | trace(0,CallableObject())(trace(1,None)) 14 | -------------------------------------------------------------------------------- /programs/testclasses6.py: -------------------------------------------------------------------------------- 1 | def helper(t): 2 | try: 3 | t() 4 | assert False 5 | except TypeError: pass 6 | 7 | import types 8 | 9 | helper(types.GeneratorType) 10 | helper(types.BuiltinFunctionType) 11 | helper(types.TracebackType) 12 | helper(types.FrameType) 13 | helper(types.FunctionType) 14 | helper(types.MethodType) 15 | 16 | def a(): 17 | x = 5 18 | def b(): return x 19 | return b 20 | 21 | CellType = a().__closure__[0].__class__ 22 | helper(CellType) 23 | -------------------------------------------------------------------------------- /programs/smeding/for-trace-basic.py: -------------------------------------------------------------------------------- 1 | # run a trace of the basic for loop 2 | 3 | class L(object) : 4 | def __iter__(self) : 5 | i = I() 6 | return trace(0, I()) 7 | 8 | class I(object) : 9 | 10 | def __iter__(self) : 11 | fail() 12 | 13 | cnt = True 14 | 15 | def __next__(self) : 16 | if I.cnt : 17 | I.cnt = False 18 | return trace(1, "foo") 19 | else : 20 | trace(3, None) 21 | raise StopIteration() 22 | 23 | 24 | for i in L() : 25 | trace(2, None) 26 | -------------------------------------------------------------------------------- /programs/smeding/for-type-stopiter-subtype.py: -------------------------------------------------------------------------------- 1 | # the for loop stops not just when next raises StopIteration, but also 2 | # when it raises a child of StopIteration 3 | 4 | class A(object) : 5 | def __iter__(self) : 6 | return MyIterator() 7 | 8 | class MyIterator(object) : 9 | def __iter__(self) : 10 | return self 11 | 12 | def __next__(self) : 13 | raise MyStopIteration() 14 | 15 | class MyStopIteration(StopIteration) : 16 | pass 17 | 18 | for i in A() : 19 | pass 20 | -------------------------------------------------------------------------------- /programs/testassert3.py: -------------------------------------------------------------------------------- 1 | x = compile("assert False", "foo", "exec", 0, 0, 1) 2 | assert exec(x) is None 3 | x = compile(""" 4 | y = compile("assert False", "foo", "exec", 0, 0, -1) 5 | assert exec(y) is None 6 | """, "foo", "exec", 0, 0, 1) 7 | assert exec(x) is None 8 | x = compile(""" 9 | y = compile("assert False", "foo", "exec", 0, 0, 0) 10 | try: 11 | exec(y) 12 | raise SystemError 13 | except AssertionError: pass 14 | """, "foo", "exec", 0, 0, 1) 15 | assert exec(x) is None 16 | -------------------------------------------------------------------------------- /programs/politz/dict-values-set.py: -------------------------------------------------------------------------------- 1 | k1 = {1:1, 2:2}.items() 2 | k2 = {1:1, 2:2, 3:3}.items() 3 | k3 = {4:4}.items() 4 | 5 | ___assertEqual(k1 - k2, set()) 6 | ___assertEqual(k1 - k3, {(1,1), (2,2)}) 7 | ___assertEqual(k2 - k1, {(3,3)}) 8 | ___assertEqual(k3 - k1, {(4,4)}) 9 | ___assertEqual(k1 & k2, {(1,1), (2,2)}) 10 | ___assertEqual(k1 & k3, set()) 11 | ___assertEqual(k1 | k2, {(1,1), (2,2), (3,3)}) 12 | ___assertEqual(k1 ^ k2, {(3,3)}) 13 | ___assertEqual(k1 ^ k3, {(1,1), (2,2), (4,4)}) 14 | -------------------------------------------------------------------------------- /programs/politz/global-in-parallel-nested-functions.py: -------------------------------------------------------------------------------- 1 | # h closes over the variable y, and so returns it with 2 | # the value it had locally when declared. However, g 3 | # declares y as a global variable, which then picks up 4 | # the top-level definition y = 9. 5 | 6 | def f(): 7 | y = 1 8 | def g(): 9 | global y 10 | return y 11 | def h(): 12 | return y + 1 13 | return g, h 14 | 15 | y = 9 16 | g, h = f() 17 | ___assertEqual(g(), 9) 18 | ___assertEqual(h(), 2) 19 | -------------------------------------------------------------------------------- /programs/testclasses1.py: -------------------------------------------------------------------------------- 1 | class A: 2 | x = 5 3 | 4 | assert A.__doc__ is None 5 | assert A.__module__ == "__main__" 6 | assert A.__name__ == "A" 7 | assert A.__bases__ == (object,) 8 | assert A.__mro__ == (A, object) 9 | assert A.x == 5 10 | 11 | class B(int, A): pass 12 | 13 | assert B.__bases__ == (int, A) 14 | assert B.__mro__ == (B, int, A, object) 15 | 16 | class C(B, * (int, A)): pass 17 | 18 | assert C.__bases__ == (B, int, A) 19 | assert C.__mro__ == (C, B, int, A, object) 20 | -------------------------------------------------------------------------------- /programs/testscope7.py: -------------------------------------------------------------------------------- 1 | def a(): 2 | x = 5 3 | def b(): 4 | def c(): 5 | nonlocal x 6 | assert x == 5 7 | del x 8 | try: 9 | x 10 | assert False 11 | except NameError: 12 | pass 13 | assert x == 5 14 | c() 15 | try: 16 | x 17 | assert False 18 | except NameError: 19 | pass 20 | assert x == 5 21 | b() 22 | try: 23 | x 24 | assert False 25 | except NameError: 26 | pass 27 | a() 28 | -------------------------------------------------------------------------------- /programs/politz/isinstance-tuple.py: -------------------------------------------------------------------------------- 1 | class C: 2 | pass 3 | class D(C): 4 | pass 5 | class E: 6 | pass 7 | class F(C, E): 8 | pass 9 | c = C() 10 | d = D() 11 | e = E() 12 | f = F() 13 | ___assertTrue(isinstance(c, (C, E))) 14 | ___assertTrue(isinstance(d, (E, C))) 15 | ___assertTrue(not isinstance(e, (C, F))) 16 | ___assertTrue(not isinstance(c, (F, D))) 17 | ___assertTrue(not isinstance('foo', (C, D, E, F))) 18 | ___assertTrue(isinstance(f, (E,))) 19 | ___assertTrue(isinstance(f, (C,))) -------------------------------------------------------------------------------- /programs/politz/keywords_args3.py: -------------------------------------------------------------------------------- 1 | def f(a, b=2, c=3): 2 | return (a, b, c) 3 | 4 | # mix keywords, defaults and *expression 5 | ___assertEqual(f(b=22, c=33, *[1]), (1, 22, 33)) 6 | ___assertEqual(f(b=22, *[1]), (1, 22, 3)) 7 | ___assertEqual(f(c=33, *[1]), (1, 2, 33)) 8 | 9 | # mix keywords, defaults, *expression and **expression 10 | ___assertEqual(f(b=22, *[1], **{'c':33}), (1, 22, 33)) 11 | ___assertEqual(f(*[1], **{'b':22}), (1, 22, 3)) 12 | ___assertEqual(f(*[1], **{'c':33}), (1, 2, 33)) 13 | -------------------------------------------------------------------------------- /programs/testformat3.py: -------------------------------------------------------------------------------- 1 | assert format("12345", ".3") == "123" 2 | assert format("12345", ".6") == "12345" 3 | assert format("12345", "3") == "12345" 4 | assert format("12345", "10") == "12345 " 5 | assert format("12345", "<10") == "12345 " 6 | assert format("12345", ">10") == " 12345" 7 | assert format("12345", "^9") == " 12345 " 8 | assert format("12345", "^10") == " 12345 " 9 | assert format("12345", "x>10") == "xxxxx12345" 10 | assert format("12345", "{^10.3") == "{{{123{{{{" 11 | -------------------------------------------------------------------------------- /programs/politz/metaclass-class3.py: -------------------------------------------------------------------------------- 1 | class Meta(type): 2 | def __new__(self, name, bases, dict): 3 | dict.__delitem__("x") 4 | return type.__new__(self, name, bases, dict) 5 | 6 | class B(metaclass=Meta): 7 | x = "class var" 8 | y = x 9 | 10 | x = "global x" 11 | class C(B): 12 | x = "class var" 13 | def m(self): 14 | return x 15 | 16 | ___assertFalse(hasattr(C, x)) 17 | ___assertEqual(C.y, "class var") 18 | ___assertEqual(C().m(), "global x") 19 | ___assertIs(C.__class__, Meta) 20 | -------------------------------------------------------------------------------- /programs/smeding/metaclass-basic-trace.py: -------------------------------------------------------------------------------- 1 | # check that both new and init of the metaclass are called.. in the correct order 2 | 3 | class M(type) : 4 | def __new__(self, name, bases, attrs) : 5 | trace(1, None) 6 | return type.__new__(self, name, bases, attrs) 7 | 8 | def __init__(self, name, bases, attrs) : 9 | trace(2, None) 10 | 11 | def __call__(self) : 12 | fail() 13 | 14 | class A(object, metaclass=M) : 15 | trace(0,None) 16 | 17 | trace(3,None) 18 | -------------------------------------------------------------------------------- /programs/testio1.py: -------------------------------------------------------------------------------- 1 | import _io 2 | i = _io._IOBase() 3 | assert i.__iter__() is i 4 | assert not i.closed 5 | assert i.close() is None 6 | assert i.closed 7 | i = _io._IOBase() 8 | assert i.__enter__() is i 9 | assert i.__exit__(1, 2, 3, 4, 5, 6, 7) is None 10 | assert i.closed 11 | i = _io._IOBase() 12 | assert i.flush() is None 13 | assert i.readline(0) == b"" 14 | assert not i.isatty() 15 | assert not i.readable() 16 | assert not i.seekable() 17 | assert not i.writable() 18 | assert i.writelines([]) is None 19 | -------------------------------------------------------------------------------- /programs/politz/simple_property.py: -------------------------------------------------------------------------------- 1 | class C(object): 2 | def __init__(self): 3 | self.x = 42 4 | def fget(self): 5 | self.x += 1 6 | return self.x 7 | def fset(self, value): 8 | self.x = value 9 | def fdel(self): 10 | del self.x 11 | f = property(fget, fset, fdel) 12 | 13 | c = C() 14 | assert c.x == 42 15 | assert c.f == 43 16 | c.f = 55 17 | assert c.x == 55 18 | assert c.f == 56 19 | del c.f 20 | assert not hasattr(c, 'x') 21 | assert not hasattr(c, 'f') 22 | assert hasattr(C, 'f') 23 | -------------------------------------------------------------------------------- /programs/politz/types_truthy1.py: -------------------------------------------------------------------------------- 1 | if None: raise Exception('None is true instead of false') 2 | if 0: raise Exception('0 is true instead of false') 3 | if 0.0: raise Exception('0.0 is true instead of false') 4 | if '': raise Exception('\'\' is true instead of false') 5 | if not 1: raise Exception('1 is false instead of true') 6 | if not 1.0: raise Exception('1.0 is false instead of true') 7 | if not 'x': raise Exception('\'x\' is false instead of true') 8 | if not {'x': 1}: raise Exception('{\'x\': 1} is false instead of true') 9 | -------------------------------------------------------------------------------- /programs/testgetattr3.py: -------------------------------------------------------------------------------- 1 | class A: 2 | def __getattr__(self, s): 3 | return 5 4 | 5 | assert not hasattr(A, "x") 6 | try: 7 | A.x 8 | assert False 9 | except AttributeError: 10 | try: 11 | getattr(A, "x") 12 | assert False 13 | except AttributeError: 14 | y = 4 15 | assert y == 4 16 | assert A().x == 5 17 | assert getattr(A(), "x") == 5 18 | assert hasattr(A(), "x") 19 | 20 | try: 21 | A().__getattribute__("x") 22 | assert False 23 | except AttributeError: 24 | x = 5 25 | assert x == 5 26 | -------------------------------------------------------------------------------- /programs/politz/all.py: -------------------------------------------------------------------------------- 1 | ___assertEqual(all([2, 4, 6]), True) 2 | ___assertEqual(all([2, None, 6]), False) 3 | ___assertRaises(TypeError, all, 10) # Non-iterable 4 | ___assertRaises(TypeError, all) # No args 5 | ___assertRaises(TypeError, all, [2, 4, 6], []) # Too many args 6 | ___assertEqual(all([]), True) # Empty iterator 7 | 8 | S = [50, 60] 9 | ___assertEqual(all(x > 42 for x in S), True) 10 | 11 | S = [50, 40, 60] 12 | ___assertEqual(all(x > 42 for x in S), False) 13 | -------------------------------------------------------------------------------- /programs/politz/simple_property_decorator.py: -------------------------------------------------------------------------------- 1 | class C(object): 2 | def __init__(self): 3 | self.x = 42 4 | @property 5 | def f(self): 6 | self.x += 1 7 | return self.x 8 | @f.setter 9 | def f(self, value): 10 | self.x = value 11 | @f.deleter 12 | def f(self): 13 | del self.x 14 | 15 | c = C() 16 | assert c.x == 42 17 | assert c.f == 43 18 | c.f = 55 19 | assert c.x == 55 20 | assert c.f == 56 21 | del c.f 22 | assert not hasattr(c, 'x') 23 | assert not hasattr(c, 'f') 24 | assert hasattr(C, 'f') 25 | -------------------------------------------------------------------------------- /programs/smeding/exception-basic-throwingcatch.py: -------------------------------------------------------------------------------- 1 | # raising an exception in the except clause will override the original exception 2 | 3 | class TestException(Exception) : 4 | pass 5 | 6 | def raiseException() : 7 | raise TestException() 8 | return TestException 9 | 10 | try : 11 | try : 12 | raise TestException() 13 | except raiseException() as e: # evaluating the function will throw an exception 14 | fail() # therefore, the body of the except cluase should not be entered 15 | except TestException as e: 16 | pass 17 | -------------------------------------------------------------------------------- /programs/testdel.py: -------------------------------------------------------------------------------- 1 | a, b, c, d, e = 1, 2, 3, 4, 5 2 | 3 | del a, b 4 | 5 | try: 6 | a 7 | assert False 8 | except NameError: 9 | pass 10 | 11 | try: 12 | b 13 | assert False 14 | except NameError: 15 | pass 16 | 17 | assert c == 3 and d == 4 and e == 5 18 | del (c, [d, (e,)]) 19 | 20 | try: 21 | c 22 | assert False 23 | except NameError: 24 | pass 25 | 26 | try: 27 | d 28 | assert False 29 | except NameError: 30 | pass 31 | 32 | try: 33 | e 34 | assert False 35 | except NameError: 36 | pass 37 | -------------------------------------------------------------------------------- /programs/testclasses5.py: -------------------------------------------------------------------------------- 1 | def helper(* t): 2 | try: 3 | class A(* t): pass 4 | assert False 5 | except TypeError: pass 6 | 7 | import types 8 | 9 | helper(int, float) 10 | helper(types.GeneratorType) 11 | helper(types.BuiltinFunctionType) 12 | helper(types.TracebackType) 13 | helper(types.CodeType) 14 | helper(types.FrameType) 15 | helper(types.MethodType) 16 | helper(slice) 17 | 18 | def a(): 19 | x = 5 20 | def b(): return x 21 | return b 22 | 23 | CellType = a().__closure__[0].__class__ 24 | helper(CellType) 25 | -------------------------------------------------------------------------------- /programs/testeval1.py: -------------------------------------------------------------------------------- 1 | class A: 2 | def keys(self): return () 3 | 4 | def a(): 5 | x = 5 6 | def b(): 7 | return x 8 | eval(b.__code__) 9 | 10 | def c(d): pass 11 | 12 | try: 13 | eval("5", {}, 5) 14 | assert False, 0 15 | except TypeError: 16 | try: 17 | eval("5", A()) 18 | assert False, 1 19 | except TypeError: 20 | try: 21 | a() 22 | assert False, 2 23 | except TypeError: 24 | try: 25 | eval(c.__code__) 26 | assert False, 3 27 | except TypeError: pass 28 | -------------------------------------------------------------------------------- /programs/testexec1.py: -------------------------------------------------------------------------------- 1 | class A: 2 | def keys(self): return () 3 | 4 | def a(): 5 | x = 5 6 | def b(): 7 | return x 8 | exec(b.__code__) 9 | 10 | def c(d): pass 11 | 12 | try: 13 | exec("5", {}, 5) 14 | assert False, 0 15 | except TypeError: 16 | try: 17 | exec("5", A()) 18 | assert False, 1 19 | except TypeError: 20 | try: 21 | a() 22 | assert False, 2 23 | except TypeError: 24 | try: 25 | exec(c.__code__) 26 | assert False, 3 27 | except TypeError: pass 28 | -------------------------------------------------------------------------------- /programs/testmockimport1.py: -------------------------------------------------------------------------------- 1 | def __import__(name, globals=None, locals=None, fromlist=(), level=0): 2 | assert name == "foo" 3 | assert fromlist == ("*",) 4 | assert level == 0 5 | class A: pass 6 | a = A() 7 | a.x = 5 8 | a._foo = 6 9 | a.__foo__ = 7 10 | return a 11 | 12 | import builtins 13 | builtins.__import__ = __import__ 14 | from foo import * 15 | assert x == 5 16 | try: 17 | _foo 18 | assert False 19 | except NameError: 20 | try: 21 | __foo__ 22 | assert False 23 | except NameError: pass 24 | -------------------------------------------------------------------------------- /programs/politz/any.py: -------------------------------------------------------------------------------- 1 | ___assertEqual(any([None, None, None]), False) 2 | ___assertEqual(any([None, 4, None]), True) 3 | ___assertRaises(TypeError, any, 10) # Non-iterable 4 | ___assertRaises(TypeError, any) # No args 5 | ___assertRaises(TypeError, any, [2, 4, 6], []) # Too many args 6 | ___assertEqual(any([]), False) # Empty iterator 7 | 8 | S = [40, 60, 30] 9 | ___assertEqual(any(x > 42 for x in S), True) 10 | 11 | S = [10, 20, 30] 12 | ___assertEqual(any(x > 42 for x in S), False) 13 | -------------------------------------------------------------------------------- /programs/politz/dunder-methods.py: -------------------------------------------------------------------------------- 1 | class Foo: 2 | def __getitem__(*a): 3 | return 0 4 | 5 | foo = Foo() 6 | ___assertEqual(foo[2], 0) 7 | 8 | 9 | # a[i] should use __getitem__ from class, not from object. 10 | # This applies to all __xxx__ methods, not just __getitem__. 11 | def getitem(*a): 12 | return 42 13 | foo.__getitem__ = getitem 14 | ___assertEqual(foo[2], 0) 15 | 16 | # The __xxx__ methods are for the objects. They shoundn't be accessible to the class it self. 17 | ___assertRaises(TypeError, lambda: Foo[1]) 18 | -------------------------------------------------------------------------------- /programs/testwith2.py: -------------------------------------------------------------------------------- 1 | class A: 2 | def __enter__(self): 3 | global x 4 | x = 0 5 | def __exit__(self, type, value, traceback): 6 | global x 7 | assert x == 1 8 | x = 2 9 | 10 | class B: 11 | def __enter__(self): 12 | global x 13 | def __exit__(self, type, value, traceback): 14 | global x 15 | assert x == 0 16 | x = 1 17 | assert type is value.__class__ is RuntimeError 18 | assert traceback.tb_next is None 19 | return True 20 | 21 | with A(), B(): y = 5; raise 22 | assert y == 5 23 | -------------------------------------------------------------------------------- /programs/politz/class-bases-scope.py: -------------------------------------------------------------------------------- 1 | # Test scoping of bases in class definition 2 | def f(): 3 | # C should be in the local scope 4 | class C: 5 | pass 6 | # if bases lookup is global this should fail 7 | class S(C): 8 | pass 9 | return S 10 | 11 | # Now we add S to the global scope and use it 12 | S = f() 13 | s = S() 14 | ___assertTrue(isinstance(s, S)) 15 | 16 | # But C shouldn't be in the global scope 17 | try: 18 | c = C() 19 | except NameError: 20 | pass 21 | else: 22 | ___assertFail() 23 | -------------------------------------------------------------------------------- /programs/politz/immutable-alias.py: -------------------------------------------------------------------------------- 1 | # alias shouldn't affect immutable object 2 | x = 1 3 | y = x 4 | x = 2 5 | ___assertEqual(y, 1) 6 | ___assertEqual(x, 2) 7 | 8 | y = 3 9 | ___assertEqual(x, 2) 10 | ___assertEqual(y, 3) 11 | 12 | t = (1,2) 13 | t1 = t 14 | t = (1,2,3) 15 | ___assertEqual(t1, (1,2)) 16 | 17 | t1 = (1,2,3,4) 18 | ___assertEqual(t1, (1,2,3,4)) 19 | ___assertEqual(t, (1,2,3)) 20 | 21 | s = "1" 22 | s1 = s 23 | s = "2" 24 | ___assertEqual(s1, "1") 25 | 26 | s1 = "3" 27 | ___assertEqual(s1, "3") 28 | ___assertEqual(s, "2") 29 | --------------------------------------------------------------------------------