@@ -330,10 +330,10 @@ def title(text):
330330
331331_site_gethistoryfile = site .gethistoryfile
332332def _gethistoryfile ():
333- """Patch site.gethistoryfile() to ignore -I for PYTHON_HISTORY.
333+ """Patch site.gethistoryfile() to ignore -I/-E for PYTHON_HISTORY.
334334
335- The -I option is necessary for test_no_memory() but using it
336- forbids using a custom PYTHON_HISTORY.
335+ Some tests (e.g, test_repl. test_no_memory) require -I/-E,
336+ but those options forbid using a custom PYTHON_HISTORY.
337337 """
338338 history = os .environ .get ("PYTHON_HISTORY" )
339339 return history or os .path .join (os .path .expanduser ('~' ), '.python_history' )
@@ -351,16 +351,22 @@ def _file_signature(file):
351351class EnsureSafeUserHistory (unittest .TestCase ):
352352
353353 @classmethod
354- def setUpClass (cls ):
355- super (). setUpClass ()
356- cls . __history_file = cls . __history_stat = None
357- history_file = _site_gethistoryfile ()
354+ def __history_setup_check (cls ):
355+ # Ensure that the system-wide history file is not altered by tests.
356+ history_file = os . path . join ( os . path . expanduser ( '~' ), '.python_history' )
357+ cls . __history_file = history_file
358358 if os .path .exists (history_file ):
359- cls .__history_file = history_file
360359 cls .__history_stat = _file_signature (history_file )
360+ else :
361+ cls .__history_stat = None
361362
362- def tearDown (self ):
363- if self .__history_file is not None :
363+ def __history_teardown_check (self ):
364+ if self .__history_stat is None :
365+ self .assertFalse (
366+ os .path .exists (self .__history_file ),
367+ f"PYTHON_HISTORY file ({ self .__history_file !r} ) was created"
368+ )
369+ else :
364370 self .assertTrue (
365371 os .path .exists (self .__history_file ),
366372 f"PYTHON_HISTORY file ({ self .__history_file !r} ) was deleted"
@@ -370,4 +376,12 @@ def tearDown(self):
370376 _file_signature (self .__history_file ),
371377 f"PYTHON_HISTORY file ({ self .__history_file !r} ) was altered"
372378 )
379+
380+ @classmethod
381+ def setUpClass (cls ):
382+ cls .__history_setup_check ()
383+ super ().setUpClass ()
384+
385+ def tearDown (self ):
373386 super ().tearDown ()
387+ self .__history_teardown_check ()
0 commit comments