We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
%gui osx hangs with ipython 8.13.2 on macOS 13.2.1
This causes ipython to hang.  The process cannot be suspended with ^Z and must be killed from another shell:

```% ipython
Python 3.11.2 (v3.11.2:878ead1ac1, Feb  7 2023, 10:02:41) [Clang 13.0.0 (clang-1300.0.29.30)]
Type 'copyright', 'credits' or 'license' for more information
IPython 8.13.2 -- An enhanced Interactive Python. Type '?' for help.

In [1]: %gui osx
Installed osx event loop hook.

In [2]: 
```

This does not hang:
```% ipython
Python 3.11.2 (v3.11.2:878ead1ac1, Feb  7 2023, 10:02:41) [Clang 13.0.0 (clang-1300.0.29.30)]
Type 'copyright', 'credits' or 'license' for more information
IPython 8.13.2 -- An enhanced Interactive Python. Type '?' for help.

In [1]: import matplotlib

In [2]: matplotlib.get_backend()
Installed osx event loop hook.
Out[2]: 'MacOSX'

In [3]: 2+2
Out[3]: 4
```
If the hung process is killed with ABRT, the crash report shows a deadlock:
```
hread 0 Crashed::  Dispatch queue: com.apple.main-thread
0   libsystem_kernel.dylib        	    0x7ff80a34c5c2 mach_msg2_trap + 10
1   libsystem_kernel.dylib        	    0x7ff80a35a604 mach_msg2_internal + 82
2   libsystem_kernel.dylib        	    0x7ff80a353635 mach_msg_overwrite + 723
3   libsystem_kernel.dylib        	    0x7ff80a34c8a8 mach_msg + 19
4   CoreFoundation                	    0x7ff80a466cbe __CFRunLoopServiceMachPort + 145
5   CoreFoundation                	    0x7ff80a46572a __CFRunLoopRun + 1360
6   CoreFoundation                	    0x7ff80a464b60 CFRunLoopRunSpecific + 560
7   CoreFoundation                	    0x7ff80a4e97aa CFRunLoopRun + 40
8   libffi.dylib                  	    0x7ff8198e3912 ffi_call_unix64 + 82
9   libffi.dylib                  	    0x7ff8198e323a ffi_call_int + 840
[ ... same line repeated 500 times ...]
510 libffi.dylib                  	    0x7ff8198e323a ffi_call_int + 840
511 libffi.dylib                  	    0x7ff8198e323a ffi_call_int + 840

Thread 1:
0   libsystem_kernel.dylib        	    0x7ff80a34f11a __psynch_cvwait + 10
1   libsystem_pthread.dylib       	    0x7ff80a38b7e1 _pthread_cond_wait + 1243
2   Python                        	       0x105a41272 0x1057e3000 + 2482802
3   Python                        	       0x105ac4d9e 0x1057e3000 + 3022238
4   Python                        	       0x105ac4fdc 0x1057e3000 + 3022812
5   Python                        	       0x10589ab07 0x1057e3000 + 752391
6   Python                        	       0x1059b72ef 0x1057e3000 + 1917679
7   Python                        	       0x1059bd4d0 0x1057e3000 + 1942736
8   Python                        	       0x1059ba54a 0x1057e3000 + 1930570
9   Python                        	       0x1059bd4d0 0x1057e3000 + 1942736
10  Python                        	       0x1059ba54a 0x1057e3000 + 1930570
11  Python                        	       0x1059bd4d0 0x1057e3000 + 1942736
12  Python                        	       0x10588d816 0x1057e3000 + 698390
13  Python                        	       0x105ac5b26 0x1057e3000 + 3025702
14  Python                        	       0x105a40ea4 0x1057e3000 + 2481828
15  libsystem_pthread.dylib       	    0x7ff80a38b259 _pthread_start + 125
16  libsystem_pthread.dylib       	    0x7ff80a386c7b thread_start + 15

Thread 2:
0   libsystem_kernel.dylib        	    0x7ff80a34f11a __psynch_cvwait + 10
1   libsystem_pthread.dylib       	    0x7ff80a38b7e1 _pthread_cond_wait + 1243
2   Python                        	       0x105a41272 0x1057e3000 + 2482802
3   Python                        	       0x105ac4d9e 0x1057e3000 + 3022238
4   Python                        	       0x105ac4fdc 0x1057e3000 + 3022812
5   Python                        	       0x10589ab07 0x1057e3000 + 752391
6   Python                        	       0x1059b72ef 0x1057e3000 + 1917679
7   Python                        	       0x1059bd4d0 0x1057e3000 + 1942736
8   Python                        	       0x10588d816 0x1057e3000 + 698390
9   Python                        	       0x1059ba54a 0x1057e3000 + 1930570
10  Python                        	       0x1059bd4d0 0x1057e3000 + 1942736
11  Python                        	       0x10588d816 0x1057e3000 + 698390
12  Python                        	       0x105ac5b26 0x1057e3000 + 3025702
13  Python                        	       0x105a40ea4 0x1057e3000 + 2481828
14  libsystem_pthread.dylib       	    0x7ff80a38b259 _pthread_start + 125
15  libsystem_pthread.dylib       	    0x7ff80a386c7b thread_start + 15


--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `IPython/terminal/pt_inputhooks/osx.py`
Content:
```
1 """Inputhook for OS X
2 
3 Calls NSApp / CoreFoundation APIs via ctypes.
4 """
5 
6 # obj-c boilerplate from appnope, used under BSD 2-clause
7 
8 import ctypes
9 import ctypes.util
10 from threading import Event
11 
12 objc = ctypes.cdll.LoadLibrary(ctypes.util.find_library("objc"))  # type: ignore
13 
14 void_p = ctypes.c_void_p
15 
16 objc.objc_getClass.restype = void_p
17 objc.sel_registerName.restype = void_p
18 objc.objc_msgSend.restype = void_p
19 objc.objc_msgSend.argtypes = [void_p, void_p]
20 
21 msg = objc.objc_msgSend
22 
23 def _utf8(s):
24     """ensure utf8 bytes"""
25     if not isinstance(s, bytes):
26         s = s.encode('utf8')
27     return s
28 
29 def n(name):
30     """create a selector name (for ObjC methods)"""
31     return objc.sel_registerName(_utf8(name))
32 
33 def C(classname):
34     """get an ObjC Class by name"""
35     return objc.objc_getClass(_utf8(classname))
36 
37 # end obj-c boilerplate from appnope
38 
39 # CoreFoundation C-API calls we will use:
40 CoreFoundation = ctypes.cdll.LoadLibrary(ctypes.util.find_library("CoreFoundation"))  # type: ignore
41 
42 CFFileDescriptorCreate = CoreFoundation.CFFileDescriptorCreate
43 CFFileDescriptorCreate.restype = void_p
44 CFFileDescriptorCreate.argtypes = [void_p, ctypes.c_int, ctypes.c_bool, void_p, void_p]
45 
46 CFFileDescriptorGetNativeDescriptor = CoreFoundation.CFFileDescriptorGetNativeDescriptor
47 CFFileDescriptorGetNativeDescriptor.restype = ctypes.c_int
48 CFFileDescriptorGetNativeDescriptor.argtypes = [void_p]
49 
50 CFFileDescriptorEnableCallBacks = CoreFoundation.CFFileDescriptorEnableCallBacks
51 CFFileDescriptorEnableCallBacks.restype = None
52 CFFileDescriptorEnableCallBacks.argtypes = [void_p, ctypes.c_ulong]
53 
54 CFFileDescriptorCreateRunLoopSource = CoreFoundation.CFFileDescriptorCreateRunLoopSource
55 CFFileDescriptorCreateRunLoopSource.restype = void_p
56 CFFileDescriptorCreateRunLoopSource.argtypes = [void_p, void_p, void_p]
57 
58 CFRunLoopGetCurrent = CoreFoundation.CFRunLoopGetCurrent
59 CFRunLoopGetCurrent.restype = void_p
60 
61 CFRunLoopAddSource = CoreFoundation.CFRunLoopAddSource
62 CFRunLoopAddSource.restype = None
63 CFRunLoopAddSource.argtypes = [void_p, void_p, void_p]
64 
65 CFRelease = CoreFoundation.CFRelease
66 CFRelease.restype = None
67 CFRelease.argtypes = [void_p]
68 
69 CFFileDescriptorInvalidate = CoreFoundation.CFFileDescriptorInvalidate
70 CFFileDescriptorInvalidate.restype = None
71 CFFileDescriptorInvalidate.argtypes = [void_p]
72 
73 # From CFFileDescriptor.h
74 kCFFileDescriptorReadCallBack = 1
75 kCFRunLoopCommonModes = void_p.in_dll(CoreFoundation, 'kCFRunLoopCommonModes')
76 
77 
78 def _NSApp():
79     """Return the global NSApplication instance (NSApp)"""
80     objc.objc_msgSend.argtypes = [void_p, void_p]
81     return msg(C('NSApplication'), n('sharedApplication'))
82 
83 
84 def _wake(NSApp):
85     """Wake the Application"""
86     objc.objc_msgSend.argtypes = [
87         void_p,
88         void_p,
89         void_p,
90         void_p,
91         void_p,
92         void_p,
93         void_p,
94         void_p,
95         void_p,
96         void_p,
97         void_p,
98     ]
99     event = msg(
100         C("NSEvent"),
101         n(
102             "otherEventWithType:location:modifierFlags:"
103             "timestamp:windowNumber:context:subtype:data1:data2:"
104         ),
105         15,  # Type
106         0,  # location
107         0,  # flags
108         0,  # timestamp
109         0,  # window
110         None,  # context
111         0,  # subtype
112         0,  # data1
113         0,  # data2
114     )
115     objc.objc_msgSend.argtypes = [void_p, void_p, void_p, void_p]
116     msg(NSApp, n('postEvent:atStart:'), void_p(event), True)
117 
118 
119 _triggered = Event()
120 
121 def _input_callback(fdref, flags, info):
122     """Callback to fire when there's input to be read"""
123     _triggered.set()
124     CFFileDescriptorInvalidate(fdref)
125     CFRelease(fdref)
126     NSApp = _NSApp()
127     objc.objc_msgSend.argtypes = [void_p, void_p, void_p]
128     msg(NSApp, n('stop:'), NSApp)
129     _wake(NSApp)
130 
131 _c_callback_func_type = ctypes.CFUNCTYPE(None, void_p, void_p, void_p)
132 _c_input_callback = _c_callback_func_type(_input_callback)
133 
134 
135 def _stop_on_read(fd):
136     """Register callback to stop eventloop when there's data on fd"""
137     _triggered.clear()
138     fdref = CFFileDescriptorCreate(None, fd, False, _c_input_callback, None)
139     CFFileDescriptorEnableCallBacks(fdref, kCFFileDescriptorReadCallBack)
140     source = CFFileDescriptorCreateRunLoopSource(None, fdref, 0)
141     loop = CFRunLoopGetCurrent()
142     CFRunLoopAddSource(loop, source, kCFRunLoopCommonModes)
143     CFRelease(source)
144 
145 
146 def inputhook(context):
147     """Inputhook for Cocoa (NSApp)"""
148     NSApp = _NSApp()
149     _stop_on_read(context.fileno())
150     objc.objc_msgSend.argtypes = [void_p, void_p]
151     msg(NSApp, n('run'))
152     if not _triggered.is_set():
153         # app closed without firing callback,
154         # probably due to last window being closed.
155         # Run the loop manually in this case,
156         # since there may be events still to process (#9734)
157         CoreFoundation.CFRunLoopRun()
158
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/IPython/terminal/pt_inputhooks/osx.py b/IPython/terminal/pt_inputhooks/osx.py
--- a/IPython/terminal/pt_inputhooks/osx.py
+++ b/IPython/terminal/pt_inputhooks/osx.py
@@ -116,11 +116,8 @@
     msg(NSApp, n('postEvent:atStart:'), void_p(event), True)
 
 
-_triggered = Event()
-
 def _input_callback(fdref, flags, info):
     """Callback to fire when there's input to be read"""
-    _triggered.set()
     CFFileDescriptorInvalidate(fdref)
     CFRelease(fdref)
     NSApp = _NSApp()
@@ -134,7 +131,6 @@
 
 def _stop_on_read(fd):
     """Register callback to stop eventloop when there's data on fd"""
-    _triggered.clear()
     fdref = CFFileDescriptorCreate(None, fd, False, _c_input_callback, None)
     CFFileDescriptorEnableCallBacks(fdref, kCFFileDescriptorReadCallBack)
     source = CFFileDescriptorCreateRunLoopSource(None, fdref, 0)
@@ -149,9 +145,3 @@
     _stop_on_read(context.fileno())
     objc.objc_msgSend.argtypes = [void_p, void_p]
     msg(NSApp, n('run'))
-    if not _triggered.is_set():
-        # app closed without firing callback,
-        # probably due to last window being closed.
-        # Run the loop manually in this case,
-        # since there may be events still to process (#9734)
-        CoreFoundation.CFRunLoopRun()

{"golden_diff": "diff --git a/IPython/terminal/pt_inputhooks/osx.py b/IPython/terminal/pt_inputhooks/osx.py\n--- a/IPython/terminal/pt_inputhooks/osx.py\n+++ b/IPython/terminal/pt_inputhooks/osx.py\n@@ -116,11 +116,8 @@\n     msg(NSApp, n('postEvent:atStart:'), void_p(event), True)\n \n \n-_triggered = Event()\n-\n def _input_callback(fdref, flags, info):\n     \"\"\"Callback to fire when there's input to be read\"\"\"\n-    _triggered.set()\n     CFFileDescriptorInvalidate(fdref)\n     CFRelease(fdref)\n     NSApp = _NSApp()\n@@ -134,7 +131,6 @@\n \n def _stop_on_read(fd):\n     \"\"\"Register callback to stop eventloop when there's data on fd\"\"\"\n-    _triggered.clear()\n     fdref = CFFileDescriptorCreate(None, fd, False, _c_input_callback, None)\n     CFFileDescriptorEnableCallBacks(fdref, kCFFileDescriptorReadCallBack)\n     source = CFFileDescriptorCreateRunLoopSource(None, fdref, 0)\n@@ -149,9 +145,3 @@\n     _stop_on_read(context.fileno())\n     objc.objc_msgSend.argtypes = [void_p, void_p]\n     msg(NSApp, n('run'))\n-    if not _triggered.is_set():\n-        # app closed without firing callback,\n-        # probably due to last window being closed.\n-        # Run the loop manually in this case,\n-        # since there may be events still to process (#9734)\n-        CoreFoundation.CFRunLoopRun()\n", "issue": "%gui osx hangs with ipython 8.13.2 on macOS 13.2.1\nThis causes ipython to hang.  The process cannot be suspended with ^Z and must be killed from another shell:\r\n\r\n```% ipython\r\nPython 3.11.2 (v3.11.2:878ead1ac1, Feb  7 2023, 10:02:41) [Clang 13.0.0 (clang-1300.0.29.30)]\r\nType 'copyright', 'credits' or 'license' for more information\r\nIPython 8.13.2 -- An enhanced Interactive Python. Type '?' for help.\r\n\r\nIn [1]: %gui osx\r\nInstalled osx event loop hook.\r\n\r\nIn [2]: \r\n```\r\n\r\nThis does not hang:\r\n```% ipython\r\nPython 3.11.2 (v3.11.2:878ead1ac1, Feb  7 2023, 10:02:41) [Clang 13.0.0 (clang-1300.0.29.30)]\r\nType 'copyright', 'credits' or 'license' for more information\r\nIPython 8.13.2 -- An enhanced Interactive Python. Type '?' for help.\r\n\r\nIn [1]: import matplotlib\r\n\r\nIn [2]: matplotlib.get_backend()\r\nInstalled osx event loop hook.\r\nOut[2]: 'MacOSX'\r\n\r\nIn [3]: 2+2\r\nOut[3]: 4\r\n```\r\nIf the hung process is killed with ABRT, the crash report shows a deadlock:\r\n```\r\nhread 0 Crashed::  Dispatch queue: com.apple.main-thread\r\n0   libsystem_kernel.dylib        \t    0x7ff80a34c5c2 mach_msg2_trap + 10\r\n1   libsystem_kernel.dylib        \t    0x7ff80a35a604 mach_msg2_internal + 82\r\n2   libsystem_kernel.dylib        \t    0x7ff80a353635 mach_msg_overwrite + 723\r\n3   libsystem_kernel.dylib        \t    0x7ff80a34c8a8 mach_msg + 19\r\n4   CoreFoundation                \t    0x7ff80a466cbe __CFRunLoopServiceMachPort + 145\r\n5   CoreFoundation                \t    0x7ff80a46572a __CFRunLoopRun + 1360\r\n6   CoreFoundation                \t    0x7ff80a464b60 CFRunLoopRunSpecific + 560\r\n7   CoreFoundation                \t    0x7ff80a4e97aa CFRunLoopRun + 40\r\n8   libffi.dylib                  \t    0x7ff8198e3912 ffi_call_unix64 + 82\r\n9   libffi.dylib                  \t    0x7ff8198e323a ffi_call_int + 840\r\n[ ... same line repeated 500 times ...]\r\n510 libffi.dylib                  \t    0x7ff8198e323a ffi_call_int + 840\r\n511 libffi.dylib                  \t    0x7ff8198e323a ffi_call_int + 840\r\n\r\nThread 1:\r\n0   libsystem_kernel.dylib        \t    0x7ff80a34f11a __psynch_cvwait + 10\r\n1   libsystem_pthread.dylib       \t    0x7ff80a38b7e1 _pthread_cond_wait + 1243\r\n2   Python                        \t       0x105a41272 0x1057e3000 + 2482802\r\n3   Python                        \t       0x105ac4d9e 0x1057e3000 + 3022238\r\n4   Python                        \t       0x105ac4fdc 0x1057e3000 + 3022812\r\n5   Python                        \t       0x10589ab07 0x1057e3000 + 752391\r\n6   Python                        \t       0x1059b72ef 0x1057e3000 + 1917679\r\n7   Python                        \t       0x1059bd4d0 0x1057e3000 + 1942736\r\n8   Python                        \t       0x1059ba54a 0x1057e3000 + 1930570\r\n9   Python                        \t       0x1059bd4d0 0x1057e3000 + 1942736\r\n10  Python                        \t       0x1059ba54a 0x1057e3000 + 1930570\r\n11  Python                        \t       0x1059bd4d0 0x1057e3000 + 1942736\r\n12  Python                        \t       0x10588d816 0x1057e3000 + 698390\r\n13  Python                        \t       0x105ac5b26 0x1057e3000 + 3025702\r\n14  Python                        \t       0x105a40ea4 0x1057e3000 + 2481828\r\n15  libsystem_pthread.dylib       \t    0x7ff80a38b259 _pthread_start + 125\r\n16  libsystem_pthread.dylib       \t    0x7ff80a386c7b thread_start + 15\r\n\r\nThread 2:\r\n0   libsystem_kernel.dylib        \t    0x7ff80a34f11a __psynch_cvwait + 10\r\n1   libsystem_pthread.dylib       \t    0x7ff80a38b7e1 _pthread_cond_wait + 1243\r\n2   Python                        \t       0x105a41272 0x1057e3000 + 2482802\r\n3   Python                        \t       0x105ac4d9e 0x1057e3000 + 3022238\r\n4   Python                        \t       0x105ac4fdc 0x1057e3000 + 3022812\r\n5   Python                        \t       0x10589ab07 0x1057e3000 + 752391\r\n6   Python                        \t       0x1059b72ef 0x1057e3000 + 1917679\r\n7   Python                        \t       0x1059bd4d0 0x1057e3000 + 1942736\r\n8   Python                        \t       0x10588d816 0x1057e3000 + 698390\r\n9   Python                        \t       0x1059ba54a 0x1057e3000 + 1930570\r\n10  Python                        \t       0x1059bd4d0 0x1057e3000 + 1942736\r\n11  Python                        \t       0x10588d816 0x1057e3000 + 698390\r\n12  Python                        \t       0x105ac5b26 0x1057e3000 + 3025702\r\n13  Python                        \t       0x105a40ea4 0x1057e3000 + 2481828\r\n14  libsystem_pthread.dylib       \t    0x7ff80a38b259 _pthread_start + 125\r\n15  libsystem_pthread.dylib       \t    0x7ff80a386c7b thread_start + 15\r\n\n", "before_files": [{"content": "\"\"\"Inputhook for OS X\n\nCalls NSApp / CoreFoundation APIs via ctypes.\n\"\"\"\n\n# obj-c boilerplate from appnope, used under BSD 2-clause\n\nimport ctypes\nimport ctypes.util\nfrom threading import Event\n\nobjc = ctypes.cdll.LoadLibrary(ctypes.util.find_library(\"objc\"))  # type: ignore\n\nvoid_p = ctypes.c_void_p\n\nobjc.objc_getClass.restype = void_p\nobjc.sel_registerName.restype = void_p\nobjc.objc_msgSend.restype = void_p\nobjc.objc_msgSend.argtypes = [void_p, void_p]\n\nmsg = objc.objc_msgSend\n\ndef _utf8(s):\n    \"\"\"ensure utf8 bytes\"\"\"\n    if not isinstance(s, bytes):\n        s = s.encode('utf8')\n    return s\n\ndef n(name):\n    \"\"\"create a selector name (for ObjC methods)\"\"\"\n    return objc.sel_registerName(_utf8(name))\n\ndef C(classname):\n    \"\"\"get an ObjC Class by name\"\"\"\n    return objc.objc_getClass(_utf8(classname))\n\n# end obj-c boilerplate from appnope\n\n# CoreFoundation C-API calls we will use:\nCoreFoundation = ctypes.cdll.LoadLibrary(ctypes.util.find_library(\"CoreFoundation\"))  # type: ignore\n\nCFFileDescriptorCreate = CoreFoundation.CFFileDescriptorCreate\nCFFileDescriptorCreate.restype = void_p\nCFFileDescriptorCreate.argtypes = [void_p, ctypes.c_int, ctypes.c_bool, void_p, void_p]\n\nCFFileDescriptorGetNativeDescriptor = CoreFoundation.CFFileDescriptorGetNativeDescriptor\nCFFileDescriptorGetNativeDescriptor.restype = ctypes.c_int\nCFFileDescriptorGetNativeDescriptor.argtypes = [void_p]\n\nCFFileDescriptorEnableCallBacks = CoreFoundation.CFFileDescriptorEnableCallBacks\nCFFileDescriptorEnableCallBacks.restype = None\nCFFileDescriptorEnableCallBacks.argtypes = [void_p, ctypes.c_ulong]\n\nCFFileDescriptorCreateRunLoopSource = CoreFoundation.CFFileDescriptorCreateRunLoopSource\nCFFileDescriptorCreateRunLoopSource.restype = void_p\nCFFileDescriptorCreateRunLoopSource.argtypes = [void_p, void_p, void_p]\n\nCFRunLoopGetCurrent = CoreFoundation.CFRunLoopGetCurrent\nCFRunLoopGetCurrent.restype = void_p\n\nCFRunLoopAddSource = CoreFoundation.CFRunLoopAddSource\nCFRunLoopAddSource.restype = None\nCFRunLoopAddSource.argtypes = [void_p, void_p, void_p]\n\nCFRelease = CoreFoundation.CFRelease\nCFRelease.restype = None\nCFRelease.argtypes = [void_p]\n\nCFFileDescriptorInvalidate = CoreFoundation.CFFileDescriptorInvalidate\nCFFileDescriptorInvalidate.restype = None\nCFFileDescriptorInvalidate.argtypes = [void_p]\n\n# From CFFileDescriptor.h\nkCFFileDescriptorReadCallBack = 1\nkCFRunLoopCommonModes = void_p.in_dll(CoreFoundation, 'kCFRunLoopCommonModes')\n\n\ndef _NSApp():\n    \"\"\"Return the global NSApplication instance (NSApp)\"\"\"\n    objc.objc_msgSend.argtypes = [void_p, void_p]\n    return msg(C('NSApplication'), n('sharedApplication'))\n\n\ndef _wake(NSApp):\n    \"\"\"Wake the Application\"\"\"\n    objc.objc_msgSend.argtypes = [\n        void_p,\n        void_p,\n        void_p,\n        void_p,\n        void_p,\n        void_p,\n        void_p,\n        void_p,\n        void_p,\n        void_p,\n        void_p,\n    ]\n    event = msg(\n        C(\"NSEvent\"),\n        n(\n            \"otherEventWithType:location:modifierFlags:\"\n            \"timestamp:windowNumber:context:subtype:data1:data2:\"\n        ),\n        15,  # Type\n        0,  # location\n        0,  # flags\n        0,  # timestamp\n        0,  # window\n        None,  # context\n        0,  # subtype\n        0,  # data1\n        0,  # data2\n    )\n    objc.objc_msgSend.argtypes = [void_p, void_p, void_p, void_p]\n    msg(NSApp, n('postEvent:atStart:'), void_p(event), True)\n\n\n_triggered = Event()\n\ndef _input_callback(fdref, flags, info):\n    \"\"\"Callback to fire when there's input to be read\"\"\"\n    _triggered.set()\n    CFFileDescriptorInvalidate(fdref)\n    CFRelease(fdref)\n    NSApp = _NSApp()\n    objc.objc_msgSend.argtypes = [void_p, void_p, void_p]\n    msg(NSApp, n('stop:'), NSApp)\n    _wake(NSApp)\n\n_c_callback_func_type = ctypes.CFUNCTYPE(None, void_p, void_p, void_p)\n_c_input_callback = _c_callback_func_type(_input_callback)\n\n\ndef _stop_on_read(fd):\n    \"\"\"Register callback to stop eventloop when there's data on fd\"\"\"\n    _triggered.clear()\n    fdref = CFFileDescriptorCreate(None, fd, False, _c_input_callback, None)\n    CFFileDescriptorEnableCallBacks(fdref, kCFFileDescriptorReadCallBack)\n    source = CFFileDescriptorCreateRunLoopSource(None, fdref, 0)\n    loop = CFRunLoopGetCurrent()\n    CFRunLoopAddSource(loop, source, kCFRunLoopCommonModes)\n    CFRelease(source)\n\n\ndef inputhook(context):\n    \"\"\"Inputhook for Cocoa (NSApp)\"\"\"\n    NSApp = _NSApp()\n    _stop_on_read(context.fileno())\n    objc.objc_msgSend.argtypes = [void_p, void_p]\n    msg(NSApp, n('run'))\n    if not _triggered.is_set():\n        # app closed without firing callback,\n        # probably due to last window being closed.\n        # Run the loop manually in this case,\n        # since there may be events still to process (#9734)\n        CoreFoundation.CFRunLoopRun()\n", "path": "IPython/terminal/pt_inputhooks/osx.py"}], "after_files": [{"content": "\"\"\"Inputhook for OS X\n\nCalls NSApp / CoreFoundation APIs via ctypes.\n\"\"\"\n\n# obj-c boilerplate from appnope, used under BSD 2-clause\n\nimport ctypes\nimport ctypes.util\nfrom threading import Event\n\nobjc = ctypes.cdll.LoadLibrary(ctypes.util.find_library(\"objc\"))  # type: ignore\n\nvoid_p = ctypes.c_void_p\n\nobjc.objc_getClass.restype = void_p\nobjc.sel_registerName.restype = void_p\nobjc.objc_msgSend.restype = void_p\nobjc.objc_msgSend.argtypes = [void_p, void_p]\n\nmsg = objc.objc_msgSend\n\ndef _utf8(s):\n    \"\"\"ensure utf8 bytes\"\"\"\n    if not isinstance(s, bytes):\n        s = s.encode('utf8')\n    return s\n\ndef n(name):\n    \"\"\"create a selector name (for ObjC methods)\"\"\"\n    return objc.sel_registerName(_utf8(name))\n\ndef C(classname):\n    \"\"\"get an ObjC Class by name\"\"\"\n    return objc.objc_getClass(_utf8(classname))\n\n# end obj-c boilerplate from appnope\n\n# CoreFoundation C-API calls we will use:\nCoreFoundation = ctypes.cdll.LoadLibrary(ctypes.util.find_library(\"CoreFoundation\"))  # type: ignore\n\nCFFileDescriptorCreate = CoreFoundation.CFFileDescriptorCreate\nCFFileDescriptorCreate.restype = void_p\nCFFileDescriptorCreate.argtypes = [void_p, ctypes.c_int, ctypes.c_bool, void_p, void_p]\n\nCFFileDescriptorGetNativeDescriptor = CoreFoundation.CFFileDescriptorGetNativeDescriptor\nCFFileDescriptorGetNativeDescriptor.restype = ctypes.c_int\nCFFileDescriptorGetNativeDescriptor.argtypes = [void_p]\n\nCFFileDescriptorEnableCallBacks = CoreFoundation.CFFileDescriptorEnableCallBacks\nCFFileDescriptorEnableCallBacks.restype = None\nCFFileDescriptorEnableCallBacks.argtypes = [void_p, ctypes.c_ulong]\n\nCFFileDescriptorCreateRunLoopSource = CoreFoundation.CFFileDescriptorCreateRunLoopSource\nCFFileDescriptorCreateRunLoopSource.restype = void_p\nCFFileDescriptorCreateRunLoopSource.argtypes = [void_p, void_p, void_p]\n\nCFRunLoopGetCurrent = CoreFoundation.CFRunLoopGetCurrent\nCFRunLoopGetCurrent.restype = void_p\n\nCFRunLoopAddSource = CoreFoundation.CFRunLoopAddSource\nCFRunLoopAddSource.restype = None\nCFRunLoopAddSource.argtypes = [void_p, void_p, void_p]\n\nCFRelease = CoreFoundation.CFRelease\nCFRelease.restype = None\nCFRelease.argtypes = [void_p]\n\nCFFileDescriptorInvalidate = CoreFoundation.CFFileDescriptorInvalidate\nCFFileDescriptorInvalidate.restype = None\nCFFileDescriptorInvalidate.argtypes = [void_p]\n\n# From CFFileDescriptor.h\nkCFFileDescriptorReadCallBack = 1\nkCFRunLoopCommonModes = void_p.in_dll(CoreFoundation, 'kCFRunLoopCommonModes')\n\n\ndef _NSApp():\n    \"\"\"Return the global NSApplication instance (NSApp)\"\"\"\n    objc.objc_msgSend.argtypes = [void_p, void_p]\n    return msg(C('NSApplication'), n('sharedApplication'))\n\n\ndef _wake(NSApp):\n    \"\"\"Wake the Application\"\"\"\n    objc.objc_msgSend.argtypes = [\n        void_p,\n        void_p,\n        void_p,\n        void_p,\n        void_p,\n        void_p,\n        void_p,\n        void_p,\n        void_p,\n        void_p,\n        void_p,\n    ]\n    event = msg(\n        C(\"NSEvent\"),\n        n(\n            \"otherEventWithType:location:modifierFlags:\"\n            \"timestamp:windowNumber:context:subtype:data1:data2:\"\n        ),\n        15,  # Type\n        0,  # location\n        0,  # flags\n        0,  # timestamp\n        0,  # window\n        None,  # context\n        0,  # subtype\n        0,  # data1\n        0,  # data2\n    )\n    objc.objc_msgSend.argtypes = [void_p, void_p, void_p, void_p]\n    msg(NSApp, n('postEvent:atStart:'), void_p(event), True)\n\n\ndef _input_callback(fdref, flags, info):\n    \"\"\"Callback to fire when there's input to be read\"\"\"\n    CFFileDescriptorInvalidate(fdref)\n    CFRelease(fdref)\n    NSApp = _NSApp()\n    objc.objc_msgSend.argtypes = [void_p, void_p, void_p]\n    msg(NSApp, n('stop:'), NSApp)\n    _wake(NSApp)\n\n_c_callback_func_type = ctypes.CFUNCTYPE(None, void_p, void_p, void_p)\n_c_input_callback = _c_callback_func_type(_input_callback)\n\n\ndef _stop_on_read(fd):\n    \"\"\"Register callback to stop eventloop when there's data on fd\"\"\"\n    fdref = CFFileDescriptorCreate(None, fd, False, _c_input_callback, None)\n    CFFileDescriptorEnableCallBacks(fdref, kCFFileDescriptorReadCallBack)\n    source = CFFileDescriptorCreateRunLoopSource(None, fdref, 0)\n    loop = CFRunLoopGetCurrent()\n    CFRunLoopAddSource(loop, source, kCFRunLoopCommonModes)\n    CFRelease(source)\n\n\ndef inputhook(context):\n    \"\"\"Inputhook for Cocoa (NSApp)\"\"\"\n    NSApp = _NSApp()\n    _stop_on_read(context.fileno())\n    objc.objc_msgSend.argtypes = [void_p, void_p]\n    msg(NSApp, n('run'))\n", "path": "IPython/terminal/pt_inputhooks/osx.py"}]}