API Reference¶
CancelableJSFunction
¶
Bases: JSMappedObject
JavaScript function.
This type is returned by JSFunction.cancelable().
You can call this object from Python, passing in positional args to match what the JavaScript function expects. Calls on the Python side are async, regardless of whether the underlying JS function is async (so a call to an async JS function will return a JSPromise, requiring a "double await" in Python to get the result).
Source code in src/py_mini_racer/_types.py
JSArray
¶
Bases: MutableSequence['PythonJSConvertedTypes'], JSObject
JavaScript array.
Has Pythonic MutableSequence methods (e.g., insert(), __getitem__(), ...).
JSArrayIndexError
¶
Bases: IndexError, MiniRacerBaseException
Invalid index into a JSArray.
Source code in src/py_mini_racer/_exc.py
JSEvalException
¶
JSFunction
¶
Bases: JSMappedObject
JavaScript function.
This type is returned by synchronous MiniRacer contexts.
You can call this object from Python, passing in positional args to match what the JavaScript function expects.
In synchronous code you may supply an additional keyword argument, timeout_sec.
If you are running in an async context in the same event loop as Miniracer, you must not supply a non-None value for timeout_sec. Instead call func.cancelable() to obtain a CancellableJSFunction, and use a construct like asyncio.wait_for(...) to apply a timeout.
Source code in src/py_mini_racer/_types.py
JSKeyError
¶
Bases: JSEvalException, KeyError
No such key found.
JSMappedObject
¶
Bases: MutableMapping['PythonJSConvertedTypes', 'PythonJSConvertedTypes'], JSObject
A JavaScript object with Pythonic MutableMapping methods (keys(),
__getitem__(), etc).
keys() and __iter__() will return properties from any prototypes as well as this
object, as if using a for-in statement in JavaScript.
Source code in src/py_mini_racer/_types.py
JSOOMException
¶
Bases: JSEvalException
JavaScript execution ran out of memory.
JSObject
¶
JSParseException
¶
Bases: JSEvalException
JavaScript could not be parsed.
JSPromise
¶
Bases: JSObject
JavaScript Promise.
To get a value, call promise.get() (which blocks) or await promise (in async
code). Both operations will raise a Python exception if the JavaScript Promise is
rejected.
Source code in src/py_mini_racer/_types.py
JSPromiseError
¶
Bases: MiniRacerBaseException
JavaScript rejected a promise.
Source code in src/py_mini_racer/_exc.py
JSSymbol
¶
Bases: JSMappedObject
JavaScript symbol.
JSTimeoutException
¶
Bases: JSEvalException, TimeoutError
JavaScript execution timed out.
Source code in src/py_mini_racer/_exc.py
JSUndefinedType
¶
The JavaScript undefined type.
Where JavaScript null is represented as None, undefined is represented as this type.
Source code in src/py_mini_racer/_types.py
JSValueError
¶
Bases: JSEvalException, ValueError
Bad value passed to JavaScript engine.
LibAlreadyInitializedError
¶
Bases: MiniRacerBaseException
MiniRacer-wrapped V8 build not found.
Source code in src/py_mini_racer/_dll.py
LibNotFoundError
¶
Bases: MiniRacerBaseException
MiniRacer-wrapped V8 build not found.
Source code in src/py_mini_racer/_dll.py
MiniRacer
¶
MiniRacer evaluates JavaScript code using a V8 isolate.
A MiniRacer instance can be explicitly closed using the close() method, or by using the MiniRacer as a context manager, i.e,:
with MiniRacer() as mr: ...
The MiniRacer instance will otherwise clean up the underlying V8 resources upon garbage collection.
Attributes:
| Name | Type | Description |
|---|---|---|
json_impl |
Any
|
JSON module used by helper methods default is json |
Source code in src/py_mini_racer/_mini_racer.py
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 | |
v8_version
property
¶
Return the V8 version string.
call(expr, *args, encoder=None, timeout=None, timeout_sec=None, max_memory=None)
¶
Helper to call a JavaScript function and return compositve types.
The expr argument refers to a JavaScript function in the current V8
isolate context. Further positional arguments are serialized using the JSON
implementation json_impl and passed to the JavaScript function as arguments.
Returned value is serialized to JSON inside the V8 isolate and deserialized
using json_impl.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
expr
|
str
|
JavaScript expression referring to a function |
required |
encoder
|
type[JSONEncoder] | None
|
Custom JSON encoder |
None
|
timeout
|
float | None
|
number of milliseconds after which the execution is interrupted. |
None
|
timeout_sec
|
float | None
|
number of seconds after which the execution is interrupted |
None
|
max_memory
|
int | None
|
hard memory limit, in bytes, after which the execution is interrupted |
None
|
Source code in src/py_mini_racer/_mini_racer.py
close(exc_type=None, exc_val=None, exc_tb=None)
¶
Close this MiniRacer instance.
It is an error to use this MiniRacer instance or any JS objects returned by it after calling this method.
Source code in src/py_mini_racer/_mini_racer.py
eval(code, timeout=None, timeout_sec=None, max_memory=None)
¶
Evaluate JavaScript code in the V8 isolate.
Side effects from the JavaScript evaluation is persisted inside a context (meaning variables set are kept for the next evaluation).
The JavaScript value returned by the last expression in code is converted to
a Python value and returned by this method. Only primitive types are supported
(numbers, strings, buffers...). Use the
py_mini_racer.MiniRacer.execute method to return more complex
types such as arrays or objects.
The evaluation can be interrupted by an exception for several reasons: a limit was reached, the code could not be parsed, a returned value could not be converted to a Python value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
code
|
str
|
JavaScript code |
required |
timeout
|
float | None
|
number of milliseconds after which the execution is interrupted. This is deprecated; use timeout_sec instead. |
None
|
timeout_sec
|
float | None
|
number of seconds after which the execution is interrupted |
None
|
max_memory
|
int | None
|
hard memory limit, in bytes, after which the execution is interrupted. |
None
|
Source code in src/py_mini_racer/_mini_racer.py
eval_cancelable(code)
async
¶
Evaluate JavaScript code in the V8 isolate.
Similar to eval(), but runaway calls can be canceled by canceling the coroutine's task, e.g., using:
await asyncio.wait_for(mr.eval_cancelable(...), timeout=some_timeout)
Source code in src/py_mini_racer/_mini_racer.py
execute(expr, timeout=None, timeout_sec=None, max_memory=None)
¶
Helper to evaluate a JavaScript expression and return composite types.
Returned value is serialized to JSON inside the V8 isolate and deserialized
using json_impl.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
expr
|
str
|
JavaScript expression |
required |
timeout
|
float | None
|
number of milliseconds after which the execution is interrupted. This is deprecated; use timeout_sec instead. |
None
|
timeout_sec
|
float | None
|
number of seconds after which the execution is interrupted |
None
|
max_memory
|
int | None
|
hard memory limit, in bytes, after which the execution is interrupted. |
None
|
Source code in src/py_mini_racer/_mini_racer.py
heap_snapshot()
¶
heap_stats()
¶
low_memory_notification()
¶
set_hard_memory_limit(limit)
¶
Set a hard memory limit on this V8 isolate.
JavaScript execution will be terminated when this limit is reached.
:param int limit: memory limit in bytes or 0 to reset the limit
Source code in src/py_mini_racer/_mini_racer.py
set_soft_memory_limit(limit)
¶
Set a soft memory limit on this V8 isolate.
The Garbage Collection will use a more aggressive strategy when the soft limit is reached but the execution will not be stopped.
:param int limit: memory limit in bytes or 0 to reset the limit
Source code in src/py_mini_racer/_mini_racer.py
was_hard_memory_limit_reached()
¶
Return true if the hard memory limit was reached on the V8 isolate.
was_soft_memory_limit_reached()
¶
Return true if the soft memory limit was reached on the V8 isolate.
wrap_py_function(func)
async
¶
Wrap a Python function such that it can be called from JS.
To be wrapped and exposed in JavaScript, a Python function should:
- Be async,
- Accept variable positional arguments each of type PythonJSConvertedTypes, and
- Return one value of type PythonJSConvertedTypes (a type union which includes None).
The function is rendered on the JavaScript side as an async function (i.e., a function which returns a Promise).
Returns:
| Type | Description |
|---|---|
AsyncGenerator[JSFunction, None]
|
An async context manager which, when entered, yields a JS Function which |
AsyncGenerator[JSFunction, None]
|
can be passed into MiniRacer and called by JS code. |
Source code in src/py_mini_racer/_mini_racer.py
init_mini_racer(*, flags=DEFAULT_V8_FLAGS, ignore_duplicate_init=False)
¶
Initialize py_mini_racer (and V8).
This function can optionally be used to set V8 flags. This function can be called at most once, before any instances of MiniRacer are initialized. Instances of MiniRacer will automatically call this function to initialize MiniRacer and V8.