Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Inherits test_gzip/test_tarfile failures on 64-bit platforms from the trunk,
but I don't want the merge to hang around too long (even though the regular
p3yk-contributors are/have been busy with other things.)

Merged revisions 45621-46490 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk

........
  r45621 | george.yoshida | 2006-04-21 18:34:17 +0200 (Fri, 21 Apr 2006) | 2 lines

  Correct the grammar
........
  r45622 | tim.peters | 2006-04-21 18:34:54 +0200 (Fri, 21 Apr 2006) | 2 lines

  Whitespace normalization.
........
  r45624 | thomas.heller | 2006-04-21 18:48:56 +0200 (Fri, 21 Apr 2006) | 1 line

  Merge in changes from ctypes 0.9.9.6 upstream version.
........
  r45625 | thomas.heller | 2006-04-21 18:51:04 +0200 (Fri, 21 Apr 2006) | 1 line

  Merge in changes from ctypes 0.9.9.6 upstream version.
........
  r45630 | thomas.heller | 2006-04-21 20:29:17 +0200 (Fri, 21 Apr 2006) | 8 lines

  Documentation for ctypes.
  I think that 'generic operating system services' is the best category.
  Note that the Doc/lib/libctypes.latex file is generated from reST sources.
  You are welcome to make typo fixes, and I'll try to keep the reST sources
  in sync, but markup changes would be lost - they should be fixed in the tool
  that creates the latex file.
  The conversion script is external/ctypes/docs/manual/mkpydoc.py.
........
  r45631 | tim.peters | 2006-04-21 23:18:10 +0200 (Fri, 21 Apr 2006) | 24 lines

  SF bug #1473760 TempFile can hang on Windows.

  Python 2.4 changed ntpath.abspath to do an import
  inside the function.  As a result, due to Python's
  import lock, anything calling abspath on Windows
  (directly, or indirectly like tempfile.TemporaryFile)
  hung when it was called from a thread spawned as a
  side effect of importing a module.

  This is a depressingly frequent problem, and
  deserves a more general fix.  I'm settling for
  a micro-fix here because this specific one accounts
  for a report of Zope Corp's ZEO hanging on Windows,
  and it was an odd way to change abspath to begin
  with (ntpath needs a different implementation
  depending on whether we're actually running on
  Windows, and the _obvious_ way to arrange for that
  is not to bury a possibly-failing import _inside_
  the function).

  Note that if/when other micro-fixes of this kind
  get made, the new Lib/test/threaded_import_hangers.py
  is a convenient place to add tests for them.
........
  r45634 | phillip.eby | 2006-04-21 23:53:37 +0200 (Fri, 21 Apr 2006) | 2 lines

  Guido wrote contextlib, not me, but thanks anyway.  ;)
........
  r45636 | andrew.kuchling | 2006-04-22 03:51:41 +0200 (Sat, 22 Apr 2006) | 1 line

  Typo fixes
........
  r45638 | andrew.kuchling | 2006-04-22 03:58:40 +0200 (Sat, 22 Apr 2006) | 1 line

  Fix comment typo
........
  r45639 | andrew.kuchling | 2006-04-22 04:06:03 +0200 (Sat, 22 Apr 2006) | 8 lines

  Make copy of test_mailbox.py.  We'll still want to check the backward
  compatibility classes in the new mailbox.py that I'll be committing in
  a few minutes.

  One change has been made: the tests use len(mbox) instead of len(mbox.boxes).
  The 'boxes' attribute was never documented and contains some internal state
  that seems unlikely to have been useful.
........
  r45640 | andrew.kuchling | 2006-04-22 04:32:43 +0200 (Sat, 22 Apr 2006) | 16 lines

  Add Gregory K. Johnson's revised version of mailbox.py (funded by
  the 2005 Summer of Code).

  The revision adds a number of new mailbox classes that support adding
  and removing messages; these classes also support mailbox locking and
  default to using email.Message instead of rfc822.Message.

  The old mailbox classes are largely left alone for backward compatibility.
  The exception is the Maildir class, which was present in the old module
  and now inherits from the new classes.  The Maildir class's interface
  is pretty simple, though, so I think it'll be compatible with existing
  code.

  (The change to the NEWS file also adds a missing word to a different
  news item, which unfortunately required rewrapping the line.)
........
  r45641 | tim.peters | 2006-04-22 07:52:59 +0200 (Sat, 22 Apr 2006) | 2 lines

  Whitespace normalization.
........
  r45642 | neal.norwitz | 2006-04-22 08:07:46 +0200 (Sat, 22 Apr 2006) | 1 line

  Add libctypes as a dep
........
  r45643 | martin.v.loewis | 2006-04-22 13:15:41 +0200 (Sat, 22 Apr 2006) | 1 line

  Fix more ssize_t problems.
........
  r45644 | martin.v.loewis | 2006-04-22 13:40:03 +0200 (Sat, 22 Apr 2006) | 1 line

  Fix more ssize_t issues.
........
  r45645 | george.yoshida | 2006-04-22 17:10:49 +0200 (Sat, 22 Apr 2006) | 2 lines

  Typo fixes
........
  r45647 | martin.v.loewis | 2006-04-22 17:19:54 +0200 (Sat, 22 Apr 2006) | 1 line

  Port to Python 2.5. Drop .DEF file. Change output file names to .pyd.
........
  r45648 | george.yoshida | 2006-04-22 17:27:14 +0200 (Sat, 22 Apr 2006) | 3 lines

  - add versionadded tag
  - make arbitrary arguments come last
........
  r45649 | hyeshik.chang | 2006-04-22 17:48:15 +0200 (Sat, 22 Apr 2006) | 3 lines

  Remove $CJKCodecs$ RCS tags.  The CJKCodecs isn't maintained outside
  anymore.
........
  r45654 | greg.ward | 2006-04-23 05:47:58 +0200 (Sun, 23 Apr 2006) | 2 lines

  Update optparse to Optik 1.5.1.
........
  r45658 | george.yoshida | 2006-04-23 11:27:10 +0200 (Sun, 23 Apr 2006) | 2 lines

  wrap SyntaxError with \exception{}
........
  r45660 | ronald.oussoren | 2006-04-23 13:59:25 +0200 (Sun, 23 Apr 2006) | 6 lines

  Patch 1471925 - Weak linking support for OSX

  This patch causes several symbols in the socket and posix module to be weakly
  linked on OSX and disables usage of ftime on OSX. These changes make it possible
  to use a binary build on OSX 10.4 on a 10.3 system.
........
  r45661 | ronald.oussoren | 2006-04-23 14:36:23 +0200 (Sun, 23 Apr 2006) | 5 lines

  Patch 1471761 - test for broken poll at runtime

  This patch checks if poll is broken when the select module is loaded instead
  of doing so at configure-time. This functionality is only active on Mac OS X.
........
  r45662 | nick.coghlan | 2006-04-23 17:13:32 +0200 (Sun, 23 Apr 2006) | 1 line

  Add a Context Types section to parallel the Iterator Types section (uses the same terminology as the 2.5a1 implementation)
........
  r45663 | nick.coghlan | 2006-04-23 17:14:37 +0200 (Sun, 23 Apr 2006) | 1 line

  Update contextlib documentation to use the same terminology as the module implementation
........
  r45664 | gerhard.haering | 2006-04-23 17:24:26 +0200 (Sun, 23 Apr 2006) | 2 lines

  Updated the sqlite3 module to the external pysqlite 2.2.2 version.
........
  r45666 | nick.coghlan | 2006-04-23 17:39:16 +0200 (Sun, 23 Apr 2006) | 1 line

  Update with statement documentation to use same terminology as 2.5a1 implementation
........
  r45667 | nick.coghlan | 2006-04-23 18:05:04 +0200 (Sun, 23 Apr 2006) | 1 line

  Add a (very) brief mention of the with statement to the end of chapter 8
........
  r45668 | nick.coghlan | 2006-04-23 18:35:19 +0200 (Sun, 23 Apr 2006) | 1 line

  Take 2 on mentioning the with statement, this time without inadvertently killing the Unicode examples
........
  r45669 | nick.coghlan | 2006-04-23 19:04:07 +0200 (Sun, 23 Apr 2006) | 1 line

  Backdated NEWS entry to record the implementation of PEP 338 for alpha 1
........
  r45670 | tim.peters | 2006-04-23 20:13:45 +0200 (Sun, 23 Apr 2006) | 2 lines

  Whitespace normalization.
........
  r45671 | skip.montanaro | 2006-04-23 21:14:27 +0200 (Sun, 23 Apr 2006) | 1 line

  first cut at trace module doc
........
  r45672 | skip.montanaro | 2006-04-23 21:26:33 +0200 (Sun, 23 Apr 2006) | 1 line

  minor tweak
........
  r45673 | skip.montanaro | 2006-04-23 21:30:50 +0200 (Sun, 23 Apr 2006) | 1 line

  it's always helpful if the example works...
........
  r45674 | skip.montanaro | 2006-04-23 21:32:14 +0200 (Sun, 23 Apr 2006) | 1 line

  correct example
........
  r45675 | andrew.kuchling | 2006-04-23 23:01:04 +0200 (Sun, 23 Apr 2006) | 1 line

  Edits to the PEP 343 section
........
  r45676 | andrew.kuchling | 2006-04-23 23:51:10 +0200 (Sun, 23 Apr 2006) | 1 line

  Add two items
........
  r45677 | tim.peters | 2006-04-24 04:03:16 +0200 (Mon, 24 Apr 2006) | 5 lines

  Bug #1337990: clarified that `doctest` does not support examples
  requiring both expected output and an exception.

  I'll backport to 2.4 next.
........
  r45679 | nick.coghlan | 2006-04-24 05:04:43 +0200 (Mon, 24 Apr 2006) | 1 line

  Note changes made to PEP 343 related documentation
........
  r45681 | nick.coghlan | 2006-04-24 06:17:02 +0200 (Mon, 24 Apr 2006) | 1 line

  Change PEP 343 related documentation to use the term context specifier instead of context object
........
  r45682 | nick.coghlan | 2006-04-24 06:32:47 +0200 (Mon, 24 Apr 2006) | 1 line

  Add unit tests for the -m and -c command line switches
........
  r45683 | nick.coghlan | 2006-04-24 06:37:15 +0200 (Mon, 24 Apr 2006) | 1 line

  Fix contextlib.nested to cope with exit methods raising and handling exceptions
........
  r45685 | nick.coghlan | 2006-04-24 06:59:28 +0200 (Mon, 24 Apr 2006) | 1 line

  Fix broken contextlib test from last checkin (I'd've sworn I tested that before checking it in. . .)
........
  r45686 | nick.coghlan | 2006-04-24 07:24:26 +0200 (Mon, 24 Apr 2006) | 1 line

  Back out new command line tests (broke buildbot)
........
  r45687 | nick.coghlan | 2006-04-24 07:52:15 +0200 (Mon, 24 Apr 2006) | 1 line

  More reliable version of new command line tests that just checks the exit codes
........
  r45688 | thomas.wouters | 2006-04-24 13:37:13 +0200 (Mon, 24 Apr 2006) | 4 lines


  Stop test_tcl's testLoadTk from leaking the Tk commands 'loadtk' registers.
........
  r45690 | andrew.kuchling | 2006-04-24 16:30:47 +0200 (Mon, 24 Apr 2006) | 2 lines

  Edits, using the new term
  'context specifier' in a few places
........
  r45697 | phillip.eby | 2006-04-24 22:53:13 +0200 (Mon, 24 Apr 2006) | 2 lines

  Revert addition of setuptools
........
  r45698 | tim.peters | 2006-04-25 00:45:13 +0200 (Tue, 25 Apr 2006) | 2 lines

  Whitespace normalization.
........
  r45700 | trent.mick | 2006-04-25 02:34:50 +0200 (Tue, 25 Apr 2006) | 4 lines

  Put break at correct level so *all* root HKEYs acutally get checked for
  an installed VC6. Otherwise only the first such tree gets checked and this
  warning doesn't get displayed.
........
  r45701 | tim.peters | 2006-04-25 05:31:36 +0200 (Tue, 25 Apr 2006) | 3 lines

  Patch #1475231:  add a new SKIP doctest option, thanks to
  Edward Loper.
........
  r45702 | neal.norwitz | 2006-04-25 07:04:35 +0200 (Tue, 25 Apr 2006) | 1 line

  versionadded for SKIP
........
  r45703 | neal.norwitz | 2006-04-25 07:05:03 +0200 (Tue, 25 Apr 2006) | 1 line

  Restore Walters name
........
  r45704 | neal.norwitz | 2006-04-25 07:49:42 +0200 (Tue, 25 Apr 2006) | 1 line

  Revert previous change, SKIP had a versionadded elsewhere
........
  r45706 | nick.coghlan | 2006-04-25 12:56:51 +0200 (Tue, 25 Apr 2006) | 31 lines

  Move the PEP 343 documentation and implementation closer to the
  terminology in the alpha 1 documentation.

   - "context manager" reverts to its alpha 1 definition
   - the term "context specifier" goes away entirely
   - contextlib.GeneratorContextManager is renamed GeneratorContext

  There are still a number of changes relative to alpha 1:

    - the expression in the with statement is explicitly called the
      "context expression" in the language reference
    - the terms 'with statement context', 'context object' or 'with
      statement context' are used in several places instead of a bare
      'context'. The aim of this is to avoid ambiguity in relation to the
      runtime context set up when the block is executed, and the context
      objects that already exist in various application domains (such as
      decimal.Context)
    - contextlib.contextmanager is renamed to contextfactory
      This best reflects the nature of the function resulting from the
      use of that decorator
    - decimal.ContextManager is renamed to WithStatementContext
      Simple dropping the 'Manager' part wasn't possible due to the
      fact that decimal.Context already exists and means something
      different. WithStatementContext is ugly but workable.

  A technically unrelated change snuck into this commit:
  contextlib.closing now avoids the overhead of creating a
  generator, since it's trivial to implement that particular
  context manager directly.
........
  r45707 | nick.coghlan | 2006-04-25 13:05:56 +0200 (Tue, 25 Apr 2006) | 1 line

  Fix latex typo
........
  r45708 | thomas.wouters | 2006-04-25 14:28:56 +0200 (Tue, 25 Apr 2006) | 4 lines


  Fix markup glitch in unittest docs. Will backport.
........
  r45710 | andrew.kuchling | 2006-04-25 14:31:38 +0200 (Tue, 25 Apr 2006) | 1 line

  Add two items; easy_install is now off the table, though pkgutil still is
........
  r45711 | andrew.kuchling | 2006-04-25 14:47:25 +0200 (Tue, 25 Apr 2006) | 1 line

  Rework context terminology
........
  r45712 | thomas.wouters | 2006-04-25 15:53:23 +0200 (Tue, 25 Apr 2006) | 9 lines


  SF bug/patch #1433877: string parameter to ioctl not null terminated

  The new char-array used in ioctl calls wasn't explicitly NUL-terminated;
  quite probably the cause for the test_pty failures on Solaris that we
  circumvented earlier. (I wasn't able to reproduce it with this patch, but it
  has been somewhat elusive to start with.)
........
  r45713 | george.yoshida | 2006-04-25 16:09:58 +0200 (Tue, 25 Apr 2006) | 2 lines

  minor tweak
........
  r45714 | thomas.wouters | 2006-04-25 17:08:10 +0200 (Tue, 25 Apr 2006) | 7 lines


  Fix SF bug #1476111: SystemError in socket sendto. The AF_INET6 and
  AF_PACKET cases in getsockaddrarg were missing their own checks for
  tuple-ness of the address argument, which means a confusing SystemError was
  raised by PyArg_ParseTuple instead.
........
  r45715 | thomas.wouters | 2006-04-25 17:29:46 +0200 (Tue, 25 Apr 2006) | 10 lines


  Define MAXPATHLEN to be at least PATH_MAX, if that's defined. Python uses
  MAXPATHLEN-sized buffers for various output-buffers (like to realpath()),
  and that's correct on BSD platforms, but not Linux (which uses PATH_MAX, and
  does not define MAXPATHLEN.) Cursory googling suggests Linux is following a
  newer standard than BSD, but in cases like this, who knows. Using the
  greater of PATH_MAX and 1024 as a fallback for MAXPATHLEN seems to be the
  most portable solution.
........
  r45717 | thomas.heller | 2006-04-25 20:26:08 +0200 (Tue, 25 Apr 2006) | 3 lines

  Fix compiler warnings on Darwin.
  Patch by Brett Canon, see
  https://sourceforge.net/tracker/?func=detail&atid=532156&aid=1475959&group_id=71702
........
  r45718 | guido.van.rossum | 2006-04-25 22:12:45 +0200 (Tue, 25 Apr 2006) | 4 lines

  Implement MvL's improvement on __context__ in Condition;
  this can just call __context__ on the underlying lock.
  (The same change for Semaphore does *not* work!)
........
  r45721 | tim.peters | 2006-04-26 03:15:53 +0200 (Wed, 26 Apr 2006) | 13 lines

  Rev 45706 renamed stuff in contextlib.py, but didn't rename
  uses of it in test_with.py.  As a result, test_with has been skipped
  (due to failing imports) on all buildbot boxes since.  Alas, that's
  not a test failure -- you have to pay attention to the

      1 skip unexpected on PLATFORM:
          test_with

  kinds of output at the ends of test runs to notice that this got
  broken.

  It's likely that more renaming in test_with.py would be desirable.
........
  r45722 | fred.drake | 2006-04-26 07:15:41 +0200 (Wed, 26 Apr 2006) | 1 line

  markup fixes, cleanup
........
  r45723 | fred.drake | 2006-04-26 07:19:39 +0200 (Wed, 26 Apr 2006) | 1 line

  minor adjustment suggested by Peter Gephardt
........
  r45724 | neal.norwitz | 2006-04-26 07:34:03 +0200 (Wed, 26 Apr 2006) | 10 lines

  Patch from Aldo Cortesi (OpenBSD buildbot owner).

  After the patch (45590) to add extra debug stats to the gc module, Python
  was crashing on OpenBSD due to:
  	Fatal Python error: Interpreter not initialized (version mismatch?)

  This seems to occur due to calling collect() when initialized (in pythonrun.c)
  is set to 0.  Now, the import will occur in the init function which
  shouldn't suffer this problem.
........
  r45725 | neal.norwitz | 2006-04-26 08:26:12 +0200 (Wed, 26 Apr 2006) | 3 lines

  Fix this test on Solaris.  There can be embedded \r, so don't just replace
  the one at the end.
........
  r45727 | nick.coghlan | 2006-04-26 13:50:04 +0200 (Wed, 26 Apr 2006) | 1 line

  Fix an error in the last contextlib.closing example
........
  r45728 | andrew.kuchling | 2006-04-26 14:21:06 +0200 (Wed, 26 Apr 2006) | 1 line

  [Bug #1475080] Fix example
........
  r45729 | andrew.kuchling | 2006-04-26 14:23:39 +0200 (Wed, 26 Apr 2006) | 1 line

  Add labels to all sections
........
  r45730 | thomas.wouters | 2006-04-26 17:53:30 +0200 (Wed, 26 Apr 2006) | 7 lines


  The result of SF patch #1471578: big-memory tests for strings, lists and
  tuples. Lots to be added, still, but this will give big-memory people
  something to play with in 2.5 alpha 2, and hopefully get more people to
  write these tests.
........
  r45731 | tim.peters | 2006-04-26 19:11:16 +0200 (Wed, 26 Apr 2006) | 2 lines

  Whitespace normalization.
........
  r45732 | martin.v.loewis | 2006-04-26 19:19:44 +0200 (Wed, 26 Apr 2006) | 1 line

  Use GS- and bufferoverlowU.lib where appropriate, for AMD64.
........
  r45733 | thomas.wouters | 2006-04-26 20:46:01 +0200 (Wed, 26 Apr 2006) | 5 lines


  Add tests for += and *= on strings, and fix the memory-use estimate for the
  list.extend tests (they were estimating half the actual use.)
........
  r45734 | thomas.wouters | 2006-04-26 21:14:46 +0200 (Wed, 26 Apr 2006) | 5 lines


  Some more test-size-estimate fixes: test_append and test_insert trigger a
  list resize, which overallocates.
........
  r45735 | hyeshik.chang | 2006-04-26 21:20:26 +0200 (Wed, 26 Apr 2006) | 3 lines

  Fix build on MIPS for libffi.  I haven't tested this yet because I
  don't have an access on MIPS machines.  Will be tested by buildbot. :)
........
  r45737 | fred.drake | 2006-04-27 01:40:32 +0200 (Thu, 27 Apr 2006) | 1 line

  one more place to use the current Python version
........
  r45738 | fred.drake | 2006-04-27 02:02:24 +0200 (Thu, 27 Apr 2006) | 3 lines

  - update version numbers in file names again, until we have a better way
  - elaborate instructions for Cygwin support (closes SF #839709)
........
  r45739 | fred.drake | 2006-04-27 02:20:14 +0200 (Thu, 27 Apr 2006) | 1 line

  add missing word
........
  r45740 | anthony.baxter | 2006-04-27 04:11:24 +0200 (Thu, 27 Apr 2006) | 2 lines

  2.5a2
........
  r45741 | anthony.baxter | 2006-04-27 04:13:13 +0200 (Thu, 27 Apr 2006) | 1 line

  2.5a2
........
  r45749 | andrew.kuchling | 2006-04-27 14:22:37 +0200 (Thu, 27 Apr 2006) | 1 line

  Now that 2.5a2 is out, revert to the current date
........
  r45750 | andrew.kuchling | 2006-04-27 14:23:07 +0200 (Thu, 27 Apr 2006) | 1 line

  Bump document version
........
  r45751 | andrew.kuchling | 2006-04-27 14:34:39 +0200 (Thu, 27 Apr 2006) | 6 lines

  [Bug #1477102] Add necessary import to example

  This may be a useful style question for the docs -- should examples show
  the necessary imports, or should it be assumed that the reader will
  figure it out?  In the What's New, I'm not consistent but usually opt
  for omitting the imports.
........
  r45753 | andrew.kuchling | 2006-04-27 14:38:35 +0200 (Thu, 27 Apr 2006) | 1 line

  [Bug #1477140] Import Error base class
........
  r45754 | andrew.kuchling | 2006-04-27 14:42:54 +0200 (Thu, 27 Apr 2006) | 1 line

  Mention the xmlrpclib.Error base class, which is used in one of the examples
........
  r45756 | george.yoshida | 2006-04-27 15:41:07 +0200 (Thu, 27 Apr 2006) | 2 lines

  markup fix
........
  r45757 | thomas.wouters | 2006-04-27 15:46:59 +0200 (Thu, 27 Apr 2006) | 4 lines


  Some more size-estimate fixes, for large-list-tests.
........
  r45758 | thomas.heller | 2006-04-27 17:50:42 +0200 (Thu, 27 Apr 2006) | 3 lines

  Rerun the libffi configuration if any of the files used for that
  are newer then fficonfig.py.
........
  r45766 | thomas.wouters | 2006-04-28 00:37:50 +0200 (Fri, 28 Apr 2006) | 6 lines


  Some style fixes and size-calculation fixes. Also do the small-memory run
  using a prime number, rather than a convenient power-of-2-and-multiple-of-5,
  so incorrect testing algorithms fail more easily.
........
  r45767 | thomas.wouters | 2006-04-28 00:38:32 +0200 (Fri, 28 Apr 2006) | 6 lines


  Do the small-memory run of big-meormy tests using a prime number, rather
  than a convenient power-of-2-and-multiple-of-5, so incorrect testing
  algorithms fail more easily.
........
  r45768 | david.goodger | 2006-04-28 00:53:05 +0200 (Fri, 28 Apr 2006) | 1 line

  Added SVN access for Steven Bethard and Talin, for PEP updating.
........
  r45770 | thomas.wouters | 2006-04-28 01:13:20 +0200 (Fri, 28 Apr 2006) | 16 lines


   - Add new Warning class, ImportWarning

   - Warn-raise ImportWarning when importing would have picked up a directory
     as package, if only it'd had an __init__.py. This swaps two tests (for
     case-ness and __init__-ness), but case-test is not really more expensive,
     and it's not in a speed-critical section.

   - Test for the new warning by importing a common non-package directory on
     sys.path: site-packages

   - In regrtest.py, silence warnings generated by the build-environment
     because Modules/ (which is added to sys.path for Setup-created modules)
     has 'zlib' and '_ctypes' directories without __init__.py's.
........
  r45771 | thomas.wouters | 2006-04-28 01:41:27 +0200 (Fri, 28 Apr 2006) | 6 lines


  Add more ignores of ImportWarnings; these are all just potential triggers
  (since they won't trigger if zlib is already sucessfully imported); they
  were found by grepping .py files, instead of looking at warning output :)
........
  r45773 | neal.norwitz | 2006-04-28 06:32:20 +0200 (Fri, 28 Apr 2006) | 1 line

  Add some whitespace to be more consistent.
........
  r45774 | neal.norwitz | 2006-04-28 06:34:43 +0200 (Fri, 28 Apr 2006) | 5 lines

  Try to really fix the slow buildbots this time.
  Printing to stdout, doesn't mean the data was actually written.
  It depends on the buffering, so we need to flush.  This will hopefully
  really fix the buildbots getting killed due to no output on the slow bots.
........
  r45775 | neal.norwitz | 2006-04-28 07:28:05 +0200 (Fri, 28 Apr 2006) | 1 line

  Fix some warnings on Mac OS X 10.4
........
  r45776 | neal.norwitz | 2006-04-28 07:28:30 +0200 (Fri, 28 Apr 2006) | 1 line

  Fix a warning on alpha
........
  r45777 | neal.norwitz | 2006-04-28 07:28:54 +0200 (Fri, 28 Apr 2006) | 1 line

  Fix a warning on ppc (debian)
........
  r45778 | george.yoshida | 2006-04-28 18:09:45 +0200 (Fri, 28 Apr 2006) | 2 lines

  fix markup glitch
........
  r45780 | georg.brandl | 2006-04-28 18:31:17 +0200 (Fri, 28 Apr 2006) | 3 lines

  Add SeaMonkey to the list of Mozilla browsers.
........
  r45781 | georg.brandl | 2006-04-28 18:36:55 +0200 (Fri, 28 Apr 2006) | 2 lines

  Bug #1475009: clarify ntpath.join behavior with absolute components
........
  r45783 | george.yoshida | 2006-04-28 18:40:14 +0200 (Fri, 28 Apr 2006) | 2 lines

  correct a dead link
........
  r45785 | georg.brandl | 2006-04-28 18:54:25 +0200 (Fri, 28 Apr 2006) | 4 lines

  Bug #1472949: stringify IOErrors in shutil.copytree when appending
  them to the Error errors list.
........
  r45786 | georg.brandl | 2006-04-28 18:58:52 +0200 (Fri, 28 Apr 2006) | 3 lines

  Bug #1478326: don't allow '/' in distutils.util.get_platform machine names
  since this value is used to name the build directory.
........
  r45788 | thomas.heller | 2006-04-28 19:02:18 +0200 (Fri, 28 Apr 2006) | 1 line

  Remove a duplicated test (the same test is in test_incomplete.py).
........
  r45792 | georg.brandl | 2006-04-28 21:09:24 +0200 (Fri, 28 Apr 2006) | 3 lines

  Bug #1478429: make datetime.datetime.fromtimestamp accept every float,
  possibly "rounding up" to the next whole second.
........
  r45796 | george.yoshida | 2006-04-29 04:43:30 +0200 (Sat, 29 Apr 2006) | 2 lines

  grammar fix
........
  r45800 | ronald.oussoren | 2006-04-29 13:31:35 +0200 (Sat, 29 Apr 2006) | 2 lines

  Patch 1471883: --enable-universalsdk on Mac OS X
........
  r45801 | andrew.kuchling | 2006-04-29 13:53:15 +0200 (Sat, 29 Apr 2006) | 1 line

  Add item
........
  r45802 | andrew.kuchling | 2006-04-29 14:10:28 +0200 (Sat, 29 Apr 2006) | 1 line

  Make case of 'ZIP' consistent
........
  r45803 | andrew.kuchling | 2006-04-29 14:10:43 +0200 (Sat, 29 Apr 2006) | 1 line

  Add item
........
  r45808 | martin.v.loewis | 2006-04-29 14:37:25 +0200 (Sat, 29 Apr 2006) | 3 lines

  Further changes for #1471883: Edit Misc/NEWS, and
  add expat_config.h.
........
  r45809 | brett.cannon | 2006-04-29 23:29:50 +0200 (Sat, 29 Apr 2006) | 2 lines

  Fix docstring for contextfactory; mentioned old contextmanager name.
........
  r45810 | gerhard.haering | 2006-04-30 01:12:41 +0200 (Sun, 30 Apr 2006) | 3 lines

  This is the start of documentation for the sqlite3 module. Please feel free to
  find a better place for the link to it than alongside bsddb & friends.
........
  r45811 | andrew.kuchling | 2006-04-30 03:07:09 +0200 (Sun, 30 Apr 2006) | 1 line

  Add two items
........
  r45814 | george.yoshida | 2006-04-30 05:49:56 +0200 (Sun, 30 Apr 2006) | 2 lines

  Use \versionchanged instead of \versionadded for new parameter support.
........
  r45815 | georg.brandl | 2006-04-30 09:06:11 +0200 (Sun, 30 Apr 2006) | 2 lines

  Patch #1470846: fix urllib2 ProxyBasicAuthHandler.
........
  r45817 | georg.brandl | 2006-04-30 10:57:35 +0200 (Sun, 30 Apr 2006) | 3 lines

  In stdlib, use hashlib instead of deprecated md5 and sha modules.
........
  r45819 | georg.brandl | 2006-04-30 11:23:59 +0200 (Sun, 30 Apr 2006) | 3 lines

  Patch #1470976: don't NLST files when retrieving over FTP.
........
  r45821 | georg.brandl | 2006-04-30 13:13:56 +0200 (Sun, 30 Apr 2006) | 6 lines

  Bug #1473625: stop cPickle making float dumps locale dependent in protocol 0.

  On the way, add a decorator to test_support to facilitate running single
  test functions in different locales with automatic cleanup.
........
  r45822 | phillip.eby | 2006-04-30 17:59:26 +0200 (Sun, 30 Apr 2006) | 2 lines

  Fix infinite regress when inspecting <string> or <stdin> frames.
........
  r45824 | georg.brandl | 2006-04-30 19:42:26 +0200 (Sun, 30 Apr 2006) | 3 lines

  Fix another problem in inspect: if the module for an object cannot be found, don't try to give its __dict__ to linecache.
........
  r45825 | georg.brandl | 2006-04-30 20:14:54 +0200 (Sun, 30 Apr 2006) | 3 lines

  Patch #1472854: make the rlcompleter.Completer class usable on non-
  UNIX platforms.
........
  r45826 | georg.brandl | 2006-04-30 21:34:19 +0200 (Sun, 30 Apr 2006) | 3 lines

  Patch #1479438: add \keyword markup for "with".
........
  r45827 | andrew.kuchling | 2006-04-30 23:19:31 +0200 (Sun, 30 Apr 2006) | 1 line

  Add urllib2 HOWTO from Michael Foord
........
  r45828 | andrew.kuchling | 2006-04-30 23:19:49 +0200 (Sun, 30 Apr 2006) | 1 line

  Add item
........
  r45830 | barry.warsaw | 2006-05-01 05:03:02 +0200 (Mon, 01 May 2006) | 11 lines

  Port forward from 2.4 branch:

  Patch #1464708 from William McVey: fixed handling of nested comments in mail
  addresses.  E.g.

  "Foo ((Foo Bar)) <foo@example.com>"

  Fixes for both rfc822.py and email package.  This patch needs to be back
  ported to Python 2.3 for email 2.5.
........
  r45832 | fred.drake | 2006-05-01 08:25:58 +0200 (Mon, 01 May 2006) | 4 lines

  - minor clarification in section title
  - markup adjustments
  (there is clearly much to be done in this section)
........
  r45833 | martin.v.loewis | 2006-05-01 08:28:01 +0200 (Mon, 01 May 2006) | 2 lines

  Work around deadlock risk. Will backport.
........
  r45836 | andrew.kuchling | 2006-05-01 14:45:02 +0200 (Mon, 01 May 2006) | 1 line

  Some ElementTree fixes: import from xml, not xmlcore; fix case of module name; mention list() instead of getchildren()
........
  r45837 | gerhard.haering | 2006-05-01 17:14:48 +0200 (Mon, 01 May 2006) | 3 lines

  Further integration of the documentation for the sqlite3 module. There's still
  quite some content to move over from the pysqlite manual, but it's a start now.
........
  r45838 | martin.v.loewis | 2006-05-01 17:56:03 +0200 (Mon, 01 May 2006) | 2 lines

  Rename uisample to text, drop all non-text tables.
........
  r45839 | martin.v.loewis | 2006-05-01 18:12:44 +0200 (Mon, 01 May 2006) | 2 lines

  Add msilib documentation.
........
  r45840 | martin.v.loewis | 2006-05-01 18:14:16 +0200 (Mon, 01 May 2006) | 4 lines

  Rename parameters to match the documentation (which
  in turn matches Microsoft's documentation).
  Drop unused parameter in CAB.append.
........
  r45841 | fred.drake | 2006-05-01 18:28:54 +0200 (Mon, 01 May 2006) | 1 line

  add dependency
........
  r45842 | andrew.kuchling | 2006-05-01 18:30:25 +0200 (Mon, 01 May 2006) | 1 line

  Markup fixes; add some XXX comments noting problems
........
  r45843 | andrew.kuchling | 2006-05-01 18:32:49 +0200 (Mon, 01 May 2006) | 1 line

  Add item
........
  r45844 | andrew.kuchling | 2006-05-01 19:06:54 +0200 (Mon, 01 May 2006) | 1 line

  Markup fixes
........
  r45850 | neal.norwitz | 2006-05-02 06:43:14 +0200 (Tue, 02 May 2006) | 3 lines

  SF #1479181: split open() and file() from being aliases for each other.
........
  r45852 | neal.norwitz | 2006-05-02 08:23:22 +0200 (Tue, 02 May 2006) | 1 line

  Try to fix breakage caused by patch #1479181, r45850
........
  r45853 | fred.drake | 2006-05-02 08:53:59 +0200 (Tue, 02 May 2006) | 3 lines

  SF #1479988: add methods to allow access to weakrefs for the
  weakref.WeakKeyDictionary and weakref.WeakValueDictionary
........
  r45854 | neal.norwitz | 2006-05-02 09:27:47 +0200 (Tue, 02 May 2006) | 5 lines

  Fix breakage from patch 1471883 (r45800 & r45808) on OSF/1.
  The problem was that pyconfig.h was being included before some system headers
  which caused redefinitions and other breakage.  This moves system headers
  after expat_config.h which includes pyconfig.h.
........
  r45855 | vinay.sajip | 2006-05-02 10:35:36 +0200 (Tue, 02 May 2006) | 1 line

  Replaced my dumb way of calculating seconds to midnight with Tim Peters' much more sensible suggestion. What was I thinking ?!?
........
  r45856 | andrew.kuchling | 2006-05-02 13:30:03 +0200 (Tue, 02 May 2006) | 1 line

  Provide encoding as keyword argument; soften warning paragraph about encodings
........
  r45858 | guido.van.rossum | 2006-05-02 19:36:09 +0200 (Tue, 02 May 2006) | 2 lines

  Fix the formatting of KeyboardInterrupt -- a bad issubclass() call.
........
  r45862 | guido.van.rossum | 2006-05-02 21:47:52 +0200 (Tue, 02 May 2006) | 7 lines

  Get rid of __context__, per the latest changes to PEP 343 and python-dev
  discussion.
  There are two places of documentation that still mention __context__:
  Doc/lib/libstdtypes.tex -- I wasn't quite sure how to rewrite that without
  spending a whole lot of time thinking about it; and whatsnew, which Andrew
  usually likes to change himself.
........
  r45863 | armin.rigo | 2006-05-02 21:52:32 +0200 (Tue, 02 May 2006) | 4 lines

  Documentation bug: PySet_Pop() returns a new reference (because the
  caller becomes the owner of that reference).
........
  r45864 | guido.van.rossum | 2006-05-02 22:47:36 +0200 (Tue, 02 May 2006) | 4 lines

  Hopefully this will fix the spurious failures of test_mailbox.py that I'm
  experiencing.  (This code and mailbox.py itself are full of calls to file()
  that should be calls to open() -- but I'm not fixing those.)
........
  r45865 | andrew.kuchling | 2006-05-02 23:44:33 +0200 (Tue, 02 May 2006) | 1 line

  Use open() instead of file()
........
  r45866 | andrew.kuchling | 2006-05-03 00:47:49 +0200 (Wed, 03 May 2006) | 1 line

  Update context manager section for removal of __context__
........
  r45867 | fred.drake | 2006-05-03 03:46:52 +0200 (Wed, 03 May 2006) | 1 line

  remove unnecessary assignment
........
  r45868 | fred.drake | 2006-05-03 03:48:24 +0200 (Wed, 03 May 2006) | 4 lines

  tell LaTeX2HTML to:
  - use UTF-8 output
  - not mess with the >>> prompt!
........
  r45869 | fred.drake | 2006-05-03 04:04:40 +0200 (Wed, 03 May 2006) | 3 lines

  avoid ugly markup based on the unfortunate conversions of ">>" and "<<" to
  guillemets; no need for magic here
........
  r45870 | fred.drake | 2006-05-03 04:12:47 +0200 (Wed, 03 May 2006) | 1 line

  at least comment on why curly-quotes are not enabled
........
  r45871 | fred.drake | 2006-05-03 04:27:40 +0200 (Wed, 03 May 2006) | 1 line

  one more place to avoid extra markup
........
  r45872 | fred.drake | 2006-05-03 04:29:09 +0200 (Wed, 03 May 2006) | 1 line

  one more place to avoid extra markup (how many will there be?)
........
  r45873 | fred.drake | 2006-05-03 04:29:39 +0200 (Wed, 03 May 2006) | 1 line

  fix up whitespace in prompt strings
........
  r45876 | tim.peters | 2006-05-03 06:46:14 +0200 (Wed, 03 May 2006) | 2 lines

  Whitespace normalization.
........
  r45877 | martin.v.loewis | 2006-05-03 06:52:04 +0200 (Wed, 03 May 2006) | 2 lines

  Correct some formulations, fix XXX comments.
........
  r45879 | georg.brandl | 2006-05-03 07:05:02 +0200 (Wed, 03 May 2006) | 2 lines

  Patch #1480067: don't redirect HTTP digest auth in urllib2
........
  r45881 | georg.brandl | 2006-05-03 07:15:10 +0200 (Wed, 03 May 2006) | 3 lines

  Move network tests from test_urllib2 to test_urllib2net.
........
  r45887 | nick.coghlan | 2006-05-03 15:02:47 +0200 (Wed, 03 May 2006) | 1 line

  Finish bringing SVN into line with latest version of PEP 343 by getting rid of all remaining references to context objects that I could find. Without a __context__() method context objects no longer exist. Also get test_with working again, and adopt a suggestion from Neal for decimal.Context.get_manager()
........
  r45888 | nick.coghlan | 2006-05-03 15:17:49 +0200 (Wed, 03 May 2006) | 1 line

  Get rid of a couple more context object references, fix some markup and clarify what happens when a generator context function swallows an exception.
........
  r45889 | georg.brandl | 2006-05-03 19:46:13 +0200 (Wed, 03 May 2006) | 3 lines

  Add seamonkey to list of Windows browsers too.
........
  r45890 | georg.brandl | 2006-05-03 20:03:22 +0200 (Wed, 03 May 2006) | 3 lines

  RFE #1472176: In httplib, don't encode the netloc and hostname with "idna" if not necessary.
........
  r45891 | georg.brandl | 2006-05-03 20:12:33 +0200 (Wed, 03 May 2006) | 2 lines

  Bug #1472191: convert breakpoint indices to ints before comparing them to ints
........
  r45893 | georg.brandl | 2006-05-03 20:18:32 +0200 (Wed, 03 May 2006) | 3 lines

  Bug #1385040: don't allow "def foo(a=1, b): pass" in the compiler package.
........
  r45894 | thomas.heller | 2006-05-03 20:35:39 +0200 (Wed, 03 May 2006) | 1 line

  Don't fail the tests when libglut.so or libgle.so cannot be loaded.
........
  r45895 | georg.brandl | 2006-05-04 07:08:10 +0200 (Thu, 04 May 2006) | 2 lines

  Bug #1481530: allow "from os.path import ..." with imputil
........
  r45897 | martin.v.loewis | 2006-05-04 07:51:03 +0200 (Thu, 04 May 2006) | 2 lines

  Patch #1475845: Raise IndentationError for unexpected indent.
........
  r45898 | martin.v.loewis | 2006-05-04 12:08:42 +0200 (Thu, 04 May 2006) | 1 line

  Implement os.{chdir,rename,rmdir,remove} using Win32 directly.
........
  r45899 | martin.v.loewis | 2006-05-04 14:04:27 +0200 (Thu, 04 May 2006) | 2 lines

  Drop now-unnecessary arguments to posix_2str.
........
  r45900 | martin.v.loewis | 2006-05-04 16:27:52 +0200 (Thu, 04 May 2006) | 1 line

  Update checks to consider Windows error numbers.
........
  r45913 | thomas.heller | 2006-05-05 20:42:14 +0200 (Fri, 05 May 2006) | 2 lines

  Export the 'free' standard C function for use in the test suite.
........
  r45914 | thomas.heller | 2006-05-05 20:43:24 +0200 (Fri, 05 May 2006) | 3 lines

  Fix memory leaks in the ctypes test suite, reported by valgrind, by
  free()ing the memory we allocate.
........
  r45915 | thomas.heller | 2006-05-05 20:46:27 +0200 (Fri, 05 May 2006) | 1 line

  oops - the function is exported as 'my_free', not 'free'.
........
  r45916 | thomas.heller | 2006-05-05 21:14:24 +0200 (Fri, 05 May 2006) | 2 lines

  Clean up.
........
  r45920 | george.yoshida | 2006-05-06 15:09:45 +0200 (Sat, 06 May 2006) | 2 lines

  describe optional arguments for DocFileSuite
........
  r45924 | george.yoshida | 2006-05-06 16:16:51 +0200 (Sat, 06 May 2006) | 2 lines

  Use \versionchanged for the feature change
........
  r45925 | martin.v.loewis | 2006-05-06 18:32:54 +0200 (Sat, 06 May 2006) | 1 line

  Port access, chmod, parts of getcwdu, mkdir, and utime to direct Win32 API.
........
  r45926 | martin.v.loewis | 2006-05-06 22:04:08 +0200 (Sat, 06 May 2006) | 2 lines

  Handle ERROR_ALREADY_EXISTS.
........
  r45931 | andrew.kuchling | 2006-05-07 19:12:12 +0200 (Sun, 07 May 2006) | 1 line

  [Patch #1479977] Revised version of urllib2 HOWTO, edited by John J. Lee
........
  r45932 | andrew.kuchling | 2006-05-07 19:14:53 +0200 (Sun, 07 May 2006) | 1 line

  Minor language edit
........
  r45934 | georg.brandl | 2006-05-07 22:44:34 +0200 (Sun, 07 May 2006) | 3 lines

  Patch #1483395: add new TLDs to cookielib
........
  r45936 | martin.v.loewis | 2006-05-08 07:25:56 +0200 (Mon, 08 May 2006) | 2 lines

  Add missing PyMem_Free.
........
  r45938 | georg.brandl | 2006-05-08 19:28:47 +0200 (Mon, 08 May 2006) | 3 lines

  Add test for rev. 45934.
........
  r45939 | georg.brandl | 2006-05-08 19:36:08 +0200 (Mon, 08 May 2006) | 3 lines

  Patch #1479302: Make urllib2 digest auth and basic auth play together.
........
  r45940 | georg.brandl | 2006-05-08 19:48:01 +0200 (Mon, 08 May 2006) | 3 lines

  Patch #1478993: take advantage of BaseException/Exception split in cookielib
........
  r45941 | neal.norwitz | 2006-05-09 07:38:56 +0200 (Tue, 09 May 2006) | 5 lines

  Micro optimization.  In the first case, we know that frame->f_exc_type
  is NULL, so there's no reason to do anything with it.  In the second case,
  we know frame->f_exc_type is not NULL, so we can just do an INCREF.
........
  r45943 | thomas.heller | 2006-05-09 22:20:15 +0200 (Tue, 09 May 2006) | 2 lines

  Disable a test that is unreliable.
........
  r45944 | tim.peters | 2006-05-10 04:43:01 +0200 (Wed, 10 May 2006) | 4 lines

  Variant of patch #1478292.  doctest.register_optionflag(name)
  shouldn't create a new flag when `name` is already the name of
  an option flag.
........
  r45947 | neal.norwitz | 2006-05-10 08:57:58 +0200 (Wed, 10 May 2006) | 14 lines

  Fix problems found by Coverity.

  longobject.c: also fix an ssize_t problem
    <a> could have been NULL, so hoist the size calc to not use <a>.

  _ssl.c: under fail: self is DECREF'd, but it would have been NULL.

  _elementtree.c: delete self if there was an error.

  _csv.c: I'm not sure if lineterminator could have been anything other than
  a string.  However, other string method calls are checked, so check this
  one too.
........
  r45948 | thomas.wouters | 2006-05-10 17:04:11 +0200 (Wed, 10 May 2006) | 4 lines


  Ignore reflog.txt, too.
........
  r45949 | georg.brandl | 2006-05-10 17:59:06 +0200 (Wed, 10 May 2006) | 3 lines

  Bug #1482988: indicate more prominently that the Stats class is in the pstats module.
........
  r45950 | georg.brandl | 2006-05-10 18:09:03 +0200 (Wed, 10 May 2006) | 2 lines

  Bug #1485447: subprocess: document that the "cwd" parameter isn't used to find the executable. Misc. other markup fixes.
........
  r45952 | georg.brandl | 2006-05-10 18:11:44 +0200 (Wed, 10 May 2006) | 2 lines

  Bug #1484978: curses.panel: clarify that Panel objects are destroyed on garbage collection.
........
  r45954 | georg.brandl | 2006-05-10 18:26:03 +0200 (Wed, 10 May 2006) | 4 lines

  Patch #1484695: Update the tarfile module to version 0.8. This fixes
  a couple of issues, notably handling of long file names using the
  GNU LONGNAME extension.
........
  r45955 | georg.brandl | 2006-05-10 19:13:20 +0200 (Wed, 10 May 2006) | 4 lines

  Patch #721464: pdb.Pdb instances can now be given explicit stdin and
  stdout arguments, making it possible to redirect input and output
  for remote debugging.
........
  r45956 | andrew.kuchling | 2006-05-10 19:19:04 +0200 (Wed, 10 May 2006) | 1 line

  Clarify description of exception handling
........
  r45957 | georg.brandl | 2006-05-10 22:09:23 +0200 (Wed, 10 May 2006) | 2 lines

  Fix two small errors in argument lists.
........
  r45960 | brett.cannon | 2006-05-11 07:11:33 +0200 (Thu, 11 May 2006) | 5 lines

  Detect if %zd is supported by printf() during configure and sets
  PY_FORMAT_SIZE_T appropriately.  Removes warnings on
  OS X under gcc 4.0.1 when PY_FORMAT_SIZE_T is set to "" instead of "z" as is
  needed.
........
  r45963 | neal.norwitz | 2006-05-11 09:51:59 +0200 (Thu, 11 May 2006) | 1 line

  Don't mask a no memory error with a less meaningful one as discussed on python-checkins
........
  r45964 | martin.v.loewis | 2006-05-11 15:28:43 +0200 (Thu, 11 May 2006) | 3 lines

  Change WindowsError to carry the Win32 error code in winerror,
  and the DOS error code in errno. Revert changes where
  WindowsError catch blocks unnecessarily special-case OSError.
........
  r45965 | george.yoshida | 2006-05-11 17:53:27 +0200 (Thu, 11 May 2006) | 2 lines

  Grammar fix
........
  r45967 | andrew.kuchling | 2006-05-11 18:32:24 +0200 (Thu, 11 May 2006) | 1 line

  typo fix
........
  r45968 | tim.peters | 2006-05-11 18:37:42 +0200 (Thu, 11 May 2006) | 5 lines

  BaseThreadedTestCase.setup():  stop special-casing WindowsError.

  Rev 45964 fiddled with WindowsError, and broke test_bsddb3 on all
  the Windows buildbot slaves as a result.  This should repair it.
........
  r45969 | georg.brandl | 2006-05-11 21:57:09 +0200 (Thu, 11 May 2006) | 2 lines

  Typo fix.
........
  r45970 | tim.peters | 2006-05-12 03:57:59 +0200 (Fri, 12 May 2006) | 5 lines

  SF patch #1473132:  Improve docs for tp_clear and tp_traverse,
  by Collin Winter.

  Bugfix candidate (but I'm not going to bother).
........
  r45974 | martin.v.loewis | 2006-05-12 14:27:28 +0200 (Fri, 12 May 2006) | 4 lines

  Dynamically allocate path name buffer for Unicode
  path name in listdir. Fixes #1431582.
  Stop overallocating MAX_PATH characters for ANSI
  path names. Stop assigning to errno.
........
  r45975 | martin.v.loewis | 2006-05-12 15:57:36 +0200 (Fri, 12 May 2006) | 1 line

  Move icon files into DLLs dir. Fixes #1477968.
........
  r45976 | george.yoshida | 2006-05-12 18:40:11 +0200 (Fri, 12 May 2006) | 2 lines

  At first there were 6 steps, but one was removed after that.
........
  r45977 | martin.v.loewis | 2006-05-12 19:22:04 +0200 (Fri, 12 May 2006) | 1 line

  Fix alignment error on Itanium.
........
  r45978 | george.yoshida | 2006-05-12 19:25:26 +0200 (Fri, 12 May 2006) | 3 lines

  Duplicated description about the illegal continue usage can be found in nearly the same place.
  They are same, so keep the original one and remove the later-added one.
........
  r45980 | thomas.heller | 2006-05-12 20:16:03 +0200 (Fri, 12 May 2006) | 2 lines

  Add missing svn properties.
........
  r45981 | thomas.heller | 2006-05-12 20:47:35 +0200 (Fri, 12 May 2006) | 1 line

  set svn properties
........
  r45982 | thomas.heller | 2006-05-12 21:31:46 +0200 (Fri, 12 May 2006) | 1 line

  add svn:eol-style native svn:keywords Id
........
  r45987 | gerhard.haering | 2006-05-13 01:49:49 +0200 (Sat, 13 May 2006) | 3 lines

  Integrated the rest of the pysqlite reference manual into the Python
  documentation. Ready to be reviewed and improved upon.
........
  r45988 | george.yoshida | 2006-05-13 08:53:31 +0200 (Sat, 13 May 2006) | 2 lines

  Add \exception markup
........
  r45990 | martin.v.loewis | 2006-05-13 15:34:04 +0200 (Sat, 13 May 2006) | 2 lines

  Revert 43315: Printing of %zd must be signed.
........
  r45992 | tim.peters | 2006-05-14 01:28:20 +0200 (Sun, 14 May 2006) | 11 lines

  Teach PyString_FromFormat, PyErr_Format, and PyString_FromFormatV
  about "%u", "%lu" and "%zu" formats.

  Since PyString_FromFormat and PyErr_Format have exactly the same rules
  (both inherited from PyString_FromFormatV), it would be good if someone
  with more LaTeX Fu changed one of them to just point to the other.
  Their docs were way out of synch before this patch, and I just did a
  mass copy+paste to repair that.

  Not a backport candidate (this is a new feature).
........
  r45993 | tim.peters | 2006-05-14 01:31:05 +0200 (Sun, 14 May 2006) | 2 lines

  Typo repair.
........
  r45994 | tim.peters | 2006-05-14 01:33:19 +0200 (Sun, 14 May 2006) | 2 lines

  Remove lie in new comment.
........
  r45995 | ronald.oussoren | 2006-05-14 21:56:34 +0200 (Sun, 14 May 2006) | 11 lines

  Rework the build system for osx applications:

  * Don't use xcodebuild for building PythonLauncher, but use a normal unix
    makefile. This makes it a lot easier to use the same build flags as for the
    rest of python (e.g. make a universal version of python launcher)
  * Convert the mac makefile-s to makefile.in-s and use configure to set makefile
    variables instead of forwarding them as command-line arguments
  * Add a C version of pythonw, that we you can use '#!/usr/local/bin/pythonw'
  * Build IDLE.app using bundlebuilder instead of BuildApplet, that will allow
    easier modification of the bundle contents later on.
........
  r45996 | ronald.oussoren | 2006-05-14 22:35:41 +0200 (Sun, 14 May 2006) | 6 lines

  A first cut at replacing the icons on MacOS X. This replaces all icons by icons
  based on the new python.org logo. These are also the first icons that are
  "proper" OSX icons.

  These icons were created by Jacob Rus.
........
  r45997 | ronald.oussoren | 2006-05-14 23:07:41 +0200 (Sun, 14 May 2006) | 3 lines

  I missed one small detail in my rewrite of the osx build files: the path
  to the Python.app template.
........
  r45998 | martin.v.loewis | 2006-05-15 07:51:36 +0200 (Mon, 15 May 2006) | 2 lines

  Fix memory leak.
........
  r45999 | neal.norwitz | 2006-05-15 08:48:14 +0200 (Mon, 15 May 2006) | 1 line

  Move items implemented after a2 into the new a3 section
........
  r46000 | neal.norwitz | 2006-05-15 09:04:36 +0200 (Mon, 15 May 2006) | 5 lines

  - Bug #1487966: Fix SystemError with conditional expression in assignment

  Most of the test_syntax changes are just updating the numbers.
........
  r46001 | neal.norwitz | 2006-05-15 09:17:23 +0200 (Mon, 15 May 2006) | 1 line

  Patch #1488312, Fix memory alignment problem on SPARC in unicode.  Will backport
........
  r46003 | martin.v.loewis | 2006-05-15 11:22:27 +0200 (Mon, 15 May 2006) | 3 lines

  Remove bogus DECREF of self.
  Change __str__() functions to METH_O.
  Change WindowsError__str__ to use PyTuple_Pack.
........
  r46005 | georg.brandl | 2006-05-15 21:30:35 +0200 (Mon, 15 May 2006) | 3 lines

  [ 1488881 ] tarfile.py: support for file-objects and bz2 (cp. #1488634)
........
  r46007 | tim.peters | 2006-05-15 22:44:10 +0200 (Mon, 15 May 2006) | 9 lines

  ReadDetectFileobjTest:  repair Windows disasters by opening
  the file object in binary mode.

  The Windows buildbot slaves shouldn't swap themselves to death
  anymore.  However, test_tarfile may still fail because of a
  temp directory left behind from a previous failing run.
  Windows buildbot owners may need to remove that directory
  by hand.
........
  r46009 | tim.peters | 2006-05-15 23:32:25 +0200 (Mon, 15 May 2006) | 3 lines

  test_directory():  Remove the leftover temp directory that's making
  the Windows buildbots fail test_tarfile.
........
  r46010 | martin.v.loewis | 2006-05-16 09:05:37 +0200 (Tue, 16 May 2006) | 4 lines

  - Test for sys/statvfs.h before including it, as statvfs is present
    on some OSX installation, but its header file is not.
  Will backport to 2.4
........
  r46012 | georg.brandl | 2006-05-16 09:38:27 +0200 (Tue, 16 May 2006) | 3 lines

  Patch #1435422: zlib's compress and decompress objects now have a
  copy() method.
........
  r46015 | andrew.kuchling | 2006-05-16 18:11:54 +0200 (Tue, 16 May 2006) | 1 line

  Add item
........
  r46016 | andrew.kuchling | 2006-05-16 18:27:31 +0200 (Tue, 16 May 2006) | 3 lines

  PEP 243 has been withdrawn, so don't refer to it any more.
  The PyPI upload material has been moved into the section on PEP314.
........
  r46017 | george.yoshida | 2006-05-16 19:42:16 +0200 (Tue, 16 May 2006) | 2 lines

  Update for 'ImportWarning'
........
  r46018 | george.yoshida | 2006-05-16 20:07:00 +0200 (Tue, 16 May 2006) | 4 lines

  Mention that Exception is now a subclass of BaseException.
  Remove a sentence that says that BaseException inherits from BaseException.
  (I guess this is just a copy & paste mistake.)
........
  r46019 | george.yoshida | 2006-05-16 20:26:10 +0200 (Tue, 16 May 2006) | 2 lines

  Document ImportWarning
........
  r46020 | tim.peters | 2006-05-17 01:22:20 +0200 (Wed, 17 May 2006) | 2 lines

  Whitespace normalization.
........
  r46021 | tim.peters | 2006-05-17 01:24:08 +0200 (Wed, 17 May 2006) | 2 lines

  Text files missing the SVN eol-style property.
........
  r46022 | tim.peters | 2006-05-17 03:30:11 +0200 (Wed, 17 May 2006) | 2 lines

  PyZlib_copy(), PyZlib_uncopy():  Repair leaks on the normal-case path.
........
  r46023 | georg.brandl | 2006-05-17 16:06:07 +0200 (Wed, 17 May 2006) | 3 lines

  Remove misleading comment about type-class unification.
........
  r46024 | georg.brandl | 2006-05-17 16:11:36 +0200 (Wed, 17 May 2006) | 3 lines

  Apply patch #1489784 from Michael Foord.
........
  r46025 | georg.brandl | 2006-05-17 16:18:20 +0200 (Wed, 17 May 2006) | 3 lines

  Fix typo in os.utime docstring (patch #1490189)
........
  r46026 | georg.brandl | 2006-05-17 16:26:50 +0200 (Wed, 17 May 2006) | 3 lines

  Patch #1490224: set time.altzone correctly on Cygwin.
........
  r46027 | georg.brandl | 2006-05-17 16:45:06 +0200 (Wed, 17 May 2006) | 4 lines

  Add global debug flag to cookielib to avoid heavy dependency on the logging module.
  Resolves #1484758.
........
  r46028 | georg.brandl | 2006-05-17 16:56:04 +0200 (Wed, 17 May 2006) | 3 lines

  Patch #1486962: Several bugs in the turtle Tk demo module were fixed
  and several features added, such as speed and geometry control.
........
  r46029 | georg.brandl | 2006-05-17 17:17:00 +0200 (Wed, 17 May 2006) | 4 lines

  Delay-import some large modules to speed up urllib2 import.
  (fixes #1484793).
........
  r46030 | georg.brandl | 2006-05-17 17:51:16 +0200 (Wed, 17 May 2006) | 3 lines

  Patch #1180296: improve locale string formatting functions
........
  r46032 | tim.peters | 2006-05-18 04:06:40 +0200 (Thu, 18 May 2006) | 2 lines

  Whitespace normalization.
........
  r46033 | georg.brandl | 2006-05-18 08:11:19 +0200 (Thu, 18 May 2006) | 3 lines

  Amendments to patch #1484695.
........
  r46034 | georg.brandl | 2006-05-18 08:18:06 +0200 (Thu, 18 May 2006) | 3 lines

  Remove unused import.
........
  r46035 | georg.brandl | 2006-05-18 08:33:27 +0200 (Thu, 18 May 2006) | 3 lines

  Fix test_locale for platforms without a default thousands separator.
........
  r46036 | neal.norwitz | 2006-05-18 08:51:46 +0200 (Thu, 18 May 2006) | 1 line

  Little cleanup
........
  r46037 | georg.brandl | 2006-05-18 09:01:27 +0200 (Thu, 18 May 2006) | 4 lines

  Bug #1462152: file() now checks more thoroughly for invalid mode
  strings and removes a possible "U" before passing the mode to the
  C library function.
........
  r46038 | georg.brandl | 2006-05-18 09:20:05 +0200 (Thu, 18 May 2006) | 3 lines

  Bug #1490688: properly document %e, %f, %g format subtleties.
........
  r46039 | vinay.sajip | 2006-05-18 09:28:58 +0200 (Thu, 18 May 2006) | 1 line

  Changed status from "beta" to "production"; since logging has been part of the stdlib since 2.3, it should be safe to make this assertion ;-)
........
  r46040 | ronald.oussoren | 2006-05-18 11:04:15 +0200 (Thu, 18 May 2006) | 2 lines

  Fix some minor issues with the generated application bundles on MacOSX
........
  r46041 | andrew.kuchling | 2006-05-19 02:03:55 +0200 (Fri, 19 May 2006) | 1 line

  Typo fix; add clarifying word
........
  r46044 | neal.norwitz | 2006-05-19 08:31:23 +0200 (Fri, 19 May 2006) | 3 lines

  Fix #132 from Coverity, retval could have been derefed
  if a continue inside a try failed.
........
  r46045 | neal.norwitz | 2006-05-19 08:43:50 +0200 (Fri, 19 May 2006) | 2 lines

  Fix #1474677, non-keyword argument following keyword.
........
  r46046 | neal.norwitz | 2006-05-19 09:00:58 +0200 (Fri, 19 May 2006) | 4 lines

  Bug/Patch #1481770: Use .so extension for shared libraries on HP-UX for ia64.

  I suppose this could be backported if anyone cares.
........
  r46047 | neal.norwitz | 2006-05-19 09:05:01 +0200 (Fri, 19 May 2006) | 7 lines

  Oops, I forgot to include this file in the last commit (46046):

  Bug/Patch #1481770: Use .so extension for shared libraries on HP-UX for ia64.

  I suppose this could be backported if anyone cares.
........
  r46050 | ronald.oussoren | 2006-05-19 20:17:31 +0200 (Fri, 19 May 2006) | 6 lines

  * Change working directory to the users home
    directory, that makes the file open/save
    dialogs more useable.
  * Don't use argv emulator, its not needed
    for idle.
........
  r46052 | tim.peters | 2006-05-19 21:16:34 +0200 (Fri, 19 May 2006) | 2 lines

  Whitespace normalization.
........
  r46054 | ronald.oussoren | 2006-05-20 08:17:01 +0200 (Sat, 20 May 2006) | 9 lines

  Fix bug #1000914 (again).

  This patches a file that is generated by bgen, however the code is now the
  same as a current copy of bgen would generate.  Without this patch most types
  in the Carbon.CF module are unusable.

  I haven't managed to coax bgen into generating a complete copy of _CFmodule.c
  yet :-(, hence the manual patching.
........
  r46055 | george.yoshida | 2006-05-20 17:36:19 +0200 (Sat, 20 May 2006) | 3 lines

  - markup fix
  - add clarifying words
........
  r46057 | george.yoshida | 2006-05-20 18:29:14 +0200 (Sat, 20 May 2006) | 3 lines

  - Add 'as' and 'with' as new keywords in 2.5.
  - Regenerate keyword lists with reswords.py.
........
  r46058 | george.yoshida | 2006-05-20 20:07:26 +0200 (Sat, 20 May 2006) | 2 lines

  Apply patch #1492147 from Mike Foord.
........
  r46059 | andrew.kuchling | 2006-05-20 21:25:16 +0200 (Sat, 20 May 2006) | 1 line

  Minor edits
........
  r46061 | george.yoshida | 2006-05-21 06:22:59 +0200 (Sun, 21 May 2006) | 2 lines

  Fix the TeX compile error.
........
  r46062 | george.yoshida | 2006-05-21 06:40:32 +0200 (Sun, 21 May 2006) | 2 lines

  Apply patch #1492255 from Mike Foord.
........
  r46063 | martin.v.loewis | 2006-05-22 10:48:14 +0200 (Mon, 22 May 2006) | 1 line

  Patch 1490384: New Icons for the PC build.
........
  r46064 | martin.v.loewis | 2006-05-22 11:15:18 +0200 (Mon, 22 May 2006) | 1 line

  Patch #1492356: Port to Windows CE (patch set 1).
........
  r46065 | tim.peters | 2006-05-22 13:29:41 +0200 (Mon, 22 May 2006) | 4 lines

  Define SIZEOF_{DOUBLE,FLOAT} on Windows.  Else
  Michael Hudson's nice gimmicks for IEEE special
  values (infinities, NaNs) don't work.
........
  r46070 | bob.ippolito | 2006-05-22 16:31:24 +0200 (Mon, 22 May 2006) | 2 lines

  GzipFile.readline performance improvement (~30-40%), patch #1281707
........
  r46071 | bob.ippolito | 2006-05-22 17:22:46 +0200 (Mon, 22 May 2006) | 1 line

  Revert gzip readline performance patch #1281707 until a more generic performance improvement can be found
........
  r46073 | fredrik.lundh | 2006-05-22 17:35:12 +0200 (Mon, 22 May 2006) | 4 lines

  docstring tweaks: count counts non-overlapping substrings, not
  total number of occurences
........
  r46075 | bob.ippolito | 2006-05-22 17:59:12 +0200 (Mon, 22 May 2006) | 1 line

  Apply revised patch for GzipFile.readline performance #1281707
........
  r46076 | fredrik.lundh | 2006-05-22 18:29:30 +0200 (Mon, 22 May 2006) | 3 lines

  needforspeed: speed up unicode repeat, unicode string copy
........
  r46079 | fredrik.lundh | 2006-05-22 19:12:58 +0200 (Mon, 22 May 2006) | 4 lines

  needforspeed: use memcpy for "long" strings; use a better algorithm
  for long repeats.
........
  r46084 | tim.peters | 2006-05-22 21:17:04 +0200 (Mon, 22 May 2006) | 7 lines

  PyUnicode_Join():  Recent code changes introduced new
  compiler warnings on Windows (signed vs unsigned mismatch
  in comparisons).  Cleaned that up by switching more locals
  to Py_ssize_t.  Simplified overflow checking (it can _be_
  simpler because while these things are declared as
  Py_ssize_t, then should in fact never be negative).
........
  r46085 | tim.peters | 2006-05-23 07:47:16 +0200 (Tue, 23 May 2006) | 3 lines

  unicode_repeat():  Change type of local to Py_ssize_t,
  since that's what it should be.
........
  r46094 | fredrik.lundh | 2006-05-23 12:10:57 +0200 (Tue, 23 May 2006) | 3 lines

  needforspeed: check first *and* last character before doing a full memcmp
........
  r46095 | fredrik.lundh | 2006-05-23 12:12:21 +0200 (Tue, 23 May 2006) | 4 lines

  needforspeed: fixed unicode "in" operator to use same implementation
  approach as find/index
........
  r46096 | richard.jones | 2006-05-23 12:37:38 +0200 (Tue, 23 May 2006) | 7 lines

  Merge from rjones-funccall branch.

  Applied patch zombie-frames-2.diff from sf patch 876206 with updates for
  Python 2.5 and also modified to retain the free_list to avoid the 67%
  slow-down in pybench recursion test. 5% speed up in function call pybench.
........
  r46098 | ronald.oussoren | 2006-05-23 13:04:24 +0200 (Tue, 23 May 2006) | 2 lines

  Avoid creating a mess when installing a framework for the second time.
........
  r46101 | georg.brandl | 2006-05-23 13:17:21 +0200 (Tue, 23 May 2006) | 3 lines

  PyErr_NewException now accepts a tuple of base classes as its
  "base" parameter.
........
  r46103 | ronald.oussoren | 2006-05-23 13:47:16 +0200 (Tue, 23 May 2006) | 3 lines

  Disable linking extensions with -lpython2.5 for darwin. This should fix bug
  #1487105.
........
  r46104 | ronald.oussoren | 2006-05-23 14:01:11 +0200 (Tue, 23 May 2006) | 6 lines

  Patch #1488098.

  This patchs makes it possible to create a universal build on OSX 10.4 and use
  the result to build extensions on 10.3. It also makes it possible to override
  the '-arch' and '-isysroot' compiler arguments for specific extensions.
........
  r46108 | andrew.kuchling | 2006-05-23 14:44:36 +0200 (Tue, 23 May 2006) | 1 line

  Add some items; mention the sprint
........
  r46109 | andrew.kuchling | 2006-05-23 14:47:01 +0200 (Tue, 23 May 2006) | 1 line

  Mention string improvements
........
  r46110 | andrew.kuchling | 2006-05-23 14:49:35 +0200 (Tue, 23 May 2006) | 4 lines

  Use 'speed' instead of 'performance', because I agree with the argument
  at http://zestyping.livejournal.com/193260.html that 'erformance' really means
  something more general.
........
  r46113 | ronald.oussoren | 2006-05-23 17:09:57 +0200 (Tue, 23 May 2006) | 2 lines

  An improved script for building the binary distribution on MacOSX.
........
  r46128 | richard.jones | 2006-05-23 20:28:17 +0200 (Tue, 23 May 2006) | 3 lines

  Applied patch 1337051 by Neal Norwitz, saving 4 ints on frame objects.
........
  r46129 | richard.jones | 2006-05-23 20:32:11 +0200 (Tue, 23 May 2006) | 1 line

  fix broken merge
........
  r46130 | bob.ippolito | 2006-05-23 20:41:17 +0200 (Tue, 23 May 2006) | 1 line

  Update Misc/NEWS for gzip patch #1281707
........
  r46131 | bob.ippolito | 2006-05-23 20:43:47 +0200 (Tue, 23 May 2006) | 1 line

  Update Misc/NEWS for gzip patch #1281707
........
  r46132 | fredrik.lundh | 2006-05-23 20:44:25 +0200 (Tue, 23 May 2006) | 7 lines

  needforspeed: use append+reverse for rsplit, use "bloom filters" to
  speed up splitlines and strip with charsets; etc.  rsplit is now as
  fast as split in all our tests (reverse takes no time at all), and
  splitlines() is nearly as fast as a plain split("\n") in our tests.
  and we're not done yet... ;-)
........
  r46133 | tim.peters | 2006-05-23 20:45:30 +0200 (Tue, 23 May 2006) | 38 lines

  Bug #1334662 / patch #1335972:  int(string, base) wrong answers.

  In rare cases of strings specifying true values near sys.maxint,
  and oddball bases (not decimal or a power of 2), int(string, base)
  could deliver insane answers.  This repairs all such problems, and
  also speeds string->int significantly.  On my box, here are %
  speedups for decimal strings of various lengths:

  length speedup
  ------ -------
   1       12.4%
   2       15.7%
   3       20.6%
   4       28.1%
   5       33.2%
   6       37.5%
   7       41.9%
   8       46.3%
   9       51.2%
  10       19.5%
  11       19.9%
  12       23.9%
  13       23.7%
  14       23.3%
  15       24.9%
  16       25.3%
  17       28.3%
  18       27.9%
  19       35.7%

  Note that the difference between 9 and 10 is the difference between
  short and long Python ints on a 32-bit box.  The patch doesn't
  actually do anything to speed conversion to long:  the speedup is
  due to detecting "unsigned long" overflow more quickly.

  This is a bugfix candidate, but it's a non-trivial patch and it
  would be painful to separate the "bug fix" from the "speed up" parts.
........
  r46134 | bob.ippolito | 2006-05-23 20:46:41 +0200 (Tue, 23 May 2006) | 1 line

  Patch #1493701: performance enhancements for struct module.
........
  r46136 | andrew.kuchling | 2006-05-23 21:00:45 +0200 (Tue, 23 May 2006) | 1 line

  Remove duplicate item
........
  r46141 | bob.ippolito | 2006-05-23 21:09:51 +0200 (Tue, 23 May 2006) | 1 line

  revert #1493701
........
  r46142 | bob.ippolito | 2006-05-23 21:11:34 +0200 (Tue, 23 May 2006) | 1 line

  patch #1493701: performance enhancements for struct module
........
  r46144 | bob.ippolito | 2006-05-23 21:12:41 +0200 (Tue, 23 May 2006) | 1 line

  patch #1493701: performance enhancements for struct module
........
  r46148 | bob.ippolito | 2006-05-23 21:25:52 +0200 (Tue, 23 May 2006) | 1 line

  fix linking issue, warnings, in struct
........
  r46149 | andrew.kuchling | 2006-05-23 21:29:38 +0200 (Tue, 23 May 2006) | 1 line

  Add two items
........
  r46150 | bob.ippolito | 2006-05-23 21:31:23 +0200 (Tue, 23 May 2006) | 1 line

  forward declaration for PyStructType
........
  r46151 | bob.ippolito | 2006-05-23 21:32:25 +0200 (Tue, 23 May 2006) | 1 line

  fix typo in _struct
........
  r46152 | andrew.kuchling | 2006-05-23 21:32:35 +0200 (Tue, 23 May 2006) | 1 line

  Add item
........
  r46153 | tim.peters | 2006-05-23 21:34:37 +0200 (Tue, 23 May 2006) | 3 lines

  Get the Windows build working again (recover from
  `struct` module changes).
........
  r46155 | fredrik.lundh | 2006-05-23 21:47:35 +0200 (Tue, 23 May 2006) | 3 lines

  return 0 on misses, not -1.
........
  r46156 | tim.peters | 2006-05-23 23:51:35 +0200 (Tue, 23 May 2006) | 4 lines

  test_struct grew weird behavior under regrtest.py -R,
  due to a module-level cache.  Clearing the cache should
  make it stop showing up in refleak reports.
........
  r46157 | tim.peters | 2006-05-23 23:54:23 +0200 (Tue, 23 May 2006) | 2 lines

  Whitespace normalization.
........
  r46158 | tim.peters | 2006-05-23 23:55:53 +0200 (Tue, 23 May 2006) | 2 lines

  Add missing svn:eol-style property to text files.
........
  r46161 | fredrik.lundh | 2006-05-24 12:20:36 +0200 (Wed, 24 May 2006) | 3 lines

  use Py_ssize_t for string indexes (thanks, neal!)
........
  r46173 | fredrik.lundh | 2006-05-24 16:28:11 +0200 (Wed, 24 May 2006) | 14 lines

  needforspeed: use "fastsearch" for count and findstring helpers.  this
  results in a 2.5x speedup on the stringbench count tests, and a 20x (!)
  speedup on the stringbench search/find/contains test, compared to 2.5a2.

  for more on the algorithm, see:

      http://effbot.org/zone/stringlib.htm

  if you get weird results, you can disable the new algoritm by undefining
  USE_FAST in Objects/unicodeobject.c.

  enjoy /F
........
  r46182 | fredrik.lundh | 2006-05-24 17:11:01 +0200 (Wed, 24 May 2006) | 3 lines

  needforspeedindeed: use fastsearch also for __contains__
........
  r46184 | bob.ippolito | 2006-05-24 17:32:06 +0200 (Wed, 24 May 2006) | 1 line

  refactor unpack, add unpack_from
........
  r46189 | fredrik.lundh | 2006-05-24 18:35:18 +0200 (Wed, 24 May 2006) | 4 lines

  needforspeed: refactored the replace code slightly; special-case
  constant-length changes; use fastsearch to locate the first match.
........
  r46198 | andrew.dalke | 2006-05-24 20:55:37 +0200 (Wed, 24 May 2006) | 10 lines

  Added a slew of test for string replace, based various corner cases from
  the Need For Speed sprint coding.  Includes commented out overflow tests
  which will be uncommented once the code is fixed.

  This test will break the 8-bit string tests because
      "".replace("", "A") == "" when it should == "A"

  We have a fix for it, which should be added tomorrow.
........
  r46200 | tim.peters | 2006-05-24 22:27:18 +0200 (Wed, 24 May 2006) | 2 lines

  We can't leave the checked-in tests broken.
........
  r46201 | tim.peters | 2006-05-24 22:29:44 +0200 (Wed, 24 May 2006) | 2 lines

  Whitespace normalization.
........
  r46202 | tim.peters | 2006-05-24 23:00:45 +0200 (Wed, 24 May 2006) | 4 lines

  Disable the damn empty-string replace test -- it can't
  be make to pass now for unicode if it passes for str, or
  vice versa.
........
  r46203 | tim.peters | 2006-05-24 23:10:40 +0200 (Wed, 24 May 2006) | 58 lines

  Heavily fiddled variant of patch #1442927: PyLong_FromString optimization.

  ``long(str, base)`` is now up to 6x faster for non-power-of-2 bases.  The
  largest speedup is for inputs with about 1000 decimal digits.  Conversion
  from non-power-of-2 bases remains quadratic-time in the number of input
  digits (it was and remains linear-time for bases 2, 4, 8, 16 and 32).

  Speedups at various lengths for decimal inputs, comparing 2.4.3 with
  current trunk.  Note that it's actually a bit slower for 1-digit strings:

    len  speedup
   ----  -------
     1     -4.5%
     2      4.6%
     3      8.3%
     4     12.7%
     5     16.9%
     6     28.6%
     7     35.5%
     8     44.3%
     9     46.6%
    10     55.3%
    11     65.7%
    12     77.7%
    13     73.4%
    14     75.3%
    15     85.2%
    16    103.0%
    17     95.1%
    18    112.8%
    19    117.9%
    20    128.3%
    30    174.5%
    40    209.3%
    50    236.3%
    60    254.3%
    70    262.9%
    80    295.8%
    90    297.3%
   100    324.5%
   200    374.6%
   300    403.1%
   400    391.1%
   500    388.7%
   600    440.6%
   700    468.7%
   800    498.0%
   900    507.2%
  1000    501.2%
  2000    450.2%
  3000    463.2%
  4000    452.5%
  5000    440.6%
  6000    439.6%
  7000    424.8%
  8000    418.1%
  9000    417.7%
........
  r46204 | andrew.kuchling | 2006-05-25 02:23:03 +0200 (Thu, 25 May 2006) | 1 line

  Minor edits; add an item
........
  r46205 | fred.drake | 2006-05-25 04:42:25 +0200 (Thu, 25 May 2006) | 3 lines

  fix broken links in PDF
  (SF patch #1281291, contributed by Rory Yorke)
........
  r46208 | walter.doerwald | 2006-05-25 10:53:28 +0200 (Thu, 25 May 2006) | 2 lines

  Replace tab inside comment with space.
........
  r46209 | thomas.wouters | 2006-05-25 13:25:51 +0200 (Thu, 25 May 2006) | 4 lines


  Fix #1488915, Multiple dots in relative import statement raise SyntaxError.

........
  r46210 | thomas.wouters | 2006-05-25 13:26:25 +0200 (Thu, 25 May 2006) | 5 lines


  Update graminit.c for the fix for #1488915, Multiple dots in relative import
  statement raise SyntaxError, and add testcase.
........
  r46211 | andrew.kuchling | 2006-05-25 14:27:59 +0200 (Thu, 25 May 2006) | 1 line

  Add entry; and fix a typo
........
  r46214 | fredrik.lundh | 2006-05-25 17:22:03 +0200 (Thu, 25 May 2006) | 7 lines

  needforspeed: speed up upper and lower for 8-bit string objects.
  (the unicode versions of these are still 2x faster on windows,
  though...)

  based on work by Andrew Dalke, with tweaks by yours truly.
........
  r46216 | fredrik.lundh | 2006-05-25 17:49:45 +0200 (Thu, 25 May 2006) | 5 lines

  needforspeed: make new upper/lower work properly for single-character
  strings too... (thanks to georg brandl for spotting the exact problem
  faster than anyone else)
........
  r46217 | kristjan.jonsson | 2006-05-25 17:53:30 +0200 (Thu, 25 May 2006) | 1 line

  Added a new macro, Py_IS_FINITE(X).  On windows there is an intrinsic for this and it is more efficient than to use !Py_IS_INFINITE(X) && !Py_IS_NAN(X).  No change on other platforms
........
  r46219 | fredrik.lundh | 2006-05-25 18:10:12 +0200 (Thu, 25 May 2006) | 4 lines

  needforspeed: _toupper/_tolower is a SUSv2 thing; fall back on ISO C
  versions if they're not defined.
........
  r46220 | andrew.kuchling | 2006-05-25 18:23:15 +0200 (Thu, 25 May 2006) | 1 line

  Fix comment typos
........
  r46221 | andrew.dalke | 2006-05-25 18:30:52 +0200 (Thu, 25 May 2006) | 2 lines

  Added tests for implementation error we came up with in the need for speed sprint.
........
  r46222 | andrew.kuchling | 2006-05-25 18:34:54 +0200 (Thu, 25 May 2006) | 1 line

  Fix another typo
........
  r46223 | kristjan.jonsson | 2006-05-25 18:39:27 +0200 (Thu, 25 May 2006) | 1 line

  Fix incorrect documentation for the Py_IS_FINITE(X) macro.
........
  r46224 | fredrik.lundh | 2006-05-25 18:46:54 +0200 (Thu, 25 May 2006) | 3 lines

  needforspeed: check for overflow in replace (from Andrew Dalke)
........
  r46226 | fredrik.lundh | 2006-05-25 19:08:14 +0200 (Thu, 25 May 2006) | 5 lines

  needforspeed: new replace implementation by Andrew Dalke.  replace is
  now about 3x faster on my machine, for the replace tests from string-
  bench.
........
  r46227 | tim.peters | 2006-05-25 19:34:03 +0200 (Thu, 25 May 2006) | 5 lines

  A new table to help string->integer conversion was added yesterday to
  both mystrtoul.c and longobject.c.  Share the table instead.  Also
  cut its size by 64 entries (they had been used for an inscrutable
  trick originally, but the code no longer tries to use that trick).
........
  r46229 | andrew.dalke | 2006-05-25 19:53:00 +0200 (Thu, 25 May 2006) | 11 lines

  Fixed problem identified by Georg.  The special-case in-place code for replace
  made a copy of the string using PyString_FromStringAndSize(s, n) and modify
  the copied string in-place.  However, 1 (and 0) character strings are shared
  from a cache.  This cause "A".replace("A", "a") to change the cached version
  of "A" -- used by everyone.

  Now may the copy with NULL as the string and do the memcpy manually.  I've
  added regression tests to check if this happens in the future.  Perhaps
  there should be a PyString_Copy for this case?
........
  r46230 | fredrik.lundh | 2006-05-25 19:55:31 +0200 (Thu, 25 May 2006) | 4 lines

  needforspeed: use "fastsearch" for count.  this results in a 3x speedup
  for the related stringbench tests.
........
  r46231 | andrew.dalke | 2006-05-25 20:03:25 +0200 (Thu, 25 May 2006) | 4 lines

  Code had returned an ssize_t, upcast to long, then converted with PyInt_FromLong.
  Now using PyInt_FromSsize_t.
........
  r46233 | andrew.kuchling | 2006-05-25 20:11:16 +0200 (Thu, 25 May 2006) | 1 line

  Comment typo
........
  r46234 | andrew.dalke | 2006-05-25 20:18:39 +0200 (Thu, 25 May 2006) | 4 lines

  Added overflow test for adding two (very) large strings where the
  new string is over max Py_ssize_t.  I have no way to test it on my
  box or any box I have access to.  At least it doesn't break anything.
........
  r46235 | bob.ippolito | 2006-05-25 20:20:23 +0200 (Thu, 25 May 2006) | 1 line

  Faster path for PyLong_FromLongLong, using PyLong_FromLong algorithm
........
  r46238 | georg.brandl | 2006-05-25 20:44:09 +0200 (Thu, 25 May 2006) | 3 lines

  Guard the _active.remove() call to avoid errors when there is no _active list.
........
  r46239 | fredrik.lundh | 2006-05-25 20:44:29 +0200 (Thu, 25 May 2006) | 4 lines

  needforspeed: use fastsearch also for find/index and contains.  the
  related tests are now about 10x faster.
........
  r46240 | bob.ippolito | 2006-05-25 20:44:50 +0200 (Thu, 25 May 2006) | 1 line

  Struct now unpacks to PY_LONG_LONG directly when possible, also include #ifdef'ed out code that will return int instead of long when in bounds (not active since it's an API and doc change)
........
  r46241 | jack.diederich | 2006-05-25 20:47:15 +0200 (Thu, 25 May 2006) | 1 line

  * eliminate warning by reverting tmp_s type to 'const char*'
........
  r46242 | bob.ippolito | 2006-05-25 21:03:19 +0200 (Thu, 25 May 2006) | 1 line

  Fix Cygwin compiler issue
........
  r46243 | bob.ippolito | 2006-05-25 21:15:27 +0200 (Thu, 25 May 2006) | 1 line

  fix a struct regression where long would be returned for short unsigned integers
........
  r46244 | georg.brandl | 2006-05-25 21:15:31 +0200 (Thu, 25 May 2006) | 4 lines

  Replace PyObject_CallFunction calls with only object args
  with PyObject_CallFunctionObjArgs, which is 30% faster.
........
  r46245 | fredrik.lundh | 2006-05-25 21:19:05 +0200 (Thu, 25 May 2006) | 3 lines

  needforspeed: use insert+reverse instead of append
........
  r46246 | bob.ippolito | 2006-05-25 21:33:38 +0200 (Thu, 25 May 2006) | 1 line

  Use LONG_MIN and LONG_MAX to check Python integer bounds instead of the incorrect INT_MIN and INT_MAX
........
  r46248 | bob.ippolito | 2006-05-25 21:56:56 +0200 (Thu, 25 May 2006) | 1 line

  Use faster struct pack/unpack functions for the endian table that matches the host's
........
  r46249 | bob.ippolito | 2006-05-25 21:59:56 +0200 (Thu, 25 May 2006) | 1 line

  enable darwin/x86 support for libffi and hence ctypes (doesn't yet support --enable-universalsdk)
........
  r46252 | georg.brandl | 2006-05-25 22:28:10 +0200 (Thu, 25 May 2006) | 4 lines

  Someone seems to just have copy-pasted the docs of
  tp_compare to tp_richcompare ;)
........
  r46253 | brett.cannon | 2006-05-25 22:44:08 +0200 (Thu, 25 May 2006) | 2 lines

  Swap out bare malloc()/free() use for PyMem_MALLOC()/PyMem_FREE() .
........
  r46254 | bob.ippolito | 2006-05-25 22:52:38 +0200 (Thu, 25 May 2006) | 1 line

  squelch gcc4 darwin/x86 compiler warnings
........
  r46255 | bob.ippolito | 2006-05-25 23:09:45 +0200 (Thu, 25 May 2006) | 1 line

  fix test_float regression and 64-bit size mismatch issue
........
  r46256 | georg.brandl | 2006-05-25 23:11:56 +0200 (Thu, 25 May 2006) | 3 lines

  Add a x-ref to newer calling APIs.
........
  r46257 | ronald.oussoren | 2006-05-25 23:30:54 +0200 (Thu, 25 May 2006) | 2 lines

  Fix minor typo in prep_cif.c
........
  r46259 | brett.cannon | 2006-05-25 23:33:11 +0200 (Thu, 25 May 2006) | 4 lines

  Change test_values so that it compares the lowercasing of group names since getgrall() can return all lowercase names while getgrgid() returns proper casing.

  Discovered on Ubuntu 5.04 (custom).
........
  r46261 | tim.peters | 2006-05-25 23:50:17 +0200 (Thu, 25 May 2006) | 7 lines

  Some Win64 pre-release in 2000 didn't support
  QueryPerformanceCounter(), but we believe Win64 does
  support it now.  So use in time.clock().

  It would be peachy if someone with a Win64 box tried
  this ;-)
........
  r46262 | tim.peters | 2006-05-25 23:52:19 +0200 (Thu, 25 May 2006) | 2 lines

  Whitespace normalization.
........
  r46263 | bob.ippolito | 2006-05-25 23:58:05 +0200 (Thu, 25 May 2006) | 1 line

  Add missing files from x86 darwin ctypes patch
........
  r46264 | brett.cannon | 2006-05-26 00:00:14 +0200 (Fri, 26 May 2006) | 2 lines

  Move over to use of METH_O and METH_NOARGS.
........
  r46265 | tim.peters | 2006-05-26 00:25:25 +0200 (Fri, 26 May 2006) | 3 lines

  Repair idiot typo, and complete the job of trying to
  use the Windows time.clock() implementation on Win64.
........
  r46266 | tim.peters | 2006-05-26 00:28:46 +0200 (Fri, 26 May 2006) | 9 lines

  Patch #1494387: SVN longobject.c compiler warnings

  The SIGCHECK macro defined here has always been bizarre, but
  it apparently causes compiler warnings on "Sun Studio 11".
  I believe the warnings are bogus, but it doesn't hurt to make
  the macro definition saner.

  Bugfix candidate (but I'm not going to bother).
........
  r46268 | fredrik.lundh | 2006-05-26 01:27:53 +0200 (Fri, 26 May 2006) | 8 lines

  needforspeed: partition for 8-bit strings.  for some simple tests,
  this is on par with a corresponding find, and nearly twice as fast
  as split(sep, 1)

  full tests, a unicode version, and documentation will follow to-
  morrow.
........
  r46271 | andrew.kuchling | 2006-05-26 03:46:22 +0200 (Fri, 26 May 2006) | 1 line

  Add Soc student
........
  r46272 | ronald.oussoren | 2006-05-26 10:41:25 +0200 (Fri, 26 May 2006) | 3 lines

  Without this patch OSX users couldn't add new help sources because the code
  tried to update one item in a tuple.
........
  r46273 | fredrik.lundh | 2006-05-26 10:54:28 +0200 (Fri, 26 May 2006) | 5 lines

  needforspeed: partition implementation, part two.

  feel free to improve the documentation and the docstrings.
........
  r46274 | georg.brandl | 2006-05-26 11:05:54 +0200 (Fri, 26 May 2006) | 3 lines

  Clarify docs for str.partition().
........
  r46278 | fredrik.lundh | 2006-05-26 11:46:59 +0200 (Fri, 26 May 2006) | 5 lines

  needforspeed: use METH_O for argument handling, which made partition some
  ~15% faster for the current tests (which is noticable faster than a corre-
  sponding find call).  thanks to neal-who-never-sleeps for the tip.
........
  r46280 | fredrik.lundh | 2006-05-26 12:27:17 +0200 (Fri, 26 May 2006) | 5 lines

  needforspeed: use Py_ssize_t for the fastsearch counter and skip
  length (thanks, neal!).  and yes, I've verified that this doesn't
  slow things down ;-)
........
  r46285 | andrew.dalke | 2006-05-26 13:11:38 +0200 (Fri, 26 May 2006) | 2 lines

  Added a few more test cases for whitespace split.  These strings have leading whitespace.
........
  r46286 | jack.diederich | 2006-05-26 13:15:17 +0200 (Fri, 26 May 2006) | 1 line

  use Py_ssize_t in places that may need it
........
  r46287 | andrew.dalke | 2006-05-26 13:15:22 +0200 (Fri, 26 May 2006) | 2 lines

  Added split whitespace checks for characters other than space.
........
  r46288 | ronald.oussoren | 2006-05-26 13:17:55 +0200 (Fri, 26 May 2006) | 2 lines

  Fix buglet in postinstall script, it would generate an invalid .cshrc file.
........
  r46290 | georg.brandl | 2006-05-26 13:26:11 +0200 (Fri, 26 May 2006) | 3 lines

  Add "partition" to UserString.
........
  r46291 | fredrik.lundh | 2006-05-26 13:29:39 +0200 (Fri, 26 May 2006) | 5 lines

  needforspeed: added Py_LOCAL macro, based on the LOCAL macro used
  for SRE and others.  applied Py_LOCAL to relevant portion of ceval,
  which gives a 1-2% speedup on my machine.  ymmv.
........
  r46292 | jack.diederich | 2006-05-26 13:37:20 +0200 (Fri, 26 May 2006) | 1 line

  when generating python code prefer to generate valid python code
........
  r46293 | fredrik.lundh | 2006-05-26 13:38:15 +0200 (Fri, 26 May 2006) | 3 lines

  use Py_LOCAL also for string and unicode objects
........
  r46294 | ronald.oussoren | 2006-05-26 13:38:39 +0200 (Fri, 26 May 2006) | 12 lines

  - Search the sqlite specific search directories
    after the normal include directories when looking
    for the version of sqlite to use.
  - On OSX:
    * Extract additional include and link directories
      from the CFLAGS and LDFLAGS, if the user has
      bothered to specify them we might as wel use them.
    * Add '-Wl,-search_paths_first' to the extra_link_args
      for readline and sqlite. This makes it possible to
      use a static library to override the system provided
      dynamic library.
........
  r46295 | ronald.oussoren | 2006-05-26 13:43:26 +0200 (Fri, 26 May 2006) | 6 lines

  Integrate installing a framework in the 'make install'
  target. Until now users had to use 'make frameworkinstall'
  to install python when it is configured with '--enable-framework'.
  This tends to confuse users that don't hunt for readme files
  hidden in platform specific directories :-)
........
  r46297 | fredrik.lundh | 2006-05-26 13:54:04 +0200 (Fri, 26 May 2006) | 4 lines

  needforspeed: added PY_LOCAL_AGGRESSIVE macro to enable "aggressive"
  LOCAL inlining; also added some missing whitespace
........
  r46298 | andrew.kuchling | 2006-05-26 14:01:44 +0200 (Fri, 26 May 2006) | 1 line

  Typo fixes
........
  r46299 | fredrik.lundh | 2006-05-26 14:01:49 +0200 (Fri, 26 May 2006) | 4 lines

  Py_LOCAL shouldn't be used for data; it works for some .NET 2003 compilers,
  but Trent's copy thinks that it's an anachronism...
........
  r46300 | martin.blais | 2006-05-26 14:03:27 +0200 (Fri, 26 May 2006) | 12 lines

  Support for buffer protocol for socket and struct.

  * Added socket.recv_buf() and socket.recvfrom_buf() methods, that use the buffer
    protocol (send and sendto already did).

  * Added struct.pack_to(), that is the corresponding buffer compatible method to
    unpack_from().

  * Fixed minor typos in arraymodule.
........
  r46302 | ronald.oussoren | 2006-05-26 14:23:20 +0200 (Fri, 26 May 2006) | 6 lines

  - Remove previous version of the binary distribution script for OSX
  - Some small bugfixes for the IDLE.app wrapper
  - Tweaks to build-installer to ensure that python gets build in the right way,
    including sqlite3.
  - Updated readme files
........
  r46305 | tim.peters | 2006-05-26 14:26:21 +0200 (Fri, 26 May 2006) | 2 lines

  Whitespace normalization.
........
  r46307 | andrew.dalke | 2006-05-26 14:28:15 +0200 (Fri, 26 May 2006) | 7 lines

  I like tests.

  The new split functions use a preallocated list.  Added tests which exceed
  the preallocation size, to exercise list appends/resizes.

  Also added more edge case tests.
........
  r46308 | andrew.dalke | 2006-05-26 14:31:00 +0200 (Fri, 26 May 2006) | 2 lines

  Test cases for off-by-one errors in string split with multicharacter pattern.
........
  r46309 | tim.peters | 2006-05-26 14:31:20 +0200 (Fri, 26 May 2006) | 2 lines

  Whitespace normalization.
........
  r46313 | andrew.kuchling | 2006-05-26 14:39:48 +0200 (Fri, 26 May 2006) | 1 line

  Add str.partition()
........
  r46314 | bob.ippolito | 2006-05-26 14:52:53 +0200 (Fri, 26 May 2006) | 1 line

  quick hack to fix busted binhex test
........
  r46316 | andrew.dalke | 2006-05-26 15:05:55 +0200 (Fri, 26 May 2006) | 2 lines

  Added more rstrip tests, including for prealloc'ed arrays
........
  r46320 | bob.ippolito | 2006-05-26 15:15:44 +0200 (Fri, 26 May 2006) | 1 line

  fix #1229380 No struct.pack exception for some out of range integers
........
  r46325 | tim.peters | 2006-05-26 15:39:17 +0200 (Fri, 26 May 2006) | 2 lines

  Use open() to open files (was using file()).
........
  r46327 | andrew.dalke | 2006-05-26 16:00:45 +0200 (Fri, 26 May 2006) | 37 lines

  Changes to string.split/rsplit on whitespace to preallocate space in the
  results list.

  Originally it allocated 0 items and used the list growth during append.  Now
  it preallocates 12 items so the first few appends don't need list reallocs.

  ("Here are some words ."*2).split(None, 1) is 7% faster
  ("Here are some words ."*2).split() is is 15% faster

    (Your milage may vary, see dealership for details.)

  File parsing like this

      for line in f:
          count += len(line.split())

  is also about 15% faster.  There is a slowdown of about 3% for large
  strings because of the additional overhead of checking if the append is
  to a preallocated region of the list or not.  This will be the rare case.
  It could be improved with special case code but we decided it was not
  useful enough.

  There is a cost of 12*sizeof(PyObject *) bytes per list.  For the normal
  case of file parsing this is not a problem because of the lists have
  a short lifetime.  We have not come up with cases where this is a problem
  in real life.

  I chose 12 because human text averages about 11 words per line in books,
  one of my data sets averages 6.2 words with a final peak at 11 words per
  line, and I work with a tab delimited data set with 8 tabs per line (or
  9 words per line).  12 encompasses all of these.

  Also changed the last rstrip code to append then reverse, rather than
  doing insert(0).  The strip() and rstrip() times are now comparable.
........
  r46328 | tim.peters | 2006-05-26 16:02:05 +0200 (Fri, 26 May 2006) | 5 lines

  Explicitly close files.  I'm trying to stop the frequent spurious test_tarfile
  failures on Windows buildbots, but it's hard to know how since the regrtest
  failure output is useless here, and it never fails when a buildbot slave runs
  test_tarfile the second time in verbose mode.
........
  r46329 | andrew.kuchling | 2006-05-26 16:03:41 +0200 (Fri, 26 May 2006) | 1 line

  Add buffer support for struct, socket
........
  r46330 | andrew.kuchling | 2006-05-26 16:04:19 +0200 (Fri, 26 May 2006) | 1 line

  Typo fix
........
  r46331 | bob.ippolito | 2006-05-26 16:07:23 +0200 (Fri, 26 May 2006) | 1 line

  Fix distutils so that libffi will cross-compile between darwin/x86 and darwin/ppc
........
  r46333 | bob.ippolito | 2006-05-26 16:23:21 +0200 (Fri, 26 May 2006) | 1 line

  Fix _struct typo that broke some 64-bit platforms
........
  r46335 | bob.ippolito | 2006-05-26 16:29:35 +0200 (Fri, 26 May 2006) | 1 line

  Enable PY_USE_INT_WHEN_POSSIBLE in struct
........
  r46343 | andrew.dalke | 2006-05-26 17:21:01 +0200 (Fri, 26 May 2006) | 2 lines

  Eeked out another 3% or so performance in split whitespace by cleaning up the algorithm.
........
  r46352 | andrew.dalke | 2006-05-26 18:22:52 +0200 (Fri, 26 May 2006) | 3 lines

  Test for more edge strip cases; leading and trailing separator gets removed
  even with strip(..., 0)
........
  r46354 | bob.ippolito | 2006-05-26 18:23:28 +0200 (Fri, 26 May 2006) | 1 line

  fix signed/unsigned mismatch in struct
........
  r46355 | steve.holden | 2006-05-26 18:27:59 +0200 (Fri, 26 May 2006) | 5 lines

  Add -t option to allow easy test selection.
  Action verbose option correctly.
  Tweak operation counts. Add empty and new instances tests.
  Enable comparisons across different warp factors. Change version.
........
  r46356 | fredrik.lundh | 2006-05-26 18:32:42 +0200 (Fri, 26 May 2006) | 3 lines

  needforspeed: use Py_LOCAL on a few more locals in stringobject.c
........
  r46357 | thomas.heller | 2006-05-26 18:42:44 +0200 (Fri, 26 May 2006) | 4 lines

  For now, I gave up with automatic conversion of reST to Python-latex,
  so I'm writing this in latex now.

  Skeleton for the ctypes reference.
........
  r46358 | tim.peters | 2006-05-26 18:49:28 +0200 (Fri, 26 May 2006) | 3 lines

  Repair Windows compiler warnings about mixing
  signed and unsigned integral types in comparisons.
........
  r46359 | tim.peters | 2006-05-26 18:52:04 +0200 (Fri, 26 May 2006) | 2 lines

  Whitespace normalization.
........
  r46360 | tim.peters | 2006-05-26 18:53:04 +0200 (Fri, 26 May 2006) | 2 lines

  Add missing svn:eol-style property to text files.
........
  r46362 | fredrik.lundh | 2006-05-26 19:04:58 +0200 (Fri, 26 May 2006) | 3 lines

  needforspeed: stringlib refactoring (in progress)
........
  r46363 | thomas.heller | 2006-05-26 19:18:33 +0200 (Fri, 26 May 2006) | 1 line

  Write some docs.
........
  r46364 | fredrik.lundh | 2006-05-26 19:22:38 +0200 (Fri, 26 May 2006) | 3 lines

  needforspeed: stringlib refactoring (in progress)
........
  r46366 | fredrik.lundh | 2006-05-26 19:26:39 +0200 (Fri, 26 May 2006) | 3 lines

  needforspeed: cleanup
........
  r46367 | fredrik.lundh | 2006-05-26 19:31:41 +0200 (Fri, 26 May 2006) | 4 lines

  needforspeed: remove remaining USE_FAST macros; if fastsearch was
  broken, someone would have noticed by now ;-)
........
  r46368 | steve.holden | 2006-05-26 19:41:32 +0200 (Fri, 26 May 2006) | 5 lines

  Use minimum calibration time rather than avergae to avoid
  the illusion of negative run times. Halt with an error if
  run times go below 10 ms, indicating that results will be
  unreliable.
........
  r46370 | thomas.heller | 2006-05-26 19:47:40 +0200 (Fri, 26 May 2006) | 2 lines

  Reordered, and wrote more docs.
........
  r46372 | georg.brandl | 2006-05-26 20:03:31 +0200 (Fri, 26 May 2006) | 9 lines

  Need for speed: Patch #921466 : sys.path_importer_cache is now used to cache valid and
    invalid file paths for the built-in import machinery which leads to
    fewer open calls on startup.

    Also fix issue with PEP 302 style import hooks which lead to more open()
    calls than necessary.
........
  r46373 | fredrik.lundh | 2006-05-26 20:05:34 +0200 (Fri, 26 May 2006) | 3 lines

  removed unnecessary include
........
  r46377 | fredrik.lundh | 2006-05-26 20:15:38 +0200 (Fri, 26 May 2006) | 3 lines

  needforspeed: added rpartition implementation
........
  r46380 | fredrik.lundh | 2006-05-26 20:24:15 +0200 (Fri, 26 May 2006) | 5 lines

  needspeed: rpartition documentation, tests, and a bug fixes.

  feel free to add more tests and improve the documentation.
........
  r46381 | steve.holden | 2006-05-26 20:26:21 +0200 (Fri, 26 May 2006) | 4 lines

  Revert tests to MAL's original round sizes to retiain comparability
  from long ago and far away. Stop calling this pybench 1.4 because it
  isn't. Remove the empty test, which was a bad idea.
........
  r46387 | andrew.kuchling | 2006-05-26 20:41:18 +0200 (Fri, 26 May 2006) | 1 line

  Add rpartition() and path caching
........
  r46388 | andrew.dalke | 2006-05-26 21:02:09 +0200 (Fri, 26 May 2006) | 10 lines

  substring split now uses /F's fast string matching algorithm.
    (If compiled without FAST search support, changed the pre-memcmp test
     to check the last character as well as the first.  This gave a 25%
     speedup for my test case.)

  Rewrote the split algorithms so they stop when maxsplit gets to 0.
  Previously they did a string match first then checked if the maxsplit
  was reached.  The new way prevents a needless string search.
........
  r46391 | brett.cannon | 2006-05-26 21:04:47 +0200 (Fri, 26 May 2006) | 2 lines

  Change C spacing to 4 spaces by default to match PEP 7 for new C files.
........
  r46392 | georg.brandl | 2006-05-26 21:04:47 +0200 (Fri, 26 May 2006) | 3 lines

  Exception isn't the root of all exception classes anymore.
........
  r46397 | fredrik.lundh | 2006-05-26 21:23:21 +0200 (Fri, 26 May 2006) | 3 lines

  added rpartition method to UserString class
........
  r46398 | fredrik.lundh | 2006-05-26 21:24:53 +0200 (Fri, 26 May 2006) | 4 lines

  needforspeed: stringlib refactoring, continued.  added count and
  find helpers; updated unicodeobject to use stringlib_count
........
  r46400 | fredrik.lundh | 2006-05-26 21:29:05 +0200 (Fri, 26 May 2006) | 4 lines

  needforspeed: stringlib refactoring: use stringlib/find for unicode
  find
........
  r46403 | fredrik.lundh | 2006-05-26 21:33:03 +0200 (Fri, 26 May 2006) | 3 lines

  needforspeed: use a macro to fix slice indexes
........
  r46404 | thomas.heller | 2006-05-26 21:43:45 +0200 (Fri, 26 May 2006) | 1 line

  Write more docs.
........
  r46406 | fredrik.lundh | 2006-05-26 21:48:07 +0200 (Fri, 26 May 2006) | 3 lines

  needforspeed: stringlib refactoring: use stringlib/find for string find
........
  r46407 | andrew.kuchling | 2006-05-26 21:51:10 +0200 (Fri, 26 May 2006) | 1 line

  Comment typo
........
  r46409 | georg.brandl | 2006-05-26 22:04:44 +0200 (Fri, 26 May 2006) | 3 lines

  Replace Py_BuildValue("OO") by PyTuple_Pack.
........
  r46411 | georg.brandl | 2006-05-26 22:14:47 +0200 (Fri, 26 May 2006) | 2 lines

  Patch #1492218: document None being a constant.
........
  r46415 | georg.brandl | 2006-05-26 22:22:50 +0200 (Fri, 26 May 2006) | 3 lines

  Simplify calling.
........
  r46416 | andrew.dalke | 2006-05-26 22:25:22 +0200 (Fri, 26 May 2006) | 4 lines

  Added limits to the replace code so it does not count all of the matching
  patterns in a string, only the number needed by the max limit.
........
  r46417 | bob.ippolito | 2006-05-26 22:25:23 +0200 (Fri, 26 May 2006) | 1 line

  enable all of the struct tests, use ssize_t, fix some whitespace
........
  r46418 | tim.peters | 2006-05-26 22:56:56 +0200 (Fri, 26 May 2006) | 2 lines

  Record Iceland sprint attendees.
........
  r46421 | tim.peters | 2006-05-26 23:51:13 +0200 (Fri, 26 May 2006) | 2 lines

  Whitespace normalization.
........
  r46422 | steve.holden | 2006-05-27 00:17:54 +0200 (Sat, 27 May 2006) | 2 lines

  Add Richard Tew to developers
........
  r46423 | steve.holden | 2006-05-27 00:33:20 +0200 (Sat, 27 May 2006) | 2 lines

  Update help text and documentaition.
........
  r46424 | steve.holden | 2006-05-27 00:39:27 +0200 (Sat, 27 May 2006) | 2 lines

  Blasted typos ...
........
  r46425 | andrew.dalke | 2006-05-27 00:49:03 +0200 (Sat, 27 May 2006) | 2 lines

  Added description of why splitlines doesn't use the prealloc strategy
........
  r46426 | tim.peters | 2006-05-27 01:14:37 +0200 (Sat, 27 May 2006) | 19 lines

  Patch 1145039.

  set_exc_info(), reset_exc_info():  By exploiting the
  likely (who knows?) invariant that when an exception's
  `type` is NULL, its `value` and `traceback` are also NULL,
  save some cycles in heavily-executed code.

  This is a "a kronar saved is a kronar earned" patch:  the
  speedup isn't reliably measurable, but it obviously does
  reduce the operation count in the normal (no exception
  raised) path through PyEval_EvalFrameEx().

  The tim-exc_sanity branch tries to push this harder, but
  is still blowing up (at least in part due to pre-existing
  subtle bugs that appear to have no other visible
  consequences!).

  Not a bugfix candidate.
........
  r46429 | steve.holden | 2006-05-27 02:51:52 +0200 (Sat, 27 May 2006) | 2 lines

  Reinstate new-style object tests.
........
  r46430 | neal.norwitz | 2006-05-27 07:18:57 +0200 (Sat, 27 May 2006) | 1 line

  Fix compiler warning (and whitespace) on Mac OS 10.4.  (A lot of this code looked duplicated, I wonder if a utility function could help reduce the duplication here.)
........
  r46431 | neal.norwitz | 2006-05-27 07:21:30 +0200 (Sat, 27 May 2006) | 4 lines

  Fix Coverity warnings.
   - Check the correct variable (str_obj, not str) for NULL
   - sep_len was already verified it wasn't 0
........
  r46432 | martin.v.loewis | 2006-05-27 10:36:52 +0200 (Sat, 27 May 2006) | 2 lines

  Patch 1494554: Update numeric properties to Unicode 4.1.
........
  r46433 | martin.v.loewis | 2006-05-27 10:54:29 +0200 (Sat, 27 May 2006) | 2 lines

  Explain why 'consumed' is initialized.
........
  r46436 | fredrik.lundh | 2006-05-27 12:05:10 +0200 (Sat, 27 May 2006) | 3 lines

  needforspeed: more stringlib refactoring
........
  r46438 | fredrik.lundh | 2006-05-27 12:39:48 +0200 (Sat, 27 May 2006) | 5 lines

  needforspeed: backed out the Py_LOCAL-isation of ceval; the massive in-
  lining killed performance on certain Intel boxes, and the "aggressive"
  macro itself gives most of the benefits on others.
........
  r46439 | andrew.dalke | 2006-05-27 13:04:36 +0200 (Sat, 27 May 2006) | 2 lines

  fixed typo
........
  r46440 | martin.v.loewis | 2006-05-27 13:07:49 +0200 (Sat, 27 May 2006) | 2 lines

  Revert bogus change committed in 46432 to this file.
........
  r46444 | andrew.kuchling | 2006-05-27 13:26:33 +0200 (Sat, 27 May 2006) | 1 line

  Add Py_LOCAL macros
........
  r46450 | bob.ippolito | 2006-05-27 13:47:12 +0200 (Sat, 27 May 2006) | 1 line

  Remove the range checking and int usage #defines from _struct and strip out the now-dead code
........
  r46454 | bob.ippolito | 2006-05-27 14:11:36 +0200 (Sat, 27 May 2006) | 1 line

  Fix up struct docstrings, add struct.pack_to function for symmetry
........
  r46456 | richard.jones | 2006-05-27 14:29:24 +0200 (Sat, 27 May 2006) | 2 lines

  Conversion of exceptions over from faked-up classes to new-style C types.
........
  r46457 | georg.brandl | 2006-05-27 14:30:25 +0200 (Sat, 27 May 2006) | 3 lines

  Add news item for new-style exception class branch merge.
........
  r46458 | tim.peters | 2006-05-27 14:36:53 +0200 (Sat, 27 May 2006) | 3 lines

  More random thrashing trying to understand spurious
  Windows failures.  Who's keeping a bz2 file open?
........
  r46460 | andrew.kuchling | 2006-05-27 15:44:37 +0200 (Sat, 27 May 2006) | 1 line

  Mention new-style exceptions
........
  r46461 | richard.jones | 2006-05-27 15:50:42 +0200 (Sat, 27 May 2006) | 1 line

  credit where credit is due
........
  r46462 | georg.brandl | 2006-05-27 16:02:03 +0200 (Sat, 27 May 2006) | 3 lines

  Always close BZ2Proxy object. Remove unnecessary struct usage.
........
  r46463 | tim.peters | 2006-05-27 16:13:13 +0200 (Sat, 27 May 2006) | 2 lines

  The cheery optimism of old age.
........
  r46464 | andrew.dalke | 2006-05-27 16:16:40 +0200 (Sat, 27 May 2006) | 2 lines

  cleanup - removed trailing whitespace
........
  r46465 | georg.brandl | 2006-05-27 16:41:55 +0200 (Sat, 27 May 2006) | 3 lines

  Remove spurious semicolons after macro invocations.
........
  r46468 | fredrik.lundh | 2006-05-27 16:58:20 +0200 (Sat, 27 May 2006) | 4 lines

  needforspeed: replace improvements, changed to Py_LOCAL_INLINE
  where appropriate
........
  r46469 | fredrik.lundh | 2006-05-27 17:20:22 +0200 (Sat, 27 May 2006) | 4 lines

  needforspeed: stringlib refactoring: changed find_obj to find_slice,
  to enable use from stringobject
........
  r46470 | fredrik.lundh | 2006-05-27 17:26:19 +0200 (Sat, 27 May 2006) | 3 lines

  needforspeed: stringlib refactoring: use find_slice for stringobject
........
  r46472 | kristjan.jonsson | 2006-05-27 17:41:31 +0200 (Sat, 27 May 2006) | 1 line

  Add a PCBuild8 build directory for building with Visual Studio .NET 2005.  Contains a special project to perform profile guided optimizations on the pythoncore.dll, by instrumenting and running pybench.py
........
  r46473 | jack.diederich | 2006-05-27 17:44:34 +0200 (Sat, 27 May 2006) | 3 lines

  needforspeed: use PyObject_MALLOC instead of system malloc for small
  allocations.  Use PyMem_MALLOC for larger (1k+) chunks.  1%-2% speedup.
........
  r46474 | bob.ippolito | 2006-05-27 17:53:49 +0200 (Sat, 27 May 2006) | 1 line

  fix struct regression on 64-bit platforms
........
  r46475 | richard.jones | 2006-05-27 18:07:28 +0200 (Sat, 27 May 2006) | 1 line

  doc string additions and tweaks
........
  r46477 | richard.jones | 2006-05-27 18:15:11 +0200 (Sat, 27 May 2006) | 1 line

  move semicolons
........
  r46478 | george.yoshida | 2006-05-27 18:32:44 +0200 (Sat, 27 May 2006) | 2 lines

  minor markup nits
........
  r46488 | george.yoshida | 2006-05-27 18:51:43 +0200 (Sat, 27 May 2006) | 3 lines

  End of Ch.3 is now about "with statement".
  Avoid obsolescence by directly referring to the section.
........
  r46489 | george.yoshida | 2006-05-27 19:09:17 +0200 (Sat, 27 May 2006) | 2 lines

  fix typo
........
diff --git a/Modules/Setup.dist b/Modules/Setup.dist
index 49c8425..1b2502d 100644
--- a/Modules/Setup.dist
+++ b/Modules/Setup.dist
@@ -167,7 +167,7 @@
 #array arraymodule.c	# array objects
 #cmath cmathmodule.c # -lm # complex math library functions
 #math mathmodule.c # -lm # math library functions, e.g. sin()
-#struct structmodule.c	# binary structure packing/unpacking
+#_struct _struct.c	# binary structure packing/unpacking
 #time timemodule.c # -lm # time operations and variables
 #operator operator.c	# operator.add() and similar goodies
 #_weakref _weakref.c	# basic weak reference support
diff --git a/Modules/_codecsmodule.c b/Modules/_codecsmodule.c
index 39b443b..d8d23c4 100644
--- a/Modules/_codecsmodule.c
+++ b/Modules/_codecsmodule.c
@@ -169,7 +169,7 @@
 
 static
 PyObject *codec_tuple(PyObject *unicode,
-		      int len)
+		      Py_ssize_t len)
 {
     PyObject *v,*w;
 
@@ -181,7 +181,7 @@
 	return NULL;
     }
     PyTuple_SET_ITEM(v,0,unicode);
-    w = PyInt_FromLong(len);
+    w = PyInt_FromSsize_t(len);
     if (w == NULL) {
 	Py_DECREF(v);
 	return NULL;
@@ -213,7 +213,7 @@
 	PyObject *str;
 	const char *errors = NULL;
 	char *buf;
-	int len;
+	Py_ssize_t len;
 
 	if (!PyArg_ParseTuple(args, "O!|z:escape_encode",
 			      &PyString_Type, &str, &errors))
@@ -319,12 +319,11 @@
     if (!PyArg_ParseTuple(args, "t#|zi:utf_16_decode",
 			  &data, &size, &errors, &final))
 	return NULL;
-    /* XXX Why is consumed initialized to size? mvl */
     if (size < 0) {
 	    PyErr_SetString(PyExc_ValueError, "negative argument");
 	    return 0;
     }
-    consumed = size;
+    consumed = size; /* This is overwritten unless final is true. */
     decoded = PyUnicode_DecodeUTF16Stateful(data, size, errors, &byteorder,
 					    final ? NULL : &consumed);
     if (decoded == NULL)
@@ -348,12 +347,11 @@
 			  &data, &size, &errors, &final))
 	return NULL;
 
-    /* XXX Why is consumed initialized to size? mvl */
     if (size < 0) {
           PyErr_SetString(PyExc_ValueError, "negative argument");
           return 0;
     }
-    consumed = size;
+    consumed = size; /* This is overwritten unless final is true. */
     decoded = PyUnicode_DecodeUTF16Stateful(data, size, errors,
 	&byteorder, final ? NULL : &consumed);
     if (decoded == NULL)
@@ -377,12 +375,11 @@
     if (!PyArg_ParseTuple(args, "t#|zi:utf_16_be_decode",
 			  &data, &size, &errors, &final))
 	return NULL;
-    /* XXX Why is consumed initialized to size? mvl */
     if (size < 0) {
           PyErr_SetString(PyExc_ValueError, "negative argument");
           return 0;
     }
-    consumed = size;
+    consumed = size; /* This is overwritten unless final is true. */
     decoded = PyUnicode_DecodeUTF16Stateful(data, size, errors,
 	&byteorder, final ? NULL : &consumed);
     if (decoded == NULL)
@@ -413,12 +410,11 @@
     if (!PyArg_ParseTuple(args, "t#|zii:utf_16_ex_decode",
 			  &data, &size, &errors, &byteorder, &final))
 	return NULL;
-    /* XXX Why is consumed initialized to size? mvl */
     if (size < 0) {
 	    PyErr_SetString(PyExc_ValueError, "negative argument");
 	    return 0;
     }
-    consumed = size;
+    consumed = size; /* This is overwritten unless final is true. */
     unicode = PyUnicode_DecodeUTF16Stateful(data, size, errors, &byteorder,
 					    final ? NULL : &consumed);
     if (unicode == NULL)
diff --git a/Modules/_csv.c b/Modules/_csv.c
index 4704c16..a341eb4 100644
--- a/Modules/_csv.c
+++ b/Modules/_csv.c
@@ -1104,6 +1104,8 @@
 	char *terminator;
 
 	terminator_len = PyString_Size(self->dialect->lineterminator);
+	if (terminator_len == -1)
+		return 0;
 
 	/* grow record buffer if necessary */
 	if (!join_check_rec_size(self, self->rec_len + terminator_len))
diff --git a/Modules/_ctypes/_ctypes.c b/Modules/_ctypes/_ctypes.c
index e6d6aa4..eaa3ced 100644
--- a/Modules/_ctypes/_ctypes.c
+++ b/Modules/_ctypes/_ctypes.c
@@ -1223,6 +1223,19 @@
 			return value;
 		}
 	}
+/* function pointer */
+	if (CFuncPtrObject_Check(value)) {
+		PyCArgObject *parg;
+		CFuncPtrObject *func;
+		func = (CFuncPtrObject *)value;
+		parg = new_CArgObject();
+		parg->pffi_type = &ffi_type_pointer;
+		parg->tag = 'P';
+		Py_INCREF(value);
+		parg->value.p = *(void **)func->b_ptr;
+		parg->obj = value;
+		return (PyObject *)parg;
+	}
 /* c_char_p, c_wchar_p */
 	stgd = PyObject_stgdict(value);
 	if (stgd && CDataObject_Check(value) && stgd->proto && PyString_Check(stgd->proto)) {
@@ -2172,7 +2185,7 @@
 		  only it's object list.  So we create a tuple, containing
 		  b_objects list PLUS the array itself, and return that!
 		*/
-		return Py_BuildValue("(OO)", keep, value);
+		return PyTuple_Pack(2, keep, value);
 	}
 	PyErr_Format(PyExc_TypeError,
 		     "incompatible types, %s instance instead of %s instance",
@@ -4405,6 +4418,8 @@
 
 	if (PointerTypeObject_Check(arg))
 		return 1;
+	if (CFuncPtrTypeObject_Check(arg))
+		return 1;
 	dict = PyType_stgdict(arg);
 	if (dict) {
 		if (PyString_Check(dict->proto)
@@ -4564,7 +4579,7 @@
 #endif
 	PyModule_AddObject(m, "FUNCFLAG_CDECL", PyInt_FromLong(FUNCFLAG_CDECL));
 	PyModule_AddObject(m, "FUNCFLAG_PYTHONAPI", PyInt_FromLong(FUNCFLAG_PYTHONAPI));
-	PyModule_AddStringConstant(m, "__version__", "0.9.9.4");
+	PyModule_AddStringConstant(m, "__version__", "0.9.9.6");
 
 	PyModule_AddObject(m, "_memmove_addr", PyLong_FromVoidPtr(memmove));
 	PyModule_AddObject(m, "_memset_addr", PyLong_FromVoidPtr(memset));
diff --git a/Modules/_ctypes/_ctypes_test.c b/Modules/_ctypes/_ctypes_test.c
index ad3b047..b10d6fe 100644
--- a/Modules/_ctypes/_ctypes_test.c
+++ b/Modules/_ctypes/_ctypes_test.c
@@ -96,6 +96,11 @@
 	return dst;
 }
 
+EXPORT(void)my_free(void *ptr)
+{
+	free(ptr);
+}
+
 #ifdef HAVE_WCHAR_H
 EXPORT(wchar_t *) my_wcsdup(wchar_t *src)
 {
@@ -199,11 +204,6 @@
 	return 0;
 }
 
-EXPORT(void) my_free(void *p)
-{
-	printf("my_free got %p\n", p);
-}
-
 typedef struct {
 	char *name;
 	char *value;
diff --git a/Modules/_ctypes/libffi/configure b/Modules/_ctypes/libffi/configure
index c1e5cd4..9fdd654 100755
--- a/Modules/_ctypes/libffi/configure
+++ b/Modules/_ctypes/libffi/configure
@@ -310,7 +310,7 @@
 # include <unistd.h>
 #endif"
 
-ac_subst_vars='SHELL PATH_SEPARATOR PACKAGE_NAME PACKAGE_TARNAME PACKAGE_VERSION PACKAGE_STRING PACKAGE_BUGREPORT exec_prefix prefix program_transform_name bindir sbindir libexecdir datadir sysconfdir sharedstatedir localstatedir libdir includedir oldincludedir infodir mandir build_alias host_alias target_alias DEFS ECHO_C ECHO_N ECHO_T LIBS build build_cpu build_vendor build_os host host_cpu host_vendor host_os target target_cpu target_vendor target_os CC ac_ct_CC EXEEXT OBJEXT CFLAGS CPP CPPFLAGS EGREP ALLOCA HAVE_LONG_DOUBLE TARGET TARGETDIR LIBOBJS LTLIBOBJS'
+ac_subst_vars='SHELL PATH_SEPARATOR PACKAGE_NAME PACKAGE_TARNAME PACKAGE_VERSION PACKAGE_STRING PACKAGE_BUGREPORT exec_prefix prefix program_transform_name bindir sbindir libexecdir datadir sysconfdir sharedstatedir localstatedir libdir includedir oldincludedir infodir mandir build_alias host_alias target_alias DEFS ECHO_C ECHO_N ECHO_T LIBS build build_cpu build_vendor build_os host host_cpu host_vendor host_os target target_cpu target_vendor target_os CC ac_ct_CC EXEEXT OBJEXT CFLAGS CPP CPPFLAGS EGREP ALLOCA HAVE_LONG_DOUBLE TARGET TARGETDIR MKTARGET LIBOBJS LTLIBOBJS'
 ac_subst_files=''
 
 # Initialize some variables set by options.
@@ -3483,6 +3483,7 @@
 
 TARGETDIR="unknown"
 case "$host" in
+i*86-*-darwin*) TARGET=X86_DARWIN; TARGETDIR=x86;;
 i*86-*-linux*) TARGET=X86; TARGETDIR=x86;;
 i*86-*-gnu*) TARGET=X86; TARGETDIR=x86;;
 i*86-*-solaris2.1[0-9]*) TARGET=X86_64; TARGETDIR=x86;;
@@ -3534,6 +3535,8 @@
    { (exit 1); exit 1; }; }
 fi
 
+MKTARGET=$TARGET
+
 case x$TARGET in
   xMIPS*) TARGET=MIPS ;;
   *) ;;
@@ -5241,6 +5244,9 @@
 esac
 
 
+
+
+
 if test x$TARGET = xSPARC; then
     echo "$as_me:$LINENO: checking assembler and linker support unaligned pc related relocs" >&5
 echo $ECHO_N "checking assembler and linker support unaligned pc related relocs... $ECHO_C" >&6
@@ -5457,6 +5463,7 @@
 
 
 
+
 cat >>confdefs.h <<\_ACEOF
 #define FFI_NO_RAW_API 1
 _ACEOF
@@ -5467,7 +5474,15 @@
           ac_config_commands="$ac_config_commands src"
 
 
-          ac_config_links="$ac_config_links include/ffitarget.h:src/$TARGETDIR/ffitarget.h"
+TARGETINCDIR=$TARGETDIR
+case $host in
+*-*-darwin*)
+   TARGETINCDIR="darwin"
+   ;;
+esac
+
+
+          ac_config_links="$ac_config_links include/ffitarget.h:src/$TARGETINCDIR/ffitarget.h"
 
           ac_config_links="$ac_config_links include/ffi_common.h:include/ffi_common.h"
 
@@ -6014,7 +6029,7 @@
   # Handling of arguments.
   "include/ffi.h" ) CONFIG_FILES="$CONFIG_FILES include/ffi.h" ;;
   "fficonfig.py" ) CONFIG_FILES="$CONFIG_FILES fficonfig.py" ;;
-  "include/ffitarget.h" ) CONFIG_LINKS="$CONFIG_LINKS include/ffitarget.h:src/$TARGETDIR/ffitarget.h" ;;
+  "include/ffitarget.h" ) CONFIG_LINKS="$CONFIG_LINKS include/ffitarget.h:src/$TARGETINCDIR/ffitarget.h" ;;
   "include/ffi_common.h" ) CONFIG_LINKS="$CONFIG_LINKS include/ffi_common.h:include/ffi_common.h" ;;
   "include" ) CONFIG_COMMANDS="$CONFIG_COMMANDS include" ;;
   "src" ) CONFIG_COMMANDS="$CONFIG_COMMANDS src" ;;
@@ -6129,6 +6144,7 @@
 s,@HAVE_LONG_DOUBLE@,$HAVE_LONG_DOUBLE,;t t
 s,@TARGET@,$TARGET,;t t
 s,@TARGETDIR@,$TARGETDIR,;t t
+s,@MKTARGET@,$MKTARGET,;t t
 s,@LIBOBJS@,$LIBOBJS,;t t
 s,@LTLIBOBJS@,$LTLIBOBJS,;t t
 CEOF
diff --git a/Modules/_ctypes/libffi/configure.ac b/Modules/_ctypes/libffi/configure.ac
index c7f05d6..a0d7513 100644
--- a/Modules/_ctypes/libffi/configure.ac
+++ b/Modules/_ctypes/libffi/configure.ac
@@ -21,6 +21,7 @@
 
 TARGETDIR="unknown"
 case "$host" in
+i*86-*-darwin*) TARGET=X86_DARWIN; TARGETDIR=x86;;
 i*86-*-linux*) TARGET=X86; TARGETDIR=x86;;
 i*86-*-gnu*) TARGET=X86; TARGETDIR=x86;;
 i*86-*-solaris2.1[[0-9]]*) TARGET=X86_64; TARGETDIR=x86;;
@@ -70,6 +71,12 @@
   AC_MSG_ERROR(["libffi has not been ported to $host."])
 fi
 
+dnl  libffi changes TARGET for MIPS to define a such macro in the header
+dnl  while MIPS_IRIX or MIPS_LINUX is separatedly used to decide which
+dnl  files will be compiled.  So, we need to keep the original decision
+dnl  of TARGET to use in fficonfig.py.in.
+MKTARGET=$TARGET
+
 case x$TARGET in
   xMIPS*) TARGET=MIPS ;;
   *) ;;
@@ -93,6 +100,24 @@
 AC_SUBST(HAVE_LONG_DOUBLE)
 
 AC_C_BIGENDIAN
+AH_VERBATIM([WORDS_BIGENDIAN],
+[
+/* Define to 1 if your processor stores words with the most significant byte
+   first (like Motorola and SPARC, unlike Intel and VAX). 
+
+   The block below does compile-time checking for endianness on platforms
+   that use GCC and therefore allows compiling fat binaries on OSX by using
+   '-arch ppc -arch i386' as the compile flags. The phrasing was choosen
+   such that the configure-result is used on systems that don't use GCC.
+*/
+#ifdef __BIG_ENDIAN__
+#define WORDS_BIGENDIAN 1
+#else
+#ifndef __LITTLE_ENDIAN__
+#undef WORDS_BIGENDIAN
+#endif
+#endif])
+
 
 if test x$TARGET = xSPARC; then
     AC_CACHE_CHECK([assembler and linker support unaligned pc related relocs],
@@ -183,6 +208,7 @@
 
 AC_SUBST(TARGET)
 AC_SUBST(TARGETDIR)
+AC_SUBST(MKTARGET)
 
 AC_SUBST(SHELL)
 
@@ -194,7 +220,15 @@
 test -d src/$TARGETDIR || mkdir src/$TARGETDIR
 ], [TARGETDIR="$TARGETDIR"])
 
-AC_CONFIG_LINKS(include/ffitarget.h:src/$TARGETDIR/ffitarget.h)
+TARGETINCDIR=$TARGETDIR
+case $host in
+*-*-darwin*)
+   TARGETINCDIR="darwin"
+   ;;
+esac
+
+
+AC_CONFIG_LINKS(include/ffitarget.h:src/$TARGETINCDIR/ffitarget.h)
 AC_CONFIG_LINKS(include/ffi_common.h:include/ffi_common.h)
 
 AC_CONFIG_FILES(include/ffi.h fficonfig.py)
diff --git a/Modules/_ctypes/libffi/fficonfig.h.in b/Modules/_ctypes/libffi/fficonfig.h.in
index 0fd2db5..bcc5a58 100644
--- a/Modules/_ctypes/libffi/fficonfig.h.in
+++ b/Modules/_ctypes/libffi/fficonfig.h.in
@@ -114,9 +114,22 @@
 /* Define to 1 if you have the ANSI C header files. */
 #undef STDC_HEADERS
 
+
 /* Define to 1 if your processor stores words with the most significant byte
-   first (like Motorola and SPARC, unlike Intel and VAX). */
+   first (like Motorola and SPARC, unlike Intel and VAX). 
+
+   The block below does compile-time checking for endianness on platforms
+   that use GCC and therefore allows compiling fat binaries on OSX by using
+   '-arch ppc -arch i386' as the compile flags. The phrasing was choosen
+   such that the configure-result is used on systems that don't use GCC.
+*/
+#ifdef __BIG_ENDIAN__
+#define WORDS_BIGENDIAN 1
+#else
+#ifndef __LITTLE_ENDIAN__
 #undef WORDS_BIGENDIAN
+#endif
+#endif
 
 
 #ifdef HAVE_HIDDEN_VISIBILITY_ATTRIBUTE
diff --git a/Modules/_ctypes/libffi/fficonfig.py.in b/Modules/_ctypes/libffi/fficonfig.py.in
index 2ed2347..3364049 100644
--- a/Modules/_ctypes/libffi/fficonfig.py.in
+++ b/Modules/_ctypes/libffi/fficonfig.py.in
@@ -6,6 +6,7 @@
     'MIPS_IRIX': ['src/mips/ffi.c', 'src/mips/o32.S', 'src/mips/n32.S'],
     'MIPS_LINUX': ['src/mips/ffi.c', 'src/mips/o32.S'],
     'X86': ['src/x86/ffi.c', 'src/x86/sysv.S'],
+    'X86_DARWIN': ['src/x86/ffi_darwin.c', 'src/x86/darwin.S'],
     'X86_WIN32': ['src/x86/ffi.c', 'src/x86/win32.S'],
     'SPARC': ['src/sparc/ffi.c', 'src/sparc/v8.S', 'src/sparc/v9.S'],
     'ALPHA': ['src/alpha/ffi.c', 'src/alpha/osf.S'],
@@ -26,10 +27,22 @@
     'PA': ['src/pa/linux.S', 'src/pa/ffi.c'],
 }
 
+# Build all darwin related files on all supported darwin architectures, this
+# makes it easier to build universal binaries.
+if 1:
+    all_darwin = ('X86_DARWIN', 'POWERPC_DARWIN')
+    all_darwin_files = []
+    for pn in all_darwin:
+        all_darwin_files.extend(ffi_platforms[pn])
+    for pn in all_darwin:
+        ffi_platforms[pn] = all_darwin_files
+    del all_darwin, all_darwin_files, pn
+
 ffi_srcdir = '@srcdir@'
-ffi_sources += ffi_platforms['@TARGET@']
+ffi_sources += ffi_platforms['@MKTARGET@']
 ffi_sources = [os.path.join('@srcdir@', f) for f in ffi_sources]
 
 ffi_cflags = '@CFLAGS@'
+# I think this may no longer be needed:
 if sys.platform == "openbsd3":
     ffi_cflags += " -fno-stack-protector"
diff --git a/Modules/_ctypes/libffi/src/darwin/ffitarget.h b/Modules/_ctypes/libffi/src/darwin/ffitarget.h
new file mode 100644
index 0000000..2dc308a
--- /dev/null
+++ b/Modules/_ctypes/libffi/src/darwin/ffitarget.h
@@ -0,0 +1,25 @@
+/*
+ * This file is for MacOSX only. Dispatch to the right architecture include
+ * file based on the current archictecture (instead of relying on a symlink
+ * created by configure). This makes is possible to build a univeral binary
+ * of ctypes in one go.
+ */
+#if defined(__i386__)
+
+#ifndef X86_DARWIN
+#define X86_DARWIN
+#endif
+#undef POWERPC_DARWIN
+
+#include "../src/x86/ffitarget.h"
+
+#elif defined(__ppc__)
+
+#ifndef POWERPC_DARWIN
+#define POWERPC_DARWIN
+#endif
+#undef X86_DARWIN
+
+#include "../src/powerpc/ffitarget.h"
+
+#endif
diff --git a/Modules/_ctypes/libffi/src/powerpc/darwin.S b/Modules/_ctypes/libffi/src/powerpc/darwin.S
index d8a1df5..917dc93 100644
--- a/Modules/_ctypes/libffi/src/powerpc/darwin.S
+++ b/Modules/_ctypes/libffi/src/powerpc/darwin.S
@@ -1,3 +1,4 @@
+#ifdef __ppc__
 /* -----------------------------------------------------------------------
    darwin.S - Copyright (c) 2000 John Hornkvist
 	      Copyright (c) 2004 Free Software Foundation, Inc.
@@ -243,3 +244,4 @@
 	.align LOG2_GPR_BYTES
 LLFB0$non_lazy_ptr:
 	.g_long LFB0
+#endif
diff --git a/Modules/_ctypes/libffi/src/powerpc/darwin_closure.S b/Modules/_ctypes/libffi/src/powerpc/darwin_closure.S
index 6d9a364..71054f5 100644
--- a/Modules/_ctypes/libffi/src/powerpc/darwin_closure.S
+++ b/Modules/_ctypes/libffi/src/powerpc/darwin_closure.S
@@ -1,3 +1,4 @@
+#ifdef __ppc__
 /* -----------------------------------------------------------------------
    darwin_closure.S - Copyright (c) 2002, 2003, 2004, Free Software Foundation,
    Inc. based on ppc_closure.S
@@ -315,3 +316,4 @@
 	.align LOG2_GPR_BYTES
 LLFB1$non_lazy_ptr:
 	.g_long LFB1
+#endif
diff --git a/Modules/_ctypes/libffi/src/powerpc/ffi_darwin.c b/Modules/_ctypes/libffi/src/powerpc/ffi_darwin.c
index d758f8f..1595b00 100644
--- a/Modules/_ctypes/libffi/src/powerpc/ffi_darwin.c
+++ b/Modules/_ctypes/libffi/src/powerpc/ffi_darwin.c
@@ -1,3 +1,4 @@
+#ifdef __ppc__
 /* -----------------------------------------------------------------------
    ffi.c - Copyright (c) 1998 Geoffrey Keating
 
@@ -380,18 +381,18 @@
 extern void ffi_call_AIX(/*@out@*/ extended_cif *,
 			 unsigned, unsigned,
 			 /*@out@*/ unsigned *,
-			 void (*fn)(),
-			 void (*fn2)());
+			 void (*fn)(void),
+			 void (*fn2)(extended_cif *, unsigned *const));
 extern void ffi_call_DARWIN(/*@out@*/ extended_cif *,
 			    unsigned, unsigned,
 			    /*@out@*/ unsigned *,
-			    void (*fn)(),
-			    void (*fn2)());
+			    void (*fn)(void),
+			    void (*fn2)(extended_cif *, unsigned *const));
 /*@=declundef@*/
 /*@=exportheader@*/
 
 void ffi_call(/*@dependent@*/ ffi_cif *cif,
-	      void (*fn)(),
+	      void (*fn)(void),
 	      /*@out@*/ void *rvalue,
 	      /*@dependent@*/ void **avalue)
 {
@@ -767,3 +768,4 @@
   /* Tell ffi_closure_ASM to perform return type promotions.  */
   return cif->rtype->type;
 }
+#endif
diff --git a/Modules/_ctypes/libffi/src/prep_cif.c b/Modules/_ctypes/libffi/src/prep_cif.c
index 0faa5dd..2db65ce 100644
--- a/Modules/_ctypes/libffi/src/prep_cif.c
+++ b/Modules/_ctypes/libffi/src/prep_cif.c
@@ -55,11 +55,29 @@
       /* Perform a sanity check on the argument type */
       FFI_ASSERT_VALID_TYPE(*ptr);
 
+#ifdef POWERPC_DARWIN
+      {
+	      int curalign;
+
+	      curalign = (*ptr)->alignment;
+	      if (ptr != &(arg->elements[0])) {
+		      if (curalign > 4 && curalign != 16) {
+			      curalign = 4;
+		      }
+	      }
+      arg->size = ALIGN(arg->size, curalign);
+      arg->size += (*ptr)->size;
+
+      arg->alignment = (arg->alignment > curalign) ? 
+	      arg->alignment : curalign;
+      }
+#else
       arg->size = ALIGN(arg->size, (*ptr)->alignment);
       arg->size += (*ptr)->size;
 
       arg->alignment = (arg->alignment > (*ptr)->alignment) ? 
 	arg->alignment : (*ptr)->alignment;
+#endif
 
       ptr++;
     }
@@ -89,6 +107,19 @@
 /* Perform machine independent ffi_cif preparation, then call
    machine dependent routine. */
 
+#ifdef X86_DARWIN
+static inline int struct_on_stack(int size)
+{
+	if (size > 8) return 1;
+	/* This is not what the ABI says, but is what is really implemented */
+	switch (size) {
+	case 1: case 2: case 4: case 8: return 0;
+	}
+	return 1;
+}
+#endif
+
+
 ffi_status ffi_prep_cif(/*@out@*/ /*@partial@*/ ffi_cif *cif, 
 			ffi_abi abi, unsigned int nargs, 
 			/*@dependent@*/ /*@out@*/ /*@partial@*/ ffi_type *rtype, 
@@ -124,6 +155,10 @@
 #ifdef SPARC
       && (cif->abi != FFI_V9 || cif->rtype->size > 32)
 #endif
+#ifdef X86_DARWIN
+
+      && (struct_on_stack(cif->rtype->size))
+#endif
       )
     bytes = STACK_ARG_SIZE(sizeof(void*));
 #endif
@@ -139,7 +174,16 @@
 	 check after the initialization.  */
       FFI_ASSERT_VALID_TYPE(*ptr);
 
-#if !defined __x86_64__ && !defined S390 && !defined PA
+#if defined(X86_DARWIN)
+      {
+	      int align = (*ptr)->alignment;
+	      if (align > 4) align = 4;
+	      if ((align - 1) & bytes)
+		 bytes = ALIGN(bytes, align);
+	      bytes += STACK_ARG_SIZE((*ptr)->size);
+      }
+
+#elif !defined __x86_64__ && !defined S390 && !defined PA
 #ifdef SPARC
       if (((*ptr)->type == FFI_TYPE_STRUCT
 	   && ((*ptr)->size > 16 || cif->abi != FFI_V9))
diff --git a/Modules/_ctypes/libffi/src/x86/darwin.S b/Modules/_ctypes/libffi/src/x86/darwin.S
new file mode 100644
index 0000000..c5e55b5
--- /dev/null
+++ b/Modules/_ctypes/libffi/src/x86/darwin.S
@@ -0,0 +1,195 @@
+#ifdef __i386__
+/* -----------------------------------------------------------------------
+   darwin.S - Copyright (c) 1996, 1998, 2001, 2002, 2003  Red Hat, Inc.
+   
+   X86 Foreign Function Interface 
+
+   Permission is hereby granted, free of charge, to any person obtaining
+   a copy of this software and associated documentation files (the
+   ``Software''), to deal in the Software without restriction, including
+   without limitation the rights to use, copy, modify, merge, publish,
+   distribute, sublicense, and/or sell copies of the Software, and to
+   permit persons to whom the Software is furnished to do so, subject to
+   the following conditions:
+
+   The above copyright notice and this permission notice shall be included
+   in all copies or substantial portions of the Software.
+
+   THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF ANY KIND, EXPRESS
+   OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+   IN NO EVENT SHALL CYGNUS SOLUTIONS BE LIABLE FOR ANY CLAIM, DAMAGES OR
+   OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+   ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+   OTHER DEALINGS IN THE SOFTWARE.
+   ----------------------------------------------------------------------- */
+  
+/*
+ * This file is based on sysv.S and then hacked up by Ronald who hasn't done
+ * assembly programming in 8 years.
+ */
+
+#ifndef __x86_64__
+
+#define LIBFFI_ASM	
+#include <fficonfig.h>
+#include <ffi.h>
+
+.text
+
+.globl _ffi_prep_args
+
+.align 4
+.globl _ffi_call_SYSV
+
+_ffi_call_SYSV:
+.LFB1:
+        pushl %ebp
+.LCFI0:
+        movl  %esp,%ebp
+.LCFI1:
+	/* Make room for all of the new args.  */
+	movl  16(%ebp),%ecx
+	subl  %ecx,%esp
+
+	movl  %esp,%eax
+
+	/* Place all of the ffi_prep_args in position  */
+	pushl 12(%ebp)
+	pushl %eax
+	call  *8(%ebp)
+
+	/* Return stack to previous state and call the function  */
+	addl  $8,%esp	
+
+	call  *28(%ebp)
+
+	/* Remove the space we pushed for the args  */
+	movl  16(%ebp),%ecx
+	addl  %ecx,%esp
+
+	/* Load %ecx with the return type code  */
+	movl  20(%ebp),%ecx	
+
+	/* If the return value pointer is NULL, assume no return value.  */
+	cmpl  $0,24(%ebp)
+	jne   retint
+
+	/* Even if there is no space for the return value, we are 
+	   obliged to handle floating-point values.  */
+	cmpl  $FFI_TYPE_FLOAT,%ecx
+	jne   noretval
+	fstp  %st(0)
+
+        jmp   epilogue
+
+retint:
+	cmpl  $FFI_TYPE_INT,%ecx
+	jne   retfloat
+	/* Load %ecx with the pointer to storage for the return value  */
+	movl  24(%ebp),%ecx	
+	movl  %eax,0(%ecx)
+	jmp   epilogue
+
+retfloat:
+	cmpl  $FFI_TYPE_FLOAT,%ecx
+	jne   retdouble
+	/* Load %ecx with the pointer to storage for the return value  */
+	movl  24(%ebp),%ecx	
+	fstps (%ecx)
+	jmp   epilogue
+
+retdouble:
+	cmpl  $FFI_TYPE_DOUBLE,%ecx
+	jne   retlongdouble
+	/* Load %ecx with the pointer to storage for the return value  */
+	movl  24(%ebp),%ecx	
+	fstpl (%ecx)
+	jmp   epilogue
+
+retlongdouble:
+	cmpl  $FFI_TYPE_LONGDOUBLE,%ecx
+	jne   retint64
+	/* Load %ecx with the pointer to storage for the return value  */
+	movl  24(%ebp),%ecx	
+	fstpt (%ecx)
+	jmp   epilogue
+	
+retint64:	
+	cmpl  $FFI_TYPE_SINT64,%ecx
+        jne   retstruct
+	/* Load %ecx with the pointer to storage for the return value  */
+	movl  24(%ebp),%ecx	
+	movl  %eax,0(%ecx)
+	movl  %edx,4(%ecx)
+	
+retstruct:
+	/* Nothing to do!  */
+
+noretval:
+epilogue:
+        movl %ebp,%esp
+        popl %ebp
+        ret
+.LFE1:
+.ffi_call_SYSV_end:
+#if 0
+        .size    ffi_call_SYSV,.ffi_call_SYSV_end-ffi_call_SYSV
+#endif
+
+#if 0
+	.section	.eh_frame,EH_FRAME_FLAGS,@progbits
+.Lframe1:
+	.long	.LECIE1-.LSCIE1	/* Length of Common Information Entry */
+.LSCIE1:
+	.long	0x0	/* CIE Identifier Tag */
+	.byte	0x1	/* CIE Version */
+#ifdef __PIC__
+	.ascii "zR\0"	/* CIE Augmentation */
+#else
+	.ascii "\0"	/* CIE Augmentation */
+#endif
+	.byte	0x1	/* .uleb128 0x1; CIE Code Alignment Factor */
+	.byte	0x7c	/* .sleb128 -4; CIE Data Alignment Factor */
+	.byte	0x8	/* CIE RA Column */
+#ifdef __PIC__
+	.byte	0x1	/* .uleb128 0x1; Augmentation size */
+	.byte	0x1b	/* FDE Encoding (pcrel sdata4) */
+#endif
+	.byte	0xc	/* DW_CFA_def_cfa */
+	.byte	0x4	/* .uleb128 0x4 */
+	.byte	0x4	/* .uleb128 0x4 */
+	.byte	0x88	/* DW_CFA_offset, column 0x8 */
+	.byte	0x1	/* .uleb128 0x1 */
+	.align 4
+.LECIE1:
+.LSFDE1:
+	.long	.LEFDE1-.LASFDE1	/* FDE Length */
+.LASFDE1:
+	.long	.LASFDE1-.Lframe1	/* FDE CIE offset */
+#ifdef __PIC__
+	.long	.LFB1-.	/* FDE initial location */
+#else
+	.long	.LFB1	/* FDE initial location */
+#endif
+	.long	.LFE1-.LFB1	/* FDE address range */
+#ifdef __PIC__
+	.byte	0x0	/* .uleb128 0x0; Augmentation size */
+#endif
+	.byte	0x4	/* DW_CFA_advance_loc4 */
+	.long	.LCFI0-.LFB1
+	.byte	0xe	/* DW_CFA_def_cfa_offset */
+	.byte	0x8	/* .uleb128 0x8 */
+	.byte	0x85	/* DW_CFA_offset, column 0x5 */
+	.byte	0x2	/* .uleb128 0x2 */
+	.byte	0x4	/* DW_CFA_advance_loc4 */
+	.long	.LCFI1-.LCFI0
+	.byte	0xd	/* DW_CFA_def_cfa_register */
+	.byte	0x5	/* .uleb128 0x5 */
+	.align 4
+.LEFDE1:
+#endif
+
+#endif /* ifndef __x86_64__ */
+
+#endif /* defined __i386__ */
diff --git a/Modules/_ctypes/libffi/src/x86/ffi_darwin.c b/Modules/_ctypes/libffi/src/x86/ffi_darwin.c
new file mode 100644
index 0000000..4f82b3a
--- /dev/null
+++ b/Modules/_ctypes/libffi/src/x86/ffi_darwin.c
@@ -0,0 +1,610 @@
+# ifdef __i386__
+/* -----------------------------------------------------------------------
+   ffi.c - Copyright (c) 1996, 1998, 1999, 2001  Red Hat, Inc.
+           Copyright (c) 2002  Ranjit Mathew
+           Copyright (c) 2002  Bo Thorsen
+           Copyright (c) 2002  Roger Sayle
+   
+   x86 Foreign Function Interface 
+
+   Permission is hereby granted, free of charge, to any person obtaining
+   a copy of this software and associated documentation files (the
+   ``Software''), to deal in the Software without restriction, including
+   without limitation the rights to use, copy, modify, merge, publish,
+   distribute, sublicense, and/or sell copies of the Software, and to
+   permit persons to whom the Software is furnished to do so, subject to
+   the following conditions:
+
+   The above copyright notice and this permission notice shall be included
+   in all copies or substantial portions of the Software.
+
+   THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF ANY KIND, EXPRESS
+   OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+   IN NO EVENT SHALL CYGNUS SOLUTIONS BE LIABLE FOR ANY CLAIM, DAMAGES OR
+   OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+   ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+   OTHER DEALINGS IN THE SOFTWARE.
+   ----------------------------------------------------------------------- */
+
+#ifndef __x86_64__
+
+#include <ffi.h>
+#include <ffi_common.h>
+
+#include <stdlib.h>
+
+/* ffi_prep_args is called by the assembly routine once stack space
+   has been allocated for the function's arguments */
+
+/*@-exportheader@*/
+void ffi_prep_args(char *stack, extended_cif *ecif);
+
+static inline int retval_on_stack(ffi_type* tp)
+{
+	if (tp->type == FFI_TYPE_STRUCT) {
+		int sz = tp->size;
+		if (sz > 8) {
+			return 1;
+		}
+		switch (sz) {
+		case 1: case 2: case 4: case 8: return 0;
+		default: return 1;
+		}
+	}
+	return 0;
+}
+
+
+void ffi_prep_args(char *stack, extended_cif *ecif)
+/*@=exportheader@*/
+{
+  register unsigned int i;
+  register void **p_argv;
+  register char *argp;
+  register ffi_type **p_arg;
+
+  argp = stack;
+
+  if (retval_on_stack(ecif->cif->rtype)) {
+      *(void **) argp = ecif->rvalue;
+      argp += 4;
+  }
+
+
+  p_argv = ecif->avalue;
+
+  for (i = ecif->cif->nargs, p_arg = ecif->cif->arg_types;
+       i != 0;
+       i--, p_arg++)
+    {
+      size_t z;
+
+      /* Align if necessary */
+      if ((sizeof(int) - 1) & (unsigned) argp)
+	      argp = (char *) ALIGN(argp, sizeof(int));
+
+      z = (*p_arg)->size;
+      if (z < sizeof(int))
+	{
+	  z = sizeof(int);
+	  switch ((*p_arg)->type)
+	    {
+	    case FFI_TYPE_SINT8:
+	      *(signed int *) argp = (signed int)*(SINT8 *)(* p_argv);
+	      break;
+
+	    case FFI_TYPE_UINT8:
+	      *(unsigned int *) argp = (unsigned int)*(UINT8 *)(* p_argv);
+	      break;
+
+	    case FFI_TYPE_SINT16:
+	      *(signed int *) argp = (signed int)*(SINT16 *)(* p_argv);
+	      break;
+
+	    case FFI_TYPE_UINT16:
+	      *(unsigned int *) argp = (unsigned int)*(UINT16 *)(* p_argv);
+	      break;
+
+	    case FFI_TYPE_SINT32:
+	      *(signed int *) argp = (signed int)*(SINT32 *)(* p_argv);
+	      break;
+
+	    case FFI_TYPE_UINT32:
+	      *(unsigned int *) argp = (unsigned int)*(UINT32 *)(* p_argv);
+	      break;
+
+	    case FFI_TYPE_STRUCT:
+	      *(unsigned int *) argp = (unsigned int)*(UINT32 *)(* p_argv);
+	      break;
+
+	    default:
+	      FFI_ASSERT(0);
+	    }
+	}
+      else
+	{
+	  memcpy(argp, *p_argv, z);
+	}
+      p_argv++;
+      argp += z;
+    }
+  
+  return;
+}
+
+/* Perform machine dependent cif processing */
+ffi_status ffi_prep_cif_machdep(ffi_cif *cif)
+{
+  /* Set the return type flag */
+  switch (cif->rtype->type)
+    {
+    case FFI_TYPE_VOID:
+#if !defined(X86_WIN32) 
+    case FFI_TYPE_STRUCT:
+#endif
+    case FFI_TYPE_SINT64:
+    case FFI_TYPE_FLOAT:
+    case FFI_TYPE_DOUBLE:
+    case FFI_TYPE_LONGDOUBLE:
+      cif->flags = (unsigned) cif->rtype->type;
+      break;
+
+    case FFI_TYPE_UINT64:
+      cif->flags = FFI_TYPE_SINT64;
+      break;
+
+#if defined X86_WIN32  
+
+    case FFI_TYPE_STRUCT:
+      if (cif->rtype->size == 1)
+        {
+          cif->flags = FFI_TYPE_SINT8; /* same as char size */
+        }
+      else if (cif->rtype->size == 2)
+        {
+          cif->flags = FFI_TYPE_SINT16; /* same as short size */
+        }
+      else if (cif->rtype->size == 4)
+        {
+          cif->flags = FFI_TYPE_INT; /* same as int type */
+        }
+      else if (cif->rtype->size == 8)
+        {
+          cif->flags = FFI_TYPE_SINT64; /* same as int64 type */
+        }
+      else
+        {
+          cif->flags = FFI_TYPE_STRUCT;
+        }
+      break;
+#endif
+
+    default:
+      cif->flags = FFI_TYPE_INT;
+      break;
+    }
+
+  /* Darwin: The stack needs to be aligned to a multiple of 16 bytes */
+#if 0
+  cif->bytes = (cif->bytes + 15) & ~0xF;
+#endif
+
+  return FFI_OK;
+}
+
+/*@-declundef@*/
+/*@-exportheader@*/
+extern void ffi_call_SYSV(void (*)(char *, extended_cif *), 
+			  /*@out@*/ extended_cif *, 
+			  unsigned, unsigned, 
+			  /*@out@*/ unsigned *, 
+			  void (*fn)(void));
+/*@=declundef@*/
+/*@=exportheader@*/
+
+#ifdef X86_WIN32
+/*@-declundef@*/
+/*@-exportheader@*/
+extern void ffi_call_STDCALL(void (*)(char *, extended_cif *),
+			  /*@out@*/ extended_cif *,
+			  unsigned, unsigned,
+			  /*@out@*/ unsigned *,
+			  void (*fn)(void));
+/*@=declundef@*/
+/*@=exportheader@*/
+#endif /* X86_WIN32 */
+
+void ffi_call(/*@dependent@*/ ffi_cif *cif, 
+	      void (*fn)(), 
+	      /*@out@*/ void *rvalue, 
+	      /*@dependent@*/ void **avalue)
+{
+  extended_cif ecif;
+  int flags;
+
+  ecif.cif = cif;
+  ecif.avalue = avalue;
+  
+  /* If the return value is a struct and we don't have a return	*/
+  /* value address then we need to make one		        */
+
+  if ((rvalue == NULL) && retval_on_stack(cif->rtype))
+    {
+      /*@-sysunrecog@*/
+      ecif.rvalue = alloca(cif->rtype->size);
+      /*@=sysunrecog@*/
+    }
+  else
+    ecif.rvalue = rvalue;
+
+  flags = cif->flags;
+  if (flags == FFI_TYPE_STRUCT) {
+    if (cif->rtype->size == 8) {
+	flags = FFI_TYPE_SINT64;
+    } else if (cif->rtype->size == 4) {
+        flags = FFI_TYPE_INT;
+    } else if (cif->rtype->size == 2) {
+        flags = FFI_TYPE_INT;
+    } else if (cif->rtype->size == 1) {
+        flags = FFI_TYPE_INT;
+    }
+  }
+
+  
+  switch (cif->abi) 
+    {
+    case FFI_SYSV:
+      /*@-usedef@*/
+      /* To avoid changing the assembly code make sure the size of the argument 
+       * block is a multiple of 16. Then add 8 to compensate for local variables
+       * in ffi_call_SYSV.
+       */
+      ffi_call_SYSV(ffi_prep_args, &ecif, ALIGN(cif->bytes, 16) +8, 
+		    flags, ecif.rvalue, fn);
+      /*@=usedef@*/
+      break;
+#ifdef X86_WIN32
+    case FFI_STDCALL:
+      /*@-usedef@*/
+      ffi_call_STDCALL(ffi_prep_args, &ecif, cif->bytes,
+		    cif->flags, ecif.rvalue, fn);
+      /*@=usedef@*/
+      break;
+#endif /* X86_WIN32 */
+    default:
+      FFI_ASSERT(0);
+      break;
+    }
+}
+
+
+/** private members **/
+
+static void ffi_prep_incoming_args_SYSV (char *stack, void **ret,
+					 void** args, ffi_cif* cif);
+static void ffi_closure_SYSV (ffi_closure *)
+     __attribute__ ((regparm(1)));
+#if !FFI_NO_RAW_API
+static void ffi_closure_raw_SYSV (ffi_raw_closure *)
+     __attribute__ ((regparm(1)));
+#endif
+
+/* This function is jumped to by the trampoline */
+
+static void
+ffi_closure_SYSV (closure)
+     ffi_closure *closure;
+{
+  // this is our return value storage
+  long double  res;
+
+  // our various things...
+  ffi_cif       *cif;
+  void         **arg_area;
+  unsigned short rtype;
+  void          *resp = (void*)&res;
+  void *args = __builtin_dwarf_cfa ();
+
+  cif         = closure->cif;
+  arg_area    = (void**) alloca (cif->nargs * sizeof (void*));  
+
+  /* this call will initialize ARG_AREA, such that each
+   * element in that array points to the corresponding 
+   * value on the stack; and if the function returns
+   * a structure, it will re-set RESP to point to the
+   * structure return address.  */
+
+  ffi_prep_incoming_args_SYSV(args, (void**)&resp, arg_area, cif);
+  
+  (closure->fun) (cif, resp, arg_area, closure->user_data);
+
+  rtype = cif->flags;
+
+  if (!retval_on_stack(cif->rtype) && cif->flags == FFI_TYPE_STRUCT) {
+      if (cif->rtype->size == 8) {
+         rtype = FFI_TYPE_SINT64;
+      } else {
+         rtype = FFI_TYPE_INT;
+      }
+  }
+
+  /* now, do a generic return based on the value of rtype */
+  if (rtype == FFI_TYPE_INT)
+    {
+      asm ("movl (%0),%%eax" : : "r" (resp) : "eax");
+    }
+  else if (rtype == FFI_TYPE_FLOAT)
+    {
+      asm ("flds (%0)" : : "r" (resp) : "st" );
+    }
+  else if (rtype == FFI_TYPE_DOUBLE)
+    {
+      asm ("fldl (%0)" : : "r" (resp) : "st", "st(1)" );
+    }
+  else if (rtype == FFI_TYPE_LONGDOUBLE)
+    {
+      asm ("fldt (%0)" : : "r" (resp) : "st", "st(1)" );
+    }
+  else if (rtype == FFI_TYPE_SINT64)
+    {
+      asm ("movl 0(%0),%%eax;"
+	   "movl 4(%0),%%edx" 
+	   : : "r"(resp)
+	   : "eax", "edx");
+    }
+#ifdef X86_WIN32
+  else if (rtype == FFI_TYPE_SINT8) /* 1-byte struct  */
+    {
+      asm ("movsbl (%0),%%eax" : : "r" (resp) : "eax");
+    }
+  else if (rtype == FFI_TYPE_SINT16) /* 2-bytes struct */
+    {
+      asm ("movswl (%0),%%eax" : : "r" (resp) : "eax");
+    }
+#endif
+}
+
+/*@-exportheader@*/
+static void 
+ffi_prep_incoming_args_SYSV(char *stack, void **rvalue,
+			    void **avalue, ffi_cif *cif)
+/*@=exportheader@*/
+{
+  register unsigned int i;
+  register void **p_argv;
+  register char *argp;
+  register ffi_type **p_arg;
+
+  argp = stack;
+
+  if (retval_on_stack(cif->rtype)) {
+    *rvalue = *(void **) argp;
+    argp += 4;
+  }
+
+  p_argv = avalue;
+
+  for (i = cif->nargs, p_arg = cif->arg_types; (i != 0); i--, p_arg++)
+    {
+      size_t z;
+
+      /* Align if necessary */
+      if ((sizeof(int) - 1) & (unsigned) argp) {
+	argp = (char *) ALIGN(argp, sizeof(int));
+      }
+
+      z = (*p_arg)->size;
+
+      /* because we're little endian, this is what it turns into.   */
+
+      *p_argv = (void*) argp;
+
+      p_argv++;
+      argp += z;
+    }
+  
+  return;
+}
+
+/* How to make a trampoline.  Derived from gcc/config/i386/i386.c. */
+
+#define FFI_INIT_TRAMPOLINE(TRAMP,FUN,CTX) \
+({ unsigned char *__tramp = (unsigned char*)(TRAMP); \
+   unsigned int  __fun = (unsigned int)(FUN); \
+   unsigned int  __ctx = (unsigned int)(CTX); \
+   unsigned int  __dis = __fun - ((unsigned int) __tramp + FFI_TRAMPOLINE_SIZE); \
+   *(unsigned char*) &__tramp[0] = 0xb8; \
+   *(unsigned int*)  &__tramp[1] = __ctx; /* movl __ctx, %eax */ \
+   *(unsigned char *)  &__tramp[5] = 0xe9; \
+   *(unsigned int*)  &__tramp[6] = __dis; /* jmp __fun  */ \
+ })
+
+
+/* the cif must already be prep'ed */
+
+ffi_status
+ffi_prep_closure (ffi_closure* closure,
+		  ffi_cif* cif,
+		  void (*fun)(ffi_cif*,void*,void**,void*),
+		  void *user_data)
+{
+  FFI_ASSERT (cif->abi == FFI_SYSV);
+
+  FFI_INIT_TRAMPOLINE (&closure->tramp[0], \
+		       &ffi_closure_SYSV,  \
+		       (void*)closure);
+    
+  closure->cif  = cif;
+  closure->user_data = user_data;
+  closure->fun  = fun;
+
+  return FFI_OK;
+}
+
+/* ------- Native raw API support -------------------------------- */
+
+#if !FFI_NO_RAW_API
+
+static void
+ffi_closure_raw_SYSV (closure)
+     ffi_raw_closure *closure;
+{
+  // this is our return value storage
+  long double    res;
+
+  // our various things...
+  ffi_raw         *raw_args;
+  ffi_cif         *cif;
+  unsigned short   rtype;
+  void            *resp = (void*)&res;
+
+  /* get the cif */
+  cif = closure->cif;
+
+  /* the SYSV/X86 abi matches the RAW API exactly, well.. almost */
+  raw_args = (ffi_raw*) __builtin_dwarf_cfa ();
+
+  (closure->fun) (cif, resp, raw_args, closure->user_data);
+
+  rtype = cif->flags;
+
+  /* now, do a generic return based on the value of rtype */
+  if (rtype == FFI_TYPE_INT)
+    {
+      asm ("movl (%0),%%eax" : : "r" (resp) : "eax");
+    }
+  else if (rtype == FFI_TYPE_FLOAT)
+    {
+      asm ("flds (%0)" : : "r" (resp) : "st" );
+    }
+  else if (rtype == FFI_TYPE_DOUBLE)
+    {
+      asm ("fldl (%0)" : : "r" (resp) : "st", "st(1)" );
+    }
+  else if (rtype == FFI_TYPE_LONGDOUBLE)
+    {
+      asm ("fldt (%0)" : : "r" (resp) : "st", "st(1)" );
+    }
+  else if (rtype == FFI_TYPE_SINT64)
+    {
+      asm ("movl 0(%0),%%eax; movl 4(%0),%%edx" 
+	   : : "r"(resp)
+	   : "eax", "edx");
+    }
+}
+
+ 
+
+
+ffi_status
+ffi_prep_raw_closure (ffi_raw_closure* closure,
+		      ffi_cif* cif,
+		      void (*fun)(ffi_cif*,void*,ffi_raw*,void*),
+		      void *user_data)
+{
+  int i;
+
+  FFI_ASSERT (cif->abi == FFI_SYSV);
+
+  // we currently don't support certain kinds of arguments for raw
+  // closures.  This should be implemented by a separate assembly language
+  // routine, since it would require argument processing, something we
+  // don't do now for performance.
+
+  for (i = cif->nargs-1; i >= 0; i--)
+    {
+      FFI_ASSERT (cif->arg_types[i]->type != FFI_TYPE_STRUCT);
+      FFI_ASSERT (cif->arg_types[i]->type != FFI_TYPE_LONGDOUBLE);
+    }
+  
+
+  FFI_INIT_TRAMPOLINE (&closure->tramp[0], &ffi_closure_raw_SYSV,
+		       (void*)closure);
+    
+  closure->cif  = cif;
+  closure->user_data = user_data;
+  closure->fun  = fun;
+
+  return FFI_OK;
+}
+
+static void 
+ffi_prep_args_raw(char *stack, extended_cif *ecif)
+{
+  memcpy (stack, ecif->avalue, ecif->cif->bytes);
+}
+
+/* we borrow this routine from libffi (it must be changed, though, to
+ * actually call the function passed in the first argument.  as of
+ * libffi-1.20, this is not the case.)
+ */
+
+extern void 
+ffi_call_SYSV(void (*)(char *, extended_cif *), 
+	      /*@out@*/ extended_cif *, 
+	      unsigned, unsigned, 
+	      /*@out@*/ unsigned *, 
+	      void (*fn)());
+
+#ifdef X86_WIN32
+extern void
+ffi_call_STDCALL(void (*)(char *, extended_cif *),
+	      /*@out@*/ extended_cif *,
+	      unsigned, unsigned,
+	      /*@out@*/ unsigned *,
+	      void (*fn)());
+#endif /* X86_WIN32 */
+
+void
+ffi_raw_call(/*@dependent@*/ ffi_cif *cif, 
+	     void (*fn)(), 
+	     /*@out@*/ void *rvalue, 
+	     /*@dependent@*/ ffi_raw *fake_avalue)
+{
+  extended_cif ecif;
+  void **avalue = (void **)fake_avalue;
+
+  ecif.cif = cif;
+  ecif.avalue = avalue;
+  
+  /* If the return value is a struct and we don't have a return	*/
+  /* value address then we need to make one		        */
+
+  if ((rvalue == NULL) && retval_on_stack(cif->rtype)) 
+    {
+      /*@-sysunrecog@*/
+      ecif.rvalue = alloca(cif->rtype->size);
+      /*@=sysunrecog@*/
+    }
+  else
+    ecif.rvalue = rvalue;
+    
+  
+  switch (cif->abi) 
+    {
+    case FFI_SYSV:
+      /*@-usedef@*/
+      ffi_call_SYSV(ffi_prep_args_raw, &ecif, cif->bytes, 
+		    cif->flags, ecif.rvalue, fn);
+      /*@=usedef@*/
+      break;
+#ifdef X86_WIN32
+    case FFI_STDCALL:
+      /*@-usedef@*/
+      ffi_call_STDCALL(ffi_prep_args_raw, &ecif, cif->bytes,
+		    cif->flags, ecif.rvalue, fn);
+      /*@=usedef@*/
+      break;
+#endif /* X86_WIN32 */
+    default:
+      FFI_ASSERT(0);
+      break;
+    }
+}
+
+#endif
+
+#endif /* __x86_64__  */
+
+#endif /* __i386__ */
diff --git a/Modules/_ctypes/libffi/src/x86/ffitarget.h b/Modules/_ctypes/libffi/src/x86/ffitarget.h
index 9500f40..8b20d3c 100644
--- a/Modules/_ctypes/libffi/src/x86/ffitarget.h
+++ b/Modules/_ctypes/libffi/src/x86/ffitarget.h
@@ -51,7 +51,7 @@
 #endif
 
   /* ---- Intel x86 and AMD x86-64 - */
-#if !defined(X86_WIN32) && (defined(__i386__) || defined(__x86_64__))
+#if !defined(X86_WIN32) && (defined(__i386__) || defined(__x86_64__)) 
   FFI_SYSV,
   FFI_UNIX64,   /* Unix variants all use the same ABI for x86-64  */
 #ifdef __i386__
diff --git a/Modules/_ctypes/libffi_msvc/ffi.c b/Modules/_ctypes/libffi_msvc/ffi.c
index 5c49b39..e5600b2 100644
--- a/Modules/_ctypes/libffi_msvc/ffi.c
+++ b/Modules/_ctypes/libffi_msvc/ffi.c
@@ -26,8 +26,6 @@
    OTHER DEALINGS IN THE SOFTWARE.
    ----------------------------------------------------------------------- */
 
-#ifndef __x86_64__
-
 #include <ffi.h>
 #include <ffi_common.h>
 
@@ -143,11 +141,7 @@
 
 /*@-declundef@*/
 /*@-exportheader@*/
-#ifdef _MSC_VER
 extern int
-#else
-extern void
-#endif
 ffi_call_SYSV(void (*)(char *, extended_cif *), 
 	      /*@out@*/ extended_cif *, 
 	      unsigned, unsigned, 
@@ -156,14 +150,9 @@
 /*@=declundef@*/
 /*@=exportheader@*/
 
-#if defined(X86_WIN32) || defined(_MSC_VER)
 /*@-declundef@*/
 /*@-exportheader@*/
-#ifdef _MSC_VER
 extern int
-#else
-extern void
-#endif
 ffi_call_STDCALL(void (*)(char *, extended_cif *),
 		 /*@out@*/ extended_cif *,
 		 unsigned, unsigned,
@@ -171,13 +160,8 @@
 		 void (*fn)());
 /*@=declundef@*/
 /*@=exportheader@*/
-#endif /* X86_WIN32 || _MSC_VER*/
 
-#ifdef _MSC_VER
 int
-#else
-void
-#endif
 ffi_call(/*@dependent@*/ ffi_cif *cif, 
 	 void (*fn)(), 
 	 /*@out@*/ void *rvalue, 
@@ -206,24 +190,18 @@
     {
     case FFI_SYSV:
       /*@-usedef@*/
-#ifdef _MSC_VER
-      return
-#endif
-	      ffi_call_SYSV(ffi_prep_args, &ecif, cif->bytes, 
-			    cif->flags, ecif.rvalue, fn);
+      return ffi_call_SYSV(ffi_prep_args, &ecif, cif->bytes, 
+			   cif->flags, ecif.rvalue, fn);
       /*@=usedef@*/
       break;
-#if defined(X86_WIN32) || defined(_MSC_VER)
+
     case FFI_STDCALL:
       /*@-usedef@*/
-#ifdef _MSC_VER
-      return
-#endif
-	      ffi_call_STDCALL(ffi_prep_args, &ecif, cif->bytes,
-			       cif->flags, ecif.rvalue, fn);
+      return ffi_call_STDCALL(ffi_prep_args, &ecif, cif->bytes,
+			      cif->flags, ecif.rvalue, fn);
       /*@=usedef@*/
       break;
-#endif /* X86_WIN32 */
+
     default:
       FFI_ASSERT(0);
       break;
@@ -236,23 +214,10 @@
 
 static void ffi_prep_incoming_args_SYSV (char *stack, void **ret,
 					 void** args, ffi_cif* cif);
-#ifndef _MSC_VER
-static void ffi_closure_SYSV (ffi_closure *)
-     __attribute__ ((regparm(1)));
-static void ffi_closure_raw_SYSV (ffi_raw_closure *)
-     __attribute__ ((regparm(1)));
-#endif
-
 /* This function is jumped to by the trampoline */
 
-#ifdef _MSC_VER
 static void __fastcall
 ffi_closure_SYSV (ffi_closure *closure, int *argp)
-#else
-static void
-ffi_closure_SYSV (closure)
-     ffi_closure *closure;
-#endif
 {
   // this is our return value storage
   long double    res;
@@ -262,11 +227,11 @@
   void         **arg_area;
   unsigned short rtype;
   void          *resp = (void*)&res;
-#ifdef _MSC_VER
+//#ifdef _MSC_VER
   void *args = &argp[1];
-#else
-  void *args = __builtin_dwarf_cfa ();
-#endif
+//#else
+//  void *args = __builtin_dwarf_cfa ();
+//#endif
 
   cif         = closure->cif;
   arg_area    = (void**) alloca (cif->nargs * sizeof (void*));  
@@ -390,7 +355,7 @@
 
 /* MOV EDX, ESP is 0x8b 0xd4 */
 
-#ifdef _MSC_VER
+//#ifdef _MSC_VER
 
 #define FFI_INIT_TRAMPOLINE(TRAMP,FUN,CTX,BYTES) \
 { unsigned char *__tramp = (unsigned char*)(TRAMP); \
@@ -407,18 +372,18 @@
    *(unsigned short*) &__tramp[13] = BYTES; \
  }
 
-#else
-#define FFI_INIT_TRAMPOLINE(TRAMP,FUN,CTX,BYTES) \
-({ unsigned char *__tramp = (unsigned char*)(TRAMP); \
-   unsigned int  __fun = (unsigned int)(FUN); \
-   unsigned int  __ctx = (unsigned int)(CTX); \
-   unsigned int  __dis = __fun - ((unsigned int) __tramp + FFI_TRAMPOLINE_SIZE); \
-   *(unsigned char*) &__tramp[0] = 0xb8; \
-   *(unsigned int*)  &__tramp[1] = __ctx; /* movl __ctx, %eax */ \
-   *(unsigned char *)  &__tramp[5] = 0xe9; \
-   *(unsigned int*)  &__tramp[6] = __dis; /* jmp __fun  */ \
- })
-#endif
+//#else
+//#define FFI_INIT_TRAMPOLINE(TRAMP,FUN,CTX,BYTES) \
+//({ unsigned char *__tramp = (unsigned char*)(TRAMP); \
+//   unsigned int  __fun = (unsigned int)(FUN); \
+//   unsigned int  __ctx = (unsigned int)(CTX); \
+//   unsigned int  __dis = __fun - ((unsigned int) __tramp + FFI_TRAMPOLINE_SIZE); \
+//   *(unsigned char*) &__tramp[0] = 0xb8; \
+//   *(unsigned int*)  &__tramp[1] = __ctx; /* movl __ctx, %eax */ \
+//   *(unsigned char *)  &__tramp[5] = 0xe9; \
+//   *(unsigned int*)  &__tramp[6] = __dis; /* jmp __fun  */ \
+// })
+//#endif
 
 /* the cif must already be prep'ed */
 
@@ -433,10 +398,8 @@
   
   if (cif->abi == FFI_SYSV)
     bytes = 0;
-#ifdef _MSC_VER
   else if (cif->abi == FFI_STDCALL)
     bytes = cif->bytes;
-#endif
   else
     return FFI_BAD_ABI;
 
@@ -450,5 +413,3 @@
 
   return FFI_OK;
 }
-
-#endif /* __x86_64__  */
diff --git a/Modules/_ctypes/libffi_msvc/ffi.h b/Modules/_ctypes/libffi_msvc/ffi.h
index b9d31fd..203142d 100644
--- a/Modules/_ctypes/libffi_msvc/ffi.h
+++ b/Modules/_ctypes/libffi_msvc/ffi.h
@@ -272,11 +272,7 @@
 			/*@dependent@*/ /*@out@*/ /*@partial@*/ ffi_type *rtype, 
 			/*@dependent@*/ ffi_type **atypes);
 
-#ifdef _MSC_VER
 int
-#else
-void
-#endif
 ffi_call(/*@dependent@*/ ffi_cif *cif, 
 	 void (*fn)(), 
 	 /*@out@*/ void *rvalue, 
diff --git a/Modules/_ctypes/libffi_msvc/ffi_common.h b/Modules/_ctypes/libffi_msvc/ffi_common.h
index 1b948d5..43fb83b 100644
--- a/Modules/_ctypes/libffi_msvc/ffi_common.h
+++ b/Modules/_ctypes/libffi_msvc/ffi_common.h
@@ -13,24 +13,7 @@
 #endif
 
 #include <fficonfig.h>
-
-/* Do not move this. Some versions of AIX are very picky about where
-   this is positioned. */
-#ifdef __GNUC__
-# define alloca __builtin_alloca
-#else
-# if HAVE_ALLOCA_H
-#  include <alloca.h>
-# else
-#  ifdef _AIX
- #pragma alloca
-#  else
-#   ifndef alloca /* predefined by HP cc +Olibcalls */
-char *alloca ();
-#   endif
-#  endif
-# endif
-#endif
+#include <malloc.h>
 
 /* Check for the existence of memcpy. */
 #if STDC_HEADERS
diff --git a/Modules/_ctypes/libffi_msvc/ffitarget.h b/Modules/_ctypes/libffi_msvc/ffitarget.h
index c9d95bc..57d275b 100644
--- a/Modules/_ctypes/libffi_msvc/ffitarget.h
+++ b/Modules/_ctypes/libffi_msvc/ffitarget.h
@@ -43,23 +43,21 @@
   FFI_FIRST_ABI = 0,
 
   /* ---- Intel x86 Win32 ---------- */
-#if defined(X86_WIN32) || defined(_MSC_VER)
   FFI_SYSV,
   FFI_STDCALL,
   /* TODO: Add fastcall support for the sake of completeness */
   FFI_DEFAULT_ABI = FFI_SYSV,
-#endif
 
   /* ---- Intel x86 and AMD x86-64 - */
-#if !defined(X86_WIN32) && (defined(__i386__) || defined(__x86_64__))
-  FFI_SYSV,
-  FFI_UNIX64,   /* Unix variants all use the same ABI for x86-64  */
-#ifdef __i386__
-  FFI_DEFAULT_ABI = FFI_SYSV,
-#else
-  FFI_DEFAULT_ABI = FFI_UNIX64,
-#endif
-#endif
+/* #if !defined(X86_WIN32) && (defined(__i386__) || defined(__x86_64__)) */
+/*   FFI_SYSV, */
+/*   FFI_UNIX64,*/   /* Unix variants all use the same ABI for x86-64  */
+/* #ifdef __i386__ */
+/*   FFI_DEFAULT_ABI = FFI_SYSV, */
+/* #else */
+/*   FFI_DEFAULT_ABI = FFI_UNIX64, */
+/* #endif */
+/* #endif */
 
   FFI_LAST_ABI = FFI_DEFAULT_ABI + 1
 } ffi_abi;
diff --git a/Modules/_ctypes/libffi_msvc/mingwin32.S b/Modules/_ctypes/libffi_msvc/mingwin32.S
new file mode 100644
index 0000000..e71f2b2
--- /dev/null
+++ b/Modules/_ctypes/libffi_msvc/mingwin32.S
@@ -0,0 +1,228 @@
+/* -----------------------------------------------------------------------
+   win32.S - Copyright (c) 1996, 1998, 2001, 2002  Red Hat, Inc.
+	     Copyright (c) 2001  John Beniton
+	     Copyright (c) 2002  Ranjit Mathew
+			
+ 
+   X86 Foreign Function Interface
+ 
+   Permission is hereby granted, free of charge, to any person obtaining
+   a copy of this software and associated documentation files (the
+   ``Software''), to deal in the Software without restriction, including
+   without limitation the rights to use, copy, modify, merge, publish,
+   distribute, sublicense, and/or sell copies of the Software, and to
+   permit persons to whom the Software is furnished to do so, subject to
+   the following conditions:
+ 
+   The above copyright notice and this permission notice shall be included
+   in all copies or substantial portions of the Software.
+ 
+   THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF ANY KIND, EXPRESS
+   OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+   IN NO EVENT SHALL CYGNUS SOLUTIONS BE LIABLE FOR ANY CLAIM, DAMAGES OR
+   OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+   ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+   OTHER DEALINGS IN THE SOFTWARE.
+   ----------------------------------------------------------------------- */
+ 
+#define LIBFFI_ASM
+#include <fficonfig.h>
+#include <ffi.h>
+ 
+.text
+ 
+.globl ffi_prep_args
+ 
+        # This assumes we are using gas.
+        .balign 16
+.globl _ffi_call_SYSV
+ 
+_ffi_call_SYSV:
+        pushl %ebp
+        movl  %esp,%ebp
+ 
+        # Make room for all of the new args.
+        movl  16(%ebp),%ecx                                                     
+        subl  %ecx,%esp
+ 
+        movl  %esp,%eax
+ 
+        # Place all of the ffi_prep_args in position
+        pushl 12(%ebp)
+        pushl %eax
+        call  *8(%ebp)
+ 
+        # Return stack to previous state and call the function
+        addl  $8,%esp
+ 
+        # FIXME: Align the stack to a 128-bit boundary to avoid
+        # potential performance hits.
+
+	call  *28(%ebp)
+ 
+        # Remove the space we pushed for the args
+        movl  16(%ebp),%ecx
+        addl  %ecx,%esp
+ 
+        # Load %ecx with the return type code
+        movl  20(%ebp),%ecx
+ 
+        # If the return value pointer is NULL, assume no return value.
+        cmpl  $0,24(%ebp)
+        jne   retint
+ 
+        # Even if there is no space for the return value, we are
+        # obliged to handle floating-point values.
+        cmpl  $2,%ecx  # Float_type
+        jne   noretval
+        fstp  %st(0)
+ 
+        jmp   epilogue
+ 
+retint:
+        cmpl  $1,%ecx    # Int_type
+        jne   retfloat
+        # Load %ecx with the pointer to storage for the return value
+        movl  24(%ebp),%ecx
+        movl  %eax,0(%ecx)
+        jmp   epilogue
+ 
+retfloat:
+        cmpl  $2,%ecx   # Float_type
+        jne   retdouble   
+         # Load %ecx with the pointer to storage for the return value
+        movl  24(%ebp),%ecx
+        fstps (%ecx)
+        jmp   epilogue
+ 
+retdouble:
+        cmpl  $3,%ecx     # Double_type
+        jne   retlongdouble
+        # Load %ecx with the pointer to storage for the return value
+        movl  24(%ebp),%ecx
+        fstpl (%ecx)
+        jmp   epilogue
+ 
+retlongdouble:
+        cmpl  $4,%ecx     # Longdouble_type
+        jne   retint64
+        # Load %ecx with the pointer to storage for the return value
+        movl  24(%ebp),%ecx
+        fstpt (%ecx)
+        jmp   epilogue
+ 
+retint64:
+        cmpl  $12,%ecx      # SINT64_type
+        jne   retstruct
+        # Load %ecx with the pointer to storage for the return value
+        movl  24(%ebp),%ecx
+        movl  %eax,0(%ecx)
+        movl  %edx,4(%ecx)
+ 
+retstruct:
+        # Nothing to do!
+ 
+noretval:
+epilogue:
+        movl %ebp,%esp
+        popl %ebp
+        ret
+ 
+.ffi_call_SYSV_end:
+
+        # This assumes we are using gas.
+        .balign 16
+.globl _ffi_call_STDCALL
+
+_ffi_call_STDCALL:
+        pushl %ebp
+        movl  %esp,%ebp
+
+        # Make room for all of the new args.
+        movl  16(%ebp),%ecx 
+        subl  %ecx,%esp
+
+        movl  %esp,%eax
+
+        # Place all of the ffi_prep_args in position
+        pushl 12(%ebp)
+        pushl %eax
+        call  *8(%ebp)
+
+        # Return stack to previous state and call the function
+        addl  $8,%esp
+
+        # FIXME: Align the stack to a 128-bit boundary to avoid
+        # potential performance hits.
+
+        call  *28(%ebp)
+
+        # stdcall functions pop arguments off the stack themselves
+
+        # Load %ecx with the return type code
+        movl  20(%ebp),%ecx
+
+        # If the return value pointer is NULL, assume no return value.
+        cmpl  $0,24(%ebp)
+        jne   sc_retint
+
+        # Even if there is no space for the return value, we are
+        # obliged to handle floating-point values.
+        cmpl  $2,%ecx      # Float_type
+        jne   sc_noretval
+        fstp  %st(0)
+
+        jmp   sc_epilogue
+
+sc_retint:
+        cmpl  $1,%ecx       # Int_type
+        jne   sc_retfloat
+        # Load %ecx with the pointer to storage for the return value
+        movl  24(%ebp),%ecx
+        movl  %eax,0(%ecx)
+        jmp   sc_epilogue
+
+sc_retfloat:
+        cmpl  $2,%ecx       # Float_type
+        jne   sc_retdouble
+         # Load %ecx with the pointer to storage for the return value
+        movl  24(%ebp),%ecx
+        fstps (%ecx)
+        jmp   sc_epilogue
+
+sc_retdouble:
+        cmpl  $2,%ecx       # Double_type
+        jne   sc_retlongdouble
+        # Load %ecx with the pointer to storage for the return value
+        movl  24(%ebp),%ecx
+        fstpl (%ecx)
+        jmp   sc_epilogue
+
+sc_retlongdouble:
+        cmpl  $4,%ecx      # Longdouble_type 
+        jne   sc_retint64
+        # Load %ecx with the pointer to storage for the return value
+        movl  24(%ebp),%ecx
+        fstpt (%ecx)
+        jmp   sc_epilogue
+
+sc_retint64:
+        cmpl  $12,%ecx      # SINT64_Type
+        jne   sc_retstruct
+        # Load %ecx with the pointer to storage for the return value
+        movl  24(%ebp),%ecx
+        movl  %eax,0(%ecx)
+        movl  %edx,4(%ecx)
+
+sc_retstruct:
+        # Nothing to do!
+
+sc_noretval:
+sc_epilogue:
+        movl %ebp,%esp
+        popl %ebp
+        ret
+
+.ffi_call_STDCALL_end:
+
diff --git a/Modules/_ctypes/libffi_msvc/prep_cif.c b/Modules/_ctypes/libffi_msvc/prep_cif.c
index 9edce2f..2650fa0 100644
--- a/Modules/_ctypes/libffi_msvc/prep_cif.c
+++ b/Modules/_ctypes/libffi_msvc/prep_cif.c
@@ -147,7 +147,7 @@
       else
 #endif
 	{
-#ifndef _MSC_VER
+#if !defined(_MSC_VER) && !defined(__MINGW32__)
 		/* Don't know if this is a libffi bug or not.  At least on
 		   Windows with MSVC, function call parameters are *not*
 		   aligned in the same way as structure fields are, they are
diff --git a/Modules/_ctypes/libffi_msvc/win32.S b/Modules/_ctypes/libffi_msvc/win32.S
index 40743af..cc82ab9 100644
--- a/Modules/_ctypes/libffi_msvc/win32.S
+++ b/Modules/_ctypes/libffi_msvc/win32.S
@@ -41,7 +41,11 @@
 _ffi_call_SYSV:
         pushl %ebp
         movl  %esp,%ebp
- 
+
+	#THe: save previous %esi, and store the current stack pointer in %esi
+	pushl %esi
+	movl %esp,%esi
+
         # Make room for all of the new args.
         movl  16(%ebp),%ecx                                                     
         subl  %ecx,%esp
@@ -64,7 +68,9 @@
         # Remove the space we pushed for the args
         movl  16(%ebp),%ecx
         addl  %ecx,%esp
- 
+
+	sub %esp,%esi # calculate stack pointer difference
+
         # Load %ecx with the return type code
         movl  20(%ebp),%ecx
  
@@ -125,6 +131,8 @@
  
 noretval:
 epilogue:
+	movl %esi,%eax # return the stack pointer detlta in %eax
+	popl %esi # restore previous %esi
         movl %ebp,%esp
         popl %ebp
         ret
@@ -139,6 +147,10 @@
         pushl %ebp
         movl  %esp,%ebp
 
+	#THe: save previous %esi, and store the current stack pointer in %esi
+	pushl %esi
+	movl %esp,%esi
+	
         # Make room for all of the new args.
         movl  16(%ebp),%ecx 
         subl  %ecx,%esp
@@ -158,6 +170,8 @@
 
         call  *28(%ebp)
 
+	sub %esp,%esi # difference in stack
+
         # stdcall functions pop arguments off the stack themselves
 
         # Load %ecx with the return type code
@@ -220,6 +234,8 @@
 
 sc_noretval:
 sc_epilogue:
+        movl %esi,%eax # return the stack difference
+	popl %esi # restore previous %esi value
         movl %ebp,%esp
         popl %ebp
         ret
diff --git a/Modules/_elementtree.c b/Modules/_elementtree.c
index 7e36ea3..871aa18 100644
--- a/Modules/_elementtree.c
+++ b/Modules/_elementtree.c
@@ -327,8 +327,10 @@
 
     if (attrib != Py_None) {
 
-        if (element_new_extra(self, attrib) < 0)
+        if (element_new_extra(self, attrib) < 0) {
+            PyObject_Del(self);
             return NULL;
+	}
 
         self->extra->length = 0;
         self->extra->allocated = STATIC_CHILDREN;
diff --git a/Modules/_hotshot.c b/Modules/_hotshot.c
index 2ee4eb9..3ad0a9e 100644
--- a/Modules/_hotshot.c
+++ b/Modules/_hotshot.c
@@ -1420,7 +1420,7 @@
     char *buffer;
     char cwdbuffer[PATH_MAX];
     PyObject *temp;
-    int i, len;
+    Py_ssize_t i, len;
 
     buffer = get_version_string();
     if (buffer == NULL) {
diff --git a/Modules/_localemodule.c b/Modules/_localemodule.c
index e3d1e7f..58beb5c 100644
--- a/Modules/_localemodule.c
+++ b/Modules/_localemodule.c
@@ -12,11 +12,14 @@
 #include "Python.h"
 
 #include <stdio.h>
-#include <errno.h>
 #include <locale.h>
 #include <string.h>
 #include <ctype.h>
 
+#ifndef DONT_HAVE_ERRNO_H
+#include <errno.h>
+#endif
+
 #ifdef HAVE_LANGINFO_H
 #include <langinfo.h>
 #endif
diff --git a/Modules/_sqlite/adapters.c b/Modules/_sqlite/adapters.c
deleted file mode 100644
index e6fde03..0000000
--- a/Modules/_sqlite/adapters.c
+++ /dev/null
@@ -1,40 +0,0 @@
-/* adapters.c - default adapters
- *
- * Copyright (C) 2005 Gerhard Häring <gh@ghaering.de>
- *
- * This file is part of pysqlite.
- *
- * This software is provided 'as-is', without any express or implied
- * warranty.  In no event will the authors be held liable for any damages
- * arising from the use of this software.
- *
- * Permission is granted to anyone to use this software for any purpose,
- * including commercial applications, and to alter it and redistribute it
- * freely, subject to the following restrictions:
- *
- * 1. The origin of this software must not be misrepresented; you must not
- *    claim that you wrote the original software. If you use this software
- *    in a product, an acknowledgment in the product documentation would be
- *    appreciated but is not required.
- * 2. Altered source versions must be plainly marked as such, and must not be
- *    misrepresented as being the original software.
- * 3. This notice may not be removed or altered from any source distribution.
- */
-
-#include "util.h"
-#include "module.h"
-#include "adapters.h"
-
-/* dummy, will be implemented in a later version */
-
-PyObject* adapt_date(PyObject* self, PyObject* args, PyObject* kwargs)
-{
-    Py_INCREF(Py_None);
-    return Py_None;
-}
-
-PyObject* adapt_datetime(PyObject* self, PyObject* args, PyObject* kwargs)
-{
-    Py_INCREF(Py_None);
-    return Py_None;
-}
diff --git a/Modules/_sqlite/adapters.h b/Modules/_sqlite/adapters.h
deleted file mode 100644
index d2e8479..0000000
--- a/Modules/_sqlite/adapters.h
+++ /dev/null
@@ -1,33 +0,0 @@
-/* adapters.h - default adapters
- *
- * Copyright (C) 2005 Gerhard Häring <gh@ghaering.de>
- *
- * This file is part of pysqlite.
- *
- * This software is provided 'as-is', without any express or implied
- * warranty.  In no event will the authors be held liable for any damages
- * arising from the use of this software.
- *
- * Permission is granted to anyone to use this software for any purpose,
- * including commercial applications, and to alter it and redistribute it
- * freely, subject to the following restrictions:
- *
- * 1. The origin of this software must not be misrepresented; you must not
- *    claim that you wrote the original software. If you use this software
- *    in a product, an acknowledgment in the product documentation would be
- *    appreciated but is not required.
- * 2. Altered source versions must be plainly marked as such, and must not be
- *    misrepresented as being the original software.
- * 3. This notice may not be removed or altered from any source distribution.
- */
-
-#ifndef PYSQLITE_ADAPTERS_H
-#define PYSQLITE_ADAPTERS_H
-#include "Python.h"
-#include "pythread.h"
-#include "sqlite3.h"
-
-PyObject* adapt_date(PyObject* self, PyObject* args, PyObject* kwargs);
-PyObject* adapt_datetime(PyObject* self, PyObject* args, PyObject* kwargs);
-
-#endif
diff --git a/Modules/_sqlite/cache.c b/Modules/_sqlite/cache.c
index d102e97..6962695 100644
--- a/Modules/_sqlite/cache.c
+++ b/Modules/_sqlite/cache.c
@@ -22,6 +22,7 @@
  */
 
 #include "cache.h"
+#include <limits.h>
 
 /* only used internally */
 Node* new_node(PyObject* key, PyObject* data)
@@ -60,11 +61,11 @@
 
     self->factory = NULL;
 
-    if (!PyArg_ParseTuple(args, "O|i", &factory, &size))
-    {
-        return -1; 
+    if (!PyArg_ParseTuple(args, "O|i", &factory, &size)) {
+        return -1;
     }
 
+    /* minimum cache size is 5 entries */
     if (size < 5) {
         size = 5;
     }
@@ -95,6 +96,7 @@
         return;
     }
 
+    /* iterate over all nodes and deallocate them */
     node = self->first;
     while (node) {
         delete_node = node;
@@ -119,7 +121,14 @@
 
     node = (Node*)PyDict_GetItem(self->mapping, key);
     if (node) {
-        node->count++;
+        /* an entry for this key already exists in the cache */
+
+        /* increase usage counter of the node found */
+        if (node->count < LONG_MAX) {
+            node->count++;
+        }
+
+        /* if necessary, reorder entries in the cache by swapping positions */
         if (node->prev && node->count > node->prev->count) {
             ptr = node->prev;
 
@@ -149,6 +158,10 @@
             ptr->prev = node;
         }
     } else {
+        /* There is no entry for this key in the cache, yet. We'll insert a new
+         * entry in the cache, and make space if necessary by throwing the
+         * least used item out of the cache. */
+
         if (PyDict_Size(self->mapping) == self->size) {
             if (self->last) {
                 node = self->last;
@@ -253,7 +266,7 @@
 
 static PyMethodDef cache_methods[] = {
     {"get", (PyCFunction)cache_get, METH_O,
-        PyDoc_STR("Gets an entry from the cache.")},
+        PyDoc_STR("Gets an entry from the cache or calls the factory function to produce one.")},
     {"display", (PyCFunction)cache_display, METH_NOARGS,
         PyDoc_STR("For debugging only.")},
     {NULL, NULL}
diff --git a/Modules/_sqlite/cache.h b/Modules/_sqlite/cache.h
index 5cc16f3..1f13907 100644
--- a/Modules/_sqlite/cache.h
+++ b/Modules/_sqlite/cache.h
@@ -1,6 +1,6 @@
 /* cache.h - definitions for the LRU cache
  *
- * Copyright (C) 2004-2005 Gerhard Häring <gh@ghaering.de>
+ * Copyright (C) 2004-2006 Gerhard Häring <gh@ghaering.de>
  *
  * This file is part of pysqlite.
  *
@@ -25,6 +25,10 @@
 #define PYSQLITE_CACHE_H
 #include "Python.h"
 
+/* The LRU cache is implemented as a combination of a doubly-linked with a
+ * dictionary. The list items are of type 'Node' and the dictionary has the
+ * nodes as values. */
+
 typedef struct _Node
 {
     PyObject_HEAD
@@ -39,10 +43,18 @@
 {
     PyObject_HEAD
     int size;
+
+    /* a dictionary mapping keys to Node entries */
     PyObject* mapping;
+
+    /* the factory callable */
     PyObject* factory;
+
     Node* first;
     Node* last;
+
+    /* if set, decrement the factory function when the Cache is deallocated.
+     * this is almost always desirable, but not in the pysqlite context */
     int decref_factory;
 } Cache;
 
diff --git a/Modules/_sqlite/connection.c b/Modules/_sqlite/connection.c
index 78aad37..64e43eb 100644
--- a/Modules/_sqlite/connection.c
+++ b/Modules/_sqlite/connection.c
@@ -56,6 +56,7 @@
     self->begin_statement = NULL;
 
     self->statement_cache = NULL;
+    self->statements = NULL;
 
     Py_INCREF(Py_None);
     self->row_factory = Py_None;
@@ -74,6 +75,9 @@
 
     if (!isolation_level) {
         isolation_level = PyString_FromString("");
+        if (!isolation_level) {
+            return -1;
+        }
     } else {
         Py_INCREF(isolation_level);
     }
@@ -86,6 +90,12 @@
         return -1;
     }
 
+    self->statements = PyList_New(0);
+    if (!self->statements) {
+        return -1;
+    }
+    self->created_statements = 0;
+
     /* By default, the Cache class INCREFs the factory in its initializer, and
      * decrefs it in its deallocator method. Since this would create a circular
      * reference here, we're breaking it by decrementing self, and telling the
@@ -126,6 +136,7 @@
     return 0;
 }
 
+/* Empty the entire statement cache of this connection */
 void flush_statement_cache(Connection* self)
 {
     Node* node;
@@ -147,15 +158,16 @@
 
 void reset_all_statements(Connection* self)
 {
-    Node* node;
-    Statement* statement;
+    int i;
+    PyObject* weakref;
+    PyObject* statement;
 
-    node = self->statement_cache->first;
-
-    while (node) {
-        statement = (Statement*)(node->data);
-        (void)statement_reset(statement);
-        node = node->next;
+    for (i = 0; i < PyList_Size(self->statements); i++) {
+        weakref = PyList_GetItem(self->statements, i);
+        statement = PyWeakref_GetObject(weakref);
+        if (statement != Py_None) {
+            (void)statement_reset((Statement*)statement);
+        }
     }
 }
 
@@ -178,6 +190,7 @@
     Py_XDECREF(self->row_factory);
     Py_XDECREF(self->text_factory);
     Py_XDECREF(self->collations);
+    Py_XDECREF(self->statements);
 
     self->ob_type->tp_free((PyObject*)self);
 }
@@ -391,7 +404,7 @@
     Py_ssize_t buflen;
     PyObject* stringval;
 
-    if (PyErr_Occurred()) {
+    if ((!py_val) || PyErr_Occurred()) {
         /* Errors in callbacks are ignored, and we return NULL */
         PyErr_Clear();
         sqlite3_result_null(context);
@@ -399,21 +412,23 @@
         sqlite3_result_null(context);
     } else if (PyInt_Check(py_val)) {
         longval = PyInt_AsLong(py_val);
-        /* TODO: investigate what to do with range overflows - long vs. long long */
         sqlite3_result_int64(context, (PY_LONG_LONG)longval);
     } else if (PyFloat_Check(py_val)) {
         sqlite3_result_double(context, PyFloat_AsDouble(py_val));
     } else if (PyBuffer_Check(py_val)) {
         if (PyObject_AsCharBuffer(py_val, &buffer, &buflen) != 0) {
             PyErr_SetString(PyExc_ValueError, "could not convert BLOB to buffer");
+        } else {
+            sqlite3_result_blob(context, buffer, buflen, SQLITE_TRANSIENT);
         }
-        sqlite3_result_blob(context, buffer, buflen, SQLITE_TRANSIENT);
     } else if (PyString_Check(py_val)) {
         sqlite3_result_text(context, PyString_AsString(py_val), -1, SQLITE_TRANSIENT);
     } else if (PyUnicode_Check(py_val)) {
         stringval = PyUnicode_AsUTF8String(py_val);
-        sqlite3_result_text(context, PyString_AsString(stringval), -1, SQLITE_TRANSIENT);
-        Py_DECREF(stringval);
+        if (stringval) {
+            sqlite3_result_text(context, PyString_AsString(stringval), -1, SQLITE_TRANSIENT);
+            Py_DECREF(stringval);
+        }
     } else {
         /* TODO: raise error */
     }
@@ -450,6 +465,7 @@
                 cur_py_value = PyUnicode_DecodeUTF8(val_str, strlen(val_str), NULL);
                 /* TODO: have a way to show errors here */
                 if (!cur_py_value) {
+                    PyErr_Clear();
                     Py_INCREF(Py_None);
                     cur_py_value = Py_None;
                 }
@@ -458,10 +474,12 @@
                 buflen = sqlite3_value_bytes(cur_value);
                 cur_py_value = PyBuffer_New(buflen);
                 if (!cur_py_value) {
-                    /* TODO: error */
+                    break;
                 }
                 if (PyObject_AsWriteBuffer(cur_py_value, &raw_buffer, &buflen)) {
-                    /* TODO: error */
+                    Py_DECREF(cur_py_value);
+                    cur_py_value = NULL;
+                    break;
                 }
                 memcpy(raw_buffer, sqlite3_value_blob(cur_value), buflen);
                 break;
@@ -470,6 +488,12 @@
                 Py_INCREF(Py_None);
                 cur_py_value = Py_None;
         }
+
+        if (!cur_py_value) {
+            Py_DECREF(args);
+            return NULL;
+        }
+
         PyTuple_SetItem(args, i, cur_py_value);
 
     }
@@ -481,8 +505,7 @@
 {
     PyObject* args;
     PyObject* py_func;
-    PyObject* py_retval;
-
+    PyObject* py_retval = NULL;
 
     PyGILState_STATE threadstate;
 
@@ -491,9 +514,10 @@
     py_func = (PyObject*)sqlite3_user_data(context);
 
     args = _build_py_params(context, argc, argv);
-
-    py_retval = PyObject_CallObject(py_func, args);
-    Py_DECREF(args);
+    if (args) {
+        py_retval = PyObject_CallObject(py_func, args);
+        Py_DECREF(args);
+    }
 
     _set_result(context, py_retval);
     Py_XDECREF(py_retval);
@@ -504,10 +528,10 @@
 static void _step_callback(sqlite3_context *context, int argc, sqlite3_value** params)
 {
     PyObject* args;
-    PyObject* function_result;
+    PyObject* function_result = NULL;
     PyObject* aggregate_class;
     PyObject** aggregate_instance;
-    PyObject* stepmethod;
+    PyObject* stepmethod = NULL;
 
     PyGILState_STATE threadstate;
 
@@ -520,44 +544,42 @@
     if (*aggregate_instance == 0) {
         *aggregate_instance = PyObject_CallFunction(aggregate_class, "");
 
-        if (PyErr_Occurred())
-        {
+        if (PyErr_Occurred()) {
             PyErr_Clear();
             *aggregate_instance = 0;
-            PyGILState_Release(threadstate);
-            return;
+            goto error;
         }
     }
 
     stepmethod = PyObject_GetAttrString(*aggregate_instance, "step");
-    if (!stepmethod)
-    {
-        PyGILState_Release(threadstate);
-        return;
+    if (!stepmethod) {
+        goto error;
     }
 
     args = _build_py_params(context, argc, params);
+    if (!args) {
+        goto error;
+    }
 
     function_result = PyObject_CallObject(stepmethod, args);
     Py_DECREF(args);
-    Py_DECREF(stepmethod);
 
-    if (function_result == NULL) {
+    if (!function_result) {
         PyErr_Clear();
-    } else {
-        Py_DECREF(function_result);
     }
 
+error:
+    Py_XDECREF(stepmethod);
+    Py_XDECREF(function_result);
+
     PyGILState_Release(threadstate);
 }
 
 void _final_callback(sqlite3_context* context)
 {
-    PyObject* args;
-    PyObject* function_result;
+    PyObject* function_result = NULL;
     PyObject** aggregate_instance;
     PyObject* aggregate_class;
-    PyObject* finalizemethod;
 
     PyGILState_STATE threadstate;
 
@@ -570,35 +592,56 @@
         /* this branch is executed if there was an exception in the aggregate's
          * __init__ */
 
-        PyGILState_Release(threadstate);
-        return;
+        goto error;
     }
 
-    finalizemethod = PyObject_GetAttrString(*aggregate_instance, "finalize");
-
-    if (!finalizemethod) {
-        /*
-        PyErr_SetString(ProgrammingError, "finalize method missing");
-        goto error;
-        */
+    function_result = PyObject_CallMethod(*aggregate_instance, "finalize", "");
+    if (!function_result) {
+        PyErr_Clear();
         Py_INCREF(Py_None);
         function_result = Py_None;
-    } else {
-        args = PyTuple_New(0);
-        if (!args)
-                return;
-        function_result = PyObject_CallObject(finalizemethod, args);
-        Py_DECREF(args);
-        Py_DECREF(finalizemethod);
     }
 
     _set_result(context, function_result);
+
+error:
     Py_XDECREF(*aggregate_instance);
     Py_XDECREF(function_result);
 
     PyGILState_Release(threadstate);
 }
 
+void _drop_unused_statement_references(Connection* self)
+{
+    PyObject* new_list;
+    PyObject* weakref;
+    int i;
+
+    /* we only need to do this once in a while */
+    if (self->created_statements++ < 200) {
+        return;
+    }
+
+    self->created_statements = 0;
+
+    new_list = PyList_New(0);
+    if (!new_list) {
+        return;
+    }
+
+    for (i = 0; i < PyList_Size(self->statements); i++) {
+        weakref = PyList_GetItem(self->statements, i);
+        if (weakref != Py_None) {
+            if (PyList_Append(new_list, weakref) != 0) {
+                Py_DECREF(new_list);
+                return;
+            }
+        }
+    }
+
+    Py_DECREF(self->statements);
+    self->statements = new_list;
+}
 
 PyObject* connection_create_function(Connection* self, PyObject* args, PyObject* kwargs)
 {
@@ -617,10 +660,16 @@
 
     rc = sqlite3_create_function(self->db, name, narg, SQLITE_UTF8, (void*)func, _func_callback, NULL, NULL);
 
-    PyDict_SetItem(self->function_pinboard, func, Py_None);
+    if (rc != SQLITE_OK) {
+        /* Workaround for SQLite bug: no error code or string is available here */
+        PyErr_SetString(OperationalError, "Error creating function");
+        return NULL;
+    } else {
+        PyDict_SetItem(self->function_pinboard, func, Py_None);
 
-    Py_INCREF(Py_None);
-    return Py_None;
+        Py_INCREF(Py_None);
+        return Py_None;
+    }
 }
 
 PyObject* connection_create_aggregate(Connection* self, PyObject* args, PyObject* kwargs)
@@ -639,7 +688,8 @@
 
     rc = sqlite3_create_function(self->db, name, n_arg, SQLITE_UTF8, (void*)aggregate_class, 0, &_step_callback, &_final_callback);
     if (rc != SQLITE_OK) {
-        _seterror(self->db);
+        /* Workaround for SQLite bug: no error code or string is available here */
+        PyErr_SetString(OperationalError, "Error creating aggregate");
         return NULL;
     } else {
         PyDict_SetItem(self->function_pinboard, aggregate_class, Py_None);
@@ -682,7 +732,6 @@
 
 static int connection_set_isolation_level(Connection* self, PyObject* isolation_level)
 {
-    PyObject* empty;
     PyObject* res;
     PyObject* begin_statement;
 
@@ -697,15 +746,10 @@
         Py_INCREF(Py_None);
         self->isolation_level = Py_None;
 
-        empty = PyTuple_New(0);
-        if (!empty) {
-            return -1;
-        }
-        res = connection_commit(self, empty);
+        res = connection_commit(self, NULL);
         if (!res) {
             return -1;
         }
-        Py_DECREF(empty);
         Py_DECREF(res);
 
         self->inTransaction = 0;
@@ -738,12 +782,15 @@
 {
     PyObject* sql;
     Statement* statement;
+    PyObject* weakref;
     int rc;
 
     if (!PyArg_ParseTuple(args, "O", &sql)) {
         return NULL;
     }
 
+    _drop_unused_statement_references(self);
+
     statement = PyObject_New(Statement, &StatementType);
     if (!statement) {
         return NULL;
@@ -762,8 +809,24 @@
 
         Py_DECREF(statement);
         statement = 0;
+    } else {
+        weakref = PyWeakref_NewRef((PyObject*)statement, NULL);
+        if (!weakref) {
+            Py_DECREF(statement);
+            statement = 0;
+            goto error;
+        }
+
+        if (PyList_Append(self->statements, weakref) != 0) {
+            Py_DECREF(weakref);
+            statement = 0;
+            goto error;
+        }
+
+        Py_DECREF(weakref);
     }
 
+error:
     return (PyObject*)statement;
 }
 
@@ -983,7 +1046,7 @@
 }
 
 static char connection_doc[] =
-PyDoc_STR("<missing docstring>");
+PyDoc_STR("SQLite database connection object.");
 
 static PyGetSetDef connection_getset[] = {
     {"isolation_level",  (getter)connection_get_isolation_level, (setter)connection_set_isolation_level},
@@ -1011,7 +1074,7 @@
     {"executescript", (PyCFunction)connection_executescript, METH_VARARGS,
         PyDoc_STR("Executes a multiple SQL statements at once. Non-standard.")},
     {"create_collation", (PyCFunction)connection_create_collation, METH_VARARGS,
-        PyDoc_STR("Creates a collation function.")},
+        PyDoc_STR("Creates a collation function. Non-standard.")},
     {NULL, NULL}
 };
 
diff --git a/Modules/_sqlite/connection.h b/Modules/_sqlite/connection.h
index faae6e4..8f4d36e 100644
--- a/Modules/_sqlite/connection.h
+++ b/Modules/_sqlite/connection.h
@@ -1,6 +1,6 @@
 /* connection.h - definitions for the connection type
  *
- * Copyright (C) 2004-2005 Gerhard Häring <gh@ghaering.de>
+ * Copyright (C) 2004-2006 Gerhard Häring <gh@ghaering.de>
  *
  * This file is part of pysqlite.
  *
@@ -37,7 +37,12 @@
     PyObject_HEAD
     sqlite3* db;
 
+    /* 1 if we are currently within a transaction, i. e. if a BEGIN has been
+     * issued */
     int inTransaction;
+
+    /* the type detection mode. Only 0, PARSE_DECLTYPES, PARSE_COLNAMES or a
+     * bitwise combination thereof makes sense */
     int detect_types;
 
     /* the timeout value in seconds for database locks */
@@ -54,13 +59,31 @@
      * freed in connection destructor */
     char* begin_statement;
 
+    /* 1 if a check should be performed for each API call if the connection is
+     * used from the same thread it was created in */
     int check_same_thread;
+
+    /* thread identification of the thread the connection was created in */
     long thread_ident;
 
     Cache* statement_cache;
 
+    /* A list of weak references to statements used within this connection */
+    PyObject* statements;
+
+    /* a counter for how many statements were created in the connection. May be
+     * reset to 0 at certain intervals */
+    int created_statements;
+
     PyObject* row_factory;
 
+    /* Determines how bytestrings from SQLite are converted to Python objects:
+     * - PyUnicode_Type:        Python Unicode objects are constructed from UTF-8 bytestrings
+     * - OptimizedUnicode:      Like before, but for ASCII data, only PyStrings are created.
+     * - PyString_Type:         PyStrings are created as-is.
+     * - Any custom callable:   Any object returned from the callable called with the bytestring
+     *                          as single parameter.
+     */
     PyObject* text_factory;
 
     /* remember references to functions/classes used in
diff --git a/Modules/_sqlite/converters.c b/Modules/_sqlite/converters.c
deleted file mode 100644
index 018063a..0000000
--- a/Modules/_sqlite/converters.c
+++ /dev/null
@@ -1,40 +0,0 @@
-/* converters.c - default converters
- *
- * Copyright (C) 2005 Gerhard Häring <gh@ghaering.de>
- *
- * This file is part of pysqlite.
- *
- * This software is provided 'as-is', without any express or implied
- * warranty.  In no event will the authors be held liable for any damages
- * arising from the use of this software.
- *
- * Permission is granted to anyone to use this software for any purpose,
- * including commercial applications, and to alter it and redistribute it
- * freely, subject to the following restrictions:
- *
- * 1. The origin of this software must not be misrepresented; you must not
- *    claim that you wrote the original software. If you use this software
- *    in a product, an acknowledgment in the product documentation would be
- *    appreciated but is not required.
- * 2. Altered source versions must be plainly marked as such, and must not be
- *    misrepresented as being the original software.
- * 3. This notice may not be removed or altered from any source distribution.
- */
-
-#include "util.h"
-#include "module.h"
-#include "adapters.h"
-
-/* dummy, will be implemented in a later version */
-
-PyObject* convert_date(PyObject* self, PyObject* args, PyObject* kwargs)
-{
-    Py_INCREF(Py_None);
-    return Py_None;
-}
-
-PyObject* convert_timestamp(PyObject* self, PyObject* args, PyObject* kwargs)
-{
-    Py_INCREF(Py_None);
-    return Py_None;
-}
diff --git a/Modules/_sqlite/converters.h b/Modules/_sqlite/converters.h
deleted file mode 100644
index df3768a..0000000
--- a/Modules/_sqlite/converters.h
+++ /dev/null
@@ -1,33 +0,0 @@
-/* converters.h - default converters
- *
- * Copyright (C) 2005 Gerhard Häring <gh@ghaering.de>
- *
- * This file is part of pysqlite.
- *
- * This software is provided 'as-is', without any express or implied
- * warranty.  In no event will the authors be held liable for any damages
- * arising from the use of this software.
- *
- * Permission is granted to anyone to use this software for any purpose,
- * including commercial applications, and to alter it and redistribute it
- * freely, subject to the following restrictions:
- *
- * 1. The origin of this software must not be misrepresented; you must not
- *    claim that you wrote the original software. If you use this software
- *    in a product, an acknowledgment in the product documentation would be
- *    appreciated but is not required.
- * 2. Altered source versions must be plainly marked as such, and must not be
- *    misrepresented as being the original software.
- * 3. This notice may not be removed or altered from any source distribution.
- */
-
-#ifndef PYSQLITE_CONVERTERS_H
-#define PYSQLITE_CONVERTERS_H
-#include "Python.h"
-#include "pythread.h"
-#include "sqlite3.h"
-
-PyObject* convert_date(PyObject* self, PyObject* args, PyObject* kwargs);
-PyObject* convert_timestamp(PyObject* self, PyObject* args, PyObject* kwargs);
-
-#endif
diff --git a/Modules/_sqlite/cursor.c b/Modules/_sqlite/cursor.c
index c6b8c77..6ee8bea 100644
--- a/Modules/_sqlite/cursor.c
+++ b/Modules/_sqlite/cursor.c
@@ -27,8 +27,12 @@
 #include "sqlitecompat.h"
 
 /* used to decide wether to call PyInt_FromLong or PyLong_FromLongLong */
+#ifndef INT32_MIN
 #define INT32_MIN (-2147483647 - 1)
+#endif
+#ifndef INT32_MAX
 #define INT32_MAX 2147483647
+#endif
 
 PyObject* cursor_iternext(Cursor *self);
 
@@ -157,24 +161,24 @@
 
         if (self->connection->detect_types | PARSE_COLNAMES) {
             colname = sqlite3_column_name(self->statement->st, i);
+            if (colname) {
+                for (pos = colname; *pos != 0; pos++) {
+                    if (*pos == '[') {
+                        type_start = pos + 1;
+                    } else if (*pos == ']' && type_start != (const char*)-1) {
+                        key = PyString_FromStringAndSize(type_start, pos - type_start);
+                        if (!key) {
+                            /* creating a string failed, but it is too complicated
+                             * to propagate the error here, we just assume there is
+                             * no converter and proceed */
+                            break;
+                        }
 
-            for (pos = colname; *pos != 0; pos++) {
-                if (*pos == '[') {
-                    type_start = pos + 1;
-                } else if (*pos == ']' && type_start != (const char*)-1) {
-                    key = PyString_FromStringAndSize(type_start, pos - type_start);
-                    if (!key) {
-                        /* creating a string failed, but it is too complicated
-                         * to propagate the error here, we just assume there is
-                         * no converter and proceed */
+                        converter = PyDict_GetItem(converters, key);
+                        Py_DECREF(key);
                         break;
                     }
-
-                    converter = PyDict_GetItem(converters, key);
-                    Py_DECREF(key);
-                    break;
                 }
-
             }
         }
 
@@ -276,6 +280,7 @@
     void* raw_buffer;
     const char* val_str;
     char buf[200];
+    const char* colname;
 
     Py_BEGIN_ALLOW_THREADS
     numcols = sqlite3_data_count(self->statement->st);
@@ -340,8 +345,12 @@
                         self->connection->text_factory == OptimizedUnicode ? 1 : 0);
 
                     if (!converted) {
+                        colname = sqlite3_column_name(self->statement->st, i);
+                        if (colname) {
+                            colname = "<unknown column name>";
+                        }
                         PyOS_snprintf(buf, sizeof(buf) - 1, "Could not decode to UTF-8 column %s with text %s",
-                                    sqlite3_column_name(self->statement->st, i), val_str);
+                                     colname , val_str);
                         PyErr_SetString(OperationalError, buf);
                     }
                 } else if (self->connection->text_factory == (PyObject*)&PyString_Type) {
@@ -419,8 +428,7 @@
         } else {
             /* sequence */
             parameters_iter = PyObject_GetIter(second_argument);
-            if (!parameters_iter)
-            {
+            if (!parameters_iter) {
                 return NULL;
             }
         }
@@ -506,12 +514,7 @@
                 /* it's a DDL statement or something similar
                    - we better COMMIT first so it works for all cases */
                 if (self->connection->inTransaction) {
-                    func_args = PyTuple_New(0);
-                    if (!func_args) {
-                        goto error;
-                    }
-                    result = connection_commit(self->connection, func_args);
-                    Py_DECREF(func_args);
+                    result = connection_commit(self->connection, NULL);
                     if (!result) {
                         goto error;
                     }
@@ -701,7 +704,6 @@
     const char* script_cstr;
     sqlite3_stmt* statement;
     int rc;
-    PyObject* func_args;
     PyObject* result;
     int statement_completed = 0;
 
@@ -728,12 +730,7 @@
     }
 
     /* commit first */
-    func_args = PyTuple_New(0);
-    if (!func_args) {
-        goto error;
-    }
-    result = connection_commit(self->connection, func_args);
-    Py_DECREF(func_args);
+    result = connection_commit(self->connection, NULL);
     if (!result) {
         goto error;
     }
@@ -977,6 +974,9 @@
     {NULL}
 };
 
+static char cursor_doc[] =
+PyDoc_STR("SQLite database cursor class.");
+
 PyTypeObject CursorType = {
         PyObject_HEAD_INIT(NULL)
         0,                                              /* ob_size */
@@ -999,7 +999,7 @@
         0,                                              /* tp_setattro */
         0,                                              /* tp_as_buffer */
         Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_ITER|Py_TPFLAGS_BASETYPE, /* tp_flags */
-        0,                                              /* tp_doc */
+        cursor_doc,                                     /* tp_doc */
         0,                                              /* tp_traverse */
         0,                                              /* tp_clear */
         0,                                              /* tp_richcompare */
diff --git a/Modules/_sqlite/cursor.h b/Modules/_sqlite/cursor.h
index 7f56799..831ff81 100644
--- a/Modules/_sqlite/cursor.h
+++ b/Modules/_sqlite/cursor.h
@@ -1,6 +1,6 @@
 /* cursor.h - definitions for the cursor type
  *
- * Copyright (C) 2004-2005 Gerhard Häring <gh@ghaering.de>
+ * Copyright (C) 2004-2006 Gerhard Häring <gh@ghaering.de>
  *
  * This file is part of pysqlite.
  *
diff --git a/Modules/_sqlite/microprotocols.h b/Modules/_sqlite/microprotocols.h
index d2d9b65..f601bb3 100644
--- a/Modules/_sqlite/microprotocols.h
+++ b/Modules/_sqlite/microprotocols.h
@@ -54,6 +54,6 @@
 extern PyObject *
     psyco_microprotocols_adapt(Cursor* self, PyObject *args);   
 #define psyco_microprotocols_adapt_doc \
-    "adapt(obj, protocol, alternate) -> adapt obj to given protocol"
-    
+    "adapt(obj, protocol, alternate) -> adapt obj to given protocol. Non-standard."
+
 #endif /* !defined(PSYCOPG_MICROPROTOCOLS_H) */
diff --git a/Modules/_sqlite/module.c b/Modules/_sqlite/module.c
index 1537e79..fb6eb06 100644
--- a/Modules/_sqlite/module.c
+++ b/Modules/_sqlite/module.c
@@ -167,12 +167,12 @@
 
 static PyMethodDef module_methods[] = {
     {"connect",  (PyCFunction)module_connect,  METH_VARARGS|METH_KEYWORDS, PyDoc_STR("Creates a connection.")},
-    {"complete_statement",  (PyCFunction)module_complete,  METH_VARARGS|METH_KEYWORDS, PyDoc_STR("Checks if a string contains a complete SQL statement.")},
+    {"complete_statement",  (PyCFunction)module_complete,  METH_VARARGS|METH_KEYWORDS, PyDoc_STR("Checks if a string contains a complete SQL statement. Non-standard.")},
 #ifdef HAVE_SHARED_CACHE
-    {"enable_shared_cache",  (PyCFunction)module_enable_shared_cache,  METH_VARARGS|METH_KEYWORDS, PyDoc_STR("Enable or disable shared cache mode for the calling thread.")},
+    {"enable_shared_cache",  (PyCFunction)module_enable_shared_cache,  METH_VARARGS|METH_KEYWORDS, PyDoc_STR("Enable or disable shared cache mode for the calling thread. Experimental/Non-standard.")},
 #endif
-    {"register_adapter", (PyCFunction)module_register_adapter, METH_VARARGS, PyDoc_STR("Registers an adapter with sqlite's adapter registry.")},
-    {"register_converter", (PyCFunction)module_register_converter, METH_VARARGS, PyDoc_STR("Registers a converter with sqlite.")},
+    {"register_adapter", (PyCFunction)module_register_adapter, METH_VARARGS, PyDoc_STR("Registers an adapter with pysqlite's adapter registry. Non-standard.")},
+    {"register_converter", (PyCFunction)module_register_converter, METH_VARARGS, PyDoc_STR("Registers a converter with pysqlite. Non-standard.")},
     {"adapt",  (PyCFunction)psyco_microprotocols_adapt, METH_VARARGS, psyco_microprotocols_adapt_doc},
     {NULL, NULL}
 };
diff --git a/Modules/_sqlite/module.h b/Modules/_sqlite/module.h
index 6694735..f3e2aa1 100644
--- a/Modules/_sqlite/module.h
+++ b/Modules/_sqlite/module.h
@@ -1,6 +1,6 @@
 /* module.h - definitions for the module
  *
- * Copyright (C) 2004-2005 Gerhard Häring <gh@ghaering.de>
+ * Copyright (C) 2004-2006 Gerhard Häring <gh@ghaering.de>
  *
  * This file is part of pysqlite.
  *
@@ -25,7 +25,7 @@
 #define PYSQLITE_MODULE_H
 #include "Python.h"
 
-#define PYSQLITE_VERSION "2.2.0"
+#define PYSQLITE_VERSION "2.2.2"
 
 extern PyObject* Error;
 extern PyObject* Warning;
diff --git a/Modules/_sqlite/statement.c b/Modules/_sqlite/statement.c
index 0c93651..55923e7 100644
--- a/Modules/_sqlite/statement.c
+++ b/Modules/_sqlite/statement.c
@@ -64,6 +64,7 @@
         return rc;
     }
 
+    self->in_weakreflist = NULL;
     self->sql = sql_str;
 
     sql_cstr = PyString_AsString(sql_str);
@@ -304,6 +305,10 @@
 
     Py_XDECREF(self->sql);
 
+    if (self->in_weakreflist != NULL) {
+        PyObject_ClearWeakRefs((PyObject*)self);
+    }
+
     self->ob_type->tp_free((PyObject*)self);
 }
 
@@ -398,12 +403,12 @@
         0,                                              /* tp_getattro */
         0,                                              /* tp_setattro */
         0,                                              /* tp_as_buffer */
-        Py_TPFLAGS_DEFAULT,                             /* tp_flags */
+        Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_WEAKREFS,  /* tp_flags */
         0,                                              /* tp_doc */
         0,                                              /* tp_traverse */
         0,                                              /* tp_clear */
         0,                                              /* tp_richcompare */
-        0,                                              /* tp_weaklistoffset */
+        offsetof(Statement, in_weakreflist),            /* tp_weaklistoffset */
         0,                                              /* tp_iter */
         0,                                              /* tp_iternext */
         0,                                              /* tp_methods */
diff --git a/Modules/_sqlite/statement.h b/Modules/_sqlite/statement.h
index e45a0fc..57ee36f 100644
--- a/Modules/_sqlite/statement.h
+++ b/Modules/_sqlite/statement.h
@@ -38,6 +38,7 @@
     sqlite3_stmt* st;
     PyObject* sql;
     int in_use;
+    PyObject* in_weakreflist; /* List of weak references */
 } Statement;
 
 extern PyTypeObject StatementType;
diff --git a/Modules/_sre.c b/Modules/_sre.c
index 4af08ed..06acb68 100644
--- a/Modules/_sre.c
+++ b/Modules/_sre.c
@@ -254,7 +254,7 @@
 data_stack_dealloc(SRE_STATE* state)
 {
     if (state->data_stack) {
-        free(state->data_stack);
+        PyMem_FREE(state->data_stack);
         state->data_stack = NULL;
     }
     state->data_stack_size = state->data_stack_base = 0;
@@ -270,7 +270,7 @@
         void* stack;
         cursize = minsize+minsize/4+1024;
         TRACE(("allocate/grow stack %d\n", cursize));
-        stack = realloc(state->data_stack, cursize);
+        stack = PyMem_REALLOC(state->data_stack, cursize);
         if (!stack) {
             data_stack_dealloc(state);
             return SRE_ERROR_MEMORY;
@@ -1163,7 +1163,7 @@
                    ctx->pattern[1], ctx->pattern[2]));
 
             /* install new repeat context */
-            ctx->u.rep = (SRE_REPEAT*) malloc(sizeof(*ctx->u.rep));
+            ctx->u.rep = (SRE_REPEAT*) PyObject_MALLOC(sizeof(*ctx->u.rep));
             ctx->u.rep->count = -1;
             ctx->u.rep->pattern = ctx->pattern;
             ctx->u.rep->prev = state->repeat;
@@ -1173,7 +1173,7 @@
             state->ptr = ctx->ptr;
             DO_JUMP(JUMP_REPEAT, jump_repeat, ctx->pattern+ctx->pattern[0]);
             state->repeat = ctx->u.rep->prev;
-            free(ctx->u.rep);
+            PyObject_FREE(ctx->u.rep);
 
             if (ret) {
                 RETURN_ON_ERROR(ret);
diff --git a/Modules/_ssl.c b/Modules/_ssl.c
index 4c0da6f..f49391d 100644
--- a/Modules/_ssl.c
+++ b/Modules/_ssl.c
@@ -183,10 +183,8 @@
 	int sockstate;
 
 	self = PyObject_New(PySSLObject, &PySSL_Type); /* Create new object */
-	if (self == NULL){
-		errstr = "newPySSLObject error";
-		goto fail;
-	}
+	if (self == NULL)
+		return NULL;
 	memset(self->server, '\0', sizeof(char) * X509_NAME_MAXLEN);
 	memset(self->issuer, '\0', sizeof(char) * X509_NAME_MAXLEN);
 	self->server_cert = NULL;
diff --git a/Modules/_struct.c b/Modules/_struct.c
new file mode 100644
index 0000000..cb2e538
--- /dev/null
+++ b/Modules/_struct.c
@@ -0,0 +1,1660 @@
+/* struct module -- pack values into and (out of) strings */
+
+/* New version supporting byte order, alignment and size options,
+   character strings, and unsigned numbers */
+
+#define PY_SSIZE_T_CLEAN
+
+#include "Python.h"
+#include "structseq.h"
+#include "structmember.h"
+#include <ctype.h>
+
+static PyTypeObject PyStructType;
+
+/* compatibility macros */
+#if (PY_VERSION_HEX < 0x02050000)
+typedef int Py_ssize_t;
+#endif
+
+/* The translation function for each format character is table driven */
+typedef struct _formatdef {
+	char format;
+	Py_ssize_t size;
+	Py_ssize_t alignment;
+	PyObject* (*unpack)(const char *,
+			    const struct _formatdef *);
+	int (*pack)(char *, PyObject *,
+		    const struct _formatdef *);
+} formatdef;
+
+typedef struct _formatcode {
+	const struct _formatdef *fmtdef;
+	Py_ssize_t offset;
+	Py_ssize_t size;
+} formatcode;
+
+/* Struct object interface */
+
+typedef struct {
+	PyObject_HEAD
+	Py_ssize_t s_size;
+	Py_ssize_t s_len;
+	formatcode *s_codes;
+	PyObject *s_format;
+	PyObject *weakreflist; /* List of weak references */
+} PyStructObject;
+
+
+#define PyStruct_Check(op) PyObject_TypeCheck(op, &PyStructType)
+#define PyStruct_CheckExact(op) ((op)->ob_type == &PyStructType)
+
+
+/* Exception */
+
+static PyObject *StructError;
+
+
+/* Define various structs to figure out the alignments of types */
+
+
+typedef struct { char c; short x; } st_short;
+typedef struct { char c; int x; } st_int;
+typedef struct { char c; long x; } st_long;
+typedef struct { char c; float x; } st_float;
+typedef struct { char c; double x; } st_double;
+typedef struct { char c; void *x; } st_void_p;
+
+#define SHORT_ALIGN (sizeof(st_short) - sizeof(short))
+#define INT_ALIGN (sizeof(st_int) - sizeof(int))
+#define LONG_ALIGN (sizeof(st_long) - sizeof(long))
+#define FLOAT_ALIGN (sizeof(st_float) - sizeof(float))
+#define DOUBLE_ALIGN (sizeof(st_double) - sizeof(double))
+#define VOID_P_ALIGN (sizeof(st_void_p) - sizeof(void *))
+
+/* We can't support q and Q in native mode unless the compiler does;
+   in std mode, they're 8 bytes on all platforms. */
+#ifdef HAVE_LONG_LONG
+typedef struct { char c; PY_LONG_LONG x; } s_long_long;
+#define LONG_LONG_ALIGN (sizeof(s_long_long) - sizeof(PY_LONG_LONG))
+#endif
+
+#define STRINGIFY(x)    #x
+
+#ifdef __powerc
+#pragma options align=reset
+#endif
+
+/* Helper to get a PyLongObject by hook or by crook.  Caller should decref. */
+
+static PyObject *
+get_pylong(PyObject *v)
+{
+	PyNumberMethods *m;
+
+	assert(v != NULL);
+	if (PyInt_Check(v))
+		return PyLong_FromLong(PyInt_AS_LONG(v));
+	if (PyLong_Check(v)) {
+		Py_INCREF(v);
+		return v;
+	}
+	m = v->ob_type->tp_as_number;
+	if (m != NULL && m->nb_long != NULL) {
+		v = m->nb_long(v);
+		if (v == NULL)
+			return NULL;
+		if (PyLong_Check(v))
+			return v;
+		Py_DECREF(v);
+	}
+	PyErr_SetString(StructError,
+			"cannot convert argument to long");
+	return NULL;
+}
+
+/* Helper routine to get a Python integer and raise the appropriate error
+   if it isn't one */
+
+static int
+get_long(PyObject *v, long *p)
+{
+	long x = PyInt_AsLong(v);
+	if (x == -1 && PyErr_Occurred()) {
+		if (PyErr_ExceptionMatches(PyExc_TypeError))
+			PyErr_SetString(StructError,
+					"required argument is not an integer");
+		return -1;
+	}
+	*p = x;
+	return 0;
+}
+
+
+/* Same, but handling unsigned long */
+
+static int
+get_ulong(PyObject *v, unsigned long *p)
+{
+	if (PyLong_Check(v)) {
+		unsigned long x = PyLong_AsUnsignedLong(v);
+		if (x == (unsigned long)(-1) && PyErr_Occurred())
+			return -1;
+		*p = x;
+		return 0;
+	}
+	if (get_long(v, (long *)p) < 0)
+		return -1;
+	if (((long)*p) < 0) {
+		PyErr_SetString(StructError,
+				"unsigned argument is < 0");
+		return -1;
+	}
+	return 0;
+}
+
+#ifdef HAVE_LONG_LONG
+
+/* Same, but handling native long long. */
+
+static int
+get_longlong(PyObject *v, PY_LONG_LONG *p)
+{
+	PY_LONG_LONG x;
+
+	v = get_pylong(v);
+	if (v == NULL)
+		return -1;
+	assert(PyLong_Check(v));
+	x = PyLong_AsLongLong(v);
+	Py_DECREF(v);
+	if (x == (PY_LONG_LONG)-1 && PyErr_Occurred())
+		return -1;
+	*p = x;
+	return 0;
+}
+
+/* Same, but handling native unsigned long long. */
+
+static int
+get_ulonglong(PyObject *v, unsigned PY_LONG_LONG *p)
+{
+	unsigned PY_LONG_LONG x;
+
+	v = get_pylong(v);
+	if (v == NULL)
+		return -1;
+	assert(PyLong_Check(v));
+	x = PyLong_AsUnsignedLongLong(v);
+	Py_DECREF(v);
+	if (x == (unsigned PY_LONG_LONG)-1 && PyErr_Occurred())
+		return -1;
+	*p = x;
+	return 0;
+}
+
+#endif
+
+/* Floating point helpers */
+
+static PyObject *
+unpack_float(const char *p,  /* start of 4-byte string */
+             int le)	     /* true for little-endian, false for big-endian */
+{
+	double x;
+
+	x = _PyFloat_Unpack4((unsigned char *)p, le);
+	if (x == -1.0 && PyErr_Occurred())
+		return NULL;
+	return PyFloat_FromDouble(x);
+}
+
+static PyObject *
+unpack_double(const char *p,  /* start of 8-byte string */
+              int le)         /* true for little-endian, false for big-endian */
+{
+	double x;
+
+	x = _PyFloat_Unpack8((unsigned char *)p, le);
+	if (x == -1.0 && PyErr_Occurred())
+		return NULL;
+	return PyFloat_FromDouble(x);
+}
+
+/* Helper to format the range error exceptions */
+static int
+_range_error(char format, Py_ssize_t size, int is_unsigned)
+{
+	if (is_unsigned == 0) {
+		long smallest = 0, largest = 0;
+		Py_ssize_t i = size * 8;
+		while (--i > 0) {
+			smallest = (smallest * 2) - 1;
+			largest = (largest * 2) + 1;
+		}
+		PyErr_Format(StructError,
+			"'%c' format requires %ld <= number <= %ld",
+			format,
+			smallest,
+			largest);
+	} else {
+		unsigned long largest = 0;
+		Py_ssize_t i = size * 8;
+		while (--i >= 0)
+			largest = (largest * 2) + 1;
+		PyErr_Format(StructError,
+			"'%c' format requires 0 <= number <= %lu",
+			format,
+			largest);
+	}
+	return -1;
+}
+
+
+
+/* A large number of small routines follow, with names of the form
+
+   [bln][up]_TYPE
+
+   [bln] distiguishes among big-endian, little-endian and native.
+   [pu] distiguishes between pack (to struct) and unpack (from struct).
+   TYPE is one of char, byte, ubyte, etc.
+*/
+
+/* Native mode routines. ****************************************************/
+/* NOTE:
+   In all n[up]_<type> routines handling types larger than 1 byte, there is
+   *no* guarantee that the p pointer is properly aligned for each type,
+   therefore memcpy is called.  An intermediate variable is used to
+   compensate for big-endian architectures.
+   Normally both the intermediate variable and the memcpy call will be
+   skipped by C optimisation in little-endian architectures (gcc >= 2.91
+   does this). */
+
+static PyObject *
+nu_char(const char *p, const formatdef *f)
+{
+	return PyString_FromStringAndSize(p, 1);
+}
+
+static PyObject *
+nu_byte(const char *p, const formatdef *f)
+{
+	return PyInt_FromLong((long) *(signed char *)p);
+}
+
+static PyObject *
+nu_ubyte(const char *p, const formatdef *f)
+{
+	return PyInt_FromLong((long) *(unsigned char *)p);
+}
+
+static PyObject *
+nu_short(const char *p, const formatdef *f)
+{
+	short x;
+	memcpy((char *)&x, p, sizeof x);
+	return PyInt_FromLong((long)x);
+}
+
+static PyObject *
+nu_ushort(const char *p, const formatdef *f)
+{
+	unsigned short x;
+	memcpy((char *)&x, p, sizeof x);
+	return PyInt_FromLong((long)x);
+}
+
+static PyObject *
+nu_int(const char *p, const formatdef *f)
+{
+	int x;
+	memcpy((char *)&x, p, sizeof x);
+	return PyInt_FromLong((long)x);
+}
+
+static PyObject *
+nu_uint(const char *p, const formatdef *f)
+{
+	unsigned int x;
+	memcpy((char *)&x, p, sizeof x);
+#if (SIZEOF_LONG > SIZEOF_INT)
+	return PyInt_FromLong((long)x);
+#else
+	if (x <= ((unsigned int)LONG_MAX))
+		return PyInt_FromLong((long)x);
+	return PyLong_FromUnsignedLong((unsigned long)x);
+#endif
+}
+
+static PyObject *
+nu_long(const char *p, const formatdef *f)
+{
+	long x;
+	memcpy((char *)&x, p, sizeof x);
+	return PyInt_FromLong(x);
+}
+
+static PyObject *
+nu_ulong(const char *p, const formatdef *f)
+{
+	unsigned long x;
+	memcpy((char *)&x, p, sizeof x);
+	if (x <= LONG_MAX)
+		return PyInt_FromLong((long)x);
+	return PyLong_FromUnsignedLong(x);
+}
+
+/* Native mode doesn't support q or Q unless the platform C supports
+   long long (or, on Windows, __int64). */
+
+#ifdef HAVE_LONG_LONG
+
+static PyObject *
+nu_longlong(const char *p, const formatdef *f)
+{
+	PY_LONG_LONG x;
+	memcpy((char *)&x, p, sizeof x);
+	if (x >= LONG_MIN && x <= LONG_MAX)
+		return PyInt_FromLong(Py_SAFE_DOWNCAST(x, PY_LONG_LONG, long));
+	return PyLong_FromLongLong(x);
+}
+
+static PyObject *
+nu_ulonglong(const char *p, const formatdef *f)
+{
+	unsigned PY_LONG_LONG x;
+	memcpy((char *)&x, p, sizeof x);
+	if (x <= LONG_MAX)
+		return PyInt_FromLong(Py_SAFE_DOWNCAST(x, unsigned PY_LONG_LONG, long));
+	return PyLong_FromUnsignedLongLong(x);
+}
+
+#endif
+
+static PyObject *
+nu_float(const char *p, const formatdef *f)
+{
+	float x;
+	memcpy((char *)&x, p, sizeof x);
+	return PyFloat_FromDouble((double)x);
+}
+
+static PyObject *
+nu_double(const char *p, const formatdef *f)
+{
+	double x;
+	memcpy((char *)&x, p, sizeof x);
+	return PyFloat_FromDouble(x);
+}
+
+static PyObject *
+nu_void_p(const char *p, const formatdef *f)
+{
+	void *x;
+	memcpy((char *)&x, p, sizeof x);
+	return PyLong_FromVoidPtr(x);
+}
+
+static int
+np_byte(char *p, PyObject *v, const formatdef *f)
+{
+	long x;
+	if (get_long(v, &x) < 0)
+		return -1;
+	if (x < -128 || x > 127){
+		PyErr_SetString(StructError,
+				"byte format requires -128 <= number <= 127");
+		return -1;
+	}
+	*p = (char)x;
+	return 0;
+}
+
+static int
+np_ubyte(char *p, PyObject *v, const formatdef *f)
+{
+	long x;
+	if (get_long(v, &x) < 0)
+		return -1;
+	if (x < 0 || x > 255){
+		PyErr_SetString(StructError,
+				"ubyte format requires 0 <= number <= 255");
+		return -1;
+	}
+	*p = (char)x;
+	return 0;
+}
+
+static int
+np_char(char *p, PyObject *v, const formatdef *f)
+{
+	if (!PyString_Check(v) || PyString_Size(v) != 1) {
+		PyErr_SetString(StructError,
+				"char format require string of length 1");
+		return -1;
+	}
+	*p = *PyString_AsString(v);
+	return 0;
+}
+
+static int
+np_short(char *p, PyObject *v, const formatdef *f)
+{
+	long x;
+	short y;
+	if (get_long(v, &x) < 0)
+		return -1;
+	if (x < SHRT_MIN || x > SHRT_MAX){
+		PyErr_SetString(StructError,
+				"short format requires " STRINGIFY(SHRT_MIN)
+				" <= number <= " STRINGIFY(SHRT_MAX));
+		return -1;
+	}
+	y = (short)x;
+	memcpy(p, (char *)&y, sizeof y);
+	return 0;
+}
+
+static int
+np_ushort(char *p, PyObject *v, const formatdef *f)
+{
+	long x;
+	unsigned short y;
+	if (get_long(v, &x) < 0)
+		return -1;
+	if (x < 0 || x > USHRT_MAX){
+		PyErr_SetString(StructError,
+				"short format requires 0 <= number <= " STRINGIFY(USHRT_MAX));
+		return -1;
+	}
+	y = (unsigned short)x;
+	memcpy(p, (char *)&y, sizeof y);
+	return 0;
+}
+
+static int
+np_int(char *p, PyObject *v, const formatdef *f)
+{
+	long x;
+	int y;
+	if (get_long(v, &x) < 0)
+		return -1;
+#if (SIZEOF_LONG > SIZEOF_INT)
+	if ((x < ((long)INT_MIN)) || (x > ((long)INT_MAX)))
+		return _range_error(f->format, sizeof(y), 0);
+#endif
+	y = (int)x;
+	memcpy(p, (char *)&y, sizeof y);
+	return 0;
+}
+
+static int
+np_uint(char *p, PyObject *v, const formatdef *f)
+{
+	unsigned long x;
+	unsigned int y;
+	if (get_ulong(v, &x) < 0)
+		return _range_error(f->format, sizeof(y), 1);
+	y = (unsigned int)x;
+#if (SIZEOF_LONG > SIZEOF_INT)
+	if (x > ((unsigned long)UINT_MAX))
+		return _range_error(f->format, sizeof(y), 1);
+#endif
+	memcpy(p, (char *)&y, sizeof y);
+	return 0;
+}
+
+static int
+np_long(char *p, PyObject *v, const formatdef *f)
+{
+	long x;
+	if (get_long(v, &x) < 0)
+		return -1;
+	memcpy(p, (char *)&x, sizeof x);
+	return 0;
+}
+
+static int
+np_ulong(char *p, PyObject *v, const formatdef *f)
+{
+	unsigned long x;
+	if (get_ulong(v, &x) < 0)
+		return _range_error(f->format, sizeof(x), 1);
+	memcpy(p, (char *)&x, sizeof x);
+	return 0;
+}
+
+#ifdef HAVE_LONG_LONG
+
+static int
+np_longlong(char *p, PyObject *v, const formatdef *f)
+{
+	PY_LONG_LONG x;
+	if (get_longlong(v, &x) < 0)
+		return -1;
+	memcpy(p, (char *)&x, sizeof x);
+	return 0;
+}
+
+static int
+np_ulonglong(char *p, PyObject *v, const formatdef *f)
+{
+	unsigned PY_LONG_LONG x;
+	if (get_ulonglong(v, &x) < 0)
+		return -1;
+	memcpy(p, (char *)&x, sizeof x);
+	return 0;
+}
+#endif
+
+static int
+np_float(char *p, PyObject *v, const formatdef *f)
+{
+	float x = (float)PyFloat_AsDouble(v);
+	if (x == -1 && PyErr_Occurred()) {
+		PyErr_SetString(StructError,
+				"required argument is not a float");
+		return -1;
+	}
+	memcpy(p, (char *)&x, sizeof x);
+	return 0;
+}
+
+static int
+np_double(char *p, PyObject *v, const formatdef *f)
+{
+	double x = PyFloat_AsDouble(v);
+	if (x == -1 && PyErr_Occurred()) {
+		PyErr_SetString(StructError,
+				"required argument is not a float");
+		return -1;
+	}
+	memcpy(p, (char *)&x, sizeof(double));
+	return 0;
+}
+
+static int
+np_void_p(char *p, PyObject *v, const formatdef *f)
+{
+	void *x;
+
+	v = get_pylong(v);
+	if (v == NULL)
+		return -1;
+	assert(PyLong_Check(v));
+	x = PyLong_AsVoidPtr(v);
+	Py_DECREF(v);
+	if (x == NULL && PyErr_Occurred())
+		return -1;
+	memcpy(p, (char *)&x, sizeof x);
+	return 0;
+}
+
+static formatdef native_table[] = {
+	{'x',	sizeof(char),	0,		NULL},
+	{'b',	sizeof(char),	0,		nu_byte,	np_byte},
+	{'B',	sizeof(char),	0,		nu_ubyte,	np_ubyte},
+	{'c',	sizeof(char),	0,		nu_char,	np_char},
+	{'s',	sizeof(char),	0,		NULL},
+	{'p',	sizeof(char),	0,		NULL},
+	{'h',	sizeof(short),	SHORT_ALIGN,	nu_short,	np_short},
+	{'H',	sizeof(short),	SHORT_ALIGN,	nu_ushort,	np_ushort},
+	{'i',	sizeof(int),	INT_ALIGN,	nu_int,		np_int},
+	{'I',	sizeof(int),	INT_ALIGN,	nu_uint,	np_uint},
+	{'l',	sizeof(long),	LONG_ALIGN,	nu_long,	np_long},
+	{'L',	sizeof(long),	LONG_ALIGN,	nu_ulong,	np_ulong},
+#ifdef HAVE_LONG_LONG
+	{'q',	sizeof(PY_LONG_LONG), LONG_LONG_ALIGN, nu_longlong, np_longlong},
+	{'Q',	sizeof(PY_LONG_LONG), LONG_LONG_ALIGN, nu_ulonglong,np_ulonglong},
+#endif
+	{'f',	sizeof(float),	FLOAT_ALIGN,	nu_float,	np_float},
+	{'d',	sizeof(double),	DOUBLE_ALIGN,	nu_double,	np_double},
+	{'P',	sizeof(void *),	VOID_P_ALIGN,	nu_void_p,	np_void_p},
+	{0}
+};
+
+/* Big-endian routines. *****************************************************/
+
+static PyObject *
+bu_int(const char *p, const formatdef *f)
+{
+	long x = 0;
+	Py_ssize_t i = f->size;
+	do {
+		x = (x<<8) | (*p++ & 0xFF);
+	} while (--i > 0);
+	/* Extend the sign bit. */
+	if (SIZEOF_LONG > f->size)
+		x |= -(x & (1L << ((8 * f->size) - 1)));
+	return PyInt_FromLong(x);
+}
+
+static PyObject *
+bu_uint(const char *p, const formatdef *f)
+{
+	unsigned long x = 0;
+	Py_ssize_t i = f->size;
+	do {
+		x = (x<<8) | (*p++ & 0xFF);
+	} while (--i > 0);
+	if (x <= LONG_MAX)
+		return PyInt_FromLong((long)x);
+	return PyLong_FromUnsignedLong(x);
+}
+
+static PyObject *
+bu_longlong(const char *p, const formatdef *f)
+{
+#ifdef HAVE_LONG_LONG
+	PY_LONG_LONG x = 0;
+	Py_ssize_t i = f->size;
+	do {
+		x = (x<<8) | (*p++ & 0xFF);
+	} while (--i > 0);
+	/* Extend the sign bit. */
+	if (SIZEOF_LONG_LONG > f->size)
+		x |= -(x & (1L << ((8 * f->size) - 1)));
+	if (x >= LONG_MIN && x <= LONG_MAX)
+		return PyInt_FromLong(Py_SAFE_DOWNCAST(x, PY_LONG_LONG, long));
+	return PyLong_FromLongLong(x);
+#else
+	return _PyLong_FromByteArray((const unsigned char *)p,
+				      8,
+				      0, /* little-endian */
+				      1  /* signed */);
+#endif
+}
+
+static PyObject *
+bu_ulonglong(const char *p, const formatdef *f)
+{
+#ifdef HAVE_LONG_LONG
+	unsigned PY_LONG_LONG x = 0;
+	Py_ssize_t i = f->size;
+	do {
+		x = (x<<8) | (*p++ & 0xFF);
+	} while (--i > 0);
+	if (x <= LONG_MAX)
+		return PyInt_FromLong(Py_SAFE_DOWNCAST(x, unsigned PY_LONG_LONG, long));
+	return PyLong_FromUnsignedLongLong(x);
+#else
+	return _PyLong_FromByteArray((const unsigned char *)p,
+				      8,
+				      0, /* little-endian */
+				      0  /* signed */);
+#endif
+}
+
+static PyObject *
+bu_float(const char *p, const formatdef *f)
+{
+	return unpack_float(p, 0);
+}
+
+static PyObject *
+bu_double(const char *p, const formatdef *f)
+{
+	return unpack_double(p, 0);
+}
+
+static int
+bp_int(char *p, PyObject *v, const formatdef *f)
+{
+	long x;
+	Py_ssize_t i;
+	if (get_long(v, &x) < 0)
+		return -1;
+	i = f->size;
+	if (i != SIZEOF_LONG) {
+		if ((i == 2) && (x < -32768 || x > 32767))
+			return _range_error(f->format, i, 0);
+#if (SIZEOF_LONG != 4)
+		else if ((i == 4) && (x < -2147483648L || x > 2147483647L))
+			return _range_error(f->format, i, 0);
+#endif
+	}
+	do {
+		p[--i] = (char)x;
+		x >>= 8;
+	} while (i > 0);
+	return 0;
+}
+
+static int
+bp_uint(char *p, PyObject *v, const formatdef *f)
+{
+	unsigned long x;
+	Py_ssize_t i;
+	if (get_ulong(v, &x) < 0)
+		return -1;
+	i = f->size;
+	if (i != SIZEOF_LONG) {
+		unsigned long maxint = 1;
+		maxint <<= (unsigned long)(i * 8);
+		if (x >= maxint)
+			return _range_error(f->format, f->size, 1);
+	}
+	do {
+		p[--i] = (char)x;
+		x >>= 8;
+	} while (i > 0);
+	return 0;
+}
+
+static int
+bp_longlong(char *p, PyObject *v, const formatdef *f)
+{
+	int res;
+	v = get_pylong(v);
+	if (v == NULL)
+		return -1;
+	res = _PyLong_AsByteArray((PyLongObject *)v,
+			   	  (unsigned char *)p,
+				  8,
+				  0, /* little_endian */
+				  1  /* signed */);
+	Py_DECREF(v);
+	return res;
+}
+
+static int
+bp_ulonglong(char *p, PyObject *v, const formatdef *f)
+{
+	int res;
+	v = get_pylong(v);
+	if (v == NULL)
+		return -1;
+	res = _PyLong_AsByteArray((PyLongObject *)v,
+			   	  (unsigned char *)p,
+				  8,
+				  0, /* little_endian */
+				  0  /* signed */);
+	Py_DECREF(v);
+	return res;
+}
+
+static int
+bp_float(char *p, PyObject *v, const formatdef *f)
+{
+	double x = PyFloat_AsDouble(v);
+	if (x == -1 && PyErr_Occurred()) {
+		PyErr_SetString(StructError,
+				"required argument is not a float");
+		return -1;
+	}
+	return _PyFloat_Pack4(x, (unsigned char *)p, 0);
+}
+
+static int
+bp_double(char *p, PyObject *v, const formatdef *f)
+{
+	double x = PyFloat_AsDouble(v);
+	if (x == -1 && PyErr_Occurred()) {
+		PyErr_SetString(StructError,
+				"required argument is not a float");
+		return -1;
+	}
+	return _PyFloat_Pack8(x, (unsigned char *)p, 0);
+}
+
+static formatdef bigendian_table[] = {
+	{'x',	1,		0,		NULL},
+	{'b',	1,		0,		nu_byte,	np_byte},
+	{'B',	1,		0,		nu_ubyte,	np_ubyte},
+	{'c',	1,		0,		nu_char,	np_char},
+	{'s',	1,		0,		NULL},
+	{'p',	1,		0,		NULL},
+	{'h',	2,		0,		bu_int,		bp_int},
+	{'H',	2,		0,		bu_uint,	bp_uint},
+	{'i',	4,		0,		bu_int,		bp_int},
+	{'I',	4,		0,		bu_uint,	bp_uint},
+	{'l',	4,		0,		bu_int,		bp_int},
+	{'L',	4,		0,		bu_uint,	bp_uint},
+	{'q',	8,		0,		bu_longlong,	bp_longlong},
+	{'Q',	8,		0,		bu_ulonglong,	bp_ulonglong},
+	{'f',	4,		0,		bu_float,	bp_float},
+	{'d',	8,		0,		bu_double,	bp_double},
+	{0}
+};
+
+/* Little-endian routines. *****************************************************/
+
+static PyObject *
+lu_int(const char *p, const formatdef *f)
+{
+	long x = 0;
+	Py_ssize_t i = f->size;
+	do {
+		x = (x<<8) | (p[--i] & 0xFF);
+	} while (i > 0);
+	/* Extend the sign bit. */
+	if (SIZEOF_LONG > f->size)
+		x |= -(x & (1L << ((8 * f->size) - 1)));
+	return PyInt_FromLong(x);
+}
+
+static PyObject *
+lu_uint(const char *p, const formatdef *f)
+{
+	unsigned long x = 0;
+	Py_ssize_t i = f->size;
+	do {
+		x = (x<<8) | (p[--i] & 0xFF);
+	} while (i > 0);
+	if (x <= LONG_MAX)
+		return PyInt_FromLong((long)x);
+	return PyLong_FromUnsignedLong((long)x);
+}
+
+static PyObject *
+lu_longlong(const char *p, const formatdef *f)
+{
+#ifdef HAVE_LONG_LONG
+	PY_LONG_LONG x = 0;
+	Py_ssize_t i = f->size;
+	do {
+		x = (x<<8) | (p[--i] & 0xFF);
+	} while (i > 0);
+	/* Extend the sign bit. */
+	if (SIZEOF_LONG_LONG > f->size)
+		x |= -(x & (1L << ((8 * f->size) - 1)));
+	if (x >= LONG_MIN && x <= LONG_MAX)
+		return PyInt_FromLong(Py_SAFE_DOWNCAST(x, PY_LONG_LONG, long));
+	return PyLong_FromLongLong(x);
+#else
+	return _PyLong_FromByteArray((const unsigned char *)p,
+				      8,
+				      1, /* little-endian */
+				      1  /* signed */);
+#endif
+}
+
+static PyObject *
+lu_ulonglong(const char *p, const formatdef *f)
+{
+#ifdef HAVE_LONG_LONG
+	unsigned PY_LONG_LONG x = 0;
+	Py_ssize_t i = f->size;
+	do {
+		x = (x<<8) | (p[--i] & 0xFF);
+	} while (i > 0);
+	if (x <= LONG_MAX)
+		return PyInt_FromLong(Py_SAFE_DOWNCAST(x, unsigned PY_LONG_LONG, long));
+	return PyLong_FromUnsignedLongLong(x);
+#else
+	return _PyLong_FromByteArray((const unsigned char *)p,
+				      8,
+				      1, /* little-endian */
+				      0  /* signed */);
+#endif
+}
+
+static PyObject *
+lu_float(const char *p, const formatdef *f)
+{
+	return unpack_float(p, 1);
+}
+
+static PyObject *
+lu_double(const char *p, const formatdef *f)
+{
+	return unpack_double(p, 1);
+}
+
+static int
+lp_int(char *p, PyObject *v, const formatdef *f)
+{
+	long x;
+	Py_ssize_t i;
+	if (get_long(v, &x) < 0)
+		return -1;
+	i = f->size;
+	if (i != SIZEOF_LONG) {
+		if ((i == 2) && (x < -32768 || x > 32767))
+			return _range_error(f->format, i, 0);
+#if (SIZEOF_LONG != 4)
+		else if ((i == 4) && (x < -2147483648L || x > 2147483647L))
+			return _range_error(f->format, i, 0);
+#endif
+	}
+	do {
+		*p++ = (char)x;
+		x >>= 8;
+	} while (--i > 0);
+	return 0;
+}
+
+static int
+lp_uint(char *p, PyObject *v, const formatdef *f)
+{
+	unsigned long x;
+	Py_ssize_t i;
+	if (get_ulong(v, &x) < 0)
+		return -1;
+	i = f->size;
+	if (i != SIZEOF_LONG) {
+		unsigned long maxint = 1;
+		maxint <<= (unsigned long)(i * 8);
+		if (x >= maxint)
+			return _range_error(f->format, f->size, 1);
+	}
+	do {
+		*p++ = (char)x;
+		x >>= 8;
+	} while (--i > 0);
+	return 0;
+}
+
+static int
+lp_longlong(char *p, PyObject *v, const formatdef *f)
+{
+	int res;
+	v = get_pylong(v);
+	if (v == NULL)
+		return -1;
+	res = _PyLong_AsByteArray((PyLongObject*)v,
+			   	  (unsigned char *)p,
+				  8,
+				  1, /* little_endian */
+				  1  /* signed */);
+	Py_DECREF(v);
+	return res;
+}
+
+static int
+lp_ulonglong(char *p, PyObject *v, const formatdef *f)
+{
+	int res;
+	v = get_pylong(v);
+	if (v == NULL)
+		return -1;
+	res = _PyLong_AsByteArray((PyLongObject*)v,
+			   	  (unsigned char *)p,
+				  8,
+				  1, /* little_endian */
+				  0  /* signed */);
+	Py_DECREF(v);
+	return res;
+}
+
+static int
+lp_float(char *p, PyObject *v, const formatdef *f)
+{
+	double x = PyFloat_AsDouble(v);
+	if (x == -1 && PyErr_Occurred()) {
+		PyErr_SetString(StructError,
+				"required argument is not a float");
+		return -1;
+	}
+	return _PyFloat_Pack4(x, (unsigned char *)p, 1);
+}
+
+static int
+lp_double(char *p, PyObject *v, const formatdef *f)
+{
+	double x = PyFloat_AsDouble(v);
+	if (x == -1 && PyErr_Occurred()) {
+		PyErr_SetString(StructError,
+				"required argument is not a float");
+		return -1;
+	}
+	return _PyFloat_Pack8(x, (unsigned char *)p, 1);
+}
+
+static formatdef lilendian_table[] = {
+	{'x',	1,		0,		NULL},
+	{'b',	1,		0,		nu_byte,	np_byte},
+	{'B',	1,		0,		nu_ubyte,	np_ubyte},
+	{'c',	1,		0,		nu_char,	np_char},
+	{'s',	1,		0,		NULL},
+	{'p',	1,		0,		NULL},
+	{'h',	2,		0,		lu_int,		lp_int},
+	{'H',	2,		0,		lu_uint,	lp_uint},
+	{'i',	4,		0,		lu_int,		lp_int},
+	{'I',	4,		0,		lu_uint,	lp_uint},
+	{'l',	4,		0,		lu_int,		lp_int},
+	{'L',	4,		0,		lu_uint,	lp_uint},
+	{'q',	8,		0,		lu_longlong,	lp_longlong},
+	{'Q',	8,		0,		lu_ulonglong,	lp_ulonglong},
+	{'f',	4,		0,		lu_float,	lp_float},
+	{'d',	8,		0,		lu_double,	lp_double},
+	{0}
+};
+
+
+static const formatdef *
+whichtable(char **pfmt)
+{
+	const char *fmt = (*pfmt)++; /* May be backed out of later */
+	switch (*fmt) {
+	case '<':
+		return lilendian_table;
+	case '>':
+	case '!': /* Network byte order is big-endian */
+		return bigendian_table;
+	case '=': { /* Host byte order -- different from native in aligment! */
+		int n = 1;
+		char *p = (char *) &n;
+		if (*p == 1)
+			return lilendian_table;
+		else
+			return bigendian_table;
+	}
+	default:
+		--*pfmt; /* Back out of pointer increment */
+		/* Fall through */
+	case '@':
+		return native_table;
+	}
+}
+
+
+/* Get the table entry for a format code */
+
+static const formatdef *
+getentry(int c, const formatdef *f)
+{
+	for (; f->format != '\0'; f++) {
+		if (f->format == c) {
+			return f;
+		}
+	}
+	PyErr_SetString(StructError, "bad char in struct format");
+	return NULL;
+}
+
+
+/* Align a size according to a format code */
+
+static int
+align(Py_ssize_t size, char c, const formatdef *e)
+{
+	if (e->format == c) {
+		if (e->alignment) {
+			size = ((size + e->alignment - 1)
+				/ e->alignment)
+				* e->alignment;
+		}
+	}
+	return size;
+}
+
+
+/* calculate the size of a format string */
+
+static int
+prepare_s(PyStructObject *self)
+{
+	const formatdef *f;
+	const formatdef *e;
+	formatcode *codes;
+	
+	const char *s;
+	const char *fmt;
+	char c;
+	Py_ssize_t size, len, num, itemsize, x;
+
+	fmt = PyString_AS_STRING(self->s_format);
+
+	f = whichtable((char **)&fmt);
+	
+	s = fmt;
+	size = 0;
+	len = 0;
+	while ((c = *s++) != '\0') {
+		if (isspace(Py_CHARMASK(c)))
+			continue;
+		if ('0' <= c && c <= '9') {
+			num = c - '0';
+			while ('0' <= (c = *s++) && c <= '9') {
+				x = num*10 + (c - '0');
+				if (x/10 != num) {
+					PyErr_SetString(
+						StructError,
+						"overflow in item count");
+					return -1;
+				}
+				num = x;
+			}
+			if (c == '\0')
+				break;
+		}
+		else
+			num = 1;
+
+		e = getentry(c, f);
+		if (e == NULL)
+			return -1;
+		
+		switch (c) {
+			case 's': /* fall through */
+			case 'p': len++; break;
+			case 'x': break;
+			default: len += num; break;
+		}
+
+		itemsize = e->size;
+		size = align(size, c, e);
+		x = num * itemsize;
+		size += x;
+		if (x/itemsize != num || size < 0) {
+			PyErr_SetString(StructError,
+					"total struct size too long");
+			return -1;
+		}
+	}
+
+	self->s_size = size;
+	self->s_len = len;
+	codes = PyMem_MALLOC((len + 1) * sizeof(formatcode));
+	if (codes == NULL) {
+		PyErr_NoMemory();
+		return -1;
+	}
+	self->s_codes = codes;
+	
+	s = fmt;
+	size = 0;
+	while ((c = *s++) != '\0') {
+		if (isspace(Py_CHARMASK(c)))
+			continue;
+		if ('0' <= c && c <= '9') {
+			num = c - '0';
+			while ('0' <= (c = *s++) && c <= '9')
+				num = num*10 + (c - '0');
+			if (c == '\0')
+				break;
+		}
+		else
+			num = 1;
+
+		e = getentry(c, f);
+		
+		size = align(size, c, e);
+		if (c == 's' || c == 'p') {
+			codes->offset = size;
+			codes->size = num;
+			codes->fmtdef = e;
+			codes++;
+			size += num;
+		} else if (c == 'x') {
+			size += num;
+		} else {
+			while (--num >= 0) {
+				codes->offset = size;
+				codes->size = e->size;
+				codes->fmtdef = e;
+				codes++;
+				size += e->size;
+			}
+		}
+	}
+	codes->fmtdef = NULL;
+	codes->offset = size;
+	codes->size = 0;
+	
+	return 0;
+}
+
+static PyObject *
+s_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
+{
+	PyObject *self;
+
+	assert(type != NULL && type->tp_alloc != NULL);
+
+	self = type->tp_alloc(type, 0);
+	if (self != NULL) {
+		PyStructObject *s = (PyStructObject*)self;
+		Py_INCREF(Py_None);
+		s->s_format = Py_None;
+		s->s_codes = NULL;
+		s->s_size = -1;
+		s->s_len = -1;
+	}
+	return self;
+}
+
+static int
+s_init(PyObject *self, PyObject *args, PyObject *kwds)
+{
+	PyStructObject *soself = (PyStructObject *)self;
+	PyObject *o_format = NULL;
+	int ret = 0;
+	static char *kwlist[] = {"format", 0};
+
+	assert(PyStruct_Check(self));
+
+	if (!PyArg_ParseTupleAndKeywords(args, kwds, "S:Struct", kwlist,
+					 &o_format))
+		return -1;
+
+	Py_INCREF(o_format);
+	Py_XDECREF(soself->s_format);
+	soself->s_format = o_format;
+	
+	ret = prepare_s(soself);
+	return ret;
+}
+
+static void
+s_dealloc(PyStructObject *s)
+{
+	if (s->weakreflist != NULL)
+		PyObject_ClearWeakRefs((PyObject *)s);
+	if (s->s_codes != NULL) {
+		PyMem_FREE(s->s_codes);
+	}
+	Py_XDECREF(s->s_format);
+	s->ob_type->tp_free((PyObject *)s);
+}
+
+static PyObject *
+s_unpack_internal(PyStructObject *soself, char *startfrom) {
+	formatcode *code;
+	Py_ssize_t i = 0;
+	PyObject *result = PyTuple_New(soself->s_len);
+	if (result == NULL)
+		return NULL;
+
+	for (code = soself->s_codes; code->fmtdef != NULL; code++) {
+		PyObject *v;
+		const formatdef *e = code->fmtdef;
+		const char *res = startfrom + code->offset;
+		if (e->format == 's') {
+			v = PyString_FromStringAndSize(res, code->size);
+			if (v == NULL)
+				goto fail;
+			PyTuple_SET_ITEM(result, i++, v);
+		} else if (e->format == 'p') {
+			Py_ssize_t n = *(unsigned char*)res;
+			if (n >= code->size)
+				n = code->size - 1;
+			v = PyString_FromStringAndSize(res + 1, n);
+			if (v == NULL)
+				goto fail;
+			PyTuple_SET_ITEM(result, i++, v);
+		} else {
+			v = e->unpack(res, e);
+			if (v == NULL)
+				goto fail;
+			PyTuple_SET_ITEM(result, i++, v);
+		}
+	}
+
+	return result;
+fail:
+	Py_DECREF(result);
+	return NULL;
+}
+
+
+PyDoc_STRVAR(s_unpack__doc__,
+"S.unpack(str) -> (v1, v2, ...)\n\
+\n\
+Return tuple containing values unpacked according to this Struct's format.\n\
+Requires len(str) == self.size. See struct.__doc__ for more on format\n\
+strings.");
+
+static PyObject *
+s_unpack(PyObject *self, PyObject *inputstr)
+{
+	PyStructObject *soself = (PyStructObject *)self;
+	assert(PyStruct_Check(self));
+	assert(soself->s_codes != NULL);	
+	if (inputstr == NULL || !PyString_Check(inputstr) ||
+		PyString_GET_SIZE(inputstr) != soself->s_size) {
+		PyErr_Format(StructError,
+			"unpack requires a string argument of length %zd", soself->s_size);
+		return NULL;
+	}
+	return s_unpack_internal(soself, PyString_AS_STRING(inputstr));
+}
+
+PyDoc_STRVAR(s_unpack_from__doc__,
+"S.unpack_from(buffer[, offset]) -> (v1, v2, ...)\n\
+\n\
+Return tuple containing values unpacked according to this Struct's format.\n\
+Unlike unpack, unpack_from can unpack values from any object supporting\n\
+the buffer API, not just str. Requires len(buffer[offset:]) >= self.size.\n\
+See struct.__doc__ for more on format strings.");
+
+static PyObject *
+s_unpack_from(PyObject *self, PyObject *args, PyObject *kwds)
+{
+	static char *kwlist[] = {"buffer", "offset", 0};
+#if (PY_VERSION_HEX < 0x02050000)
+	static char *fmt = "z#|i:unpack_from";
+#else
+	static char *fmt = "z#|n:unpack_from";
+#endif
+	Py_ssize_t buffer_len = 0, offset = 0;
+	char *buffer = NULL;
+	PyStructObject *soself = (PyStructObject *)self;
+	assert(PyStruct_Check(self));
+	assert(soself->s_codes != NULL);
+
+	if (!PyArg_ParseTupleAndKeywords(args, kwds, fmt, kwlist,
+					 &buffer, &buffer_len, &offset))
+		return NULL;
+
+	if (buffer == NULL) {
+		PyErr_Format(StructError,
+			"unpack_from requires a buffer argument");
+		return NULL;
+	}
+	
+	if (offset < 0)
+		offset += buffer_len;
+
+	if (offset < 0 || (buffer_len - offset) < soself->s_size) {
+		PyErr_Format(StructError,
+			"unpack_from requires a buffer of at least %zd bytes",
+			soself->s_size);
+		return NULL;
+	}
+	return s_unpack_internal(soself, buffer + offset);
+}
+
+
+/*
+ * Guts of the pack function.
+ *
+ * Takes a struct object, a tuple of arguments, and offset in that tuple of
+ * argument for where to start processing the arguments for packing, and a
+ * character buffer for writing the packed string.  The caller must insure
+ * that the buffer may contain the required length for packing the arguments.
+ * 0 is returned on success, 1 is returned if there is an error.
+ *
+ */
+static int
+s_pack_internal(PyStructObject *soself, PyObject *args, int offset, char* buf)
+{
+	formatcode *code;
+	Py_ssize_t i;
+
+	memset(buf, '\0', soself->s_size);
+	i = offset;
+	for (code = soself->s_codes; code->fmtdef != NULL; code++) {
+		Py_ssize_t n;
+		PyObject *v;
+		const formatdef *e = code->fmtdef;
+		char *res = buf + code->offset;
+		if (e->format == 's') {
+			v = PyTuple_GET_ITEM(args, i++);
+			if (!PyString_Check(v)) {
+				PyErr_SetString(StructError,
+						"argument for 's' must be a string");
+				return -1;
+			}
+			n = PyString_GET_SIZE(v);
+			if (n > code->size)
+				n = code->size;
+			if (n > 0)
+				memcpy(res, PyString_AS_STRING(v), n);
+		} else if (e->format == 'p') {
+			v = PyTuple_GET_ITEM(args, i++);
+			if (!PyString_Check(v)) {
+				PyErr_SetString(StructError,
+						"argument for 'p' must be a string");
+				return -1;
+			}
+			n = PyString_GET_SIZE(v);
+			if (n > (code->size - 1))
+				n = code->size - 1;
+			if (n > 0)
+				memcpy(res + 1, PyString_AS_STRING(v), n);
+			if (n > 255)
+				n = 255;
+			*res = Py_SAFE_DOWNCAST(n, Py_ssize_t, unsigned char);
+		} else {
+			v = PyTuple_GET_ITEM(args, i++);
+			if (e->pack(res, v, e) < 0)
+				return -1;
+		}
+	}
+	
+	/* Success */
+	return 0;
+}
+
+
+PyDoc_STRVAR(s_pack__doc__,
+"S.pack(v1, v2, ...) -> string\n\
+\n\
+Return a string containing values v1, v2, ... packed according to this\n\
+Struct's format. See struct.__doc__ for more on format strings.");
+
+static PyObject *
+s_pack(PyObject *self, PyObject *args)
+{
+	PyStructObject *soself;
+	PyObject *result;
+
+	/* Validate arguments. */
+	soself = (PyStructObject *)self;
+	assert(PyStruct_Check(self));
+	assert(soself->s_codes != NULL);
+	if (args == NULL || !PyTuple_Check(args) ||
+	    PyTuple_GET_SIZE(args) != soself->s_len)
+	{
+		PyErr_Format(StructError,
+			"pack requires exactly %zd arguments", soself->s_len);
+		return NULL;
+	}
+	
+	/* Allocate a new string */
+	result = PyString_FromStringAndSize((char *)NULL, soself->s_size);
+	if (result == NULL)
+		return NULL;
+	
+	/* Call the guts */
+	if ( s_pack_internal(soself, args, 0, PyString_AS_STRING(result)) != 0 ) {
+		Py_DECREF(result);
+		return NULL;
+	}
+
+	return result;
+}
+
+PyDoc_STRVAR(s_pack_to__doc__,
+"S.pack_to(buffer, offset, v1, v2, ...)\n\
+\n\
+Pack the values v2, v2, ... according to this Struct's format, write \n\
+the packed bytes into the writable buffer buf starting at offset.  Note\n\
+that the offset is not an optional argument.  See struct.__doc__ for \n\
+more on format strings.");
+
+static PyObject *
+s_pack_to(PyObject *self, PyObject *args)
+{
+	PyStructObject *soself;
+	char *buffer;
+	Py_ssize_t buffer_len, offset;
+
+	/* Validate arguments.  +1 is for the first arg as buffer. */
+	soself = (PyStructObject *)self;
+	assert(PyStruct_Check(self));
+	assert(soself->s_codes != NULL);
+	if (args == NULL || !PyTuple_Check(args) ||
+	    PyTuple_GET_SIZE(args) != (soself->s_len + 2))
+	{
+		PyErr_Format(StructError,
+			     "pack_to requires exactly %zd arguments", 
+			     (soself->s_len + 2));
+		return NULL;
+	}
+
+	/* Extract a writable memory buffer from the first argument */
+	if ( PyObject_AsWriteBuffer(PyTuple_GET_ITEM(args, 0), 
+								(void**)&buffer, &buffer_len) == -1 ) { 
+		return NULL;
+	}
+	assert( buffer_len >= 0 );
+
+	/* Extract the offset from the first argument */
+	offset = PyInt_AsLong(PyTuple_GET_ITEM(args, 1));
+
+	/* Support negative offsets. */
+	if (offset < 0)
+		offset += buffer_len;
+
+	/* Check boundaries */
+	if (offset < 0 || (buffer_len - offset) < soself->s_size) {
+		PyErr_Format(StructError,
+			     "pack_to requires a buffer of at least %zd bytes",
+			     soself->s_size);
+		return NULL;
+	}
+	
+	/* Call the guts */
+	if ( s_pack_internal(soself, args, 2, buffer + offset) != 0 ) {
+		return NULL;
+	}
+
+	return Py_None;
+}
+
+static PyObject *
+s_get_format(PyStructObject *self, void *unused)
+{
+	Py_INCREF(self->s_format);
+	return self->s_format;
+}
+
+static PyObject *
+s_get_size(PyStructObject *self, void *unused)
+{
+    return PyInt_FromSsize_t(self->s_size);
+}
+
+/* List of functions */
+
+static struct PyMethodDef s_methods[] = {
+	{"pack",	(PyCFunction)s_pack,		METH_VARARGS, s_pack__doc__},
+	{"pack_to",	(PyCFunction)s_pack_to,		METH_VARARGS, s_pack_to__doc__}, 
+	{"unpack",	(PyCFunction)s_unpack,		METH_O, s_unpack__doc__},
+	{"unpack_from",	(PyCFunction)s_unpack_from,	METH_KEYWORDS, s_unpack_from__doc__},
+	{NULL,	 NULL}		/* sentinel */
+};
+
+PyDoc_STRVAR(s__doc__, "Compiled struct object");
+
+#define OFF(x) offsetof(PyStructObject, x)
+
+static PyGetSetDef s_getsetlist[] = {
+	{"format", (getter)s_get_format, (setter)NULL, "struct format string", NULL},
+	{"size", (getter)s_get_size, (setter)NULL, "struct size in bytes", NULL},
+	{NULL} /* sentinel */
+};
+
+static
+PyTypeObject PyStructType = {
+	PyObject_HEAD_INIT(NULL)
+	0,
+	"Struct",
+	sizeof(PyStructObject),
+	0,
+	(destructor)s_dealloc,	/* 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 */
+	PyObject_GenericSetAttr,	/* tp_setattro */
+	0,					/* tp_as_buffer */
+	Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_WEAKREFS,/* tp_flags */
+	s__doc__,			/* tp_doc */
+	0,					/* tp_traverse */
+	0,					/* tp_clear */
+	0,					/* tp_richcompare */
+	offsetof(PyStructObject, weakreflist),	/* tp_weaklistoffset */
+	0,					/* tp_iter */
+	0,					/* tp_iternext */
+	s_methods,			/* tp_methods */
+	NULL,				/* tp_members */
+	s_getsetlist,		/* tp_getset */
+	0,					/* tp_base */
+	0,					/* tp_dict */
+	0,					/* tp_descr_get */
+	0,					/* tp_descr_set */
+	0,					/* tp_dictoffset */
+	s_init,				/* tp_init */
+	PyType_GenericAlloc,/* tp_alloc */
+	s_new,				/* tp_new */
+	PyObject_Del,		/* tp_free */
+};
+
+/* Module initialization */
+
+PyMODINIT_FUNC
+init_struct(void)
+{
+	PyObject *m = Py_InitModule("_struct", NULL);
+	if (m == NULL)
+		return;
+
+	PyStructType.ob_type = &PyType_Type;
+	if (PyType_Ready(&PyStructType) < 0)
+		return;
+
+	/* Check endian and swap in faster functions */
+	{
+		int one = 1;
+		formatdef *native = native_table;
+		formatdef *other, *ptr;
+		if ((int)*(unsigned char*)&one)
+			other = lilendian_table;
+		else
+			other = bigendian_table;
+		/* Scan through the native table, find a matching
+		   entry in the endian table and swap in the
+		   native implementations whenever possible
+		   (64-bit platforms may not have "standard" sizes) */
+		while (native->format != '\0' && other->format != '\0') {
+			ptr = other;
+			while (ptr->format != '\0') {
+				if (ptr->format == native->format) {
+					/* Match faster when formats are
+					   listed in the same order */
+					if (ptr == other)
+						other++;
+					/* Only use the trick if the 
+					   size matches */
+					if (ptr->size != native->size)
+						break;
+					/* Skip float and double, could be
+					   "unknown" float format */
+					if (ptr->format == 'd' || ptr->format == 'f')
+						break;
+					ptr->pack = native->pack;
+					ptr->unpack = native->unpack;
+					break;
+				}
+				ptr++;
+			}
+			native++;
+		}
+	}
+	
+	/* Add some symbolic constants to the module */
+	if (StructError == NULL) {
+		StructError = PyErr_NewException("struct.error", NULL, NULL);
+		if (StructError == NULL)
+			return;
+	}
+
+	Py_INCREF(StructError);
+	PyModule_AddObject(m, "error", StructError);
+
+	Py_INCREF((PyObject*)&PyStructType);
+	PyModule_AddObject(m, "Struct", (PyObject*)&PyStructType);
+}
diff --git a/Modules/_testcapimodule.c b/Modules/_testcapimodule.c
index e8881dc..6b9dffd 100644
--- a/Modules/_testcapimodule.c
+++ b/Modules/_testcapimodule.c
@@ -486,8 +486,8 @@
 	return Py_None;
 }
 
-static
-PyObject *codec_incrementalencoder(PyObject *self, PyObject *args)
+static PyObject *
+codec_incrementalencoder(PyObject *self, PyObject *args)
 {
 	const char *encoding, *errors = NULL;
 	if (!PyArg_ParseTuple(args, "s|s:test_incrementalencoder",
@@ -496,8 +496,8 @@
 	return PyCodec_IncrementalEncoder(encoding, errors);
 }
 
-static
-PyObject *codec_incrementaldecoder(PyObject *self, PyObject *args)
+static PyObject *
+codec_incrementaldecoder(PyObject *self, PyObject *args)
 {
 	const char *encoding, *errors = NULL;
 	if (!PyArg_ParseTuple(args, "s|s:test_incrementaldecoder",
@@ -660,6 +660,42 @@
 }
 #endif
 
+/* Some tests of PyString_FromFormat().  This needs more tests. */
+static PyObject *
+test_string_from_format(PyObject *self, PyObject *args)
+{
+	PyObject *result;
+	char *msg;
+
+#define CHECK_1_FORMAT(FORMAT, TYPE) 			\
+	result = PyString_FromFormat(FORMAT, (TYPE)1);	\
+	if (result == NULL)				\
+		return NULL;				\
+	if (strcmp(PyString_AsString(result), "1")) {	\
+		msg = FORMAT " failed at 1";		\
+		goto Fail;				\
+	}						\
+	Py_DECREF(result)
+
+	CHECK_1_FORMAT("%d", int);
+	CHECK_1_FORMAT("%ld", long);
+	/* The z width modifier was added in Python 2.5. */
+	CHECK_1_FORMAT("%zd", Py_ssize_t);
+
+	/* The u type code was added in Python 2.5. */
+	CHECK_1_FORMAT("%u", unsigned int);
+	CHECK_1_FORMAT("%lu", unsigned long);
+	CHECK_1_FORMAT("%zu", size_t);
+
+	Py_RETURN_NONE;
+
+ Fail:
+ 	Py_XDECREF(result);
+	return raiseTestError("test_string_from_format", msg);
+
+#undef CHECK_1_FORMAT
+}
+
 static PyMethodDef TestMethods[] = {
 	{"raise_exception",	raise_exception,		 METH_VARARGS},
 	{"test_config",		(PyCFunction)test_config,	 METH_NOARGS},
@@ -669,6 +705,7 @@
 	{"test_long_numbits",	(PyCFunction)test_long_numbits,	 METH_NOARGS},
 	{"test_k_code",		(PyCFunction)test_k_code,	 METH_NOARGS},
 	{"test_null_strings",	(PyCFunction)test_null_strings,	 METH_NOARGS},
+	{"test_string_from_format", (PyCFunction)test_string_from_format, METH_NOARGS},
 
 	{"getargs_b",		getargs_b,			 METH_VARARGS},
 	{"getargs_B",		getargs_B,			 METH_VARARGS},
diff --git a/Modules/_tkinter.c b/Modules/_tkinter.c
index c17b6c6..77583b7 100644
--- a/Modules/_tkinter.c
+++ b/Modules/_tkinter.c
@@ -2619,21 +2619,32 @@
 static PyObject	*
 Tkapp_TkInit(PyObject *self, PyObject *args)
 {
+	static int has_failed;
 	Tcl_Interp *interp = Tkapp_Interp(self);
 	Tk_Window main_window;
 	const char * _tk_exists = NULL;
-	PyObject *res =	NULL;
 	int err;
 	main_window = Tk_MainWindow(interp);
 
+	/* In all current versions of Tk (including 8.4.13), Tk_Init
+	   deadlocks on the second call when the first call failed.
+	   To avoid the deadlock, we just refuse the second call through
+	   a static variable. */
+	if (has_failed) {
+		PyErr_SetString(Tkinter_TclError, 
+				"Calling Tk_Init again after a previous call failed might deadlock");
+		return NULL;
+	}
+	   
 	/* We want to guard against calling Tk_Init() multiple times */
 	CHECK_TCL_APPARTMENT;
 	ENTER_TCL
 	err = Tcl_Eval(Tkapp_Interp(self), "info exists	tk_version");
 	ENTER_OVERLAP
 	if (err == TCL_ERROR) {
-		/* XXX: shouldn't we do something with res? */
-		res = Tkinter_Error(self);
+		/* This sets an exception, but we cannot return right
+		   away because we need to exit the overlap first. */
+		Tkinter_Error(self);
 	} else {
 		_tk_exists = Tkapp_Result(self);
 	}
@@ -2644,6 +2655,7 @@
 	if (_tk_exists == NULL || strcmp(_tk_exists, "1") != 0)	{
 		if (Tk_Init(interp)	== TCL_ERROR) {
 		        PyErr_SetString(Tkinter_TclError, Tcl_GetStringResult(Tkapp_Interp(self)));
+			has_failed = 1;
 			return NULL;
 		}
 	}
diff --git a/Modules/arraymodule.c b/Modules/arraymodule.c
index 52a7f5e..af12769 100644
--- a/Modules/arraymodule.c
+++ b/Modules/arraymodule.c
@@ -1975,9 +1975,9 @@
 	0,					/* tp_setattr */
 	0,					/* tp_compare */
 	(reprfunc)array_repr,			/* tp_repr */
-	0,					/* tp_as _number*/
-	&array_as_sequence,			/* tp_as _sequence*/
-	&array_as_mapping,			/* tp_as _mapping*/
+	0,					/* tp_as_number*/
+	&array_as_sequence,			/* tp_as_sequence*/
+	&array_as_mapping,			/* tp_as_mapping*/
 	0, 					/* tp_hash */
 	0,					/* tp_call */
 	0,					/* tp_str */
diff --git a/Modules/cPickle.c b/Modules/cPickle.c
index 18df599..85fd459 100644
--- a/Modules/cPickle.c
+++ b/Modules/cPickle.c
@@ -1151,7 +1151,9 @@
 	else {
 		char c_str[250];
 		c_str[0] = FLOAT;
-		PyOS_snprintf(c_str + 1, sizeof(c_str) - 1, "%.17g\n", x);
+		PyOS_ascii_formatd(c_str + 1, sizeof(c_str) - 2, "%.17g", x);
+		/* Extend the formatted string with a newline character */
+		strcat(c_str, "\n");
 
 		if (self->write_func(self, c_str, strlen(c_str)) < 0)
 			return -1;
@@ -3071,8 +3073,8 @@
 					"pickles are not supported.");
 			return NULL;
 		}
-		return PyObject_CallFunction(fc, "OO", py_module_name,
-					     py_global_name);
+		return PyObject_CallFunctionObjArgs(fc, py_module_name,
+					            py_global_name, NULL);
 	}
 
 	module = PySys_GetObject("modules");
@@ -5623,7 +5625,6 @@
 
 	if (!( t=PyDict_New()))  return -1;
 	if (!( r=PyRun_String(
-		       "def __init__(self, *args): self.args=args\n\n"
 		       "def __str__(self):\n"
 		       "  return self.args and ('%s' % self.args[0]) or '(what)'\n",
 		       Py_file_input,
@@ -5643,7 +5644,6 @@
 
 	if (!( t=PyDict_New()))  return -1;
 	if (!( r=PyRun_String(
-		       "def __init__(self, *args): self.args=args\n\n"
 		       "def __str__(self):\n"
 		       "  a=self.args\n"
 		       "  a=a and type(a[0]) or '(what)'\n"
diff --git a/Modules/cjkcodecs/multibytecodec.c b/Modules/cjkcodecs/multibytecodec.c
index aa0096a..7e6aedc 100644
--- a/Modules/cjkcodecs/multibytecodec.c
+++ b/Modules/cjkcodecs/multibytecodec.c
@@ -831,7 +831,7 @@
 {
 	while (buf->inbuf < buf->inbuf_end) {
 		Py_ssize_t inleft, outleft;
-		int r;
+		Py_ssize_t r;
 
 		inleft = (Py_ssize_t)(buf->inbuf_end - buf->inbuf);
 		outleft = (Py_ssize_t)(buf->outbuf_end - buf->outbuf);
diff --git a/Modules/datetimemodule.c b/Modules/datetimemodule.c
index 9ae235a..760ab31 100644
--- a/Modules/datetimemodule.c
+++ b/Modules/datetimemodule.c
@@ -3679,6 +3679,13 @@
 		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);
 }
 
diff --git a/Modules/expat/expat_config.h b/Modules/expat/expat_config.h
new file mode 100644
index 0000000..b8c1639
--- /dev/null
+++ b/Modules/expat/expat_config.h
@@ -0,0 +1,19 @@
+/*
+ * Expat configuration for python. This file is not part of the expat
+ * distribution.
+ */
+#ifndef EXPAT_CONFIG_H
+#define EXPAT_CONFIG_H
+
+#include <pyconfig.h>
+#ifdef WORDS_BIGENDIAN
+#define BYTEORDER 4321
+#else
+#define BYTEORDER 1234
+#endif
+
+#define XML_NS 1
+#define XML_DTD 1
+#define XML_CONTEXT_BYTES 1024
+
+#endif /* EXPAT_CONFIG_H */
diff --git a/Modules/expat/xmlparse.c b/Modules/expat/xmlparse.c
index 3372bc9..42d95b7 100644
--- a/Modules/expat/xmlparse.c
+++ b/Modules/expat/xmlparse.c
@@ -2,10 +2,6 @@
    See the file COPYING for copying permission.
 */
 
-#include <stddef.h>
-#include <string.h>                     /* memset(), memcpy() */
-#include <assert.h>
-
 #define XML_BUILDING_EXPAT 1
 
 #ifdef COMPILED_FROM_DSP
@@ -16,6 +12,10 @@
 #include <expat_config.h>
 #endif /* ndef COMPILED_FROM_DSP */
 
+#include <stddef.h>
+#include <string.h>                     /* memset(), memcpy() */
+#include <assert.h>
+
 #include "expat.h"
 
 #ifdef XML_UNICODE
diff --git a/Modules/expat/xmlrole.c b/Modules/expat/xmlrole.c
index 1924fcb..2587fdf 100644
--- a/Modules/expat/xmlrole.c
+++ b/Modules/expat/xmlrole.c
@@ -2,8 +2,6 @@
    See the file COPYING for copying permission.
 */
 
-#include <stddef.h>
-
 #ifdef COMPILED_FROM_DSP
 #include "winconfig.h"
 #elif defined(MACOS_CLASSIC)
@@ -14,6 +12,8 @@
 #endif
 #endif /* ndef COMPILED_FROM_DSP */
 
+#include <stddef.h>
+
 #include "expat_external.h"
 #include "internal.h"
 #include "xmlrole.h"
diff --git a/Modules/expat/xmltok.c b/Modules/expat/xmltok.c
index 160fa40..8b9d997 100644
--- a/Modules/expat/xmltok.c
+++ b/Modules/expat/xmltok.c
@@ -2,8 +2,6 @@
    See the file COPYING for copying permission.
 */
 
-#include <stddef.h>
-
 #ifdef COMPILED_FROM_DSP
 #include "winconfig.h"
 #elif defined(MACOS_CLASSIC)
@@ -14,6 +12,8 @@
 #endif
 #endif /* ndef COMPILED_FROM_DSP */
 
+#include <stddef.h>
+
 #include "expat_external.h"
 #include "internal.h"
 #include "xmltok.h"
diff --git a/Modules/fcntlmodule.c b/Modules/fcntlmodule.c
index a368494..477af06 100644
--- a/Modules/fcntlmodule.c
+++ b/Modules/fcntlmodule.c
@@ -95,6 +95,7 @@
 static PyObject *
 fcntl_ioctl(PyObject *self, PyObject *args)
 {
+#define IOCTL_BUFSZ 1024
 	int fd;
 	/* In PyArg_ParseTuple below, use the unsigned int 'I' format for
 	   the signed int 'code' variable, because Python turns 0x8000000
@@ -106,7 +107,7 @@
 	char *str;
 	Py_ssize_t len;
 	int mutate_arg = 1;
-	char buf[1024];
+ 	char buf[IOCTL_BUFSZ+1];  /* argument plus NUL byte */
 
 	if (PyArg_ParseTuple(args, "O&Iw#|i:ioctl",
                              conv_descriptor, &fd, &code, 
@@ -114,8 +115,9 @@
 		char *arg;
 
 	       	if (mutate_arg) {
-			if (len <= sizeof buf) {
+			if (len <= IOCTL_BUFSZ) {
 				memcpy(buf, str, len);
+				buf[len] = '\0';
 				arg = buf;
 			} 
 			else {
@@ -123,13 +125,14 @@
 			}
 		}
 		else {
-			if (len > sizeof buf) {
+			if (len > IOCTL_BUFSZ) {
 				PyErr_SetString(PyExc_ValueError,
 					"ioctl string arg too long");
 				return NULL;
 			}
 			else {
 				memcpy(buf, str, len);
+				buf[len] = '\0';
 				arg = buf;
 			}
 		}
@@ -141,7 +144,7 @@
 		else {
 			ret = ioctl(fd, code, arg);
 		}
-		if (mutate_arg && (len < sizeof buf)) {
+		if (mutate_arg && (len < IOCTL_BUFSZ)) {
 			memcpy(str, buf, len);
 		}
 		if (ret < 0) {
@@ -159,12 +162,13 @@
 	PyErr_Clear();
 	if (PyArg_ParseTuple(args, "O&Is#:ioctl",
                              conv_descriptor, &fd, &code, &str, &len)) {
-		if (len > sizeof buf) {
+		if (len > IOCTL_BUFSZ) {
 			PyErr_SetString(PyExc_ValueError,
 					"ioctl string arg too long");
 			return NULL;
 		}
 		memcpy(buf, str, len);
+		buf[len] = '\0';
 		Py_BEGIN_ALLOW_THREADS
 		ret = ioctl(fd, code, buf);
 		Py_END_ALLOW_THREADS
@@ -195,6 +199,7 @@
 		return NULL;
 	}
 	return PyInt_FromLong((long)ret);
+#undef IOCTL_BUFSZ
 }
 
 PyDoc_STRVAR(ioctl_doc,
diff --git a/Modules/gcmodule.c b/Modules/gcmodule.c
index 0176d6f..872727d 100644
--- a/Modules/gcmodule.c
+++ b/Modules/gcmodule.c
@@ -75,6 +75,7 @@
 				DEBUG_OBJECTS | \
 				DEBUG_SAVEALL
 static int debug;
+static PyObject *tmod = NULL;
 
 /*--------------------------------------------------------------------------
 gc_refs values.
@@ -602,7 +603,7 @@
 		assert(callback != NULL);
 
 		/* copy-paste of weakrefobject.c's handle_callback() */
-		temp = PyObject_CallFunction(callback, "O", wr);
+		temp = PyObject_CallFunctionObjArgs(callback, wr, NULL);
 		if (temp == NULL)
 			PyErr_WriteUnraisable(callback);
 		else
@@ -734,7 +735,6 @@
 	PyGC_Head unreachable; /* non-problematic unreachable trash */
 	PyGC_Head finalizers;  /* objects with, & reachable from, __del__ */
 	PyGC_Head *gc;
-	static PyObject *tmod = NULL;
 	double t1 = 0.0;
 
 	if (delstr == NULL) {
@@ -743,12 +743,6 @@
 			Py_FatalError("gc couldn't allocate \"__del__\"");
 	}
 
-	if (tmod == NULL) {
-		tmod = PyImport_ImportModule("time");
-		if (tmod == NULL)
-			PyErr_Clear();
-	}
-
 	if (debug & DEBUG_STATS) {
 		if (tmod != NULL) {
 			PyObject *f = PyObject_CallMethod(tmod, "time", NULL);
@@ -1233,6 +1227,19 @@
 	Py_INCREF(garbage);
 	if (PyModule_AddObject(m, "garbage", garbage) < 0)
 		return;
+
+	/* Importing can't be done in collect() because collect()
+	 * can be called via PyGC_Collect() in Py_Finalize().
+	 * This wouldn't be a problem, except that <initialized> is
+	 * reset to 0 before calling collect which trips up
+	 * the import and triggers an assertion.
+	 */
+	if (tmod == NULL) {
+		tmod = PyImport_ImportModule("time");
+		if (tmod == NULL)
+			PyErr_Clear();
+	}
+
 #define ADD_INT(NAME) if (PyModule_AddIntConstant(m, #NAME, NAME) < 0) return
 	ADD_INT(DEBUG_STATS);
 	ADD_INT(DEBUG_COLLECTABLE);
diff --git a/Modules/grpmodule.c b/Modules/grpmodule.c
index 12d33dd..e5b9f47 100644
--- a/Modules/grpmodule.c
+++ b/Modules/grpmodule.c
@@ -84,12 +84,18 @@
 }
 
 static PyObject *
-grp_getgrgid(PyObject *self, PyObject *args)
+grp_getgrgid(PyObject *self, PyObject *pyo_id)
 {
+    PyObject *py_int_id;
     unsigned int gid;
     struct group *p;
-    if (!PyArg_ParseTuple(args, "I:getgrgid", &gid))
-        return NULL;
+
+    py_int_id = PyNumber_Int(pyo_id);
+    if (!py_int_id)
+	    return NULL;
+    gid = PyInt_AS_LONG(py_int_id);
+    Py_DECREF(py_int_id);
+
     if ((p = getgrgid(gid)) == NULL) {
 	PyErr_Format(PyExc_KeyError, "getgrgid(): gid not found: %d", gid);
         return NULL;
@@ -98,27 +104,33 @@
 }
 
 static PyObject *
-grp_getgrnam(PyObject *self, PyObject *args)
+grp_getgrnam(PyObject *self, PyObject *pyo_name)
 {
+    PyObject *py_str_name;
     char *name;
     struct group *p;
-    if (!PyArg_ParseTuple(args, "s:getgrnam", &name))
-        return NULL;
+
+    py_str_name = PyObject_Str(pyo_name);
+    if (!py_str_name)
+	    return NULL;
+    name = PyString_AS_STRING(py_str_name);
+    
     if ((p = getgrnam(name)) == NULL) {
 	PyErr_Format(PyExc_KeyError, "getgrnam(): name not found: %s", name);
+	Py_DECREF(py_str_name);
         return NULL;
     }
+
+    Py_DECREF(py_str_name);
     return mkgrent(p);
 }
 
 static PyObject *
-grp_getgrall(PyObject *self, PyObject *args)
+grp_getgrall(PyObject *self, PyObject *ignore)
 {
     PyObject *d;
     struct group *p;
 
-    if (!PyArg_ParseTuple(args, ":getgrall"))
-        return NULL;
     if ((d = PyList_New(0)) == NULL)
         return NULL;
     setgrent();
@@ -136,15 +148,15 @@
 }
 
 static PyMethodDef grp_methods[] = {
-    {"getgrgid",	grp_getgrgid,	METH_VARARGS,
+    {"getgrgid",	grp_getgrgid,	METH_O,
      "getgrgid(id) -> tuple\n\
 Return the group database entry for the given numeric group ID.  If\n\
 id is not valid, raise KeyError."},
-    {"getgrnam",	grp_getgrnam,	METH_VARARGS,
+    {"getgrnam",	grp_getgrnam,	METH_O,
      "getgrnam(name) -> tuple\n\
 Return the group database entry for the given group name.  If\n\
 name is not valid, raise KeyError."},
-    {"getgrall",	grp_getgrall,	METH_VARARGS,
+    {"getgrall",	grp_getgrall,	METH_NOARGS,
      "getgrall() -> list of tuples\n\
 Return a list of all available group entries, in arbitrary order."},
     {NULL,		NULL}		/* sentinel */
diff --git a/Modules/itertoolsmodule.c b/Modules/itertoolsmodule.c
index 94617a9..86b1bbf 100644
--- a/Modules/itertoolsmodule.c
+++ b/Modules/itertoolsmodule.c
@@ -1093,10 +1093,10 @@
 typedef struct {
 	PyObject_HEAD
 	PyObject *it;
-	long	next;
-	long	stop;
-	long	step;
-	long	cnt;
+	Py_ssize_t next;
+	Py_ssize_t stop;
+	Py_ssize_t step;
+	Py_ssize_t cnt;
 } isliceobject;
 
 static PyTypeObject islice_type;
@@ -1105,7 +1105,7 @@
 islice_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
 {
 	PyObject *seq;
-	long start=0, stop=-1, step=1;
+	Py_ssize_t start=0, stop=-1, step=1;
 	PyObject *it, *a1=NULL, *a2=NULL, *a3=NULL;
 	Py_ssize_t numargs;
 	isliceobject *lz;
@@ -1119,7 +1119,7 @@
 	numargs = PyTuple_Size(args);
 	if (numargs == 2) {
 		if (a1 != Py_None) {
-			stop = PyInt_AsLong(a1);
+			stop = PyInt_AsSsize_t(a1);
 			if (stop == -1) {
 				if (PyErr_Occurred())
 					PyErr_Clear();
@@ -1130,11 +1130,11 @@
 		}
 	} else {
 		if (a1 != Py_None)
-			start = PyInt_AsLong(a1);
+			start = PyInt_AsSsize_t(a1);
 		if (start == -1 && PyErr_Occurred())
 			PyErr_Clear();
 		if (a2 != Py_None) {
-			stop = PyInt_AsLong(a2);
+			stop = PyInt_AsSsize_t(a2);
 			if (stop == -1) {
 				if (PyErr_Occurred())
 					PyErr_Clear();
@@ -1152,7 +1152,7 @@
 
 	if (a3 != NULL) {
 		if (a3 != Py_None)
-			step = PyInt_AsLong(a3);
+			step = PyInt_AsSsize_t(a3);
 		if (step == -1 && PyErr_Occurred())
 			PyErr_Clear();
 	}
@@ -1202,7 +1202,7 @@
 {
 	PyObject *item;
 	PyObject *it = lz->it;
-	long oldnext;
+	Py_ssize_t oldnext;
 	PyObject *(*iternext)(PyObject *);
 
 	assert(PyIter_Check(it));
@@ -1600,8 +1600,8 @@
 
 typedef struct {
 	PyObject_HEAD
-	Py_ssize_t	tuplesize;
-	long	iternum;		/* which iterator is active */
+	Py_ssize_t tuplesize;
+	Py_ssize_t iternum;		/* which iterator is active */
 	PyObject *ittuple;		/* tuple of iterators */
 } chainobject;
 
@@ -1612,7 +1612,7 @@
 {
 	chainobject *lz;
 	Py_ssize_t tuplesize = PySequence_Length(args);
-	int i;
+	Py_ssize_t i;
 	PyObject *ittuple;
 
 	if (!_PyArg_NoKeywords("chain()", kwds))
@@ -1621,7 +1621,7 @@
 	/* obtain iterators */
 	assert(PyTuple_Check(args));
 	ittuple = PyTuple_New(tuplesize);
-	if(ittuple == NULL)
+	if (ittuple == NULL)
 		return NULL;
 	for (i=0; i < tuplesize; ++i) {
 		PyObject *item = PyTuple_GET_ITEM(args, i);
@@ -1629,7 +1629,7 @@
 		if (it == NULL) {
 			if (PyErr_ExceptionMatches(PyExc_TypeError))
 				PyErr_Format(PyExc_TypeError,
-				    "chain argument #%d must support iteration",
+				    "chain argument #%zd must support iteration",
 				    i+1);
 			Py_DECREF(ittuple);
 			return NULL;
@@ -2033,7 +2033,7 @@
 
 typedef struct {
 	PyObject_HEAD
-	long	cnt;
+	Py_ssize_t cnt;
 } countobject;
 
 static PyTypeObject count_type;
@@ -2042,12 +2042,12 @@
 count_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
 {
 	countobject *lz;
-	long cnt = 0;
+	Py_ssize_t cnt = 0;
 
 	if (!_PyArg_NoKeywords("count()", kwds))
 		return NULL;
 
-	if (!PyArg_ParseTuple(args, "|l:count", &cnt))
+	if (!PyArg_ParseTuple(args, "|n:count", &cnt))
 		return NULL;
 
 	/* create countobject structure */
@@ -2062,13 +2062,13 @@
 static PyObject *
 count_next(countobject *lz)
 {
-	return PyInt_FromLong(lz->cnt++);
+	return PyInt_FromSize_t(lz->cnt++);
 }
 
 static PyObject *
 count_repr(countobject *lz)
 {
-	return PyString_FromFormat("count(%ld)", lz->cnt);
+	return PyString_FromFormat("count(%zd)", lz->cnt);
 }
 
 PyDoc_STRVAR(count_doc,
@@ -2138,7 +2138,7 @@
 izip_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
 {
 	izipobject *lz;
-	int i;
+	Py_ssize_t i;
 	PyObject *ittuple;  /* tuple of iterators */
 	PyObject *result;
 	Py_ssize_t tuplesize = PySequence_Length(args);
@@ -2151,7 +2151,7 @@
 
 	/* obtain iterators */
 	ittuple = PyTuple_New(tuplesize);
-	if(ittuple == NULL)
+	if (ittuple == NULL)
 		return NULL;
 	for (i=0; i < tuplesize; ++i) {
 		PyObject *item = PyTuple_GET_ITEM(args, i);
@@ -2159,7 +2159,7 @@
 		if (it == NULL) {
 			if (PyErr_ExceptionMatches(PyExc_TypeError))
 				PyErr_Format(PyExc_TypeError,
-				    "izip argument #%d must support iteration",
+				    "izip argument #%zd must support iteration",
 				    i+1);
 			Py_DECREF(ittuple);
 			return NULL;
@@ -2212,7 +2212,7 @@
 static PyObject *
 izip_next(izipobject *lz)
 {
-	int i;
+	Py_ssize_t i;
 	Py_ssize_t tuplesize = lz->tuplesize;
 	PyObject *result = lz->result;
 	PyObject *it;
@@ -2314,7 +2314,7 @@
 typedef struct {
 	PyObject_HEAD
 	PyObject *element;
-	long cnt;
+	Py_ssize_t cnt;
 } repeatobject;
 
 static PyTypeObject repeat_type;
@@ -2324,12 +2324,12 @@
 {
 	repeatobject *ro;
 	PyObject *element;
-	long cnt = -1;
+	Py_ssize_t cnt = -1;
 
 	if (!_PyArg_NoKeywords("repeat()", kwds))
 		return NULL;
 
-	if (!PyArg_ParseTuple(args, "O|l:repeat", &element, &cnt))
+	if (!PyArg_ParseTuple(args, "O|n:repeat", &element, &cnt))
 		return NULL;
 
 	if (PyTuple_Size(args) == 2 && cnt < 0)
@@ -2383,7 +2383,7 @@
 		result = PyString_FromFormat("repeat(%s)",
 			PyString_AS_STRING(objrepr));
 	else
-		result = PyString_FromFormat("repeat(%s, %ld)",
+		result = PyString_FromFormat("repeat(%s, %zd)",
 			PyString_AS_STRING(objrepr), ro->cnt);
 	Py_DECREF(objrepr);
 	return result;
@@ -2396,7 +2396,7 @@
                 PyErr_SetString(PyExc_TypeError, "len() of unsized object");
 		return NULL;
 	}
-        return PyInt_FromLong(ro->cnt);
+        return PyInt_FromSize_t(ro->cnt);
 }
 
 PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
diff --git a/Modules/main.c b/Modules/main.c
index 7326a27..23a760f 100644
--- a/Modules/main.c
+++ b/Modules/main.c
@@ -9,8 +9,10 @@
 #endif
 
 #if defined(MS_WINDOWS) || defined(__CYGWIN__)
+#ifdef HAVE_FCNTL_H
 #include <fcntl.h>
 #endif
+#endif
 
 #if (defined(PYOS_OS2) && !defined(PYCC_GCC)) || defined(MS_WINDOWS)
 #define PYTHONHOMEHELP "<prefix>\\lib"
diff --git a/Modules/operator.c b/Modules/operator.c
index cbce16e..bf3def4 100644
--- a/Modules/operator.c
+++ b/Modules/operator.c
@@ -48,6 +48,12 @@
   if(-1 == (r=AOP(a1,a2))) return NULL; \
   return PyInt_FromLong(r); }
 
+#define spamn2(OP,AOP) static PyObject *OP(PyObject *s, PyObject *a) { \
+  PyObject *a1, *a2; Py_ssize_t r; \
+  if(! PyArg_UnpackTuple(a,#OP,2,2,&a1,&a2)) return NULL; \
+  if(-1 == (r=AOP(a1,a2))) return NULL; \
+  return PyInt_FromSsize_t(r); }
+
 #define spami2b(OP,AOP) static PyObject *OP(PyObject *s, PyObject *a) { \
   PyObject *a1, *a2; long r; \
   if(! PyArg_UnpackTuple(a,#OP,2,2,&a1,&a2)) return NULL; \
@@ -97,8 +103,8 @@
 spamoi(op_irepeat      , PySequence_InPlaceRepeat)
 spami2b(op_contains     , PySequence_Contains)
 spami2b(sequenceIncludes, PySequence_Contains)
-spami2(indexOf         , PySequence_Index)
-spami2(countOf         , PySequence_Count)
+spamn2(indexOf         , PySequence_Index)
+spamn2(countOf         , PySequence_Count)
 spami(isMappingType    , PyMapping_Check)
 spam2(op_getitem       , PyObject_GetItem)
 spam2n(op_delitem       , PyObject_DelItem)
diff --git a/Modules/parsermodule.c b/Modules/parsermodule.c
index c9edae6..e33197e 100644
--- a/Modules/parsermodule.c
+++ b/Modules/parsermodule.c
@@ -3267,8 +3267,8 @@
             && (pickler != NULL)) {
             PyObject *res;
 
-            res = PyObject_CallFunction(func, "OOO", &PyST_Type, pickler,
-                                        pickle_constructor);
+            res = PyObject_CallFunctionObjArgs(func, &PyST_Type, pickler,
+                                               pickle_constructor, NULL);
             Py_XDECREF(res);
         }
         Py_XDECREF(func);
diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c
index b51ba5d..7f0a261 100644
--- a/Modules/posixmodule.c
+++ b/Modules/posixmodule.c
@@ -13,6 +13,18 @@
 
 /* See also ../Dos/dosmodule.c */
 
+#ifdef __APPLE__
+   /*
+    * Step 1 of support for weak-linking a number of symbols existing on 
+    * OSX 10.4 and later, see the comment in the #ifdef __APPLE__ block
+    * at the end of this file for more information.
+    */
+#  pragma weak lchown
+#  pragma weak statvfs
+#  pragma weak fstatvfs
+
+#endif /* __APPLE__ */
+
 #define PY_SSIZE_T_CLEAN
 
 #include "Python.h"
@@ -250,7 +262,11 @@
 #endif /* OS2 */
 
 #ifndef MAXPATHLEN
+#if defined(PATH_MAX) && PATH_MAX > 1024
+#define MAXPATHLEN PATH_MAX
+#else
 #define MAXPATHLEN 1024
+#endif
 #endif /* MAXPATHLEN */
 
 #ifdef UNION_WAIT
@@ -442,21 +458,29 @@
 
 static PyObject *_PyUnicode_FromFileSystemEncodedObject(register PyObject *obj)
 {
-	/* XXX Perhaps we should make this API an alias of
-	   PyObject_Unicode() instead ?! */
-	if (PyUnicode_CheckExact(obj)) {
-		Py_INCREF(obj);
-		return obj;
-	}
-	if (PyUnicode_Check(obj)) {
+}
+
+/* Function suitable for O& conversion */
+static int
+convert_to_unicode(PyObject *arg, void* _param)
+{
+	PyObject **param = (PyObject**)_param;
+	if (PyUnicode_CheckExact(arg)) {
+		Py_INCREF(arg);
+		*param = arg;
+	} 
+	else if (PyUnicode_Check(arg)) {
 		/* For a Unicode subtype that's not a Unicode object,
 		   return a true Unicode object with the same data. */
-	return PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(obj),
-	                             PyUnicode_GET_SIZE(obj));
+		*param = PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(arg),
+					       PyUnicode_GET_SIZE(arg));
+		return *param != NULL;
 	}
-	return PyUnicode_FromEncodedObject(obj,
-	                                   Py_FileSystemDefaultEncoding,
-	                                   "strict");
+	else
+		*param = PyUnicode_FromEncodedObject(arg,
+				                     Py_FileSystemDefaultEncoding,
+					             "strict");
+	return (*param) != NULL;
 }
 
 #endif /* Py_WIN_WIDE_FILENAMES */
@@ -573,35 +597,10 @@
 #endif
 
 static PyObject *
-posix_1str(PyObject *args, char *format, int (*func)(const char*),
-	   char *wformat, int (*wfunc)(const Py_UNICODE*))
+posix_1str(PyObject *args, char *format, int (*func)(const char*))
 {
 	char *path1 = NULL;
 	int res;
-#ifdef Py_WIN_WIDE_FILENAMES
-	if (unicode_file_names()) {
-		PyUnicodeObject *po;
-		if (PyArg_ParseTuple(args, wformat, &po)) {
-			Py_BEGIN_ALLOW_THREADS
-			/*  PyUnicode_AS_UNICODE OK without thread
-			    lock as it is a simple dereference. */
-			res = (*wfunc)(PyUnicode_AS_UNICODE(po));
-			Py_END_ALLOW_THREADS
-			if (res < 0)
-				return posix_error_with_unicode_filename(PyUnicode_AS_UNICODE(po));
-			Py_INCREF(Py_None);
-			return Py_None;
-		}
-		/* Drop the argument parsing error as narrow
-		   strings are also valid. */
-		PyErr_Clear();
-	}
-#else
-	/* Platforms that don't support Unicode filenames
-	   shouldn't be passing these extra params */
-	assert(wformat==NULL && wfunc == NULL);
-#endif
-
 	if (!PyArg_ParseTuple(args, format,
 	                      Py_FileSystemDefaultEncoding, &path1))
 		return NULL;
@@ -618,52 +617,10 @@
 static PyObject *
 posix_2str(PyObject *args,
 	   char *format,
-	   int (*func)(const char *, const char *),
-	   char *wformat,
-	   int (*wfunc)(const Py_UNICODE *, const Py_UNICODE *))
+	   int (*func)(const char *, const char *))
 {
 	char *path1 = NULL, *path2 = NULL;
 	int res;
-#ifdef Py_WIN_WIDE_FILENAMES
-	if (unicode_file_names()) {
-		PyObject *po1;
-		PyObject *po2;
-		if (PyArg_ParseTuple(args, wformat, &po1, &po2)) {
-			if (PyUnicode_Check(po1) || PyUnicode_Check(po2)) {
-				PyObject *wpath1;
-				PyObject *wpath2;
-				wpath1 = _PyUnicode_FromFileSystemEncodedObject(po1);
-				wpath2 = _PyUnicode_FromFileSystemEncodedObject(po2);
-				if (!wpath1 || !wpath2) {
-					Py_XDECREF(wpath1);
-					Py_XDECREF(wpath2);
-					return NULL;
-				}
-				Py_BEGIN_ALLOW_THREADS
-				/* PyUnicode_AS_UNICODE OK without thread
-				   lock as it is a simple dereference.  */
-				res = (*wfunc)(PyUnicode_AS_UNICODE(wpath1),
-					       PyUnicode_AS_UNICODE(wpath2));
-				Py_END_ALLOW_THREADS
-				Py_XDECREF(wpath1);
-				Py_XDECREF(wpath2);
-				if (res != 0)
-					return posix_error();
-				Py_INCREF(Py_None);
-				return Py_None;
-			}
-			/* Else flow through as neither is Unicode. */
-		}
-		/* Drop the argument parsing error as narrow
-		   strings are also valid. */
-		PyErr_Clear();
-	}
-#else
-	/* Platforms that don't support Unicode filenames
-	   shouldn't be passing these extra params */
-	assert(wformat==NULL && wfunc == NULL);
-#endif
-
 	if (!PyArg_ParseTuple(args, format,
 	                      Py_FileSystemDefaultEncoding, &path1,
 	                      Py_FileSystemDefaultEncoding, &path2))
@@ -680,6 +637,101 @@
 	return Py_None;
 }
 
+#ifdef Py_WIN_WIDE_FILENAMES
+static PyObject*
+win32_1str(PyObject* args, char* func, 
+	   char* format, BOOL (__stdcall *funcA)(LPCSTR), 
+	   char* wformat, BOOL (__stdcall *funcW)(LPWSTR))
+{
+	PyObject *uni;
+	char *ansi;
+	BOOL result;
+	if (unicode_file_names()) {
+		if (!PyArg_ParseTuple(args, wformat, &uni))
+			PyErr_Clear();
+		else {
+			Py_BEGIN_ALLOW_THREADS
+			result = funcW(PyUnicode_AsUnicode(uni));
+			Py_END_ALLOW_THREADS
+			if (!result)
+				return win32_error_unicode(func, PyUnicode_AsUnicode(uni));
+			Py_INCREF(Py_None);
+			return Py_None;
+		}
+	}
+	if (!PyArg_ParseTuple(args, format, &ansi))
+		return NULL;
+	Py_BEGIN_ALLOW_THREADS
+	result = funcA(ansi);
+	Py_END_ALLOW_THREADS
+	if (!result)
+		return win32_error(func, ansi);
+	Py_INCREF(Py_None);
+	return Py_None;
+
+}
+
+/* This is a reimplementation of the C library's chdir function,
+   but one that produces Win32 errors instead of DOS error codes.
+   chdir is essentially a wrapper around SetCurrentDirectory; however,
+   it also needs to set "magic" environment variables indicating
+   the per-drive current directory, which are of the form =<drive>: */
+BOOL __stdcall
+win32_chdir(LPCSTR path)
+{
+	char new_path[MAX_PATH+1];
+	int result;
+	char env[4] = "=x:";
+
+	if(!SetCurrentDirectoryA(path))
+		return FALSE;
+	result = GetCurrentDirectoryA(MAX_PATH+1, new_path);
+	if (!result)
+		return FALSE;
+	/* In the ANSI API, there should not be any paths longer
+	   than MAX_PATH. */
+	assert(result <= MAX_PATH+1);
+	if (strncmp(new_path, "\\\\", 2) == 0 ||
+	    strncmp(new_path, "//", 2) == 0)
+	    /* UNC path, nothing to do. */
+	    return TRUE;
+	env[1] = new_path[0];
+	return SetEnvironmentVariableA(env, new_path);
+}
+
+/* The Unicode version differs from the ANSI version
+   since the current directory might exceed MAX_PATH characters */
+BOOL __stdcall
+win32_wchdir(LPCWSTR path)
+{
+	wchar_t _new_path[MAX_PATH+1], *new_path = _new_path;
+	int result;
+	wchar_t env[4] = L"=x:";
+
+	if(!SetCurrentDirectoryW(path))
+		return FALSE;
+	result = GetCurrentDirectoryW(MAX_PATH+1, new_path);
+	if (!result)
+		return FALSE;
+	if (result > MAX_PATH+1) {
+		new_path = malloc(result);
+		if (!new_path) {
+			SetLastError(ERROR_OUTOFMEMORY);
+			return FALSE;
+		}
+	}
+	if (wcsncmp(new_path, L"\\\\", 2) == 0 ||
+	    wcsncmp(new_path, L"//", 2) == 0)
+	    /* UNC path, nothing to do. */
+	    return TRUE;
+	env[1] = new_path[0];
+	result = SetEnvironmentVariableW(env, new_path);
+	if (new_path != _new_path)
+		free(new_path);
+	return result;
+}
+#endif
+
 #ifdef MS_WINDOWS
 /* The CRT of Windows has a number of flaws wrt. its stat() implementation:
    - time stamps are restricted to second resolution
@@ -711,13 +763,26 @@
 static void
 FILE_TIME_to_time_t_nsec(FILETIME *in_ptr, int *time_out, int* nsec_out)
 {
-	/* XXX endianness */
-	__int64 in = *(__int64*)in_ptr;
+	/* XXX endianness. Shouldn't matter, as all Windows implementations are little-endian */
+	/* Cannot simply cast and dereference in_ptr, 
+	   since it might not be aligned properly */
+	__int64 in;
+	memcpy(&in, in_ptr, sizeof(in));
 	*nsec_out = (int)(in % 10000000) * 100; /* FILETIME is in units of 100 nsec. */
 	/* XXX Win32 supports time stamps past 2038; we currently don't */
 	*time_out = Py_SAFE_DOWNCAST((in / 10000000) - secs_between_epochs, __int64, int);
 }
 
+static void
+time_t_to_FILE_TIME(int time_in, int nsec_in, FILETIME *out_ptr)
+{
+	/* XXX endianness */
+	__int64 out;
+	out = time_in + secs_between_epochs;
+	out = out * 10000000 + nsec_in;
+	memcpy(out_ptr, &out, sizeof(out));
+}
+
 /* Below, we *know* that ugo+r is 0444 */
 #if _S_IREAD != 0400
 #error Unsupported C library
@@ -1292,24 +1357,39 @@
 {
 	char *path;
 	int mode;
-	int res;
-
+	
 #ifdef Py_WIN_WIDE_FILENAMES
+	DWORD attr;
 	if (unicode_file_names()) {
 		PyUnicodeObject *po;
 		if (PyArg_ParseTuple(args, "Ui:access", &po, &mode)) {
 			Py_BEGIN_ALLOW_THREADS
 			/* PyUnicode_AS_UNICODE OK without thread lock as
 			   it is a simple dereference. */
-			res = _waccess(PyUnicode_AS_UNICODE(po), mode);
+			attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po));
 			Py_END_ALLOW_THREADS
-			return PyBool_FromLong(res == 0);
+			goto finish;
 		}
 		/* Drop the argument parsing error as narrow strings
 		   are also valid. */
 		PyErr_Clear();
 	}
-#endif
+	if (!PyArg_ParseTuple(args, "eti:access",
+			      Py_FileSystemDefaultEncoding, &path, &mode))
+		return 0;
+	Py_BEGIN_ALLOW_THREADS
+	attr = GetFileAttributesA(path);
+	Py_END_ALLOW_THREADS
+	PyMem_Free(path);
+finish:
+	if (attr == 0xFFFFFFFF)
+		/* File does not exist, or cannot read attributes */
+		return PyBool_FromLong(0);
+	/* Access is possible if either write access wasn't requested, or
+	   the file isn't read-only. */
+	return PyBool_FromLong(!(mode & 2) || !(attr && FILE_ATTRIBUTE_READONLY));
+#else
+	int res;
 	if (!PyArg_ParseTuple(args, "eti:access", 
 			      Py_FileSystemDefaultEncoding, &path, &mode))
 		return NULL;
@@ -1318,6 +1398,7 @@
 	Py_END_ALLOW_THREADS
 	PyMem_Free(path);
 	return PyBool_FromLong(res == 0);
+#endif
 }
 
 #ifndef F_OK
@@ -1394,14 +1475,13 @@
 posix_chdir(PyObject *self, PyObject *args)
 {
 #ifdef MS_WINDOWS
-	return posix_1str(args, "et:chdir", chdir, "U:chdir", _wchdir);
+	return win32_1str(args, "chdir", "s:chdir", win32_chdir, "U:chdir", win32_wchdir);
 #elif defined(PYOS_OS2) && defined(PYCC_GCC)
-	return posix_1str(args, "et:chdir", _chdir2, NULL, NULL);
+	return posix_1str(args, "et:chdir", _chdir2);
 #elif defined(__VMS)
-	return posix_1str(args, "et:chdir", (int (*)(const char *))chdir,
-			  NULL, NULL);
+	return posix_1str(args, "et:chdir", (int (*)(const char *))chdir);
 #else
-	return posix_1str(args, "et:chdir", chdir, NULL, NULL);
+	return posix_1str(args, "et:chdir", chdir);
 #endif
 }
 
@@ -1430,14 +1510,24 @@
 	int i;
 	int res;
 #ifdef Py_WIN_WIDE_FILENAMES
+	DWORD attr;
 	if (unicode_file_names()) {
 		PyUnicodeObject *po;
 		if (PyArg_ParseTuple(args, "Ui|:chmod", &po, &i)) {
 			Py_BEGIN_ALLOW_THREADS
-			res = _wchmod(PyUnicode_AS_UNICODE(po), i);
+			attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po));
+			if (attr != 0xFFFFFFFF) {
+				if (i & _S_IWRITE)
+					attr &= ~FILE_ATTRIBUTE_READONLY;
+				else
+					attr |= FILE_ATTRIBUTE_READONLY;
+				res = SetFileAttributesW(PyUnicode_AS_UNICODE(po), attr);
+			}
+			else
+				res = 0;
 			Py_END_ALLOW_THREADS
-			if (res < 0)
-				return posix_error_with_unicode_filename(
+			if (!res)
+				return win32_error_unicode("chmod",
 						PyUnicode_AS_UNICODE(po));
 			Py_INCREF(Py_None);
 			return Py_None;
@@ -1446,7 +1536,30 @@
 		   are also valid. */
 		PyErr_Clear();
 	}
-#endif /* Py_WIN_WIDE_FILENAMES */
+	if (!PyArg_ParseTuple(args, "eti:chmod", Py_FileSystemDefaultEncoding,
+	                      &path, &i))
+		return NULL;
+	Py_BEGIN_ALLOW_THREADS
+	attr = GetFileAttributesA(path);
+	if (attr != 0xFFFFFFFF) {
+		if (i & _S_IWRITE)
+			attr &= ~FILE_ATTRIBUTE_READONLY;
+		else
+			attr |= FILE_ATTRIBUTE_READONLY;
+		res = SetFileAttributesA(path, attr);
+	}
+	else
+		res = 0;
+	Py_END_ALLOW_THREADS
+	if (!res) {
+		win32_error("chmod", path);
+		PyMem_Free(path);
+		return NULL;
+	}
+	PyMem_Free(path);
+	Py_INCREF(Py_None);
+	return Py_None;
+#else /* Py_WIN_WIDE_FILENAMES */
 	if (!PyArg_ParseTuple(args, "eti:chmod", Py_FileSystemDefaultEncoding,
 	                      &path, &i))
 		return NULL;
@@ -1458,6 +1571,7 @@
 	PyMem_Free(path);
 	Py_INCREF(Py_None);
 	return Py_None;
+#endif
 }
 
 
@@ -1469,7 +1583,7 @@
 static PyObject *
 posix_chroot(PyObject *self, PyObject *args)
 {
-	return posix_1str(args, "et:chroot", chroot, NULL, NULL);
+	return posix_1str(args, "et:chroot", chroot);
 }
 #endif
 
@@ -1593,15 +1707,33 @@
 	char *res;
 
 #ifdef Py_WIN_WIDE_FILENAMES
+	DWORD len;
 	if (unicode_file_names()) {
-		wchar_t *wres;
 		wchar_t wbuf[1026];
+		wchar_t *wbuf2 = wbuf;
+		PyObject *resobj;
 		Py_BEGIN_ALLOW_THREADS
-		wres = _wgetcwd(wbuf, sizeof wbuf/ sizeof wbuf[0]);
+		len = GetCurrentDirectoryW(sizeof wbuf/ sizeof wbuf[0], wbuf);
+		/* If the buffer is large enough, len does not include the
+		   terminating \0. If the buffer is too small, len includes
+		   the space needed for the terminator. */
+		if (len >= sizeof wbuf/ sizeof wbuf[0]) {
+			wbuf2 = malloc(len * sizeof(wchar_t));
+			if (wbuf2)
+				len = GetCurrentDirectoryW(len, wbuf2);
+		}
 		Py_END_ALLOW_THREADS
-		if (wres == NULL)
-			return posix_error();
-		return PyUnicode_FromWideChar(wbuf, wcslen(wbuf));
+		if (!wbuf2) {
+			PyErr_NoMemory();
+			return NULL;
+		}
+		if (!len) {
+			if (wbuf2 != wbuf) free(wbuf2);
+			return win32_error("getcwdu", NULL);
+		}
+		resobj = PyUnicode_FromWideChar(wbuf2, len);
+		if (wbuf2 != wbuf) free(wbuf2);
+		return resobj;
 	}
 #endif
 
@@ -1628,7 +1760,7 @@
 static PyObject *
 posix_link(PyObject *self, PyObject *args)
 {
-	return posix_2str(args, "etet:link", link, NULL, NULL);
+	return posix_2str(args, "etet:link", link);
 }
 #endif /* HAVE_LINK */
 
@@ -1653,37 +1785,46 @@
 	HANDLE hFindFile;
 	BOOL result;
 	WIN32_FIND_DATA FileData;
-	/* MAX_PATH characters could mean a bigger encoded string */
-	char namebuf[MAX_PATH*2+5];
+	char namebuf[MAX_PATH+5]; /* Overallocate for \\*.*\0 */
 	char *bufptr = namebuf;
-	Py_ssize_t len = sizeof(namebuf)/sizeof(namebuf[0]);
+	Py_ssize_t len = sizeof(namebuf)-5; /* only claim to have space for MAX_PATH */
 
 #ifdef Py_WIN_WIDE_FILENAMES
 	/* If on wide-character-capable OS see if argument
 	   is Unicode and if so use wide API.  */
 	if (unicode_file_names()) {
-		PyUnicodeObject *po;
+		PyObject *po;
 		if (PyArg_ParseTuple(args, "U:listdir", &po)) {
 			WIN32_FIND_DATAW wFileData;
-			Py_UNICODE wnamebuf[MAX_PATH*2+5];
+			Py_UNICODE *wnamebuf;
 			Py_UNICODE wch;
-			wcsncpy(wnamebuf, PyUnicode_AS_UNICODE(po), MAX_PATH);
-			wnamebuf[MAX_PATH] = L'\0';
-			len = wcslen(wnamebuf);
-			wch = (len > 0) ? wnamebuf[len-1] : L'\0';
-			if (wch != L'/' && wch != L'\\' && wch != L':')
-				wnamebuf[len++] = L'/';
-			wcscpy(wnamebuf + len, L"*.*");
-			if ((d = PyList_New(0)) == NULL)
+			/* Overallocate for \\*.*\0 */
+			len = PyUnicode_GET_SIZE(po);
+			wnamebuf = malloc((len + 5) * sizeof(wchar_t));
+			if (!wnamebuf) {
+				PyErr_NoMemory();
 				return NULL;
+			}
+			wcscpy(wnamebuf, PyUnicode_AS_UNICODE(po));
+			wch = len > 0 ? wnamebuf[len-1] : '\0';
+			if (wch != L'/' && wch != L'\\' && wch != L':')
+				wnamebuf[len++] = L'\\';
+			wcscpy(wnamebuf + len, L"*.*");
+			if ((d = PyList_New(0)) == NULL) {
+				free(wnamebuf);
+				return NULL;
+			}
 			hFindFile = FindFirstFileW(wnamebuf, &wFileData);
 			if (hFindFile == INVALID_HANDLE_VALUE) {
-				errno = GetLastError();
-				if (errno == ERROR_FILE_NOT_FOUND) {
+				int error = GetLastError();
+				if (error == ERROR_FILE_NOT_FOUND) {
+					free(wnamebuf);
 					return d;
 				}
 				Py_DECREF(d);
-				return win32_error_unicode("FindFirstFileW", wnamebuf);
+				win32_error_unicode("FindFirstFileW", wnamebuf);
+				free(wnamebuf);
+				return NULL;
 			}
 			do {
 				/* Skip over . and .. */
@@ -1710,8 +1851,11 @@
 
 			if (FindClose(hFindFile) == FALSE) {
 				Py_DECREF(d);
-				return win32_error_unicode("FindClose", wnamebuf);
+				win32_error_unicode("FindClose", wnamebuf);
+				free(wnamebuf);
+				return NULL;
 			}
+			free(wnamebuf);
 			return d;
 		}
 		/* Drop the argument parsing error as narrow strings
@@ -1735,8 +1879,8 @@
 
 	hFindFile = FindFirstFile(namebuf, &FileData);
 	if (hFindFile == INVALID_HANDLE_VALUE) {
-		errno = GetLastError();
-		if (errno == ERROR_FILE_NOT_FOUND)
+		int error = GetLastError();
+		if (error == ERROR_FILE_NOT_FOUND)
 			return d;
 		Py_DECREF(d);
 		return win32_error("FindFirstFile", namebuf);
@@ -1982,10 +2126,10 @@
 			Py_BEGIN_ALLOW_THREADS
 			/* PyUnicode_AS_UNICODE OK without thread lock as
 			   it is a simple dereference. */
-			res = _wmkdir(PyUnicode_AS_UNICODE(po));
+			res = CreateDirectoryW(PyUnicode_AS_UNICODE(po), NULL);
 			Py_END_ALLOW_THREADS
-			if (res < 0)
-				return posix_error();
+			if (!res)
+				return win32_error_unicode("mkdir", PyUnicode_AS_UNICODE(po));
 			Py_INCREF(Py_None);
 			return Py_None;
 		}
@@ -1993,13 +2137,29 @@
 		   are also valid. */
 		PyErr_Clear();
 	}
-#endif
+	if (!PyArg_ParseTuple(args, "et|i:mkdir",
+	                      Py_FileSystemDefaultEncoding, &path, &mode))
+		return NULL;
+	Py_BEGIN_ALLOW_THREADS
+	/* PyUnicode_AS_UNICODE OK without thread lock as
+	   it is a simple dereference. */
+	res = CreateDirectoryA(path, NULL);
+	Py_END_ALLOW_THREADS
+	if (!res) {
+		win32_error("mkdir", path);
+		PyMem_Free(path);
+		return NULL;
+	}
+	PyMem_Free(path);
+	Py_INCREF(Py_None);
+	return Py_None;
+#else
 
 	if (!PyArg_ParseTuple(args, "et|i:mkdir",
 	                      Py_FileSystemDefaultEncoding, &path, &mode))
 		return NULL;
 	Py_BEGIN_ALLOW_THREADS
-#if ( defined(__WATCOMC__) || defined(_MSC_VER) || defined(PYCC_VACPP) ) && !defined(__QNX__)
+#if ( defined(__WATCOMC__) || defined(PYCC_VACPP) ) && !defined(__QNX__)
 	res = mkdir(path);
 #else
 	res = mkdir(path, mode);
@@ -2010,6 +2170,7 @@
 	PyMem_Free(path);
 	Py_INCREF(Py_None);
 	return Py_None;
+#endif
 }
 
 
@@ -2055,7 +2216,6 @@
 }
 #endif /* HAVE_NICE */
 
-
 PyDoc_STRVAR(posix_rename__doc__,
 "rename(old, new)\n\n\
 Rename a file or directory.");
@@ -2064,9 +2224,38 @@
 posix_rename(PyObject *self, PyObject *args)
 {
 #ifdef MS_WINDOWS
-	return posix_2str(args, "etet:rename", rename, "OO:rename", _wrename);
+	PyObject *o1, *o2;
+	char *p1, *p2;
+	BOOL result;
+	if (unicode_file_names()) {
+	    if (!PyArg_ParseTuple(args, "O&O&:rename", 
+		convert_to_unicode, &o1,
+		convert_to_unicode, &o2))
+		    PyErr_Clear();
+	    else {
+		    Py_BEGIN_ALLOW_THREADS
+		    result = MoveFileW(PyUnicode_AsUnicode(o1),
+				       PyUnicode_AsUnicode(o2));
+		    Py_END_ALLOW_THREADS
+		    Py_DECREF(o1);
+		    Py_DECREF(o2);
+		    if (!result)
+			    return win32_error("rename", NULL);
+		    Py_INCREF(Py_None);
+		    return Py_None;
+	    }
+	}
+	if (!PyArg_ParseTuple(args, "ss:rename", &p1, &p2))
+		return NULL;
+	Py_BEGIN_ALLOW_THREADS
+	result = MoveFileA(p1, p2);
+	Py_END_ALLOW_THREADS
+	if (!result)
+		return win32_error("rename", NULL);
+	Py_INCREF(Py_None);
+	return Py_None;
 #else
-	return posix_2str(args, "etet:rename", rename, NULL, NULL);
+	return posix_2str(args, "etet:rename", rename);
 #endif
 }
 
@@ -2079,9 +2268,9 @@
 posix_rmdir(PyObject *self, PyObject *args)
 {
 #ifdef MS_WINDOWS
-	return posix_1str(args, "et:rmdir", rmdir, "U:rmdir", _wrmdir);
+	return win32_1str(args, "rmdir", "s:rmdir", RemoveDirectoryA, "U:rmdir", RemoveDirectoryW);
 #else
-	return posix_1str(args, "et:rmdir", rmdir, NULL, NULL);
+	return posix_1str(args, "et:rmdir", rmdir);
 #endif
 }
 
@@ -2150,9 +2339,9 @@
 posix_unlink(PyObject *self, PyObject *args)
 {
 #ifdef MS_WINDOWS
-	return posix_1str(args, "et:remove", unlink, "U:remove", _wunlink);
+	return win32_1str(args, "remove", "s:remove", DeleteFileA, "U:remove", DeleteFileW);
 #else
-	return posix_1str(args, "et:remove", unlink, NULL, NULL);
+	return posix_1str(args, "et:remove", unlink);
 #endif
 }
 
@@ -2212,7 +2401,7 @@
 }
 
 PyDoc_STRVAR(posix_utime__doc__,
-"utime(path, (atime, utime))\n\
+"utime(path, (atime, mtime))\n\
 utime(path, None)\n\n\
 Set the access and modified time of the file to the given values.  If the\n\
 second form is used, set the access and modified times to the current time.");
@@ -2220,6 +2409,84 @@
 static PyObject *
 posix_utime(PyObject *self, PyObject *args)
 {
+#ifdef Py_WIN_WIDE_FILENAMES
+	PyObject *arg;
+	PyUnicodeObject *obwpath;
+	wchar_t *wpath = NULL;
+	char *apath = NULL;
+	HANDLE hFile;
+	long atimesec, mtimesec, ausec, musec;
+	FILETIME atime, mtime;
+	PyObject *result = NULL;
+
+	if (unicode_file_names()) {
+		if (PyArg_ParseTuple(args, "UO|:utime", &obwpath, &arg)) {
+			wpath = PyUnicode_AS_UNICODE(obwpath);
+			Py_BEGIN_ALLOW_THREADS
+			hFile = CreateFileW(wpath, FILE_WRITE_ATTRIBUTES, 0,
+					    NULL, OPEN_EXISTING, 0, NULL);
+			Py_END_ALLOW_THREADS
+			if (hFile == INVALID_HANDLE_VALUE)
+				return win32_error_unicode("utime", wpath);
+		} else
+			/* Drop the argument parsing error as narrow strings
+			   are also valid. */
+			PyErr_Clear();
+	}
+	if (!wpath) {
+		if (!PyArg_ParseTuple(args, "etO:utime",
+				Py_FileSystemDefaultEncoding, &apath, &arg))
+			return NULL;
+		Py_BEGIN_ALLOW_THREADS
+		hFile = CreateFileA(apath, FILE_WRITE_ATTRIBUTES, 0,
+				    NULL, OPEN_EXISTING, 0, NULL);
+		Py_END_ALLOW_THREADS
+		if (hFile == INVALID_HANDLE_VALUE) {
+			win32_error("utime", apath);
+			PyMem_Free(apath);
+			return NULL;
+		}
+		PyMem_Free(apath);
+	}
+	
+	if (arg == Py_None) {
+		SYSTEMTIME now;
+		GetSystemTime(&now);
+		if (!SystemTimeToFileTime(&now, &mtime) ||
+		    !SystemTimeToFileTime(&now, &atime)) {
+			win32_error("utime", NULL);
+			goto done;
+		    }
+	}
+	else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
+		PyErr_SetString(PyExc_TypeError,
+				"utime() arg 2 must be a tuple (atime, mtime)");
+		goto done;
+	}
+	else {
+		if (extract_time(PyTuple_GET_ITEM(arg, 0),
+				 &atimesec, &ausec) == -1)
+			goto done;
+		time_t_to_FILE_TIME(atimesec, ausec, &atime);
+		if (extract_time(PyTuple_GET_ITEM(arg, 1),
+				 &mtimesec, &musec) == -1)
+			goto done;
+		time_t_to_FILE_TIME(mtimesec, musec, &mtime);
+	}
+	if (!SetFileTime(hFile, NULL, &atime, &mtime)) {
+		/* Avoid putting the file name into the error here,
+		   as that may confuse the user into believing that
+		   something is wrong with the file, when it also
+		   could be the time stamp that gives a problem. */
+		win32_error("utime", NULL);
+	}
+	Py_INCREF(Py_None);
+	result = Py_None;
+done:
+	CloseHandle(hFile);
+	return result;
+#else /* Py_WIN_WIDE_FILENAMES */
+
 	char *path = NULL;
 	long atime, mtime, ausec, musec;
 	int res;
@@ -2242,33 +2509,13 @@
 #define UTIME_ARG buf
 #endif /* HAVE_UTIMES */
 
-	int have_unicode_filename = 0;
-#ifdef Py_WIN_WIDE_FILENAMES
-	PyUnicodeObject *obwpath;
-	wchar_t *wpath;
-	if (unicode_file_names()) {
-		if (PyArg_ParseTuple(args, "UO|:utime", &obwpath, &arg)) {
-			wpath = PyUnicode_AS_UNICODE(obwpath);
-			have_unicode_filename = 1;
-		} else
-			/* Drop the argument parsing error as narrow strings
-			   are also valid. */
-			PyErr_Clear();
-	}
-#endif /* Py_WIN_WIDE_FILENAMES */
 
-	if (!have_unicode_filename && \
-		!PyArg_ParseTuple(args, "etO:utime",
+	if (!PyArg_ParseTuple(args, "etO:utime",
 				  Py_FileSystemDefaultEncoding, &path, &arg))
 		return NULL;
 	if (arg == Py_None) {
 		/* optional time values not given */
 		Py_BEGIN_ALLOW_THREADS
-#ifdef Py_WIN_WIDE_FILENAMES
-		if (have_unicode_filename)
-			res = _wutime(wpath, NULL);
-		else
-#endif /* Py_WIN_WIDE_FILENAMES */
 		res = utime(path, NULL);
 		Py_END_ALLOW_THREADS
 	}
@@ -2299,23 +2546,11 @@
 		Py_END_ALLOW_THREADS
 #else
 		Py_BEGIN_ALLOW_THREADS
-#ifdef Py_WIN_WIDE_FILENAMES
-		if (have_unicode_filename)
-			/* utime is OK with utimbuf, but _wutime insists
-			   on _utimbuf (the msvc headers assert the
-			   underscore version is ansi) */
-			res = _wutime(wpath, (struct _utimbuf *)UTIME_ARG);
-		else
-#endif /* Py_WIN_WIDE_FILENAMES */
 		res = utime(path, UTIME_ARG);
 		Py_END_ALLOW_THREADS
 #endif /* HAVE_UTIMES */
 	}
 	if (res < 0) {
-#ifdef Py_WIN_WIDE_FILENAMES
-		if (have_unicode_filename)
-			return posix_error_with_unicode_filename(wpath);
-#endif /* Py_WIN_WIDE_FILENAMES */
 		return posix_error_with_allocated_filename(path);
 	}
 	PyMem_Free(path);
@@ -2324,6 +2559,7 @@
 #undef UTIME_ARG
 #undef ATIME
 #undef MTIME
+#endif /* Py_WIN_WIDE_FILENAMES */
 }
 
 
@@ -2440,7 +2676,7 @@
 	PyObject *key, *val, *keys=NULL, *vals=NULL;
 	Py_ssize_t i, pos, argc, envc;
 	PyObject *(*getitem)(PyObject *, Py_ssize_t);
-	int lastarg = 0;
+	Py_ssize_t lastarg = 0;
 
 	/* execve has three arguments: (path, argv, env), where
 	   argv is a list or tuple of strings and env is a dictionary
@@ -2581,7 +2817,8 @@
 	char *path;
 	PyObject *argv;
 	char **argvlist;
-	int mode, i, argc;
+	int mode, i;
+	Py_ssize_t argc;
 	Py_intptr_t spawnval;
 	PyObject *(*getitem)(PyObject *, Py_ssize_t);
 
@@ -2670,10 +2907,11 @@
 	char **argvlist;
 	char **envlist;
 	PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL;
-	int mode, i, pos, argc, envc;
+	int mode, pos, envc;
+	Py_ssize_t argc, i;
 	Py_intptr_t spawnval;
 	PyObject *(*getitem)(PyObject *, Py_ssize_t);
-	int lastarg = 0;
+	Py_ssize_t lastarg = 0;
 
 	/* spawnve has four arguments: (mode, path, argv, env), where
 	   argv is a list or tuple of strings and env is a dictionary
@@ -4374,7 +4612,7 @@
 			char modulepath[_MAX_PATH];
 			struct stat statinfo;
 			GetModuleFileName(NULL, modulepath, sizeof(modulepath));
-			for (i = x = 0; modulepath[i]; i++)
+			for (x = i = 0; modulepath[i]; i++)
 				if (modulepath[i] == SEP)
 					x = i+1;
 			modulepath[x] = '\0';
@@ -4551,7 +4789,7 @@
 		 switch (mode & (_O_RDONLY | _O_TEXT | _O_BINARY | _O_WRONLY)) {
 		 case _O_WRONLY | _O_TEXT:
 			 /* Case for writing to child Stdin in text mode. */
-			 fd1 = _open_osfhandle((intptr_t)hChildStdinWrDup, mode);
+			 fd1 = _open_osfhandle((Py_intptr_t)hChildStdinWrDup, mode);
 			 f1 = _fdopen(fd1, "w");
 			 f = PyFile_FromFile(f1, cmdstring, "w", _PyPclose);
 			 PyFile_SetBufSize(f, 0);
@@ -4562,7 +4800,7 @@
 
 		 case _O_RDONLY | _O_TEXT:
 			 /* Case for reading from child Stdout in text mode. */
-			 fd1 = _open_osfhandle((intptr_t)hChildStdoutRdDup, mode);
+			 fd1 = _open_osfhandle((Py_intptr_t)hChildStdoutRdDup, mode);
 			 f1 = _fdopen(fd1, "r");
 			 f = PyFile_FromFile(f1, cmdstring, "r", _PyPclose);
 			 PyFile_SetBufSize(f, 0);
@@ -4573,7 +4811,7 @@
 
 		 case _O_RDONLY | _O_BINARY:
 			 /* Case for readinig from child Stdout in binary mode. */
-			 fd1 = _open_osfhandle((intptr_t)hChildStdoutRdDup, mode);
+			 fd1 = _open_osfhandle((Py_intptr_t)hChildStdoutRdDup, mode);
 			 f1 = _fdopen(fd1, "rb");
 			 f = PyFile_FromFile(f1, cmdstring, "rb", _PyPclose);
 			 PyFile_SetBufSize(f, 0);
@@ -4584,7 +4822,7 @@
 
 		 case _O_WRONLY | _O_BINARY:
 			 /* Case for writing to child Stdin in binary mode. */
-			 fd1 = _open_osfhandle((intptr_t)hChildStdinWrDup, mode);
+			 fd1 = _open_osfhandle((Py_intptr_t)hChildStdinWrDup, mode);
 			 f1 = _fdopen(fd1, "wb");
 			 f = PyFile_FromFile(f1, cmdstring, "wb", _PyPclose);
 			 PyFile_SetBufSize(f, 0);
@@ -4610,9 +4848,9 @@
 			 m2 = "wb";
 		 }
 
-		 fd1 = _open_osfhandle((intptr_t)hChildStdinWrDup, mode);
+		 fd1 = _open_osfhandle((Py_intptr_t)hChildStdinWrDup, mode);
 		 f1 = _fdopen(fd1, m2);
-		 fd2 = _open_osfhandle((intptr_t)hChildStdoutRdDup, mode);
+		 fd2 = _open_osfhandle((Py_intptr_t)hChildStdoutRdDup, mode);
 		 f2 = _fdopen(fd2, m1);
 		 p1 = PyFile_FromFile(f1, cmdstring, m2, _PyPclose);
 		 PyFile_SetBufSize(p1, 0);
@@ -4642,11 +4880,11 @@
 			 m2 = "wb";
 		 }
 
-		 fd1 = _open_osfhandle((intptr_t)hChildStdinWrDup, mode);
+		 fd1 = _open_osfhandle((Py_intptr_t)hChildStdinWrDup, mode);
 		 f1 = _fdopen(fd1, m2);
-		 fd2 = _open_osfhandle((intptr_t)hChildStdoutRdDup, mode);
+		 fd2 = _open_osfhandle((Py_intptr_t)hChildStdoutRdDup, mode);
 		 f2 = _fdopen(fd2, m1);
-		 fd3 = _open_osfhandle((intptr_t)hChildStderrRdDup, mode);
+		 fd3 = _open_osfhandle((Py_intptr_t)hChildStderrRdDup, mode);
 		 f3 = _fdopen(fd3, m1);
 		 p1 = PyFile_FromFile(f1, cmdstring, m2, _PyPclose);
 		 p2 = PyFile_FromFile(f2, cmdstring, m1, _PyPclose);
@@ -5250,7 +5488,7 @@
 static PyObject *
 posix_waitpid(PyObject *self, PyObject *args)
 {
-	intptr_t pid;
+	Py_intptr_t pid;
 	int status, options;
 
 	if (!PyArg_ParseTuple(args, "ii:waitpid", &pid, &options))
@@ -5339,7 +5577,7 @@
 static PyObject *
 posix_symlink(PyObject *self, PyObject *args)
 {
-	return posix_2str(args, "etet:symlink", symlink, NULL, NULL);
+	return posix_2str(args, "etet:symlink", symlink);
 }
 #endif /* HAVE_SYMLINK */
 
@@ -6280,7 +6518,7 @@
 #endif /* HAVE_SYS_WAIT_H */
 
 
-#if defined(HAVE_FSTATVFS)
+#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
 #ifdef _SCO_DS
 /* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the
    needed definitions in sys/statvfs.h */
@@ -6347,10 +6585,10 @@
 
         return _pystatvfs_fromstructstatvfs(st);
 }
-#endif /* HAVE_FSTATVFS */
+#endif /* HAVE_FSTATVFS && HAVE_SYS_STATVFS_H */
 
 
-#if defined(HAVE_STATVFS)
+#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
 #include <sys/statvfs.h>
 
 PyDoc_STRVAR(posix_statvfs__doc__,
@@ -7888,10 +8126,10 @@
         {"WSTOPSIG",	posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__},
 #endif /* WSTOPSIG */
 #endif /* HAVE_SYS_WAIT_H */
-#ifdef HAVE_FSTATVFS
+#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
 	{"fstatvfs",	posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__},
 #endif
-#ifdef HAVE_STATVFS
+#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
 	{"statvfs",	posix_statvfs, METH_VARARGS, posix_statvfs__doc__},
 #endif
 #ifdef HAVE_TMPFILE
@@ -8264,6 +8502,45 @@
 	PyModule_AddObject(m, "statvfs_result",
 			   (PyObject*) &StatVFSResultType);
 	initialized = 1;
+
+#ifdef __APPLE__
+	/*
+	 * Step 2 of weak-linking support on Mac OS X.
+	 *
+	 * The code below removes functions that are not available on the
+	 * currently active platform. 
+	 *
+	 * This block allow one to use a python binary that was build on
+	 * OSX 10.4 on OSX 10.3, without loosing access to new APIs on 
+	 * OSX 10.4.
+	 */
+#ifdef HAVE_FSTATVFS
+	if (fstatvfs == NULL) {
+		if (PyObject_DelAttrString(m, "fstatvfs") == -1) {
+			return;
+		}
+	}
+#endif /* HAVE_FSTATVFS */
+
+#ifdef HAVE_STATVFS
+	if (statvfs == NULL) {
+		if (PyObject_DelAttrString(m, "statvfs") == -1) {
+			return;
+		}
+	}
+#endif /* HAVE_STATVFS */
+
+# ifdef HAVE_LCHOWN
+	if (lchown == NULL) {
+		if (PyObject_DelAttrString(m, "lchown") == -1) {
+			return;
+		}
+	}
+#endif /* HAVE_LCHOWN */
+
+
+#endif /* __APPLE__ */
+
 }
 
 #ifdef __cplusplus
diff --git a/Modules/rotatingtree.h b/Modules/rotatingtree.h
index 97cc8e8..3aa0986 100644
--- a/Modules/rotatingtree.h
+++ b/Modules/rotatingtree.h
@@ -4,7 +4,7 @@
  *
  * It's a dict-like data structure that works best when accesses are not
  * random, but follow a strong pattern.  The one implemented here is for
- * accesses patterns where the same small set of keys is looked up over
+ * access patterns where the same small set of keys is looked up over
  * and over again, and this set of keys evolves slowly over time.
  */
 
diff --git a/Modules/selectmodule.c b/Modules/selectmodule.c
index c9d403a..4a04af1 100644
--- a/Modules/selectmodule.c
+++ b/Modules/selectmodule.c
@@ -8,6 +8,13 @@
 
 #include "Python.h"
 
+#ifdef __APPLE__
+    /* Perform runtime testing for a broken poll on OSX to make it easier
+     * to use the same binary on multiple releases of the OS.
+     */
+#undef HAVE_BROKEN_POLL
+#endif
+
 /* Windows #defines FD_SETSIZE to 64 if FD_SETSIZE isn't already defined.
    64 is too small (too many people have bumped into that limit).
    Here we boost it.
@@ -618,7 +625,38 @@
 		return NULL;
 	return (PyObject *)rv;
 }
-#endif /* HAVE_POLL && !HAVE_BROKEN_POLL */
+
+#ifdef __APPLE__
+/* 
+ * On some systems poll() sets errno on invalid file descriptors. We test
+ * for this at runtime because this bug may be fixed or introduced between
+ * OS releases.
+ */
+static int select_have_broken_poll(void)
+{
+	int poll_test;
+	int filedes[2];
+
+	struct pollfd poll_struct = { 0, POLLIN|POLLPRI|POLLOUT, 0 };
+
+	/* Create a file descriptor to make invalid */
+	if (pipe(filedes) < 0) {
+		return 1;
+	}
+	poll_struct.fd = filedes[0];
+	close(filedes[0]);
+	close(filedes[1]);
+	poll_test = poll(&poll_struct, 1, 0);
+	if (poll_test < 0) {
+		return 1;
+	} else if (poll_test == 0 && poll_struct.revents != POLLNVAL) {
+		return 1;
+	}
+	return 0;
+}
+#endif /* __APPLE__ */
+
+#endif /* HAVE_POLL */
 
 PyDoc_STRVAR(select_doc,
 "select(rlist, wlist, xlist[, timeout]) -> (rlist, wlist, xlist)\n\
@@ -645,9 +683,9 @@
 
 static PyMethodDef select_methods[] = {
     {"select",	select_select, METH_VARARGS, select_doc},
-#if defined(HAVE_POLL) && !defined(HAVE_BROKEN_POLL)
+#if defined(HAVE_POLL) 
     {"poll",    select_poll,   METH_VARARGS, poll_doc},
-#endif /* HAVE_POLL && !HAVE_BROKEN_POLL */
+#endif /* HAVE_POLL */
     {0,  	0},			     /* sentinel */
 };
 
@@ -668,29 +706,40 @@
 	SelectError = PyErr_NewException("select.error", NULL, NULL);
 	Py_INCREF(SelectError);
 	PyModule_AddObject(m, "error", SelectError);
-#if defined(HAVE_POLL) && !defined(HAVE_BROKEN_POLL)
-	poll_Type.ob_type = &PyType_Type;
-	PyModule_AddIntConstant(m, "POLLIN", POLLIN);
-	PyModule_AddIntConstant(m, "POLLPRI", POLLPRI);
-	PyModule_AddIntConstant(m, "POLLOUT", POLLOUT);
-	PyModule_AddIntConstant(m, "POLLERR", POLLERR);
-	PyModule_AddIntConstant(m, "POLLHUP", POLLHUP);
-	PyModule_AddIntConstant(m, "POLLNVAL", POLLNVAL);
+#if defined(HAVE_POLL) 
+
+#ifdef __APPLE__
+	if (select_have_broken_poll()) {
+		if (PyObject_DelAttrString(m, "poll") == -1) {
+			PyErr_Clear();
+		}
+	} else {
+#else
+	{
+#endif
+		poll_Type.ob_type = &PyType_Type;
+		PyModule_AddIntConstant(m, "POLLIN", POLLIN);
+		PyModule_AddIntConstant(m, "POLLPRI", POLLPRI);
+		PyModule_AddIntConstant(m, "POLLOUT", POLLOUT);
+		PyModule_AddIntConstant(m, "POLLERR", POLLERR);
+		PyModule_AddIntConstant(m, "POLLHUP", POLLHUP);
+		PyModule_AddIntConstant(m, "POLLNVAL", POLLNVAL);
 
 #ifdef POLLRDNORM
-	PyModule_AddIntConstant(m, "POLLRDNORM", POLLRDNORM);
+		PyModule_AddIntConstant(m, "POLLRDNORM", POLLRDNORM);
 #endif
 #ifdef POLLRDBAND
-	PyModule_AddIntConstant(m, "POLLRDBAND", POLLRDBAND);
+		PyModule_AddIntConstant(m, "POLLRDBAND", POLLRDBAND);
 #endif
 #ifdef POLLWRNORM
-	PyModule_AddIntConstant(m, "POLLWRNORM", POLLWRNORM);
+		PyModule_AddIntConstant(m, "POLLWRNORM", POLLWRNORM);
 #endif
 #ifdef POLLWRBAND
-	PyModule_AddIntConstant(m, "POLLWRBAND", POLLWRBAND);
+		PyModule_AddIntConstant(m, "POLLWRBAND", POLLWRBAND);
 #endif
 #ifdef POLLMSG
-	PyModule_AddIntConstant(m, "POLLMSG", POLLMSG);
+		PyModule_AddIntConstant(m, "POLLMSG", POLLMSG);
 #endif
-#endif /* HAVE_POLL && !HAVE_BROKEN_POLL */
+	}
+#endif /* HAVE_POLL */
 }
diff --git a/Modules/sha512module.c b/Modules/sha512module.c
index 539204e..c5a85ff 100644
--- a/Modules/sha512module.c
+++ b/Modules/sha512module.c
@@ -121,12 +121,12 @@
 
 /* Various logical functions */
 #define ROR64(x, y) \
-    ( ((((x) & 0xFFFFFFFFFFFFFFFFULL)>>((unsigned PY_LONG_LONG)(y) & 63)) | \
-      ((x)<<((unsigned PY_LONG_LONG)(64-((y) & 63))))) & 0xFFFFFFFFFFFFFFFFULL)
+    ( ((((x) & Py_ULL(0xFFFFFFFFFFFFFFFF))>>((unsigned PY_LONG_LONG)(y) & 63)) | \
+      ((x)<<((unsigned PY_LONG_LONG)(64-((y) & 63))))) & Py_ULL(0xFFFFFFFFFFFFFFFF))
 #define Ch(x,y,z)       (z ^ (x & (y ^ z)))
 #define Maj(x,y,z)      (((x | y) & z) | (x & y)) 
 #define S(x, n)         ROR64((x),(n))
-#define R(x, n)         (((x) & 0xFFFFFFFFFFFFFFFFULL) >> ((unsigned PY_LONG_LONG)n))
+#define R(x, n)         (((x) & Py_ULL(0xFFFFFFFFFFFFFFFF)) >> ((unsigned PY_LONG_LONG)n))
 #define Sigma0(x)       (S(x, 28) ^ S(x, 34) ^ S(x, 39))
 #define Sigma1(x)       (S(x, 14) ^ S(x, 18) ^ S(x, 41))
 #define Gamma0(x)       (S(x, 1) ^ S(x, 8) ^ R(x, 7))
@@ -156,86 +156,86 @@
      d += t0;                                        \
      h  = t0 + t1;
 
-    RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],0,0x428a2f98d728ae22ULL);
-    RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],1,0x7137449123ef65cdULL);
-    RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],2,0xb5c0fbcfec4d3b2fULL);
-    RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],3,0xe9b5dba58189dbbcULL);
-    RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],4,0x3956c25bf348b538ULL);
-    RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],5,0x59f111f1b605d019ULL);
-    RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],6,0x923f82a4af194f9bULL);
-    RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],7,0xab1c5ed5da6d8118ULL);
-    RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],8,0xd807aa98a3030242ULL);
-    RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],9,0x12835b0145706fbeULL);
-    RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],10,0x243185be4ee4b28cULL);
-    RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],11,0x550c7dc3d5ffb4e2ULL);
-    RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],12,0x72be5d74f27b896fULL);
-    RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],13,0x80deb1fe3b1696b1ULL);
-    RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],14,0x9bdc06a725c71235ULL);
-    RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],15,0xc19bf174cf692694ULL);
-    RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],16,0xe49b69c19ef14ad2ULL);
-    RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],17,0xefbe4786384f25e3ULL);
-    RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],18,0x0fc19dc68b8cd5b5ULL);
-    RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],19,0x240ca1cc77ac9c65ULL);
-    RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],20,0x2de92c6f592b0275ULL);
-    RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],21,0x4a7484aa6ea6e483ULL);
-    RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],22,0x5cb0a9dcbd41fbd4ULL);
-    RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],23,0x76f988da831153b5ULL);
-    RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],24,0x983e5152ee66dfabULL);
-    RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],25,0xa831c66d2db43210ULL);
-    RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],26,0xb00327c898fb213fULL);
-    RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],27,0xbf597fc7beef0ee4ULL);
-    RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],28,0xc6e00bf33da88fc2ULL);
-    RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],29,0xd5a79147930aa725ULL);
-    RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],30,0x06ca6351e003826fULL);
-    RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],31,0x142929670a0e6e70ULL);
-    RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],32,0x27b70a8546d22ffcULL);
-    RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],33,0x2e1b21385c26c926ULL);
-    RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],34,0x4d2c6dfc5ac42aedULL);
-    RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],35,0x53380d139d95b3dfULL);
-    RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],36,0x650a73548baf63deULL);
-    RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],37,0x766a0abb3c77b2a8ULL);
-    RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],38,0x81c2c92e47edaee6ULL);
-    RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],39,0x92722c851482353bULL);
-    RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],40,0xa2bfe8a14cf10364ULL);
-    RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],41,0xa81a664bbc423001ULL);
-    RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],42,0xc24b8b70d0f89791ULL);
-    RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],43,0xc76c51a30654be30ULL);
-    RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],44,0xd192e819d6ef5218ULL);
-    RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],45,0xd69906245565a910ULL);
-    RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],46,0xf40e35855771202aULL);
-    RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],47,0x106aa07032bbd1b8ULL);
-    RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],48,0x19a4c116b8d2d0c8ULL);
-    RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],49,0x1e376c085141ab53ULL);
-    RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],50,0x2748774cdf8eeb99ULL);
-    RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],51,0x34b0bcb5e19b48a8ULL);
-    RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],52,0x391c0cb3c5c95a63ULL);
-    RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],53,0x4ed8aa4ae3418acbULL);
-    RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],54,0x5b9cca4f7763e373ULL);
-    RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],55,0x682e6ff3d6b2b8a3ULL);
-    RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],56,0x748f82ee5defb2fcULL);
-    RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],57,0x78a5636f43172f60ULL);
-    RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],58,0x84c87814a1f0ab72ULL);
-    RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],59,0x8cc702081a6439ecULL);
-    RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],60,0x90befffa23631e28ULL);
-    RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],61,0xa4506cebde82bde9ULL);
-    RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],62,0xbef9a3f7b2c67915ULL);
-    RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],63,0xc67178f2e372532bULL);
-    RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],64,0xca273eceea26619cULL);
-    RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],65,0xd186b8c721c0c207ULL);
-    RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],66,0xeada7dd6cde0eb1eULL);
-    RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],67,0xf57d4f7fee6ed178ULL);
-    RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],68,0x06f067aa72176fbaULL);
-    RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],69,0x0a637dc5a2c898a6ULL);
-    RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],70,0x113f9804bef90daeULL);
-    RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],71,0x1b710b35131c471bULL);
-    RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],72,0x28db77f523047d84ULL);
-    RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],73,0x32caab7b40c72493ULL);
-    RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],74,0x3c9ebe0a15c9bebcULL);
-    RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],75,0x431d67c49c100d4cULL);
-    RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],76,0x4cc5d4becb3e42b6ULL);
-    RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],77,0x597f299cfc657e2aULL);
-    RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],78,0x5fcb6fab3ad6faecULL);
-    RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],79,0x6c44198c4a475817ULL);
+    RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],0,Py_ULL(0x428a2f98d728ae22));
+    RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],1,Py_ULL(0x7137449123ef65cd));
+    RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],2,Py_ULL(0xb5c0fbcfec4d3b2f));
+    RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],3,Py_ULL(0xe9b5dba58189dbbc));
+    RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],4,Py_ULL(0x3956c25bf348b538));
+    RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],5,Py_ULL(0x59f111f1b605d019));
+    RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],6,Py_ULL(0x923f82a4af194f9b));
+    RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],7,Py_ULL(0xab1c5ed5da6d8118));
+    RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],8,Py_ULL(0xd807aa98a3030242));
+    RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],9,Py_ULL(0x12835b0145706fbe));
+    RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],10,Py_ULL(0x243185be4ee4b28c));
+    RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],11,Py_ULL(0x550c7dc3d5ffb4e2));
+    RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],12,Py_ULL(0x72be5d74f27b896f));
+    RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],13,Py_ULL(0x80deb1fe3b1696b1));
+    RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],14,Py_ULL(0x9bdc06a725c71235));
+    RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],15,Py_ULL(0xc19bf174cf692694));
+    RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],16,Py_ULL(0xe49b69c19ef14ad2));
+    RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],17,Py_ULL(0xefbe4786384f25e3));
+    RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],18,Py_ULL(0x0fc19dc68b8cd5b5));
+    RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],19,Py_ULL(0x240ca1cc77ac9c65));
+    RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],20,Py_ULL(0x2de92c6f592b0275));
+    RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],21,Py_ULL(0x4a7484aa6ea6e483));
+    RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],22,Py_ULL(0x5cb0a9dcbd41fbd4));
+    RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],23,Py_ULL(0x76f988da831153b5));
+    RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],24,Py_ULL(0x983e5152ee66dfab));
+    RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],25,Py_ULL(0xa831c66d2db43210));
+    RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],26,Py_ULL(0xb00327c898fb213f));
+    RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],27,Py_ULL(0xbf597fc7beef0ee4));
+    RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],28,Py_ULL(0xc6e00bf33da88fc2));
+    RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],29,Py_ULL(0xd5a79147930aa725));
+    RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],30,Py_ULL(0x06ca6351e003826f));
+    RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],31,Py_ULL(0x142929670a0e6e70));
+    RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],32,Py_ULL(0x27b70a8546d22ffc));
+    RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],33,Py_ULL(0x2e1b21385c26c926));
+    RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],34,Py_ULL(0x4d2c6dfc5ac42aed));
+    RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],35,Py_ULL(0x53380d139d95b3df));
+    RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],36,Py_ULL(0x650a73548baf63de));
+    RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],37,Py_ULL(0x766a0abb3c77b2a8));
+    RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],38,Py_ULL(0x81c2c92e47edaee6));
+    RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],39,Py_ULL(0x92722c851482353b));
+    RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],40,Py_ULL(0xa2bfe8a14cf10364));
+    RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],41,Py_ULL(0xa81a664bbc423001));
+    RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],42,Py_ULL(0xc24b8b70d0f89791));
+    RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],43,Py_ULL(0xc76c51a30654be30));
+    RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],44,Py_ULL(0xd192e819d6ef5218));
+    RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],45,Py_ULL(0xd69906245565a910));
+    RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],46,Py_ULL(0xf40e35855771202a));
+    RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],47,Py_ULL(0x106aa07032bbd1b8));
+    RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],48,Py_ULL(0x19a4c116b8d2d0c8));
+    RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],49,Py_ULL(0x1e376c085141ab53));
+    RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],50,Py_ULL(0x2748774cdf8eeb99));
+    RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],51,Py_ULL(0x34b0bcb5e19b48a8));
+    RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],52,Py_ULL(0x391c0cb3c5c95a63));
+    RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],53,Py_ULL(0x4ed8aa4ae3418acb));
+    RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],54,Py_ULL(0x5b9cca4f7763e373));
+    RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],55,Py_ULL(0x682e6ff3d6b2b8a3));
+    RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],56,Py_ULL(0x748f82ee5defb2fc));
+    RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],57,Py_ULL(0x78a5636f43172f60));
+    RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],58,Py_ULL(0x84c87814a1f0ab72));
+    RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],59,Py_ULL(0x8cc702081a6439ec));
+    RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],60,Py_ULL(0x90befffa23631e28));
+    RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],61,Py_ULL(0xa4506cebde82bde9));
+    RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],62,Py_ULL(0xbef9a3f7b2c67915));
+    RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],63,Py_ULL(0xc67178f2e372532b));
+    RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],64,Py_ULL(0xca273eceea26619c));
+    RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],65,Py_ULL(0xd186b8c721c0c207));
+    RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],66,Py_ULL(0xeada7dd6cde0eb1e));
+    RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],67,Py_ULL(0xf57d4f7fee6ed178));
+    RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],68,Py_ULL(0x06f067aa72176fba));
+    RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],69,Py_ULL(0x0a637dc5a2c898a6));
+    RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],70,Py_ULL(0x113f9804bef90dae));
+    RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],71,Py_ULL(0x1b710b35131c471b));
+    RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],72,Py_ULL(0x28db77f523047d84));
+    RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],73,Py_ULL(0x32caab7b40c72493));
+    RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],74,Py_ULL(0x3c9ebe0a15c9bebc));
+    RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],75,Py_ULL(0x431d67c49c100d4c));
+    RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],76,Py_ULL(0x4cc5d4becb3e42b6));
+    RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],77,Py_ULL(0x597f299cfc657e2a));
+    RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],78,Py_ULL(0x5fcb6fab3ad6faec));
+    RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],79,Py_ULL(0x6c44198c4a475817));
 
 #undef RND     
     
@@ -254,14 +254,14 @@
 sha512_init(SHAobject *sha_info)
 {
     TestEndianness(sha_info->Endianness)
-    sha_info->digest[0] = 0x6a09e667f3bcc908ULL;
-    sha_info->digest[1] = 0xbb67ae8584caa73bULL;
-    sha_info->digest[2] = 0x3c6ef372fe94f82bULL;
-    sha_info->digest[3] = 0xa54ff53a5f1d36f1ULL;
-    sha_info->digest[4] = 0x510e527fade682d1ULL;
-    sha_info->digest[5] = 0x9b05688c2b3e6c1fULL;
-    sha_info->digest[6] = 0x1f83d9abfb41bd6bULL;
-    sha_info->digest[7] = 0x5be0cd19137e2179ULL;
+    sha_info->digest[0] = Py_ULL(0x6a09e667f3bcc908);
+    sha_info->digest[1] = Py_ULL(0xbb67ae8584caa73b);
+    sha_info->digest[2] = Py_ULL(0x3c6ef372fe94f82b);
+    sha_info->digest[3] = Py_ULL(0xa54ff53a5f1d36f1);
+    sha_info->digest[4] = Py_ULL(0x510e527fade682d1);
+    sha_info->digest[5] = Py_ULL(0x9b05688c2b3e6c1f);
+    sha_info->digest[6] = Py_ULL(0x1f83d9abfb41bd6b);
+    sha_info->digest[7] = Py_ULL(0x5be0cd19137e2179);
     sha_info->count_lo = 0L;
     sha_info->count_hi = 0L;
     sha_info->local = 0;
@@ -272,14 +272,14 @@
 sha384_init(SHAobject *sha_info)
 {
     TestEndianness(sha_info->Endianness)
-    sha_info->digest[0] = 0xcbbb9d5dc1059ed8ULL;
-    sha_info->digest[1] = 0x629a292a367cd507ULL;
-    sha_info->digest[2] = 0x9159015a3070dd17ULL;
-    sha_info->digest[3] = 0x152fecd8f70e5939ULL;
-    sha_info->digest[4] = 0x67332667ffc00b31ULL;
-    sha_info->digest[5] = 0x8eb44a8768581511ULL;
-    sha_info->digest[6] = 0xdb0c2e0d64f98fa7ULL;
-    sha_info->digest[7] = 0x47b5481dbefa4fa4ULL;
+    sha_info->digest[0] = Py_ULL(0xcbbb9d5dc1059ed8);
+    sha_info->digest[1] = Py_ULL(0x629a292a367cd507);
+    sha_info->digest[2] = Py_ULL(0x9159015a3070dd17);
+    sha_info->digest[3] = Py_ULL(0x152fecd8f70e5939);
+    sha_info->digest[4] = Py_ULL(0x67332667ffc00b31);
+    sha_info->digest[5] = Py_ULL(0x8eb44a8768581511);
+    sha_info->digest[6] = Py_ULL(0xdb0c2e0d64f98fa7);
+    sha_info->digest[7] = Py_ULL(0x47b5481dbefa4fa4);
     sha_info->count_lo = 0L;
     sha_info->count_hi = 0L;
     sha_info->local = 0;
diff --git a/Modules/socketmodule.c b/Modules/socketmodule.c
index c9dd4a3..164a5d1 100644
--- a/Modules/socketmodule.c
+++ b/Modules/socketmodule.c
@@ -61,6 +61,15 @@
 
 */
 
+#ifdef __APPLE__
+  /*
+   * inet_aton is not available on OSX 10.3, yet we want to use a binary
+   * that was build on 10.4 or later to work on that release, weak linking
+   * comes to the rescue.
+   */
+# pragma weak inet_aton
+#endif
+
 #include "Python.h"
 #include "structmember.h"
 
@@ -95,7 +104,10 @@
 listen(n) -- start listening for incoming connections\n\
 makefile([mode, [bufsize]]) -- return a file object for the socket [*]\n\
 recv(buflen[, flags]) -- receive data\n\
-recvfrom(buflen[, flags]) -- receive data and sender's address\n\
+recv_buf(buffer[, nbytes[, flags]]) -- receive data (into a buffer)\n\
+recvfrom(buflen[, flags]) -- receive data and sender\'s address\n\
+recvfrom_buf(buffer[, nbytes, [, flags])\n\
+  -- receive data and sender\'s address (into a buffer)\n\
 sendall(data[, flags]) -- send all data\n\
 send(data[, flags]) -- send data, may not send all of it\n\
 sendto(data[, flags], addr) -- send data to a given address\n\
@@ -196,7 +208,7 @@
    functions are declared correctly if compiling with
    MIPSPro 7.x in ANSI C mode (default) */
 
-/* XXX Using _SGIAPI is the wrong thing, 
+/* XXX Using _SGIAPI is the wrong thing,
    but I don't know what the right thing is. */
 #undef _SGIAPI /* to avoid warning */
 #define _SGIAPI 1
@@ -214,8 +226,8 @@
 #include <netdb.h>
 #endif
 
-/* Irix 6.5 fails to define this variable at all. This is needed 
-   for both GCC and SGI's compiler. I'd say that the SGI headers 
+/* Irix 6.5 fails to define this variable at all. This is needed
+   for both GCC and SGI's compiler. I'd say that the SGI headers
    are just busted. Same thing for Solaris. */
 #if (defined(__sgi) || defined(sun)) && !defined(INET_ADDRSTRLEN)
 #define INET_ADDRSTRLEN 16
@@ -306,6 +318,11 @@
    older releases don't have */
 #undef HAVE_GETADDRINFO
 #endif
+
+#ifdef HAVE_INET_ATON
+#define USE_INET_ATON_WEAKLINK
+#endif
+
 #endif
 
 /* I know this is a bad practice, but it is the easiest... */
@@ -1180,10 +1197,10 @@
 				args->ob_type->tp_name);
 			return 0;
 		}
-		if (!PyArg_ParseTuple(args, "eti:getsockaddrarg", 
+		if (!PyArg_ParseTuple(args, "eti:getsockaddrarg",
 				      "idna", &host, &port))
 			return 0;
-                result = setipaddr(host, (struct sockaddr *)addr, 
+                result = setipaddr(host, (struct sockaddr *)addr,
                                    sizeof(*addr),  AF_INET);
                 PyMem_Free(host);
                 if (result < 0)
@@ -1203,12 +1220,20 @@
 		int port, flowinfo, scope_id, result;
  		addr = (struct sockaddr_in6*)&(s->sock_addr).in6;
 		flowinfo = scope_id = 0;
-		if (!PyArg_ParseTuple(args, "eti|ii", 
+		if (!PyTuple_Check(args)) {
+			PyErr_Format(
+				PyExc_TypeError,
+				"getsockaddrarg: "
+				"AF_INET6 address must be tuple, not %.500s",
+				args->ob_type->tp_name);
+			return 0;
+		}
+		if (!PyArg_ParseTuple(args, "eti|ii",
 				      "idna", &host, &port, &flowinfo,
 				      &scope_id)) {
 			return 0;
 		}
-                result = setipaddr(host, (struct sockaddr *)addr,  
+                result = setipaddr(host, (struct sockaddr *)addr,
                                    sizeof(*addr), AF_INET6);
                 PyMem_Free(host);
                 if (result < 0)
@@ -1305,6 +1330,14 @@
 		char *haddr = NULL;
 		unsigned int halen = 0;
 
+		if (!PyTuple_Check(args)) {
+			PyErr_Format(
+				PyExc_TypeError,
+				"getsockaddrarg: "
+				"AF_PACKET address must be tuple, not %.500s",
+				args->ob_type->tp_name);
+			return 0;
+		}
 		if (!PyArg_ParseTuple(args, "si|iis#", &interfaceName,
 				      &protoNumber, &pkttype, &hatype,
 				      &haddr, &halen))
@@ -1809,7 +1842,7 @@
 					int res_size = sizeof res;
 					/* It must be in the exception set */
 					assert(FD_ISSET(s->sock_fd, &fds_exc));
-					if (0 == getsockopt(s->sock_fd, SOL_SOCKET, SO_ERROR, 
+					if (0 == getsockopt(s->sock_fd, SOL_SOCKET, SO_ERROR,
 					                    (char *)&res, &res_size))
 						/* getsockopt also clears WSAGetLastError,
 						   so reset it back. */
@@ -2105,95 +2138,126 @@
 
 #endif /* NO_DUP */
 
+/*
+ * This is the guts of the recv() and recv_buf() methods, which reads into a
+ * char buffer.  If you have any inc/def ref to do to the objects that contain
+ * the buffer, do it in the caller.  This function returns the number of bytes
+ * succesfully read.  If there was an error, it returns -1.  Note that it is
+ * also possible that we return a number of bytes smaller than the request
+ * bytes.
+ */
+static int
+sock_recv_guts(PySocketSockObject *s, char* cbuf, int len, int flags)
+{
+        int timeout, outlen = 0;
+#ifdef __VMS
+	int remaining, nread;
+	char *read_buf;
+#endif
+
+	if (!IS_SELECTABLE(s)) {
+		select_error();
+		return -1;
+	}
+
+#ifndef __VMS
+	Py_BEGIN_ALLOW_THREADS
+	timeout = internal_select(s, 0);
+	if (!timeout)
+		outlen = recv(s->sock_fd, cbuf, len, flags);
+	Py_END_ALLOW_THREADS
+
+	if (timeout) {
+		PyErr_SetString(socket_timeout, "timed out");
+		return -1;
+	}
+	if (outlen < 0) {
+		/* Note: the call to errorhandler() ALWAYS indirectly returned
+		   NULL, so ignore its return value */
+		s->errorhandler();
+		return -1;
+	}
+#else
+	read_buf = cbuf;
+	remaining = len;
+	while (remaining != 0) {
+		unsigned int segment;
+
+		segment = remaining /SEGMENT_SIZE;
+		if (segment != 0) {
+			segment = SEGMENT_SIZE;
+		}
+		else {
+			segment = remaining;
+		}
+
+		Py_BEGIN_ALLOW_THREADS
+		timeout = internal_select(s, 0);
+		if (!timeout)
+			nread = recv(s->sock_fd, read_buf, segment, flags);
+		Py_END_ALLOW_THREADS
+
+		if (timeout) {
+			PyErr_SetString(socket_timeout, "timed out");
+			return -1;
+		}
+		if (nread < 0) {
+			s->errorhandler();
+			return -1;
+		}
+		if (nread != remaining) {
+			read_buf += nread;
+			break;
+		}
+
+		remaining -= segment;
+		read_buf += segment;
+	}
+	outlen = read_buf - cbuf;
+#endif /* !__VMS */
+
+	return outlen;
+}
+
 
 /* s.recv(nbytes [,flags]) method */
 
 static PyObject *
 sock_recv(PySocketSockObject *s, PyObject *args)
 {
-	int len, n = 0, flags = 0, timeout;
+	int recvlen, flags = 0, outlen;
 	PyObject *buf;
-#ifdef __VMS
-	int read_length;
-	char *read_buf;
-#endif
 
-	if (!PyArg_ParseTuple(args, "i|i:recv", &len, &flags))
+	if (!PyArg_ParseTuple(args, "i|i:recv", &recvlen, &flags))
 		return NULL;
 
-	if (len < 0) {
+	if (recvlen < 0) {
 		PyErr_SetString(PyExc_ValueError,
 				"negative buffersize in recv");
 		return NULL;
 	}
 
-	buf = PyString_FromStringAndSize((char *) 0, len);
+	/* Allocate a new string. */
+	buf = PyString_FromStringAndSize((char *) 0, recvlen);
 	if (buf == NULL)
 		return NULL;
 
-	if (!IS_SELECTABLE(s))
-		return select_error();
-
-#ifndef __VMS
-	Py_BEGIN_ALLOW_THREADS
-	timeout = internal_select(s, 0);
-	if (!timeout)
-		n = recv(s->sock_fd, PyString_AS_STRING(buf), len, flags);
-	Py_END_ALLOW_THREADS
-
-	if (timeout) {
+	/* Call the guts */
+	outlen = sock_recv_guts(s, PyString_AsString(buf), recvlen, flags);
+	if (outlen < 0) {
+		/* An error occured, release the string and return an
+		   error. */
 		Py_DECREF(buf);
-		PyErr_SetString(socket_timeout, "timed out");
 		return NULL;
 	}
-	if (n < 0) {
-		Py_DECREF(buf);
-		return s->errorhandler();
-	}
-	if (n != len)
-		_PyString_Resize(&buf, n);
-#else
-	read_buf = PyString_AsString(buf);
-	read_length = len;
-	while (read_length != 0) {
-		unsigned int segment;
-
-		segment = read_length /SEGMENT_SIZE;
-		if (segment != 0) {
-			segment = SEGMENT_SIZE;
-		}
-		else {
-			segment = read_length;
-		}
-
-		Py_BEGIN_ALLOW_THREADS
-		timeout = internal_select(s, 0);
-		if (!timeout)
-			n = recv(s->sock_fd, read_buf, segment, flags);
-		Py_END_ALLOW_THREADS
-
-		if (timeout) {
-			Py_DECREF(buf);
-			PyErr_SetString(socket_timeout, "timed out");
+	if (outlen != recvlen) {
+		/* We did not read as many bytes as we anticipated, resize the
+		   string if possible and be succesful. */
+		if (_PyString_Resize(&buf, outlen) < 0)
+			/* Oopsy, not so succesful after all. */
 			return NULL;
-		}
-		if (n < 0) {
-			Py_DECREF(buf);
-			return s->errorhandler();
-		}
-		if (n != read_length) {
-			read_buf += n;
-			break;
-		}
+	}
 
-		read_length -= segment;
-		read_buf += segment;
-	}
-	if (_PyString_Resize(&buf, (read_buf - PyString_AsString(buf))) < 0)
-	{
-	    return NULL;
-	}
-#endif /* !__VMS */
 	return buf;
 }
 
@@ -2206,29 +2270,90 @@
 the remote end is closed and all data is read, return the empty string.");
 
 
-/* s.recvfrom(nbytes [,flags]) method */
+/* s.recv_buf(buffer, [nbytes [,flags]]) method */
 
-static PyObject *
-sock_recvfrom(PySocketSockObject *s, PyObject *args)
+static PyObject*
+sock_recv_buf(PySocketSockObject *s, PyObject *args, PyObject *kwds)
+{
+	static char *kwlist[] = {"buffer", "nbytes", "flags", 0};
+
+	int recvlen = 0, flags = 0, readlen;
+	char *buf;
+	int buflen;
+
+	/* Get the buffer's memory */
+	if (!PyArg_ParseTupleAndKeywords(args, kwds, "s#|ii:recv", kwlist,
+					 &buf, &buflen, &recvlen, &flags))
+		return NULL;
+	assert(buf != 0 && buflen > 0);
+
+	if (recvlen < 0) {
+		PyErr_SetString(PyExc_ValueError,
+				"negative buffersize in recv");
+		return NULL;
+	}
+	if (recvlen == 0) {
+            /* If nbytes was not specified, use the buffer's length */
+            recvlen = buflen;
+	}
+
+	/* Check if the buffer is large enough */
+	if (buflen < recvlen) {
+		PyErr_SetString(PyExc_ValueError,
+				"buffer too small for requested bytes");
+		return NULL;
+	}
+
+	/* Call the guts */
+	readlen = sock_recv_guts(s, buf, recvlen, flags);
+	if (readlen < 0) {
+		/* Return an error. */
+		return NULL;
+	}
+
+	/* Return the number of bytes read.  Note that we do not do anything
+	   special here in the case that readlen < recvlen. */
+	return PyInt_FromLong(readlen);
+}
+
+PyDoc_STRVAR(recv_buf_doc,
+"recv_buf(buffer, [nbytes[, flags]]) -> nbytes_read\n\
+\n\
+A version of recv() that stores its data into a buffer rather than creating \n\
+a new string.  Receive up to buffersize bytes from the socket.  If buffersize \n\
+is not specified (or 0), receive up to the size available in the given buffer.\n\
+\n\
+See recv() for documentation about the flags.");
+
+
+/*
+ * This is the guts of the recv() and recv_buf() methods, which reads into a
+ * char buffer.  If you have any inc/def ref to do to the objects that contain
+ * the buffer, do it in the caller.  This function returns the number of bytes
+ * succesfully read.  If there was an error, it returns -1.  Note that it is
+ * also possible that we return a number of bytes smaller than the request
+ * bytes.
+ *
+ * 'addr' is a return value for the address object.  Note that you must decref
+ * it yourself.
+ */
+static int
+sock_recvfrom_guts(PySocketSockObject *s, char* cbuf, int len, int flags,
+		   PyObject** addr)
 {
 	sock_addr_t addrbuf;
-	PyObject *buf = NULL;
-	PyObject *addr = NULL;
-	PyObject *ret = NULL;
-	int len, n = 0, flags = 0, timeout;
+	int n = 0, timeout;
 	socklen_t addrlen;
 
-	if (!PyArg_ParseTuple(args, "i|i:recvfrom", &len, &flags))
-		return NULL;
+	*addr = NULL;
 
 	if (!getsockaddrlen(s, &addrlen))
-		return NULL;
-	buf = PyString_FromStringAndSize((char *) 0, len);
-	if (buf == NULL)
-		return NULL;
+		return -1;
 
-	if (!IS_SELECTABLE(s))
-		return select_error();
+	if (!IS_SELECTABLE(s)) {
+		select_error();
+		return -1;
+	}
 
 	Py_BEGIN_ALLOW_THREADS
 	memset(&addrbuf, 0, addrlen);
@@ -2236,41 +2361,71 @@
 	if (!timeout) {
 #ifndef MS_WINDOWS
 #if defined(PYOS_OS2) && !defined(PYCC_GCC)
-		n = recvfrom(s->sock_fd, PyString_AS_STRING(buf), len, flags,
+		n = recvfrom(s->sock_fd, cbuf, len, flags,
 			     (struct sockaddr *) &addrbuf, &addrlen);
 #else
-		n = recvfrom(s->sock_fd, PyString_AS_STRING(buf), len, flags,
+		n = recvfrom(s->sock_fd, cbuf, len, flags,
 			     (void *) &addrbuf, &addrlen);
 #endif
 #else
-		n = recvfrom(s->sock_fd, PyString_AS_STRING(buf), len, flags,
+		n = recvfrom(s->sock_fd, cbuf, len, flags,
 			     (struct sockaddr *) &addrbuf, &addrlen);
 #endif
 	}
 	Py_END_ALLOW_THREADS
 
 	if (timeout) {
-		Py_DECREF(buf);
 		PyErr_SetString(socket_timeout, "timed out");
-		return NULL;
+		return -1;
 	}
 	if (n < 0) {
-		Py_DECREF(buf);
-		return s->errorhandler();
+		s->errorhandler();
+                return -1;
 	}
 
-	if (n != len && _PyString_Resize(&buf, n) < 0)
+	if (!(*addr = makesockaddr(s->sock_fd, (struct sockaddr *) &addrbuf,
+				   addrlen, s->sock_proto)))
+		return -1;
+
+	return n;
+}
+
+/* s.recvfrom(nbytes [,flags]) method */
+
+static PyObject *
+sock_recvfrom(PySocketSockObject *s, PyObject *args)
+{
+	PyObject *buf = NULL;
+	PyObject *addr = NULL;
+	PyObject *ret = NULL;
+	int recvlen, outlen, flags = 0;
+
+	if (!PyArg_ParseTuple(args, "i|i:recvfrom", &recvlen, &flags))
 		return NULL;
 
-	if (!(addr = makesockaddr(s->sock_fd, (struct sockaddr *) &addrbuf,
-				  addrlen, s->sock_proto)))
+	buf = PyString_FromStringAndSize((char *) 0, recvlen);
+	if (buf == NULL)
+		return NULL;
+
+	outlen = sock_recvfrom_guts(s, PyString_AS_STRING(buf),
+				    recvlen, flags, &addr);
+	if (outlen < 0) {
 		goto finally;
+	}
+
+	if (outlen != recvlen) {
+		/* We did not read as many bytes as we anticipated, resize the
+		   string if possible and be succesful. */
+		if (_PyString_Resize(&buf, outlen) < 0)
+			/* Oopsy, not so succesful after all. */
+			goto finally;
+	}
 
 	ret = PyTuple_Pack(2, buf, addr);
 
 finally:
-	Py_XDECREF(addr);
 	Py_XDECREF(buf);
+	Py_XDECREF(addr);
 	return ret;
 }
 
@@ -2279,6 +2434,57 @@
 \n\
 Like recv(buffersize, flags) but also return the sender's address info.");
 
+
+/* s.recvfrom_buf(buffer[, nbytes [,flags]]) method */
+
+static PyObject *
+sock_recvfrom_buf(PySocketSockObject *s, PyObject *args, PyObject* kwds)
+{
+	static char *kwlist[] = {"buffer", "nbytes", "flags", 0};
+
+	int recvlen = 0, flags = 0, readlen;
+	char *buf;
+	int buflen;
+
+	PyObject *addr = NULL;
+	PyObject *ret = NULL;
+
+	if (!PyArg_ParseTupleAndKeywords(args, kwds, "s#|ii:recvfrom", kwlist,
+					 &buf, &buflen, &recvlen, &flags))
+		return NULL;
+	assert(buf != 0 && buflen > 0);
+
+	if (recvlen < 0) {
+		PyErr_SetString(PyExc_ValueError,
+				"negative buffersize in recv");
+		return NULL;
+	}
+	if (recvlen == 0) {
+            /* If nbytes was not specified, use the buffer's length */
+            recvlen = buflen;
+	}
+
+	readlen = sock_recvfrom_guts(s, buf, recvlen, flags, &addr);
+	if (readlen < 0) {
+		/* Return an error */
+		goto finally;
+	}
+
+	/* Return the number of bytes read and the address.  Note that we do
+	   not do anything special here in the case that readlen < recvlen. */
+	ret = PyTuple_Pack(2, PyInt_FromLong(readlen), addr);
+	
+finally:
+	Py_XDECREF(addr);
+	return ret;
+}
+
+PyDoc_STRVAR(recvfrom_buf_doc,
+"recvfrom_buf(buffer[, nbytes[, flags]]) -> (nbytes, address info)\n\
+\n\
+Like recv_buf(buffer[, nbytes[, flags]]) but also return the sender's address info.");
+
+
 /* s.send(data [,flags]) method */
 
 static PyObject *
@@ -2473,59 +2679,63 @@
 /* List of methods for socket objects */
 
 static PyMethodDef sock_methods[] = {
-	{"accept",	(PyCFunction)sock_accept, METH_NOARGS,
-			accept_doc},
-	{"bind",	(PyCFunction)sock_bind, METH_O,
-			bind_doc},
-	{"close",	(PyCFunction)sock_close, METH_NOARGS,
-			close_doc},
-	{"connect",	(PyCFunction)sock_connect, METH_O,
-			connect_doc},
-	{"connect_ex",	(PyCFunction)sock_connect_ex, METH_O,
-			connect_ex_doc},
+	{"accept",	  (PyCFunction)sock_accept, METH_NOARGS,
+			  accept_doc},
+	{"bind",	  (PyCFunction)sock_bind, METH_O,
+			  bind_doc},
+	{"close",	  (PyCFunction)sock_close, METH_NOARGS,
+			  close_doc},
+	{"connect",	  (PyCFunction)sock_connect, METH_O,
+			  connect_doc},
+	{"connect_ex",	  (PyCFunction)sock_connect_ex, METH_O,
+			  connect_ex_doc},
 #ifndef NO_DUP
-	{"dup",		(PyCFunction)sock_dup, METH_NOARGS,
-			dup_doc},
+	{"dup",		  (PyCFunction)sock_dup, METH_NOARGS,
+			  dup_doc},
 #endif
-	{"fileno",	(PyCFunction)sock_fileno, METH_NOARGS,
-			fileno_doc},
+	{"fileno",	  (PyCFunction)sock_fileno, METH_NOARGS,
+			  fileno_doc},
 #ifdef HAVE_GETPEERNAME
-	{"getpeername",	(PyCFunction)sock_getpeername,
-			METH_NOARGS, getpeername_doc},
+	{"getpeername",	  (PyCFunction)sock_getpeername,
+			  METH_NOARGS, getpeername_doc},
 #endif
-	{"getsockname",	(PyCFunction)sock_getsockname,
-			METH_NOARGS, getsockname_doc},
-	{"getsockopt",	(PyCFunction)sock_getsockopt, METH_VARARGS,
-			getsockopt_doc},
-	{"listen",	(PyCFunction)sock_listen, METH_O,
-			listen_doc},
+	{"getsockname",	  (PyCFunction)sock_getsockname,
+			  METH_NOARGS, getsockname_doc},
+	{"getsockopt",	  (PyCFunction)sock_getsockopt, METH_VARARGS,
+			  getsockopt_doc},
+	{"listen",	  (PyCFunction)sock_listen, METH_O,
+			  listen_doc},
 #ifndef NO_DUP
-	{"makefile",	(PyCFunction)sock_makefile, METH_VARARGS,
-			makefile_doc},
+	{"makefile",	  (PyCFunction)sock_makefile, METH_VARARGS,
+			  makefile_doc},
 #endif
-	{"recv",	(PyCFunction)sock_recv, METH_VARARGS,
-			recv_doc},
-	{"recvfrom",	(PyCFunction)sock_recvfrom, METH_VARARGS,
-			recvfrom_doc},
-	{"send",	(PyCFunction)sock_send, METH_VARARGS,
-			send_doc},
-	{"sendall",	(PyCFunction)sock_sendall, METH_VARARGS,
-			sendall_doc},
-	{"sendto",	(PyCFunction)sock_sendto, METH_VARARGS,
-			sendto_doc},
-	{"setblocking",	(PyCFunction)sock_setblocking, METH_O,
-			setblocking_doc},
-	{"settimeout", (PyCFunction)sock_settimeout, METH_O,
-			settimeout_doc},
-	{"gettimeout", (PyCFunction)sock_gettimeout, METH_NOARGS,
-			gettimeout_doc},
-	{"setsockopt",	(PyCFunction)sock_setsockopt, METH_VARARGS,
-			setsockopt_doc},
-	{"shutdown",	(PyCFunction)sock_shutdown, METH_O,
-			shutdown_doc},
+	{"recv",	  (PyCFunction)sock_recv, METH_VARARGS,
+			  recv_doc},
+	{"recv_buf",	  (PyCFunction)sock_recv_buf, METH_VARARGS | METH_KEYWORDS,
+			  recv_buf_doc},
+	{"recvfrom",	  (PyCFunction)sock_recvfrom, METH_VARARGS,
+			  recvfrom_doc},
+	{"recvfrom_buf",  (PyCFunction)sock_recvfrom_buf, METH_VARARGS | METH_KEYWORDS,
+			  recvfrom_buf_doc},
+	{"send",	  (PyCFunction)sock_send, METH_VARARGS,
+			  send_doc},
+	{"sendall",	  (PyCFunction)sock_sendall, METH_VARARGS,
+			  sendall_doc},
+	{"sendto",	  (PyCFunction)sock_sendto, METH_VARARGS,
+			  sendto_doc},
+	{"setblocking",	  (PyCFunction)sock_setblocking, METH_O,
+			  setblocking_doc},
+	{"settimeout",    (PyCFunction)sock_settimeout, METH_O,
+			  settimeout_doc},
+	{"gettimeout",    (PyCFunction)sock_gettimeout, METH_NOARGS,
+			  gettimeout_doc},
+	{"setsockopt",	  (PyCFunction)sock_setsockopt, METH_VARARGS,
+			  setsockopt_doc},
+	{"shutdown",	  (PyCFunction)sock_shutdown, METH_O,
+			  shutdown_doc},
 #ifdef RISCOS
-	{"sleeptaskw",	(PyCFunction)sock_sleeptaskw, METH_O,
-	 		sleeptaskw_doc},
+	{"sleeptaskw",	  (PyCFunction)sock_sleeptaskw, METH_O,
+	 		  sleeptaskw_doc},
 #endif
 	{NULL,			NULL}		/* sentinel */
 };
@@ -3333,7 +3543,9 @@
 #endif
 #ifdef HAVE_INET_ATON
 	struct in_addr buf;
-#else
+#endif
+
+#if !defined(HAVE_INET_ATON) || defined(USE_INET_ATON_WEAKLINK)
 	/* Have to use inet_addr() instead */
 	unsigned long packed_addr;
 #endif
@@ -3344,6 +3556,10 @@
 
 
 #ifdef HAVE_INET_ATON
+
+#ifdef USE_INET_ATON_WEAKLINK
+    if (inet_aton != NULL) {
+#endif
 	if (inet_aton(ip_addr, &buf))
 		return PyString_FromStringAndSize((char *)(&buf),
 						  sizeof(buf));
@@ -3352,13 +3568,20 @@
 			"illegal IP address string passed to inet_aton");
 	return NULL;
 
-#else /* ! HAVE_INET_ATON */
+#ifdef USE_INET_ATON_WEAKLINK
+   } else {
+#endif
+
+#endif
+
+#if !defined(HAVE_INET_ATON) || defined(USE_INET_ATON_WEAKLINK)
+
 	/* special-case this address as inet_addr might return INADDR_NONE
 	 * for this */
 	if (strcmp(ip_addr, "255.255.255.255") == 0) {
 		packed_addr = 0xFFFFFFFF;
 	} else {
-	
+
 		packed_addr = inet_addr(ip_addr);
 
 		if (packed_addr == INADDR_NONE) {	/* invalid address */
@@ -3369,6 +3592,11 @@
 	}
 	return PyString_FromStringAndSize((char *) &packed_addr,
 					  sizeof(packed_addr));
+
+#ifdef USE_INET_ATON_WEAKLINK
+   }
+#endif
+
 #endif
 }
 
@@ -3428,7 +3656,7 @@
 				"can't use AF_INET6, IPv6 is disabled");
 		return NULL;
 	}
-#endif 
+#endif
 
 	retval = inet_pton(af, ip, packed);
 	if (retval < 0) {
@@ -3451,7 +3679,7 @@
 		return NULL;
 	}
 }
-	
+
 PyDoc_STRVAR(inet_ntop_doc,
 "inet_ntop(af, packed_ip) -> string formatted IP address\n\
 \n\
@@ -3469,7 +3697,7 @@
 #else
 	char ip[INET_ADDRSTRLEN + 1];
 #endif
-	
+
 	/* Guarantee NUL-termination for PyString_FromString() below */
 	memset((void *) &ip[0], '\0', sizeof(ip));
 
@@ -3547,7 +3775,7 @@
 	} else if (PyString_Check(hobj)) {
 		hptr = PyString_AsString(hobj);
 	} else {
-		PyErr_SetString(PyExc_TypeError, 
+		PyErr_SetString(PyExc_TypeError,
 				"getaddrinfo() argument 1 must be string or None");
 		return NULL;
 	}
diff --git a/Modules/structmodule.c b/Modules/structmodule.c
deleted file mode 100644
index 4713c0c..0000000
--- a/Modules/structmodule.c
+++ /dev/null
@@ -1,1293 +0,0 @@
-/* struct module -- pack values into and (out of) strings */
-
-/* New version supporting byte order, alignment and size options,
-   character strings, and unsigned numbers */
-
-#include "Python.h"
-#include <ctype.h>
-
-PyDoc_STRVAR(struct__doc__,
-"Functions to convert between Python values and C structs.\n\
-Python strings are used to hold the data representing the C struct\n\
-and also as format strings to describe the layout of data in the C struct.\n\
-\n\
-The optional first format char indicates byte order, size and alignment:\n\
- @: native order, size & alignment (default)\n\
- =: native order, std. size & alignment\n\
- <: little-endian, std. size & alignment\n\
- >: big-endian, std. size & alignment\n\
- !: same as >\n\
-\n\
-The remaining chars indicate types of args and must match exactly;\n\
-these can be preceded by a decimal repeat count:\n\
- x: pad byte (no data); c:char; b:signed byte; B:unsigned byte;\n\
- h:short; H:unsigned short; i:int; I:unsigned int;\n\
- l:long; L:unsigned long; f:float; d:double.\n\
-Special cases (preceding decimal count indicates length):\n\
- s:string (array of char); p: pascal string (with count byte).\n\
-Special case (only available in native format):\n\
- P:an integer type that is wide enough to hold a pointer.\n\
-Special case (not in native mode unless 'long long' in platform C):\n\
- q:long long; Q:unsigned long long\n\
-Whitespace between formats is ignored.\n\
-\n\
-The variable struct.error is an exception raised on errors.");
-
-
-/* Exception */
-
-static PyObject *StructError;
-
-
-/* Define various structs to figure out the alignments of types */
-
-
-typedef struct { char c; short x; } st_short;
-typedef struct { char c; int x; } st_int;
-typedef struct { char c; long x; } st_long;
-typedef struct { char c; float x; } st_float;
-typedef struct { char c; double x; } st_double;
-typedef struct { char c; void *x; } st_void_p;
-
-#define SHORT_ALIGN (sizeof(st_short) - sizeof(short))
-#define INT_ALIGN (sizeof(st_int) - sizeof(int))
-#define LONG_ALIGN (sizeof(st_long) - sizeof(long))
-#define FLOAT_ALIGN (sizeof(st_float) - sizeof(float))
-#define DOUBLE_ALIGN (sizeof(st_double) - sizeof(double))
-#define VOID_P_ALIGN (sizeof(st_void_p) - sizeof(void *))
-
-/* We can't support q and Q in native mode unless the compiler does;
-   in std mode, they're 8 bytes on all platforms. */
-#ifdef HAVE_LONG_LONG
-typedef struct { char c; PY_LONG_LONG x; } s_long_long;
-#define LONG_LONG_ALIGN (sizeof(s_long_long) - sizeof(PY_LONG_LONG))
-#endif
-
-#define STRINGIFY(x)    #x
-
-#ifdef __powerc
-#pragma options align=reset
-#endif
-
-/* Helper to get a PyLongObject by hook or by crook.  Caller should decref. */
-
-static PyObject *
-get_pylong(PyObject *v)
-{
-	PyNumberMethods *m;
-
-	assert(v != NULL);
-	if (PyInt_Check(v))
-		return PyLong_FromLong(PyInt_AS_LONG(v));
-	if (PyLong_Check(v)) {
-		Py_INCREF(v);
-		return v;
-	}
-	m = v->ob_type->tp_as_number;
-	if (m != NULL && m->nb_long != NULL) {
-		v = m->nb_long(v);
-		if (v == NULL)
-			return NULL;
-		if (PyLong_Check(v))
-			return v;
-		Py_DECREF(v);
-	}
-	PyErr_SetString(StructError,
-			"cannot convert argument to long");
-	return NULL;
-}
-
-/* Helper routine to get a Python integer and raise the appropriate error
-   if it isn't one */
-
-static int
-get_long(PyObject *v, long *p)
-{
-	long x = PyInt_AsLong(v);
-	if (x == -1 && PyErr_Occurred()) {
-		if (PyErr_ExceptionMatches(PyExc_TypeError))
-			PyErr_SetString(StructError,
-					"required argument is not an integer");
-		return -1;
-	}
-	*p = x;
-	return 0;
-}
-
-
-/* Same, but handling unsigned long */
-
-static int
-get_ulong(PyObject *v, unsigned long *p)
-{
-	if (PyLong_Check(v)) {
-		unsigned long x = PyLong_AsUnsignedLong(v);
-		if (x == (unsigned long)(-1) && PyErr_Occurred())
-			return -1;
-		*p = x;
-		return 0;
-	}
-	else {
-		return get_long(v, (long *)p);
-	}
-}
-
-#ifdef HAVE_LONG_LONG
-
-/* Same, but handling native long long. */
-
-static int
-get_longlong(PyObject *v, PY_LONG_LONG *p)
-{
-	PY_LONG_LONG x;
-
-	v = get_pylong(v);
-	if (v == NULL)
-		return -1;
-	assert(PyLong_Check(v));
-	x = PyLong_AsLongLong(v);
-	Py_DECREF(v);
-	if (x == (PY_LONG_LONG)-1 && PyErr_Occurred())
-		return -1;
-	*p = x;
-	return 0;
-}
-
-/* Same, but handling native unsigned long long. */
-
-static int
-get_ulonglong(PyObject *v, unsigned PY_LONG_LONG *p)
-{
-	unsigned PY_LONG_LONG x;
-
-	v = get_pylong(v);
-	if (v == NULL)
-		return -1;
-	assert(PyLong_Check(v));
-	x = PyLong_AsUnsignedLongLong(v);
-	Py_DECREF(v);
-	if (x == (unsigned PY_LONG_LONG)-1 && PyErr_Occurred())
-		return -1;
-	*p = x;
-	return 0;
-}
-
-#endif
-
-/* Floating point helpers */
-
-static PyObject *
-unpack_float(const char *p,  /* start of 4-byte string */
-             int le)	     /* true for little-endian, false for big-endian */
-{
-	double x;
-
-	x = _PyFloat_Unpack4((unsigned char *)p, le);
-	if (x == -1.0 && PyErr_Occurred())
-		return NULL;
-	return PyFloat_FromDouble(x);
-}
-
-static PyObject *
-unpack_double(const char *p,  /* start of 8-byte string */
-              int le)         /* true for little-endian, false for big-endian */
-{
-	double x;
-
-	x = _PyFloat_Unpack8((unsigned char *)p, le);
-	if (x == -1.0 && PyErr_Occurred())
-		return NULL;
-	return PyFloat_FromDouble(x);
-}
-
-
-/* The translation function for each format character is table driven */
-
-typedef struct _formatdef {
-	char format;
-	int size;
-	int alignment;
-	PyObject* (*unpack)(const char *,
-			    const struct _formatdef *);
-	int (*pack)(char *, PyObject *,
-		    const struct _formatdef *);
-} formatdef;
-
-/* A large number of small routines follow, with names of the form
-
-	[bln][up]_TYPE
-
-   [bln] distiguishes among big-endian, little-endian and native.
-   [pu] distiguishes between pack (to struct) and unpack (from struct).
-   TYPE is one of char, byte, ubyte, etc.
-*/
-
-/* Native mode routines. ****************************************************/
-/* NOTE:
-   In all n[up]_<type> routines handling types larger than 1 byte, there is
-   *no* guarantee that the p pointer is properly aligned for each type,
-   therefore memcpy is called.  An intermediate variable is used to
-   compensate for big-endian architectures.
-   Normally both the intermediate variable and the memcpy call will be
-   skipped by C optimisation in little-endian architectures (gcc >= 2.91
-   does this). */
-
-static PyObject *
-nu_char(const char *p, const formatdef *f)
-{
-	return PyString_FromStringAndSize(p, 1);
-}
-
-static PyObject *
-nu_byte(const char *p, const formatdef *f)
-{
-	return PyInt_FromLong((long) *(signed char *)p);
-}
-
-static PyObject *
-nu_ubyte(const char *p, const formatdef *f)
-{
-	return PyInt_FromLong((long) *(unsigned char *)p);
-}
-
-static PyObject *
-nu_short(const char *p, const formatdef *f)
-{
-	short x;
-	memcpy((char *)&x, p, sizeof x);
-	return PyInt_FromLong((long)x);
-}
-
-static PyObject *
-nu_ushort(const char *p, const formatdef *f)
-{
-	unsigned short x;
-	memcpy((char *)&x, p, sizeof x);
-	return PyInt_FromLong((long)x);
-}
-
-static PyObject *
-nu_int(const char *p, const formatdef *f)
-{
-	int x;
-	memcpy((char *)&x, p, sizeof x);
-	return PyInt_FromLong((long)x);
-}
-
-static PyObject *
-nu_uint(const char *p, const formatdef *f)
-{
-	unsigned int x;
-	memcpy((char *)&x, p, sizeof x);
-	return PyLong_FromUnsignedLong((unsigned long)x);
-}
-
-static PyObject *
-nu_long(const char *p, const formatdef *f)
-{
-	long x;
-	memcpy((char *)&x, p, sizeof x);
-	return PyInt_FromLong(x);
-}
-
-static PyObject *
-nu_ulong(const char *p, const formatdef *f)
-{
-	unsigned long x;
-	memcpy((char *)&x, p, sizeof x);
-	return PyLong_FromUnsignedLong(x);
-}
-
-/* Native mode doesn't support q or Q unless the platform C supports
-   long long (or, on Windows, __int64). */
-
-#ifdef HAVE_LONG_LONG
-
-static PyObject *
-nu_longlong(const char *p, const formatdef *f)
-{
-	PY_LONG_LONG x;
-	memcpy((char *)&x, p, sizeof x);
-	return PyLong_FromLongLong(x);
-}
-
-static PyObject *
-nu_ulonglong(const char *p, const formatdef *f)
-{
-	unsigned PY_LONG_LONG x;
-	memcpy((char *)&x, p, sizeof x);
-	return PyLong_FromUnsignedLongLong(x);
-}
-
-#endif
-
-static PyObject *
-nu_float(const char *p, const formatdef *f)
-{
-	float x;
-	memcpy((char *)&x, p, sizeof x);
-	return PyFloat_FromDouble((double)x);
-}
-
-static PyObject *
-nu_double(const char *p, const formatdef *f)
-{
-	double x;
-	memcpy((char *)&x, p, sizeof x);
-	return PyFloat_FromDouble(x);
-}
-
-static PyObject *
-nu_void_p(const char *p, const formatdef *f)
-{
-	void *x;
-	memcpy((char *)&x, p, sizeof x);
-	return PyLong_FromVoidPtr(x);
-}
-
-static int
-np_byte(char *p, PyObject *v, const formatdef *f)
-{
-	long x;
-	if (get_long(v, &x) < 0)
-		return -1;
-	if (x < -128 || x > 127){
-		PyErr_SetString(StructError,
-				"byte format requires -128<=number<=127");
-		return -1;
-	}
-	*p = (char)x;
-	return 0;
-}
-
-static int
-np_ubyte(char *p, PyObject *v, const formatdef *f)
-{
-	long x;
-	if (get_long(v, &x) < 0)
-		return -1;
-	if (x < 0 || x > 255){
-		PyErr_SetString(StructError,
-				"ubyte format requires 0<=number<=255");
-		return -1;
-	}
-	*p = (char)x;
-	return 0;
-}
-
-static int
-np_char(char *p, PyObject *v, const formatdef *f)
-{
-	if (!PyString_Check(v) || PyString_Size(v) != 1) {
-		PyErr_SetString(StructError,
-				"char format require string of length 1");
-		return -1;
-	}
-	*p = *PyString_AsString(v);
-	return 0;
-}
-
-static int
-np_short(char *p, PyObject *v, const formatdef *f)
-{
-	long x;
-	short y;
-	if (get_long(v, &x) < 0)
-		return -1;
-	if (x < SHRT_MIN || x > SHRT_MAX){
-		PyErr_SetString(StructError,
-				"short format requires " STRINGIFY(SHRT_MIN)
-				"<=number<=" STRINGIFY(SHRT_MAX));
-		return -1;
-	}
-	y = (short)x;
-	memcpy(p, (char *)&y, sizeof y);
-	return 0;
-}
-
-static int
-np_ushort(char *p, PyObject *v, const formatdef *f)
-{
-	long x;
-	unsigned short y;
-	if (get_long(v, &x) < 0)
-		return -1;
-	if (x < 0 || x > USHRT_MAX){
-		PyErr_SetString(StructError,
-				"short format requires 0<=number<=" STRINGIFY(USHRT_MAX));
-		return -1;
-	}
-	y = (unsigned short)x;
-	memcpy(p, (char *)&y, sizeof y);
-	return 0;
-}
-
-static int
-np_int(char *p, PyObject *v, const formatdef *f)
-{
-	long x;
-	int y;
-	if (get_long(v, &x) < 0)
-		return -1;
-	y = (int)x;
-	memcpy(p, (char *)&y, sizeof y);
-	return 0;
-}
-
-static int
-np_uint(char *p, PyObject *v, const formatdef *f)
-{
-	unsigned long x;
-	unsigned int y;
-	if (get_ulong(v, &x) < 0)
-		return -1;
-	y = (unsigned int)x;
-	memcpy(p, (char *)&y, sizeof y);
-	return 0;
-}
-
-static int
-np_long(char *p, PyObject *v, const formatdef *f)
-{
-	long x;
-	if (get_long(v, &x) < 0)
-		return -1;
-	memcpy(p, (char *)&x, sizeof x);
-	return 0;
-}
-
-static int
-np_ulong(char *p, PyObject *v, const formatdef *f)
-{
-	unsigned long x;
-	if (get_ulong(v, &x) < 0)
-		return -1;
-	memcpy(p, (char *)&x, sizeof x);
-	return 0;
-}
-
-#ifdef HAVE_LONG_LONG
-
-static int
-np_longlong(char *p, PyObject *v, const formatdef *f)
-{
-	PY_LONG_LONG x;
-	if (get_longlong(v, &x) < 0)
-		return -1;
-	memcpy(p, (char *)&x, sizeof x);
-	return 0;
-}
-
-static int
-np_ulonglong(char *p, PyObject *v, const formatdef *f)
-{
-	unsigned PY_LONG_LONG x;
-	if (get_ulonglong(v, &x) < 0)
-		return -1;
-	memcpy(p, (char *)&x, sizeof x);
-	return 0;
-}
-#endif
-
-static int
-np_float(char *p, PyObject *v, const formatdef *f)
-{
-	float x = (float)PyFloat_AsDouble(v);
-	if (x == -1 && PyErr_Occurred()) {
-		PyErr_SetString(StructError,
-				"required argument is not a float");
-		return -1;
-	}
-	memcpy(p, (char *)&x, sizeof x);
-	return 0;
-}
-
-static int
-np_double(char *p, PyObject *v, const formatdef *f)
-{
-	double x = PyFloat_AsDouble(v);
-	if (x == -1 && PyErr_Occurred()) {
-		PyErr_SetString(StructError,
-				"required argument is not a float");
-		return -1;
-	}
-	memcpy(p, (char *)&x, sizeof(double));
-	return 0;
-}
-
-static int
-np_void_p(char *p, PyObject *v, const formatdef *f)
-{
-	void *x;
-
-	v = get_pylong(v);
-	if (v == NULL)
-		return -1;
-	assert(PyLong_Check(v));
-	x = PyLong_AsVoidPtr(v);
-	Py_DECREF(v);
-	if (x == NULL && PyErr_Occurred())
-		return -1;
-	memcpy(p, (char *)&x, sizeof x);
-	return 0;
-}
-
-static formatdef native_table[] = {
-	{'x',	sizeof(char),	0,		NULL},
-	{'b',	sizeof(char),	0,		nu_byte,	np_byte},
-	{'B',	sizeof(char),	0,		nu_ubyte,	np_ubyte},
-	{'c',	sizeof(char),	0,		nu_char,	np_char},
-	{'s',	sizeof(char),	0,		NULL},
-	{'p',	sizeof(char),	0,		NULL},
-	{'h',	sizeof(short),	SHORT_ALIGN,	nu_short,	np_short},
-	{'H',	sizeof(short),	SHORT_ALIGN,	nu_ushort,	np_ushort},
-	{'i',	sizeof(int),	INT_ALIGN,	nu_int,		np_int},
-	{'I',	sizeof(int),	INT_ALIGN,	nu_uint,	np_uint},
-	{'l',	sizeof(long),	LONG_ALIGN,	nu_long,	np_long},
-	{'L',	sizeof(long),	LONG_ALIGN,	nu_ulong,	np_ulong},
-	{'f',	sizeof(float),	FLOAT_ALIGN,	nu_float,	np_float},
-	{'d',	sizeof(double),	DOUBLE_ALIGN,	nu_double,	np_double},
-	{'P',	sizeof(void *),	VOID_P_ALIGN,	nu_void_p,	np_void_p},
-#ifdef HAVE_LONG_LONG
-	{'q',	sizeof(PY_LONG_LONG), LONG_LONG_ALIGN, nu_longlong, np_longlong},
-	{'Q',	sizeof(PY_LONG_LONG), LONG_LONG_ALIGN, nu_ulonglong,np_ulonglong},
-#endif
-	{0}
-};
-
-/* Big-endian routines. *****************************************************/
-
-static PyObject *
-bu_int(const char *p, const formatdef *f)
-{
-	long x = 0;
-	int i = f->size;
-	do {
-		x = (x<<8) | (*p++ & 0xFF);
-	} while (--i > 0);
-	/* Extend the sign bit. */
-	if (SIZEOF_LONG > f->size)
-		x |= -(x & (1L << (8*f->size - 1)));
-	return PyInt_FromLong(x);
-}
-
-static PyObject *
-bu_uint(const char *p, const formatdef *f)
-{
-	unsigned long x = 0;
-	int i = f->size;
-	do {
-		x = (x<<8) | (*p++ & 0xFF);
-	} while (--i > 0);
-	if (f->size >= 4)
-		return PyLong_FromUnsignedLong(x);
-	else
-		return PyInt_FromLong((long)x);
-}
-
-static PyObject *
-bu_longlong(const char *p, const formatdef *f)
-{
-	return _PyLong_FromByteArray((const unsigned char *)p,
-				      8,
-				      0, /* little-endian */
-				      1  /* signed */);
-}
-
-static PyObject *
-bu_ulonglong(const char *p, const formatdef *f)
-{
-	return _PyLong_FromByteArray((const unsigned char *)p,
-				      8,
-				      0, /* little-endian */
-				      0  /* signed */);
-}
-
-static PyObject *
-bu_float(const char *p, const formatdef *f)
-{
-	return unpack_float(p, 0);
-}
-
-static PyObject *
-bu_double(const char *p, const formatdef *f)
-{
-	return unpack_double(p, 0);
-}
-
-static int
-bp_int(char *p, PyObject *v, const formatdef *f)
-{
-	long x;
-	int i;
-	if (get_long(v, &x) < 0)
-		return -1;
-	i = f->size;
-	do {
-		p[--i] = (char)x;
-		x >>= 8;
-	} while (i > 0);
-	return 0;
-}
-
-static int
-bp_uint(char *p, PyObject *v, const formatdef *f)
-{
-	unsigned long x;
-	int i;
-	if (get_ulong(v, &x) < 0)
-		return -1;
-	i = f->size;
-	do {
-		p[--i] = (char)x;
-		x >>= 8;
-	} while (i > 0);
-	return 0;
-}
-
-static int
-bp_longlong(char *p, PyObject *v, const formatdef *f)
-{
-	int res;
-	v = get_pylong(v);
-	if (v == NULL)
-		return -1;
-	res = _PyLong_AsByteArray((PyLongObject *)v,
-			   	  (unsigned char *)p,
-				  8,
-				  0, /* little_endian */
-				  1  /* signed */);
-	Py_DECREF(v);
-	return res;
-}
-
-static int
-bp_ulonglong(char *p, PyObject *v, const formatdef *f)
-{
-	int res;
-	v = get_pylong(v);
-	if (v == NULL)
-		return -1;
-	res = _PyLong_AsByteArray((PyLongObject *)v,
-			   	  (unsigned char *)p,
-				  8,
-				  0, /* little_endian */
-				  0  /* signed */);
-	Py_DECREF(v);
-	return res;
-}
-
-static int
-bp_float(char *p, PyObject *v, const formatdef *f)
-{
-	double x = PyFloat_AsDouble(v);
-	if (x == -1 && PyErr_Occurred()) {
-		PyErr_SetString(StructError,
-				"required argument is not a float");
-		return -1;
-	}
-	return _PyFloat_Pack4(x, (unsigned char *)p, 0);
-}
-
-static int
-bp_double(char *p, PyObject *v, const formatdef *f)
-{
-	double x = PyFloat_AsDouble(v);
-	if (x == -1 && PyErr_Occurred()) {
-		PyErr_SetString(StructError,
-				"required argument is not a float");
-		return -1;
-	}
-	return _PyFloat_Pack8(x, (unsigned char *)p, 0);
-}
-
-static formatdef bigendian_table[] = {
-	{'x',	1,		0,		NULL},
-	{'b',	1,		0,		bu_int,		bp_int},
-	{'B',	1,		0,		bu_uint,	bp_int},
-	{'c',	1,		0,		nu_char,	np_char},
-	{'s',	1,		0,		NULL},
-	{'p',	1,		0,		NULL},
-	{'h',	2,		0,		bu_int,		bp_int},
-	{'H',	2,		0,		bu_uint,	bp_uint},
-	{'i',	4,		0,		bu_int,		bp_int},
-	{'I',	4,		0,		bu_uint,	bp_uint},
-	{'l',	4,		0,		bu_int,		bp_int},
-	{'L',	4,		0,		bu_uint,	bp_uint},
-	{'q',	8,		0,		bu_longlong,	bp_longlong},
-	{'Q',	8,		0,		bu_ulonglong,	bp_ulonglong},
-	{'f',	4,		0,		bu_float,	bp_float},
-	{'d',	8,		0,		bu_double,	bp_double},
-	{0}
-};
-
-/* Little-endian routines. *****************************************************/
-
-static PyObject *
-lu_int(const char *p, const formatdef *f)
-{
-	long x = 0;
-	int i = f->size;
-	do {
-		x = (x<<8) | (p[--i] & 0xFF);
-	} while (i > 0);
-	/* Extend the sign bit. */
-	if (SIZEOF_LONG > f->size)
-		x |= -(x & (1L << (8*f->size - 1)));
-	return PyInt_FromLong(x);
-}
-
-static PyObject *
-lu_uint(const char *p, const formatdef *f)
-{
-	unsigned long x = 0;
-	int i = f->size;
-	do {
-		x = (x<<8) | (p[--i] & 0xFF);
-	} while (i > 0);
-	if (f->size >= 4)
-		return PyLong_FromUnsignedLong(x);
-	else
-		return PyInt_FromLong((long)x);
-}
-
-static PyObject *
-lu_longlong(const char *p, const formatdef *f)
-{
-	return _PyLong_FromByteArray((const unsigned char *)p,
-				      8,
-				      1, /* little-endian */
-				      1  /* signed */);
-}
-
-static PyObject *
-lu_ulonglong(const char *p, const formatdef *f)
-{
-	return _PyLong_FromByteArray((const unsigned char *)p,
-				      8,
-				      1, /* little-endian */
-				      0  /* signed */);
-}
-
-static PyObject *
-lu_float(const char *p, const formatdef *f)
-{
-	return unpack_float(p, 1);
-}
-
-static PyObject *
-lu_double(const char *p, const formatdef *f)
-{
-	return unpack_double(p, 1);
-}
-
-static int
-lp_int(char *p, PyObject *v, const formatdef *f)
-{
-	long x;
-	int i;
-	if (get_long(v, &x) < 0)
-		return -1;
-	i = f->size;
-	do {
-		*p++ = (char)x;
-		x >>= 8;
-	} while (--i > 0);
-	return 0;
-}
-
-static int
-lp_uint(char *p, PyObject *v, const formatdef *f)
-{
-	unsigned long x;
-	int i;
-	if (get_ulong(v, &x) < 0)
-		return -1;
-	i = f->size;
-	do {
-		*p++ = (char)x;
-		x >>= 8;
-	} while (--i > 0);
-	return 0;
-}
-
-static int
-lp_longlong(char *p, PyObject *v, const formatdef *f)
-{
-	int res;
-	v = get_pylong(v);
-	if (v == NULL)
-		return -1;
-	res = _PyLong_AsByteArray((PyLongObject*)v,
-			   	  (unsigned char *)p,
-				  8,
-				  1, /* little_endian */
-				  1  /* signed */);
-	Py_DECREF(v);
-	return res;
-}
-
-static int
-lp_ulonglong(char *p, PyObject *v, const formatdef *f)
-{
-	int res;
-	v = get_pylong(v);
-	if (v == NULL)
-		return -1;
-	res = _PyLong_AsByteArray((PyLongObject*)v,
-			   	  (unsigned char *)p,
-				  8,
-				  1, /* little_endian */
-				  0  /* signed */);
-	Py_DECREF(v);
-	return res;
-}
-
-static int
-lp_float(char *p, PyObject *v, const formatdef *f)
-{
-	double x = PyFloat_AsDouble(v);
-	if (x == -1 && PyErr_Occurred()) {
-		PyErr_SetString(StructError,
-				"required argument is not a float");
-		return -1;
-	}
-	return _PyFloat_Pack4(x, (unsigned char *)p, 1);
-}
-
-static int
-lp_double(char *p, PyObject *v, const formatdef *f)
-{
-	double x = PyFloat_AsDouble(v);
-	if (x == -1 && PyErr_Occurred()) {
-		PyErr_SetString(StructError,
-				"required argument is not a float");
-		return -1;
-	}
-	return _PyFloat_Pack8(x, (unsigned char *)p, 1);
-}
-
-static formatdef lilendian_table[] = {
-	{'x',	1,		0,		NULL},
-	{'b',	1,		0,		lu_int,		lp_int},
-	{'B',	1,		0,		lu_uint,	lp_int},
-	{'c',	1,		0,		nu_char,	np_char},
-	{'s',	1,		0,		NULL},
-	{'p',	1,		0,		NULL},
-	{'h',	2,		0,		lu_int,		lp_int},
-	{'H',	2,		0,		lu_uint,	lp_uint},
-	{'i',	4,		0,		lu_int,		lp_int},
-	{'I',	4,		0,		lu_uint,	lp_uint},
-	{'l',	4,		0,		lu_int,		lp_int},
-	{'L',	4,		0,		lu_uint,	lp_uint},
-	{'q',	8,		0,		lu_longlong,	lp_longlong},
-	{'Q',	8,		0,		lu_ulonglong,	lp_ulonglong},
-	{'f',	4,		0,		lu_float,	lp_float},
-	{'d',	8,		0,		lu_double,	lp_double},
-	{0}
-};
-
-
-static const formatdef *
-whichtable(char **pfmt)
-{
-	const char *fmt = (*pfmt)++; /* May be backed out of later */
-	switch (*fmt) {
-	case '<':
-		return lilendian_table;
-	case '>':
-	case '!': /* Network byte order is big-endian */
-		return bigendian_table;
-	case '=': { /* Host byte order -- different from native in aligment! */
-		int n = 1;
-		char *p = (char *) &n;
-		if (*p == 1)
-			return lilendian_table;
-		else
-			return bigendian_table;
-	}
-	default:
-		--*pfmt; /* Back out of pointer increment */
-		/* Fall through */
-	case '@':
-		return native_table;
-	}
-}
-
-
-/* Get the table entry for a format code */
-
-static const formatdef *
-getentry(int c, const formatdef *f)
-{
-	for (; f->format != '\0'; f++) {
-		if (f->format == c) {
-			return f;
-		}
-	}
-	PyErr_SetString(StructError, "bad char in struct format");
-	return NULL;
-}
-
-
-/* Align a size according to a format code */
-
-static int
-align(int size, int c, const formatdef *e)
-{
-	if (e->format == c) {
-		if (e->alignment) {
-			size = ((size + e->alignment - 1)
-				/ e->alignment)
-				* e->alignment;
-		}
-	}
-	return size;
-}
-
-
-/* calculate the size of a format string */
-
-static int
-calcsize(const char *fmt, const formatdef *f)
-{
-	const formatdef *e;
-	const char *s;
-	char c;
-	int size,  num, itemsize, x;
-
-	s = fmt;
-	size = 0;
-	while ((c = *s++) != '\0') {
-		if (isspace(Py_CHARMASK(c)))
-			continue;
-		if ('0' <= c && c <= '9') {
-			num = c - '0';
-			while ('0' <= (c = *s++) && c <= '9') {
-				x = num*10 + (c - '0');
-				if (x/10 != num) {
-					PyErr_SetString(
-						StructError,
-						"overflow in item count");
-					return -1;
-				}
-				num = x;
-			}
-			if (c == '\0')
-				break;
-		}
-		else
-			num = 1;
-
-		e = getentry(c, f);
-		if (e == NULL)
-			return -1;
-		itemsize = e->size;
-		size = align(size, c, e);
-		x = num * itemsize;
-		size += x;
-		if (x/itemsize != num || size < 0) {
-			PyErr_SetString(StructError,
-					"total struct size too long");
-			return -1;
-		}
-	}
-
-	return size;
-}
-
-
-PyDoc_STRVAR(calcsize__doc__,
-"calcsize(fmt) -> int\n\
-Return size of C struct described by format string fmt.\n\
-See struct.__doc__ for more on format strings.");
-
-static PyObject *
-struct_calcsize(PyObject *self, PyObject *args)
-{
-	char *fmt;
-	const formatdef *f;
-	int size;
-
-	if (!PyArg_ParseTuple(args, "s:calcsize", &fmt))
-		return NULL;
-	f = whichtable(&fmt);
-	size = calcsize(fmt, f);
-	if (size < 0)
-		return NULL;
-	return PyInt_FromLong((long)size);
-}
-
-
-PyDoc_STRVAR(pack__doc__,
-"pack(fmt, v1, v2, ...) -> string\n\
-Return string containing values v1, v2, ... packed according to fmt.\n\
-See struct.__doc__ for more on format strings.");
-
-static PyObject *
-struct_pack(PyObject *self, PyObject *args)
-{
-	const formatdef *f, *e;
-	PyObject *format, *result, *v;
-	char *fmt;
-	int size, num;
-	Py_ssize_t i, n;
-	char *s, *res, *restart, *nres;
-	char c;
-
-	if (args == NULL || !PyTuple_Check(args) ||
-	    (n = PyTuple_Size(args)) < 1)
-	{
-		PyErr_SetString(PyExc_TypeError,
-			"struct.pack requires at least one argument");
-		return NULL;
-	}
-	format = PyTuple_GetItem(args, 0);
-	fmt = PyString_AsString(format);
-	if (!fmt)
-		return NULL;
-	f = whichtable(&fmt);
-	size = calcsize(fmt, f);
-	if (size < 0)
-		return NULL;
-	result = PyString_FromStringAndSize((char *)NULL, size);
-	if (result == NULL)
-		return NULL;
-
-	s = fmt;
-	i = 1;
-	res = restart = PyString_AsString(result);
-
-	while ((c = *s++) != '\0') {
-		if (isspace(Py_CHARMASK(c)))
-			continue;
-		if ('0' <= c && c <= '9') {
-			num = c - '0';
-			while ('0' <= (c = *s++) && c <= '9')
-			       num = num*10 + (c - '0');
-			if (c == '\0')
-				break;
-		}
-		else
-			num = 1;
-
-		e = getentry(c, f);
-		if (e == NULL)
-			goto fail;
-		nres = restart + align((int)(res-restart), c, e);
-		/* Fill padd bytes with zeros */
-		while (res < nres)
-			*res++ = '\0';
-		if (num == 0 && c != 's')
-			continue;
-		do {
-			if (c == 'x') {
-				/* doesn't consume arguments */
-				memset(res, '\0', num);
-				res += num;
-				break;
-			}
-			if (i >= n) {
-				PyErr_SetString(StructError,
-					"insufficient arguments to pack");
-				goto fail;
-				}
-			v = PyTuple_GetItem(args, i++);
-			if (v == NULL)
-				goto fail;
-			if (c == 's') {
-				/* num is string size, not repeat count */
-				Py_ssize_t n;
-				if (!PyString_Check(v)) {
-					PyErr_SetString(StructError,
-					  "argument for 's' must be a string");
-					goto fail;
-				}
-				n = PyString_Size(v);
-				if (n > num)
-					n = num;
-				if (n > 0)
-					memcpy(res, PyString_AsString(v), n);
-				if (n < num)
-					memset(res+n, '\0', num-n);
-				res += num;
-				break;
-			}
-			else if (c == 'p') {
-				/* num is string size + 1,
-				   to fit in the count byte */
-				Py_ssize_t n;
-				num--; /* now num is max string size */
-				if (!PyString_Check(v)) {
-					PyErr_SetString(StructError,
-					  "argument for 'p' must be a string");
-					goto fail;
-				}
-				n = PyString_Size(v);
-				if (n > num)
-					n = num;
-				if (n > 0)
-					memcpy(res+1, PyString_AsString(v), n);
-				if (n < num)
-					/* no real need, just to be nice */
-					memset(res+1+n, '\0', num-n);
-				if (n > 255)
-					n = 255;
-				/* store the length byte */
-				*res++ = Py_SAFE_DOWNCAST(n, Py_ssize_t, unsigned char);
-				res += num;
-				break;
-			}
-			else {
-				if (e->pack(res, v, e) < 0)
-					goto fail;
-				res += e->size;
-			}
-		} while (--num > 0);
-	}
-
-	if (i < n) {
-		PyErr_SetString(StructError,
-				"too many arguments for pack format");
-		goto fail;
-	}
-
-	return result;
-
- fail:
-	Py_DECREF(result);
-	return NULL;
-}
-
-
-PyDoc_STRVAR(unpack__doc__,
-"unpack(fmt, string) -> (v1, v2, ...)\n\
-Unpack the string, containing packed C structure data, according\n\
-to fmt.  Requires len(string)==calcsize(fmt).\n\
-See struct.__doc__ for more on format strings.");
-
-static PyObject *
-struct_unpack(PyObject *self, PyObject *args)
-{
-	const formatdef *f, *e;
-	char *str, *start, *fmt, *s;
-	char c;
-	int len, size, num;
-	PyObject *res, *v;
-
-	if (!PyArg_ParseTuple(args, "ss#:unpack", &fmt, &start, &len))
-		return NULL;
-	f = whichtable(&fmt);
-	size = calcsize(fmt, f);
-	if (size < 0)
-		return NULL;
-	if (size != len) {
-		PyErr_SetString(StructError,
-				"unpack str size does not match format");
-		return NULL;
-	}
-	res = PyList_New(0);
-	if (res == NULL)
-		return NULL;
-	str = start;
-	s = fmt;
-	while ((c = *s++) != '\0') {
-		if (isspace(Py_CHARMASK(c)))
-			continue;
-		if ('0' <= c && c <= '9') {
-			num = c - '0';
-			while ('0' <= (c = *s++) && c <= '9')
-			       num = num*10 + (c - '0');
-			if (c == '\0')
-				break;
-		}
-		else
-			num = 1;
-
-		e = getentry(c, f);
-		if (e == NULL)
-			goto fail;
-		str = start + align((int)(str-start), c, e);
-		if (num == 0 && c != 's')
-			continue;
-
-		do {
-			if (c == 'x') {
-				str += num;
-				break;
-			}
-			if (c == 's') {
-				/* num is string size, not repeat count */
-				v = PyString_FromStringAndSize(str, num);
-				if (v == NULL)
-					goto fail;
-				str += num;
-				num = 0;
-			}
-			else if (c == 'p') {
-				/* num is string buffer size,
-				   not repeat count */
-				int n = *(unsigned char*)str;
-				/* first byte (unsigned) is string size */
-				if (n >= num)
-					n = num-1;
-				v = PyString_FromStringAndSize(str+1, n);
-				if (v == NULL)
-					goto fail;
-				str += num;
-				num = 0;
-			}
-			else {
-				v = e->unpack(str, e);
-				if (v == NULL)
-					goto fail;
-				str += e->size;
-			}
-			if (v == NULL || PyList_Append(res, v) < 0)
-				goto fail;
-			Py_DECREF(v);
-		} while (--num > 0);
-	}
-
-	v = PyList_AsTuple(res);
-	Py_DECREF(res);
-	return v;
-
- fail:
-	Py_DECREF(res);
-	return NULL;
-}
-
-
-/* List of functions */
-
-static PyMethodDef struct_methods[] = {
-	{"calcsize",	struct_calcsize,	METH_VARARGS, calcsize__doc__},
-	{"pack",	struct_pack,		METH_VARARGS, pack__doc__},
-	{"unpack",	struct_unpack,		METH_VARARGS, unpack__doc__},
-	{NULL,		NULL}		/* sentinel */
-};
-
-
-/* Module initialization */
-
-PyMODINIT_FUNC
-initstruct(void)
-{
-	PyObject *m;
-
-	/* Create the module and add the functions */
-	m = Py_InitModule4("struct", struct_methods, struct__doc__,
-			   (PyObject*)NULL, PYTHON_API_VERSION);
-	if (m == NULL)
-		return;
-
-	/* Add some symbolic constants to the module */
-	if (StructError == NULL) {
-		StructError = PyErr_NewException("struct.error", NULL, NULL);
-		if (StructError == NULL)
-			return;
-	}
-	Py_INCREF(StructError);
-	PyModule_AddObject(m, "error", StructError);
-}
diff --git a/Modules/threadmodule.c b/Modules/threadmodule.c
index 83313df..9ac9881 100644
--- a/Modules/threadmodule.c
+++ b/Modules/threadmodule.c
@@ -98,13 +98,6 @@
 \n\
 Return whether the lock is in the locked state.");
 
-static PyObject *
-lock_context(lockobject *self)
-{
-	Py_INCREF(self);
-	return (PyObject *)self;
-}
-
 static PyMethodDef lock_methods[] = {
 	{"acquire_lock", (PyCFunction)lock_PyThread_acquire_lock, 
 	 METH_VARARGS, acquire_doc},
@@ -118,8 +111,6 @@
 	 METH_NOARGS, locked_doc},
 	{"locked",       (PyCFunction)lock_locked_lock,  
 	 METH_NOARGS, locked_doc},
-	{"__context__",  (PyCFunction)lock_context,
-	 METH_NOARGS, PyDoc_STR("__context__() -> self.")},
 	{"__enter__",    (PyCFunction)lock_PyThread_acquire_lock,
 	 METH_VARARGS, acquire_doc},
 	{"__exit__",    (PyCFunction)lock_PyThread_release_lock,
diff --git a/Modules/timemodule.c b/Modules/timemodule.c
index 08d28a1..e03b7e1 100644
--- a/Modules/timemodule.c
+++ b/Modules/timemodule.c
@@ -5,6 +5,18 @@
 #include "structseq.h"
 #include "timefuncs.h"
 
+#ifdef __APPLE__
+#if defined(HAVE_GETTIMEOFDAY) && defined(HAVE_FTIME)
+  /*
+   * floattime falls back to ftime when getttimeofday fails because the latter
+   * might fail on some platforms. This fallback is unwanted on MacOSX because
+   * that makes it impossible to use a binary build on OSX 10.4 on earlier
+   * releases of the OS. Therefore claim we don't support ftime.
+   */
+# undef HAVE_FTIME
+#endif
+#endif
+
 #include <ctype.h>
 
 #include <sys/types.h>
@@ -51,11 +63,10 @@
 #endif /* MS_WINDOWS */
 #endif /* !__WATCOMC__ || __QNX__ */
 
-#if defined(MS_WINDOWS) && !defined(MS_WIN64) && !defined(__BORLANDC__)
-/* Win32 has better clock replacement
-   XXX Win64 does not yet, but might when the platform matures. */
-#undef HAVE_CLOCK /* We have our own version down below */
-#endif /* MS_WINDOWS && !MS_WIN64 */
+#if defined(MS_WINDOWS) && !defined(__BORLANDC__)
+/* Win32 has better clock replacement; we have our own version below. */
+#undef HAVE_CLOCK
+#endif /* MS_WINDOWS && !defined(__BORLANDC__) */
 
 #if defined(PYOS_OS2)
 #define INCL_DOS
@@ -150,7 +161,7 @@
 }
 #endif /* HAVE_CLOCK */
 
-#if defined(MS_WINDOWS) && !defined(MS_WIN64) && !defined(__BORLANDC__)
+#if defined(MS_WINDOWS) && !defined(__BORLANDC__)
 /* Due to Mark Hammond and Tim Peters */
 static PyObject *
 time_clock(PyObject *self, PyObject *args)
@@ -179,7 +190,7 @@
 }
 
 #define HAVE_CLOCK /* So it gets included in the methods */
-#endif /* MS_WINDOWS && !MS_WIN64 */
+#endif /* MS_WINDOWS && !defined(__BORLANDC__) */
 
 #ifdef HAVE_CLOCK
 PyDoc_STRVAR(clock_doc,
@@ -701,7 +712,7 @@
 #ifdef __CYGWIN__
 	tzset();
 	PyModule_AddIntConstant(m, "timezone", _timezone);
-	PyModule_AddIntConstant(m, "altzone", _timezone);
+	PyModule_AddIntConstant(m, "altzone", _timezone-3600);
 	PyModule_AddIntConstant(m, "daylight", _daylight);
 	PyModule_AddObject(m, "tzname",
 			   Py_BuildValue("(zz)", _tzname[0], _tzname[1]));
@@ -809,7 +820,7 @@
 	SetConsoleCtrlHandler( PyCtrlHandler, TRUE);
 #endif /* MS_WINDOWS */
 	if (!initialized) {
-		PyStructSequence_InitType(&StructTimeType, 
+		PyStructSequence_InitType(&StructTimeType,
 					  &struct_time_type_desc);
 	}
 	Py_INCREF(&StructTimeType);
@@ -842,6 +853,7 @@
 			return (double)t.tv_sec + t.tv_usec*0.000001;
 #endif /* !GETTIMEOFDAY_NO_TZ */
 	}
+
 #endif /* !HAVE_GETTIMEOFDAY */
 	{
 #if defined(HAVE_FTIME)
diff --git a/Modules/unicodedata.c b/Modules/unicodedata.c
index 297611c..0660353 100644
--- a/Modules/unicodedata.c
+++ b/Modules/unicodedata.c
@@ -209,7 +209,7 @@
         if (old->category_changed == 0) {
             /* unassigned */
             have_old = 1;
-            rc = -1;
+            rc = -1.0;
         } 
         else if (old->decimal_changed != 0xFF) {
             have_old = 1;
@@ -219,7 +219,7 @@
 
     if (!have_old)
         rc = Py_UNICODE_TONUMERIC(*PyUnicode_AS_UNICODE(v));
-    if (rc < 0) {
+    if (rc == -1.0) {
 	if (defobj == NULL) {
 	    PyErr_SetString(PyExc_ValueError, "not a numeric character");
 	    return NULL;
diff --git a/Modules/zlibmodule.c b/Modules/zlibmodule.c
index 35b8c32..06b0690 100644
--- a/Modules/zlibmodule.c
+++ b/Modules/zlibmodule.c
@@ -653,6 +653,108 @@
     return RetVal;
 }
 
+PyDoc_STRVAR(comp_copy__doc__,
+"copy() -- Return a copy of the compression object.");
+
+static PyObject *
+PyZlib_copy(compobject *self)
+{
+    compobject *retval = NULL;
+    int err;
+
+    retval = newcompobject(&Comptype);
+    if (!retval) return NULL;
+
+    /* Copy the zstream state
+     * We use ENTER_ZLIB / LEAVE_ZLIB to make this thread-safe
+     */
+    ENTER_ZLIB
+    err = deflateCopy(&retval->zst, &self->zst);
+    switch(err) {
+    case(Z_OK):
+        break;
+    case(Z_STREAM_ERROR):
+        PyErr_SetString(PyExc_ValueError, "Inconsistent stream state");
+        goto error;
+    case(Z_MEM_ERROR):
+        PyErr_SetString(PyExc_MemoryError,
+                        "Can't allocate memory for compression object");
+        goto error;
+    default:
+        zlib_error(self->zst, err, "while copying compression object");
+        goto error;
+    }
+
+    Py_INCREF(self->unused_data);
+    Py_INCREF(self->unconsumed_tail);
+    Py_XDECREF(retval->unused_data);
+    Py_XDECREF(retval->unconsumed_tail);
+    retval->unused_data = self->unused_data;
+    retval->unconsumed_tail = self->unconsumed_tail;
+
+    /* Mark it as being initialized */
+    retval->is_initialised = 1;
+
+    LEAVE_ZLIB
+    return (PyObject *)retval;
+
+error:
+    LEAVE_ZLIB
+    Py_XDECREF(retval);
+    return NULL;
+}
+
+PyDoc_STRVAR(decomp_copy__doc__,
+"copy() -- Return a copy of the decompression object.");
+
+static PyObject *
+PyZlib_uncopy(compobject *self)
+{
+    compobject *retval = NULL;
+    int err;
+
+    retval = newcompobject(&Decomptype);
+    if (!retval) return NULL;
+
+    /* Copy the zstream state
+     * We use ENTER_ZLIB / LEAVE_ZLIB to make this thread-safe
+     */
+    ENTER_ZLIB
+    err = inflateCopy(&retval->zst, &self->zst);
+    switch(err) {
+    case(Z_OK):
+        break;
+    case(Z_STREAM_ERROR):
+        PyErr_SetString(PyExc_ValueError, "Inconsistent stream state");
+        goto error;
+    case(Z_MEM_ERROR):
+        PyErr_SetString(PyExc_MemoryError,
+                        "Can't allocate memory for decompression object");
+        goto error;
+    default:
+        zlib_error(self->zst, err, "while copying decompression object");
+        goto error;
+    }
+
+    Py_INCREF(self->unused_data);
+    Py_INCREF(self->unconsumed_tail);
+    Py_XDECREF(retval->unused_data);
+    Py_XDECREF(retval->unconsumed_tail);
+    retval->unused_data = self->unused_data;
+    retval->unconsumed_tail = self->unconsumed_tail;
+
+    /* Mark it as being initialized */
+    retval->is_initialised = 1;
+
+    LEAVE_ZLIB
+    return (PyObject *)retval;
+
+error:
+    LEAVE_ZLIB
+    Py_XDECREF(retval);
+    return NULL;
+}
+
 PyDoc_STRVAR(decomp_flush__doc__,
 "flush( [length] ) -- Return a string containing any remaining\n"
 "decompressed data. length, if given, is the initial size of the\n"
@@ -725,6 +827,8 @@
                  comp_compress__doc__},
     {"flush", (binaryfunc)PyZlib_flush, METH_VARARGS,
               comp_flush__doc__},
+    {"copy",  (PyCFunction)PyZlib_copy, METH_NOARGS,
+              comp_copy__doc__},
     {NULL, NULL}
 };
 
@@ -734,6 +838,8 @@
                    decomp_decompress__doc__},
     {"flush", (binaryfunc)PyZlib_unflush, METH_VARARGS,
               decomp_flush__doc__},
+    {"copy",  (PyCFunction)PyZlib_uncopy, METH_NOARGS,
+              decomp_copy__doc__},
     {NULL, NULL}
 };