mirror of
https://github.com/RustPython/RustPython.git
synced 2026-10-29 03:04:38 +08:00
* type lock * Drop old PyObjectRef outside type lock to prevent deadlock Dropping values inside with_type_lock can trigger weakref callbacks, which may access attributes (LOAD_ATTR specialization) and re-acquire the non-reentrant type mutex, causing deadlock. Return old values from lock closures so they drop after lock release. * Align type lock behavior with CPython * Add PUBLISHED flag bit to RefCount state word Assisted-by: Claude * Add QSBR module for deferred memory reclamation Assisted-by: Claude * Defer memory free of cache-published objects via QSBR Assisted-by: Claude * Wire QSBR checkpoints into thread lifecycle and eval breaker Assisted-by: Claude * Publish type-cache values to QSBR and bypass cache without a VM Mark cached type-method values as QSBR-published before storing their pointer in TYPE_CACHE, so racing readers never try-incref freed memory. Debug-assert that the current thread is ATTACHED whenever a lock-free cache read happens. Delete find_name_in_mro_without_vm and its unsound direct SeqLock read outside any VM/thread registration; find_name_in_mro now falls back to the locked, uncached MRO walk when no VM is current. This also removes assign_version_tag()'s last caller, so its unlocked fallback is deleted along with it (version_for_specialization keeps the locked path). VirtualMachine::initialize() runs Python bytecode (e.g. importing codecs/encodings) before any enter_vm scope exists, which left the bootstrap thread not ATTACHED for cache reads during startup. Add thread::VmBootstrapGuard, an RAII counterpart to enter_vm usable across statements that need &mut VirtualMachine, and wrap initialize() with it. Assisted-by: Claude * Use try-incref reads and QSBR-backed swaps in specialization cache Assisted-by: Claude * Reset QSBR thread registry after fork in the child Add Qsbr::reset_after_fork, which clears the registered thread slots before draining the retire queue. Call it in py_os_after_fork_child, right after type_cache_after_fork() and before _thread::after_fork_child() re-registers the surviving thread. Dead parent threads' Arc<QsbrSlot> handles live on in the child's copied memory with no destructor ever running, so without this the registry keeps them 'online' forever and future grace periods never complete. Assisted-by: Claude * Add threaded stress test for type cache mutation races Assisted-by: Claude * Restrict qsbr internals to pub(crate) and fix clippy lints Downgrade pub items in the private qsbr::threading module (QsbrSlot, Qsbr, QSBR, and its methods) to pub(crate), since they were never reachable outside the crate. Downgrade ThreadSlot::qsbr accordingly to avoid a private-interfaces warning. Import Arc/Weak from alloc instead of std to match clippy::std_instead_of_alloc. Rewrite the process() comment: goals are not strictly ordered by push order since advance() and the queue push aren't atomic together, so concurrent free_delayed calls can interleave; the drained prefix stays sound because each item individually passed poll, with reordering only affecting reclamation latency. Assisted-by: Claude * Gate per-instruction QSBR check behind a global pending flag eval_breaker_tripped() and check_signals() called thread::qsbr_break_requested() on every bytecode instruction, which does a thread-local lookup, RefCell borrow, and atomic load even when no QSBR retirement is pending. This caused a measured 10-18% slowdown on tight interpreter loops. Add Qsbr::pending, a global AtomicBool set under the queue lock while free_delayed() holds a retired allocation, and cleared under the same lock by process()/drain_all() once the queue empties. The hot path now checks Qsbr::break_pending() (a single relaxed static load) before touching thread-local state, and only pays the TLS cost while a retirement is actually in flight. Assisted-by: Claude * Merge signal and QSBR eval-breaker flags into one atomic word Replace the separate ANY_TRIGGERED AtomicBool and QSBR pending AtomicBool checks in the per-instruction eval-breaker path with a single AtomicU8 (EVAL_BREAKER) holding a bit per source. The global QSBR instance mirrors its pending state into the QSBR bit under the same queue-lock critical sections that already toggle pending, while local QSBR instances used by unit tests are left untouched. eval_breaker_tripped() and check_signals() now read/act on the merged word (eval_breaker_pending(), qsbr_bit_set()) instead of issuing two separate atomic loads per instruction. set_triggered() switches from store to fetch_or so a signal handler never clobbers the QSBR bit. Removed the now-unreferenced is_triggered() helper; break_pending() is kept for unit tests only and marked allow(dead_code) outside test builds. Assisted-by: Claude * Skip freelist reuse for published objects in default_dealloc Freelist-eligible payloads (tuple, int, list, dict, float, ...) were routed to T::freelist_push before ever reaching PyInner::dealloc, so a published object (e.g. a tuple cached as a type attribute) bypassed the is_published -> free_delayed QSBR hook entirely. That let PyRef::new_ref reuse the slot and rewrite the refcount word with a non-atomic core::ptr::write racing a reader's atomic try-incref, and let FreeList::drop call alloc::dealloc directly while a reader could still be mid-read. Gate the freelist branch in default_dealloc on RefCount::is_published(), checked before any teardown. A published object now always falls through to PyInner::dealloc, whose existing hook defers the memory free via QSBR. Document the non-unix (e.g. Windows) reclamation-latency limitation in the QSBR design doc's Lifecycle integration section (previously only noted in a code comment in vm/thread.rs). Extend the type-cache stress test so the mutator also churns a freelist-eligible published tuple class attribute (C.shape), read by readers each inner loop and deleted on the same cadence as C.m, to exercise the fixed bypass. Assisted-by: Claude * Use itoa for PyInt::to_str_radix_10 i64 fast path Format small integers with itoa::Buffer instead of i64::to_string() in PyInt::to_str_radix_10(), which backs both str(int)/repr(int) for exact int and PyObject::str()'s exact-int fast path. Adds the itoa crate as a workspace dependency. Assisted-by: Claude * Make object attribute dunders wrapper descriptors Remove the __getattribute__, __setattr__ and __delattr__ #[pymethod]s from PyBaseObject so add_operators installs PyWrapper slot wrappers for them from the #[pyslot] functions instead of method descriptors. lookup_slot_in_mro now classifies these entries as NativeSlot, so heap types without Python-level overrides keep the native getattro/setattro slot functions, which lets LOAD_ATTR/STORE_ATTR specialization apply to instances of Python-defined classes. Since __setattr__ and __delattr__ share the setattro slot, resolve both names together in update_one_slot: overriding only one of them no longer lets the other name's native resolution overwrite the dispatching wrapper. Assisted-by: Claude * Resolve TpSetattro pair on attribute deletion, not just addition update_one_slot's TpSetattro branch only ran the combined __setattr__/__delattr__ resolution (added in the previous commit) when ADD was true. On deletion (e.g. del C.__setattr__), it fell back to inherit_from_mro, which copies the base class's slot and discards the class's own remaining Python-level override of the other name. lookup_slot_in_mro reads the current attribute dicts, so the same resolution is correct for both addition and deletion; the ADD-only guard is removed and the logic now always runs. Assisted-by: Claude * Generate Opcode::cache_entries/deopt as table lookups generate_rs_opcode_metadata.py now emits a 256-entry const array for each function instead of a chained match over opcode names. Opcode::deopt() indexes an [Option<Opcode>; 256] table built from the existing specialization-to-base mapping, and Opcode::cache_entries() indexes a [u8; 256] table that bakes in the same deopt/to_base composition deoptimize() previously performed before its own match. Regenerated opcode_metadata.rs from the updated script; PseudoOpcode's bodies are unchanged since it has no cache/deopt entries to table. Added an exhaustive test in instruction.rs that checks every valid u8 opcode against a frozen copy of the old chained-match bodies. Assisted-by: Claude * Make Opcode/Instruction numeric conversions O(1) Opcode::as_numeric, Opcode::as_instruction and Instruction::as_opcode were each a per-variant match over ~230 arms in the define_opcodes! macro. Opcode now carries the same explicit $op_id discriminants and #[repr($typ)] as Instruction, which makes the conversions sound as: - Opcode::as_numeric: a plain `self as $typ` identity cast. - Opcode::as_instruction / Instruction::as_opcode: a mem::transmute, relying on both enums sharing one-$typ-wide layout (Instruction's payload fields are all the zero-sized Arg<T> marker, checked by a new per-instantiation size_of assertion). try_from_numeric is left as a match: $op_id values have gaps (specialized/instrumented opcode ranges), so a range check can't replace it. Adds #[inline] to the rewritten conversions plus deoptimize, and to the small generated wrapper/table-lookup functions from the previous opcode-metadata change (as_u8/as_u16, cache_entries, deopt, to_base) via generate_rs_opcode_metadata.py, then regenerates opcode_metadata.rs. Extends the instruction.rs equivalence-test pattern with two exhaustive tests (u8 and u16 instantiations) checking the new identity-cast/transmute conversions against the untouched try_from_numeric/TryFrom paths. Assisted-by: Claude * Pop CallIsinstance arguments directly instead of collecting into Vecs The specialized handler built two heap-allocated Vecs per call (pop_multiple().collect() plus a with_capacity(2) buffer) before invoking is_instance. Pop the class and instance straight off the stack for the effective two-argument case, removing the per-call allocations. Assisted-by: Claude * Inherit tp_new instead of always installing new_wrapper update_one_slot's TpNew branch stored new_wrapper for every ADD, so all heap types had slots.new == new_wrapper and the CallAllocAndEnterInit specialization never fired. Now __new__ is resolved through the MRO dicts: an own or Python-level definition installs new_wrapper, while a builtin __new__ entry (or none) inherits slots.new from the solid base. The now-reachable CallAllocAndEnterInit path gains the missing guards: the specializer requires co_argcount == oparg + 1 (via can_specialize_call) and rejects generator-like __init__, and the handler re-checks the argcount against the class-level init cache. Without the argcount check, __init__ defaults broke re._parser (UnboundLocalError in SubPattern.__init__). Remove the now-unused is_simple_for_call_specialization. Assisted-by: Claude * Skip locals dict allocation for the init-cleanup shim frame Mark the shim code object NEWLOCALS and create the shim frame with no locals mapping so FrameLocals::lazy() is used. The shim only executes ExitInitCheck/ReturnValue and never touches locals, so this removes one dict allocation per specialized instantiation. Assisted-by: Claude * Guard CALL_ALLOC_AND_ENTER_INIT with cached function version The init specialization cache stored only the type version, so swapping __init__.__code__ (which zeroes func_version) did not deopt the warmed call site and stale code assumptions (argcount shape, no varargs/kwonly, not a generator) were applied to the new code object. Store get_version_for_current_state() in the specialization cache when caching __init__ and re-check func_version() in the handler before using the cached function, deopting to the generic call path on mismatch, the same way the getitem specialization cache does. Assisted-by: Claude * Apply rustfmt and ruff-format fixes to files from earlier commits Assisted-by: Claude * Add frame object freelist Frame.iframe becomes FrameUnsafeCell<Option<InterpreterFrame>> so a dead frame can be left as an empty husk. Traverse::clear extracts all owned child references (including localsplus values via LocalsPlus::clear_into) and empties the cell. Dead frame allocations are cached in a thread-local FreeList<Frame> (up to 200 entries) and reused by PyRef::new_ref. clear_generator returns early when the frame was already cleared by cycle collection; generator drops can run after tp_clear of their frame. gc: skip tp_clear for objects saved to gc.garbage by DEBUG_SAVEALL, since they remain reachable from Python. Assisted-by: Claude * Drop frame children directly in tp_clear instead of extracting Pushing the frame's ~10 child references into the clear buffer grew a Vec on every frame deallocation. Take the interpreter frame out of the cell and drop it in place; LocalsPlus::clear_into is removed. Assisted-by: Claude * Untrack objects before tp_clear and guard cross-thread f_locals The cycle collector called tp_clear on unreachable objects while they were still linked into the GC generation lists, so another thread could obtain a strong reference to an already-cleared object (e.g. a frame husk with iframe == None) via gc.get_objects() and access the cleared payload. Untrack the dead set before the clear phase, mirroring the untrack-then-clear ordering of the refcount dealloc path. Objects that gained an external reference before untracking are found by comparing each object's strong count against the references coming from within the dead set; such late-resurrected objects and everything reachable from them are re-tracked and skipped, letting a later collection retry once the external references are released. Also reject f_locals access for a frame currently executing on another thread. Reading fastlocals of such a frame races with the executing thread overwriting the slots (torn reads of dropped values). Access from the executing thread itself (locals(), trace callbacks) is still allowed via the current-frame chain check. Assisted-by: Claude * Skip localsplus heap copy for uniquely referenced dying frames release_datastack_frame now checks the frame's strong count after untracking it from the GC generation lists. When the caller holds the only reference, localsplus values are dropped in place and the data stack storage is released without the heap copy; escaped frames (traceback, sys._getframe, trace callbacks) keep the copy path. The three inline materialize+pop call sites in function.rs are routed through release_datastack_frame. Assisted-by: Claude * Stage exact-args call arguments in stack slot buffers CallPyExactArgs, CallBoundMethodExactArgs and CallAllocAndEnterInit no longer collect popped arguments into a per-call Vec (plus a second Vec for self-prepending). Arguments are popped into a fixed-size Option<PyObjectRef> slot buffer (CallArgBuffer, 8 inline slots, heap fallback for larger arities) and moved into fastlocals via the new invoke_exact_args_slots / a take() iterator. - prepare_exact_args_frame now accepts an ExactSizeIterator of args - specialization_run_init_cleanup_shim takes the slot buffer and fills slot 0 with new_obj, dropping one redundant clone - LoadAttrGetattributeOverridden, LoadAttrProperty and BinaryOpSubscrGetitem pass inline slot arrays instead of vec![] Assisted-by: Claude * Reduce allocations in generic call paths - FuncArgs::prepend_arg: use reserve instead of reserve_exact so exact-capacity vectors do not realloc on every prepend. - IntoFuncArgs::into_method_args: build the final args vec once with capacity len + 1 instead of prepending into a full vector. - PyType::call: skip cloning FuncArgs when no init slot can run after slot_new (no init slot, not `type`, and slot_new is not new_wrapper). Assisted-by: Claude * Skip redundant exc_info restore on frame exit with_frame_impl saves the current exc_info slot value and restores it when the frame exits. When the slot still holds the same value, the restore rewrites the slot and recomputes the thread-exception mirror for no effect. Add restore_exception, which compares the slot against the saved value by pointer identity and skips the store when they match. The saved value is a strong reference held for the whole frame scope, so the pointed-to object cannot be freed and its address reused while the frame runs, making the pointer comparison free of ABA. Assisted-by: Claude * Apply rustfmt to gc_state.rs Assisted-by: Claude * Avoid empty FuncArgs clone in PyType::call Cloning args for the init call clones the kwargs map even when args is empty, which shows up in no-argument instantiation profiles. Prepare the init-call args before slot_new: a default FuncArgs when args is empty (indistinguishable from a clone of empty args), the existing clone otherwise. The empty check also keeps the init-slot load of the clone-elision path off the no-argument call path. Assisted-by: Claude * Push freelist husks only after tp_clear and child drops default_dealloc pushed the object onto the thread-local freelist before running clear_fn. Payloads that drop children inside clear (Frame) can run __del__ there, and a reentrant allocation could pop the husk and overwrite its payload while clear_fn still held a &mut borrow of it. Tuples had the same window through the extracted-edges drop, which ran after the push. Reorder default_dealloc to clear, drop the extracted children, and only then attempt the freelist push, so the husk becomes reusable only when no borrows into the payload remain. The tuple freelist bucketed husks by element count, which required reading the payload during push; after this reordering the elements are already cleared. PyInner<PyTuple> is a fixed-size allocation (the elements box is dropped and replaced on reuse), so replace the per-size buckets with the shared single-list FreeList. Remove the now-unused pyinner_layout helper. Assisted-by: Claude * Resolve __set__ and __delete__ together for the descr_set slot The descr_set slot serves both __set__ and __delete__, but the TpDescrSet accessor resolved only the single modified name and fell back to MRO inheritance on delete. Deleting one of the pair could disable the surviving operation: after `del D.__set__`, an instance delete stopped calling the remaining __delete__. Resolve both names together like the TpSetattro accessor does: any Python-level definition selects the dispatching wrapper, matching native functions are stored directly, a single native function with the other name absent is stored directly, and the slot is inherited from the MRO only when neither name resolves. Assisted-by: Claude * Update type base on __bases__ assignment set_bases replaced bases and mro but never updated the stored base, so __base__ kept reporting the old value after reassignment and slot resolution (tp_new inheritance, set_new/set_alloc during init_slots) read the stale solid base. Change the base field to PyAtomicRef<Option<PyType>> so it can be swapped under the type lock while remaining lock-free for readers, and recompute it in set_bases with the same best_base validation type creation uses (BASETYPE flag, instance layout conflict among bases). The swapped-out base is parked in the frame's temporary refs; other references released inside the critical section are dropped after the lock is released. Also in set_bases: - remove this class from the old bases' subclass lists (pruning dead entries), so repeated assignment no longer accumulates duplicates - roll back bases, base, and all updated mros when the recursive mro update fails, instead of leaving the type half-reparented - fix the misformatted empty-tuple error message Remove the expectedFailure marker from test_unsubclassable_types, which now passes. Assisted-by: Claude * Fix rollback order, dead weakrefs, and lock-held drops in set_bases - Restore recorded mros in reverse on rollback so a class visited multiple times through diamond inheritance ends with its original mro instead of an intermediate one. - Skip dead weakrefs in the subclass list during the recursive mro update instead of panicking on upgrade. - Retire the replaced mros on the success path instead of dropping them while the type lock is held. Assisted-by: Claude * Reify number sub-slot wrapper once in update_one_slot The update_sub_slot! macro expanded the Python-method wrapper closure at both the own and inherited store sites. Each expansion is a distinct fn item, so a base and a non-overriding subclass stored wrappers with distinct addresses in unmerged debug builds. binary_op1 compares slot fn addresses to detect whether a subclass overrides the operator, so the inherited slot was misread as an override and C() // E() dispatched to __rfloordiv__ instead of __floordiv__. Bind the wrapper store in a single closure and call it from both branches so the fn item is reified once and the slot value stays identical across a base and its subclasses. Document the conservative-on-mismatch nature of the new_wrapper address guard in call_wrapped. Assisted-by: Claude * Restrict type_cache_after_fork visibility and guard type-cache reads type_cache_after_fork is only called from within the crate, so mark it pub(crate) to resolve the unreachable-pub warning. Add debug_assert_current_thread_attached at the has_name_in_mro lock-free type-cache read site, matching lookup_ref_and_version_interned, and gate the function on debug_assertions so it is not compiled unused in release builds where the call sites are elided. Assisted-by: Claude * Guard BinaryOpSubscrGetitem with the cached type version The specialized BINARY_OP_SUBSCR_GETITEM handler used the cached __getitem__ after checking only the function version, unlike the sibling specialized handlers which revalidate the type version tag first. Store the type version in the inline cache at specialization time and revalidate owner.class().tp_version_tag against it before using the cached function, deopting to the generic subscript path on mismatch. Assisted-by: Claude * Call __abstractmethods__ __len__ once in object.__new__ The abstract-method check invoked the user-visible __len__ twice: once via length_opt for the count and again while materializing the method names. Derive the count from the materialized list instead, so __len__ runs once. Assisted-by: Claude * Use i64 fast paths for specialized int add/sub/mul Rewrite execute_binary_op_int to box results through new_int via i64 checked arithmetic instead of raw BigInt ops with new_bigint. Add an int_mul helper and a shared int_fast_op that computes the i64 result and falls back to the BigInt operation on to_i64 or checked-op failure. Wire int_mul into both the specialized BinaryOpMultiplyInt handler and the generic execute_bin_op Multiply/InplaceMultiply path, gated on exact int operands so subclasses keep dispatching through _mul/_imul. Assisted-by: Claude * Use i64 fast paths for exact int floordiv and remainder Add floordiv_i64/mod_i64 computing i64 floor-division and divisor-signed remainder, guarding zero divisor and i64::MIN overflow by returning None. Wire them through int_floordiv/int_mod (shared int_div_fast_op boxes via new_int) into the generic execute_bin_op FloorDivide/Remainder and their Inplace variants, gated on exact int operands so subclasses and zero divisors fall through to the existing _floordiv/_mod/_ifloordiv/_imod slow path. Assisted-by: Claude * Skip trashcan and untrack for non-GC-tracked objects in dealloc default_dealloc read is_gc_tracked() only to decide untracking, and entered the trashcan recursion guard unconditionally. Non-GC objects (int, float, str, ...) own no child references that recurse during deallocation, so they need neither the trashcan nor untracking. Read is_gc_tracked() once and gate both trashcan begin/end and untrack on it, removing three thread-local accesses per non-GC object deallocation. Assisted-by: Claude * Merge trashcan depth and defer queue into one thread-local struct trashcan::end accessed two separate thread-locals (DEALLOC_DEPTH and DEALLOC_QUEUE) at the outermost deallocation. Combine both into a single `Trashcan` thread-local holding Cell-based depth and queue fields, so begin and end each reach their state through one thread-local access. The queue is set back before each deferred dealloc call so reentrant begin/end during draining never holds an outstanding borrow. Assisted-by: Claude * Check object layout compatibility on __bases__ and __class__ assignment Add a shared compatible_for_assignment helper that walks each type to the base that fixes its instance layout and rejects the assignment when the old and new layouts differ (basicsize, itemsize, member count, __dict__, __weakref__, and __slots__ names). Wire it into set_bases, which previously performed no layout check, and replace the inline check in __class__ assignment, which compared the two types directly and over-rejected a subclass that adds no layout. Enable test_descr.test_builtin_bases, which the new set_bases check passes. Assisted-by: Claude * Stop the world around GC pointer-reading phases The cycle collector reads each tracked object's interpreter state during reference subtraction, the reachability walk and the strong-reference snapshot, including the localsplus of frames other threads are executing. Those slots are written without synchronization, so the reads are a data race under threading. collect_inner now stops the world before taking the generation read locks and restarts it after the strong-reference snapshot, before the finalizer/weakref/tp_clear phases. A debug assertion checks that no frame on any thread's call stack was classified unreachable. Automatic collections from maybe_collect are deferred to the next bytecode safepoint via a new eval-breaker GC bit instead of running synchronously: a synchronous collection can hold an internal lock (e.g. the lazy frame locals cell) that another thread is blocked on with no way to reach a safepoint, deadlocking the stop. Hardens the stop-the-world machinery for these frequent, concurrent GC stops: attach_thread now honors a pending stop after re-attaching so a thread doing rapid allow_threads calls cannot run past the requester; stop_the_world completion is level-triggered on all-threads-suspended rather than an edge-triggered countdown; and the thread-start started/ready handshakes detach while waiting so the waiter is parkable. Updates the Frame and PyRwLock/PyMutex traversal SAFETY comments to state the actual invariant. Assisted-by: Claude * Add threaded GC vs executing-frame stress snippet Workers churn frame state (recursion, generators, frame cycles) while a collector loops gc.collect() and an introspector walks live frame objects. Exercises the stop-the-world barrier around GC traversal. Assisted-by: Claude * Serialize fork and GC stop-the-world requesters fork() (posix before/after-fork) and the cycle collector both drive the single StopTheWorldState. With no mutual exclusion their requester word and suspension countdown could interleave and be clobbered, so the completion check never converged and a requester waited on itself forever (reproducible parent-side hang when forking with GC enabled). Add one exclusion held for the whole stop->start span of either requester: stop_the_world acquires it before any stop bookkeeping and start_the_world/reset_after_fork release it. The acquire is park-friendly (poll try-lock and honor a pending suspend between tries) so a requester blocked behind an active stop can still be force-parked instead of deadlocking the active requester. The acquirer holds no other lock while spinning. Also correct the PyRwLock traverse SAFETY note: a failed try_read may mean a force-parked thread holds the write lock; skipping is safe because under-traversal only over-approximates liveness. Document the residual gc.collect()-under-a-non-generation-lock exposure at the collect_inner barrier. Assisted-by: Claude * Add fork under concurrent GC stop-the-world snippet Worker threads allocate cyclic garbage with GC enabled while the main thread forks repeatedly and each child collects. Exercises the fork/GC stop-the-world exclusion; the allocation rate is throttled so a collection stays cheap in unoptimized builds. Assisted-by: Claude * Detach while acquiring the import lock The global import lock is held across bytecode by the importlib bootstrap, so its holder can be parked at a safepoint mid-hold. Acquiring it while attached let another thread block attached on the lock, so a stop-the-world requester could wait forever for that attached thread to suspend while the holder stayed parked. Wrap IMP_LOCK acquisition in allow_threads in both _imp.acquire_lock and the pre-fork acquire_imp_lock_for_fork so the wait honors stop-the-world requests. Correct the acquire_exclusion comment: a spinning requester may hold IMP_LOCK (fork) or the collecting mutex (GC); safety relies on those never being acquired attached-blocking by another thread. Assisted-by: Claude * Add concurrent-import vs GC deadlock snippet Two threads re-import modules (contending the import lock) while a third storms the cycle collector and a fourth allocates cyclic garbage. Regressed as a hang before the import lock acquisition was made park-friendly. Assisted-by: Claude * Create call frames untracked and track generators explicitly Add PyPayload::NEW_REF_UNTRACKED (default false, true for Frame) so PyRef::new_ref skips auto-tracking ordinary call frames in the GC. Generator/coroutine/async-generator frames are tracked explicitly in invoke_with_locals before their generator back-reference is installed. run_frame debug-asserts that a datastack frame is untracked on entry. Assisted-by: Claude * Track escaped call frames lazily at datastack release Rewrite release_datastack_frame around the invariant that a datastack frame is never GC-tracked while it runs: strong_count() == 1 means the frame never escaped, so drop localsplus in place without touching the GC; strong_count() > 1 means it escaped, so materialize localsplus onto the heap and then track the frame. This removes the untrack and the untrack-recheck dance from the common non-escaping path. Assisted-by: Claude * Guard the tracked-frame localsplus invariant Document at Frame::traverse that references to a frame are always recorded as graph edges, so the collector reads a frame's localsplus only when the frame is itself a tracked candidate. Add debug assertions at both frame track sites (escaped datastack frames and generator frames) that a frame is heap-backed before it is tracked, so a collector never reads data-stack-resident, still-mutating storage. Assisted-by: Claude * Reword frame/dealloc invariant comments and drop needless borrow - frame.rs: replace the task codename in the tracked-frame localsplus invariant comment with a description of the collector-vs-executing-frame behavior. - object.rs: remove needless borrow on current_cls in the __class__ assignment compatibility check. - core.rs: rewrite the default_dealloc trashcan/untrack-skip comment to state the actual invariant, which now covers untracked non-escaped frames releasing at interpreter depth with bounded recursion. Assisted-by: Claude * Move type SetAttr slot rewrite inside the type lock and tighten cache reads Run update_slot inside the same with_type_lock transaction as modified_inner and the attributes dict mutation, so the version invalidation, dict change, and slot-table rewrite are published together. Load init_version and getitem_version with Acquire in the specialization cache readers to pair with the Release stores in the writers. Assisted-by: Claude * Capture the type version before reading mutable slots in specializers specialize_load_attr, specialize_store_attr, specialize_to_bool, and the CallAllocAndEnterInit path of specialize_call read the version tag with version_for_specialization before inspecting getattro, setattro, the bool/len slots, and tp_new/tp_alloc, so a concurrent install of the corresponding dunder invalidates the version the specialization is cached against. In the BinaryOpSubscr __getitem__ path, check the HEAPTYPE and eval-frame gates before lookup_ref_and_version_interned, which takes the global type lock and may allocate a version tag. Assisted-by: Claude * Use _get_method_dict for the test_type check in the test runner Replace the direct __func__.__dict__ access with the _get_method_dict helper so plain functions without __func__ do not raise AttributeError. Assisted-by: Claude * Fix lint hooks: cspell dictionary entries, spelling fix, formatting Add qsbr to the rustpython dictionary and reborrows/reparenting to the top-level word list. Rename oldto/newto locals to old_to/new_to. Fix Stabilise -> Stabilize typo in a frame.rs comment. Apply cargo fmt to object.rs, type.rs, and frame.rs, and ruff format/check fixes to two extra_tests snippet files. Assisted-by: Claude * Rebuild all slots for type and descendants on __bases__ reassignment Add PyType::update_all_slots, which invalidates version tags and iterates the full SLOT_DEFS name table calling update_slot::<true> for each distinct name. Unlike init_slots, which is additive and driven only by dunder names present in the current MRO, this resets a slot whose method left the MRO instead of leaving a stale dispatcher pointer. Call it from set_bases in place of the previous modified_inner + init_slots pair, so reassigning __bases__ rebuilds slots for the type and every descendant. Add extra_tests/snippets/type_bases_slot_rebuild.py covering removed-method resets on zelf and deep descendants, the wrong-target switch, __getattr__, the added-method mirror, and a swap-away-and-back round trip. Assisted-by: Claude * Derive the thread-local frame stack from the frame chain Remove the per-VM `frames` Vec. current_frame, sys._getframe, sys._getframemodulename, frame.f_back, sys.monitoring re-instrumentation, gc.get_referrers, faulthandler stack dumps and the post-fork slot rebuild now walk the signal-safe CURRENT_FRAME/`previous` chain instead. Add `Py::from_payload_ptr` to recover a frame object from a chain pointer. The cross-thread `ThreadSlot::frames` registry (sys._current_frames, cross-thread f_back, GC stop-the-world assertion) is unchanged. Assisted-by: Claude * Inherit sub-slot fallback into the field being resolved update_one_slot's number/sequence/mapping fallback inherited via the accessor's default field. Left and right binary ops (add/right_add) share one accessor but occupy distinct fields, so resolving an absent right op or deleting it reset the left op's field, dropping a still-defined __add__ dispatcher. Inherit the exact field under resolution instead. Assisted-by: Claude * Gate type_cache_after_fork to its fork caller and use Self in downcast type_cache_after_fork is only called from the unix fork path, so gate it with all(feature = "host_env", unix) to match the caller and avoid a dead-code error on non-unix targets. Replace the explicit PyType with Self in the modified_inner downcast. Assisted-by: Claude * Unmark test_attr and test_method_call_error in test_monitoring Both TestLoadSuperAttr tests now pass; remove their expectedFailure markers. Assisted-by: Claude * Run tp_new specialization __init__ without a trampoline frame CallAllocAndEnterInit ran __init__ inside a synthetic init-cleanup shim frame whose code carried the __init__ name. Since the thread frame stack is derived from the frame chain, that shim frame was visible to sys._getframe, f_back walks, traceback construction and inspect.stack / inspect.trace. Its code object has empty co_positions, so a stack walk that reached it raised StopIteration inside inspect._get_code_position, cascading into unrelated failures (test_inspect trace/stack/frame, asyncio source traceback). Call __init__ directly via run_frame, enforce the __init__() should return None contract inline, and drop the shim: the init-cleanup code object, its builder, with_frame_untraced, monitoring_disabled_for_code, and the extra-frame datastack/recursion budget. Removing the second frame per construction also cuts the specialization's per-call cost. Assisted-by: Claude * Replace per-call thread-frame mutex with an atomic top-of-stack (unix) On unix threading builds, publish each thread's top Python frame in a single relaxed AtomicPtr store from set_current_frame instead of pushing onto a parking_lot::Mutex<Vec<FramePtr>> per call. Cross-thread readers (sys._current_frames, cross-thread f_back, faulthandler.dump_traceback, the GC unreachable debug-assert) run under stop-the-world and walk the published top frame down the Frame::previous chain; the owning thread is then parked at a safepoint, so the pointer and the frames it reaches are quiescent and alive. The faulthandler watchdog is a plain OS thread that cannot stop-the-world, so it walks the chain lock-free and best-effort. Non-unix threading builds have no stop-the-world and keep the existing mutex-guarded frame stack unchanged. Assisted-by: Claude * Skip exc_info save/restore for callees that never touch the slot with_frame saves and restores the shared exc_info slot around every Python call to contain frames that leave it unbalanced. Cache a has_exc_handling bit on PyCode at creation, set when the bytecode contains any opcode that calls vm.set_exception (PushExcInfo, PopExcept, CheckEgMatch, EndAsyncFor, InstrumentedEndAsyncFor). A callee whose code has none of these cannot mutate the slot, so the save and restore are skipped for it. Generators go through resume_gen_frame and are unaffected. Assisted-by: Claude * Return a write-through FrameLocalsProxy from frame.f_locals Optimized (function) frames now expose `f_locals` as a `FrameLocalsProxy` implementing PEP 667 semantics instead of a cached snapshot dict: - reads go live through the fast-local slots; each access mints a fresh proxy; keys that do not name a fast local are stored in a per-frame `f_extra_locals` side dict and folded into `locals()`. - writes to a fast-local key store into the slot (or its cell) in place; deleting a fast local raises ValueError; extra keys delete normally. - full mapping protocol: keys/values/items (lists), get/pop/setdefault, update (dict or FrameLocalsProxy only), __or__/__ior__/__ror__ (dict result), copy (plain dict), __reduce__ blocks pickling/copy, repr with recursion guard, mapping-pattern and Mapping ABC support. Class/module/exec frames keep returning their namespace mapping directly. Cross-thread access to a frame running on another thread still raises RuntimeError. A closed generator now keeps its frame locals when a durable frame reference escaped (f_locals proxy, sys._getframe, f_back), matching take_ownership; the escape is tracked with a per-frame flag. The snapshot-then-fold locals_to_fast/locals_dirty write-back is retired since proxy writes reach the slots directly. Assisted-by: Claude * Retain the caller frame so f_back resolves after it returns When a frame escapes its execution (referenced through a traceback, `sys._getframe`, `f_locals`, ...), capture a strong reference to its caller at release time. `f_back` consults it once the caller has left the live frame chain, so the Python-visible frame chain survives return. The retained reference is a GC-traversed edge and is cleared by `frame.clear()`, so ancestor chains stay collectable. Assisted-by: Claude * Fix debug-build native stack overflow on deep recursion Make check_c_stack_overflow one-sided (trip whenever the stack pointer is below the soft limit) so a single native frame larger than the margin cannot step past the danger band undetected. Raise the debug STACK_MARGIN_BYTES from 4096 to 16384 words so the margin exceeds a single debug interpreter frame, leaving headroom to raise RecursionError. Release margin unchanged. Clamp the soft-limit margin to half the stack so small explicit thread stacks do not get a soft limit above their stack top. Assisted-by: Claude * Allocate exception instance __dict__ lazily Exception construction went through into_ref_with_type, which eagerly allocated an empty instance dict for every HAS_DICT type. Add into_ref_with_type_lazy_dict, which builds the instance with an unallocated dict slot, and route the four exception construction sites (PyBaseException, PyOSError, OSErrorBuilder, PyBaseExceptionGroup) through it. The dict now materializes on first attribute write or __dict__ access via the existing get_or_insert path. add_note and PyImportError::slot_init now obtain the dict through object_get_dict so they materialize it instead of assuming it exists. A freshly constructed exception no longer reports an empty dict in gc.get_referents, matching the reference interpreter. Assisted-by: Claude * Allocate exception __dict__ lazily in vm.new_exception Route vm.new_exception() through into_ref_with_type_lazy_dict so internally raised exceptions (new_type_error, new_value_error, etc.) start without an instance dict, matching the slot_new path. The dict is materialized on the first attribute write or __dict__ access. Assisted-by: Claude * Validate FrameLocalsProxy.update() arguments Reject keyword arguments and require exactly one positional argument, raising TypeError with the "takes no keyword arguments" and "takes exactly one argument (N given)" messages. Assisted-by: Claude * Address review nits in frame and faulthandler - Drop stale vm.frames reference from the release_datastack_frame uniqueness argument. - Assert has_exc_handling when unwinding an Except-typed stack slot, documenting the invariant that guards the shared exc_info write. - Truncate the specialized __init__ return-type name to 200 chars, matching the unspecialized wrapper. - Reword two comments to describe behavior without prose references to CPython. Assisted-by: Claude * Use scopeguard for start_the_world in faulthandler dump_all_threads Wrap the unix stop-the-world registry walk in a scope with scopeguard::defer! so start_the_world runs on panic, matching the f_back and get_all_current_frames sites. Add scopeguard to the stdlib dependencies. Also correct the restore_exception doc comment to name with_frame after the rename. Assisted-by: Claude * Gitignore docs/superpowers Assisted-by: Claude * Rename type_bases_slot_rebuild.py to builtin_type_bases.py Match the builtin_* naming convention of extra_tests/snippets. Assisted-by: Claude * Apply rustfmt, ruff, and cspell lint fixes Reformat with rustfmt, fix import spacing in builtin_type_bases.py with ruff, and add "pointee" to the cspell word list. Assisted-by: Claude * Gate unix-only QSBR methods to their call sites `online`, `drain_all`, and `reset_after_fork` are called only from unix code (thread attach/detach, post-fork reset), and `offline` from unix code plus a unit test. Gate them with matching cfg so `-D dead_code` does not fire on non-unix targets. Assisted-by: Claude * Visit the function closure tuple as a GC edge PyFunction::traverse visited the cells inside the closure tuple instead of the tuple object itself, so the tuple's reference from the function was never subtracted during cycle collection. A closure tuple that reached back to its function (or, through a frame retained by f_back, to a Thread) was stranded as a false GC root and never collected, leaking the whole cycle. Visit the tuple itself, matching clear(). Assisted-by: Claude * Apply formatting hook fix to builtin_type_bases.py Assisted-by: Claude * Unmark asyncgen finalization-by-gc tests in test_base_events test_asyncgen_finalization_by_gc and test_asyncgen_finalization_by_gc_in_other_thread now pass; GC finalizes the async generators. Assisted-by: Claude * Unmark test_sni_callback_refcycle in test_ssl The servername-callback reference cycle is now collected by GC. Assisted-by: Claude * Widen GC stop-the-world gates from unix-only to all threading builds Change `cfg(all(unix, feature = "threading"))` to `cfg(feature = "threading")` on the stop-the-world machinery so it also compiles and runs on non-unix threading builds: - StopTheWorldState, its stats, stw_trace, and the stop_the_world field - ThreadSlot state/stop_requested/thread fields and their initializers - wait_while_suspended/attach_thread/detach_thread/suspend_if_needed/do_suspend, allow_threads, stop_requested_for_current_thread, and the enter_vm / VmBootstrapGuard / attach_current_thread / release_current_thread / cleanup attach-state wiring - eval_breaker_tripped, check_signals, run_scheduled_gc, signal GC_BIT / schedule_gc / take_gc_scheduled, and the frame.rs safepoint call - CollectStopTheWorld and its use in collect_inner - QSBR::online/offline, now called from attach/detach on all threading builds - debug_assert_current_thread_attached and its type-cache call sites maybe_collect defers auto-collection to the bytecode safepoint on every threading build instead of only unix; non-threading builds keep the inline collect. stw_trace writes to std stderr on non-unix. top_frame publishing (CURRENT_TOP_FRAME_SLOT, set_current_frame), the frame-walk debug assert in collect_inner, and the fork reinit helpers remain unix-only; non-unix keeps ThreadSlot::frames for introspection. Assisted-by: Claude * Detach current thread around blocking _winapi/_overlapped waits Wrap the blocking Windows wait calls in `vm.allow_threads` so the calling thread transitions ATTACHED -> DETACHED for the duration of the wait: - _winapi: WaitForSingleObject, WaitForMultipleObjects, BatchedWaitForMultipleObjects, ConnectNamedPipe, ReadFile, Overlapped.GetOverlappedResult - _overlapped: Overlapped.getresult These previously blocked while ATTACHED, so a stop-the-world requester could never suspend the thread and spun in its wait loop indefinitely. * Format WaitForMultipleObjects allow_threads closure * Treat concurrent sni_callback removal as no-op in invoke_sni_callback * Assert capi refcount on a fresh mortal list instead of the int type object The refcount test asserted exact incref/decref deltas on PyInt's shared type object. That object's reference count is perturbed by the other capi tests running in parallel, and is immortal under some interpreter configurations, so the deltas were not reliably +1/-1. Assert them on a freshly created, uniquely owned list whose reference count is private to the test and mortal. Assisted-by: Claude * Traverse and GC-track PyOSError instances The `#[pyexception]` struct macro now forwards a `traverse` option to the generated `#[pyclass]`, and `ExceptionItemMeta` accepts the `traverse` key. `PyOSError` is marked `traverse = "manual"`, so `HAS_TRAVERSE` is true and OSError-family instances are tracked at creation and traversed by the collector. `PyOSError::traverse` now visits the underlying `PyBaseException` (traceback, cause, context, args) instead of `PyException::try_traverse`, which was a no-op because `PyException` has `HAS_TRAVERSE = false`. * Unmark test_blockingioerror in test_io The BlockingIOError reference cycle is now collected by GC. Assisted-by: Claude
2891 lines
77 KiB
Python
Vendored
2891 lines
77 KiB
Python
Vendored
import copy
|
|
import gc
|
|
import pickle
|
|
import sys
|
|
import doctest
|
|
import unittest
|
|
import weakref
|
|
import inspect
|
|
import textwrap
|
|
import types
|
|
|
|
from test import support
|
|
|
|
try:
|
|
import _testcapi
|
|
except ImportError:
|
|
_testcapi = None
|
|
|
|
|
|
# This tests to make sure that if a SIGINT arrives just before we send into a
|
|
# yield from chain, the KeyboardInterrupt is raised in the innermost
|
|
# generator (see bpo-30039).
|
|
@unittest.skipUnless(_testcapi is not None and
|
|
hasattr(_testcapi, "raise_SIGINT_then_send_None"),
|
|
"needs _testcapi.raise_SIGINT_then_send_None")
|
|
class SignalAndYieldFromTest(unittest.TestCase):
|
|
|
|
def generator1(self):
|
|
return (yield from self.generator2())
|
|
|
|
def generator2(self):
|
|
try:
|
|
yield
|
|
except KeyboardInterrupt:
|
|
return "PASSED"
|
|
else:
|
|
return "FAILED"
|
|
|
|
def test_raise_and_yield_from(self):
|
|
gen = self.generator1()
|
|
gen.send(None)
|
|
try:
|
|
_testcapi.raise_SIGINT_then_send_None(gen)
|
|
except BaseException as _exc:
|
|
exc = _exc
|
|
self.assertIs(type(exc), StopIteration)
|
|
self.assertEqual(exc.value, "PASSED")
|
|
|
|
|
|
class FinalizationTest(unittest.TestCase):
|
|
|
|
def test_frame_resurrect(self):
|
|
# A generator frame can be resurrected by a generator's finalization.
|
|
def gen():
|
|
nonlocal frame
|
|
try:
|
|
yield
|
|
finally:
|
|
frame = sys._getframe()
|
|
|
|
g = gen()
|
|
wr = weakref.ref(g)
|
|
next(g)
|
|
del g
|
|
support.gc_collect()
|
|
self.assertIs(wr(), None)
|
|
self.assertTrue(frame)
|
|
del frame
|
|
support.gc_collect()
|
|
|
|
def test_refcycle(self):
|
|
# A generator caught in a refcycle gets finalized anyway.
|
|
old_garbage = gc.garbage[:]
|
|
finalized = False
|
|
def gen():
|
|
nonlocal finalized
|
|
try:
|
|
g = yield
|
|
yield 1
|
|
finally:
|
|
finalized = True
|
|
|
|
g = gen()
|
|
next(g)
|
|
g.send(g)
|
|
self.assertGreaterEqual(sys.getrefcount(g), 2)
|
|
self.assertFalse(finalized)
|
|
del g
|
|
support.gc_collect()
|
|
self.assertTrue(finalized)
|
|
self.assertEqual(gc.garbage, old_garbage)
|
|
|
|
def test_lambda_generator(self):
|
|
# bpo-23192, gh-119897: Test that a lambda returning a generator behaves
|
|
# like the equivalent function
|
|
f = lambda: (yield 1)
|
|
self.assertIsInstance(f(), types.GeneratorType)
|
|
self.assertEqual(next(f()), 1)
|
|
|
|
def g(): return (yield 1)
|
|
|
|
# test 'yield from'
|
|
f2 = lambda: (yield from g())
|
|
def g2(): return (yield from g())
|
|
|
|
f3 = lambda: (yield from f())
|
|
def g3(): return (yield from f())
|
|
|
|
for gen_fun in (f, g, f2, g2, f3, g3):
|
|
gen = gen_fun()
|
|
self.assertEqual(next(gen), 1)
|
|
with self.assertRaises(StopIteration) as cm:
|
|
gen.send(2)
|
|
self.assertEqual(cm.exception.value, 2)
|
|
|
|
def test_generator_resurrect(self):
|
|
# Test that a resurrected generator still has a valid gi_code
|
|
resurrected = []
|
|
|
|
# Resurrect a generator in a finalizer
|
|
exec(textwrap.dedent("""
|
|
def gen():
|
|
try:
|
|
yield
|
|
except:
|
|
resurrected.append(g)
|
|
|
|
g = gen()
|
|
next(g)
|
|
"""), {"resurrected": resurrected})
|
|
|
|
support.gc_collect()
|
|
|
|
self.assertEqual(len(resurrected), 1)
|
|
self.assertIsInstance(resurrected[0].gi_code, types.CodeType)
|
|
|
|
@unittest.expectedFailure # TODO: RUSTPYTHON; AssertionError: <frame object at 0xb4000073269f09e0> is not None
|
|
def test_exhausted_generator_frame_cycle(self):
|
|
def g():
|
|
yield
|
|
|
|
generator = g()
|
|
frame = generator.gi_frame
|
|
self.assertIsNone(frame.f_back)
|
|
next(generator)
|
|
self.assertIsNone(frame.f_back)
|
|
next(generator, None)
|
|
self.assertIsNone(frame.f_back)
|
|
|
|
|
|
class GeneratorTest(unittest.TestCase):
|
|
|
|
def test_name(self):
|
|
def func():
|
|
yield 1
|
|
|
|
# check generator names
|
|
gen = func()
|
|
self.assertEqual(gen.__name__, "func")
|
|
self.assertEqual(gen.__qualname__,
|
|
"GeneratorTest.test_name.<locals>.func")
|
|
|
|
# modify generator names
|
|
gen.__name__ = "name"
|
|
gen.__qualname__ = "qualname"
|
|
self.assertEqual(gen.__name__, "name")
|
|
self.assertEqual(gen.__qualname__, "qualname")
|
|
|
|
# generator names must be a string and cannot be deleted
|
|
self.assertRaises(TypeError, setattr, gen, '__name__', 123)
|
|
self.assertRaises(TypeError, setattr, gen, '__qualname__', 123)
|
|
self.assertRaises(TypeError, delattr, gen, '__name__')
|
|
self.assertRaises(TypeError, delattr, gen, '__qualname__')
|
|
|
|
# modify names of the function creating the generator
|
|
func.__qualname__ = "func_qualname"
|
|
func.__name__ = "func_name"
|
|
gen = func()
|
|
self.assertEqual(gen.__name__, "func_name")
|
|
self.assertEqual(gen.__qualname__, "func_qualname")
|
|
|
|
# unnamed generator
|
|
gen = (x for x in range(10))
|
|
self.assertEqual(gen.__name__,
|
|
"<genexpr>")
|
|
self.assertEqual(gen.__qualname__,
|
|
"GeneratorTest.test_name.<locals>.<genexpr>")
|
|
|
|
def test_copy(self):
|
|
def f():
|
|
yield 1
|
|
g = f()
|
|
with self.assertRaises(TypeError):
|
|
copy.copy(g)
|
|
|
|
def test_pickle(self):
|
|
def f():
|
|
yield 1
|
|
g = f()
|
|
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
|
|
with self.assertRaises((TypeError, pickle.PicklingError)):
|
|
pickle.dumps(g, proto)
|
|
|
|
def test_send_non_none_to_new_gen(self):
|
|
def f():
|
|
yield 1
|
|
g = f()
|
|
with self.assertRaises(TypeError):
|
|
g.send(0)
|
|
self.assertEqual(next(g), 1)
|
|
|
|
def test_handle_frame_object_in_creation(self):
|
|
|
|
#Attempt to expose partially constructed frames
|
|
#See https://github.com/python/cpython/issues/94262
|
|
|
|
def cb(*args):
|
|
inspect.stack()
|
|
|
|
def gen():
|
|
yield 1
|
|
|
|
thresholds = gc.get_threshold()
|
|
|
|
gc.callbacks.append(cb)
|
|
gc.set_threshold(1, 0, 0)
|
|
try:
|
|
gen()
|
|
finally:
|
|
gc.set_threshold(*thresholds)
|
|
gc.callbacks.pop()
|
|
|
|
class Sneaky:
|
|
def __del__(self):
|
|
inspect.stack()
|
|
|
|
sneaky = Sneaky()
|
|
sneaky._s = Sneaky()
|
|
sneaky._s._s = sneaky
|
|
|
|
gc.set_threshold(1, 0, 0)
|
|
try:
|
|
del sneaky
|
|
gen()
|
|
finally:
|
|
gc.set_threshold(*thresholds)
|
|
|
|
def test_ag_frame_f_back(self):
|
|
async def f():
|
|
yield
|
|
ag = f()
|
|
self.assertIsNone(ag.ag_frame.f_back)
|
|
|
|
def test_cr_frame_f_back(self):
|
|
async def f():
|
|
pass
|
|
cr = f()
|
|
self.assertIsNone(cr.cr_frame.f_back)
|
|
cr.close() # Suppress RuntimeWarning.
|
|
|
|
def test_gi_frame_f_back(self):
|
|
def f():
|
|
yield
|
|
gi = f()
|
|
self.assertIsNone(gi.gi_frame.f_back)
|
|
|
|
def test_issue103488(self):
|
|
|
|
def gen_raises():
|
|
yield
|
|
raise ValueError()
|
|
|
|
def loop():
|
|
try:
|
|
for _ in gen_raises():
|
|
if True is False:
|
|
return
|
|
except ValueError:
|
|
pass
|
|
|
|
#This should not raise
|
|
loop()
|
|
|
|
def test_genexpr_only_calls_dunder_iter_once(self):
|
|
|
|
class Iterator:
|
|
|
|
def __init__(self):
|
|
self.val = 0
|
|
|
|
def __next__(self):
|
|
if self.val == 2:
|
|
raise StopIteration
|
|
self.val += 1
|
|
return self.val
|
|
|
|
# No __iter__ method
|
|
|
|
class C:
|
|
|
|
def __iter__(self):
|
|
return Iterator()
|
|
|
|
self.assertEqual([1, 2], list(i for i in C()))
|
|
|
|
@unittest.expectedFailure # TODO: RUSTPYTHON; AssertionError: False is not true
|
|
def test_close_clears_frame(self):
|
|
# gh-142766: Test that closing a generator clears its frame
|
|
class DetectDelete:
|
|
def __init__(self):
|
|
DetectDelete.deleted = False
|
|
|
|
def __del__(self):
|
|
DetectDelete.deleted = True
|
|
|
|
def generator(arg):
|
|
yield
|
|
|
|
# Test a freshly created generator (not suspended)
|
|
g = generator(DetectDelete())
|
|
g.close()
|
|
self.assertTrue(DetectDelete.deleted)
|
|
|
|
# Test a suspended generator
|
|
g = generator(DetectDelete())
|
|
next(g)
|
|
g.close()
|
|
self.assertTrue(DetectDelete.deleted)
|
|
|
|
# Clear via gi_frame.clear()
|
|
g = generator(DetectDelete())
|
|
g.gi_frame.clear()
|
|
self.assertTrue(DetectDelete.deleted)
|
|
|
|
class ModifyUnderlyingIterableTest(unittest.TestCase):
|
|
iterables = [
|
|
range(0),
|
|
range(20),
|
|
[1, 2, 3],
|
|
(2,),
|
|
{13, 48, 211},
|
|
frozenset((15, 8, 6)),
|
|
{1: 2, 3: 4},
|
|
]
|
|
|
|
non_iterables = [
|
|
None,
|
|
42,
|
|
3.0,
|
|
2j,
|
|
]
|
|
|
|
def genexpr(self):
|
|
return (x for x in range(10))
|
|
|
|
def genfunc(self):
|
|
def gen(it):
|
|
for x in it:
|
|
yield x
|
|
return gen(range(10))
|
|
|
|
def process_tests(self, get_generator, is_expr):
|
|
err_iterator = "'.*' object is not an iterator"
|
|
err_iterable = "'.*' object is not iterable"
|
|
for obj in self.iterables:
|
|
g_obj = get_generator(obj)
|
|
with self.subTest(g_obj=g_obj, obj=obj):
|
|
if is_expr:
|
|
self.assertRaisesRegex(TypeError, err_iterator, list, g_obj)
|
|
else:
|
|
self.assertListEqual(list(g_obj), list(obj))
|
|
|
|
g_iter = get_generator(iter(obj))
|
|
with self.subTest(g_iter=g_iter, obj=obj):
|
|
self.assertListEqual(list(g_iter), list(obj))
|
|
|
|
for obj in self.non_iterables:
|
|
g_obj = get_generator(obj)
|
|
with self.subTest(g_obj=g_obj):
|
|
err = err_iterator if is_expr else err_iterable
|
|
self.assertRaisesRegex(TypeError, err, list, g_obj)
|
|
|
|
def test_modify_f_locals(self):
|
|
def modify_f_locals(g, local, obj):
|
|
g.gi_frame.f_locals[local] = obj
|
|
return g
|
|
|
|
def get_generator_genexpr(obj):
|
|
return modify_f_locals(self.genexpr(), '.0', obj)
|
|
|
|
def get_generator_genfunc(obj):
|
|
return modify_f_locals(self.genfunc(), 'it', obj)
|
|
|
|
self.process_tests(get_generator_genexpr, True)
|
|
self.process_tests(get_generator_genfunc, False)
|
|
|
|
def test_new_gen_from_gi_code(self):
|
|
def new_gen_from_gi_code(g, obj):
|
|
generator_func = types.FunctionType(g.gi_code, {})
|
|
return generator_func(obj)
|
|
|
|
def get_generator_genexpr(obj):
|
|
return new_gen_from_gi_code(self.genexpr(), obj)
|
|
|
|
def get_generator_genfunc(obj):
|
|
return new_gen_from_gi_code(self.genfunc(), obj)
|
|
|
|
self.process_tests(get_generator_genexpr, True)
|
|
self.process_tests(get_generator_genfunc, False)
|
|
|
|
|
|
class ExceptionTest(unittest.TestCase):
|
|
# Tests for the issue #23353: check that the currently handled exception
|
|
# is correctly saved/restored in PyEval_EvalFrameEx().
|
|
|
|
def test_except_throw(self):
|
|
def store_raise_exc_generator():
|
|
try:
|
|
self.assertIsNone(sys.exception())
|
|
yield
|
|
except Exception as exc:
|
|
# exception raised by gen.throw(exc)
|
|
self.assertIsInstance(sys.exception(), ValueError)
|
|
self.assertIsNone(exc.__context__)
|
|
yield
|
|
|
|
# ensure that the exception is not lost
|
|
self.assertIsInstance(sys.exception(), ValueError)
|
|
yield
|
|
|
|
# we should be able to raise back the ValueError
|
|
raise
|
|
|
|
make = store_raise_exc_generator()
|
|
next(make)
|
|
|
|
try:
|
|
raise ValueError()
|
|
except Exception as exc:
|
|
try:
|
|
make.throw(exc)
|
|
except Exception:
|
|
pass
|
|
|
|
next(make)
|
|
with self.assertRaises(ValueError) as cm:
|
|
next(make)
|
|
self.assertIsNone(cm.exception.__context__)
|
|
|
|
self.assertIsNone(sys.exception())
|
|
|
|
def test_except_next(self):
|
|
def gen():
|
|
self.assertIsInstance(sys.exception(), ValueError)
|
|
yield "done"
|
|
|
|
g = gen()
|
|
try:
|
|
raise ValueError
|
|
except Exception:
|
|
self.assertEqual(next(g), "done")
|
|
self.assertIsNone(sys.exception())
|
|
|
|
def test_except_gen_except(self):
|
|
def gen():
|
|
try:
|
|
self.assertIsNone(sys.exception())
|
|
yield
|
|
# we are called from "except ValueError:", TypeError must
|
|
# inherit ValueError in its context
|
|
raise TypeError()
|
|
except TypeError as exc:
|
|
self.assertIsInstance(sys.exception(), TypeError)
|
|
self.assertEqual(type(exc.__context__), ValueError)
|
|
# here we are still called from the "except ValueError:"
|
|
self.assertIsInstance(sys.exception(), ValueError)
|
|
yield
|
|
self.assertIsNone(sys.exception())
|
|
yield "done"
|
|
|
|
g = gen()
|
|
next(g)
|
|
try:
|
|
raise ValueError
|
|
except Exception:
|
|
next(g)
|
|
|
|
self.assertEqual(next(g), "done")
|
|
self.assertIsNone(sys.exception())
|
|
|
|
def test_nested_gen_except_loop(self):
|
|
def gen():
|
|
for i in range(100):
|
|
self.assertIsInstance(sys.exception(), TypeError)
|
|
yield "doing"
|
|
|
|
def outer():
|
|
try:
|
|
raise TypeError
|
|
except:
|
|
for x in gen():
|
|
yield x
|
|
|
|
try:
|
|
raise ValueError
|
|
except Exception:
|
|
for x in outer():
|
|
self.assertEqual(x, "doing")
|
|
self.assertEqual(sys.exception(), None)
|
|
|
|
def test_except_throw_exception_context(self):
|
|
def gen():
|
|
try:
|
|
try:
|
|
self.assertIsNone(sys.exception())
|
|
yield
|
|
except ValueError:
|
|
# we are called from "except ValueError:"
|
|
self.assertIsInstance(sys.exception(), ValueError)
|
|
raise TypeError()
|
|
except Exception as exc:
|
|
self.assertIsInstance(sys.exception(), TypeError)
|
|
self.assertEqual(type(exc.__context__), ValueError)
|
|
# we are still called from "except ValueError:"
|
|
self.assertIsInstance(sys.exception(), ValueError)
|
|
yield
|
|
self.assertIsNone(sys.exception())
|
|
yield "done"
|
|
|
|
g = gen()
|
|
next(g)
|
|
try:
|
|
raise ValueError
|
|
except Exception as exc:
|
|
g.throw(exc)
|
|
|
|
self.assertEqual(next(g), "done")
|
|
self.assertIsNone(sys.exception())
|
|
|
|
def test_except_throw_bad_exception(self):
|
|
class E(Exception):
|
|
def __new__(cls, *args, **kwargs):
|
|
return cls
|
|
|
|
def boring_generator():
|
|
yield
|
|
|
|
gen = boring_generator()
|
|
|
|
err_msg = 'should have returned an instance of BaseException'
|
|
|
|
with self.assertRaisesRegex(TypeError, err_msg):
|
|
gen.throw(E)
|
|
|
|
self.assertRaises(StopIteration, next, gen)
|
|
|
|
def generator():
|
|
with self.assertRaisesRegex(TypeError, err_msg):
|
|
yield
|
|
|
|
gen = generator()
|
|
next(gen)
|
|
with self.assertRaises(StopIteration):
|
|
gen.throw(E)
|
|
|
|
def test_gen_3_arg_deprecation_warning(self):
|
|
def g():
|
|
yield 42
|
|
|
|
gen = g()
|
|
with self.assertWarns(DeprecationWarning):
|
|
with self.assertRaises(TypeError):
|
|
gen.throw(TypeError, TypeError(24), None)
|
|
|
|
def test_stopiteration_error(self):
|
|
# See also PEP 479.
|
|
|
|
def gen():
|
|
raise StopIteration
|
|
yield
|
|
|
|
with self.assertRaisesRegex(RuntimeError, 'raised StopIteration'):
|
|
next(gen())
|
|
|
|
def test_tutorial_stopiteration(self):
|
|
# Raise StopIteration" stops the generator too:
|
|
|
|
def f():
|
|
yield 1
|
|
raise StopIteration
|
|
yield 2 # never reached
|
|
|
|
g = f()
|
|
self.assertEqual(next(g), 1)
|
|
|
|
with self.assertRaisesRegex(RuntimeError, 'raised StopIteration'):
|
|
next(g)
|
|
|
|
def test_return_tuple(self):
|
|
def g():
|
|
return (yield 1)
|
|
|
|
gen = g()
|
|
self.assertEqual(next(gen), 1)
|
|
with self.assertRaises(StopIteration) as cm:
|
|
gen.send((2,))
|
|
self.assertEqual(cm.exception.value, (2,))
|
|
|
|
def test_return_stopiteration(self):
|
|
def g():
|
|
return (yield 1)
|
|
|
|
gen = g()
|
|
self.assertEqual(next(gen), 1)
|
|
with self.assertRaises(StopIteration) as cm:
|
|
gen.send(StopIteration(2))
|
|
self.assertIsInstance(cm.exception.value, StopIteration)
|
|
self.assertEqual(cm.exception.value.value, 2)
|
|
|
|
|
|
class GeneratorCloseTest(unittest.TestCase):
|
|
|
|
def test_close_no_return_value(self):
|
|
def f():
|
|
yield
|
|
|
|
gen = f()
|
|
gen.send(None)
|
|
self.assertIsNone(gen.close())
|
|
|
|
def test_close_return_value(self):
|
|
def f():
|
|
try:
|
|
yield
|
|
# close() raises GeneratorExit here, which is caught
|
|
except GeneratorExit:
|
|
return 0
|
|
|
|
gen = f()
|
|
gen.send(None)
|
|
self.assertEqual(gen.close(), 0)
|
|
|
|
def test_close_not_catching_exit(self):
|
|
def f():
|
|
yield
|
|
# close() raises GeneratorExit here, which isn't caught and
|
|
# therefore propagates -- no return value
|
|
return 0
|
|
|
|
gen = f()
|
|
gen.send(None)
|
|
self.assertIsNone(gen.close())
|
|
|
|
def test_close_not_started(self):
|
|
def f():
|
|
try:
|
|
yield
|
|
except GeneratorExit:
|
|
return 0
|
|
|
|
gen = f()
|
|
self.assertIsNone(gen.close())
|
|
|
|
def test_close_exhausted(self):
|
|
def f():
|
|
try:
|
|
yield
|
|
except GeneratorExit:
|
|
return 0
|
|
|
|
gen = f()
|
|
next(gen)
|
|
with self.assertRaises(StopIteration):
|
|
next(gen)
|
|
self.assertIsNone(gen.close())
|
|
|
|
def test_close_closed(self):
|
|
def f():
|
|
try:
|
|
yield
|
|
except GeneratorExit:
|
|
return 0
|
|
|
|
gen = f()
|
|
gen.send(None)
|
|
self.assertEqual(gen.close(), 0)
|
|
self.assertIsNone(gen.close())
|
|
|
|
def test_close_raises(self):
|
|
def f():
|
|
try:
|
|
yield
|
|
except GeneratorExit:
|
|
pass
|
|
raise RuntimeError
|
|
|
|
gen = f()
|
|
gen.send(None)
|
|
with self.assertRaises(RuntimeError):
|
|
gen.close()
|
|
|
|
def test_close_releases_frame_locals(self):
|
|
# See gh-118272
|
|
|
|
class Foo:
|
|
pass
|
|
|
|
f = Foo()
|
|
f_wr = weakref.ref(f)
|
|
|
|
def genfn():
|
|
a = f
|
|
yield
|
|
|
|
g = genfn()
|
|
next(g)
|
|
del f
|
|
g.close()
|
|
support.gc_collect()
|
|
self.assertIsNone(f_wr())
|
|
|
|
|
|
# See https://github.com/python/cpython/issues/125723
|
|
class GeneratorDeallocTest(unittest.TestCase):
|
|
@unittest.expectedFailure # TODO: RUSTPYTHON; frame uses shared Arc, no ownership transfer
|
|
def test_frame_outlives_generator(self):
|
|
def g1():
|
|
a = 42
|
|
yield sys._getframe()
|
|
|
|
def g2():
|
|
a = 42
|
|
yield
|
|
|
|
def g3(obj):
|
|
a = 42
|
|
obj.frame = sys._getframe()
|
|
yield
|
|
|
|
class ObjectWithFrame():
|
|
def __init__(self):
|
|
self.frame = None
|
|
|
|
def get_frame(index):
|
|
if index == 1:
|
|
return next(g1())
|
|
elif index == 2:
|
|
gen = g2()
|
|
next(gen)
|
|
return gen.gi_frame
|
|
elif index == 3:
|
|
obj = ObjectWithFrame()
|
|
next(g3(obj))
|
|
return obj.frame
|
|
else:
|
|
return None
|
|
|
|
for index in (1, 2, 3):
|
|
with self.subTest(index=index):
|
|
frame = get_frame(index)
|
|
frame_locals = frame.f_locals
|
|
self.assertIn('a', frame_locals)
|
|
self.assertEqual(frame_locals['a'], 42)
|
|
|
|
def test_frame_locals_outlive_generator(self):
|
|
frame_locals1 = None
|
|
|
|
def g1():
|
|
nonlocal frame_locals1
|
|
frame_locals1 = sys._getframe().f_locals
|
|
a = 42
|
|
yield
|
|
|
|
def g2():
|
|
a = 42
|
|
yield sys._getframe().f_locals
|
|
|
|
def get_frame_locals(index):
|
|
if index == 1:
|
|
nonlocal frame_locals1
|
|
next(g1())
|
|
return frame_locals1
|
|
if index == 2:
|
|
return next(g2())
|
|
else:
|
|
return None
|
|
|
|
for index in (1, 2):
|
|
with self.subTest(index=index):
|
|
frame_locals = get_frame_locals(index)
|
|
self.assertIn('a', frame_locals)
|
|
self.assertEqual(frame_locals['a'], 42)
|
|
|
|
def test_frame_locals_outlive_generator_with_exec(self):
|
|
def g():
|
|
a = 42
|
|
yield locals(), sys._getframe().f_locals
|
|
|
|
locals_ = {'g': g}
|
|
for i in range(10):
|
|
exec("snapshot, live_locals = next(g())", locals=locals_)
|
|
for l in (locals_['snapshot'], locals_['live_locals']):
|
|
self.assertIn('a', l)
|
|
self.assertEqual(l['a'], 42)
|
|
|
|
|
|
class GeneratorThrowTest(unittest.TestCase):
|
|
|
|
def test_exception_context_with_yield(self):
|
|
def f():
|
|
try:
|
|
raise KeyError('a')
|
|
except Exception:
|
|
yield
|
|
|
|
gen = f()
|
|
gen.send(None)
|
|
with self.assertRaises(ValueError) as cm:
|
|
gen.throw(ValueError)
|
|
context = cm.exception.__context__
|
|
self.assertEqual((type(context), context.args), (KeyError, ('a',)))
|
|
|
|
def test_exception_context_with_yield_inside_generator(self):
|
|
# Check that the context is also available from inside the generator
|
|
# with yield, as opposed to outside.
|
|
def f():
|
|
try:
|
|
raise KeyError('a')
|
|
except Exception:
|
|
try:
|
|
yield
|
|
except Exception as exc:
|
|
self.assertEqual(type(exc), ValueError)
|
|
context = exc.__context__
|
|
self.assertEqual((type(context), context.args),
|
|
(KeyError, ('a',)))
|
|
yield 'b'
|
|
|
|
gen = f()
|
|
gen.send(None)
|
|
actual = gen.throw(ValueError)
|
|
# This ensures that the assertions inside were executed.
|
|
self.assertEqual(actual, 'b')
|
|
|
|
def test_exception_context_with_yield_from(self):
|
|
def f():
|
|
yield
|
|
|
|
def g():
|
|
try:
|
|
raise KeyError('a')
|
|
except Exception:
|
|
yield from f()
|
|
|
|
gen = g()
|
|
gen.send(None)
|
|
with self.assertRaises(ValueError) as cm:
|
|
gen.throw(ValueError)
|
|
context = cm.exception.__context__
|
|
self.assertEqual((type(context), context.args), (KeyError, ('a',)))
|
|
|
|
def test_exception_context_with_yield_from_with_context_cycle(self):
|
|
# Check trying to create an exception context cycle:
|
|
# https://bugs.python.org/issue40696
|
|
has_cycle = None
|
|
|
|
def f():
|
|
yield
|
|
|
|
def g(exc):
|
|
nonlocal has_cycle
|
|
try:
|
|
raise exc
|
|
except Exception:
|
|
try:
|
|
yield from f()
|
|
except Exception as exc:
|
|
has_cycle = (exc is exc.__context__)
|
|
yield
|
|
|
|
exc = KeyError('a')
|
|
gen = g(exc)
|
|
gen.send(None)
|
|
gen.throw(exc)
|
|
# This also distinguishes from the initial has_cycle=None.
|
|
self.assertEqual(has_cycle, False)
|
|
|
|
def test_throw_after_none_exc_type(self):
|
|
def g():
|
|
try:
|
|
raise KeyError
|
|
except KeyError:
|
|
pass
|
|
|
|
try:
|
|
yield
|
|
except Exception:
|
|
raise RuntimeError
|
|
|
|
gen = g()
|
|
gen.send(None)
|
|
with self.assertRaises(RuntimeError) as cm:
|
|
gen.throw(ValueError)
|
|
|
|
|
|
class GeneratorStackTraceTest(unittest.TestCase):
|
|
|
|
def check_stack_names(self, frame, expected):
|
|
names = []
|
|
while frame:
|
|
name = frame.f_code.co_name
|
|
# Stop checking frames when we get to our test helper.
|
|
if (name.startswith('check_') or name.startswith('call_')
|
|
or name.startswith('test')):
|
|
break
|
|
|
|
names.append(name)
|
|
frame = frame.f_back
|
|
|
|
self.assertEqual(names, expected)
|
|
|
|
def check_yield_from_example(self, call_method):
|
|
def f():
|
|
self.check_stack_names(sys._getframe(), ['f', 'g'])
|
|
try:
|
|
yield
|
|
except Exception:
|
|
pass
|
|
self.check_stack_names(sys._getframe(), ['f', 'g'])
|
|
|
|
def g():
|
|
self.check_stack_names(sys._getframe(), ['g'])
|
|
yield from f()
|
|
self.check_stack_names(sys._getframe(), ['g'])
|
|
|
|
gen = g()
|
|
gen.send(None)
|
|
try:
|
|
call_method(gen)
|
|
except StopIteration:
|
|
pass
|
|
|
|
def test_send_with_yield_from(self):
|
|
def call_send(gen):
|
|
gen.send(None)
|
|
|
|
self.check_yield_from_example(call_send)
|
|
|
|
def test_throw_with_yield_from(self):
|
|
def call_throw(gen):
|
|
gen.throw(RuntimeError)
|
|
|
|
self.check_yield_from_example(call_throw)
|
|
|
|
def test_throw_with_yield_from_custom_generator(self):
|
|
|
|
class CustomGen:
|
|
def __init__(self, test):
|
|
self.test = test
|
|
def throw(self, *args):
|
|
self.test.check_stack_names(sys._getframe(), ['throw', 'g'])
|
|
def __iter__(self):
|
|
return self
|
|
def __next__(self):
|
|
return 42
|
|
|
|
def g(target):
|
|
yield from target
|
|
|
|
gen = g(CustomGen(self))
|
|
gen.send(None)
|
|
gen.throw(RuntimeError)
|
|
|
|
|
|
class YieldFromTests(unittest.TestCase):
|
|
def test_generator_gi_yieldfrom(self):
|
|
def a():
|
|
self.assertEqual(inspect.getgeneratorstate(gen_b), inspect.GEN_RUNNING)
|
|
self.assertIsNone(gen_b.gi_yieldfrom)
|
|
yield
|
|
self.assertEqual(inspect.getgeneratorstate(gen_b), inspect.GEN_RUNNING)
|
|
self.assertIsNone(gen_b.gi_yieldfrom)
|
|
|
|
def b():
|
|
self.assertIsNone(gen_b.gi_yieldfrom)
|
|
yield from a()
|
|
self.assertIsNone(gen_b.gi_yieldfrom)
|
|
yield
|
|
self.assertIsNone(gen_b.gi_yieldfrom)
|
|
|
|
gen_b = b()
|
|
self.assertEqual(inspect.getgeneratorstate(gen_b), inspect.GEN_CREATED)
|
|
self.assertIsNone(gen_b.gi_yieldfrom)
|
|
|
|
gen_b.send(None)
|
|
self.assertEqual(inspect.getgeneratorstate(gen_b), inspect.GEN_SUSPENDED)
|
|
self.assertEqual(gen_b.gi_yieldfrom.gi_code.co_name, 'a')
|
|
|
|
gen_b.send(None)
|
|
self.assertEqual(inspect.getgeneratorstate(gen_b), inspect.GEN_SUSPENDED)
|
|
self.assertIsNone(gen_b.gi_yieldfrom)
|
|
|
|
[] = gen_b # Exhaust generator
|
|
self.assertEqual(inspect.getgeneratorstate(gen_b), inspect.GEN_CLOSED)
|
|
self.assertIsNone(gen_b.gi_yieldfrom)
|
|
|
|
|
|
tutorial_tests = """
|
|
Let's try a simple generator:
|
|
|
|
>>> def f():
|
|
... yield 1
|
|
... yield 2
|
|
|
|
>>> for i in f():
|
|
... print(i)
|
|
1
|
|
2
|
|
>>> g = f()
|
|
>>> next(g)
|
|
1
|
|
>>> next(g)
|
|
2
|
|
|
|
"Falling off the end" stops the generator:
|
|
|
|
>>> next(g)
|
|
Traceback (most recent call last):
|
|
File "<stdin>", line 1, in ?
|
|
File "<stdin>", line 2, in g
|
|
StopIteration
|
|
|
|
"return" also stops the generator:
|
|
|
|
>>> def f():
|
|
... yield 1
|
|
... return
|
|
... yield 2 # never reached
|
|
...
|
|
>>> g = f()
|
|
>>> next(g)
|
|
1
|
|
>>> next(g)
|
|
Traceback (most recent call last):
|
|
File "<stdin>", line 1, in ?
|
|
File "<stdin>", line 3, in f
|
|
StopIteration
|
|
>>> next(g) # once stopped, can't be resumed
|
|
Traceback (most recent call last):
|
|
File "<stdin>", line 1, in ?
|
|
StopIteration
|
|
|
|
However, "return" and StopIteration are not exactly equivalent:
|
|
|
|
>>> def g1():
|
|
... try:
|
|
... return
|
|
... except:
|
|
... yield 1
|
|
...
|
|
>>> list(g1())
|
|
[]
|
|
|
|
>>> def g2():
|
|
... try:
|
|
... raise StopIteration
|
|
... except:
|
|
... yield 42
|
|
>>> print(list(g2()))
|
|
[42]
|
|
|
|
This may be surprising at first:
|
|
|
|
>>> def g3():
|
|
... try:
|
|
... return
|
|
... finally:
|
|
... yield 1
|
|
...
|
|
>>> list(g3())
|
|
[1]
|
|
|
|
Let's create an alternate range() function implemented as a generator:
|
|
|
|
>>> def yrange(n):
|
|
... for i in range(n):
|
|
... yield i
|
|
...
|
|
>>> list(yrange(5))
|
|
[0, 1, 2, 3, 4]
|
|
|
|
Generators always return to the most recent caller:
|
|
|
|
>>> def creator():
|
|
... r = yrange(5)
|
|
... print("creator", next(r))
|
|
... return r
|
|
...
|
|
>>> def caller():
|
|
... r = creator()
|
|
... for i in r:
|
|
... print("caller", i)
|
|
...
|
|
>>> caller()
|
|
creator 0
|
|
caller 1
|
|
caller 2
|
|
caller 3
|
|
caller 4
|
|
|
|
Generators can call other generators:
|
|
|
|
>>> def zrange(n):
|
|
... for i in yrange(n):
|
|
... yield i
|
|
...
|
|
>>> list(zrange(5))
|
|
[0, 1, 2, 3, 4]
|
|
|
|
"""
|
|
|
|
# The examples from PEP 255.
|
|
|
|
pep_tests = """
|
|
|
|
Specification: Yield
|
|
|
|
Restriction: A generator cannot be resumed while it is actively
|
|
running:
|
|
|
|
>>> def g():
|
|
... i = next(me)
|
|
... yield i
|
|
>>> me = g()
|
|
>>> next(me)
|
|
Traceback (most recent call last):
|
|
...
|
|
File "<string>", line 2, in g
|
|
ValueError: generator already executing
|
|
|
|
Specification: Return
|
|
|
|
Note that return isn't always equivalent to raising StopIteration: the
|
|
difference lies in how enclosing try/except constructs are treated.
|
|
For example,
|
|
|
|
>>> def f1():
|
|
... try:
|
|
... return
|
|
... except:
|
|
... yield 1
|
|
>>> print(list(f1()))
|
|
[]
|
|
|
|
because, as in any function, return simply exits, but
|
|
|
|
>>> def f2():
|
|
... try:
|
|
... raise StopIteration
|
|
... except:
|
|
... yield 42
|
|
>>> print(list(f2()))
|
|
[42]
|
|
|
|
because StopIteration is captured by a bare "except", as is any
|
|
exception.
|
|
|
|
Specification: Generators and Exception Propagation
|
|
|
|
>>> def f():
|
|
... return 1//0
|
|
>>> def g():
|
|
... yield f() # the zero division exception propagates
|
|
... yield 42 # and we'll never get here
|
|
>>> k = g()
|
|
>>> next(k)
|
|
Traceback (most recent call last):
|
|
File "<stdin>", line 1, in ?
|
|
File "<stdin>", line 2, in g
|
|
File "<stdin>", line 2, in f
|
|
ZeroDivisionError: division by zero
|
|
>>> next(k) # and the generator cannot be resumed
|
|
Traceback (most recent call last):
|
|
File "<stdin>", line 1, in ?
|
|
StopIteration
|
|
>>>
|
|
|
|
Specification: Try/Except/Finally
|
|
|
|
>>> def f():
|
|
... try:
|
|
... yield 1
|
|
... try:
|
|
... yield 2
|
|
... 1//0
|
|
... yield 3 # never get here
|
|
... except ZeroDivisionError:
|
|
... yield 4
|
|
... yield 5
|
|
... raise
|
|
... except:
|
|
... yield 6
|
|
... yield 7 # the "raise" above stops this
|
|
... except:
|
|
... yield 8
|
|
... yield 9
|
|
... try:
|
|
... x = 12
|
|
... finally:
|
|
... yield 10
|
|
... yield 11
|
|
>>> print(list(f()))
|
|
[1, 2, 4, 5, 8, 9, 10, 11]
|
|
>>>
|
|
|
|
Guido's binary tree example.
|
|
|
|
>>> # A binary tree class.
|
|
>>> class Tree:
|
|
...
|
|
... def __init__(self, label, left=None, right=None):
|
|
... self.label = label
|
|
... self.left = left
|
|
... self.right = right
|
|
...
|
|
... def __repr__(self, level=0, indent=" "):
|
|
... s = level*indent + repr(self.label)
|
|
... if self.left:
|
|
... s = s + "\\n" + self.left.__repr__(level+1, indent)
|
|
... if self.right:
|
|
... s = s + "\\n" + self.right.__repr__(level+1, indent)
|
|
... return s
|
|
...
|
|
... def __iter__(self):
|
|
... return inorder(self)
|
|
|
|
>>> # Create a Tree from a list.
|
|
>>> def tree(list):
|
|
... n = len(list)
|
|
... if n == 0:
|
|
... return []
|
|
... i = n // 2
|
|
... return Tree(list[i], tree(list[:i]), tree(list[i+1:]))
|
|
|
|
>>> # Show it off: create a tree.
|
|
>>> t = tree("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
|
|
|
|
>>> # A recursive generator that generates Tree labels in in-order.
|
|
>>> def inorder(t):
|
|
... if t:
|
|
... for x in inorder(t.left):
|
|
... yield x
|
|
... yield t.label
|
|
... for x in inorder(t.right):
|
|
... yield x
|
|
|
|
>>> # Show it off: create a tree.
|
|
>>> t = tree("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
|
|
>>> # Print the nodes of the tree in in-order.
|
|
>>> for x in t:
|
|
... print(' '+x, end='')
|
|
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
|
|
|
|
>>> # A non-recursive generator.
|
|
>>> def inorder(node):
|
|
... stack = []
|
|
... while node:
|
|
... while node.left:
|
|
... stack.append(node)
|
|
... node = node.left
|
|
... yield node.label
|
|
... while not node.right:
|
|
... try:
|
|
... node = stack.pop()
|
|
... except IndexError:
|
|
... return
|
|
... yield node.label
|
|
... node = node.right
|
|
|
|
>>> # Exercise the non-recursive generator.
|
|
>>> for x in t:
|
|
... print(' '+x, end='')
|
|
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
|
|
|
|
"""
|
|
|
|
# Examples from Iterator-List and Python-Dev and c.l.py.
|
|
|
|
email_tests = """
|
|
|
|
The difference between yielding None and returning it.
|
|
|
|
>>> def g():
|
|
... for i in range(3):
|
|
... yield None
|
|
... yield None
|
|
... return
|
|
>>> list(g())
|
|
[None, None, None, None]
|
|
|
|
Ensure that explicitly raising StopIteration acts like any other exception
|
|
in try/except, not like a return.
|
|
|
|
>>> def g():
|
|
... yield 1
|
|
... try:
|
|
... raise StopIteration
|
|
... except:
|
|
... yield 2
|
|
... yield 3
|
|
>>> list(g())
|
|
[1, 2, 3]
|
|
|
|
Next one was posted to c.l.py.
|
|
|
|
>>> def gcomb(x, k):
|
|
... "Generate all combinations of k elements from list x."
|
|
...
|
|
... if k > len(x):
|
|
... return
|
|
... if k == 0:
|
|
... yield []
|
|
... else:
|
|
... first, rest = x[0], x[1:]
|
|
... # A combination does or doesn't contain first.
|
|
... # If it does, the remainder is a k-1 comb of rest.
|
|
... for c in gcomb(rest, k-1):
|
|
... c.insert(0, first)
|
|
... yield c
|
|
... # If it doesn't contain first, it's a k comb of rest.
|
|
... for c in gcomb(rest, k):
|
|
... yield c
|
|
|
|
>>> seq = list(range(1, 5))
|
|
>>> for k in range(len(seq) + 2):
|
|
... print("%d-combs of %s:" % (k, seq))
|
|
... for c in gcomb(seq, k):
|
|
... print(" ", c)
|
|
0-combs of [1, 2, 3, 4]:
|
|
[]
|
|
1-combs of [1, 2, 3, 4]:
|
|
[1]
|
|
[2]
|
|
[3]
|
|
[4]
|
|
2-combs of [1, 2, 3, 4]:
|
|
[1, 2]
|
|
[1, 3]
|
|
[1, 4]
|
|
[2, 3]
|
|
[2, 4]
|
|
[3, 4]
|
|
3-combs of [1, 2, 3, 4]:
|
|
[1, 2, 3]
|
|
[1, 2, 4]
|
|
[1, 3, 4]
|
|
[2, 3, 4]
|
|
4-combs of [1, 2, 3, 4]:
|
|
[1, 2, 3, 4]
|
|
5-combs of [1, 2, 3, 4]:
|
|
|
|
From the Iterators list, about the types of these things.
|
|
|
|
>>> def g():
|
|
... yield 1
|
|
...
|
|
>>> type(g)
|
|
<class 'function'>
|
|
>>> i = g()
|
|
>>> type(i)
|
|
<class 'generator'>
|
|
>>> [s for s in dir(i) if not s.startswith('_')]
|
|
['close', 'gi_code', 'gi_frame', 'gi_running', 'gi_suspended', 'gi_yieldfrom', 'send', 'throw']
|
|
>>> from test.support import HAVE_DOCSTRINGS
|
|
>>> print(i.__next__.__doc__ if HAVE_DOCSTRINGS else 'Implement next(self).') # TODO: RUSTPYTHON # doctest: +EXPECTED_FAILURE
|
|
Implement next(self).
|
|
>>> iter(i) is i
|
|
True
|
|
>>> import types
|
|
>>> isinstance(i, types.GeneratorType)
|
|
True
|
|
|
|
And more, added later.
|
|
|
|
>>> i.gi_running
|
|
0
|
|
>>> type(i.gi_frame)
|
|
<class 'frame'>
|
|
>>> i.gi_running = 42
|
|
Traceback (most recent call last):
|
|
...
|
|
AttributeError: attribute 'gi_running' of 'generator' objects is not writable
|
|
>>> def g():
|
|
... yield me.gi_running
|
|
>>> me = g()
|
|
>>> me.gi_running
|
|
0
|
|
>>> next(me)
|
|
1
|
|
>>> me.gi_running
|
|
0
|
|
|
|
A clever union-find implementation from c.l.py, due to David Eppstein.
|
|
Sent: Friday, June 29, 2001 12:16 PM
|
|
To: python-list@python.org
|
|
Subject: Re: PEP 255: Simple Generators
|
|
|
|
>>> class disjointSet:
|
|
... def __init__(self, name):
|
|
... self.name = name
|
|
... self.parent = None
|
|
... self.generator = self.generate()
|
|
...
|
|
... def generate(self):
|
|
... while not self.parent:
|
|
... yield self
|
|
... for x in self.parent.generator:
|
|
... yield x
|
|
...
|
|
... def find(self):
|
|
... return next(self.generator)
|
|
...
|
|
... def union(self, parent):
|
|
... if self.parent:
|
|
... raise ValueError("Sorry, I'm not a root!")
|
|
... self.parent = parent
|
|
...
|
|
... def __str__(self):
|
|
... return self.name
|
|
|
|
>>> names = "ABCDEFGHIJKLM"
|
|
>>> sets = [disjointSet(name) for name in names]
|
|
>>> roots = sets[:]
|
|
|
|
>>> import random
|
|
>>> gen = random.Random(42)
|
|
>>> while 1:
|
|
... for s in sets:
|
|
... print(" %s->%s" % (s, s.find()), end='')
|
|
... print()
|
|
... if len(roots) > 1:
|
|
... s1 = gen.choice(roots)
|
|
... roots.remove(s1)
|
|
... s2 = gen.choice(roots)
|
|
... s1.union(s2)
|
|
... print("merged", s1, "into", s2)
|
|
... else:
|
|
... break
|
|
A->A B->B C->C D->D E->E F->F G->G H->H I->I J->J K->K L->L M->M
|
|
merged K into B
|
|
A->A B->B C->C D->D E->E F->F G->G H->H I->I J->J K->B L->L M->M
|
|
merged A into F
|
|
A->F B->B C->C D->D E->E F->F G->G H->H I->I J->J K->B L->L M->M
|
|
merged E into F
|
|
A->F B->B C->C D->D E->F F->F G->G H->H I->I J->J K->B L->L M->M
|
|
merged D into C
|
|
A->F B->B C->C D->C E->F F->F G->G H->H I->I J->J K->B L->L M->M
|
|
merged M into C
|
|
A->F B->B C->C D->C E->F F->F G->G H->H I->I J->J K->B L->L M->C
|
|
merged J into B
|
|
A->F B->B C->C D->C E->F F->F G->G H->H I->I J->B K->B L->L M->C
|
|
merged B into C
|
|
A->F B->C C->C D->C E->F F->F G->G H->H I->I J->C K->C L->L M->C
|
|
merged F into G
|
|
A->G B->C C->C D->C E->G F->G G->G H->H I->I J->C K->C L->L M->C
|
|
merged L into C
|
|
A->G B->C C->C D->C E->G F->G G->G H->H I->I J->C K->C L->C M->C
|
|
merged G into I
|
|
A->I B->C C->C D->C E->I F->I G->I H->H I->I J->C K->C L->C M->C
|
|
merged I into H
|
|
A->H B->C C->C D->C E->H F->H G->H H->H I->H J->C K->C L->C M->C
|
|
merged C into H
|
|
A->H B->H C->H D->H E->H F->H G->H H->H I->H J->H K->H L->H M->H
|
|
|
|
"""
|
|
# Emacs turd '
|
|
|
|
# Fun tests (for sufficiently warped notions of "fun").
|
|
|
|
fun_tests = """
|
|
|
|
Build up to a recursive Sieve of Eratosthenes generator.
|
|
|
|
>>> def firstn(g, n):
|
|
... return [next(g) for i in range(n)]
|
|
|
|
>>> def intsfrom(i):
|
|
... while 1:
|
|
... yield i
|
|
... i += 1
|
|
|
|
>>> firstn(intsfrom(5), 7)
|
|
[5, 6, 7, 8, 9, 10, 11]
|
|
|
|
>>> def exclude_multiples(n, ints):
|
|
... for i in ints:
|
|
... if i % n:
|
|
... yield i
|
|
|
|
>>> firstn(exclude_multiples(3, intsfrom(1)), 6)
|
|
[1, 2, 4, 5, 7, 8]
|
|
|
|
>>> def sieve(ints):
|
|
... prime = next(ints)
|
|
... yield prime
|
|
... not_divisible_by_prime = exclude_multiples(prime, ints)
|
|
... for p in sieve(not_divisible_by_prime):
|
|
... yield p
|
|
|
|
>>> primes = sieve(intsfrom(2))
|
|
>>> firstn(primes, 20)
|
|
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71]
|
|
|
|
|
|
Another famous problem: generate all integers of the form
|
|
2**i * 3**j * 5**k
|
|
in increasing order, where i,j,k >= 0. Trickier than it may look at first!
|
|
Try writing it without generators, and correctly, and without generating
|
|
3 internal results for each result output.
|
|
|
|
>>> def times(n, g):
|
|
... for i in g:
|
|
... yield n * i
|
|
>>> firstn(times(10, intsfrom(1)), 10)
|
|
[10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
|
|
|
|
>>> def merge(g, h):
|
|
... ng = next(g)
|
|
... nh = next(h)
|
|
... while 1:
|
|
... if ng < nh:
|
|
... yield ng
|
|
... ng = next(g)
|
|
... elif ng > nh:
|
|
... yield nh
|
|
... nh = next(h)
|
|
... else:
|
|
... yield ng
|
|
... ng = next(g)
|
|
... nh = next(h)
|
|
|
|
The following works, but is doing a whale of a lot of redundant work --
|
|
it's not clear how to get the internal uses of m235 to share a single
|
|
generator. Note that me_times2 (etc) each need to see every element in the
|
|
result sequence. So this is an example where lazy lists are more natural
|
|
(you can look at the head of a lazy list any number of times).
|
|
|
|
>>> def m235():
|
|
... yield 1
|
|
... me_times2 = times(2, m235())
|
|
... me_times3 = times(3, m235())
|
|
... me_times5 = times(5, m235())
|
|
... for i in merge(merge(me_times2,
|
|
... me_times3),
|
|
... me_times5):
|
|
... yield i
|
|
|
|
Don't print "too many" of these -- the implementation above is extremely
|
|
inefficient: each call of m235() leads to 3 recursive calls, and in
|
|
turn each of those 3 more, and so on, and so on, until we've descended
|
|
enough levels to satisfy the print stmts. Very odd: when I printed 5
|
|
lines of results below, this managed to screw up Win98's malloc in "the
|
|
usual" way, i.e. the heap grew over 4Mb so Win98 started fragmenting
|
|
address space, and it *looked* like a very slow leak.
|
|
|
|
>>> result = m235()
|
|
>>> for i in range(3):
|
|
... print(firstn(result, 15))
|
|
[1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, 16, 18, 20, 24]
|
|
[25, 27, 30, 32, 36, 40, 45, 48, 50, 54, 60, 64, 72, 75, 80]
|
|
[81, 90, 96, 100, 108, 120, 125, 128, 135, 144, 150, 160, 162, 180, 192]
|
|
|
|
Heh. Here's one way to get a shared list, complete with an excruciating
|
|
namespace renaming trick. The *pretty* part is that the times() and merge()
|
|
functions can be reused as-is, because they only assume their stream
|
|
arguments are iterable -- a LazyList is the same as a generator to times().
|
|
|
|
>>> class LazyList:
|
|
... def __init__(self, g):
|
|
... self.sofar = []
|
|
... self.fetch = g.__next__
|
|
...
|
|
... def __getitem__(self, i):
|
|
... sofar, fetch = self.sofar, self.fetch
|
|
... while i >= len(sofar):
|
|
... sofar.append(fetch())
|
|
... return sofar[i]
|
|
|
|
>>> def m235():
|
|
... yield 1
|
|
... # Gack: m235 below actually refers to a LazyList.
|
|
... me_times2 = times(2, m235)
|
|
... me_times3 = times(3, m235)
|
|
... me_times5 = times(5, m235)
|
|
... for i in merge(merge(me_times2,
|
|
... me_times3),
|
|
... me_times5):
|
|
... yield i
|
|
|
|
Print as many of these as you like -- *this* implementation is memory-
|
|
efficient.
|
|
|
|
>>> m235 = LazyList(m235())
|
|
>>> for i in range(5):
|
|
... print([m235[j] for j in range(15*i, 15*(i+1))])
|
|
[1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, 16, 18, 20, 24]
|
|
[25, 27, 30, 32, 36, 40, 45, 48, 50, 54, 60, 64, 72, 75, 80]
|
|
[81, 90, 96, 100, 108, 120, 125, 128, 135, 144, 150, 160, 162, 180, 192]
|
|
[200, 216, 225, 240, 243, 250, 256, 270, 288, 300, 320, 324, 360, 375, 384]
|
|
[400, 405, 432, 450, 480, 486, 500, 512, 540, 576, 600, 625, 640, 648, 675]
|
|
|
|
Ye olde Fibonacci generator, LazyList style.
|
|
|
|
>>> def fibgen(a, b):
|
|
...
|
|
... def sum(g, h):
|
|
... while 1:
|
|
... yield next(g) + next(h)
|
|
...
|
|
... def tail(g):
|
|
... next(g) # throw first away
|
|
... for x in g:
|
|
... yield x
|
|
...
|
|
... yield a
|
|
... yield b
|
|
... for s in sum(iter(fib),
|
|
... tail(iter(fib))):
|
|
... yield s
|
|
|
|
>>> fib = LazyList(fibgen(1, 2))
|
|
>>> firstn(iter(fib), 17)
|
|
[1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584]
|
|
|
|
|
|
Running after your tail with itertools.tee (new in version 2.4)
|
|
|
|
The algorithms "m235" (Hamming) and Fibonacci presented above are both
|
|
examples of a whole family of FP (functional programming) algorithms
|
|
where a function produces and returns a list while the production algorithm
|
|
suppose the list as already produced by recursively calling itself.
|
|
For these algorithms to work, they must:
|
|
|
|
- produce at least a first element without presupposing the existence of
|
|
the rest of the list
|
|
- produce their elements in a lazy manner
|
|
|
|
To work efficiently, the beginning of the list must not be recomputed over
|
|
and over again. This is ensured in most FP languages as a built-in feature.
|
|
In python, we have to explicitly maintain a list of already computed results
|
|
and abandon genuine recursivity.
|
|
|
|
This is what had been attempted above with the LazyList class. One problem
|
|
with that class is that it keeps a list of all of the generated results and
|
|
therefore continually grows. This partially defeats the goal of the generator
|
|
concept, viz. produce the results only as needed instead of producing them
|
|
all and thereby wasting memory.
|
|
|
|
Thanks to itertools.tee, it is now clear "how to get the internal uses of
|
|
m235 to share a single generator".
|
|
|
|
>>> from itertools import tee
|
|
>>> def m235():
|
|
... def _m235():
|
|
... yield 1
|
|
... for n in merge(times(2, m2),
|
|
... merge(times(3, m3),
|
|
... times(5, m5))):
|
|
... yield n
|
|
... m1 = _m235()
|
|
... m2, m3, m5, mRes = tee(m1, 4)
|
|
... return mRes
|
|
|
|
>>> it = m235()
|
|
>>> for i in range(5):
|
|
... print(firstn(it, 15))
|
|
[1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, 16, 18, 20, 24]
|
|
[25, 27, 30, 32, 36, 40, 45, 48, 50, 54, 60, 64, 72, 75, 80]
|
|
[81, 90, 96, 100, 108, 120, 125, 128, 135, 144, 150, 160, 162, 180, 192]
|
|
[200, 216, 225, 240, 243, 250, 256, 270, 288, 300, 320, 324, 360, 375, 384]
|
|
[400, 405, 432, 450, 480, 486, 500, 512, 540, 576, 600, 625, 640, 648, 675]
|
|
|
|
The "tee" function does just what we want. It internally keeps a generated
|
|
result for as long as it has not been "consumed" from all of the duplicated
|
|
iterators, whereupon it is deleted. You can therefore print the hamming
|
|
sequence during hours without increasing memory usage, or very little.
|
|
|
|
The beauty of it is that recursive running-after-their-tail FP algorithms
|
|
are quite straightforwardly expressed with this Python idiom.
|
|
|
|
Ye olde Fibonacci generator, tee style.
|
|
|
|
>>> def fib():
|
|
...
|
|
... def _isum(g, h):
|
|
... while 1:
|
|
... yield next(g) + next(h)
|
|
...
|
|
... def _fib():
|
|
... yield 1
|
|
... yield 2
|
|
... next(fibTail) # throw first away
|
|
... for res in _isum(fibHead, fibTail):
|
|
... yield res
|
|
...
|
|
... realfib = _fib()
|
|
... fibHead, fibTail, fibRes = tee(realfib, 3)
|
|
... return fibRes
|
|
|
|
>>> firstn(fib(), 17)
|
|
[1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584]
|
|
|
|
"""
|
|
|
|
# syntax_tests mostly provokes SyntaxErrors. Also fiddling with #if 0
|
|
# hackery.
|
|
|
|
syntax_tests = """
|
|
|
|
These are fine:
|
|
|
|
>>> def f():
|
|
... yield 1
|
|
... return
|
|
|
|
>>> def f():
|
|
... try:
|
|
... yield 1
|
|
... finally:
|
|
... pass
|
|
|
|
>>> def f():
|
|
... try:
|
|
... try:
|
|
... 1//0
|
|
... except ZeroDivisionError:
|
|
... yield 666
|
|
... except:
|
|
... pass
|
|
... finally:
|
|
... pass
|
|
|
|
>>> def f():
|
|
... try:
|
|
... try:
|
|
... yield 12
|
|
... 1//0
|
|
... except ZeroDivisionError:
|
|
... yield 666
|
|
... except:
|
|
... try:
|
|
... x = 12
|
|
... finally:
|
|
... yield 12
|
|
... except:
|
|
... return
|
|
>>> list(f())
|
|
[12, 666]
|
|
|
|
>>> def f():
|
|
... yield
|
|
>>> type(f())
|
|
<class 'generator'>
|
|
|
|
|
|
>>> def f():
|
|
... if 0:
|
|
... yield
|
|
>>> type(f())
|
|
<class 'generator'>
|
|
|
|
|
|
>>> def f():
|
|
... if 0:
|
|
... yield 1
|
|
>>> type(f())
|
|
<class 'generator'>
|
|
|
|
>>> def f():
|
|
... if "":
|
|
... yield None
|
|
>>> type(f())
|
|
<class 'generator'>
|
|
|
|
>>> def f():
|
|
... return
|
|
... try:
|
|
... if x==4:
|
|
... pass
|
|
... elif 0:
|
|
... try:
|
|
... 1//0
|
|
... except SyntaxError:
|
|
... pass
|
|
... else:
|
|
... if 0:
|
|
... while 12:
|
|
... x += 1
|
|
... yield 2 # don't blink
|
|
... f(a, b, c, d, e)
|
|
... else:
|
|
... pass
|
|
... except:
|
|
... x = 1
|
|
... return
|
|
>>> type(f())
|
|
<class 'generator'>
|
|
|
|
>>> def f():
|
|
... if 0:
|
|
... def g():
|
|
... yield 1
|
|
...
|
|
>>> type(f())
|
|
<class 'NoneType'>
|
|
|
|
>>> def f():
|
|
... if 0:
|
|
... class C:
|
|
... def __init__(self):
|
|
... yield 1
|
|
... def f(self):
|
|
... yield 2
|
|
>>> type(f())
|
|
<class 'NoneType'>
|
|
|
|
>>> def f():
|
|
... if 0:
|
|
... return
|
|
... if 0:
|
|
... yield 2
|
|
>>> type(f())
|
|
<class 'generator'>
|
|
|
|
This one caused a crash (see SF bug 567538):
|
|
|
|
>>> def f():
|
|
... for i in range(3):
|
|
... try:
|
|
... continue
|
|
... finally:
|
|
... yield i
|
|
...
|
|
>>> g = f()
|
|
>>> print(next(g))
|
|
0
|
|
>>> print(next(g))
|
|
1
|
|
>>> print(next(g))
|
|
2
|
|
>>> print(next(g))
|
|
Traceback (most recent call last):
|
|
StopIteration
|
|
|
|
|
|
Test the gi_code attribute
|
|
|
|
>>> def f():
|
|
... yield 5
|
|
...
|
|
>>> g = f()
|
|
>>> g.gi_code is f.__code__
|
|
True
|
|
>>> next(g)
|
|
5
|
|
>>> next(g)
|
|
Traceback (most recent call last):
|
|
StopIteration
|
|
>>> g.gi_code is f.__code__
|
|
True
|
|
|
|
|
|
Test the __name__ attribute and the repr()
|
|
|
|
>>> def f():
|
|
... yield 5
|
|
...
|
|
>>> g = f()
|
|
>>> g.__name__
|
|
'f'
|
|
>>> repr(g) # doctest: +ELLIPSIS
|
|
'<generator object f at ...>'
|
|
|
|
Lambdas shouldn't have their usual return behavior.
|
|
|
|
>>> x = lambda: (yield 1)
|
|
>>> list(x())
|
|
[1]
|
|
|
|
>>> x = lambda: ((yield 1), (yield 2))
|
|
>>> list(x())
|
|
[1, 2]
|
|
"""
|
|
|
|
# conjoin is a simple backtracking generator, named in honor of Icon's
|
|
# "conjunction" control structure. Pass a list of no-argument functions
|
|
# that return iterable objects. Easiest to explain by example: assume the
|
|
# function list [x, y, z] is passed. Then conjoin acts like:
|
|
#
|
|
# def g():
|
|
# values = [None] * 3
|
|
# for values[0] in x():
|
|
# for values[1] in y():
|
|
# for values[2] in z():
|
|
# yield values
|
|
#
|
|
# So some 3-lists of values *may* be generated, each time we successfully
|
|
# get into the innermost loop. If an iterator fails (is exhausted) before
|
|
# then, it "backtracks" to get the next value from the nearest enclosing
|
|
# iterator (the one "to the left"), and starts all over again at the next
|
|
# slot (pumps a fresh iterator). Of course this is most useful when the
|
|
# iterators have side-effects, so that which values *can* be generated at
|
|
# each slot depend on the values iterated at previous slots.
|
|
|
|
def simple_conjoin(gs):
|
|
|
|
values = [None] * len(gs)
|
|
|
|
def gen(i):
|
|
if i >= len(gs):
|
|
yield values
|
|
else:
|
|
for values[i] in gs[i]():
|
|
for x in gen(i+1):
|
|
yield x
|
|
|
|
for x in gen(0):
|
|
yield x
|
|
|
|
# That works fine, but recursing a level and checking i against len(gs) for
|
|
# each item produced is inefficient. By doing manual loop unrolling across
|
|
# generator boundaries, it's possible to eliminate most of that overhead.
|
|
# This isn't worth the bother *in general* for generators, but conjoin() is
|
|
# a core building block for some CPU-intensive generator applications.
|
|
|
|
def conjoin(gs):
|
|
|
|
n = len(gs)
|
|
values = [None] * n
|
|
|
|
# Do one loop nest at time recursively, until the # of loop nests
|
|
# remaining is divisible by 3.
|
|
|
|
def gen(i):
|
|
if i >= n:
|
|
yield values
|
|
|
|
elif (n-i) % 3:
|
|
ip1 = i+1
|
|
for values[i] in gs[i]():
|
|
for x in gen(ip1):
|
|
yield x
|
|
|
|
else:
|
|
for x in _gen3(i):
|
|
yield x
|
|
|
|
# Do three loop nests at a time, recursing only if at least three more
|
|
# remain. Don't call directly: this is an internal optimization for
|
|
# gen's use.
|
|
|
|
def _gen3(i):
|
|
assert i < n and (n-i) % 3 == 0
|
|
ip1, ip2, ip3 = i+1, i+2, i+3
|
|
g, g1, g2 = gs[i : ip3]
|
|
|
|
if ip3 >= n:
|
|
# These are the last three, so we can yield values directly.
|
|
for values[i] in g():
|
|
for values[ip1] in g1():
|
|
for values[ip2] in g2():
|
|
yield values
|
|
|
|
else:
|
|
# At least 6 loop nests remain; peel off 3 and recurse for the
|
|
# rest.
|
|
for values[i] in g():
|
|
for values[ip1] in g1():
|
|
for values[ip2] in g2():
|
|
for x in _gen3(ip3):
|
|
yield x
|
|
|
|
for x in gen(0):
|
|
yield x
|
|
|
|
# And one more approach: For backtracking apps like the Knight's Tour
|
|
# solver below, the number of backtracking levels can be enormous (one
|
|
# level per square, for the Knight's Tour, so that e.g. a 100x100 board
|
|
# needs 10,000 levels). In such cases Python is likely to run out of
|
|
# stack space due to recursion. So here's a recursion-free version of
|
|
# conjoin too.
|
|
# NOTE WELL: This allows large problems to be solved with only trivial
|
|
# demands on stack space. Without explicitly resumable generators, this is
|
|
# much harder to achieve. OTOH, this is much slower (up to a factor of 2)
|
|
# than the fancy unrolled recursive conjoin.
|
|
|
|
def flat_conjoin(gs): # rename to conjoin to run tests with this instead
|
|
n = len(gs)
|
|
values = [None] * n
|
|
iters = [None] * n
|
|
_StopIteration = StopIteration # make local because caught a *lot*
|
|
i = 0
|
|
while 1:
|
|
# Descend.
|
|
try:
|
|
while i < n:
|
|
it = iters[i] = gs[i]().__next__
|
|
values[i] = it()
|
|
i += 1
|
|
except _StopIteration:
|
|
pass
|
|
else:
|
|
assert i == n
|
|
yield values
|
|
|
|
# Backtrack until an older iterator can be resumed.
|
|
i -= 1
|
|
while i >= 0:
|
|
try:
|
|
values[i] = iters[i]()
|
|
# Success! Start fresh at next level.
|
|
i += 1
|
|
break
|
|
except _StopIteration:
|
|
# Continue backtracking.
|
|
i -= 1
|
|
else:
|
|
assert i < 0
|
|
break
|
|
|
|
# A conjoin-based N-Queens solver.
|
|
|
|
class Queens:
|
|
def __init__(self, n):
|
|
self.n = n
|
|
rangen = range(n)
|
|
|
|
# Assign a unique int to each column and diagonal.
|
|
# columns: n of those, range(n).
|
|
# NW-SE diagonals: 2n-1 of these, i-j unique and invariant along
|
|
# each, smallest i-j is 0-(n-1) = 1-n, so add n-1 to shift to 0-
|
|
# based.
|
|
# NE-SW diagonals: 2n-1 of these, i+j unique and invariant along
|
|
# each, smallest i+j is 0, largest is 2n-2.
|
|
|
|
# For each square, compute a bit vector of the columns and
|
|
# diagonals it covers, and for each row compute a function that
|
|
# generates the possibilities for the columns in that row.
|
|
self.rowgenerators = []
|
|
for i in rangen:
|
|
rowuses = [(1 << j) | # column ordinal
|
|
(1 << (n + i-j + n-1)) | # NW-SE ordinal
|
|
(1 << (n + 2*n-1 + i+j)) # NE-SW ordinal
|
|
for j in rangen]
|
|
|
|
def rowgen(rowuses=rowuses):
|
|
for j in rangen:
|
|
uses = rowuses[j]
|
|
if uses & self.used == 0:
|
|
self.used |= uses
|
|
yield j
|
|
self.used &= ~uses
|
|
|
|
self.rowgenerators.append(rowgen)
|
|
|
|
# Generate solutions.
|
|
def solve(self):
|
|
self.used = 0
|
|
for row2col in conjoin(self.rowgenerators):
|
|
yield row2col
|
|
|
|
def printsolution(self, row2col):
|
|
n = self.n
|
|
assert n == len(row2col)
|
|
sep = "+" + "-+" * n
|
|
print(sep)
|
|
for i in range(n):
|
|
squares = [" " for j in range(n)]
|
|
squares[row2col[i]] = "Q"
|
|
print("|" + "|".join(squares) + "|")
|
|
print(sep)
|
|
|
|
# A conjoin-based Knight's Tour solver. This is pretty sophisticated
|
|
# (e.g., when used with flat_conjoin above, and passing hard=1 to the
|
|
# constructor, a 200x200 Knight's Tour was found quickly -- note that we're
|
|
# creating 10s of thousands of generators then!), and is lengthy.
|
|
|
|
class Knights:
|
|
def __init__(self, m, n, hard=0):
|
|
self.m, self.n = m, n
|
|
|
|
# solve() will set up succs[i] to be a list of square #i's
|
|
# successors.
|
|
succs = self.succs = []
|
|
|
|
# Remove i0 from each of its successor's successor lists, i.e.
|
|
# successors can't go back to i0 again. Return 0 if we can
|
|
# detect this makes a solution impossible, else return 1.
|
|
|
|
def remove_from_successors(i0, len=len):
|
|
# If we remove all exits from a free square, we're dead:
|
|
# even if we move to it next, we can't leave it again.
|
|
# If we create a square with one exit, we must visit it next;
|
|
# else somebody else will have to visit it, and since there's
|
|
# only one adjacent, there won't be a way to leave it again.
|
|
# Finally, if we create more than one free square with a
|
|
# single exit, we can only move to one of them next, leaving
|
|
# the other one a dead end.
|
|
ne0 = ne1 = 0
|
|
for i in succs[i0]:
|
|
s = succs[i]
|
|
s.remove(i0)
|
|
e = len(s)
|
|
if e == 0:
|
|
ne0 += 1
|
|
elif e == 1:
|
|
ne1 += 1
|
|
return ne0 == 0 and ne1 < 2
|
|
|
|
# Put i0 back in each of its successor's successor lists.
|
|
|
|
def add_to_successors(i0):
|
|
for i in succs[i0]:
|
|
succs[i].append(i0)
|
|
|
|
# Generate the first move.
|
|
def first():
|
|
if m < 1 or n < 1:
|
|
return
|
|
|
|
# Since we're looking for a cycle, it doesn't matter where we
|
|
# start. Starting in a corner makes the 2nd move easy.
|
|
corner = self.coords2index(0, 0)
|
|
remove_from_successors(corner)
|
|
self.lastij = corner
|
|
yield corner
|
|
add_to_successors(corner)
|
|
|
|
# Generate the second moves.
|
|
def second():
|
|
corner = self.coords2index(0, 0)
|
|
assert self.lastij == corner # i.e., we started in the corner
|
|
if m < 3 or n < 3:
|
|
return
|
|
assert len(succs[corner]) == 2
|
|
assert self.coords2index(1, 2) in succs[corner]
|
|
assert self.coords2index(2, 1) in succs[corner]
|
|
# Only two choices. Whichever we pick, the other must be the
|
|
# square picked on move m*n, as it's the only way to get back
|
|
# to (0, 0). Save its index in self.final so that moves before
|
|
# the last know it must be kept free.
|
|
for i, j in (1, 2), (2, 1):
|
|
this = self.coords2index(i, j)
|
|
final = self.coords2index(3-i, 3-j)
|
|
self.final = final
|
|
|
|
remove_from_successors(this)
|
|
succs[final].append(corner)
|
|
self.lastij = this
|
|
yield this
|
|
succs[final].remove(corner)
|
|
add_to_successors(this)
|
|
|
|
# Generate moves 3 through m*n-1.
|
|
def advance(len=len):
|
|
# If some successor has only one exit, must take it.
|
|
# Else favor successors with fewer exits.
|
|
candidates = []
|
|
for i in succs[self.lastij]:
|
|
e = len(succs[i])
|
|
assert e > 0, "else remove_from_successors() pruning flawed"
|
|
if e == 1:
|
|
candidates = [(e, i)]
|
|
break
|
|
candidates.append((e, i))
|
|
else:
|
|
candidates.sort()
|
|
|
|
for e, i in candidates:
|
|
if i != self.final:
|
|
if remove_from_successors(i):
|
|
self.lastij = i
|
|
yield i
|
|
add_to_successors(i)
|
|
|
|
# Generate moves 3 through m*n-1. Alternative version using a
|
|
# stronger (but more expensive) heuristic to order successors.
|
|
# Since the # of backtracking levels is m*n, a poor move early on
|
|
# can take eons to undo. Smallest square board for which this
|
|
# matters a lot is 52x52.
|
|
def advance_hard(vmid=(m-1)/2.0, hmid=(n-1)/2.0, len=len):
|
|
# If some successor has only one exit, must take it.
|
|
# Else favor successors with fewer exits.
|
|
# Break ties via max distance from board centerpoint (favor
|
|
# corners and edges whenever possible).
|
|
candidates = []
|
|
for i in succs[self.lastij]:
|
|
e = len(succs[i])
|
|
assert e > 0, "else remove_from_successors() pruning flawed"
|
|
if e == 1:
|
|
candidates = [(e, 0, i)]
|
|
break
|
|
i1, j1 = self.index2coords(i)
|
|
d = (i1 - vmid)**2 + (j1 - hmid)**2
|
|
candidates.append((e, -d, i))
|
|
else:
|
|
candidates.sort()
|
|
|
|
for e, d, i in candidates:
|
|
if i != self.final:
|
|
if remove_from_successors(i):
|
|
self.lastij = i
|
|
yield i
|
|
add_to_successors(i)
|
|
|
|
# Generate the last move.
|
|
def last():
|
|
assert self.final in succs[self.lastij]
|
|
yield self.final
|
|
|
|
if m*n < 4:
|
|
self.squaregenerators = [first]
|
|
else:
|
|
self.squaregenerators = [first, second] + \
|
|
[hard and advance_hard or advance] * (m*n - 3) + \
|
|
[last]
|
|
|
|
def coords2index(self, i, j):
|
|
assert 0 <= i < self.m
|
|
assert 0 <= j < self.n
|
|
return i * self.n + j
|
|
|
|
def index2coords(self, index):
|
|
assert 0 <= index < self.m * self.n
|
|
return divmod(index, self.n)
|
|
|
|
def _init_board(self):
|
|
succs = self.succs
|
|
del succs[:]
|
|
m, n = self.m, self.n
|
|
c2i = self.coords2index
|
|
|
|
offsets = [( 1, 2), ( 2, 1), ( 2, -1), ( 1, -2),
|
|
(-1, -2), (-2, -1), (-2, 1), (-1, 2)]
|
|
rangen = range(n)
|
|
for i in range(m):
|
|
for j in rangen:
|
|
s = [c2i(i+io, j+jo) for io, jo in offsets
|
|
if 0 <= i+io < m and
|
|
0 <= j+jo < n]
|
|
succs.append(s)
|
|
|
|
# Generate solutions.
|
|
def solve(self):
|
|
self._init_board()
|
|
for x in conjoin(self.squaregenerators):
|
|
yield x
|
|
|
|
def printsolution(self, x):
|
|
m, n = self.m, self.n
|
|
assert len(x) == m*n
|
|
w = len(str(m*n))
|
|
format = "%" + str(w) + "d"
|
|
|
|
squares = [[None] * n for i in range(m)]
|
|
k = 1
|
|
for i in x:
|
|
i1, j1 = self.index2coords(i)
|
|
squares[i1][j1] = format % k
|
|
k += 1
|
|
|
|
sep = "+" + ("-" * w + "+") * n
|
|
print(sep)
|
|
for i in range(m):
|
|
row = squares[i]
|
|
print("|" + "|".join(row) + "|")
|
|
print(sep)
|
|
|
|
conjoin_tests = """
|
|
|
|
Generate the 3-bit binary numbers in order. This illustrates dumbest-
|
|
possible use of conjoin, just to generate the full cross-product.
|
|
|
|
>>> for c in conjoin([lambda: iter((0, 1))] * 3):
|
|
... print(c)
|
|
[0, 0, 0]
|
|
[0, 0, 1]
|
|
[0, 1, 0]
|
|
[0, 1, 1]
|
|
[1, 0, 0]
|
|
[1, 0, 1]
|
|
[1, 1, 0]
|
|
[1, 1, 1]
|
|
|
|
For efficiency in typical backtracking apps, conjoin() yields the same list
|
|
object each time. So if you want to save away a full account of its
|
|
generated sequence, you need to copy its results.
|
|
|
|
>>> def gencopy(iterator):
|
|
... for x in iterator:
|
|
... yield x[:]
|
|
|
|
>>> for n in range(10):
|
|
... all = list(gencopy(conjoin([lambda: iter((0, 1))] * n)))
|
|
... print(n, len(all), all[0] == [0] * n, all[-1] == [1] * n)
|
|
0 1 True True
|
|
1 2 True True
|
|
2 4 True True
|
|
3 8 True True
|
|
4 16 True True
|
|
5 32 True True
|
|
6 64 True True
|
|
7 128 True True
|
|
8 256 True True
|
|
9 512 True True
|
|
|
|
And run an 8-queens solver.
|
|
|
|
>>> q = Queens(8)
|
|
>>> LIMIT = 2
|
|
>>> count = 0
|
|
>>> for row2col in q.solve():
|
|
... count += 1
|
|
... if count <= LIMIT:
|
|
... print("Solution", count)
|
|
... q.printsolution(row2col)
|
|
Solution 1
|
|
+-+-+-+-+-+-+-+-+
|
|
|Q| | | | | | | |
|
|
+-+-+-+-+-+-+-+-+
|
|
| | | | |Q| | | |
|
|
+-+-+-+-+-+-+-+-+
|
|
| | | | | | | |Q|
|
|
+-+-+-+-+-+-+-+-+
|
|
| | | | | |Q| | |
|
|
+-+-+-+-+-+-+-+-+
|
|
| | |Q| | | | | |
|
|
+-+-+-+-+-+-+-+-+
|
|
| | | | | | |Q| |
|
|
+-+-+-+-+-+-+-+-+
|
|
| |Q| | | | | | |
|
|
+-+-+-+-+-+-+-+-+
|
|
| | | |Q| | | | |
|
|
+-+-+-+-+-+-+-+-+
|
|
Solution 2
|
|
+-+-+-+-+-+-+-+-+
|
|
|Q| | | | | | | |
|
|
+-+-+-+-+-+-+-+-+
|
|
| | | | | |Q| | |
|
|
+-+-+-+-+-+-+-+-+
|
|
| | | | | | | |Q|
|
|
+-+-+-+-+-+-+-+-+
|
|
| | |Q| | | | | |
|
|
+-+-+-+-+-+-+-+-+
|
|
| | | | | | |Q| |
|
|
+-+-+-+-+-+-+-+-+
|
|
| | | |Q| | | | |
|
|
+-+-+-+-+-+-+-+-+
|
|
| |Q| | | | | | |
|
|
+-+-+-+-+-+-+-+-+
|
|
| | | | |Q| | | |
|
|
+-+-+-+-+-+-+-+-+
|
|
|
|
>>> print(count, "solutions in all.")
|
|
92 solutions in all.
|
|
|
|
And run a Knight's Tour on a 10x10 board. Note that there are about
|
|
20,000 solutions even on a 6x6 board, so don't dare run this to exhaustion.
|
|
|
|
>>> k = Knights(10, 10)
|
|
>>> LIMIT = 2
|
|
>>> count = 0
|
|
>>> for x in k.solve():
|
|
... count += 1
|
|
... if count <= LIMIT:
|
|
... print("Solution", count)
|
|
... k.printsolution(x)
|
|
... else:
|
|
... break
|
|
Solution 1
|
|
+---+---+---+---+---+---+---+---+---+---+
|
|
| 1| 58| 27| 34| 3| 40| 29| 10| 5| 8|
|
|
+---+---+---+---+---+---+---+---+---+---+
|
|
| 26| 35| 2| 57| 28| 33| 4| 7| 30| 11|
|
|
+---+---+---+---+---+---+---+---+---+---+
|
|
| 59|100| 73| 36| 41| 56| 39| 32| 9| 6|
|
|
+---+---+---+---+---+---+---+---+---+---+
|
|
| 74| 25| 60| 55| 72| 37| 42| 49| 12| 31|
|
|
+---+---+---+---+---+---+---+---+---+---+
|
|
| 61| 86| 99| 76| 63| 52| 47| 38| 43| 50|
|
|
+---+---+---+---+---+---+---+---+---+---+
|
|
| 24| 75| 62| 85| 54| 71| 64| 51| 48| 13|
|
|
+---+---+---+---+---+---+---+---+---+---+
|
|
| 87| 98| 91| 80| 77| 84| 53| 46| 65| 44|
|
|
+---+---+---+---+---+---+---+---+---+---+
|
|
| 90| 23| 88| 95| 70| 79| 68| 83| 14| 17|
|
|
+---+---+---+---+---+---+---+---+---+---+
|
|
| 97| 92| 21| 78| 81| 94| 19| 16| 45| 66|
|
|
+---+---+---+---+---+---+---+---+---+---+
|
|
| 22| 89| 96| 93| 20| 69| 82| 67| 18| 15|
|
|
+---+---+---+---+---+---+---+---+---+---+
|
|
Solution 2
|
|
+---+---+---+---+---+---+---+---+---+---+
|
|
| 1| 58| 27| 34| 3| 40| 29| 10| 5| 8|
|
|
+---+---+---+---+---+---+---+---+---+---+
|
|
| 26| 35| 2| 57| 28| 33| 4| 7| 30| 11|
|
|
+---+---+---+---+---+---+---+---+---+---+
|
|
| 59|100| 73| 36| 41| 56| 39| 32| 9| 6|
|
|
+---+---+---+---+---+---+---+---+---+---+
|
|
| 74| 25| 60| 55| 72| 37| 42| 49| 12| 31|
|
|
+---+---+---+---+---+---+---+---+---+---+
|
|
| 61| 86| 99| 76| 63| 52| 47| 38| 43| 50|
|
|
+---+---+---+---+---+---+---+---+---+---+
|
|
| 24| 75| 62| 85| 54| 71| 64| 51| 48| 13|
|
|
+---+---+---+---+---+---+---+---+---+---+
|
|
| 87| 98| 89| 80| 77| 84| 53| 46| 65| 44|
|
|
+---+---+---+---+---+---+---+---+---+---+
|
|
| 90| 23| 92| 95| 70| 79| 68| 83| 14| 17|
|
|
+---+---+---+---+---+---+---+---+---+---+
|
|
| 97| 88| 21| 78| 81| 94| 19| 16| 45| 66|
|
|
+---+---+---+---+---+---+---+---+---+---+
|
|
| 22| 91| 96| 93| 20| 69| 82| 67| 18| 15|
|
|
+---+---+---+---+---+---+---+---+---+---+
|
|
"""
|
|
|
|
weakref_tests = """\
|
|
Generators are weakly referencable:
|
|
|
|
>>> import weakref
|
|
>>> def gen():
|
|
... yield 'foo!'
|
|
...
|
|
>>> wr = weakref.ref(gen)
|
|
>>> wr() is gen
|
|
True
|
|
>>> p = weakref.proxy(gen)
|
|
|
|
Generator-iterators are weakly referencable as well:
|
|
|
|
>>> gi = gen()
|
|
>>> wr = weakref.ref(gi)
|
|
>>> wr() is gi
|
|
True
|
|
>>> p = weakref.proxy(gi)
|
|
>>> list(p)
|
|
['foo!']
|
|
|
|
"""
|
|
|
|
coroutine_tests = """\
|
|
>>> from test.support import gc_collect
|
|
|
|
Sending a value into a started generator:
|
|
|
|
>>> def f():
|
|
... print((yield 1))
|
|
... yield 2
|
|
>>> g = f()
|
|
>>> next(g)
|
|
1
|
|
>>> g.send(42)
|
|
42
|
|
2
|
|
|
|
Sending a value into a new generator produces a TypeError:
|
|
|
|
>>> f().send("foo")
|
|
Traceback (most recent call last):
|
|
...
|
|
TypeError: can't send non-None value to a just-started generator
|
|
|
|
|
|
Yield by itself yields None:
|
|
|
|
>>> def f(): yield
|
|
>>> list(f())
|
|
[None]
|
|
|
|
|
|
Yield is allowed only in the outermost iterable in generator expression:
|
|
|
|
>>> def f(): list(i for i in [(yield 26)])
|
|
>>> type(f())
|
|
<class 'generator'>
|
|
|
|
|
|
A yield expression with augmented assignment.
|
|
|
|
>>> def coroutine(seq):
|
|
... count = 0
|
|
... while count < 200:
|
|
... count += yield
|
|
... seq.append(count)
|
|
>>> seq = []
|
|
>>> c = coroutine(seq)
|
|
>>> next(c)
|
|
>>> print(seq)
|
|
[]
|
|
>>> c.send(10)
|
|
>>> print(seq)
|
|
[10]
|
|
>>> c.send(10)
|
|
>>> print(seq)
|
|
[10, 20]
|
|
>>> c.send(10)
|
|
>>> print(seq)
|
|
[10, 20, 30]
|
|
|
|
|
|
Check some syntax errors for yield expressions:
|
|
|
|
>>> f=lambda: (yield 1),(yield 2)
|
|
Traceback (most recent call last):
|
|
...
|
|
SyntaxError: 'yield' outside function
|
|
|
|
>>> f=lambda: (yield from (1,2)), (yield from (3,4))
|
|
Traceback (most recent call last):
|
|
...
|
|
SyntaxError: 'yield from' outside function
|
|
|
|
>>> yield from [1,2]
|
|
Traceback (most recent call last):
|
|
...
|
|
SyntaxError: 'yield from' outside function
|
|
|
|
>>> def f(): x = yield = y
|
|
Traceback (most recent call last):
|
|
...
|
|
SyntaxError: assignment to yield expression not possible
|
|
|
|
>>> def f(): (yield bar) = y # TODO: RUSTPYTHON # doctest: +EXPECTED_FAILURE
|
|
Traceback (most recent call last):
|
|
...
|
|
SyntaxError: cannot assign to yield expression here. Maybe you meant '==' instead of '='?
|
|
|
|
>>> def f(): (yield bar) += y # TODO: RUSTPYTHON # doctest: +EXPECTED_FAILURE
|
|
Traceback (most recent call last):
|
|
...
|
|
SyntaxError: 'yield expression' is an illegal expression for augmented assignment
|
|
|
|
|
|
Now check some throw() conditions:
|
|
|
|
>>> def f():
|
|
... while True:
|
|
... try:
|
|
... print((yield))
|
|
... except ValueError as v:
|
|
... print("caught ValueError (%s)" % (v))
|
|
>>> import sys
|
|
>>> g = f()
|
|
>>> next(g)
|
|
|
|
>>> g.throw(ValueError) # type only
|
|
caught ValueError ()
|
|
|
|
>>> g.throw(ValueError("xyz")) # value only
|
|
caught ValueError (xyz)
|
|
|
|
>>> import warnings
|
|
>>> old_filters = warnings.filters.copy()
|
|
>>> warnings.filterwarnings("ignore", category=DeprecationWarning)
|
|
|
|
# Filter DeprecationWarning: regarding the (type, val, tb) signature of throw().
|
|
# Deprecation warnings are re-enabled below.
|
|
|
|
>>> g.throw(ValueError, ValueError(1)) # value+matching type
|
|
caught ValueError (1)
|
|
|
|
>>> g.throw(ValueError, TypeError(1)) # mismatched type, rewrapped
|
|
caught ValueError (1)
|
|
|
|
>>> g.throw(ValueError, ValueError(1), None) # explicit None traceback
|
|
caught ValueError (1)
|
|
|
|
>>> g.throw(ValueError(1), "foo") # bad args
|
|
Traceback (most recent call last):
|
|
...
|
|
TypeError: instance exception may not have a separate value
|
|
|
|
>>> g.throw(ValueError, "foo", 23) # bad args
|
|
Traceback (most recent call last):
|
|
...
|
|
TypeError: throw() third argument must be a traceback object
|
|
|
|
>>> g.throw("abc")
|
|
Traceback (most recent call last):
|
|
...
|
|
TypeError: exceptions must be classes or instances deriving from BaseException, not str
|
|
|
|
>>> g.throw(0)
|
|
Traceback (most recent call last):
|
|
...
|
|
TypeError: exceptions must be classes or instances deriving from BaseException, not int
|
|
|
|
>>> g.throw(list)
|
|
Traceback (most recent call last):
|
|
...
|
|
TypeError: exceptions must be classes or instances deriving from BaseException, not type
|
|
|
|
>>> def throw(g,exc):
|
|
... try:
|
|
... raise exc
|
|
... except:
|
|
... g.throw(*sys.exc_info())
|
|
>>> throw(g,ValueError) # do it with traceback included
|
|
caught ValueError ()
|
|
|
|
>>> g.send(1)
|
|
1
|
|
|
|
>>> throw(g,TypeError) # terminate the generator
|
|
Traceback (most recent call last):
|
|
...
|
|
TypeError
|
|
|
|
>>> print(g.gi_frame)
|
|
None
|
|
|
|
>>> g.send(2)
|
|
Traceback (most recent call last):
|
|
...
|
|
StopIteration
|
|
|
|
>>> g.throw(ValueError,6) # throw on closed generator
|
|
Traceback (most recent call last):
|
|
...
|
|
ValueError: 6
|
|
|
|
>>> f().throw(ValueError,7) # throw on just-opened generator
|
|
Traceback (most recent call last):
|
|
...
|
|
ValueError: 7
|
|
|
|
>>> warnings.filters[:] = old_filters
|
|
|
|
# Re-enable DeprecationWarning: the (type, val, tb) exception representation is deprecated,
|
|
# and may be removed in a future version of Python.
|
|
|
|
Plain "raise" inside a generator should preserve the traceback (#13188).
|
|
The traceback should have 3 levels:
|
|
- g.throw()
|
|
- f()
|
|
- 1/0
|
|
|
|
>>> def f():
|
|
... try:
|
|
... yield
|
|
... except:
|
|
... raise
|
|
>>> g = f()
|
|
>>> try:
|
|
... 1/0
|
|
... except ZeroDivisionError as v:
|
|
... try:
|
|
... g.throw(v)
|
|
... except Exception as w:
|
|
... tb = w.__traceback__
|
|
>>> levels = 0
|
|
>>> while tb:
|
|
... levels += 1
|
|
... tb = tb.tb_next
|
|
>>> levels
|
|
3
|
|
|
|
Now let's try closing a generator:
|
|
|
|
>>> def f():
|
|
... try: yield
|
|
... except GeneratorExit:
|
|
... print("exiting")
|
|
|
|
>>> g = f()
|
|
>>> next(g)
|
|
>>> g.close()
|
|
exiting
|
|
>>> g.close() # should be no-op now
|
|
|
|
>>> f().close() # close on just-opened generator should be fine
|
|
|
|
>>> def f(): yield # an even simpler generator
|
|
>>> f().close() # close before opening
|
|
>>> g = f()
|
|
>>> next(g)
|
|
>>> g.close() # close normally
|
|
|
|
And finalization:
|
|
|
|
>>> def f():
|
|
... try: yield
|
|
... finally:
|
|
... print("exiting")
|
|
|
|
>>> g = f()
|
|
>>> next(g)
|
|
>>> del g; gc_collect() # For PyPy or other GCs.
|
|
exiting
|
|
|
|
|
|
GeneratorExit is not caught by except Exception:
|
|
|
|
>>> def f():
|
|
... try: yield
|
|
... except Exception:
|
|
... print('except')
|
|
... finally:
|
|
... print('finally')
|
|
|
|
>>> g = f()
|
|
>>> next(g)
|
|
>>> del g; gc_collect() # For PyPy or other GCs.
|
|
finally
|
|
|
|
|
|
Now let's try some ill-behaved generators:
|
|
|
|
>>> def f():
|
|
... try: yield
|
|
... except GeneratorExit:
|
|
... yield "foo!"
|
|
>>> g = f()
|
|
>>> next(g)
|
|
>>> g.close()
|
|
Traceback (most recent call last):
|
|
...
|
|
RuntimeError: generator ignored GeneratorExit
|
|
>>> g.close()
|
|
|
|
|
|
Our ill-behaved code should be invoked during GC:
|
|
|
|
>>> with support.catch_unraisable_exception() as cm: # TODO: RUSTPYTHON # doctest: +EXPECTED_FAILURE
|
|
... g = f()
|
|
... next(g)
|
|
... gen_repr = repr(g)
|
|
... del g
|
|
...
|
|
... cm.unraisable.err_msg == (f'Exception ignored while closing '
|
|
... f'generator {gen_repr}')
|
|
... cm.unraisable.exc_type == RuntimeError
|
|
... "generator ignored GeneratorExit" in str(cm.unraisable.exc_value)
|
|
... cm.unraisable.exc_traceback is not None
|
|
True
|
|
True
|
|
True
|
|
True
|
|
|
|
And errors thrown during closing should propagate:
|
|
|
|
>>> def f():
|
|
... try: yield
|
|
... except GeneratorExit:
|
|
... raise TypeError("fie!")
|
|
>>> g = f()
|
|
>>> next(g)
|
|
>>> g.close()
|
|
Traceback (most recent call last):
|
|
...
|
|
TypeError: fie!
|
|
|
|
|
|
Ensure that various yield expression constructs make their
|
|
enclosing function a generator:
|
|
|
|
>>> def f(): x += yield
|
|
>>> type(f())
|
|
<class 'generator'>
|
|
|
|
>>> def f(): x = yield
|
|
>>> type(f())
|
|
<class 'generator'>
|
|
|
|
>>> def f(): lambda x=(yield): 1
|
|
>>> type(f())
|
|
<class 'generator'>
|
|
|
|
>>> def f(d): d[(yield "a")] = d[(yield "b")] = 27
|
|
>>> data = [1,2]
|
|
>>> g = f(data)
|
|
>>> type(g)
|
|
<class 'generator'>
|
|
>>> g.send(None)
|
|
'a'
|
|
>>> data
|
|
[1, 2]
|
|
>>> g.send(0)
|
|
'b'
|
|
>>> data
|
|
[27, 2]
|
|
>>> try: g.send(1)
|
|
... except StopIteration: pass
|
|
>>> data
|
|
[27, 27]
|
|
|
|
"""
|
|
|
|
refleaks_tests = """
|
|
Prior to adding cycle-GC support to itertools.tee, this code would leak
|
|
references. We add it to the standard suite so the routine refleak-tests
|
|
would trigger if it starts being uncleanable again.
|
|
|
|
>>> import itertools
|
|
>>> def leak():
|
|
... class gen:
|
|
... def __iter__(self):
|
|
... return self
|
|
... def __next__(self):
|
|
... return self.item
|
|
... g = gen()
|
|
... head, tail = itertools.tee(g)
|
|
... g.item = head
|
|
... return head
|
|
>>> it = leak()
|
|
|
|
Make sure to also test the involvement of the tee-internal teedataobject,
|
|
which stores returned items.
|
|
|
|
>>> item = next(it)
|
|
|
|
|
|
|
|
This test leaked at one point due to generator finalization/destruction.
|
|
It was copied from Lib/test/leakers/test_generator_cycle.py before the file
|
|
was removed.
|
|
|
|
>>> def leak():
|
|
... def gen():
|
|
... while True:
|
|
... yield g
|
|
... g = gen()
|
|
|
|
>>> leak()
|
|
|
|
|
|
|
|
This test isn't really generator related, but rather exception-in-cleanup
|
|
related. The coroutine tests (above) just happen to cause an exception in
|
|
the generator's __del__ (tp_del) method. We can also test for this
|
|
explicitly, without generators. We do have to redirect stderr to avoid
|
|
printing warnings and to doublecheck that we actually tested what we wanted
|
|
to test.
|
|
|
|
>>> from test import support
|
|
>>> class Leaker:
|
|
... def __del__(self):
|
|
... def invoke(message):
|
|
... raise RuntimeError(message)
|
|
... invoke("del failed")
|
|
...
|
|
>>> with support.catch_unraisable_exception() as cm: # TODO: RUSTPYTHON # doctest: +EXPECTED_FAILURE
|
|
... leaker = Leaker()
|
|
... del_repr = repr(type(leaker).__del__)
|
|
... del leaker
|
|
...
|
|
... cm.unraisable.err_msg == (f'Exception ignored while '
|
|
... f'calling deallocator {del_repr}')
|
|
... cm.unraisable.exc_type == RuntimeError
|
|
... str(cm.unraisable.exc_value) == "del failed"
|
|
... cm.unraisable.exc_traceback is not None
|
|
True
|
|
True
|
|
True
|
|
True
|
|
|
|
|
|
These refleak tests should perhaps be in a testfile of their own,
|
|
test_generators just happened to be the test that drew these out.
|
|
|
|
"""
|
|
|
|
__test__ = {"tut": tutorial_tests,
|
|
"pep": pep_tests,
|
|
"email": email_tests,
|
|
"fun": fun_tests,
|
|
"syntax": syntax_tests,
|
|
"conjoin": conjoin_tests,
|
|
"weakref": weakref_tests,
|
|
"coroutine": coroutine_tests,
|
|
"refleaks": refleaks_tests,
|
|
}
|
|
|
|
def load_tests(loader, tests, pattern):
|
|
from test.support.rustpython import DocTestChecker # TODO: RUSTPYTHON
|
|
tests.addTest(doctest.DocTestSuite(checker=DocTestChecker())) # TODO: RUSTPYTHON
|
|
return tests
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|