@@ -1878,66 +1878,33 @@ def test_opcodes_collection(self):
18781878 """Test that opcodes are collected when the opcodes flag is set."""
18791879 script = textwrap .dedent (
18801880 """\
1881- import time
1882- import sys
1883- import socket
1884-
1885- def compute():
1886- # Do some work that involves bytecode execution
1887- total = 0
1888- for i in range(1000):
1889- total += i
1890- return total
1891-
1892- def bar():
1893- compute()
1894-
1895- def foo():
1896- bar()
1881+ import time, sys, socket
18971882
1898- # Signal that we're ready
18991883 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
19001884 sock.connect(('localhost', {port}))
1901- sock.sendall(b"ready")
1902- sock.close()
19031885
1904- # Keep computing in a loop
1905- while True:
1906- foo()
1886+ def foo():
1887+ sock.sendall(b"ready")
1888+ time.sleep(10_000)
1889+
1890+ foo()
19071891 """
19081892 )
19091893
19101894 def get_trace_with_opcodes (pid ):
1911- unwinder = RemoteUnwinder (pid , opcodes = True )
1912- return unwinder .get_stack_trace ()
1895+ return RemoteUnwinder (pid , opcodes = True ).get_stack_trace ()
19131896
19141897 stack_trace , _ = self ._run_script_and_get_trace (
1915- script ,
1916- get_trace_with_opcodes ,
1917- wait_for_signals = b"ready" ,
1898+ script , get_trace_with_opcodes , wait_for_signals = b"ready"
19181899 )
19191900
1920- # Find the thread with our compute/bar/foo stack
1921- found_opcodes = False
1922- for interpreter_info in stack_trace :
1923- for thread_info in interpreter_info .threads :
1924- for frame in thread_info .frame_info :
1925- # Check that frames have opcodes (not None)
1926- # when opcodes=True is set
1927- if frame .funcname in ("compute" , "bar" , "foo" ):
1928- # Opcode should be an integer, not None
1929- self .assertIsInstance (
1930- frame .opcode ,
1931- int ,
1932- f"Expected opcode to be int for { frame .funcname } , got { type (frame .opcode )} "
1933- )
1934- self .assertGreaterEqual (frame .opcode , 0 )
1935- found_opcodes = True
1936-
1937- self .assertTrue (
1938- found_opcodes ,
1939- "Did not find any frames with opcodes from compute/bar/foo"
1901+ # Find our foo frame and verify it has an opcode
1902+ foo_frame = self ._find_frame_in_trace (
1903+ stack_trace , lambda f : f .funcname == "foo"
19401904 )
1905+ self .assertIsNotNone (foo_frame , "Could not find foo frame" )
1906+ self .assertIsInstance (foo_frame .opcode , int )
1907+ self .assertGreaterEqual (foo_frame .opcode , 0 )
19411908
19421909 @skip_if_not_supported
19431910 @unittest .skipIf (
@@ -1950,10 +1917,10 @@ def test_location_tuple_format(self):
19501917 """\
19511918 import time, sys, socket
19521919
1920+ sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
1921+ sock.connect(('localhost', {port}))
1922+
19531923 def foo():
1954- x = 1 + 2
1955- sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
1956- sock.connect(('localhost', {port}))
19571924 sock.sendall(b"ready")
19581925 time.sleep(10_000)
19591926
@@ -1962,13 +1929,10 @@ def foo():
19621929 )
19631930
19641931 def get_trace_with_opcodes (pid ):
1965- unwinder = RemoteUnwinder (pid , opcodes = True )
1966- return unwinder .get_stack_trace ()
1932+ return RemoteUnwinder (pid , opcodes = True ).get_stack_trace ()
19671933
1968- stack_trace , script_name = self ._run_script_and_get_trace (
1969- script ,
1970- get_trace_with_opcodes ,
1971- wait_for_signals = b"ready" ,
1934+ stack_trace , _ = self ._run_script_and_get_trace (
1935+ script , get_trace_with_opcodes , wait_for_signals = b"ready"
19721936 )
19731937
19741938 # Find our foo frame
@@ -1977,25 +1941,15 @@ def get_trace_with_opcodes(pid):
19771941 )
19781942 self .assertIsNotNone (foo_frame , "Could not find foo frame" )
19791943
1980- # Check location is a tuple with 4 elements
1944+ # Check location is a 4- tuple with valid values
19811945 location = foo_frame .location
1982- self .assertIsInstance (location , tuple , "Location should be a tuple" )
1983- self .assertEqual (
1984- len (location ), 4 ,
1985- f"Location should have 4 elements (lineno, end_lineno, col_offset, end_col_offset), got { len (location )} "
1986- )
1987-
1946+ self .assertIsInstance (location , tuple )
1947+ self .assertEqual (len (location ), 4 )
19881948 lineno , end_lineno , col_offset , end_col_offset = location
1989-
1990- # Lineno should be positive
19911949 self .assertIsInstance (lineno , int )
1992- self .assertGreater (lineno , 0 , "lineno should be positive" )
1993-
1994- # end_lineno should be >= lineno
1950+ self .assertGreater (lineno , 0 )
19951951 self .assertIsInstance (end_lineno , int )
19961952 self .assertGreaterEqual (end_lineno , lineno )
1997-
1998- # col_offset and end_col_offset should be non-negative
19991953 self .assertIsInstance (col_offset , int )
20001954 self .assertGreaterEqual (col_offset , 0 )
20011955 self .assertIsInstance (end_col_offset , int )
0 commit comments