Repository navigation
Fixes for running SymPy under Brython - #2912
Merged
Merged
Conversation
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>
Contributor
|
Many thanks for the PR Johnathon ! And sorry for the late reaction, August is more dedicated to holidays and family... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
@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:
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
ctypessubset. Together with the recent FAST_ITER fix already on master, they takeimport 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
type.__subclasscheck__only checked direct bases — with a custom metaclass,issubclass(C, A)wasFalseforA ← B ← C, and zero-argsuper()inside__new__raised for such hierarchies:abs()bypassed the descriptor protocol — a dunder defined as a zero-argument staticmethod (as insympy.core.numbers) got called with the instance:exec(src, globals, locals)with two distinct dicts gave defined functions a global namespace that ignored the globals argument:tp_iterwas resolved throughtp_baseonly, 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).dict()from a sequence readitem.length/item[0]as raw JS properties instead of using the iteration protocol on each pair.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 raisedCannot mix BigInt and other types— e.g. anyfunctools.lru_cachekey containing such an int. Element hashes are now reduced withBigInt.asIntN(32, ...)at the mixing sites only, sohash(2**61 - 2) == 2**61 - 2still holds (test_numbers.py passes).Stdlib fixes
math.lcmcrashed on every call ($B.$getattr(product, "__floordiv__")on a JS number).array.arrayraisedIndexErrorfor negative indices (a[-1]).import cmathalways failed: the cosmetic "set all functions as built-in" loop (type(abs)(func)) raises under Brython; it is now skipped when unsupported.Lib/ctypes/__init__.py: fundamentalc_*types,sizeof()/alignment()(wasm32-like sizes),CDLLraisingOSError— enough for pure-Python libraries that only probe the platform word size (sympy needssizeof(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.make_dist.pyrebuild ofbrython.js/brython_stdlib.js.