Skip to content

Commit bea7197

Browse files
committed
Few bz2 fixes
1 parent d13090d commit bea7197

File tree

4 files changed

+34
-38
lines changed

4 files changed

+34
-38
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/bz2/BZ2CompressorBuiltins.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ abstract static class CompressNode extends PythonBinaryBuiltinNode {
124124

125125
@Specialization(guards = {"!self.isFlushed()"})
126126
PBytes doNativeBytes(BZ2Object.BZ2Compressor self, PBytesLike data,
127-
@Shared("c") @CachedContext(PythonLanguage.class) PythonContext ctxt,
127+
@Shared("ct") @CachedContext(PythonLanguage.class) PythonContext ctxt,
128128
@Cached SequenceStorageNodes.GetInternalByteArrayNode toBytes,
129129
@Cached SequenceStorageNodes.LenNode lenNode,
130130
@Shared("c") @Cached Bz2Nodes.Bz2NativeCompress compress) {
@@ -137,7 +137,7 @@ PBytes doNativeBytes(BZ2Object.BZ2Compressor self, PBytesLike data,
137137

138138
@Specialization(guards = {"!self.isFlushed()"})
139139
PBytes doNativeObject(BZ2Object.BZ2Compressor self, Object data,
140-
@Shared("c") @CachedContext(PythonLanguage.class) PythonContext ctxt,
140+
@Shared("ct") @CachedContext(PythonLanguage.class) PythonContext ctxt,
141141
@Cached BytesNodes.ToBytesNode toBytes,
142142
@Shared("c") @Cached Bz2Nodes.Bz2NativeCompress compress) {
143143
synchronized (self) {

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/bz2/BZ2DecompressorBuiltins.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,26 +114,24 @@ protected ArgumentClinicProvider getArgumentClinic() {
114114

115115
@Specialization(guards = {"!self.isEOF()"})
116116
PBytes doNativeBytes(BZ2Object.BZ2Decompressor self, PBytesLike data, int maxLength,
117-
@Shared("c") @CachedContext(PythonLanguage.class) PythonContext ctxt,
118117
@Cached SequenceStorageNodes.GetInternalByteArrayNode toBytes,
119118
@Cached SequenceStorageNodes.LenNode lenNode,
120119
@Shared("d") @Cached Bz2Nodes.Bz2NativeDecompress decompress) {
121120
synchronized (self) {
122121
byte[] bytes = toBytes.execute(data.getSequenceStorage());
123122
int len = lenNode.execute(data.getSequenceStorage());
124-
return factory().createBytes(decompress.execute(self, ctxt, bytes, len, maxLength));
123+
return factory().createBytes(decompress.execute(self, bytes, len, maxLength));
125124
}
126125
}
127126

128127
@Specialization(guards = {"!self.isEOF()"})
129128
PBytes doNativeObject(BZ2Object.BZ2Decompressor self, Object data, int maxLength,
130-
@Shared("c") @CachedContext(PythonLanguage.class) PythonContext ctxt,
131129
@Cached BytesNodes.ToBytesNode toBytes,
132130
@Shared("d") @Cached Bz2Nodes.Bz2NativeDecompress decompress) {
133131
synchronized (self) {
134132
byte[] bytes = toBytes.execute(data);
135133
int len = bytes.length;
136-
return factory().createBytes(decompress.execute(self, ctxt, bytes, len, maxLength));
134+
return factory().createBytes(decompress.execute(self, bytes, len, maxLength));
137135
}
138136
}
139137

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/bz2/BZ2Object.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ public void resizeInputBuffer(int size) {
169169
assert size >= inputBufferSize;
170170
byte[] tmp = new byte[size];
171171
if (inputBuffer != null && getBzsAvailInReal() != 0) {
172-
PythonUtils.arraycopy(inputBuffer, 0, tmp, 0, getBzsAvailInReal());
172+
PythonUtils.arraycopy(inputBuffer, 0, tmp, 0, inputBuffer.length);
173173
}
174174
this.inputBuffer = tmp;
175175
this.inputBufferSize = size;
@@ -187,18 +187,21 @@ public byte[] getNextIn() {
187187
return nextIn;
188188
}
189189

190-
public void setNextIn(byte[] in, PythonContext context) {
190+
public void setNextIn(byte[] in) {
191191
assert in != null;
192192
this.nextIn = in;
193-
this.nextInGuest = context.getEnv().asGuestValue(nextIn);
193+
this.nextInGuest = null;
194194
}
195195

196196
public void clearNextIn() {
197197
this.nextIn = null;
198198
this.nextInGuest = null;
199199
}
200200

201-
public Object getNextInGuest() {
201+
public Object getNextInGuest(PythonContext context) {
202+
if (nextInGuest == null) {
203+
this.nextInGuest = context.getEnv().asGuestValue(nextIn);
204+
}
202205
return nextInGuest;
203206
}
204207

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/bz2/Bz2Nodes.java

Lines changed: 23 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -56,16 +56,17 @@
5656
import static com.oracle.graal.python.runtime.exception.PythonErrorType.RuntimeError;
5757
import static com.oracle.graal.python.runtime.exception.PythonErrorType.ValueError;
5858

59+
import com.oracle.graal.python.PythonLanguage;
5960
import com.oracle.graal.python.builtins.objects.ints.PInt;
60-
import com.oracle.graal.python.nodes.PNodeWithContext;
61+
import com.oracle.graal.python.nodes.PNodeWithRaise;
6162
import com.oracle.graal.python.nodes.PRaiseNode;
6263
import com.oracle.graal.python.runtime.NFIBz2Support;
6364
import com.oracle.graal.python.runtime.NativeLibrary;
6465
import com.oracle.graal.python.runtime.PythonContext;
6566
import com.oracle.graal.python.util.OverflowException;
6667
import com.oracle.graal.python.util.PythonUtils;
6768
import com.oracle.truffle.api.dsl.Cached;
68-
import com.oracle.truffle.api.dsl.GenerateUncached;
69+
import com.oracle.truffle.api.dsl.CachedContext;
6970
import com.oracle.truffle.api.dsl.Specialization;
7071
import com.oracle.truffle.api.nodes.Node;
7172
import com.oracle.truffle.api.profiles.BranchProfile;
@@ -92,8 +93,7 @@ public class Bz2Nodes {
9293
protected static final int BZ_OUTBUFF_FULL = (-8);
9394
protected static final int BZ_CONFIG_ERROR = (-9);
9495

95-
@GenerateUncached
96-
public abstract static class Bz2NativeCompress extends Node {
96+
public abstract static class Bz2NativeCompress extends PNodeWithRaise {
9797

9898
public abstract byte[] execute(BZ2Object.BZ2Compressor self, PythonContext context, byte[] bytes, int len, int action);
9999

@@ -109,26 +109,24 @@ public byte[] flush(BZ2Object.BZ2Compressor self, PythonContext context) {
109109
byte[] nativeCompress(BZ2Object.BZ2Compressor self, PythonContext context, byte[] bytes, int len, int action,
110110
@Cached NativeLibrary.InvokeNativeFunction compress,
111111
@Cached GetOutputNativeBufferNode getBuffer,
112-
@Cached PRaiseNode raiseNode,
113112
@Cached ConditionProfile errProfile) {
114113
NFIBz2Support bz2Support = context.getNFIBz2Support();
115114
Object inGuest = context.getEnv().asGuestValue(bytes);
116115
int err = bz2Support.compress(self.getBzs(), inGuest, len, action, INITIAL_BUFFER_SIZE, compress);
117116
if (errProfile.profile(err != BZ_OK)) {
118-
errorHandling(err, raiseNode);
117+
errorHandling(err, getRaiseNode());
119118
}
120119
return getBuffer.execute(self.getBzs(), context);
121120
}
122121

123122
}
124123

125-
@GenerateUncached
126124
public abstract static class Bz2NativeDecompress extends Node {
127125

128-
public abstract byte[] execute(BZ2Object.BZ2Decompressor self, PythonContext context, byte[] data, int len, int maxLength);
126+
public abstract byte[] execute(BZ2Object.BZ2Decompressor self, byte[] data, int len, int maxLength);
129127

130128
@Specialization
131-
byte[] nativeDecompress(BZ2Object.BZ2Decompressor self, PythonContext context, byte[] bytes, int len, int maxLength,
129+
byte[] nativeDecompress(BZ2Object.BZ2Decompressor self, byte[] bytes, int len, int maxLength,
132130
@Cached Bz2NativeInternalDecompress decompress) {
133131
boolean inputBufferInUse;
134132
/* Prepend unconsumed input if necessary */
@@ -150,23 +148,23 @@ byte[] nativeDecompress(BZ2Object.BZ2Decompressor self, PythonContext context, b
150148
* buffer if realloc fails
151149
*/
152150
self.resizeInputBuffer(newSize);
153-
self.setNextIn(self.getInputBuffer(), context);
151+
self.setNextIn(self.getInputBuffer());
154152
} else if (availNow < len) {
155153
PythonUtils.arraycopy(self.getNextIn(), self.getNextInIndex(), self.getInputBuffer(), 0, self.getBzsAvailInReal());
156-
self.setNextIn(self.getInputBuffer(), context);
154+
self.setNextIn(self.getInputBuffer());
157155
self.setNextInIndex(0);
158156
}
159157
PythonUtils.arraycopy(bytes, 0, self.getNextIn(), self.getNextInIndex() + self.getBzsAvailInReal(), len);
160158
// memcpy((void*)(bzs->next_in + self.getBzsAvailInReal()), data, len);
161159
self.incBzsAvailInReal(len);
162160
inputBufferInUse = true;
163161
} else {
164-
self.setNextIn(bytes, context);
162+
self.setNextIn(bytes);
165163
self.setBzsAvailInReal(len);
166164
inputBufferInUse = false;
167165
}
168166

169-
byte[] result = decompress.execute(self, context, maxLength);
167+
byte[] result = decompress.execute(self, maxLength);
170168

171169
if (self.isEOF()) {
172170
self.setNeedsInput(false);
@@ -202,7 +200,7 @@ byte[] nativeDecompress(BZ2Object.BZ2Decompressor self, PythonContext context, b
202200
/* Copy tail */
203201
// memcpy(d->input_buffer, bzs->next_in, self.getBzsAvailInReal());
204202
PythonUtils.arraycopy(self.getNextIn(), self.getNextInIndex(), self.getInputBuffer(), 0, self.getBzsAvailInReal());
205-
self.setNextIn(self.getInputBuffer(), context);
203+
self.setNextIn(self.getInputBuffer());
206204
self.setNextInIndex(0);
207205
}
208206
}
@@ -211,22 +209,21 @@ byte[] nativeDecompress(BZ2Object.BZ2Decompressor self, PythonContext context, b
211209
}
212210
}
213211

214-
@GenerateUncached
215-
public abstract static class Bz2NativeInternalDecompress extends Node {
212+
public abstract static class Bz2NativeInternalDecompress extends PNodeWithRaise {
216213

217-
public abstract byte[] execute(BZ2Object.BZ2Decompressor self, PythonContext context, int maxLength);
214+
public abstract byte[] execute(BZ2Object.BZ2Decompressor self, int maxLength);
218215

219216
@Specialization
220-
byte[] nativeInternalDecompress(BZ2Object.BZ2Decompressor self, PythonContext context, int maxLength,
217+
byte[] nativeInternalDecompress(BZ2Object.BZ2Decompressor self, int maxLength,
218+
@CachedContext(PythonLanguage.class) PythonContext context,
221219
@Cached NativeLibrary.InvokeNativeFunction decompress,
222220
@Cached NativeLibrary.InvokeNativeFunction getBzsAvailInReal,
223221
@Cached NativeLibrary.InvokeNativeFunction getNextInIndex,
224222
@Cached GetOutputNativeBufferNode getBuffer,
225-
@Cached PRaiseNode raiseNode,
226223
@Cached ConditionProfile errProfile,
227224
@Cached BranchProfile ofProfile) {
228225
NFIBz2Support bz2Support = context.getNFIBz2Support();
229-
Object inGuest = self.getNextInGuest();
226+
Object inGuest = self.getNextInGuest(context);
230227
int offset = self.getNextInIndex();
231228
int err = bz2Support.decompress(self.getBzs(), inGuest, offset, maxLength, INITIAL_BUFFER_SIZE, self.getBzsAvailInReal(), decompress);
232229
long nextInIdx = bz2Support.getNextInIndex(self.getBzs(), getNextInIndex);
@@ -236,35 +233,33 @@ byte[] nativeInternalDecompress(BZ2Object.BZ2Decompressor self, PythonContext co
236233
self.setBzsAvailInReal(bzsAvailInReal);
237234
} catch (OverflowException of) {
238235
ofProfile.enter();
239-
raiseNode.raise(SystemError, VALUE_TOO_LARGE_TO_FIT_INTO_INDEX);
236+
throw raise(SystemError, VALUE_TOO_LARGE_TO_FIT_INTO_INDEX);
240237
}
241238
if (err == BZ_STREAM_END) {
242239
self.setEOF();
243240
} else if (errProfile.profile(err != BZ_OK)) {
244-
errorHandling(err, raiseNode);
241+
errorHandling(err, getRaiseNode());
245242
}
246243
return getBuffer.execute(self.getBzs(), context);
247244
}
248245
}
249246

250-
@GenerateUncached
251-
public abstract static class GetOutputNativeBufferNode extends PNodeWithContext {
247+
public abstract static class GetOutputNativeBufferNode extends PNodeWithRaise {
252248

253249
public abstract byte[] execute(Object bzst, PythonContext context);
254250

255251
@Specialization
256-
static byte[] getBuffer(Object bzst, PythonContext context,
252+
byte[] getBuffer(Object bzst, PythonContext context,
257253
@Cached NativeLibrary.InvokeNativeFunction getBufferSize,
258254
@Cached NativeLibrary.InvokeNativeFunction getBuffer,
259-
@Cached PRaiseNode raiseNode,
260255
@Cached BranchProfile ofProfile) {
261256
NFIBz2Support bz2Support = context.getNFIBz2Support();
262-
int size = 0;
257+
int size;
263258
try {
264259
size = PInt.intValueExact(bz2Support.getOutputBufferSize(bzst, getBufferSize));
265260
} catch (OverflowException of) {
266261
ofProfile.enter();
267-
raiseNode.raise(SystemError, VALUE_TOO_LARGE_TO_FIT_INTO_INDEX);
262+
throw raise(SystemError, VALUE_TOO_LARGE_TO_FIT_INTO_INDEX);
268263
}
269264
if (size == 0) {
270265
return PythonUtils.EMPTY_BYTE_ARRAY;

0 commit comments

Comments
 (0)