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

Implement GetTempPath() #135

Merged
merged 4 commits into from
Oct 29, 2017
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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`

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)