Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ https://mhammond.github.io/pywin32_installers.html.

Coming in build 306, as yet unreleased
--------------------------------------
* Add GetSystemPowerStatus (#2010, @CristiFati)

* Add CascadeWindows (#1999, @CristiFati)

* Fix for service registration code updated in build 305 (#1985)
Expand Down
27 changes: 27 additions & 0 deletions win32/src/win32apimodule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5829,6 +5829,31 @@ PyObject *PyGetPwrCapabilities(PyObject *self, PyObject *args)
"DefaultLowLatencyWake", PyLong_FromLong(spc.DefaultLowLatencyWake));
}


// @pymethod dict|win32api|GetSystemPowerStatus|Retrieves the power status of the system
// @pyseeapi GetSystemPowerStatus
// @comm Requires Winxp or later.
// @rdesc Returns a dict representing a SYSTEM_POWER_STATUS struct
PyObject* PyGetSystemPowerStatus(PyObject *self, PyObject *args)
{
SYSTEM_POWER_STATUS sps;
BOOL res = FALSE;
Py_BEGIN_ALLOW_THREADS;
res = GetSystemPowerStatus(&sps);
Py_END_ALLOW_THREADS;
if (!res)
return PyWin_SetAPIError("GetSystemPowerStatus");
return Py_BuildValue(
"{s:h, s:h, s:h, s:B, s:L, s:L}",
"ACLineStatus", (sps.ACLineStatus == (BYTE)-1) ? -1 : sps.ACLineStatus,
"BatteryFlag", (sps.BatteryFlag == (BYTE)-1) ? -1 : sps.BatteryFlag,
"BatteryLifePercent", (sps.BatteryLifePercent == (BYTE)-1) ? -1 : sps.BatteryLifePercent,
"SystemStatusFlag", sps.SystemStatusFlag,
"BatteryLifeTime", (sps.BatteryLifeTime == (DWORD)-1) ? -1 : (long long)sps.BatteryLifeTime,
"BatteryFullLifeTime", (sps.BatteryFullLifeTime == (DWORD)-1) ? -1 : (long long)sps.BatteryFullLifeTime);
}


/* List of functions exported by this module */
// @module win32api|A module, encapsulating the Windows Win32 API.
static struct PyMethodDef win32api_functions[] = {
Expand Down Expand Up @@ -5996,6 +6021,8 @@ static struct PyMethodDef win32api_functions[] = {
{"GetNativeSystemInfo", PyGetNativeSystemInfo,
1}, // @pymeth GetNativeSystemInfo|Retrieves information about the current system for a Wow64 process.
{"GetSystemMetrics", PyGetSystemMetrics, 1}, // @pymeth GetSystemMetrics|Returns the specified system metrics.
{"GetSystemPowerStatus", PyGetSystemPowerStatus,
METH_NOARGS}, // @pymeth GetSystemPowerStatus|Retrieves the power status of the system
{"GetSystemTime", PyGetSystemTime, 1}, // @pymeth GetSystemTime|Returns the current system time.
{"GetTempFileName", PyGetTempFileName, 1}, // @pymeth GetTempFileName|Creates a temporary file.
{"GetTempPath", PyGetTempPath, 1}, // @pymeth GetTempPath|Returns the path designated as holding temporary files.
Expand Down
14 changes: 14 additions & 0 deletions win32/test/test_win32api.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,20 @@ def testVkKeyScanEx(self):
# hopefully ' ' doesn't depend on the locale!
self.assertEqual(win32api.VkKeyScanEx(" ", 0), 32)

def testGetSystemPowerStatus(self):
# Dummy
sps = win32api.GetSystemPowerStatus()
self.assertIsInstance(sps, dict)
test_keys = (
"ACLineStatus",
"BatteryFlag",
"BatteryLifePercent",
"SystemStatusFlag",
"BatteryLifeTime",
"BatteryFullLifeTime",
)
self.assertEqual(set(test_keys), set(sps.keys()))


if __name__ == "__main__":
unittest.main()