Utilities

Functions associated with v8codec.jstypes.

jstypes.js_repr_settings

jstypes.js_repr_settings(js_repr=None, *, force_restore=False, **kwargs)

Override the active repr settings for JS types.

This returns a context manager that will restore the previous settings at the end of the context block. The context object is an instance of JSRepr.

Notes

If someone changes the js_repr_settings within your block and your block closes before theirs, your block will emit a JSReprSettingsNotRestored warning and leave the repr settings unchanged. Pass force_restore=True to restore your initial state anyway and not warn.

jstypes.JSRepr

jstypes.JSRepr(self, *, maxjsobject=20, maxjsarray=20, **kwargs)

Generate repr strings for JS types.

This implements the repr strings used by JSObject, JSArray and others, which can be indented and handle cyclic references by substituting ... after several recursive calls.

Warning

Indented reprs are not available before Python 3.12 because JSRepr uses reprlib, which added indented reprs in 3.12.

Attributes

Name Description
fillvalue
indent
maxjsarray
maxjsobject

Methods

Name Description
repr_JSArray
repr_JSArrayBuffer
repr_JSError
repr_JSMap
repr_JSObject
repr_JSSet
repr_bytearray
repr_bytes
repr_memoryview

repr_JSArray

jstypes.JSRepr.repr_JSArray(obj, level)

repr_JSArrayBuffer

jstypes.JSRepr.repr_JSArrayBuffer(obj, level)

repr_JSError

jstypes.JSRepr.repr_JSError(obj, level)

repr_JSMap

jstypes.JSRepr.repr_JSMap(obj, level)

repr_JSObject

jstypes.JSRepr.repr_JSObject(obj, level)

repr_JSSet

jstypes.JSRepr.repr_JSSet(obj, level)

repr_bytearray

jstypes.JSRepr.repr_bytearray(obj, level=1, truncated=None)

repr_bytes

jstypes.JSRepr.repr_bytes(obj, level=1, *, truncated=None)

repr_memoryview

jstypes.JSRepr.repr_memoryview(obj, level)

jstypes.JSReprSettingsNotRestored

jstypes.JSReprSettingsNotRestored()

jstypes.same_value_zero

jstypes.same_value_zero(value)

Get a surrogate value that follows JavaScript same-value-zero equality rules.

Python values can be compared according to same-value-zero by using ==, hash() on the result of calling this function on the values, rather than on the values them directly. Like a key function when sorting.

same_value_zero(x) == same_value_zero(y) is True if x and y are equal under JavaScript’s same-value-zero rule.

Parameters

Name Type Description Default
value object Any Python object required

Returns

Type Description
JSSameValueZero An opaque value that follows the same-value-zero rules when compared with == or passed to hash().

Examples

>>> NaN = float('nan')
>>> NaN == NaN
False
>>> same_value_zero(NaN) == same_value_zero(NaN)
True
>>> True == 1
True
>>> same_value_zero(True) == same_value_zero(1)
False
>>> l1, l2 = [0], [0]
>>> l1 is l2
False
>>> l1 == l2
True
>>> same_value_zero(l1) == same_value_zero(l2)
False
>>> same_value_zero(l1) == same_value_zero(l1)
True

Strings and numbers are equal by value.

>>> s1, s2 = str([ord('a')]), str([ord('a')])
>>> s1 is s2
False
>>> s1 == s2
True
>>> same_value_zero(s1) == same_value_zero(s2)
True
>>> same_value_zero(1.0) == same_value_zero(1)
True

jstypes.JSSameValueZero

jstypes.JSSameValueZero

The type of the opaque values returned by same_value_zero.