pymkstemp -- a python wrapper for mkstemp(3)
Overview
pymkstemp is a Python extension module written in C. It provides access to the C library function mkstemp, which is used for securely creating temporary files and avoiding race conditions or symlink attacks. If you have a python program that uses temporary files, and you are currently using the standard Python library module "tempfile", you might consider using this instead.
Note: in Python 2.0 and later, this module is less necessary, as tempfile.TemporaryFile() performs the same function as mkstemp.tmpfile() does here. You may still want the functionality of mkstemp() (e.g., for creating backup files with unique names), or you may want to install this module under both Python 1.5 and 2.0 on the same machine for portability purposes, which you can't do with Python 2.0's tempfile library.
API
This module is more-or-less Unix specific. I haven't tested it on any non-GNU systems.
The module defines two user-callable functions, and one exception. Please note that while I am distributing this package as "pymkstemp", the module name is "mkstemp", so you would load it with "import mkstemp".
- mkstemp(template)
-
Create a uniquely named tempfile and return the open file object. The "template" argument is required, and should be a string containing the substring "XXXXXX". The filename created will match the template with "XXXXXX" replaced so as to form a unique filename. For example, mkstemp("/tmp/fooXXXXXX") might create "/tmp/fooa4l83bk" and return an opened file object for that file.
- tmpfile()
Return a file object that can be used for temporary storage. It is created securely using mkstemp and is unlinked immediately after it is created so that it is not accessible for other processes.
- error
Raised if there is there is any problem creating the temporary file.
Downloads
| Format | File |
|---|---|
| Source tarball | pymkstemp-0.9.tar.gz |
| Source rpm | pymkstemp-0.9-1.src.rpm |
| Binary tarball (Linux x86, Python 1.5.2, glibc 2.1) | pymkstemp-0.9.linux-i386.tar.gz |
| Binary RPM (Redhat 6.2, Python 1.5) | pymkstemp-0.9-1.i386.rpm |
Building from Source
You will need the Python Distutils installed (or know something about compiling Python extensions) to build and install from source (including from the source RPM). If you do have the Distutils, it's obvious what to do -- just run setup.py and read the help.
The Distutils are built-in in Python 2.0; if you're running Python 1.5, you can either use one of the binary distributions (if they match your platform), install the distutils yourself (follow the above link), or read the Python documentation on extending and embedding Python.