A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://mail.python.org/pipermail/python-dev/2000-February/002323.html below:

[Python-Dev] Safe destruction of recursive objects

[Python-Dev] Safe destruction of recursive objectsChristian Tismer tismer@tismer.com
Sun, 27 Feb 2000 17:30:25 +0100
Hi Guido,

When a user does the following with standard Python:

tup = ()
for i in xrange(100000): tup = (tup, i)

del tup  # ka-boom

He will get a core dump due to stack limitations.
Recently, I changed Stackless Python to be safe
for any recursive object built from
lists, tuples, dictionaries, tracebacks and frames.

The implementation is not Stackless Python dependant
and very efficient (for my eyes at least).

For efficiency, locality and minimum changes to five
modules, it is implemented as two embracing macroes
which are stuffed around the bodies of the deallocator
methods, that makes just 3-4 lines of change for
every module.
(Well, the macro *can* be expanded if you like that more)

I can submit patches, but please have a look at the example
below, to save me the time in case you don't like it.

It works great for SLP.

cheers - chris


--------------------------------------
Example of modified list deallocator:

/* Methods */

static void
list_dealloc(op)
    PyListObject *op;
{
    int i;
    Py_TRASHCAN_SAFE_BEGIN(op)
    if (op->ob_item != NULL) {
        /* Do it backwards, for Christian Tismer.
           There's a simple test case where somehow this reduces
           thrashing when a *very* large list is created and
           immediately deleted. */
        i = op->ob_size;
        while (--i >= 0) {
            Py_XDECREF(op->ob_item[i]);
        }
        free((ANY *)op->ob_item);
    }
    free((ANY *)op);
    Py_TRASHCAN_SAFE_END(op)
}

This is the original 1.5.2+ code, with two macro lines added.

--------------------------------------
Here the macro code (which may of course be expanded)

#define PyTrash_UNWIND_LEVEL 50

#define Py_TRASHCAN_SAFE_BEGIN(op) \
    { \
        ++_PyTrash_delete_nesting; \
        if (_PyTrash_delete_nesting < PyTrash_UNWIND_LEVEL) { \

#define Py_TRASHCAN_SAFE_END(op) \
        ;} \
        else { \
            if (!_PyTrash_delete_later) \
                _PyTrash_delete_later = PyList_New(0); \
            if (_PyTrash_delete_later) \
                PyList_Append(_PyTrash_delete_later, (PyObject *)op); \
        } \
        --_PyTrash_delete_nesting; \
        while (_PyTrash_delete_later && _PyTrash_delete_nesting <= 0) {
\
            PyObject *shredder = _PyTrash_delete_later; \
            _PyTrash_delete_later = NULL; \
            ++_PyTrash_delete_nesting; \
            Py_DECREF(shredder); \
            --_PyTrash_delete_nesting; \
        } \
    } \

extern DL_IMPORT(int) _PyTrash_delete_nesting;
extern DL_IMPORT(PyObject *) _PyTrash_delete_later;

-- 
Christian Tismer             :^)   <mailto:tismer@appliedbiometrics.com>
Applied Biometrics GmbH      :     Have a break! Take a ride on Python's
Kaunstr. 26                  :    *Starship* http://starship.python.net
14163 Berlin                 :     PGP key -> http://wwwkeys.pgp.net
PGP Fingerprint       E182 71C7 1A9D 66E9 9D15  D3CC D4D7 93E2 1FAE F6DF
     we're tired of banana software - shipped green, ripens at home



RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4