Skip to content

Accept an __index__ object in datetime.utcfromtimestamp - #1343

Open
taxor03 wants to merge 1 commit into
oracle:masterfrom
taxor03:GH-utcfromtimestamp-index
Open

taxor03 wants to merge 1 commit into
oracle:masterfrom
taxor03:GH-utcfromtimestamp-index

Conversation

@taxor03

@taxor03 taxor03 commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

Description

Fixes #1339

datetime.datetime.utcfromtimestamp() accepted only an exact int or float. For anything else it raised a TypeError from a %p message template without the argument, and formatting that message crashed with ArrayIndexOutOfBoundsException. CPython's utcfromtimestamp and fromtimestamp both convert the timestamp with _PyTime_ObjectToTimeval, which uses __index__ for a non-float (pytime.c#L371-L393). GraalPy's fromtimestamp already follows it.

AS-IS

>>> datetime.datetime.utcfromtimestamp(Seconds())   # Seconds().__index__() == 86400
ERROR: java.lang.ArrayIndexOutOfBoundsException: Index 0 out of bounds for length 0

TO-BE

datetime.datetime(1970, 1, 2, 0, 0)

(matching CPython)

Changes

  • In DateTimeBuiltins.UtcFromTimestampNode, convert the timestamp as FromTimestampNode does: PyFloatAsDoubleNode for a float, PyLongAsLongNode otherwise. Other types now raise a normal TypeError. The unused cast nodes and the error message import are removed.
  • Add DateTimeTest.test_utcfromtimestamp_index to tests/test_datetime.py: an __index__ object gives the same datetime as the int, and a str raises TypeError. It passes on CPython 3.12, 3.13 and 3.14.

Testing

mx python-jvm on darwin-aarch64, then mx graalpytest test_datetime (35 passed; run with an English JVM locale) and --tagged test_datetime (3292 passed, 32 failed; master gives the same 32 ZoneInfo failures on this machine). Without the change, the new test crashes the run with the ArrayIndexOutOfBoundsException. mx checkstyle --primary and mx eclipseformat --primary report nothing.

Signed-off-by: Sungmin Park <ryan040@korea.ac.kr>
@oracle-contributor-agreement oracle-contributor-agreement Bot added the OCA Verified All contributors have signed the Oracle Contributor Agreement. label Nov 24, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

OCA Verified All contributors have signed the Oracle Contributor Agreement.

Projects

None yet

1 participant