@@ -115,13 +115,17 @@ public SSTNode parseExpression(String text, PythonSSTNodeFactory nodeFactory) {
115
115
null ).antlrResult ;
116
116
}
117
117
118
- public static byte [] serialize (Source source , SSTNode node , ScopeInfo scope , boolean isModule ) {
118
+ public static byte [] serialize (SourceSection section , SSTNode node , ScopeInfo scope , boolean isModule ) {
119
+ Source source = section .getSource ();
119
120
ByteArrayOutputStream baos = new ByteArrayOutputStream ();
120
121
DataOutputStream dos = new DataOutputStream (baos );
121
122
try {
122
123
dos .writeByte (SerializationUtils .VERSION );
123
124
dos .writeUTF (source .getName () == null ? "" : encodeHome (source .getName ()));
124
125
dos .writeUTF (source .getPath () == null ? "" : encodeHome (source .getPath ()));
126
+ byte [] bytes = section .getCharacters ().toString ().getBytes (StandardCharsets .UTF_8 );
127
+ dos .writeInt (bytes .length );
128
+ dos .write (bytes );
125
129
ScopeInfo .write (dos , scope );
126
130
dos .writeInt (isModule ? 0 : node .getStartOffset ());
127
131
node .accept (new SSTSerializerVisitor (dos ));
@@ -155,20 +159,20 @@ public byte[] serialize(RootNode rootNode) {
155
159
Source source = rootNode .getSourceSection ().getSource ();
156
160
assert source != null ;
157
161
CacheItem lastParserResult = cachedLastAntlrResult ;
158
- if (!source .equals (lastParserResult .source )) {
162
+ if (source != lastParserResult . source && !source .equals (lastParserResult .source )) {
159
163
// we need to parse the source again
160
164
PythonSSTNodeFactory sstFactory = new PythonSSTNodeFactory (PythonLanguage .getCore (), source , this );
161
165
lastParserResult = parseWithANTLR (ParserMode .File , 0 , PythonLanguage .getCore (), sstFactory , source , null , null );
162
166
}
163
167
if (rootNode instanceof ModuleRootNode ) {
164
168
// serialize whole module
165
- return serialize (source , lastParserResult .antlrResult , lastParserResult .globalScope , true );
169
+ return serialize (rootNode . getSourceSection () , lastParserResult .antlrResult , lastParserResult .globalScope , true );
166
170
} else {
167
171
// serialize just the part
168
172
SSTNodeWithScopeFinder finder = new SSTNodeWithScopeFinder (rootNode .getSourceSection ().getCharIndex (), rootNode .getSourceSection ().getCharEndIndex ());
169
173
SSTNodeWithScope rootSST = lastParserResult .antlrResult .accept (finder );
170
174
// store with parent scope
171
- return serialize (source , rootSST , rootSST .getScope ().getParent (), false );
175
+ return serialize (rootNode . getSourceSection () , rootSST , rootSST .getScope ().getParent (), false );
172
176
}
173
177
}
174
178
@@ -195,18 +199,17 @@ public RootNode deserialize(byte[] data, String[] cellvars, String[] freevars) {
195
199
}
196
200
String name = decodeHome (dis .readUTF ()).intern ();
197
201
String path = decodeHome (dis .readUTF ()).intern ();
198
- if (path .isEmpty ()) {
199
- source = Source .newBuilder (PythonLanguage .ID , "" , name ).build ();
200
- } else {
201
- try {
202
- source = Source .newBuilder (PythonLanguage .ID , PythonLanguage .getContext ().getEnv ().getPublicTruffleFile (path )).name (name ).build ();
203
- } catch (IOException e ) {
204
- // error accessing source file, ignore and continue with an empty source
205
- source = Source .newBuilder (PythonLanguage .ID , "" , name ).build ();
206
- }
207
- }
202
+ byte [] bytes = new byte [dis .readInt ()];
203
+ dis .readFully (bytes );
204
+ String contents = new String (bytes , StandardCharsets .UTF_8 );
208
205
globalScope = ScopeInfo .read (dis , null );
209
206
int offset = dis .readInt ();
207
+
208
+ if (path .isEmpty () || offset != 0 ) {
209
+ source = Source .newBuilder (PythonLanguage .ID , contents , name ).build ();
210
+ } else {
211
+ source = Source .newBuilder (PythonLanguage .ID , PythonLanguage .getContext ().getEnv ().getPublicTruffleFile (path )).content (contents ).name (name ).build ();
212
+ }
210
213
sstNode = new SSTDeserializer (dis , globalScope , offset ).readNode ();
211
214
} catch (IOException e ) {
212
215
throw PythonLanguage .getCore ().raise (PythonBuiltinClassType .ValueError , "Is not possible get correct bytecode data %s, %s" , e .getClass ().getSimpleName (), e .getMessage ());
0 commit comments