Merge current trunk into p3yk. This includes the PyNumber_Index API change,

which unfortunately means the errors from the bytes type change somewhat:

bytes([300]) still raises a ValueError, but bytes([10**100]) now raises a
TypeError (either that, or bytes(1.0) also raises a ValueError --
PyNumber_AsSsize_t() can only raise one type of exception.)

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

........
  r51189 | kurt.kaiser | 2006-08-10 19:11:09 +0200 (Thu, 10 Aug 2006) | 4 lines

  Retrieval of previous shell command was not always preserving indentation
  since 1.2a1) Patch 1528468 Tal Einat.
........
  r51190 | guido.van.rossum | 2006-08-10 19:41:07 +0200 (Thu, 10 Aug 2006) | 3 lines

  Chris McDonough's patch to defend against certain DoS attacks on FieldStorage.
  SF bug #1112549.
........
  r51191 | guido.van.rossum | 2006-08-10 19:42:50 +0200 (Thu, 10 Aug 2006) | 2 lines

  News item for SF bug 1112549.
........
  r51192 | guido.van.rossum | 2006-08-10 20:09:25 +0200 (Thu, 10 Aug 2006) | 2 lines

  Fix title -- it's rc1, not beta3.
........
  r51194 | martin.v.loewis | 2006-08-10 21:04:00 +0200 (Thu, 10 Aug 2006) | 3 lines

  Update dangling references to the 3.2 database to
  mention that this is UCD 4.1 now.
........
  r51195 | tim.peters | 2006-08-11 00:45:34 +0200 (Fri, 11 Aug 2006) | 6 lines

  Followup to bug #1069160.

  PyThreadState_SetAsyncExc():  internal correctness changes wrt
  refcount safety and deadlock avoidance.  Also added a basic test
  case (relying on ctypes) and repaired the docs.
........
  r51196 | tim.peters | 2006-08-11 00:48:45 +0200 (Fri, 11 Aug 2006) | 2 lines

  Whitespace normalization.
........
  r51197 | tim.peters | 2006-08-11 01:22:13 +0200 (Fri, 11 Aug 2006) | 5 lines

  Whitespace normalization broke test_cgi, because a line
  of quoted test data relied on preserving a single trailing
  blank.  Changed the string from raw to regular, and forced
  in the trailing blank via an explicit \x20 escape.
........
  r51198 | tim.peters | 2006-08-11 02:49:01 +0200 (Fri, 11 Aug 2006) | 10 lines

  test_PyThreadState_SetAsyncExc():  This is failing on some
  64-bit boxes.  I have no idea what the ctypes docs mean
  by "integers", and blind-guessing here that it intended to
  mean the signed C "int" type, in which case perhaps I can
  repair this by feeding the thread id argument to type
  ctypes.c_long().

  Also made the worker thread daemonic, so it doesn't hang
  Python shutdown if the test continues to fail.
........
  r51199 | tim.peters | 2006-08-11 05:49:10 +0200 (Fri, 11 Aug 2006) | 6 lines

  force_test_exit():  This has been completely ineffective
  at stopping test_signal from hanging forever on the Tru64
  buildbot.  That could be because there's no such thing as
  signal.SIGALARM.  Changed to the idiotic (but standard)
  signal.SIGALRM instead, and added some more debug output.
........
  r51202 | neal.norwitz | 2006-08-11 08:09:41 +0200 (Fri, 11 Aug 2006) | 6 lines

  Fix the failures on cygwin (2006-08-10 fixed the actual locking issue).

  The first hunk changes the colon to an ! like other Windows variants.
  We need to always wait on the child so the lock gets released and
  no other tests fail.  This is the try/finally in the second hunk.
........
  r51205 | georg.brandl | 2006-08-11 09:15:38 +0200 (Fri, 11 Aug 2006) | 3 lines

  Add Chris McDonough (latest cgi.py patch)
........
  r51206 | georg.brandl | 2006-08-11 09:26:10 +0200 (Fri, 11 Aug 2006) | 3 lines

  logging's atexit hook now runs even if the rest of the module has
  already been cleaned up.
........
  r51212 | thomas.wouters | 2006-08-11 17:02:39 +0200 (Fri, 11 Aug 2006) | 4 lines


  Add ignore of *.pyc and *.pyo to Lib/xml/etree/.
........
  r51215 | thomas.heller | 2006-08-11 21:55:35 +0200 (Fri, 11 Aug 2006) | 7 lines

  When a ctypes C callback function is called, zero out the result
  storage before converting the result to C data.  See the comment in
  the code for details.

  Provide a better context for errors when the conversion of a callback
  function's result cannot be converted.
........
  r51218 | neal.norwitz | 2006-08-12 03:43:40 +0200 (Sat, 12 Aug 2006) | 6 lines

  Klocwork made another run and found a bunch more problems.
  This is the first batch of fixes that should be easy to verify based on context.

  This fixes problem numbers: 220 (ast), 323-324 (symtable),
  321-322 (structseq), 215 (array), 210 (hotshot), 182 (codecs), 209 (etree).
........
  r51219 | neal.norwitz | 2006-08-12 03:45:47 +0200 (Sat, 12 Aug 2006) | 9 lines

  Even though _Py_Mangle() isn't truly public anyone can call it and
  there was no verification that privateobj was a PyString.  If it wasn't
  a string, this could have allowed a NULL pointer to creep in below and crash.

  I wonder if this should be PyString_CheckExact?  Must identifiers be strings
  or can they be subclasses?

  Klocwork #275
........
  r51220 | neal.norwitz | 2006-08-12 03:46:42 +0200 (Sat, 12 Aug 2006) | 5 lines

  It's highly unlikely, though possible for PyEval_Get*() to return NULLs.
  So be safe and do an XINCREF.

  Klocwork # 221-222.
........
  r51221 | neal.norwitz | 2006-08-12 03:47:59 +0200 (Sat, 12 Aug 2006) | 7 lines

  This code is actually not used unless WITHOUT_COMPLEX is defined.
  However, there was no error checking that PyFloat_FromDouble returned
  a valid pointer.  I believe this change is correct as it seemed
  to follow other code in the area.

  Klocwork # 292.
........
  r51222 | neal.norwitz | 2006-08-12 03:49:12 +0200 (Sat, 12 Aug 2006) | 5 lines

  Handle NULL nodes while parsing.  I'm not entirely sure this is correct.
  There might be something else that needs to be done to setup the error.

  Klocwork #295.
........
  r51223 | neal.norwitz | 2006-08-12 03:50:38 +0200 (Sat, 12 Aug 2006) | 6 lines

  If _stat_float_times is false, we will try to INCREF ival which could be NULL.
  Return early in that case.  The caller checks for PyErr_Occurred so this
  should be ok.

  Klocwork #297
........
  r51224 | neal.norwitz | 2006-08-12 03:51:12 +0200 (Sat, 12 Aug 2006) | 3 lines

  Move the assert which checks for a NULL pointer first.
  Klocwork #274.
........
  r51225 | neal.norwitz | 2006-08-12 03:53:28 +0200 (Sat, 12 Aug 2006) | 5 lines

  Try to handle a malloc failure.  I'm not entirely sure this is correct.
  There might be something else we need to do to handle the exception.

  Klocwork # 212-213
........
  r51226 | neal.norwitz | 2006-08-12 03:57:47 +0200 (Sat, 12 Aug 2006) | 6 lines

  I'm not sure why this code allocates this string for the error message.
  I think it would be better to always use snprintf and have the format
  limit the size of the name appropriately (like %.200s).

  Klocwork #340
........
  r51227 | neal.norwitz | 2006-08-12 04:06:34 +0200 (Sat, 12 Aug 2006) | 3 lines

  Check returned pointer is valid.
  Klocwork #233
........
  r51228 | neal.norwitz | 2006-08-12 04:12:30 +0200 (Sat, 12 Aug 2006) | 1 line

  Whoops, how did that get in there. :-)  Revert all the parts of 51227 that were not supposed to go it.  Only Modules/_ctypes/cfields.c was supposed to be changed
........
  r51229 | neal.norwitz | 2006-08-12 04:33:36 +0200 (Sat, 12 Aug 2006) | 4 lines

  Don't deref v if it's NULL.

  Klocwork #214
........
  r51230 | neal.norwitz | 2006-08-12 05:16:54 +0200 (Sat, 12 Aug 2006) | 5 lines

  Check return of PyMem_MALLOC (garbage) is non-NULL.
  Check seq in both portions of if/else.

  Klocwork #289-290.
........
  r51231 | neal.norwitz | 2006-08-12 05:17:41 +0200 (Sat, 12 Aug 2006) | 4 lines

  PyModule_GetDict() can fail, produce fatal errors if this happens on startup.

  Klocwork #298-299.
........
  r51232 | neal.norwitz | 2006-08-12 05:18:50 +0200 (Sat, 12 Aug 2006) | 5 lines

  Verify verdat which is returned from malloc is not NULL.
  Ensure we don't pass NULL to free.

  Klocwork #306 (at least the first part, checking malloc)
........
  r51233 | tim.peters | 2006-08-12 06:42:47 +0200 (Sat, 12 Aug 2006) | 35 lines

  test_signal:  Signal handling on the Tru64 buildbot
  appears to be utterly insane.  Plug some theoretical
  insecurities in the test script:

  - Verify that the SIGALRM handler was actually installed.

  - Don't call alarm() before the handler is installed.

  - Move everything that can fail inside the try/finally,
    so the test cleans up after itself more often.

  - Try sending all the expected signals in
    force_test_exit(), not just SIGALRM.  Since that was
    fixed to actually send SIGALRM (instead of invisibly
    dying with an AttributeError), we've seen that sending
    SIGALRM alone does not stop this from hanging.

  - Move the "kill the child" business into the finally
    clause, so the child doesn't survive test failure
    to send SIGALRM to other tests later (there are also
    baffling SIGALRM-related failures in test_socket).

  - Cancel the alarm in the finally clause -- if the
    test dies early, we again don't want SIGALRM showing
    up to confuse a later test.

  Alas, this still relies on timing luck wrt the spawned
  script that sends the test signals, but it's hard to see
  how waiting for seconds can so often be so unlucky.

  test_threadedsignals:  curiously, this test never fails
  on Tru64, but doesn't normally signal SIGALRM.  Anyway,
  fixed an obvious (but probably inconsequential) logic
  error.
........
  r51234 | tim.peters | 2006-08-12 07:17:41 +0200 (Sat, 12 Aug 2006) | 8 lines

  Ah, fudge.  One of the prints here actually "shouldn't be"
  protected by "if verbose:", which caused the test to fail on
  all non-Windows boxes.

  Note that I deliberately didn't convert this to unittest yet,
  because I expect it would be even harder to debug this on Tru64
  after conversion.
........
  r51235 | georg.brandl | 2006-08-12 10:32:02 +0200 (Sat, 12 Aug 2006) | 3 lines

  Repair logging test spew caused by rev. 51206.
........
  r51236 | neal.norwitz | 2006-08-12 19:03:09 +0200 (Sat, 12 Aug 2006) | 8 lines

  Patch #1538606, Patch to fix __index__() clipping.

  I modified this patch some by fixing style, some error checking, and adding
  XXX comments.  This patch requires review and some changes are to be expected.
  I'm checking in now to get the greatest possible review and establish a
  baseline for moving forward.  I don't want this to hold up release if possible.
........
  r51238 | neal.norwitz | 2006-08-12 20:44:06 +0200 (Sat, 12 Aug 2006) | 10 lines

  Fix a couple of bugs exposed by the new __index__ code.  The 64-bit buildbots
  were failing due to inappropriate clipping of numbers larger than 2**31
  with new-style classes. (typeobject.c)  In reviewing the code for classic
  classes, there were 2 problems.  Any negative value return could be returned.
  Always return -1 if there was an error.  Also make the checks similar
  with the new-style classes.  I believe this is correct for 32 and 64 bit
  boxes, including Windows64.

  Add a test of classic classes too.
........
  r51240 | neal.norwitz | 2006-08-13 02:20:49 +0200 (Sun, 13 Aug 2006) | 1 line

  SF bug #1539336, distutils example code missing
........
  r51245 | neal.norwitz | 2006-08-13 20:10:10 +0200 (Sun, 13 Aug 2006) | 6 lines

  Move/copy assert for tstate != NULL before first use.
  Verify that PyEval_Get{Globals,Locals} returned valid pointers.

  Klocwork 231-232
........
  r51246 | neal.norwitz | 2006-08-13 20:10:28 +0200 (Sun, 13 Aug 2006) | 5 lines

  Handle a whole lot of failures from PyString_FromInternedString().

  Should fix most of Klocwork 234-272.
........
  r51247 | neal.norwitz | 2006-08-13 20:10:47 +0200 (Sun, 13 Aug 2006) | 8 lines

  cpathname could be NULL if it was longer than MAXPATHLEN.  Don't try
  to write the .pyc to NULL.

  Check results of PyList_GetItem() and PyModule_GetDict() are not NULL.

  Klocwork 282, 283, 285
........
  r51248 | neal.norwitz | 2006-08-13 20:11:08 +0200 (Sun, 13 Aug 2006) | 6 lines

  Fix segfault when doing string formatting on subclasses of long if
  __oct__, __hex__ don't return a string.

  Klocwork 308
........
  r51250 | neal.norwitz | 2006-08-13 20:11:27 +0200 (Sun, 13 Aug 2006) | 5 lines

  Check return result of PyModule_GetDict().
  Fix a bunch of refleaks in the init of the module.  This would only be found
  when running python -v.
........
  r51251 | neal.norwitz | 2006-08-13 20:11:43 +0200 (Sun, 13 Aug 2006) | 5 lines

  Handle malloc and fopen failures more gracefully.

  Klocwork 180-181
........
  r51252 | neal.norwitz | 2006-08-13 20:12:03 +0200 (Sun, 13 Aug 2006) | 7 lines

  It's very unlikely, though possible that source is not a string.  Verify
  that PyString_AsString() returns a valid pointer.  (The problem can
  arise when zlib.decompress doesn't return a string.)

  Klocwork 346
........
  r51253 | neal.norwitz | 2006-08-13 20:12:26 +0200 (Sun, 13 Aug 2006) | 5 lines

  Handle failures from lookup.

  Klocwork 341-342
........
  r51254 | neal.norwitz | 2006-08-13 20:12:45 +0200 (Sun, 13 Aug 2006) | 6 lines

  Handle failure from PyModule_GetDict() (Klocwork 208).

  Fix a bunch of refleaks in the init of the module.  This would only be found
  when running python -v.
........
  r51255 | neal.norwitz | 2006-08-13 20:13:02 +0200 (Sun, 13 Aug 2006) | 4 lines

  Really address the issue of where to place the assert for leftblock.
  (Followup of Klocwork 274)
........
  r51256 | neal.norwitz | 2006-08-13 20:13:36 +0200 (Sun, 13 Aug 2006) | 4 lines

  Handle malloc failure.

  Klocwork 281
........
  r51258 | neal.norwitz | 2006-08-13 20:40:39 +0200 (Sun, 13 Aug 2006) | 4 lines

  Handle alloca failures.

  Klocwork 225-228
........
  r51259 | neal.norwitz | 2006-08-13 20:41:15 +0200 (Sun, 13 Aug 2006) | 1 line

  Get rid of compiler warning
........
  r51261 | neal.norwitz | 2006-08-14 02:51:15 +0200 (Mon, 14 Aug 2006) | 1 line

  Ignore pgen.exe and kill_python.exe for cygwin
........
  r51262 | neal.norwitz | 2006-08-14 02:59:03 +0200 (Mon, 14 Aug 2006) | 4 lines

  Can't return NULL from a void function.  If there is a memory error,
  about the best we can do is call PyErr_WriteUnraisable and go on.
  We won't be able to do the call below either, so verify delstr is valid.
........
  r51263 | neal.norwitz | 2006-08-14 03:49:54 +0200 (Mon, 14 Aug 2006) | 1 line

  Update purify doc some.
........
  r51264 | thomas.heller | 2006-08-14 09:13:05 +0200 (Mon, 14 Aug 2006) | 2 lines

  Remove unused, buggy test function.
  Fixes klockwork issue #207.
........
  r51265 | thomas.heller | 2006-08-14 09:14:09 +0200 (Mon, 14 Aug 2006) | 2 lines

  Check for NULL return value from new_CArgObject().
  Fixes klockwork issues #183, #184, #185.
........
  r51266 | thomas.heller | 2006-08-14 09:50:14 +0200 (Mon, 14 Aug 2006) | 2 lines

  Check for NULL return value of GenericCData_new().
  Fixes klockwork issues #188, #189.
........
  r51274 | thomas.heller | 2006-08-14 12:02:24 +0200 (Mon, 14 Aug 2006) | 2 lines

  Revert the change that tries to zero out a closure's result storage
  area because the size if unknown in source/callproc.c.
........
  r51276 | marc-andre.lemburg | 2006-08-14 12:55:19 +0200 (Mon, 14 Aug 2006) | 11 lines

  Slightly revised version of patch #1538956:

  Replace UnicodeDecodeErrors raised during == and !=
  compares of Unicode and other objects with a new
  UnicodeWarning.

  All other comparisons continue to raise exceptions.
  Exceptions other than UnicodeDecodeErrors are also left
  untouched.
........
  r51277 | thomas.heller | 2006-08-14 13:17:48 +0200 (Mon, 14 Aug 2006) | 13 lines

  Apply the patch #1532975 plus ideas from the patch #1533481.

  ctypes instances no longer have the internal and undocumented
  '_as_parameter_' attribute which was used to adapt them to foreign
  function calls; this mechanism is replaced by a function pointer in
  the type's stgdict.

  In the 'from_param' class methods, try the _as_parameter_ attribute if
  other conversions are not possible.

  This makes the documented _as_parameter_ mechanism work as intended.

  Change the ctypes version number to 1.0.1.
........
  r51278 | marc-andre.lemburg | 2006-08-14 13:44:34 +0200 (Mon, 14 Aug 2006) | 3 lines

  Readd NEWS items that were accidentally removed by r51276.
........
  r51279 | georg.brandl | 2006-08-14 14:36:06 +0200 (Mon, 14 Aug 2006) | 3 lines

  Improve markup in PyUnicode_RichCompare.
........
  r51280 | marc-andre.lemburg | 2006-08-14 14:57:27 +0200 (Mon, 14 Aug 2006) | 3 lines

  Correct an accidentally removed previous patch.
........
  r51281 | thomas.heller | 2006-08-14 18:17:41 +0200 (Mon, 14 Aug 2006) | 3 lines

  Patch #1536908: Add support for AMD64 / OpenBSD.
  Remove the -no-stack-protector compiler flag for OpenBSD
  as it has been reported to be unneeded.
........
  r51282 | thomas.heller | 2006-08-14 18:20:04 +0200 (Mon, 14 Aug 2006) | 1 line

  News item for rev 51281.
........
  r51283 | georg.brandl | 2006-08-14 22:25:39 +0200 (Mon, 14 Aug 2006) | 3 lines

  Fix refleak introduced in rev. 51248.
........
  r51284 | georg.brandl | 2006-08-14 23:34:08 +0200 (Mon, 14 Aug 2006) | 5 lines

  Make tabnanny recognize IndentationErrors raised by tokenize.
  Add a test to test_inspect to make sure indented source
  is recognized correctly. (fixes #1224621)
........
  r51285 | georg.brandl | 2006-08-14 23:42:55 +0200 (Mon, 14 Aug 2006) | 3 lines

  Patch #1535500: fix segfault in BZ2File.writelines and make sure it
  raises the correct exceptions.
........
  r51287 | georg.brandl | 2006-08-14 23:45:32 +0200 (Mon, 14 Aug 2006) | 3 lines

  Add an additional test: BZ2File write methods should raise IOError
  when file is read-only.
........
  r51289 | georg.brandl | 2006-08-14 23:55:28 +0200 (Mon, 14 Aug 2006) | 3 lines

  Patch #1536071: trace.py should now find the full module name of a
  file correctly even on Windows.
........
  r51290 | georg.brandl | 2006-08-15 00:01:24 +0200 (Tue, 15 Aug 2006) | 3 lines

  Cookie.py shouldn't "bogusly" use string._idmap.
........
  r51291 | georg.brandl | 2006-08-15 00:10:24 +0200 (Tue, 15 Aug 2006) | 3 lines

  Patch #1511317: don't crash on invalid hostname info
........
  r51292 | tim.peters | 2006-08-15 02:25:04 +0200 (Tue, 15 Aug 2006) | 2 lines

  Whitespace normalization.
........
  r51293 | neal.norwitz | 2006-08-15 06:14:57 +0200 (Tue, 15 Aug 2006) | 3 lines

  Georg fixed one of my bugs, so I'll repay him with 2 NEWS entries.
  Now we're even. :-)
........
  r51295 | neal.norwitz | 2006-08-15 06:58:28 +0200 (Tue, 15 Aug 2006) | 8 lines

  Fix the test for SocketServer so it should pass on cygwin and not fail
  sporadically on other platforms.  This is really a band-aid that doesn't
  fix the underlying issue in SocketServer.  It's not clear if it's worth
  it to fix SocketServer, however, I opened a bug to track it:

  	http://python.org/sf/1540386
........
  r51296 | neal.norwitz | 2006-08-15 06:59:30 +0200 (Tue, 15 Aug 2006) | 3 lines

  Update the docstring to use a version a little newer than 1999.  This was
  taken from a Debian patch.  Should we update the version for each release?
........
  r51298 | neal.norwitz | 2006-08-15 08:29:03 +0200 (Tue, 15 Aug 2006) | 2 lines

  Subclasses of int/long are allowed to define an __index__.
........
  r51300 | thomas.heller | 2006-08-15 15:07:21 +0200 (Tue, 15 Aug 2006) | 1 line

  Check for NULL return value from new_CArgObject calls.
........
  r51303 | kurt.kaiser | 2006-08-16 05:15:26 +0200 (Wed, 16 Aug 2006) | 2 lines

  The 'with' statement is now a Code Context block opener
........
  r51304 | anthony.baxter | 2006-08-16 05:42:26 +0200 (Wed, 16 Aug 2006) | 1 line

  preparing for 2.5c1
........
  r51305 | anthony.baxter | 2006-08-16 05:58:37 +0200 (Wed, 16 Aug 2006) | 1 line

  preparing for 2.5c1 - no, really this time
........
  r51306 | kurt.kaiser | 2006-08-16 07:01:42 +0200 (Wed, 16 Aug 2006) | 9 lines

  Patch #1540892: site.py Quitter() class attempts to close sys.stdin
  before raising SystemExit, allowing IDLE to honor quit() and exit().

  M    Lib/site.py
  M    Lib/idlelib/PyShell.py
  M    Lib/idlelib/CREDITS.txt
  M    Lib/idlelib/NEWS.txt
  M    Misc/NEWS
........
  r51307 | ka-ping.yee | 2006-08-16 09:02:50 +0200 (Wed, 16 Aug 2006) | 6 lines

  Update code and tests to support the 'bytes_le' attribute (for
  little-endian byte order on Windows), and to work around clocks
  with low resolution yielding duplicate UUIDs.

  Anthony Baxter has approved this change.
........
  r51308 | kurt.kaiser | 2006-08-16 09:04:17 +0200 (Wed, 16 Aug 2006) | 2 lines

  Get quit() and exit() to work cleanly when not using subprocess.
........
  r51309 | marc-andre.lemburg | 2006-08-16 10:13:26 +0200 (Wed, 16 Aug 2006) | 2 lines

  Revert to having static version numbers again.
........
  r51310 | martin.v.loewis | 2006-08-16 14:55:10 +0200 (Wed, 16 Aug 2006) | 2 lines

  Build _hashlib on Windows. Build OpenSSL with masm assembler code.
  Fixes #1535502.
........
  r51311 | thomas.heller | 2006-08-16 15:03:11 +0200 (Wed, 16 Aug 2006) | 6 lines

  Add commented assert statements to check that the result of
  PyObject_stgdict() and PyType_stgdict() calls are non-NULL before
  dereferencing the result.  Hopefully this fixes what klocwork is
  complaining about.

  Fix a few other nits as well.
........
  r51312 | anthony.baxter | 2006-08-16 15:08:25 +0200 (Wed, 16 Aug 2006) | 1 line

  news entry for 51307
........
  r51313 | andrew.kuchling | 2006-08-16 15:22:20 +0200 (Wed, 16 Aug 2006) | 1 line

  Add UnicodeWarning
........
  r51314 | andrew.kuchling | 2006-08-16 15:41:52 +0200 (Wed, 16 Aug 2006) | 1 line

  Bump document version to 1.0; remove pystone paragraph
........
  r51315 | andrew.kuchling | 2006-08-16 15:51:32 +0200 (Wed, 16 Aug 2006) | 1 line

  Link to docs; remove an XXX comment
........
  r51316 | martin.v.loewis | 2006-08-16 15:58:51 +0200 (Wed, 16 Aug 2006) | 1 line

  Make cl build step compile-only (/c). Remove libs from source list.
........
  r51317 | thomas.heller | 2006-08-16 16:07:44 +0200 (Wed, 16 Aug 2006) | 5 lines

  The __repr__ method of a NULL py_object does no longer raise an
  exception.  Remove a stray '?' character from the exception text
  when the value is retrieved of such an object.

  Includes tests.
........
  r51318 | andrew.kuchling | 2006-08-16 16:18:23 +0200 (Wed, 16 Aug 2006) | 1 line

  Update bug/patch counts
........
  r51319 | andrew.kuchling | 2006-08-16 16:21:14 +0200 (Wed, 16 Aug 2006) | 1 line

  Wording/typo fixes
........
  r51320 | thomas.heller | 2006-08-16 17:10:12 +0200 (Wed, 16 Aug 2006) | 9 lines

  Remove the special casing of Py_None when converting the return value
  of the Python part of a callback function to C.  If it cannot be
  converted, call PyErr_WriteUnraisable with the exception we got.
  Before, arbitrary data has been passed to the calling C code in this
  case.

  (I'm not really sure the NEWS entry is understandable, but I cannot
  find better words)
........
  r51321 | marc-andre.lemburg | 2006-08-16 18:11:01 +0200 (Wed, 16 Aug 2006) | 2 lines

  Add NEWS item mentioning the reverted distutils version number patch.
........
  r51322 | fredrik.lundh | 2006-08-16 18:47:07 +0200 (Wed, 16 Aug 2006) | 5 lines

  SF#1534630

  ignore data that arrives before the opening start tag
........
  r51324 | andrew.kuchling | 2006-08-16 19:11:18 +0200 (Wed, 16 Aug 2006) | 1 line

  Grammar fix
........
  r51328 | thomas.heller | 2006-08-16 20:02:11 +0200 (Wed, 16 Aug 2006) | 12 lines

  Tutorial:

      Clarify somewhat how parameters are passed to functions
      (especially explain what integer means).

      Correct the table - Python integers and longs can both be used.
      Further clarification to the table comparing ctypes types, Python
      types, and C types.

  Reference:

      Replace integer by C ``int`` where it makes sense.
........
  r51329 | kurt.kaiser | 2006-08-16 23:45:59 +0200 (Wed, 16 Aug 2006) | 8 lines

  File menu hotkeys: there were three 'p' assignments.  Reassign the
  'Save Copy As' and 'Print' hotkeys to 'y' and 't'.  Change the
  Shell menu hotkey from 's' to 'l'.

  M    Bindings.py
  M    PyShell.py
  M    NEWS.txt
........
  r51330 | neil.schemenauer | 2006-08-17 01:38:05 +0200 (Thu, 17 Aug 2006) | 3 lines

  Fix a bug in the ``compiler`` package that caused invalid code to be
  generated for generator expressions.
........
  r51342 | martin.v.loewis | 2006-08-17 21:19:32 +0200 (Thu, 17 Aug 2006) | 3 lines

  Merge 51340 and 51341 from 2.5 branch:
  Leave tk build directory to restore original path.
  Invoke debug mk1mf.pl after running Configure.
........
  r51354 | martin.v.loewis | 2006-08-18 05:47:18 +0200 (Fri, 18 Aug 2006) | 3 lines

  Bug #1541863: uuid.uuid1 failed to generate unique identifiers
  on systems with low clock resolution.
........
  r51355 | neal.norwitz | 2006-08-18 05:57:54 +0200 (Fri, 18 Aug 2006) | 1 line

  Add template for 2.6 on HEAD
........
  r51356 | neal.norwitz | 2006-08-18 06:01:38 +0200 (Fri, 18 Aug 2006) | 1 line

  More post-release wibble
........
  r51357 | neal.norwitz | 2006-08-18 06:58:33 +0200 (Fri, 18 Aug 2006) | 1 line

  Try to get Windows bots working again
........
  r51358 | neal.norwitz | 2006-08-18 07:10:00 +0200 (Fri, 18 Aug 2006) | 1 line

  Try to get Windows bots working again. Take 2
........
  r51359 | neal.norwitz | 2006-08-18 07:39:20 +0200 (Fri, 18 Aug 2006) | 1 line

  Try to get Unix bots install working again.
........
  r51360 | neal.norwitz | 2006-08-18 07:41:46 +0200 (Fri, 18 Aug 2006) | 1 line

  Set version to 2.6a0, seems more consistent.
........
  r51362 | neal.norwitz | 2006-08-18 08:14:52 +0200 (Fri, 18 Aug 2006) | 1 line

  More version wibble
........
  r51364 | georg.brandl | 2006-08-18 09:27:59 +0200 (Fri, 18 Aug 2006) | 4 lines

  Bug #1541682: Fix example in the "Refcount details" API docs.
  Additionally, remove a faulty example showing PySequence_SetItem applied
  to a newly created list object and add notes that this isn't a good idea.
........
  r51366 | anthony.baxter | 2006-08-18 09:29:02 +0200 (Fri, 18 Aug 2006) | 3 lines

  Updating IDLE's version number to match Python's (as per python-dev
  discussion).
........
  r51367 | anthony.baxter | 2006-08-18 09:30:07 +0200 (Fri, 18 Aug 2006) | 1 line

  RPM specfile updates
........
  r51368 | georg.brandl | 2006-08-18 09:35:47 +0200 (Fri, 18 Aug 2006) | 2 lines

  Typo in tp_clear docs.
........
  r51378 | andrew.kuchling | 2006-08-18 15:57:13 +0200 (Fri, 18 Aug 2006) | 1 line

  Minor edits
........
  r51379 | thomas.heller | 2006-08-18 16:38:46 +0200 (Fri, 18 Aug 2006) | 6 lines

  Add asserts to check for 'impossible' NULL values, with comments.
  In one place where I'n not 1000% sure about the non-NULL, raise
  a RuntimeError for safety.

  This should fix the klocwork issues that Neal sent me.  If so,
  it should be applied to the release25-maint branch also.
........
  r51400 | neal.norwitz | 2006-08-19 06:22:33 +0200 (Sat, 19 Aug 2006) | 5 lines

  Move initialization of interned strings to before allocating the
  object so we don't leak op.  (Fixes an earlier patch to this code)

  Klockwork #350
........
  r51401 | neal.norwitz | 2006-08-19 06:23:04 +0200 (Sat, 19 Aug 2006) | 4 lines

  Move assert to after NULL check, otherwise we deref NULL in the assert.

  Klocwork #307
........
  r51402 | neal.norwitz | 2006-08-19 06:25:29 +0200 (Sat, 19 Aug 2006) | 2 lines

  SF #1542693: Remove semi-colon at end of PyImport_ImportModuleEx macro
........
  r51403 | neal.norwitz | 2006-08-19 06:28:55 +0200 (Sat, 19 Aug 2006) | 6 lines

  Move initialization to after the asserts for non-NULL values.

  Klocwork 286-287.

  (I'm not backporting this, but if someone wants to, feel free.)
........
  r51404 | neal.norwitz | 2006-08-19 06:52:03 +0200 (Sat, 19 Aug 2006) | 6 lines

  Handle PyString_FromInternedString() failing (unlikely, but possible).

  Klocwork #325

  (I'm not backporting this, but if someone wants to, feel free.)
........
  r51416 | georg.brandl | 2006-08-20 15:15:39 +0200 (Sun, 20 Aug 2006) | 2 lines

  Patch #1542948: fix urllib2 header casing issue. With new test.
........
  r51428 | jeremy.hylton | 2006-08-21 18:19:37 +0200 (Mon, 21 Aug 2006) | 3 lines

  Move peephole optimizer to separate file.
........
  r51429 | jeremy.hylton | 2006-08-21 18:20:29 +0200 (Mon, 21 Aug 2006) | 2 lines

  Move peephole optimizer to separate file.  (Forgot .h in previous checkin.)
........
  r51432 | neal.norwitz | 2006-08-21 19:59:46 +0200 (Mon, 21 Aug 2006) | 5 lines

  Fix bug #1543303, tarfile adds padding that breaks gunzip.
  Patch # 1543897.

  Will backport to 2.5
........
  r51433 | neal.norwitz | 2006-08-21 20:01:30 +0200 (Mon, 21 Aug 2006) | 2 lines

  Add assert to make Klocwork happy (#276)
........
This commit is contained in:
Thomas Wouters 2006-08-21 19:07:27 +00:00
parent 18a67ba0bd
commit 00ee7baf49
133 changed files with 2704 additions and 1508 deletions

View File

@ -5,6 +5,10 @@ of their type, or with wide classes of object types (e.g. all
numerical types, or all sequence types). When used on object types numerical types, or all sequence types). When used on object types
for which they do not apply, they will raise a Python exception. for which they do not apply, they will raise a Python exception.
It is not possible to use these functions on objects that are not properly
initialized, such as a list object that has been created by
\cfunction{PyList_New()}, but whose items have not been set to some
non-\code{NULL} value yet.
\section{Object Protocol \label{object}} \section{Object Protocol \label{object}}
@ -673,12 +677,31 @@ determination.
\samp{float(\var{o})}.\bifuncindex{float} \samp{float(\var{o})}.\bifuncindex{float}
\end{cfuncdesc} \end{cfuncdesc}
\begin{cfuncdesc}{Py_ssize_t}{PyNumber_Index}{PyObject *o} \begin{cfuncdesc}{PyObject*}{PyNumber_Index}{PyObject *o}
Returns the \var{o} converted to a Py_ssize_t integer on success, or Returns the \var{o} converted to a Python int or long on success or \NULL{}
-1 with an exception raised on failure. with a TypeError exception raised on failure.
\versionadded{2.5} \versionadded{2.5}
\end{cfuncdesc} \end{cfuncdesc}
\begin{cfuncdesc}{Py_ssize_t}{PyNumber_AsSsize_t}{PyObject *o, PyObject *exc}
Returns \var{o} converted to a Py_ssize_t value if \var{o}
can be interpreted as an integer. If \var{o} can be converted to a Python
int or long but the attempt to convert to a Py_ssize_t value
would raise an \exception{OverflowError}, then the \var{exc} argument
is the type of exception that will be raised (usually \exception{IndexError}
or \exception{OverflowError}). If \var{exc} is \NULL{}, then the exception
is cleared and the value is clipped to \var{PY_SSIZE_T_MIN}
for a negative integer or \var{PY_SSIZE_T_MAX} for a positive integer.
\versionadded{2.5}
\end{cfuncdesc}
\begin{cfuncdesc}{int}{PyIndex_Check}{PyObject *o}
Returns True if \var{o} is an index integer (has the nb_index slot of
the tp_as_number structure filled in).
\versionadded{2.5}
\end{cfuncdesc}
\section{Sequence Protocol \label{sequence}} \section{Sequence Protocol \label{sequence}}
\begin{cfuncdesc}{int}{PySequence_Check}{PyObject *o} \begin{cfuncdesc}{int}{PySequence_Check}{PyObject *o}

View File

@ -1560,6 +1560,26 @@ They all return \NULL{} or \code{-1} if an exception occurs.
greater than, respectively. greater than, respectively.
\end{cfuncdesc} \end{cfuncdesc}
\begin{cfuncdesc}{int}{PyUnicode_RichCompare}{PyObject *left,
PyObject *right,
int op}
Rich compare two unicode strings and return one of the following:
\begin{itemize}
\item \code{NULL} in case an exception was raised
\item \constant{Py_True} or \constant{Py_False} for successful comparisons
\item \constant{Py_NotImplemented} in case the type combination is unknown
\end{itemize}
Note that \constant{Py_EQ} and \constant{Py_NE} comparisons can cause a
\exception{UnicodeWarning} in case the conversion of the arguments to
Unicode fails with a \exception{UnicodeDecodeError}.
Possible values for \var{op} are
\constant{Py_GT}, \constant{Py_GE}, \constant{Py_EQ},
\constant{Py_NE}, \constant{Py_LT}, and \constant{Py_LE}.
\end{cfuncdesc}
\begin{cfuncdesc}{PyObject*}{PyUnicode_Format}{PyObject *format, \begin{cfuncdesc}{PyObject*}{PyUnicode_Format}{PyObject *format,
PyObject *args} PyObject *args}
Return a new string object from \var{format} and \var{args}; this Return a new string object from \var{format} and \var{args}; this
@ -1820,6 +1840,11 @@ format.
\begin{cfuncdesc}{PyObject*}{PyList_New}{Py_ssize_t len} \begin{cfuncdesc}{PyObject*}{PyList_New}{Py_ssize_t len}
Return a new list of length \var{len} on success, or \NULL{} on Return a new list of length \var{len} on success, or \NULL{} on
failure. failure.
\note{If \var{length} is greater than zero, the returned list object's
items are set to \code{NULL}. Thus you cannot use abstract
API functions such as \cfunction{PySequence_SetItem()}
or expose the object to Python code before setting all items to a
real object with \cfunction{PyList_SetItem()}.}
\end{cfuncdesc} \end{cfuncdesc}
\begin{cfuncdesc}{Py_ssize_t}{PyList_Size}{PyObject *list} \begin{cfuncdesc}{Py_ssize_t}{PyList_Size}{PyObject *list}

View File

@ -285,10 +285,11 @@ error indicator for each thread.
names are \samp{PyExc_} followed by the Python exception name. names are \samp{PyExc_} followed by the Python exception name.
These have the type \ctype{PyObject*}; they are all class objects. These have the type \ctype{PyObject*}; they are all class objects.
Their names are \cdata{PyExc_Warning}, \cdata{PyExc_UserWarning}, Their names are \cdata{PyExc_Warning}, \cdata{PyExc_UserWarning},
\cdata{PyExc_DeprecationWarning}, \cdata{PyExc_SyntaxWarning}, \cdata{PyExc_UnicodeWarning}, \cdata{PyExc_DeprecationWarning},
\cdata{PyExc_RuntimeWarning}, and \cdata{PyExc_FutureWarning}. \cdata{PyExc_SyntaxWarning}, \cdata{PyExc_RuntimeWarning}, and
\cdata{PyExc_Warning} is a subclass of \cdata{PyExc_Exception}; the \cdata{PyExc_FutureWarning}. \cdata{PyExc_Warning} is a subclass of
other warning categories are subclasses of \cdata{PyExc_Warning}. \cdata{PyExc_Exception}; the other warning categories are subclasses
of \cdata{PyExc_Warning}.
For information about warning control, see the documentation for the For information about warning control, see the documentation for the
\module{warnings} module and the \programopt{-W} option in the \module{warnings} module and the \programopt{-W} option in the

View File

@ -696,15 +696,15 @@ interpreter lock has been created.
\end{cfuncdesc} \end{cfuncdesc}
\begin{cfuncdesc}{int}{PyThreadState_SetAsyncExc}{long id, PyObject *exc} \begin{cfuncdesc}{int}{PyThreadState_SetAsyncExc}{long id, PyObject *exc}
Asynchronously raise an exception in a thread. Asynchronously raise an exception in a thread.
The \var{id} argument is the thread id of the target thread; The \var{id} argument is the thread id of the target thread;
\var{exc} is the exception object to be raised. \var{exc} is the exception object to be raised.
This function does not steal any references to \var{exc}. This function does not steal any references to \var{exc}.
To prevent naive misuse, you must write your own C extension To prevent naive misuse, you must write your own C extension
to call this. Must be called with the GIL held. to call this. Must be called with the GIL held.
Returns the number of thread states modified; if it returns a number Returns the number of thread states modified; this is normally one, but
greater than one, you're in trouble, and you should call it again will be zero if the thread id isn't found. If \var{exc} is
with \var{exc} set to \constant{NULL} to revert the effect. \constant{NULL}, the pending exception (if any) for the thread is cleared.
This raises no exceptions. This raises no exceptions.
\versionadded{2.3} \versionadded{2.3}
\end{cfuncdesc} \end{cfuncdesc}

View File

@ -225,25 +225,10 @@ immutable data type. You should only use
\cfunction{PyTuple_SetItem()} for tuples that you are creating \cfunction{PyTuple_SetItem()} for tuples that you are creating
yourself. yourself.
Equivalent code for populating a list can be written using Equivalent code for populating a list can be written using
\cfunction{PyList_New()} and \cfunction{PyList_SetItem()}. Such code \cfunction{PyList_New()} and \cfunction{PyList_SetItem()}.
can also use \cfunction{PySequence_SetItem()}; this illustrates the
difference between the two (the extra \cfunction{Py_DECREF()} calls):
\begin{verbatim} However, in practice, you will rarely use these ways of
PyObject *l, *x;
l = PyList_New(3);
x = PyInt_FromLong(1L);
PySequence_SetItem(l, 0, x); Py_DECREF(x);
x = PyInt_FromLong(2L);
PySequence_SetItem(l, 1, x); Py_DECREF(x);
x = PyString_FromString("three");
PySequence_SetItem(l, 2, x); Py_DECREF(x);
\end{verbatim}
You might find it strange that the ``recommended'' approach takes more
code. However, in practice, you will rarely use these ways of
creating and populating a tuple or list. There's a generic function, creating and populating a tuple or list. There's a generic function,
\cfunction{Py_BuildValue()}, that can create most common objects from \cfunction{Py_BuildValue()}, that can create most common objects from
C values, directed by a \dfn{format string}. For example, the C values, directed by a \dfn{format string}. For example, the
@ -251,10 +236,10 @@ above two blocks of code could be replaced by the following (which
also takes care of the error checking): also takes care of the error checking):
\begin{verbatim} \begin{verbatim}
PyObject *t, *l; PyObject *tuple, *list;
t = Py_BuildValue("(iis)", 1, 2, "three"); tuple = Py_BuildValue("(iis)", 1, 2, "three");
l = Py_BuildValue("[iis]", 1, 2, "three"); list = Py_BuildValue("[iis]", 1, 2, "three");
\end{verbatim} \end{verbatim}
It is much more common to use \cfunction{PyObject_SetItem()} and It is much more common to use \cfunction{PyObject_SetItem()} and
@ -276,8 +261,12 @@ set_all(PyObject *target, PyObject *item)
if (n < 0) if (n < 0)
return -1; return -1;
for (i = 0; i < n; i++) { for (i = 0; i < n; i++) {
if (PyObject_SetItem(target, i, item) < 0) PyObject *index = PyInt_FromLong(i);
if (!index)
return -1; return -1;
if (PyObject_SetItem(target, index, item) < 0)
return -1;
Py_DECREF(index);
} }
return 0; return 0;
} }

View File

@ -979,7 +979,7 @@ The following three fields only exist if the
More information about Python's garbage collection More information about Python's garbage collection
scheme can be found in section \ref{supporting-cycle-detection}. scheme can be found in section \ref{supporting-cycle-detection}.
This field is inherited by subtypes together with \member{tp_clear} This field is inherited by subtypes together with \member{tp_traverse}
and the \constant{Py_TPFLAGS_HAVE_GC} flag bit: the flag bit, and the \constant{Py_TPFLAGS_HAVE_GC} flag bit: the flag bit,
\member{tp_traverse}, and \member{tp_clear} are all inherited from \member{tp_traverse}, and \member{tp_clear} are all inherited from
the base type if they are all zero in the subtype \emph{and} the the base type if they are all zero in the subtype \emph{and} the

View File

@ -5,5 +5,5 @@
Email: \email{docs@python.org} Email: \email{docs@python.org}
} }
\date{3rd August, 2006} % XXX update before final release! \date{\today} % XXX update before final release!
\input{patchlevel} % include Python version information \input{patchlevel} % include Python version information

2
Doc/dist/dist.tex vendored
View File

@ -1992,6 +1992,7 @@ If the \module{foo} extension belongs in the root package, the setup
script for this could be script for this could be
\begin{verbatim} \begin{verbatim}
from distutils.core import setup from distutils.core import setup
from distutils.extension import Extension
setup(name='foobar', setup(name='foobar',
version='1.0', version='1.0',
ext_modules=[Extension('foo', ['foo.c'])], ext_modules=[Extension('foo', ['foo.c'])],
@ -2006,6 +2007,7 @@ the \module{foopkg} package simply by changing the name of the
extension: extension:
\begin{verbatim} \begin{verbatim}
from distutils.core import setup from distutils.core import setup
from distutils.extension import Extension
setup(name='foobar', setup(name='foobar',
version='1.0', version='1.0',
ext_modules=[Extension('foopkg.foo', ['foo.c'])], ext_modules=[Extension('foopkg.foo', ['foo.c'])],

View File

@ -199,8 +199,13 @@ WindowsError: exception: access violation reading 0x00000020
There are, however, enough ways to crash Python with \code{ctypes}, so There are, however, enough ways to crash Python with \code{ctypes}, so
you should be careful anyway. you should be careful anyway.
Python integers, strings and unicode strings are the only objects that \code{None}, integers, longs, byte strings and unicode strings are the
can directly be used as parameters in these function calls. only native Python objects that can directly be used as parameters in
these function calls. \code{None} is passed as a C \code{NULL} pointer,
byte strings and unicode strings are passed as pointer to the memory
block that contains their data (\code{char *} or \code{wchar{\_}t *}). Python
integers and Python longs are passed as the platforms default C
\code{int} type, their value is masked to fit into the C type.
Before we move on calling functions with other parameter types, we Before we move on calling functions with other parameter types, we
have to learn more about \code{ctypes} data types. have to learn more about \code{ctypes} data types.
@ -227,7 +232,18 @@ Python type
\code{char} \code{char}
} }
{ {
character 1-character
string
}
\lineiii{
\class{c{\_}wchar}
}
{
\code{wchar{\_}t}
}
{
1-character
unicode string
} }
\lineiii{ \lineiii{
\class{c{\_}byte} \class{c{\_}byte}
@ -236,7 +252,7 @@ character
\code{char} \code{char}
} }
{ {
integer int/long
} }
\lineiii{ \lineiii{
\class{c{\_}ubyte} \class{c{\_}ubyte}
@ -245,7 +261,7 @@ integer
\code{unsigned char} \code{unsigned char}
} }
{ {
integer int/long
} }
\lineiii{ \lineiii{
\class{c{\_}short} \class{c{\_}short}
@ -254,7 +270,7 @@ integer
\code{short} \code{short}
} }
{ {
integer int/long
} }
\lineiii{ \lineiii{
\class{c{\_}ushort} \class{c{\_}ushort}
@ -263,7 +279,7 @@ integer
\code{unsigned short} \code{unsigned short}
} }
{ {
integer int/long
} }
\lineiii{ \lineiii{
\class{c{\_}int} \class{c{\_}int}
@ -272,7 +288,7 @@ integer
\code{int} \code{int}
} }
{ {
integer int/long
} }
\lineiii{ \lineiii{
\class{c{\_}uint} \class{c{\_}uint}
@ -281,7 +297,7 @@ integer
\code{unsigned int} \code{unsigned int}
} }
{ {
integer int/long
} }
\lineiii{ \lineiii{
\class{c{\_}long} \class{c{\_}long}
@ -290,7 +306,7 @@ integer
\code{long} \code{long}
} }
{ {
integer int/long
} }
\lineiii{ \lineiii{
\class{c{\_}ulong} \class{c{\_}ulong}
@ -299,7 +315,7 @@ integer
\code{unsigned long} \code{unsigned long}
} }
{ {
long int/long
} }
\lineiii{ \lineiii{
\class{c{\_}longlong} \class{c{\_}longlong}
@ -309,7 +325,7 @@ long
\code{long long} \code{long long}
} }
{ {
long int/long
} }
\lineiii{ \lineiii{
\class{c{\_}ulonglong} \class{c{\_}ulonglong}
@ -319,7 +335,7 @@ long
\code{unsigned long long} \code{unsigned long long}
} }
{ {
long int/long
} }
\lineiii{ \lineiii{
\class{c{\_}float} \class{c{\_}float}
@ -368,8 +384,8 @@ unicode or
\code{void *} \code{void *}
} }
{ {
integer or int/long
\code{None} or \code{None}
} }
\end{tableiii} \end{tableiii}
\end{quote} \end{quote}
@ -554,11 +570,11 @@ the \member{{\_}as{\_}parameter{\_}} attribute.
\subsubsection{Return types\label{ctypes-return-types}} \subsubsection{Return types\label{ctypes-return-types}}
By default functions are assumed to return integers. Other return By default functions are assumed to return the C \code{int} type. Other
types can be specified by setting the \member{restype} attribute of the return types can be specified by setting the \member{restype} attribute of
function object. the function object.
Here is a more advanced example, it uses the strchr function, which Here is a more advanced example, it uses the \code{strchr} function, which
expects a string pointer and a char, and returns a pointer to a expects a string pointer and a char, and returns a pointer to a
string: string:
\begin{verbatim} \begin{verbatim}
@ -1611,8 +1627,8 @@ library object is available:
\begin{datadescni}{pythonapi} \begin{datadescni}{pythonapi}
An instance of \class{PyDLL} that exposes Python C api functions as An instance of \class{PyDLL} that exposes Python C api functions as
attributes. Note that all these functions are assumed to return attributes. Note that all these functions are assumed to return C
integers, which is of course not always the truth, so you have to \code{int}, which is of course not always the truth, so you have to
assign the correct \member{restype} attribute to use these functions. assign the correct \member{restype} attribute to use these functions.
\end{datadescni} \end{datadescni}
@ -1642,8 +1658,8 @@ function. Use \code{None} for \code{void} a function not returning
anything. anything.
It is possible to assign a callable Python object that is not a It is possible to assign a callable Python object that is not a
ctypes type, in this case the function is assumed to return an ctypes type, in this case the function is assumed to return a
integer, and the callable will be called with this integer, C \code{int}, and the callable will be called with this integer,
allowing to do further processing or error checking. Using this allowing to do further processing or error checking. Using this
is deprecated, for more flexible postprocessing or error checking is deprecated, for more flexible postprocessing or error checking
use a ctypes data type as \member{restype} and assign a callable to the use a ctypes data type as \member{restype} and assign a callable to the
@ -2283,9 +2299,12 @@ Windows only: Represents a \class{HRESULT} value, which contains success
or error information for a function or method call. or error information for a function or method call.
\end{classdesc*} \end{classdesc*}
\begin{classdesc*}{py_object} \code{py{\_}object} : classdesc*
Represents the C \code{PyObject *} datatype. \begin{quote}
\end{classdesc*}
Represents the C \code{PyObject *} datatype. Calling this with an
without an argument creates a \code{NULL} \code{PyObject *} pointer.
\end{quote}
The \code{ctypes.wintypes} module provides quite some other Windows The \code{ctypes.wintypes} module provides quite some other Windows
specific data types, for example \code{HWND}, \code{WPARAM}, or \code{DWORD}. specific data types, for example \code{HWND}, \code{WPARAM}, or \code{DWORD}.
@ -2324,9 +2343,9 @@ A sequence defining the structure fields. The items must be
the second item specifies the type of the field; it can be any the second item specifies the type of the field; it can be any
ctypes data type. ctypes data type.
For integer type fields, a third optional item can be given. It For integer type fields like \class{c{\_}int}, a third optional item can
must be a small positive integer defining the bit width of the be given. It must be a small positive integer defining the bit
field. width of the field.
Field names must be unique within one structure or union. This is Field names must be unique within one structure or union. This is
not checked, only one field can be accessed when names are not checked, only one field can be accessed when names are

View File

@ -451,6 +451,11 @@ Base class for warnings about probable mistakes in module imports.
\versionadded{2.5} \versionadded{2.5}
\end{excdesc} \end{excdesc}
\begin{excdesc}{UnicodeWarning}
Base class for warnings related to Unicode.
\versionadded{2.5}
\end{excdesc}
The class hierarchy for built-in exceptions is: The class hierarchy for built-in exceptions is:
\verbatiminput{../../Lib/test/exception_hierarchy.txt} \verbatiminput{../../Lib/test/exception_hierarchy.txt}

View File

@ -76,6 +76,9 @@ features that will be deprecated in the future (ignored by default).}
\lineii{ImportWarning}{Base category for warnings triggered during the \lineii{ImportWarning}{Base category for warnings triggered during the
process of importing a module (ignored by default).} process of importing a module (ignored by default).}
\lineii{UnicodeWarning}{Base category for warnings related to Unicode.}
\end{tableii} \end{tableii}
While these are technically built-in exceptions, they are documented While these are technically built-in exceptions, they are documented

View File

@ -5,7 +5,7 @@
% Fix XXX comments % Fix XXX comments
\title{What's New in Python 2.5} \title{What's New in Python 2.5}
\release{0.9} \release{1.0}
\author{A.M. Kuchling} \author{A.M. Kuchling}
\authoraddress{\email{amk@amk.ca}} \authoraddress{\email{amk@amk.ca}}
@ -40,15 +40,14 @@ Python's semantics.
As well as the language and library additions, other improvements and As well as the language and library additions, other improvements and
bugfixes were made throughout the source tree. A search through the bugfixes were made throughout the source tree. A search through the
SVN change logs finds there were 334 patches applied and 443 bugs SVN change logs finds there were 353 patches applied and 458 bugs
fixed between Python 2.4 and 2.5. (Both figures are likely to be fixed between Python 2.4 and 2.5. (Both figures are likely to be
underestimates.) underestimates.)
This article doesn't try to be a complete specification of the new This article doesn't try to be a complete specification of the new
features; instead changes are briefly introduced using helpful features; instead changes are briefly introduced using helpful
examples. For full details, you should always refer to the examples. For full details, you should always refer to the
documentation for Python 2.5. documentation for Python 2.5 at \url{http://docs.python.org}.
% XXX add hyperlink when the documentation becomes available online.
If you want to understand the complete implementation and design If you want to understand the complete implementation and design
rationale, refer to the PEP for a particular new feature. rationale, refer to the PEP for a particular new feature.
@ -751,7 +750,6 @@ either committed, meaning that all the changes are written into the
database, or rolled back, meaning that the changes are all discarded database, or rolled back, meaning that the changes are all discarded
and the database is unchanged. See any database textbook for more and the database is unchanged. See any database textbook for more
information.) information.)
% XXX find a shorter reference?
Let's assume there's an object representing a database connection. Let's assume there's an object representing a database connection.
Our goal will be to let the user write code like this: Our goal will be to let the user write code like this:
@ -1184,6 +1182,35 @@ a line like this near the top of the source file:
# -*- coding: latin1 -*- # -*- coding: latin1 -*-
\end{verbatim} \end{verbatim}
\item A new warning, \class{UnicodeWarning}, is triggered when
you attempt to compare a Unicode string and an 8-bit string
that can't be converted to Unicode using the default ASCII encoding.
The result of the comparison is false:
\begin{verbatim}
>>> chr(128) == unichr(128) # Can't convert chr(128) to Unicode
__main__:1: UnicodeWarning: Unicode equal comparison failed
to convert both arguments to Unicode - interpreting them
as being unequal
False
>>> chr(127) == unichr(127) # chr(127) can be converted
True
\end{verbatim}
Previously this would raise a \class{UnicodeDecodeError} exception,
but in 2.5 this could result in puzzling problems when accessing a
dictionary. If you looked up \code{unichr(128)} and \code{chr(128)}
was being used as a key, you'd get a \class{UnicodeDecodeError}
exception. Other changes in 2.5 resulted in this exception being
raised instead of suppressed by the code in \file{dictobject.c} that
implements dictionaries.
Raising an exception for such a comparison is strictly correct, but
the change might have broken code, so instead
\class{UnicodeWarning} was introduced.
(Implemented by Marc-Andr\'e Lemburg.)
\item One error that Python programmers sometimes make is forgetting \item One error that Python programmers sometimes make is forgetting
to include an \file{__init__.py} module in a package directory. to include an \file{__init__.py} module in a package directory.
Debugging this mistake can be confusing, and usually requires running Debugging this mistake can be confusing, and usually requires running
@ -1305,9 +1332,6 @@ they exist or not so that the interpreter makes fewer
\end{itemize} \end{itemize}
The net result of the 2.5 optimizations is that Python 2.5 runs the
pystone benchmark around XXX\% faster than Python 2.4.
%====================================================================== %======================================================================
\section{New, Improved, and Removed Modules\label{modules}} \section{New, Improved, and Removed Modules\label{modules}}
@ -2423,6 +2447,11 @@ was always a frame object. Because of the \pep{342} changes
described in section~\ref{pep-342}, it's now possible described in section~\ref{pep-342}, it's now possible
for \member{gi_frame} to be \code{None}. for \member{gi_frame} to be \code{None}.
\item A new warning, \class{UnicodeWarning}, is triggered when
you attempt to compare a Unicode string and an 8-bit string that can't
be converted to Unicode using the default ASCII encoding. Previously
such comparisons would raise a \class{UnicodeDecodeError} exception.
\item Library: the \module{csv} module is now stricter about multi-line quoted \item Library: the \module{csv} module is now stricter about multi-line quoted
fields. If your files contain newlines embedded within fields, the fields. If your files contain newlines embedded within fields, the
input should be split into lines in a manner which preserves the input should be split into lines in a manner which preserves the

View File

@ -694,13 +694,26 @@ xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx*/
expression: o1|o2. expression: o1|o2.
*/ */
PyAPI_FUNC(Py_ssize_t) PyNumber_Index(PyObject *); #define PyIndex_Check(obj) \
((obj)->ob_type->tp_as_number != NULL && \
(obj)->ob_type->tp_as_number->nb_index != NULL)
PyAPI_FUNC(PyObject *) PyNumber_Index(PyObject *o);
/* /*
Returns the object converted to Py_ssize_t on success Returns the object converted to a Python long or int
or -1 with an error raised on failure. or NULL with an error raised on failure.
*/ */
PyAPI_FUNC(Py_ssize_t) PyNumber_AsSsize_t(PyObject *o, PyObject *exc);
/*
Returns the object converted to Py_ssize_t by going through
PyNumber_Index first. If an overflow error occurs while
converting the int-or-long to Py_ssize_t, then the second argument
is the error-type to return. If it is NULL, then the overflow error
is cleared and the value is clipped.
*/
PyAPI_FUNC(PyObject *) PyNumber_Int(PyObject *o); PyAPI_FUNC(PyObject *) PyNumber_Int(PyObject *o);

View File

@ -88,6 +88,9 @@ typedef struct _addr_pair {
PyAPI_FUNC(int) PyCode_CheckLineNumber(PyCodeObject* co, PyAPI_FUNC(int) PyCode_CheckLineNumber(PyCodeObject* co,
int lasti, PyAddrPair *bounds); int lasti, PyAddrPair *bounds);
PyAPI_FUNC(PyObject*) PyCode_Optimize(PyObject *code, PyObject* consts,
PyObject *names, PyObject *lineno_obj);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View File

@ -22,7 +22,7 @@ PyAPI_FUNC(PyObject *) PyImport_ImportModuleLevel(char *name,
PyAPI_FUNC(PyObject *) PyImport_ImportModuleEx( PyAPI_FUNC(PyObject *) PyImport_ImportModuleEx(
char *name, PyObject *globals, PyObject *locals, PyObject *fromlist); char *name, PyObject *globals, PyObject *locals, PyObject *fromlist);
#define PyImport_ImportModuleEx(n, g, l, f) \ #define PyImport_ImportModuleEx(n, g, l, f) \
PyImport_ImportModuleLevel(n, g, l, f, -1); PyImport_ImportModuleLevel(n, g, l, f, -1)
PyAPI_FUNC(PyObject *) PyImport_Import(PyObject *name); PyAPI_FUNC(PyObject *) PyImport_Import(PyObject *name);
PyAPI_FUNC(PyObject *) PyImport_ReloadModule(PyObject *m); PyAPI_FUNC(PyObject *) PyImport_ReloadModule(PyObject *m);

View File

@ -190,7 +190,7 @@ typedef struct {
binaryfunc nb_inplace_floor_divide; binaryfunc nb_inplace_floor_divide;
binaryfunc nb_inplace_true_divide; binaryfunc nb_inplace_true_divide;
lenfunc nb_index; unaryfunc nb_index;
} PyNumberMethods; } PyNumberMethods;
typedef struct { typedef struct {

View File

@ -166,6 +166,7 @@ PyAPI_DATA(PyObject *) PyExc_SyntaxWarning;
PyAPI_DATA(PyObject *) PyExc_RuntimeWarning; PyAPI_DATA(PyObject *) PyExc_RuntimeWarning;
PyAPI_DATA(PyObject *) PyExc_FutureWarning; PyAPI_DATA(PyObject *) PyExc_FutureWarning;
PyAPI_DATA(PyObject *) PyExc_ImportWarning; PyAPI_DATA(PyObject *) PyExc_ImportWarning;
PyAPI_DATA(PyObject *) PyExc_UnicodeWarning;
/* Convenience functions */ /* Convenience functions */

View File

@ -189,6 +189,7 @@ typedef PY_UNICODE_TYPE Py_UNICODE;
# define PyUnicode_RSplit PyUnicodeUCS2_RSplit # define PyUnicode_RSplit PyUnicodeUCS2_RSplit
# define PyUnicode_Replace PyUnicodeUCS2_Replace # define PyUnicode_Replace PyUnicodeUCS2_Replace
# define PyUnicode_Resize PyUnicodeUCS2_Resize # define PyUnicode_Resize PyUnicodeUCS2_Resize
# define PyUnicode_RichCompare PyUnicodeUCS2_RichCompare
# define PyUnicode_SetDefaultEncoding PyUnicodeUCS2_SetDefaultEncoding # define PyUnicode_SetDefaultEncoding PyUnicodeUCS2_SetDefaultEncoding
# define PyUnicode_Split PyUnicodeUCS2_Split # define PyUnicode_Split PyUnicodeUCS2_Split
# define PyUnicode_Splitlines PyUnicodeUCS2_Splitlines # define PyUnicode_Splitlines PyUnicodeUCS2_Splitlines
@ -266,6 +267,7 @@ typedef PY_UNICODE_TYPE Py_UNICODE;
# define PyUnicode_RSplit PyUnicodeUCS4_RSplit # define PyUnicode_RSplit PyUnicodeUCS4_RSplit
# define PyUnicode_Replace PyUnicodeUCS4_Replace # define PyUnicode_Replace PyUnicodeUCS4_Replace
# define PyUnicode_Resize PyUnicodeUCS4_Resize # define PyUnicode_Resize PyUnicodeUCS4_Resize
# define PyUnicode_RichCompare PyUnicodeUCS4_RichCompare
# define PyUnicode_SetDefaultEncoding PyUnicodeUCS4_SetDefaultEncoding # define PyUnicode_SetDefaultEncoding PyUnicodeUCS4_SetDefaultEncoding
# define PyUnicode_Split PyUnicodeUCS4_Split # define PyUnicode_Split PyUnicodeUCS4_Split
# define PyUnicode_Splitlines PyUnicodeUCS4_Splitlines # define PyUnicode_Splitlines PyUnicodeUCS4_Splitlines
@ -1139,6 +1141,28 @@ PyAPI_FUNC(int) PyUnicode_Compare(
PyObject *right /* Right string */ PyObject *right /* Right string */
); );
/* Rich compare two strings and return one of the following:
- NULL in case an exception was raised
- Py_True or Py_False for successfuly comparisons
- Py_NotImplemented in case the type combination is unknown
Note that Py_EQ and Py_NE comparisons can cause a UnicodeWarning in
case the conversion of the arguments to Unicode fails with a
UnicodeDecodeError.
Possible values for op:
Py_GT, Py_GE, Py_EQ, Py_NE, Py_LT, Py_LE
*/
PyAPI_FUNC(PyObject *) PyUnicode_RichCompare(
PyObject *left, /* Left string */
PyObject *right, /* Right string */
int op /* Operation: Py_EQ, Py_NE, Py_GT, etc. */
);
/* Apply a argument tuple or dictionary to a format string and return /* Apply a argument tuple or dictionary to a format string and return
the resulting Unicode string. */ the resulting Unicode string. */

View File

@ -305,8 +305,10 @@ _Translator = {
'\375' : '\\375', '\376' : '\\376', '\377' : '\\377' '\375' : '\\375', '\376' : '\\376', '\377' : '\\377'
} }
_idmap = ''.join(chr(x) for x in xrange(256))
def _quote(str, LegalChars=_LegalChars, def _quote(str, LegalChars=_LegalChars,
idmap=string._idmap, translate=string.translate): idmap=_idmap, translate=string.translate):
# #
# If the string does not need to be double-quoted, # If the string does not need to be double-quoted,
# then just return the string. Otherwise, surround # then just return the string. Otherwise, surround
@ -440,7 +442,7 @@ class Morsel(dict):
def set(self, key, val, coded_val, def set(self, key, val, coded_val,
LegalChars=_LegalChars, LegalChars=_LegalChars,
idmap=string._idmap, translate=string.translate ): idmap=_idmap, translate=string.translate):
# First we verify that the key isn't a reserved word # First we verify that the key isn't a reserved word
# Second we make sure it only contains legal characters # Second we make sure it only contains legal characters
if key.lower() in self._reserved: if key.lower() in self._reserved:

View File

@ -251,6 +251,10 @@ def parse_multipart(fp, pdict):
XXX This should really be subsumed by FieldStorage altogether -- no XXX This should really be subsumed by FieldStorage altogether -- no
point in having two implementations of the same parsing algorithm. point in having two implementations of the same parsing algorithm.
Also, FieldStorage protects itself better against certain DoS attacks
by limiting the size of the data read in one chunk. The API here
does not support that kind of protection. This also affects parse()
since it can call parse_multipart().
""" """
boundary = "" boundary = ""
@ -691,7 +695,7 @@ class FieldStorage:
def read_lines_to_eof(self): def read_lines_to_eof(self):
"""Internal: read lines until EOF.""" """Internal: read lines until EOF."""
while 1: while 1:
line = self.fp.readline() line = self.fp.readline(1<<16)
if not line: if not line:
self.done = -1 self.done = -1
break break
@ -702,12 +706,13 @@ class FieldStorage:
next = "--" + self.outerboundary next = "--" + self.outerboundary
last = next + "--" last = next + "--"
delim = "" delim = ""
last_line_lfend = True
while 1: while 1:
line = self.fp.readline() line = self.fp.readline(1<<16)
if not line: if not line:
self.done = -1 self.done = -1
break break
if line[:2] == "--": if line[:2] == "--" and last_line_lfend:
strippedline = line.strip() strippedline = line.strip()
if strippedline == next: if strippedline == next:
break break
@ -718,11 +723,14 @@ class FieldStorage:
if line[-2:] == "\r\n": if line[-2:] == "\r\n":
delim = "\r\n" delim = "\r\n"
line = line[:-2] line = line[:-2]
last_line_lfend = True
elif line[-1] == "\n": elif line[-1] == "\n":
delim = "\n" delim = "\n"
line = line[:-1] line = line[:-1]
last_line_lfend = True
else: else:
delim = "" delim = ""
last_line_lfend = False
self.__write(odelim + line) self.__write(odelim + line)
def skip_lines(self): def skip_lines(self):
@ -731,18 +739,20 @@ class FieldStorage:
return return
next = "--" + self.outerboundary next = "--" + self.outerboundary
last = next + "--" last = next + "--"
last_line_lfend = True
while 1: while 1:
line = self.fp.readline() line = self.fp.readline(1<<16)
if not line: if not line:
self.done = -1 self.done = -1
break break
if line[:2] == "--": if line[:2] == "--" and last_line_lfend:
strippedline = line.strip() strippedline = line.strip()
if strippedline == next: if strippedline == next:
break break
if strippedline == last: if strippedline == last:
self.done = 1 self.done = 1
break break
last_line_lfend = line.endswith('\n')
def make_file(self, binary=None): def make_file(self, binary=None):
"""Overridable: return a readable & writable file. """Overridable: return a readable & writable file.

View File

@ -583,11 +583,9 @@ class GenExpr(Node):
def __init__(self, code, lineno=None): def __init__(self, code, lineno=None):
self.code = code self.code = code
self.lineno = lineno self.lineno = lineno
self.argnames = ['[outmost-iterable]'] self.argnames = ['.0']
self.varargs = self.kwargs = None self.varargs = self.kwargs = None
def getChildren(self): def getChildren(self):
return self.code, return self.code,

View File

@ -656,18 +656,19 @@ class CodeGenerator:
stack = [] stack = []
for i, for_ in zip(range(len(node.quals)), node.quals): for i, for_ in zip(range(len(node.quals)), node.quals):
start, anchor = self.visit(for_) start, anchor, end = self.visit(for_)
cont = None cont = None
for if_ in for_.ifs: for if_ in for_.ifs:
if cont is None: if cont is None:
cont = self.newBlock() cont = self.newBlock()
self.visit(if_, cont) self.visit(if_, cont)
stack.insert(0, (start, cont, anchor)) stack.insert(0, (start, cont, anchor, end))
self.visit(node.expr) self.visit(node.expr)
self.emit('YIELD_VALUE') self.emit('YIELD_VALUE')
self.emit('POP_TOP')
for start, cont, anchor in stack: for start, cont, anchor, end in stack:
if cont: if cont:
skip_one = self.newBlock() skip_one = self.newBlock()
self.emit('JUMP_FORWARD', skip_one) self.emit('JUMP_FORWARD', skip_one)
@ -676,14 +677,22 @@ class CodeGenerator:
self.nextBlock(skip_one) self.nextBlock(skip_one)
self.emit('JUMP_ABSOLUTE', start) self.emit('JUMP_ABSOLUTE', start)
self.startBlock(anchor) self.startBlock(anchor)
self.emit('POP_BLOCK')
self.setups.pop()
self.startBlock(end)
self.emit('LOAD_CONST', None) self.emit('LOAD_CONST', None)
def visitGenExprFor(self, node): def visitGenExprFor(self, node):
start = self.newBlock() start = self.newBlock()
anchor = self.newBlock() anchor = self.newBlock()
end = self.newBlock()
self.setups.push((LOOP, start))
self.emit('SETUP_LOOP', end)
if node.is_outmost: if node.is_outmost:
self.loadName('[outmost-iterable]') self.loadName('.0')
else: else:
self.visit(node.iter) self.visit(node.iter)
self.emit('GET_ITER') self.emit('GET_ITER')
@ -693,7 +702,7 @@ class CodeGenerator:
self.emit('FOR_ITER', anchor) self.emit('FOR_ITER', anchor)
self.nextBlock() self.nextBlock()
self.visit(node.assign) self.visit(node.assign)
return start, anchor return start, anchor, end
def visitGenExprIf(self, node, branch): def visitGenExprIf(self, node, branch):
self.set_lineno(node, force=True) self.set_lineno(node, force=True)

View File

@ -188,7 +188,7 @@ class GenExprScope(Scope):
i = self.__counter i = self.__counter
self.__counter += 1 self.__counter += 1
self.__super_init("generator expression<%d>"%i, module, klass) self.__super_init("generator expression<%d>"%i, module, klass)
self.add_param('[outmost-iterable]') self.add_param('.0')
def get_names(self): def get_names(self):
keys = Scope.get_names(self) keys = Scope.get_names(self)

View File

@ -5,7 +5,7 @@
import os as _os, sys as _sys import os as _os, sys as _sys
__version__ = "1.0.0" __version__ = "1.0.1"
from _ctypes import Union, Structure, Array from _ctypes import Union, Structure, Array
from _ctypes import _Pointer from _ctypes import _Pointer
@ -135,6 +135,11 @@ from _ctypes import _SimpleCData
class py_object(_SimpleCData): class py_object(_SimpleCData):
_type_ = "O" _type_ = "O"
def __repr__(self):
try:
return super(py_object, self).__repr__()
except ValueError:
return "%s(<NULL>)" % type(self).__name__
class c_short(_SimpleCData): class c_short(_SimpleCData):
_type_ = "h" _type_ = "h"

View File

@ -0,0 +1,215 @@
import unittest
from ctypes import *
import _ctypes_test
dll = CDLL(_ctypes_test.__file__)
try:
CALLBACK_FUNCTYPE = WINFUNCTYPE
except NameError:
# fake to enable this test on Linux
CALLBACK_FUNCTYPE = CFUNCTYPE
class POINT(Structure):
_fields_ = [("x", c_int), ("y", c_int)]
class BasicWrapTestCase(unittest.TestCase):
def wrap(self, param):
return param
def test_wchar_parm(self):
try:
c_wchar
except NameError:
return
f = dll._testfunc_i_bhilfd
f.argtypes = [c_byte, c_wchar, c_int, c_long, c_float, c_double]
result = f(self.wrap(1), self.wrap(u"x"), self.wrap(3), self.wrap(4), self.wrap(5.0), self.wrap(6.0))
self.failUnlessEqual(result, 139)
self.failUnless(type(result), int)
def test_pointers(self):
f = dll._testfunc_p_p
f.restype = POINTER(c_int)
f.argtypes = [POINTER(c_int)]
# This only works if the value c_int(42) passed to the
# function is still alive while the pointer (the result) is
# used.
v = c_int(42)
self.failUnlessEqual(pointer(v).contents.value, 42)
result = f(self.wrap(pointer(v)))
self.failUnlessEqual(type(result), POINTER(c_int))
self.failUnlessEqual(result.contents.value, 42)
# This on works...
result = f(self.wrap(pointer(v)))
self.failUnlessEqual(result.contents.value, v.value)
p = pointer(c_int(99))
result = f(self.wrap(p))
self.failUnlessEqual(result.contents.value, 99)
def test_shorts(self):
f = dll._testfunc_callback_i_if
args = []
expected = [262144, 131072, 65536, 32768, 16384, 8192, 4096, 2048,
1024, 512, 256, 128, 64, 32, 16, 8, 4, 2, 1]
def callback(v):
args.append(v)
return v
CallBack = CFUNCTYPE(c_int, c_int)
cb = CallBack(callback)
f(self.wrap(2**18), self.wrap(cb))
self.failUnlessEqual(args, expected)
################################################################
def test_callbacks(self):
f = dll._testfunc_callback_i_if
f.restype = c_int
MyCallback = CFUNCTYPE(c_int, c_int)
def callback(value):
#print "called back with", value
return value
cb = MyCallback(callback)
result = f(self.wrap(-10), self.wrap(cb))
self.failUnlessEqual(result, -18)
# test with prototype
f.argtypes = [c_int, MyCallback]
cb = MyCallback(callback)
result = f(self.wrap(-10), self.wrap(cb))
self.failUnlessEqual(result, -18)
result = f(self.wrap(-10), self.wrap(cb))
self.failUnlessEqual(result, -18)
AnotherCallback = CALLBACK_FUNCTYPE(c_int, c_int, c_int, c_int, c_int)
# check that the prototype works: we call f with wrong
# argument types
cb = AnotherCallback(callback)
self.assertRaises(ArgumentError, f, self.wrap(-10), self.wrap(cb))
def test_callbacks_2(self):
# Can also use simple datatypes as argument type specifiers
# for the callback function.
# In this case the call receives an instance of that type
f = dll._testfunc_callback_i_if
f.restype = c_int
MyCallback = CFUNCTYPE(c_int, c_int)
f.argtypes = [c_int, MyCallback]
def callback(value):
#print "called back with", value
self.failUnlessEqual(type(value), int)
return value
cb = MyCallback(callback)
result = f(self.wrap(-10), self.wrap(cb))
self.failUnlessEqual(result, -18)
def test_longlong_callbacks(self):
f = dll._testfunc_callback_q_qf
f.restype = c_longlong
MyCallback = CFUNCTYPE(c_longlong, c_longlong)
f.argtypes = [c_longlong, MyCallback]
def callback(value):
self.failUnless(isinstance(value, (int, long)))
return value & 0x7FFFFFFF
cb = MyCallback(callback)
self.failUnlessEqual(13577625587, int(f(self.wrap(1000000000000), self.wrap(cb))))
def test_byval(self):
# without prototype
ptin = POINT(1, 2)
ptout = POINT()
# EXPORT int _testfunc_byval(point in, point *pout)
result = dll._testfunc_byval(ptin, byref(ptout))
got = result, ptout.x, ptout.y
expected = 3, 1, 2
self.failUnlessEqual(got, expected)
# with prototype
ptin = POINT(101, 102)
ptout = POINT()
dll._testfunc_byval.argtypes = (POINT, POINTER(POINT))
dll._testfunc_byval.restype = c_int
result = dll._testfunc_byval(self.wrap(ptin), byref(ptout))
got = result, ptout.x, ptout.y
expected = 203, 101, 102
self.failUnlessEqual(got, expected)
def test_struct_return_2H(self):
class S2H(Structure):
_fields_ = [("x", c_short),
("y", c_short)]
dll.ret_2h_func.restype = S2H
dll.ret_2h_func.argtypes = [S2H]
inp = S2H(99, 88)
s2h = dll.ret_2h_func(self.wrap(inp))
self.failUnlessEqual((s2h.x, s2h.y), (99*2, 88*3))
def test_struct_return_8H(self):
class S8I(Structure):
_fields_ = [("a", c_int),
("b", c_int),
("c", c_int),
("d", c_int),
("e", c_int),
("f", c_int),
("g", c_int),
("h", c_int)]
dll.ret_8i_func.restype = S8I
dll.ret_8i_func.argtypes = [S8I]
inp = S8I(9, 8, 7, 6, 5, 4, 3, 2)
s8i = dll.ret_8i_func(self.wrap(inp))
self.failUnlessEqual((s8i.a, s8i.b, s8i.c, s8i.d, s8i.e, s8i.f, s8i.g, s8i.h),
(9*2, 8*3, 7*4, 6*5, 5*6, 4*7, 3*8, 2*9))
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
class AsParamWrapper(object):
def __init__(self, param):
self._as_parameter_ = param
class AsParamWrapperTestCase(BasicWrapTestCase):
wrap = AsParamWrapper
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
class AsParamPropertyWrapper(object):
def __init__(self, param):
self._param = param
def getParameter(self):
return self._param
_as_parameter_ = property(getParameter)
class AsParamPropertyWrapperTestCase(BasicWrapTestCase):
wrap = AsParamPropertyWrapper
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
if __name__ == '__main__':
unittest.main()

View File

@ -222,6 +222,7 @@ class FunctionTestCase(unittest.TestCase):
def callback(v): def callback(v):
args.append(v) args.append(v)
return v
CallBack = CFUNCTYPE(c_int, c_int) CallBack = CFUNCTYPE(c_int, c_int)

View File

@ -19,7 +19,7 @@ def valid_ranges(*types):
result.append((min(a, b, c, d), max(a, b, c, d))) result.append((min(a, b, c, d), max(a, b, c, d)))
return result return result
ArgType = type(c_int(0)._as_parameter_) ArgType = type(byref(c_int(0)))
unsigned_types = [c_ubyte, c_ushort, c_uint, c_ulong] unsigned_types = [c_ubyte, c_ushort, c_uint, c_ulong]
signed_types = [c_byte, c_short, c_int, c_long, c_longlong] signed_types = [c_byte, c_short, c_int, c_long, c_longlong]
@ -80,19 +80,6 @@ class NumberTestCase(unittest.TestCase):
for t in signed_types + unsigned_types + float_types: for t in signed_types + unsigned_types + float_types:
self.failUnlessEqual(ArgType, type(t.from_param(0))) self.failUnlessEqual(ArgType, type(t.from_param(0)))
def test_as_parameter(self):
# The _as_parameter_ property must also
# be a PyCArgObject instance
for t in signed_types + unsigned_types + float_types:
parm = t()._as_parameter_
self.failUnlessEqual(ArgType, type(parm))
# _as_parameter_ is readonly!
#
# Python 2.3 and 2.4 raise a TypeError when trying to set
# a readonly attribute, 2.5 raises an AttributeError.
self.assertRaises((AttributeError, TypeError), setattr, t(), "_as_parameter_", None)
def test_byref(self): def test_byref(self):
# calling byref returns also a PyCArgObject instance # calling byref returns also a PyCArgObject instance
for t in signed_types + unsigned_types + float_types: for t in signed_types + unsigned_types + float_types:

View File

@ -125,13 +125,18 @@ class CharPointersTestCase(unittest.TestCase):
self.failUnlessEqual(None, func(c_wchar_p(None))) self.failUnlessEqual(None, func(c_wchar_p(None)))
self.failUnlessEqual(u"123", func(c_wchar_p(u"123"))) self.failUnlessEqual(u"123", func(c_wchar_p(u"123")))
## def test_instance(self): def test_instance(self):
## func = testdll._testfunc_p_p func = testdll._testfunc_p_p
func.restype = c_void_p
## class X: class X:
## _as_parameter_ = 0 _as_parameter_ = None
## self.failUnlessEqual(0, func(X())) func.argtypes = c_void_p,
self.failUnlessEqual(None, func(X()))
func.argtypes = None
self.failUnlessEqual(None, func(X()))
try: try:
c_wchar c_wchar

View File

@ -78,5 +78,10 @@ class PythonAPITestCase(unittest.TestCase):
# not enough arguments # not enough arguments
self.failUnlessRaises(TypeError, PyOS_snprintf, buf) self.failUnlessRaises(TypeError, PyOS_snprintf, buf)
def test_pyobject_repr(self):
self.failUnlessEqual(repr(py_object()), "py_object(<NULL>)")
self.failUnlessEqual(repr(py_object(42)), "py_object(42)")
self.failUnlessEqual(repr(py_object(object)), "py_object(%r)" % object)
if __name__ == "__main__": if __name__ == "__main__":
unittest.main() unittest.main()

View File

@ -12,6 +12,12 @@ used from a setup script as
__revision__ = "$Id$" __revision__ = "$Id$"
import sys # Distutils version
__version__ = "%d.%d.%d" % sys.version_info[:3] #
del sys # Please coordinate with Marc-Andre Lemburg <mal@egenix.com> when adding
# new features to distutils that would warrant bumping the version number.
#
# In general, major and minor version should loosely follow the Python
# version number the distutils code was shipped with.
#
__version__ = "2.5.0"

View File

@ -22,9 +22,9 @@ menudefs = [
None, None,
('_Save', '<<save-window>>'), ('_Save', '<<save-window>>'),
('Save _As...', '<<save-window-as-file>>'), ('Save _As...', '<<save-window-as-file>>'),
('Save Co_py As...', '<<save-copy-of-window-as-file>>'), ('Save Cop_y As...', '<<save-copy-of-window-as-file>>'),
None, None,
('_Print Window', '<<print-window>>'), ('Prin_t Window', '<<print-window>>'),
None, None,
('_Close', '<<close-window>>'), ('_Close', '<<close-window>>'),
('E_xit', '<<close-all-windows>>'), ('E_xit', '<<close-all-windows>>'),

View File

@ -24,8 +24,8 @@ Noam Raphael (Code Context, Call Tips, many other patches), and Chui Tey (RPC
integration, debugger integration and persistent breakpoints). integration, debugger integration and persistent breakpoints).
Scott David Daniels, Tal Einat, Hernan Foffani, Christos Georgiou, Scott David Daniels, Tal Einat, Hernan Foffani, Christos Georgiou,
Martin v. Löwis, Jason Orendorff, Josh Robb, Nigel Rowe, Bruce Sherwood, Jim Jewett, Martin v. Löwis, Jason Orendorff, Josh Robb, Nigel Rowe,
and Jeff Shute have submitted useful patches. Thanks, guys! Bruce Sherwood, and Jeff Shute have submitted useful patches. Thanks, guys!
For additional details refer to NEWS.txt and Changelog. For additional details refer to NEWS.txt and Changelog.

View File

@ -15,7 +15,7 @@ import re
from sys import maxint as INFINITY from sys import maxint as INFINITY
BLOCKOPENERS = set(["class", "def", "elif", "else", "except", "finally", "for", BLOCKOPENERS = set(["class", "def", "elif", "else", "except", "finally", "for",
"if", "try", "while"]) "if", "try", "while", "with"])
UPDATEINTERVAL = 100 # millisec UPDATEINTERVAL = 100 # millisec
FONTUPDATEINTERVAL = 1000 # millisec FONTUPDATEINTERVAL = 1000 # millisec

View File

@ -1,7 +1,29 @@
What's New in IDLE 2.6a1?
=========================
*Release date: XX-XXX-200X*
- IDLE's version number takes a big jump to match the version number of
the Python release of which it's a part.
What's New in IDLE 1.2c1? What's New in IDLE 1.2c1?
========================= =========================
*Release date: XX-AUG-2006* *Release date: 17-AUG-2006*
- File menu hotkeys: there were three 'p' assignments. Reassign the
'Save Copy As' and 'Print' hotkeys to 'y' and 't'. Change the
Shell hotkey from 's' to 'l'.
- IDLE honors new quit() and exit() commands from site.py Quitter() object.
Patch 1540892, Jim Jewett
- The 'with' statement is now a Code Context block opener.
Patch 1540851, Jim Jewett
- Retrieval of previous shell command was not always preserving indentation
(since 1.2a1) Patch 1528468 Tal Einat.
- Changing tokenize (39046) to detect dedent broke tabnanny check (since 1.2a1) - Changing tokenize (39046) to detect dedent broke tabnanny check (since 1.2a1)

View File

@ -478,9 +478,6 @@ class ModifiedInterpreter(InteractiveInterpreter):
import sys as _sys import sys as _sys
_sys.path = %r _sys.path = %r
del _sys del _sys
_msg = 'Use File/Exit or your end-of-file key to quit IDLE'
__builtins__.quit = __builtins__.exit = _msg
del _msg
\n""" % (sys.path,)) \n""" % (sys.path,))
active_seq = None active_seq = None
@ -514,7 +511,10 @@ class ModifiedInterpreter(InteractiveInterpreter):
print >>sys.__stderr__, errmsg, what print >>sys.__stderr__, errmsg, what
print >>console, errmsg, what print >>console, errmsg, what
# we received a response to the currently active seq number: # we received a response to the currently active seq number:
self.tkconsole.endexecuting() try:
self.tkconsole.endexecuting()
except AttributeError: # shell may have closed
pass
# Reschedule myself # Reschedule myself
if not self.tkconsole.closing: if not self.tkconsole.closing:
self.tkconsole.text.after(self.tkconsole.pollinterval, self.tkconsole.text.after(self.tkconsole.pollinterval,
@ -593,7 +593,7 @@ class ModifiedInterpreter(InteractiveInterpreter):
source = source.encode(IOBinding.encoding) source = source.encode(IOBinding.encoding)
except UnicodeError: except UnicodeError:
self.tkconsole.resetoutput() self.tkconsole.resetoutput()
self.write("Unsupported characters in input") self.write("Unsupported characters in input\n")
return return
try: try:
# InteractiveInterpreter.runsource() calls its runcode() method, # InteractiveInterpreter.runsource() calls its runcode() method,
@ -713,14 +713,17 @@ class ModifiedInterpreter(InteractiveInterpreter):
else: else:
exec code in self.locals exec code in self.locals
except SystemExit: except SystemExit:
if tkMessageBox.askyesno( if not self.tkconsole.closing:
"Exit?", if tkMessageBox.askyesno(
"Do you want to exit altogether?", "Exit?",
default="yes", "Do you want to exit altogether?",
master=self.tkconsole.text): default="yes",
raise master=self.tkconsole.text):
raise
else:
self.showtraceback()
else: else:
self.showtraceback() raise
except: except:
if use_subprocess: if use_subprocess:
print >> self.tkconsole.stderr, \ print >> self.tkconsole.stderr, \
@ -730,7 +733,10 @@ class ModifiedInterpreter(InteractiveInterpreter):
self.tkconsole.endexecuting() self.tkconsole.endexecuting()
finally: finally:
if not use_subprocess: if not use_subprocess:
self.tkconsole.endexecuting() try:
self.tkconsole.endexecuting()
except AttributeError: # shell may have closed
pass
def write(self, s): def write(self, s):
"Override base class method" "Override base class method"
@ -794,7 +800,7 @@ class PyShell(OutputWindow):
if use_subprocess: if use_subprocess:
ms = self.menu_specs ms = self.menu_specs
if ms[2][0] != "shell": if ms[2][0] != "shell":
ms.insert(2, ("shell", "_Shell")) ms.insert(2, ("shell", "She_ll"))
self.interp = ModifiedInterpreter(self) self.interp = ModifiedInterpreter(self)
if flist is None: if flist is None:
root = Tk() root = Tk()
@ -804,9 +810,6 @@ class PyShell(OutputWindow):
# #
OutputWindow.__init__(self, flist, None, None) OutputWindow.__init__(self, flist, None, None)
# #
import __builtin__
__builtin__.quit = __builtin__.exit = "To exit, type Ctrl-D."
#
## self.config(usetabs=1, indentwidth=8, context_use_ps1=1) ## self.config(usetabs=1, indentwidth=8, context_use_ps1=1)
self.usetabs = True self.usetabs = True
# indentwidth must be 8 when using tabs. See note in EditorWindow: # indentwidth must be 8 when using tabs. See note in EditorWindow:
@ -1138,21 +1141,27 @@ class PyShell(OutputWindow):
return "break" return "break"
def recall(self, s, event): def recall(self, s, event):
# remove leading and trailing empty or whitespace lines
s = re.sub(r'^\s*\n', '' , s)
s = re.sub(r'\n\s*$', '', s)
lines = s.split('\n')
self.text.undo_block_start() self.text.undo_block_start()
try: try:
self.text.tag_remove("sel", "1.0", "end") self.text.tag_remove("sel", "1.0", "end")
self.text.mark_set("insert", "end-1c") self.text.mark_set("insert", "end-1c")
s = s.strip() prefix = self.text.get("insert linestart", "insert")
lines = s.split('\n') if prefix.rstrip().endswith(':'):
prefix = self.text.get("insert linestart","insert").rstrip()
if prefix and prefix[-1]==':':
self.newline_and_indent_event(event) self.newline_and_indent_event(event)
self.text.insert("insert",lines[0].strip()) prefix = self.text.get("insert linestart", "insert")
self.text.insert("insert", lines[0].strip())
if len(lines) > 1: if len(lines) > 1:
self.newline_and_indent_event(event) orig_base_indent = re.search(r'^([ \t]*)', lines[0]).group(0)
new_base_indent = re.search(r'^([ \t]*)', prefix).group(0)
for line in lines[1:]: for line in lines[1:]:
self.text.insert("insert", line.strip()) if line.startswith(orig_base_indent):
self.newline_and_indent_event(event) # replace orig base indentation with new indentation
line = new_base_indent + line[len(orig_base_indent):]
self.text.insert('insert', '\n'+line.rstrip())
finally: finally:
self.text.see("insert") self.text.see("insert")
self.text.undo_block_stop() self.text.undo_block_stop()

View File

@ -1 +1 @@
IDLE_VERSION = "1.2b3" IDLE_VERSION = "2.6a0"

View File

@ -1337,14 +1337,14 @@ def disable(level):
""" """
root.manager.disable = level root.manager.disable = level
def shutdown(): def shutdown(handlerList=_handlerList):
""" """
Perform any cleanup actions in the logging system (e.g. flushing Perform any cleanup actions in the logging system (e.g. flushing
buffers). buffers).
Should be called at application exit. Should be called at application exit.
""" """
for h in _handlerList[:]: # was _handlers.keys(): for h in handlerList[:]:
#errors might occur, for example, if files are locked #errors might occur, for example, if files are locked
#we just ignore them if raiseExceptions is not set #we just ignore them if raiseExceptions is not set
try: try:

View File

@ -79,7 +79,7 @@ def fileConfig(fname, defaults=None):
logging._acquireLock() logging._acquireLock()
try: try:
logging._handlers.clear() logging._handlers.clear()
logging._handlerList = [] del logging._handlerList[:]
# Handlers add themselves to logging._handlers # Handlers add themselves to logging._handlers
handlers = _install_handlers(cp, formatters) handlers = _install_handlers(cp, formatters)
_install_loggers(cp, handlers) _install_loggers(cp, handlers)

View File

@ -27,7 +27,7 @@ sys.path more than once. Blank lines and lines beginning with
'#' are skipped. Lines starting with 'import' are executed. '#' are skipped. Lines starting with 'import' are executed.
For example, suppose sys.prefix and sys.exec_prefix are set to For example, suppose sys.prefix and sys.exec_prefix are set to
/usr/local and there is a directory /usr/local/lib/python1.5/site-packages /usr/local and there is a directory /usr/local/lib/python2.5/site-packages
with three subdirectories, foo, bar and spam, and two path with three subdirectories, foo, bar and spam, and two path
configuration files, foo.pth and bar.pth. Assume foo.pth contains the configuration files, foo.pth and bar.pth. Assume foo.pth contains the
following: following:
@ -44,8 +44,8 @@ and bar.pth contains:
Then the following directories are added to sys.path, in this order: Then the following directories are added to sys.path, in this order:
/usr/local/lib/python1.5/site-packages/bar /usr/local/lib/python2.5/site-packages/bar
/usr/local/lib/python1.5/site-packages/foo /usr/local/lib/python2.5/site-packages/foo
Note that bletch is omitted because it doesn't exist; bar precedes foo Note that bletch is omitted because it doesn't exist; bar precedes foo
because bar.pth comes alphabetically before foo.pth; and spam is because bar.pth comes alphabetically before foo.pth; and spam is
@ -242,6 +242,12 @@ def setquit():
def __repr__(self): def __repr__(self):
return 'Use %s() or %s to exit' % (self.name, eof) return 'Use %s() or %s to exit' % (self.name, eof)
def __call__(self, code=None): def __call__(self, code=None):
# Shells like IDLE catch the SystemExit, but listen when their
# stdin wrapper is closed.
try:
sys.stdin.close()
except:
pass
raise SystemExit(code) raise SystemExit(code)
__builtin__.quit = Quitter('quit') __builtin__.quit = Quitter('quit')
__builtin__.exit = Quitter('exit') __builtin__.exit = Quitter('exit')

View File

@ -35,7 +35,6 @@ printable = digits + letters + punctuation + whitespace
# Case conversion helpers # Case conversion helpers
# Use str to convert Unicode literal in case of -U # Use str to convert Unicode literal in case of -U
# Note that Cookie.py bogusly uses _idmap :(
l = map(chr, xrange(256)) l = map(chr, xrange(256))
_idmap = str('').join(l) _idmap = str('').join(l)
del l del l

View File

@ -109,6 +109,10 @@ def check(file):
errprint("%r: Token Error: %s" % (file, msg)) errprint("%r: Token Error: %s" % (file, msg))
return return
except IndentationError, msg:
errprint("%r: Indentation Error: %s" % (file, msg))
return
except NannyNag, nag: except NannyNag, nag:
badline = nag.get_lineno() badline = nag.get_lineno()
line = nag.get_line() line = nag.get_line()

View File

@ -411,9 +411,6 @@ class _Stream:
self.buf += self.cmp.flush() self.buf += self.cmp.flush()
if self.mode == "w" and self.buf: if self.mode == "w" and self.buf:
blocks, remainder = divmod(len(self.buf), self.bufsize)
if remainder > 0:
self.buf += NUL * (self.bufsize - remainder)
self.fileobj.write(self.buf) self.fileobj.write(self.buf)
self.buf = "" self.buf = ""
if self.comptype == "gz": if self.comptype == "gz":

View File

@ -45,3 +45,4 @@ BaseException
+-- UserWarning +-- UserWarning
+-- FutureWarning +-- FutureWarning
+-- ImportWarning +-- ImportWarning
+-- UnicodeWarning

View File

@ -88,3 +88,12 @@ extra85 = 'stop'
def func88(): def func88():
# comment # comment
return 90 return 90
# line 92
def f():
class X:
def g():
"doc"
return 42
return X
method_in_dynamic_class = f().g.im_func

View File

@ -38,3 +38,5 @@ test_cgi
Testing log Testing log
Testing initlog 1 Testing initlog 1
Testing log 2 Testing log 2
Test FieldStorage methods that use readline
Test basic FieldStorage multipart parsing

View File

@ -60,13 +60,13 @@ class BytesTest(unittest.TestCase):
self.assertRaises(ValueError, bytes, [-1]) self.assertRaises(ValueError, bytes, [-1])
self.assertRaises(ValueError, bytes, [-sys.maxint]) self.assertRaises(ValueError, bytes, [-sys.maxint])
self.assertRaises(ValueError, bytes, [-sys.maxint-1]) self.assertRaises(ValueError, bytes, [-sys.maxint-1])
self.assertRaises(ValueError, bytes, [-sys.maxint-2]) self.assertRaises(TypeError, bytes, [-sys.maxint-2])
self.assertRaises(ValueError, bytes, [-10**100]) self.assertRaises(TypeError, bytes, [-10**100])
self.assertRaises(ValueError, bytes, [256]) self.assertRaises(ValueError, bytes, [256])
self.assertRaises(ValueError, bytes, [257]) self.assertRaises(ValueError, bytes, [257])
self.assertRaises(ValueError, bytes, [sys.maxint]) self.assertRaises(ValueError, bytes, [sys.maxint])
self.assertRaises(ValueError, bytes, [sys.maxint+1]) self.assertRaises(TypeError, bytes, [sys.maxint+1])
self.assertRaises(ValueError, bytes, [10**100]) self.assertRaises(TypeError, bytes, [10**100])
def test_repr(self): def test_repr(self):
self.assertEqual(repr(bytes()), "bytes()") self.assertEqual(repr(bytes()), "bytes()")

View File

@ -158,10 +158,21 @@ class BZ2FileTest(BaseTest):
sio = StringIO(self.TEXT) sio = StringIO(self.TEXT)
bz2f.writelines(sio.readlines()) bz2f.writelines(sio.readlines())
bz2f.close() bz2f.close()
# patch #1535500
self.assertRaises(ValueError, bz2f.writelines, ["a"])
f = open(self.filename, 'rb') f = open(self.filename, 'rb')
self.assertEqual(self.decompress(f.read()), self.TEXT) self.assertEqual(self.decompress(f.read()), self.TEXT)
f.close() f.close()
def testWriteMethodsOnReadOnlyFile(self):
bz2f = BZ2File(self.filename, "w")
bz2f.write("abc")
bz2f.close()
bz2f = BZ2File(self.filename, "r")
self.assertRaises(IOError, bz2f.write, "a")
self.assertRaises(IOError, bz2f.writelines, ["a"])
def testSeekForward(self): def testSeekForward(self):
# "Test BZ2File.seek(150, 0)" # "Test BZ2File.seek(150, 0)"
self.createTempFile() self.createTempFile()

View File

@ -2,6 +2,8 @@ from test.test_support import verify, verbose
import cgi import cgi
import os import os
import sys import sys
import tempfile
from StringIO import StringIO
class HackedSysModule: class HackedSysModule:
# The regression test will have real values in sys.argv, which # The regression test will have real values in sys.argv, which
@ -203,4 +205,71 @@ def main():
cgi.initlog("%s", "Testing log 3") cgi.initlog("%s", "Testing log 3")
cgi.log("Testing log 4") cgi.log("Testing log 4")
print "Test FieldStorage methods that use readline"
# FieldStorage uses readline, which has the capacity to read all
# contents of the input file into memory; we use readline's size argument
# to prevent that for files that do not contain any newlines in
# non-GET/HEAD requests
class TestReadlineFile:
def __init__(self, file):
self.file = file
self.numcalls = 0
def readline(self, size=None):
self.numcalls += 1
if size:
return self.file.readline(size)
else:
return self.file.readline()
def __getattr__(self, name):
file = self.__dict__['file']
a = getattr(file, name)
if not isinstance(a, int):
setattr(self, name, a)
return a
f = TestReadlineFile(tempfile.TemporaryFile())
f.write('x' * 256 * 1024)
f.seek(0)
env = {'REQUEST_METHOD':'PUT'}
fs = cgi.FieldStorage(fp=f, environ=env)
# if we're not chunking properly, readline is only called twice
# (by read_binary); if we are chunking properly, it will be called 5 times
# as long as the chunksize is 1 << 16.
verify(f.numcalls > 2)
print "Test basic FieldStorage multipart parsing"
env = {'REQUEST_METHOD':'POST', 'CONTENT_TYPE':'multipart/form-data; boundary=---------------------------721837373350705526688164684', 'CONTENT_LENGTH':'558'}
postdata = """-----------------------------721837373350705526688164684
Content-Disposition: form-data; name="id"
1234
-----------------------------721837373350705526688164684
Content-Disposition: form-data; name="title"
-----------------------------721837373350705526688164684
Content-Disposition: form-data; name="file"; filename="test.txt"
Content-Type: text/plain
Testing 123.
-----------------------------721837373350705526688164684
Content-Disposition: form-data; name="submit"
Add\x20
-----------------------------721837373350705526688164684--
"""
fs = cgi.FieldStorage(fp=StringIO(postdata), environ=env)
verify(len(fs.list) == 4)
expect = [{'name':'id', 'filename':None, 'value':'1234'},
{'name':'title', 'filename':None, 'value':''},
{'name':'file', 'filename':'test.txt','value':'Testing 123.\n'},
{'name':'submit', 'filename':None, 'value':' Add '}]
for x in range(len(fs.list)):
for k, exp in expect[x].items():
got = getattr(fs.list[x], k)
verify(got == exp)
main() main()

View File

@ -116,6 +116,13 @@ class CompilerTest(unittest.TestCase):
exec c in dct exec c in dct
self.assertEquals(dct.get('result'), 3) self.assertEquals(dct.get('result'), 3)
def testGenExp(self):
c = compiler.compile('list((i,j) for i in range(3) if i < 3'
' for j in range(4) if j > 2)',
'<string>',
'eval')
self.assertEquals(eval(c), [(0, 3), (1, 3), (2, 3)])
NOLINENO = (compiler.ast.Module, compiler.ast.Stmt, compiler.ast.Discard) NOLINENO = (compiler.ast.Module, compiler.ast.Stmt, compiler.ast.Discard)

View File

@ -230,6 +230,14 @@ test_exc(u'no format', '1', TypeError,
test_exc(u'no format', u'1', TypeError, test_exc(u'no format', u'1', TypeError,
"not all arguments converted during string formatting") "not all arguments converted during string formatting")
class Foobar(long):
def __oct__(self):
# Returning a non-string should not blow up.
return self + 1
test_exc('%o', Foobar(), TypeError,
"expected string or Unicode object, long found")
if sys.maxint == 2**31-1: if sys.maxint == 2**31-1:
# crashes 2.2.1 and earlier: # crashes 2.2.1 and earlier:
try: try:

View File

@ -1,6 +1,7 @@
import unittest import unittest
from test import test_support from test import test_support
import operator import operator
from sys import maxint
class oldstyle: class oldstyle:
def __index__(self): def __index__(self):
@ -10,68 +11,116 @@ class newstyle(object):
def __index__(self): def __index__(self):
return self.ind return self.ind
class TrapInt(int):
def __index__(self):
return self
class TrapLong(long):
def __index__(self):
return self
class BaseTestCase(unittest.TestCase): class BaseTestCase(unittest.TestCase):
def setUp(self):
self.o = oldstyle()
self.n = newstyle()
def test_basic(self):
self.o.ind = -2
self.n.ind = 2
self.assertEqual(operator.index(self.o), -2)
self.assertEqual(operator.index(self.n), 2)
def test_slice(self):
self.o.ind = 1
self.n.ind = 2
slc = slice(self.o, self.o, self.o)
check_slc = slice(1, 1, 1)
self.assertEqual(slc.indices(self.o), check_slc.indices(1))
slc = slice(self.n, self.n, self.n)
check_slc = slice(2, 2, 2)
self.assertEqual(slc.indices(self.n), check_slc.indices(2))
def test_wrappers(self):
self.o.ind = 4
self.n.ind = 5
self.assertEqual(6 .__index__(), 6)
self.assertEqual(-7L.__index__(), -7)
self.assertEqual(self.o.__index__(), 4)
self.assertEqual(self.n.__index__(), 5)
def test_subclasses(self):
r = range(10)
self.assertEqual(r[TrapInt(5):TrapInt(10)], r[5:10])
self.assertEqual(r[TrapLong(5):TrapLong(10)], r[5:10])
self.assertEqual(slice(TrapInt()).indices(0), (0,0,1))
self.assertEqual(slice(TrapLong(0)).indices(0), (0,0,1))
def test_error(self):
self.o.ind = 'dumb'
self.n.ind = 'bad'
self.failUnlessRaises(TypeError, operator.index, self.o)
self.failUnlessRaises(TypeError, operator.index, self.n)
self.failUnlessRaises(TypeError, slice(self.o).indices, 0)
self.failUnlessRaises(TypeError, slice(self.n).indices, 0)
class SeqTestCase(unittest.TestCase):
# This test case isn't run directly. It just defines common tests
# to the different sequence types below
def setUp(self): def setUp(self):
self.o = oldstyle() self.o = oldstyle()
self.n = newstyle() self.n = newstyle()
self.o2 = oldstyle() self.o2 = oldstyle()
self.n2 = newstyle() self.n2 = newstyle()
def test_basic(self): def test_index(self):
self.o.ind = -2 self.o.ind = -2
self.n.ind = 2 self.n.ind = 2
assert(self.seq[self.n] == self.seq[2]) self.assertEqual(self.seq[self.n], self.seq[2])
assert(self.seq[self.o] == self.seq[-2]) self.assertEqual(self.seq[self.o], self.seq[-2])
assert(operator.index(self.o) == -2)
assert(operator.index(self.n) == 2)
def test_error(self):
self.o.ind = 'dumb'
self.n.ind = 'bad'
myfunc = lambda x, obj: obj.seq[x]
self.failUnlessRaises(TypeError, operator.index, self.o)
self.failUnlessRaises(TypeError, operator.index, self.n)
self.failUnlessRaises(TypeError, myfunc, self.o, self)
self.failUnlessRaises(TypeError, myfunc, self.n, self)
def test_slice(self): def test_slice(self):
self.o.ind = 1 self.o.ind = 1
self.o2.ind = 3 self.o2.ind = 3
self.n.ind = 2 self.n.ind = 2
self.n2.ind = 4 self.n2.ind = 4
assert(self.seq[self.o:self.o2] == self.seq[1:3]) self.assertEqual(self.seq[self.o:self.o2], self.seq[1:3])
assert(self.seq[self.n:self.n2] == self.seq[2:4]) self.assertEqual(self.seq[self.n:self.n2], self.seq[2:4])
def test_repeat(self): def test_repeat(self):
self.o.ind = 3 self.o.ind = 3
self.n.ind = 2 self.n.ind = 2
assert(self.seq * self.o == self.seq * 3) self.assertEqual(self.seq * self.o, self.seq * 3)
assert(self.seq * self.n == self.seq * 2) self.assertEqual(self.seq * self.n, self.seq * 2)
assert(self.o * self.seq == self.seq * 3) self.assertEqual(self.o * self.seq, self.seq * 3)
assert(self.n * self.seq == self.seq * 2) self.assertEqual(self.n * self.seq, self.seq * 2)
def test_wrappers(self): def test_wrappers(self):
n = self.n self.o.ind = 4
n.ind = 5 self.n.ind = 5
assert n.__index__() == 5 self.assertEqual(self.seq.__getitem__(self.o), self.seq[4])
assert 6 .__index__() == 6 self.assertEqual(self.seq.__mul__(self.o), self.seq * 4)
assert -7L.__index__() == -7 self.assertEqual(self.seq.__rmul__(self.o), self.seq * 4)
assert self.seq.__getitem__(n) == self.seq[5] self.assertEqual(self.seq.__getitem__(self.n), self.seq[5])
assert self.seq.__mul__(n) == self.seq * 5 self.assertEqual(self.seq.__mul__(self.n), self.seq * 5)
assert self.seq.__rmul__(n) == self.seq * 5 self.assertEqual(self.seq.__rmul__(self.n), self.seq * 5)
def test_infinite_recusion(self): def test_subclasses(self):
class Trap1(int): self.assertEqual(self.seq[TrapInt()], self.seq[0])
def __index__(self): self.assertEqual(self.seq[TrapLong()], self.seq[0])
return self
class Trap2(long): def test_error(self):
def __index__(self): self.o.ind = 'dumb'
return self self.n.ind = 'bad'
self.failUnlessRaises(TypeError, operator.getitem, self.seq, Trap1()) indexobj = lambda x, obj: obj.seq[x]
self.failUnlessRaises(TypeError, operator.getitem, self.seq, Trap2()) self.failUnlessRaises(TypeError, indexobj, self.o, self)
self.failUnlessRaises(TypeError, indexobj, self.n, self)
sliceobj = lambda x, obj: obj.seq[x:]
self.failUnlessRaises(TypeError, sliceobj, self.o, self)
self.failUnlessRaises(TypeError, sliceobj, self.n, self)
class ListTestCase(BaseTestCase): class ListTestCase(SeqTestCase):
seq = [0,10,20,30,40,50] seq = [0,10,20,30,40,50]
def test_setdelitem(self): def test_setdelitem(self):
@ -82,36 +131,36 @@ class ListTestCase(BaseTestCase):
del lst[self.n] del lst[self.n]
lst[self.o] = 'X' lst[self.o] = 'X'
lst[self.n] = 'Y' lst[self.n] = 'Y'
assert lst == list('abYdefghXj') self.assertEqual(lst, list('abYdefghXj'))
lst = [5, 6, 7, 8, 9, 10, 11] lst = [5, 6, 7, 8, 9, 10, 11]
lst.__setitem__(self.n, "here") lst.__setitem__(self.n, "here")
assert lst == [5, 6, "here", 8, 9, 10, 11] self.assertEqual(lst, [5, 6, "here", 8, 9, 10, 11])
lst.__delitem__(self.n) lst.__delitem__(self.n)
assert lst == [5, 6, 8, 9, 10, 11] self.assertEqual(lst, [5, 6, 8, 9, 10, 11])
def test_inplace_repeat(self): def test_inplace_repeat(self):
self.o.ind = 2 self.o.ind = 2
self.n.ind = 3 self.n.ind = 3
lst = [6, 4] lst = [6, 4]
lst *= self.o lst *= self.o
assert lst == [6, 4, 6, 4] self.assertEqual(lst, [6, 4, 6, 4])
lst *= self.n lst *= self.n
assert lst == [6, 4, 6, 4] * 3 self.assertEqual(lst, [6, 4, 6, 4] * 3)
lst = [5, 6, 7, 8, 9, 11] lst = [5, 6, 7, 8, 9, 11]
l2 = lst.__imul__(self.n) l2 = lst.__imul__(self.n)
assert l2 is lst self.assert_(l2 is lst)
assert lst == [5, 6, 7, 8, 9, 11] * 3 self.assertEqual(lst, [5, 6, 7, 8, 9, 11] * 3)
class TupleTestCase(BaseTestCase): class TupleTestCase(SeqTestCase):
seq = (0,10,20,30,40,50) seq = (0,10,20,30,40,50)
class StringTestCase(BaseTestCase): class StringTestCase(SeqTestCase):
seq = "this is a test" seq = "this is a test"
class UnicodeTestCase(BaseTestCase): class UnicodeTestCase(SeqTestCase):
seq = u"this is a test" seq = u"this is a test"
@ -120,17 +169,54 @@ class XRangeTestCase(unittest.TestCase):
def test_xrange(self): def test_xrange(self):
n = newstyle() n = newstyle()
n.ind = 5 n.ind = 5
assert xrange(1, 20)[n] == 6 self.assertEqual(xrange(1, 20)[n], 6)
assert xrange(1, 20).__getitem__(n) == 6 self.assertEqual(xrange(1, 20).__getitem__(n), 6)
class OverflowTestCase(unittest.TestCase):
def setUp(self):
self.pos = 2**100
self.neg = -self.pos
def test_large_longs(self):
self.assertEqual(self.pos.__index__(), self.pos)
self.assertEqual(self.neg.__index__(), self.neg)
def _getitem_helper(self, base):
class GetItem(base):
def __len__(self):
return maxint
def __getitem__(self, key):
return key
def __getslice__(self, i, j):
return i, j
x = GetItem()
self.assertEqual(x[self.pos], self.pos)
self.assertEqual(x[self.neg], self.neg)
self.assertEqual(x[self.neg:self.pos], (-1, maxint))
self.assertEqual(x[self.neg:self.pos:1].indices(maxint), (0, maxint, 1))
def test_getitem(self):
self._getitem_helper(object)
def test_getitem_classic(self):
class Empty: pass
self._getitem_helper(Empty)
def test_sequence_repeat(self):
self.failUnlessRaises(OverflowError, lambda: "a" * self.pos)
self.failUnlessRaises(OverflowError, lambda: "a" * self.neg)
def test_main(): def test_main():
test_support.run_unittest( test_support.run_unittest(
BaseTestCase,
ListTestCase, ListTestCase,
TupleTestCase, TupleTestCase,
StringTestCase, StringTestCase,
UnicodeTestCase, UnicodeTestCase,
XRangeTestCase, XRangeTestCase,
OverflowTestCase,
) )
if __name__ == "__main__": if __name__ == "__main__":

View File

@ -276,6 +276,9 @@ class TestBuggyCases(GetSourceBase):
def test_with_comment_instead_of_docstring(self): def test_with_comment_instead_of_docstring(self):
self.assertSourceEqual(mod2.func88, 88, 90) self.assertSourceEqual(mod2.func88, 88, 90)
def test_method_in_dynamic_class(self):
self.assertSourceEqual(mod2.method_in_dynamic_class, 95, 97)
# Helper for testing classify_class_attrs. # Helper for testing classify_class_attrs.
def attrs_wo_objs(cls): def attrs_wo_objs(cls):
return [t[:3] for t in inspect.classify_class_attrs(cls)] return [t[:3] for t in inspect.classify_class_attrs(cls)]

View File

@ -493,7 +493,7 @@ def test4():
try: try:
logging._handlers.clear() logging._handlers.clear()
logging._handlers.update(saved_handlers) logging._handlers.update(saved_handlers)
logging._handlerList = saved_handler_list logging._handlerList[:] = saved_handler_list
loggerDict = logging.getLogger().manager.loggerDict loggerDict = logging.getLogger().manager.loggerDict
loggerDict.clear() loggerDict.clear()
loggerDict.update(saved_loggers) loggerDict.update(saved_loggers)
@ -560,7 +560,7 @@ def test5():
try: try:
logging._handlers.clear() logging._handlers.clear()
logging._handlers.update(saved_handlers) logging._handlers.update(saved_handlers)
logging._handlerList = saved_handler_list logging._handlerList[:] = saved_handler_list
loggerDict = logging.getLogger().manager.loggerDict loggerDict = logging.getLogger().manager.loggerDict
loggerDict.clear() loggerDict.clear()
loggerDict.update(saved_loggers) loggerDict.update(saved_loggers)

View File

@ -1,4 +1,5 @@
import os import os
import sys
import time import time
import stat import stat
import socket import socket
@ -454,7 +455,7 @@ class TestMaildir(TestMailbox):
def setUp(self): def setUp(self):
TestMailbox.setUp(self) TestMailbox.setUp(self)
if os.name in ('nt', 'os2'): if os.name in ('nt', 'os2') or sys.platform == 'cygwin':
self._box.colon = '!' self._box.colon = '!'
def test_add_MM(self): def test_add_MM(self):
@ -729,11 +730,13 @@ class _TestMboxMMDF(TestMailbox):
# In the parent, sleep a bit to give the child time to acquire # In the parent, sleep a bit to give the child time to acquire
# the lock. # the lock.
time.sleep(0.5) time.sleep(0.5)
self.assertRaises(mailbox.ExternalClashError, try:
self._box.lock) self.assertRaises(mailbox.ExternalClashError,
self._box.lock)
finally:
# Wait for child to exit. Locking should now succeed.
exited_pid, status = os.waitpid(pid, 0)
# Wait for child to exit. Locking should now succeed.
exited_pid, status = os.waitpid(pid, 0)
self._box.lock() self._box.lock()
self._box.unlock() self._box.unlock()

View File

@ -6,11 +6,16 @@ import os, sys, time
if sys.platform[:3] in ('win', 'os2') or sys.platform=='riscos': if sys.platform[:3] in ('win', 'os2') or sys.platform=='riscos':
raise TestSkipped, "Can't test signal on %s" % sys.platform raise TestSkipped, "Can't test signal on %s" % sys.platform
MAX_DURATION = 20 # Entire test should last at most 20 sec.
if verbose: if verbose:
x = '-x' x = '-x'
else: else:
x = '+x' x = '+x'
pid = os.getpid() pid = os.getpid()
if verbose:
print "test runner's pid is", pid
# Shell script that will send us asynchronous signals # Shell script that will send us asynchronous signals
script = """ script = """
@ -31,7 +36,7 @@ def handlerA(*args):
global a_called global a_called
a_called = True a_called = True
if verbose: if verbose:
print "handlerA", args print "handlerA invoked", args
class HandlerBCalled(Exception): class HandlerBCalled(Exception):
pass pass
@ -40,96 +45,102 @@ def handlerB(*args):
global b_called global b_called
b_called = True b_called = True
if verbose: if verbose:
print "handlerB", args print "handlerB invoked", args
raise HandlerBCalled, args raise HandlerBCalled, args
MAX_DURATION = 20 # Set up a child to send signals to us (the parent) after waiting long
signal.alarm(MAX_DURATION) # Entire test should last at most 20 sec. # enough to receive the alarm. It seems we miss the alarm for some
# reason. This will hopefully stop the hangs on Tru64/Alpha.
# Alas, it doesn't. Tru64 appears to miss all the signals at times, or
# seemingly random subsets of them, and nothing done in force_test_exit
# so far has actually helped.
def force_test_exit():
# Sigh, both imports seem necessary to avoid errors.
import os
fork_pid = os.fork()
if fork_pid:
# In parent.
return fork_pid
# In child.
import os, time
try:
# Wait 5 seconds longer than the expected alarm to give enough
# time for the normal sequence of events to occur. This is
# just a stop-gap to try to prevent the test from hanging.
time.sleep(MAX_DURATION + 5)
print >> sys.__stdout__, ' child should not have to kill parent'
for signame in "SIGHUP", "SIGUSR1", "SIGUSR2", "SIGALRM":
os.kill(pid, getattr(signal, signame))
print >> sys.__stdout__, " child sent", signame, "to", pid
time.sleep(1)
finally:
os._exit(0)
# Install handlers.
hup = signal.signal(signal.SIGHUP, handlerA) hup = signal.signal(signal.SIGHUP, handlerA)
usr1 = signal.signal(signal.SIGUSR1, handlerB) usr1 = signal.signal(signal.SIGUSR1, handlerB)
usr2 = signal.signal(signal.SIGUSR2, signal.SIG_IGN) usr2 = signal.signal(signal.SIGUSR2, signal.SIG_IGN)
alrm = signal.signal(signal.SIGALRM, signal.default_int_handler) alrm = signal.signal(signal.SIGALRM, signal.default_int_handler)
vereq(signal.getsignal(signal.SIGHUP), handlerA)
vereq(signal.getsignal(signal.SIGUSR1), handlerB)
vereq(signal.getsignal(signal.SIGUSR2), signal.SIG_IGN)
try: try:
signal.signal(4242, handlerB)
raise TestFailed, 'expected ValueError for invalid signal # to signal()'
except ValueError:
pass
try: signal.alarm(MAX_DURATION)
signal.getsignal(4242) vereq(signal.getsignal(signal.SIGHUP), handlerA)
raise TestFailed, 'expected ValueError for invalid signal # to getsignal()' vereq(signal.getsignal(signal.SIGUSR1), handlerB)
except ValueError: vereq(signal.getsignal(signal.SIGUSR2), signal.SIG_IGN)
pass vereq(signal.getsignal(signal.SIGALRM), signal.default_int_handler)
try:
signal.signal(signal.SIGUSR1, None)
raise TestFailed, 'expected TypeError for non-callable'
except TypeError:
pass
# Set up a child to send an alarm signal to us (the parent) after waiting
# long enough to receive the alarm. It seems we miss the alarm for some
# reason. This will hopefully stop the hangs on Tru64/Alpha.
def force_test_exit():
# Sigh, both imports seem necessary to avoid errors.
import os
fork_pid = os.fork()
if fork_pid == 0:
# In child
import os, time
try:
# Wait 5 seconds longer than the expected alarm to give enough
# time for the normal sequence of events to occur. This is
# just a stop-gap to prevent the test from hanging.
time.sleep(MAX_DURATION + 5)
print >> sys.__stdout__, ' child should not have to kill parent'
for i in range(3):
os.kill(pid, signal.SIGALARM)
finally:
os._exit(0)
# In parent (or error)
return fork_pid
try:
os.system(script)
# Try to ensure this test exits even if there is some problem with alarm. # Try to ensure this test exits even if there is some problem with alarm.
# Tru64/Alpha sometimes hangs and is ultimately killed by the buildbot. # Tru64/Alpha often hangs and is ultimately killed by the buildbot.
fork_pid = force_test_exit() fork_pid = force_test_exit()
print "starting pause() loop..."
try: try:
signal.getsignal(4242)
raise TestFailed('expected ValueError for invalid signal # to '
'getsignal()')
except ValueError:
pass
try:
signal.signal(4242, handlerB)
raise TestFailed('expected ValueError for invalid signal # to '
'signal()')
except ValueError:
pass
try:
signal.signal(signal.SIGUSR1, None)
raise TestFailed('expected TypeError for non-callable')
except TypeError:
pass
# Launch an external script to send us signals.
# We expect the external script to:
# send HUP, which invokes handlerA to set a_called
# send USR1, which invokes handlerB to set b_called and raise
# HandlerBCalled
# send USR2, which is ignored
#
# Then we expect the alarm to go off, and its handler raises
# KeyboardInterrupt, finally getting us out of the loop.
os.system(script)
try:
print "starting pause() loop..."
while 1: while 1:
if verbose:
print "call pause()..."
try: try:
if verbose:
print "call pause()..."
signal.pause() signal.pause()
if verbose: if verbose:
print "pause() returned" print "pause() returned"
except HandlerBCalled: except HandlerBCalled:
if verbose: if verbose:
print "HandlerBCalled exception caught" print "HandlerBCalled exception caught"
else:
pass
except KeyboardInterrupt: except KeyboardInterrupt:
if verbose: if verbose:
print "KeyboardInterrupt (assume the alarm() went off)" print "KeyboardInterrupt (the alarm() went off)"
# Forcibly kill the child we created to ping us if there was a test error.
try:
# Make sure we don't kill ourself if there was a fork error.
if fork_pid > 0:
os.kill(fork_pid, signal.SIGKILL)
except:
# If the child killed us, it has probably exited. Killing a
# non-existant process will raise an error which we don't care about.
pass
if not a_called: if not a_called:
print 'HandlerA not called' print 'HandlerA not called'
@ -138,6 +149,18 @@ try:
print 'HandlerB not called' print 'HandlerB not called'
finally: finally:
# Forcibly kill the child we created to ping us if there was a test error.
try:
# Make sure we don't kill ourself if there was a fork error.
if fork_pid > 0:
os.kill(fork_pid, signal.SIGKILL)
except:
# If the child killed us, it has probably exited. Killing a
# non-existent process will raise an error which we don't care about.
pass
# Restore handlers.
signal.alarm(0) # cancel alarm in case we died early
signal.signal(signal.SIGHUP, hup) signal.signal(signal.SIGHUP, hup)
signal.signal(signal.SIGUSR1, usr1) signal.signal(signal.SIGUSR1, usr1)
signal.signal(signal.SIGUSR2, usr2) signal.signal(signal.SIGUSR2, usr2)

View File

@ -181,10 +181,19 @@ else:
if hasattr(os, 'fork') and os.name not in ('os2',): if hasattr(os, 'fork') and os.name not in ('os2',):
dgramservers.append(ForkingUnixDatagramServer) dgramservers.append(ForkingUnixDatagramServer)
def sloppy_cleanup():
# See http://python.org/sf/1540386
# We need to reap children here otherwise a child from one server
# can be left running for the next server and cause a test failure.
time.sleep(DELAY)
reap_children()
def testall(): def testall():
testloop(socket.AF_INET, tcpservers, MyStreamHandler, teststream) testloop(socket.AF_INET, tcpservers, MyStreamHandler, teststream)
sloppy_cleanup()
testloop(socket.AF_INET, udpservers, MyDatagramHandler, testdgram) testloop(socket.AF_INET, udpservers, MyDatagramHandler, testdgram)
if hasattr(socket, 'AF_UNIX'): if hasattr(socket, 'AF_UNIX'):
sloppy_cleanup()
testloop(socket.AF_UNIX, streamservers, MyStreamHandler, teststream) testloop(socket.AF_UNIX, streamservers, MyStreamHandler, teststream)
# Alas, on Linux (at least) recvfrom() doesn't return a meaningful # Alas, on Linux (at least) recvfrom() doesn't return a meaningful
# client address so this cannot work: # client address so this cannot work:

View File

@ -324,6 +324,27 @@ class WriteSize0Test(BaseTest):
class WriteStreamTest(WriteTest): class WriteStreamTest(WriteTest):
sep = '|' sep = '|'
def test_padding(self):
self.dst.close()
if self.comp == "gz":
f = gzip.GzipFile(self.dstname)
s = f.read()
f.close()
elif self.comp == "bz2":
f = bz2.BZ2Decompressor()
s = file(self.dstname).read()
s = f.decompress(s)
self.assertEqual(len(f.unused_data), 0, "trailing data")
else:
f = file(self.dstname)
s = f.read()
f.close()
self.assertEqual(s.count("\0"), tarfile.RECORDSIZE,
"incorrect zero padding")
class WriteGNULongTest(unittest.TestCase): class WriteGNULongTest(unittest.TestCase):
"""This testcase checks for correct creation of GNU Longname """This testcase checks for correct creation of GNU Longname
and Longlink extensions. and Longlink extensions.

View File

@ -131,6 +131,76 @@ class ThreadTests(unittest.TestCase):
threading._DummyThread)) threading._DummyThread))
del threading._active[tid] del threading._active[tid]
# PyThreadState_SetAsyncExc() is a CPython-only gimmick, not (currently)
# exposed at the Python level. This test relies on ctypes to get at it.
def test_PyThreadState_SetAsyncExc(self):
try:
import ctypes
except ImportError:
if verbose:
print "test_PyThreadState_SetAsyncExc can't import ctypes"
return # can't do anything
set_async_exc = ctypes.pythonapi.PyThreadState_SetAsyncExc
class AsyncExc(Exception):
pass
exception = ctypes.py_object(AsyncExc)
# `worker_started` is set by the thread when it's inside a try/except
# block waiting to catch the asynchronously set AsyncExc exception.
# `worker_saw_exception` is set by the thread upon catching that
# exception.
worker_started = threading.Event()
worker_saw_exception = threading.Event()
class Worker(threading.Thread):
def run(self):
self.id = thread.get_ident()
self.finished = False
try:
while True:
worker_started.set()
time.sleep(0.1)
except AsyncExc:
self.finished = True
worker_saw_exception.set()
t = Worker()
t.setDaemon(True) # so if this fails, we don't hang Python at shutdown
t.start()
if verbose:
print " started worker thread"
# Try a thread id that doesn't make sense.
if verbose:
print " trying nonsensical thread id"
result = set_async_exc(ctypes.c_long(-1), exception)
self.assertEqual(result, 0) # no thread states modified
# Now raise an exception in the worker thread.
if verbose:
print " waiting for worker thread to get started"
worker_started.wait()
if verbose:
print " verifying worker hasn't exited"
self.assert_(not t.finished)
if verbose:
print " attempting to raise asynch exception in worker"
result = set_async_exc(ctypes.c_long(t.id), exception)
self.assertEqual(result, 1) # one thread state modified
if verbose:
print " waiting for worker to say it caught the exception"
worker_saw_exception.wait(timeout=10)
self.assert_(t.finished)
if verbose:
print " all OK -- joining worker"
if t.finished:
t.join()
# else the thread is still running, and we have no way to kill it
def test_main(): def test_main():
test.test_support.run_unittest(ThreadTests) test.test_support.run_unittest(ThreadTests)

View File

@ -49,7 +49,7 @@ class ThreadSignals(unittest.TestCase):
# and might be out of order.) If we haven't seen # and might be out of order.) If we haven't seen
# the signals yet, send yet another signal and # the signals yet, send yet another signal and
# wait for it return. # wait for it return.
if signal_blackboard[signal.SIGUSR2]['tripped'] == 0 \ if signal_blackboard[signal.SIGUSR1]['tripped'] == 0 \
or signal_blackboard[signal.SIGUSR2]['tripped'] == 0: or signal_blackboard[signal.SIGUSR2]['tripped'] == 0:
signal.alarm(1) signal.alarm(1)
signal.pause() signal.pause()

View File

@ -46,6 +46,69 @@ class TrivialTests(unittest.TestCase):
self.assertEquals(urllib2.parse_http_list(string), list) self.assertEquals(urllib2.parse_http_list(string), list)
def test_request_headers_dict():
"""
The Request.headers dictionary is not a documented interface. It should
stay that way, because the complete set of headers are only accessible
through the .get_header(), .has_header(), .header_items() interface.
However, .headers pre-dates those methods, and so real code will be using
the dictionary.
The introduction in 2.4 of those methods was a mistake for the same reason:
code that previously saw all (urllib2 user)-provided headers in .headers
now sees only a subset (and the function interface is ugly and incomplete).
A better change would have been to replace .headers dict with a dict
subclass (or UserDict.DictMixin instance?) that preserved the .headers
interface and also provided access to the "unredirected" headers. It's
probably too late to fix that, though.
Check .capitalize() case normalization:
>>> url = "http://example.com"
>>> Request(url, headers={"Spam-eggs": "blah"}).headers["Spam-eggs"]
'blah'
>>> Request(url, headers={"spam-EggS": "blah"}).headers["Spam-eggs"]
'blah'
Currently, Request(url, "Spam-eggs").headers["Spam-Eggs"] raises KeyError,
but that could be changed in future.
"""
def test_request_headers_methods():
"""
Note the case normalization of header names here, to .capitalize()-case.
This should be preserved for backwards-compatibility. (In the HTTP case,
normalization to .title()-case is done by urllib2 before sending headers to
httplib).
>>> url = "http://example.com"
>>> r = Request(url, headers={"Spam-eggs": "blah"})
>>> r.has_header("Spam-eggs")
True
>>> r.header_items()
[('Spam-eggs', 'blah')]
>>> r.add_header("Foo-Bar", "baz")
>>> items = r.header_items()
>>> items.sort()
>>> items
[('Foo-bar', 'baz'), ('Spam-eggs', 'blah')]
Note that e.g. r.has_header("spam-EggS") is currently False, and
r.get_header("spam-EggS") returns None, but that could be changed in
future.
>>> r.has_header("Not-there")
False
>>> print r.get_header("Not-there")
None
>>> r.get_header("Not-there", "default")
'default'
"""
def test_password_manager(self): def test_password_manager(self):
""" """
>>> mgr = urllib2.HTTPPasswordMgr() >>> mgr = urllib2.HTTPPasswordMgr()
@ -676,11 +739,11 @@ class HandlerTests(unittest.TestCase):
r = MockResponse(200, "OK", {}, "") r = MockResponse(200, "OK", {}, "")
newreq = h.do_request_(req) newreq = h.do_request_(req)
if data is None: # GET if data is None: # GET
self.assert_("Content-Length" not in req.unredirected_hdrs) self.assert_("Content-length" not in req.unredirected_hdrs)
self.assert_("Content-Type" not in req.unredirected_hdrs) self.assert_("Content-type" not in req.unredirected_hdrs)
else: # POST else: # POST
self.assertEqual(req.unredirected_hdrs["Content-Length"], "0") self.assertEqual(req.unredirected_hdrs["Content-length"], "0")
self.assertEqual(req.unredirected_hdrs["Content-Type"], self.assertEqual(req.unredirected_hdrs["Content-type"],
"application/x-www-form-urlencoded") "application/x-www-form-urlencoded")
# XXX the details of Host could be better tested # XXX the details of Host could be better tested
self.assertEqual(req.unredirected_hdrs["Host"], "example.com") self.assertEqual(req.unredirected_hdrs["Host"], "example.com")
@ -692,8 +755,8 @@ class HandlerTests(unittest.TestCase):
req.add_unredirected_header("Host", "baz") req.add_unredirected_header("Host", "baz")
req.add_unredirected_header("Spam", "foo") req.add_unredirected_header("Spam", "foo")
newreq = h.do_request_(req) newreq = h.do_request_(req)
self.assertEqual(req.unredirected_hdrs["Content-Length"], "foo") self.assertEqual(req.unredirected_hdrs["Content-length"], "foo")
self.assertEqual(req.unredirected_hdrs["Content-Type"], "bar") self.assertEqual(req.unredirected_hdrs["Content-type"], "bar")
self.assertEqual(req.unredirected_hdrs["Host"], "baz") self.assertEqual(req.unredirected_hdrs["Host"], "baz")
self.assertEqual(req.unredirected_hdrs["Spam"], "foo") self.assertEqual(req.unredirected_hdrs["Spam"], "foo")
@ -847,7 +910,7 @@ class HandlerTests(unittest.TestCase):
407, 'Proxy-Authenticate: Basic realm="%s"\r\n\r\n' % realm) 407, 'Proxy-Authenticate: Basic realm="%s"\r\n\r\n' % realm)
opener.add_handler(auth_handler) opener.add_handler(auth_handler)
opener.add_handler(http_handler) opener.add_handler(http_handler)
self._test_basic_auth(opener, auth_handler, "Proxy-Authorization", self._test_basic_auth(opener, auth_handler, "Proxy-authorization",
realm, http_handler, password_manager, realm, http_handler, password_manager,
"http://acme.example.com:3128/protected", "http://acme.example.com:3128/protected",
"proxy.example.com:3128", "proxy.example.com:3128",

View File

@ -16,12 +16,13 @@ class TestUUID(TestCase):
def test_UUID(self): def test_UUID(self):
equal = self.assertEqual equal = self.assertEqual
ascending = [] ascending = []
for (string, curly, hex, bytes, fields, integer, urn, for (string, curly, hex, bytes, bytes_le, fields, integer, urn,
time, clock_seq, variant, version) in [ time, clock_seq, variant, version) in [
('00000000-0000-0000-0000-000000000000', ('00000000-0000-0000-0000-000000000000',
'{00000000-0000-0000-0000-000000000000}', '{00000000-0000-0000-0000-000000000000}',
'00000000000000000000000000000000', '00000000000000000000000000000000',
'\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0', '\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0',
'\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0',
(0, 0, 0, 0, 0, 0), (0, 0, 0, 0, 0, 0),
0, 0,
'urn:uuid:00000000-0000-0000-0000-000000000000', 'urn:uuid:00000000-0000-0000-0000-000000000000',
@ -30,6 +31,7 @@ class TestUUID(TestCase):
'{00010203-0405-0607-0809-0a0b0c0d0e0f}', '{00010203-0405-0607-0809-0a0b0c0d0e0f}',
'000102030405060708090a0b0c0d0e0f', '000102030405060708090a0b0c0d0e0f',
'\0\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\x0c\x0d\x0e\x0f', '\0\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\x0c\x0d\x0e\x0f',
'\x03\x02\x01\0\x05\x04\x07\x06\x08\t\n\x0b\x0c\x0d\x0e\x0f',
(0x00010203L, 0x0405, 0x0607, 8, 9, 0x0a0b0c0d0e0fL), (0x00010203L, 0x0405, 0x0607, 8, 9, 0x0a0b0c0d0e0fL),
0x000102030405060708090a0b0c0d0e0fL, 0x000102030405060708090a0b0c0d0e0fL,
'urn:uuid:00010203-0405-0607-0809-0a0b0c0d0e0f', 'urn:uuid:00010203-0405-0607-0809-0a0b0c0d0e0f',
@ -38,6 +40,7 @@ class TestUUID(TestCase):
'{02d9e6d5-9467-382e-8f9b-9300a64ac3cd}', '{02d9e6d5-9467-382e-8f9b-9300a64ac3cd}',
'02d9e6d59467382e8f9b9300a64ac3cd', '02d9e6d59467382e8f9b9300a64ac3cd',
'\x02\xd9\xe6\xd5\x94\x67\x38\x2e\x8f\x9b\x93\x00\xa6\x4a\xc3\xcd', '\x02\xd9\xe6\xd5\x94\x67\x38\x2e\x8f\x9b\x93\x00\xa6\x4a\xc3\xcd',
'\xd5\xe6\xd9\x02\x67\x94\x2e\x38\x8f\x9b\x93\x00\xa6\x4a\xc3\xcd',
(0x02d9e6d5L, 0x9467, 0x382e, 0x8f, 0x9b, 0x9300a64ac3cdL), (0x02d9e6d5L, 0x9467, 0x382e, 0x8f, 0x9b, 0x9300a64ac3cdL),
0x02d9e6d59467382e8f9b9300a64ac3cdL, 0x02d9e6d59467382e8f9b9300a64ac3cdL,
'urn:uuid:02d9e6d5-9467-382e-8f9b-9300a64ac3cd', 'urn:uuid:02d9e6d5-9467-382e-8f9b-9300a64ac3cd',
@ -46,6 +49,7 @@ class TestUUID(TestCase):
'{12345678-1234-5678-1234-567812345678}', '{12345678-1234-5678-1234-567812345678}',
'12345678123456781234567812345678', '12345678123456781234567812345678',
'\x12\x34\x56\x78'*4, '\x12\x34\x56\x78'*4,
'\x78\x56\x34\x12\x34\x12\x78\x56\x12\x34\x56\x78\x12\x34\x56\x78',
(0x12345678, 0x1234, 0x5678, 0x12, 0x34, 0x567812345678), (0x12345678, 0x1234, 0x5678, 0x12, 0x34, 0x567812345678),
0x12345678123456781234567812345678, 0x12345678123456781234567812345678,
'urn:uuid:12345678-1234-5678-1234-567812345678', 'urn:uuid:12345678-1234-5678-1234-567812345678',
@ -54,6 +58,7 @@ class TestUUID(TestCase):
'{6ba7b810-9dad-11d1-80b4-00c04fd430c8}', '{6ba7b810-9dad-11d1-80b4-00c04fd430c8}',
'6ba7b8109dad11d180b400c04fd430c8', '6ba7b8109dad11d180b400c04fd430c8',
'\x6b\xa7\xb8\x10\x9d\xad\x11\xd1\x80\xb4\x00\xc0\x4f\xd4\x30\xc8', '\x6b\xa7\xb8\x10\x9d\xad\x11\xd1\x80\xb4\x00\xc0\x4f\xd4\x30\xc8',
'\x10\xb8\xa7\x6b\xad\x9d\xd1\x11\x80\xb4\x00\xc0\x4f\xd4\x30\xc8',
(0x6ba7b810L, 0x9dad, 0x11d1, 0x80, 0xb4, 0x00c04fd430c8L), (0x6ba7b810L, 0x9dad, 0x11d1, 0x80, 0xb4, 0x00c04fd430c8L),
0x6ba7b8109dad11d180b400c04fd430c8L, 0x6ba7b8109dad11d180b400c04fd430c8L,
'urn:uuid:6ba7b810-9dad-11d1-80b4-00c04fd430c8', 'urn:uuid:6ba7b810-9dad-11d1-80b4-00c04fd430c8',
@ -62,6 +67,7 @@ class TestUUID(TestCase):
'{6ba7b811-9dad-11d1-80b4-00c04fd430c8}', '{6ba7b811-9dad-11d1-80b4-00c04fd430c8}',
'6ba7b8119dad11d180b400c04fd430c8', '6ba7b8119dad11d180b400c04fd430c8',
'\x6b\xa7\xb8\x11\x9d\xad\x11\xd1\x80\xb4\x00\xc0\x4f\xd4\x30\xc8', '\x6b\xa7\xb8\x11\x9d\xad\x11\xd1\x80\xb4\x00\xc0\x4f\xd4\x30\xc8',
'\x11\xb8\xa7\x6b\xad\x9d\xd1\x11\x80\xb4\x00\xc0\x4f\xd4\x30\xc8',
(0x6ba7b811L, 0x9dad, 0x11d1, 0x80, 0xb4, 0x00c04fd430c8L), (0x6ba7b811L, 0x9dad, 0x11d1, 0x80, 0xb4, 0x00c04fd430c8L),
0x6ba7b8119dad11d180b400c04fd430c8L, 0x6ba7b8119dad11d180b400c04fd430c8L,
'urn:uuid:6ba7b811-9dad-11d1-80b4-00c04fd430c8', 'urn:uuid:6ba7b811-9dad-11d1-80b4-00c04fd430c8',
@ -70,6 +76,7 @@ class TestUUID(TestCase):
'{6ba7b812-9dad-11d1-80b4-00c04fd430c8}', '{6ba7b812-9dad-11d1-80b4-00c04fd430c8}',
'6ba7b8129dad11d180b400c04fd430c8', '6ba7b8129dad11d180b400c04fd430c8',
'\x6b\xa7\xb8\x12\x9d\xad\x11\xd1\x80\xb4\x00\xc0\x4f\xd4\x30\xc8', '\x6b\xa7\xb8\x12\x9d\xad\x11\xd1\x80\xb4\x00\xc0\x4f\xd4\x30\xc8',
'\x12\xb8\xa7\x6b\xad\x9d\xd1\x11\x80\xb4\x00\xc0\x4f\xd4\x30\xc8',
(0x6ba7b812L, 0x9dad, 0x11d1, 0x80, 0xb4, 0x00c04fd430c8L), (0x6ba7b812L, 0x9dad, 0x11d1, 0x80, 0xb4, 0x00c04fd430c8L),
0x6ba7b8129dad11d180b400c04fd430c8L, 0x6ba7b8129dad11d180b400c04fd430c8L,
'urn:uuid:6ba7b812-9dad-11d1-80b4-00c04fd430c8', 'urn:uuid:6ba7b812-9dad-11d1-80b4-00c04fd430c8',
@ -78,6 +85,7 @@ class TestUUID(TestCase):
'{6ba7b814-9dad-11d1-80b4-00c04fd430c8}', '{6ba7b814-9dad-11d1-80b4-00c04fd430c8}',
'6ba7b8149dad11d180b400c04fd430c8', '6ba7b8149dad11d180b400c04fd430c8',
'\x6b\xa7\xb8\x14\x9d\xad\x11\xd1\x80\xb4\x00\xc0\x4f\xd4\x30\xc8', '\x6b\xa7\xb8\x14\x9d\xad\x11\xd1\x80\xb4\x00\xc0\x4f\xd4\x30\xc8',
'\x14\xb8\xa7\x6b\xad\x9d\xd1\x11\x80\xb4\x00\xc0\x4f\xd4\x30\xc8',
(0x6ba7b814L, 0x9dad, 0x11d1, 0x80, 0xb4, 0x00c04fd430c8L), (0x6ba7b814L, 0x9dad, 0x11d1, 0x80, 0xb4, 0x00c04fd430c8L),
0x6ba7b8149dad11d180b400c04fd430c8L, 0x6ba7b8149dad11d180b400c04fd430c8L,
'urn:uuid:6ba7b814-9dad-11d1-80b4-00c04fd430c8', 'urn:uuid:6ba7b814-9dad-11d1-80b4-00c04fd430c8',
@ -86,6 +94,7 @@ class TestUUID(TestCase):
'{7d444840-9dc0-11d1-b245-5ffdce74fad2}', '{7d444840-9dc0-11d1-b245-5ffdce74fad2}',
'7d4448409dc011d1b2455ffdce74fad2', '7d4448409dc011d1b2455ffdce74fad2',
'\x7d\x44\x48\x40\x9d\xc0\x11\xd1\xb2\x45\x5f\xfd\xce\x74\xfa\xd2', '\x7d\x44\x48\x40\x9d\xc0\x11\xd1\xb2\x45\x5f\xfd\xce\x74\xfa\xd2',
'\x40\x48\x44\x7d\xc0\x9d\xd1\x11\xb2\x45\x5f\xfd\xce\x74\xfa\xd2',
(0x7d444840L, 0x9dc0, 0x11d1, 0xb2, 0x45, 0x5ffdce74fad2L), (0x7d444840L, 0x9dc0, 0x11d1, 0xb2, 0x45, 0x5ffdce74fad2L),
0x7d4448409dc011d1b2455ffdce74fad2L, 0x7d4448409dc011d1b2455ffdce74fad2L,
'urn:uuid:7d444840-9dc0-11d1-b245-5ffdce74fad2', 'urn:uuid:7d444840-9dc0-11d1-b245-5ffdce74fad2',
@ -94,6 +103,7 @@ class TestUUID(TestCase):
'{e902893a-9d22-3c7e-a7b8-d6e313b71d9f}', '{e902893a-9d22-3c7e-a7b8-d6e313b71d9f}',
'e902893a9d223c7ea7b8d6e313b71d9f', 'e902893a9d223c7ea7b8d6e313b71d9f',
'\xe9\x02\x89\x3a\x9d\x22\x3c\x7e\xa7\xb8\xd6\xe3\x13\xb7\x1d\x9f', '\xe9\x02\x89\x3a\x9d\x22\x3c\x7e\xa7\xb8\xd6\xe3\x13\xb7\x1d\x9f',
'\x3a\x89\x02\xe9\x22\x9d\x7e\x3c\xa7\xb8\xd6\xe3\x13\xb7\x1d\x9f',
(0xe902893aL, 0x9d22, 0x3c7e, 0xa7, 0xb8, 0xd6e313b71d9fL), (0xe902893aL, 0x9d22, 0x3c7e, 0xa7, 0xb8, 0xd6e313b71d9fL),
0xe902893a9d223c7ea7b8d6e313b71d9fL, 0xe902893a9d223c7ea7b8d6e313b71d9fL,
'urn:uuid:e902893a-9d22-3c7e-a7b8-d6e313b71d9f', 'urn:uuid:e902893a-9d22-3c7e-a7b8-d6e313b71d9f',
@ -102,6 +112,7 @@ class TestUUID(TestCase):
'{eb424026-6f54-4ef8-a4d0-bb658a1fc6cf}', '{eb424026-6f54-4ef8-a4d0-bb658a1fc6cf}',
'eb4240266f544ef8a4d0bb658a1fc6cf', 'eb4240266f544ef8a4d0bb658a1fc6cf',
'\xeb\x42\x40\x26\x6f\x54\x4e\xf8\xa4\xd0\xbb\x65\x8a\x1f\xc6\xcf', '\xeb\x42\x40\x26\x6f\x54\x4e\xf8\xa4\xd0\xbb\x65\x8a\x1f\xc6\xcf',
'\x26\x40\x42\xeb\x54\x6f\xf8\x4e\xa4\xd0\xbb\x65\x8a\x1f\xc6\xcf',
(0xeb424026L, 0x6f54, 0x4ef8, 0xa4, 0xd0, 0xbb658a1fc6cfL), (0xeb424026L, 0x6f54, 0x4ef8, 0xa4, 0xd0, 0xbb658a1fc6cfL),
0xeb4240266f544ef8a4d0bb658a1fc6cfL, 0xeb4240266f544ef8a4d0bb658a1fc6cfL,
'urn:uuid:eb424026-6f54-4ef8-a4d0-bb658a1fc6cf', 'urn:uuid:eb424026-6f54-4ef8-a4d0-bb658a1fc6cf',
@ -110,6 +121,7 @@ class TestUUID(TestCase):
'{f81d4fae-7dec-11d0-a765-00a0c91e6bf6}', '{f81d4fae-7dec-11d0-a765-00a0c91e6bf6}',
'f81d4fae7dec11d0a76500a0c91e6bf6', 'f81d4fae7dec11d0a76500a0c91e6bf6',
'\xf8\x1d\x4f\xae\x7d\xec\x11\xd0\xa7\x65\x00\xa0\xc9\x1e\x6b\xf6', '\xf8\x1d\x4f\xae\x7d\xec\x11\xd0\xa7\x65\x00\xa0\xc9\x1e\x6b\xf6',
'\xae\x4f\x1d\xf8\xec\x7d\xd0\x11\xa7\x65\x00\xa0\xc9\x1e\x6b\xf6',
(0xf81d4faeL, 0x7dec, 0x11d0, 0xa7, 0x65, 0x00a0c91e6bf6L), (0xf81d4faeL, 0x7dec, 0x11d0, 0xa7, 0x65, 0x00a0c91e6bf6L),
0xf81d4fae7dec11d0a76500a0c91e6bf6L, 0xf81d4fae7dec11d0a76500a0c91e6bf6L,
'urn:uuid:f81d4fae-7dec-11d0-a765-00a0c91e6bf6', 'urn:uuid:f81d4fae-7dec-11d0-a765-00a0c91e6bf6',
@ -118,6 +130,7 @@ class TestUUID(TestCase):
'{fffefdfc-fffe-fffe-fffe-fffefdfcfbfa}', '{fffefdfc-fffe-fffe-fffe-fffefdfcfbfa}',
'fffefdfcfffefffefffefffefdfcfbfa', 'fffefdfcfffefffefffefffefdfcfbfa',
'\xff\xfe\xfd\xfc\xff\xfe\xff\xfe\xff\xfe\xff\xfe\xfd\xfc\xfb\xfa', '\xff\xfe\xfd\xfc\xff\xfe\xff\xfe\xff\xfe\xff\xfe\xfd\xfc\xfb\xfa',
'\xfc\xfd\xfe\xff\xfe\xff\xfe\xff\xff\xfe\xff\xfe\xfd\xfc\xfb\xfa',
(0xfffefdfcL, 0xfffe, 0xfffe, 0xff, 0xfe, 0xfffefdfcfbfaL), (0xfffefdfcL, 0xfffe, 0xfffe, 0xff, 0xfe, 0xfffefdfcfbfaL),
0xfffefdfcfffefffefffefffefdfcfbfaL, 0xfffefdfcfffefffefffefffefdfcfbfaL,
'urn:uuid:fffefdfc-fffe-fffe-fffe-fffefdfcfbfa', 'urn:uuid:fffefdfc-fffe-fffe-fffe-fffefdfcfbfa',
@ -126,6 +139,7 @@ class TestUUID(TestCase):
'{ffffffff-ffff-ffff-ffff-ffffffffffff}', '{ffffffff-ffff-ffff-ffff-ffffffffffff}',
'ffffffffffffffffffffffffffffffff', 'ffffffffffffffffffffffffffffffff',
'\xff'*16, '\xff'*16,
'\xff'*16,
(0xffffffffL, 0xffffL, 0xffffL, 0xff, 0xff, 0xffffffffffffL), (0xffffffffL, 0xffffL, 0xffffL, 0xff, 0xff, 0xffffffffffffL),
0xffffffffffffffffffffffffffffffffL, 0xffffffffffffffffffffffffffffffffL,
'urn:uuid:ffffffff-ffff-ffff-ffff-ffffffffffff', 'urn:uuid:ffffffff-ffff-ffff-ffff-ffffffffffff',
@ -134,12 +148,14 @@ class TestUUID(TestCase):
equivalents = [] equivalents = []
# Construct each UUID in several different ways. # Construct each UUID in several different ways.
for u in [uuid.UUID(string), uuid.UUID(curly), uuid.UUID(hex), for u in [uuid.UUID(string), uuid.UUID(curly), uuid.UUID(hex),
uuid.UUID(bytes=bytes), uuid.UUID(fields=fields), uuid.UUID(bytes=bytes), uuid.UUID(bytes_le=bytes_le),
uuid.UUID(int=integer), uuid.UUID(urn)]: uuid.UUID(fields=fields), uuid.UUID(int=integer),
uuid.UUID(urn)]:
# Test all conversions and properties of the UUID object. # Test all conversions and properties of the UUID object.
equal(str(u), string) equal(str(u), string)
equal(int(u), integer) equal(int(u), integer)
equal(u.bytes, bytes) equal(u.bytes, bytes)
equal(u.bytes_le, bytes_le)
equal(u.fields, fields) equal(u.fields, fields)
equal(u.time_low, fields[0]) equal(u.time_low, fields[0])
equal(u.time_mid, fields[1]) equal(u.time_mid, fields[1])
@ -189,6 +205,11 @@ class TestUUID(TestCase):
badvalue(lambda: uuid.UUID(bytes='\0'*15)) badvalue(lambda: uuid.UUID(bytes='\0'*15))
badvalue(lambda: uuid.UUID(bytes='\0'*17)) badvalue(lambda: uuid.UUID(bytes='\0'*17))
# Badly formed bytes_le.
badvalue(lambda: uuid.UUID(bytes_le='abc'))
badvalue(lambda: uuid.UUID(bytes_le='\0'*15))
badvalue(lambda: uuid.UUID(bytes_le='\0'*17))
# Badly formed fields. # Badly formed fields.
badvalue(lambda: uuid.UUID(fields=(1,))) badvalue(lambda: uuid.UUID(fields=(1,)))
badvalue(lambda: uuid.UUID(fields=(1, 2, 3, 4, 5))) badvalue(lambda: uuid.UUID(fields=(1, 2, 3, 4, 5)))
@ -221,51 +242,43 @@ class TestUUID(TestCase):
uuid.UUID(h) uuid.UUID(h)
uuid.UUID(hex=h) uuid.UUID(hex=h)
uuid.UUID(bytes=b) uuid.UUID(bytes=b)
uuid.UUID(bytes_le=b)
uuid.UUID(fields=f) uuid.UUID(fields=f)
uuid.UUID(int=i) uuid.UUID(int=i)
# Wrong number of arguments (positional). # Wrong number of arguments (positional).
badtype(lambda: uuid.UUID()) badtype(lambda: uuid.UUID())
badtype(lambda: uuid.UUID(h, b)) badtype(lambda: uuid.UUID(h, b))
badtype(lambda: uuid.UUID(h, b, f)) badtype(lambda: uuid.UUID(h, b, b))
badtype(lambda: uuid.UUID(h, b, f, i)) badtype(lambda: uuid.UUID(h, b, b, f))
badtype(lambda: uuid.UUID(h, b, b, f, i))
# Duplicate arguments (named). # Duplicate arguments.
badtype(lambda: uuid.UUID(hex=h, bytes=b)) for hh in [[], [('hex', h)]]:
badtype(lambda: uuid.UUID(hex=h, fields=f)) for bb in [[], [('bytes', b)]]:
badtype(lambda: uuid.UUID(hex=h, int=i)) for bble in [[], [('bytes_le', b)]]:
badtype(lambda: uuid.UUID(bytes=b, fields=f)) for ii in [[], [('int', i)]]:
badtype(lambda: uuid.UUID(bytes=b, int=i)) for ff in [[], [('fields', f)]]:
badtype(lambda: uuid.UUID(fields=f, int=i)) args = dict(hh + bb + bble + ii + ff)
badtype(lambda: uuid.UUID(hex=h, bytes=b, fields=f)) if len(args) != 0:
badtype(lambda: uuid.UUID(hex=h, bytes=b, int=i)) badtype(lambda: uuid.UUID(h, **args))
badtype(lambda: uuid.UUID(hex=h, fields=f, int=i)) if len(args) != 1:
badtype(lambda: uuid.UUID(bytes=b, int=i, fields=f)) badtype(lambda: uuid.UUID(**args))
badtype(lambda: uuid.UUID(hex=h, bytes=b, int=i, fields=f))
# Duplicate arguments (positional and named).
badtype(lambda: uuid.UUID(h, hex=h))
badtype(lambda: uuid.UUID(h, bytes=b))
badtype(lambda: uuid.UUID(h, fields=f))
badtype(lambda: uuid.UUID(h, int=i))
badtype(lambda: uuid.UUID(h, hex=h, bytes=b))
badtype(lambda: uuid.UUID(h, hex=h, fields=f))
badtype(lambda: uuid.UUID(h, hex=h, int=i))
badtype(lambda: uuid.UUID(h, bytes=b, fields=f))
badtype(lambda: uuid.UUID(h, bytes=b, int=i))
badtype(lambda: uuid.UUID(h, fields=f, int=i))
badtype(lambda: uuid.UUID(h, hex=h, bytes=b, fields=f))
badtype(lambda: uuid.UUID(h, hex=h, bytes=b, int=i))
badtype(lambda: uuid.UUID(h, hex=h, fields=f, int=i))
badtype(lambda: uuid.UUID(h, bytes=b, int=i, fields=f))
badtype(lambda: uuid.UUID(h, hex=h, bytes=b, int=i, fields=f))
# Immutability. # Immutability.
u = uuid.UUID(h) u = uuid.UUID(h)
badtype(lambda: setattr(u, 'hex', h)) badtype(lambda: setattr(u, 'hex', h))
badtype(lambda: setattr(u, 'bytes', b)) badtype(lambda: setattr(u, 'bytes', b))
badtype(lambda: setattr(u, 'bytes_le', b))
badtype(lambda: setattr(u, 'fields', f)) badtype(lambda: setattr(u, 'fields', f))
badtype(lambda: setattr(u, 'int', i)) badtype(lambda: setattr(u, 'int', i))
badtype(lambda: setattr(u, 'time_low', 0))
badtype(lambda: setattr(u, 'time_mid', 0))
badtype(lambda: setattr(u, 'time_hi_version', 0))
badtype(lambda: setattr(u, 'time_hi_version', 0))
badtype(lambda: setattr(u, 'clock_seq_hi_variant', 0))
badtype(lambda: setattr(u, 'clock_seq_low', 0))
badtype(lambda: setattr(u, 'node', 0))
def check_node(self, node, source): def check_node(self, node, source):
individual_group_bit = (node >> 40L) & 1 individual_group_bit = (node >> 40L) & 1
@ -356,11 +369,17 @@ class TestUUID(TestCase):
def test_uuid1(self): def test_uuid1(self):
equal = self.assertEqual equal = self.assertEqual
# Make sure uuid4() generates UUIDs that are actually version 1. # Make sure uuid1() generates UUIDs that are actually version 1.
for u in [uuid.uuid1() for i in range(10)]: for u in [uuid.uuid1() for i in range(10)]:
equal(u.variant, uuid.RFC_4122) equal(u.variant, uuid.RFC_4122)
equal(u.version, 1) equal(u.version, 1)
# Make sure the generated UUIDs are actually unique.
uuids = {}
for u in [uuid.uuid1() for i in range(1000)]:
uuids[u] = 1
equal(len(uuids.keys()), 1000)
# Make sure the supplied node ID appears in the UUID. # Make sure the supplied node ID appears in the UUID.
u = uuid.uuid1(0) u = uuid.uuid1(0)
equal(u.node, 0) equal(u.node, 0)
@ -408,6 +427,12 @@ class TestUUID(TestCase):
equal(u.variant, uuid.RFC_4122) equal(u.variant, uuid.RFC_4122)
equal(u.version, 4) equal(u.version, 4)
# Make sure the generated UUIDs are actually unique.
uuids = {}
for u in [uuid.uuid4() for i in range(1000)]:
uuids[u] = 1
equal(len(uuids.keys()), 1000)
def test_uuid5(self): def test_uuid5(self):
equal = self.assertEqual equal = self.assertEqual

View File

@ -204,6 +204,17 @@ def check_encoding(encoding):
"<?xml version='1.0' encoding='%s'?><xml />" % encoding "<?xml version='1.0' encoding='%s'?><xml />" % encoding
) )
def bug_1534630():
"""
>>> bob = ET.TreeBuilder()
>>> e = bob.data("data")
>>> e = bob.start("tag", {})
>>> e = bob.end("tag")
>>> e = bob.close()
>>> serialize(ET, e)
'<tag />'
"""
def test_main(): def test_main():
from test import test_xml_etree_c from test import test_xml_etree_c
test_support.run_doctest(test_xml_etree_c, verbosity=True) test_support.run_doctest(test_xml_etree_c, verbosity=True)

View File

@ -273,7 +273,8 @@ def generate_tokens(readline):
while column < indents[-1]: while column < indents[-1]:
if column not in indents: if column not in indents:
raise IndentationError( raise IndentationError(
"unindent does not match any outer indentation level") "unindent does not match any outer indentation level",
("<tokenize>", lnum, pos, line))
indents = indents[:-1] indents = indents[:-1]
yield (DEDENT, '', (lnum, pos), (lnum, pos), line) yield (DEDENT, '', (lnum, pos), (lnum, pos), line)

View File

@ -179,9 +179,11 @@ def fullmodname(path):
# looking in sys.path for the longest matching prefix. We'll # looking in sys.path for the longest matching prefix. We'll
# assume that the rest is the package name. # assume that the rest is the package name.
comparepath = os.path.normcase(path)
longest = "" longest = ""
for dir in sys.path: for dir in sys.path:
if path.startswith(dir) and path[len(dir)] == os.path.sep: dir = os.path.normcase(dir)
if comparepath.startswith(dir) and comparepath[len(dir)] == os.sep:
if len(dir) > len(longest): if len(dir) > len(longest):
longest = dir longest = dir

View File

@ -263,11 +263,11 @@ class Request:
def add_header(self, key, val): def add_header(self, key, val):
# useful for something like authentication # useful for something like authentication
self.headers[key.title()] = val self.headers[key.capitalize()] = val
def add_unredirected_header(self, key, val): def add_unredirected_header(self, key, val):
# will not be added to a redirected request # will not be added to a redirected request
self.unredirected_hdrs[key.title()] = val self.unredirected_hdrs[key.capitalize()] = val
def has_header(self, header_name): def has_header(self, header_name):
return (header_name in self.headers or return (header_name in self.headers or
@ -286,7 +286,7 @@ class Request:
class OpenerDirector: class OpenerDirector:
def __init__(self): def __init__(self):
client_version = "Python-urllib/%s" % __version__ client_version = "Python-urllib/%s" % __version__
self.addheaders = [('User-Agent', client_version)] self.addheaders = [('User-agent', client_version)]
# manage the individual handlers # manage the individual handlers
self.handlers = [] self.handlers = []
self.handle_open = {} self.handle_open = {}
@ -675,7 +675,7 @@ class ProxyHandler(BaseHandler):
if user and password: if user and password:
user_pass = '%s:%s' % (unquote(user), unquote(password)) user_pass = '%s:%s' % (unquote(user), unquote(password))
creds = base64.encodestring(user_pass).strip() creds = base64.encodestring(user_pass).strip()
req.add_header('Proxy-Authorization', 'Basic ' + creds) req.add_header('Proxy-authorization', 'Basic ' + creds)
hostport = unquote(hostport) hostport = unquote(hostport)
req.set_proxy(hostport, proxy_type) req.set_proxy(hostport, proxy_type)
if orig_type == proxy_type: if orig_type == proxy_type:
@ -819,7 +819,7 @@ class HTTPBasicAuthHandler(AbstractBasicAuthHandler, BaseHandler):
class ProxyBasicAuthHandler(AbstractBasicAuthHandler, BaseHandler): class ProxyBasicAuthHandler(AbstractBasicAuthHandler, BaseHandler):
auth_header = 'Proxy-Authorization' auth_header = 'Proxy-authorization'
def http_error_407(self, req, fp, code, msg, headers): def http_error_407(self, req, fp, code, msg, headers):
# http_error_auth_reqed requires that there is no userinfo component in # http_error_auth_reqed requires that there is no userinfo component in
@ -1022,20 +1022,20 @@ class AbstractHTTPHandler(BaseHandler):
if request.has_data(): # POST if request.has_data(): # POST
data = request.get_data() data = request.get_data()
if not request.has_header('Content-Type'): if not request.has_header('Content-type'):
request.add_unredirected_header( request.add_unredirected_header(
'Content-Type', 'Content-type',
'application/x-www-form-urlencoded') 'application/x-www-form-urlencoded')
if not request.has_header('Content-Length'): if not request.has_header('Content-length'):
request.add_unredirected_header( request.add_unredirected_header(
'Content-Length', '%d' % len(data)) 'Content-length', '%d' % len(data))
scheme, sel = splittype(request.get_selector()) scheme, sel = splittype(request.get_selector())
sel_host, sel_path = splithost(sel) sel_host, sel_path = splithost(sel)
if not request.has_header('Host'): if not request.has_header('Host'):
request.add_unredirected_header('Host', sel_host or host) request.add_unredirected_header('Host', sel_host or host)
for name, value in self.parent.addheaders: for name, value in self.parent.addheaders:
name = name.title() name = name.capitalize()
if not request.has_header(name): if not request.has_header(name):
request.add_unredirected_header(name, value) request.add_unredirected_header(name, value)
@ -1067,6 +1067,8 @@ class AbstractHTTPHandler(BaseHandler):
# So make sure the connection gets closed after the (only) # So make sure the connection gets closed after the (only)
# request. # request.
headers["Connection"] = "close" headers["Connection"] = "close"
headers = dict(
(name.title(), val) for name, val in headers.items())
try: try:
h.request(req.get_method(), req.get_selector(), req.data, headers) h.request(req.get_method(), req.get_selector(), req.data, headers)
r = h.getresponse() r = h.getresponse()
@ -1217,7 +1219,7 @@ class FileHandler(BaseHandler):
modified = email.Utils.formatdate(stats.st_mtime, usegmt=True) modified = email.Utils.formatdate(stats.st_mtime, usegmt=True)
mtype = mimetypes.guess_type(file)[0] mtype = mimetypes.guess_type(file)[0]
headers = mimetools.Message(StringIO( headers = mimetools.Message(StringIO(
'Content-Type: %s\nContent-Length: %d\nLast-Modified: %s\n' % 'Content-type: %s\nContent-length: %d\nLast-modified: %s\n' %
(mtype or 'text/plain', size, modified))) (mtype or 'text/plain', size, modified)))
if host: if host:
host, port = splitport(host) host, port = splitport(host)
@ -1272,9 +1274,9 @@ class FTPHandler(BaseHandler):
headers = "" headers = ""
mtype = mimetypes.guess_type(req.get_full_url())[0] mtype = mimetypes.guess_type(req.get_full_url())[0]
if mtype: if mtype:
headers += "Content-Type: %s\n" % mtype headers += "Content-type: %s\n" % mtype
if retrlen is not None and retrlen >= 0: if retrlen is not None and retrlen >= 0:
headers += "Content-Length: %d\n" % retrlen headers += "Content-length: %d\n" % retrlen
sf = StringIO(headers) sf = StringIO(headers)
headers = mimetools.Message(sf) headers = mimetools.Message(sf)
return addinfourl(fp, headers, req.get_full_url()) return addinfourl(fp, headers, req.get_full_url())

View File

@ -45,8 +45,6 @@ Typical usage:
""" """
__author__ = 'Ka-Ping Yee <ping@zesty.ca>' __author__ = 'Ka-Ping Yee <ping@zesty.ca>'
__date__ = '$Date: 2006/06/12 23:15:40 $'.split()[1].replace('/', '-')
__version__ = '$Revision: 1.30 $'.split()[1]
RESERVED_NCS, RFC_4122, RESERVED_MICROSOFT, RESERVED_FUTURE = [ RESERVED_NCS, RFC_4122, RESERVED_MICROSOFT, RESERVED_FUTURE = [
'reserved for NCS compatibility', 'specified in RFC 4122', 'reserved for NCS compatibility', 'specified in RFC 4122',
@ -57,15 +55,21 @@ class UUID(object):
UUID objects are immutable, hashable, and usable as dictionary keys. UUID objects are immutable, hashable, and usable as dictionary keys.
Converting a UUID to a string with str() yields something in the form Converting a UUID to a string with str() yields something in the form
'12345678-1234-1234-1234-123456789abc'. The UUID constructor accepts '12345678-1234-1234-1234-123456789abc'. The UUID constructor accepts
four possible forms: a similar string of hexadecimal digits, or a five possible forms: a similar string of hexadecimal digits, or a tuple
string of 16 raw bytes as an argument named 'bytes', or a tuple of of six integer fields (with 32-bit, 16-bit, 16-bit, 8-bit, 8-bit, and
six integer fields (with 32-bit, 16-bit, 16-bit, 8-bit, 8-bit, and 48-bit values respectively) as an argument named 'fields', or a string
48-bit values respectively) as an argument named 'fields', or a single of 16 bytes (with all the integer fields in big-endian order) as an
128-bit integer as an argument named 'int'. argument named 'bytes', or a string of 16 bytes (with the first three
fields in little-endian order) as an argument named 'bytes_le', or a
single 128-bit integer as an argument named 'int'.
UUIDs have these read-only attributes: UUIDs have these read-only attributes:
bytes the UUID as a 16-byte string bytes the UUID as a 16-byte string (containing the six
integer fields in big-endian byte order)
bytes_le the UUID as a 16-byte string (with time_low, time_mid,
and time_hi_version in little-endian byte order)
fields a tuple of the six integer fields of the UUID, fields a tuple of the six integer fields of the UUID,
which are also available as six individual attributes which are also available as six individual attributes
@ -94,10 +98,11 @@ class UUID(object):
when the variant is RFC_4122) when the variant is RFC_4122)
""" """
def __init__(self, hex=None, bytes=None, fields=None, int=None, def __init__(self, hex=None, bytes=None, bytes_le=None, fields=None,
version=None): int=None, version=None):
r"""Create a UUID from either a string of 32 hexadecimal digits, r"""Create a UUID from either a string of 32 hexadecimal digits,
a string of 16 bytes as the 'bytes' argument, a tuple of six a string of 16 bytes as the 'bytes' argument, a string of 16 bytes
in little-endian order as the 'bytes_le' argument, a tuple of six
integers (32-bit time_low, 16-bit time_mid, 16-bit time_hi_version, integers (32-bit time_low, 16-bit time_mid, 16-bit time_hi_version,
8-bit clock_seq_hi_variant, 8-bit clock_seq_low, 48-bit node) as 8-bit clock_seq_hi_variant, 8-bit clock_seq_low, 48-bit node) as
the 'fields' argument, or a single 128-bit integer as the 'int' the 'fields' argument, or a single 128-bit integer as the 'int'
@ -109,23 +114,31 @@ class UUID(object):
UUID('12345678123456781234567812345678') UUID('12345678123456781234567812345678')
UUID('urn:uuid:12345678-1234-5678-1234-567812345678') UUID('urn:uuid:12345678-1234-5678-1234-567812345678')
UUID(bytes='\x12\x34\x56\x78'*4) UUID(bytes='\x12\x34\x56\x78'*4)
UUID(bytes_le='\x78\x56\x34\x12\x34\x12\x78\x56' +
'\x12\x34\x56\x78\x12\x34\x56\x78')
UUID(fields=(0x12345678, 0x1234, 0x5678, 0x12, 0x34, 0x567812345678)) UUID(fields=(0x12345678, 0x1234, 0x5678, 0x12, 0x34, 0x567812345678))
UUID(int=0x12345678123456781234567812345678) UUID(int=0x12345678123456781234567812345678)
Exactly one of 'hex', 'bytes', 'fields', or 'int' must be given. Exactly one of 'hex', 'bytes', 'bytes_le', 'fields', or 'int' must
The 'version' argument is optional; if given, the resulting UUID be given. The 'version' argument is optional; if given, the resulting
will have its variant and version number set according to RFC 4122, UUID will have its variant and version set according to RFC 4122,
overriding bits in the given 'hex', 'bytes', 'fields', or 'int'. overriding the given 'hex', 'bytes', 'bytes_le', 'fields', or 'int'.
""" """
if [hex, bytes, fields, int].count(None) != 3: if [hex, bytes, bytes_le, fields, int].count(None) != 4:
raise TypeError('need just one of hex, bytes, fields, or int') raise TypeError('need one of hex, bytes, bytes_le, fields, or int')
if hex is not None: if hex is not None:
hex = hex.replace('urn:', '').replace('uuid:', '') hex = hex.replace('urn:', '').replace('uuid:', '')
hex = hex.strip('{}').replace('-', '') hex = hex.strip('{}').replace('-', '')
if len(hex) != 32: if len(hex) != 32:
raise ValueError('badly formed hexadecimal UUID string') raise ValueError('badly formed hexadecimal UUID string')
int = long(hex, 16) int = long(hex, 16)
if bytes_le is not None:
if len(bytes_le) != 16:
raise ValueError('bytes_le is not a 16-char string')
bytes = (bytes_le[3] + bytes_le[2] + bytes_le[1] + bytes_le[0] +
bytes_le[5] + bytes_le[4] + bytes_le[7] + bytes_le[6] +
bytes_le[8:])
if bytes is not None: if bytes is not None:
if len(bytes) != 16: if len(bytes) != 16:
raise ValueError('bytes is not a 16-char string') raise ValueError('bytes is not a 16-char string')
@ -194,6 +207,13 @@ class UUID(object):
bytes = property(get_bytes) bytes = property(get_bytes)
def get_bytes_le(self):
bytes = self.bytes
return (bytes[3] + bytes[2] + bytes[1] + bytes[0] +
bytes[5] + bytes[4] + bytes[7] + bytes[6] + bytes[8:])
bytes_le = property(get_bytes_le)
def get_fields(self): def get_fields(self):
return (self.time_low, self.time_mid, self.time_hi_version, return (self.time_low, self.time_mid, self.time_hi_version,
self.clock_seq_hi_variant, self.clock_seq_low, self.node) self.clock_seq_hi_variant, self.clock_seq_low, self.node)
@ -448,6 +468,8 @@ def getnode():
if _node is not None: if _node is not None:
return _node return _node
_last_timestamp = None
def uuid1(node=None, clock_seq=None): def uuid1(node=None, clock_seq=None):
"""Generate a UUID from a host ID, sequence number, and the current time. """Generate a UUID from a host ID, sequence number, and the current time.
If 'node' is not given, getnode() is used to obtain the hardware If 'node' is not given, getnode() is used to obtain the hardware
@ -460,11 +482,15 @@ def uuid1(node=None, clock_seq=None):
_uuid_generate_time(_buffer) _uuid_generate_time(_buffer)
return UUID(bytes=_buffer.raw) return UUID(bytes=_buffer.raw)
global _last_timestamp
import time import time
nanoseconds = int(time.time() * 1e9) nanoseconds = int(time.time() * 1e9)
# 0x01b21dd213814000 is the number of 100-ns intervals between the # 0x01b21dd213814000 is the number of 100-ns intervals between the
# UUID epoch 1582-10-15 00:00:00 and the Unix epoch 1970-01-01 00:00:00. # UUID epoch 1582-10-15 00:00:00 and the Unix epoch 1970-01-01 00:00:00.
timestamp = int(nanoseconds/100) + 0x01b21dd213814000L timestamp = int(nanoseconds/100) + 0x01b21dd213814000L
if timestamp <= _last_timestamp:
timestamp = _last_timestamp + 1
_last_timestamp = timestamp
if clock_seq is None: if clock_seq is None:
import random import random
clock_seq = random.randrange(1<<14L) # instead of stable storage clock_seq = random.randrange(1<<14L) # instead of stable storage

View File

@ -260,6 +260,7 @@ PYTHON_OBJS= \
Python/modsupport.o \ Python/modsupport.o \
Python/mystrtoul.o \ Python/mystrtoul.o \
Python/mysnprintf.o \ Python/mysnprintf.o \
Python/peephole.o \
Python/pyarena.o \ Python/pyarena.o \
Python/pyfpe.o \ Python/pyfpe.o \
Python/pystate.o \ Python/pystate.o \

View File

@ -412,6 +412,7 @@ Greg McFarlane
Michael McLay Michael McLay
Gordon McMillan Gordon McMillan
Jay T. Miller Jay T. Miller
Chris McDonough
Andrew McNamara Andrew McNamara
Caolan McNamara Caolan McNamara
Craig McPheeters Craig McPheeters

View File

@ -1,6 +1,6 @@
Purify (tm) and Quantify (tm) are commercial software quality Purify (tm) and Quantify (tm) are commercial software quality
assurance tools available from Rational Software Corporation assurance tools available from IBM <http://www.ibm.com/software/rational/>.
<http://www.rational.com/>. Purify is essentially a memory access Purify is essentially a memory access
verifier and leak detector; Quantify is a C level profiler. The rest verifier and leak detector; Quantify is a C level profiler. The rest
of this file assumes you generally know how to use Purify and of this file assumes you generally know how to use Purify and
Quantify, and that you have installed valid licenses for these Quantify, and that you have installed valid licenses for these
@ -20,6 +20,17 @@ Quantify'd interpreter, do this:
make PURIFY=quantify make PURIFY=quantify
Starting with Python 2.3, pymalloc is enabled by default. This
will cause many supurious warnings. Modify Objects/obmalloc.c
and enable Py_USING_MEMORY_DEBUGGER by uncommenting it.
README.valgrind has more details about why this is necessary.
See below about setting up suppressions. Some tests may not
run well with Purify due to heavy memory or CPU usage. These
tests may include: test_largefile, test_import, and test_long.
Please report any findings (problems or no warnings) to python-dev@python.org.
It may be useful to submit a bug report for any problems.
When running the regression test (make test), I have found it useful When running the regression test (make test), I have found it useful
to set my PURIFYOPTIONS environment variable using the following to set my PURIFYOPTIONS environment variable using the following
(bash) shell function. Check out the Purify documentation for (bash) shell function. Check out the Purify documentation for
@ -52,6 +63,11 @@ following in your .purify file:
suppress umr ...; "nismodule.c" suppress umr ...; "nismodule.c"
suppress umr ...; "pwdmodule.c" suppress umr ...; "pwdmodule.c"
Note: this list is very old and may not be accurate any longer.
It's possible some of these no longer need to be suppressed.
You will also need to suppress warnings (at least umr)
from Py_ADDRESS_IN_RANGE.
This will still leave you with just a few UMR, mostly in the readline This will still leave you with just a few UMR, mostly in the readline
library, which you can safely ignore. A lot of work has gone into library, which you can safely ignore. A lot of work has gone into
Python 1.5 to plug as many leaks as possible. Python 1.5 to plug as many leaks as possible.

View File

@ -23,4 +23,8 @@ in addition to any analysis.
False positives were also annotated so that the comments can False positives were also annotated so that the comments can
be reviewed and reversed if the analysis was incorrect. be reviewed and reversed if the analysis was incorrect.
A second run was performed on 10-Aug-2006. The tool was tuned to remove
some false positives and perform some additional checks. ~150 new
warnings were produced, primarily related to dereferencing NULL pointers.
Contact python-dev@python.org for more information. Contact python-dev@python.org for more information.

View File

@ -6,7 +6,7 @@
# "python2"? # "python2"?
#WARNING: Commenting out doesn't work. Last line is what's used. #WARNING: Commenting out doesn't work. Last line is what's used.
%define config_binsuffix none %define config_binsuffix none
%define config_binsuffix 2.5 %define config_binsuffix 2.6
# Build tkinter? "auto" enables it if /usr/bin/wish exists. # Build tkinter? "auto" enables it if /usr/bin/wish exists.
#WARNING: Commenting out doesn't work. Last line is what's used. #WARNING: Commenting out doesn't work. Last line is what's used.
@ -33,8 +33,8 @@
################################# #################################
%define name python %define name python
%define version 2.5b3 %define version 2.6a1
%define libvers 2.5 %define libvers 2.6
%define release 1pydotorg %define release 1pydotorg
%define __prefix /usr %define __prefix /usr

View File

@ -58,7 +58,7 @@ RSYNC_OPTS="-aC -e ssh"
PYTHON=$INSTALL_DIR/bin/python PYTHON=$INSTALL_DIR/bin/python
# Python options and regression test program that should always be run. # Python options and regression test program that should always be run.
REGRTEST_ARGS="-E -tt $INSTALL_DIR/lib/python2.5/test/regrtest.py" REGRTEST_ARGS="-E -tt $INSTALL_DIR/lib/python2.6/test/regrtest.py"
REFLOG="build/reflog.txt.out" REFLOG="build/reflog.txt.out"
# These tests are not stable and falsely report leaks sometimes. # These tests are not stable and falsely report leaks sometimes.

View File

@ -1797,7 +1797,6 @@ DB_join(DBObject* self, PyObject* args)
DBC** cursors; DBC** cursors;
DBC* dbc; DBC* dbc;
if (!PyArg_ParseTuple(args,"O|i:join", &cursorsObj, &flags)) if (!PyArg_ParseTuple(args,"O|i:join", &cursorsObj, &flags))
return NULL; return NULL;
@ -1811,6 +1810,11 @@ DB_join(DBObject* self, PyObject* args)
length = PyObject_Length(cursorsObj); length = PyObject_Length(cursorsObj);
cursors = malloc((length+1) * sizeof(DBC*)); cursors = malloc((length+1) * sizeof(DBC*));
if (!cursors) {
PyErr_NoMemory();
return NULL;
}
cursors[length] = NULL; cursors[length] = NULL;
for (x=0; x<length; x++) { for (x=0; x<length; x++) {
PyObject* item = PySequence_GetItem(cursorsObj, x); PyObject* item = PySequence_GetItem(cursorsObj, x);
@ -2622,11 +2626,13 @@ DB_verify(DBObject* self, PyObject* args, PyObject* kwargs)
CHECK_DB_NOT_CLOSED(self); CHECK_DB_NOT_CLOSED(self);
if (outFileName) if (outFileName)
outFile = fopen(outFileName, "w"); outFile = fopen(outFileName, "w");
/* XXX(nnorwitz): it should probably be an exception if outFile
can't be opened. */
MYDB_BEGIN_ALLOW_THREADS; MYDB_BEGIN_ALLOW_THREADS;
err = self->db->verify(self->db, fileName, dbName, outFile, flags); err = self->db->verify(self->db, fileName, dbName, outFile, flags);
MYDB_END_ALLOW_THREADS; MYDB_END_ALLOW_THREADS;
if (outFileName) if (outFile)
fclose(outFile); fclose(outFile);
/* DB.verify acts as a DB handle destructor (like close); this was /* DB.verify acts as a DB handle destructor (like close); this was

View File

@ -192,7 +192,8 @@ escape_encode(PyObject *self,
buf = PyString_AS_STRING (str); buf = PyString_AS_STRING (str);
len = PyString_GET_SIZE (str); len = PyString_GET_SIZE (str);
memmove(buf, buf+1, len-2); memmove(buf, buf+1, len-2);
_PyString_Resize(&str, len-2); if (_PyString_Resize(&str, len-2) < 0)
return NULL;
return codec_tuple(str, PyString_Size(str)); return codec_tuple(str, PyString_Size(str));
} }

View File

@ -140,6 +140,31 @@ char *conversion_mode_errors = NULL;
accessible fields somehow. accessible fields somehow.
*/ */
static PyCArgObject *
StructUnionType_paramfunc(CDataObject *self)
{
PyCArgObject *parg;
StgDictObject *stgdict;
parg = new_CArgObject();
if (parg == NULL)
return NULL;
parg->tag = 'V';
stgdict = PyObject_stgdict((PyObject *)self);
assert(stgdict); /* Cannot be NULL for structure/union instances */
parg->pffi_type = &stgdict->ffi_type_pointer;
/* For structure parameters (by value), parg->value doesn't contain the structure
data itself, instead parg->value.p *points* to the structure's data
See also _ctypes.c, function _call_function_pointer().
*/
parg->value.p = self->b_ptr;
parg->size = self->b_size;
Py_INCREF(self);
parg->obj = (PyObject *)self;
return parg;
}
static PyObject * static PyObject *
StructUnionType_new(PyTypeObject *type, PyObject *args, PyObject *kwds, int isStruct) StructUnionType_new(PyTypeObject *type, PyObject *args, PyObject *kwds, int isStruct)
{ {
@ -172,6 +197,8 @@ StructUnionType_new(PyTypeObject *type, PyObject *args, PyObject *kwds, int isSt
Py_DECREF(result->tp_dict); Py_DECREF(result->tp_dict);
result->tp_dict = (PyObject *)dict; result->tp_dict = (PyObject *)dict;
dict->paramfunc = StructUnionType_paramfunc;
fields = PyDict_GetItemString((PyObject *)dict, "_fields_"); fields = PyDict_GetItemString((PyObject *)dict, "_fields_");
if (!fields) { if (!fields) {
StgDictObject *basedict = PyType_stgdict((PyObject *)result->tp_base); StgDictObject *basedict = PyType_stgdict((PyObject *)result->tp_base);
@ -287,6 +314,7 @@ static char from_param_doc[] =
static PyObject * static PyObject *
CDataType_from_param(PyObject *type, PyObject *value) CDataType_from_param(PyObject *type, PyObject *value)
{ {
PyObject *as_parameter;
if (1 == PyObject_IsInstance(value, type)) { if (1 == PyObject_IsInstance(value, type)) {
Py_INCREF(value); Py_INCREF(value);
return value; return value;
@ -300,7 +328,6 @@ CDataType_from_param(PyObject *type, PyObject *value)
/* If we got a PyCArgObject, we must check if the object packed in it /* If we got a PyCArgObject, we must check if the object packed in it
is an instance of the type's dict->proto */ is an instance of the type's dict->proto */
// if(dict && ob && dict->proto == (PyObject *)ob->ob_type){
if(dict && ob if(dict && ob
&& PyObject_IsInstance(ob, dict->proto)) { && PyObject_IsInstance(ob, dict->proto)) {
Py_INCREF(value); Py_INCREF(value);
@ -330,6 +357,13 @@ CDataType_from_param(PyObject *type, PyObject *value)
} }
/* ... and leave the rest */ /* ... and leave the rest */
#endif #endif
as_parameter = PyObject_GetAttrString(value, "_as_parameter_");
if (as_parameter) {
value = CDataType_from_param(type, as_parameter);
Py_DECREF(as_parameter);
return value;
}
PyErr_Format(PyExc_TypeError, PyErr_Format(PyExc_TypeError,
"expected %s instance instead of %s", "expected %s instance instead of %s",
((PyTypeObject *)type)->tp_name, ((PyTypeObject *)type)->tp_name,
@ -540,6 +574,23 @@ PointerType_SetProto(StgDictObject *stgdict, PyObject *proto)
return 0; return 0;
} }
static PyCArgObject *
PointerType_paramfunc(CDataObject *self)
{
PyCArgObject *parg;
parg = new_CArgObject();
if (parg == NULL)
return NULL;
parg->tag = 'P';
parg->pffi_type = &ffi_type_pointer;
Py_INCREF(self);
parg->obj = (PyObject *)self;
parg->value.p = *(void **)self->b_ptr;
return parg;
}
static PyObject * static PyObject *
PointerType_new(PyTypeObject *type, PyObject *args, PyObject *kwds) PointerType_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{ {
@ -563,6 +614,7 @@ PointerType_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
stgdict->align = getentry("P")->pffi_type->alignment; stgdict->align = getentry("P")->pffi_type->alignment;
stgdict->length = 1; stgdict->length = 1;
stgdict->ffi_type_pointer = ffi_type_pointer; stgdict->ffi_type_pointer = ffi_type_pointer;
stgdict->paramfunc = PointerType_paramfunc;
proto = PyDict_GetItemString(typedict, "_type_"); /* Borrowed ref */ proto = PyDict_GetItemString(typedict, "_type_"); /* Borrowed ref */
if (proto && -1 == PointerType_SetProto(stgdict, proto)) { if (proto && -1 == PointerType_SetProto(stgdict, proto)) {
@ -620,6 +672,7 @@ PointerType_from_param(PyObject *type, PyObject *value)
return PyInt_FromLong(0); /* NULL pointer */ return PyInt_FromLong(0); /* NULL pointer */
typedict = PyType_stgdict(type); typedict = PyType_stgdict(type);
assert(typedict); /* Cannot be NULL for pointer types */
/* If we expect POINTER(<type>), but receive a <type> instance, accept /* If we expect POINTER(<type>), but receive a <type> instance, accept
it by calling byref(<type>). it by calling byref(<type>).
@ -640,6 +693,7 @@ PointerType_from_param(PyObject *type, PyObject *value)
the item types are the same. the item types are the same.
*/ */
StgDictObject *v = PyObject_stgdict(value); StgDictObject *v = PyObject_stgdict(value);
assert(v); /* Cannot be NULL for pointer or array objects */
if (PyObject_IsSubclass(v->proto, typedict->proto)) { if (PyObject_IsSubclass(v->proto, typedict->proto)) {
Py_INCREF(value); Py_INCREF(value);
return value; return value;
@ -906,6 +960,19 @@ add_getset(PyTypeObject *type, PyGetSetDef *gsp)
return 0; return 0;
} }
static PyCArgObject *
ArrayType_paramfunc(CDataObject *self)
{
PyCArgObject *p = new_CArgObject();
if (p == NULL)
return NULL;
p->tag = 'P';
p->pffi_type = &ffi_type_pointer;
p->value.p = (char *)self->b_ptr;
Py_INCREF(self);
p->obj = (PyObject *)self;
return p;
}
static PyObject * static PyObject *
ArrayType_new(PyTypeObject *type, PyObject *args, PyObject *kwds) ArrayType_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
@ -961,6 +1028,8 @@ ArrayType_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Py_INCREF(proto); Py_INCREF(proto);
stgdict->proto = proto; stgdict->proto = proto;
stgdict->paramfunc = &ArrayType_paramfunc;
/* Arrays are passed as pointers to function calls. */ /* Arrays are passed as pointers to function calls. */
stgdict->ffi_type_pointer = ffi_type_pointer; stgdict->ffi_type_pointer = ffi_type_pointer;
@ -1055,6 +1124,7 @@ static char *SIMPLE_TYPE_CHARS = "cbBhHiIlLdfuzZqQPXOv";
static PyObject * static PyObject *
c_wchar_p_from_param(PyObject *type, PyObject *value) c_wchar_p_from_param(PyObject *type, PyObject *value)
{ {
PyObject *as_parameter;
#if (PYTHON_API_VERSION < 1012) #if (PYTHON_API_VERSION < 1012)
# error not supported # error not supported
#endif #endif
@ -1067,6 +1137,8 @@ c_wchar_p_from_param(PyObject *type, PyObject *value)
struct fielddesc *fd = getentry("Z"); struct fielddesc *fd = getentry("Z");
parg = new_CArgObject(); parg = new_CArgObject();
if (parg == NULL)
return NULL;
parg->pffi_type = &ffi_type_pointer; parg->pffi_type = &ffi_type_pointer;
parg->tag = 'Z'; parg->tag = 'Z';
parg->obj = fd->setfunc(&parg->value, value, 0); parg->obj = fd->setfunc(&parg->value, value, 0);
@ -1083,7 +1155,9 @@ c_wchar_p_from_param(PyObject *type, PyObject *value)
if (ArrayObject_Check(value) || PointerObject_Check(value)) { if (ArrayObject_Check(value) || PointerObject_Check(value)) {
/* c_wchar array instance or pointer(c_wchar(...)) */ /* c_wchar array instance or pointer(c_wchar(...)) */
StgDictObject *dt = PyObject_stgdict(value); StgDictObject *dt = PyObject_stgdict(value);
StgDictObject *dict = dt && dt->proto ? PyType_stgdict(dt->proto) : NULL; StgDictObject *dict;
assert(dt); /* Cannot be NULL for pointer or array objects */
dict = dt && dt->proto ? PyType_stgdict(dt->proto) : NULL;
if (dict && (dict->setfunc == getentry("u")->setfunc)) { if (dict && (dict->setfunc == getentry("u")->setfunc)) {
Py_INCREF(value); Py_INCREF(value);
return value; return value;
@ -1098,6 +1172,13 @@ c_wchar_p_from_param(PyObject *type, PyObject *value)
return value; return value;
} }
} }
as_parameter = PyObject_GetAttrString(value, "_as_parameter_");
if (as_parameter) {
value = c_wchar_p_from_param(type, as_parameter);
Py_DECREF(as_parameter);
return value;
}
/* XXX better message */ /* XXX better message */
PyErr_SetString(PyExc_TypeError, PyErr_SetString(PyExc_TypeError,
"wrong type"); "wrong type");
@ -1107,6 +1188,7 @@ c_wchar_p_from_param(PyObject *type, PyObject *value)
static PyObject * static PyObject *
c_char_p_from_param(PyObject *type, PyObject *value) c_char_p_from_param(PyObject *type, PyObject *value)
{ {
PyObject *as_parameter;
#if (PYTHON_API_VERSION < 1012) #if (PYTHON_API_VERSION < 1012)
# error not supported # error not supported
#endif #endif
@ -1119,6 +1201,8 @@ c_char_p_from_param(PyObject *type, PyObject *value)
struct fielddesc *fd = getentry("z"); struct fielddesc *fd = getentry("z");
parg = new_CArgObject(); parg = new_CArgObject();
if (parg == NULL)
return NULL;
parg->pffi_type = &ffi_type_pointer; parg->pffi_type = &ffi_type_pointer;
parg->tag = 'z'; parg->tag = 'z';
parg->obj = fd->setfunc(&parg->value, value, 0); parg->obj = fd->setfunc(&parg->value, value, 0);
@ -1135,7 +1219,9 @@ c_char_p_from_param(PyObject *type, PyObject *value)
if (ArrayObject_Check(value) || PointerObject_Check(value)) { if (ArrayObject_Check(value) || PointerObject_Check(value)) {
/* c_char array instance or pointer(c_char(...)) */ /* c_char array instance or pointer(c_char(...)) */
StgDictObject *dt = PyObject_stgdict(value); StgDictObject *dt = PyObject_stgdict(value);
StgDictObject *dict = dt && dt->proto ? PyType_stgdict(dt->proto) : NULL; StgDictObject *dict;
assert(dt); /* Cannot be NULL for pointer or array objects */
dict = dt && dt->proto ? PyType_stgdict(dt->proto) : NULL;
if (dict && (dict->setfunc == getentry("c")->setfunc)) { if (dict && (dict->setfunc == getentry("c")->setfunc)) {
Py_INCREF(value); Py_INCREF(value);
return value; return value;
@ -1150,6 +1236,13 @@ c_char_p_from_param(PyObject *type, PyObject *value)
return value; return value;
} }
} }
as_parameter = PyObject_GetAttrString(value, "_as_parameter_");
if (as_parameter) {
value = c_char_p_from_param(type, as_parameter);
Py_DECREF(as_parameter);
return value;
}
/* XXX better message */ /* XXX better message */
PyErr_SetString(PyExc_TypeError, PyErr_SetString(PyExc_TypeError,
"wrong type"); "wrong type");
@ -1160,6 +1253,7 @@ static PyObject *
c_void_p_from_param(PyObject *type, PyObject *value) c_void_p_from_param(PyObject *type, PyObject *value)
{ {
StgDictObject *stgd; StgDictObject *stgd;
PyObject *as_parameter;
#if (PYTHON_API_VERSION < 1012) #if (PYTHON_API_VERSION < 1012)
# error not supported # error not supported
#endif #endif
@ -1176,6 +1270,8 @@ c_void_p_from_param(PyObject *type, PyObject *value)
struct fielddesc *fd = getentry("P"); struct fielddesc *fd = getentry("P");
parg = new_CArgObject(); parg = new_CArgObject();
if (parg == NULL)
return NULL;
parg->pffi_type = &ffi_type_pointer; parg->pffi_type = &ffi_type_pointer;
parg->tag = 'P'; parg->tag = 'P';
parg->obj = fd->setfunc(&parg->value, value, 0); parg->obj = fd->setfunc(&parg->value, value, 0);
@ -1191,6 +1287,8 @@ c_void_p_from_param(PyObject *type, PyObject *value)
struct fielddesc *fd = getentry("z"); struct fielddesc *fd = getentry("z");
parg = new_CArgObject(); parg = new_CArgObject();
if (parg == NULL)
return NULL;
parg->pffi_type = &ffi_type_pointer; parg->pffi_type = &ffi_type_pointer;
parg->tag = 'z'; parg->tag = 'z';
parg->obj = fd->setfunc(&parg->value, value, 0); parg->obj = fd->setfunc(&parg->value, value, 0);
@ -1206,6 +1304,8 @@ c_void_p_from_param(PyObject *type, PyObject *value)
struct fielddesc *fd = getentry("Z"); struct fielddesc *fd = getentry("Z");
parg = new_CArgObject(); parg = new_CArgObject();
if (parg == NULL)
return NULL;
parg->pffi_type = &ffi_type_pointer; parg->pffi_type = &ffi_type_pointer;
parg->tag = 'Z'; parg->tag = 'Z';
parg->obj = fd->setfunc(&parg->value, value, 0); parg->obj = fd->setfunc(&parg->value, value, 0);
@ -1242,6 +1342,8 @@ c_void_p_from_param(PyObject *type, PyObject *value)
CFuncPtrObject *func; CFuncPtrObject *func;
func = (CFuncPtrObject *)value; func = (CFuncPtrObject *)value;
parg = new_CArgObject(); parg = new_CArgObject();
if (parg == NULL)
return NULL;
parg->pffi_type = &ffi_type_pointer; parg->pffi_type = &ffi_type_pointer;
parg->tag = 'P'; parg->tag = 'P';
Py_INCREF(value); Py_INCREF(value);
@ -1269,6 +1371,13 @@ c_void_p_from_param(PyObject *type, PyObject *value)
return (PyObject *)parg; return (PyObject *)parg;
} }
} }
as_parameter = PyObject_GetAttrString(value, "_as_parameter_");
if (as_parameter) {
value = c_void_p_from_param(type, as_parameter);
Py_DECREF(as_parameter);
return value;
}
/* XXX better message */ /* XXX better message */
PyErr_SetString(PyExc_TypeError, PyErr_SetString(PyExc_TypeError,
"wrong type"); "wrong type");
@ -1355,6 +1464,33 @@ static PyObject *CreateSwappedType(PyTypeObject *type, PyObject *args, PyObject
return (PyObject *)result; return (PyObject *)result;
} }
static PyCArgObject *
SimpleType_paramfunc(CDataObject *self)
{
StgDictObject *dict;
char *fmt;
PyCArgObject *parg;
struct fielddesc *fd;
dict = PyObject_stgdict((PyObject *)self);
assert(dict); /* Cannot be NULL for CDataObject instances */
fmt = PyString_AsString(dict->proto);
assert(fmt);
fd = getentry(fmt);
assert(fd);
parg = new_CArgObject();
if (parg == NULL)
return NULL;
parg->tag = fmt[0];
parg->pffi_type = fd->pffi_type;
Py_INCREF(self);
parg->obj = (PyObject *)self;
memcpy(&parg->value, self->b_ptr, self->b_size);
return parg;
}
static PyObject * static PyObject *
SimpleType_new(PyTypeObject *type, PyObject *args, PyObject *kwds) SimpleType_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
@ -1404,6 +1540,8 @@ SimpleType_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
stgdict->size = fmt->pffi_type->size; stgdict->size = fmt->pffi_type->size;
stgdict->setfunc = fmt->setfunc; stgdict->setfunc = fmt->setfunc;
stgdict->getfunc = fmt->getfunc; stgdict->getfunc = fmt->getfunc;
stgdict->paramfunc = SimpleType_paramfunc;
/* /*
if (result->tp_base != &Simple_Type) { if (result->tp_base != &Simple_Type) {
stgdict->setfunc = NULL; stgdict->setfunc = NULL;
@ -1502,23 +1640,6 @@ SimpleType_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
/* /*
* This is a *class method*. * This is a *class method*.
* Convert a parameter into something that ConvParam can handle. * Convert a parameter into something that ConvParam can handle.
*
* This is either an instance of the requested type, a Python integer, or a
* 'magic' 3-tuple.
*
* (These are somewhat related to Martin v. Loewis 'Enhanced Argument Tuples',
* described in PEP 286.)
*
* The tuple must contain
*
* - a format character, currently 'ifdqc' are understood
* which will inform ConvParam about how to push the argument on the stack.
*
* - a corresponding Python object: i - integer, f - float, d - float,
* q - longlong, c - integer
*
* - any object which can be used to keep the original parameter alive
* as long as the tuple lives.
*/ */
static PyObject * static PyObject *
SimpleType_from_param(PyObject *type, PyObject *value) SimpleType_from_param(PyObject *type, PyObject *value)
@ -1527,6 +1648,7 @@ SimpleType_from_param(PyObject *type, PyObject *value)
char *fmt; char *fmt;
PyCArgObject *parg; PyCArgObject *parg;
struct fielddesc *fd; struct fielddesc *fd;
PyObject *as_parameter;
/* If the value is already an instance of the requested type, /* If the value is already an instance of the requested type,
we can use it as is */ we can use it as is */
@ -1552,11 +1674,20 @@ SimpleType_from_param(PyObject *type, PyObject *value)
parg->tag = fmt[0]; parg->tag = fmt[0];
parg->pffi_type = fd->pffi_type; parg->pffi_type = fd->pffi_type;
parg->obj = fd->setfunc(&parg->value, value, 0); parg->obj = fd->setfunc(&parg->value, value, 0);
if (parg->obj == NULL) { if (parg->obj)
Py_DECREF(parg); return (PyObject *)parg;
return NULL; PyErr_Clear();
Py_DECREF(parg);
as_parameter = PyObject_GetAttrString(value, "_as_parameter_");
if (as_parameter) {
value = SimpleType_from_param(type, as_parameter);
Py_DECREF(as_parameter);
return value;
} }
return (PyObject *)parg; PyErr_SetString(PyExc_TypeError,
"wrong type");
return NULL;
} }
static PyMethodDef SimpleType_methods[] = { static PyMethodDef SimpleType_methods[] = {
@ -1721,6 +1852,23 @@ make_funcptrtype_dict(StgDictObject *stgdict)
} }
static PyCArgObject *
CFuncPtrType_paramfunc(CDataObject *self)
{
PyCArgObject *parg;
parg = new_CArgObject();
if (parg == NULL)
return NULL;
parg->tag = 'P';
parg->pffi_type = &ffi_type_pointer;
Py_INCREF(self);
parg->obj = (PyObject *)self;
parg->value.p = *(void **)self->b_ptr;
return parg;
}
static PyObject * static PyObject *
CFuncPtrType_new(PyTypeObject *type, PyObject *args, PyObject *kwds) CFuncPtrType_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{ {
@ -1732,6 +1880,8 @@ CFuncPtrType_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
if (!stgdict) if (!stgdict)
return NULL; return NULL;
stgdict->paramfunc = CFuncPtrType_paramfunc;
/* create the new instance (which is a class, /* create the new instance (which is a class,
since we are a metatype!) */ since we are a metatype!) */
result = (PyTypeObject *)PyType_Type.tp_new(type, args, kwds); result = (PyTypeObject *)PyType_Type.tp_new(type, args, kwds);
@ -1921,6 +2071,7 @@ static int
CData_clear(CDataObject *self) CData_clear(CDataObject *self)
{ {
StgDictObject *dict = PyObject_stgdict((PyObject *)self); StgDictObject *dict = PyObject_stgdict((PyObject *)self);
assert(dict); /* Cannot be NULL for CDataObject instances */
Py_CLEAR(self->b_objects); Py_CLEAR(self->b_objects);
if ((self->b_needsfree) if ((self->b_needsfree)
&& ((size_t)dict->size > sizeof(self->b_value))) && ((size_t)dict->size > sizeof(self->b_value)))
@ -2218,7 +2369,9 @@ _CData_set(CDataObject *dst, PyObject *type, SETFUNC setfunc, PyObject *value,
StgDictObject *p1, *p2; StgDictObject *p1, *p2;
PyObject *keep; PyObject *keep;
p1 = PyObject_stgdict(value); p1 = PyObject_stgdict(value);
assert(p1); /* Cannot be NULL for array instances */
p2 = PyType_stgdict(type); p2 = PyType_stgdict(type);
assert(p2); /* Cannot be NULL for pointer types */
if (p1->proto != p2->proto) { if (p1->proto != p2->proto) {
PyErr_Format(PyExc_TypeError, PyErr_Format(PyExc_TypeError,
@ -2308,23 +2461,6 @@ GenericCData_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
CFuncPtr_Type CFuncPtr_Type
*/ */
static PyObject *
CFuncPtr_as_parameter(CDataObject *self)
{
PyCArgObject *parg;
parg = new_CArgObject();
if (parg == NULL)
return NULL;
parg->tag = 'P';
parg->pffi_type = &ffi_type_pointer;
Py_INCREF(self);
parg->obj = (PyObject *)self;
parg->value.p = *(void **)self->b_ptr;
return (PyObject *)parg;
}
static int static int
CFuncPtr_set_errcheck(CFuncPtrObject *self, PyObject *ob) CFuncPtr_set_errcheck(CFuncPtrObject *self, PyObject *ob)
{ {
@ -2384,7 +2520,7 @@ CFuncPtr_get_restype(CFuncPtrObject *self)
return self->restype; return self->restype;
} }
dict = PyObject_stgdict((PyObject *)self); dict = PyObject_stgdict((PyObject *)self);
assert(dict); assert(dict); /* Cannot be NULL for CFuncPtrObject instances */
if (dict->restype) { if (dict->restype) {
Py_INCREF(dict->restype); Py_INCREF(dict->restype);
return dict->restype; return dict->restype;
@ -2426,7 +2562,7 @@ CFuncPtr_get_argtypes(CFuncPtrObject *self)
return self->argtypes; return self->argtypes;
} }
dict = PyObject_stgdict((PyObject *)self); dict = PyObject_stgdict((PyObject *)self);
assert(dict); assert(dict); /* Cannot be NULL for CFuncPtrObject instances */
if (dict->argtypes) { if (dict->argtypes) {
Py_INCREF(dict->argtypes); Py_INCREF(dict->argtypes);
return dict->argtypes; return dict->argtypes;
@ -2444,9 +2580,6 @@ static PyGetSetDef CFuncPtr_getsets[] = {
{ "argtypes", (getter)CFuncPtr_get_argtypes, { "argtypes", (getter)CFuncPtr_get_argtypes,
(setter)CFuncPtr_set_argtypes, (setter)CFuncPtr_set_argtypes,
"specify the argument types", NULL }, "specify the argument types", NULL },
{ "_as_parameter_", (getter)CFuncPtr_as_parameter, NULL,
"return a magic value so that this can be converted to a C parameter (readonly)",
NULL },
{ NULL, NULL } { NULL, NULL }
}; };
@ -2475,6 +2608,8 @@ static PPROC FindAddress(void *handle, char *name, PyObject *type)
where n is 0, 4, 8, 12, ..., 128 where n is 0, 4, 8, 12, ..., 128
*/ */
mangled_name = alloca(strlen(name) + 1 + 1 + 1 + 3); /* \0 _ @ %d */ mangled_name = alloca(strlen(name) + 1 + 1 + 1 + 3); /* \0 _ @ %d */
if (!mangled_name)
return NULL;
for (i = 0; i < 32; ++i) { for (i = 0; i < 32; ++i) {
sprintf(mangled_name, "_%s@%d", name, i*4); sprintf(mangled_name, "_%s@%d", name, i*4);
address = (PPROC)GetProcAddress(handle, mangled_name); address = (PPROC)GetProcAddress(handle, mangled_name);
@ -2520,8 +2655,12 @@ static int
_validate_paramflags(PyTypeObject *type, PyObject *paramflags) _validate_paramflags(PyTypeObject *type, PyObject *paramflags)
{ {
int i, len; int i, len;
StgDictObject *dict = PyType_stgdict((PyObject *)type); StgDictObject *dict;
PyObject *argtypes = dict->argtypes; PyObject *argtypes;
dict = PyType_stgdict((PyObject *)type);
assert(dict); /* Cannot be NULL. 'type' is a CFuncPtr type. */
argtypes = dict->argtypes;
if (paramflags == NULL || dict->argtypes == NULL) if (paramflags == NULL || dict->argtypes == NULL)
return 1; return 1;
@ -2744,6 +2883,8 @@ CFuncPtr_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
if (ptr == NULL) if (ptr == NULL)
return NULL; return NULL;
ob = (CDataObject *)GenericCData_new(type, args, kwds); ob = (CDataObject *)GenericCData_new(type, args, kwds);
if (ob == NULL)
return NULL;
*(void **)ob->b_ptr = ptr; *(void **)ob->b_ptr = ptr;
return (PyObject *)ob; return (PyObject *)ob;
} }
@ -2791,6 +2932,8 @@ CFuncPtr_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
return NULL; return NULL;
self = (CFuncPtrObject *)GenericCData_new(type, args, kwds); self = (CFuncPtrObject *)GenericCData_new(type, args, kwds);
if (self == NULL)
return NULL;
Py_INCREF(callable); Py_INCREF(callable);
self->callable = callable; self->callable = callable;
@ -2987,6 +3130,13 @@ _build_callargs(CFuncPtrObject *self, PyObject *argtypes,
} }
ob = PyTuple_GET_ITEM(argtypes, i); ob = PyTuple_GET_ITEM(argtypes, i);
dict = PyType_stgdict(ob); dict = PyType_stgdict(ob);
if (dict == NULL) {
/* Cannot happen: _validate_paramflags()
would not accept such an object */
PyErr_Format(PyExc_RuntimeError,
"NULL stgdict unexpected");
goto error;
}
if (PyString_Check(dict->proto)) { if (PyString_Check(dict->proto)) {
PyErr_Format( PyErr_Format(
PyExc_TypeError, PyExc_TypeError,
@ -3129,7 +3279,7 @@ CFuncPtr_call(CFuncPtrObject *self, PyObject *inargs, PyObject *kwds)
int outmask; int outmask;
unsigned int numretvals; unsigned int numretvals;
assert(dict); /* if not, it's a bug */ assert(dict); /* Cannot be NULL for CFuncPtrObject instances */
restype = self->restype ? self->restype : dict->restype; restype = self->restype ? self->restype : dict->restype;
converters = self->converters ? self->converters : dict->converters; converters = self->converters ? self->converters : dict->converters;
checker = self->checker ? self->checker : dict->checker; checker = self->checker ? self->checker : dict->checker;
@ -3349,30 +3499,6 @@ IBUG(char *msg)
return -1; return -1;
} }
static PyObject *
Struct_as_parameter(CDataObject *self)
{
PyCArgObject *parg;
StgDictObject *stgdict;
parg = new_CArgObject();
if (parg == NULL)
return NULL;
parg->tag = 'V';
stgdict = PyObject_stgdict((PyObject *)self);
parg->pffi_type = &stgdict->ffi_type_pointer;
/* For structure parameters (by value), parg->value doesn't contain the structure
data itself, instead parg->value.p *points* to the structure's data
See also _ctypes.c, function _call_function_pointer().
*/
parg->value.p = self->b_ptr;
parg->size = self->b_size;
Py_INCREF(self);
parg->obj = (PyObject *)self;
return (PyObject *)parg;
}
static int static int
Struct_init(PyObject *self, PyObject *args, PyObject *kwds) Struct_init(PyObject *self, PyObject *args, PyObject *kwds)
{ {
@ -3447,13 +3573,6 @@ Struct_init(PyObject *self, PyObject *args, PyObject *kwds)
return 0; return 0;
} }
static PyGetSetDef Struct_getsets[] = {
{ "_as_parameter_", (getter)Struct_as_parameter, NULL,
"return a magic value so that this can be converted to a C parameter (readonly)",
NULL },
{ NULL, NULL }
};
static PyTypeObject Struct_Type = { static PyTypeObject Struct_Type = {
PyObject_HEAD_INIT(NULL) PyObject_HEAD_INIT(NULL)
0, 0,
@ -3485,7 +3604,7 @@ static PyTypeObject Struct_Type = {
0, /* tp_iternext */ 0, /* tp_iternext */
0, /* tp_methods */ 0, /* tp_methods */
0, /* tp_members */ 0, /* tp_members */
Struct_getsets, /* tp_getset */ 0, /* tp_getset */
0, /* tp_base */ 0, /* tp_base */
0, /* tp_dict */ 0, /* tp_dict */
0, /* tp_descr_get */ 0, /* tp_descr_get */
@ -3528,7 +3647,7 @@ static PyTypeObject Union_Type = {
0, /* tp_iternext */ 0, /* tp_iternext */
0, /* tp_methods */ 0, /* tp_methods */
0, /* tp_members */ 0, /* tp_members */
Struct_getsets, /* tp_getset */ 0, /* tp_getset */
0, /* tp_base */ 0, /* tp_base */
0, /* tp_dict */ 0, /* tp_dict */
0, /* tp_descr_get */ 0, /* tp_descr_get */
@ -3581,7 +3700,7 @@ Array_item(PyObject *_self, Py_ssize_t index)
} }
stgdict = PyObject_stgdict((PyObject *)self); stgdict = PyObject_stgdict((PyObject *)self);
assert(stgdict); assert(stgdict); /* Cannot be NULL for array instances */
/* Would it be clearer if we got the item size from /* Would it be clearer if we got the item size from
stgdict->proto's stgdict? stgdict->proto's stgdict?
*/ */
@ -3612,8 +3731,11 @@ Array_slice(PyObject *_self, Py_ssize_t ilow, Py_ssize_t ihigh)
len = ihigh - ilow; len = ihigh - ilow;
stgdict = PyObject_stgdict((PyObject *)self); stgdict = PyObject_stgdict((PyObject *)self);
assert(stgdict); /* Cannot be NULL for array object instances */
proto = stgdict->proto; proto = stgdict->proto;
itemdict = PyType_stgdict(proto); itemdict = PyType_stgdict(proto);
assert(itemdict); /* proto is the item type of the array, a ctypes
type, so this cannot be NULL */
if (itemdict->getfunc == getentry("c")->getfunc) { if (itemdict->getfunc == getentry("c")->getfunc) {
char *ptr = (char *)self->b_ptr; char *ptr = (char *)self->b_ptr;
return PyString_FromStringAndSize(ptr + ilow, len); return PyString_FromStringAndSize(ptr + ilow, len);
@ -3650,6 +3772,7 @@ Array_ass_item(PyObject *_self, Py_ssize_t index, PyObject *value)
} }
stgdict = PyObject_stgdict((PyObject *)self); stgdict = PyObject_stgdict((PyObject *)self);
assert(stgdict); /* Cannot be NULL for array object instances */
if (index < 0 || index >= stgdict->length) { if (index < 0 || index >= stgdict->length) {
PyErr_SetString(PyExc_IndexError, PyErr_SetString(PyExc_IndexError,
"invalid index"); "invalid index");
@ -3726,26 +3849,6 @@ static PySequenceMethods Array_as_sequence = {
0, /* sq_inplace_repeat; */ 0, /* sq_inplace_repeat; */
}; };
static PyObject *
Array_as_parameter(CDataObject *self)
{
PyCArgObject *p = new_CArgObject();
if (p == NULL)
return NULL;
p->tag = 'P';
p->pffi_type = &ffi_type_pointer;
p->value.p = (char *)self->b_ptr;
Py_INCREF(self);
p->obj = (PyObject *)self;
return (PyObject *)p;
}
static PyGetSetDef Array_getsets[] = {
{ "_as_parameter_", (getter)Array_as_parameter,
(setter)NULL, "convert to a parameter", NULL },
{ NULL },
};
PyTypeObject Array_Type = { PyTypeObject Array_Type = {
PyObject_HEAD_INIT(NULL) PyObject_HEAD_INIT(NULL)
0, 0,
@ -3777,7 +3880,7 @@ PyTypeObject Array_Type = {
0, /* tp_iternext */ 0, /* tp_iternext */
0, /* tp_methods */ 0, /* tp_methods */
0, /* tp_members */ 0, /* tp_members */
Array_getsets, /* tp_getset */ 0, /* tp_getset */
0, /* tp_base */ 0, /* tp_base */
0, /* tp_dict */ 0, /* tp_dict */
0, /* tp_descr_get */ 0, /* tp_descr_get */
@ -3861,6 +3964,7 @@ Simple_set_value(CDataObject *self, PyObject *value)
PyObject *result; PyObject *result;
StgDictObject *dict = PyObject_stgdict((PyObject *)self); StgDictObject *dict = PyObject_stgdict((PyObject *)self);
assert(dict); /* Cannot be NULL for CDataObject instances */
assert(dict->setfunc); assert(dict->setfunc);
result = dict->setfunc(self->b_ptr, value, dict->size); result = dict->setfunc(self->b_ptr, value, dict->size);
if (!result) if (!result)
@ -3886,40 +3990,14 @@ Simple_get_value(CDataObject *self)
{ {
StgDictObject *dict; StgDictObject *dict;
dict = PyObject_stgdict((PyObject *)self); dict = PyObject_stgdict((PyObject *)self);
assert(dict); /* Cannot be NULL for CDataObject instances */
assert(dict->getfunc); assert(dict->getfunc);
dict = PyObject_stgdict((PyObject *)self);
return dict->getfunc(self->b_ptr, self->b_size); return dict->getfunc(self->b_ptr, self->b_size);
} }
static PyObject *
Simple_as_parameter(CDataObject *self)
{
StgDictObject *dict = PyObject_stgdict((PyObject *)self);
char *fmt = PyString_AsString(dict->proto);
PyCArgObject *parg;
struct fielddesc *fd;
fd = getentry(fmt);
assert(fd);
parg = new_CArgObject();
if (parg == NULL)
return NULL;
parg->tag = fmt[0];
parg->pffi_type = fd->pffi_type;
Py_INCREF(self);
parg->obj = (PyObject *)self;
memcpy(&parg->value, self->b_ptr, self->b_size);
return (PyObject *)parg;
}
static PyGetSetDef Simple_getsets[] = { static PyGetSetDef Simple_getsets[] = {
{ "value", (getter)Simple_get_value, (setter)Simple_set_value, { "value", (getter)Simple_get_value, (setter)Simple_set_value,
"current value", NULL }, "current value", NULL },
{ "_as_parameter_", (getter)Simple_as_parameter, NULL,
"return a magic value so that this can be converted to a C parameter (readonly)",
NULL },
{ NULL, NULL } { NULL, NULL }
}; };
@ -4085,12 +4163,14 @@ Pointer_item(PyObject *_self, Py_ssize_t index)
} }
stgdict = PyObject_stgdict((PyObject *)self); stgdict = PyObject_stgdict((PyObject *)self);
assert(stgdict); assert(stgdict); /* Cannot be NULL for pointer object instances */
assert(stgdict->proto);
proto = stgdict->proto; proto = stgdict->proto;
/* XXXXXX MAKE SURE PROTO IS NOT NULL! */ assert(proto);
itemdict = PyType_stgdict(proto); itemdict = PyType_stgdict(proto);
assert(itemdict); /* proto is the item type of the pointer, a ctypes
type, so this cannot be NULL */
size = itemdict->size; size = itemdict->size;
offset = index * itemdict->size; offset = index * itemdict->size;
@ -4120,12 +4200,15 @@ Pointer_ass_item(PyObject *_self, Py_ssize_t index, PyObject *value)
} }
stgdict = PyObject_stgdict((PyObject *)self); stgdict = PyObject_stgdict((PyObject *)self);
assert(stgdict); assert(stgdict); /* Cannot be NULL fr pointer instances */
assert(stgdict->proto);
proto = stgdict->proto; proto = stgdict->proto;
/* XXXXXX MAKE SURE PROTO IS NOT NULL! */ assert(proto);
itemdict = PyType_stgdict(proto); itemdict = PyType_stgdict(proto);
assert(itemdict); /* Cannot be NULL because the itemtype of a pointer
is always a ctypes type */
size = itemdict->size; size = itemdict->size;
offset = index * itemdict->size; offset = index * itemdict->size;
@ -4145,7 +4228,7 @@ Pointer_get_contents(CDataObject *self, void *closure)
} }
stgdict = PyObject_stgdict((PyObject *)self); stgdict = PyObject_stgdict((PyObject *)self);
assert(stgdict); assert(stgdict); /* Cannot be NULL fr pointer instances */
return CData_FromBaseObj(stgdict->proto, return CData_FromBaseObj(stgdict->proto,
(PyObject *)self, 0, (PyObject *)self, 0,
*(void **)self->b_ptr); *(void **)self->b_ptr);
@ -4164,7 +4247,7 @@ Pointer_set_contents(CDataObject *self, PyObject *value, void *closure)
return -1; return -1;
} }
stgdict = PyObject_stgdict((PyObject *)self); stgdict = PyObject_stgdict((PyObject *)self);
/* should have been catched in Pointer_new() */ assert(stgdict); /* Cannot be NULL fr pointer instances */
assert(stgdict->proto); assert(stgdict->proto);
if (!CDataObject_Check(value) if (!CDataObject_Check(value)
|| 0 == PyObject_IsInstance(value, stgdict->proto)) { || 0 == PyObject_IsInstance(value, stgdict->proto)) {
@ -4193,30 +4276,10 @@ Pointer_set_contents(CDataObject *self, PyObject *value, void *closure)
return KeepRef(self, 0, keep); return KeepRef(self, 0, keep);
} }
static PyObject *
Pointer_as_parameter(CDataObject *self)
{
PyCArgObject *parg;
parg = new_CArgObject();
if (parg == NULL)
return NULL;
parg->tag = 'P';
parg->pffi_type = &ffi_type_pointer;
Py_INCREF(self);
parg->obj = (PyObject *)self;
parg->value.p = *(void **)self->b_ptr;
return (PyObject *)parg;
}
static PyGetSetDef Pointer_getsets[] = { static PyGetSetDef Pointer_getsets[] = {
{ "contents", (getter)Pointer_get_contents, { "contents", (getter)Pointer_get_contents,
(setter)Pointer_set_contents, (setter)Pointer_set_contents,
"the object this pointer points to (read-write)", NULL }, "the object this pointer points to (read-write)", NULL },
{ "_as_parameter_", (getter)Pointer_as_parameter, NULL,
"return a magic value so that this can be converted to a C parameter (readonly)",
NULL },
{ NULL, NULL } { NULL, NULL }
}; };
@ -4260,8 +4323,11 @@ Pointer_slice(PyObject *_self, Py_ssize_t ilow, Py_ssize_t ihigh)
len = ihigh - ilow; len = ihigh - ilow;
stgdict = PyObject_stgdict((PyObject *)self); stgdict = PyObject_stgdict((PyObject *)self);
assert(stgdict); /* Cannot be NULL fr pointer instances */
proto = stgdict->proto; proto = stgdict->proto;
assert(proto);
itemdict = PyType_stgdict(proto); itemdict = PyType_stgdict(proto);
assert(itemdict);
if (itemdict->getfunc == getentry("c")->getfunc) { if (itemdict->getfunc == getentry("c")->getfunc) {
char *ptr = *(char **)self->b_ptr; char *ptr = *(char **)self->b_ptr;
return PyString_FromStringAndSize(ptr + ilow, len); return PyString_FromStringAndSize(ptr + ilow, len);
@ -4676,7 +4742,7 @@ init_ctypes(void)
#endif #endif
PyModule_AddObject(m, "FUNCFLAG_CDECL", PyInt_FromLong(FUNCFLAG_CDECL)); PyModule_AddObject(m, "FUNCFLAG_CDECL", PyInt_FromLong(FUNCFLAG_CDECL));
PyModule_AddObject(m, "FUNCFLAG_PYTHONAPI", PyInt_FromLong(FUNCFLAG_PYTHONAPI)); PyModule_AddObject(m, "FUNCFLAG_PYTHONAPI", PyInt_FromLong(FUNCFLAG_PYTHONAPI));
PyModule_AddStringConstant(m, "__version__", "1.0.0"); PyModule_AddStringConstant(m, "__version__", "1.0.1");
PyModule_AddObject(m, "_memmove_addr", PyLong_FromVoidPtr(memmove)); PyModule_AddObject(m, "_memmove_addr", PyLong_FromVoidPtr(memmove));
PyModule_AddObject(m, "_memset_addr", PyLong_FromVoidPtr(memset)); PyModule_AddObject(m, "_memset_addr", PyLong_FromVoidPtr(memset));

View File

@ -197,18 +197,6 @@ EXPORT(PY_LONG_LONG) _testfunc_callback_q_qf(PY_LONG_LONG value,
#endif #endif
EXPORT(int) _testfunc_ppp(char ***p)
{
static char message[] = "Hello, World";
if (p) {
*p = (char **)malloc(sizeof(char *));
printf("malloc returned %p\n", *p);
**p = message;
return 1;
}
return 0;
}
typedef struct { typedef struct {
char *name; char *name;
char *value; char *value;

View File

@ -205,7 +205,7 @@ if (x == NULL) _AddTraceback(what, __FILE__, __LINE__ - 1), PyErr_Print()
result = PyObject_CallObject(callable, arglist); result = PyObject_CallObject(callable, arglist);
CHECK("'calling callback function'", result); CHECK("'calling callback function'", result);
if ((restype != &ffi_type_void) && result && result != Py_None) { if ((restype != &ffi_type_void) && result) {
PyObject *keep; PyObject *keep;
assert(setfunc); assert(setfunc);
#ifdef WORDS_BIGENDIAN #ifdef WORDS_BIGENDIAN
@ -225,13 +225,13 @@ if (x == NULL) _AddTraceback(what, __FILE__, __LINE__ - 1), PyErr_Print()
itself knows how to manage the refcount of these objects. itself knows how to manage the refcount of these objects.
*/ */
if (keep == NULL) /* Could not convert callback result. */ if (keep == NULL) /* Could not convert callback result. */
PyErr_WriteUnraisable(Py_None); PyErr_WriteUnraisable(callable);
else if (keep == Py_None) /* Nothing to keep */ else if (keep == Py_None) /* Nothing to keep */
Py_DECREF(keep); Py_DECREF(keep);
else if (setfunc != getentry("O")->setfunc) { else if (setfunc != getentry("O")->setfunc) {
if (-1 == PyErr_Warn(PyExc_RuntimeWarning, if (-1 == PyErr_Warn(PyExc_RuntimeWarning,
"memory leak in callback function.")) "memory leak in callback function."))
PyErr_WriteUnraisable(Py_None); PyErr_WriteUnraisable(callable);
} }
} }
Py_XDECREF(result); Py_XDECREF(result);

View File

@ -465,7 +465,21 @@ struct argument {
*/ */
static int ConvParam(PyObject *obj, int index, struct argument *pa) static int ConvParam(PyObject *obj, int index, struct argument *pa)
{ {
StgDictObject *dict;
pa->keep = NULL; /* so we cannot forget it later */ pa->keep = NULL; /* so we cannot forget it later */
dict = PyObject_stgdict(obj);
if (dict) {
PyCArgObject *carg;
assert(dict->paramfunc);
/* If it has an stgdict, it is a CDataObject */
carg = dict->paramfunc((CDataObject *)obj);
pa->ffi_type = carg->pffi_type;
memcpy(&pa->value, &carg->value, sizeof(pa->value));
pa->keep = (PyObject *)carg;
return 0;
}
if (PyCArg_CheckExact(obj)) { if (PyCArg_CheckExact(obj)) {
PyCArgObject *carg = (PyCArgObject *)obj; PyCArgObject *carg = (PyCArgObject *)obj;
pa->ffi_type = carg->pffi_type; pa->ffi_type = carg->pffi_type;
@ -548,25 +562,12 @@ static int ConvParam(PyObject *obj, int index, struct argument *pa)
as parameters (they have to expose the '_as_parameter_' as parameters (they have to expose the '_as_parameter_'
attribute) attribute)
*/ */
if (arg == 0) { if (arg) {
PyErr_Format(PyExc_TypeError, int result;
"Don't know how to convert parameter %d", index); result = ConvParam(arg, index, pa);
return -1; Py_DECREF(arg);
return result;
} }
if (PyCArg_CheckExact(arg)) {
PyCArgObject *carg = (PyCArgObject *)arg;
pa->ffi_type = carg->pffi_type;
memcpy(&pa->value, &carg->value, sizeof(pa->value));
pa->keep = arg;
return 0;
}
if (PyInt_Check(arg)) {
pa->ffi_type = &ffi_type_sint;
pa->value.i = PyInt_AS_LONG(arg);
pa->keep = arg;
return 0;
}
Py_DECREF(arg);
PyErr_Format(PyExc_TypeError, PyErr_Format(PyExc_TypeError,
"Don't know how to convert parameter %d", index); "Don't know how to convert parameter %d", index);
return -1; return -1;
@ -915,6 +916,10 @@ PyObject *_CallProc(PPROC pProc,
#endif #endif
args = (struct argument *)alloca(sizeof(struct argument) * argcount); args = (struct argument *)alloca(sizeof(struct argument) * argcount);
if (!args) {
PyErr_NoMemory();
return NULL;
}
memset(args, 0, sizeof(struct argument) * argcount); memset(args, 0, sizeof(struct argument) * argcount);
argtype_count = argtypes ? PyTuple_GET_SIZE(argtypes) : 0; argtype_count = argtypes ? PyTuple_GET_SIZE(argtypes) : 0;
#ifdef MS_WIN32 #ifdef MS_WIN32
@ -968,6 +973,10 @@ PyObject *_CallProc(PPROC pProc,
avalues = (void **)alloca(sizeof(void *) * argcount); avalues = (void **)alloca(sizeof(void *) * argcount);
atypes = (ffi_type **)alloca(sizeof(ffi_type *) * argcount); atypes = (ffi_type **)alloca(sizeof(ffi_type *) * argcount);
if (!resbuf || !avalues || !atypes) {
PyErr_NoMemory();
goto cleanup;
}
for (i = 0; i < argcount; ++i) { for (i = 0; i < argcount; ++i) {
atypes[i] = args[i].ffi_type; atypes[i] = args[i].ffi_type;
if (atypes[i]->type == FFI_TYPE_STRUCT) if (atypes[i]->type == FFI_TYPE_STRUCT)
@ -1068,6 +1077,11 @@ static PyObject *load_library(PyObject *self, PyObject *args)
return NULL; return NULL;
#ifdef _UNICODE #ifdef _UNICODE
name = alloca((PyString_Size(nameobj) + 1) * sizeof(WCHAR)); name = alloca((PyString_Size(nameobj) + 1) * sizeof(WCHAR));
if (!name) {
PyErr_NoMemory();
return NULL;
}
{ {
int r; int r;
char *aname = PyString_AsString(nameobj); char *aname = PyString_AsString(nameobj);

View File

@ -105,6 +105,12 @@ CField_FromDesc(PyObject *desc, int index,
StgDictObject *idict; StgDictObject *idict;
if (adict && adict->proto) { if (adict && adict->proto) {
idict = PyType_stgdict(adict->proto); idict = PyType_stgdict(adict->proto);
if (!idict) {
PyErr_SetString(PyExc_TypeError,
"has no _stginfo_");
Py_DECREF(self);
return NULL;
}
if (idict->getfunc == getentry("c")->getfunc) { if (idict->getfunc == getentry("c")->getfunc) {
struct fielddesc *fd = getentry("s"); struct fielddesc *fd = getentry("s");
getfunc = fd->getfunc; getfunc = fd->getfunc;
@ -1094,7 +1100,7 @@ O_get(void *ptr, unsigned size)
if (!PyErr_Occurred()) if (!PyErr_Occurred())
/* Set an error if not yet set */ /* Set an error if not yet set */
PyErr_SetString(PyExc_ValueError, PyErr_SetString(PyExc_ValueError,
"PyObject is NULL?"); "PyObject is NULL");
return NULL; return NULL;
} }
Py_INCREF(ob); Py_INCREF(ob);

View File

@ -23,9 +23,11 @@ typedef int Py_ssize_t;
#define PY_LONG_LONG LONG_LONG #define PY_LONG_LONG LONG_LONG
#endif #endif
typedef struct tagPyCArgObject PyCArgObject;
typedef struct tagCDataObject CDataObject; typedef struct tagCDataObject CDataObject;
typedef PyObject *(* GETFUNC)(void *, unsigned size); typedef PyObject *(* GETFUNC)(void *, unsigned size);
typedef PyObject *(* SETFUNC)(void *, PyObject *value, unsigned size); typedef PyObject *(* SETFUNC)(void *, PyObject *value, unsigned size);
typedef PyCArgObject *(* PARAMFUNC)(CDataObject *obj);
/* A default buffer in CDataObject, which can be used for small C types. If /* A default buffer in CDataObject, which can be used for small C types. If
this buffer is too small, PyMem_Malloc will be called to create a larger one, this buffer is too small, PyMem_Malloc will be called to create a larger one,
@ -205,6 +207,7 @@ typedef struct {
PyObject *proto; /* Only for Pointer/ArrayObject */ PyObject *proto; /* Only for Pointer/ArrayObject */
SETFUNC setfunc; /* Only for simple objects */ SETFUNC setfunc; /* Only for simple objects */
GETFUNC getfunc; /* Only for simple objects */ GETFUNC getfunc; /* Only for simple objects */
PARAMFUNC paramfunc;
/* Following fields only used by CFuncPtrType_Type instances */ /* Following fields only used by CFuncPtrType_Type instances */
PyObject *argtypes; /* tuple of CDataObjects */ PyObject *argtypes; /* tuple of CDataObjects */
@ -283,7 +286,7 @@ PyObject *_CallProc(PPROC pProc,
#define DICTFLAG_FINAL 0x1000 #define DICTFLAG_FINAL 0x1000
typedef struct { struct tagPyCArgObject {
PyObject_HEAD PyObject_HEAD
ffi_type *pffi_type; ffi_type *pffi_type;
char tag; char tag;
@ -302,7 +305,7 @@ typedef struct {
} value; } value;
PyObject *obj; PyObject *obj;
int size; /* for the 'V' tag */ int size; /* for the 'V' tag */
} PyCArgObject; };
extern PyTypeObject PyCArg_Type; extern PyTypeObject PyCArg_Type;
extern PyCArgObject *new_CArgObject(void); extern PyCArgObject *new_CArgObject(void);

View File

@ -3470,6 +3470,7 @@ fi
TARGETDIR="unknown" TARGETDIR="unknown"
case "$host" in case "$host" in
x86_64-*-openbsd*) TARGET=X86_64; TARGETDIR=x86;;
mips*-*-openbsd*) TARGET=MIPS; TARGETDIR=mips;; mips*-*-openbsd*) TARGET=MIPS; TARGETDIR=mips;;
sparc-*-openbsd*) TARGET=SPARC; TARGETDIR=sparc;; sparc-*-openbsd*) TARGET=SPARC; TARGETDIR=sparc;;
sparc64-*-openbsd*) TARGET=SPARC; TARGETDIR=sparc;; sparc64-*-openbsd*) TARGET=SPARC; TARGETDIR=sparc;;

View File

@ -21,6 +21,7 @@ AC_FUNC_MMAP_BLACKLIST
TARGETDIR="unknown" TARGETDIR="unknown"
case "$host" in case "$host" in
x86_64-*-openbsd*) TARGET=X86_64; TARGETDIR=x86;;
mips*-*-openbsd*) TARGET=MIPS; TARGETDIR=mips;; mips*-*-openbsd*) TARGET=MIPS; TARGETDIR=mips;;
sparc-*-openbsd*) TARGET=SPARC; TARGETDIR=sparc;; sparc-*-openbsd*) TARGET=SPARC; TARGETDIR=sparc;;
sparc64-*-openbsd*) TARGET=SPARC; TARGETDIR=sparc;; sparc64-*-openbsd*) TARGET=SPARC; TARGETDIR=sparc;;

View File

@ -43,6 +43,3 @@ ffi_sources += ffi_platforms['@MKTARGET@']
ffi_sources = [os.path.join('@srcdir@', f) for f in ffi_sources] ffi_sources = [os.path.join('@srcdir@', f) for f in ffi_sources]
ffi_cflags = '@CFLAGS@' ffi_cflags = '@CFLAGS@'
# I think this may no longer be needed:
if sys.platform == "openbsd3":
ffi_cflags += " -fno-stack-protector"

View File

@ -204,12 +204,12 @@ MakeFields(PyObject *type, CFieldObject *descr,
continue; continue;
} }
new_descr = (CFieldObject *)PyObject_CallObject((PyObject *)&CField_Type, NULL); new_descr = (CFieldObject *)PyObject_CallObject((PyObject *)&CField_Type, NULL);
assert(new_descr->ob_type == &CField_Type);
if (new_descr == NULL) { if (new_descr == NULL) {
Py_DECREF(fdescr); Py_DECREF(fdescr);
Py_DECREF(fieldlist); Py_DECREF(fieldlist);
return -1; return -1;
} }
assert(new_descr->ob_type == &CField_Type);
new_descr->size = fdescr->size; new_descr->size = fdescr->size;
new_descr->offset = fdescr->offset + offset; new_descr->offset = fdescr->offset + offset;
new_descr->index = fdescr->index + index; new_descr->index = fdescr->index + index;

View File

@ -1784,7 +1784,6 @@ static PyObject *
PyCurses_InitScr(PyObject *self) PyCurses_InitScr(PyObject *self)
{ {
WINDOW *win; WINDOW *win;
PyObject *nlines, *cols;
if (initialised == TRUE) { if (initialised == TRUE) {
wrefresh(stdscr); wrefresh(stdscr);
@ -1803,7 +1802,12 @@ PyCurses_InitScr(PyObject *self)
/* This was moved from initcurses() because it core dumped on SGI, /* This was moved from initcurses() because it core dumped on SGI,
where they're not defined until you've called initscr() */ where they're not defined until you've called initscr() */
#define SetDictInt(string,ch) \ #define SetDictInt(string,ch) \
PyDict_SetItemString(ModDict,string,PyInt_FromLong((long) (ch))); do { \
PyObject *o = PyInt_FromLong((long) (ch)); \
if (o && PyDict_SetItemString(ModDict, string, o) == 0) { \
Py_DECREF(o); \
} \
} while (0)
/* Here are some graphic symbols you can use */ /* Here are some graphic symbols you can use */
SetDictInt("ACS_ULCORNER", (ACS_ULCORNER)); SetDictInt("ACS_ULCORNER", (ACS_ULCORNER));
@ -1872,12 +1876,8 @@ PyCurses_InitScr(PyObject *self)
SetDictInt("ACS_STERLING", (ACS_STERLING)); SetDictInt("ACS_STERLING", (ACS_STERLING));
#endif #endif
nlines = PyInt_FromLong((long) LINES); SetDictInt("LINES", LINES);
PyDict_SetItemString(ModDict, "LINES", nlines); SetDictInt("COLS", COLS);
Py_DECREF(nlines);
cols = PyInt_FromLong((long) COLS);
PyDict_SetItemString(ModDict, "COLS", cols);
Py_DECREF(cols);
return (PyObject *)PyCursesWindow_New(win); return (PyObject *)PyCursesWindow_New(win);
} }
@ -2554,6 +2554,8 @@ init_curses(void)
/* Add some symbolic constants to the module */ /* Add some symbolic constants to the module */
d = PyModule_GetDict(m); d = PyModule_GetDict(m);
if (d == NULL)
return;
ModDict = d; /* For PyCurses_InitScr to use later */ ModDict = d; /* For PyCurses_InitScr to use later */
/* Add a CObject for the C API */ /* Add a CObject for the C API */
@ -2667,6 +2669,10 @@ init_curses(void)
if (strncmp(key_n,"KEY_F(",6)==0) { if (strncmp(key_n,"KEY_F(",6)==0) {
char *p1, *p2; char *p1, *p2;
key_n2 = malloc(strlen(key_n)+1); key_n2 = malloc(strlen(key_n)+1);
if (!key_n2) {
PyErr_NoMemory();
break;
}
p1 = key_n; p1 = key_n;
p2 = key_n2; p2 = key_n2;
while (*p1) { while (*p1) {
@ -2679,7 +2685,7 @@ init_curses(void)
*p2 = (char)0; *p2 = (char)0;
} else } else
key_n2 = key_n; key_n2 = key_n;
PyDict_SetItemString(d,key_n2,PyInt_FromLong((long) key)); SetDictInt(key_n2,key);
if (key_n2 != key_n) if (key_n2 != key_n)
free(key_n2); free(key_n2);
} }

View File

@ -48,7 +48,7 @@
#include "Python.h" #include "Python.h"
#define VERSION "1.0.6-snapshot" #define VERSION "1.0.6"
/* -------------------------------------------------------------------- */ /* -------------------------------------------------------------------- */
/* configuration */ /* configuration */
@ -809,7 +809,7 @@ element_findtext(ElementObject* self, PyObject* args)
PyObject* text = element_get_text(item); PyObject* text = element_get_text(item);
if (text == Py_None) if (text == Py_None)
return PyString_FromString(""); return PyString_FromString("");
Py_INCREF(text); Py_XINCREF(text);
return text; return text;
} }
} }
@ -1599,6 +1599,10 @@ LOCAL(PyObject*)
treebuilder_handle_data(TreeBuilderObject* self, PyObject* data) treebuilder_handle_data(TreeBuilderObject* self, PyObject* data)
{ {
if (!self->data) { if (!self->data) {
if (self->last == (ElementObject*) Py_None) {
/* ignore calls to data before the first call to start */
Py_RETURN_NONE;
}
/* store the first item as is */ /* store the first item as is */
Py_INCREF(data); self->data = data; Py_INCREF(data); self->data = data;
} else { } else {

View File

@ -313,6 +313,11 @@ unpack_string(LogReaderObject *self, PyObject **pvalue)
return err; return err;
buf = (char *)malloc(len); buf = (char *)malloc(len);
if (!buf) {
PyErr_NoMemory();
return ERR_EXCEPTION;
}
for (i=0; i < len; i++) { for (i=0; i < len; i++) {
ch = fgetc(self->logfp); ch = fgetc(self->logfp);
buf[i] = ch; buf[i] = ch;

View File

@ -1166,6 +1166,9 @@ entrance:
/* install new repeat context */ /* install new repeat context */
ctx->u.rep = (SRE_REPEAT*) PyObject_MALLOC(sizeof(*ctx->u.rep)); ctx->u.rep = (SRE_REPEAT*) PyObject_MALLOC(sizeof(*ctx->u.rep));
/* XXX(nnorwitz): anything else we need to do on error? */
if (!ctx->u.rep)
RETURN_FAILURE;
ctx->u.rep->count = -1; ctx->u.rep->count = -1;
ctx->u.rep->pattern = ctx->pattern; ctx->u.rep->pattern = ctx->pattern;
ctx->u.rep->prev = state->repeat; ctx->u.rep->prev = state->repeat;

View File

@ -2493,8 +2493,10 @@ Tkapp_CreateTimerHandler(PyObject *self, PyObject *args)
} }
v = Tktt_New(func); v = Tktt_New(func);
v->token = Tcl_CreateTimerHandler(milliseconds, TimerHandler, if (v) {
(ClientData)v); v->token = Tcl_CreateTimerHandler(milliseconds, TimerHandler,
(ClientData)v);
}
return (PyObject *) v; return (PyObject *) v;
} }

View File

@ -702,6 +702,8 @@ array_ass_slice(arrayobject *a, Py_ssize_t ilow, Py_ssize_t ihigh, PyObject *v)
/* Special case "a[i:j] = a" -- copy b first */ /* Special case "a[i:j] = a" -- copy b first */
int ret; int ret;
v = array_slice(b, 0, n); v = array_slice(b, 0, n);
if (!v)
return -1;
ret = array_ass_slice(a, ilow, ihigh, v); ret = array_ass_slice(a, ilow, ihigh, v);
Py_DECREF(v); Py_DECREF(v);
return ret; return ret;
@ -1573,9 +1575,8 @@ array_repr(arrayobject *a)
static PyObject* static PyObject*
array_subscr(arrayobject* self, PyObject* item) array_subscr(arrayobject* self, PyObject* item)
{ {
PyNumberMethods *nb = item->ob_type->tp_as_number; if (PyIndex_Check(item)) {
if (nb != NULL && nb->nb_index != NULL) { Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
Py_ssize_t i = nb->nb_index(item);
if (i==-1 && PyErr_Occurred()) { if (i==-1 && PyErr_Occurred()) {
return NULL; return NULL;
} }
@ -1623,9 +1624,8 @@ array_subscr(arrayobject* self, PyObject* item)
static int static int
array_ass_subscr(arrayobject* self, PyObject* item, PyObject* value) array_ass_subscr(arrayobject* self, PyObject* item, PyObject* value)
{ {
PyNumberMethods *nb = item->ob_type->tp_as_number; if (PyIndex_Check(item)) {
if (nb != NULL && nb->nb_index != NULL) { Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
Py_ssize_t i = nb->nb_index(item);
if (i==-1 && PyErr_Occurred()) if (i==-1 && PyErr_Occurred())
return -1; return -1;
if (i < 0) if (i < 0)
@ -1706,6 +1706,8 @@ array_ass_subscr(arrayobject* self, PyObject* item, PyObject* value)
if (self == av) { if (self == av) {
value = array_slice(av, 0, av->ob_size); value = array_slice(av, 0, av->ob_size);
av = (arrayobject*)value; av = (arrayobject*)value;
if (!av)
return -1;
} }
else { else {
Py_INCREF(value); Py_INCREF(value);

View File

@ -805,12 +805,12 @@ BZ2File_write(BZ2FileObject *self, PyObject *args)
case MODE_CLOSED: case MODE_CLOSED:
PyErr_SetString(PyExc_ValueError, PyErr_SetString(PyExc_ValueError,
"I/O operation on closed file"); "I/O operation on closed file");
goto cleanup;; goto cleanup;
default: default:
PyErr_SetString(PyExc_IOError, PyErr_SetString(PyExc_IOError,
"file is not ready for writing"); "file is not ready for writing");
goto cleanup;; goto cleanup;
} }
self->f_softspace = 0; self->f_softspace = 0;
@ -854,6 +854,21 @@ BZ2File_writelines(BZ2FileObject *self, PyObject *seq)
int bzerror; int bzerror;
ACQUIRE_LOCK(self); ACQUIRE_LOCK(self);
switch (self->mode) {
case MODE_WRITE:
break;
case MODE_CLOSED:
PyErr_SetString(PyExc_ValueError,
"I/O operation on closed file");
goto error;
default:
PyErr_SetString(PyExc_IOError,
"file is not ready for writing");
goto error;
}
islist = PyList_Check(seq); islist = PyList_Check(seq);
if (!islist) { if (!islist) {
iter = PyObject_GetIter(seq); iter = PyObject_GetIter(seq);

View File

@ -211,6 +211,7 @@ deque_popleft(dequeobject *deque, PyObject *unused)
PyErr_SetString(PyExc_IndexError, "pop from an empty deque"); PyErr_SetString(PyExc_IndexError, "pop from an empty deque");
return NULL; return NULL;
} }
assert(deque->leftblock != NULL);
item = deque->leftblock->data[deque->leftindex]; item = deque->leftblock->data[deque->leftindex];
deque->leftindex++; deque->leftindex++;
deque->len--; deque->len--;
@ -226,7 +227,6 @@ deque_popleft(dequeobject *deque, PyObject *unused)
} else { } else {
assert(deque->leftblock != deque->rightblock); assert(deque->leftblock != deque->rightblock);
prevblock = deque->leftblock->rightlink; prevblock = deque->leftblock->rightlink;
assert(deque->leftblock != NULL);
PyMem_Free(deque->leftblock); PyMem_Free(deque->leftblock);
assert(prevblock != NULL); assert(prevblock != NULL);
prevblock->leftlink = NULL; prevblock->leftlink = NULL;

View File

@ -2795,6 +2795,8 @@ storeAtts(XML_Parser parser, const ENCODING *enc,
unsigned long uriHash = 0; unsigned long uriHash = 0;
((XML_Char *)s)[-1] = 0; /* clear flag */ ((XML_Char *)s)[-1] = 0; /* clear flag */
id = (ATTRIBUTE_ID *)lookup(&dtd->attributeIds, s, 0); id = (ATTRIBUTE_ID *)lookup(&dtd->attributeIds, s, 0);
if (!id)
return XML_ERROR_NO_MEMORY;
b = id->prefix->binding; b = id->prefix->binding;
if (!b) if (!b)
return XML_ERROR_UNBOUND_PREFIX; return XML_ERROR_UNBOUND_PREFIX;
@ -5340,6 +5342,8 @@ getAttributeId(XML_Parser parser, const ENCODING *enc,
return NULL; return NULL;
id->prefix = (PREFIX *)lookup(&dtd->prefixes, poolStart(&dtd->pool), id->prefix = (PREFIX *)lookup(&dtd->prefixes, poolStart(&dtd->pool),
sizeof(PREFIX)); sizeof(PREFIX));
if (!id->prefix)
return NULL;
if (id->prefix->name == poolStart(&dtd->pool)) if (id->prefix->name == poolStart(&dtd->pool))
poolFinish(&dtd->pool); poolFinish(&dtd->pool);
else else

View File

@ -815,31 +815,19 @@ static PyTypeObject mmap_object_type = {
static Py_ssize_t static Py_ssize_t
_GetMapSize(PyObject *o) _GetMapSize(PyObject *o)
{ {
PyNumberMethods *nb = o->ob_type->tp_as_number; if (PyIndex_Check(o)) {
if (nb != NULL && nb->nb_index != NULL) { Py_ssize_t i = PyNumber_AsSsize_t(o, PyExc_OverflowError);
Py_ssize_t i = nb->nb_index(o);
if (i==-1 && PyErr_Occurred()) if (i==-1 && PyErr_Occurred())
return -1; return -1;
if (i < 0) if (i < 0) {
goto onnegoverflow; PyErr_SetString(PyExc_OverflowError,
if (i==PY_SSIZE_T_MAX) "memory mapped size must be positive");
goto onposoverflow; return -1;
}
return i; return i;
} }
else {
PyErr_SetString(PyExc_TypeError,
"map size must be an integral value");
return -1;
}
onnegoverflow: PyErr_SetString(PyExc_TypeError, "map size must be an integral value");
PyErr_SetString(PyExc_OverflowError,
"memory mapped size must be positive");
return -1;
onposoverflow:
PyErr_SetString(PyExc_OverflowError,
"memory mapped size is too large (limited by C int)");
return -1; return -1;
} }
@ -1138,6 +1126,15 @@ static struct PyMethodDef mmap_functions[] = {
{NULL, NULL} /* Sentinel */ {NULL, NULL} /* Sentinel */
}; };
static void
setint(PyObject *d, const char *name, long value)
{
PyObject *o = PyInt_FromLong(value);
if (o && PyDict_SetItemString(d, name, o) == 0) {
Py_DECREF(o);
}
}
PyMODINIT_FUNC PyMODINIT_FUNC
initmmap(void) initmmap(void)
{ {
@ -1150,47 +1147,40 @@ PyMODINIT_FUNC
if (module == NULL) if (module == NULL)
return; return;
dict = PyModule_GetDict(module); dict = PyModule_GetDict(module);
if (!dict)
return;
mmap_module_error = PyExc_EnvironmentError; mmap_module_error = PyExc_EnvironmentError;
Py_INCREF(mmap_module_error);
PyDict_SetItemString(dict, "error", mmap_module_error); PyDict_SetItemString(dict, "error", mmap_module_error);
#ifdef PROT_EXEC #ifdef PROT_EXEC
PyDict_SetItemString(dict, "PROT_EXEC", PyInt_FromLong(PROT_EXEC) ); setint(dict, "PROT_EXEC", PROT_EXEC);
#endif #endif
#ifdef PROT_READ #ifdef PROT_READ
PyDict_SetItemString(dict, "PROT_READ", PyInt_FromLong(PROT_READ) ); setint(dict, "PROT_READ", PROT_READ);
#endif #endif
#ifdef PROT_WRITE #ifdef PROT_WRITE
PyDict_SetItemString(dict, "PROT_WRITE", PyInt_FromLong(PROT_WRITE) ); setint(dict, "PROT_WRITE", PROT_WRITE);
#endif #endif
#ifdef MAP_SHARED #ifdef MAP_SHARED
PyDict_SetItemString(dict, "MAP_SHARED", PyInt_FromLong(MAP_SHARED) ); setint(dict, "MAP_SHARED", MAP_SHARED);
#endif #endif
#ifdef MAP_PRIVATE #ifdef MAP_PRIVATE
PyDict_SetItemString(dict, "MAP_PRIVATE", setint(dict, "MAP_PRIVATE", MAP_PRIVATE);
PyInt_FromLong(MAP_PRIVATE) );
#endif #endif
#ifdef MAP_DENYWRITE #ifdef MAP_DENYWRITE
PyDict_SetItemString(dict, "MAP_DENYWRITE", setint(dict, "MAP_DENYWRITE", MAP_DENYWRITE);
PyInt_FromLong(MAP_DENYWRITE) );
#endif #endif
#ifdef MAP_EXECUTABLE #ifdef MAP_EXECUTABLE
PyDict_SetItemString(dict, "MAP_EXECUTABLE", setint(dict, "MAP_EXECUTABLE", MAP_EXECUTABLE);
PyInt_FromLong(MAP_EXECUTABLE) );
#endif #endif
#ifdef MAP_ANONYMOUS #ifdef MAP_ANONYMOUS
PyDict_SetItemString(dict, "MAP_ANON", PyInt_FromLong(MAP_ANONYMOUS) ); setint(dict, "MAP_ANON", MAP_ANONYMOUS);
PyDict_SetItemString(dict, "MAP_ANONYMOUS", setint(dict, "MAP_ANONYMOUS", MAP_ANONYMOUS);
PyInt_FromLong(MAP_ANONYMOUS) );
#endif #endif
PyDict_SetItemString(dict, "PAGESIZE", setint(dict, "PAGESIZE", (long)my_getpagesize());
PyInt_FromLong((long)my_getpagesize()));
PyDict_SetItemString(dict, "ACCESS_READ", setint(dict, "ACCESS_READ", ACCESS_READ);
PyInt_FromLong(ACCESS_READ)); setint(dict, "ACCESS_WRITE", ACCESS_WRITE);
PyDict_SetItemString(dict, "ACCESS_WRITE", setint(dict, "ACCESS_COPY", ACCESS_COPY);
PyInt_FromLong(ACCESS_WRITE));
PyDict_SetItemString(dict, "ACCESS_COPY",
PyInt_FromLong(ACCESS_COPY));
} }

View File

@ -137,15 +137,7 @@ op_ipow(PyObject *s, PyObject *a)
static PyObject * static PyObject *
op_index(PyObject *s, PyObject *a) op_index(PyObject *s, PyObject *a)
{ {
Py_ssize_t i; return PyNumber_Index(a);
PyObject *a1;
if (!PyArg_UnpackTuple(a,"index", 1, 1, &a1))
return NULL;
i = PyNumber_Index(a1);
if (i == -1 && PyErr_Occurred())
return NULL;
else
return PyInt_FromSsize_t(i);
} }
static PyObject* static PyObject*
@ -247,7 +239,7 @@ spam1o(isMappingType,
spam1(is_, "is_(a, b) -- Same as a is b.") spam1(is_, "is_(a, b) -- Same as a is b.")
spam1(is_not, "is_not(a, b) -- Same as a is not b.") spam1(is_not, "is_not(a, b) -- Same as a is not b.")
spam2(index, __index__, "index(a) -- Same as a.__index__()") spam2o(index, __index__, "index(a) -- Same as a.__index__()")
spam2(add,__add__, "add(a, b) -- Same as a + b.") spam2(add,__add__, "add(a, b) -- Same as a + b.")
spam2(sub,__sub__, "sub(a, b) -- Same as a - b.") spam2(sub,__sub__, "sub(a, b) -- Same as a - b.")
spam2(mul,__mul__, "mul(a, b) -- Same as a * b.") spam2(mul,__mul__, "mul(a, b) -- Same as a * b.")

View File

@ -1121,6 +1121,8 @@ fill_time(PyObject *v, int index, time_t sec, unsigned long nsec)
#else #else
ival = PyInt_FromLong((long)sec); ival = PyInt_FromLong((long)sec);
#endif #endif
if (!ival)
return;
if (_stat_float_times) { if (_stat_float_times) {
fval = PyFloat_FromDouble(sec + 1e-9*nsec); fval = PyFloat_FromDouble(sec + 1e-9*nsec);
} else { } else {

View File

@ -410,6 +410,11 @@ longimagedata(PyObject *self, PyObject *args)
addlongimgtag(base, xsize, ysize); addlongimgtag(base, xsize, ysize);
#endif #endif
verdat = (unsigned char *)malloc(xsize); verdat = (unsigned char *)malloc(xsize);
if (!verdat) {
Py_CLEAR(rv);
goto finally;
}
fseek(inf, 512, SEEK_SET); fseek(inf, 512, SEEK_SET);
for (z = 0; z < zsize; z++) { for (z = 0; z < zsize; z++) {
lptr = base; lptr = base;
@ -431,10 +436,14 @@ longimagedata(PyObject *self, PyObject *args)
copybw((Py_Int32 *) base, xsize * ysize); copybw((Py_Int32 *) base, xsize * ysize);
} }
finally: finally:
free(starttab); if (starttab)
free(lengthtab); free(starttab);
free(rledat); if (lengthtab)
free(verdat); free(lengthtab);
if (rledat)
free(rledat);
if (verdat)
free(verdat);
fclose(inf); fclose(inf);
return rv; return rv;
} }

View File

@ -3041,17 +3041,20 @@ gethost_common(struct hostent *h, struct sockaddr *addr, int alen, int af)
if ((addr_list = PyList_New(0)) == NULL) if ((addr_list = PyList_New(0)) == NULL)
goto err; goto err;
for (pch = h->h_aliases; *pch != NULL; pch++) { /* SF #1511317: h_aliases can be NULL */
int status; if (h->h_aliases) {
tmp = PyString_FromString(*pch); for (pch = h->h_aliases; *pch != NULL; pch++) {
if (tmp == NULL) int status;
goto err; tmp = PyString_FromString(*pch);
if (tmp == NULL)
goto err;
status = PyList_Append(name_list, tmp); status = PyList_Append(name_list, tmp);
Py_DECREF(tmp); Py_DECREF(tmp);
if (status) if (status)
goto err; goto err;
}
} }
for (pch = h->h_addr_list; *pch != NULL; pch++) { for (pch = h->h_addr_list; *pch != NULL; pch++) {

View File

@ -1,8 +1,8 @@
/* ------------------------------------------------------------------------ /* ------------------------------------------------------------------------
unicodedata -- Provides access to the Unicode 3.2 data base. unicodedata -- Provides access to the Unicode 4.1 data base.
Data was extracted from the Unicode 3.2 UnicodeData.txt file. Data was extracted from the Unicode 4.1 UnicodeData.txt file.
Written by Marc-Andre Lemburg (mal@lemburg.com). Written by Marc-Andre Lemburg (mal@lemburg.com).
Modified for Python 2.0 by Fredrik Lundh (fredrik@pythonware.com) Modified for Python 2.0 by Fredrik Lundh (fredrik@pythonware.com)
@ -1078,6 +1078,7 @@ unicodedata_lookup(PyObject* self, PyObject* args)
{ {
Py_UCS4 code; Py_UCS4 code;
Py_UNICODE str[1]; Py_UNICODE str[1];
char errbuf[256];
char* name; char* name;
int namelen; int namelen;
@ -1085,11 +1086,19 @@ unicodedata_lookup(PyObject* self, PyObject* args)
return NULL; return NULL;
if (!_getcode(self, name, namelen, &code)) { if (!_getcode(self, name, namelen, &code)) {
/* XXX(nnorwitz): why are we allocating for the error msg?
Why not always use snprintf? */
char fmt[] = "undefined character name '%s'"; char fmt[] = "undefined character name '%s'";
char *buf = PyMem_MALLOC(sizeof(fmt) + namelen); char *buf = PyMem_MALLOC(sizeof(fmt) + namelen);
sprintf(buf, fmt, name); if (buf)
sprintf(buf, fmt, name);
else {
buf = errbuf;
PyOS_snprintf(buf, sizeof(errbuf), fmt, name);
}
PyErr_SetString(PyExc_KeyError, buf); PyErr_SetString(PyExc_KeyError, buf);
PyMem_FREE(buf); if (buf != errbuf)
PyMem_FREE(buf);
return NULL; return NULL;
} }
@ -1173,11 +1182,11 @@ PyDoc_STRVAR(unicodedata_docstring,
"This module provides access to the Unicode Character Database which\n\ "This module provides access to the Unicode Character Database which\n\
defines character properties for all Unicode characters. The data in\n\ defines character properties for all Unicode characters. The data in\n\
this database is based on the UnicodeData.txt file version\n\ this database is based on the UnicodeData.txt file version\n\
3.2.0 which is publically available from ftp://ftp.unicode.org/.\n\ 4.1.0 which is publically available from ftp://ftp.unicode.org/.\n\
\n\ \n\
The module uses the same names and symbols as defined by the\n\ The module uses the same names and symbols as defined by the\n\
UnicodeData File Format 3.2.0 (see\n\ UnicodeData File Format 4.1.0 (see\n\
http://www.unicode.org/Public/3.2-Update/UnicodeData-3.2.0.html)."); http://www.unicode.org/Public/4.1.0/ucd/UCD.html).");
PyMODINIT_FUNC PyMODINIT_FUNC
initunicodedata(void) initunicodedata(void)

View File

@ -950,6 +950,9 @@ normalize_line_endings(PyObject *source)
char *buf, *q, *p = PyString_AsString(source); char *buf, *q, *p = PyString_AsString(source);
PyObject *fixed_source; PyObject *fixed_source;
if (!p)
return NULL;
/* one char extra for trailing \n and one for terminating \0 */ /* one char extra for trailing \n and one for terminating \0 */
buf = (char *)PyMem_Malloc(PyString_Size(source) + 2); buf = (char *)PyMem_Malloc(PyString_Size(source) + 2);
if (buf == NULL) { if (buf == NULL) {

Some files were not shown because too many files have changed in this diff Show More