MCPcopy Index your code
hub / github.com/RustPython/RustPython / _send

Method _send

Lib/multiprocessing/resource_tracker.py:282–313  ·  view source on GitHub ↗
(self, cmd, name, rtype)

Source from the content-addressed store, hash-verified

280 assert nbytes == len(msg), f"{nbytes=} != {len(msg)=}"
281
282 def _send(self, cmd, name, rtype):
283 if self._use_simple_format and '\n' not in name:
284 msg = f"{cmd}:{name}:{rtype}\n".encode("ascii")
285 if len(msg) > 512:
286 # posix guarantees that writes to a pipe of less than PIPE_BUF
287 # bytes are atomic, and that PIPE_BUF >= 512
288 raise ValueError('msg too long')
289 self._ensure_running_and_write(msg)
290 return
291
292 # POSIX guarantees that writes to a pipe of less than PIPE_BUF (512 on Linux)
293 # bytes are atomic. Therefore, we want the message to be shorter than 512 bytes.
294 # POSIX shm_open() and sem_open() require the name, including its leading slash,
295 # to be at most NAME_MAX bytes (255 on Linux)
296 # With json.dump(..., ensure_ascii=True) every non-ASCII byte becomes a 6-char
297 # escape like \uDC80.
298 # As we want the overall message to be kept atomic and therefore smaller than 512,
299 # we encode encode the raw name bytes with URL-safe Base64 - so a 255 long name
300 # will not exceed 340 bytes.
301 b = name.encode('utf-8', 'surrogateescape')
302 if len(b) > 255:
303 raise ValueError('shared memory name too long (max 255 bytes)')
304 b64 = base64.urlsafe_b64encode(b).decode('ascii')
305
306 payload = {"cmd": cmd, "rtype": rtype, "base64_name": b64}
307 msg = (json.dumps(payload, ensure_ascii=True, separators=(",", ":")) + "\n").encode("ascii")
308
309 # The entire JSON message is guaranteed < PIPE_BUF (512 bytes) by construction.
310 assert len(msg) <= 512, f"internal error: message too long ({len(msg)} bytes)"
311 assert msg.startswith(b'{')
312
313 self._ensure_running_and_write(msg)
314
315_resource_tracker = ResourceTracker()
316ensure_running = _resource_tracker.ensure_running

Callers 2

registerMethod · 0.95
unregisterMethod · 0.95

Calls 6

lenFunction · 0.85
encodeMethod · 0.45
decodeMethod · 0.45
dumpsMethod · 0.45
startswithMethod · 0.45

Tested by

no test coverage detected