Skip to content

Commit 965b917

Browse files
committed
lets use nb$float, nb$int, and nb$long to better match cpython
1 parent c5d9186 commit 965b917

File tree

7 files changed

+51
-51
lines changed

7 files changed

+51
-51
lines changed

doc/ReferenceManual.rst

+3-3
Original file line numberDiff line numberDiff line change
@@ -253,9 +253,9 @@ Misc
253253
Slot("__invert__", "nb$invert", "unary",
254254
opcode="UNARY_INVERT"),
255255
Slot("__coerce__", "nb$coerce", "coercion"), # not needed
256-
Slot("__int__", "nb$int_", "unary"), # expects exact int as return
257-
Slot("__long__", "nb$lng", "unary"), # expects exact long as return
258-
Slot("__float__", "nb$float_", "unary"), # expects exact float as return
256+
Slot("__int__", "nb$int", "unary"), # expects exact int as return
257+
Slot("__long__", "nb$long", "unary"), # expects exact long as return
258+
Slot("__float__", "nb$float", "unary"), # expects exact float as return
259259
Slot("__oct__", "nb$oct", "unary"),
260260
Slot("__hex__", "nb$hex", "unary"),
261261

src/builtin.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ Sk.builtin.sum = function sum(iter, start) {
350350
if (i.constructor === Sk.builtin.int_) {
351351
tot = tot.nb$add(i);
352352
} else if (i.constructor === Sk.builtin.float_) {
353-
tot = tot.nb$float_().nb$add(i);
353+
tot = tot.nb$float().nb$add(i);
354354
return new Sk.misceval.Break("float");
355355
} else {
356356
tot = Sk.abstr.numberBinOp(tot, i, "Add");

src/complex.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,13 @@ Sk.builtin.complex = Sk.abstr.buildNativeClass("complex", {
5151
},
5252

5353
// number slots
54-
nb$int_() {
54+
nb$int() {
5555
throw new Sk.builtin.TypeError("can't convert complex to int");
5656
},
57-
nb$lng() {
57+
nb$long() {
5858
throw new Sk.builtin.TypeError("can't convert complex to long");
5959
},
60-
nb$float_() {
60+
nb$float() {
6161
throw new Sk.builtin.TypeError("can't convert complex to float");
6262
},
6363
nb$positive() {
@@ -194,8 +194,8 @@ function PyFloat_AsDouble(op) {
194194
let v = op.v;
195195
if (typeof v === "number") {
196196
return v;
197-
} else if (op.nb$float_) {
198-
v = op.nb$float_();
197+
} else if (op.nb$float) {
198+
v = op.nb$float();
199199
}
200200
if (v === undefined) {
201201
throw new Sk.builtin.TypeError("a float is required");
@@ -293,7 +293,7 @@ function complex_from_py(real, imag) {
293293

294294
// just a temporary function to match cpython
295295
function check_number(nb) {
296-
return nb.nb$float_ !== undefined;
296+
return nb.nb$float !== undefined;
297297
}
298298

299299
if (r != null) {

src/float.js

+10-10
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ Sk.builtin.float_ = Sk.abstr.buildNativeClass("float", {
2424
} else if (typeof x === "string") {
2525
// be careful with converting a string as it could result in infinity
2626
this.v = parseFloat(x);
27-
} else if (x.nb$float_) {
28-
return x.nb$float_(); // allow this as a slow path
27+
} else if (x.nb$float) {
28+
return x.nb$float(); // allow this as a slow path
2929
} else {
3030
Sk.asserts.fail("bad argument to float constructor");
3131
}
@@ -40,7 +40,7 @@ Sk.builtin.float_ = Sk.abstr.buildNativeClass("float", {
4040
if (hash !== undefined) {
4141
return hash;
4242
} else if (Number.isInteger(v)) {
43-
hash = this.nb$int_().tp$hash();
43+
hash = this.nb$int().tp$hash();
4444
} else {
4545
hash = Math.floor(Math.random() * Number.MAX_SAFE_INTEGER - Number.MAX_SAFE_INTEGER / 2);
4646
}
@@ -61,8 +61,8 @@ Sk.builtin.float_ = Sk.abstr.buildNativeClass("float", {
6161
// is args always an empty list?
6262
if (arg === undefined) {
6363
x = new Sk.builtin.float_(0.0);
64-
} else if (arg.nb$float_) {
65-
x = arg.nb$float_();
64+
} else if (arg.nb$float) {
65+
x = arg.nb$float();
6666
} else if (Sk.builtin.checkString(arg)) {
6767
x = _str_to_float(arg.v);
6868
}
@@ -79,7 +79,7 @@ Sk.builtin.float_ = Sk.abstr.buildNativeClass("float", {
7979
},
8080

8181
// number slots
82-
nb$int_() {
82+
nb$int() {
8383
let v = this.v;
8484
if (v < 0) {
8585
v = Math.ceil(v);
@@ -95,9 +95,9 @@ Sk.builtin.float_ = Sk.abstr.buildNativeClass("float", {
9595
return new Sk.builtin.int_(JSBI.BigInt(v));
9696
}
9797
},
98-
nb$float_: cloneSelf,
99-
nb$lng() {
100-
return new Sk.builtin.lng(this.nb$int_().v);
98+
nb$float: cloneSelf,
99+
nb$long() {
100+
return new Sk.builtin.lng(this.nb$int().v);
101101
},
102102
nb$add: numberSlot((v, w) => new Sk.builtin.float_(v + w)),
103103

@@ -167,7 +167,7 @@ Sk.builtin.float_ = Sk.abstr.buildNativeClass("float", {
167167
},
168168
__trunc__: {
169169
$meth() {
170-
return this.nb$int_();
170+
return this.nb$int();
171171
},
172172
$flags: { NoArgs: true },
173173
$textsig: "($self, /)",

src/int.js

+9-9
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* If the number is a string then the size will be checked to determined whether it should be a number or BigInt
88
* It assumed that a number passed it is within `Number.MaxSafeInteger`
99
* Similarly if a BigInt is passed it is assumed that this is larger than `Number.MaxSafeInteger`
10-
* Internal code like `float.nb$int_` checks the resulting JS instance before calling `new Sk.builtin.int_`
10+
* Internal code like `float.nb$int` checks the resulting JS instance before calling `new Sk.builtin.int_`
1111
*
1212
* @param {number|JSBI|string=} x
1313
*
@@ -22,8 +22,8 @@ Sk.builtin.int_ = Sk.abstr.buildNativeClass("int", {
2222
v = 0;
2323
} else if (typeof x === "string") {
2424
v = stringToNumberOrBig(x);
25-
} else if (x.nb$int_) {
26-
return x.nb$int_(); // allow this as a slow path
25+
} else if (x.nb$int) {
26+
return x.nb$int(); // allow this as a slow path
2727
} else {
2828
Sk.asserts.fail("bad argument to int constructor");
2929
}
@@ -68,11 +68,11 @@ Sk.builtin.int_ = Sk.abstr.buildNativeClass("int", {
6868
ob$lt: compareSlot((v, w) => v < w, JSBI.lessThan),
6969
ob$le: compareSlot((v, w) => v <= w, JSBI.lessThanOrEqual),
7070

71-
nb$int_: cloneSelf,
71+
nb$int: cloneSelf,
7272
nb$index() {
7373
return this.v;
7474
},
75-
nb$float_() {
75+
nb$float() {
7676
const v = this.v;
7777
if (typeof v === "number") {
7878
return new Sk.builtin.float_(v);
@@ -111,7 +111,7 @@ Sk.builtin.int_ = Sk.abstr.buildNativeClass("int", {
111111
nb$multiply: numberSlot((v, w) => v * w, JSBI.multiply),
112112
nb$divide(other) {
113113
if (Sk.__future__.division) {
114-
return this.nb$float_().nb$divide(other);
114+
return this.nb$float().nb$divide(other);
115115
}
116116
return this.nb$floor_divide(other);
117117
},
@@ -187,7 +187,7 @@ Sk.builtin.int_ = Sk.abstr.buildNativeClass("int", {
187187
}
188188
return Sk.builtin.NotImplemented.NotImplemented$;
189189
},
190-
nb$lng() {
190+
nb$long() {
191191
return new Sk.builtin.lng(this.v);
192192
},
193193
},
@@ -654,8 +654,8 @@ function getInt(x, base) {
654654
return new Sk.builtin.int_(Sk.str2number(x.v, base));
655655
} else if (base !== null) {
656656
throw new Sk.builtin.TypeError("int() can't convert non-string with explicit base");
657-
} else if (x.nb$int_) {
658-
return x.nb$int_();
657+
} else if (x.nb$int) {
658+
return x.nb$int();
659659
}
660660

661661
if ((func = Sk.abstr.lookupSpecial(x, Sk.builtin.str.$trunc))) {

src/lib/math.js

+10-10
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,10 @@ const $builtinmodule = function (name) {
104104
let _x = x.v;
105105
let _y = y.v;
106106
if (typeof _x !== "number") {
107-
_x = x.nb$float_().v;
107+
_x = x.nb$float().v;
108108
}
109109
if (typeof _y !== "number") {
110-
_y = y.nb$float_().v;
110+
_y = y.nb$float().v;
111111
}
112112

113113
if ((_y == Infinity || _y == -Infinity) && isFinite(_x)) {
@@ -168,7 +168,7 @@ const $builtinmodule = function (name) {
168168
i = 0;
169169
let _x = x.v;
170170
if (typeof _x !== "number") {
171-
_x = x.nb$float_().v;
171+
_x = x.nb$float().v;
172172
}
173173
x = _x;
174174
for (let j = 0, len = partials.length; j < len; j++) {
@@ -321,7 +321,7 @@ const $builtinmodule = function (name) {
321321

322322
let _x = x.v;
323323
if (typeof _x !== "number") {
324-
_x = x.nb$float_().v;
324+
_x = x.nb$float().v;
325325
}
326326
const _i = Sk.builtin.asnum$(i);
327327

@@ -373,10 +373,10 @@ const $builtinmodule = function (name) {
373373
let _x = x.v;
374374
let _y = y.v;
375375
if (typeof _x !== "number") {
376-
_x = x.nb$float_().v;
376+
_x = x.nb$float().v;
377377
}
378378
if (typeof _y !== "number") {
379-
_y = y.nb$float_().v;
379+
_y = y.nb$float().v;
380380
}
381381

382382
// deal with most common cases first
@@ -430,7 +430,7 @@ const $builtinmodule = function (name) {
430430
Sk.builtin.pyCheckType("x", "number", Sk.builtin.checkNumber(x));
431431
let _x = x.v;
432432
if (typeof _x !== "number") {
433-
_x = x.nb$float_().v;
433+
_x = x.nb$float().v;
434434
}
435435
if (_x == Infinity || _x == -Infinity || isNaN(_x)) {
436436
return new Sk.builtin.float_(Math.exp(_x));
@@ -501,7 +501,7 @@ const $builtinmodule = function (name) {
501501

502502
let _x = x.v;
503503
if (typeof _x !== "number") {
504-
_x = x.nb$float_().v;
504+
_x = x.nb$float().v;
505505
}
506506

507507
if (_x <= -1.0) {
@@ -568,10 +568,10 @@ const $builtinmodule = function (name) {
568568
let _x = x.v;
569569
let _y = y.v;
570570
if (typeof _x !== "number") {
571-
_x = x.nb$float_().v;
571+
_x = x.nb$float().v;
572572
}
573573
if (typeof _y !== "number") {
574-
_y = y.nb$float_().v;
574+
_y = y.nb$float().v;
575575
}
576576

577577
if (_x == 0 && _y < 0) {

src/slotdefs.js

+12-12
Original file line numberDiff line numberDiff line change
@@ -975,7 +975,7 @@ slots.__delitem__ = {
975975
*
976976
* Examples:
977977
* - [nb$add]{@link Sk.slots.nb$add}
978-
* - [nb$int_]{@link Sk.slots.nb$int_}
978+
* - [nb$int]{@link Sk.slots.nb$int}
979979
* - [nb$divide]{@link Sk.slots.nb$divide} - note we do not use `nb$true_divide`
980980
* - [nb$bool]{@link Sk.slots.nb$bool} - should return a js boolean
981981
*
@@ -1511,13 +1511,13 @@ slots.__ior__ = {
15111511
};
15121512
/**
15131513
* @memberof Sk.slots
1514-
* @method nb$int_
1514+
* @method nb$int
15151515
* @implements __int__
15161516
* @suppress {checkTypes}
15171517
*/
15181518
slots.__int__ = {
15191519
$name: "__int__",
1520-
$slot_name: "nb$int_",
1520+
$slot_name: "nb$int",
15211521
$slot_func: slotFuncNoArgsWithCheck("__int__", Sk.builtin.checkInt, "int"),
15221522
$wrapper: wrapperCallNoArgs,
15231523
$textsig: "($self, /)",
@@ -1526,13 +1526,13 @@ slots.__int__ = {
15261526
};
15271527
/**
15281528
* @memberof Sk.slots
1529-
* @method nb$float_
1529+
* @method nb$float
15301530
* @implements __float__
15311531
* @suppress {checkTypes}
15321532
*/
15331533
slots.__float__ = {
15341534
$name: "__float__",
1535-
$slot_name: "nb$float_",
1535+
$slot_name: "nb$float",
15361536
$slot_func: slotFuncNoArgsWithCheck("__float__", Sk.builtin.checkFloat, "float"),
15371537
$wrapper: wrapperCallNoArgs,
15381538
$textsig: "($self, /)",
@@ -1747,7 +1747,7 @@ slots.__imatmul__ = {
17471747
// py2 ONLY slots
17481748
slots.__long__ = {
17491749
$name: "__long__",
1750-
$slot_name: "nb$lng",
1750+
$slot_name: "nb$long",
17511751
$slot_func: slotFuncNoArgsWithCheck("__long__", Sk.builtin.checkInt, "int"),
17521752
$wrapper: wrapperCallNoArgs,
17531753
$textsig: "($self, /)",
@@ -1843,9 +1843,9 @@ Sk.subSlots = {
18431843
nb$abs: "__abs__",
18441844
nb$negative: "__neg__",
18451845
nb$positive: "__pos__",
1846-
nb$int_: "__int__",
1847-
nb$lng: "__long__",
1848-
nb$float_: "__float__",
1846+
nb$int: "__int__",
1847+
nb$long: "__long__",
1848+
nb$float: "__float__",
18491849
nb$add: "__add__",
18501850
nb$reflected_add: "__radd__",
18511851
nb$inplace_add: "__iadd__",
@@ -2052,8 +2052,8 @@ Sk.dunderToSkulpt = {
20522052
__abs__: "nb$abs",
20532053
__neg__: "nb$negative",
20542054
__pos__: "nb$positive",
2055-
__int__: "nb$int_",
2056-
__float__: "nb$float_",
2055+
__int__: "nb$int",
2056+
__float__: "nb$float",
20572057

20582058
__add__: "nb$add",
20592059
__radd__: "nb$reflected_add",
@@ -2082,7 +2082,7 @@ Sk.dunderToSkulpt = {
20822082

20832083
__bool__: "nb$bool",
20842084
// py2 only
2085-
__long__: "nb$lng",
2085+
__long__: "nb$long",
20862086

20872087
__lshift__: "nb$lshift",
20882088
__rlshift__: "nb$reflected_lshift",

0 commit comments

Comments
 (0)