mirror of
https://github.com/python/cpython.git
synced 2024-12-16 13:25:18 +08:00
73e5a5b65d
Merged revisions 46490-46494,46496,46498,46500,46506,46521,46538,46558,46563-46567,46570-46571,46583,46593,46595-46598,46604,46606,46609-46753 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r46610 | martin.v.loewis | 2006-06-03 09:42:26 +0200 (Sat, 03 Jun 2006) | 2 lines Updated version (win32-icons2.zip) from #1490384. ........ r46612 | andrew.kuchling | 2006-06-03 20:09:41 +0200 (Sat, 03 Jun 2006) | 1 line [Bug #1472084] Fix description of do_tag ........ r46614 | andrew.kuchling | 2006-06-03 20:33:35 +0200 (Sat, 03 Jun 2006) | 1 line [Bug #1475554] Strengthen text to say 'must' instead of 'should' ........ r46616 | andrew.kuchling | 2006-06-03 20:41:28 +0200 (Sat, 03 Jun 2006) | 1 line [Bug #1441864] Clarify description of 'data' argument ........ r46617 | andrew.kuchling | 2006-06-03 20:43:24 +0200 (Sat, 03 Jun 2006) | 1 line Minor rewording ........ r46619 | andrew.kuchling | 2006-06-03 21:02:35 +0200 (Sat, 03 Jun 2006) | 9 lines [Bug #1497414] _self is a reserved word in the WATCOM 10.6 C compiler. Fix by renaming the variable. In a different module, Neal fixed it by renaming _self to self. There's already a variable named 'self' here, so I used selfptr. (I'm committing this on a Mac without Tk, but it's a simple search-and-replace. <crosses fingers>, so I'll watch the buildbots and see what happens.) ........ r46621 | fredrik.lundh | 2006-06-03 23:56:05 +0200 (Sat, 03 Jun 2006) | 5 lines "_self" is a said to be a reserved word in Watcom C 10.6. I'm not sure that's really standard compliant behaviour, but I guess we have to fix that anyway... ........ r46622 | andrew.kuchling | 2006-06-04 00:44:42 +0200 (Sun, 04 Jun 2006) | 1 line Update readme ........ r46623 | andrew.kuchling | 2006-06-04 00:59:23 +0200 (Sun, 04 Jun 2006) | 1 line Drop 0 parameter ........ r46624 | andrew.kuchling | 2006-06-04 00:59:59 +0200 (Sun, 04 Jun 2006) | 1 line Some code tidying; use curses.wrapper ........ r46625 | andrew.kuchling | 2006-06-04 01:02:15 +0200 (Sun, 04 Jun 2006) | 1 line Use True; value returned from main is unused ........ r46626 | andrew.kuchling | 2006-06-04 01:07:21 +0200 (Sun, 04 Jun 2006) | 1 line Use true division, and the True value ........ r46627 | andrew.kuchling | 2006-06-04 01:09:58 +0200 (Sun, 04 Jun 2006) | 1 line Docstring fix; use True ........ r46628 | andrew.kuchling | 2006-06-04 01:15:56 +0200 (Sun, 04 Jun 2006) | 1 line Put code in a main() function; loosen up the spacing to match current code style ........ r46629 | andrew.kuchling | 2006-06-04 01:39:07 +0200 (Sun, 04 Jun 2006) | 1 line Use functions; modernize code ........ r46630 | andrew.kuchling | 2006-06-04 01:43:22 +0200 (Sun, 04 Jun 2006) | 1 line This demo requires Medusa (not just asyncore); remove it ........ r46631 | andrew.kuchling | 2006-06-04 01:46:36 +0200 (Sun, 04 Jun 2006) | 2 lines Remove xmlrpc demo -- it duplicates the SimpleXMLRPCServer module. ........ r46632 | andrew.kuchling | 2006-06-04 01:47:22 +0200 (Sun, 04 Jun 2006) | 1 line Remove xmlrpc/ directory ........ r46633 | andrew.kuchling | 2006-06-04 01:51:21 +0200 (Sun, 04 Jun 2006) | 1 line Remove dangling reference ........ r46634 | andrew.kuchling | 2006-06-04 01:59:36 +0200 (Sun, 04 Jun 2006) | 1 line Add more whitespace; use a better socket name ........ r46635 | tim.peters | 2006-06-04 03:22:53 +0200 (Sun, 04 Jun 2006) | 2 lines Whitespace normalization. ........ r46637 | tim.peters | 2006-06-04 05:26:02 +0200 (Sun, 04 Jun 2006) | 16 lines In a PYMALLOC_DEBUG build obmalloc adds extra debugging info to each allocated block. This was using 4 bytes for each such piece of info regardless of platform. This didn't really matter before (proof: no bug reports, and the debug-build obmalloc would have assert-failed if it was ever asked for a chunk of memory >= 2**32 bytes), since container indices were plain ints. But after the Py_ssize_t changes, it's at least theoretically possible to allocate a list or string whose guts exceed 2**32 bytes, and the PYMALLOC_DEBUG routines would fail then (having only 4 bytes to record the originally requested size). Now we use sizeof(size_t) bytes for each of a PYMALLOC_DEBUG build's extra debugging fields. This won't make any difference on 32-bit boxes, but will add 16 bytes to each allocation in a debug build on a 64-bit box. ........ r46638 | tim.peters | 2006-06-04 05:38:04 +0200 (Sun, 04 Jun 2006) | 4 lines _PyObject_DebugMalloc(): The return value should add 2*sizeof(size_t) now, not 8. This probably accounts for current disasters on the 64-bit buildbot slaves. ........ r46639 | neal.norwitz | 2006-06-04 08:19:31 +0200 (Sun, 04 Jun 2006) | 1 line SF #1499797, Fix for memory leak in WindowsError_str ........ r46640 | andrew.macintyre | 2006-06-04 14:31:09 +0200 (Sun, 04 Jun 2006) | 2 lines Patch #1454481: Make thread stack size runtime tunable. ........ r46641 | andrew.macintyre | 2006-06-04 14:59:59 +0200 (Sun, 04 Jun 2006) | 2 lines clean up function declarations to conform to PEP-7 style. ........ r46642 | martin.blais | 2006-06-04 15:49:49 +0200 (Sun, 04 Jun 2006) | 15 lines Fixes in struct and socket from merge reviews. - Following Guido's comments, renamed * pack_to -> pack_into * recv_buf -> recv_into * recvfrom_buf -> recvfrom_into - Made fixes to _struct.c according to Neal Norwitz comments on the checkins list. - Converted some ints into the appropriate -- I hope -- ssize_t and size_t. ........ r46643 | ronald.oussoren | 2006-06-04 16:05:28 +0200 (Sun, 04 Jun 2006) | 3 lines "Import" LDFLAGS in Mac/OSX/Makefile.in to ensure pythonw gets build with the right compiler flags. ........ r46644 | ronald.oussoren | 2006-06-04 16:24:59 +0200 (Sun, 04 Jun 2006) | 2 lines Drop Mac wrappers for the WASTE library. ........ r46645 | tim.peters | 2006-06-04 17:49:07 +0200 (Sun, 04 Jun 2006) | 3 lines s_methods[]: Stop compiler warnings by casting s_unpack_from to PyCFunction. ........ r46646 | george.yoshida | 2006-06-04 19:04:12 +0200 (Sun, 04 Jun 2006) | 2 lines Remove a redundant word ........ r46647 | george.yoshida | 2006-06-04 19:17:25 +0200 (Sun, 04 Jun 2006) | 2 lines Markup fix ........ r46648 | martin.v.loewis | 2006-06-04 21:36:28 +0200 (Sun, 04 Jun 2006) | 2 lines Patch #1359618: Speed-up charmap encoder. ........ r46649 | georg.brandl | 2006-06-04 23:46:16 +0200 (Sun, 04 Jun 2006) | 3 lines Repair refleaks in unicodeobject. ........ r46650 | georg.brandl | 2006-06-04 23:56:52 +0200 (Sun, 04 Jun 2006) | 4 lines Patch #1346214: correctly optimize away "if 0"-style stmts (thanks to Neal for review) ........ r46651 | georg.brandl | 2006-06-05 00:15:37 +0200 (Mon, 05 Jun 2006) | 2 lines Bug #1500293: fix memory leaks in _subprocess module. ........ r46654 | tim.peters | 2006-06-05 01:43:53 +0200 (Mon, 05 Jun 2006) | 2 lines Whitespace normalization. ........ r46655 | tim.peters | 2006-06-05 01:52:47 +0200 (Mon, 05 Jun 2006) | 16 lines Revert revisions: 46640 Patch #1454481: Make thread stack size runtime tunable. 46647 Markup fix The first is causing many buildbots to fail test runs, and there are multiple causes with seemingly no immediate prospects for repairing them. See python-dev discussion. Note that a branch can (and should) be created for resolving these problems, like svn copy svn+ssh://svn.python.org/python/trunk -r46640 svn+ssh://svn.python.org/python/branches/NEW_BRANCH followed by merging rev 46647 to the new branch. ........ r46656 | andrew.kuchling | 2006-06-05 02:08:09 +0200 (Mon, 05 Jun 2006) | 1 line Mention second encoding speedup ........ r46657 | gregory.p.smith | 2006-06-05 02:31:01 +0200 (Mon, 05 Jun 2006) | 7 lines bugfix: when log_archive was called with the DB_ARCH_REMOVE flag present in BerkeleyDB >= 4.2 it tried to construct a list out of an uninitialized char **log_list. feature: export the DB_ARCH_REMOVE flag by name in the module on BerkeleyDB >= 4.2. ........ r46658 | gregory.p.smith | 2006-06-05 02:33:35 +0200 (Mon, 05 Jun 2006) | 5 lines fix a bug in the previous commit. don't leak empty list on error return and fix the additional rare (out of memory only) bug that it was supposed to fix of not freeing log_list when the python allocator failed. ........ r46660 | tim.peters | 2006-06-05 02:55:26 +0200 (Mon, 05 Jun 2006) | 9 lines "Flat is better than nested." Move the long-winded, multiply-nested -R support out of runtest() and into some module-level helper functions. This makes runtest() and the -R code easier to follow. That in turn allowed seeing some opportunities for code simplification, and made it obvious that reglog.txt never got closed. ........ r46661 | hyeshik.chang | 2006-06-05 02:59:54 +0200 (Mon, 05 Jun 2006) | 3 lines Fix a potentially invalid memory access of CJKCodecs' shift-jis decoder. (found by Neal Norwitz) ........ r46663 | gregory.p.smith | 2006-06-05 03:39:52 +0200 (Mon, 05 Jun 2006) | 3 lines * support DBEnv.log_stat() method on BerkeleyDB >= 4.0 [patch #1494885] ........ r46664 | tim.peters | 2006-06-05 03:43:03 +0200 (Mon, 05 Jun 2006) | 3 lines Remove doctest.testmod's deprecated (in 2.4) `isprivate` argument. A lot of hair went into supporting that! ........ r46665 | tim.peters | 2006-06-05 03:47:24 +0200 (Mon, 05 Jun 2006) | 2 lines Whitespace normalization. ........ r46666 | tim.peters | 2006-06-05 03:48:21 +0200 (Mon, 05 Jun 2006) | 2 lines Make doctest news more accurate. ........ r46667 | gregory.p.smith | 2006-06-05 03:56:15 +0200 (Mon, 05 Jun 2006) | 3 lines * support DBEnv.lsn_reset() method on BerkeleyDB >= 4.4 [patch #1494902] ........ r46668 | gregory.p.smith | 2006-06-05 04:02:25 +0200 (Mon, 05 Jun 2006) | 3 lines mention the just committed bsddb changes ........ r46671 | gregory.p.smith | 2006-06-05 19:38:04 +0200 (Mon, 05 Jun 2006) | 3 lines * add support for DBSequence objects [patch #1466734] ........ r46672 | gregory.p.smith | 2006-06-05 20:20:07 +0200 (Mon, 05 Jun 2006) | 3 lines forgot to add this file in previous commit ........ r46673 | tim.peters | 2006-06-05 20:36:12 +0200 (Mon, 05 Jun 2006) | 2 lines Whitespace normalization. ........ r46674 | tim.peters | 2006-06-05 20:36:54 +0200 (Mon, 05 Jun 2006) | 2 lines Add missing svn:eol-style property to text files. ........ r46675 | gregory.p.smith | 2006-06-05 20:48:21 +0200 (Mon, 05 Jun 2006) | 4 lines * fix DBCursor.pget() bug with keyword argument names when no data= is supplied [SF pybsddb bug #1477863] ........ r46676 | andrew.kuchling | 2006-06-05 21:05:32 +0200 (Mon, 05 Jun 2006) | 1 line Remove use of Trove name, which isn't very helpful to users ........ r46677 | andrew.kuchling | 2006-06-05 21:08:25 +0200 (Mon, 05 Jun 2006) | 1 line [Bug #1470026] Include link to list of classifiers ........ r46679 | tim.peters | 2006-06-05 22:48:49 +0200 (Mon, 05 Jun 2006) | 10 lines Access _struct attributes directly instead of mucking with getattr. string_reverse(): Simplify. assertRaises(): Raise TestFailed on failure. test_unpack_from(), test_pack_into(), test_pack_into_fn(): never use `assert` to test for an expected result (it doesn't test anything when Python is run with -O). ........ r46680 | tim.peters | 2006-06-05 22:49:27 +0200 (Mon, 05 Jun 2006) | 2 lines Add missing svn:eol-style property to text files. ........ r46681 | gregory.p.smith | 2006-06-06 01:38:06 +0200 (Tue, 06 Jun 2006) | 3 lines add depends = ['md5.h'] to the _md5 module extension for correctness sake. ........ r46682 | brett.cannon | 2006-06-06 01:51:55 +0200 (Tue, 06 Jun 2006) | 4 lines Add 3 more bytes to a buffer to cover constants in string and null byte on top of 10 possible digits for an int. Closes bug #1501223. ........ r46684 | gregory.p.smith | 2006-06-06 01:59:37 +0200 (Tue, 06 Jun 2006) | 5 lines - bsddb: the __len__ method of a DB object has been fixed to return correct results. It could previously incorrectly return 0 in some cases. Fixes SF bug 1493322 (pybsddb bug 1184012). ........ r46686 | tim.peters | 2006-06-06 02:25:07 +0200 (Tue, 06 Jun 2006) | 7 lines _PySys_Init(): It's rarely a good idea to size a buffer to the exact maximum size someone guesses is needed. In this case, if we're really worried about extreme integers, then "cp%d" can actually need 14 bytes (2 for "cp" + 1 for \0 at the end + 11 for -(2**31-1)). So reserve 128 bytes instead -- nothing is actually saved by making a stack-local buffer tiny. ........ r46687 | neal.norwitz | 2006-06-06 09:22:08 +0200 (Tue, 06 Jun 2006) | 1 line Remove unused variable (and stop compiler warning) ........ r46688 | neal.norwitz | 2006-06-06 09:23:01 +0200 (Tue, 06 Jun 2006) | 1 line Fix a bunch of parameter strings ........ r46689 | thomas.heller | 2006-06-06 13:34:33 +0200 (Tue, 06 Jun 2006) | 6 lines Convert CFieldObject tp_members to tp_getset, since there is no structmember typecode for Py_ssize_t fields. This should fix some of the errors on the PPC64 debian machine (64-bit, big endian). Assigning to readonly fields now raises AttributeError instead of TypeError, so the testcase has to be changed as well. ........ r46690 | thomas.heller | 2006-06-06 13:54:32 +0200 (Tue, 06 Jun 2006) | 1 line Damn - the sentinel was missing. And fix another silly mistake. ........ r46691 | martin.blais | 2006-06-06 14:46:55 +0200 (Tue, 06 Jun 2006) | 13 lines Normalized a few cases of whitespace in function declarations. Found them using:: find . -name '*.py' | while read i ; do grep 'def[^(]*( ' $i /dev/null ; done find . -name '*.py' | while read i ; do grep ' ):' $i /dev/null ; done (I was doing this all over my own code anyway, because I'd been using spaces in all defs, so I thought I'd make a run on the Python code as well. If you need to do such fixes in your own code, you can use xx-rename or parenregu.el within emacs.) ........ r46693 | thomas.heller | 2006-06-06 17:34:18 +0200 (Tue, 06 Jun 2006) | 1 line Specify argtypes for all test functions. Maybe that helps on strange ;-) architectures ........ r46694 | tim.peters | 2006-06-06 17:50:17 +0200 (Tue, 06 Jun 2006) | 5 lines BSequence_set_range(): Rev 46688 ("Fix a bunch of parameter strings") changed this function's signature seemingly by mistake, which is causing buildbots to fail test_bsddb3. Restored the pre-46688 signature. ........ r46695 | tim.peters | 2006-06-06 17:52:35 +0200 (Tue, 06 Jun 2006) | 4 lines On python-dev Thomas Heller said these were committed by mistake in rev 46693, so reverting this part of rev 46693. ........ r46696 | andrew.kuchling | 2006-06-06 19:10:41 +0200 (Tue, 06 Jun 2006) | 1 line Fix comment typo ........ r46697 | brett.cannon | 2006-06-06 20:08:16 +0200 (Tue, 06 Jun 2006) | 2 lines Fix coding style guide bug. ........ r46698 | thomas.heller | 2006-06-06 20:50:46 +0200 (Tue, 06 Jun 2006) | 2 lines Add a hack so that foreign functions returning float now do work on 64-bit big endian platforms. ........ r46699 | thomas.heller | 2006-06-06 21:25:13 +0200 (Tue, 06 Jun 2006) | 3 lines Use the same big-endian hack as in _ctypes/callproc.c for callback functions. This fixes the callback function tests that return float. ........ r46700 | ronald.oussoren | 2006-06-06 21:50:24 +0200 (Tue, 06 Jun 2006) | 5 lines * Ensure that "make altinstall" works when the tree was configured with --enable-framework * Also for --enable-framework: allow users to use --prefix to specify the location of the compatibility symlinks (such as /usr/local/bin/python) ........ r46701 | ronald.oussoren | 2006-06-06 21:56:00 +0200 (Tue, 06 Jun 2006) | 3 lines A quick hack to ensure the right key-bindings for IDLE on osx: install patched configuration files during a framework install. ........ r46702 | tim.peters | 2006-06-07 03:04:59 +0200 (Wed, 07 Jun 2006) | 4 lines dash_R_cleanup(): Clear filecmp._cache. This accounts for different results across -R runs (at least on Windows) of test_filecmp. ........ r46705 | tim.peters | 2006-06-07 08:57:51 +0200 (Wed, 07 Jun 2006) | 17 lines SF patch 1501987: Remove randomness from test_exceptions, from ?iga Seilnacht (sorry about the name, but Firefox on my box can't display the first character of the name -- the SF "Unix name" is zseil). This appears to cure the oddball intermittent leaks across runs when running test_exceptions under -R. I'm not sure why, but I'm too sleepy to care ;-) The thrust of the SF patch was to remove randomness in the pickle protocol used. I changed the patch to use range(pickle.HIGHEST_PROTOCOL + 1), to try both pickle and cPickle, and randomly mucked with other test lines to put statements on their own lines. Not a bugfix candidate (this is fiddling new-in-2.5 code). ........ r46706 | andrew.kuchling | 2006-06-07 15:55:33 +0200 (Wed, 07 Jun 2006) | 1 line Add an SQLite introduction, taken from the 'What's New' text ........ r46708 | andrew.kuchling | 2006-06-07 19:02:52 +0200 (Wed, 07 Jun 2006) | 1 line Mention other placeholders ........ r46709 | andrew.kuchling | 2006-06-07 19:03:46 +0200 (Wed, 07 Jun 2006) | 1 line Add an item; also, escape % ........ r46710 | andrew.kuchling | 2006-06-07 19:04:01 +0200 (Wed, 07 Jun 2006) | 1 line Mention other placeholders ........ r46716 | ronald.oussoren | 2006-06-07 20:57:44 +0200 (Wed, 07 Jun 2006) | 2 lines Move Mac/OSX/Tools one level up ........ r46717 | ronald.oussoren | 2006-06-07 20:58:01 +0200 (Wed, 07 Jun 2006) | 2 lines Move Mac/OSX/PythonLauncher one level up ........ r46718 | ronald.oussoren | 2006-06-07 20:58:42 +0200 (Wed, 07 Jun 2006) | 2 lines mv Mac/OSX/BuildScript one level up ........ r46719 | ronald.oussoren | 2006-06-07 21:02:03 +0200 (Wed, 07 Jun 2006) | 2 lines Move Mac/OSX/* one level up ........ r46720 | ronald.oussoren | 2006-06-07 21:06:01 +0200 (Wed, 07 Jun 2006) | 2 lines And the last bit: move IDLE one level up and adjust makefiles ........ r46723 | ronald.oussoren | 2006-06-07 21:38:53 +0200 (Wed, 07 Jun 2006) | 4 lines - Patch the correct version of python in the Info.plists at build time, instead of relying on a maintainer to update them before releases. - Remove the now empty Mac/OSX directory ........ r46727 | ronald.oussoren | 2006-06-07 22:18:44 +0200 (Wed, 07 Jun 2006) | 7 lines * If BuildApplet.py is used as an applet it starts with a version of sys.exutable that isn't usuable on an #!-line. That results in generated applets that don't actually work. Work around this problem by resetting sys.executable. * argvemulator.py didn't work on intel macs. This patch fixes this (bug #1491468) ........ r46728 | tim.peters | 2006-06-07 22:40:06 +0200 (Wed, 07 Jun 2006) | 2 lines Whitespace normalization. ........ r46729 | tim.peters | 2006-06-07 22:40:54 +0200 (Wed, 07 Jun 2006) | 2 lines Add missing svn:eol-style property to text files. ........ r46730 | thomas.heller | 2006-06-07 22:43:06 +0200 (Wed, 07 Jun 2006) | 7 lines Fix for foreign functions returning small structures on 64-bit big endian machines. Should fix the remaininf failure in the PPC64 Debian buildbot. Thanks to Matthias Klose for providing access to a machine to debug and test this. ........ r46731 | brett.cannon | 2006-06-07 23:48:17 +0200 (Wed, 07 Jun 2006) | 2 lines Clarify documentation for bf_getcharbuffer. ........ r46735 | neal.norwitz | 2006-06-08 07:12:45 +0200 (Thu, 08 Jun 2006) | 1 line Fix a refleak in recvfrom_into ........ r46736 | gregory.p.smith | 2006-06-08 07:17:08 +0200 (Thu, 08 Jun 2006) | 9 lines - bsddb: the bsddb.dbtables Modify method now raises the proper error and aborts the db transaction safely when a modifier callback fails. Fixes SF python patch/bug #1408584. Also cleans up the bsddb.dbtables docstrings since thats the only documentation that exists for that unadvertised module. (people really should really just use sqlite3) ........ r46737 | gregory.p.smith | 2006-06-08 07:38:11 +0200 (Thu, 08 Jun 2006) | 4 lines * Turn the deadlock situation described in SF bug #775414 into a DBDeadLockError exception. * add the test case for my previous dbtables commit. ........ r46738 | gregory.p.smith | 2006-06-08 07:39:54 +0200 (Thu, 08 Jun 2006) | 2 lines pasted set_lk_detect line in wrong spot in previous commit. fixed. passes tests this time. ........ r46739 | armin.rigo | 2006-06-08 12:56:24 +0200 (Thu, 08 Jun 2006) | 6 lines (arre, arigo) SF bug #1350060 Give a consistent behavior for comparison and hashing of method objects (both user- and built-in methods). Now compares the 'self' recursively. The hash was already asking for the hash of 'self'. ........ r46740 | andrew.kuchling | 2006-06-08 13:56:44 +0200 (Thu, 08 Jun 2006) | 1 line Typo fix ........ r46741 | georg.brandl | 2006-06-08 14:45:01 +0200 (Thu, 08 Jun 2006) | 2 lines Bug #1502750: Fix getargs "i" format to use LONG_MIN and LONG_MAX for bounds checking. ........ r46743 | georg.brandl | 2006-06-08 14:54:13 +0200 (Thu, 08 Jun 2006) | 2 lines Bug #1502728: Correctly link against librt library on HP-UX. ........ r46745 | georg.brandl | 2006-06-08 14:55:47 +0200 (Thu, 08 Jun 2006) | 3 lines Add news for recent bugfix. ........ r46746 | georg.brandl | 2006-06-08 15:31:07 +0200 (Thu, 08 Jun 2006) | 4 lines Argh. "integer" is a very confusing word ;) Actually, checking for INT_MAX and INT_MIN is correct since the format code explicitly handles a C "int". ........ r46748 | nick.coghlan | 2006-06-08 15:54:49 +0200 (Thu, 08 Jun 2006) | 1 line Add functools.update_wrapper() and functools.wraps() as described in PEP 356 ........ r46751 | georg.brandl | 2006-06-08 16:50:21 +0200 (Thu, 08 Jun 2006) | 4 lines Bug #1502805: don't alias file.__exit__ to file.close since the latter can return something that's true. ........ r46752 | georg.brandl | 2006-06-08 16:50:53 +0200 (Thu, 08 Jun 2006) | 3 lines Convert test_file to unittest. ........
892 lines
29 KiB
Python
Executable File
892 lines
29 KiB
Python
Executable File
#! /usr/bin/env python
|
|
|
|
# Original code by Guido van Rossum; extensive changes by Sam Bayer,
|
|
# including code to check URL fragments.
|
|
|
|
"""Web tree checker.
|
|
|
|
This utility is handy to check a subweb of the world-wide web for
|
|
errors. A subweb is specified by giving one or more ``root URLs''; a
|
|
page belongs to the subweb if one of the root URLs is an initial
|
|
prefix of it.
|
|
|
|
File URL extension:
|
|
|
|
In order to easy the checking of subwebs via the local file system,
|
|
the interpretation of ``file:'' URLs is extended to mimic the behavior
|
|
of your average HTTP daemon: if a directory pathname is given, the
|
|
file index.html in that directory is returned if it exists, otherwise
|
|
a directory listing is returned. Now, you can point webchecker to the
|
|
document tree in the local file system of your HTTP daemon, and have
|
|
most of it checked. In fact the default works this way if your local
|
|
web tree is located at /usr/local/etc/httpd/htdpcs (the default for
|
|
the NCSA HTTP daemon and probably others).
|
|
|
|
Report printed:
|
|
|
|
When done, it reports pages with bad links within the subweb. When
|
|
interrupted, it reports for the pages that it has checked already.
|
|
|
|
In verbose mode, additional messages are printed during the
|
|
information gathering phase. By default, it prints a summary of its
|
|
work status every 50 URLs (adjustable with the -r option), and it
|
|
reports errors as they are encountered. Use the -q option to disable
|
|
this output.
|
|
|
|
Checkpoint feature:
|
|
|
|
Whether interrupted or not, it dumps its state (a Python pickle) to a
|
|
checkpoint file and the -R option allows it to restart from the
|
|
checkpoint (assuming that the pages on the subweb that were already
|
|
processed haven't changed). Even when it has run till completion, -R
|
|
can still be useful -- it will print the reports again, and -Rq prints
|
|
the errors only. In this case, the checkpoint file is not written
|
|
again. The checkpoint file can be set with the -d option.
|
|
|
|
The checkpoint file is written as a Python pickle. Remember that
|
|
Python's pickle module is currently quite slow. Give it the time it
|
|
needs to load and save the checkpoint file. When interrupted while
|
|
writing the checkpoint file, the old checkpoint file is not
|
|
overwritten, but all work done in the current run is lost.
|
|
|
|
Miscellaneous:
|
|
|
|
- You may find the (Tk-based) GUI version easier to use. See wcgui.py.
|
|
|
|
- Webchecker honors the "robots.txt" convention. Thanks to Skip
|
|
Montanaro for his robotparser.py module (included in this directory)!
|
|
The agent name is hardwired to "webchecker". URLs that are disallowed
|
|
by the robots.txt file are reported as external URLs.
|
|
|
|
- Because the SGML parser is a bit slow, very large SGML files are
|
|
skipped. The size limit can be set with the -m option.
|
|
|
|
- When the server or protocol does not tell us a file's type, we guess
|
|
it based on the URL's suffix. The mimetypes.py module (also in this
|
|
directory) has a built-in table mapping most currently known suffixes,
|
|
and in addition attempts to read the mime.types configuration files in
|
|
the default locations of Netscape and the NCSA HTTP daemon.
|
|
|
|
- We follow links indicated by <A>, <FRAME> and <IMG> tags. We also
|
|
honor the <BASE> tag.
|
|
|
|
- We now check internal NAME anchor links, as well as toplevel links.
|
|
|
|
- Checking external links is now done by default; use -x to *disable*
|
|
this feature. External links are now checked during normal
|
|
processing. (XXX The status of a checked link could be categorized
|
|
better. Later...)
|
|
|
|
- If external links are not checked, you can use the -t flag to
|
|
provide specific overrides to -x.
|
|
|
|
Usage: webchecker.py [option] ... [rooturl] ...
|
|
|
|
Options:
|
|
|
|
-R -- restart from checkpoint file
|
|
-d file -- checkpoint filename (default %(DUMPFILE)s)
|
|
-m bytes -- skip HTML pages larger than this size (default %(MAXPAGE)d)
|
|
-n -- reports only, no checking (use with -R)
|
|
-q -- quiet operation (also suppresses external links report)
|
|
-r number -- number of links processed per round (default %(ROUNDSIZE)d)
|
|
-t root -- specify root dir which should be treated as internal (can repeat)
|
|
-v -- verbose operation; repeating -v will increase verbosity
|
|
-x -- don't check external links (these are often slow to check)
|
|
-a -- don't check name anchors
|
|
|
|
Arguments:
|
|
|
|
rooturl -- URL to start checking
|
|
(default %(DEFROOT)s)
|
|
|
|
"""
|
|
|
|
|
|
__version__ = "$Revision$"
|
|
|
|
|
|
import sys
|
|
import os
|
|
from types import *
|
|
import StringIO
|
|
import getopt
|
|
import pickle
|
|
|
|
import urllib
|
|
import urlparse
|
|
import sgmllib
|
|
import cgi
|
|
|
|
import mimetypes
|
|
import robotparser
|
|
|
|
# Extract real version number if necessary
|
|
if __version__[0] == '$':
|
|
_v = __version__.split()
|
|
if len(_v) == 3:
|
|
__version__ = _v[1]
|
|
|
|
|
|
# Tunable parameters
|
|
DEFROOT = "file:/usr/local/etc/httpd/htdocs/" # Default root URL
|
|
CHECKEXT = 1 # Check external references (1 deep)
|
|
VERBOSE = 1 # Verbosity level (0-3)
|
|
MAXPAGE = 150000 # Ignore files bigger than this
|
|
ROUNDSIZE = 50 # Number of links processed per round
|
|
DUMPFILE = "@webchecker.pickle" # Pickled checkpoint
|
|
AGENTNAME = "webchecker" # Agent name for robots.txt parser
|
|
NONAMES = 0 # Force name anchor checking
|
|
|
|
|
|
# Global variables
|
|
|
|
|
|
def main():
|
|
checkext = CHECKEXT
|
|
verbose = VERBOSE
|
|
maxpage = MAXPAGE
|
|
roundsize = ROUNDSIZE
|
|
dumpfile = DUMPFILE
|
|
restart = 0
|
|
norun = 0
|
|
|
|
try:
|
|
opts, args = getopt.getopt(sys.argv[1:], 'Rd:m:nqr:t:vxa')
|
|
except getopt.error, msg:
|
|
sys.stdout = sys.stderr
|
|
print msg
|
|
print __doc__%globals()
|
|
sys.exit(2)
|
|
|
|
# The extra_roots variable collects extra roots.
|
|
extra_roots = []
|
|
nonames = NONAMES
|
|
|
|
for o, a in opts:
|
|
if o == '-R':
|
|
restart = 1
|
|
if o == '-d':
|
|
dumpfile = a
|
|
if o == '-m':
|
|
maxpage = int(a)
|
|
if o == '-n':
|
|
norun = 1
|
|
if o == '-q':
|
|
verbose = 0
|
|
if o == '-r':
|
|
roundsize = int(a)
|
|
if o == '-t':
|
|
extra_roots.append(a)
|
|
if o == '-a':
|
|
nonames = not nonames
|
|
if o == '-v':
|
|
verbose = verbose + 1
|
|
if o == '-x':
|
|
checkext = not checkext
|
|
|
|
if verbose > 0:
|
|
print AGENTNAME, "version", __version__
|
|
|
|
if restart:
|
|
c = load_pickle(dumpfile=dumpfile, verbose=verbose)
|
|
else:
|
|
c = Checker()
|
|
|
|
c.setflags(checkext=checkext, verbose=verbose,
|
|
maxpage=maxpage, roundsize=roundsize,
|
|
nonames=nonames
|
|
)
|
|
|
|
if not restart and not args:
|
|
args.append(DEFROOT)
|
|
|
|
for arg in args:
|
|
c.addroot(arg)
|
|
|
|
# The -t flag is only needed if external links are not to be
|
|
# checked. So -t values are ignored unless -x was specified.
|
|
if not checkext:
|
|
for root in extra_roots:
|
|
# Make sure it's terminated by a slash,
|
|
# so that addroot doesn't discard the last
|
|
# directory component.
|
|
if root[-1] != "/":
|
|
root = root + "/"
|
|
c.addroot(root, add_to_do = 0)
|
|
|
|
try:
|
|
|
|
if not norun:
|
|
try:
|
|
c.run()
|
|
except KeyboardInterrupt:
|
|
if verbose > 0:
|
|
print "[run interrupted]"
|
|
|
|
try:
|
|
c.report()
|
|
except KeyboardInterrupt:
|
|
if verbose > 0:
|
|
print "[report interrupted]"
|
|
|
|
finally:
|
|
if c.save_pickle(dumpfile):
|
|
if dumpfile == DUMPFILE:
|
|
print "Use ``%s -R'' to restart." % sys.argv[0]
|
|
else:
|
|
print "Use ``%s -R -d %s'' to restart." % (sys.argv[0],
|
|
dumpfile)
|
|
|
|
|
|
def load_pickle(dumpfile=DUMPFILE, verbose=VERBOSE):
|
|
if verbose > 0:
|
|
print "Loading checkpoint from %s ..." % dumpfile
|
|
f = open(dumpfile, "rb")
|
|
c = pickle.load(f)
|
|
f.close()
|
|
if verbose > 0:
|
|
print "Done."
|
|
print "Root:", "\n ".join(c.roots)
|
|
return c
|
|
|
|
|
|
class Checker:
|
|
|
|
checkext = CHECKEXT
|
|
verbose = VERBOSE
|
|
maxpage = MAXPAGE
|
|
roundsize = ROUNDSIZE
|
|
nonames = NONAMES
|
|
|
|
validflags = tuple(dir())
|
|
|
|
def __init__(self):
|
|
self.reset()
|
|
|
|
def setflags(self, **kw):
|
|
for key in kw.keys():
|
|
if key not in self.validflags:
|
|
raise NameError, "invalid keyword argument: %s" % str(key)
|
|
for key, value in kw.items():
|
|
setattr(self, key, value)
|
|
|
|
def reset(self):
|
|
self.roots = []
|
|
self.todo = {}
|
|
self.done = {}
|
|
self.bad = {}
|
|
|
|
# Add a name table, so that the name URLs can be checked. Also
|
|
# serves as an implicit cache for which URLs are done.
|
|
self.name_table = {}
|
|
|
|
self.round = 0
|
|
# The following are not pickled:
|
|
self.robots = {}
|
|
self.errors = {}
|
|
self.urlopener = MyURLopener()
|
|
self.changed = 0
|
|
|
|
def note(self, level, format, *args):
|
|
if self.verbose > level:
|
|
if args:
|
|
format = format%args
|
|
self.message(format)
|
|
|
|
def message(self, format, *args):
|
|
if args:
|
|
format = format%args
|
|
print format
|
|
|
|
def __getstate__(self):
|
|
return (self.roots, self.todo, self.done, self.bad, self.round)
|
|
|
|
def __setstate__(self, state):
|
|
self.reset()
|
|
(self.roots, self.todo, self.done, self.bad, self.round) = state
|
|
for root in self.roots:
|
|
self.addrobot(root)
|
|
for url in self.bad.keys():
|
|
self.markerror(url)
|
|
|
|
def addroot(self, root, add_to_do = 1):
|
|
if root not in self.roots:
|
|
troot = root
|
|
scheme, netloc, path, params, query, fragment = \
|
|
urlparse.urlparse(root)
|
|
i = path.rfind("/") + 1
|
|
if 0 < i < len(path):
|
|
path = path[:i]
|
|
troot = urlparse.urlunparse((scheme, netloc, path,
|
|
params, query, fragment))
|
|
self.roots.append(troot)
|
|
self.addrobot(root)
|
|
if add_to_do:
|
|
self.newlink((root, ""), ("<root>", root))
|
|
|
|
def addrobot(self, root):
|
|
root = urlparse.urljoin(root, "/")
|
|
if self.robots.has_key(root): return
|
|
url = urlparse.urljoin(root, "/robots.txt")
|
|
self.robots[root] = rp = robotparser.RobotFileParser()
|
|
self.note(2, "Parsing %s", url)
|
|
rp.debug = self.verbose > 3
|
|
rp.set_url(url)
|
|
try:
|
|
rp.read()
|
|
except (OSError, IOError), msg:
|
|
self.note(1, "I/O error parsing %s: %s", url, msg)
|
|
|
|
def run(self):
|
|
while self.todo:
|
|
self.round = self.round + 1
|
|
self.note(0, "\nRound %d (%s)\n", self.round, self.status())
|
|
urls = self.todo.keys()
|
|
urls.sort()
|
|
del urls[self.roundsize:]
|
|
for url in urls:
|
|
self.dopage(url)
|
|
|
|
def status(self):
|
|
return "%d total, %d to do, %d done, %d bad" % (
|
|
len(self.todo)+len(self.done),
|
|
len(self.todo), len(self.done),
|
|
len(self.bad))
|
|
|
|
def report(self):
|
|
self.message("")
|
|
if not self.todo: s = "Final"
|
|
else: s = "Interim"
|
|
self.message("%s Report (%s)", s, self.status())
|
|
self.report_errors()
|
|
|
|
def report_errors(self):
|
|
if not self.bad:
|
|
self.message("\nNo errors")
|
|
return
|
|
self.message("\nError Report:")
|
|
sources = self.errors.keys()
|
|
sources.sort()
|
|
for source in sources:
|
|
triples = self.errors[source]
|
|
self.message("")
|
|
if len(triples) > 1:
|
|
self.message("%d Errors in %s", len(triples), source)
|
|
else:
|
|
self.message("Error in %s", source)
|
|
# Call self.format_url() instead of referring
|
|
# to the URL directly, since the URLs in these
|
|
# triples is now a (URL, fragment) pair. The value
|
|
# of the "source" variable comes from the list of
|
|
# origins, and is a URL, not a pair.
|
|
for url, rawlink, msg in triples:
|
|
if rawlink != self.format_url(url): s = " (%s)" % rawlink
|
|
else: s = ""
|
|
self.message(" HREF %s%s\n msg %s",
|
|
self.format_url(url), s, msg)
|
|
|
|
def dopage(self, url_pair):
|
|
|
|
# All printing of URLs uses format_url(); argument changed to
|
|
# url_pair for clarity.
|
|
if self.verbose > 1:
|
|
if self.verbose > 2:
|
|
self.show("Check ", self.format_url(url_pair),
|
|
" from", self.todo[url_pair])
|
|
else:
|
|
self.message("Check %s", self.format_url(url_pair))
|
|
url, local_fragment = url_pair
|
|
if local_fragment and self.nonames:
|
|
self.markdone(url_pair)
|
|
return
|
|
try:
|
|
page = self.getpage(url_pair)
|
|
except sgmllib.SGMLParseError, msg:
|
|
msg = self.sanitize(msg)
|
|
self.note(0, "Error parsing %s: %s",
|
|
self.format_url(url_pair), msg)
|
|
# Dont actually mark the URL as bad - it exists, just
|
|
# we can't parse it!
|
|
page = None
|
|
if page:
|
|
# Store the page which corresponds to this URL.
|
|
self.name_table[url] = page
|
|
# If there is a fragment in this url_pair, and it's not
|
|
# in the list of names for the page, call setbad(), since
|
|
# it's a missing anchor.
|
|
if local_fragment and local_fragment not in page.getnames():
|
|
self.setbad(url_pair, ("Missing name anchor `%s'" % local_fragment))
|
|
for info in page.getlinkinfos():
|
|
# getlinkinfos() now returns the fragment as well,
|
|
# and we store that fragment here in the "todo" dictionary.
|
|
link, rawlink, fragment = info
|
|
# However, we don't want the fragment as the origin, since
|
|
# the origin is logically a page.
|
|
origin = url, rawlink
|
|
self.newlink((link, fragment), origin)
|
|
else:
|
|
# If no page has been created yet, we want to
|
|
# record that fact.
|
|
self.name_table[url_pair[0]] = None
|
|
self.markdone(url_pair)
|
|
|
|
def newlink(self, url, origin):
|
|
if self.done.has_key(url):
|
|
self.newdonelink(url, origin)
|
|
else:
|
|
self.newtodolink(url, origin)
|
|
|
|
def newdonelink(self, url, origin):
|
|
if origin not in self.done[url]:
|
|
self.done[url].append(origin)
|
|
|
|
# Call self.format_url(), since the URL here
|
|
# is now a (URL, fragment) pair.
|
|
self.note(3, " Done link %s", self.format_url(url))
|
|
|
|
# Make sure that if it's bad, that the origin gets added.
|
|
if self.bad.has_key(url):
|
|
source, rawlink = origin
|
|
triple = url, rawlink, self.bad[url]
|
|
self.seterror(source, triple)
|
|
|
|
def newtodolink(self, url, origin):
|
|
# Call self.format_url(), since the URL here
|
|
# is now a (URL, fragment) pair.
|
|
if self.todo.has_key(url):
|
|
if origin not in self.todo[url]:
|
|
self.todo[url].append(origin)
|
|
self.note(3, " Seen todo link %s", self.format_url(url))
|
|
else:
|
|
self.todo[url] = [origin]
|
|
self.note(3, " New todo link %s", self.format_url(url))
|
|
|
|
def format_url(self, url):
|
|
link, fragment = url
|
|
if fragment: return link + "#" + fragment
|
|
else: return link
|
|
|
|
def markdone(self, url):
|
|
self.done[url] = self.todo[url]
|
|
del self.todo[url]
|
|
self.changed = 1
|
|
|
|
def inroots(self, url):
|
|
for root in self.roots:
|
|
if url[:len(root)] == root:
|
|
return self.isallowed(root, url)
|
|
return 0
|
|
|
|
def isallowed(self, root, url):
|
|
root = urlparse.urljoin(root, "/")
|
|
return self.robots[root].can_fetch(AGENTNAME, url)
|
|
|
|
def getpage(self, url_pair):
|
|
# Incoming argument name is a (URL, fragment) pair.
|
|
# The page may have been cached in the name_table variable.
|
|
url, fragment = url_pair
|
|
if self.name_table.has_key(url):
|
|
return self.name_table[url]
|
|
|
|
scheme, path = urllib.splittype(url)
|
|
if scheme in ('mailto', 'news', 'javascript', 'telnet'):
|
|
self.note(1, " Not checking %s URL" % scheme)
|
|
return None
|
|
isint = self.inroots(url)
|
|
|
|
# Ensure that openpage gets the URL pair to
|
|
# print out its error message and record the error pair
|
|
# correctly.
|
|
if not isint:
|
|
if not self.checkext:
|
|
self.note(1, " Not checking ext link")
|
|
return None
|
|
f = self.openpage(url_pair)
|
|
if f:
|
|
self.safeclose(f)
|
|
return None
|
|
text, nurl = self.readhtml(url_pair)
|
|
|
|
if nurl != url:
|
|
self.note(1, " Redirected to %s", nurl)
|
|
url = nurl
|
|
if text:
|
|
return Page(text, url, maxpage=self.maxpage, checker=self)
|
|
|
|
# These next three functions take (URL, fragment) pairs as
|
|
# arguments, so that openpage() receives the appropriate tuple to
|
|
# record error messages.
|
|
def readhtml(self, url_pair):
|
|
url, fragment = url_pair
|
|
text = None
|
|
f, url = self.openhtml(url_pair)
|
|
if f:
|
|
text = f.read()
|
|
f.close()
|
|
return text, url
|
|
|
|
def openhtml(self, url_pair):
|
|
url, fragment = url_pair
|
|
f = self.openpage(url_pair)
|
|
if f:
|
|
url = f.geturl()
|
|
info = f.info()
|
|
if not self.checkforhtml(info, url):
|
|
self.safeclose(f)
|
|
f = None
|
|
return f, url
|
|
|
|
def openpage(self, url_pair):
|
|
url, fragment = url_pair
|
|
try:
|
|
return self.urlopener.open(url)
|
|
except (OSError, IOError), msg:
|
|
msg = self.sanitize(msg)
|
|
self.note(0, "Error %s", msg)
|
|
if self.verbose > 0:
|
|
self.show(" HREF ", url, " from", self.todo[url_pair])
|
|
self.setbad(url_pair, msg)
|
|
return None
|
|
|
|
def checkforhtml(self, info, url):
|
|
if info.has_key('content-type'):
|
|
ctype = cgi.parse_header(info['content-type'])[0].lower()
|
|
if ';' in ctype:
|
|
# handle content-type: text/html; charset=iso8859-1 :
|
|
ctype = ctype.split(';', 1)[0].strip()
|
|
else:
|
|
if url[-1:] == "/":
|
|
return 1
|
|
ctype, encoding = mimetypes.guess_type(url)
|
|
if ctype == 'text/html':
|
|
return 1
|
|
else:
|
|
self.note(1, " Not HTML, mime type %s", ctype)
|
|
return 0
|
|
|
|
def setgood(self, url):
|
|
if self.bad.has_key(url):
|
|
del self.bad[url]
|
|
self.changed = 1
|
|
self.note(0, "(Clear previously seen error)")
|
|
|
|
def setbad(self, url, msg):
|
|
if self.bad.has_key(url) and self.bad[url] == msg:
|
|
self.note(0, "(Seen this error before)")
|
|
return
|
|
self.bad[url] = msg
|
|
self.changed = 1
|
|
self.markerror(url)
|
|
|
|
def markerror(self, url):
|
|
try:
|
|
origins = self.todo[url]
|
|
except KeyError:
|
|
origins = self.done[url]
|
|
for source, rawlink in origins:
|
|
triple = url, rawlink, self.bad[url]
|
|
self.seterror(source, triple)
|
|
|
|
def seterror(self, url, triple):
|
|
try:
|
|
# Because of the way the URLs are now processed, I need to
|
|
# check to make sure the URL hasn't been entered in the
|
|
# error list. The first element of the triple here is a
|
|
# (URL, fragment) pair, but the URL key is not, since it's
|
|
# from the list of origins.
|
|
if triple not in self.errors[url]:
|
|
self.errors[url].append(triple)
|
|
except KeyError:
|
|
self.errors[url] = [triple]
|
|
|
|
# The following used to be toplevel functions; they have been
|
|
# changed into methods so they can be overridden in subclasses.
|
|
|
|
def show(self, p1, link, p2, origins):
|
|
self.message("%s %s", p1, link)
|
|
i = 0
|
|
for source, rawlink in origins:
|
|
i = i+1
|
|
if i == 2:
|
|
p2 = ' '*len(p2)
|
|
if rawlink != link: s = " (%s)" % rawlink
|
|
else: s = ""
|
|
self.message("%s %s%s", p2, source, s)
|
|
|
|
def sanitize(self, msg):
|
|
if isinstance(IOError, ClassType) and isinstance(msg, IOError):
|
|
# Do the other branch recursively
|
|
msg.args = self.sanitize(msg.args)
|
|
elif isinstance(msg, TupleType):
|
|
if len(msg) >= 4 and msg[0] == 'http error' and \
|
|
isinstance(msg[3], InstanceType):
|
|
# Remove the Message instance -- it may contain
|
|
# a file object which prevents pickling.
|
|
msg = msg[:3] + msg[4:]
|
|
return msg
|
|
|
|
def safeclose(self, f):
|
|
try:
|
|
url = f.geturl()
|
|
except AttributeError:
|
|
pass
|
|
else:
|
|
if url[:4] == 'ftp:' or url[:7] == 'file://':
|
|
# Apparently ftp connections don't like to be closed
|
|
# prematurely...
|
|
text = f.read()
|
|
f.close()
|
|
|
|
def save_pickle(self, dumpfile=DUMPFILE):
|
|
if not self.changed:
|
|
self.note(0, "\nNo need to save checkpoint")
|
|
elif not dumpfile:
|
|
self.note(0, "No dumpfile, won't save checkpoint")
|
|
else:
|
|
self.note(0, "\nSaving checkpoint to %s ...", dumpfile)
|
|
newfile = dumpfile + ".new"
|
|
f = open(newfile, "wb")
|
|
pickle.dump(self, f)
|
|
f.close()
|
|
try:
|
|
os.unlink(dumpfile)
|
|
except os.error:
|
|
pass
|
|
os.rename(newfile, dumpfile)
|
|
self.note(0, "Done.")
|
|
return 1
|
|
|
|
|
|
class Page:
|
|
|
|
def __init__(self, text, url, verbose=VERBOSE, maxpage=MAXPAGE, checker=None):
|
|
self.text = text
|
|
self.url = url
|
|
self.verbose = verbose
|
|
self.maxpage = maxpage
|
|
self.checker = checker
|
|
|
|
# The parsing of the page is done in the __init__() routine in
|
|
# order to initialize the list of names the file
|
|
# contains. Stored the parser in an instance variable. Passed
|
|
# the URL to MyHTMLParser().
|
|
size = len(self.text)
|
|
if size > self.maxpage:
|
|
self.note(0, "Skip huge file %s (%.0f Kbytes)", self.url, (size*0.001))
|
|
self.parser = None
|
|
return
|
|
self.checker.note(2, " Parsing %s (%d bytes)", self.url, size)
|
|
self.parser = MyHTMLParser(url, verbose=self.verbose,
|
|
checker=self.checker)
|
|
self.parser.feed(self.text)
|
|
self.parser.close()
|
|
|
|
def note(self, level, msg, *args):
|
|
if self.checker:
|
|
self.checker.note(level, msg, *args)
|
|
else:
|
|
if self.verbose >= level:
|
|
if args:
|
|
msg = msg%args
|
|
print msg
|
|
|
|
# Method to retrieve names.
|
|
def getnames(self):
|
|
if self.parser:
|
|
return self.parser.names
|
|
else:
|
|
return []
|
|
|
|
def getlinkinfos(self):
|
|
# File reading is done in __init__() routine. Store parser in
|
|
# local variable to indicate success of parsing.
|
|
|
|
# If no parser was stored, fail.
|
|
if not self.parser: return []
|
|
|
|
rawlinks = self.parser.getlinks()
|
|
base = urlparse.urljoin(self.url, self.parser.getbase() or "")
|
|
infos = []
|
|
for rawlink in rawlinks:
|
|
t = urlparse.urlparse(rawlink)
|
|
# DON'T DISCARD THE FRAGMENT! Instead, include
|
|
# it in the tuples which are returned. See Checker.dopage().
|
|
fragment = t[-1]
|
|
t = t[:-1] + ('',)
|
|
rawlink = urlparse.urlunparse(t)
|
|
link = urlparse.urljoin(base, rawlink)
|
|
infos.append((link, rawlink, fragment))
|
|
|
|
return infos
|
|
|
|
|
|
class MyStringIO(StringIO.StringIO):
|
|
|
|
def __init__(self, url, info):
|
|
self.__url = url
|
|
self.__info = info
|
|
StringIO.StringIO.__init__(self)
|
|
|
|
def info(self):
|
|
return self.__info
|
|
|
|
def geturl(self):
|
|
return self.__url
|
|
|
|
|
|
class MyURLopener(urllib.FancyURLopener):
|
|
|
|
http_error_default = urllib.URLopener.http_error_default
|
|
|
|
def __init__(*args):
|
|
self = args[0]
|
|
urllib.FancyURLopener.__init__(*args)
|
|
self.addheaders = [
|
|
('User-agent', 'Python-webchecker/%s' % __version__),
|
|
]
|
|
|
|
def http_error_401(self, url, fp, errcode, errmsg, headers):
|
|
return None
|
|
|
|
def open_file(self, url):
|
|
path = urllib.url2pathname(urllib.unquote(url))
|
|
if os.path.isdir(path):
|
|
if path[-1] != os.sep:
|
|
url = url + '/'
|
|
indexpath = os.path.join(path, "index.html")
|
|
if os.path.exists(indexpath):
|
|
return self.open_file(url + "index.html")
|
|
try:
|
|
names = os.listdir(path)
|
|
except os.error, msg:
|
|
raise IOError, msg, sys.exc_traceback
|
|
names.sort()
|
|
s = MyStringIO("file:"+url, {'content-type': 'text/html'})
|
|
s.write('<BASE HREF="file:%s">\n' %
|
|
urllib.quote(os.path.join(path, "")))
|
|
for name in names:
|
|
q = urllib.quote(name)
|
|
s.write('<A HREF="%s">%s</A>\n' % (q, q))
|
|
s.seek(0)
|
|
return s
|
|
return urllib.FancyURLopener.open_file(self, url)
|
|
|
|
|
|
class MyHTMLParser(sgmllib.SGMLParser):
|
|
|
|
def __init__(self, url, verbose=VERBOSE, checker=None):
|
|
self.myverbose = verbose # now unused
|
|
self.checker = checker
|
|
self.base = None
|
|
self.links = {}
|
|
self.names = []
|
|
self.url = url
|
|
sgmllib.SGMLParser.__init__(self)
|
|
|
|
def check_name_id(self, attributes):
|
|
""" Check the name or id attributes on an element.
|
|
"""
|
|
# We must rescue the NAME or id (name is deprecated in XHTML)
|
|
# attributes from the anchor, in order to
|
|
# cache the internal anchors which are made
|
|
# available in the page.
|
|
for name, value in attributes:
|
|
if name == "name" or name == "id":
|
|
if value in self.names:
|
|
self.checker.message("WARNING: duplicate ID name %s in %s",
|
|
value, self.url)
|
|
else: self.names.append(value)
|
|
break
|
|
|
|
def unknown_starttag(self, tag, attributes):
|
|
""" In XHTML, you can have id attributes on any element.
|
|
"""
|
|
self.check_name_id(attributes)
|
|
|
|
def start_a(self, attributes):
|
|
self.link_attr(attributes, 'href')
|
|
self.check_name_id(attributes)
|
|
|
|
def end_a(self): pass
|
|
|
|
def do_area(self, attributes):
|
|
self.link_attr(attributes, 'href')
|
|
self.check_name_id(attributes)
|
|
|
|
def do_body(self, attributes):
|
|
self.link_attr(attributes, 'background', 'bgsound')
|
|
self.check_name_id(attributes)
|
|
|
|
def do_img(self, attributes):
|
|
self.link_attr(attributes, 'src', 'lowsrc')
|
|
self.check_name_id(attributes)
|
|
|
|
def do_frame(self, attributes):
|
|
self.link_attr(attributes, 'src', 'longdesc')
|
|
self.check_name_id(attributes)
|
|
|
|
def do_iframe(self, attributes):
|
|
self.link_attr(attributes, 'src', 'longdesc')
|
|
self.check_name_id(attributes)
|
|
|
|
def do_link(self, attributes):
|
|
for name, value in attributes:
|
|
if name == "rel":
|
|
parts = value.lower().split()
|
|
if ( parts == ["stylesheet"]
|
|
or parts == ["alternate", "stylesheet"]):
|
|
self.link_attr(attributes, "href")
|
|
break
|
|
self.check_name_id(attributes)
|
|
|
|
def do_object(self, attributes):
|
|
self.link_attr(attributes, 'data', 'usemap')
|
|
self.check_name_id(attributes)
|
|
|
|
def do_script(self, attributes):
|
|
self.link_attr(attributes, 'src')
|
|
self.check_name_id(attributes)
|
|
|
|
def do_table(self, attributes):
|
|
self.link_attr(attributes, 'background')
|
|
self.check_name_id(attributes)
|
|
|
|
def do_td(self, attributes):
|
|
self.link_attr(attributes, 'background')
|
|
self.check_name_id(attributes)
|
|
|
|
def do_th(self, attributes):
|
|
self.link_attr(attributes, 'background')
|
|
self.check_name_id(attributes)
|
|
|
|
def do_tr(self, attributes):
|
|
self.link_attr(attributes, 'background')
|
|
self.check_name_id(attributes)
|
|
|
|
def link_attr(self, attributes, *args):
|
|
for name, value in attributes:
|
|
if name in args:
|
|
if value: value = value.strip()
|
|
if value: self.links[value] = None
|
|
|
|
def do_base(self, attributes):
|
|
for name, value in attributes:
|
|
if name == 'href':
|
|
if value: value = value.strip()
|
|
if value:
|
|
if self.checker:
|
|
self.checker.note(1, " Base %s", value)
|
|
self.base = value
|
|
self.check_name_id(attributes)
|
|
|
|
def getlinks(self):
|
|
return self.links.keys()
|
|
|
|
def getbase(self):
|
|
return self.base
|
|
|
|
|
|
if __name__ == '__main__':
|
|
main()
|