This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Python C-API: __all__ Creator
Type: enhancement Stage:
Components: Interpreter Core Versions: Python 3.6
process
Status: closed Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: Devyn Johnson, skrah, vstinner
Priority: normal Keywords:

Created on 2016-01-22 13:35 by Devyn Johnson, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (4)
msg258804 - (view) Author: Devyn Johnson (Devyn Johnson) Date: 2016-01-22 13:35
When creating Python modules via th C-API, it would be wonderful if there were an easier and more efficient way of creating an "__all__" for the module. In my opinion, an API function should be made; i.e., something like PyALL("FUNC1", "FUNC2", ...)


Currently, I use something like the below code.

"""
PyObject *create_all(void);

PyObject *create_all(void) {  // Create __all__
#define _ALLSTRING "[ssssss"
#define _ENDSTRING "]"
    return Py_BuildValue(
        _ALLSTRING
#if defined(ENV64BIT) && (defined(__x86_64__) || defined(__x86_64))
        "sss"
#ifdef __BMI2__
        "ssss"
#endif
#endif
        _ENDSTRING,
        // STRING CONVERSIONS
        "lowercasestr",
        "uppercasestr",
        // FIND AND REPLACE/REMOVE
        "strreplace",
        "strreplace_once",
        "rmgravequote",
        // ASSEMBLY-RELATED COMMANDS
#if defined(ENV64BIT) && (defined(__x86_64__) || defined(__x86_64))
        "rdtsc",
        "get_vendor_id",
        "get_cpu_stepping",
#ifdef __BMI2__
        "is_fpu_aval",
        "is_avx_aval",
        "is_fma_aval",
        "is_aes_aval",
#endif
#endif
        "nop"
    );
}

// Some code excluded

MODINIT {  // Initialize module
    PyObject *m;
    m = PyModule_Create(&module);
    PyModule_AddObject(m, "__all__", create_all());
    PyModule_AddStringConstant(m, "__author__", __author__);
    PyModule_AddStringConstant(m, "__version__", __version__);
    if (m == NULL)
        return NULL;
    return m;
}
"""
msg258805 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2016-01-22 13:43
Seting __all__ is usually need to exclude some symbols from "from module import *". In Python, it's common to write public or private (name prefixed by "_") helper functions which are excluded from __all__. In C extensions, you have to explicitly expose a function. Why would you expose a function but exclude it from __all__?
msg258806 - (view) Author: Stefan Krah (skrah) * (Python committer) Date: 2016-01-22 13:47
I agree, and that's pretty much what Guido said here, too:

https://mail.python.org/pipermail/python-dev/2001-February/012591.html
msg258807 - (view) Author: Devyn Johnson (Devyn Johnson) Date: 2016-01-22 14:01
Thanks, @skrah and @haypo . I never thought of it that way. I made "__all__" in my extensions because "__all__" was used in many Python scripts. Thanks for the alternative perspective.
History
Date User Action Args
2022-04-11 14:58:26adminsetgithub: 70366
2016-01-22 14:01:59Devyn Johnsonsetstatus: open -> closed

messages: + msg258807
2016-01-22 13:47:23skrahsetnosy: + skrah
messages: + msg258806
2016-01-22 13:43:24vstinnersetnosy: + vstinner
messages: + msg258805
2016-01-22 13:35:34Devyn Johnsoncreate