Skip to content

Commit 10a1f69

Browse files
committed
[GR-29996] Implementation of _json module.
PullRequest: graalpython/1684
2 parents 6889aaa + 456b25f commit 10a1f69

File tree

12 files changed

+1383
-17
lines changed

12 files changed

+1383
-17
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/Python3Core.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,9 @@
124124
import com.oracle.graal.python.builtins.modules.io.StringIOBuiltins;
125125
import com.oracle.graal.python.builtins.modules.io.TextIOBaseBuiltins;
126126
import com.oracle.graal.python.builtins.modules.io.TextIOWrapperBuiltins;
127+
import com.oracle.graal.python.builtins.modules.json.JSONEncoderBuiltins;
128+
import com.oracle.graal.python.builtins.modules.json.JSONModuleBuiltins;
129+
import com.oracle.graal.python.builtins.modules.json.JSONScannerBuiltins;
127130
import com.oracle.graal.python.builtins.modules.lzma.LZMACompressorBuiltins;
128131
import com.oracle.graal.python.builtins.modules.lzma.LZMADecompressorBuiltins;
129132
import com.oracle.graal.python.builtins.modules.lzma.LZMAModuleBuiltins;
@@ -438,6 +441,7 @@ private static PythonBuiltins[] initializeBuiltins(boolean nativeAccessAllowed)
438441
new CollectionsModuleBuiltins(),
439442
new JavaModuleBuiltins(),
440443
new JArrayModuleBuiltins(),
444+
new JSONModuleBuiltins(),
441445
new SREModuleBuiltins(),
442446
new AstModuleBuiltins(),
443447
new SelectModuleBuiltins(),
@@ -496,7 +500,11 @@ private static PythonBuiltins[] initializeBuiltins(boolean nativeAccessAllowed)
496500
new MultiprocessingModuleBuiltins(),
497501
new SemLockBuiltins(),
498502
new WarningsModuleBuiltins(),
499-
new GraalPythonModuleBuiltins()));
503+
new GraalPythonModuleBuiltins(),
504+
505+
// json
506+
new JSONScannerBuiltins(),
507+
new JSONEncoderBuiltins()));
500508
if (hasCoverageTool) {
501509
builtins.add(new TraceModuleBuiltins());
502510
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/PythonBuiltinClassType.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,10 @@ public enum PythonBuiltinClassType implements TruffleObject {
202202
PSSLSocket("_SSLSocket", "_ssl"),
203203
PMemoryBIO("MemoryBIO", "_ssl"),
204204

205+
// json
206+
JSONScanner("Scanner", "_json", Flags.PUBLIC_BASE_WODICT),
207+
JSONEncoder("Encoder", "_json", Flags.PUBLIC_BASE_WODICT),
208+
205209
// Errors and exceptions:
206210

207211
// everything after BaseException is considered to be an exception

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinConstructors.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1158,7 +1158,7 @@ private static Object stringToIntInternal(String num, int base) {
11581158

11591159
private Object stringToInt(VirtualFrame frame, Object cls, String number, int base, Object origObj) {
11601160
if (base == 0 || base == 10) {
1161-
Object value = parseSimpleDecimalLiteral(number);
1161+
Object value = parseSimpleDecimalLiteral(number, 0, number.length());
11621162
if (value != null) {
11631163
return createInt(cls, value);
11641164
}
@@ -1325,23 +1325,23 @@ private static BigInteger asciiToBigInteger(String str, int possibleBase) throws
13251325
* @param arg the string to parse
13261326
* @return parsed integer, long or null if the literal is not simple enough
13271327
*/
1328-
private static Object parseSimpleDecimalLiteral(String arg) {
1329-
if (arg.isEmpty()) {
1328+
public static Object parseSimpleDecimalLiteral(String arg, int offset, int remaining) {
1329+
if (remaining <= 0) {
13301330
return null;
13311331
}
1332-
int start = arg.charAt(0) == '-' ? 1 : 0;
1333-
if (arg.length() <= start || arg.length() > 18 + start) {
1332+
int start = arg.charAt(offset) == '-' ? 1 : 0;
1333+
if (remaining <= start || remaining > 18 + start) {
13341334
return null;
13351335
}
1336-
if (arg.charAt(start) == '0') {
1337-
if (arg.length() > start + 1) {
1336+
if (arg.charAt(start + offset) == '0') {
1337+
if (remaining > start + 1) {
13381338
return null;
13391339
}
13401340
return 0;
13411341
}
13421342
long value = 0;
1343-
for (int i = start; i < arg.length(); i++) {
1344-
char c = arg.charAt(i);
1343+
for (int i = start; i < remaining; i++) {
1344+
char c = arg.charAt(i + offset);
13451345
if (c < '0' || c > '9') {
13461346
return null;
13471347
}

0 commit comments

Comments
 (0)