forked from ObjectProfile/PythonBridge
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathPBExecutionHandler.class.st
More file actions
203 lines (176 loc) · 5.75 KB
/
PBExecutionHandler.class.st
File metadata and controls
203 lines (176 loc) · 5.75 KB
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
Class {
#name : #PBExecutionHandler,
#superclass : #PBHandler,
#instVars : [
'commandQueue',
'weakRegistry',
'objectRegistry',
'promiseRegistry',
'mapperFactory'
],
#category : #'PythonBridge-Execution'
}
{ #category : #'private protocol' }
PBExecutionHandler >> buildMessageFromCommand: command [
| message |
message := PBEnqueueCommandMessage
commandId: command id
statements: command pythonCode
bindings: (command bindings asDictionary collect: [ :obj | self serialize: obj ]).
message addBinding: #pharoCommandId -> (self serialize: command id).
^ message
]
{ #category : #'private protocol' }
PBExecutionHandler >> communicationHandler [
^ self application communicationHandler
]
{ #category : #'private protocol' }
PBExecutionHandler >> deserialize: anObject [
^ mapperFactory deserializer deserialize: anObject
]
{ #category : #'private protocol' }
PBExecutionHandler >> getObserverAtCommandId: commandId id: observerId [
^ (commandQueue getCommand: commandId) getObserver: observerId
]
{ #category : #initialization }
PBExecutionHandler >> initialize [
super initialize.
commandQueue := PBCommandQueue new.
mapperFactory := PBMapperFactory forExecutionHandler: self.
promiseRegistry := PBPromiseRegistry new.
weakRegistry := PBPlatform current weakRegistry.
objectRegistry := PBObjectRegistry new
]
{ #category : #initialization }
PBExecutionHandler >> initializeHandler [
self communicationHandler
addHandler: [ :msg | self notifyHandler: msg ] forMessageClass: PBUpdatePromiseMessage;
addHandler: [ :msg | self notifyErrorHandler: msg ] forMessageClass: PBErrorMessage;
addHandler: [ :msg | self notifyCallbackHandler: msg ] forMessageClass: PBCallbackMessage
]
{ #category : #'private protocol' }
PBExecutionHandler >> mapperFactory [
^ mapperFactory
]
{ #category : #'as yet unclassified' }
PBExecutionHandler >> newCommandFactory [
^ PBCommandFactory new
application: self application;
yourself
]
{ #category : #'as yet unclassified' }
PBExecutionHandler >> newCommandStringFactory [
^ PBCommandStringFactory new
application: self application;
yourself
]
{ #category : #handlers }
PBExecutionHandler >> notifyCallbackHandler: msg [
| val serialization |
val := self
signalObserver: (self getObserverAtCommandId: msg commandId id: msg observerId)
withValue: (self deserialize: msg value).
serialization := [ self serialize: val ] on: Error do: [ :err |
self application log: err printString.
self serialize: nil ].
^ msg createAnswer
value: serialization;
yourself
]
{ #category : #instructions }
PBExecutionHandler >> notifyDebuggerPaused: aDebugger [
"Notify all promises that the server debugger has paused in case the Bloc UI process is waiting on one of the promises."
promiseRegistry notifyDebuggerPaused: aDebugger.
]
{ #category : #handlers }
PBExecutionHandler >> notifyErrorHandler: errorMsg [
| error |
error := PBPythonError new
application: self application;
command: (commandQueue getCommand: errorMsg commandId);
errorMessage: errorMsg errorMessage;
trace: errorMsg trace;
yourself.
self safeTriggerDebugger: error.
^ errorMsg createAnswer
handlingAction: error proceedAction;
yourself
]
{ #category : #handlers }
PBExecutionHandler >> notifyHandler: updatePromiseMsg [
self
updatePromiseId: updatePromiseMsg promiseId
with: (self deserialize: updatePromiseMsg value)
]
{ #category : #initialization }
PBExecutionHandler >> objectRegistry [
^ objectRegistry
]
{ #category : #'private protocol' }
PBExecutionHandler >> primitiveSendCommand: command [
^ self communicationHandler sendMessage: (self buildMessageFromCommand: command)
]
{ #category : #'as yet unclassified' }
PBExecutionHandler >> registerObject: aPythonObject [
objectRegistry registerObject: aPythonObject.
weakRegistry
registerObject: aPythonObject
finalizer: (PBRegistryFinalizer
pythonVariable: aPythonObject pythonVariable
executionHandler: self)
]
{ #category : #'private protocol' }
PBExecutionHandler >> registerPromiseForCommand: aCommand [
| promise |
promise := PBPromise new
id: aCommand id;
transformBlock: aCommand transformBlock;
yourself.
aCommand promise: promise.
promiseRegistry addPromise: promise.
^ promise
]
{ #category : #'as yet unclassified' }
PBExecutionHandler >> safeTriggerDebugger: error [
[ promiseRegistry signalPromiseId: error command id with: error ] fork.
]
{ #category : #'private protocol' }
PBExecutionHandler >> sendCommand: command [
| promise |
self assert: command isValid.
commandQueue enqueueCommand: command.
promise := self registerPromiseForCommand: command.
self primitiveSendCommand: command.
^ promise
]
{ #category : #'private protocol' }
PBExecutionHandler >> serialize: anObject [
^ mapperFactory serializer serialize: anObject
]
{ #category : #'private protocol' }
PBExecutionHandler >> signalObserver: observer withValue: object [
^ observer pharoNotify: object
]
{ #category : #accessing }
PBExecutionHandler >> stop [
super stop.
weakRegistry destroy
]
{ #category : #'as yet unclassified' }
PBExecutionHandler >> triggerDebugger: pythonError [
"This method is called when an error rised on Python execution."
"Inspect error to review the Command that caused the error.
For ignoring error:
pythonError proceedAction: PBIgnore new
For aborting:
pythonError proceedAction: PBDropQueue new
For replacing the command that caused the error:
pythonError proceedAction: P3ReplaceCommandAction new newCommand: **Your crafted command**
To execute your action you just need to 'Proceed' in the debugger."
pythonError signal.
]
{ #category : #'private protocol' }
PBExecutionHandler >> updatePromiseId: id with: anObject [
commandQueue finishCommandId: id.
promiseRegistry removeAndSignalPromiseId: id with: anObject
]