@@ -69,6 +69,8 @@ static PyObject *run_pyc_file Py_PROTO((FILE *fp, char *filename,
6969static void err_input Py_PROTO ((perrdetail * ) );
7070static void initsigs Py_PROTO ((void ));
7171static void finisigs Py_PROTO ((void ));
72+ static void call_sys_exitfunc Py_PROTO ((void ));
73+ static void call_ll_exitfuncs Py_PROTO ((void ));
7274
7375int Py_DebugFlag ; /* Needed by parser.c */
7476int Py_VerboseFlag ; /* Needed by import.c */
@@ -162,6 +164,8 @@ Py_Finalize()
162164 PyInterpreterState * interp ;
163165 PyThreadState * tstate ;
164166
167+ call_sys_exitfunc ();
168+
165169 if (!initialized )
166170 Py_FatalError ("Py_Finalize: not initialized" );
167171 initialized = 0 ;
@@ -177,9 +181,38 @@ Py_Finalize()
177181 finisigs ();
178182 _PyImport_Fini ();
179183 _PyBuiltin_Fini ();
184+ PyMethod_Fini ();
185+ PyFrame_Fini ();
186+ PyCFunction_Fini ();
187+ PyTuple_Fini ();
180188 PyString_Fini ();
189+ PyInt_Fini ();
190+ PyFloat_Fini ();
191+
192+ /* XXX Still allocated:
193+ - various static ad-hoc pointers to interned strings
194+ - int and float free list blocks
195+ - whatever various modules and libraries allocate
196+ */
181197
182198 PyGrammar_RemoveAccelerators (& _PyParser_Grammar );
199+
200+ call_ll_exitfuncs ();
201+
202+ #ifdef COUNT_ALLOCS
203+ dump_counts ();
204+ #endif
205+
206+ #ifdef Py_REF_DEBUG
207+ fprintf (stderr , "[%ld refs]\n" , _Py_RefTotal );
208+ #endif
209+
210+ #ifdef Py_TRACE_REFS
211+ if (_Py_AskYesNo ("Print left references?" )) {
212+ _Py_PrintReferences (stderr );
213+ }
214+ _Py_ResetReferences ();
215+ #endif /* Py_TRACE_REFS */
183216}
184217
185218/* Create and initialize a new interpreter and thread, and return the
@@ -297,22 +330,6 @@ Py_GetProgramName()
297330 return progname ;
298331}
299332
300- /*
301- Py_Initialize()
302- -- do everything, no-op on second call, call fatal on failure, set path
303-
304- #2
305- -- create new interp+tstate & make it current, return NULL on failure,
306- make it current, do all setup, set path
307-
308- #3
309- -- #2 without set path
310-
311- #4
312- -- is there any point to #3 for caller-provided current interp+tstate?
313-
314- */
315-
316333/* Create __main__ module */
317334
318335static void
@@ -831,8 +848,8 @@ int Py_AtExit(func)
831848 return 0 ;
832849}
833850
834- void
835- Py_Cleanup ()
851+ static void
852+ call_sys_exitfunc ()
836853{
837854 PyObject * exitfunc = PySys_GetObject ("exitfunc" );
838855
@@ -849,9 +866,11 @@ Py_Cleanup()
849866 }
850867
851868 Py_FlushLine ();
869+ }
852870
853- Py_Finalize ();
854-
871+ static void
872+ call_ll_exitfuncs ()
873+ {
855874 while (nexitfuncs > 0 )
856875 (* exitfuncs [-- nexitfuncs ])();
857876
@@ -867,21 +886,7 @@ void
867886Py_Exit (sts )
868887 int sts ;
869888{
870- Py_Cleanup ();
871-
872- #ifdef COUNT_ALLOCS
873- dump_counts ();
874- #endif
875-
876- #ifdef Py_REF_DEBUG
877- fprintf (stderr , "[%ld refs]\n" , _Py_RefTotal );
878- #endif
879-
880- #ifdef Py_TRACE_REFS
881- if (_Py_AskYesNo ("Print left references?" )) {
882- _Py_PrintReferences (stderr );
883- }
884- #endif /* Py_TRACE_REFS */
889+ Py_Finalize ();
885890
886891#ifdef macintosh
887892 PyMac_Exit (sts );
@@ -896,7 +901,9 @@ sighandler(sig)
896901 int sig ;
897902{
898903 signal (sig , SIG_DFL ); /* Don't catch recursive signals */
899- Py_Cleanup (); /* Do essential clean-up */
904+ /* Do essential exit processing only */
905+ call_sys_exitfunc ();
906+ call_ll_exitfuncs ();
900907#ifdef HAVE_KILL
901908 kill (getpid (), sig ); /* Pretend the signal killed us */
902909#else
0 commit comments