Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor getenvironment() in _winapi.c #80568

Closed
serhiy-storchaka opened this issue Mar 21, 2019 · 2 comments
Closed

Refactor getenvironment() in _winapi.c #80568

serhiy-storchaka opened this issue Mar 21, 2019 · 2 comments
Labels
3.8 only security fixes extension-modules C modules in the Modules dir OS-windows

Comments

@serhiy-storchaka
Copy link
Member

BPO 36387
Nosy @malemburg, @pfmoore, @tjguk, @zware, @serhiy-storchaka, @zooba
PRs
  • bpo-36387: Refactor getenvironment() in _winapi.c. #12482
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2019-03-28.14:32:41.753>
    created_at = <Date 2019-03-21.06:32:57.872>
    labels = ['extension-modules', '3.8', 'OS-windows']
    title = 'Refactor getenvironment() in _winapi.c'
    updated_at = <Date 2019-03-28.14:32:41.752>
    user = 'https://github.com/serhiy-storchaka'

    bugs.python.org fields:

    activity = <Date 2019-03-28.14:32:41.752>
    actor = 'serhiy.storchaka'
    assignee = 'none'
    closed = True
    closed_date = <Date 2019-03-28.14:32:41.753>
    closer = 'serhiy.storchaka'
    components = ['Extension Modules', 'Windows']
    creation = <Date 2019-03-21.06:32:57.872>
    creator = 'serhiy.storchaka'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 36387
    keywords = ['patch']
    message_count = 2.0
    messages = ['338527', '339046']
    nosy_count = 6.0
    nosy_names = ['lemburg', 'paul.moore', 'tim.golden', 'zach.ware', 'serhiy.storchaka', 'steve.dower']
    pr_nums = ['12482']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = None
    url = 'https://bugs.python.org/issue36387'
    versions = ['Python 3.8']

    @serhiy-storchaka
    Copy link
    Member Author

    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 bpo-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.

    @serhiy-storchaka serhiy-storchaka added 3.9 only security fixes extension-modules C modules in the Modules dir 3.8 only security fixes OS-windows and removed 3.9 only security fixes labels Mar 21, 2019
    @serhiy-storchaka
    Copy link
    Member Author

    New changeset 8abd7c7 by Serhiy Storchaka in branch 'master':
    bpo-36387: Refactor getenvironment() in _winapi.c. (GH-12482)
    8abd7c7

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.8 only security fixes extension-modules C modules in the Modules dir OS-windows
    Projects
    None yet
    Development

    No branches or pull requests

    1 participant