@@ -330,10 +330,10 @@ def title(text):
330
330
331
331
_site_gethistoryfile = site .gethistoryfile
332
332
def _gethistoryfile ():
333
- """Patch site.gethistoryfile() to ignore -I for PYTHON_HISTORY.
333
+ """Patch site.gethistoryfile() to ignore -I/-E for PYTHON_HISTORY.
334
334
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.
337
337
"""
338
338
history = os .environ .get ("PYTHON_HISTORY" )
339
339
return history or os .path .join (os .path .expanduser ('~' ), '.python_history' )
@@ -351,16 +351,22 @@ def _file_signature(file):
351
351
class EnsureSafeUserHistory (unittest .TestCase ):
352
352
353
353
@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
358
358
if os .path .exists (history_file ):
359
- cls .__history_file = history_file
360
359
cls .__history_stat = _file_signature (history_file )
360
+ else :
361
+ cls .__history_stat = None
361
362
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 :
364
370
self .assertTrue (
365
371
os .path .exists (self .__history_file ),
366
372
f"PYTHON_HISTORY file ({ self .__history_file !r} ) was deleted"
@@ -370,4 +376,12 @@ def tearDown(self):
370
376
_file_signature (self .__history_file ),
371
377
f"PYTHON_HISTORY file ({ self .__history_file !r} ) was altered"
372
378
)
379
+
380
+ @classmethod
381
+ def setUpClass (cls ):
382
+ cls .__history_setup_check ()
383
+ super ().setUpClass ()
384
+
385
+ def tearDown (self ):
373
386
super ().tearDown ()
387
+ self .__history_teardown_check ()
0 commit comments