Skip to main content

A minimal, robust, and Unicode-aware process launching library using ctypes.

Project description

ctypes-unicode-proclaunch

A minimal, robust, and Unicode-aware process launching library using ctypes.

  • No:
    • Shell involvement: Arguments are not interpolated or globbed by any shell, ensuring "what you pass is what you get".
    • Python subprocess abstractions: No Popen/communicate/universal_newlines/encoding drama.
  • Yes:
    • Native system call usage for maximum control and correctness.
      • CreateProcessW (NT)
      • fork and execve (POSIX)
    • Unicode on both Python 2 and 3.
    • Fully typed.

Features

  • Precise Process Spawning: Direct system call wrappers, not wrappers around wrappers.
  • Escape-proof Argument Passing: Arguments are not interpreted or glob-expanded.
  • Full Unicode Support: Give and receive Unicode paths/arguments everywhere.
  • Explicit Environment: Can supply a Unicode environment dictionary. Does not mutate global environment variables.
  • Handles Redirection: Pass open file descriptors for stdin, stdout, stderr just like with the C API.

Installation

pip install ctypes-unicode-proclaunch

Usage

Process Launched

A Python script print_argv.py:

# coding=utf-8
from __future__ import print_function
import sys

if __name__ == '__main__':
    for i, arg in enumerate(sys.argv):
        print('sys.argv[%d]=%s' % (i, arg))

NT

# coding=utf-8
from ctypes_unicode_proclaunch import launch, wait

# No `cmd.exe`'s handling of special characters:
process_handle_1 = launch([u'python', u'print_argv.py', u'%USERNAME%'])
wait(process_handle_1)
# sys.argv[0]=print_argv.py
# sys.argv[1]=%USERNAME%

process_handle_2 = launch([u'python', u'print_argv.py', u'Hello', u'&', u'python', u'print_argv.py', u'injected'])
wait(process_handle_2)
# sys.argv[0]=print_argv.py
# sys.argv[1]=Hello
# sys.argv[2]=&
# sys.argv[3]=python
# sys.argv[4]=print_argv.py
# sys.argv[5]=injected

process_handle_3 = launch([u'python', u'print_argv.py', u'*.txt'])
wait(process_handle_3)
# sys.argv[0]=print_argv.py
# sys.argv[1]=*.txt

POSIX

# coding=utf-8
from ctypes_unicode_proclaunch import launch, wait

pid_1 = launch([u'python', u'print_argv.py', u'$HOME'])
wait(pid_1)
# sys.argv[0]=print_argv.py
# sys.argv[1]=$HOME

pid_2 = launch([u'python', u'print_argv.py', u'"test"', u'>', u'$file'])
wait(pid_2)
# sys.argv[0]=print_argv.py
# sys.argv[1]="test"
# sys.argv[2]=>
# sys.argv[3]=$file

pid_3 = launch([u'python', u'print_argv.py', u'*.txt'])
wait(pid_3)
# sys.argv[0]=print_argv.py
# sys.argv[1]=*.txt

Custom Environment and File Redirection

# coding=utf-8
from ctypes_unicode_proclaunch import launch, wait
from read_unicode_environment_variables_dictionary import read_unicode_environment_variables_dictionary

# Doesn't modify `os.environ`
unicode_environment_variables_dictionary = read_unicode_environment_variables_dictionary()
unicode_environment_variables_dictionary[u'COVERAGE_FILE'] = u'test_coverage.sqlite3'

# We pass the file descriptors of the opened files for redirection.
# Just as in the Unix C API.
# The launched processes DO NOT respect the encodings you specified if you opened the files in text mode.
# This means that you should always open files in binary mode (`'rb'`, `'wb'`, etc.) to make that explicit.
with open('test_stdin.txt', 'rb') as f_0, open('test_stdout.txt', 'wb') as f_1, open('test_stderr.txt', 'wb') as f_2:
    pid_or_process_handle = launch(
        [u'python', u'-m', u'coverage', u'run', u'test.py'],
        environment=unicode_environment_variables_dictionary,
        stdin_file_descriptor=f_0.fileno(),
        stdout_file_descriptor=f_1.fileno(),
        stderr_file_descriptor=f_2.fileno()
    )
    wait(pid_or_process_handle)

wait(...) returns an integer process status:

  • On normal exit: the exit code.
  • On POSIX signal termination: the negative signal number (matching subprocess).

Contributing

Contributions are welcome! Please submit pull requests or open issues on the GitHub repository.

License

This project is licensed under the MIT License.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ctypes_unicode_proclaunch-0.1.0a2.tar.gz (8.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ctypes_unicode_proclaunch-0.1.0a2-py2.py3-none-any.whl (8.3 kB view details)

Uploaded Python 2Python 3

File details

Details for the file ctypes_unicode_proclaunch-0.1.0a2.tar.gz.

File metadata

File hashes

Hashes for ctypes_unicode_proclaunch-0.1.0a2.tar.gz
Algorithm Hash digest
SHA256 96d85b656aef5ffb06113874ad5b19d32eb4af57922f0dff9eb562cafdbbfd67
MD5 a1a890c7ac7f6681efbbad921b468d5c
BLAKE2b-256 a14962deeaaa3ef00fc00c9c32d8691866e41571eaf120a60337936c6317243e

See more details on using hashes here.

File details

Details for the file ctypes_unicode_proclaunch-0.1.0a2-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for ctypes_unicode_proclaunch-0.1.0a2-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 6c1e12066086a6b27391b1fd26f3acfe9fa3149768498475fbac6db62a9626a4
MD5 983669d5f2881d479bff93b6712a4d9d
BLAKE2b-256 3764c0a1fde608ee0d9728949c3c31e2a12a4a286620be422b48f9fe835266d7

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page