Convert MS-format datetimes without platform-bound fromtimestamp (#59)#229
Open
CedricConday wants to merge 1 commit into
Open
Convert MS-format datetimes without platform-bound fromtimestamp (#59)#229CedricConday wants to merge 1 commit into
CedricConday wants to merge 1 commit into
Conversation
…oAPI#59) deserialize_datetime_ms used datetime.fromtimestamp, which is bounded by the platform C library: it raises OSError on Windows for pre-1970 (negative) timestamps and leaks OSError/OverflowError for out-of-range values. Some Xero organisations return such timestamps, turning a normal API call (e.g. get_employees) into an uncaught OSError. Build the datetime from the Unix epoch with timedelta instead, which is platform independent, and surface the ValueError the function already documents for values python's datetime cannot represent. Closes XeroAPI#59.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #59
get_employees(and other calls) intermittently fail for some organisations with:datetime.fromtimestampdelegates to the platform C library and is not portable at the edges:OSErrorfor pre-1970 (negative) timestamps;OSError/OverflowErrorinstead of theValueErrorthis function documents ("Raise ValueError if the input is well formatted but not a valid datetime").Some Xero responses carry such
/Date(...)/values, so the SDK raises an unexpected error type that callers can't sensibly catch.Change
Build the datetime from the Unix epoch with
timedelta, which is platform independent, and coerce genuinely unrepresentable values to the documentedValueError:This is behaviour-preserving for all in-range values (verified against the existing
test_deserialize_datetime_mscases, including the+1300offset).Tests
Added to
test_deserializer.py:test_deserialize_datetime_ms_pre_epoch— a pre-1970 timestamp now converts cleanly (this is the Windows scenario from the issue; it can't fail in Linux CI but is what users hit).test_deserialize_datetime_ms_out_of_range_raises_value_error— an out-of-range timestamp now raises the documentedValueError. This fails onmaster(leaksOSError) and passes with the fix.Full
test_api_clientsuite (149 tests) stays green.