@@ -898,20 +898,19 @@ def test_uuid5(self):
898898 equal (str (u ), v )
899899
900900 @contextlib .contextmanager
901- def _uuid6_hooks (self , nanoseconds , * , random_func = None ):
901+ def _uuid6_hooks (self , nanoseconds , * , random_func = None , node_func = None ):
902902 time_func = nanoseconds if callable (nanoseconds ) else lambda : nanoseconds
903- with support .swap_item (sys .modules , 'uuid' , self .uuid ):
904- self .uuid ._install_py_hooks (random_func = random_func ,
905- time_func = time_func )
903+ self .uuid ._install_py_hooks (random_func = random_func ,
904+ time_func = time_func , node_func = node_func )
905+ if self .is_c_uuid :
906+ self .uuid ._install_c_hooks (random_func = random_func ,
907+ time_func = time_func , node_func = node_func )
908+ try :
909+ yield
910+ finally :
911+ self .uuid ._install_py_hooks (random_func = None , time_func = None )
906912 if self .is_c_uuid :
907- self .uuid ._install_c_hooks (random_func = random_func ,
908- time_func = time_func )
909- try :
910- yield
911- finally :
912- self .uuid ._install_py_hooks (random_func = None , time_func = None )
913- if self .is_c_uuid :
914- self .uuid ._install_c_hooks (random_func = None , time_func = None )
913+ self .uuid ._install_c_hooks (random_func = None , time_func = None )
915914
916915 def test_uuid6 (self ):
917916 equal = self .assertEqual
@@ -928,10 +927,8 @@ def random_func(size):
928927 data = (fake_clock_seq | 0xc000 ).to_bytes (2 , 'big' )
929928 return (data * ((size + 1 ) // 2 ))[:size ]
930929
931- with (
932- self ._uuid6_hooks (fake_nanoseconds , random_func = random_func ),
933- mock .patch .object (self .uuid , 'getnode' , return_value = fake_node_value ),
934- ):
930+ with self ._uuid6_hooks (fake_nanoseconds , random_func = random_func ,
931+ node_func = lambda : fake_node_value ):
935932 u = self .uuid .uuid6 ()
936933 equal (u .variant , self .uuid .RFC_4122 )
937934 equal (u .version , 6 )
@@ -1069,11 +1066,13 @@ def random_func(size):
10691066 with self .assertRaises (RuntimeError ) as cm :
10701067 self .uuid .uuid6 (node = 0 )
10711068 self .assertIs (cm .exception , error )
1072- with self ._uuid6_hooks (0 ):
1073- with mock .patch .object (self .uuid , 'getnode' , side_effect = error ):
1074- with self .assertRaises (RuntimeError ) as cm :
1075- self .uuid .uuid6 (clock_seq = 0 )
1076- self .assertIs (cm .exception , error )
1069+ def node_func ():
1070+ raise error
1071+
1072+ with self ._uuid6_hooks (0 , node_func = node_func ):
1073+ with self .assertRaises (RuntimeError ) as cm :
1074+ self .uuid .uuid6 (clock_seq = 0 )
1075+ self .assertIs (cm .exception , error )
10771076
10781077 def test_uuid6_monotonicity (self ):
10791078 # Repeated and backward clock readings must still advance the timestamp.
@@ -1918,17 +1917,20 @@ def full_test(p, u):
19181917 self .assertEqual (len (all_ps ), len (all_us ))
19191918 self .assertEqual (len (all_ps ), len (uuids ))
19201919
1921- def _install_hooks (self , uuid_mod , * , start_at = 0 , inc_by = 0 , seed = 0 ):
1920+ def _install_hooks (self , uuid_mod , * , start_at = 0 , inc_by = 0 , seed = 0 ,
1921+ node_func = None ):
19221922 py_hooks = UuidHooks (start_at = start_at , inc_by = inc_by , seed = seed )
19231923 uuid_mod ._install_py_hooks (
19241924 random_func = py_hooks .random_func ,
1925- time_func = py_hooks .time_func
1925+ time_func = py_hooks .time_func ,
1926+ node_func = node_func ,
19261927 )
19271928
19281929 c_hooks = UuidHooks (start_at = start_at , inc_by = inc_by , seed = seed )
19291930 uuid_mod ._install_c_hooks (
19301931 random_func = c_hooks .random_func ,
1931- time_func = c_hooks .time_func
1932+ time_func = c_hooks .time_func ,
1933+ node_func = node_func ,
19321934 )
19331935
19341936 def _reset_hooks (self , uuid_mod ):
@@ -1943,19 +1945,19 @@ def test_exact_same_algo_uuid6(self):
19431945
19441946 values = (None , 0 , - 1 , (1 << 100 ) | 0x123456789abc )
19451947 try :
1946- with mock . patch . object ( uuid , 'getnode' , return_value = 0x123456789abc ):
1947- for start_at in (- 1001 , 0 , 1672533296123456789 ):
1948- for inc_by in ( - 101 , 0 , 101 , 1000000 ):
1949- for node , clock_seq in product ( values , repeat = 2 ):
1950- self . _install_hooks ( uuid , start_at = start_at ,
1951- inc_by = inc_by )
1952- with self .subTest (start = start_at , inc = inc_by ,
1953- node = node , clock_seq = clock_seq ):
1954- for _ in range (200 ):
1955- self .assertEqual (
1956- uuid ._py_uuid6 (node , clock_seq ).bytes ,
1957- uuid ._c_uuid6 (node , clock_seq ).bytes ,
1958- )
1948+ for start_at in ( - 1001 , 0 , 1672533296123456789 ):
1949+ for inc_by in (- 101 , 0 , 101 , 1000000 ):
1950+ for node , clock_seq in product ( values , repeat = 2 ):
1951+ self . _install_hooks ( uuid , start_at = start_at ,
1952+ inc_by = inc_by ,
1953+ node_func = lambda : 0x123456789abc )
1954+ with self .subTest (start = start_at , inc = inc_by ,
1955+ node = node , clock_seq = clock_seq ):
1956+ for _ in range (200 ):
1957+ self .assertEqual (
1958+ uuid ._py_uuid6 (node , clock_seq ).bytes ,
1959+ uuid ._c_uuid6 (node , clock_seq ).bytes ,
1960+ )
19591961 finally :
19601962 self ._reset_hooks (uuid )
19611963
@@ -1988,36 +1990,37 @@ def test_uuid6_node_cache(self):
19881990 self .addCleanup (uuid ._install_c_hooks , random_func = None , time_func = None )
19891991 for node in (0 , - 1 ):
19901992 with self .subTest (node = node ):
1991- uuid ._install_c_hooks (random_func = None , time_func = None )
1992- with mock .patch .object (uuid , 'getnode' , return_value = node ) as getnode :
1993- self .assertEqual (uuid .uuid6 (clock_seq = 0 ).node ,
1994- node & 0xffffffffffff )
1995- self .assertEqual (uuid .uuid6 (node = 42 , clock_seq = 0 ).node , 42 )
1996- self .assertEqual (uuid .uuid6 (clock_seq = 0 ).node ,
1997- node & 0xffffffffffff )
1998- getnode .assert_called_once_with ()
1993+ getnode = mock .Mock (return_value = node )
1994+ uuid ._install_c_hooks (random_func = None , time_func = None ,
1995+ node_func = getnode )
1996+ self .assertEqual (uuid .uuid6 (clock_seq = 0 ).node ,
1997+ node & 0xffffffffffff )
1998+ self .assertEqual (uuid .uuid6 (node = 42 , clock_seq = 0 ).node , 42 )
1999+ self .assertEqual (uuid .uuid6 (clock_seq = 0 ).node ,
2000+ node & 0xffffffffffff )
2001+ getnode .assert_called_once_with ()
19992002
20002003 def test_uuid6_node_cache_error (self ):
20012004 # A failed lookup must leave the cache empty so the next call can retry.
20022005 import uuid
20032006
2004- uuid ._install_c_hooks (random_func = None , time_func = None )
20052007 self .addCleanup (uuid ._install_c_hooks , random_func = None , time_func = None )
20062008 error = RuntimeError ('node lookup failed' )
2007- with mock .patch .object (uuid , 'getnode' , side_effect = [error , 42 ]) as getnode :
2008- with self .assertRaises (RuntimeError ) as cm :
2009- uuid .uuid6 (clock_seq = 0 )
2010- self .assertIs (cm .exception , error )
2011- self .assertEqual (uuid .uuid6 (clock_seq = 0 ).node , 42 )
2012- self .assertEqual (uuid .uuid6 (clock_seq = 0 ).node , 42 )
2013- self .assertEqual (getnode .call_count , 2 )
2009+ getnode = mock .Mock (side_effect = [error , 42 ])
2010+ uuid ._install_c_hooks (random_func = None , time_func = None ,
2011+ node_func = getnode )
2012+ with self .assertRaises (RuntimeError ) as cm :
2013+ uuid .uuid6 (clock_seq = 0 )
2014+ self .assertIs (cm .exception , error )
2015+ self .assertEqual (uuid .uuid6 (clock_seq = 0 ).node , 42 )
2016+ self .assertEqual (uuid .uuid6 (clock_seq = 0 ).node , 42 )
2017+ self .assertEqual (getnode .call_count , 2 )
20142018
20152019 def test_uuid6_node_cache_reentrant (self ):
20162020 # getnode() can reenter uuid6(). The recursive call initializes the cache;
20172021 # the outer call must reuse that value without deadlocking or replacing it.
20182022 import uuid
20192023
2020- uuid ._install_c_hooks (random_func = None , time_func = None )
20212024 self .addCleanup (uuid ._install_c_hooks , random_func = None , time_func = None )
20222025 recursive = False
20232026
@@ -2029,8 +2032,9 @@ def getnode():
20292032 return 22
20302033 return 11
20312034
2032- with mock .patch .object (uuid , 'getnode' , side_effect = getnode ):
2033- self .assertEqual (uuid .uuid6 (clock_seq = 0 ).node , 11 )
2035+ uuid ._install_c_hooks (random_func = None , time_func = None ,
2036+ node_func = getnode )
2037+ self .assertEqual (uuid .uuid6 (clock_seq = 0 ).node , 11 )
20342038
20352039 @threading_helper .requires_working_threading ()
20362040 @threading_helper .reap_threads
@@ -2057,17 +2061,17 @@ def generate():
20572061 local .id = 1
20582062 return uuid .uuid6 (clock_seq = 0 )
20592063
2060- uuid ._install_c_hooks (random_func = None , time_func = None )
2064+ uuid ._install_c_hooks (random_func = None , time_func = None ,
2065+ node_func = getnode )
20612066 self .addCleanup (uuid ._install_c_hooks , random_func = None , time_func = None )
2062- with mock .patch .object (uuid , 'getnode' , side_effect = getnode ):
2063- with ThreadPoolExecutor (1 ) as pool :
2064- pending = pool .submit (generate )
2065- try :
2066- self .assertTrue (entered .wait (support .SHORT_TIMEOUT ))
2067- other = uuid .uuid6 (clock_seq = 0 )
2068- finally :
2069- resume .set ()
2070- first = pending .result (timeout = support .SHORT_TIMEOUT )
2067+ with ThreadPoolExecutor (1 ) as pool :
2068+ pending = pool .submit (generate )
2069+ try :
2070+ self .assertTrue (entered .wait (support .SHORT_TIMEOUT ))
2071+ other = uuid .uuid6 (clock_seq = 0 )
2072+ finally :
2073+ resume .set ()
2074+ first = pending .result (timeout = support .SHORT_TIMEOUT )
20712075 self .assertEqual (first .node , 2 )
20722076 self .assertEqual (other .node , 2 )
20732077
0 commit comments