mirror of
https://github.com/python/cpython
synced 2026-09-29 08:05:30 +03:00
Merged revisions 51434-53004 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r51434 | neal.norwitz | 2006-08-21 20:20:10 +0200 (Mon, 21 Aug 2006) | 1 line Fix a couple of ssize-t issues reported by Alexander Belopolsky on python-dev ........ r51439 | neal.norwitz | 2006-08-21 21:47:08 +0200 (Mon, 21 Aug 2006) | 6 lines Patch #1542451: disallow continue anywhere under a finally I'm undecided if this should be backported to 2.5 or 2.5.1. Armin suggested to wait (I'm of the same opinion). Thomas W thinks it's fine to go in 2.5. ........ r51443 | neal.norwitz | 2006-08-21 22:16:24 +0200 (Mon, 21 Aug 2006) | 4 lines Handle a few more error conditions. Klocwork 301 and 302. Will backport. ........ r51450 | neal.norwitz | 2006-08-22 00:21:19 +0200 (Tue, 22 Aug 2006) | 5 lines Patch #1541585: fix buffer overrun when performing repr() on a unicode string in a build with wide unicode (UCS-4) support. This code could be improved, so add an XXX comment. ........ r51456 | neal.norwitz | 2006-08-22 01:44:48 +0200 (Tue, 22 Aug 2006) | 1 line Try to get the windows bots working again with the new peephole.c ........ r51461 | anthony.baxter | 2006-08-22 09:36:59 +0200 (Tue, 22 Aug 2006) | 1 line patch for documentation for recent uuid changes (from ping) ........ r51473 | neal.norwitz | 2006-08-22 15:56:56 +0200 (Tue, 22 Aug 2006) | 1 line Alexander Belopolsky pointed out that pos is a size_t ........ r51489 | jeremy.hylton | 2006-08-22 22:46:00 +0200 (Tue, 22 Aug 2006) | 2 lines Expose column offset information in parse trees. ........ r51497 | andrew.kuchling | 2006-08-23 01:13:43 +0200 (Wed, 23 Aug 2006) | 1 line Move functional howto into trunk ........ r51515 | jeremy.hylton | 2006-08-23 20:37:43 +0200 (Wed, 23 Aug 2006) | 2 lines Baby steps towards better tests for tokenize ........ r51525 | alex.martelli | 2006-08-23 22:42:02 +0200 (Wed, 23 Aug 2006) | 6 lines x**2 should about equal x*x (including for a float x such that the result is inf) but didn't; added a test to test_float to verify that, and ignored the ERANGE value for errno in the pow operation to make the new test pass (with help from Marilyn Davis at the Google Python Sprint -- thanks!). ........ r51526 | jeremy.hylton | 2006-08-23 23:14:03 +0200 (Wed, 23 Aug 2006) | 20 lines Bug fixes large and small for tokenize. Small: Always generate a NL or NEWLINE token following a COMMENT token. The old code did not generate an NL token if the comment was on a line by itself. Large: The output of untokenize() will now match the input exactly if it is passed the full token sequence. The old, crufty output is still generated if a limited input sequence is provided, where limited means that it does not include position information for tokens. Remaining bug: There is no CONTINUATION token (\) so there is no way for untokenize() to handle such code. Also, expanded the number of doctests in hopes of eventually removing the old-style tests that compare against a golden file. Bug fix candidate for Python 2.5.1. (Sigh.) ........ r51527 | jeremy.hylton | 2006-08-23 23:26:46 +0200 (Wed, 23 Aug 2006) | 5 lines Replace dead code with an assert. Now that COMMENT tokens are reliably followed by NL or NEWLINE, there is never a need to add extra newlines in untokenize. ........ r51530 | alex.martelli | 2006-08-24 00:17:59 +0200 (Thu, 24 Aug 2006) | 7 lines Reverting the patch that tried to fix the issue whereby x**2 raises OverflowError while x*x succeeds and produces infinity; apparently these inconsistencies cannot be fixed across ``all'' platforms and there's a widespread feeling that therefore ``every'' platform should keep suffering forevermore. Ah well. ........ r51565 | thomas.wouters | 2006-08-24 20:40:20 +0200 (Thu, 24 Aug 2006) | 6 lines Fix SF bug #1545837: array.array borks on deepcopy. array.__deepcopy__() needs to take an argument, even if it doesn't actually use it. Will backport to 2.5 and 2.4 (if applicable.) ........ r51580 | martin.v.loewis | 2006-08-25 02:03:34 +0200 (Fri, 25 Aug 2006) | 3 lines Patch #1545507: Exclude ctypes package in Win64 MSI file. Will backport to 2.5. ........ r51589 | neal.norwitz | 2006-08-25 03:52:49 +0200 (Fri, 25 Aug 2006) | 1 line importing types is not necessary if we use isinstance ........ r51604 | thomas.heller | 2006-08-25 09:27:33 +0200 (Fri, 25 Aug 2006) | 3 lines Port _ctypes.pyd to win64 on AMD64. ........ r51605 | thomas.heller | 2006-08-25 09:34:51 +0200 (Fri, 25 Aug 2006) | 3 lines Add missing file for _ctypes.pyd port to win64 on AMD64. ........ r51606 | thomas.heller | 2006-08-25 11:26:33 +0200 (Fri, 25 Aug 2006) | 6 lines Build _ctypes.pyd for win AMD64 into the MSVC project file. Since MSVC doesn't know about .asm files, a helper batch file is needed to find ml64.exe in predefined locations. The helper script hardcodes the path to the MS Platform SDK. ........ r51608 | armin.rigo | 2006-08-25 14:44:28 +0200 (Fri, 25 Aug 2006) | 4 lines The regular expression engine in '_sre' can segfault when interpreting bogus bytecode. It is unclear whether this is a real bug or a "won't fix" case like bogus_code_obj.py. ........ r51617 | tim.peters | 2006-08-26 00:05:39 +0200 (Sat, 26 Aug 2006) | 2 lines Whitespace normalization. ........ r51618 | tim.peters | 2006-08-26 00:06:44 +0200 (Sat, 26 Aug 2006) | 2 lines Add missing svn:eol-style property to text files. ........ r51619 | tim.peters | 2006-08-26 00:26:21 +0200 (Sat, 26 Aug 2006) | 3 lines A new test here relied on preserving invisible trailing whitespace in expected output. Stop that. ........ r51624 | jack.diederich | 2006-08-26 20:42:06 +0200 (Sat, 26 Aug 2006) | 4 lines - Move functions common to all path modules into genericpath.py and have the OS speicifc path modules import them. - Have os2emxpath import common functions fron ntpath instead of using copies ........ r51642 | neal.norwitz | 2006-08-29 07:40:58 +0200 (Tue, 29 Aug 2006) | 1 line Fix a couple of typos. ........ r51647 | marc-andre.lemburg | 2006-08-29 12:34:12 +0200 (Tue, 29 Aug 2006) | 5 lines Fix a buglet in the error reporting (SF bug report #1546372). This should probably go into Python 2.5 or 2.5.1 as well. ........ r51663 | armin.rigo | 2006-08-31 10:51:06 +0200 (Thu, 31 Aug 2006) | 3 lines Doc fix: hashlib objects don't always return a digest of 16 bytes. Backport candidate for 2.5. ........ r51664 | nick.coghlan | 2006-08-31 14:00:43 +0200 (Thu, 31 Aug 2006) | 1 line Fix the wrongheaded implementation of context management in the decimal module and add unit tests. (python-dev discussion is ongoing regarding what we do about Python 2.5) ........ r51665 | nick.coghlan | 2006-08-31 14:51:25 +0200 (Thu, 31 Aug 2006) | 1 line Remove the old decimal context management tests from test_contextlib (guess who didn't run the test suite before committing...) ........ r51669 | brett.cannon | 2006-08-31 20:54:26 +0200 (Thu, 31 Aug 2006) | 4 lines Make sure memory is properly cleaned up in file_init. Backport candidate. ........ r51671 | brett.cannon | 2006-08-31 23:47:52 +0200 (Thu, 31 Aug 2006) | 2 lines Fix comment about indentation level in C files. ........ r51674 | brett.cannon | 2006-09-01 00:42:37 +0200 (Fri, 01 Sep 2006) | 3 lines Have pre-existing C files use 8 spaces indents (to match old PEP 7 style), but have all new files use 4 spaces (to match current PEP 7 style). ........ r51676 | fred.drake | 2006-09-01 05:57:19 +0200 (Fri, 01 Sep 2006) | 3 lines - SF patch #1550263: Enhance and correct unittest docs - various minor cleanups for improved consistency ........ r51677 | georg.brandl | 2006-09-02 00:30:52 +0200 (Sat, 02 Sep 2006) | 2 lines evalfile() should be execfile(). ........ r51681 | neal.norwitz | 2006-09-02 04:43:17 +0200 (Sat, 02 Sep 2006) | 1 line SF #1547931, fix typo (missing and). Will backport to 2.5 ........ r51683 | neal.norwitz | 2006-09-02 04:50:35 +0200 (Sat, 02 Sep 2006) | 1 line Bug #1548092: fix curses.tparm seg fault on invalid input. Needs backport to 2.5.1 and earlier. ........ r51684 | neal.norwitz | 2006-09-02 04:58:13 +0200 (Sat, 02 Sep 2006) | 4 lines Bug #1550714: fix SystemError from itertools.tee on negative value for n. Needs backport to 2.5.1 and earlier. ........ r51685 | nick.coghlan | 2006-09-02 05:54:17 +0200 (Sat, 02 Sep 2006) | 1 line Make decimal.ContextManager a private implementation detail of decimal.localcontext() ........ r51686 | nick.coghlan | 2006-09-02 06:04:18 +0200 (Sat, 02 Sep 2006) | 1 line Further corrections to the decimal module context management documentation ........ r51688 | raymond.hettinger | 2006-09-02 19:07:23 +0200 (Sat, 02 Sep 2006) | 1 line Fix documentation nits for decimal context managers. ........ r51690 | neal.norwitz | 2006-09-02 20:51:34 +0200 (Sat, 02 Sep 2006) | 1 line Add missing word in comment ........ r51691 | neal.norwitz | 2006-09-02 21:40:19 +0200 (Sat, 02 Sep 2006) | 7 lines Hmm, this test has failed at least twice recently on the OpenBSD and Debian sparc buildbots. Since this goes through a lot of tests and hits the disk a lot it could be slow (especially if NFS is involved). I'm not sure if that's the problem, but printing periodic msgs shouldn't hurt. The code was stolen from test_compiler. ........ r51693 | nick.coghlan | 2006-09-03 03:02:00 +0200 (Sun, 03 Sep 2006) | 1 line Fix final documentation nits before backporting decimal module fixes to 2.5 ........ r51694 | nick.coghlan | 2006-09-03 03:06:07 +0200 (Sun, 03 Sep 2006) | 1 line Typo fix for decimal docs ........ r51697 | nick.coghlan | 2006-09-03 03:20:46 +0200 (Sun, 03 Sep 2006) | 1 line NEWS entry on trunk for decimal module changes ........ r51704 | raymond.hettinger | 2006-09-04 17:32:48 +0200 (Mon, 04 Sep 2006) | 1 line Fix endcase for str.rpartition() ........ r51716 | tim.peters | 2006-09-05 04:18:09 +0200 (Tue, 05 Sep 2006) | 12 lines "Conceptual" merge of rev 51711 from the 2.5 branch. i_divmod(): As discussed on Python-Dev, changed the overflow checking to live happily with recent gcc optimizations that assume signed integer arithmetic never overflows. This differs from the corresponding change on the 2.5 and 2.4 branches, using a less obscure approach, but one that /may/ tickle platform idiocies in their definitions of LONG_MIN. The 2.4 + 2.5 change avoided introducing a dependence on LONG_MIN, at the cost of substantially goofier code. ........ r51717 | tim.peters | 2006-09-05 04:21:19 +0200 (Tue, 05 Sep 2006) | 2 lines Whitespace normalization. ........ r51719 | tim.peters | 2006-09-05 04:22:17 +0200 (Tue, 05 Sep 2006) | 2 lines Add missing svn:eol-style property to text files. ........ r51720 | neal.norwitz | 2006-09-05 04:24:03 +0200 (Tue, 05 Sep 2006) | 2 lines Fix SF bug #1546288, crash in dict_equal. ........ r51721 | neal.norwitz | 2006-09-05 04:25:41 +0200 (Tue, 05 Sep 2006) | 1 line Fix SF #1552093, eval docstring typo (3 ps in mapping) ........ r51724 | neal.norwitz | 2006-09-05 04:35:08 +0200 (Tue, 05 Sep 2006) | 1 line This was found by Guido AFAIK on p3yk (sic) branch. ........ r51725 | neal.norwitz | 2006-09-05 04:36:20 +0200 (Tue, 05 Sep 2006) | 1 line Add a NEWS entry for str.rpartition() change ........ r51728 | neal.norwitz | 2006-09-05 04:57:01 +0200 (Tue, 05 Sep 2006) | 1 line Patch #1540470, for OpenBSD 4.0. Backport candidate for 2.[34]. ........ r51729 | neal.norwitz | 2006-09-05 05:53:08 +0200 (Tue, 05 Sep 2006) | 12 lines Bug #1520864 (again): unpacking singleton tuples in list comprehensions and generator expressions (x for x, in ... ) works again. Sigh, I only fixed for loops the first time, not list comps and genexprs too. I couldn't find any more unpacking cases where there is a similar bug lurking. This code should be refactored to eliminate the duplication. I'm sure the listcomp/genexpr code can be refactored. I'm not sure if the for loop can re-use any of the same code though. Will backport to 2.5 (the only place it matters). ........ r51731 | neal.norwitz | 2006-09-05 05:58:26 +0200 (Tue, 05 Sep 2006) | 1 line Add a comment about some refactoring. (There's probably more that should be done.) I will reformat this file in the next checkin due to the inconsistent tabs/spaces. ........ r51732 | neal.norwitz | 2006-09-05 06:00:12 +0200 (Tue, 05 Sep 2006) | 1 line M-x untabify ........ r51737 | hyeshik.chang | 2006-09-05 14:07:09 +0200 (Tue, 05 Sep 2006) | 7 lines Fix a few bugs on cjkcodecs found by Oren Tirosh: - gbk and gb18030 codec now handle U+30FB KATAKANA MIDDLE DOT correctly. - iso2022_jp_2 codec now encodes into G0 for KS X 1001, GB2312 codepoints to conform the standard. - iso2022_jp_3 and iso2022_jp_2004 codec can encode JIS X 2013:2 codepoints now. ........ r51738 | hyeshik.chang | 2006-09-05 14:14:57 +0200 (Tue, 05 Sep 2006) | 2 lines Fix a typo: 2013 -> 0213 ........ r51740 | georg.brandl | 2006-09-05 14:44:58 +0200 (Tue, 05 Sep 2006) | 3 lines Bug #1552618: change docs of dict.has_key() to reflect recommendation to use "in". ........ r51742 | andrew.kuchling | 2006-09-05 15:02:40 +0200 (Tue, 05 Sep 2006) | 1 line Rearrange example a bit, and show rpartition() when separator is not found ........ r51744 | andrew.kuchling | 2006-09-05 15:15:41 +0200 (Tue, 05 Sep 2006) | 1 line [Bug #1525469] SimpleXMLRPCServer still uses the sys.exc_{value,type} module-level globals instead of calling sys.exc_info(). Reported by Russell Warren ........ r51745 | andrew.kuchling | 2006-09-05 15:19:18 +0200 (Tue, 05 Sep 2006) | 3 lines [Bug #1526834] Fix crash in pdb when you do 'b f('; the function name was placed into a regex pattern and the unbalanced paren caused re.compile() to report an error ........ r51751 | kristjan.jonsson | 2006-09-05 19:58:12 +0200 (Tue, 05 Sep 2006) | 6 lines Update the PCBuild8 solution. Facilitate cross-compilation by having binaries in separate Win32 and x64 directories. Rationalized configs by making proper use of platforms/configurations. Remove pythoncore_pgo project. Add new PGIRelease and PGORelease configurations to perform Profile Guided Optimisation. Removed I64 support, but this can be easily added by copying the x64 platform settings. ........ r51758 | gustavo.niemeyer | 2006-09-06 03:58:52 +0200 (Wed, 06 Sep 2006) | 3 lines Fixing #1531862: Do not close standard file descriptors in the subprocess module. ........ r51760 | neal.norwitz | 2006-09-06 05:58:34 +0200 (Wed, 06 Sep 2006) | 1 line Revert 51758 because it broke all the buildbots ........ r51762 | georg.brandl | 2006-09-06 08:03:59 +0200 (Wed, 06 Sep 2006) | 3 lines Bug #1551427: fix a wrong NULL pointer check in the win32 version of os.urandom(). ........ r51765 | georg.brandl | 2006-09-06 08:09:31 +0200 (Wed, 06 Sep 2006) | 3 lines Bug #1550983: emit better error messages for erroneous relative imports (if not in package and if beyond toplevel package). ........ r51767 | neal.norwitz | 2006-09-06 08:28:06 +0200 (Wed, 06 Sep 2006) | 1 line with and as are now keywords. There are some generated files I can't recreate. ........ r51770 | georg.brandl | 2006-09-06 08:50:05 +0200 (Wed, 06 Sep 2006) | 5 lines Bug #1542051: Exceptions now correctly call PyObject_GC_UnTrack. Also make sure that every exception class has __module__ set to 'exceptions'. ........ r51785 | georg.brandl | 2006-09-06 22:05:58 +0200 (Wed, 06 Sep 2006) | 2 lines Fix missing import of the types module in logging.config. ........ r51789 | marc-andre.lemburg | 2006-09-06 22:40:22 +0200 (Wed, 06 Sep 2006) | 3 lines Add news item for bug fix of SF bug report #1546372. ........ r51797 | gustavo.niemeyer | 2006-09-07 02:48:33 +0200 (Thu, 07 Sep 2006) | 3 lines Fixed subprocess bug #1531862 again, after removing tests offending buildbot ........ r51798 | raymond.hettinger | 2006-09-07 04:42:48 +0200 (Thu, 07 Sep 2006) | 1 line Fix refcounts and add error checks. ........ r51803 | nick.coghlan | 2006-09-07 12:50:34 +0200 (Thu, 07 Sep 2006) | 1 line Fix the speed regression in inspect.py by adding another cache to speed up getmodule(). Patch #1553314 ........ r51805 | ronald.oussoren | 2006-09-07 14:03:10 +0200 (Thu, 07 Sep 2006) | 2 lines Fix a glaring error and update some version numbers. ........ r51814 | andrew.kuchling | 2006-09-07 15:56:23 +0200 (Thu, 07 Sep 2006) | 1 line Typo fix ........ r51815 | andrew.kuchling | 2006-09-07 15:59:38 +0200 (Thu, 07 Sep 2006) | 8 lines [Bug #1552726] Avoid repeatedly polling in interactive mode -- only put a timeout on the select() if an input hook has been defined. Patch by Richard Boulton. This select() code is only executed with readline 2.1, or if READLINE_CALLBACKS is defined. Backport candidate for 2.5, 2.4, probably earlier versions too. ........ r51816 | armin.rigo | 2006-09-07 17:06:00 +0200 (Thu, 07 Sep 2006) | 2 lines Add a warning notice on top of the generated grammar.txt. ........ r51819 | thomas.heller | 2006-09-07 20:56:28 +0200 (Thu, 07 Sep 2006) | 5 lines Anonymous structure fields that have a bit-width specified did not work, and they gave a strange error message from PyArg_ParseTuple: function takes exactly 2 arguments (3 given). With tests. ........ r51820 | thomas.heller | 2006-09-07 21:09:54 +0200 (Thu, 07 Sep 2006) | 4 lines The cast function did not accept c_char_p or c_wchar_p instances as first argument, and failed with a 'bad argument to internal function' error message. ........ r51827 | nick.coghlan | 2006-09-08 12:04:38 +0200 (Fri, 08 Sep 2006) | 1 line Add missing NEWS entry for rev 51803 ........ r51828 | andrew.kuchling | 2006-09-08 15:25:23 +0200 (Fri, 08 Sep 2006) | 1 line Add missing word ........ r51829 | andrew.kuchling | 2006-09-08 15:35:49 +0200 (Fri, 08 Sep 2006) | 1 line Explain SQLite a bit more clearly ........ r51830 | andrew.kuchling | 2006-09-08 15:36:36 +0200 (Fri, 08 Sep 2006) | 1 line Explain SQLite a bit more clearly ........ r51832 | andrew.kuchling | 2006-09-08 16:02:45 +0200 (Fri, 08 Sep 2006) | 1 line Use native SQLite types ........ r51833 | andrew.kuchling | 2006-09-08 16:03:01 +0200 (Fri, 08 Sep 2006) | 1 line Use native SQLite types ........ r51835 | andrew.kuchling | 2006-09-08 16:05:10 +0200 (Fri, 08 Sep 2006) | 1 line Fix typo in example ........ r51837 | brett.cannon | 2006-09-09 09:11:46 +0200 (Sat, 09 Sep 2006) | 6 lines Remove the __unicode__ method from exceptions. Allows unicode() to be called on exception classes. Would require introducing a tp_unicode slot to make it work otherwise. Fixes bug #1551432 and will be backported. ........ r51854 | neal.norwitz | 2006-09-11 06:24:09 +0200 (Mon, 11 Sep 2006) | 8 lines Forward port of 51850 from release25-maint branch. As mentioned on python-dev, reverting patch #1504333 because it introduced an infinite loop in rev 47154. This patch also adds a test to prevent the regression. ........ r51855 | neal.norwitz | 2006-09-11 06:28:16 +0200 (Mon, 11 Sep 2006) | 5 lines Properly handle a NULL returned from PyArena_New(). (Also fix some whitespace) Klocwork #364. ........ r51856 | neal.norwitz | 2006-09-11 06:32:57 +0200 (Mon, 11 Sep 2006) | 1 line Add a "crasher" taken from the sgml bug report referenced in the comment ........ r51858 | georg.brandl | 2006-09-11 11:38:35 +0200 (Mon, 11 Sep 2006) | 12 lines Forward-port of rev. 51857: Building with HP's cc on HP-UX turned up a couple of problems. _PyGILState_NoteThreadState was declared as static inconsistently. Make it static as it's not necessary outside of this module. Some tests failed because errno was reset to 0. (I think the tests that failed were at least: test_fcntl and test_mailbox). Ensure that errno doesn't change after a call to Py_END_ALLOW_THREADS. This only affected debug builds. ........ r51865 | martin.v.loewis | 2006-09-12 21:49:20 +0200 (Tue, 12 Sep 2006) | 2 lines Forward-port 51862: Add sgml_input.html. ........ r51866 | andrew.kuchling | 2006-09-12 22:50:23 +0200 (Tue, 12 Sep 2006) | 1 line Markup typo fix ........ r51867 | andrew.kuchling | 2006-09-12 23:09:02 +0200 (Tue, 12 Sep 2006) | 1 line Some editing, markup fixes ........ r51868 | andrew.kuchling | 2006-09-12 23:21:51 +0200 (Tue, 12 Sep 2006) | 1 line More wordsmithing ........ r51877 | andrew.kuchling | 2006-09-14 13:22:18 +0200 (Thu, 14 Sep 2006) | 1 line Make --help mention that -v can be supplied multiple times ........ r51878 | andrew.kuchling | 2006-09-14 13:28:50 +0200 (Thu, 14 Sep 2006) | 1 line Rewrite help message to remove some of the parentheticals. (There were a lot of them.) ........ r51883 | ka-ping.yee | 2006-09-15 02:34:19 +0200 (Fri, 15 Sep 2006) | 2 lines Fix grammar errors and improve clarity. ........ r51885 | georg.brandl | 2006-09-15 07:22:24 +0200 (Fri, 15 Sep 2006) | 3 lines Correct elementtree module index entry. ........ r51889 | fred.drake | 2006-09-15 17:18:04 +0200 (Fri, 15 Sep 2006) | 4 lines - fix module name in links in formatted documentation - minor markup cleanup (forward-ported from release25-maint revision 51888) ........ r51891 | fred.drake | 2006-09-15 18:11:27 +0200 (Fri, 15 Sep 2006) | 3 lines revise explanation of returns_unicode to reflect bool values and to include the default value (merged from release25-maint revision 51890) ........ r51897 | martin.v.loewis | 2006-09-16 19:36:37 +0200 (Sat, 16 Sep 2006) | 2 lines Patch #1557515: Add RLIMIT_SBSIZE. ........ r51903 | ronald.oussoren | 2006-09-17 20:42:53 +0200 (Sun, 17 Sep 2006) | 2 lines Port of revision 51902 in release25-maint to the trunk ........ r51904 | ronald.oussoren | 2006-09-17 21:23:27 +0200 (Sun, 17 Sep 2006) | 3 lines Tweak Mac/Makefile in to ensure that pythonw gets rebuild when the major version of python changes (2.5 -> 2.6). Bug #1552935. ........ r51913 | guido.van.rossum | 2006-09-18 23:36:16 +0200 (Mon, 18 Sep 2006) | 2 lines Make this thing executable. ........ r51920 | gregory.p.smith | 2006-09-19 19:35:04 +0200 (Tue, 19 Sep 2006) | 5 lines Fixes a bug with bsddb.DB.stat where the flags and txn keyword arguments are transposed. (reported by Louis Zechtzer) ..already committed to release24-maint ..needs committing to release25-maint ........ r51926 | brett.cannon | 2006-09-20 20:34:28 +0200 (Wed, 20 Sep 2006) | 3 lines Accidentally didn't commit Misc/NEWS entry on when __unicode__() was removed from exceptions. ........ r51927 | brett.cannon | 2006-09-20 20:43:13 +0200 (Wed, 20 Sep 2006) | 6 lines Allow exceptions to be directly sliced again (e.g., ``BaseException(1,2,3)[0:2]``). Discovered in Python 2.5.0 by Thomas Heller and reported to python-dev. This should be backported to 2.5 . ........ r51928 | brett.cannon | 2006-09-20 21:28:35 +0200 (Wed, 20 Sep 2006) | 2 lines Make python.vim output more deterministic. ........ r51949 | walter.doerwald | 2006-09-21 17:09:55 +0200 (Thu, 21 Sep 2006) | 2 lines Fix typo. ........ r51950 | jack.diederich | 2006-09-21 19:50:26 +0200 (Thu, 21 Sep 2006) | 5 lines * regression bug, count_next was coercing a Py_ssize_t to an unsigned Py_size_t which breaks negative counts * added test for negative numbers will backport to 2.5.1 ........ r51953 | jack.diederich | 2006-09-21 22:34:49 +0200 (Thu, 21 Sep 2006) | 1 line added itertools.count(-n) fix ........ r51971 | neal.norwitz | 2006-09-22 10:16:26 +0200 (Fri, 22 Sep 2006) | 10 lines Fix %zd string formatting on Mac OS X so it prints negative numbers. In addition to testing positive numbers, verify negative numbers work in configure. In order to avoid compiler warnings on OS X 10.4, also change the order of the check for the format character to use (PY_FORMAT_SIZE_T) in the sprintf format for Py_ssize_t. This patch changes PY_FORMAT_SIZE_T from "" to "l" if it wasn't defined at configure time. Need to verify the buildbot results. Backport candidate (if everyone thinks this patch can't be improved). ........ r51972 | neal.norwitz | 2006-09-22 10:18:10 +0200 (Fri, 22 Sep 2006) | 7 lines Bug #1557232: fix seg fault with def f((((x)))) and def f(((x),)). These tests should be improved. Hopefully this fixes variations when flipping back and forth between fpdef and fplist. Backport candidate. ........ r51975 | neal.norwitz | 2006-09-22 10:47:23 +0200 (Fri, 22 Sep 2006) | 4 lines Mostly revert this file to the same version as before. Only force setting of PY_FORMAT_SIZE_T to "l" for Mac OSX. I don't know a better define to use. This should get rid of the warnings on other platforms and Mac too. ........ r51986 | fred.drake | 2006-09-23 02:26:31 +0200 (Sat, 23 Sep 2006) | 1 line add boilerplate "What's New" document so the docs will build ........ r51987 | neal.norwitz | 2006-09-23 06:11:38 +0200 (Sat, 23 Sep 2006) | 1 line Remove extra semi-colons reported by Johnny Lee on python-dev. Backport if anyone cares. ........ r51989 | neal.norwitz | 2006-09-23 20:11:58 +0200 (Sat, 23 Sep 2006) | 1 line SF Bug #1563963, add missing word and cleanup first sentance ........ r51990 | brett.cannon | 2006-09-23 21:53:20 +0200 (Sat, 23 Sep 2006) | 3 lines Make output on test_strptime() be more verbose in face of failure. This is in hopes that more information will help debug the failing test on HPPA Ubuntu. ........ r51991 | georg.brandl | 2006-09-24 12:36:01 +0200 (Sun, 24 Sep 2006) | 2 lines Fix webbrowser.BackgroundBrowser on Windows. ........ r51993 | georg.brandl | 2006-09-24 14:35:36 +0200 (Sun, 24 Sep 2006) | 4 lines Fix a bug in the parser's future statement handling that led to "with" not being recognized as a keyword after, e.g., this statement: from __future__ import division, with_statement ........ r51995 | georg.brandl | 2006-09-24 14:50:24 +0200 (Sun, 24 Sep 2006) | 4 lines Fix a bug in traceback.format_exception_only() that led to an error being raised when print_exc() was called without an exception set. In version 2.4, this printed "None", restored that behavior. ........ r52000 | armin.rigo | 2006-09-25 17:16:26 +0200 (Mon, 25 Sep 2006) | 2 lines Another crasher. ........ r52011 | brett.cannon | 2006-09-27 01:38:24 +0200 (Wed, 27 Sep 2006) | 2 lines Make the error message for when the time data and format do not match clearer. ........ r52014 | andrew.kuchling | 2006-09-27 18:37:30 +0200 (Wed, 27 Sep 2006) | 1 line Add news item for rev. 51815 ........ r52018 | andrew.kuchling | 2006-09-27 21:23:05 +0200 (Wed, 27 Sep 2006) | 1 line Make examples do error checking on Py_InitModule ........ r52032 | brett.cannon | 2006-09-29 00:10:14 +0200 (Fri, 29 Sep 2006) | 2 lines Very minor grammatical fix in a comment. ........ r52048 | george.yoshida | 2006-09-30 07:14:02 +0200 (Sat, 30 Sep 2006) | 4 lines SF bug #1567976 : fix typo Will backport to 2.5. ........ r52051 | gregory.p.smith | 2006-09-30 08:08:20 +0200 (Sat, 30 Sep 2006) | 2 lines wording change ........ r52053 | georg.brandl | 2006-09-30 09:24:48 +0200 (Sat, 30 Sep 2006) | 2 lines Bug #1567375: a minor logical glitch in example description. ........ r52056 | georg.brandl | 2006-09-30 09:31:57 +0200 (Sat, 30 Sep 2006) | 3 lines Bug #1565661: in webbrowser, split() the command for the default GNOME browser in case it is a command with args. ........ r52058 | georg.brandl | 2006-09-30 10:43:30 +0200 (Sat, 30 Sep 2006) | 4 lines Patch #1567691: super() and new.instancemethod() now don't accept keyword arguments any more (previously they accepted them, but didn't use them). ........ r52061 | georg.brandl | 2006-09-30 11:03:42 +0200 (Sat, 30 Sep 2006) | 3 lines Bug #1566800: make sure that EnvironmentError can be called with any number of arguments, as was the case in Python 2.4. ........ r52063 | georg.brandl | 2006-09-30 11:06:45 +0200 (Sat, 30 Sep 2006) | 2 lines Bug #1566663: remove obsolete example from datetime docs. ........ r52065 | georg.brandl | 2006-09-30 11:13:21 +0200 (Sat, 30 Sep 2006) | 3 lines Bug #1566602: correct failure of posixpath unittest when $HOME ends with a slash. ........ r52068 | georg.brandl | 2006-09-30 12:58:01 +0200 (Sat, 30 Sep 2006) | 3 lines Bug #1457823: cgi.(Sv)FormContentDict's constructor now takes keep_blank_values and strict_parsing keyword arguments. ........ r52069 | georg.brandl | 2006-09-30 13:06:47 +0200 (Sat, 30 Sep 2006) | 3 lines Bug #1560617: in pyclbr, return full module name not only for classes, but also for functions. ........ r52072 | georg.brandl | 2006-09-30 13:17:34 +0200 (Sat, 30 Sep 2006) | 3 lines Bug #1556784: allow format strings longer than 127 characters in datetime's strftime function. ........ r52075 | georg.brandl | 2006-09-30 13:22:28 +0200 (Sat, 30 Sep 2006) | 3 lines Bug #1446043: correctly raise a LookupError if an encoding name given to encodings.search_function() contains a dot. ........ r52078 | georg.brandl | 2006-09-30 14:02:57 +0200 (Sat, 30 Sep 2006) | 3 lines Bug #1546052: clarify that PyString_FromString(AndSize) copies the string pointed to by its parameter. ........ r52080 | georg.brandl | 2006-09-30 14:16:03 +0200 (Sat, 30 Sep 2006) | 3 lines Convert test_import to unittest. ........ r52083 | kurt.kaiser | 2006-10-01 23:16:45 +0200 (Sun, 01 Oct 2006) | 5 lines Some syntax errors were being caught by tokenize during the tabnanny check, resulting in obscure error messages. Do the syntax check first. Bug 1562716, 1562719 ........ r52084 | kurt.kaiser | 2006-10-01 23:54:37 +0200 (Sun, 01 Oct 2006) | 3 lines Add comment explaining that error msgs may be due to user code when running w/o subprocess. ........ r52086 | martin.v.loewis | 2006-10-02 16:55:51 +0200 (Mon, 02 Oct 2006) | 3 lines Fix test for uintptr_t. Fixes #1568842. Will backport. ........ r52089 | martin.v.loewis | 2006-10-02 17:20:37 +0200 (Mon, 02 Oct 2006) | 3 lines Guard uintptr_t test with HAVE_STDINT_H, test for stdint.h. Will backport. ........ r52100 | vinay.sajip | 2006-10-03 20:02:37 +0200 (Tue, 03 Oct 2006) | 1 line Documentation omitted the additional parameter to LogRecord.__init__ which was added in 2.5. (See SF #1569622). ........ r52101 | vinay.sajip | 2006-10-03 20:20:26 +0200 (Tue, 03 Oct 2006) | 1 line Documentation clarified to mention optional parameters. ........ r52102 | vinay.sajip | 2006-10-03 20:21:56 +0200 (Tue, 03 Oct 2006) | 1 line Modified LogRecord.__init__ to make the func parameter optional. (See SF #1569622). ........ r52121 | brett.cannon | 2006-10-03 23:58:55 +0200 (Tue, 03 Oct 2006) | 2 lines Fix minor typo in a comment. ........ r52123 | brett.cannon | 2006-10-04 01:23:14 +0200 (Wed, 04 Oct 2006) | 2 lines Convert test_imp over to unittest. ........ r52128 | barry.warsaw | 2006-10-04 04:06:36 +0200 (Wed, 04 Oct 2006) | 3 lines decode_rfc2231(): As Christian Robottom Reis points out, it makes no sense to test for parts > 3 when we use .split(..., 2). ........ r52129 | jeremy.hylton | 2006-10-04 04:24:52 +0200 (Wed, 04 Oct 2006) | 9 lines Fix for SF bug 1569998: break permitted inside try. The compiler was checking that there was something on the fblock stack, but not that there was a loop on the stack. Fixed that and added a test for the specific syntax error. Bug fix candidate. ........ r52130 | martin.v.loewis | 2006-10-04 07:47:34 +0200 (Wed, 04 Oct 2006) | 4 lines Fix integer negation and absolute value to not rely on undefined behaviour of the C compiler anymore. Will backport to 2.5 and 2.4. ........ r52135 | martin.v.loewis | 2006-10-04 11:21:20 +0200 (Wed, 04 Oct 2006) | 1 line Forward port r52134: Add uuids for 2.4.4. ........ r52137 | armin.rigo | 2006-10-04 12:23:57 +0200 (Wed, 04 Oct 2006) | 3 lines Compilation problem caused by conflicting typedefs for uint32_t (unsigned long vs. unsigned int). ........ r52139 | armin.rigo | 2006-10-04 14:17:45 +0200 (Wed, 04 Oct 2006) | 23 lines Forward-port of r52136,52138: a review of overflow-detecting code. * unified the way intobject, longobject and mystrtoul handle values around -sys.maxint-1. * in general, trying to entierely avoid overflows in any computation involving signed ints or longs is extremely involved. Fixed a few simple cases where a compiler might be too clever (but that's all guesswork). * more overflow checks against bad data in marshal.c. * 2.5 specific: fixed a number of places that were still confusing int and Py_ssize_t. Some of them could potentially have caused "real-world" breakage. * list.pop(x): fixing overflow issues on x was messy. I just reverted to PyArg_ParseTuple("n"), which does the right thing. (An obscure test was trying to give a Decimal to list.pop()... doesn't make sense any more IMHO) * trying to write a few tests... ........ r52147 | andrew.kuchling | 2006-10-04 15:42:43 +0200 (Wed, 04 Oct 2006) | 6 lines Cause a PyObject_Malloc() failure to trigger a MemoryError, and then add 'if (PyErr_Occurred())' checks to various places so that NULL is returned properly. 2.4 backport candidate. ........ r52148 | martin.v.loewis | 2006-10-04 17:25:28 +0200 (Wed, 04 Oct 2006) | 1 line Add MSVC8 project files to create wininst-8.exe. ........ r52196 | brett.cannon | 2006-10-06 00:02:31 +0200 (Fri, 06 Oct 2006) | 7 lines Clarify what "re-initialization" means for init_builtin() and init_dynamic(). Also remove warning about re-initialization as possibly raising an execption as both call _PyImport_FindExtension() which pulls any module that was already imported from the Python process' extension cache and just copies the __dict__ into the module stored in sys.modules. ........ r52200 | fred.drake | 2006-10-06 02:03:45 +0200 (Fri, 06 Oct 2006) | 3 lines - update links - remove Sleepycat name now that they have been bought ........ r52204 | andrew.kuchling | 2006-10-06 12:41:01 +0200 (Fri, 06 Oct 2006) | 1 line Case fix ........ r52208 | georg.brandl | 2006-10-06 14:46:08 +0200 (Fri, 06 Oct 2006) | 3 lines Fix name. ........ r52211 | andrew.kuchling | 2006-10-06 15:18:26 +0200 (Fri, 06 Oct 2006) | 1 line [Bug #1545341] Allow 'classifier' parameter to be a tuple as well as a list. Will backport. ........ r52212 | armin.rigo | 2006-10-06 18:33:22 +0200 (Fri, 06 Oct 2006) | 4 lines A very minor bug fix: this code looks like it is designed to accept any hue value and do the modulo itself, except it doesn't quite do it in all cases. At least, the "cannot get here" comment was wrong. ........ r52213 | andrew.kuchling | 2006-10-06 20:51:55 +0200 (Fri, 06 Oct 2006) | 1 line Comment grammar ........ r52218 | skip.montanaro | 2006-10-07 13:05:02 +0200 (Sat, 07 Oct 2006) | 6 lines Note that the excel_tab class is registered as the "excel-tab" dialect. Fixes 1572471. Make a similar change for the excel class and clean up references to the Dialects and Formatting Parameters section in a few places. ........ r52221 | georg.brandl | 2006-10-08 09:11:54 +0200 (Sun, 08 Oct 2006) | 3 lines Add missing NEWS entry for rev. 52129. ........ r52223 | hyeshik.chang | 2006-10-08 15:48:34 +0200 (Sun, 08 Oct 2006) | 3 lines Bug #1572832: fix a bug in ISO-2022 codecs which may cause segfault when encoding non-BMP unicode characters. (Submitted by Ray Chason) ........ r52227 | ronald.oussoren | 2006-10-08 19:37:58 +0200 (Sun, 08 Oct 2006) | 4 lines Add version number to the link to the python documentation in /Developer/Documentation/Python, better for users that install multiple versions of python. ........ r52229 | ronald.oussoren | 2006-10-08 19:40:02 +0200 (Sun, 08 Oct 2006) | 2 lines Fix for bug #1570284 ........ r52233 | ronald.oussoren | 2006-10-08 19:49:52 +0200 (Sun, 08 Oct 2006) | 6 lines MacOSX: distutils changes the values of BASECFLAGS and LDFLAGS when using a universal build of python on OSX 10.3 to ensure that those flags can be used to compile code (the universal build uses compiler flags that aren't supported on 10.3). This patches gives the same treatment to CFLAGS, PY_CFLAGS and BLDSHARED. ........ r52236 | ronald.oussoren | 2006-10-08 19:51:46 +0200 (Sun, 08 Oct 2006) | 5 lines MacOSX: The universal build requires that users have the MacOSX10.4u SDK installed to build extensions. This patch makes distutils emit a warning when the compiler should use an SDK but that SDK is not installed, hopefully reducing some confusion. ........ r52238 | ronald.oussoren | 2006-10-08 20:18:26 +0200 (Sun, 08 Oct 2006) | 3 lines MacOSX: add more logic to recognize the correct startup file to patch to the shell profile patching post-install script. ........ r52242 | andrew.kuchling | 2006-10-09 19:10:12 +0200 (Mon, 09 Oct 2006) | 1 line Add news item for rev. 52211 change ........ r52245 | andrew.kuchling | 2006-10-09 20:05:19 +0200 (Mon, 09 Oct 2006) | 1 line Fix wording in comment ........ r52251 | georg.brandl | 2006-10-09 21:03:06 +0200 (Mon, 09 Oct 2006) | 2 lines Patch #1572724: fix typo ('=' instead of '==') in _msi.c. ........ r52255 | barry.warsaw | 2006-10-09 21:43:24 +0200 (Mon, 09 Oct 2006) | 2 lines List gc.get_count() in the module docstring. ........ r52257 | martin.v.loewis | 2006-10-09 22:44:25 +0200 (Mon, 09 Oct 2006) | 1 line Bug #1565150: Fix subsecond processing for os.utime on Windows. ........ r52268 | ronald.oussoren | 2006-10-10 09:55:06 +0200 (Tue, 10 Oct 2006) | 2 lines MacOSX: fix permission problem in the generated installer ........ r52293 | georg.brandl | 2006-10-12 09:38:04 +0200 (Thu, 12 Oct 2006) | 2 lines Bug #1575746: fix typo in property() docs. ........ r52295 | georg.brandl | 2006-10-12 09:57:21 +0200 (Thu, 12 Oct 2006) | 3 lines Bug #813342: Start the IDLE subprocess with -Qnew if the parent is started with that option. ........ r52297 | georg.brandl | 2006-10-12 10:22:53 +0200 (Thu, 12 Oct 2006) | 2 lines Bug #1565919: document set types in the Language Reference. ........ r52299 | georg.brandl | 2006-10-12 11:20:33 +0200 (Thu, 12 Oct 2006) | 3 lines Bug #1550524: better heuristics to find correct class definition in inspect.findsource(). ........ r52301 | georg.brandl | 2006-10-12 11:47:12 +0200 (Thu, 12 Oct 2006) | 4 lines Bug #1548891: The cStringIO.StringIO() constructor now encodes unicode arguments with the system default encoding just like the write() method does, instead of converting it to a raw buffer. ........ r52303 | georg.brandl | 2006-10-12 13:14:40 +0200 (Thu, 12 Oct 2006) | 2 lines Bug #1546628: add a note about urlparse.urljoin() and absolute paths. ........ r52305 | georg.brandl | 2006-10-12 13:27:59 +0200 (Thu, 12 Oct 2006) | 3 lines Bug #1545497: when given an explicit base, int() did ignore NULs embedded in the string to convert. ........ r52307 | georg.brandl | 2006-10-12 13:41:11 +0200 (Thu, 12 Oct 2006) | 3 lines Add a note to fpectl docs that it's not built by default (bug #1556261). ........ r52309 | georg.brandl | 2006-10-12 13:46:57 +0200 (Thu, 12 Oct 2006) | 3 lines Bug #1560114: the Mac filesystem does have accurate information about the case of filenames. ........ r52311 | georg.brandl | 2006-10-12 13:59:27 +0200 (Thu, 12 Oct 2006) | 2 lines Small grammar fix, thanks Sjoerd. ........ r52313 | georg.brandl | 2006-10-12 14:03:07 +0200 (Thu, 12 Oct 2006) | 2 lines Fix tarfile depending on buggy int('1\0', base) behavior. ........ r52315 | georg.brandl | 2006-10-12 14:33:07 +0200 (Thu, 12 Oct 2006) | 2 lines Bug #1283491: follow docstring convention wrt. keyword-able args in sum(). ........ r52316 | georg.brandl | 2006-10-12 15:08:16 +0200 (Thu, 12 Oct 2006) | 3 lines Bug #1560179: speed up posixpath.(dir|base)name ........ r52327 | brett.cannon | 2006-10-14 08:36:45 +0200 (Sat, 14 Oct 2006) | 3 lines Clean up the language of a sentence relating to the connect() function and user-defined datatypes. ........ r52332 | neal.norwitz | 2006-10-14 23:33:38 +0200 (Sat, 14 Oct 2006) | 3 lines Update the peephole optimizer to remove more dead code (jumps after returns) and inline jumps to returns. ........ r52333 | martin.v.loewis | 2006-10-15 09:54:40 +0200 (Sun, 15 Oct 2006) | 4 lines Patch #1576954: Update VC6 build directory; remove redundant files in VC7. Will backport to 2.5. ........ r52335 | martin.v.loewis | 2006-10-15 10:43:33 +0200 (Sun, 15 Oct 2006) | 1 line Patch #1576166: Support os.utime for directories on Windows NT+. ........ r52336 | martin.v.loewis | 2006-10-15 10:51:22 +0200 (Sun, 15 Oct 2006) | 2 lines Patch #1577551: Add ctypes and ET build support for VC6. Will backport to 2.5. ........ r52338 | martin.v.loewis | 2006-10-15 11:35:51 +0200 (Sun, 15 Oct 2006) | 1 line Loosen the test for equal time stamps. ........ r52339 | martin.v.loewis | 2006-10-15 11:43:39 +0200 (Sun, 15 Oct 2006) | 2 lines Bug #1567666: Emulate GetFileAttributesExA for Win95. Will backport to 2.5. ........ r52341 | martin.v.loewis | 2006-10-15 13:02:07 +0200 (Sun, 15 Oct 2006) | 2 lines Round to int, because some systems support sub-second time stamps in stat, but not in utime. Also be consistent with modifying only mtime, not atime. ........ r52342 | martin.v.loewis | 2006-10-15 13:57:40 +0200 (Sun, 15 Oct 2006) | 2 lines Set the eol-style for project files to "CRLF". ........ r52343 | martin.v.loewis | 2006-10-15 13:59:56 +0200 (Sun, 15 Oct 2006) | 3 lines Drop binary property on dsp files, set eol-style to CRLF instead. ........ r52344 | martin.v.loewis | 2006-10-15 14:01:43 +0200 (Sun, 15 Oct 2006) | 2 lines Remove binary property, set eol-style to CRLF instead. ........ r52346 | martin.v.loewis | 2006-10-15 16:30:38 +0200 (Sun, 15 Oct 2006) | 2 lines Mention the bdist_msi module. Will backport to 2.5. ........ r52354 | brett.cannon | 2006-10-16 05:09:52 +0200 (Mon, 16 Oct 2006) | 3 lines Fix turtle so that you can launch the demo2 function on its own instead of only when the module is launched as a script. ........ r52356 | martin.v.loewis | 2006-10-17 17:18:06 +0200 (Tue, 17 Oct 2006) | 2 lines Patch #1457736: Update VC6 to use current PCbuild settings. Will backport to 2.5. ........ r52360 | martin.v.loewis | 2006-10-17 20:09:55 +0200 (Tue, 17 Oct 2006) | 2 lines Remove obsolete file. Will backport. ........ r52363 | martin.v.loewis | 2006-10-17 20:59:23 +0200 (Tue, 17 Oct 2006) | 4 lines Forward-port r52358: - Bug #1578513: Cross compilation was broken by a change to configure. Repair so that it's back to how it was in 2.4.3. ........ r52365 | thomas.heller | 2006-10-17 21:30:48 +0200 (Tue, 17 Oct 2006) | 6 lines ctypes callback functions only support 'fundamental' result types. Check this and raise an error when something else is used - before this change ctypes would hang or crash when such a callback was called. This is a partial fix for #1574584. Will backport to release25-maint. ........ r52377 | tim.peters | 2006-10-18 07:06:06 +0200 (Wed, 18 Oct 2006) | 2 lines newIobject(): repaired incorrect cast to quiet MSVC warning. ........ r52378 | tim.peters | 2006-10-18 07:09:12 +0200 (Wed, 18 Oct 2006) | 2 lines Whitespace normalization. ........ r52379 | tim.peters | 2006-10-18 07:10:28 +0200 (Wed, 18 Oct 2006) | 2 lines Add missing svn:eol-style to text files. ........ r52387 | martin.v.loewis | 2006-10-19 12:58:46 +0200 (Thu, 19 Oct 2006) | 3 lines Add check for the PyArg_ParseTuple format, and declare it if it is supported. ........ r52388 | martin.v.loewis | 2006-10-19 13:00:37 +0200 (Thu, 19 Oct 2006) | 3 lines Fix various minor errors in passing arguments to PyArg_ParseTuple. ........ r52389 | martin.v.loewis | 2006-10-19 18:01:37 +0200 (Thu, 19 Oct 2006) | 2 lines Restore CFLAGS after checking for __attribute__ ........ r52390 | andrew.kuchling | 2006-10-19 23:55:55 +0200 (Thu, 19 Oct 2006) | 1 line [Bug #1576348] Fix typo in example ........ r52414 | walter.doerwald | 2006-10-22 10:59:41 +0200 (Sun, 22 Oct 2006) | 2 lines Port test___future__ to unittest. ........ r52415 | ronald.oussoren | 2006-10-22 12:45:18 +0200 (Sun, 22 Oct 2006) | 3 lines Patch #1580674: with this patch os.readlink uses the filesystem encoding to decode unicode objects and returns an unicode object when the argument is one. ........ r52416 | martin.v.loewis | 2006-10-22 12:46:18 +0200 (Sun, 22 Oct 2006) | 3 lines Patch #1580872: Remove duplicate declaration of PyCallable_Check. Will backport to 2.5. ........ r52418 | martin.v.loewis | 2006-10-22 12:55:15 +0200 (Sun, 22 Oct 2006) | 4 lines - Patch #1560695: Add .note.GNU-stack to ctypes' sysv.S so that ctypes isn't considered as requiring executable stacks. Will backport to 2.5. ........ r52420 | martin.v.loewis | 2006-10-22 15:45:13 +0200 (Sun, 22 Oct 2006) | 3 lines Remove passwd.adjunct.byname from list of maps for test_nis. Will backport to 2.5. ........ r52431 | georg.brandl | 2006-10-24 18:54:16 +0200 (Tue, 24 Oct 2006) | 2 lines Patch [ 1583506 ] tarfile.py: 100-char filenames are truncated ........ r52446 | andrew.kuchling | 2006-10-26 21:10:46 +0200 (Thu, 26 Oct 2006) | 1 line [Bug #1579796] Wrong syntax for PyDateTime_IMPORT in documentation. Reported by David Faure. ........ r52449 | andrew.kuchling | 2006-10-26 21:16:46 +0200 (Thu, 26 Oct 2006) | 1 line Typo fix ........ r52452 | martin.v.loewis | 2006-10-27 08:16:31 +0200 (Fri, 27 Oct 2006) | 3 lines Patch #1549049: Rewrite type conversion in structmember. Fixes #1545696 and #1566140. Will backport to 2.5. ........ r52454 | martin.v.loewis | 2006-10-27 08:42:27 +0200 (Fri, 27 Oct 2006) | 2 lines Check for values.h. Will backport. ........ r52456 | martin.v.loewis | 2006-10-27 09:06:52 +0200 (Fri, 27 Oct 2006) | 2 lines Get DBL_MAX from float.h not values.h. Will backport. ........ r52458 | martin.v.loewis | 2006-10-27 09:13:28 +0200 (Fri, 27 Oct 2006) | 2 lines Patch #1567274: Support SMTP over TLS. ........ r52459 | andrew.kuchling | 2006-10-27 13:33:29 +0200 (Fri, 27 Oct 2006) | 1 line Set svn:keywords property ........ r52460 | andrew.kuchling | 2006-10-27 13:36:41 +0200 (Fri, 27 Oct 2006) | 1 line Add item ........ r52461 | andrew.kuchling | 2006-10-27 13:37:01 +0200 (Fri, 27 Oct 2006) | 1 line Some wording changes and markup fixes ........ r52462 | andrew.kuchling | 2006-10-27 14:18:38 +0200 (Fri, 27 Oct 2006) | 1 line [Bug #1585690] Note that line_num was added in Python 2.5 ........ r52464 | andrew.kuchling | 2006-10-27 14:50:38 +0200 (Fri, 27 Oct 2006) | 1 line [Bug #1583946] Reword description of server and issuer ........ r52466 | andrew.kuchling | 2006-10-27 15:06:25 +0200 (Fri, 27 Oct 2006) | 1 line [Bug #1562583] Mention the set_reuse_addr() method ........ r52469 | andrew.kuchling | 2006-10-27 15:22:46 +0200 (Fri, 27 Oct 2006) | 4 lines [Bug #1542016] Report PCALL_POP value. This makes the return value of sys.callstats() match its docstring. Backport candidate. Though it's an API change, this is a pretty obscure portion of the API. ........ r52473 | andrew.kuchling | 2006-10-27 16:53:41 +0200 (Fri, 27 Oct 2006) | 1 line Point users to the subprocess module in the docs for os.system, os.spawn*, os.popen2, and the popen2 and commands modules ........ r52476 | andrew.kuchling | 2006-10-27 18:39:10 +0200 (Fri, 27 Oct 2006) | 1 line [Bug #1576241] Let functools.wraps work with built-in functions ........ r52478 | andrew.kuchling | 2006-10-27 18:55:34 +0200 (Fri, 27 Oct 2006) | 1 line [Bug #1575506] The _singlefileMailbox class was using the wrong file object in its flush() method, causing an error ........ r52480 | andrew.kuchling | 2006-10-27 19:06:16 +0200 (Fri, 27 Oct 2006) | 1 line Clarify docstring ........ r52481 | andrew.kuchling | 2006-10-27 19:11:23 +0200 (Fri, 27 Oct 2006) | 5 lines [Patch #1574068 by Scott Dial] urllib and urllib2 were using base64.encodestring() for encoding authentication data. encodestring() can include newlines for very long input, which produced broken HTTP headers. ........ r52483 | andrew.kuchling | 2006-10-27 20:13:46 +0200 (Fri, 27 Oct 2006) | 1 line Check db_setup_debug for a few print statements; change sqlite_setup_debug to False ........ r52484 | andrew.kuchling | 2006-10-27 20:15:02 +0200 (Fri, 27 Oct 2006) | 1 line [Patch #1503717] Tiny patch from Chris AtLee to stop a lengthy line from being printed ........ r52485 | thomas.heller | 2006-10-27 20:31:36 +0200 (Fri, 27 Oct 2006) | 5 lines WindowsError.str should display the windows error code, not the posix error code; with test. Fixes #1576174. Will backport to release25-maint. ........ r52487 | thomas.heller | 2006-10-27 21:05:53 +0200 (Fri, 27 Oct 2006) | 4 lines Modulefinder now handles absolute and relative imports, including tests. Will backport to release25-maint. ........ r52488 | georg.brandl | 2006-10-27 22:39:43 +0200 (Fri, 27 Oct 2006) | 2 lines Patch #1552024: add decorator support to unparse.py demo script. ........ r52492 | walter.doerwald | 2006-10-28 12:47:12 +0200 (Sat, 28 Oct 2006) | 2 lines Port test_bufio to unittest. ........ r52493 | georg.brandl | 2006-10-28 15:10:17 +0200 (Sat, 28 Oct 2006) | 6 lines Convert test_global, test_scope and test_grammar to unittest. I tried to enclose all tests which must be run at the toplevel (instead of inside a method) in exec statements. ........ r52494 | georg.brandl | 2006-10-28 15:11:41 +0200 (Sat, 28 Oct 2006) | 3 lines Update outstanding bugs test file. ........ r52495 | georg.brandl | 2006-10-28 15:51:49 +0200 (Sat, 28 Oct 2006) | 3 lines Convert test_math to unittest. ........ r52496 | georg.brandl | 2006-10-28 15:56:58 +0200 (Sat, 28 Oct 2006) | 3 lines Convert test_opcodes to unittest. ........ r52497 | georg.brandl | 2006-10-28 18:04:04 +0200 (Sat, 28 Oct 2006) | 2 lines Fix nth() itertool recipe. ........ r52500 | georg.brandl | 2006-10-28 22:25:09 +0200 (Sat, 28 Oct 2006) | 2 lines make test_grammar pass with python -O ........ r52501 | neal.norwitz | 2006-10-28 23:15:30 +0200 (Sat, 28 Oct 2006) | 6 lines Add some asserts. In sysmodule, I think these were to try to silence some warnings from Klokwork. They verify the assumptions of the format of svn version output. The assert in the thread module helped debug a problem on HP-UX. ........ r52502 | neal.norwitz | 2006-10-28 23:16:54 +0200 (Sat, 28 Oct 2006) | 5 lines Fix warnings with HP's C compiler. It doesn't recognize that infinite loops are, um, infinite. These conditions should not be able to happen. Will backport. ........ r52503 | neal.norwitz | 2006-10-28 23:17:51 +0200 (Sat, 28 Oct 2006) | 5 lines Fix crash in test on HP-UX. Apparently, it's not possible to delete a lock if it's held (even by the current thread). Will backport. ........ r52504 | neal.norwitz | 2006-10-28 23:19:07 +0200 (Sat, 28 Oct 2006) | 6 lines Fix bug #1565514, SystemError not raised on too many nested blocks. It seems like this should be a different error than SystemError, but I don't have any great ideas and SystemError was raised in 2.4 and earlier. Will backport. ........ r52505 | neal.norwitz | 2006-10-28 23:20:12 +0200 (Sat, 28 Oct 2006) | 4 lines Prevent crash if alloc of garbage fails. Found by Typo.pl. Will backport. ........ r52506 | neal.norwitz | 2006-10-28 23:21:00 +0200 (Sat, 28 Oct 2006) | 4 lines Don't inline Py_ADDRESS_IN_RANGE with gcc 4+ either. Will backport. ........ r52513 | neal.norwitz | 2006-10-28 23:56:49 +0200 (Sat, 28 Oct 2006) | 2 lines Fix test_modulefinder so it doesn't fail when run after test_distutils. ........ r52514 | neal.norwitz | 2006-10-29 00:12:26 +0200 (Sun, 29 Oct 2006) | 4 lines From SF 1557890, fix problem of using wrong type in example. Will backport. ........ r52517 | georg.brandl | 2006-10-29 09:39:22 +0100 (Sun, 29 Oct 2006) | 4 lines Fix codecs.EncodedFile which did not use file_encoding in 2.5.0, and fix all codecs file wrappers to work correctly with the "with" statement (bug #1586513). ........ r52519 | georg.brandl | 2006-10-29 09:47:08 +0100 (Sun, 29 Oct 2006) | 3 lines Clean up a leftover from old listcomp generation code. ........ r52520 | georg.brandl | 2006-10-29 09:53:06 +0100 (Sun, 29 Oct 2006) | 4 lines Bug #1586448: the compiler module now emits the same bytecode for list comprehensions as the builtin compiler, using the LIST_APPEND opcode. ........ r52521 | georg.brandl | 2006-10-29 10:01:01 +0100 (Sun, 29 Oct 2006) | 3 lines Remove trailing comma. ........ r52522 | georg.brandl | 2006-10-29 10:05:04 +0100 (Sun, 29 Oct 2006) | 3 lines Bug #1357915: allow all sequence types for shell arguments in subprocess. ........ r52524 | georg.brandl | 2006-10-29 10:16:12 +0100 (Sun, 29 Oct 2006) | 3 lines Patch #1583880: fix tarfile's problems with long names and posix/ GNU modes. ........ r52526 | georg.brandl | 2006-10-29 10:18:00 +0100 (Sun, 29 Oct 2006) | 3 lines Test assert if __debug__ is true. ........ r52527 | georg.brandl | 2006-10-29 10:32:16 +0100 (Sun, 29 Oct 2006) | 2 lines Fix the new EncodedFile test to work with big endian platforms. ........ r52529 | georg.brandl | 2006-10-29 15:39:09 +0100 (Sun, 29 Oct 2006) | 2 lines Bug #1586613: fix zlib and bz2 codecs' incremental en/decoders. ........ r52532 | georg.brandl | 2006-10-29 19:01:08 +0100 (Sun, 29 Oct 2006) | 2 lines Bug #1586773: extend hashlib docstring. ........ r52534 | neal.norwitz | 2006-10-29 19:30:10 +0100 (Sun, 29 Oct 2006) | 4 lines Update comments, remove commented out code. Move assembler structure next to assembler code to make it easier to move it to a separate file. ........ r52535 | georg.brandl | 2006-10-29 19:31:42 +0100 (Sun, 29 Oct 2006) | 3 lines Bug #1576657: when setting a KeyError for a tuple key, make sure that the tuple isn't used as the "exception arguments tuple". ........ r52537 | georg.brandl | 2006-10-29 20:13:40 +0100 (Sun, 29 Oct 2006) | 3 lines Convert test_mmap to unittest. ........ r52538 | georg.brandl | 2006-10-29 20:20:45 +0100 (Sun, 29 Oct 2006) | 3 lines Convert test_poll to unittest. ........ r52539 | georg.brandl | 2006-10-29 20:24:43 +0100 (Sun, 29 Oct 2006) | 3 lines Convert test_nis to unittest. ........ r52540 | georg.brandl | 2006-10-29 20:35:03 +0100 (Sun, 29 Oct 2006) | 3 lines Convert test_types to unittest. ........ r52541 | georg.brandl | 2006-10-29 20:51:16 +0100 (Sun, 29 Oct 2006) | 3 lines Convert test_cookie to unittest. ........ r52542 | georg.brandl | 2006-10-29 21:09:12 +0100 (Sun, 29 Oct 2006) | 3 lines Convert test_cgi to unittest. ........ r52543 | georg.brandl | 2006-10-29 21:24:01 +0100 (Sun, 29 Oct 2006) | 3 lines Completely convert test_httplib to unittest. ........ r52544 | georg.brandl | 2006-10-29 21:28:26 +0100 (Sun, 29 Oct 2006) | 2 lines Convert test_MimeWriter to unittest. ........ r52545 | georg.brandl | 2006-10-29 21:31:17 +0100 (Sun, 29 Oct 2006) | 3 lines Convert test_openpty to unittest. ........ r52546 | georg.brandl | 2006-10-29 21:35:12 +0100 (Sun, 29 Oct 2006) | 3 lines Remove leftover test output file. ........ r52547 | georg.brandl | 2006-10-29 22:54:18 +0100 (Sun, 29 Oct 2006) | 3 lines Move the check for openpty to the beginning. ........ r52548 | walter.doerwald | 2006-10-29 23:06:28 +0100 (Sun, 29 Oct 2006) | 2 lines Add tests for basic argument errors. ........ r52549 | walter.doerwald | 2006-10-30 00:02:27 +0100 (Mon, 30 Oct 2006) | 3 lines Add tests for incremental codecs with an errors argument. ........ r52550 | neal.norwitz | 2006-10-30 00:39:03 +0100 (Mon, 30 Oct 2006) | 1 line Fix refleak ........ r52552 | neal.norwitz | 2006-10-30 00:58:36 +0100 (Mon, 30 Oct 2006) | 1 line I'm assuming this is correct, it fixes the tests so they pass again ........ r52555 | vinay.sajip | 2006-10-31 18:32:37 +0100 (Tue, 31 Oct 2006) | 1 line Change to improve speed of _fixupChildren ........ r52556 | vinay.sajip | 2006-10-31 18:34:31 +0100 (Tue, 31 Oct 2006) | 1 line Added relativeCreated to Formatter doc (has been in the system for a long time - was unaccountably left out of the docs and not noticed until now). ........ r52588 | thomas.heller | 2006-11-02 20:48:24 +0100 (Thu, 02 Nov 2006) | 5 lines Replace the XXX marker in the 'Arrays and pointers' reference manual section with a link to the tutorial sections. Will backport to release25-maint. ........ r52592 | thomas.heller | 2006-11-02 21:22:29 +0100 (Thu, 02 Nov 2006) | 6 lines Fix a code example by adding a missing import. Fixes #1557890. Will backport to release25-maint. ........ r52598 | tim.peters | 2006-11-03 03:32:46 +0100 (Fri, 03 Nov 2006) | 2 lines Whitespace normalization. ........ r52619 | martin.v.loewis | 2006-11-04 19:14:06 +0100 (Sat, 04 Nov 2006) | 4 lines - Patch #1060577: Extract list of RPM files from spec file in bdist_rpm Will backport to 2.5. ........ r52621 | neal.norwitz | 2006-11-04 20:25:22 +0100 (Sat, 04 Nov 2006) | 4 lines Bug #1588287: fix invalid assertion for `1,2` in debug builds. Will backport ........ r52630 | andrew.kuchling | 2006-11-05 22:04:37 +0100 (Sun, 05 Nov 2006) | 1 line Update link ........ r52631 | skip.montanaro | 2006-11-06 15:34:52 +0100 (Mon, 06 Nov 2006) | 1 line note that user can control directory location even if default dir is used ........ r52644 | ronald.oussoren | 2006-11-07 16:53:38 +0100 (Tue, 07 Nov 2006) | 2 lines Fix a number of typos in strings and comments (sf#1589070) ........ r52647 | ronald.oussoren | 2006-11-07 17:00:34 +0100 (Tue, 07 Nov 2006) | 2 lines Whitespace changes to make the source more compliant with PEP8 (SF#1589070) ........ r52651 | thomas.heller | 2006-11-07 19:01:18 +0100 (Tue, 07 Nov 2006) | 3 lines Fix markup. Will backport to release25-maint. ........ r52653 | thomas.heller | 2006-11-07 19:20:47 +0100 (Tue, 07 Nov 2006) | 3 lines Fix grammatical error as well. Will backport to release25-maint. ........ r52657 | andrew.kuchling | 2006-11-07 21:39:16 +0100 (Tue, 07 Nov 2006) | 1 line Add missing word ........ r52662 | martin.v.loewis | 2006-11-08 07:46:37 +0100 (Wed, 08 Nov 2006) | 4 lines Correctly forward exception in instance_contains(). Fixes #1591996. Patch contributed by Neal Norwitz. Will backport. ........ r52664 | martin.v.loewis | 2006-11-08 07:48:36 +0100 (Wed, 08 Nov 2006) | 2 lines News entry for 52662. ........ r52665 | martin.v.loewis | 2006-11-08 08:35:55 +0100 (Wed, 08 Nov 2006) | 2 lines Patch #1351744: Add askyesnocancel helper for tkMessageBox. ........ r52666 | georg.brandl | 2006-11-08 08:45:59 +0100 (Wed, 08 Nov 2006) | 2 lines Patch #1592072: fix docs for return value of PyErr_CheckSignals. ........ r52668 | georg.brandl | 2006-11-08 11:04:29 +0100 (Wed, 08 Nov 2006) | 3 lines Bug #1592533: rename variable in heapq doc example, to avoid shadowing "sorted". ........ r52671 | andrew.kuchling | 2006-11-08 14:35:34 +0100 (Wed, 08 Nov 2006) | 1 line Add section on the functional module ........ r52672 | andrew.kuchling | 2006-11-08 15:14:30 +0100 (Wed, 08 Nov 2006) | 1 line Add section on operator module; make a few edits ........ r52673 | andrew.kuchling | 2006-11-08 15:24:03 +0100 (Wed, 08 Nov 2006) | 1 line Add table of contents; this required fixing a few headings. Some more smalle edits. ........ r52674 | andrew.kuchling | 2006-11-08 15:30:14 +0100 (Wed, 08 Nov 2006) | 1 line More edits ........ r52686 | martin.v.loewis | 2006-11-09 12:06:03 +0100 (Thu, 09 Nov 2006) | 3 lines Patch #838546: Make terminal become controlling in pty.fork(). Will backport to 2.5. ........ r52688 | martin.v.loewis | 2006-11-09 12:27:32 +0100 (Thu, 09 Nov 2006) | 2 lines Patch #1592250: Add elidge argument to Tkinter.Text.search. ........ r52690 | andrew.kuchling | 2006-11-09 14:27:07 +0100 (Thu, 09 Nov 2006) | 7 lines [Bug #1569790] mailbox.Maildir.get_folder() loses factory information Both the Maildir and MH classes had this bug; the patch fixes both classes and adds a test. Will backport to 25-maint. ........ r52692 | andrew.kuchling | 2006-11-09 14:51:14 +0100 (Thu, 09 Nov 2006) | 1 line [Patch #1514544 by David Watson] use fsync() to ensure data is really on disk ........ r52695 | walter.doerwald | 2006-11-09 17:23:26 +0100 (Thu, 09 Nov 2006) | 2 lines Replace C++ comment with C comment (fixes SF bug #1593525). ........ r52712 | andrew.kuchling | 2006-11-09 22:16:46 +0100 (Thu, 09 Nov 2006) | 11 lines [Patch #1514543] mailbox (Maildir): avoid losing messages on name clash Two changes: Where possible, use link()/remove() to move files into a directory; this makes it easier to avoid overwriting an existing file. Use _create_carefully() to create files in tmp/, which uses O_EXCL. Backport candidate. ........ r52716 | phillip.eby | 2006-11-10 01:33:36 +0100 (Fri, 10 Nov 2006) | 4 lines Fix SF#1566719: not creating site-packages (or other target directory) when installing .egg-info for a project that contains no modules or packages, while using --root (as in bdist_rpm). ........ r52719 | andrew.kuchling | 2006-11-10 14:14:01 +0100 (Fri, 10 Nov 2006) | 1 line Reword entry ........ r52725 | andrew.kuchling | 2006-11-10 15:39:01 +0100 (Fri, 10 Nov 2006) | 1 line [Feature request #1542920] Link to wsgi.org ........ r52731 | georg.brandl | 2006-11-11 19:29:11 +0100 (Sat, 11 Nov 2006) | 2 lines Bug #1594742: wrong word in stringobject doc. ........ r52733 | georg.brandl | 2006-11-11 19:32:47 +0100 (Sat, 11 Nov 2006) | 2 lines Bug #1594758: wording improvement for dict.update() docs. ........ r52736 | martin.v.loewis | 2006-11-12 11:32:47 +0100 (Sun, 12 Nov 2006) | 3 lines Patch #1065257: Support passing open files as body in HTTPConnection.request(). ........ r52737 | martin.v.loewis | 2006-11-12 11:41:39 +0100 (Sun, 12 Nov 2006) | 2 lines Patch #1355023: support whence argument for GzipFile.seek. ........ r52738 | martin.v.loewis | 2006-11-12 19:24:26 +0100 (Sun, 12 Nov 2006) | 2 lines Bug #1067760: Deprecate passing floats to file.seek. ........ r52739 | martin.v.loewis | 2006-11-12 19:48:13 +0100 (Sun, 12 Nov 2006) | 3 lines Patch #1359217: Ignore 2xx response before 150 response. Will backport to 2.5. ........ r52741 | martin.v.loewis | 2006-11-12 19:56:03 +0100 (Sun, 12 Nov 2006) | 4 lines Patch #1360200: Use unmangled_version RPM spec field to deal with file name mangling. Will backport to 2.5. ........ r52753 | walter.doerwald | 2006-11-15 17:23:46 +0100 (Wed, 15 Nov 2006) | 2 lines Fix typo. ........ r52754 | georg.brandl | 2006-11-15 18:42:03 +0100 (Wed, 15 Nov 2006) | 2 lines Bug #1594809: add a note to README regarding PYTHONPATH and make install. ........ r52762 | georg.brandl | 2006-11-16 16:05:14 +0100 (Thu, 16 Nov 2006) | 2 lines Bug #1597576: mention that the new base64 api has been introduced in py2.4. ........ r52764 | georg.brandl | 2006-11-16 17:50:59 +0100 (Thu, 16 Nov 2006) | 3 lines Bug #1597824: return the registered function from atexit.register() to facilitate usage as a decorator. ........ r52765 | georg.brandl | 2006-11-16 18:08:45 +0100 (Thu, 16 Nov 2006) | 4 lines Bug #1588217: don't parse "= " as a soft line break in binascii's a2b_qp() function, instead leave it in the string as quopri.decode() does. ........ r52776 | andrew.kuchling | 2006-11-17 14:30:25 +0100 (Fri, 17 Nov 2006) | 17 lines Remove file-locking in MH.pack() method. This change looks massive but it's mostly a re-indenting after removing some try...finally blocks. Also adds a test case that does a pack() while the mailbox is locked; this test would have turned up bugs in the original code on some platforms. In both nmh and GNU Mailutils' implementation of MH-format mailboxes, no locking is done of individual message files when renaming them. The original mailbox.py code did do locking, which meant that message files had to be opened. This code was buggy on certain platforms (found through reading the code); there were code paths that closed the file object and then called _unlock_file() on it. Will backport to 25-maint once I see how the buildbots react to this patch. ........ r52780 | martin.v.loewis | 2006-11-18 19:00:23 +0100 (Sat, 18 Nov 2006) | 5 lines Patch #1538878: Don't make tkSimpleDialog dialogs transient if the parent window is withdrawn. This mirrors what dialog.tcl does. Will backport to 2.5. ........ r52782 | martin.v.loewis | 2006-11-18 19:05:35 +0100 (Sat, 18 Nov 2006) | 4 lines Patch #1594554: Always close a tkSimpleDialog on ok(), even if an exception occurs. Will backport to 2.5. ........ r52784 | martin.v.loewis | 2006-11-18 19:42:11 +0100 (Sat, 18 Nov 2006) | 3 lines Patch #1472877: Fix Tix subwidget name resolution. Will backport to 2.5. ........ r52786 | andrew.kuchling | 2006-11-18 23:17:33 +0100 (Sat, 18 Nov 2006) | 1 line Expand checking in test_sha ........ r52787 | georg.brandl | 2006-11-19 09:48:30 +0100 (Sun, 19 Nov 2006) | 3 lines Patch [ 1586791 ] better error msgs for some TypeErrors ........ r52788 | martin.v.loewis | 2006-11-19 11:41:41 +0100 (Sun, 19 Nov 2006) | 4 lines Make cStringIO.truncate raise IOError for negative arguments (even for -1). Fixes the last bit of #1359365. ........ r52789 | andrew.kuchling | 2006-11-19 19:40:01 +0100 (Sun, 19 Nov 2006) | 1 line Add a test case of data w/ bytes > 127 ........ r52790 | martin.v.loewis | 2006-11-19 19:51:54 +0100 (Sun, 19 Nov 2006) | 3 lines Patch #1070046: Marshal new-style objects like InstanceType in xmlrpclib. ........ r52792 | neal.norwitz | 2006-11-19 22:26:53 +0100 (Sun, 19 Nov 2006) | 4 lines Speed up function calls into the math module by using METH_O. There should be no functional changes. However, the error msgs are slightly different. Also verified that the module dict is not NULL on init. ........ r52794 | george.yoshida | 2006-11-20 03:24:48 +0100 (Mon, 20 Nov 2006) | 2 lines markup fix ........ r52795 | georg.brandl | 2006-11-20 08:12:58 +0100 (Mon, 20 Nov 2006) | 3 lines Further markup fix. ........ r52800 | andrew.kuchling | 2006-11-20 14:39:37 +0100 (Mon, 20 Nov 2006) | 2 lines Jython compatibility fix: if uu.decode() opened its output file, be sure to close it. ........ r52811 | neal.norwitz | 2006-11-21 06:26:22 +0100 (Tue, 21 Nov 2006) | 9 lines Bug #1599782: Fix segfault on bsddb.db.DB().type(). The problem is that _DB_get_type() can't be called without the GIL because it calls a bunch of PyErr_* APIs when an error occurs. There were no other cases in this file that it was called without the GIL. Removing the BEGIN/END THREAD around _DB_get_type() made everything work. Will backport. ........ r52814 | neal.norwitz | 2006-11-21 06:51:51 +0100 (Tue, 21 Nov 2006) | 1 line Oops, convert tabs to spaces ........ r52815 | neal.norwitz | 2006-11-21 07:23:44 +0100 (Tue, 21 Nov 2006) | 1 line Fix SF #1599879, socket.gethostname should ref getfqdn directly. ........ r52817 | martin.v.loewis | 2006-11-21 19:20:25 +0100 (Tue, 21 Nov 2006) | 4 lines Conditionalize definition of _CRT_SECURE_NO_DEPRECATE and _CRT_NONSTDC_NO_DEPRECATE. Will backport. ........ r52821 | martin.v.loewis | 2006-11-22 09:50:02 +0100 (Wed, 22 Nov 2006) | 4 lines Patch #1362975: Rework CodeContext indentation algorithm to avoid hard-coding pixel widths. Also make the text's scrollbar a child of the text frame, not the top widget. ........ r52826 | walter.doerwald | 2006-11-23 06:03:56 +0100 (Thu, 23 Nov 2006) | 3 lines Change decode() so that it works with a buffer (i.e. unicode(..., 'utf-8-sig')) SF bug #1601501. ........ r52833 | georg.brandl | 2006-11-23 10:55:07 +0100 (Thu, 23 Nov 2006) | 2 lines Bug #1601630: little improvement to getopt docs ........ r52835 | michael.hudson | 2006-11-23 14:54:04 +0100 (Thu, 23 Nov 2006) | 3 lines a test for an error condition not covered by existing tests (noticed this when writing the equivalent code for pypy) ........ r52839 | raymond.hettinger | 2006-11-23 22:06:03 +0100 (Thu, 23 Nov 2006) | 1 line Fix and/add typo ........ r52840 | raymond.hettinger | 2006-11-23 22:35:19 +0100 (Thu, 23 Nov 2006) | 1 line ... and the number of the counting shall be three. ........ r52841 | thomas.heller | 2006-11-24 19:45:39 +0100 (Fri, 24 Nov 2006) | 1 line Fix bug #1598620: A ctypes structure cannot contain itself. ........ r52843 | martin.v.loewis | 2006-11-25 16:39:19 +0100 (Sat, 25 Nov 2006) | 3 lines Disable _XOPEN_SOURCE on NetBSD 1.x. Will backport to 2.5 ........ r52845 | georg.brandl | 2006-11-26 20:27:47 +0100 (Sun, 26 Nov 2006) | 2 lines Bug #1603321: make pstats.Stats accept Unicode file paths. ........ r52850 | georg.brandl | 2006-11-27 19:46:21 +0100 (Mon, 27 Nov 2006) | 2 lines Bug #1603789: grammatical error in Tkinter docs. ........ r52855 | thomas.heller | 2006-11-28 21:21:54 +0100 (Tue, 28 Nov 2006) | 7 lines Fix #1563807: _ctypes built on AIX fails with ld ffi error. The contents of ffi_darwin.c must be compiled unless __APPLE__ is defined and __ppc__ is not. Will backport. ........ r52862 | armin.rigo | 2006-11-29 22:59:22 +0100 (Wed, 29 Nov 2006) | 3 lines Forgot a case where the locals can now be a general mapping instead of just a dictionary. (backporting...) ........ r52872 | guido.van.rossum | 2006-11-30 20:23:13 +0100 (Thu, 30 Nov 2006) | 2 lines Update version. ........ r52890 | walter.doerwald | 2006-12-01 17:59:47 +0100 (Fri, 01 Dec 2006) | 3 lines Move xdrlib tests from the module into a separate test script, port the tests to unittest and add a few new tests. ........ r52900 | raymond.hettinger | 2006-12-02 03:00:39 +0100 (Sat, 02 Dec 2006) | 1 line Add name to credits (for untokenize). ........ r52905 | martin.v.loewis | 2006-12-03 10:54:46 +0100 (Sun, 03 Dec 2006) | 2 lines Move IDLE news into NEWS.txt. ........ r52906 | martin.v.loewis | 2006-12-03 12:23:45 +0100 (Sun, 03 Dec 2006) | 4 lines Patch #1544279: Improve thread-safety of the socket module by moving the sock_addr_t storage out of the socket object. Will backport to 2.5. ........ r52908 | martin.v.loewis | 2006-12-03 13:01:53 +0100 (Sun, 03 Dec 2006) | 3 lines Patch #1371075: Make ConfigParser accept optional dict type for ordering, sorting, etc. ........ r52910 | matthias.klose | 2006-12-03 18:16:41 +0100 (Sun, 03 Dec 2006) | 2 lines - Fix build failure on kfreebsd and on the hurd. ........ r52915 | george.yoshida | 2006-12-04 12:41:54 +0100 (Mon, 04 Dec 2006) | 2 lines fix a versionchanged tag ........ r52917 | george.yoshida | 2006-12-05 06:39:50 +0100 (Tue, 05 Dec 2006) | 3 lines Fix pickle doc typo Patch #1608758 ........ r52938 | georg.brandl | 2006-12-06 23:21:18 +0100 (Wed, 06 Dec 2006) | 2 lines Patch #1610437: fix a tarfile bug with long filename headers. ........ r52945 | brett.cannon | 2006-12-07 00:38:48 +0100 (Thu, 07 Dec 2006) | 3 lines Fix a bad assumption that all objects assigned to '__loader__' on a module will have a '_files' attribute. ........ r52951 | georg.brandl | 2006-12-07 10:30:06 +0100 (Thu, 07 Dec 2006) | 3 lines RFE #1592899: mention string.maketrans() in docs for str.translate, remove reference to the old regex module in the former's doc. ........ r52962 | raymond.hettinger | 2006-12-08 04:17:18 +0100 (Fri, 08 Dec 2006) | 1 line Eliminate two redundant calls to PyObject_Hash(). ........ r52963 | raymond.hettinger | 2006-12-08 05:24:33 +0100 (Fri, 08 Dec 2006) | 3 lines Port Armin's fix for a dict resize vulnerability (svn revision 46589, sf bug 1456209). ........ r52964 | raymond.hettinger | 2006-12-08 05:57:50 +0100 (Fri, 08 Dec 2006) | 4 lines Port Georg's dictobject.c fix keys that were tuples got unpacked on the way to setting a KeyError (svn revision 52535, sf bug 1576657). ........ r52966 | raymond.hettinger | 2006-12-08 18:35:25 +0100 (Fri, 08 Dec 2006) | 2 lines Add test for SF bug 1576657 ........ r52970 | georg.brandl | 2006-12-08 21:46:11 +0100 (Fri, 08 Dec 2006) | 3 lines #1577756: svnversion doesn't react to LANG=C, use LC_ALL=C to force English output. ........ r52972 | georg.brandl | 2006-12-09 10:08:29 +0100 (Sat, 09 Dec 2006) | 3 lines Patch #1608267: fix a race condition in os.makedirs() is the directory to be created is already there. ........ r52975 | matthias.klose | 2006-12-09 13:15:27 +0100 (Sat, 09 Dec 2006) | 2 lines - Fix the build of the library reference in info format. ........ r52994 | neal.norwitz | 2006-12-11 02:01:06 +0100 (Mon, 11 Dec 2006) | 1 line Fix a typo ........ r52996 | georg.brandl | 2006-12-11 08:56:33 +0100 (Mon, 11 Dec 2006) | 2 lines Move errno imports back to individual functions. ........ r52998 | vinay.sajip | 2006-12-11 15:07:16 +0100 (Mon, 11 Dec 2006) | 1 line Patch by Jeremy Katz (SF #1609407) ........ r53000 | vinay.sajip | 2006-12-11 15:26:23 +0100 (Mon, 11 Dec 2006) | 1 line Patch by "cuppatea" (SF #1503765) ........
4930 lines
133 KiB
C
4930 lines
133 KiB
C
/* C implementation for the date/time type documented at
|
|
* http://www.zope.org/Members/fdrake/DateTimeWiki/FrontPage
|
|
*/
|
|
|
|
#include "Python.h"
|
|
#include "modsupport.h"
|
|
#include "structmember.h"
|
|
|
|
#include <time.h>
|
|
|
|
#include "timefuncs.h"
|
|
|
|
/* Differentiate between building the core module and building extension
|
|
* modules.
|
|
*/
|
|
#define Py_BUILD_CORE
|
|
#include "datetime.h"
|
|
#undef Py_BUILD_CORE
|
|
|
|
/* We require that C int be at least 32 bits, and use int virtually
|
|
* everywhere. In just a few cases we use a temp long, where a Python
|
|
* API returns a C long. In such cases, we have to ensure that the
|
|
* final result fits in a C int (this can be an issue on 64-bit boxes).
|
|
*/
|
|
#if SIZEOF_INT < 4
|
|
# error "datetime.c requires that C int have at least 32 bits"
|
|
#endif
|
|
|
|
#define MINYEAR 1
|
|
#define MAXYEAR 9999
|
|
|
|
/* Nine decimal digits is easy to communicate, and leaves enough room
|
|
* so that two delta days can be added w/o fear of overflowing a signed
|
|
* 32-bit int, and with plenty of room left over to absorb any possible
|
|
* carries from adding seconds.
|
|
*/
|
|
#define MAX_DELTA_DAYS 999999999
|
|
|
|
/* Rename the long macros in datetime.h to more reasonable short names. */
|
|
#define GET_YEAR PyDateTime_GET_YEAR
|
|
#define GET_MONTH PyDateTime_GET_MONTH
|
|
#define GET_DAY PyDateTime_GET_DAY
|
|
#define DATE_GET_HOUR PyDateTime_DATE_GET_HOUR
|
|
#define DATE_GET_MINUTE PyDateTime_DATE_GET_MINUTE
|
|
#define DATE_GET_SECOND PyDateTime_DATE_GET_SECOND
|
|
#define DATE_GET_MICROSECOND PyDateTime_DATE_GET_MICROSECOND
|
|
|
|
/* Date accessors for date and datetime. */
|
|
#define SET_YEAR(o, v) (((o)->data[0] = ((v) & 0xff00) >> 8), \
|
|
((o)->data[1] = ((v) & 0x00ff)))
|
|
#define SET_MONTH(o, v) (PyDateTime_GET_MONTH(o) = (v))
|
|
#define SET_DAY(o, v) (PyDateTime_GET_DAY(o) = (v))
|
|
|
|
/* Date/Time accessors for datetime. */
|
|
#define DATE_SET_HOUR(o, v) (PyDateTime_DATE_GET_HOUR(o) = (v))
|
|
#define DATE_SET_MINUTE(o, v) (PyDateTime_DATE_GET_MINUTE(o) = (v))
|
|
#define DATE_SET_SECOND(o, v) (PyDateTime_DATE_GET_SECOND(o) = (v))
|
|
#define DATE_SET_MICROSECOND(o, v) \
|
|
(((o)->data[7] = ((v) & 0xff0000) >> 16), \
|
|
((o)->data[8] = ((v) & 0x00ff00) >> 8), \
|
|
((o)->data[9] = ((v) & 0x0000ff)))
|
|
|
|
/* Time accessors for time. */
|
|
#define TIME_GET_HOUR PyDateTime_TIME_GET_HOUR
|
|
#define TIME_GET_MINUTE PyDateTime_TIME_GET_MINUTE
|
|
#define TIME_GET_SECOND PyDateTime_TIME_GET_SECOND
|
|
#define TIME_GET_MICROSECOND PyDateTime_TIME_GET_MICROSECOND
|
|
#define TIME_SET_HOUR(o, v) (PyDateTime_TIME_GET_HOUR(o) = (v))
|
|
#define TIME_SET_MINUTE(o, v) (PyDateTime_TIME_GET_MINUTE(o) = (v))
|
|
#define TIME_SET_SECOND(o, v) (PyDateTime_TIME_GET_SECOND(o) = (v))
|
|
#define TIME_SET_MICROSECOND(o, v) \
|
|
(((o)->data[3] = ((v) & 0xff0000) >> 16), \
|
|
((o)->data[4] = ((v) & 0x00ff00) >> 8), \
|
|
((o)->data[5] = ((v) & 0x0000ff)))
|
|
|
|
/* Delta accessors for timedelta. */
|
|
#define GET_TD_DAYS(o) (((PyDateTime_Delta *)(o))->days)
|
|
#define GET_TD_SECONDS(o) (((PyDateTime_Delta *)(o))->seconds)
|
|
#define GET_TD_MICROSECONDS(o) (((PyDateTime_Delta *)(o))->microseconds)
|
|
|
|
#define SET_TD_DAYS(o, v) ((o)->days = (v))
|
|
#define SET_TD_SECONDS(o, v) ((o)->seconds = (v))
|
|
#define SET_TD_MICROSECONDS(o, v) ((o)->microseconds = (v))
|
|
|
|
/* p is a pointer to a time or a datetime object; HASTZINFO(p) returns
|
|
* p->hastzinfo.
|
|
*/
|
|
#define HASTZINFO(p) (((_PyDateTime_BaseTZInfo *)(p))->hastzinfo)
|
|
|
|
/* M is a char or int claiming to be a valid month. The macro is equivalent
|
|
* to the two-sided Python test
|
|
* 1 <= M <= 12
|
|
*/
|
|
#define MONTH_IS_SANE(M) ((unsigned int)(M) - 1 < 12)
|
|
|
|
/* Forward declarations. */
|
|
static PyTypeObject PyDateTime_DateType;
|
|
static PyTypeObject PyDateTime_DateTimeType;
|
|
static PyTypeObject PyDateTime_DeltaType;
|
|
static PyTypeObject PyDateTime_TimeType;
|
|
static PyTypeObject PyDateTime_TZInfoType;
|
|
|
|
/* ---------------------------------------------------------------------------
|
|
* Math utilities.
|
|
*/
|
|
|
|
/* k = i+j overflows iff k differs in sign from both inputs,
|
|
* iff k^i has sign bit set and k^j has sign bit set,
|
|
* iff (k^i)&(k^j) has sign bit set.
|
|
*/
|
|
#define SIGNED_ADD_OVERFLOWED(RESULT, I, J) \
|
|
((((RESULT) ^ (I)) & ((RESULT) ^ (J))) < 0)
|
|
|
|
/* Compute Python divmod(x, y), returning the quotient and storing the
|
|
* remainder into *r. The quotient is the floor of x/y, and that's
|
|
* the real point of this. C will probably truncate instead (C99
|
|
* requires truncation; C89 left it implementation-defined).
|
|
* Simplification: we *require* that y > 0 here. That's appropriate
|
|
* for all the uses made of it. This simplifies the code and makes
|
|
* the overflow case impossible (divmod(LONG_MIN, -1) is the only
|
|
* overflow case).
|
|
*/
|
|
static int
|
|
divmod(int x, int y, int *r)
|
|
{
|
|
int quo;
|
|
|
|
assert(y > 0);
|
|
quo = x / y;
|
|
*r = x - quo * y;
|
|
if (*r < 0) {
|
|
--quo;
|
|
*r += y;
|
|
}
|
|
assert(0 <= *r && *r < y);
|
|
return quo;
|
|
}
|
|
|
|
/* Round a double to the nearest long. |x| must be small enough to fit
|
|
* in a C long; this is not checked.
|
|
*/
|
|
static long
|
|
round_to_long(double x)
|
|
{
|
|
if (x >= 0.0)
|
|
x = floor(x + 0.5);
|
|
else
|
|
x = ceil(x - 0.5);
|
|
return (long)x;
|
|
}
|
|
|
|
/* ---------------------------------------------------------------------------
|
|
* General calendrical helper functions
|
|
*/
|
|
|
|
/* For each month ordinal in 1..12, the number of days in that month,
|
|
* and the number of days before that month in the same year. These
|
|
* are correct for non-leap years only.
|
|
*/
|
|
static int _days_in_month[] = {
|
|
0, /* unused; this vector uses 1-based indexing */
|
|
31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
|
|
};
|
|
|
|
static int _days_before_month[] = {
|
|
0, /* unused; this vector uses 1-based indexing */
|
|
0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
|
|
};
|
|
|
|
/* year -> 1 if leap year, else 0. */
|
|
static int
|
|
is_leap(int year)
|
|
{
|
|
/* Cast year to unsigned. The result is the same either way, but
|
|
* C can generate faster code for unsigned mod than for signed
|
|
* mod (especially for % 4 -- a good compiler should just grab
|
|
* the last 2 bits when the LHS is unsigned).
|
|
*/
|
|
const unsigned int ayear = (unsigned int)year;
|
|
return ayear % 4 == 0 && (ayear % 100 != 0 || ayear % 400 == 0);
|
|
}
|
|
|
|
/* year, month -> number of days in that month in that year */
|
|
static int
|
|
days_in_month(int year, int month)
|
|
{
|
|
assert(month >= 1);
|
|
assert(month <= 12);
|
|
if (month == 2 && is_leap(year))
|
|
return 29;
|
|
else
|
|
return _days_in_month[month];
|
|
}
|
|
|
|
/* year, month -> number of days in year preceeding first day of month */
|
|
static int
|
|
days_before_month(int year, int month)
|
|
{
|
|
int days;
|
|
|
|
assert(month >= 1);
|
|
assert(month <= 12);
|
|
days = _days_before_month[month];
|
|
if (month > 2 && is_leap(year))
|
|
++days;
|
|
return days;
|
|
}
|
|
|
|
/* year -> number of days before January 1st of year. Remember that we
|
|
* start with year 1, so days_before_year(1) == 0.
|
|
*/
|
|
static int
|
|
days_before_year(int year)
|
|
{
|
|
int y = year - 1;
|
|
/* This is incorrect if year <= 0; we really want the floor
|
|
* here. But so long as MINYEAR is 1, the smallest year this
|
|
* can see is 0 (this can happen in some normalization endcases),
|
|
* so we'll just special-case that.
|
|
*/
|
|
assert (year >= 0);
|
|
if (y >= 0)
|
|
return y*365 + y/4 - y/100 + y/400;
|
|
else {
|
|
assert(y == -1);
|
|
return -366;
|
|
}
|
|
}
|
|
|
|
/* Number of days in 4, 100, and 400 year cycles. That these have
|
|
* the correct values is asserted in the module init function.
|
|
*/
|
|
#define DI4Y 1461 /* days_before_year(5); days in 4 years */
|
|
#define DI100Y 36524 /* days_before_year(101); days in 100 years */
|
|
#define DI400Y 146097 /* days_before_year(401); days in 400 years */
|
|
|
|
/* ordinal -> year, month, day, considering 01-Jan-0001 as day 1. */
|
|
static void
|
|
ord_to_ymd(int ordinal, int *year, int *month, int *day)
|
|
{
|
|
int n, n1, n4, n100, n400, leapyear, preceding;
|
|
|
|
/* ordinal is a 1-based index, starting at 1-Jan-1. The pattern of
|
|
* leap years repeats exactly every 400 years. The basic strategy is
|
|
* to find the closest 400-year boundary at or before ordinal, then
|
|
* work with the offset from that boundary to ordinal. Life is much
|
|
* clearer if we subtract 1 from ordinal first -- then the values
|
|
* of ordinal at 400-year boundaries are exactly those divisible
|
|
* by DI400Y:
|
|
*
|
|
* D M Y n n-1
|
|
* -- --- ---- ---------- ----------------
|
|
* 31 Dec -400 -DI400Y -DI400Y -1
|
|
* 1 Jan -399 -DI400Y +1 -DI400Y 400-year boundary
|
|
* ...
|
|
* 30 Dec 000 -1 -2
|
|
* 31 Dec 000 0 -1
|
|
* 1 Jan 001 1 0 400-year boundary
|
|
* 2 Jan 001 2 1
|
|
* 3 Jan 001 3 2
|
|
* ...
|
|
* 31 Dec 400 DI400Y DI400Y -1
|
|
* 1 Jan 401 DI400Y +1 DI400Y 400-year boundary
|
|
*/
|
|
assert(ordinal >= 1);
|
|
--ordinal;
|
|
n400 = ordinal / DI400Y;
|
|
n = ordinal % DI400Y;
|
|
*year = n400 * 400 + 1;
|
|
|
|
/* Now n is the (non-negative) offset, in days, from January 1 of
|
|
* year, to the desired date. Now compute how many 100-year cycles
|
|
* precede n.
|
|
* Note that it's possible for n100 to equal 4! In that case 4 full
|
|
* 100-year cycles precede the desired day, which implies the
|
|
* desired day is December 31 at the end of a 400-year cycle.
|
|
*/
|
|
n100 = n / DI100Y;
|
|
n = n % DI100Y;
|
|
|
|
/* Now compute how many 4-year cycles precede it. */
|
|
n4 = n / DI4Y;
|
|
n = n % DI4Y;
|
|
|
|
/* And now how many single years. Again n1 can be 4, and again
|
|
* meaning that the desired day is December 31 at the end of the
|
|
* 4-year cycle.
|
|
*/
|
|
n1 = n / 365;
|
|
n = n % 365;
|
|
|
|
*year += n100 * 100 + n4 * 4 + n1;
|
|
if (n1 == 4 || n100 == 4) {
|
|
assert(n == 0);
|
|
*year -= 1;
|
|
*month = 12;
|
|
*day = 31;
|
|
return;
|
|
}
|
|
|
|
/* Now the year is correct, and n is the offset from January 1. We
|
|
* find the month via an estimate that's either exact or one too
|
|
* large.
|
|
*/
|
|
leapyear = n1 == 3 && (n4 != 24 || n100 == 3);
|
|
assert(leapyear == is_leap(*year));
|
|
*month = (n + 50) >> 5;
|
|
preceding = (_days_before_month[*month] + (*month > 2 && leapyear));
|
|
if (preceding > n) {
|
|
/* estimate is too large */
|
|
*month -= 1;
|
|
preceding -= days_in_month(*year, *month);
|
|
}
|
|
n -= preceding;
|
|
assert(0 <= n);
|
|
assert(n < days_in_month(*year, *month));
|
|
|
|
*day = n + 1;
|
|
}
|
|
|
|
/* year, month, day -> ordinal, considering 01-Jan-0001 as day 1. */
|
|
static int
|
|
ymd_to_ord(int year, int month, int day)
|
|
{
|
|
return days_before_year(year) + days_before_month(year, month) + day;
|
|
}
|
|
|
|
/* Day of week, where Monday==0, ..., Sunday==6. 1/1/1 was a Monday. */
|
|
static int
|
|
weekday(int year, int month, int day)
|
|
{
|
|
return (ymd_to_ord(year, month, day) + 6) % 7;
|
|
}
|
|
|
|
/* Ordinal of the Monday starting week 1 of the ISO year. Week 1 is the
|
|
* first calendar week containing a Thursday.
|
|
*/
|
|
static int
|
|
iso_week1_monday(int year)
|
|
{
|
|
int first_day = ymd_to_ord(year, 1, 1); /* ord of 1/1 */
|
|
/* 0 if 1/1 is a Monday, 1 if a Tue, etc. */
|
|
int first_weekday = (first_day + 6) % 7;
|
|
/* ordinal of closest Monday at or before 1/1 */
|
|
int week1_monday = first_day - first_weekday;
|
|
|
|
if (first_weekday > 3) /* if 1/1 was Fri, Sat, Sun */
|
|
week1_monday += 7;
|
|
return week1_monday;
|
|
}
|
|
|
|
/* ---------------------------------------------------------------------------
|
|
* Range checkers.
|
|
*/
|
|
|
|
/* Check that -MAX_DELTA_DAYS <= days <= MAX_DELTA_DAYS. If so, return 0.
|
|
* If not, raise OverflowError and return -1.
|
|
*/
|
|
static int
|
|
check_delta_day_range(int days)
|
|
{
|
|
if (-MAX_DELTA_DAYS <= days && days <= MAX_DELTA_DAYS)
|
|
return 0;
|
|
PyErr_Format(PyExc_OverflowError,
|
|
"days=%d; must have magnitude <= %d",
|
|
days, MAX_DELTA_DAYS);
|
|
return -1;
|
|
}
|
|
|
|
/* Check that date arguments are in range. Return 0 if they are. If they
|
|
* aren't, raise ValueError and return -1.
|
|
*/
|
|
static int
|
|
check_date_args(int year, int month, int day)
|
|
{
|
|
|
|
if (year < MINYEAR || year > MAXYEAR) {
|
|
PyErr_SetString(PyExc_ValueError,
|
|
"year is out of range");
|
|
return -1;
|
|
}
|
|
if (month < 1 || month > 12) {
|
|
PyErr_SetString(PyExc_ValueError,
|
|
"month must be in 1..12");
|
|
return -1;
|
|
}
|
|
if (day < 1 || day > days_in_month(year, month)) {
|
|
PyErr_SetString(PyExc_ValueError,
|
|
"day is out of range for month");
|
|
return -1;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
/* Check that time arguments are in range. Return 0 if they are. If they
|
|
* aren't, raise ValueError and return -1.
|
|
*/
|
|
static int
|
|
check_time_args(int h, int m, int s, int us)
|
|
{
|
|
if (h < 0 || h > 23) {
|
|
PyErr_SetString(PyExc_ValueError,
|
|
"hour must be in 0..23");
|
|
return -1;
|
|
}
|
|
if (m < 0 || m > 59) {
|
|
PyErr_SetString(PyExc_ValueError,
|
|
"minute must be in 0..59");
|
|
return -1;
|
|
}
|
|
if (s < 0 || s > 59) {
|
|
PyErr_SetString(PyExc_ValueError,
|
|
"second must be in 0..59");
|
|
return -1;
|
|
}
|
|
if (us < 0 || us > 999999) {
|
|
PyErr_SetString(PyExc_ValueError,
|
|
"microsecond must be in 0..999999");
|
|
return -1;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
/* ---------------------------------------------------------------------------
|
|
* Normalization utilities.
|
|
*/
|
|
|
|
/* One step of a mixed-radix conversion. A "hi" unit is equivalent to
|
|
* factor "lo" units. factor must be > 0. If *lo is less than 0, or
|
|
* at least factor, enough of *lo is converted into "hi" units so that
|
|
* 0 <= *lo < factor. The input values must be such that int overflow
|
|
* is impossible.
|
|
*/
|
|
static void
|
|
normalize_pair(int *hi, int *lo, int factor)
|
|
{
|
|
assert(factor > 0);
|
|
assert(lo != hi);
|
|
if (*lo < 0 || *lo >= factor) {
|
|
const int num_hi = divmod(*lo, factor, lo);
|
|
const int new_hi = *hi + num_hi;
|
|
assert(! SIGNED_ADD_OVERFLOWED(new_hi, *hi, num_hi));
|
|
*hi = new_hi;
|
|
}
|
|
assert(0 <= *lo && *lo < factor);
|
|
}
|
|
|
|
/* Fiddle days (d), seconds (s), and microseconds (us) so that
|
|
* 0 <= *s < 24*3600
|
|
* 0 <= *us < 1000000
|
|
* The input values must be such that the internals don't overflow.
|
|
* The way this routine is used, we don't get close.
|
|
*/
|
|
static void
|
|
normalize_d_s_us(int *d, int *s, int *us)
|
|
{
|
|
if (*us < 0 || *us >= 1000000) {
|
|
normalize_pair(s, us, 1000000);
|
|
/* |s| can't be bigger than about
|
|
* |original s| + |original us|/1000000 now.
|
|
*/
|
|
|
|
}
|
|
if (*s < 0 || *s >= 24*3600) {
|
|
normalize_pair(d, s, 24*3600);
|
|
/* |d| can't be bigger than about
|
|
* |original d| +
|
|
* (|original s| + |original us|/1000000) / (24*3600) now.
|
|
*/
|
|
}
|
|
assert(0 <= *s && *s < 24*3600);
|
|
assert(0 <= *us && *us < 1000000);
|
|
}
|
|
|
|
/* Fiddle years (y), months (m), and days (d) so that
|
|
* 1 <= *m <= 12
|
|
* 1 <= *d <= days_in_month(*y, *m)
|
|
* The input values must be such that the internals don't overflow.
|
|
* The way this routine is used, we don't get close.
|
|
*/
|
|
static void
|
|
normalize_y_m_d(int *y, int *m, int *d)
|
|
{
|
|
int dim; /* # of days in month */
|
|
|
|
/* This gets muddy: the proper range for day can't be determined
|
|
* without knowing the correct month and year, but if day is, e.g.,
|
|
* plus or minus a million, the current month and year values make
|
|
* no sense (and may also be out of bounds themselves).
|
|
* Saying 12 months == 1 year should be non-controversial.
|
|
*/
|
|
if (*m < 1 || *m > 12) {
|
|
--*m;
|
|
normalize_pair(y, m, 12);
|
|
++*m;
|
|
/* |y| can't be bigger than about
|
|
* |original y| + |original m|/12 now.
|
|
*/
|
|
}
|
|
assert(1 <= *m && *m <= 12);
|
|
|
|
/* Now only day can be out of bounds (year may also be out of bounds
|
|
* for a datetime object, but we don't care about that here).
|
|
* If day is out of bounds, what to do is arguable, but at least the
|
|
* method here is principled and explainable.
|
|
*/
|
|
dim = days_in_month(*y, *m);
|
|
if (*d < 1 || *d > dim) {
|
|
/* Move day-1 days from the first of the month. First try to
|
|
* get off cheap if we're only one day out of range
|
|
* (adjustments for timezone alone can't be worse than that).
|
|
*/
|
|
if (*d == 0) {
|
|
--*m;
|
|
if (*m > 0)
|
|
*d = days_in_month(*y, *m);
|
|
else {
|
|
--*y;
|
|
*m = 12;
|
|
*d = 31;
|
|
}
|
|
}
|
|
else if (*d == dim + 1) {
|
|
/* move forward a day */
|
|
++*m;
|
|
*d = 1;
|
|
if (*m > 12) {
|
|
*m = 1;
|
|
++*y;
|
|
}
|
|
}
|
|
else {
|
|
int ordinal = ymd_to_ord(*y, *m, 1) +
|
|
*d - 1;
|
|
ord_to_ymd(ordinal, y, m, d);
|
|
}
|
|
}
|
|
assert(*m > 0);
|
|
assert(*d > 0);
|
|
}
|
|
|
|
/* Fiddle out-of-bounds months and days so that the result makes some kind
|
|
* of sense. The parameters are both inputs and outputs. Returns < 0 on
|
|
* failure, where failure means the adjusted year is out of bounds.
|
|
*/
|
|
static int
|
|
normalize_date(int *year, int *month, int *day)
|
|
{
|
|
int result;
|
|
|
|
normalize_y_m_d(year, month, day);
|
|
if (MINYEAR <= *year && *year <= MAXYEAR)
|
|
result = 0;
|
|
else {
|
|
PyErr_SetString(PyExc_OverflowError,
|
|
"date value out of range");
|
|
result = -1;
|
|
}
|
|
return result;
|
|
}
|
|
|
|
/* Force all the datetime fields into range. The parameters are both
|
|
* inputs and outputs. Returns < 0 on error.
|
|
*/
|
|
static int
|
|
normalize_datetime(int *year, int *month, int *day,
|
|
int *hour, int *minute, int *second,
|
|
int *microsecond)
|
|
{
|
|
normalize_pair(second, microsecond, 1000000);
|
|
normalize_pair(minute, second, 60);
|
|
normalize_pair(hour, minute, 60);
|
|
normalize_pair(day, hour, 24);
|
|
return normalize_date(year, month, day);
|
|
}
|
|
|
|
/* ---------------------------------------------------------------------------
|
|
* Basic object allocation: tp_alloc implementations. These allocate
|
|
* Python objects of the right size and type, and do the Python object-
|
|
* initialization bit. If there's not enough memory, they return NULL after
|
|
* setting MemoryError. All data members remain uninitialized trash.
|
|
*
|
|
* We abuse the tp_alloc "nitems" argument to communicate whether a tzinfo
|
|
* member is needed. This is ugly, imprecise, and possibly insecure.
|
|
* tp_basicsize for the time and datetime types is set to the size of the
|
|
* struct that has room for the tzinfo member, so subclasses in Python will
|
|
* allocate enough space for a tzinfo member whether or not one is actually
|
|
* needed. That's the "ugly and imprecise" parts. The "possibly insecure"
|
|
* part is that PyType_GenericAlloc() (which subclasses in Python end up
|
|
* using) just happens today to effectively ignore the nitems argument
|
|
* when tp_itemsize is 0, which it is for these type objects. If that
|
|
* changes, perhaps the callers of tp_alloc slots in this file should
|
|
* be changed to force a 0 nitems argument unless the type being allocated
|
|
* is a base type implemented in this file (so that tp_alloc is time_alloc
|
|
* or datetime_alloc below, which know about the nitems abuse).
|
|
*/
|
|
|
|
static PyObject *
|
|
time_alloc(PyTypeObject *type, Py_ssize_t aware)
|
|
{
|
|
PyObject *self;
|
|
|
|
self = (PyObject *)
|
|
PyObject_MALLOC(aware ?
|
|
sizeof(PyDateTime_Time) :
|
|
sizeof(_PyDateTime_BaseTime));
|
|
if (self == NULL)
|
|
return (PyObject *)PyErr_NoMemory();
|
|
PyObject_INIT(self, type);
|
|
return self;
|
|
}
|
|
|
|
static PyObject *
|
|
datetime_alloc(PyTypeObject *type, Py_ssize_t aware)
|
|
{
|
|
PyObject *self;
|
|
|
|
self = (PyObject *)
|
|
PyObject_MALLOC(aware ?
|
|
sizeof(PyDateTime_DateTime) :
|
|
sizeof(_PyDateTime_BaseDateTime));
|
|
if (self == NULL)
|
|
return (PyObject *)PyErr_NoMemory();
|
|
PyObject_INIT(self, type);
|
|
return self;
|
|
}
|
|
|
|
/* ---------------------------------------------------------------------------
|
|
* Helpers for setting object fields. These work on pointers to the
|
|
* appropriate base class.
|
|
*/
|
|
|
|
/* For date and datetime. */
|
|
static void
|
|
set_date_fields(PyDateTime_Date *self, int y, int m, int d)
|
|
{
|
|
self->hashcode = -1;
|
|
SET_YEAR(self, y);
|
|
SET_MONTH(self, m);
|
|
SET_DAY(self, d);
|
|
}
|
|
|
|
/* ---------------------------------------------------------------------------
|
|
* Create various objects, mostly without range checking.
|
|
*/
|
|
|
|
/* Create a date instance with no range checking. */
|
|
static PyObject *
|
|
new_date_ex(int year, int month, int day, PyTypeObject *type)
|
|
{
|
|
PyDateTime_Date *self;
|
|
|
|
self = (PyDateTime_Date *) (type->tp_alloc(type, 0));
|
|
if (self != NULL)
|
|
set_date_fields(self, year, month, day);
|
|
return (PyObject *) self;
|
|
}
|
|
|
|
#define new_date(year, month, day) \
|
|
new_date_ex(year, month, day, &PyDateTime_DateType)
|
|
|
|
/* Create a datetime instance with no range checking. */
|
|
static PyObject *
|
|
new_datetime_ex(int year, int month, int day, int hour, int minute,
|
|
int second, int usecond, PyObject *tzinfo, PyTypeObject *type)
|
|
{
|
|
PyDateTime_DateTime *self;
|
|
char aware = tzinfo != Py_None;
|
|
|
|
self = (PyDateTime_DateTime *) (type->tp_alloc(type, aware));
|
|
if (self != NULL) {
|
|
self->hastzinfo = aware;
|
|
set_date_fields((PyDateTime_Date *)self, year, month, day);
|
|
DATE_SET_HOUR(self, hour);
|
|
DATE_SET_MINUTE(self, minute);
|
|
DATE_SET_SECOND(self, second);
|
|
DATE_SET_MICROSECOND(self, usecond);
|
|
if (aware) {
|
|
Py_INCREF(tzinfo);
|
|
self->tzinfo = tzinfo;
|
|
}
|
|
}
|
|
return (PyObject *)self;
|
|
}
|
|
|
|
#define new_datetime(y, m, d, hh, mm, ss, us, tzinfo) \
|
|
new_datetime_ex(y, m, d, hh, mm, ss, us, tzinfo, \
|
|
&PyDateTime_DateTimeType)
|
|
|
|
/* Create a time instance with no range checking. */
|
|
static PyObject *
|
|
new_time_ex(int hour, int minute, int second, int usecond,
|
|
PyObject *tzinfo, PyTypeObject *type)
|
|
{
|
|
PyDateTime_Time *self;
|
|
char aware = tzinfo != Py_None;
|
|
|
|
self = (PyDateTime_Time *) (type->tp_alloc(type, aware));
|
|
if (self != NULL) {
|
|
self->hastzinfo = aware;
|
|
self->hashcode = -1;
|
|
TIME_SET_HOUR(self, hour);
|
|
TIME_SET_MINUTE(self, minute);
|
|
TIME_SET_SECOND(self, second);
|
|
TIME_SET_MICROSECOND(self, usecond);
|
|
if (aware) {
|
|
Py_INCREF(tzinfo);
|
|
self->tzinfo = tzinfo;
|
|
}
|
|
}
|
|
return (PyObject *)self;
|
|
}
|
|
|
|
#define new_time(hh, mm, ss, us, tzinfo) \
|
|
new_time_ex(hh, mm, ss, us, tzinfo, &PyDateTime_TimeType)
|
|
|
|
/* Create a timedelta instance. Normalize the members iff normalize is
|
|
* true. Passing false is a speed optimization, if you know for sure
|
|
* that seconds and microseconds are already in their proper ranges. In any
|
|
* case, raises OverflowError and returns NULL if the normalized days is out
|
|
* of range).
|
|
*/
|
|
static PyObject *
|
|
new_delta_ex(int days, int seconds, int microseconds, int normalize,
|
|
PyTypeObject *type)
|
|
{
|
|
PyDateTime_Delta *self;
|
|
|
|
if (normalize)
|
|
normalize_d_s_us(&days, &seconds, µseconds);
|
|
assert(0 <= seconds && seconds < 24*3600);
|
|
assert(0 <= microseconds && microseconds < 1000000);
|
|
|
|
if (check_delta_day_range(days) < 0)
|
|
return NULL;
|
|
|
|
self = (PyDateTime_Delta *) (type->tp_alloc(type, 0));
|
|
if (self != NULL) {
|
|
self->hashcode = -1;
|
|
SET_TD_DAYS(self, days);
|
|
SET_TD_SECONDS(self, seconds);
|
|
SET_TD_MICROSECONDS(self, microseconds);
|
|
}
|
|
return (PyObject *) self;
|
|
}
|
|
|
|
#define new_delta(d, s, us, normalize) \
|
|
new_delta_ex(d, s, us, normalize, &PyDateTime_DeltaType)
|
|
|
|
/* ---------------------------------------------------------------------------
|
|
* tzinfo helpers.
|
|
*/
|
|
|
|
/* Ensure that p is None or of a tzinfo subclass. Return 0 if OK; if not
|
|
* raise TypeError and return -1.
|
|
*/
|
|
static int
|
|
check_tzinfo_subclass(PyObject *p)
|
|
{
|
|
if (p == Py_None || PyTZInfo_Check(p))
|
|
return 0;
|
|
PyErr_Format(PyExc_TypeError,
|
|
"tzinfo argument must be None or of a tzinfo subclass, "
|
|
"not type '%s'",
|
|
p->ob_type->tp_name);
|
|
return -1;
|
|
}
|
|
|
|
/* Return tzinfo.methname(tzinfoarg), without any checking of results.
|
|
* If tzinfo is None, returns None.
|
|
*/
|
|
static PyObject *
|
|
call_tzinfo_method(PyObject *tzinfo, char *methname, PyObject *tzinfoarg)
|
|
{
|
|
PyObject *result;
|
|
|
|
assert(tzinfo && methname && tzinfoarg);
|
|
assert(check_tzinfo_subclass(tzinfo) >= 0);
|
|
if (tzinfo == Py_None) {
|
|
result = Py_None;
|
|
Py_INCREF(result);
|
|
}
|
|
else
|
|
result = PyObject_CallMethod(tzinfo, methname, "O", tzinfoarg);
|
|
return result;
|
|
}
|
|
|
|
/* If self has a tzinfo member, return a BORROWED reference to it. Else
|
|
* return NULL, which is NOT AN ERROR. There are no error returns here,
|
|
* and the caller must not decref the result.
|
|
*/
|
|
static PyObject *
|
|
get_tzinfo_member(PyObject *self)
|
|
{
|
|
PyObject *tzinfo = NULL;
|
|
|
|
if (PyDateTime_Check(self) && HASTZINFO(self))
|
|
tzinfo = ((PyDateTime_DateTime *)self)->tzinfo;
|
|
else if (PyTime_Check(self) && HASTZINFO(self))
|
|
tzinfo = ((PyDateTime_Time *)self)->tzinfo;
|
|
|
|
return tzinfo;
|
|
}
|
|
|
|
/* Call getattr(tzinfo, name)(tzinfoarg), and extract an int from the
|
|
* result. tzinfo must be an instance of the tzinfo class. If the method
|
|
* returns None, this returns 0 and sets *none to 1. If the method doesn't
|
|
* return None or timedelta, TypeError is raised and this returns -1. If it
|
|
* returnsa timedelta and the value is out of range or isn't a whole number
|
|
* of minutes, ValueError is raised and this returns -1.
|
|
* Else *none is set to 0 and the integer method result is returned.
|
|
*/
|
|
static int
|
|
call_utc_tzinfo_method(PyObject *tzinfo, char *name, PyObject *tzinfoarg,
|
|
int *none)
|
|
{
|
|
PyObject *u;
|
|
int result = -1;
|
|
|
|
assert(tzinfo != NULL);
|
|
assert(PyTZInfo_Check(tzinfo));
|
|
assert(tzinfoarg != NULL);
|
|
|
|
*none = 0;
|
|
u = call_tzinfo_method(tzinfo, name, tzinfoarg);
|
|
if (u == NULL)
|
|
return -1;
|
|
|
|
else if (u == Py_None) {
|
|
result = 0;
|
|
*none = 1;
|
|
}
|
|
else if (PyDelta_Check(u)) {
|
|
const int days = GET_TD_DAYS(u);
|
|
if (days < -1 || days > 0)
|
|
result = 24*60; /* trigger ValueError below */
|
|
else {
|
|
/* next line can't overflow because we know days
|
|
* is -1 or 0 now
|
|
*/
|
|
int ss = days * 24 * 3600 + GET_TD_SECONDS(u);
|
|
result = divmod(ss, 60, &ss);
|
|
if (ss || GET_TD_MICROSECONDS(u)) {
|
|
PyErr_Format(PyExc_ValueError,
|
|
"tzinfo.%s() must return a "
|
|
"whole number of minutes",
|
|
name);
|
|
result = -1;
|
|
}
|
|
}
|
|
}
|
|
else {
|
|
PyErr_Format(PyExc_TypeError,
|
|
"tzinfo.%s() must return None or "
|
|
"timedelta, not '%s'",
|
|
name, u->ob_type->tp_name);
|
|
}
|
|
|
|
Py_DECREF(u);
|
|
if (result < -1439 || result > 1439) {
|
|
PyErr_Format(PyExc_ValueError,
|
|
"tzinfo.%s() returned %d; must be in "
|
|
"-1439 .. 1439",
|
|
name, result);
|
|
result = -1;
|
|
}
|
|
return result;
|
|
}
|
|
|
|
/* Call tzinfo.utcoffset(tzinfoarg), and extract an integer from the
|
|
* result. tzinfo must be an instance of the tzinfo class. If utcoffset()
|
|
* returns None, call_utcoffset returns 0 and sets *none to 1. If uctoffset()
|
|
* doesn't return None or timedelta, TypeError is raised and this returns -1.
|
|
* If utcoffset() returns an invalid timedelta (out of range, or not a whole
|
|
* # of minutes), ValueError is raised and this returns -1. Else *none is
|
|
* set to 0 and the offset is returned (as int # of minutes east of UTC).
|
|
*/
|
|
static int
|
|
call_utcoffset(PyObject *tzinfo, PyObject *tzinfoarg, int *none)
|
|
{
|
|
return call_utc_tzinfo_method(tzinfo, "utcoffset", tzinfoarg, none);
|
|
}
|
|
|
|
/* Call tzinfo.name(tzinfoarg), and return the offset as a timedelta or None.
|
|
*/
|
|
static PyObject *
|
|
offset_as_timedelta(PyObject *tzinfo, char *name, PyObject *tzinfoarg) {
|
|
PyObject *result;
|
|
|
|
assert(tzinfo && name && tzinfoarg);
|
|
if (tzinfo == Py_None) {
|
|
result = Py_None;
|
|
Py_INCREF(result);
|
|
}
|
|
else {
|
|
int none;
|
|
int offset = call_utc_tzinfo_method(tzinfo, name, tzinfoarg,
|
|
&none);
|
|
if (offset < 0 && PyErr_Occurred())
|
|
return NULL;
|
|
if (none) {
|
|
result = Py_None;
|
|
Py_INCREF(result);
|
|
}
|
|
else
|
|
result = new_delta(0, offset * 60, 0, 1);
|
|
}
|
|
return result;
|
|
}
|
|
|
|
/* Call tzinfo.dst(tzinfoarg), and extract an integer from the
|
|
* result. tzinfo must be an instance of the tzinfo class. If dst()
|
|
* returns None, call_dst returns 0 and sets *none to 1. If dst()
|
|
& doesn't return None or timedelta, TypeError is raised and this
|
|
* returns -1. If dst() returns an invalid timedelta for a UTC offset,
|
|
* ValueError is raised and this returns -1. Else *none is set to 0 and
|
|
* the offset is returned (as an int # of minutes east of UTC).
|
|
*/
|
|
static int
|
|
call_dst(PyObject *tzinfo, PyObject *tzinfoarg, int *none)
|
|
{
|
|
return call_utc_tzinfo_method(tzinfo, "dst", tzinfoarg, none);
|
|
}
|
|
|
|
/* Call tzinfo.tzname(tzinfoarg), and return the result. tzinfo must be
|
|
* an instance of the tzinfo class or None. If tzinfo isn't None, and
|
|
* tzname() doesn't return None or a string, TypeError is raised and this
|
|
* returns NULL.
|
|
*/
|
|
static PyObject *
|
|
call_tzname(PyObject *tzinfo, PyObject *tzinfoarg)
|
|
{
|
|
PyObject *result;
|
|
|
|
assert(tzinfo != NULL);
|
|
assert(check_tzinfo_subclass(tzinfo) >= 0);
|
|
assert(tzinfoarg != NULL);
|
|
|
|
if (tzinfo == Py_None) {
|
|
result = Py_None;
|
|
Py_INCREF(result);
|
|
}
|
|
else
|
|
result = PyObject_CallMethod(tzinfo, "tzname", "O", tzinfoarg);
|
|
|
|
if (result != NULL && result != Py_None && ! PyString_Check(result)) {
|
|
PyErr_Format(PyExc_TypeError, "tzinfo.tzname() must "
|
|
"return None or a string, not '%s'",
|
|
result->ob_type->tp_name);
|
|
Py_DECREF(result);
|
|
result = NULL;
|
|
}
|
|
return result;
|
|
}
|
|
|
|
typedef enum {
|
|
/* an exception has been set; the caller should pass it on */
|
|
OFFSET_ERROR,
|
|
|
|
/* type isn't date, datetime, or time subclass */
|
|
OFFSET_UNKNOWN,
|
|
|
|
/* date,
|
|
* datetime with !hastzinfo
|
|
* datetime with None tzinfo,
|
|
* datetime where utcoffset() returns None
|
|
* time with !hastzinfo
|
|
* time with None tzinfo,
|
|
* time where utcoffset() returns None
|
|
*/
|
|
OFFSET_NAIVE,
|
|
|
|
/* time or datetime where utcoffset() doesn't return None */
|
|
OFFSET_AWARE
|
|
} naivety;
|
|
|
|
/* Classify an object as to whether it's naive or offset-aware. See
|
|
* the "naivety" typedef for details. If the type is aware, *offset is set
|
|
* to minutes east of UTC (as returned by the tzinfo.utcoffset() method).
|
|
* If the type is offset-naive (or unknown, or error), *offset is set to 0.
|
|
* tzinfoarg is the argument to pass to the tzinfo.utcoffset() method.
|
|
*/
|
|
static naivety
|
|
classify_utcoffset(PyObject *op, PyObject *tzinfoarg, int *offset)
|
|
{
|
|
int none;
|
|
PyObject *tzinfo;
|
|
|
|
assert(tzinfoarg != NULL);
|
|
*offset = 0;
|
|
tzinfo = get_tzinfo_member(op); /* NULL means no tzinfo, not error */
|
|
if (tzinfo == Py_None)
|
|
return OFFSET_NAIVE;
|
|
if (tzinfo == NULL) {
|
|
/* note that a datetime passes the PyDate_Check test */
|
|
return (PyTime_Check(op) || PyDate_Check(op)) ?
|
|
OFFSET_NAIVE : OFFSET_UNKNOWN;
|
|
}
|
|
*offset = call_utcoffset(tzinfo, tzinfoarg, &none);
|
|
if (*offset == -1 && PyErr_Occurred())
|
|
return OFFSET_ERROR;
|
|
return none ? OFFSET_NAIVE : OFFSET_AWARE;
|
|
}
|
|
|
|
/* Classify two objects as to whether they're naive or offset-aware.
|
|
* This isn't quite the same as calling classify_utcoffset() twice: for
|
|
* binary operations (comparison and subtraction), we generally want to
|
|
* ignore the tzinfo members if they're identical. This is by design,
|
|
* so that results match "naive" expectations when mixing objects from a
|
|
* single timezone. So in that case, this sets both offsets to 0 and
|
|
* both naiveties to OFFSET_NAIVE.
|
|
* The function returns 0 if everything's OK, and -1 on error.
|
|
*/
|
|
static int
|
|
classify_two_utcoffsets(PyObject *o1, int *offset1, naivety *n1,
|
|
PyObject *tzinfoarg1,
|
|
PyObject *o2, int *offset2, naivety *n2,
|
|
PyObject *tzinfoarg2)
|
|
{
|
|
if (get_tzinfo_member(o1) == get_tzinfo_member(o2)) {
|
|
*offset1 = *offset2 = 0;
|
|
*n1 = *n2 = OFFSET_NAIVE;
|
|
}
|
|
else {
|
|
*n1 = classify_utcoffset(o1, tzinfoarg1, offset1);
|
|
if (*n1 == OFFSET_ERROR)
|
|
return -1;
|
|
*n2 = classify_utcoffset(o2, tzinfoarg2, offset2);
|
|
if (*n2 == OFFSET_ERROR)
|
|
return -1;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
/* repr is like "someclass(arg1, arg2)". If tzinfo isn't None,
|
|
* stuff
|
|
* ", tzinfo=" + repr(tzinfo)
|
|
* before the closing ")".
|
|
*/
|
|
static PyObject *
|
|
append_keyword_tzinfo(PyObject *repr, PyObject *tzinfo)
|
|
{
|
|
PyObject *temp;
|
|
|
|
assert(PyString_Check(repr));
|
|
assert(tzinfo);
|
|
if (tzinfo == Py_None)
|
|
return repr;
|
|
/* Get rid of the trailing ')'. */
|
|
assert(PyString_AsString(repr)[PyString_Size(repr)-1] == ')');
|
|
temp = PyString_FromStringAndSize(PyString_AsString(repr),
|
|
PyString_Size(repr) - 1);
|
|
Py_DECREF(repr);
|
|
if (temp == NULL)
|
|
return NULL;
|
|
repr = temp;
|
|
|
|
/* Append ", tzinfo=". */
|
|
PyString_ConcatAndDel(&repr, PyString_FromString(", tzinfo="));
|
|
|
|
/* Append repr(tzinfo). */
|
|
PyString_ConcatAndDel(&repr, PyObject_Repr(tzinfo));
|
|
|
|
/* Add a closing paren. */
|
|
PyString_ConcatAndDel(&repr, PyString_FromString(")"));
|
|
return repr;
|
|
}
|
|
|
|
/* ---------------------------------------------------------------------------
|
|
* String format helpers.
|
|
*/
|
|
|
|
static PyObject *
|
|
format_ctime(PyDateTime_Date *date, int hours, int minutes, int seconds)
|
|
{
|
|
static const char *DayNames[] = {
|
|
"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"
|
|
};
|
|
static const char *MonthNames[] = {
|
|
"Jan", "Feb", "Mar", "Apr", "May", "Jun",
|
|
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
|
|
};
|
|
|
|
char buffer[128];
|
|
int wday = weekday(GET_YEAR(date), GET_MONTH(date), GET_DAY(date));
|
|
|
|
PyOS_snprintf(buffer, sizeof(buffer), "%s %s %2d %02d:%02d:%02d %04d",
|
|
DayNames[wday], MonthNames[GET_MONTH(date) - 1],
|
|
GET_DAY(date), hours, minutes, seconds,
|
|
GET_YEAR(date));
|
|
return PyString_FromString(buffer);
|
|
}
|
|
|
|
/* Add an hours & minutes UTC offset string to buf. buf has no more than
|
|
* buflen bytes remaining. The UTC offset is gotten by calling
|
|
* tzinfo.uctoffset(tzinfoarg). If that returns None, \0 is stored into
|
|
* *buf, and that's all. Else the returned value is checked for sanity (an
|
|
* integer in range), and if that's OK it's converted to an hours & minutes
|
|
* string of the form
|
|
* sign HH sep MM
|
|
* Returns 0 if everything is OK. If the return value from utcoffset() is
|
|
* bogus, an appropriate exception is set and -1 is returned.
|
|
*/
|
|
static int
|
|
format_utcoffset(char *buf, size_t buflen, const char *sep,
|
|
PyObject *tzinfo, PyObject *tzinfoarg)
|
|
{
|
|
int offset;
|
|
int hours;
|
|
int minutes;
|
|
char sign;
|
|
int none;
|
|
|
|
offset = call_utcoffset(tzinfo, tzinfoarg, &none);
|
|
if (offset == -1 && PyErr_Occurred())
|
|
return -1;
|
|
if (none) {
|
|
*buf = '\0';
|
|
return 0;
|
|
}
|
|
sign = '+';
|
|
if (offset < 0) {
|
|
sign = '-';
|
|
offset = - offset;
|
|
}
|
|
hours = divmod(offset, 60, &minutes);
|
|
PyOS_snprintf(buf, buflen, "%c%02d%s%02d", sign, hours, sep, minutes);
|
|
return 0;
|
|
}
|
|
|
|
/* I sure don't want to reproduce the strftime code from the time module,
|
|
* so this imports the module and calls it. All the hair is due to
|
|
* giving special meanings to the %z and %Z format codes via a preprocessing
|
|
* step on the format string.
|
|
* tzinfoarg is the argument to pass to the object's tzinfo method, if
|
|
* needed.
|
|
*/
|
|
static PyObject *
|
|
wrap_strftime(PyObject *object, PyObject *format, PyObject *timetuple,
|
|
PyObject *tzinfoarg)
|
|
{
|
|
PyObject *result = NULL; /* guilty until proved innocent */
|
|
|
|
PyObject *zreplacement = NULL; /* py string, replacement for %z */
|
|
PyObject *Zreplacement = NULL; /* py string, replacement for %Z */
|
|
|
|
char *pin; /* pointer to next char in input format */
|
|
char ch; /* next char in input format */
|
|
|
|
PyObject *newfmt = NULL; /* py string, the output format */
|
|
char *pnew; /* pointer to available byte in output format */
|
|
int totalnew; /* number bytes total in output format buffer,
|
|
exclusive of trailing \0 */
|
|
int usednew; /* number bytes used so far in output format buffer */
|
|
|
|
char *ptoappend; /* pointer to string to append to output buffer */
|
|
int ntoappend; /* # of bytes to append to output buffer */
|
|
|
|
assert(object && format && timetuple);
|
|
assert(PyString_Check(format));
|
|
|
|
/* Give up if the year is before 1900.
|
|
* Python strftime() plays games with the year, and different
|
|
* games depending on whether envar PYTHON2K is set. This makes
|
|
* years before 1900 a nightmare, even if the platform strftime
|
|
* supports them (and not all do).
|
|
* We could get a lot farther here by avoiding Python's strftime
|
|
* wrapper and calling the C strftime() directly, but that isn't
|
|
* an option in the Python implementation of this module.
|
|
*/
|
|
{
|
|
long year;
|
|
PyObject *pyyear = PySequence_GetItem(timetuple, 0);
|
|
if (pyyear == NULL) return NULL;
|
|
assert(PyInt_Check(pyyear));
|
|
year = PyInt_AsLong(pyyear);
|
|
Py_DECREF(pyyear);
|
|
if (year < 1900) {
|
|
PyErr_Format(PyExc_ValueError, "year=%ld is before "
|
|
"1900; the datetime strftime() "
|
|
"methods require year >= 1900",
|
|
year);
|
|
return NULL;
|
|
}
|
|
}
|
|
|
|
/* Scan the input format, looking for %z and %Z escapes, building
|
|
* a new format. Since computing the replacements for those codes
|
|
* is expensive, don't unless they're actually used.
|
|
*/
|
|
totalnew = PyString_Size(format) + 1; /* realistic if no %z/%Z */
|
|
newfmt = PyString_FromStringAndSize(NULL, totalnew);
|
|
if (newfmt == NULL) goto Done;
|
|
pnew = PyString_AsString(newfmt);
|
|
usednew = 0;
|
|
|
|
pin = PyString_AsString(format);
|
|
while ((ch = *pin++) != '\0') {
|
|
if (ch != '%') {
|
|
ptoappend = pin - 1;
|
|
ntoappend = 1;
|
|
}
|
|
else if ((ch = *pin++) == '\0') {
|
|
/* There's a lone trailing %; doesn't make sense. */
|
|
PyErr_SetString(PyExc_ValueError, "strftime format "
|
|
"ends with raw %");
|
|
goto Done;
|
|
}
|
|
/* A % has been seen and ch is the character after it. */
|
|
else if (ch == 'z') {
|
|
if (zreplacement == NULL) {
|
|
/* format utcoffset */
|
|
char buf[100];
|
|
PyObject *tzinfo = get_tzinfo_member(object);
|
|
zreplacement = PyString_FromString("");
|
|
if (zreplacement == NULL) goto Done;
|
|
if (tzinfo != Py_None && tzinfo != NULL) {
|
|
assert(tzinfoarg != NULL);
|
|
if (format_utcoffset(buf,
|
|
sizeof(buf),
|
|
"",
|
|
tzinfo,
|
|
tzinfoarg) < 0)
|
|
goto Done;
|
|
Py_DECREF(zreplacement);
|
|
zreplacement = PyString_FromString(buf);
|
|
if (zreplacement == NULL) goto Done;
|
|
}
|
|
}
|
|
assert(zreplacement != NULL);
|
|
ptoappend = PyString_AS_STRING(zreplacement);
|
|
ntoappend = PyString_GET_SIZE(zreplacement);
|
|
}
|
|
else if (ch == 'Z') {
|
|
/* format tzname */
|
|
if (Zreplacement == NULL) {
|
|
PyObject *tzinfo = get_tzinfo_member(object);
|
|
Zreplacement = PyString_FromString("");
|
|
if (Zreplacement == NULL) goto Done;
|
|
if (tzinfo != Py_None && tzinfo != NULL) {
|
|
PyObject *temp;
|
|
assert(tzinfoarg != NULL);
|
|
temp = call_tzname(tzinfo, tzinfoarg);
|
|
if (temp == NULL) goto Done;
|
|
if (temp != Py_None) {
|
|
assert(PyString_Check(temp));
|
|
/* Since the tzname is getting
|
|
* stuffed into the format, we
|
|
* have to double any % signs
|
|
* so that strftime doesn't
|
|
* treat them as format codes.
|
|
*/
|
|
Py_DECREF(Zreplacement);
|
|
Zreplacement = PyObject_CallMethod(
|
|
temp, "replace",
|
|
"ss", "%", "%%");
|
|
Py_DECREF(temp);
|
|
if (Zreplacement == NULL)
|
|
goto Done;
|
|
if (!PyString_Check(Zreplacement)) {
|
|
PyErr_SetString(PyExc_TypeError, "tzname.replace() did not return a string");
|
|
goto Done;
|
|
}
|
|
}
|
|
else
|
|
Py_DECREF(temp);
|
|
}
|
|
}
|
|
assert(Zreplacement != NULL);
|
|
ptoappend = PyString_AS_STRING(Zreplacement);
|
|
ntoappend = PyString_GET_SIZE(Zreplacement);
|
|
}
|
|
else {
|
|
/* percent followed by neither z nor Z */
|
|
ptoappend = pin - 2;
|
|
ntoappend = 2;
|
|
}
|
|
|
|
/* Append the ntoappend chars starting at ptoappend to
|
|
* the new format.
|
|
*/
|
|
assert(ptoappend != NULL);
|
|
assert(ntoappend >= 0);
|
|
if (ntoappend == 0)
|
|
continue;
|
|
while (usednew + ntoappend > totalnew) {
|
|
int bigger = totalnew << 1;
|
|
if ((bigger >> 1) != totalnew) { /* overflow */
|
|
PyErr_NoMemory();
|
|
goto Done;
|
|
}
|
|
if (_PyString_Resize(&newfmt, bigger) < 0)
|
|
goto Done;
|
|
totalnew = bigger;
|
|
pnew = PyString_AsString(newfmt) + usednew;
|
|
}
|
|
memcpy(pnew, ptoappend, ntoappend);
|
|
pnew += ntoappend;
|
|
usednew += ntoappend;
|
|
assert(usednew <= totalnew);
|
|
} /* end while() */
|
|
|
|
if (_PyString_Resize(&newfmt, usednew) < 0)
|
|
goto Done;
|
|
{
|
|
PyObject *time = PyImport_ImportModule("time");
|
|
if (time == NULL)
|
|
goto Done;
|
|
result = PyObject_CallMethod(time, "strftime", "OO",
|
|
newfmt, timetuple);
|
|
Py_DECREF(time);
|
|
}
|
|
Done:
|
|
Py_XDECREF(zreplacement);
|
|
Py_XDECREF(Zreplacement);
|
|
Py_XDECREF(newfmt);
|
|
return result;
|
|
}
|
|
|
|
static char *
|
|
isoformat_date(PyDateTime_Date *dt, char buffer[], int bufflen)
|
|
{
|
|
int x;
|
|
x = PyOS_snprintf(buffer, bufflen,
|
|
"%04d-%02d-%02d",
|
|
GET_YEAR(dt), GET_MONTH(dt), GET_DAY(dt));
|
|
return buffer + x;
|
|
}
|
|
|
|
static void
|
|
isoformat_time(PyDateTime_DateTime *dt, char buffer[], int bufflen)
|
|
{
|
|
int us = DATE_GET_MICROSECOND(dt);
|
|
|
|
PyOS_snprintf(buffer, bufflen,
|
|
"%02d:%02d:%02d", /* 8 characters */
|
|
DATE_GET_HOUR(dt),
|
|
DATE_GET_MINUTE(dt),
|
|
DATE_GET_SECOND(dt));
|
|
if (us)
|
|
PyOS_snprintf(buffer + 8, bufflen - 8, ".%06d", us);
|
|
}
|
|
|
|
/* ---------------------------------------------------------------------------
|
|
* Wrap functions from the time module. These aren't directly available
|
|
* from C. Perhaps they should be.
|
|
*/
|
|
|
|
/* Call time.time() and return its result (a Python float). */
|
|
static PyObject *
|
|
time_time(void)
|
|
{
|
|
PyObject *result = NULL;
|
|
PyObject *time = PyImport_ImportModule("time");
|
|
|
|
if (time != NULL) {
|
|
result = PyObject_CallMethod(time, "time", "()");
|
|
Py_DECREF(time);
|
|
}
|
|
return result;
|
|
}
|
|
|
|
/* Build a time.struct_time. The weekday and day number are automatically
|
|
* computed from the y,m,d args.
|
|
*/
|
|
static PyObject *
|
|
build_struct_time(int y, int m, int d, int hh, int mm, int ss, int dstflag)
|
|
{
|
|
PyObject *time;
|
|
PyObject *result = NULL;
|
|
|
|
time = PyImport_ImportModule("time");
|
|
if (time != NULL) {
|
|
result = PyObject_CallMethod(time, "struct_time",
|
|
"((iiiiiiiii))",
|
|
y, m, d,
|
|
hh, mm, ss,
|
|
weekday(y, m, d),
|
|
days_before_month(y, m) + d,
|
|
dstflag);
|
|
Py_DECREF(time);
|
|
}
|
|
return result;
|
|
}
|
|
|
|
/* ---------------------------------------------------------------------------
|
|
* Miscellaneous helpers.
|
|
*/
|
|
|
|
/* For various reasons, we need to use tp_richcompare instead of tp_compare.
|
|
* The comparisons here all most naturally compute a cmp()-like result.
|
|
* This little helper turns that into a bool result for rich comparisons.
|
|
*/
|
|
static PyObject *
|
|
diff_to_bool(int diff, int op)
|
|
{
|
|
PyObject *result;
|
|
int istrue;
|
|
|
|
switch (op) {
|
|
case Py_EQ: istrue = diff == 0; break;
|
|
case Py_NE: istrue = diff != 0; break;
|
|
case Py_LE: istrue = diff <= 0; break;
|
|
case Py_GE: istrue = diff >= 0; break;
|
|
case Py_LT: istrue = diff < 0; break;
|
|
case Py_GT: istrue = diff > 0; break;
|
|
default:
|
|
assert(! "op unknown");
|
|
istrue = 0; /* To shut up compiler */
|
|
}
|
|
result = istrue ? Py_True : Py_False;
|
|
Py_INCREF(result);
|
|
return result;
|
|
}
|
|
|
|
/* Raises a "can't compare" TypeError and returns NULL. */
|
|
static PyObject *
|
|
cmperror(PyObject *a, PyObject *b)
|
|
{
|
|
PyErr_Format(PyExc_TypeError,
|
|
"can't compare %s to %s",
|
|
a->ob_type->tp_name, b->ob_type->tp_name);
|
|
return NULL;
|
|
}
|
|
|
|
/* ---------------------------------------------------------------------------
|
|
* Cached Python objects; these are set by the module init function.
|
|
*/
|
|
|
|
/* Conversion factors. */
|
|
static PyObject *us_per_us = NULL; /* 1 */
|
|
static PyObject *us_per_ms = NULL; /* 1000 */
|
|
static PyObject *us_per_second = NULL; /* 1000000 */
|
|
static PyObject *us_per_minute = NULL; /* 1e6 * 60 as Python int */
|
|
static PyObject *us_per_hour = NULL; /* 1e6 * 3600 as Python long */
|
|
static PyObject *us_per_day = NULL; /* 1e6 * 3600 * 24 as Python long */
|
|
static PyObject *us_per_week = NULL; /* 1e6*3600*24*7 as Python long */
|
|
static PyObject *seconds_per_day = NULL; /* 3600*24 as Python int */
|
|
|
|
/* ---------------------------------------------------------------------------
|
|
* Class implementations.
|
|
*/
|
|
|
|
/*
|
|
* PyDateTime_Delta implementation.
|
|
*/
|
|
|
|
/* Convert a timedelta to a number of us,
|
|
* (24*3600*self.days + self.seconds)*1000000 + self.microseconds
|
|
* as a Python int or long.
|
|
* Doing mixed-radix arithmetic by hand instead is excruciating in C,
|
|
* due to ubiquitous overflow possibilities.
|
|
*/
|
|
static PyObject *
|
|
delta_to_microseconds(PyDateTime_Delta *self)
|
|
{
|
|
PyObject *x1 = NULL;
|
|
PyObject *x2 = NULL;
|
|
PyObject *x3 = NULL;
|
|
PyObject *result = NULL;
|
|
|
|
x1 = PyInt_FromLong(GET_TD_DAYS(self));
|
|
if (x1 == NULL)
|
|
goto Done;
|
|
x2 = PyNumber_Multiply(x1, seconds_per_day); /* days in seconds */
|
|
if (x2 == NULL)
|
|
goto Done;
|
|
Py_DECREF(x1);
|
|
x1 = NULL;
|
|
|
|
/* x2 has days in seconds */
|
|
x1 = PyInt_FromLong(GET_TD_SECONDS(self)); /* seconds */
|
|
if (x1 == NULL)
|
|
goto Done;
|
|
x3 = PyNumber_Add(x1, x2); /* days and seconds in seconds */
|
|
if (x3 == NULL)
|
|
goto Done;
|
|
Py_DECREF(x1);
|
|
Py_DECREF(x2);
|
|
x1 = x2 = NULL;
|
|
|
|
/* x3 has days+seconds in seconds */
|
|
x1 = PyNumber_Multiply(x3, us_per_second); /* us */
|
|
if (x1 == NULL)
|
|
goto Done;
|
|
Py_DECREF(x3);
|
|
x3 = NULL;
|
|
|
|
/* x1 has days+seconds in us */
|
|
x2 = PyInt_FromLong(GET_TD_MICROSECONDS(self));
|
|
if (x2 == NULL)
|
|
goto Done;
|
|
result = PyNumber_Add(x1, x2);
|
|
|
|
Done:
|
|
Py_XDECREF(x1);
|
|
Py_XDECREF(x2);
|
|
Py_XDECREF(x3);
|
|
return result;
|
|
}
|
|
|
|
/* Convert a number of us (as a Python int or long) to a timedelta.
|
|
*/
|
|
static PyObject *
|
|
microseconds_to_delta_ex(PyObject *pyus, PyTypeObject *type)
|
|
{
|
|
int us;
|
|
int s;
|
|
int d;
|
|
long temp;
|
|
|
|
PyObject *tuple = NULL;
|
|
PyObject *num = NULL;
|
|
PyObject *result = NULL;
|
|
|
|
tuple = PyNumber_Divmod(pyus, us_per_second);
|
|
if (tuple == NULL)
|
|
goto Done;
|
|
|
|
num = PyTuple_GetItem(tuple, 1); /* us */
|
|
if (num == NULL)
|
|
goto Done;
|
|
temp = PyLong_AsLong(num);
|
|
num = NULL;
|
|
if (temp == -1 && PyErr_Occurred())
|
|
goto Done;
|
|
assert(0 <= temp && temp < 1000000);
|
|
us = (int)temp;
|
|
if (us < 0) {
|
|
/* The divisor was positive, so this must be an error. */
|
|
assert(PyErr_Occurred());
|
|
goto Done;
|
|
}
|
|
|
|
num = PyTuple_GetItem(tuple, 0); /* leftover seconds */
|
|
if (num == NULL)
|
|
goto Done;
|
|
Py_INCREF(num);
|
|
Py_DECREF(tuple);
|
|
|
|
tuple = PyNumber_Divmod(num, seconds_per_day);
|
|
if (tuple == NULL)
|
|
goto Done;
|
|
Py_DECREF(num);
|
|
|
|
num = PyTuple_GetItem(tuple, 1); /* seconds */
|
|
if (num == NULL)
|
|
goto Done;
|
|
temp = PyLong_AsLong(num);
|
|
num = NULL;
|
|
if (temp == -1 && PyErr_Occurred())
|
|
goto Done;
|
|
assert(0 <= temp && temp < 24*3600);
|
|
s = (int)temp;
|
|
|
|
if (s < 0) {
|
|
/* The divisor was positive, so this must be an error. */
|
|
assert(PyErr_Occurred());
|
|
goto Done;
|
|
}
|
|
|
|
num = PyTuple_GetItem(tuple, 0); /* leftover days */
|
|
if (num == NULL)
|
|
goto Done;
|
|
Py_INCREF(num);
|
|
temp = PyLong_AsLong(num);
|
|
if (temp == -1 && PyErr_Occurred())
|
|
goto Done;
|
|
d = (int)temp;
|
|
if ((long)d != temp) {
|
|
PyErr_SetString(PyExc_OverflowError, "normalized days too "
|
|
"large to fit in a C int");
|
|
goto Done;
|
|
}
|
|
result = new_delta_ex(d, s, us, 0, type);
|
|
|
|
Done:
|
|
Py_XDECREF(tuple);
|
|
Py_XDECREF(num);
|
|
return result;
|
|
}
|
|
|
|
#define microseconds_to_delta(pymicros) \
|
|
microseconds_to_delta_ex(pymicros, &PyDateTime_DeltaType)
|
|
|
|
static PyObject *
|
|
multiply_int_timedelta(PyObject *intobj, PyDateTime_Delta *delta)
|
|
{
|
|
PyObject *pyus_in;
|
|
PyObject *pyus_out;
|
|
PyObject *result;
|
|
|
|
pyus_in = delta_to_microseconds(delta);
|
|
if (pyus_in == NULL)
|
|
return NULL;
|
|
|
|
pyus_out = PyNumber_Multiply(pyus_in, intobj);
|
|
Py_DECREF(pyus_in);
|
|
if (pyus_out == NULL)
|
|
return NULL;
|
|
|
|
result = microseconds_to_delta(pyus_out);
|
|
Py_DECREF(pyus_out);
|
|
return result;
|
|
}
|
|
|
|
static PyObject *
|
|
divide_timedelta_int(PyDateTime_Delta *delta, PyObject *intobj)
|
|
{
|
|
PyObject *pyus_in;
|
|
PyObject *pyus_out;
|
|
PyObject *result;
|
|
|
|
pyus_in = delta_to_microseconds(delta);
|
|
if (pyus_in == NULL)
|
|
return NULL;
|
|
|
|
pyus_out = PyNumber_FloorDivide(pyus_in, intobj);
|
|
Py_DECREF(pyus_in);
|
|
if (pyus_out == NULL)
|
|
return NULL;
|
|
|
|
result = microseconds_to_delta(pyus_out);
|
|
Py_DECREF(pyus_out);
|
|
return result;
|
|
}
|
|
|
|
static PyObject *
|
|
delta_add(PyObject *left, PyObject *right)
|
|
{
|
|
PyObject *result = Py_NotImplemented;
|
|
|
|
if (PyDelta_Check(left) && PyDelta_Check(right)) {
|
|
/* delta + delta */
|
|
/* The C-level additions can't overflow because of the
|
|
* invariant bounds.
|
|
*/
|
|
int days = GET_TD_DAYS(left) + GET_TD_DAYS(right);
|
|
int seconds = GET_TD_SECONDS(left) + GET_TD_SECONDS(right);
|
|
int microseconds = GET_TD_MICROSECONDS(left) +
|
|
GET_TD_MICROSECONDS(right);
|
|
result = new_delta(days, seconds, microseconds, 1);
|
|
}
|
|
|
|
if (result == Py_NotImplemented)
|
|
Py_INCREF(result);
|
|
return result;
|
|
}
|
|
|
|
static PyObject *
|
|
delta_negative(PyDateTime_Delta *self)
|
|
{
|
|
return new_delta(-GET_TD_DAYS(self),
|
|
-GET_TD_SECONDS(self),
|
|
-GET_TD_MICROSECONDS(self),
|
|
1);
|
|
}
|
|
|
|
static PyObject *
|
|
delta_positive(PyDateTime_Delta *self)
|
|
{
|
|
/* Could optimize this (by returning self) if this isn't a
|
|
* subclass -- but who uses unary + ? Approximately nobody.
|
|
*/
|
|
return new_delta(GET_TD_DAYS(self),
|
|
GET_TD_SECONDS(self),
|
|
GET_TD_MICROSECONDS(self),
|
|
0);
|
|
}
|
|
|
|
static PyObject *
|
|
delta_abs(PyDateTime_Delta *self)
|
|
{
|
|
PyObject *result;
|
|
|
|
assert(GET_TD_MICROSECONDS(self) >= 0);
|
|
assert(GET_TD_SECONDS(self) >= 0);
|
|
|
|
if (GET_TD_DAYS(self) < 0)
|
|
result = delta_negative(self);
|
|
else
|
|
result = delta_positive(self);
|
|
|
|
return result;
|
|
}
|
|
|
|
static PyObject *
|
|
delta_subtract(PyObject *left, PyObject *right)
|
|
{
|
|
PyObject *result = Py_NotImplemented;
|
|
|
|
if (PyDelta_Check(left) && PyDelta_Check(right)) {
|
|
/* delta - delta */
|
|
PyObject *minus_right = PyNumber_Negative(right);
|
|
if (minus_right) {
|
|
result = delta_add(left, minus_right);
|
|
Py_DECREF(minus_right);
|
|
}
|
|
else
|
|
result = NULL;
|
|
}
|
|
|
|
if (result == Py_NotImplemented)
|
|
Py_INCREF(result);
|
|
return result;
|
|
}
|
|
|
|
static PyObject *
|
|
delta_richcompare(PyObject *self, PyObject *other, int op)
|
|
{
|
|
if (PyDelta_Check(other)) {
|
|
int diff = GET_TD_DAYS(self) - GET_TD_DAYS(other);
|
|
if (diff == 0) {
|
|
diff = GET_TD_SECONDS(self) - GET_TD_SECONDS(other);
|
|
if (diff == 0)
|
|
diff = GET_TD_MICROSECONDS(self) -
|
|
GET_TD_MICROSECONDS(other);
|
|
}
|
|
return diff_to_bool(diff, op);
|
|
}
|
|
else {
|
|
Py_INCREF(Py_NotImplemented);
|
|
return Py_NotImplemented;
|
|
}
|
|
}
|
|
|
|
static PyObject *delta_getstate(PyDateTime_Delta *self);
|
|
|
|
static long
|
|
delta_hash(PyDateTime_Delta *self)
|
|
{
|
|
if (self->hashcode == -1) {
|
|
PyObject *temp = delta_getstate(self);
|
|
if (temp != NULL) {
|
|
self->hashcode = PyObject_Hash(temp);
|
|
Py_DECREF(temp);
|
|
}
|
|
}
|
|
return self->hashcode;
|
|
}
|
|
|
|
static PyObject *
|
|
delta_multiply(PyObject *left, PyObject *right)
|
|
{
|
|
PyObject *result = Py_NotImplemented;
|
|
|
|
if (PyDelta_Check(left)) {
|
|
/* delta * ??? */
|
|
if (PyInt_Check(right) || PyLong_Check(right))
|
|
result = multiply_int_timedelta(right,
|
|
(PyDateTime_Delta *) left);
|
|
}
|
|
else if (PyInt_Check(left) || PyLong_Check(left))
|
|
result = multiply_int_timedelta(left,
|
|
(PyDateTime_Delta *) right);
|
|
|
|
if (result == Py_NotImplemented)
|
|
Py_INCREF(result);
|
|
return result;
|
|
}
|
|
|
|
static PyObject *
|
|
delta_divide(PyObject *left, PyObject *right)
|
|
{
|
|
PyObject *result = Py_NotImplemented;
|
|
|
|
if (PyDelta_Check(left)) {
|
|
/* delta * ??? */
|
|
if (PyInt_Check(right) || PyLong_Check(right))
|
|
result = divide_timedelta_int(
|
|
(PyDateTime_Delta *)left,
|
|
right);
|
|
}
|
|
|
|
if (result == Py_NotImplemented)
|
|
Py_INCREF(result);
|
|
return result;
|
|
}
|
|
|
|
/* Fold in the value of the tag ("seconds", "weeks", etc) component of a
|
|
* timedelta constructor. sofar is the # of microseconds accounted for
|
|
* so far, and there are factor microseconds per current unit, the number
|
|
* of which is given by num. num * factor is added to sofar in a
|
|
* numerically careful way, and that's the result. Any fractional
|
|
* microseconds left over (this can happen if num is a float type) are
|
|
* added into *leftover.
|
|
* Note that there are many ways this can give an error (NULL) return.
|
|
*/
|
|
static PyObject *
|
|
accum(const char* tag, PyObject *sofar, PyObject *num, PyObject *factor,
|
|
double *leftover)
|
|
{
|
|
PyObject *prod;
|
|
PyObject *sum;
|
|
|
|
assert(num != NULL);
|
|
|
|
if (PyInt_Check(num) || PyLong_Check(num)) {
|
|
prod = PyNumber_Multiply(num, factor);
|
|
if (prod == NULL)
|
|
return NULL;
|
|
sum = PyNumber_Add(sofar, prod);
|
|
Py_DECREF(prod);
|
|
return sum;
|
|
}
|
|
|
|
if (PyFloat_Check(num)) {
|
|
double dnum;
|
|
double fracpart;
|
|
double intpart;
|
|
PyObject *x;
|
|
PyObject *y;
|
|
|
|
/* The Plan: decompose num into an integer part and a
|
|
* fractional part, num = intpart + fracpart.
|
|
* Then num * factor ==
|
|
* intpart * factor + fracpart * factor
|
|
* and the LHS can be computed exactly in long arithmetic.
|
|
* The RHS is again broken into an int part and frac part.
|
|
* and the frac part is added into *leftover.
|
|
*/
|
|
dnum = PyFloat_AsDouble(num);
|
|
if (dnum == -1.0 && PyErr_Occurred())
|
|
return NULL;
|
|
fracpart = modf(dnum, &intpart);
|
|
x = PyLong_FromDouble(intpart);
|
|
if (x == NULL)
|
|
return NULL;
|
|
|
|
prod = PyNumber_Multiply(x, factor);
|
|
Py_DECREF(x);
|
|
if (prod == NULL)
|
|
return NULL;
|
|
|
|
sum = PyNumber_Add(sofar, prod);
|
|
Py_DECREF(prod);
|
|
if (sum == NULL)
|
|
return NULL;
|
|
|
|
if (fracpart == 0.0)
|
|
return sum;
|
|
/* So far we've lost no information. Dealing with the
|
|
* fractional part requires float arithmetic, and may
|
|
* lose a little info.
|
|
*/
|
|
assert(PyInt_Check(factor) || PyLong_Check(factor));
|
|
if (PyInt_Check(factor))
|
|
dnum = (double)PyInt_AsLong(factor);
|
|
else
|
|
dnum = PyLong_AsDouble(factor);
|
|
|
|
dnum *= fracpart;
|
|
fracpart = modf(dnum, &intpart);
|
|
x = PyLong_FromDouble(intpart);
|
|
if (x == NULL) {
|
|
Py_DECREF(sum);
|
|
return NULL;
|
|
}
|
|
|
|
y = PyNumber_Add(sum, x);
|
|
Py_DECREF(sum);
|
|
Py_DECREF(x);
|
|
*leftover += fracpart;
|
|
return y;
|
|
}
|
|
|
|
PyErr_Format(PyExc_TypeError,
|
|
"unsupported type for timedelta %s component: %s",
|
|
tag, num->ob_type->tp_name);
|
|
return NULL;
|
|
}
|
|
|
|
static PyObject *
|
|
delta_new(PyTypeObject *type, PyObject *args, PyObject *kw)
|
|
{
|
|
PyObject *self = NULL;
|
|
|
|
/* Argument objects. */
|
|
PyObject *day = NULL;
|
|
PyObject *second = NULL;
|
|
PyObject *us = NULL;
|
|
PyObject *ms = NULL;
|
|
PyObject *minute = NULL;
|
|
PyObject *hour = NULL;
|
|
PyObject *week = NULL;
|
|
|
|
PyObject *x = NULL; /* running sum of microseconds */
|
|
PyObject *y = NULL; /* temp sum of microseconds */
|
|
double leftover_us = 0.0;
|
|
|
|
static char *keywords[] = {
|
|
"days", "seconds", "microseconds", "milliseconds",
|
|
"minutes", "hours", "weeks", NULL
|
|
};
|
|
|
|
if (PyArg_ParseTupleAndKeywords(args, kw, "|OOOOOOO:__new__",
|
|
keywords,
|
|
&day, &second, &us,
|
|
&ms, &minute, &hour, &week) == 0)
|
|
goto Done;
|
|
|
|
x = PyInt_FromLong(0);
|
|
if (x == NULL)
|
|
goto Done;
|
|
|
|
#define CLEANUP \
|
|
Py_DECREF(x); \
|
|
x = y; \
|
|
if (x == NULL) \
|
|
goto Done
|
|
|
|
if (us) {
|
|
y = accum("microseconds", x, us, us_per_us, &leftover_us);
|
|
CLEANUP;
|
|
}
|
|
if (ms) {
|
|
y = accum("milliseconds", x, ms, us_per_ms, &leftover_us);
|
|
CLEANUP;
|
|
}
|
|
if (second) {
|
|
y = accum("seconds", x, second, us_per_second, &leftover_us);
|
|
CLEANUP;
|
|
}
|
|
if (minute) {
|
|
y = accum("minutes", x, minute, us_per_minute, &leftover_us);
|
|
CLEANUP;
|
|
}
|
|
if (hour) {
|
|
y = accum("hours", x, hour, us_per_hour, &leftover_us);
|
|
CLEANUP;
|
|
}
|
|
if (day) {
|
|
y = accum("days", x, day, us_per_day, &leftover_us);
|
|
CLEANUP;
|
|
}
|
|
if (week) {
|
|
y = accum("weeks", x, week, us_per_week, &leftover_us);
|
|
CLEANUP;
|
|
}
|
|
if (leftover_us) {
|
|
/* Round to nearest whole # of us, and add into x. */
|
|
PyObject *temp = PyLong_FromLong(round_to_long(leftover_us));
|
|
if (temp == NULL) {
|
|
Py_DECREF(x);
|
|
goto Done;
|
|
}
|
|
y = PyNumber_Add(x, temp);
|
|
Py_DECREF(temp);
|
|
CLEANUP;
|
|
}
|
|
|
|
self = microseconds_to_delta_ex(x, type);
|
|
Py_DECREF(x);
|
|
Done:
|
|
return self;
|
|
|
|
#undef CLEANUP
|
|
}
|
|
|
|
static int
|
|
delta_bool(PyDateTime_Delta *self)
|
|
{
|
|
return (GET_TD_DAYS(self) != 0
|
|
|| GET_TD_SECONDS(self) != 0
|
|
|| GET_TD_MICROSECONDS(self) != 0);
|
|
}
|
|
|
|
static PyObject *
|
|
delta_repr(PyDateTime_Delta *self)
|
|
{
|
|
if (GET_TD_MICROSECONDS(self) != 0)
|
|
return PyString_FromFormat("%s(%d, %d, %d)",
|
|
self->ob_type->tp_name,
|
|
GET_TD_DAYS(self),
|
|
GET_TD_SECONDS(self),
|
|
GET_TD_MICROSECONDS(self));
|
|
if (GET_TD_SECONDS(self) != 0)
|
|
return PyString_FromFormat("%s(%d, %d)",
|
|
self->ob_type->tp_name,
|
|
GET_TD_DAYS(self),
|
|
GET_TD_SECONDS(self));
|
|
|
|
return PyString_FromFormat("%s(%d)",
|
|
self->ob_type->tp_name,
|
|
GET_TD_DAYS(self));
|
|
}
|
|
|
|
static PyObject *
|
|
delta_str(PyDateTime_Delta *self)
|
|
{
|
|
int days = GET_TD_DAYS(self);
|
|
int seconds = GET_TD_SECONDS(self);
|
|
int us = GET_TD_MICROSECONDS(self);
|
|
int hours;
|
|
int minutes;
|
|
char buf[100];
|
|
char *pbuf = buf;
|
|
size_t buflen = sizeof(buf);
|
|
int n;
|
|
|
|
minutes = divmod(seconds, 60, &seconds);
|
|
hours = divmod(minutes, 60, &minutes);
|
|
|
|
if (days) {
|
|
n = PyOS_snprintf(pbuf, buflen, "%d day%s, ", days,
|
|
(days == 1 || days == -1) ? "" : "s");
|
|
if (n < 0 || (size_t)n >= buflen)
|
|
goto Fail;
|
|
pbuf += n;
|
|
buflen -= (size_t)n;
|
|
}
|
|
|
|
n = PyOS_snprintf(pbuf, buflen, "%d:%02d:%02d",
|
|
hours, minutes, seconds);
|
|
if (n < 0 || (size_t)n >= buflen)
|
|
goto Fail;
|
|
pbuf += n;
|
|
buflen -= (size_t)n;
|
|
|
|
if (us) {
|
|
n = PyOS_snprintf(pbuf, buflen, ".%06d", us);
|
|
if (n < 0 || (size_t)n >= buflen)
|
|
goto Fail;
|
|
pbuf += n;
|
|
}
|
|
|
|
return PyString_FromStringAndSize(buf, pbuf - buf);
|
|
|
|
Fail:
|
|
PyErr_SetString(PyExc_SystemError, "goofy result from PyOS_snprintf");
|
|
return NULL;
|
|
}
|
|
|
|
/* Pickle support, a simple use of __reduce__. */
|
|
|
|
/* __getstate__ isn't exposed */
|
|
static PyObject *
|
|
delta_getstate(PyDateTime_Delta *self)
|
|
{
|
|
return Py_BuildValue("iii", GET_TD_DAYS(self),
|
|
GET_TD_SECONDS(self),
|
|
GET_TD_MICROSECONDS(self));
|
|
}
|
|
|
|
static PyObject *
|
|
delta_reduce(PyDateTime_Delta* self)
|
|
{
|
|
return Py_BuildValue("ON", self->ob_type, delta_getstate(self));
|
|
}
|
|
|
|
#define OFFSET(field) offsetof(PyDateTime_Delta, field)
|
|
|
|
static PyMemberDef delta_members[] = {
|
|
|
|
{"days", T_INT, OFFSET(days), READONLY,
|
|
PyDoc_STR("Number of days.")},
|
|
|
|
{"seconds", T_INT, OFFSET(seconds), READONLY,
|
|
PyDoc_STR("Number of seconds (>= 0 and less than 1 day).")},
|
|
|
|
{"microseconds", T_INT, OFFSET(microseconds), READONLY,
|
|
PyDoc_STR("Number of microseconds (>= 0 and less than 1 second).")},
|
|
{NULL}
|
|
};
|
|
|
|
static PyMethodDef delta_methods[] = {
|
|
{"__reduce__", (PyCFunction)delta_reduce, METH_NOARGS,
|
|
PyDoc_STR("__reduce__() -> (cls, state)")},
|
|
|
|
{NULL, NULL},
|
|
};
|
|
|
|
static char delta_doc[] =
|
|
PyDoc_STR("Difference between two datetime values.");
|
|
|
|
static PyNumberMethods delta_as_number = {
|
|
delta_add, /* nb_add */
|
|
delta_subtract, /* nb_subtract */
|
|
delta_multiply, /* nb_multiply */
|
|
0, /* nb_remainder */
|
|
0, /* nb_divmod */
|
|
0, /* nb_power */
|
|
(unaryfunc)delta_negative, /* nb_negative */
|
|
(unaryfunc)delta_positive, /* nb_positive */
|
|
(unaryfunc)delta_abs, /* nb_absolute */
|
|
(inquiry)delta_bool, /* nb_bool */
|
|
0, /*nb_invert*/
|
|
0, /*nb_lshift*/
|
|
0, /*nb_rshift*/
|
|
0, /*nb_and*/
|
|
0, /*nb_xor*/
|
|
0, /*nb_or*/
|
|
0, /*nb_coerce*/
|
|
0, /*nb_int*/
|
|
0, /*nb_long*/
|
|
0, /*nb_float*/
|
|
0, /*nb_oct*/
|
|
0, /*nb_hex*/
|
|
0, /*nb_inplace_add*/
|
|
0, /*nb_inplace_subtract*/
|
|
0, /*nb_inplace_multiply*/
|
|
0, /*nb_inplace_remainder*/
|
|
0, /*nb_inplace_power*/
|
|
0, /*nb_inplace_lshift*/
|
|
0, /*nb_inplace_rshift*/
|
|
0, /*nb_inplace_and*/
|
|
0, /*nb_inplace_xor*/
|
|
0, /*nb_inplace_or*/
|
|
delta_divide, /* nb_floor_divide */
|
|
0, /* nb_true_divide */
|
|
0, /* nb_inplace_floor_divide */
|
|
0, /* nb_inplace_true_divide */
|
|
};
|
|
|
|
static PyTypeObject PyDateTime_DeltaType = {
|
|
PyObject_HEAD_INIT(NULL)
|
|
0, /* ob_size */
|
|
"datetime.timedelta", /* tp_name */
|
|
sizeof(PyDateTime_Delta), /* tp_basicsize */
|
|
0, /* tp_itemsize */
|
|
0, /* tp_dealloc */
|
|
0, /* tp_print */
|
|
0, /* tp_getattr */
|
|
0, /* tp_setattr */
|
|
0, /* tp_compare */
|
|
(reprfunc)delta_repr, /* tp_repr */
|
|
&delta_as_number, /* tp_as_number */
|
|
0, /* tp_as_sequence */
|
|
0, /* tp_as_mapping */
|
|
(hashfunc)delta_hash, /* tp_hash */
|
|
0, /* tp_call */
|
|
(reprfunc)delta_str, /* tp_str */
|
|
PyObject_GenericGetAttr, /* tp_getattro */
|
|
0, /* tp_setattro */
|
|
0, /* tp_as_buffer */
|
|
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
|
|
delta_doc, /* tp_doc */
|
|
0, /* tp_traverse */
|
|
0, /* tp_clear */
|
|
delta_richcompare, /* tp_richcompare */
|
|
0, /* tp_weaklistoffset */
|
|
0, /* tp_iter */
|
|
0, /* tp_iternext */
|
|
delta_methods, /* tp_methods */
|
|
delta_members, /* tp_members */
|
|
0, /* tp_getset */
|
|
0, /* tp_base */
|
|
0, /* tp_dict */
|
|
0, /* tp_descr_get */
|
|
0, /* tp_descr_set */
|
|
0, /* tp_dictoffset */
|
|
0, /* tp_init */
|
|
0, /* tp_alloc */
|
|
delta_new, /* tp_new */
|
|
0, /* tp_free */
|
|
};
|
|
|
|
/*
|
|
* PyDateTime_Date implementation.
|
|
*/
|
|
|
|
/* Accessor properties. */
|
|
|
|
static PyObject *
|
|
date_year(PyDateTime_Date *self, void *unused)
|
|
{
|
|
return PyInt_FromLong(GET_YEAR(self));
|
|
}
|
|
|
|
static PyObject *
|
|
date_month(PyDateTime_Date *self, void *unused)
|
|
{
|
|
return PyInt_FromLong(GET_MONTH(self));
|
|
}
|
|
|
|
static PyObject *
|
|
date_day(PyDateTime_Date *self, void *unused)
|
|
{
|
|
return PyInt_FromLong(GET_DAY(self));
|
|
}
|
|
|
|
static PyGetSetDef date_getset[] = {
|
|
{"year", (getter)date_year},
|
|
{"month", (getter)date_month},
|
|
{"day", (getter)date_day},
|
|
{NULL}
|
|
};
|
|
|
|
/* Constructors. */
|
|
|
|
static char *date_kws[] = {"year", "month", "day", NULL};
|
|
|
|
static PyObject *
|
|
date_new(PyTypeObject *type, PyObject *args, PyObject *kw)
|
|
{
|
|
PyObject *self = NULL;
|
|
PyObject *state;
|
|
int year;
|
|
int month;
|
|
int day;
|
|
|
|
/* Check for invocation from pickle with __getstate__ state */
|
|
if (PyTuple_GET_SIZE(args) == 1 &&
|
|
PyString_Check(state = PyTuple_GET_ITEM(args, 0)) &&
|
|
PyString_GET_SIZE(state) == _PyDateTime_DATE_DATASIZE &&
|
|
MONTH_IS_SANE(PyString_AS_STRING(state)[2]))
|
|
{
|
|
PyDateTime_Date *me;
|
|
|
|
me = (PyDateTime_Date *) (type->tp_alloc(type, 0));
|
|
if (me != NULL) {
|
|
char *pdata = PyString_AS_STRING(state);
|
|
memcpy(me->data, pdata, _PyDateTime_DATE_DATASIZE);
|
|
me->hashcode = -1;
|
|
}
|
|
return (PyObject *)me;
|
|
}
|
|
|
|
if (PyArg_ParseTupleAndKeywords(args, kw, "iii", date_kws,
|
|
&year, &month, &day)) {
|
|
if (check_date_args(year, month, day) < 0)
|
|
return NULL;
|
|
self = new_date_ex(year, month, day, type);
|
|
}
|
|
return self;
|
|
}
|
|
|
|
/* Return new date from localtime(t). */
|
|
static PyObject *
|
|
date_local_from_time_t(PyObject *cls, double ts)
|
|
{
|
|
struct tm *tm;
|
|
time_t t;
|
|
PyObject *result = NULL;
|
|
|
|
t = _PyTime_DoubleToTimet(ts);
|
|
if (t == (time_t)-1 && PyErr_Occurred())
|
|
return NULL;
|
|
tm = localtime(&t);
|
|
if (tm)
|
|
result = PyObject_CallFunction(cls, "iii",
|
|
tm->tm_year + 1900,
|
|
tm->tm_mon + 1,
|
|
tm->tm_mday);
|
|
else
|
|
PyErr_SetString(PyExc_ValueError,
|
|
"timestamp out of range for "
|
|
"platform localtime() function");
|
|
return result;
|
|
}
|
|
|
|
/* Return new date from current time.
|
|
* We say this is equivalent to fromtimestamp(time.time()), and the
|
|
* only way to be sure of that is to *call* time.time(). That's not
|
|
* generally the same as calling C's time.
|
|
*/
|
|
static PyObject *
|
|
date_today(PyObject *cls, PyObject *dummy)
|
|
{
|
|
PyObject *time;
|
|
PyObject *result;
|
|
|
|
time = time_time();
|
|
if (time == NULL)
|
|
return NULL;
|
|
|
|
/* Note well: today() is a class method, so this may not call
|
|
* date.fromtimestamp. For example, it may call
|
|
* datetime.fromtimestamp. That's why we need all the accuracy
|
|
* time.time() delivers; if someone were gonzo about optimization,
|
|
* date.today() could get away with plain C time().
|
|
*/
|
|
result = PyObject_CallMethod(cls, "fromtimestamp", "O", time);
|
|
Py_DECREF(time);
|
|
return result;
|
|
}
|
|
|
|
/* Return new date from given timestamp (Python timestamp -- a double). */
|
|
static PyObject *
|
|
date_fromtimestamp(PyObject *cls, PyObject *args)
|
|
{
|
|
double timestamp;
|
|
PyObject *result = NULL;
|
|
|
|
if (PyArg_ParseTuple(args, "d:fromtimestamp", ×tamp))
|
|
result = date_local_from_time_t(cls, timestamp);
|
|
return result;
|
|
}
|
|
|
|
/* Return new date from proleptic Gregorian ordinal. Raises ValueError if
|
|
* the ordinal is out of range.
|
|
*/
|
|
static PyObject *
|
|
date_fromordinal(PyObject *cls, PyObject *args)
|
|
{
|
|
PyObject *result = NULL;
|
|
int ordinal;
|
|
|
|
if (PyArg_ParseTuple(args, "i:fromordinal", &ordinal)) {
|
|
int year;
|
|
int month;
|
|
int day;
|
|
|
|
if (ordinal < 1)
|
|
PyErr_SetString(PyExc_ValueError, "ordinal must be "
|
|
">= 1");
|
|
else {
|
|
ord_to_ymd(ordinal, &year, &month, &day);
|
|
result = PyObject_CallFunction(cls, "iii",
|
|
year, month, day);
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
/*
|
|
* Date arithmetic.
|
|
*/
|
|
|
|
/* date + timedelta -> date. If arg negate is true, subtract the timedelta
|
|
* instead.
|
|
*/
|
|
static PyObject *
|
|
add_date_timedelta(PyDateTime_Date *date, PyDateTime_Delta *delta, int negate)
|
|
{
|
|
PyObject *result = NULL;
|
|
int year = GET_YEAR(date);
|
|
int month = GET_MONTH(date);
|
|
int deltadays = GET_TD_DAYS(delta);
|
|
/* C-level overflow is impossible because |deltadays| < 1e9. */
|
|
int day = GET_DAY(date) + (negate ? -deltadays : deltadays);
|
|
|
|
if (normalize_date(&year, &month, &day) >= 0)
|
|
result = new_date(year, month, day);
|
|
return result;
|
|
}
|
|
|
|
static PyObject *
|
|
date_add(PyObject *left, PyObject *right)
|
|
{
|
|
if (PyDateTime_Check(left) || PyDateTime_Check(right)) {
|
|
Py_INCREF(Py_NotImplemented);
|
|
return Py_NotImplemented;
|
|
}
|
|
if (PyDate_Check(left)) {
|
|
/* date + ??? */
|
|
if (PyDelta_Check(right))
|
|
/* date + delta */
|
|
return add_date_timedelta((PyDateTime_Date *) left,
|
|
(PyDateTime_Delta *) right,
|
|
0);
|
|
}
|
|
else {
|
|
/* ??? + date
|
|
* 'right' must be one of us, or we wouldn't have been called
|
|
*/
|
|
if (PyDelta_Check(left))
|
|
/* delta + date */
|
|
return add_date_timedelta((PyDateTime_Date *) right,
|
|
(PyDateTime_Delta *) left,
|
|
0);
|
|
}
|
|
Py_INCREF(Py_NotImplemented);
|
|
return Py_NotImplemented;
|
|
}
|
|
|
|
static PyObject *
|
|
date_subtract(PyObject *left, PyObject *right)
|
|
{
|
|
if (PyDateTime_Check(left) || PyDateTime_Check(right)) {
|
|
Py_INCREF(Py_NotImplemented);
|
|
return Py_NotImplemented;
|
|
}
|
|
if (PyDate_Check(left)) {
|
|
if (PyDate_Check(right)) {
|
|
/* date - date */
|
|
int left_ord = ymd_to_ord(GET_YEAR(left),
|
|
GET_MONTH(left),
|
|
GET_DAY(left));
|
|
int right_ord = ymd_to_ord(GET_YEAR(right),
|
|
GET_MONTH(right),
|
|
GET_DAY(right));
|
|
return new_delta(left_ord - right_ord, 0, 0, 0);
|
|
}
|
|
if (PyDelta_Check(right)) {
|
|
/* date - delta */
|
|
return add_date_timedelta((PyDateTime_Date *) left,
|
|
(PyDateTime_Delta *) right,
|
|
1);
|
|
}
|
|
}
|
|
Py_INCREF(Py_NotImplemented);
|
|
return Py_NotImplemented;
|
|
}
|
|
|
|
|
|
/* Various ways to turn a date into a string. */
|
|
|
|
static PyObject *
|
|
date_repr(PyDateTime_Date *self)
|
|
{
|
|
char buffer[1028];
|
|
const char *type_name;
|
|
|
|
type_name = self->ob_type->tp_name;
|
|
PyOS_snprintf(buffer, sizeof(buffer), "%s(%d, %d, %d)",
|
|
type_name,
|
|
GET_YEAR(self), GET_MONTH(self), GET_DAY(self));
|
|
|
|
return PyString_FromString(buffer);
|
|
}
|
|
|
|
static PyObject *
|
|
date_isoformat(PyDateTime_Date *self)
|
|
{
|
|
char buffer[128];
|
|
|
|
isoformat_date(self, buffer, sizeof(buffer));
|
|
return PyString_FromString(buffer);
|
|
}
|
|
|
|
/* str() calls the appropriate isoformat() method. */
|
|
static PyObject *
|
|
date_str(PyDateTime_Date *self)
|
|
{
|
|
return PyObject_CallMethod((PyObject *)self, "isoformat", "()");
|
|
}
|
|
|
|
|
|
static PyObject *
|
|
date_ctime(PyDateTime_Date *self)
|
|
{
|
|
return format_ctime(self, 0, 0, 0);
|
|
}
|
|
|
|
static PyObject *
|
|
date_strftime(PyDateTime_Date *self, PyObject *args, PyObject *kw)
|
|
{
|
|
/* This method can be inherited, and needs to call the
|
|
* timetuple() method appropriate to self's class.
|
|
*/
|
|
PyObject *result;
|
|
PyObject *format;
|
|
PyObject *tuple;
|
|
static char *keywords[] = {"format", NULL};
|
|
|
|
if (! PyArg_ParseTupleAndKeywords(args, kw, "O!:strftime", keywords,
|
|
&PyString_Type, &format))
|
|
return NULL;
|
|
|
|
tuple = PyObject_CallMethod((PyObject *)self, "timetuple", "()");
|
|
if (tuple == NULL)
|
|
return NULL;
|
|
result = wrap_strftime((PyObject *)self, format, tuple,
|
|
(PyObject *)self);
|
|
Py_DECREF(tuple);
|
|
return result;
|
|
}
|
|
|
|
/* ISO methods. */
|
|
|
|
static PyObject *
|
|
date_isoweekday(PyDateTime_Date *self)
|
|
{
|
|
int dow = weekday(GET_YEAR(self), GET_MONTH(self), GET_DAY(self));
|
|
|
|
return PyInt_FromLong(dow + 1);
|
|
}
|
|
|
|
static PyObject *
|
|
date_isocalendar(PyDateTime_Date *self)
|
|
{
|
|
int year = GET_YEAR(self);
|
|
int week1_monday = iso_week1_monday(year);
|
|
int today = ymd_to_ord(year, GET_MONTH(self), GET_DAY(self));
|
|
int week;
|
|
int day;
|
|
|
|
week = divmod(today - week1_monday, 7, &day);
|
|
if (week < 0) {
|
|
--year;
|
|
week1_monday = iso_week1_monday(year);
|
|
week = divmod(today - week1_monday, 7, &day);
|
|
}
|
|
else if (week >= 52 && today >= iso_week1_monday(year + 1)) {
|
|
++year;
|
|
week = 0;
|
|
}
|
|
return Py_BuildValue("iii", year, week + 1, day + 1);
|
|
}
|
|
|
|
/* Miscellaneous methods. */
|
|
|
|
static PyObject *
|
|
date_richcompare(PyObject *self, PyObject *other, int op)
|
|
{
|
|
if (PyDate_Check(other)) {
|
|
int diff = memcmp(((PyDateTime_Date *)self)->data,
|
|
((PyDateTime_Date *)other)->data,
|
|
_PyDateTime_DATE_DATASIZE);
|
|
return diff_to_bool(diff, op);
|
|
}
|
|
else {
|
|
Py_INCREF(Py_NotImplemented);
|
|
return Py_NotImplemented;
|
|
}
|
|
}
|
|
|
|
static PyObject *
|
|
date_timetuple(PyDateTime_Date *self)
|
|
{
|
|
return build_struct_time(GET_YEAR(self),
|
|
GET_MONTH(self),
|
|
GET_DAY(self),
|
|
0, 0, 0, -1);
|
|
}
|
|
|
|
static PyObject *
|
|
date_replace(PyDateTime_Date *self, PyObject *args, PyObject *kw)
|
|
{
|
|
PyObject *clone;
|
|
PyObject *tuple;
|
|
int year = GET_YEAR(self);
|
|
int month = GET_MONTH(self);
|
|
int day = GET_DAY(self);
|
|
|
|
if (! PyArg_ParseTupleAndKeywords(args, kw, "|iii:replace", date_kws,
|
|
&year, &month, &day))
|
|
return NULL;
|
|
tuple = Py_BuildValue("iii", year, month, day);
|
|
if (tuple == NULL)
|
|
return NULL;
|
|
clone = date_new(self->ob_type, tuple, NULL);
|
|
Py_DECREF(tuple);
|
|
return clone;
|
|
}
|
|
|
|
static PyObject *date_getstate(PyDateTime_Date *self);
|
|
|
|
static long
|
|
date_hash(PyDateTime_Date *self)
|
|
{
|
|
if (self->hashcode == -1) {
|
|
PyObject *temp = date_getstate(self);
|
|
if (temp != NULL) {
|
|
self->hashcode = PyObject_Hash(temp);
|
|
Py_DECREF(temp);
|
|
}
|
|
}
|
|
return self->hashcode;
|
|
}
|
|
|
|
static PyObject *
|
|
date_toordinal(PyDateTime_Date *self)
|
|
{
|
|
return PyInt_FromLong(ymd_to_ord(GET_YEAR(self), GET_MONTH(self),
|
|
GET_DAY(self)));
|
|
}
|
|
|
|
static PyObject *
|
|
date_weekday(PyDateTime_Date *self)
|
|
{
|
|
int dow = weekday(GET_YEAR(self), GET_MONTH(self), GET_DAY(self));
|
|
|
|
return PyInt_FromLong(dow);
|
|
}
|
|
|
|
/* Pickle support, a simple use of __reduce__. */
|
|
|
|
/* __getstate__ isn't exposed */
|
|
static PyObject *
|
|
date_getstate(PyDateTime_Date *self)
|
|
{
|
|
return Py_BuildValue(
|
|
"(N)",
|
|
PyString_FromStringAndSize((char *)self->data,
|
|
_PyDateTime_DATE_DATASIZE));
|
|
}
|
|
|
|
static PyObject *
|
|
date_reduce(PyDateTime_Date *self, PyObject *arg)
|
|
{
|
|
return Py_BuildValue("(ON)", self->ob_type, date_getstate(self));
|
|
}
|
|
|
|
static PyMethodDef date_methods[] = {
|
|
|
|
/* Class methods: */
|
|
|
|
{"fromtimestamp", (PyCFunction)date_fromtimestamp, METH_VARARGS |
|
|
METH_CLASS,
|
|
PyDoc_STR("timestamp -> local date from a POSIX timestamp (like "
|
|
"time.time()).")},
|
|
|
|
{"fromordinal", (PyCFunction)date_fromordinal, METH_VARARGS |
|
|
METH_CLASS,
|
|
PyDoc_STR("int -> date corresponding to a proleptic Gregorian "
|
|
"ordinal.")},
|
|
|
|
{"today", (PyCFunction)date_today, METH_NOARGS | METH_CLASS,
|
|
PyDoc_STR("Current date or datetime: same as "
|
|
"self.__class__.fromtimestamp(time.time()).")},
|
|
|
|
/* Instance methods: */
|
|
|
|
{"ctime", (PyCFunction)date_ctime, METH_NOARGS,
|
|
PyDoc_STR("Return ctime() style string.")},
|
|
|
|
{"strftime", (PyCFunction)date_strftime, METH_KEYWORDS,
|
|
PyDoc_STR("format -> strftime() style string.")},
|
|
|
|
{"timetuple", (PyCFunction)date_timetuple, METH_NOARGS,
|
|
PyDoc_STR("Return time tuple, compatible with time.localtime().")},
|
|
|
|
{"isocalendar", (PyCFunction)date_isocalendar, METH_NOARGS,
|
|
PyDoc_STR("Return a 3-tuple containing ISO year, week number, and "
|
|
"weekday.")},
|
|
|
|
{"isoformat", (PyCFunction)date_isoformat, METH_NOARGS,
|
|
PyDoc_STR("Return string in ISO 8601 format, YYYY-MM-DD.")},
|
|
|
|
{"isoweekday", (PyCFunction)date_isoweekday, METH_NOARGS,
|
|
PyDoc_STR("Return the day of the week represented by the date.\n"
|
|
"Monday == 1 ... Sunday == 7")},
|
|
|
|
{"toordinal", (PyCFunction)date_toordinal, METH_NOARGS,
|
|
PyDoc_STR("Return proleptic Gregorian ordinal. January 1 of year "
|
|
"1 is day 1.")},
|
|
|
|
{"weekday", (PyCFunction)date_weekday, METH_NOARGS,
|
|
PyDoc_STR("Return the day of the week represented by the date.\n"
|
|
"Monday == 0 ... Sunday == 6")},
|
|
|
|
{"replace", (PyCFunction)date_replace, METH_KEYWORDS,
|
|
PyDoc_STR("Return date with new specified fields.")},
|
|
|
|
{"__reduce__", (PyCFunction)date_reduce, METH_NOARGS,
|
|
PyDoc_STR("__reduce__() -> (cls, state)")},
|
|
|
|
{NULL, NULL}
|
|
};
|
|
|
|
static char date_doc[] =
|
|
PyDoc_STR("date(year, month, day) --> date object");
|
|
|
|
static PyNumberMethods date_as_number = {
|
|
date_add, /* nb_add */
|
|
date_subtract, /* nb_subtract */
|
|
0, /* nb_multiply */
|
|
0, /* nb_remainder */
|
|
0, /* nb_divmod */
|
|
0, /* nb_power */
|
|
0, /* nb_negative */
|
|
0, /* nb_positive */
|
|
0, /* nb_absolute */
|
|
0, /* nb_bool */
|
|
};
|
|
|
|
static PyTypeObject PyDateTime_DateType = {
|
|
PyObject_HEAD_INIT(NULL)
|
|
0, /* ob_size */
|
|
"datetime.date", /* tp_name */
|
|
sizeof(PyDateTime_Date), /* tp_basicsize */
|
|
0, /* tp_itemsize */
|
|
0, /* tp_dealloc */
|
|
0, /* tp_print */
|
|
0, /* tp_getattr */
|
|
0, /* tp_setattr */
|
|
0, /* tp_compare */
|
|
(reprfunc)date_repr, /* tp_repr */
|
|
&date_as_number, /* tp_as_number */
|
|
0, /* tp_as_sequence */
|
|
0, /* tp_as_mapping */
|
|
(hashfunc)date_hash, /* tp_hash */
|
|
0, /* tp_call */
|
|
(reprfunc)date_str, /* tp_str */
|
|
PyObject_GenericGetAttr, /* tp_getattro */
|
|
0, /* tp_setattro */
|
|
0, /* tp_as_buffer */
|
|
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
|
|
date_doc, /* tp_doc */
|
|
0, /* tp_traverse */
|
|
0, /* tp_clear */
|
|
date_richcompare, /* tp_richcompare */
|
|
0, /* tp_weaklistoffset */
|
|
0, /* tp_iter */
|
|
0, /* tp_iternext */
|
|
date_methods, /* tp_methods */
|
|
0, /* tp_members */
|
|
date_getset, /* tp_getset */
|
|
0, /* tp_base */
|
|
0, /* tp_dict */
|
|
0, /* tp_descr_get */
|
|
0, /* tp_descr_set */
|
|
0, /* tp_dictoffset */
|
|
0, /* tp_init */
|
|
0, /* tp_alloc */
|
|
date_new, /* tp_new */
|
|
0, /* tp_free */
|
|
};
|
|
|
|
/*
|
|
* PyDateTime_TZInfo implementation.
|
|
*/
|
|
|
|
/* This is a pure abstract base class, so doesn't do anything beyond
|
|
* raising NotImplemented exceptions. Real tzinfo classes need
|
|
* to derive from this. This is mostly for clarity, and for efficiency in
|
|
* datetime and time constructors (their tzinfo arguments need to
|
|
* be subclasses of this tzinfo class, which is easy and quick to check).
|
|
*
|
|
* Note: For reasons having to do with pickling of subclasses, we have
|
|
* to allow tzinfo objects to be instantiated. This wasn't an issue
|
|
* in the Python implementation (__init__() could raise NotImplementedError
|
|
* there without ill effect), but doing so in the C implementation hit a
|
|
* brick wall.
|
|
*/
|
|
|
|
static PyObject *
|
|
tzinfo_nogo(const char* methodname)
|
|
{
|
|
PyErr_Format(PyExc_NotImplementedError,
|
|
"a tzinfo subclass must implement %s()",
|
|
methodname);
|
|
return NULL;
|
|
}
|
|
|
|
/* Methods. A subclass must implement these. */
|
|
|
|
static PyObject *
|
|
tzinfo_tzname(PyDateTime_TZInfo *self, PyObject *dt)
|
|
{
|
|
return tzinfo_nogo("tzname");
|
|
}
|
|
|
|
static PyObject *
|
|
tzinfo_utcoffset(PyDateTime_TZInfo *self, PyObject *dt)
|
|
{
|
|
return tzinfo_nogo("utcoffset");
|
|
}
|
|
|
|
static PyObject *
|
|
tzinfo_dst(PyDateTime_TZInfo *self, PyObject *dt)
|
|
{
|
|
return tzinfo_nogo("dst");
|
|
}
|
|
|
|
static PyObject *
|
|
tzinfo_fromutc(PyDateTime_TZInfo *self, PyDateTime_DateTime *dt)
|
|
{
|
|
int y, m, d, hh, mm, ss, us;
|
|
|
|
PyObject *result;
|
|
int off, dst;
|
|
int none;
|
|
int delta;
|
|
|
|
if (! PyDateTime_Check(dt)) {
|
|
PyErr_SetString(PyExc_TypeError,
|
|
"fromutc: argument must be a datetime");
|
|
return NULL;
|
|
}
|
|
if (! HASTZINFO(dt) || dt->tzinfo != (PyObject *)self) {
|
|
PyErr_SetString(PyExc_ValueError, "fromutc: dt.tzinfo "
|
|
"is not self");
|
|
return NULL;
|
|
}
|
|
|
|
off = call_utcoffset(dt->tzinfo, (PyObject *)dt, &none);
|
|
if (off == -1 && PyErr_Occurred())
|
|
return NULL;
|
|
if (none) {
|
|
PyErr_SetString(PyExc_ValueError, "fromutc: non-None "
|
|
"utcoffset() result required");
|
|
return NULL;
|
|
}
|
|
|
|
dst = call_dst(dt->tzinfo, (PyObject *)dt, &none);
|
|
if (dst == -1 && PyErr_Occurred())
|
|
return NULL;
|
|
if (none) {
|
|
PyErr_SetString(PyExc_ValueError, "fromutc: non-None "
|
|
"dst() result required");
|
|
return NULL;
|
|
}
|
|
|
|
y = GET_YEAR(dt);
|
|
m = GET_MONTH(dt);
|
|
d = GET_DAY(dt);
|
|
hh = DATE_GET_HOUR(dt);
|
|
mm = DATE_GET_MINUTE(dt);
|
|
ss = DATE_GET_SECOND(dt);
|
|
us = DATE_GET_MICROSECOND(dt);
|
|
|
|
delta = off - dst;
|
|
mm += delta;
|
|
if ((mm < 0 || mm >= 60) &&
|
|
normalize_datetime(&y, &m, &d, &hh, &mm, &ss, &us) < 0)
|
|
return NULL;
|
|
result = new_datetime(y, m, d, hh, mm, ss, us, dt->tzinfo);
|
|
if (result == NULL)
|
|
return result;
|
|
|
|
dst = call_dst(dt->tzinfo, result, &none);
|
|
if (dst == -1 && PyErr_Occurred())
|
|
goto Fail;
|
|
if (none)
|
|
goto Inconsistent;
|
|
if (dst == 0)
|
|
return result;
|
|
|
|
mm += dst;
|
|
if ((mm < 0 || mm >= 60) &&
|
|
normalize_datetime(&y, &m, &d, &hh, &mm, &ss, &us) < 0)
|
|
goto Fail;
|
|
Py_DECREF(result);
|
|
result = new_datetime(y, m, d, hh, mm, ss, us, dt->tzinfo);
|
|
return result;
|
|
|
|
Inconsistent:
|
|
PyErr_SetString(PyExc_ValueError, "fromutc: tz.dst() gave"
|
|
"inconsistent results; cannot convert");
|
|
|
|
/* fall thru to failure */
|
|
Fail:
|
|
Py_DECREF(result);
|
|
return NULL;
|
|
}
|
|
|
|
/*
|
|
* Pickle support. This is solely so that tzinfo subclasses can use
|
|
* pickling -- tzinfo itself is supposed to be uninstantiable.
|
|
*/
|
|
|
|
static PyObject *
|
|
tzinfo_reduce(PyObject *self)
|
|
{
|
|
PyObject *args, *state, *tmp;
|
|
PyObject *getinitargs, *getstate;
|
|
|
|
tmp = PyTuple_New(0);
|
|
if (tmp == NULL)
|
|
return NULL;
|
|
|
|
getinitargs = PyObject_GetAttrString(self, "__getinitargs__");
|
|
if (getinitargs != NULL) {
|
|
args = PyObject_CallObject(getinitargs, tmp);
|
|
Py_DECREF(getinitargs);
|
|
if (args == NULL) {
|
|
Py_DECREF(tmp);
|
|
return NULL;
|
|
}
|
|
}
|
|
else {
|
|
PyErr_Clear();
|
|
args = tmp;
|
|
Py_INCREF(args);
|
|
}
|
|
|
|
getstate = PyObject_GetAttrString(self, "__getstate__");
|
|
if (getstate != NULL) {
|
|
state = PyObject_CallObject(getstate, tmp);
|
|
Py_DECREF(getstate);
|
|
if (state == NULL) {
|
|
Py_DECREF(args);
|
|
Py_DECREF(tmp);
|
|
return NULL;
|
|
}
|
|
}
|
|
else {
|
|
PyObject **dictptr;
|
|
PyErr_Clear();
|
|
state = Py_None;
|
|
dictptr = _PyObject_GetDictPtr(self);
|
|
if (dictptr && *dictptr && PyDict_Size(*dictptr))
|
|
state = *dictptr;
|
|
Py_INCREF(state);
|
|
}
|
|
|
|
Py_DECREF(tmp);
|
|
|
|
if (state == Py_None) {
|
|
Py_DECREF(state);
|
|
return Py_BuildValue("(ON)", self->ob_type, args);
|
|
}
|
|
else
|
|
return Py_BuildValue("(ONN)", self->ob_type, args, state);
|
|
}
|
|
|
|
static PyMethodDef tzinfo_methods[] = {
|
|
|
|
{"tzname", (PyCFunction)tzinfo_tzname, METH_O,
|
|
PyDoc_STR("datetime -> string name of time zone.")},
|
|
|
|
{"utcoffset", (PyCFunction)tzinfo_utcoffset, METH_O,
|
|
PyDoc_STR("datetime -> minutes east of UTC (negative for "
|
|
"west of UTC).")},
|
|
|
|
{"dst", (PyCFunction)tzinfo_dst, METH_O,
|
|
PyDoc_STR("datetime -> DST offset in minutes east of UTC.")},
|
|
|
|
{"fromutc", (PyCFunction)tzinfo_fromutc, METH_O,
|
|
PyDoc_STR("datetime in UTC -> datetime in local time.")},
|
|
|
|
{"__reduce__", (PyCFunction)tzinfo_reduce, METH_NOARGS,
|
|
PyDoc_STR("-> (cls, state)")},
|
|
|
|
{NULL, NULL}
|
|
};
|
|
|
|
static char tzinfo_doc[] =
|
|
PyDoc_STR("Abstract base class for time zone info objects.");
|
|
|
|
static PyTypeObject PyDateTime_TZInfoType = {
|
|
PyObject_HEAD_INIT(NULL)
|
|
0, /* ob_size */
|
|
"datetime.tzinfo", /* tp_name */
|
|
sizeof(PyDateTime_TZInfo), /* tp_basicsize */
|
|
0, /* tp_itemsize */
|
|
0, /* tp_dealloc */
|
|
0, /* tp_print */
|
|
0, /* tp_getattr */
|
|
0, /* tp_setattr */
|
|
0, /* tp_compare */
|
|
0, /* tp_repr */
|
|
0, /* tp_as_number */
|
|
0, /* tp_as_sequence */
|
|
0, /* tp_as_mapping */
|
|
0, /* tp_hash */
|
|
0, /* tp_call */
|
|
0, /* tp_str */
|
|
PyObject_GenericGetAttr, /* tp_getattro */
|
|
0, /* tp_setattro */
|
|
0, /* tp_as_buffer */
|
|
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
|
|
tzinfo_doc, /* tp_doc */
|
|
0, /* tp_traverse */
|
|
0, /* tp_clear */
|
|
0, /* tp_richcompare */
|
|
0, /* tp_weaklistoffset */
|
|
0, /* tp_iter */
|
|
0, /* tp_iternext */
|
|
tzinfo_methods, /* tp_methods */
|
|
0, /* tp_members */
|
|
0, /* tp_getset */
|
|
0, /* tp_base */
|
|
0, /* tp_dict */
|
|
0, /* tp_descr_get */
|
|
0, /* tp_descr_set */
|
|
0, /* tp_dictoffset */
|
|
0, /* tp_init */
|
|
0, /* tp_alloc */
|
|
PyType_GenericNew, /* tp_new */
|
|
0, /* tp_free */
|
|
};
|
|
|
|
/*
|
|
* PyDateTime_Time implementation.
|
|
*/
|
|
|
|
/* Accessor properties.
|
|
*/
|
|
|
|
static PyObject *
|
|
time_hour(PyDateTime_Time *self, void *unused)
|
|
{
|
|
return PyInt_FromLong(TIME_GET_HOUR(self));
|
|
}
|
|
|
|
static PyObject *
|
|
time_minute(PyDateTime_Time *self, void *unused)
|
|
{
|
|
return PyInt_FromLong(TIME_GET_MINUTE(self));
|
|
}
|
|
|
|
/* The name time_second conflicted with some platform header file. */
|
|
static PyObject *
|
|
py_time_second(PyDateTime_Time *self, void *unused)
|
|
{
|
|
return PyInt_FromLong(TIME_GET_SECOND(self));
|
|
}
|
|
|
|
static PyObject *
|
|
time_microsecond(PyDateTime_Time *self, void *unused)
|
|
{
|
|
return PyInt_FromLong(TIME_GET_MICROSECOND(self));
|
|
}
|
|
|
|
static PyObject *
|
|
time_tzinfo(PyDateTime_Time *self, void *unused)
|
|
{
|
|
PyObject *result = HASTZINFO(self) ? self->tzinfo : Py_None;
|
|
Py_INCREF(result);
|
|
return result;
|
|
}
|
|
|
|
static PyGetSetDef time_getset[] = {
|
|
{"hour", (getter)time_hour},
|
|
{"minute", (getter)time_minute},
|
|
{"second", (getter)py_time_second},
|
|
{"microsecond", (getter)time_microsecond},
|
|
{"tzinfo", (getter)time_tzinfo},
|
|
{NULL}
|
|
};
|
|
|
|
/*
|
|
* Constructors.
|
|
*/
|
|
|
|
static char *time_kws[] = {"hour", "minute", "second", "microsecond",
|
|
"tzinfo", NULL};
|
|
|
|
static PyObject *
|
|
time_new(PyTypeObject *type, PyObject *args, PyObject *kw)
|
|
{
|
|
PyObject *self = NULL;
|
|
PyObject *state;
|
|
int hour = 0;
|
|
int minute = 0;
|
|
int second = 0;
|
|
int usecond = 0;
|
|
PyObject *tzinfo = Py_None;
|
|
|
|
/* Check for invocation from pickle with __getstate__ state */
|
|
if (PyTuple_GET_SIZE(args) >= 1 &&
|
|
PyTuple_GET_SIZE(args) <= 2 &&
|
|
PyString_Check(state = PyTuple_GET_ITEM(args, 0)) &&
|
|
PyString_GET_SIZE(state) == _PyDateTime_TIME_DATASIZE &&
|
|
((unsigned char) (PyString_AS_STRING(state)[0])) < 24)
|
|
{
|
|
PyDateTime_Time *me;
|
|
char aware;
|
|
|
|
if (PyTuple_GET_SIZE(args) == 2) {
|
|
tzinfo = PyTuple_GET_ITEM(args, 1);
|
|
if (check_tzinfo_subclass(tzinfo) < 0) {
|
|
PyErr_SetString(PyExc_TypeError, "bad "
|
|
"tzinfo state arg");
|
|
return NULL;
|
|
}
|
|
}
|
|
aware = (char)(tzinfo != Py_None);
|
|
me = (PyDateTime_Time *) (type->tp_alloc(type, aware));
|
|
if (me != NULL) {
|
|
char *pdata = PyString_AS_STRING(state);
|
|
|
|
memcpy(me->data, pdata, _PyDateTime_TIME_DATASIZE);
|
|
me->hashcode = -1;
|
|
me->hastzinfo = aware;
|
|
if (aware) {
|
|
Py_INCREF(tzinfo);
|
|
me->tzinfo = tzinfo;
|
|
}
|
|
}
|
|
return (PyObject *)me;
|
|
}
|
|
|
|
if (PyArg_ParseTupleAndKeywords(args, kw, "|iiiiO", time_kws,
|
|
&hour, &minute, &second, &usecond,
|
|
&tzinfo)) {
|
|
if (check_time_args(hour, minute, second, usecond) < 0)
|
|
return NULL;
|
|
if (check_tzinfo_subclass(tzinfo) < 0)
|
|
return NULL;
|
|
self = new_time_ex(hour, minute, second, usecond, tzinfo,
|
|
type);
|
|
}
|
|
return self;
|
|
}
|
|
|
|
/*
|
|
* Destructor.
|
|
*/
|
|
|
|
static void
|
|
time_dealloc(PyDateTime_Time *self)
|
|
{
|
|
if (HASTZINFO(self)) {
|
|
Py_XDECREF(self->tzinfo);
|
|
}
|
|
self->ob_type->tp_free((PyObject *)self);
|
|
}
|
|
|
|
/*
|
|
* Indirect access to tzinfo methods.
|
|
*/
|
|
|
|
/* These are all METH_NOARGS, so don't need to check the arglist. */
|
|
static PyObject *
|
|
time_utcoffset(PyDateTime_Time *self, PyObject *unused) {
|
|
return offset_as_timedelta(HASTZINFO(self) ? self->tzinfo : Py_None,
|
|
"utcoffset", Py_None);
|
|
}
|
|
|
|
static PyObject *
|
|
time_dst(PyDateTime_Time *self, PyObject *unused) {
|
|
return offset_as_timedelta(HASTZINFO(self) ? self->tzinfo : Py_None,
|
|
"dst", Py_None);
|
|
}
|
|
|
|
static PyObject *
|
|
time_tzname(PyDateTime_Time *self, PyObject *unused) {
|
|
return call_tzname(HASTZINFO(self) ? self->tzinfo : Py_None,
|
|
Py_None);
|
|
}
|
|
|
|
/*
|
|
* Various ways to turn a time into a string.
|
|
*/
|
|
|
|
static PyObject *
|
|
time_repr(PyDateTime_Time *self)
|
|
{
|
|
char buffer[100];
|
|
const char *type_name = self->ob_type->tp_name;
|
|
int h = TIME_GET_HOUR(self);
|
|
int m = TIME_GET_MINUTE(self);
|
|
int s = TIME_GET_SECOND(self);
|
|
int us = TIME_GET_MICROSECOND(self);
|
|
PyObject *result = NULL;
|
|
|
|
if (us)
|
|
PyOS_snprintf(buffer, sizeof(buffer),
|
|
"%s(%d, %d, %d, %d)", type_name, h, m, s, us);
|
|
else if (s)
|
|
PyOS_snprintf(buffer, sizeof(buffer),
|
|
"%s(%d, %d, %d)", type_name, h, m, s);
|
|
else
|
|
PyOS_snprintf(buffer, sizeof(buffer),
|
|
"%s(%d, %d)", type_name, h, m);
|
|
result = PyString_FromString(buffer);
|
|
if (result != NULL && HASTZINFO(self))
|
|
result = append_keyword_tzinfo(result, self->tzinfo);
|
|
return result;
|
|
}
|
|
|
|
static PyObject *
|
|
time_str(PyDateTime_Time *self)
|
|
{
|
|
return PyObject_CallMethod((PyObject *)self, "isoformat", "()");
|
|
}
|
|
|
|
static PyObject *
|
|
time_isoformat(PyDateTime_Time *self)
|
|
{
|
|
char buf[100];
|
|
PyObject *result;
|
|
/* Reuse the time format code from the datetime type. */
|
|
PyDateTime_DateTime datetime;
|
|
PyDateTime_DateTime *pdatetime = &datetime;
|
|
|
|
/* Copy over just the time bytes. */
|
|
memcpy(pdatetime->data + _PyDateTime_DATE_DATASIZE,
|
|
self->data,
|
|
_PyDateTime_TIME_DATASIZE);
|
|
|
|
isoformat_time(pdatetime, buf, sizeof(buf));
|
|
result = PyString_FromString(buf);
|
|
if (result == NULL || ! HASTZINFO(self) || self->tzinfo == Py_None)
|
|
return result;
|
|
|
|
/* We need to append the UTC offset. */
|
|
if (format_utcoffset(buf, sizeof(buf), ":", self->tzinfo,
|
|
Py_None) < 0) {
|
|
Py_DECREF(result);
|
|
return NULL;
|
|
}
|
|
PyString_ConcatAndDel(&result, PyString_FromString(buf));
|
|
return result;
|
|
}
|
|
|
|
static PyObject *
|
|
time_strftime(PyDateTime_Time *self, PyObject *args, PyObject *kw)
|
|
{
|
|
PyObject *result;
|
|
PyObject *format;
|
|
PyObject *tuple;
|
|
static char *keywords[] = {"format", NULL};
|
|
|
|
if (! PyArg_ParseTupleAndKeywords(args, kw, "O!:strftime", keywords,
|
|
&PyString_Type, &format))
|
|
return NULL;
|
|
|
|
/* Python's strftime does insane things with the year part of the
|
|
* timetuple. The year is forced to (the otherwise nonsensical)
|
|
* 1900 to worm around that.
|
|
*/
|
|
tuple = Py_BuildValue("iiiiiiiii",
|
|
1900, 1, 1, /* year, month, day */
|
|
TIME_GET_HOUR(self),
|
|
TIME_GET_MINUTE(self),
|
|
TIME_GET_SECOND(self),
|
|
0, 1, -1); /* weekday, daynum, dst */
|
|
if (tuple == NULL)
|
|
return NULL;
|
|
assert(PyTuple_Size(tuple) == 9);
|
|
result = wrap_strftime((PyObject *)self, format, tuple, Py_None);
|
|
Py_DECREF(tuple);
|
|
return result;
|
|
}
|
|
|
|
/*
|
|
* Miscellaneous methods.
|
|
*/
|
|
|
|
static PyObject *
|
|
time_richcompare(PyObject *self, PyObject *other, int op)
|
|
{
|
|
int diff;
|
|
naivety n1, n2;
|
|
int offset1, offset2;
|
|
|
|
if (! PyTime_Check(other)) {
|
|
Py_INCREF(Py_NotImplemented);
|
|
return Py_NotImplemented;
|
|
}
|
|
if (classify_two_utcoffsets(self, &offset1, &n1, Py_None,
|
|
other, &offset2, &n2, Py_None) < 0)
|
|
return NULL;
|
|
assert(n1 != OFFSET_UNKNOWN && n2 != OFFSET_UNKNOWN);
|
|
/* If they're both naive, or both aware and have the same offsets,
|
|
* we get off cheap. Note that if they're both naive, offset1 ==
|
|
* offset2 == 0 at this point.
|
|
*/
|
|
if (n1 == n2 && offset1 == offset2) {
|
|
diff = memcmp(((PyDateTime_Time *)self)->data,
|
|
((PyDateTime_Time *)other)->data,
|
|
_PyDateTime_TIME_DATASIZE);
|
|
return diff_to_bool(diff, op);
|
|
}
|
|
|
|
if (n1 == OFFSET_AWARE && n2 == OFFSET_AWARE) {
|
|
assert(offset1 != offset2); /* else last "if" handled it */
|
|
/* Convert everything except microseconds to seconds. These
|
|
* can't overflow (no more than the # of seconds in 2 days).
|
|
*/
|
|
offset1 = TIME_GET_HOUR(self) * 3600 +
|
|
(TIME_GET_MINUTE(self) - offset1) * 60 +
|
|
TIME_GET_SECOND(self);
|
|
offset2 = TIME_GET_HOUR(other) * 3600 +
|
|
(TIME_GET_MINUTE(other) - offset2) * 60 +
|
|
TIME_GET_SECOND(other);
|
|
diff = offset1 - offset2;
|
|
if (diff == 0)
|
|
diff = TIME_GET_MICROSECOND(self) -
|
|
TIME_GET_MICROSECOND(other);
|
|
return diff_to_bool(diff, op);
|
|
}
|
|
|
|
assert(n1 != n2);
|
|
PyErr_SetString(PyExc_TypeError,
|
|
"can't compare offset-naive and "
|
|
"offset-aware times");
|
|
return NULL;
|
|
}
|
|
|
|
static long
|
|
time_hash(PyDateTime_Time *self)
|
|
{
|
|
if (self->hashcode == -1) {
|
|
naivety n;
|
|
int offset;
|
|
PyObject *temp;
|
|
|
|
n = classify_utcoffset((PyObject *)self, Py_None, &offset);
|
|
assert(n != OFFSET_UNKNOWN);
|
|
if (n == OFFSET_ERROR)
|
|
return -1;
|
|
|
|
/* Reduce this to a hash of another object. */
|
|
if (offset == 0)
|
|
temp = PyString_FromStringAndSize((char *)self->data,
|
|
_PyDateTime_TIME_DATASIZE);
|
|
else {
|
|
int hour;
|
|
int minute;
|
|
|
|
assert(n == OFFSET_AWARE);
|
|
assert(HASTZINFO(self));
|
|
hour = divmod(TIME_GET_HOUR(self) * 60 +
|
|
TIME_GET_MINUTE(self) - offset,
|
|
60,
|
|
&minute);
|
|
if (0 <= hour && hour < 24)
|
|
temp = new_time(hour, minute,
|
|
TIME_GET_SECOND(self),
|
|
TIME_GET_MICROSECOND(self),
|
|
Py_None);
|
|
else
|
|
temp = Py_BuildValue("iiii",
|
|
hour, minute,
|
|
TIME_GET_SECOND(self),
|
|
TIME_GET_MICROSECOND(self));
|
|
}
|
|
if (temp != NULL) {
|
|
self->hashcode = PyObject_Hash(temp);
|
|
Py_DECREF(temp);
|
|
}
|
|
}
|
|
return self->hashcode;
|
|
}
|
|
|
|
static PyObject *
|
|
time_replace(PyDateTime_Time *self, PyObject *args, PyObject *kw)
|
|
{
|
|
PyObject *clone;
|
|
PyObject *tuple;
|
|
int hh = TIME_GET_HOUR(self);
|
|
int mm = TIME_GET_MINUTE(self);
|
|
int ss = TIME_GET_SECOND(self);
|
|
int us = TIME_GET_MICROSECOND(self);
|
|
PyObject *tzinfo = HASTZINFO(self) ? self->tzinfo : Py_None;
|
|
|
|
if (! PyArg_ParseTupleAndKeywords(args, kw, "|iiiiO:replace",
|
|
time_kws,
|
|
&hh, &mm, &ss, &us, &tzinfo))
|
|
return NULL;
|
|
tuple = Py_BuildValue("iiiiO", hh, mm, ss, us, tzinfo);
|
|
if (tuple == NULL)
|
|
return NULL;
|
|
clone = time_new(self->ob_type, tuple, NULL);
|
|
Py_DECREF(tuple);
|
|
return clone;
|
|
}
|
|
|
|
static int
|
|
time_bool(PyDateTime_Time *self)
|
|
{
|
|
int offset;
|
|
int none;
|
|
|
|
if (TIME_GET_SECOND(self) || TIME_GET_MICROSECOND(self)) {
|
|
/* Since utcoffset is in whole minutes, nothing can
|
|
* alter the conclusion that this is nonzero.
|
|
*/
|
|
return 1;
|
|
}
|
|
offset = 0;
|
|
if (HASTZINFO(self) && self->tzinfo != Py_None) {
|
|
offset = call_utcoffset(self->tzinfo, Py_None, &none);
|
|
if (offset == -1 && PyErr_Occurred())
|
|
return -1;
|
|
}
|
|
return (TIME_GET_MINUTE(self) - offset + TIME_GET_HOUR(self)*60) != 0;
|
|
}
|
|
|
|
/* Pickle support, a simple use of __reduce__. */
|
|
|
|
/* Let basestate be the non-tzinfo data string.
|
|
* If tzinfo is None, this returns (basestate,), else (basestate, tzinfo).
|
|
* So it's a tuple in any (non-error) case.
|
|
* __getstate__ isn't exposed.
|
|
*/
|
|
static PyObject *
|
|
time_getstate(PyDateTime_Time *self)
|
|
{
|
|
PyObject *basestate;
|
|
PyObject *result = NULL;
|
|
|
|
basestate = PyString_FromStringAndSize((char *)self->data,
|
|
_PyDateTime_TIME_DATASIZE);
|
|
if (basestate != NULL) {
|
|
if (! HASTZINFO(self) || self->tzinfo == Py_None)
|
|
result = PyTuple_Pack(1, basestate);
|
|
else
|
|
result = PyTuple_Pack(2, basestate, self->tzinfo);
|
|
Py_DECREF(basestate);
|
|
}
|
|
return result;
|
|
}
|
|
|
|
static PyObject *
|
|
time_reduce(PyDateTime_Time *self, PyObject *arg)
|
|
{
|
|
return Py_BuildValue("(ON)", self->ob_type, time_getstate(self));
|
|
}
|
|
|
|
static PyMethodDef time_methods[] = {
|
|
|
|
{"isoformat", (PyCFunction)time_isoformat, METH_KEYWORDS,
|
|
PyDoc_STR("Return string in ISO 8601 format, HH:MM:SS[.mmmmmm]"
|
|
"[+HH:MM].")},
|
|
|
|
{"strftime", (PyCFunction)time_strftime, METH_KEYWORDS,
|
|
PyDoc_STR("format -> strftime() style string.")},
|
|
|
|
{"utcoffset", (PyCFunction)time_utcoffset, METH_NOARGS,
|
|
PyDoc_STR("Return self.tzinfo.utcoffset(self).")},
|
|
|
|
{"tzname", (PyCFunction)time_tzname, METH_NOARGS,
|
|
PyDoc_STR("Return self.tzinfo.tzname(self).")},
|
|
|
|
{"dst", (PyCFunction)time_dst, METH_NOARGS,
|
|
PyDoc_STR("Return self.tzinfo.dst(self).")},
|
|
|
|
{"replace", (PyCFunction)time_replace, METH_KEYWORDS,
|
|
PyDoc_STR("Return time with new specified fields.")},
|
|
|
|
{"__reduce__", (PyCFunction)time_reduce, METH_NOARGS,
|
|
PyDoc_STR("__reduce__() -> (cls, state)")},
|
|
|
|
{NULL, NULL}
|
|
};
|
|
|
|
static char time_doc[] =
|
|
PyDoc_STR("time([hour[, minute[, second[, microsecond[, tzinfo]]]]]) --> a time object\n\
|
|
\n\
|
|
All arguments are optional. tzinfo may be None, or an instance of\n\
|
|
a tzinfo subclass. The remaining arguments may be ints or longs.\n");
|
|
|
|
static PyNumberMethods time_as_number = {
|
|
0, /* nb_add */
|
|
0, /* nb_subtract */
|
|
0, /* nb_multiply */
|
|
0, /* nb_remainder */
|
|
0, /* nb_divmod */
|
|
0, /* nb_power */
|
|
0, /* nb_negative */
|
|
0, /* nb_positive */
|
|
0, /* nb_absolute */
|
|
(inquiry)time_bool, /* nb_bool */
|
|
};
|
|
|
|
static PyTypeObject PyDateTime_TimeType = {
|
|
PyObject_HEAD_INIT(NULL)
|
|
0, /* ob_size */
|
|
"datetime.time", /* tp_name */
|
|
sizeof(PyDateTime_Time), /* tp_basicsize */
|
|
0, /* tp_itemsize */
|
|
(destructor)time_dealloc, /* tp_dealloc */
|
|
0, /* tp_print */
|
|
0, /* tp_getattr */
|
|
0, /* tp_setattr */
|
|
0, /* tp_compare */
|
|
(reprfunc)time_repr, /* tp_repr */
|
|
&time_as_number, /* tp_as_number */
|
|
0, /* tp_as_sequence */
|
|
0, /* tp_as_mapping */
|
|
(hashfunc)time_hash, /* tp_hash */
|
|
0, /* tp_call */
|
|
(reprfunc)time_str, /* tp_str */
|
|
PyObject_GenericGetAttr, /* tp_getattro */
|
|
0, /* tp_setattro */
|
|
0, /* tp_as_buffer */
|
|
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
|
|
time_doc, /* tp_doc */
|
|
0, /* tp_traverse */
|
|
0, /* tp_clear */
|
|
time_richcompare, /* tp_richcompare */
|
|
0, /* tp_weaklistoffset */
|
|
0, /* tp_iter */
|
|
0, /* tp_iternext */
|
|
time_methods, /* tp_methods */
|
|
0, /* tp_members */
|
|
time_getset, /* tp_getset */
|
|
0, /* tp_base */
|
|
0, /* tp_dict */
|
|
0, /* tp_descr_get */
|
|
0, /* tp_descr_set */
|
|
0, /* tp_dictoffset */
|
|
0, /* tp_init */
|
|
time_alloc, /* tp_alloc */
|
|
time_new, /* tp_new */
|
|
0, /* tp_free */
|
|
};
|
|
|
|
/*
|
|
* PyDateTime_DateTime implementation.
|
|
*/
|
|
|
|
/* Accessor properties. Properties for day, month, and year are inherited
|
|
* from date.
|
|
*/
|
|
|
|
static PyObject *
|
|
datetime_hour(PyDateTime_DateTime *self, void *unused)
|
|
{
|
|
return PyInt_FromLong(DATE_GET_HOUR(self));
|
|
}
|
|
|
|
static PyObject *
|
|
datetime_minute(PyDateTime_DateTime *self, void *unused)
|
|
{
|
|
return PyInt_FromLong(DATE_GET_MINUTE(self));
|
|
}
|
|
|
|
static PyObject *
|
|
datetime_second(PyDateTime_DateTime *self, void *unused)
|
|
{
|
|
return PyInt_FromLong(DATE_GET_SECOND(self));
|
|
}
|
|
|
|
static PyObject *
|
|
datetime_microsecond(PyDateTime_DateTime *self, void *unused)
|
|
{
|
|
return PyInt_FromLong(DATE_GET_MICROSECOND(self));
|
|
}
|
|
|
|
static PyObject *
|
|
datetime_tzinfo(PyDateTime_DateTime *self, void *unused)
|
|
{
|
|
PyObject *result = HASTZINFO(self) ? self->tzinfo : Py_None;
|
|
Py_INCREF(result);
|
|
return result;
|
|
}
|
|
|
|
static PyGetSetDef datetime_getset[] = {
|
|
{"hour", (getter)datetime_hour},
|
|
{"minute", (getter)datetime_minute},
|
|
{"second", (getter)datetime_second},
|
|
{"microsecond", (getter)datetime_microsecond},
|
|
{"tzinfo", (getter)datetime_tzinfo},
|
|
{NULL}
|
|
};
|
|
|
|
/*
|
|
* Constructors.
|
|
*/
|
|
|
|
static char *datetime_kws[] = {
|
|
"year", "month", "day", "hour", "minute", "second",
|
|
"microsecond", "tzinfo", NULL
|
|
};
|
|
|
|
static PyObject *
|
|
datetime_new(PyTypeObject *type, PyObject *args, PyObject *kw)
|
|
{
|
|
PyObject *self = NULL;
|
|
PyObject *state;
|
|
int year;
|
|
int month;
|
|
int day;
|
|
int hour = 0;
|
|
int minute = 0;
|
|
int second = 0;
|
|
int usecond = 0;
|
|
PyObject *tzinfo = Py_None;
|
|
|
|
/* Check for invocation from pickle with __getstate__ state */
|
|
if (PyTuple_GET_SIZE(args) >= 1 &&
|
|
PyTuple_GET_SIZE(args) <= 2 &&
|
|
PyString_Check(state = PyTuple_GET_ITEM(args, 0)) &&
|
|
PyString_GET_SIZE(state) == _PyDateTime_DATETIME_DATASIZE &&
|
|
MONTH_IS_SANE(PyString_AS_STRING(state)[2]))
|
|
{
|
|
PyDateTime_DateTime *me;
|
|
char aware;
|
|
|
|
if (PyTuple_GET_SIZE(args) == 2) {
|
|
tzinfo = PyTuple_GET_ITEM(args, 1);
|
|
if (check_tzinfo_subclass(tzinfo) < 0) {
|
|
PyErr_SetString(PyExc_TypeError, "bad "
|
|
"tzinfo state arg");
|
|
return NULL;
|
|
}
|
|
}
|
|
aware = (char)(tzinfo != Py_None);
|
|
me = (PyDateTime_DateTime *) (type->tp_alloc(type , aware));
|
|
if (me != NULL) {
|
|
char *pdata = PyString_AS_STRING(state);
|
|
|
|
memcpy(me->data, pdata, _PyDateTime_DATETIME_DATASIZE);
|
|
me->hashcode = -1;
|
|
me->hastzinfo = aware;
|
|
if (aware) {
|
|
Py_INCREF(tzinfo);
|
|
me->tzinfo = tzinfo;
|
|
}
|
|
}
|
|
return (PyObject *)me;
|
|
}
|
|
|
|
if (PyArg_ParseTupleAndKeywords(args, kw, "iii|iiiiO", datetime_kws,
|
|
&year, &month, &day, &hour, &minute,
|
|
&second, &usecond, &tzinfo)) {
|
|
if (check_date_args(year, month, day) < 0)
|
|
return NULL;
|
|
if (check_time_args(hour, minute, second, usecond) < 0)
|
|
return NULL;
|
|
if (check_tzinfo_subclass(tzinfo) < 0)
|
|
return NULL;
|
|
self = new_datetime_ex(year, month, day,
|
|
hour, minute, second, usecond,
|
|
tzinfo, type);
|
|
}
|
|
return self;
|
|
}
|
|
|
|
/* TM_FUNC is the shared type of localtime() and gmtime(). */
|
|
typedef struct tm *(*TM_FUNC)(const time_t *timer);
|
|
|
|
/* Internal helper.
|
|
* Build datetime from a time_t and a distinct count of microseconds.
|
|
* Pass localtime or gmtime for f, to control the interpretation of timet.
|
|
*/
|
|
static PyObject *
|
|
datetime_from_timet_and_us(PyObject *cls, TM_FUNC f, time_t timet, int us,
|
|
PyObject *tzinfo)
|
|
{
|
|
struct tm *tm;
|
|
PyObject *result = NULL;
|
|
|
|
tm = f(&timet);
|
|
if (tm) {
|
|
/* The platform localtime/gmtime may insert leap seconds,
|
|
* indicated by tm->tm_sec > 59. We don't care about them,
|
|
* except to the extent that passing them on to the datetime
|
|
* constructor would raise ValueError for a reason that
|
|
* made no sense to the user.
|
|
*/
|
|
if (tm->tm_sec > 59)
|
|
tm->tm_sec = 59;
|
|
result = PyObject_CallFunction(cls, "iiiiiiiO",
|
|
tm->tm_year + 1900,
|
|
tm->tm_mon + 1,
|
|
tm->tm_mday,
|
|
tm->tm_hour,
|
|
tm->tm_min,
|
|
tm->tm_sec,
|
|
us,
|
|
tzinfo);
|
|
}
|
|
else
|
|
PyErr_SetString(PyExc_ValueError,
|
|
"timestamp out of range for "
|
|
"platform localtime()/gmtime() function");
|
|
return result;
|
|
}
|
|
|
|
/* Internal helper.
|
|
* Build datetime from a Python timestamp. Pass localtime or gmtime for f,
|
|
* to control the interpretation of the timestamp. Since a double doesn't
|
|
* have enough bits to cover a datetime's full range of precision, it's
|
|
* better to call datetime_from_timet_and_us provided you have a way
|
|
* to get that much precision (e.g., C time() isn't good enough).
|
|
*/
|
|
static PyObject *
|
|
datetime_from_timestamp(PyObject *cls, TM_FUNC f, double timestamp,
|
|
PyObject *tzinfo)
|
|
{
|
|
time_t timet;
|
|
double fraction;
|
|
int us;
|
|
|
|
timet = _PyTime_DoubleToTimet(timestamp);
|
|
if (timet == (time_t)-1 && PyErr_Occurred())
|
|
return NULL;
|
|
fraction = timestamp - (double)timet;
|
|
us = (int)round_to_long(fraction * 1e6);
|
|
/* If timestamp is less than one microsecond smaller than a
|
|
* full second, round up. Otherwise, ValueErrors are raised
|
|
* for some floats. */
|
|
if (us == 1000000) {
|
|
timet += 1;
|
|
us = 0;
|
|
}
|
|
return datetime_from_timet_and_us(cls, f, timet, us, tzinfo);
|
|
}
|
|
|
|
/* Internal helper.
|
|
* Build most accurate possible datetime for current time. Pass localtime or
|
|
* gmtime for f as appropriate.
|
|
*/
|
|
static PyObject *
|
|
datetime_best_possible(PyObject *cls, TM_FUNC f, PyObject *tzinfo)
|
|
{
|
|
#ifdef HAVE_GETTIMEOFDAY
|
|
struct timeval t;
|
|
|
|
#ifdef GETTIMEOFDAY_NO_TZ
|
|
gettimeofday(&t);
|
|
#else
|
|
gettimeofday(&t, (struct timezone *)NULL);
|
|
#endif
|
|
return datetime_from_timet_and_us(cls, f, t.tv_sec, (int)t.tv_usec,
|
|
tzinfo);
|
|
|
|
#else /* ! HAVE_GETTIMEOFDAY */
|
|
/* No flavor of gettimeofday exists on this platform. Python's
|
|
* time.time() does a lot of other platform tricks to get the
|
|
* best time it can on the platform, and we're not going to do
|
|
* better than that (if we could, the better code would belong
|
|
* in time.time()!) We're limited by the precision of a double,
|
|
* though.
|
|
*/
|
|
PyObject *time;
|
|
double dtime;
|
|
|
|
time = time_time();
|
|
if (time == NULL)
|
|
return NULL;
|
|
dtime = PyFloat_AsDouble(time);
|
|
Py_DECREF(time);
|
|
if (dtime == -1.0 && PyErr_Occurred())
|
|
return NULL;
|
|
return datetime_from_timestamp(cls, f, dtime, tzinfo);
|
|
#endif /* ! HAVE_GETTIMEOFDAY */
|
|
}
|
|
|
|
/* Return best possible local time -- this isn't constrained by the
|
|
* precision of a timestamp.
|
|
*/
|
|
static PyObject *
|
|
datetime_now(PyObject *cls, PyObject *args, PyObject *kw)
|
|
{
|
|
PyObject *self;
|
|
PyObject *tzinfo = Py_None;
|
|
static char *keywords[] = {"tz", NULL};
|
|
|
|
if (! PyArg_ParseTupleAndKeywords(args, kw, "|O:now", keywords,
|
|
&tzinfo))
|
|
return NULL;
|
|
if (check_tzinfo_subclass(tzinfo) < 0)
|
|
return NULL;
|
|
|
|
self = datetime_best_possible(cls,
|
|
tzinfo == Py_None ? localtime : gmtime,
|
|
tzinfo);
|
|
if (self != NULL && tzinfo != Py_None) {
|
|
/* Convert UTC to tzinfo's zone. */
|
|
PyObject *temp = self;
|
|
self = PyObject_CallMethod(tzinfo, "fromutc", "O", self);
|
|
Py_DECREF(temp);
|
|
}
|
|
return self;
|
|
}
|
|
|
|
/* Return best possible UTC time -- this isn't constrained by the
|
|
* precision of a timestamp.
|
|
*/
|
|
static PyObject *
|
|
datetime_utcnow(PyObject *cls, PyObject *dummy)
|
|
{
|
|
return datetime_best_possible(cls, gmtime, Py_None);
|
|
}
|
|
|
|
/* Return new local datetime from timestamp (Python timestamp -- a double). */
|
|
static PyObject *
|
|
datetime_fromtimestamp(PyObject *cls, PyObject *args, PyObject *kw)
|
|
{
|
|
PyObject *self;
|
|
double timestamp;
|
|
PyObject *tzinfo = Py_None;
|
|
static char *keywords[] = {"timestamp", "tz", NULL};
|
|
|
|
if (! PyArg_ParseTupleAndKeywords(args, kw, "d|O:fromtimestamp",
|
|
keywords, ×tamp, &tzinfo))
|
|
return NULL;
|
|
if (check_tzinfo_subclass(tzinfo) < 0)
|
|
return NULL;
|
|
|
|
self = datetime_from_timestamp(cls,
|
|
tzinfo == Py_None ? localtime : gmtime,
|
|
timestamp,
|
|
tzinfo);
|
|
if (self != NULL && tzinfo != Py_None) {
|
|
/* Convert UTC to tzinfo's zone. */
|
|
PyObject *temp = self;
|
|
self = PyObject_CallMethod(tzinfo, "fromutc", "O", self);
|
|
Py_DECREF(temp);
|
|
}
|
|
return self;
|
|
}
|
|
|
|
/* Return new UTC datetime from timestamp (Python timestamp -- a double). */
|
|
static PyObject *
|
|
datetime_utcfromtimestamp(PyObject *cls, PyObject *args)
|
|
{
|
|
double timestamp;
|
|
PyObject *result = NULL;
|
|
|
|
if (PyArg_ParseTuple(args, "d:utcfromtimestamp", ×tamp))
|
|
result = datetime_from_timestamp(cls, gmtime, timestamp,
|
|
Py_None);
|
|
return result;
|
|
}
|
|
|
|
/* Return new datetime from time.strptime(). */
|
|
static PyObject *
|
|
datetime_strptime(PyObject *cls, PyObject *args)
|
|
{
|
|
PyObject *result = NULL, *obj, *module;
|
|
const char *string, *format;
|
|
|
|
if (!PyArg_ParseTuple(args, "ss:strptime", &string, &format))
|
|
return NULL;
|
|
|
|
if ((module = PyImport_ImportModule("time")) == NULL)
|
|
return NULL;
|
|
obj = PyObject_CallMethod(module, "strptime", "ss", string, format);
|
|
Py_DECREF(module);
|
|
|
|
if (obj != NULL) {
|
|
int i, good_timetuple = 1;
|
|
long int ia[6];
|
|
if (PySequence_Check(obj) && PySequence_Size(obj) >= 6)
|
|
for (i=0; i < 6; i++) {
|
|
PyObject *p = PySequence_GetItem(obj, i);
|
|
if (p == NULL) {
|
|
Py_DECREF(obj);
|
|
return NULL;
|
|
}
|
|
if (PyInt_Check(p))
|
|
ia[i] = PyInt_AsLong(p);
|
|
else
|
|
good_timetuple = 0;
|
|
Py_DECREF(p);
|
|
}
|
|
else
|
|
good_timetuple = 0;
|
|
if (good_timetuple)
|
|
result = PyObject_CallFunction(cls, "iiiiii",
|
|
ia[0], ia[1], ia[2], ia[3], ia[4], ia[5]);
|
|
else
|
|
PyErr_SetString(PyExc_ValueError,
|
|
"unexpected value from time.strptime");
|
|
Py_DECREF(obj);
|
|
}
|
|
return result;
|
|
}
|
|
|
|
/* Return new datetime from date/datetime and time arguments. */
|
|
static PyObject *
|
|
datetime_combine(PyObject *cls, PyObject *args, PyObject *kw)
|
|
{
|
|
static char *keywords[] = {"date", "time", NULL};
|
|
PyObject *date;
|
|
PyObject *time;
|
|
PyObject *result = NULL;
|
|
|
|
if (PyArg_ParseTupleAndKeywords(args, kw, "O!O!:combine", keywords,
|
|
&PyDateTime_DateType, &date,
|
|
&PyDateTime_TimeType, &time)) {
|
|
PyObject *tzinfo = Py_None;
|
|
|
|
if (HASTZINFO(time))
|
|
tzinfo = ((PyDateTime_Time *)time)->tzinfo;
|
|
result = PyObject_CallFunction(cls, "iiiiiiiO",
|
|
GET_YEAR(date),
|
|
GET_MONTH(date),
|
|
GET_DAY(date),
|
|
TIME_GET_HOUR(time),
|
|
TIME_GET_MINUTE(time),
|
|
TIME_GET_SECOND(time),
|
|
TIME_GET_MICROSECOND(time),
|
|
tzinfo);
|
|
}
|
|
return result;
|
|
}
|
|
|
|
/*
|
|
* Destructor.
|
|
*/
|
|
|
|
static void
|
|
datetime_dealloc(PyDateTime_DateTime *self)
|
|
{
|
|
if (HASTZINFO(self)) {
|
|
Py_XDECREF(self->tzinfo);
|
|
}
|
|
self->ob_type->tp_free((PyObject *)self);
|
|
}
|
|
|
|
/*
|
|
* Indirect access to tzinfo methods.
|
|
*/
|
|
|
|
/* These are all METH_NOARGS, so don't need to check the arglist. */
|
|
static PyObject *
|
|
datetime_utcoffset(PyDateTime_DateTime *self, PyObject *unused) {
|
|
return offset_as_timedelta(HASTZINFO(self) ? self->tzinfo : Py_None,
|
|
"utcoffset", (PyObject *)self);
|
|
}
|
|
|
|
static PyObject *
|
|
datetime_dst(PyDateTime_DateTime *self, PyObject *unused) {
|
|
return offset_as_timedelta(HASTZINFO(self) ? self->tzinfo : Py_None,
|
|
"dst", (PyObject *)self);
|
|
}
|
|
|
|
static PyObject *
|
|
datetime_tzname(PyDateTime_DateTime *self, PyObject *unused) {
|
|
return call_tzname(HASTZINFO(self) ? self->tzinfo : Py_None,
|
|
(PyObject *)self);
|
|
}
|
|
|
|
/*
|
|
* datetime arithmetic.
|
|
*/
|
|
|
|
/* factor must be 1 (to add) or -1 (to subtract). The result inherits
|
|
* the tzinfo state of date.
|
|
*/
|
|
static PyObject *
|
|
add_datetime_timedelta(PyDateTime_DateTime *date, PyDateTime_Delta *delta,
|
|
int factor)
|
|
{
|
|
/* Note that the C-level additions can't overflow, because of
|
|
* invariant bounds on the member values.
|
|
*/
|
|
int year = GET_YEAR(date);
|
|
int month = GET_MONTH(date);
|
|
int day = GET_DAY(date) + GET_TD_DAYS(delta) * factor;
|
|
int hour = DATE_GET_HOUR(date);
|
|
int minute = DATE_GET_MINUTE(date);
|
|
int second = DATE_GET_SECOND(date) + GET_TD_SECONDS(delta) * factor;
|
|
int microsecond = DATE_GET_MICROSECOND(date) +
|
|
GET_TD_MICROSECONDS(delta) * factor;
|
|
|
|
assert(factor == 1 || factor == -1);
|
|
if (normalize_datetime(&year, &month, &day,
|
|
&hour, &minute, &second, µsecond) < 0)
|
|
return NULL;
|
|
else
|
|
return new_datetime(year, month, day,
|
|
hour, minute, second, microsecond,
|
|
HASTZINFO(date) ? date->tzinfo : Py_None);
|
|
}
|
|
|
|
static PyObject *
|
|
datetime_add(PyObject *left, PyObject *right)
|
|
{
|
|
if (PyDateTime_Check(left)) {
|
|
/* datetime + ??? */
|
|
if (PyDelta_Check(right))
|
|
/* datetime + delta */
|
|
return add_datetime_timedelta(
|
|
(PyDateTime_DateTime *)left,
|
|
(PyDateTime_Delta *)right,
|
|
1);
|
|
}
|
|
else if (PyDelta_Check(left)) {
|
|
/* delta + datetime */
|
|
return add_datetime_timedelta((PyDateTime_DateTime *) right,
|
|
(PyDateTime_Delta *) left,
|
|
1);
|
|
}
|
|
Py_INCREF(Py_NotImplemented);
|
|
return Py_NotImplemented;
|
|
}
|
|
|
|
static PyObject *
|
|
datetime_subtract(PyObject *left, PyObject *right)
|
|
{
|
|
PyObject *result = Py_NotImplemented;
|
|
|
|
if (PyDateTime_Check(left)) {
|
|
/* datetime - ??? */
|
|
if (PyDateTime_Check(right)) {
|
|
/* datetime - datetime */
|
|
naivety n1, n2;
|
|
int offset1, offset2;
|
|
int delta_d, delta_s, delta_us;
|
|
|
|
if (classify_two_utcoffsets(left, &offset1, &n1, left,
|
|
right, &offset2, &n2,
|
|
right) < 0)
|
|
return NULL;
|
|
assert(n1 != OFFSET_UNKNOWN && n2 != OFFSET_UNKNOWN);
|
|
if (n1 != n2) {
|
|
PyErr_SetString(PyExc_TypeError,
|
|
"can't subtract offset-naive and "
|
|
"offset-aware datetimes");
|
|
return NULL;
|
|
}
|
|
delta_d = ymd_to_ord(GET_YEAR(left),
|
|
GET_MONTH(left),
|
|
GET_DAY(left)) -
|
|
ymd_to_ord(GET_YEAR(right),
|
|
GET_MONTH(right),
|
|
GET_DAY(right));
|
|
/* These can't overflow, since the values are
|
|
* normalized. At most this gives the number of
|
|
* seconds in one day.
|
|
*/
|
|
delta_s = (DATE_GET_HOUR(left) -
|
|
DATE_GET_HOUR(right)) * 3600 +
|
|
(DATE_GET_MINUTE(left) -
|
|
DATE_GET_MINUTE(right)) * 60 +
|
|
(DATE_GET_SECOND(left) -
|
|
DATE_GET_SECOND(right));
|
|
delta_us = DATE_GET_MICROSECOND(left) -
|
|
DATE_GET_MICROSECOND(right);
|
|
/* (left - offset1) - (right - offset2) =
|
|
* (left - right) + (offset2 - offset1)
|
|
*/
|
|
delta_s += (offset2 - offset1) * 60;
|
|
result = new_delta(delta_d, delta_s, delta_us, 1);
|
|
}
|
|
else if (PyDelta_Check(right)) {
|
|
/* datetime - delta */
|
|
result = add_datetime_timedelta(
|
|
(PyDateTime_DateTime *)left,
|
|
(PyDateTime_Delta *)right,
|
|
-1);
|
|
}
|
|
}
|
|
|
|
if (result == Py_NotImplemented)
|
|
Py_INCREF(result);
|
|
return result;
|
|
}
|
|
|
|
/* Various ways to turn a datetime into a string. */
|
|
|
|
static PyObject *
|
|
datetime_repr(PyDateTime_DateTime *self)
|
|
{
|
|
char buffer[1000];
|
|
const char *type_name = self->ob_type->tp_name;
|
|
PyObject *baserepr;
|
|
|
|
if (DATE_GET_MICROSECOND(self)) {
|
|
PyOS_snprintf(buffer, sizeof(buffer),
|
|
"%s(%d, %d, %d, %d, %d, %d, %d)",
|
|
type_name,
|
|
GET_YEAR(self), GET_MONTH(self), GET_DAY(self),
|
|
DATE_GET_HOUR(self), DATE_GET_MINUTE(self),
|
|
DATE_GET_SECOND(self),
|
|
DATE_GET_MICROSECOND(self));
|
|
}
|
|
else if (DATE_GET_SECOND(self)) {
|
|
PyOS_snprintf(buffer, sizeof(buffer),
|
|
"%s(%d, %d, %d, %d, %d, %d)",
|
|
type_name,
|
|
GET_YEAR(self), GET_MONTH(self), GET_DAY(self),
|
|
DATE_GET_HOUR(self), DATE_GET_MINUTE(self),
|
|
DATE_GET_SECOND(self));
|
|
}
|
|
else {
|
|
PyOS_snprintf(buffer, sizeof(buffer),
|
|
"%s(%d, %d, %d, %d, %d)",
|
|
type_name,
|
|
GET_YEAR(self), GET_MONTH(self), GET_DAY(self),
|
|
DATE_GET_HOUR(self), DATE_GET_MINUTE(self));
|
|
}
|
|
baserepr = PyString_FromString(buffer);
|
|
if (baserepr == NULL || ! HASTZINFO(self))
|
|
return baserepr;
|
|
return append_keyword_tzinfo(baserepr, self->tzinfo);
|
|
}
|
|
|
|
static PyObject *
|
|
datetime_str(PyDateTime_DateTime *self)
|
|
{
|
|
return PyObject_CallMethod((PyObject *)self, "isoformat", "(s)", " ");
|
|
}
|
|
|
|
static PyObject *
|
|
datetime_isoformat(PyDateTime_DateTime *self, PyObject *args, PyObject *kw)
|
|
{
|
|
char sep = 'T';
|
|
static char *keywords[] = {"sep", NULL};
|
|
char buffer[100];
|
|
char *cp;
|
|
PyObject *result;
|
|
|
|
if (!PyArg_ParseTupleAndKeywords(args, kw, "|c:isoformat", keywords,
|
|
&sep))
|
|
return NULL;
|
|
cp = isoformat_date((PyDateTime_Date *)self, buffer, sizeof(buffer));
|
|
assert(cp != NULL);
|
|
*cp++ = sep;
|
|
isoformat_time(self, cp, sizeof(buffer) - (cp - buffer));
|
|
result = PyString_FromString(buffer);
|
|
if (result == NULL || ! HASTZINFO(self))
|
|
return result;
|
|
|
|
/* We need to append the UTC offset. */
|
|
if (format_utcoffset(buffer, sizeof(buffer), ":", self->tzinfo,
|
|
(PyObject *)self) < 0) {
|
|
Py_DECREF(result);
|
|
return NULL;
|
|
}
|
|
PyString_ConcatAndDel(&result, PyString_FromString(buffer));
|
|
return result;
|
|
}
|
|
|
|
static PyObject *
|
|
datetime_ctime(PyDateTime_DateTime *self)
|
|
{
|
|
return format_ctime((PyDateTime_Date *)self,
|
|
DATE_GET_HOUR(self),
|
|
DATE_GET_MINUTE(self),
|
|
DATE_GET_SECOND(self));
|
|
}
|
|
|
|
/* Miscellaneous methods. */
|
|
|
|
static PyObject *
|
|
datetime_richcompare(PyObject *self, PyObject *other, int op)
|
|
{
|
|
int diff;
|
|
naivety n1, n2;
|
|
int offset1, offset2;
|
|
|
|
if (! PyDateTime_Check(other)) {
|
|
if (PyDate_Check(other)) {
|
|
/* Prevent invocation of date_richcompare. We want to
|
|
return NotImplemented here to give the other object
|
|
a chance. But since DateTime is a subclass of
|
|
Date, if the other object is a Date, it would
|
|
compute an ordering based on the date part alone,
|
|
and we don't want that. So force unequal or
|
|
uncomparable here in that case. */
|
|
if (op == Py_EQ)
|
|
Py_RETURN_FALSE;
|
|
if (op == Py_NE)
|
|
Py_RETURN_TRUE;
|
|
return cmperror(self, other);
|
|
}
|
|
Py_INCREF(Py_NotImplemented);
|
|
return Py_NotImplemented;
|
|
}
|
|
|
|
if (classify_two_utcoffsets(self, &offset1, &n1, self,
|
|
other, &offset2, &n2, other) < 0)
|
|
return NULL;
|
|
assert(n1 != OFFSET_UNKNOWN && n2 != OFFSET_UNKNOWN);
|
|
/* If they're both naive, or both aware and have the same offsets,
|
|
* we get off cheap. Note that if they're both naive, offset1 ==
|
|
* offset2 == 0 at this point.
|
|
*/
|
|
if (n1 == n2 && offset1 == offset2) {
|
|
diff = memcmp(((PyDateTime_DateTime *)self)->data,
|
|
((PyDateTime_DateTime *)other)->data,
|
|
_PyDateTime_DATETIME_DATASIZE);
|
|
return diff_to_bool(diff, op);
|
|
}
|
|
|
|
if (n1 == OFFSET_AWARE && n2 == OFFSET_AWARE) {
|
|
PyDateTime_Delta *delta;
|
|
|
|
assert(offset1 != offset2); /* else last "if" handled it */
|
|
delta = (PyDateTime_Delta *)datetime_subtract((PyObject *)self,
|
|
other);
|
|
if (delta == NULL)
|
|
return NULL;
|
|
diff = GET_TD_DAYS(delta);
|
|
if (diff == 0)
|
|
diff = GET_TD_SECONDS(delta) |
|
|
GET_TD_MICROSECONDS(delta);
|
|
Py_DECREF(delta);
|
|
return diff_to_bool(diff, op);
|
|
}
|
|
|
|
assert(n1 != n2);
|
|
PyErr_SetString(PyExc_TypeError,
|
|
"can't compare offset-naive and "
|
|
"offset-aware datetimes");
|
|
return NULL;
|
|
}
|
|
|
|
static long
|
|
datetime_hash(PyDateTime_DateTime *self)
|
|
{
|
|
if (self->hashcode == -1) {
|
|
naivety n;
|
|
int offset;
|
|
PyObject *temp;
|
|
|
|
n = classify_utcoffset((PyObject *)self, (PyObject *)self,
|
|
&offset);
|
|
assert(n != OFFSET_UNKNOWN);
|
|
if (n == OFFSET_ERROR)
|
|
return -1;
|
|
|
|
/* Reduce this to a hash of another object. */
|
|
if (n == OFFSET_NAIVE)
|
|
temp = PyString_FromStringAndSize(
|
|
(char *)self->data,
|
|
_PyDateTime_DATETIME_DATASIZE);
|
|
else {
|
|
int days;
|
|
int seconds;
|
|
|
|
assert(n == OFFSET_AWARE);
|
|
assert(HASTZINFO(self));
|
|
days = ymd_to_ord(GET_YEAR(self),
|
|
GET_MONTH(self),
|
|
GET_DAY(self));
|
|
seconds = DATE_GET_HOUR(self) * 3600 +
|
|
(DATE_GET_MINUTE(self) - offset) * 60 +
|
|
DATE_GET_SECOND(self);
|
|
temp = new_delta(days,
|
|
seconds,
|
|
DATE_GET_MICROSECOND(self),
|
|
1);
|
|
}
|
|
if (temp != NULL) {
|
|
self->hashcode = PyObject_Hash(temp);
|
|
Py_DECREF(temp);
|
|
}
|
|
}
|
|
return self->hashcode;
|
|
}
|
|
|
|
static PyObject *
|
|
datetime_replace(PyDateTime_DateTime *self, PyObject *args, PyObject *kw)
|
|
{
|
|
PyObject *clone;
|
|
PyObject *tuple;
|
|
int y = GET_YEAR(self);
|
|
int m = GET_MONTH(self);
|
|
int d = GET_DAY(self);
|
|
int hh = DATE_GET_HOUR(self);
|
|
int mm = DATE_GET_MINUTE(self);
|
|
int ss = DATE_GET_SECOND(self);
|
|
int us = DATE_GET_MICROSECOND(self);
|
|
PyObject *tzinfo = HASTZINFO(self) ? self->tzinfo : Py_None;
|
|
|
|
if (! PyArg_ParseTupleAndKeywords(args, kw, "|iiiiiiiO:replace",
|
|
datetime_kws,
|
|
&y, &m, &d, &hh, &mm, &ss, &us,
|
|
&tzinfo))
|
|
return NULL;
|
|
tuple = Py_BuildValue("iiiiiiiO", y, m, d, hh, mm, ss, us, tzinfo);
|
|
if (tuple == NULL)
|
|
return NULL;
|
|
clone = datetime_new(self->ob_type, tuple, NULL);
|
|
Py_DECREF(tuple);
|
|
return clone;
|
|
}
|
|
|
|
static PyObject *
|
|
datetime_astimezone(PyDateTime_DateTime *self, PyObject *args, PyObject *kw)
|
|
{
|
|
int y, m, d, hh, mm, ss, us;
|
|
PyObject *result;
|
|
int offset, none;
|
|
|
|
PyObject *tzinfo;
|
|
static char *keywords[] = {"tz", NULL};
|
|
|
|
if (! PyArg_ParseTupleAndKeywords(args, kw, "O!:astimezone", keywords,
|
|
&PyDateTime_TZInfoType, &tzinfo))
|
|
return NULL;
|
|
|
|
if (!HASTZINFO(self) || self->tzinfo == Py_None)
|
|
goto NeedAware;
|
|
|
|
/* Conversion to self's own time zone is a NOP. */
|
|
if (self->tzinfo == tzinfo) {
|
|
Py_INCREF(self);
|
|
return (PyObject *)self;
|
|
}
|
|
|
|
/* Convert self to UTC. */
|
|
offset = call_utcoffset(self->tzinfo, (PyObject *)self, &none);
|
|
if (offset == -1 && PyErr_Occurred())
|
|
return NULL;
|
|
if (none)
|
|
goto NeedAware;
|
|
|
|
y = GET_YEAR(self);
|
|
m = GET_MONTH(self);
|
|
d = GET_DAY(self);
|
|
hh = DATE_GET_HOUR(self);
|
|
mm = DATE_GET_MINUTE(self);
|
|
ss = DATE_GET_SECOND(self);
|
|
us = DATE_GET_MICROSECOND(self);
|
|
|
|
mm -= offset;
|
|
if ((mm < 0 || mm >= 60) &&
|
|
normalize_datetime(&y, &m, &d, &hh, &mm, &ss, &us) < 0)
|
|
return NULL;
|
|
|
|
/* Attach new tzinfo and let fromutc() do the rest. */
|
|
result = new_datetime(y, m, d, hh, mm, ss, us, tzinfo);
|
|
if (result != NULL) {
|
|
PyObject *temp = result;
|
|
|
|
result = PyObject_CallMethod(tzinfo, "fromutc", "O", temp);
|
|
Py_DECREF(temp);
|
|
}
|
|
return result;
|
|
|
|
NeedAware:
|
|
PyErr_SetString(PyExc_ValueError, "astimezone() cannot be applied to "
|
|
"a naive datetime");
|
|
return NULL;
|
|
}
|
|
|
|
static PyObject *
|
|
datetime_timetuple(PyDateTime_DateTime *self)
|
|
{
|
|
int dstflag = -1;
|
|
|
|
if (HASTZINFO(self) && self->tzinfo != Py_None) {
|
|
int none;
|
|
|
|
dstflag = call_dst(self->tzinfo, (PyObject *)self, &none);
|
|
if (dstflag == -1 && PyErr_Occurred())
|
|
return NULL;
|
|
|
|
if (none)
|
|
dstflag = -1;
|
|
else if (dstflag != 0)
|
|
dstflag = 1;
|
|
|
|
}
|
|
return build_struct_time(GET_YEAR(self),
|
|
GET_MONTH(self),
|
|
GET_DAY(self),
|
|
DATE_GET_HOUR(self),
|
|
DATE_GET_MINUTE(self),
|
|
DATE_GET_SECOND(self),
|
|
dstflag);
|
|
}
|
|
|
|
static PyObject *
|
|
datetime_getdate(PyDateTime_DateTime *self)
|
|
{
|
|
return new_date(GET_YEAR(self),
|
|
GET_MONTH(self),
|
|
GET_DAY(self));
|
|
}
|
|
|
|
static PyObject *
|
|
datetime_gettime(PyDateTime_DateTime *self)
|
|
{
|
|
return new_time(DATE_GET_HOUR(self),
|
|
DATE_GET_MINUTE(self),
|
|
DATE_GET_SECOND(self),
|
|
DATE_GET_MICROSECOND(self),
|
|
Py_None);
|
|
}
|
|
|
|
static PyObject *
|
|
datetime_gettimetz(PyDateTime_DateTime *self)
|
|
{
|
|
return new_time(DATE_GET_HOUR(self),
|
|
DATE_GET_MINUTE(self),
|
|
DATE_GET_SECOND(self),
|
|
DATE_GET_MICROSECOND(self),
|
|
HASTZINFO(self) ? self->tzinfo : Py_None);
|
|
}
|
|
|
|
static PyObject *
|
|
datetime_utctimetuple(PyDateTime_DateTime *self)
|
|
{
|
|
int y = GET_YEAR(self);
|
|
int m = GET_MONTH(self);
|
|
int d = GET_DAY(self);
|
|
int hh = DATE_GET_HOUR(self);
|
|
int mm = DATE_GET_MINUTE(self);
|
|
int ss = DATE_GET_SECOND(self);
|
|
int us = 0; /* microseconds are ignored in a timetuple */
|
|
int offset = 0;
|
|
|
|
if (HASTZINFO(self) && self->tzinfo != Py_None) {
|
|
int none;
|
|
|
|
offset = call_utcoffset(self->tzinfo, (PyObject *)self, &none);
|
|
if (offset == -1 && PyErr_Occurred())
|
|
return NULL;
|
|
}
|
|
/* Even if offset is 0, don't call timetuple() -- tm_isdst should be
|
|
* 0 in a UTC timetuple regardless of what dst() says.
|
|
*/
|
|
if (offset) {
|
|
/* Subtract offset minutes & normalize. */
|
|
int stat;
|
|
|
|
mm -= offset;
|
|
stat = normalize_datetime(&y, &m, &d, &hh, &mm, &ss, &us);
|
|
if (stat < 0) {
|
|
/* At the edges, it's possible we overflowed
|
|
* beyond MINYEAR or MAXYEAR.
|
|
*/
|
|
if (PyErr_ExceptionMatches(PyExc_OverflowError))
|
|
PyErr_Clear();
|
|
else
|
|
return NULL;
|
|
}
|
|
}
|
|
return build_struct_time(y, m, d, hh, mm, ss, 0);
|
|
}
|
|
|
|
/* Pickle support, a simple use of __reduce__. */
|
|
|
|
/* Let basestate be the non-tzinfo data string.
|
|
* If tzinfo is None, this returns (basestate,), else (basestate, tzinfo).
|
|
* So it's a tuple in any (non-error) case.
|
|
* __getstate__ isn't exposed.
|
|
*/
|
|
static PyObject *
|
|
datetime_getstate(PyDateTime_DateTime *self)
|
|
{
|
|
PyObject *basestate;
|
|
PyObject *result = NULL;
|
|
|
|
basestate = PyString_FromStringAndSize((char *)self->data,
|
|
_PyDateTime_DATETIME_DATASIZE);
|
|
if (basestate != NULL) {
|
|
if (! HASTZINFO(self) || self->tzinfo == Py_None)
|
|
result = PyTuple_Pack(1, basestate);
|
|
else
|
|
result = PyTuple_Pack(2, basestate, self->tzinfo);
|
|
Py_DECREF(basestate);
|
|
}
|
|
return result;
|
|
}
|
|
|
|
static PyObject *
|
|
datetime_reduce(PyDateTime_DateTime *self, PyObject *arg)
|
|
{
|
|
return Py_BuildValue("(ON)", self->ob_type, datetime_getstate(self));
|
|
}
|
|
|
|
static PyMethodDef datetime_methods[] = {
|
|
|
|
/* Class methods: */
|
|
|
|
{"now", (PyCFunction)datetime_now,
|
|
METH_KEYWORDS | METH_CLASS,
|
|
PyDoc_STR("[tz] -> new datetime with tz's local day and time.")},
|
|
|
|
{"utcnow", (PyCFunction)datetime_utcnow,
|
|
METH_NOARGS | METH_CLASS,
|
|
PyDoc_STR("Return a new datetime representing UTC day and time.")},
|
|
|
|
{"fromtimestamp", (PyCFunction)datetime_fromtimestamp,
|
|
METH_KEYWORDS | METH_CLASS,
|
|
PyDoc_STR("timestamp[, tz] -> tz's local time from POSIX timestamp.")},
|
|
|
|
{"utcfromtimestamp", (PyCFunction)datetime_utcfromtimestamp,
|
|
METH_VARARGS | METH_CLASS,
|
|
PyDoc_STR("timestamp -> UTC datetime from a POSIX timestamp "
|
|
"(like time.time()).")},
|
|
|
|
{"strptime", (PyCFunction)datetime_strptime,
|
|
METH_VARARGS | METH_CLASS,
|
|
PyDoc_STR("string, format -> new datetime parsed from a string "
|
|
"(like time.strptime()).")},
|
|
|
|
{"combine", (PyCFunction)datetime_combine,
|
|
METH_VARARGS | METH_KEYWORDS | METH_CLASS,
|
|
PyDoc_STR("date, time -> datetime with same date and time fields")},
|
|
|
|
/* Instance methods: */
|
|
|
|
{"date", (PyCFunction)datetime_getdate, METH_NOARGS,
|
|
PyDoc_STR("Return date object with same year, month and day.")},
|
|
|
|
{"time", (PyCFunction)datetime_gettime, METH_NOARGS,
|
|
PyDoc_STR("Return time object with same time but with tzinfo=None.")},
|
|
|
|
{"timetz", (PyCFunction)datetime_gettimetz, METH_NOARGS,
|
|
PyDoc_STR("Return time object with same time and tzinfo.")},
|
|
|
|
{"ctime", (PyCFunction)datetime_ctime, METH_NOARGS,
|
|
PyDoc_STR("Return ctime() style string.")},
|
|
|
|
{"timetuple", (PyCFunction)datetime_timetuple, METH_NOARGS,
|
|
PyDoc_STR("Return time tuple, compatible with time.localtime().")},
|
|
|
|
{"utctimetuple", (PyCFunction)datetime_utctimetuple, METH_NOARGS,
|
|
PyDoc_STR("Return UTC time tuple, compatible with time.localtime().")},
|
|
|
|
{"isoformat", (PyCFunction)datetime_isoformat, METH_KEYWORDS,
|
|
PyDoc_STR("[sep] -> string in ISO 8601 format, "
|
|
"YYYY-MM-DDTHH:MM:SS[.mmmmmm][+HH:MM].\n\n"
|
|
"sep is used to separate the year from the time, and "
|
|
"defaults to 'T'.")},
|
|
|
|
{"utcoffset", (PyCFunction)datetime_utcoffset, METH_NOARGS,
|
|
PyDoc_STR("Return self.tzinfo.utcoffset(self).")},
|
|
|
|
{"tzname", (PyCFunction)datetime_tzname, METH_NOARGS,
|
|
PyDoc_STR("Return self.tzinfo.tzname(self).")},
|
|
|
|
{"dst", (PyCFunction)datetime_dst, METH_NOARGS,
|
|
PyDoc_STR("Return self.tzinfo.dst(self).")},
|
|
|
|
{"replace", (PyCFunction)datetime_replace, METH_KEYWORDS,
|
|
PyDoc_STR("Return datetime with new specified fields.")},
|
|
|
|
{"astimezone", (PyCFunction)datetime_astimezone, METH_KEYWORDS,
|
|
PyDoc_STR("tz -> convert to local time in new timezone tz\n")},
|
|
|
|
{"__reduce__", (PyCFunction)datetime_reduce, METH_NOARGS,
|
|
PyDoc_STR("__reduce__() -> (cls, state)")},
|
|
|
|
{NULL, NULL}
|
|
};
|
|
|
|
static char datetime_doc[] =
|
|
PyDoc_STR("datetime(year, month, day[, hour[, minute[, second[, microsecond[,tzinfo]]]]])\n\
|
|
\n\
|
|
The year, month and day arguments are required. tzinfo may be None, or an\n\
|
|
instance of a tzinfo subclass. The remaining arguments may be ints or longs.\n");
|
|
|
|
static PyNumberMethods datetime_as_number = {
|
|
datetime_add, /* nb_add */
|
|
datetime_subtract, /* nb_subtract */
|
|
0, /* nb_multiply */
|
|
0, /* nb_remainder */
|
|
0, /* nb_divmod */
|
|
0, /* nb_power */
|
|
0, /* nb_negative */
|
|
0, /* nb_positive */
|
|
0, /* nb_absolute */
|
|
0, /* nb_bool */
|
|
};
|
|
|
|
static PyTypeObject PyDateTime_DateTimeType = {
|
|
PyObject_HEAD_INIT(NULL)
|
|
0, /* ob_size */
|
|
"datetime.datetime", /* tp_name */
|
|
sizeof(PyDateTime_DateTime), /* tp_basicsize */
|
|
0, /* tp_itemsize */
|
|
(destructor)datetime_dealloc, /* tp_dealloc */
|
|
0, /* tp_print */
|
|
0, /* tp_getattr */
|
|
0, /* tp_setattr */
|
|
0, /* tp_compare */
|
|
(reprfunc)datetime_repr, /* tp_repr */
|
|
&datetime_as_number, /* tp_as_number */
|
|
0, /* tp_as_sequence */
|
|
0, /* tp_as_mapping */
|
|
(hashfunc)datetime_hash, /* tp_hash */
|
|
0, /* tp_call */
|
|
(reprfunc)datetime_str, /* tp_str */
|
|
PyObject_GenericGetAttr, /* tp_getattro */
|
|
0, /* tp_setattro */
|
|
0, /* tp_as_buffer */
|
|
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
|
|
datetime_doc, /* tp_doc */
|
|
0, /* tp_traverse */
|
|
0, /* tp_clear */
|
|
datetime_richcompare, /* tp_richcompare */
|
|
0, /* tp_weaklistoffset */
|
|
0, /* tp_iter */
|
|
0, /* tp_iternext */
|
|
datetime_methods, /* tp_methods */
|
|
0, /* tp_members */
|
|
datetime_getset, /* tp_getset */
|
|
&PyDateTime_DateType, /* tp_base */
|
|
0, /* tp_dict */
|
|
0, /* tp_descr_get */
|
|
0, /* tp_descr_set */
|
|
0, /* tp_dictoffset */
|
|
0, /* tp_init */
|
|
datetime_alloc, /* tp_alloc */
|
|
datetime_new, /* tp_new */
|
|
0, /* tp_free */
|
|
};
|
|
|
|
/* ---------------------------------------------------------------------------
|
|
* Module methods and initialization.
|
|
*/
|
|
|
|
static PyMethodDef module_methods[] = {
|
|
{NULL, NULL}
|
|
};
|
|
|
|
/* C API. Clients get at this via PyDateTime_IMPORT, defined in
|
|
* datetime.h.
|
|
*/
|
|
static PyDateTime_CAPI CAPI = {
|
|
&PyDateTime_DateType,
|
|
&PyDateTime_DateTimeType,
|
|
&PyDateTime_TimeType,
|
|
&PyDateTime_DeltaType,
|
|
&PyDateTime_TZInfoType,
|
|
new_date_ex,
|
|
new_datetime_ex,
|
|
new_time_ex,
|
|
new_delta_ex,
|
|
datetime_fromtimestamp,
|
|
date_fromtimestamp
|
|
};
|
|
|
|
|
|
PyMODINIT_FUNC
|
|
initdatetime(void)
|
|
{
|
|
PyObject *m; /* a module object */
|
|
PyObject *d; /* its dict */
|
|
PyObject *x;
|
|
|
|
m = Py_InitModule3("datetime", module_methods,
|
|
"Fast implementation of the datetime type.");
|
|
if (m == NULL)
|
|
return;
|
|
|
|
if (PyType_Ready(&PyDateTime_DateType) < 0)
|
|
return;
|
|
if (PyType_Ready(&PyDateTime_DateTimeType) < 0)
|
|
return;
|
|
if (PyType_Ready(&PyDateTime_DeltaType) < 0)
|
|
return;
|
|
if (PyType_Ready(&PyDateTime_TimeType) < 0)
|
|
return;
|
|
if (PyType_Ready(&PyDateTime_TZInfoType) < 0)
|
|
return;
|
|
|
|
/* timedelta values */
|
|
d = PyDateTime_DeltaType.tp_dict;
|
|
|
|
x = new_delta(0, 0, 1, 0);
|
|
if (x == NULL || PyDict_SetItemString(d, "resolution", x) < 0)
|
|
return;
|
|
Py_DECREF(x);
|
|
|
|
x = new_delta(-MAX_DELTA_DAYS, 0, 0, 0);
|
|
if (x == NULL || PyDict_SetItemString(d, "min", x) < 0)
|
|
return;
|
|
Py_DECREF(x);
|
|
|
|
x = new_delta(MAX_DELTA_DAYS, 24*3600-1, 1000000-1, 0);
|
|
if (x == NULL || PyDict_SetItemString(d, "max", x) < 0)
|
|
return;
|
|
Py_DECREF(x);
|
|
|
|
/* date values */
|
|
d = PyDateTime_DateType.tp_dict;
|
|
|
|
x = new_date(1, 1, 1);
|
|
if (x == NULL || PyDict_SetItemString(d, "min", x) < 0)
|
|
return;
|
|
Py_DECREF(x);
|
|
|
|
x = new_date(MAXYEAR, 12, 31);
|
|
if (x == NULL || PyDict_SetItemString(d, "max", x) < 0)
|
|
return;
|
|
Py_DECREF(x);
|
|
|
|
x = new_delta(1, 0, 0, 0);
|
|
if (x == NULL || PyDict_SetItemString(d, "resolution", x) < 0)
|
|
return;
|
|
Py_DECREF(x);
|
|
|
|
/* time values */
|
|
d = PyDateTime_TimeType.tp_dict;
|
|
|
|
x = new_time(0, 0, 0, 0, Py_None);
|
|
if (x == NULL || PyDict_SetItemString(d, "min", x) < 0)
|
|
return;
|
|
Py_DECREF(x);
|
|
|
|
x = new_time(23, 59, 59, 999999, Py_None);
|
|
if (x == NULL || PyDict_SetItemString(d, "max", x) < 0)
|
|
return;
|
|
Py_DECREF(x);
|
|
|
|
x = new_delta(0, 0, 1, 0);
|
|
if (x == NULL || PyDict_SetItemString(d, "resolution", x) < 0)
|
|
return;
|
|
Py_DECREF(x);
|
|
|
|
/* datetime values */
|
|
d = PyDateTime_DateTimeType.tp_dict;
|
|
|
|
x = new_datetime(1, 1, 1, 0, 0, 0, 0, Py_None);
|
|
if (x == NULL || PyDict_SetItemString(d, "min", x) < 0)
|
|
return;
|
|
Py_DECREF(x);
|
|
|
|
x = new_datetime(MAXYEAR, 12, 31, 23, 59, 59, 999999, Py_None);
|
|
if (x == NULL || PyDict_SetItemString(d, "max", x) < 0)
|
|
return;
|
|
Py_DECREF(x);
|
|
|
|
x = new_delta(0, 0, 1, 0);
|
|
if (x == NULL || PyDict_SetItemString(d, "resolution", x) < 0)
|
|
return;
|
|
Py_DECREF(x);
|
|
|
|
/* module initialization */
|
|
PyModule_AddIntConstant(m, "MINYEAR", MINYEAR);
|
|
PyModule_AddIntConstant(m, "MAXYEAR", MAXYEAR);
|
|
|
|
Py_INCREF(&PyDateTime_DateType);
|
|
PyModule_AddObject(m, "date", (PyObject *) &PyDateTime_DateType);
|
|
|
|
Py_INCREF(&PyDateTime_DateTimeType);
|
|
PyModule_AddObject(m, "datetime",
|
|
(PyObject *)&PyDateTime_DateTimeType);
|
|
|
|
Py_INCREF(&PyDateTime_TimeType);
|
|
PyModule_AddObject(m, "time", (PyObject *) &PyDateTime_TimeType);
|
|
|
|
Py_INCREF(&PyDateTime_DeltaType);
|
|
PyModule_AddObject(m, "timedelta", (PyObject *) &PyDateTime_DeltaType);
|
|
|
|
Py_INCREF(&PyDateTime_TZInfoType);
|
|
PyModule_AddObject(m, "tzinfo", (PyObject *) &PyDateTime_TZInfoType);
|
|
|
|
x = PyCObject_FromVoidPtrAndDesc(&CAPI, (void*) DATETIME_API_MAGIC,
|
|
NULL);
|
|
if (x == NULL)
|
|
return;
|
|
PyModule_AddObject(m, "datetime_CAPI", x);
|
|
|
|
/* A 4-year cycle has an extra leap day over what we'd get from
|
|
* pasting together 4 single years.
|
|
*/
|
|
assert(DI4Y == 4 * 365 + 1);
|
|
assert(DI4Y == days_before_year(4+1));
|
|
|
|
/* Similarly, a 400-year cycle has an extra leap day over what we'd
|
|
* get from pasting together 4 100-year cycles.
|
|
*/
|
|
assert(DI400Y == 4 * DI100Y + 1);
|
|
assert(DI400Y == days_before_year(400+1));
|
|
|
|
/* OTOH, a 100-year cycle has one fewer leap day than we'd get from
|
|
* pasting together 25 4-year cycles.
|
|
*/
|
|
assert(DI100Y == 25 * DI4Y - 1);
|
|
assert(DI100Y == days_before_year(100+1));
|
|
|
|
us_per_us = PyInt_FromLong(1);
|
|
us_per_ms = PyInt_FromLong(1000);
|
|
us_per_second = PyInt_FromLong(1000000);
|
|
us_per_minute = PyInt_FromLong(60000000);
|
|
seconds_per_day = PyInt_FromLong(24 * 3600);
|
|
if (us_per_us == NULL || us_per_ms == NULL || us_per_second == NULL ||
|
|
us_per_minute == NULL || seconds_per_day == NULL)
|
|
return;
|
|
|
|
/* The rest are too big for 32-bit ints, but even
|
|
* us_per_week fits in 40 bits, so doubles should be exact.
|
|
*/
|
|
us_per_hour = PyLong_FromDouble(3600000000.0);
|
|
us_per_day = PyLong_FromDouble(86400000000.0);
|
|
us_per_week = PyLong_FromDouble(604800000000.0);
|
|
if (us_per_hour == NULL || us_per_day == NULL || us_per_week == NULL)
|
|
return;
|
|
}
|
|
|
|
/* ---------------------------------------------------------------------------
|
|
Some time zone algebra. For a datetime x, let
|
|
x.n = x stripped of its timezone -- its naive time.
|
|
x.o = x.utcoffset(), and assuming that doesn't raise an exception or
|
|
return None
|
|
x.d = x.dst(), and assuming that doesn't raise an exception or
|
|
return None
|
|
x.s = x's standard offset, x.o - x.d
|
|
|
|
Now some derived rules, where k is a duration (timedelta).
|
|
|
|
1. x.o = x.s + x.d
|
|
This follows from the definition of x.s.
|
|
|
|
2. If x and y have the same tzinfo member, x.s = y.s.
|
|
This is actually a requirement, an assumption we need to make about
|
|
sane tzinfo classes.
|
|
|
|
3. The naive UTC time corresponding to x is x.n - x.o.
|
|
This is again a requirement for a sane tzinfo class.
|
|
|
|
4. (x+k).s = x.s
|
|
This follows from #2, and that datimetimetz+timedelta preserves tzinfo.
|
|
|
|
5. (x+k).n = x.n + k
|
|
Again follows from how arithmetic is defined.
|
|
|
|
Now we can explain tz.fromutc(x). Let's assume it's an interesting case
|
|
(meaning that the various tzinfo methods exist, and don't blow up or return
|
|
None when called).
|
|
|
|
The function wants to return a datetime y with timezone tz, equivalent to x.
|
|
x is already in UTC.
|
|
|
|
By #3, we want
|
|
|
|
y.n - y.o = x.n [1]
|
|
|
|
The algorithm starts by attaching tz to x.n, and calling that y. So
|
|
x.n = y.n at the start. Then it wants to add a duration k to y, so that [1]
|
|
becomes true; in effect, we want to solve [2] for k:
|
|
|
|
(y+k).n - (y+k).o = x.n [2]
|
|
|
|
By #1, this is the same as
|
|
|
|
(y+k).n - ((y+k).s + (y+k).d) = x.n [3]
|
|
|
|
By #5, (y+k).n = y.n + k, which equals x.n + k because x.n=y.n at the start.
|
|
Substituting that into [3],
|
|
|
|
x.n + k - (y+k).s - (y+k).d = x.n; the x.n terms cancel, leaving
|
|
k - (y+k).s - (y+k).d = 0; rearranging,
|
|
k = (y+k).s - (y+k).d; by #4, (y+k).s == y.s, so
|
|
k = y.s - (y+k).d
|
|
|
|
On the RHS, (y+k).d can't be computed directly, but y.s can be, and we
|
|
approximate k by ignoring the (y+k).d term at first. Note that k can't be
|
|
very large, since all offset-returning methods return a duration of magnitude
|
|
less than 24 hours. For that reason, if y is firmly in std time, (y+k).d must
|
|
be 0, so ignoring it has no consequence then.
|
|
|
|
In any case, the new value is
|
|
|
|
z = y + y.s [4]
|
|
|
|
It's helpful to step back at look at [4] from a higher level: it's simply
|
|
mapping from UTC to tz's standard time.
|
|
|
|
At this point, if
|
|
|
|
z.n - z.o = x.n [5]
|
|
|
|
we have an equivalent time, and are almost done. The insecurity here is
|
|
at the start of daylight time. Picture US Eastern for concreteness. The wall
|
|
time jumps from 1:59 to 3:00, and wall hours of the form 2:MM don't make good
|
|
sense then. The docs ask that an Eastern tzinfo class consider such a time to
|
|
be EDT (because it's "after 2"), which is a redundant spelling of 1:MM EST
|
|
on the day DST starts. We want to return the 1:MM EST spelling because that's
|
|
the only spelling that makes sense on the local wall clock.
|
|
|
|
In fact, if [5] holds at this point, we do have the standard-time spelling,
|
|
but that takes a bit of proof. We first prove a stronger result. What's the
|
|
difference between the LHS and RHS of [5]? Let
|
|
|
|
diff = x.n - (z.n - z.o) [6]
|
|
|
|
Now
|
|
z.n = by [4]
|
|
(y + y.s).n = by #5
|
|
y.n + y.s = since y.n = x.n
|
|
x.n + y.s = since z and y are have the same tzinfo member,
|
|
y.s = z.s by #2
|
|
x.n + z.s
|
|
|
|
Plugging that back into [6] gives
|
|
|
|
diff =
|
|
x.n - ((x.n + z.s) - z.o) = expanding
|
|
x.n - x.n - z.s + z.o = cancelling
|
|
- z.s + z.o = by #2
|
|
z.d
|
|
|
|
So diff = z.d.
|
|
|
|
If [5] is true now, diff = 0, so z.d = 0 too, and we have the standard-time
|
|
spelling we wanted in the endcase described above. We're done. Contrarily,
|
|
if z.d = 0, then we have a UTC equivalent, and are also done.
|
|
|
|
If [5] is not true now, diff = z.d != 0, and z.d is the offset we need to
|
|
add to z (in effect, z is in tz's standard time, and we need to shift the
|
|
local clock into tz's daylight time).
|
|
|
|
Let
|
|
|
|
z' = z + z.d = z + diff [7]
|
|
|
|
and we can again ask whether
|
|
|
|
z'.n - z'.o = x.n [8]
|
|
|
|
If so, we're done. If not, the tzinfo class is insane, according to the
|
|
assumptions we've made. This also requires a bit of proof. As before, let's
|
|
compute the difference between the LHS and RHS of [8] (and skipping some of
|
|
the justifications for the kinds of substitutions we've done several times
|
|
already):
|
|
|
|
diff' = x.n - (z'.n - z'.o) = replacing z'.n via [7]
|
|
x.n - (z.n + diff - z'.o) = replacing diff via [6]
|
|
x.n - (z.n + x.n - (z.n - z.o) - z'.o) =
|
|
x.n - z.n - x.n + z.n - z.o + z'.o = cancel x.n
|
|
- z.n + z.n - z.o + z'.o = cancel z.n
|
|
- z.o + z'.o = #1 twice
|
|
-z.s - z.d + z'.s + z'.d = z and z' have same tzinfo
|
|
z'.d - z.d
|
|
|
|
So z' is UTC-equivalent to x iff z'.d = z.d at this point. If they are equal,
|
|
we've found the UTC-equivalent so are done. In fact, we stop with [7] and
|
|
return z', not bothering to compute z'.d.
|
|
|
|
How could z.d and z'd differ? z' = z + z.d [7], so merely moving z' by
|
|
a dst() offset, and starting *from* a time already in DST (we know z.d != 0),
|
|
would have to change the result dst() returns: we start in DST, and moving
|
|
a little further into it takes us out of DST.
|
|
|
|
There isn't a sane case where this can happen. The closest it gets is at
|
|
the end of DST, where there's an hour in UTC with no spelling in a hybrid
|
|
tzinfo class. In US Eastern, that's 5:MM UTC = 0:MM EST = 1:MM EDT. During
|
|
that hour, on an Eastern clock 1:MM is taken as being in standard time (6:MM
|
|
UTC) because the docs insist on that, but 0:MM is taken as being in daylight
|
|
time (4:MM UTC). There is no local time mapping to 5:MM UTC. The local
|
|
clock jumps from 1:59 back to 1:00 again, and repeats the 1:MM hour in
|
|
standard time. Since that's what the local clock *does*, we want to map both
|
|
UTC hours 5:MM and 6:MM to 1:MM Eastern. The result is ambiguous
|
|
in local time, but so it goes -- it's the way the local clock works.
|
|
|
|
When x = 5:MM UTC is the input to this algorithm, x.o=0, y.o=-5 and y.d=0,
|
|
so z=0:MM. z.d=60 (minutes) then, so [5] doesn't hold and we keep going.
|
|
z' = z + z.d = 1:MM then, and z'.d=0, and z'.d - z.d = -60 != 0 so [8]
|
|
(correctly) concludes that z' is not UTC-equivalent to x.
|
|
|
|
Because we know z.d said z was in daylight time (else [5] would have held and
|
|
we would have stopped then), and we know z.d != z'.d (else [8] would have held
|
|
and we would have stopped then), and there are only 2 possible values dst() can
|
|
return in Eastern, it follows that z'.d must be 0 (which it is in the example,
|
|
but the reasoning doesn't depend on the example -- it depends on there being
|
|
two possible dst() outcomes, one zero and the other non-zero). Therefore
|
|
z' must be in standard time, and is the spelling we want in this case.
|
|
|
|
Note again that z' is not UTC-equivalent as far as the hybrid tzinfo class is
|
|
concerned (because it takes z' as being in standard time rather than the
|
|
daylight time we intend here), but returning it gives the real-life "local
|
|
clock repeats an hour" behavior when mapping the "unspellable" UTC hour into
|
|
tz.
|
|
|
|
When the input is 6:MM, z=1:MM and z.d=0, and we stop at once, again with
|
|
the 1:MM standard time spelling we want.
|
|
|
|
So how can this break? One of the assumptions must be violated. Two
|
|
possibilities:
|
|
|
|
1) [2] effectively says that y.s is invariant across all y belong to a given
|
|
time zone. This isn't true if, for political reasons or continental drift,
|
|
a region decides to change its base offset from UTC.
|
|
|
|
2) There may be versions of "double daylight" time where the tail end of
|
|
the analysis gives up a step too early. I haven't thought about that
|
|
enough to say.
|
|
|
|
In any case, it's clear that the default fromutc() is strong enough to handle
|
|
"almost all" time zones: so long as the standard offset is invariant, it
|
|
doesn't matter if daylight time transition points change from year to year, or
|
|
if daylight time is skipped in some years; it doesn't matter how large or
|
|
small dst() may get within its bounds; and it doesn't even matter if some
|
|
perverse time zone returns a negative dst()). So a breaking case must be
|
|
pretty bizarre, and a tzinfo subclass can override fromutc() if it is.
|
|
--------------------------------------------------------------------------- */
|