[issue36387] Refactor getenvironment() in _winapi.c

Serhiy Storchaka report at bugs.python.org
Thu Mar 21 02:32:57 EDT 2019


New submission from Serhiy Storchaka <storchaka+cpython at gmail.com>:

Function getenvironment() in Modules/_winapi.c is used for converting the environment mapping to the wchar_t string for passing to CreateProcessW(). It performs the following steps:

* Allocate a Py_UCS4 buffer and copy all keys and values, converting them to Py_UCS4.
* Create a PyUnicode object from the Py_UCS4 buffer.
* Create a wchar_t buffer from the PyUnicode object (unless it has the UCS2 kind).

The proposed PR makes it performing a single steps:

* Allocate a wchar_t buffer and copy all keys and values, converting them to wchar_t.

This needs less memory allocations and smaller memory consumption. In most cases this needs also less and faster memory scans and copies.

In addition, the PR replaces PySequence_Fast C API with faster PyList C API. In the past PyMapping_Keys() and PyMapping_Values() could return a tuple in unlikely case, but now they always return a list (see issue 28280).

The current code uses the legacy Unicode C API, while the new code uses the newer (added in 3.3) wchar_t based Unicode C API, so something similar to this change should be made sooner or later.

----------
components: Extension Modules
messages: 338527
nosy: lemburg, serhiy.storchaka
priority: normal
severity: normal
status: open
title: Refactor getenvironment() in _winapi.c
versions: Python 3.9

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue36387>
_______________________________________


More information about the Python-bugs-list mailing list