Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Use CPython-like slot dispatch for unary operators and abs()
-obj / +obj / ~obj / abs(obj) resolved the dunder on the class and called
it with the instance, bypassing the descriptor protocol. A dunder defined
as a zero-argument staticmethod (as in sympy.core.numbers Zero/One/
NegativeOne) then failed with 'takes 0 positional arguments but 1 was
given'. New helper $B.call_special_unary emulates CPython's
_PyObject_LookupSpecial: search_in_mro + tp_descr_get, then call with no
arguments.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
  • Loading branch information
zalo and claude committed Aug 13, 2026
commit a4a8d0536856ef407e4f6e9ada02a0bea0381ec6
8 changes: 6 additions & 2 deletions www/src/ast_to_js.js
Original file line number Diff line number Diff line change
Expand Up @@ -4103,8 +4103,12 @@ $B.ast.UnaryOp.prototype.to_js = function(scopes) {
return -operand + ''
}
}
var method = opclass2dunder[this.op.constructor.$name]
return `$B.$call($B.$getattr($B.get_class(locals.$result = ${operand}), '${method}'), locals.$result)`
var method = opclass2dunder[this.op.constructor.$name],
op_repr = {UAdd: '+', USub: '-', Invert: '~'}[this.op.constructor.$name]
// Use CPython-like slot dispatch: resolve the method on the type with
// the descriptor protocol, so that dunders defined as zero-argument
// staticmethods (as in sympy.core.numbers) are called without arguments.
return `$B.call_special_unary(${operand}, '${method}', 'unary ${op_repr}')`
}

$B.ast.While.prototype.to_js = function(scopes) {
Expand Down
40 changes: 28 additions & 12 deletions www/src/py_builtin_functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,18 +52,9 @@ _b_.__build_class__ = function() {

_b_.abs = function(obj) {
check_nb_args_no_kw('abs', 1, arguments)

var klass = $B.get_class(obj)
try {
var method = $B.$getattr(klass, "__abs__")
} catch (err) {
if ($B.is_exc(err, [_b_.AttributeError])) {
$B.RAISE(_b_.TypeError, "Bad operand type for abs(): '" +
$B.class_name(obj) + "'")
}
throw err
}
return $B.$call(method, obj)
// use CPython-like slot dispatch (descriptor protocol on the type),
// cf. $B.call_special_unary
return $B.call_special_unary(obj, '__abs__', 'abs()')
}

_b_.aiter = function(async_iterable) {
Expand Down Expand Up @@ -712,6 +703,31 @@ $B.search_in_mro = function(klass, attr, _default) {
return _default
}

$B.call_special_unary = function(obj, name, op_repr) {
// Emulates CPython's slot dispatch for unary operations: resolve `name`
// on the type (not the instance), apply the descriptor protocol with the
// instance, then call the result with no argument. In particular, a
// dunder defined as a zero-argument staticmethod is called without the
// instance, like in CPython.
var klass = $B.get_class(obj),
raw = $B.search_in_mro(klass, name, $B.NULL)
if (raw === $B.NULL) {
$B.RAISE(_b_.TypeError,
`bad operand type for ${op_repr}: '${$B.class_name(obj)}'`)
}
if (typeof raw == 'function') {
// fast path: plain method; binding to obj then calling with no
// argument is equivalent to calling with obj
return $B.$call(raw, obj)
}
var descr_get = raw.ob_type && raw.ob_type.tp_descr_get
if (descr_get) {
return $B.$call(descr_get(raw, obj, klass))
}
// non-descriptor class attribute: "bound" to nothing, call with no args
return $B.$call(raw)
}

$B.search_in_dict = function(obj, attr, _default) {
if ($B.get_dict(obj)) {
try {
Expand Down