2424_SKIP_PERMISSION_MSG = "Insufficient permissions for remote profiling"
2525
2626
27+ def _check_remote_debugging_permissions ():
28+ """Check if we have permissions for remote debugging.
29+
30+ Returns True if we have permissions, False if we don't.
31+ On macOS without proper entitlements, this will return False.
32+ """
33+ # If is_python_process returns False for the current process,
34+ # we don't have permissions (since we ARE a Python process)
35+ import _remote_debugging
36+ return _remote_debugging .is_python_process (os .getpid ())
37+
38+
2739def _readline_with_timeout (file_obj , timeout ):
2840 # Thread-based readline with timeout - works across all platforms
2941 # including Windows where select() doesn't work with pipes.
@@ -558,16 +570,23 @@ def test_is_python_process_current_process(self):
558570 """Test that current process is detected as Python."""
559571 from profiling .sampling ._child_monitor import is_python_process
560572
573+ if not _check_remote_debugging_permissions ():
574+ self .skipTest (_SKIP_PERMISSION_MSG )
575+
561576 # Current process should be Python
577+ result = is_python_process (os .getpid ())
562578 self .assertTrue (
563- is_python_process ( os . getpid ()) ,
579+ result ,
564580 f"Current process (PID { os .getpid ()} ) should be detected as Python" ,
565581 )
566582
567583 def test_is_python_process_python_subprocess (self ):
568584 """Test that a Python subprocess is detected as Python."""
569585 from profiling .sampling ._child_monitor import is_python_process
570586
587+ if not _check_remote_debugging_permissions ():
588+ self .skipTest (_SKIP_PERMISSION_MSG )
589+
571590 # Start a Python subprocess
572591 proc = subprocess .Popen (
573592 [sys .executable , "-c" , "import time; time.sleep(10)" ],
@@ -584,12 +603,9 @@ def test_is_python_process_python_subprocess(self):
584603 while time .time () < deadline :
585604 if proc .poll () is not None :
586605 self .fail (f"Process { proc .pid } exited unexpectedly" )
587- try :
588- if is_python_process (proc .pid ):
589- detected = True
590- break
591- except PermissionError :
592- self .skipTest (_SKIP_PERMISSION_MSG )
606+ if is_python_process (proc .pid ):
607+ detected = True
608+ break
593609 time .sleep (0.05 )
594610
595611 self .assertTrue (
@@ -632,8 +648,9 @@ def test_is_python_process_nonexistent_pid(self):
632648 from profiling .sampling ._child_monitor import is_python_process
633649
634650 # Use a very high PID that's unlikely to exist
651+ result = is_python_process (999999999 )
635652 self .assertFalse (
636- is_python_process ( 999999999 ) ,
653+ result ,
637654 "Nonexistent PID 999999999 should return False" ,
638655 )
639656
0 commit comments