/* Support for dynamic loading of extension modules */ #include "Python.h" #include "importdl.h" #include /* for isdigit() */ #include /* for global errno */ #include /* for strerror() */ #include /* for malloc(), free() */ #include #ifdef AIX_GENUINE_CPLUSPLUS #include #define aix_load loadAndInit #else #define aix_load load #endif extern char *Py_GetProgramName(void); typedef struct Module { struct Module *next; void *entry; } Module, *ModulePtr; const struct filedescr _PyImport_DynLoadFiletab[] = { {".so", "rb", C_EXTENSION}, {"module.so", "rb", C_EXTENSION}, {0, 0} }; static int aix_getoldmodules(void **modlistptr) { register ModulePtr modptr, prevmodptr; register struct ld_info *ldiptr; register char *ldibuf; register int errflag, bufsize = 1024; register unsigned int offset; char *progname = Py_GetProgramName(); /* -- Get the list of loaded modules into ld_info structures. */ if ((ldibuf = malloc(bufsize)) == NULL) { PyErr_SetString(PyExc_ImportError, strerror(errno)); return -1; } while ((errflag = loadquery(L_GETINFO, ldibuf, bufsize)) == -1 && errno == ENOMEM) { free(ldibuf); bufsize += 1024; if ((ldibuf = malloc(bufsize)) == NULL) { PyErr_SetString(PyExc_ImportError, strerror(errno)); return -1; } } if (errflag == -1) { PyErr_SetString(PyExc_ImportError, strerror(errno)); return -1; } /* -- Make the modules list from the ld_info structures. */ ldiptr = (struct ld_info *)ldibuf; prevmodptr = NULL; do { if (strstr(progname, ldiptr->ldinfo_filename) == NULL && strstr(ldiptr->ldinfo_filename, "python") == NULL) { /* -- Extract only the modules belonging to the main -- executable + those containing "python" as a -- substring (like the "python[version]" binary or -- "libpython[version].a" in case it's a shared lib). */ offset = (unsigned int)ldiptr->ldinfo_next; ldiptr = (struct ld_info *)((char*)ldiptr + offset); continue; } if ((modptr = (ModulePtr)malloc(sizeof(Module))) == NULL) { PyErr_SetString(PyExc_ImportError, strerror(errno)); while (*modlistptr) { modptr = (ModulePtr)*modlistptr; *modlistptr = (void *)modptr->next; free(modptr); } return -1; } modptr->entry = ldiptr->ldinfo_dataorg; modptr->next = NULL; if (prevmodptr == NULL) *modlistptr = (void *)modptr; else prevmodptr->next = modptr; prevmodptr = modptr; offset = (unsigned int)ldiptr->ldinfo_next; ldiptr = (struct ld_info *)((char*)ldiptr + offset); } while (offset); free(ldibuf); return 0; } static void aix_loaderror(const char *pathname) { char *message[1024], errbuf[1024]; register int i,j; struct errtab { int errNo; char *errstr; } load_errtab[] = { {L_ERROR_TOOMANY, "too many errors, rest skipped."}, {L_ERROR_NOLIB, "can't load library:"}, {L_ERROR_UNDEF, "can't find symbol in library:"}, {L_ERROR_RLDBAD, "RLD index out of range or bad relocation type:"}, {L_ERROR_FORMAT, "not a valid, executable xcoff file:"}, {L_ERROR_MEMBER, "file not an archive or does not contain requested member:"}, {L_ERROR_TYPE, "symbol table mismatch:"}, {L_ERROR_ALIGN, "text alignment in file is wrong."}, {L_ERROR_SYSTEM, "System error:"}, {L_ERROR_ERRNO, NULL} }; #define LOAD_ERRTAB_LEN (sizeof(load_errtab)/sizeof(load_errtab[0])) #define ERRBUF_APPEND(s) strncat(errbuf, s, sizeof(errbuf)-strlen(errbuf)-1) PyOS_snprintf(errbuf, sizeof(errbuf), "from module %.200s ", pathname); if (!loadquery(L_GETMESSAGES, &message[0], sizeof(message))) { ERRBUF_APPEND(strerror(errno)); ERRBUF_APPEND("\n"); } for(i = 0; message[i] && *message[i]; i++) { int nerr = atoi(message[i]); for (j=0; j 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
doctests = """
########### Tests mostly copied from test_listcomps.py ############

Test simple loop with conditional

    >>> sum({i*i for i in range(100) if i&1 == 1})
    166650

Test simple case

    >>> {2*y + x + 1 for x in (0,) for y in (1,)}
    {3}

Test simple nesting

    >>> list(sorted({(i,j) for i in range(3) for j in range(4)}))
    [(0, 0), (0, 1), (0, 2), (0, 3), (1, 0), (1, 1), (1, 2), (1, 3), (2, 0), (2, 1), (2, 2), (2, 3)]

Test nesting with the inner expression dependent on the outer

    >>> list(sorted({(i,j) for i in range(4) for j in range(i)}))
    [(1, 0), (2, 0), (2, 1), (3, 0), (3, 1), (3, 2)]

Make sure the induction variable is not exposed

    >>> i = 20
    >>> sum({i*i for i in range(100)})
    328350

    >>> i
    20

Verify that syntax error's are raised for setcomps used as lvalues

    >>> {y for y in (1,2)} = 10          # doctest: +IGNORE_EXCEPTION_DETAIL
    Traceback (most recent call last):
       ...
    SyntaxError: ...

    >>> {y for y in (1,2)} += 10         # doctest: +IGNORE_EXCEPTION_DETAIL
    Traceback (most recent call last):
       ...
    SyntaxError: ...


Make a nested set comprehension that acts like set(range())

    >>> def srange(n):
    ...     return {i for i in range(n)}
    >>> list(sorted(srange(10)))
    [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Same again, only as a lambda expression instead of a function definition

    >>> lrange = lambda n:  {i for i in range(n)}
    >>> list(sorted(lrange(10)))
    [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Generators can call other generators:

    >>> def grange(n):
    ...     for x in {i for i in range(n)}:
    ...         yield x
    >>> list(sorted(grange(5)))
    [0, 1, 2, 3, 4]


Make sure that None is a valid return value

    >>> {None for i in range(10)}
    {None}

########### Tests for various scoping corner cases ############

Return lambdas that use the iteration variable as a default argument

    >>> items = {(lambda i=i: i) for i in range(5)}
    >>> {x() for x in items} == set(range(5))
    True

Same again, only this time as a closure variable

    >>> items = {(lambda: i) for i in range(5)}
    >>> {x() for x in items}
    {4}

Another way to test that the iteration variable is local to the list comp

    >>> items = {(lambda: i) for i in range(5)}
    >>> i = 20
    >>> {x() for x in items}
    {4}

And confirm that a closure can jump over the list comp scope

    >>> items = {(lambda: y) for i in range(5)}
    >>> y = 2
    >>> {x() for x in items}
    {2}

We also repeat each of the above scoping tests inside a function

    >>> def test_func():
    ...     items = {(lambda i=i: i) for i in range(5)}
    ...     return {x() for x in items}
    >>> test_func() == set(range(5))
    True

    >>> def test_func():
    ...     items = {(lambda: i) for i in range(5)}
    ...     return {x() for x in items}
    >>> test_func()
    {4}

    >>> def test_func():
    ...     items = {(lambda: i) for i in range(5)}
    ...     i = 20
    ...     return {x() for x in items}
    >>> test_func()
    {4}

    >>> def test_func():
    ...     items = {(lambda: y) for i in range(5)}
    ...     y = 2
    ...     return {x() for x in items}
    >>> test_func()
    {2}

"""


__test__ = {'doctests' : doctests}

def test_main(verbose=None):
    import sys
    from test import support
    from test import test_setcomps
    support.run_doctest(test_setcomps, verbose)

    # verify reference counting
    if verbose and hasattr(sys, "gettotalrefcount"):
        import gc
        counts = [None] * 5
        for i in range(len(counts)):
            support.run_doctest(test_setcomps, verbose)
            gc.collect()
            counts[i] = sys.gettotalrefcount()
        print(counts)

if __name__ == "__main__":
    test_main(verbose=True)