Skip to content

Commit

Permalink
Merge pull request #135 from opalmer/124-GetTempPath
Browse files Browse the repository at this point in the history
Implement GetTempPath()
  • Loading branch information
opalmer authored Oct 29, 2017
2 parents f2db400 + e07024b commit ae177e5
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
/*.egg-info/
/docs/build/
/.tox/
/.cache/
/.coverage
/.coverage.*
/coverage.*
Expand Down
1 change: 1 addition & 0 deletions docs/source/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Notable enhancements and changes are:
* Various improvements to the tests and build including replacement of
nosetests with pytest, transition from pep8 to pycodestyle and upgrading
tools and libraries to more modern versions.
* :issue:`124` - Implemented :func:`pywincffi.kernel32.file.GetTempPath`
* :issue:`123` - Implemented :func:`pywincffi.kernel32.event.SetEvent`

0.4.0
Expand Down
6 changes: 6 additions & 0 deletions pywincffi/core/cdefs/headers/functions.h
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,12 @@ BOOL WINAPI SetHandleInformation(
_In_ DWORD dwFlags
);

// https://msdn.microsoft.com/en-us/aa364992
DWORD WINAPI GetTempPath(
_In_ DWORD nBufferLength,
_Out_ LPTSTR lpBuffer
);

// https://msdn.microsoft.com/en-us/ms724251
BOOL WINAPI DuplicateHandle(
_In_ HANDLE hSourceProcessHandle,
Expand Down
2 changes: 1 addition & 1 deletion pywincffi/kernel32/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
# it's close to the way Windows would present them (as a single module)
from pywincffi.kernel32.file import (
ReadFile, WriteFile, FlushFileBuffers, MoveFileEx, CreateFile, LockFileEx,
UnlockFileEx)
UnlockFileEx, GetTempPath)
from pywincffi.kernel32.handle import (
CloseHandle, GetStdHandle, GetHandleInformation, SetHandleInformation,
DuplicateHandle)
Expand Down
19 changes: 19 additions & 0 deletions pywincffi/kernel32/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -399,3 +399,22 @@ def UnlockFileEx(
wintype_to_cdata(lpOverlapped)
)
error_check("UnlockFileEx", code=code, expected=NON_ZERO)


def GetTempPath():
"""
Retrieves the path of the directory designated for temporary files.
.. seealso::
https://msdn.microsoft.com/en-us/aa364992
:returns:
Returns a string containing the value produced by the underlying
C function.
"""
ffi, library = dist.load()
lpBuffer = ffi.new("TCHAR[{}]".format(library.MAX_PATH + 1))
code = library.GetTempPath(library.MAX_PATH + 1, lpBuffer)
error_check("GetTempPath", code=code, expected=NON_ZERO)
return ffi.string(lpBuffer)
12 changes: 11 additions & 1 deletion tests/test_kernel32/test_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import tempfile
import subprocess
import sys
from errno import EEXIST
from os.path import isfile

from mock import patch
Expand All @@ -15,7 +16,7 @@
from pywincffi.kernel32 import file as _file # used for mocks
from pywincffi.kernel32 import (
CreateFile, CloseHandle, MoveFileEx, WriteFile, FlushFileBuffers,
LockFileEx, UnlockFileEx, ReadFile)
LockFileEx, UnlockFileEx, ReadFile, GetTempPath)
from pywincffi.wintypes import handle_from_file


Expand Down Expand Up @@ -294,3 +295,12 @@ def test_unlock_file(self):

subprocess.check_call([
sys.executable, "-c", "open(%r, 'r').read()" % self.path])


class TestGetTempPath(TestCase):
def test_call(self):
path = GetTempPath()
try:
os.makedirs(path)
except OSError as err:
self.assertEqual(err.errno, EEXIST)

0 comments on commit ae177e5

Please sign in to comment.