mirror of
https://github.com/python/cpython.git
synced 2025-07-24 11:44:31 +00:00

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) ........
2815 lines
72 KiB
C
2815 lines
72 KiB
C
/*
|
|
* ElementTree
|
|
* $Id: _elementtree.c 2657 2006-03-12 20:50:32Z fredrik $
|
|
*
|
|
* elementtree accelerator
|
|
*
|
|
* History:
|
|
* 1999-06-20 fl created (as part of sgmlop)
|
|
* 2001-05-29 fl effdom edition
|
|
* 2001-06-05 fl backported to unix; fixed bogus free in clear
|
|
* 2001-07-10 fl added findall helper
|
|
* 2003-02-27 fl elementtree edition (alpha)
|
|
* 2004-06-03 fl updates for elementtree 1.2
|
|
* 2005-01-05 fl added universal name cache, Element/SubElement factories
|
|
* 2005-01-06 fl moved python helpers into C module; removed 1.5.2 support
|
|
* 2005-01-07 fl added 2.1 support; work around broken __copy__ in 2.3
|
|
* 2005-01-08 fl added makeelement method; fixed path support
|
|
* 2005-01-10 fl optimized memory usage
|
|
* 2005-01-11 fl first public release (cElementTree 0.8)
|
|
* 2005-01-12 fl split element object into base and extras
|
|
* 2005-01-13 fl use tagged pointers for tail/text (cElementTree 0.9)
|
|
* 2005-01-17 fl added treebuilder close method
|
|
* 2005-01-17 fl fixed crash in getchildren
|
|
* 2005-01-18 fl removed observer api, added iterparse (cElementTree 0.9.3)
|
|
* 2005-01-23 fl revised iterparse api; added namespace event support (0.9.8)
|
|
* 2005-01-26 fl added VERSION module property (cElementTree 1.0)
|
|
* 2005-01-28 fl added remove method (1.0.1)
|
|
* 2005-03-01 fl added iselement function; fixed makeelement aliasing (1.0.2)
|
|
* 2005-03-13 fl export Comment and ProcessingInstruction/PI helpers
|
|
* 2005-03-26 fl added Comment and PI support to XMLParser
|
|
* 2005-03-27 fl event optimizations; complain about bogus events
|
|
* 2005-08-08 fl fixed read error handling in parse
|
|
* 2005-08-11 fl added runtime test for copy workaround (1.0.3)
|
|
* 2005-12-13 fl added expat_capi support (for xml.etree) (1.0.4)
|
|
* 2005-12-16 fl added support for non-standard encodings
|
|
* 2006-03-08 fl fixed a couple of potential null-refs and leaks
|
|
* 2006-03-12 fl merge in 2.5 ssize_t changes
|
|
*
|
|
* Copyright (c) 1999-2006 by Secret Labs AB. All rights reserved.
|
|
* Copyright (c) 1999-2006 by Fredrik Lundh.
|
|
*
|
|
* info@pythonware.com
|
|
* http://www.pythonware.com
|
|
*/
|
|
|
|
/* Licensed to PSF under a Contributor Agreement. */
|
|
/* See http://www.python.org/2.4/license for licensing details. */
|
|
|
|
#include "Python.h"
|
|
|
|
#define VERSION "1.0.6"
|
|
|
|
/* -------------------------------------------------------------------- */
|
|
/* configuration */
|
|
|
|
/* Leave defined to include the expat-based XMLParser type */
|
|
#define USE_EXPAT
|
|
|
|
/* Define to to all expat calls via pyexpat's embedded expat library */
|
|
/* #define USE_PYEXPAT_CAPI */
|
|
|
|
/* An element can hold this many children without extra memory
|
|
allocations. */
|
|
#define STATIC_CHILDREN 4
|
|
|
|
/* For best performance, chose a value so that 80-90% of all nodes
|
|
have no more than the given number of children. Set this to zero
|
|
to minimize the size of the element structure itself (this only
|
|
helps if you have lots of leaf nodes with attributes). */
|
|
|
|
/* Also note that pymalloc always allocates blocks in multiples of
|
|
eight bytes. For the current version of cElementTree, this means
|
|
that the number of children should be an even number, at least on
|
|
32-bit platforms. */
|
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
#if 0
|
|
static int memory = 0;
|
|
#define ALLOC(size, comment)\
|
|
do { memory += size; printf("%8d - %s\n", memory, comment); } while (0)
|
|
#define RELEASE(size, comment)\
|
|
do { memory -= size; printf("%8d - %s\n", memory, comment); } while (0)
|
|
#else
|
|
#define ALLOC(size, comment)
|
|
#define RELEASE(size, comment)
|
|
#endif
|
|
|
|
/* compiler tweaks */
|
|
#if defined(_MSC_VER)
|
|
#define LOCAL(type) static __inline type __fastcall
|
|
#else
|
|
#define LOCAL(type) static type
|
|
#endif
|
|
|
|
/* compatibility macros */
|
|
#if (PY_VERSION_HEX < 0x02050000)
|
|
typedef int Py_ssize_t;
|
|
#define lenfunc inquiry
|
|
#endif
|
|
|
|
#if (PY_VERSION_HEX < 0x02040000)
|
|
#define PyDict_CheckExact PyDict_Check
|
|
#if (PY_VERSION_HEX < 0x02020000)
|
|
#define PyList_CheckExact PyList_Check
|
|
#define PyString_CheckExact PyString_Check
|
|
#if (PY_VERSION_HEX >= 0x01060000)
|
|
#define Py_USING_UNICODE /* always enabled for 2.0 and 2.1 */
|
|
#endif
|
|
#endif
|
|
#endif
|
|
|
|
#if !defined(Py_RETURN_NONE)
|
|
#define Py_RETURN_NONE return Py_INCREF(Py_None), Py_None
|
|
#endif
|
|
|
|
/* macros used to store 'join' flags in string object pointers. note
|
|
that all use of text and tail as object pointers must be wrapped in
|
|
JOIN_OBJ. see comments in the ElementObject definition for more
|
|
info. */
|
|
#define JOIN_GET(p) ((Py_uintptr_t) (p) & 1)
|
|
#define JOIN_SET(p, flag) ((void*) ((Py_uintptr_t) (JOIN_OBJ(p)) | (flag)))
|
|
#define JOIN_OBJ(p) ((PyObject*) ((Py_uintptr_t) (p) & ~1))
|
|
|
|
/* glue functions (see the init function for details) */
|
|
static PyObject* elementtree_copyelement_obj;
|
|
static PyObject* elementtree_deepcopy_obj;
|
|
static PyObject* elementtree_getiterator_obj;
|
|
static PyObject* elementpath_obj;
|
|
|
|
/* helpers */
|
|
|
|
LOCAL(PyObject*)
|
|
deepcopy(PyObject* object, PyObject* memo)
|
|
{
|
|
/* do a deep copy of the given object */
|
|
|
|
PyObject* args;
|
|
PyObject* result;
|
|
|
|
if (!elementtree_deepcopy_obj) {
|
|
PyErr_SetString(
|
|
PyExc_RuntimeError,
|
|
"deepcopy helper not found"
|
|
);
|
|
return NULL;
|
|
}
|
|
|
|
args = PyTuple_New(2);
|
|
if (!args)
|
|
return NULL;
|
|
|
|
Py_INCREF(object); PyTuple_SET_ITEM(args, 0, (PyObject*) object);
|
|
Py_INCREF(memo); PyTuple_SET_ITEM(args, 1, (PyObject*) memo);
|
|
|
|
result = PyObject_CallObject(elementtree_deepcopy_obj, args);
|
|
|
|
Py_DECREF(args);
|
|
|
|
return result;
|
|
}
|
|
|
|
LOCAL(PyObject*)
|
|
list_join(PyObject* list)
|
|
{
|
|
/* join list elements (destroying the list in the process) */
|
|
|
|
PyObject* joiner;
|
|
PyObject* function;
|
|
PyObject* args;
|
|
PyObject* result;
|
|
|
|
switch (PyList_GET_SIZE(list)) {
|
|
case 0:
|
|
Py_DECREF(list);
|
|
return PyString_FromString("");
|
|
case 1:
|
|
result = PyList_GET_ITEM(list, 0);
|
|
Py_INCREF(result);
|
|
Py_DECREF(list);
|
|
return result;
|
|
}
|
|
|
|
/* two or more elements: slice out a suitable separator from the
|
|
first member, and use that to join the entire list */
|
|
|
|
joiner = PySequence_GetSlice(PyList_GET_ITEM(list, 0), 0, 0);
|
|
if (!joiner)
|
|
return NULL;
|
|
|
|
function = PyObject_GetAttrString(joiner, "join");
|
|
if (!function) {
|
|
Py_DECREF(joiner);
|
|
return NULL;
|
|
}
|
|
|
|
args = PyTuple_New(1);
|
|
if (!args)
|
|
return NULL;
|
|
|
|
PyTuple_SET_ITEM(args, 0, list);
|
|
|
|
result = PyObject_CallObject(function, args);
|
|
|
|
Py_DECREF(args); /* also removes list */
|
|
Py_DECREF(function);
|
|
Py_DECREF(joiner);
|
|
|
|
return result;
|
|
}
|
|
|
|
#if (PY_VERSION_HEX < 0x02020000)
|
|
LOCAL(int)
|
|
PyDict_Update(PyObject* dict, PyObject* other)
|
|
{
|
|
/* PyDict_Update emulation for 2.1 and earlier */
|
|
|
|
PyObject* res;
|
|
|
|
res = PyObject_CallMethod(dict, "update", "O", other);
|
|
if (!res)
|
|
return -1;
|
|
|
|
Py_DECREF(res);
|
|
return 0;
|
|
}
|
|
#endif
|
|
|
|
/* -------------------------------------------------------------------- */
|
|
/* the element type */
|
|
|
|
typedef struct {
|
|
|
|
/* attributes (a dictionary object), or None if no attributes */
|
|
PyObject* attrib;
|
|
|
|
/* child elements */
|
|
int length; /* actual number of items */
|
|
int allocated; /* allocated items */
|
|
|
|
/* this either points to _children or to a malloced buffer */
|
|
PyObject* *children;
|
|
|
|
PyObject* _children[STATIC_CHILDREN];
|
|
|
|
} ElementObjectExtra;
|
|
|
|
typedef struct {
|
|
PyObject_HEAD
|
|
|
|
/* element tag (a string). */
|
|
PyObject* tag;
|
|
|
|
/* text before first child. note that this is a tagged pointer;
|
|
use JOIN_OBJ to get the object pointer. the join flag is used
|
|
to distinguish lists created by the tree builder from lists
|
|
assigned to the attribute by application code; the former
|
|
should be joined before being returned to the user, the latter
|
|
should be left intact. */
|
|
PyObject* text;
|
|
|
|
/* text after this element, in parent. note that this is a tagged
|
|
pointer; use JOIN_OBJ to get the object pointer. */
|
|
PyObject* tail;
|
|
|
|
ElementObjectExtra* extra;
|
|
|
|
} ElementObject;
|
|
|
|
static PyTypeObject Element_Type;
|
|
|
|
#define Element_CheckExact(op) ((op)->ob_type == &Element_Type)
|
|
|
|
/* -------------------------------------------------------------------- */
|
|
/* element constructor and destructor */
|
|
|
|
LOCAL(int)
|
|
element_new_extra(ElementObject* self, PyObject* attrib)
|
|
{
|
|
self->extra = PyObject_Malloc(sizeof(ElementObjectExtra));
|
|
if (!self->extra)
|
|
return -1;
|
|
|
|
if (!attrib)
|
|
attrib = Py_None;
|
|
|
|
Py_INCREF(attrib);
|
|
self->extra->attrib = attrib;
|
|
|
|
self->extra->length = 0;
|
|
self->extra->allocated = STATIC_CHILDREN;
|
|
self->extra->children = self->extra->_children;
|
|
|
|
return 0;
|
|
}
|
|
|
|
LOCAL(void)
|
|
element_dealloc_extra(ElementObject* self)
|
|
{
|
|
int i;
|
|
|
|
Py_DECREF(self->extra->attrib);
|
|
|
|
for (i = 0; i < self->extra->length; i++)
|
|
Py_DECREF(self->extra->children[i]);
|
|
|
|
if (self->extra->children != self->extra->_children)
|
|
PyObject_Free(self->extra->children);
|
|
|
|
PyObject_Free(self->extra);
|
|
}
|
|
|
|
LOCAL(PyObject*)
|
|
element_new(PyObject* tag, PyObject* attrib)
|
|
{
|
|
ElementObject* self;
|
|
|
|
self = PyObject_New(ElementObject, &Element_Type);
|
|
if (self == NULL)
|
|
return NULL;
|
|
|
|
/* use None for empty dictionaries */
|
|
if (PyDict_CheckExact(attrib) && !PyDict_Size(attrib))
|
|
attrib = Py_None;
|
|
|
|
self->extra = NULL;
|
|
|
|
if (attrib != Py_None) {
|
|
|
|
if (element_new_extra(self, attrib) < 0) {
|
|
PyObject_Del(self);
|
|
return NULL;
|
|
}
|
|
|
|
self->extra->length = 0;
|
|
self->extra->allocated = STATIC_CHILDREN;
|
|
self->extra->children = self->extra->_children;
|
|
|
|
}
|
|
|
|
Py_INCREF(tag);
|
|
self->tag = tag;
|
|
|
|
Py_INCREF(Py_None);
|
|
self->text = Py_None;
|
|
|
|
Py_INCREF(Py_None);
|
|
self->tail = Py_None;
|
|
|
|
ALLOC(sizeof(ElementObject), "create element");
|
|
|
|
return (PyObject*) self;
|
|
}
|
|
|
|
LOCAL(int)
|
|
element_resize(ElementObject* self, int extra)
|
|
{
|
|
int size;
|
|
PyObject* *children;
|
|
|
|
/* make sure self->children can hold the given number of extra
|
|
elements. set an exception and return -1 if allocation failed */
|
|
|
|
if (!self->extra)
|
|
element_new_extra(self, NULL);
|
|
|
|
size = self->extra->length + extra;
|
|
|
|
if (size > self->extra->allocated) {
|
|
/* use Python 2.4's list growth strategy */
|
|
size = (size >> 3) + (size < 9 ? 3 : 6) + size;
|
|
if (self->extra->children != self->extra->_children) {
|
|
children = PyObject_Realloc(self->extra->children,
|
|
size * sizeof(PyObject*));
|
|
if (!children)
|
|
goto nomemory;
|
|
} else {
|
|
children = PyObject_Malloc(size * sizeof(PyObject*));
|
|
if (!children)
|
|
goto nomemory;
|
|
/* copy existing children from static area to malloc buffer */
|
|
memcpy(children, self->extra->children,
|
|
self->extra->length * sizeof(PyObject*));
|
|
}
|
|
self->extra->children = children;
|
|
self->extra->allocated = size;
|
|
}
|
|
|
|
return 0;
|
|
|
|
nomemory:
|
|
PyErr_NoMemory();
|
|
return -1;
|
|
}
|
|
|
|
LOCAL(int)
|
|
element_add_subelement(ElementObject* self, PyObject* element)
|
|
{
|
|
/* add a child element to a parent */
|
|
|
|
if (element_resize(self, 1) < 0)
|
|
return -1;
|
|
|
|
Py_INCREF(element);
|
|
self->extra->children[self->extra->length] = element;
|
|
|
|
self->extra->length++;
|
|
|
|
return 0;
|
|
}
|
|
|
|
LOCAL(PyObject*)
|
|
element_get_attrib(ElementObject* self)
|
|
{
|
|
/* return borrowed reference to attrib dictionary */
|
|
/* note: this function assumes that the extra section exists */
|
|
|
|
PyObject* res = self->extra->attrib;
|
|
|
|
if (res == Py_None) {
|
|
/* create missing dictionary */
|
|
res = PyDict_New();
|
|
if (!res)
|
|
return NULL;
|
|
self->extra->attrib = res;
|
|
}
|
|
|
|
return res;
|
|
}
|
|
|
|
LOCAL(PyObject*)
|
|
element_get_text(ElementObject* self)
|
|
{
|
|
/* return borrowed reference to text attribute */
|
|
|
|
PyObject* res = self->text;
|
|
|
|
if (JOIN_GET(res)) {
|
|
res = JOIN_OBJ(res);
|
|
if (PyList_CheckExact(res)) {
|
|
res = list_join(res);
|
|
if (!res)
|
|
return NULL;
|
|
self->text = res;
|
|
}
|
|
}
|
|
|
|
return res;
|
|
}
|
|
|
|
LOCAL(PyObject*)
|
|
element_get_tail(ElementObject* self)
|
|
{
|
|
/* return borrowed reference to text attribute */
|
|
|
|
PyObject* res = self->tail;
|
|
|
|
if (JOIN_GET(res)) {
|
|
res = JOIN_OBJ(res);
|
|
if (PyList_CheckExact(res)) {
|
|
res = list_join(res);
|
|
if (!res)
|
|
return NULL;
|
|
self->tail = res;
|
|
}
|
|
}
|
|
|
|
return res;
|
|
}
|
|
|
|
static PyObject*
|
|
element(PyObject* self, PyObject* args, PyObject* kw)
|
|
{
|
|
PyObject* elem;
|
|
|
|
PyObject* tag;
|
|
PyObject* attrib = NULL;
|
|
if (!PyArg_ParseTuple(args, "O|O!:Element", &tag,
|
|
&PyDict_Type, &attrib))
|
|
return NULL;
|
|
|
|
if (attrib || kw) {
|
|
attrib = (attrib) ? PyDict_Copy(attrib) : PyDict_New();
|
|
if (!attrib)
|
|
return NULL;
|
|
if (kw)
|
|
PyDict_Update(attrib, kw);
|
|
} else {
|
|
Py_INCREF(Py_None);
|
|
attrib = Py_None;
|
|
}
|
|
|
|
elem = element_new(tag, attrib);
|
|
|
|
Py_DECREF(attrib);
|
|
|
|
return elem;
|
|
}
|
|
|
|
static PyObject*
|
|
subelement(PyObject* self, PyObject* args, PyObject* kw)
|
|
{
|
|
PyObject* elem;
|
|
|
|
ElementObject* parent;
|
|
PyObject* tag;
|
|
PyObject* attrib = NULL;
|
|
if (!PyArg_ParseTuple(args, "O!O|O!:SubElement",
|
|
&Element_Type, &parent, &tag,
|
|
&PyDict_Type, &attrib))
|
|
return NULL;
|
|
|
|
if (attrib || kw) {
|
|
attrib = (attrib) ? PyDict_Copy(attrib) : PyDict_New();
|
|
if (!attrib)
|
|
return NULL;
|
|
if (kw)
|
|
PyDict_Update(attrib, kw);
|
|
} else {
|
|
Py_INCREF(Py_None);
|
|
attrib = Py_None;
|
|
}
|
|
|
|
elem = element_new(tag, attrib);
|
|
|
|
Py_DECREF(attrib);
|
|
|
|
if (element_add_subelement(parent, elem) < 0) {
|
|
Py_DECREF(elem);
|
|
return NULL;
|
|
}
|
|
|
|
return elem;
|
|
}
|
|
|
|
static void
|
|
element_dealloc(ElementObject* self)
|
|
{
|
|
if (self->extra)
|
|
element_dealloc_extra(self);
|
|
|
|
/* discard attributes */
|
|
Py_DECREF(self->tag);
|
|
Py_DECREF(JOIN_OBJ(self->text));
|
|
Py_DECREF(JOIN_OBJ(self->tail));
|
|
|
|
RELEASE(sizeof(ElementObject), "destroy element");
|
|
|
|
PyObject_Del(self);
|
|
}
|
|
|
|
/* -------------------------------------------------------------------- */
|
|
/* methods (in alphabetical order) */
|
|
|
|
static PyObject*
|
|
element_append(ElementObject* self, PyObject* args)
|
|
{
|
|
PyObject* element;
|
|
if (!PyArg_ParseTuple(args, "O!:append", &Element_Type, &element))
|
|
return NULL;
|
|
|
|
if (element_add_subelement(self, element) < 0)
|
|
return NULL;
|
|
|
|
Py_RETURN_NONE;
|
|
}
|
|
|
|
static PyObject*
|
|
element_clear(ElementObject* self, PyObject* args)
|
|
{
|
|
if (!PyArg_ParseTuple(args, ":clear"))
|
|
return NULL;
|
|
|
|
if (self->extra) {
|
|
element_dealloc_extra(self);
|
|
self->extra = NULL;
|
|
}
|
|
|
|
Py_INCREF(Py_None);
|
|
Py_DECREF(JOIN_OBJ(self->text));
|
|
self->text = Py_None;
|
|
|
|
Py_INCREF(Py_None);
|
|
Py_DECREF(JOIN_OBJ(self->tail));
|
|
self->tail = Py_None;
|
|
|
|
Py_RETURN_NONE;
|
|
}
|
|
|
|
static PyObject*
|
|
element_copy(ElementObject* self, PyObject* args)
|
|
{
|
|
int i;
|
|
ElementObject* element;
|
|
|
|
if (!PyArg_ParseTuple(args, ":__copy__"))
|
|
return NULL;
|
|
|
|
element = (ElementObject*) element_new(
|
|
self->tag, (self->extra) ? self->extra->attrib : Py_None
|
|
);
|
|
if (!element)
|
|
return NULL;
|
|
|
|
Py_DECREF(JOIN_OBJ(element->text));
|
|
element->text = self->text;
|
|
Py_INCREF(JOIN_OBJ(element->text));
|
|
|
|
Py_DECREF(JOIN_OBJ(element->tail));
|
|
element->tail = self->tail;
|
|
Py_INCREF(JOIN_OBJ(element->tail));
|
|
|
|
if (self->extra) {
|
|
|
|
if (element_resize(element, self->extra->length) < 0) {
|
|
Py_DECREF(element);
|
|
return NULL;
|
|
}
|
|
|
|
for (i = 0; i < self->extra->length; i++) {
|
|
Py_INCREF(self->extra->children[i]);
|
|
element->extra->children[i] = self->extra->children[i];
|
|
}
|
|
|
|
element->extra->length = self->extra->length;
|
|
|
|
}
|
|
|
|
return (PyObject*) element;
|
|
}
|
|
|
|
static PyObject*
|
|
element_deepcopy(ElementObject* self, PyObject* args)
|
|
{
|
|
int i;
|
|
ElementObject* element;
|
|
PyObject* tag;
|
|
PyObject* attrib;
|
|
PyObject* text;
|
|
PyObject* tail;
|
|
PyObject* id;
|
|
|
|
PyObject* memo;
|
|
if (!PyArg_ParseTuple(args, "O:__deepcopy__", &memo))
|
|
return NULL;
|
|
|
|
tag = deepcopy(self->tag, memo);
|
|
if (!tag)
|
|
return NULL;
|
|
|
|
if (self->extra) {
|
|
attrib = deepcopy(self->extra->attrib, memo);
|
|
if (!attrib) {
|
|
Py_DECREF(tag);
|
|
return NULL;
|
|
}
|
|
} else {
|
|
Py_INCREF(Py_None);
|
|
attrib = Py_None;
|
|
}
|
|
|
|
element = (ElementObject*) element_new(tag, attrib);
|
|
|
|
Py_DECREF(tag);
|
|
Py_DECREF(attrib);
|
|
|
|
if (!element)
|
|
return NULL;
|
|
|
|
text = deepcopy(JOIN_OBJ(self->text), memo);
|
|
if (!text)
|
|
goto error;
|
|
Py_DECREF(element->text);
|
|
element->text = JOIN_SET(text, JOIN_GET(self->text));
|
|
|
|
tail = deepcopy(JOIN_OBJ(self->tail), memo);
|
|
if (!tail)
|
|
goto error;
|
|
Py_DECREF(element->tail);
|
|
element->tail = JOIN_SET(tail, JOIN_GET(self->tail));
|
|
|
|
if (self->extra) {
|
|
|
|
if (element_resize(element, self->extra->length) < 0)
|
|
goto error;
|
|
|
|
for (i = 0; i < self->extra->length; i++) {
|
|
PyObject* child = deepcopy(self->extra->children[i], memo);
|
|
if (!child) {
|
|
element->extra->length = i;
|
|
goto error;
|
|
}
|
|
element->extra->children[i] = child;
|
|
}
|
|
|
|
element->extra->length = self->extra->length;
|
|
|
|
}
|
|
|
|
/* add object to memo dictionary (so deepcopy won't visit it again) */
|
|
id = PyInt_FromLong((Py_uintptr_t) self);
|
|
|
|
i = PyDict_SetItem(memo, id, (PyObject*) element);
|
|
|
|
Py_DECREF(id);
|
|
|
|
if (i < 0)
|
|
goto error;
|
|
|
|
return (PyObject*) element;
|
|
|
|
error:
|
|
Py_DECREF(element);
|
|
return NULL;
|
|
}
|
|
|
|
LOCAL(int)
|
|
checkpath(PyObject* tag)
|
|
{
|
|
Py_ssize_t i;
|
|
int check = 1;
|
|
|
|
/* check if a tag contains an xpath character */
|
|
|
|
#define PATHCHAR(ch) (ch == '/' || ch == '*' || ch == '[' || ch == '@')
|
|
|
|
#if defined(Py_USING_UNICODE)
|
|
if (PyUnicode_Check(tag)) {
|
|
Py_UNICODE *p = PyUnicode_AS_UNICODE(tag);
|
|
for (i = 0; i < PyUnicode_GET_SIZE(tag); i++) {
|
|
if (p[i] == '{')
|
|
check = 0;
|
|
else if (p[i] == '}')
|
|
check = 1;
|
|
else if (check && PATHCHAR(p[i]))
|
|
return 1;
|
|
}
|
|
return 0;
|
|
}
|
|
#endif
|
|
if (PyString_Check(tag)) {
|
|
char *p = PyString_AS_STRING(tag);
|
|
for (i = 0; i < PyString_GET_SIZE(tag); i++) {
|
|
if (p[i] == '{')
|
|
check = 0;
|
|
else if (p[i] == '}')
|
|
check = 1;
|
|
else if (check && PATHCHAR(p[i]))
|
|
return 1;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
return 1; /* unknown type; might be path expression */
|
|
}
|
|
|
|
static PyObject*
|
|
element_find(ElementObject* self, PyObject* args)
|
|
{
|
|
int i;
|
|
|
|
PyObject* tag;
|
|
if (!PyArg_ParseTuple(args, "O:find", &tag))
|
|
return NULL;
|
|
|
|
if (checkpath(tag))
|
|
return PyObject_CallMethod(
|
|
elementpath_obj, "find", "OO", self, tag
|
|
);
|
|
|
|
if (!self->extra)
|
|
Py_RETURN_NONE;
|
|
|
|
for (i = 0; i < self->extra->length; i++) {
|
|
PyObject* item = self->extra->children[i];
|
|
if (Element_CheckExact(item) &&
|
|
PyObject_Compare(((ElementObject*)item)->tag, tag) == 0) {
|
|
Py_INCREF(item);
|
|
return item;
|
|
}
|
|
}
|
|
|
|
Py_RETURN_NONE;
|
|
}
|
|
|
|
static PyObject*
|
|
element_findtext(ElementObject* self, PyObject* args)
|
|
{
|
|
int i;
|
|
|
|
PyObject* tag;
|
|
PyObject* default_value = Py_None;
|
|
if (!PyArg_ParseTuple(args, "O|O:findtext", &tag, &default_value))
|
|
return NULL;
|
|
|
|
if (checkpath(tag))
|
|
return PyObject_CallMethod(
|
|
elementpath_obj, "findtext", "OOO", self, tag, default_value
|
|
);
|
|
|
|
if (!self->extra) {
|
|
Py_INCREF(default_value);
|
|
return default_value;
|
|
}
|
|
|
|
for (i = 0; i < self->extra->length; i++) {
|
|
ElementObject* item = (ElementObject*) self->extra->children[i];
|
|
if (Element_CheckExact(item) && !PyObject_Compare(item->tag, tag)) {
|
|
PyObject* text = element_get_text(item);
|
|
if (text == Py_None)
|
|
return PyString_FromString("");
|
|
Py_XINCREF(text);
|
|
return text;
|
|
}
|
|
}
|
|
|
|
Py_INCREF(default_value);
|
|
return default_value;
|
|
}
|
|
|
|
static PyObject*
|
|
element_findall(ElementObject* self, PyObject* args)
|
|
{
|
|
int i;
|
|
PyObject* out;
|
|
|
|
PyObject* tag;
|
|
if (!PyArg_ParseTuple(args, "O:findall", &tag))
|
|
return NULL;
|
|
|
|
if (checkpath(tag))
|
|
return PyObject_CallMethod(
|
|
elementpath_obj, "findall", "OO", self, tag
|
|
);
|
|
|
|
out = PyList_New(0);
|
|
if (!out)
|
|
return NULL;
|
|
|
|
if (!self->extra)
|
|
return out;
|
|
|
|
for (i = 0; i < self->extra->length; i++) {
|
|
PyObject* item = self->extra->children[i];
|
|
if (Element_CheckExact(item) &&
|
|
PyObject_Compare(((ElementObject*)item)->tag, tag) == 0) {
|
|
if (PyList_Append(out, item) < 0) {
|
|
Py_DECREF(out);
|
|
return NULL;
|
|
}
|
|
}
|
|
}
|
|
|
|
return out;
|
|
}
|
|
|
|
static PyObject*
|
|
element_get(ElementObject* self, PyObject* args)
|
|
{
|
|
PyObject* value;
|
|
|
|
PyObject* key;
|
|
PyObject* default_value = Py_None;
|
|
if (!PyArg_ParseTuple(args, "O|O:get", &key, &default_value))
|
|
return NULL;
|
|
|
|
if (!self->extra || self->extra->attrib == Py_None)
|
|
value = default_value;
|
|
else {
|
|
value = PyDict_GetItem(self->extra->attrib, key);
|
|
if (!value)
|
|
value = default_value;
|
|
}
|
|
|
|
Py_INCREF(value);
|
|
return value;
|
|
}
|
|
|
|
static PyObject*
|
|
element_getchildren(ElementObject* self, PyObject* args)
|
|
{
|
|
int i;
|
|
PyObject* list;
|
|
|
|
if (!PyArg_ParseTuple(args, ":getchildren"))
|
|
return NULL;
|
|
|
|
if (!self->extra)
|
|
return PyList_New(0);
|
|
|
|
list = PyList_New(self->extra->length);
|
|
if (!list)
|
|
return NULL;
|
|
|
|
for (i = 0; i < self->extra->length; i++) {
|
|
PyObject* item = self->extra->children[i];
|
|
Py_INCREF(item);
|
|
PyList_SET_ITEM(list, i, item);
|
|
}
|
|
|
|
return list;
|
|
}
|
|
|
|
static PyObject*
|
|
element_getiterator(ElementObject* self, PyObject* args)
|
|
{
|
|
PyObject* result;
|
|
|
|
PyObject* tag = Py_None;
|
|
if (!PyArg_ParseTuple(args, "|O:getiterator", &tag))
|
|
return NULL;
|
|
|
|
if (!elementtree_getiterator_obj) {
|
|
PyErr_SetString(
|
|
PyExc_RuntimeError,
|
|
"getiterator helper not found"
|
|
);
|
|
return NULL;
|
|
}
|
|
|
|
args = PyTuple_New(2);
|
|
if (!args)
|
|
return NULL;
|
|
|
|
Py_INCREF(self); PyTuple_SET_ITEM(args, 0, (PyObject*) self);
|
|
Py_INCREF(tag); PyTuple_SET_ITEM(args, 1, (PyObject*) tag);
|
|
|
|
result = PyObject_CallObject(elementtree_getiterator_obj, args);
|
|
|
|
Py_DECREF(args);
|
|
|
|
return result;
|
|
}
|
|
|
|
static PyObject*
|
|
element_getitem(PyObject* self_, Py_ssize_t index)
|
|
{
|
|
ElementObject* self = (ElementObject*) self_;
|
|
|
|
if (!self->extra || index < 0 || index >= self->extra->length) {
|
|
PyErr_SetString(
|
|
PyExc_IndexError,
|
|
"child index out of range"
|
|
);
|
|
return NULL;
|
|
}
|
|
|
|
Py_INCREF(self->extra->children[index]);
|
|
return self->extra->children[index];
|
|
}
|
|
|
|
static PyObject*
|
|
element_getslice(PyObject* self_, Py_ssize_t start, Py_ssize_t end)
|
|
{
|
|
ElementObject* self = (ElementObject*) self_;
|
|
Py_ssize_t i;
|
|
PyObject* list;
|
|
|
|
if (!self->extra)
|
|
return PyList_New(0);
|
|
|
|
/* standard clamping */
|
|
if (start < 0)
|
|
start = 0;
|
|
if (end < 0)
|
|
end = 0;
|
|
if (end > self->extra->length)
|
|
end = self->extra->length;
|
|
if (start > end)
|
|
start = end;
|
|
|
|
list = PyList_New(end - start);
|
|
if (!list)
|
|
return NULL;
|
|
|
|
for (i = start; i < end; i++) {
|
|
PyObject* item = self->extra->children[i];
|
|
Py_INCREF(item);
|
|
PyList_SET_ITEM(list, i - start, item);
|
|
}
|
|
|
|
return list;
|
|
}
|
|
|
|
static PyObject*
|
|
element_insert(ElementObject* self, PyObject* args)
|
|
{
|
|
int i;
|
|
|
|
int index;
|
|
PyObject* element;
|
|
if (!PyArg_ParseTuple(args, "iO!:insert", &index,
|
|
&Element_Type, &element))
|
|
return NULL;
|
|
|
|
if (!self->extra)
|
|
element_new_extra(self, NULL);
|
|
|
|
if (index < 0)
|
|
index = 0;
|
|
if (index > self->extra->length)
|
|
index = self->extra->length;
|
|
|
|
if (element_resize(self, 1) < 0)
|
|
return NULL;
|
|
|
|
for (i = self->extra->length; i > index; i--)
|
|
self->extra->children[i] = self->extra->children[i-1];
|
|
|
|
Py_INCREF(element);
|
|
self->extra->children[index] = element;
|
|
|
|
self->extra->length++;
|
|
|
|
Py_RETURN_NONE;
|
|
}
|
|
|
|
static PyObject*
|
|
element_items(ElementObject* self, PyObject* args)
|
|
{
|
|
if (!PyArg_ParseTuple(args, ":items"))
|
|
return NULL;
|
|
|
|
if (!self->extra || self->extra->attrib == Py_None)
|
|
return PyList_New(0);
|
|
|
|
return PyDict_Items(self->extra->attrib);
|
|
}
|
|
|
|
static PyObject*
|
|
element_keys(ElementObject* self, PyObject* args)
|
|
{
|
|
if (!PyArg_ParseTuple(args, ":keys"))
|
|
return NULL;
|
|
|
|
if (!self->extra || self->extra->attrib == Py_None)
|
|
return PyList_New(0);
|
|
|
|
return PyDict_Keys(self->extra->attrib);
|
|
}
|
|
|
|
static Py_ssize_t
|
|
element_length(ElementObject* self)
|
|
{
|
|
if (!self->extra)
|
|
return 0;
|
|
|
|
return self->extra->length;
|
|
}
|
|
|
|
static PyObject*
|
|
element_makeelement(PyObject* self, PyObject* args, PyObject* kw)
|
|
{
|
|
PyObject* elem;
|
|
|
|
PyObject* tag;
|
|
PyObject* attrib;
|
|
if (!PyArg_ParseTuple(args, "OO:makeelement", &tag, &attrib))
|
|
return NULL;
|
|
|
|
attrib = PyDict_Copy(attrib);
|
|
if (!attrib)
|
|
return NULL;
|
|
|
|
elem = element_new(tag, attrib);
|
|
|
|
Py_DECREF(attrib);
|
|
|
|
return elem;
|
|
}
|
|
|
|
static PyObject*
|
|
element_reduce(ElementObject* self, PyObject* args)
|
|
{
|
|
if (!PyArg_ParseTuple(args, ":__reduce__"))
|
|
return NULL;
|
|
|
|
/* Hack alert: This method is used to work around a __copy__
|
|
problem on certain 2.3 and 2.4 versions. To save time and
|
|
simplify the code, we create the copy in here, and use a dummy
|
|
copyelement helper to trick the copy module into doing the
|
|
right thing. */
|
|
|
|
if (!elementtree_copyelement_obj) {
|
|
PyErr_SetString(
|
|
PyExc_RuntimeError,
|
|
"copyelement helper not found"
|
|
);
|
|
return NULL;
|
|
}
|
|
|
|
return Py_BuildValue(
|
|
"O(N)", elementtree_copyelement_obj, element_copy(self, args)
|
|
);
|
|
}
|
|
|
|
static PyObject*
|
|
element_remove(ElementObject* self, PyObject* args)
|
|
{
|
|
int i;
|
|
|
|
PyObject* element;
|
|
if (!PyArg_ParseTuple(args, "O!:remove", &Element_Type, &element))
|
|
return NULL;
|
|
|
|
if (!self->extra) {
|
|
/* element has no children, so raise exception */
|
|
PyErr_SetString(
|
|
PyExc_ValueError,
|
|
"list.remove(x): x not in list"
|
|
);
|
|
return NULL;
|
|
}
|
|
|
|
for (i = 0; i < self->extra->length; i++) {
|
|
if (self->extra->children[i] == element)
|
|
break;
|
|
if (PyObject_Compare(self->extra->children[i], element) == 0)
|
|
break;
|
|
}
|
|
|
|
if (i == self->extra->length) {
|
|
/* element is not in children, so raise exception */
|
|
PyErr_SetString(
|
|
PyExc_ValueError,
|
|
"list.remove(x): x not in list"
|
|
);
|
|
return NULL;
|
|
}
|
|
|
|
Py_DECREF(self->extra->children[i]);
|
|
|
|
self->extra->length--;
|
|
|
|
for (; i < self->extra->length; i++)
|
|
self->extra->children[i] = self->extra->children[i+1];
|
|
|
|
Py_RETURN_NONE;
|
|
}
|
|
|
|
static PyObject*
|
|
element_repr(ElementObject* self)
|
|
{
|
|
PyObject* repr;
|
|
char buffer[100];
|
|
|
|
repr = PyString_FromString("<Element ");
|
|
|
|
PyString_ConcatAndDel(&repr, PyObject_Repr(self->tag));
|
|
|
|
sprintf(buffer, " at %p>", self);
|
|
PyString_ConcatAndDel(&repr, PyString_FromString(buffer));
|
|
|
|
return repr;
|
|
}
|
|
|
|
static PyObject*
|
|
element_set(ElementObject* self, PyObject* args)
|
|
{
|
|
PyObject* attrib;
|
|
|
|
PyObject* key;
|
|
PyObject* value;
|
|
if (!PyArg_ParseTuple(args, "OO:set", &key, &value))
|
|
return NULL;
|
|
|
|
if (!self->extra)
|
|
element_new_extra(self, NULL);
|
|
|
|
attrib = element_get_attrib(self);
|
|
if (!attrib)
|
|
return NULL;
|
|
|
|
if (PyDict_SetItem(attrib, key, value) < 0)
|
|
return NULL;
|
|
|
|
Py_RETURN_NONE;
|
|
}
|
|
|
|
static int
|
|
element_setslice(PyObject* self_, Py_ssize_t start, Py_ssize_t end, PyObject* item)
|
|
{
|
|
ElementObject* self = (ElementObject*) self_;
|
|
Py_ssize_t i, new, old;
|
|
PyObject* recycle = NULL;
|
|
|
|
if (!self->extra)
|
|
element_new_extra(self, NULL);
|
|
|
|
/* standard clamping */
|
|
if (start < 0)
|
|
start = 0;
|
|
if (end < 0)
|
|
end = 0;
|
|
if (end > self->extra->length)
|
|
end = self->extra->length;
|
|
if (start > end)
|
|
start = end;
|
|
|
|
old = end - start;
|
|
|
|
if (item == NULL)
|
|
new = 0;
|
|
else if (PyList_CheckExact(item)) {
|
|
new = PyList_GET_SIZE(item);
|
|
} else {
|
|
/* FIXME: support arbitrary sequences? */
|
|
PyErr_Format(
|
|
PyExc_TypeError,
|
|
"expected list, not \"%.200s\"", item->ob_type->tp_name
|
|
);
|
|
return -1;
|
|
}
|
|
|
|
if (old > 0) {
|
|
/* to avoid recursive calls to this method (via decref), move
|
|
old items to the recycle bin here, and get rid of them when
|
|
we're done modifying the element */
|
|
recycle = PyList_New(old);
|
|
for (i = 0; i < old; i++)
|
|
PyList_SET_ITEM(recycle, i, self->extra->children[i + start]);
|
|
}
|
|
|
|
if (new < old) {
|
|
/* delete slice */
|
|
for (i = end; i < self->extra->length; i++)
|
|
self->extra->children[i + new - old] = self->extra->children[i];
|
|
} else if (new > old) {
|
|
/* insert slice */
|
|
if (element_resize(self, new - old) < 0)
|
|
return -1;
|
|
for (i = self->extra->length-1; i >= end; i--)
|
|
self->extra->children[i + new - old] = self->extra->children[i];
|
|
}
|
|
|
|
/* replace the slice */
|
|
for (i = 0; i < new; i++) {
|
|
PyObject* element = PyList_GET_ITEM(item, i);
|
|
Py_INCREF(element);
|
|
self->extra->children[i + start] = element;
|
|
}
|
|
|
|
self->extra->length += new - old;
|
|
|
|
/* discard the recycle bin, and everything in it */
|
|
Py_XDECREF(recycle);
|
|
|
|
return 0;
|
|
}
|
|
|
|
static int
|
|
element_setitem(PyObject* self_, Py_ssize_t index, PyObject* item)
|
|
{
|
|
ElementObject* self = (ElementObject*) self_;
|
|
int i;
|
|
PyObject* old;
|
|
|
|
if (!self->extra || index < 0 || index >= self->extra->length) {
|
|
PyErr_SetString(
|
|
PyExc_IndexError,
|
|
"child assignment index out of range");
|
|
return -1;
|
|
}
|
|
|
|
old = self->extra->children[index];
|
|
|
|
if (item) {
|
|
Py_INCREF(item);
|
|
self->extra->children[index] = item;
|
|
} else {
|
|
self->extra->length--;
|
|
for (i = index; i < self->extra->length; i++)
|
|
self->extra->children[i] = self->extra->children[i+1];
|
|
}
|
|
|
|
Py_DECREF(old);
|
|
|
|
return 0;
|
|
}
|
|
|
|
static PyMethodDef element_methods[] = {
|
|
|
|
{"clear", (PyCFunction) element_clear, METH_VARARGS},
|
|
|
|
{"get", (PyCFunction) element_get, METH_VARARGS},
|
|
{"set", (PyCFunction) element_set, METH_VARARGS},
|
|
|
|
{"find", (PyCFunction) element_find, METH_VARARGS},
|
|
{"findtext", (PyCFunction) element_findtext, METH_VARARGS},
|
|
{"findall", (PyCFunction) element_findall, METH_VARARGS},
|
|
|
|
{"append", (PyCFunction) element_append, METH_VARARGS},
|
|
{"insert", (PyCFunction) element_insert, METH_VARARGS},
|
|
{"remove", (PyCFunction) element_remove, METH_VARARGS},
|
|
|
|
{"getiterator", (PyCFunction) element_getiterator, METH_VARARGS},
|
|
{"getchildren", (PyCFunction) element_getchildren, METH_VARARGS},
|
|
|
|
{"items", (PyCFunction) element_items, METH_VARARGS},
|
|
{"keys", (PyCFunction) element_keys, METH_VARARGS},
|
|
|
|
{"makeelement", (PyCFunction) element_makeelement, METH_VARARGS},
|
|
|
|
{"__copy__", (PyCFunction) element_copy, METH_VARARGS},
|
|
{"__deepcopy__", (PyCFunction) element_deepcopy, METH_VARARGS},
|
|
|
|
/* Some 2.3 and 2.4 versions do not handle the __copy__ method on
|
|
C objects correctly, so we have to fake it using a __reduce__-
|
|
based hack (see the element_reduce implementation above for
|
|
details). */
|
|
|
|
/* The behaviour has been changed in 2.3.5 and 2.4.1, so we're
|
|
using a runtime test to figure out if we need to fake things
|
|
or now (see the init code below). The following entry is
|
|
enabled only if the hack is needed. */
|
|
|
|
{"!__reduce__", (PyCFunction) element_reduce, METH_VARARGS},
|
|
|
|
{NULL, NULL}
|
|
};
|
|
|
|
static PyObject*
|
|
element_getattr(ElementObject* self, char* name)
|
|
{
|
|
PyObject* res;
|
|
|
|
res = Py_FindMethod(element_methods, (PyObject*) self, name);
|
|
if (res)
|
|
return res;
|
|
|
|
PyErr_Clear();
|
|
|
|
if (strcmp(name, "tag") == 0)
|
|
res = self->tag;
|
|
else if (strcmp(name, "text") == 0)
|
|
res = element_get_text(self);
|
|
else if (strcmp(name, "tail") == 0) {
|
|
res = element_get_tail(self);
|
|
} else if (strcmp(name, "attrib") == 0) {
|
|
if (!self->extra)
|
|
element_new_extra(self, NULL);
|
|
res = element_get_attrib(self);
|
|
} else {
|
|
PyErr_SetString(PyExc_AttributeError, name);
|
|
return NULL;
|
|
}
|
|
|
|
if (!res)
|
|
return NULL;
|
|
|
|
Py_INCREF(res);
|
|
return res;
|
|
}
|
|
|
|
static int
|
|
element_setattr(ElementObject* self, const char* name, PyObject* value)
|
|
{
|
|
if (value == NULL) {
|
|
PyErr_SetString(
|
|
PyExc_AttributeError,
|
|
"can't delete element attributes"
|
|
);
|
|
return -1;
|
|
}
|
|
|
|
if (strcmp(name, "tag") == 0) {
|
|
Py_DECREF(self->tag);
|
|
self->tag = value;
|
|
Py_INCREF(self->tag);
|
|
} else if (strcmp(name, "text") == 0) {
|
|
Py_DECREF(JOIN_OBJ(self->text));
|
|
self->text = value;
|
|
Py_INCREF(self->text);
|
|
} else if (strcmp(name, "tail") == 0) {
|
|
Py_DECREF(JOIN_OBJ(self->tail));
|
|
self->tail = value;
|
|
Py_INCREF(self->tail);
|
|
} else if (strcmp(name, "attrib") == 0) {
|
|
if (!self->extra)
|
|
element_new_extra(self, NULL);
|
|
Py_DECREF(self->extra->attrib);
|
|
self->extra->attrib = value;
|
|
Py_INCREF(self->extra->attrib);
|
|
} else {
|
|
PyErr_SetString(PyExc_AttributeError, name);
|
|
return -1;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
static PySequenceMethods element_as_sequence = {
|
|
(lenfunc) element_length,
|
|
0, /* sq_concat */
|
|
0, /* sq_repeat */
|
|
element_getitem,
|
|
element_getslice,
|
|
element_setitem,
|
|
element_setslice,
|
|
};
|
|
|
|
static PyTypeObject Element_Type = {
|
|
PyObject_HEAD_INIT(NULL)
|
|
0, "Element", sizeof(ElementObject), 0,
|
|
/* methods */
|
|
(destructor)element_dealloc, /* tp_dealloc */
|
|
0, /* tp_print */
|
|
(getattrfunc)element_getattr, /* tp_getattr */
|
|
(setattrfunc)element_setattr, /* tp_setattr */
|
|
0, /* tp_compare */
|
|
(reprfunc)element_repr, /* tp_repr */
|
|
0, /* tp_as_number */
|
|
&element_as_sequence, /* tp_as_sequence */
|
|
};
|
|
|
|
/* ==================================================================== */
|
|
/* the tree builder type */
|
|
|
|
typedef struct {
|
|
PyObject_HEAD
|
|
|
|
PyObject* root; /* root node (first created node) */
|
|
|
|
ElementObject* this; /* current node */
|
|
ElementObject* last; /* most recently created node */
|
|
|
|
PyObject* data; /* data collector (string or list), or NULL */
|
|
|
|
PyObject* stack; /* element stack */
|
|
Py_ssize_t index; /* current stack size (0=empty) */
|
|
|
|
/* element tracing */
|
|
PyObject* events; /* list of events, or NULL if not collecting */
|
|
PyObject* start_event_obj; /* event objects (NULL to ignore) */
|
|
PyObject* end_event_obj;
|
|
PyObject* start_ns_event_obj;
|
|
PyObject* end_ns_event_obj;
|
|
|
|
} TreeBuilderObject;
|
|
|
|
static PyTypeObject TreeBuilder_Type;
|
|
|
|
#define TreeBuilder_CheckExact(op) ((op)->ob_type == &TreeBuilder_Type)
|
|
|
|
/* -------------------------------------------------------------------- */
|
|
/* constructor and destructor */
|
|
|
|
LOCAL(PyObject*)
|
|
treebuilder_new(void)
|
|
{
|
|
TreeBuilderObject* self;
|
|
|
|
self = PyObject_New(TreeBuilderObject, &TreeBuilder_Type);
|
|
if (self == NULL)
|
|
return NULL;
|
|
|
|
self->root = NULL;
|
|
|
|
Py_INCREF(Py_None);
|
|
self->this = (ElementObject*) Py_None;
|
|
|
|
Py_INCREF(Py_None);
|
|
self->last = (ElementObject*) Py_None;
|
|
|
|
self->data = NULL;
|
|
|
|
self->stack = PyList_New(20);
|
|
self->index = 0;
|
|
|
|
self->events = NULL;
|
|
self->start_event_obj = self->end_event_obj = NULL;
|
|
self->start_ns_event_obj = self->end_ns_event_obj = NULL;
|
|
|
|
ALLOC(sizeof(TreeBuilderObject), "create treebuilder");
|
|
|
|
return (PyObject*) self;
|
|
}
|
|
|
|
static PyObject*
|
|
treebuilder(PyObject* self_, PyObject* args)
|
|
{
|
|
if (!PyArg_ParseTuple(args, ":TreeBuilder"))
|
|
return NULL;
|
|
|
|
return treebuilder_new();
|
|
}
|
|
|
|
static void
|
|
treebuilder_dealloc(TreeBuilderObject* self)
|
|
{
|
|
Py_XDECREF(self->end_ns_event_obj);
|
|
Py_XDECREF(self->start_ns_event_obj);
|
|
Py_XDECREF(self->end_event_obj);
|
|
Py_XDECREF(self->start_event_obj);
|
|
Py_XDECREF(self->events);
|
|
Py_DECREF(self->stack);
|
|
Py_XDECREF(self->data);
|
|
Py_DECREF(self->last);
|
|
Py_DECREF(self->this);
|
|
Py_XDECREF(self->root);
|
|
|
|
RELEASE(sizeof(TreeBuilderObject), "destroy treebuilder");
|
|
|
|
PyObject_Del(self);
|
|
}
|
|
|
|
/* -------------------------------------------------------------------- */
|
|
/* handlers */
|
|
|
|
LOCAL(PyObject*)
|
|
treebuilder_handle_xml(TreeBuilderObject* self, PyObject* encoding,
|
|
PyObject* standalone)
|
|
{
|
|
Py_RETURN_NONE;
|
|
}
|
|
|
|
LOCAL(PyObject*)
|
|
treebuilder_handle_start(TreeBuilderObject* self, PyObject* tag,
|
|
PyObject* attrib)
|
|
{
|
|
PyObject* node;
|
|
PyObject* this;
|
|
|
|
if (self->data) {
|
|
if (self->this == self->last) {
|
|
Py_DECREF(JOIN_OBJ(self->last->text));
|
|
self->last->text = JOIN_SET(
|
|
self->data, PyList_CheckExact(self->data)
|
|
);
|
|
} else {
|
|
Py_DECREF(JOIN_OBJ(self->last->tail));
|
|
self->last->tail = JOIN_SET(
|
|
self->data, PyList_CheckExact(self->data)
|
|
);
|
|
}
|
|
self->data = NULL;
|
|
}
|
|
|
|
node = element_new(tag, attrib);
|
|
if (!node)
|
|
return NULL;
|
|
|
|
this = (PyObject*) self->this;
|
|
|
|
if (this != Py_None) {
|
|
if (element_add_subelement((ElementObject*) this, node) < 0)
|
|
goto error;
|
|
} else {
|
|
if (self->root) {
|
|
PyErr_SetString(
|
|
PyExc_SyntaxError,
|
|
"multiple elements on top level"
|
|
);
|
|
goto error;
|
|
}
|
|
Py_INCREF(node);
|
|
self->root = node;
|
|
}
|
|
|
|
if (self->index < PyList_GET_SIZE(self->stack)) {
|
|
if (PyList_SetItem(self->stack, self->index, this) < 0)
|
|
goto error;
|
|
Py_INCREF(this);
|
|
} else {
|
|
if (PyList_Append(self->stack, this) < 0)
|
|
goto error;
|
|
}
|
|
self->index++;
|
|
|
|
Py_DECREF(this);
|
|
Py_INCREF(node);
|
|
self->this = (ElementObject*) node;
|
|
|
|
Py_DECREF(self->last);
|
|
Py_INCREF(node);
|
|
self->last = (ElementObject*) node;
|
|
|
|
if (self->start_event_obj) {
|
|
PyObject* res;
|
|
PyObject* action = self->start_event_obj;
|
|
res = PyTuple_New(2);
|
|
if (res) {
|
|
Py_INCREF(action); PyTuple_SET_ITEM(res, 0, (PyObject*) action);
|
|
Py_INCREF(node); PyTuple_SET_ITEM(res, 1, (PyObject*) node);
|
|
PyList_Append(self->events, res);
|
|
Py_DECREF(res);
|
|
} else
|
|
PyErr_Clear(); /* FIXME: propagate error */
|
|
}
|
|
|
|
return node;
|
|
|
|
error:
|
|
Py_DECREF(node);
|
|
return NULL;
|
|
}
|
|
|
|
LOCAL(PyObject*)
|
|
treebuilder_handle_data(TreeBuilderObject* self, PyObject* 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 */
|
|
Py_INCREF(data); self->data = data;
|
|
} else {
|
|
/* more than one item; use a list to collect items */
|
|
if (PyString_CheckExact(self->data) && self->data->ob_refcnt == 1 &&
|
|
PyString_CheckExact(data) && PyString_GET_SIZE(data) == 1) {
|
|
/* expat often generates single character data sections; handle
|
|
the most common case by resizing the existing string... */
|
|
Py_ssize_t size = PyString_GET_SIZE(self->data);
|
|
if (_PyString_Resize(&self->data, size + 1) < 0)
|
|
return NULL;
|
|
PyString_AS_STRING(self->data)[size] = PyString_AS_STRING(data)[0];
|
|
} else if (PyList_CheckExact(self->data)) {
|
|
if (PyList_Append(self->data, data) < 0)
|
|
return NULL;
|
|
} else {
|
|
PyObject* list = PyList_New(2);
|
|
if (!list)
|
|
return NULL;
|
|
PyList_SET_ITEM(list, 0, self->data);
|
|
Py_INCREF(data); PyList_SET_ITEM(list, 1, data);
|
|
self->data = list;
|
|
}
|
|
}
|
|
|
|
Py_RETURN_NONE;
|
|
}
|
|
|
|
LOCAL(PyObject*)
|
|
treebuilder_handle_end(TreeBuilderObject* self, PyObject* tag)
|
|
{
|
|
PyObject* item;
|
|
|
|
if (self->data) {
|
|
if (self->this == self->last) {
|
|
Py_DECREF(JOIN_OBJ(self->last->text));
|
|
self->last->text = JOIN_SET(
|
|
self->data, PyList_CheckExact(self->data)
|
|
);
|
|
} else {
|
|
Py_DECREF(JOIN_OBJ(self->last->tail));
|
|
self->last->tail = JOIN_SET(
|
|
self->data, PyList_CheckExact(self->data)
|
|
);
|
|
}
|
|
self->data = NULL;
|
|
}
|
|
|
|
if (self->index == 0) {
|
|
PyErr_SetString(
|
|
PyExc_IndexError,
|
|
"pop from empty stack"
|
|
);
|
|
return NULL;
|
|
}
|
|
|
|
self->index--;
|
|
|
|
item = PyList_GET_ITEM(self->stack, self->index);
|
|
Py_INCREF(item);
|
|
|
|
Py_DECREF(self->last);
|
|
|
|
self->last = (ElementObject*) self->this;
|
|
self->this = (ElementObject*) item;
|
|
|
|
if (self->end_event_obj) {
|
|
PyObject* res;
|
|
PyObject* action = self->end_event_obj;
|
|
PyObject* node = (PyObject*) self->last;
|
|
res = PyTuple_New(2);
|
|
if (res) {
|
|
Py_INCREF(action); PyTuple_SET_ITEM(res, 0, (PyObject*) action);
|
|
Py_INCREF(node); PyTuple_SET_ITEM(res, 1, (PyObject*) node);
|
|
PyList_Append(self->events, res);
|
|
Py_DECREF(res);
|
|
} else
|
|
PyErr_Clear(); /* FIXME: propagate error */
|
|
}
|
|
|
|
Py_INCREF(self->last);
|
|
return (PyObject*) self->last;
|
|
}
|
|
|
|
LOCAL(void)
|
|
treebuilder_handle_namespace(TreeBuilderObject* self, int start,
|
|
const char* prefix, const char *uri)
|
|
{
|
|
PyObject* res;
|
|
PyObject* action;
|
|
PyObject* parcel;
|
|
|
|
if (!self->events)
|
|
return;
|
|
|
|
if (start) {
|
|
if (!self->start_ns_event_obj)
|
|
return;
|
|
action = self->start_ns_event_obj;
|
|
/* FIXME: prefix and uri use utf-8 encoding! */
|
|
parcel = Py_BuildValue("ss", (prefix) ? prefix : "", uri);
|
|
if (!parcel)
|
|
return;
|
|
Py_INCREF(action);
|
|
} else {
|
|
if (!self->end_ns_event_obj)
|
|
return;
|
|
action = self->end_ns_event_obj;
|
|
Py_INCREF(action);
|
|
parcel = Py_None;
|
|
Py_INCREF(parcel);
|
|
}
|
|
|
|
res = PyTuple_New(2);
|
|
|
|
if (res) {
|
|
PyTuple_SET_ITEM(res, 0, action);
|
|
PyTuple_SET_ITEM(res, 1, parcel);
|
|
PyList_Append(self->events, res);
|
|
Py_DECREF(res);
|
|
} else
|
|
PyErr_Clear(); /* FIXME: propagate error */
|
|
}
|
|
|
|
/* -------------------------------------------------------------------- */
|
|
/* methods (in alphabetical order) */
|
|
|
|
static PyObject*
|
|
treebuilder_data(TreeBuilderObject* self, PyObject* args)
|
|
{
|
|
PyObject* data;
|
|
if (!PyArg_ParseTuple(args, "O:data", &data))
|
|
return NULL;
|
|
|
|
return treebuilder_handle_data(self, data);
|
|
}
|
|
|
|
static PyObject*
|
|
treebuilder_end(TreeBuilderObject* self, PyObject* args)
|
|
{
|
|
PyObject* tag;
|
|
if (!PyArg_ParseTuple(args, "O:end", &tag))
|
|
return NULL;
|
|
|
|
return treebuilder_handle_end(self, tag);
|
|
}
|
|
|
|
LOCAL(PyObject*)
|
|
treebuilder_done(TreeBuilderObject* self)
|
|
{
|
|
PyObject* res;
|
|
|
|
/* FIXME: check stack size? */
|
|
|
|
if (self->root)
|
|
res = self->root;
|
|
else
|
|
res = Py_None;
|
|
|
|
Py_INCREF(res);
|
|
return res;
|
|
}
|
|
|
|
static PyObject*
|
|
treebuilder_close(TreeBuilderObject* self, PyObject* args)
|
|
{
|
|
if (!PyArg_ParseTuple(args, ":close"))
|
|
return NULL;
|
|
|
|
return treebuilder_done(self);
|
|
}
|
|
|
|
static PyObject*
|
|
treebuilder_start(TreeBuilderObject* self, PyObject* args)
|
|
{
|
|
PyObject* tag;
|
|
PyObject* attrib = Py_None;
|
|
if (!PyArg_ParseTuple(args, "O|O:start", &tag, &attrib))
|
|
return NULL;
|
|
|
|
return treebuilder_handle_start(self, tag, attrib);
|
|
}
|
|
|
|
static PyObject*
|
|
treebuilder_xml(TreeBuilderObject* self, PyObject* args)
|
|
{
|
|
PyObject* encoding;
|
|
PyObject* standalone;
|
|
if (!PyArg_ParseTuple(args, "OO:xml", &encoding, &standalone))
|
|
return NULL;
|
|
|
|
return treebuilder_handle_xml(self, encoding, standalone);
|
|
}
|
|
|
|
static PyMethodDef treebuilder_methods[] = {
|
|
{"data", (PyCFunction) treebuilder_data, METH_VARARGS},
|
|
{"start", (PyCFunction) treebuilder_start, METH_VARARGS},
|
|
{"end", (PyCFunction) treebuilder_end, METH_VARARGS},
|
|
{"xml", (PyCFunction) treebuilder_xml, METH_VARARGS},
|
|
{"close", (PyCFunction) treebuilder_close, METH_VARARGS},
|
|
{NULL, NULL}
|
|
};
|
|
|
|
static PyObject*
|
|
treebuilder_getattr(TreeBuilderObject* self, char* name)
|
|
{
|
|
return Py_FindMethod(treebuilder_methods, (PyObject*) self, name);
|
|
}
|
|
|
|
static PyTypeObject TreeBuilder_Type = {
|
|
PyObject_HEAD_INIT(NULL)
|
|
0, "TreeBuilder", sizeof(TreeBuilderObject), 0,
|
|
/* methods */
|
|
(destructor)treebuilder_dealloc, /* tp_dealloc */
|
|
0, /* tp_print */
|
|
(getattrfunc)treebuilder_getattr, /* tp_getattr */
|
|
};
|
|
|
|
/* ==================================================================== */
|
|
/* the expat interface */
|
|
|
|
#if defined(USE_EXPAT)
|
|
|
|
#include "expat.h"
|
|
|
|
#if defined(USE_PYEXPAT_CAPI)
|
|
#include "pyexpat.h"
|
|
static struct PyExpat_CAPI* expat_capi;
|
|
#define EXPAT(func) (expat_capi->func)
|
|
#else
|
|
#define EXPAT(func) (XML_##func)
|
|
#endif
|
|
|
|
typedef struct {
|
|
PyObject_HEAD
|
|
|
|
XML_Parser parser;
|
|
|
|
PyObject* target;
|
|
PyObject* entity;
|
|
|
|
PyObject* names;
|
|
|
|
PyObject* handle_xml;
|
|
PyObject* handle_start;
|
|
PyObject* handle_data;
|
|
PyObject* handle_end;
|
|
|
|
PyObject* handle_comment;
|
|
PyObject* handle_pi;
|
|
|
|
} XMLParserObject;
|
|
|
|
static PyTypeObject XMLParser_Type;
|
|
|
|
/* helpers */
|
|
|
|
#if defined(Py_USING_UNICODE)
|
|
LOCAL(int)
|
|
checkstring(const char* string, int size)
|
|
{
|
|
int i;
|
|
|
|
/* check if an 8-bit string contains UTF-8 characters */
|
|
for (i = 0; i < size; i++)
|
|
if (string[i] & 0x80)
|
|
return 1;
|
|
|
|
return 0;
|
|
}
|
|
#endif
|
|
|
|
LOCAL(PyObject*)
|
|
makestring(const char* string, int size)
|
|
{
|
|
/* convert a UTF-8 string to either a 7-bit ascii string or a
|
|
Unicode string */
|
|
|
|
#if defined(Py_USING_UNICODE)
|
|
if (checkstring(string, size))
|
|
return PyUnicode_DecodeUTF8(string, size, "strict");
|
|
#endif
|
|
|
|
return PyString_FromStringAndSize(string, size);
|
|
}
|
|
|
|
LOCAL(PyObject*)
|
|
makeuniversal(XMLParserObject* self, const char* string)
|
|
{
|
|
/* convert a UTF-8 tag/attribute name from the expat parser
|
|
to a universal name string */
|
|
|
|
int size = strlen(string);
|
|
PyObject* key;
|
|
PyObject* value;
|
|
|
|
/* look the 'raw' name up in the names dictionary */
|
|
key = PyString_FromStringAndSize(string, size);
|
|
if (!key)
|
|
return NULL;
|
|
|
|
value = PyDict_GetItem(self->names, key);
|
|
|
|
if (value) {
|
|
Py_INCREF(value);
|
|
} else {
|
|
/* new name. convert to universal name, and decode as
|
|
necessary */
|
|
|
|
PyObject* tag;
|
|
char* p;
|
|
int i;
|
|
|
|
/* look for namespace separator */
|
|
for (i = 0; i < size; i++)
|
|
if (string[i] == '}')
|
|
break;
|
|
if (i != size) {
|
|
/* convert to universal name */
|
|
tag = PyString_FromStringAndSize(NULL, size+1);
|
|
p = PyString_AS_STRING(tag);
|
|
p[0] = '{';
|
|
memcpy(p+1, string, size);
|
|
size++;
|
|
} else {
|
|
/* plain name; use key as tag */
|
|
Py_INCREF(key);
|
|
tag = key;
|
|
}
|
|
|
|
/* decode universal name */
|
|
#if defined(Py_USING_UNICODE)
|
|
/* inline makestring, to avoid duplicating the source string if
|
|
it's not an utf-8 string */
|
|
p = PyString_AS_STRING(tag);
|
|
if (checkstring(p, size)) {
|
|
value = PyUnicode_DecodeUTF8(p, size, "strict");
|
|
Py_DECREF(tag);
|
|
if (!value) {
|
|
Py_DECREF(key);
|
|
return NULL;
|
|
}
|
|
} else
|
|
#endif
|
|
value = tag; /* use tag as is */
|
|
|
|
/* add to names dictionary */
|
|
if (PyDict_SetItem(self->names, key, value) < 0) {
|
|
Py_DECREF(key);
|
|
Py_DECREF(value);
|
|
return NULL;
|
|
}
|
|
}
|
|
|
|
Py_DECREF(key);
|
|
return value;
|
|
}
|
|
|
|
/* -------------------------------------------------------------------- */
|
|
/* handlers */
|
|
|
|
static void
|
|
expat_default_handler(XMLParserObject* self, const XML_Char* data_in,
|
|
int data_len)
|
|
{
|
|
PyObject* key;
|
|
PyObject* value;
|
|
PyObject* res;
|
|
|
|
if (data_len < 2 || data_in[0] != '&')
|
|
return;
|
|
|
|
key = makestring(data_in + 1, data_len - 2);
|
|
if (!key)
|
|
return;
|
|
|
|
value = PyDict_GetItem(self->entity, key);
|
|
|
|
if (value) {
|
|
if (TreeBuilder_CheckExact(self->target))
|
|
res = treebuilder_handle_data(
|
|
(TreeBuilderObject*) self->target, value
|
|
);
|
|
else if (self->handle_data)
|
|
res = PyObject_CallFunction(self->handle_data, "O", value);
|
|
else
|
|
res = NULL;
|
|
Py_XDECREF(res);
|
|
} else {
|
|
PyErr_Format(
|
|
PyExc_SyntaxError, "undefined entity &%s;: line %ld, column %ld",
|
|
PyString_AS_STRING(key),
|
|
EXPAT(GetErrorLineNumber)(self->parser),
|
|
EXPAT(GetErrorColumnNumber)(self->parser)
|
|
);
|
|
}
|
|
|
|
Py_DECREF(key);
|
|
}
|
|
|
|
static void
|
|
expat_start_handler(XMLParserObject* self, const XML_Char* tag_in,
|
|
const XML_Char **attrib_in)
|
|
{
|
|
PyObject* res;
|
|
PyObject* tag;
|
|
PyObject* attrib;
|
|
int ok;
|
|
|
|
/* tag name */
|
|
tag = makeuniversal(self, tag_in);
|
|
if (!tag)
|
|
return; /* parser will look for errors */
|
|
|
|
/* attributes */
|
|
if (attrib_in[0]) {
|
|
attrib = PyDict_New();
|
|
if (!attrib)
|
|
return;
|
|
while (attrib_in[0] && attrib_in[1]) {
|
|
PyObject* key = makeuniversal(self, attrib_in[0]);
|
|
PyObject* value = makestring(attrib_in[1], strlen(attrib_in[1]));
|
|
if (!key || !value) {
|
|
Py_XDECREF(value);
|
|
Py_XDECREF(key);
|
|
Py_DECREF(attrib);
|
|
return;
|
|
}
|
|
ok = PyDict_SetItem(attrib, key, value);
|
|
Py_DECREF(value);
|
|
Py_DECREF(key);
|
|
if (ok < 0) {
|
|
Py_DECREF(attrib);
|
|
return;
|
|
}
|
|
attrib_in += 2;
|
|
}
|
|
} else {
|
|
Py_INCREF(Py_None);
|
|
attrib = Py_None;
|
|
}
|
|
|
|
if (TreeBuilder_CheckExact(self->target))
|
|
/* shortcut */
|
|
res = treebuilder_handle_start((TreeBuilderObject*) self->target,
|
|
tag, attrib);
|
|
else if (self->handle_start)
|
|
res = PyObject_CallFunction(self->handle_start, "OO", tag, attrib);
|
|
else
|
|
res = NULL;
|
|
|
|
Py_DECREF(tag);
|
|
Py_DECREF(attrib);
|
|
|
|
Py_XDECREF(res);
|
|
}
|
|
|
|
static void
|
|
expat_data_handler(XMLParserObject* self, const XML_Char* data_in,
|
|
int data_len)
|
|
{
|
|
PyObject* data;
|
|
PyObject* res;
|
|
|
|
data = makestring(data_in, data_len);
|
|
if (!data)
|
|
return; /* parser will look for errors */
|
|
|
|
if (TreeBuilder_CheckExact(self->target))
|
|
/* shortcut */
|
|
res = treebuilder_handle_data((TreeBuilderObject*) self->target, data);
|
|
else if (self->handle_data)
|
|
res = PyObject_CallFunction(self->handle_data, "O", data);
|
|
else
|
|
res = NULL;
|
|
|
|
Py_DECREF(data);
|
|
|
|
Py_XDECREF(res);
|
|
}
|
|
|
|
static void
|
|
expat_end_handler(XMLParserObject* self, const XML_Char* tag_in)
|
|
{
|
|
PyObject* tag;
|
|
PyObject* res = NULL;
|
|
|
|
if (TreeBuilder_CheckExact(self->target))
|
|
/* shortcut */
|
|
/* the standard tree builder doesn't look at the end tag */
|
|
res = treebuilder_handle_end(
|
|
(TreeBuilderObject*) self->target, Py_None
|
|
);
|
|
else if (self->handle_end) {
|
|
tag = makeuniversal(self, tag_in);
|
|
if (tag) {
|
|
res = PyObject_CallFunction(self->handle_end, "O", tag);
|
|
Py_DECREF(tag);
|
|
}
|
|
}
|
|
|
|
Py_XDECREF(res);
|
|
}
|
|
|
|
static void
|
|
expat_start_ns_handler(XMLParserObject* self, const XML_Char* prefix,
|
|
const XML_Char *uri)
|
|
{
|
|
treebuilder_handle_namespace(
|
|
(TreeBuilderObject*) self->target, 1, prefix, uri
|
|
);
|
|
}
|
|
|
|
static void
|
|
expat_end_ns_handler(XMLParserObject* self, const XML_Char* prefix_in)
|
|
{
|
|
treebuilder_handle_namespace(
|
|
(TreeBuilderObject*) self->target, 0, NULL, NULL
|
|
);
|
|
}
|
|
|
|
static void
|
|
expat_comment_handler(XMLParserObject* self, const XML_Char* comment_in)
|
|
{
|
|
PyObject* comment;
|
|
PyObject* res;
|
|
|
|
if (self->handle_comment) {
|
|
comment = makestring(comment_in, strlen(comment_in));
|
|
if (comment) {
|
|
res = PyObject_CallFunction(self->handle_comment, "O", comment);
|
|
Py_XDECREF(res);
|
|
Py_DECREF(comment);
|
|
}
|
|
}
|
|
}
|
|
|
|
static void
|
|
expat_pi_handler(XMLParserObject* self, const XML_Char* target_in,
|
|
const XML_Char* data_in)
|
|
{
|
|
PyObject* target;
|
|
PyObject* data;
|
|
PyObject* res;
|
|
|
|
if (self->handle_pi) {
|
|
target = makestring(target_in, strlen(target_in));
|
|
data = makestring(data_in, strlen(data_in));
|
|
if (target && data) {
|
|
res = PyObject_CallFunction(self->handle_pi, "OO", target, data);
|
|
Py_XDECREF(res);
|
|
Py_DECREF(data);
|
|
Py_DECREF(target);
|
|
} else {
|
|
Py_XDECREF(data);
|
|
Py_XDECREF(target);
|
|
}
|
|
}
|
|
}
|
|
|
|
#if defined(Py_USING_UNICODE)
|
|
static int
|
|
expat_unknown_encoding_handler(XMLParserObject *self, const XML_Char *name,
|
|
XML_Encoding *info)
|
|
{
|
|
PyObject* u;
|
|
Py_UNICODE* p;
|
|
unsigned char s[256];
|
|
int i;
|
|
|
|
memset(info, 0, sizeof(XML_Encoding));
|
|
|
|
for (i = 0; i < 256; i++)
|
|
s[i] = i;
|
|
|
|
u = PyUnicode_Decode((char*) s, 256, name, "replace");
|
|
if (!u)
|
|
return XML_STATUS_ERROR;
|
|
|
|
if (PyUnicode_GET_SIZE(u) != 256) {
|
|
Py_DECREF(u);
|
|
return XML_STATUS_ERROR;
|
|
}
|
|
|
|
p = PyUnicode_AS_UNICODE(u);
|
|
|
|
for (i = 0; i < 256; i++) {
|
|
if (p[i] != Py_UNICODE_REPLACEMENT_CHARACTER)
|
|
info->map[i] = p[i];
|
|
else
|
|
info->map[i] = -1;
|
|
}
|
|
|
|
Py_DECREF(u);
|
|
|
|
return XML_STATUS_OK;
|
|
}
|
|
#endif
|
|
|
|
/* -------------------------------------------------------------------- */
|
|
/* constructor and destructor */
|
|
|
|
static PyObject*
|
|
xmlparser(PyObject* self_, PyObject* args, PyObject* kw)
|
|
{
|
|
XMLParserObject* self;
|
|
/* FIXME: does this need to be static? */
|
|
static XML_Memory_Handling_Suite memory_handler;
|
|
|
|
PyObject* target = NULL;
|
|
char* encoding = NULL;
|
|
static char* kwlist[] = { "target", "encoding", NULL };
|
|
if (!PyArg_ParseTupleAndKeywords(args, kw, "|Oz:XMLParser", kwlist,
|
|
&target, &encoding))
|
|
return NULL;
|
|
|
|
#if defined(USE_PYEXPAT_CAPI)
|
|
if (!expat_capi) {
|
|
PyErr_SetString(
|
|
PyExc_RuntimeError, "cannot load dispatch table from pyexpat"
|
|
);
|
|
return NULL;
|
|
}
|
|
#endif
|
|
|
|
self = PyObject_New(XMLParserObject, &XMLParser_Type);
|
|
if (self == NULL)
|
|
return NULL;
|
|
|
|
self->entity = PyDict_New();
|
|
if (!self->entity) {
|
|
PyObject_Del(self);
|
|
return NULL;
|
|
}
|
|
|
|
self->names = PyDict_New();
|
|
if (!self->names) {
|
|
PyObject_Del(self->entity);
|
|
PyObject_Del(self);
|
|
return NULL;
|
|
}
|
|
|
|
memory_handler.malloc_fcn = PyObject_Malloc;
|
|
memory_handler.realloc_fcn = PyObject_Realloc;
|
|
memory_handler.free_fcn = PyObject_Free;
|
|
|
|
self->parser = EXPAT(ParserCreate_MM)(encoding, &memory_handler, "}");
|
|
if (!self->parser) {
|
|
PyObject_Del(self->names);
|
|
PyObject_Del(self->entity);
|
|
PyObject_Del(self);
|
|
PyErr_NoMemory();
|
|
return NULL;
|
|
}
|
|
|
|
/* setup target handlers */
|
|
if (!target) {
|
|
target = treebuilder_new();
|
|
if (!target) {
|
|
EXPAT(ParserFree)(self->parser);
|
|
PyObject_Del(self->names);
|
|
PyObject_Del(self->entity);
|
|
PyObject_Del(self);
|
|
return NULL;
|
|
}
|
|
} else
|
|
Py_INCREF(target);
|
|
self->target = target;
|
|
|
|
self->handle_xml = PyObject_GetAttrString(target, "xml");
|
|
self->handle_start = PyObject_GetAttrString(target, "start");
|
|
self->handle_data = PyObject_GetAttrString(target, "data");
|
|
self->handle_end = PyObject_GetAttrString(target, "end");
|
|
self->handle_comment = PyObject_GetAttrString(target, "comment");
|
|
self->handle_pi = PyObject_GetAttrString(target, "pi");
|
|
|
|
PyErr_Clear();
|
|
|
|
/* configure parser */
|
|
EXPAT(SetUserData)(self->parser, self);
|
|
EXPAT(SetElementHandler)(
|
|
self->parser,
|
|
(XML_StartElementHandler) expat_start_handler,
|
|
(XML_EndElementHandler) expat_end_handler
|
|
);
|
|
EXPAT(SetDefaultHandlerExpand)(
|
|
self->parser,
|
|
(XML_DefaultHandler) expat_default_handler
|
|
);
|
|
EXPAT(SetCharacterDataHandler)(
|
|
self->parser,
|
|
(XML_CharacterDataHandler) expat_data_handler
|
|
);
|
|
if (self->handle_comment)
|
|
EXPAT(SetCommentHandler)(
|
|
self->parser,
|
|
(XML_CommentHandler) expat_comment_handler
|
|
);
|
|
if (self->handle_pi)
|
|
EXPAT(SetProcessingInstructionHandler)(
|
|
self->parser,
|
|
(XML_ProcessingInstructionHandler) expat_pi_handler
|
|
);
|
|
#if defined(Py_USING_UNICODE)
|
|
EXPAT(SetUnknownEncodingHandler)(
|
|
self->parser,
|
|
(XML_UnknownEncodingHandler) expat_unknown_encoding_handler, NULL
|
|
);
|
|
#endif
|
|
|
|
ALLOC(sizeof(XMLParserObject), "create expatparser");
|
|
|
|
return (PyObject*) self;
|
|
}
|
|
|
|
static void
|
|
xmlparser_dealloc(XMLParserObject* self)
|
|
{
|
|
EXPAT(ParserFree)(self->parser);
|
|
|
|
Py_XDECREF(self->handle_pi);
|
|
Py_XDECREF(self->handle_comment);
|
|
Py_XDECREF(self->handle_end);
|
|
Py_XDECREF(self->handle_data);
|
|
Py_XDECREF(self->handle_start);
|
|
Py_XDECREF(self->handle_xml);
|
|
|
|
Py_DECREF(self->target);
|
|
Py_DECREF(self->entity);
|
|
Py_DECREF(self->names);
|
|
|
|
RELEASE(sizeof(XMLParserObject), "destroy expatparser");
|
|
|
|
PyObject_Del(self);
|
|
}
|
|
|
|
/* -------------------------------------------------------------------- */
|
|
/* methods (in alphabetical order) */
|
|
|
|
LOCAL(PyObject*)
|
|
expat_parse(XMLParserObject* self, char* data, int data_len, int final)
|
|
{
|
|
int ok;
|
|
|
|
ok = EXPAT(Parse)(self->parser, data, data_len, final);
|
|
|
|
if (PyErr_Occurred())
|
|
return NULL;
|
|
|
|
if (!ok) {
|
|
PyErr_Format(
|
|
PyExc_SyntaxError, "%s: line %ld, column %ld",
|
|
EXPAT(ErrorString)(EXPAT(GetErrorCode)(self->parser)),
|
|
EXPAT(GetErrorLineNumber)(self->parser),
|
|
EXPAT(GetErrorColumnNumber)(self->parser)
|
|
);
|
|
return NULL;
|
|
}
|
|
|
|
Py_RETURN_NONE;
|
|
}
|
|
|
|
static PyObject*
|
|
xmlparser_close(XMLParserObject* self, PyObject* args)
|
|
{
|
|
/* end feeding data to parser */
|
|
|
|
PyObject* res;
|
|
if (!PyArg_ParseTuple(args, ":close"))
|
|
return NULL;
|
|
|
|
res = expat_parse(self, "", 0, 1);
|
|
|
|
if (res && TreeBuilder_CheckExact(self->target)) {
|
|
Py_DECREF(res);
|
|
return treebuilder_done((TreeBuilderObject*) self->target);
|
|
}
|
|
|
|
return res;
|
|
}
|
|
|
|
static PyObject*
|
|
xmlparser_feed(XMLParserObject* self, PyObject* args)
|
|
{
|
|
/* feed data to parser */
|
|
|
|
char* data;
|
|
int data_len;
|
|
if (!PyArg_ParseTuple(args, "s#:feed", &data, &data_len))
|
|
return NULL;
|
|
|
|
return expat_parse(self, data, data_len, 0);
|
|
}
|
|
|
|
static PyObject*
|
|
xmlparser_parse(XMLParserObject* self, PyObject* args)
|
|
{
|
|
/* (internal) parse until end of input stream */
|
|
|
|
PyObject* reader;
|
|
PyObject* buffer;
|
|
PyObject* res;
|
|
|
|
PyObject* fileobj;
|
|
if (!PyArg_ParseTuple(args, "O:_parse", &fileobj))
|
|
return NULL;
|
|
|
|
reader = PyObject_GetAttrString(fileobj, "read");
|
|
if (!reader)
|
|
return NULL;
|
|
|
|
/* read from open file object */
|
|
for (;;) {
|
|
|
|
buffer = PyObject_CallFunction(reader, "i", 64*1024);
|
|
|
|
if (!buffer) {
|
|
/* read failed (e.g. due to KeyboardInterrupt) */
|
|
Py_DECREF(reader);
|
|
return NULL;
|
|
}
|
|
|
|
if (!PyString_CheckExact(buffer) || PyString_GET_SIZE(buffer) == 0) {
|
|
Py_DECREF(buffer);
|
|
break;
|
|
}
|
|
|
|
res = expat_parse(
|
|
self, PyString_AS_STRING(buffer), PyString_GET_SIZE(buffer), 0
|
|
);
|
|
|
|
Py_DECREF(buffer);
|
|
|
|
if (!res) {
|
|
Py_DECREF(reader);
|
|
return NULL;
|
|
}
|
|
Py_DECREF(res);
|
|
|
|
}
|
|
|
|
Py_DECREF(reader);
|
|
|
|
res = expat_parse(self, "", 0, 1);
|
|
|
|
if (res && TreeBuilder_CheckExact(self->target)) {
|
|
Py_DECREF(res);
|
|
return treebuilder_done((TreeBuilderObject*) self->target);
|
|
}
|
|
|
|
return res;
|
|
}
|
|
|
|
static PyObject*
|
|
xmlparser_setevents(XMLParserObject* self, PyObject* args)
|
|
{
|
|
/* activate element event reporting */
|
|
|
|
Py_ssize_t i;
|
|
TreeBuilderObject* target;
|
|
|
|
PyObject* events; /* event collector */
|
|
PyObject* event_set = Py_None;
|
|
if (!PyArg_ParseTuple(args, "O!|O:_setevents", &PyList_Type, &events,
|
|
&event_set))
|
|
return NULL;
|
|
|
|
if (!TreeBuilder_CheckExact(self->target)) {
|
|
PyErr_SetString(
|
|
PyExc_TypeError,
|
|
"event handling only supported for cElementTree.Treebuilder "
|
|
"targets"
|
|
);
|
|
return NULL;
|
|
}
|
|
|
|
target = (TreeBuilderObject*) self->target;
|
|
|
|
Py_INCREF(events);
|
|
Py_XDECREF(target->events);
|
|
target->events = events;
|
|
|
|
/* clear out existing events */
|
|
Py_XDECREF(target->start_event_obj); target->start_event_obj = NULL;
|
|
Py_XDECREF(target->end_event_obj); target->end_event_obj = NULL;
|
|
Py_XDECREF(target->start_ns_event_obj); target->start_ns_event_obj = NULL;
|
|
Py_XDECREF(target->end_ns_event_obj); target->end_ns_event_obj = NULL;
|
|
|
|
if (event_set == Py_None) {
|
|
/* default is "end" only */
|
|
target->end_event_obj = PyString_FromString("end");
|
|
Py_RETURN_NONE;
|
|
}
|
|
|
|
if (!PyTuple_Check(event_set)) /* FIXME: handle arbitrary sequences */
|
|
goto error;
|
|
|
|
for (i = 0; i < PyTuple_GET_SIZE(event_set); i++) {
|
|
PyObject* item = PyTuple_GET_ITEM(event_set, i);
|
|
char* event;
|
|
if (!PyString_Check(item))
|
|
goto error;
|
|
event = PyString_AS_STRING(item);
|
|
if (strcmp(event, "start") == 0) {
|
|
Py_INCREF(item);
|
|
target->start_event_obj = item;
|
|
} else if (strcmp(event, "end") == 0) {
|
|
Py_INCREF(item);
|
|
Py_XDECREF(target->end_event_obj);
|
|
target->end_event_obj = item;
|
|
} else if (strcmp(event, "start-ns") == 0) {
|
|
Py_INCREF(item);
|
|
Py_XDECREF(target->start_ns_event_obj);
|
|
target->start_ns_event_obj = item;
|
|
EXPAT(SetNamespaceDeclHandler)(
|
|
self->parser,
|
|
(XML_StartNamespaceDeclHandler) expat_start_ns_handler,
|
|
(XML_EndNamespaceDeclHandler) expat_end_ns_handler
|
|
);
|
|
} else if (strcmp(event, "end-ns") == 0) {
|
|
Py_INCREF(item);
|
|
Py_XDECREF(target->end_ns_event_obj);
|
|
target->end_ns_event_obj = item;
|
|
EXPAT(SetNamespaceDeclHandler)(
|
|
self->parser,
|
|
(XML_StartNamespaceDeclHandler) expat_start_ns_handler,
|
|
(XML_EndNamespaceDeclHandler) expat_end_ns_handler
|
|
);
|
|
} else {
|
|
PyErr_Format(
|
|
PyExc_ValueError,
|
|
"unknown event '%s'", event
|
|
);
|
|
return NULL;
|
|
}
|
|
}
|
|
|
|
Py_RETURN_NONE;
|
|
|
|
error:
|
|
PyErr_SetString(
|
|
PyExc_TypeError,
|
|
"invalid event tuple"
|
|
);
|
|
return NULL;
|
|
}
|
|
|
|
static PyMethodDef xmlparser_methods[] = {
|
|
{"feed", (PyCFunction) xmlparser_feed, METH_VARARGS},
|
|
{"close", (PyCFunction) xmlparser_close, METH_VARARGS},
|
|
{"_parse", (PyCFunction) xmlparser_parse, METH_VARARGS},
|
|
{"_setevents", (PyCFunction) xmlparser_setevents, METH_VARARGS},
|
|
{NULL, NULL}
|
|
};
|
|
|
|
static PyObject*
|
|
xmlparser_getattr(XMLParserObject* self, char* name)
|
|
{
|
|
PyObject* res;
|
|
|
|
res = Py_FindMethod(xmlparser_methods, (PyObject*) self, name);
|
|
if (res)
|
|
return res;
|
|
|
|
PyErr_Clear();
|
|
|
|
if (strcmp(name, "entity") == 0)
|
|
res = self->entity;
|
|
else if (strcmp(name, "target") == 0)
|
|
res = self->target;
|
|
else if (strcmp(name, "version") == 0) {
|
|
char buffer[100];
|
|
sprintf(buffer, "Expat %d.%d.%d", XML_MAJOR_VERSION,
|
|
XML_MINOR_VERSION, XML_MICRO_VERSION);
|
|
return PyString_FromString(buffer);
|
|
} else {
|
|
PyErr_SetString(PyExc_AttributeError, name);
|
|
return NULL;
|
|
}
|
|
|
|
Py_INCREF(res);
|
|
return res;
|
|
}
|
|
|
|
static PyTypeObject XMLParser_Type = {
|
|
PyObject_HEAD_INIT(NULL)
|
|
0, "XMLParser", sizeof(XMLParserObject), 0,
|
|
/* methods */
|
|
(destructor)xmlparser_dealloc, /* tp_dealloc */
|
|
0, /* tp_print */
|
|
(getattrfunc)xmlparser_getattr, /* tp_getattr */
|
|
};
|
|
|
|
#endif
|
|
|
|
/* ==================================================================== */
|
|
/* python module interface */
|
|
|
|
static PyMethodDef _functions[] = {
|
|
{"Element", (PyCFunction) element, METH_VARARGS|METH_KEYWORDS},
|
|
{"SubElement", (PyCFunction) subelement, METH_VARARGS|METH_KEYWORDS},
|
|
{"TreeBuilder", (PyCFunction) treebuilder, METH_VARARGS},
|
|
#if defined(USE_EXPAT)
|
|
{"XMLParser", (PyCFunction) xmlparser, METH_VARARGS|METH_KEYWORDS},
|
|
{"XMLTreeBuilder", (PyCFunction) xmlparser, METH_VARARGS|METH_KEYWORDS},
|
|
#endif
|
|
{NULL, NULL}
|
|
};
|
|
|
|
DL_EXPORT(void)
|
|
init_elementtree(void)
|
|
{
|
|
PyObject* m;
|
|
PyObject* g;
|
|
char* bootstrap;
|
|
#if defined(USE_PYEXPAT_CAPI)
|
|
struct PyExpat_CAPI* capi;
|
|
#endif
|
|
|
|
/* Patch object type */
|
|
Element_Type.ob_type = TreeBuilder_Type.ob_type = &PyType_Type;
|
|
#if defined(USE_EXPAT)
|
|
XMLParser_Type.ob_type = &PyType_Type;
|
|
#endif
|
|
|
|
m = Py_InitModule("_elementtree", _functions);
|
|
if (!m)
|
|
return;
|
|
|
|
/* python glue code */
|
|
|
|
g = PyDict_New();
|
|
if (!g)
|
|
return;
|
|
|
|
PyDict_SetItemString(g, "__builtins__", PyEval_GetBuiltins());
|
|
|
|
bootstrap = (
|
|
|
|
#if (PY_VERSION_HEX >= 0x02020000 && PY_VERSION_HEX < 0x02030000)
|
|
"from __future__ import generators\n" /* enable yield under 2.2 */
|
|
#endif
|
|
|
|
"from copy import copy, deepcopy\n"
|
|
|
|
"try:\n"
|
|
" from xml.etree import ElementTree\n"
|
|
"except ImportError:\n"
|
|
" import ElementTree\n"
|
|
"ET = ElementTree\n"
|
|
"del ElementTree\n"
|
|
|
|
"import _elementtree as cElementTree\n"
|
|
|
|
"try:\n" /* check if copy works as is */
|
|
" copy(cElementTree.Element('x'))\n"
|
|
"except:\n"
|
|
" def copyelement(elem):\n"
|
|
" return elem\n"
|
|
|
|
"def Comment(text=None):\n" /* public */
|
|
" element = cElementTree.Element(ET.Comment)\n"
|
|
" element.text = text\n"
|
|
" return element\n"
|
|
"cElementTree.Comment = Comment\n"
|
|
|
|
"class ElementTree(ET.ElementTree):\n" /* public */
|
|
" def parse(self, source, parser=None):\n"
|
|
" if not hasattr(source, 'read'):\n"
|
|
" source = open(source, 'rb')\n"
|
|
" if parser is not None:\n"
|
|
" while 1:\n"
|
|
" data = source.read(65536)\n"
|
|
" if not data:\n"
|
|
" break\n"
|
|
" parser.feed(data)\n"
|
|
" self._root = parser.close()\n"
|
|
" else:\n"
|
|
" parser = cElementTree.XMLParser()\n"
|
|
" self._root = parser._parse(source)\n"
|
|
" return self._root\n"
|
|
"cElementTree.ElementTree = ElementTree\n"
|
|
|
|
"def getiterator(node, tag=None):\n" /* helper */
|
|
" if tag == '*':\n"
|
|
" tag = None\n"
|
|
#if (PY_VERSION_HEX < 0x02020000)
|
|
" nodes = []\n" /* 2.1 doesn't have yield */
|
|
" if tag is None or node.tag == tag:\n"
|
|
" nodes.append(node)\n"
|
|
" for node in node:\n"
|
|
" nodes.extend(getiterator(node, tag))\n"
|
|
" return nodes\n"
|
|
#else
|
|
" if tag is None or node.tag == tag:\n"
|
|
" yield node\n"
|
|
" for node in node:\n"
|
|
" for node in getiterator(node, tag):\n"
|
|
" yield node\n"
|
|
#endif
|
|
|
|
"def parse(source, parser=None):\n" /* public */
|
|
" tree = ElementTree()\n"
|
|
" tree.parse(source, parser)\n"
|
|
" return tree\n"
|
|
"cElementTree.parse = parse\n"
|
|
|
|
#if (PY_VERSION_HEX < 0x02020000)
|
|
"if hasattr(ET, 'iterparse'):\n"
|
|
" cElementTree.iterparse = ET.iterparse\n" /* delegate on 2.1 */
|
|
#else
|
|
"class iterparse(object):\n"
|
|
" root = None\n"
|
|
" def __init__(self, file, events=None):\n"
|
|
" if not hasattr(file, 'read'):\n"
|
|
" file = open(file, 'rb')\n"
|
|
" self._file = file\n"
|
|
" self._events = events\n"
|
|
" def __iter__(self):\n"
|
|
" events = []\n"
|
|
" b = cElementTree.TreeBuilder()\n"
|
|
" p = cElementTree.XMLParser(b)\n"
|
|
" p._setevents(events, self._events)\n"
|
|
" while 1:\n"
|
|
" data = self._file.read(16384)\n"
|
|
" if not data:\n"
|
|
" break\n"
|
|
" p.feed(data)\n"
|
|
" for event in events:\n"
|
|
" yield event\n"
|
|
" del events[:]\n"
|
|
" root = p.close()\n"
|
|
" for event in events:\n"
|
|
" yield event\n"
|
|
" self.root = root\n"
|
|
"cElementTree.iterparse = iterparse\n"
|
|
#endif
|
|
|
|
"def PI(target, text=None):\n" /* public */
|
|
" element = cElementTree.Element(ET.ProcessingInstruction)\n"
|
|
" element.text = target\n"
|
|
" if text:\n"
|
|
" element.text = element.text + ' ' + text\n"
|
|
" return element\n"
|
|
|
|
" elem = cElementTree.Element(ET.PI)\n"
|
|
" elem.text = text\n"
|
|
" return elem\n"
|
|
"cElementTree.PI = cElementTree.ProcessingInstruction = PI\n"
|
|
|
|
"def XML(text):\n" /* public */
|
|
" parser = cElementTree.XMLParser()\n"
|
|
" parser.feed(text)\n"
|
|
" return parser.close()\n"
|
|
"cElementTree.XML = cElementTree.fromstring = XML\n"
|
|
|
|
"def XMLID(text):\n" /* public */
|
|
" tree = XML(text)\n"
|
|
" ids = {}\n"
|
|
" for elem in tree.getiterator():\n"
|
|
" id = elem.get('id')\n"
|
|
" if id:\n"
|
|
" ids[id] = elem\n"
|
|
" return tree, ids\n"
|
|
"cElementTree.XMLID = XMLID\n"
|
|
|
|
"cElementTree.dump = ET.dump\n"
|
|
"cElementTree.ElementPath = ElementPath = ET.ElementPath\n"
|
|
"cElementTree.iselement = ET.iselement\n"
|
|
"cElementTree.QName = ET.QName\n"
|
|
"cElementTree.tostring = ET.tostring\n"
|
|
"cElementTree.VERSION = '" VERSION "'\n"
|
|
"cElementTree.__version__ = '" VERSION "'\n"
|
|
"cElementTree.XMLParserError = SyntaxError\n"
|
|
|
|
);
|
|
|
|
PyRun_String(bootstrap, Py_file_input, g, NULL);
|
|
|
|
elementpath_obj = PyDict_GetItemString(g, "ElementPath");
|
|
|
|
elementtree_copyelement_obj = PyDict_GetItemString(g, "copyelement");
|
|
if (elementtree_copyelement_obj) {
|
|
/* reduce hack needed; enable reduce method */
|
|
PyMethodDef* mp;
|
|
for (mp = element_methods; mp->ml_name; mp++)
|
|
if (mp->ml_meth == (PyCFunction) element_reduce) {
|
|
mp->ml_name = "__reduce__";
|
|
break;
|
|
}
|
|
} else
|
|
PyErr_Clear();
|
|
elementtree_deepcopy_obj = PyDict_GetItemString(g, "deepcopy");
|
|
elementtree_getiterator_obj = PyDict_GetItemString(g, "getiterator");
|
|
|
|
#if defined(USE_PYEXPAT_CAPI)
|
|
/* link against pyexpat, if possible */
|
|
capi = PyCObject_Import("pyexpat", "expat_CAPI");
|
|
if (capi &&
|
|
strcmp(capi->magic, PyExpat_CAPI_MAGIC) == 0 &&
|
|
capi->size <= sizeof(*expat_capi) &&
|
|
capi->MAJOR_VERSION == XML_MAJOR_VERSION &&
|
|
capi->MINOR_VERSION == XML_MINOR_VERSION &&
|
|
capi->MICRO_VERSION == XML_MICRO_VERSION)
|
|
expat_capi = capi;
|
|
else
|
|
expat_capi = NULL;
|
|
#endif
|
|
|
|
}
|