@@ -516,33 +516,36 @@ def addsitedir(sitedir, known_paths=None, *, startup_state=None):
516516 if known_paths is not None and startup_state is not None :
517517 raise TypeError ("known_paths and startup_state are mutually exclusive" )
518518
519- # Select the processing mode. known_paths is the deduplication ledger,
520- # reset controls the historical return value, flush_now says whether this
521- # call processes startup data eagerly, and process_known_sitedirs controls
522- # whether site directories already present in known_paths still have their
523- # startup files read.
519+ # Select the processing mode. Each arm sets:
520+ # - startup_state: the StartupState that holds the deduplication ledger
521+ # and accumulates this call's .pth and .start data
522+ # - flush_now: whether this call applies the accumulated state before
523+ # returning
524+ # - process_known_sitedirs: whether site directories already present in
525+ # the ledger should still have their startup files read
526+ # - return_value: what this call returns, merging the historical
527+ # addsitedir() behavior (None, the caller's known_paths set), with the new
528+ # StartupState instance behavior.
524529 if startup_state is not None :
525530 # Explicit batch mode: accumulate startup data in the caller's state.
526531 # The caller is responsible for calling startup_state.process().
527- known_paths = startup_state ._known_paths
528- reset = False
529532 flush_now = False
530533 process_known_sitedirs = True
534+ return_value = startup_state
531535 elif known_paths is None :
532536 # Standalone mode: derive known paths from current sys.path, process
533537 # eagerly, and preserve the historical return value of None.
534- known_paths = _init_pathinfo ()
535- reset = True
536- startup_state = StartupState (known_paths )
538+ startup_state = StartupState (_init_pathinfo ())
537539 flush_now = True
538540 process_known_sitedirs = False
541+ return_value = None
539542 else :
540543 # Legacy known_paths mode: process eagerly and return the caller's
541- # updated known_paths set.
542- reset = False
544+ # known_paths set, mutated in place by the StartupState.
543545 startup_state = StartupState (known_paths )
544546 flush_now = True
545547 process_known_sitedirs = False
548+ return_value = known_paths
546549
547550 # Reach into StartupState's non-public API deliberately: sitedir
548551 # bookkeeping is a detail of how addsitedir() drives a batch, not
@@ -553,17 +556,14 @@ def addsitedir(sitedir, known_paths=None, *, startup_state=None):
553556 process_known_sitedirs = process_known_sitedirs ,
554557 )
555558 if sitedir is None :
556- if flush_now :
557- return None if reset else known_paths
558- return startup_state
559+ return return_value
559560
560561 try :
561562 names = os .listdir (sitedir )
562563 except OSError :
563564 if flush_now :
564565 startup_state .process ()
565- return None if reset else known_paths
566- return startup_state
566+ return return_value
567567
568568 # The following phases are defined by PEP 829.
569569 # Phases 1-3: Read .pth files, accumulating paths and import lines.
@@ -586,8 +586,7 @@ def addsitedir(sitedir, known_paths=None, *, startup_state=None):
586586
587587 if flush_now :
588588 startup_state .process ()
589- return None if reset else known_paths
590- return startup_state
589+ return return_value
591590
592591
593592def check_enableusersite ():
0 commit comments