We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
python crashes when importing matplotlib in terminal mode on m1 mac
Hello community, 

I am experiencing the following crash when trying to plot from the command line on my M1 mac:

```
(base) ➜  ~ ipython
Python 3.9.1 | packaged by conda-forge | (default, Jan 26 2021, 01:30:54) 
Type 'copyright', 'credits' or 'license' for more information
IPython 7.20.0 -- An enhanced Interactive Python. Type '?' for help.
In [1]: import matplotlib

In [2]: matplotlib.get_backend()
Out[2]: 'MacOSX'

In [3]: [1]    2797 bus error  ipython
```

Even more weird, if I try to set any value of `c.InteractiveShellApp.matplotlib` in the `ipython_config.py`, the crash occurs instantly (that is, when ipython starts). In "vanilla" python REPL, everything works as expected.

My ipython (7.20.0) and matplotlib (3.3.4) are installed through conda and this is otherwise a "fresh" install. 

Any help is appreciated! 

--- 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'))
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'))
41 
42 CFFileDescriptorCreate = CoreFoundation.CFFileDescriptorCreate
43 CFFileDescriptorCreate.restype = void_p
44 CFFileDescriptorCreate.argtypes = [void_p, ctypes.c_int, ctypes.c_bool, 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
@@ -41,7 +41,7 @@
 
 CFFileDescriptorCreate = CoreFoundation.CFFileDescriptorCreate
 CFFileDescriptorCreate.restype = void_p
-CFFileDescriptorCreate.argtypes = [void_p, ctypes.c_int, ctypes.c_bool, void_p]
+CFFileDescriptorCreate.argtypes = [void_p, ctypes.c_int, ctypes.c_bool, void_p, void_p]
 
 CFFileDescriptorGetNativeDescriptor = CoreFoundation.CFFileDescriptorGetNativeDescriptor
 CFFileDescriptorGetNativeDescriptor.restype = ctypes.c_int

{"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@@ -41,7 +41,7 @@\n \n CFFileDescriptorCreate = CoreFoundation.CFFileDescriptorCreate\n CFFileDescriptorCreate.restype = void_p\n-CFFileDescriptorCreate.argtypes = [void_p, ctypes.c_int, ctypes.c_bool, void_p]\n+CFFileDescriptorCreate.argtypes = [void_p, ctypes.c_int, ctypes.c_bool, void_p, void_p]\n \n CFFileDescriptorGetNativeDescriptor = CoreFoundation.CFFileDescriptorGetNativeDescriptor\n CFFileDescriptorGetNativeDescriptor.restype = ctypes.c_int\n", "issue": "python crashes when importing matplotlib in terminal mode on m1 mac\nHello community, \r\n\r\nI am experiencing the following crash when trying to plot from the command line on my M1 mac:\r\n\r\n```\r\n(base) \u279c  ~ ipython\r\nPython 3.9.1 | packaged by conda-forge | (default, Jan 26 2021, 01:30:54) \r\nType 'copyright', 'credits' or 'license' for more information\r\nIPython 7.20.0 -- An enhanced Interactive Python. Type '?' for help.\r\nIn [1]: import matplotlib\r\n\r\nIn [2]: matplotlib.get_backend()\r\nOut[2]: 'MacOSX'\r\n\r\nIn [3]: [1]    2797 bus error  ipython\r\n```\r\n\r\nEven more weird, if I try to set any value of `c.InteractiveShellApp.matplotlib` in the `ipython_config.py`, the crash occurs instantly (that is, when ipython starts). In \"vanilla\" python REPL, everything works as expected.\r\n\r\nMy ipython (7.20.0) and matplotlib (3.3.4) are installed through conda and this is otherwise a \"fresh\" install. \r\n\r\nAny help is appreciated! \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'))\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'))\n\nCFFileDescriptorCreate = CoreFoundation.CFFileDescriptorCreate\nCFFileDescriptorCreate.restype = void_p\nCFFileDescriptorCreate.argtypes = [void_p, ctypes.c_int, ctypes.c_bool, 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'))\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'))\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"}]}