[issue42376] Add helpers to populate modules in C

2021-11-16 Thread Erlend E. Aasland


Change by Erlend E. Aasland :


--
nosy: +erlendaasland

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue42376] Add helpers to populate modules in C

2020-11-25 Thread hai shi


Change by hai shi :


--
nosy: +shihai1991

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue42376] Add helpers to populate modules in C

2020-11-23 Thread Serhiy Storchaka


Change by Serhiy Storchaka :


--
nosy: +serhiy.storchaka

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue42376] Add helpers to populate modules in C

2020-11-16 Thread Christian Heimes


Change by Christian Heimes :


--
keywords: +patch
pull_requests: +22213
pull_request: https://github.com/python/cpython/pull/23286

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue42376] Add helpers to populate modules in C

2020-11-16 Thread Christian Heimes


New submission from Christian Heimes :

It's currently inconvenient and sometimes error-prone to initialize module 
members in C. Even stdlib modules are sometimes missing error checks or have 
reference counting issues in error path. I propose add three helpers to deal 
with common cases:

1) declaration of simple attributes (int, str, bool, float) in an array of 
structs
2) creation and addition of a new type from a type spec
3) creation and addition a new exception object


(1) Simple attribute declaration uses a NULL terminated array of 
PyModuleConst_Def. The internal definition of the type can be hidden by macros. 
Example:

   static PyModuleConst_Def example_constants[] = {
   PyModuleConst_None("none_value"),
   PyModuleConst_Long("integer", 42),
   PyModuleConst_Bool("false_value", 0),
   PyModuleConst_Bool("true_value", 1),
   PyModuleConst_String("somestring", "Hello"),
   PyModuleConst_LongMacro(EXAMPLE_INT),
   PyModuleConst_StringMacro(EXAMPLE_STRING),
   {NULL},
   }

A new function "int PyModule_AddConstants(PyObject *module, PyModuleConst_Def 
*def)" populates the module object with attributes.


(2) Type initialization

The function "PyTypeObject * PyModule_AddNewTypeFromSpec(PyObject *module, 
PyType_Spec *spec, PyObject *base)" is PyType_FromModuleAndSpec() + 
PyModule_AddType(). Additionally to PyType_FromModuleAndSpec() it also handles 
"base" argument with single type instance. The extra feature avoids 
"PyTuple_Pack(1, baseclass)" in the caller. The function adds a strong 
reference to the module object and returns a borrowed reference. The borrowed 
reference is designed module state assignment. 


(3) exception creation

The function "PyObject* PyModule_AddNewException(PyObject *module, const char 
*name, const char *doc, PyObject *base, PyObject *dict)" creates a new 
exception, calculates the attribute name for the qualified exception name, and 
adds the exception to the module. It's PyErr_NewExceptionWithDoc() + 
PyModule_AddObjectRef().


Note about (1): I first proposed to add PyModuleDef.m_constants, however we 
cannot extend the struct easily becaues it conflicts with stable ABI. Petr 
mentioned that he has plans to deal with the issue. Also see discussion 
https://discuss.python.org/t/define-module-constants-in-pymoduledef/5749

--
assignee: christian.heimes
components: C API
messages: 381144
nosy: christian.heimes
priority: normal
severity: normal
stage: patch review
status: open
title: Add helpers to populate modules in C
type: enhancement
versions: Python 3.10

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com