Skip to content

Fixes for running SymPy under Brython - #2912

Merged
PierreQuentel merged 12 commits into
brython-dev:masterfrom
zalo:sympy-compat
Aug 23, 2026
Merged

PierreQuentel merged 12 commits into
brython-dev:masterfrom
zalo:sympy-compat

Conversation

@zalo

@zalo zalo commented Aug 13, 2026 •

Copy link
Copy Markdown
Contributor

@PierreQuentel Implemented these for a personal project, but feel free to pick and choose modifications from this omnibus to merge.


Sad News:

Measured head-to-head on this machine for a VS Code Extension:

 ┌───────────────────┬───────────────────┬────────────────────────┐
 │                   │ Pyodide (default) │        Brython         │
 ├───────────────────┼───────────────────┼────────────────────────┤
 │ First evaluation  │ ~1.2 s            │ ~8.3 s (sympy compile) │
 ├───────────────────┼───────────────────┼────────────────────────┤
 │ Warm evaluations  │ near-instant      │ ~0.3 s                 │
 ├───────────────────┼───────────────────┼────────────────────────┤
 │ Resident memory   │ ~220 MB           │ ~642 MB                │
 ├───────────────────┼───────────────────┼────────────────────────┤
 │ Disk in extension │ ~18.5 MB          │ ~29 MB                 │
 └───────────────────┴───────────────────┴────────────────────────┘

Transpiling ~500 SymPy modules to JavaScript makes V8 hold a lot of codegen state — the opposite of the browser intuition, where Brython wins because there's no multi-megabyte WASM download. Both engines drop to ~110 MB baseline after the 5-minute idle shutdown, so the cost only exists while you're actively doing math.


This PR fixes ten runtime/stdlib bugs found while getting SymPy 1.14 to run under Brython, plus adds a minimal ctypes subset. Together with the recent FAST_ITER fix already on master, they take import sympy (pure-Python sources served over HTTP) from failing in 0.3 s to a fully working CAS in the browser — no monkey-patches needed. This picks up where sympy/sympy#18889 left off.

Each commit is one self-contained fix with a minimal repro below. All were found by bisecting real SymPy failures down to pure-Python repros, so none of them are SymPy-specific.

Runtime fixes

  1. type.__subclasscheck__ only checked direct bases — with a custom metaclass, issubclass(C, A) was False for A ← B ← C, and zero-arg super() inside __new__ raised for such hierarchies:
    class Meta(type): pass
    class A(metaclass=Meta): pass
    class B(A): pass
    class C(B): pass
    assert issubclass(C, A)   # was False
  2. Unary operators and abs() bypassed the descriptor protocol — a dunder defined as a zero-argument staticmethod (as in sympy.core.numbers) got called with the instance:
    class W:
        @staticmethod
        def __neg__(): return "ok"
    -W()   # was TypeError: __neg__() takes 0 positional arguments but 1 was given
  3. exec(src, globals, locals) with two distinct dicts gave defined functions a global namespace that ignored the globals argument:
    g = {"V": 42, "__name__": "m"}; l = {}
    exec("def f():\n    return V", g, l)
    l["f"]()   # was NameError: V
  4. tp_iter was resolved through tp_base only, so a dict subclass whose first base is a plain class lost __iter__; and the __getitem__ fallback iterator required __len__ (CPython only needs __getitem__ + IndexError).
  5. dict() from a sequence read item.length / item[0] as raw JS properties instead of using the iteration protocol on each pair.
  6. hash() of ints in (2**53, 2**61) is a BigInt (the correct exact value); tuple hash (y & 0xFFFFFFFF) and frozenset hash (_h << 16) then raised Cannot mix BigInt and other types — e.g. any functools.lru_cache key containing such an int. Element hashes are now reduced with BigInt.asIntN(32, ...) at the mixing sites only, so hash(2**61 - 2) == 2**61 - 2 still holds (test_numbers.py passes).

Stdlib fixes

  1. math.lcm crashed on every call ($B.$getattr(product, "__floordiv__") on a JS number).
  2. array.array raised IndexError for negative indices (a[-1]).
  3. import cmath always failed: the cosmetic "set all functions as built-in" loop (type(abs)(func)) raises under Brython; it is now skipped when unsupported.
  4. New minimal Lib/ctypes/__init__.py: fundamental c_* types, sizeof()/alignment() (wasm32-like sizes), CDLL raising OSError — enough for pure-Python libraries that only probe the platform word size (sympy needs sizeof(c_long)).

Validation

  • import sympy (1.14.0) succeeds in ~6.5 s; integrate/diff/limit/solve/simplify/series/Matrix/latex all return correct results (14-probe battery, 0 failures).
  • www/tests: test_suite, test_rmethods, test_classes, test_descriptors, test_dict, test_exceptions, test_exec, test_generators, test_iterators, test_list, test_numbers, test_types, test_math, test_builtins, test_set, test_strings, test_special_methods all pass.
  • Last commit is the make_dist.py rebuild of brython.js / brython_stdlib.js.

zalo and others added 11 commits August 12, 2026 22:39
issubclass(C, A) returned False for A <- B <- C when A's metaclass is not
type (the custom-metaclass path delegates to type.__subclasscheck__, which
only checked tp_bases). This also broke zero-argument super() inside
__new__ for such hierarchies. Walk the full MRO instead.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
-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>
…cals

Functions, classes and comprehensions defined by exec(src, g, l) got
frames whose globals slot referenced the exec *locals* object, so their
bodies raised NameError for names present in g (breaks mpmath's
exec-generated operators). New make_globals_name() returns the name of the
actual globals object for exec scopes; also used for augmented assignment
to global names, and unbound-name resolution now searches the exec globals
after the locals.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
…lback

1. make_iter followed tp_base only, so a dict subclass whose first base is
a plain class (class PolyElement(DomainElement, ..., dict)) lost dict's
__iter__ and iter() failed. Resolve tp_iter with search_slot (full MRO).
2. The __getitem__ fallback iterator required __len__ and crashed with a
raw JS error when it was missing. CPython semantics: only __getitem__ is
required, and IndexError ends the iteration.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
init_from_list read item.length and item[0]/item[1] as raw JS properties,
so dict() over a sequence of Python sequence objects (e.g. instances with
__len__/__getitem__) failed with 'has length undefined; 2 is required'.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
hash(n) for 2**53 < n < 2**61 is a JS BigInt (the exact value, too large
for Number). tuple hash then evaluated 'y & 0xFFFFFFFF' and frozenset hash
'_h << 16', raising 'Cannot mix BigInt and other types' (breaks
functools.lru_cache keys containing big ints). Reduce bigint element
hashes with BigInt.asIntN(32, ...) before Number mixing.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
$B.$getattr(product, '__floordiv__') on a JS number is not callable; use
$B.rich_op which handles both small ints and long ints.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
a[-1] and a[-1] = v raised IndexError because the index was used directly
as a JS array subscript.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
The 'set all functions as built-in' compliance loop calls
type(abs)(func), which raises TypeError in Brython, so 'import cmath'
always failed (and mpmath/sympy with it). Skip the cosmetic conversion if
the implementation does not support it.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Fundamental c_* types with sizeof()/alignment() (wasm32-like sizes),
CDLL raising OSError. Enough for pure-Python libraries that only probe
the platform word size (e.g. sympy.external needs sizeof(c_long));
loading native libraries stays an explicit error.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Covers: issubclass/super() with custom metaclasses across multiple
inheritance levels, unary dunders as zero-argument staticmethods,
exec() with distinct globals and locals, iteration of dict subclasses
and sequence-protocol objects, dict() from Python pair objects, hashing
tuples/frozensets containing large ints, math.lcm, array negative
indices, cmath and ctypes imports.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
property.$factory inserted positional defaults before forwarding to
tp_init, which shifted a keyword-arguments marker into the fget slot:
property(fset=f) raised "__init__() got multiple values for argument
'fset'" (breaks mpmath 1.4). Forward the arguments unchanged and let
tp_init's argument parser apply the defaults.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@PierreQuentel

Copy link
Copy Markdown
Contributor

Many thanks for the PR Johnathon ! And sorry for the late reaction, August is more dedicated to holidays and family...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants