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.

Title: Refactor getenvironment() in _winapi.c
Type: Stage: resolved
Components: Extension Modules, Windows Versions: Python 3.8
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: lemburg, paul.moore, serhiy.storchaka, steve.dower, tim.golden, zach.ware
Priority: normal Keywords: patch

Created on 2019-03-21 06:32 by serhiy.storchaka, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 12482 merged serhiy.storchaka, 2019-03-21 13:00
Messages (2)
msg338527 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2019-03-21 06:32
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.
msg339046 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2019-03-28 14:01
New changeset 8abd7c7e37714ce0c42f871f81e52f14c155d1bd by Serhiy Storchaka in branch 'master':
bpo-36387: Refactor getenvironment() in _winapi.c. (GH-12482)
Date User Action Args
2022-04-11 14:59:12adminsetgithub: 80568
2019-03-28 14:32:41serhiy.storchakasetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2019-03-28 14:01:39serhiy.storchakasetmessages: + msg339046
2019-03-21 13:00:29serhiy.storchakasetnosy: + paul.moore, tim.golden, zach.ware, steve.dower
components: + Windows
2019-03-21 13:00:06serhiy.storchakasetkeywords: + patch
stage: patch review
pull_requests: + pull_request12434
2019-03-21 06:40:27serhiy.storchakasetversions: + Python 3.8, - Python 3.9
2019-03-21 06:40:17serhiy.storchakalinkissue36346 dependencies
2019-03-21 06:32:57serhiy.storchakacreate