From 346cbd351ee0dd3ab9cb9f0e4cb625556707877e Mon Sep 17 00:00:00 2001 From: Antoine Pitrou Date: Sat, 27 May 2017 17:50:54 +0200 Subject: bpo-16500: Allow registering at-fork handlers (#1715) * bpo-16500: Allow registering at-fork handlers * Address Serhiy's comments * Add doc for new C API * Add doc for new Python-facing function * Add NEWS entry + doc nit --- Lib/test/test_random.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'Lib/test/test_random.py') diff --git a/Lib/test/test_random.py b/Lib/test/test_random.py index 45468c7ce4..f657b46b3a 100644 --- a/Lib/test/test_random.py +++ b/Lib/test/test_random.py @@ -1,6 +1,7 @@ import unittest import unittest.mock import random +import os import time import pickle import warnings @@ -902,6 +903,24 @@ class TestModule(unittest.TestCase): random.Random.__init__(self) Subclass(newarg=1) + @unittest.skipUnless(hasattr(os, "fork"), "fork() required") + def test_after_fork(self): + # Test the global Random instance gets reseeded in child + r, w = os.pipe() + if os.fork() == 0: + try: + val = random.getrandbits(128) + with open(w, "w") as f: + f.write(str(val)) + finally: + os._exit(0) + else: + os.close(w) + val = random.getrandbits(128) + with open(r, "r") as f: + child_val = eval(f.read()) + self.assertNotEqual(val, child_val) + if __name__ == "__main__": unittest.main() -- cgit v1.2.1