{"record":{"id":"8437c2df5d335794","repo":"python/cpython","slug":"profiling-trace-placeholder-pattern-not-found-in","errorCode":null,"errorMessage":"profiling_trace: Placeholder pattern not found in {js_path.name}","messagePattern":"profiling_trace: Placeholder pattern not found in (.+?)","errorType":"exception","errorClass":"ExtensionError","httpStatus":null,"severity":"error","filePath":"Doc/tools/extensions/profiling_trace.py","lineNumber":152,"sourceCode":"    trace = generate_trace(DEMO_SOURCE)\n\n    demo_data = {'source': DEMO_SOURCE.rstrip(), 'trace': trace, 'samples': []}\n\n    demo_json = json.dumps(demo_data, indent=2)\n    content = js_path.read_text(encoding='utf-8')\n\n    pattern = r\"(const DEMO_SIMPLE\\s*=\\s*/\\* PROFILING_TRACE_DATA \\*/)[^;]+;\"\n\n    if re.search(pattern, content):\n        content = re.sub(\n            pattern, lambda m: f\"{m.group(1)} {demo_json};\", content\n        )\n        js_path.write_text(content, encoding='utf-8')\n        print(\n            f\"profiling_trace: Injected {len(trace)} trace events into {js_path.name}\"\n        )\n    else:\n        raise ExtensionError(\n            f\"profiling_trace: Placeholder pattern not found in {js_path.name}\"\n        )\n\n\ndef add_assets(app, pagename, templatename, context, doctree):\n    if pagename == 'library/profiling.sampling':\n        app.add_js_file('profiling-sampling-visualization.js')\n        app.add_css_file('profiling-sampling-visualization.css')\n\n\ndef setup(app):\n    app.connect('build-finished', inject_trace)\n    app.connect('html-page-context', add_assets)\n\n    return {\n        'version': '1.0',\n        'parallel_read_safe': True,\n        'parallel_write_safe': True,","sourceCodeStart":134,"sourceCodeEnd":170,"githubUrl":"https://github.com/python/cpython/blob/bc6749cc3b5ae4a5e88a6cc2d5b3bebbe354eae6/Doc/tools/extensions/profiling_trace.py#L134-L170","documentation":"_ensure_fd_no_transport() raises ValueError('Invalid file object: ...') when asked for a file descriptor and the object is neither an int nor something with a working fileno(). This mirrors selectors._fileobj_tofd: the fd must be an integer or expose .fileno() returning one. It is a caller-contract error at the point a socket/fd is registered with the loop.","triggerScenarios":"Passing an already-closed socket whose fileno() returns -1 or raises, a mock/dummy object in tests whose fileno() raises ValueError, a plain object (str, dict) where a socket was expected, or an fd wrapper that raises TypeError in __int__-like conversion. Hit via loop.add_reader/add_writer, sock_* APIs, or transport creation that route through _ensure_fd_no_transport.","commonSituations":"Unit tests with unittest.mock sockets lacking a real fileno(); using closed sockets after close() (fileno() == -1 path differs but similar mocks fail here); passing an SSL object or file wrapper whose fileno is unavailable; refactor changing a parameter from socket to a handler object while call sites were not updated.","solutions":["Pass the real socket object or its integer fd, and verify it is open before use","In tests, give mocks a fileno() returning a valid unique int, or use a real socketpair","Check for closure (sock.fileno() >= 0) and re-establish the connection instead of reusing dead fds","Fix call sites after refactors so the socket itself, not a wrapper, is handed to loop APIs"],"exampleFix":"# before\nloop.add_reader(fake_obj, cb)  # fake_obj has no fileno -> ValueError\n# after\nr, _ = socket.socketpair()\nloop.add_reader(r, cb, r)","handlingStrategy":"validation","validationCode":"def fd_of(obj):\n    if isinstance(obj, int):\n        return obj\n    try:\n        fd = int(obj.fileno())\n    except (AttributeError, TypeError, ValueError):\n        raise ValueError(f'Invalid file object: {obj!r}') from None\n    if fd < 0:\n        raise ValueError(f'file object is closed: {obj!r}')\n    return fd","typeGuard":"def has_valid_fileno(obj) -> bool:\n    try:\n        return int(obj.fileno()) >= 0\n    except (AttributeError, TypeError, ValueError):\n        return False","tryCatchPattern":"try:\n    loop.add_reader(sock, cb)\nexcept ValueError as e:\n    if 'Invalid file object' in str(e):\n        raise TypeError(f'pass an open socket, got {sock!r}') from e\n    raise","preventionTips":["Hand the socket object itself (or its int fd) to loop APIs","Verify fileno() >= 0 before registering; recreate closed connections","Give test mocks a fileno() returning unique positive ints","Update call sites when refactors replace sockets with wrapper objects"],"tags":["asyncio","file-descriptor","validation","selector"],"backgroundTag":null,"analyzedSha":"bc6749cc3b5ae4a5e88a6cc2d5b3bebbe354eae6","analyzedAt":"2026-08-14T22:01:13.976Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}