/
JupyterKernelVariableSharingTests.can_handle_errors_for_send_value_from_kernel.python3.json
353 lines (353 loc) · 19 KB
/
JupyterKernelVariableSharingTests.can_handle_errors_for_send_value_from_kernel.python3.json
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
[
{
"header": {
"msg_id": "acfef954-3dc1fbdf0174fbe77b594b9f_21516_10",
"username": "username",
"session": "acfef954-3dc1fbdf0174fbe77b594b9f",
"date": "2023-03-21T23:57:22.796317Z",
"msg_type": "status",
"version": "5.3"
},
"parent_header": {
"msg_id": "cc7c8dae-734a-4369-b423-d670be0c8047",
"username": "dotnet_kernel",
"session": "fdb3e29d-2eed-496a-a275-0aa3dd8a5be7",
"date": "2023-03-21T23:57:22.7781131Z",
"msg_type": "kernel_info_request",
"version": "5.3"
},
"metadata": {},
"content": {
"execution_state": "busy"
},
"buffers": [],
"channel": "shell"
},
{
"header": {
"msg_id": "acfef954-3dc1fbdf0174fbe77b594b9f_21516_11",
"username": "username",
"session": "acfef954-3dc1fbdf0174fbe77b594b9f",
"date": "2023-03-21T23:57:22.796317Z",
"msg_type": "kernel_info_reply",
"version": "5.3"
},
"parent_header": {
"msg_id": "cc7c8dae-734a-4369-b423-d670be0c8047",
"username": "dotnet_kernel",
"session": "fdb3e29d-2eed-496a-a275-0aa3dd8a5be7",
"date": "2023-03-21T23:57:22.7781131Z",
"msg_type": "kernel_info_request",
"version": "5.3"
},
"metadata": {},
"content": {
"protocol_version": "5.3",
"implementation": "ipython",
"implementation_version": "7.31.1",
"language_info": {
"name": "python",
"version": "3.9.13",
"mimetype": "text/x-python",
"file_extension": ".py",
"pygments_lexer": "ipython3",
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"nbconvert_exporter": "python"
},
"banner": "Python 3.9.13 (main, Aug 25 2022, 23:51:50) [MSC v.1916 64 bit (AMD64)]\nType 'copyright', 'credits' or 'license' for more information\nIPython 7.31.1 -- An enhanced Interactive Python. Type '?' for help.\n",
"status": "ok",
"help_links": [
{
"text": "Python Reference",
"url": "https://docs.python.org/3.9"
},
{
"text": "IPython Reference",
"url": "https://ipython.org/documentation.html"
},
{
"text": "NumPy Reference",
"url": "https://docs.scipy.org/doc/numpy/reference/"
},
{
"text": "SciPy Reference",
"url": "https://docs.scipy.org/doc/scipy/reference/"
},
{
"text": "Matplotlib Reference",
"url": "https://matplotlib.org/contents.html"
},
{
"text": "SymPy Reference",
"url": "http://docs.sympy.org/latest/index.html"
},
{
"text": "pandas Reference",
"url": "https://pandas.pydata.org/pandas-docs/stable/"
}
]
},
"buffers": [],
"channel": "shell"
},
{
"header": {
"msg_id": "acfef954-3dc1fbdf0174fbe77b594b9f_21516_12",
"username": "username",
"session": "acfef954-3dc1fbdf0174fbe77b594b9f",
"date": "2023-03-21T23:57:22.797316Z",
"msg_type": "status",
"version": "5.3"
},
"parent_header": {
"msg_id": "cc7c8dae-734a-4369-b423-d670be0c8047",
"username": "dotnet_kernel",
"session": "fdb3e29d-2eed-496a-a275-0aa3dd8a5be7",
"date": "2023-03-21T23:57:22.7781131Z",
"msg_type": "kernel_info_request",
"version": "5.3"
},
"metadata": {},
"content": {
"execution_state": "idle"
},
"buffers": [],
"channel": "shell"
},
{
"header": {
"msg_id": "acfef954-3dc1fbdf0174fbe77b594b9f_21516_13",
"username": "username",
"session": "acfef954-3dc1fbdf0174fbe77b594b9f",
"date": "2023-03-21T23:57:22.801318Z",
"msg_type": "status",
"version": "5.3"
},
"parent_header": {
"msg_id": "af6b61d0-7d40-42af-8c35-391eb87c662c",
"username": "dotnet_kernel",
"session": "22a1fc49-fba7-4f2a-b0a7-2a147e8ae8a5",
"date": "2023-03-21T23:57:22.7993326Z",
"msg_type": "execute_request",
"version": "5.3"
},
"metadata": {},
"content": {
"execution_state": "busy"
},
"buffers": [],
"channel": "shell"
},
{
"header": {
"msg_id": "acfef954-3dc1fbdf0174fbe77b594b9f_21516_14",
"username": "username",
"session": "acfef954-3dc1fbdf0174fbe77b594b9f",
"date": "2023-03-21T23:57:22.801318Z",
"msg_type": "execute_input",
"version": "5.3"
},
"parent_header": {
"msg_id": "af6b61d0-7d40-42af-8c35-391eb87c662c",
"username": "dotnet_kernel",
"session": "22a1fc49-fba7-4f2a-b0a7-2a147e8ae8a5",
"date": "2023-03-21T23:57:22.7993326Z",
"msg_type": "execute_request",
"version": "5.3"
},
"metadata": {},
"content": {
"code": "# Copyright (c) .NET Foundation and contributors. All rights reserved.\n# Licensed under the MIT license. See LICENSE file in the project root for full license information.\n\ntry:\n get_ipython().__class__.__name__\nexcept NameError:\n raise Exception(\"This script needs to be run in ipython\")\n \nimport json\ndef __get_dotnet_coe_comm_handler(): \n\n class CommandEventCommTarget:\n __control_comm = None\n __coe_handler = None\n\n def handle_control_comm_opened(self, comm, msg):\n if comm is None:\n raise RuntimeError('Control comm required to open')\n \n self.__control_comm = comm\n self.__control_comm.on_msg(self.handle_control_comm_msg)\n\n self.__coe_handler = CommandEventHandler()\n self.__control_comm.send(self.__coe_handler.is_ready())\n\n def handle_control_comm_msg(self, msg):\n # This shouldn't happen unless someone calls this method manually\n if self.__control_comm is None and not self._is_debug:\n raise RuntimeError('Control comm has not been properly opened')\n\n data = msg['content']['data']\n response = self.__coe_handler.handle_command_or_event(data)\n self.__control_comm.send(response)\n \n \n class CommandEventHandler:\n __exclude_types = [\"<class 'module'>\"]\n\n \n def handle_command_or_event(self, data):\n try:\n msg_type = data['type']\n commandOrEvent = json.loads(data['commandOrEvent'])\n # self.__debugLog('handle_command_or_event.last_data_recv', commandOrEvent)\n \n if (msg_type == \"command\"):\n return self.__handle_command(commandOrEvent)\n \n except Exception as e: \n self. __debugLog('handle_command_or_event.commandFailed', e)\n return EventEnvelope(CommandFailed(f'failed to process comm data. {str(e)}')).payload()\n \n def __handle_command(self, commandOrEvent):\n commandType = commandOrEvent['commandType']\n\n envelop = None\n if (commandType == SendValue.__name__):\n envelop = self.__handle_send_value(commandOrEvent)\n elif (commandType == RequestValue.__name__):\n envelop = self.__handle_request_value(commandOrEvent)\n elif (commandType == RequestValueInfos.__name__):\n envelop = self.__handle_request_value_infos(commandOrEvent)\n else: \n envelop = EventEnvelope(CommandFailed(f'command \"{commandType}\" not supported'))\n\n return envelop.payload()\n\n def __handle_request_value_infos(self, command):\n results_who_ls = get_ipython().magic('who_ls')\n variables = globals()\n results = [KernelValueInfo(x, FormattedValue.fromValue(variables[x]), str(type(variables[x]))) \n for x in results_who_ls \n if x in variables and str(type(variables[x])) not in self.__exclude_types]\n\n\n return EventEnvelope(ValueInfosProduced(results), command)\n \n def __handle_request_value(self, command):\n requestValue = RequestValue(command['command'])\n name = requestValue.name\n mimeType = requestValue.mimeType\n \n if (name not in globals()):\n return EventEnvelope(CommandFailed(f'Variable \"{name}\" not found.'))\n \n rawValue = globals()[name]\n updatedValue = None\n\n try: \n import pandas as pd; \n if (isinstance(rawValue, pd.DataFrame)):\n mimeType = 'application/table-schema+json'\n updatedValue = rawValue.to_dict('records')\n except Exception as e: \n self. __debugLog('__handle_request_value.dataframe.error', e)\n pass\n\n formattedValue = FormattedValue.fromValue(rawValue, mimeType) \n\n return EventEnvelope(ValueProduced(name, rawValue if updatedValue is None else updatedValue, formattedValue), command)\n \n def __handle_send_value(self, command):\n sendValue = SendValue(command['command'])\n mimeType = sendValue.formattedValue['mimeType']\n name = sendValue.name\n rawValue = sendValue.formattedValue['value']\n resultValue = None\n \n if (not str.isidentifier(name)):\n return EventEnvelope(CommandFailed(f'Invalid Identifier: \"{name}\"'))\n \n if (mimeType == 'application/json'):\n import json; resultValue = json.loads(rawValue)\n elif (mimeType == 'application/table-schema+json'):\n import json; resultValue = json.loads(rawValue)\n try:\n import pandas as pd; resultValue = pd.DataFrame(data=resultValue['data'])\n except Exception as e:\n self.__debugLog('__handle_send_value.dataframe.error', e)\n return EventEnvelope(CommandFailed(f'Cannot create pandas dataframe for: \"{name}\". {str(e)}'))\n \n if (resultValue is not None): \n self.__setVariable(name, resultValue) \n return EventEnvelope(CommandSucceeded())\n \n return EventEnvelope(CommandFailed(f'Failed to set value for \"{name}\". \"{mimeType}\" mimetype not supported.'))\n \n def is_ready(self):\n return EventEnvelope(KernelReady()).payload()\n \n @staticmethod\n def __setVariable(name, value):\n globals()[name] = value\n \n @staticmethod\n def __debugLog(event, message):\n globals()[f'__log__coe_handler.{str(event)}'] = message\n \n \n class KernelCommand: \n pass\n\n class SendValue(KernelCommand): \n def __init__(self, entries):\n self.__dict__.update(**entries)\n\n class RequestValue(KernelCommand):\n def __init__(self, entries):\n self.__dict__.update(**entries)\n\n class RequestValueInfos(KernelCommand):\n def __init__(self, entries):\n self.__dict__.update(**entries)\n \n class FormattedValue:\n def __init__(self, mimeType = 'application/json', value = None):\n self.mimeType = mimeType\n self.value = value\n \n @staticmethod\n def fromValue(value, mimeType = 'application/json'):\n formattedValue = None\n try: \n import pandas as pd; \n if (isinstance(value, pd.DataFrame)):\n mimeType = 'application/table-schema+json'\n except Exception: \n pass\n\n if (mimeType == 'application/json'):\n import json; formattedValue = json.dumps(value)\n elif (mimeType == 'application/table-schema+json'):\n formattedValue = value.to_string(index=False, max_rows=5)\n\n return FormattedValue(mimeType, formattedValue)\n\n class KernelValueInfo:\n def __init__(self, name, formattedValue: FormattedValue, typeName = None):\n self.name = name\n self.formattedValue = formattedValue\n self.typeName = typeName\n \n class KernelEvent:\n pass\n\n class KernelReady(KernelEvent):\n def __init__(self, kernelInfos = []):\n self.kernelInfos = kernelInfos\n\n class CommandSucceeded(KernelEvent):\n pass\n\n class CommandFailed(KernelEvent):\n def __init__(self, message = None):\n self.message = message\n\n class ValueProduced(KernelEvent):\n def __init__(self, name, value, formattedValue: FormattedValue):\n self.name = name\n self.value = value \n self.formattedValue = formattedValue\n \n class ValueInfosProduced(KernelEvent):\n def __init__(self, valueInfos: list[KernelValueInfo]):\n self.valueInfos = valueInfos\n \n class Envelope:\n def payload(self):\n return { 'commandOrEvent': self.__to_json_string(self) }\n\n @staticmethod\n def __to_json_string(obj):\n return json.dumps(obj, default=lambda o: o.__dict__)\n\n class EventEnvelope(Envelope):\n def __init__(self, event: KernelEvent = None, command = None):\n self.event = event\n self.eventType = type(event).__name__\n self.command = command\n\n def payload(self):\n ret = super().payload()\n ret['type'] = 'event'\n return ret\n \n return CommandEventCommTarget()\n\nif hasattr(get_ipython(), 'kernel'):\n get_ipython().kernel.comm_manager.register_target('dotnet_coe_handler_comm', __get_dotnet_coe_comm_handler().handle_control_comm_opened)\n",
"execution_count": 1
},
"buffers": [],
"channel": "shell"
},
{
"header": {
"msg_id": "acfef954-3dc1fbdf0174fbe77b594b9f_21516_15",
"username": "username",
"session": "acfef954-3dc1fbdf0174fbe77b594b9f",
"date": "2023-03-21T23:57:22.834836Z",
"msg_type": "execute_reply",
"version": "5.3"
},
"parent_header": {
"msg_id": "af6b61d0-7d40-42af-8c35-391eb87c662c",
"username": "dotnet_kernel",
"session": "22a1fc49-fba7-4f2a-b0a7-2a147e8ae8a5",
"date": "2023-03-21T23:57:22.7993326Z",
"msg_type": "execute_request",
"version": "5.3"
},
"metadata": {
"started": "2023-03-21T23:57:22.801318Z",
"dependencies_met": true,
"engine": "415590a7-f835-4aeb-8059-c8fa613d0052",
"status": "ok"
},
"content": {
"payload": [],
"user_expressions": {},
"status": "ok",
"execution_count": 0
},
"buffers": [],
"channel": "shell"
},
{
"header": {
"msg_id": "acfef954-3dc1fbdf0174fbe77b594b9f_21516_16",
"username": "username",
"session": "acfef954-3dc1fbdf0174fbe77b594b9f",
"date": "2023-03-21T23:57:22.835842Z",
"msg_type": "status",
"version": "5.3"
},
"parent_header": {
"msg_id": "af6b61d0-7d40-42af-8c35-391eb87c662c",
"username": "dotnet_kernel",
"session": "22a1fc49-fba7-4f2a-b0a7-2a147e8ae8a5",
"date": "2023-03-21T23:57:22.7993326Z",
"msg_type": "execute_request",
"version": "5.3"
},
"metadata": {},
"content": {
"execution_state": "idle"
},
"buffers": [],
"channel": "shell"
},
{
"header": {
"msg_id": "acfef954-3dc1fbdf0174fbe77b594b9f_21516_17",
"username": "username",
"session": "acfef954-3dc1fbdf0174fbe77b594b9f",
"date": "2023-03-21T23:57:22.840852Z",
"msg_type": "status",
"version": "5.3"
},
"parent_header": {
"msg_id": "c85e8ac8-edde-4d31-b719-1f5a0090595a",
"username": "dotnet_kernel",
"session": "194aa72b-216e-43fe-88ae-339e0a25ffa2",
"date": "2023-03-21T23:57:22.8390988Z",
"msg_type": "comm_open",
"version": "5.3"
},
"metadata": {},
"content": {
"execution_state": "busy"
},
"buffers": [],
"channel": "shell"
},
{
"header": {
"msg_id": "acfef954-3dc1fbdf0174fbe77b594b9f_21516_18",
"username": "username",
"session": "acfef954-3dc1fbdf0174fbe77b594b9f",
"date": "2023-03-21T23:57:22.840852Z",
"msg_type": "comm_msg",
"version": "5.3"
},
"parent_header": {
"msg_id": "c85e8ac8-edde-4d31-b719-1f5a0090595a",
"username": "dotnet_kernel",
"session": "194aa72b-216e-43fe-88ae-339e0a25ffa2",
"date": "2023-03-21T23:57:22.8390988Z",
"msg_type": "comm_open",
"version": "5.3"
},
"metadata": {},
"content": {
"comm_id": "96fb7d64-2ba7-4468-90ba-66b590fe5d3c",
"data": {
"commandOrEvent": "{\"event\": {\"kernelInfos\": []}, \"eventType\": \"KernelReady\", \"command\": null}",
"type": "event"
}
},
"buffers": [],
"channel": "shell"
},
{
"header": {
"msg_id": "acfef954-3dc1fbdf0174fbe77b594b9f_21516_19",
"username": "username",
"session": "acfef954-3dc1fbdf0174fbe77b594b9f",
"date": "2023-03-21T23:57:22.841851Z",
"msg_type": "status",
"version": "5.3"
},
"parent_header": {
"msg_id": "c85e8ac8-edde-4d31-b719-1f5a0090595a",
"username": "dotnet_kernel",
"session": "194aa72b-216e-43fe-88ae-339e0a25ffa2",
"date": "2023-03-21T23:57:22.8390988Z",
"msg_type": "comm_open",
"version": "5.3"
},
"metadata": {},
"content": {
"execution_state": "idle"
},
"buffers": [],
"channel": "shell"
},
{
"header": {
"msg_id": "acfef954-3dc1fbdf0174fbe77b594b9f_21516_20",
"username": "username",
"session": "acfef954-3dc1fbdf0174fbe77b594b9f",
"date": "2023-03-21T23:57:22.846848Z",
"msg_type": "status",
"version": "5.3"
},
"parent_header": {
"msg_id": "f71e85b3-efe5-4668-8223-6858aefacb44",
"username": "dotnet_kernel",
"session": "8a93c36a-353a-4dcd-8e40-17bb28f4f4b3",
"date": "2023-03-21T23:57:22.8455872Z",
"msg_type": "comm_msg",
"version": "5.3"
},
"metadata": {},
"content": {
"execution_state": "busy"
},
"buffers": [],
"channel": "shell"
},
{
"header": {
"msg_id": "acfef954-3dc1fbdf0174fbe77b594b9f_21516_21",
"username": "username",
"session": "acfef954-3dc1fbdf0174fbe77b594b9f",
"date": "2023-03-21T23:57:22.847853Z",
"msg_type": "comm_msg",
"version": "5.3"
},
"parent_header": {
"msg_id": "f71e85b3-efe5-4668-8223-6858aefacb44",
"username": "dotnet_kernel",
"session": "8a93c36a-353a-4dcd-8e40-17bb28f4f4b3",
"date": "2023-03-21T23:57:22.8455872Z",
"msg_type": "comm_msg",
"version": "5.3"
},
"metadata": {},
"content": {
"comm_id": "96fb7d64-2ba7-4468-90ba-66b590fe5d3c",
"data": {
"commandOrEvent": "{\"event\": {\"message\": \"Invalid Identifier: \\\"a.b\\\"\"}, \"eventType\": \"CommandFailed\", \"command\": null}",
"type": "event"
}
},
"buffers": [],
"channel": "shell"
}
]