Skip to content

gh-159446: Avoid TypeError on FileIO construction - #159480

Merged
cmaloney merged 1 commit into
python:mainfrom
cmaloney:fileiospeed_1_typeerror
Nov 3, 2026
Merged

cmaloney merged 1 commit into
python:mainfrom
cmaloney:fileiospeed_1_typeerror

Conversation

@cmaloney

@cmaloney cmaloney commented Oct 15, 2026 •

Copy link
Copy Markdown
Contributor

FileIO can be constructed off a string or an integer file descriptor. It used PyLong_AsInt first to check "is this a integer". That raised a TypeError when passed a string which would then be silenced. Proactively check if it is a number with _PyIndex_Check saving instructions in the common filename path.

Callgrind, instructions per FileIO(path).readall() + close() of a 111-byte file:

Build base this change
PGO+LTO 10,641 8,918
-O3, no PGO 14,154 12,016

pyperf, the same on a 182-byte file, PGO+LTO, 4 interleaved rounds of 10 processes:

Benchmark base this change
fileio_readall 3.71 us 3.62 us: 1.02x faster

FileIO can be constructed off a string or an integer file descriptor.
It used PyLong_AsInt first to check "is this an integer". That raised a
TypeError when passed a string which would then be silenced. Proactively
check if it is a number with _PyIndex_Check saving instructions in the
common filename path.

Callgrind, instructions per `FileIO(path).readall()` + `close()` of a
111-byte file:

| Build            | base   | this change |
|------------------|-------:|------------:|
| PGO+LTO          | 10,641 | 8,918 |
| -O3, no PGO      | 14,154 | 12,016 |

pyperf, the same on a 182-byte file, PGO+LTO, 4 interleaved rounds of
10 processes:

| Benchmark      | base | this change              |
|----------------|:-------:|:---------------------:|
| fileio_readall | 3.71 us | 3.62 us: 1.02x faster |

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@cmaloney
cmaloney force-pushed the fileiospeed_1_typeerror branch from 7305371 to 83216f0 Compare October 15, 2026 18:03
@cmaloney

Copy link
Copy Markdown
Contributor Author

apologies for the force push

@cmaloney cmaloney changed the title gh-159446: Avoid TypeError on FielIO construction gh-159446: Avoid TypeError on FileIO construction Oct 16, 2026
@cmaloney
cmaloney merged commit 4c497b1 into python:main Nov 3, 2026
53 checks passed
@cmaloney
cmaloney deleted the fileiospeed_1_typeerror branch November 3, 2026 20:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant