Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c401881

Browse files
committed
Closes #27982: Allow keyword arguments to winsound functions
1 parent ef537db commit c401881

5 files changed

Lines changed: 51 additions & 27 deletions

File tree

Doc/whatsnew/3.6.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -692,6 +692,14 @@ Added the 64-bit integer type :data:`REG_QWORD <winreg.REG_QWORD>`.
692692
(Contributed by Clement Rouault in :issue:`23026`.)
693693

694694

695+
winsound
696+
--------
697+
698+
Allowed keyword arguments to be passed to :func:`Beep <winsound.Beep>`,
699+
:func:`MessageBeep <winsound.MessageBeep>`, and :func:`PlaySound
700+
<winsound.PlaySound>` (:issue:`27982`).
701+
702+
695703
zipfile
696704
-------
697705

Lib/test/test_winsound.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ def test_increasingfrequency(self):
5151
for i in range(100, 2000, 100):
5252
safe_Beep(i, 75)
5353

54+
def test_keyword_args(self):
55+
safe_Beep(duration=75, frequency=2000)
56+
57+
5458
class MessageBeepTest(unittest.TestCase):
5559

5660
def tearDown(self):
@@ -76,6 +80,9 @@ def test_hand(self):
7680
def test_question(self):
7781
safe_MessageBeep(winsound.MB_ICONQUESTION)
7882

83+
def test_keyword_args(self):
84+
safe_MessageBeep(type=winsound.MB_OK)
85+
7986

8087
class PlaySoundTest(unittest.TestCase):
8188

@@ -92,6 +99,9 @@ def test_errors(self):
9299
winsound.SND_MEMORY)
93100
self.assertRaises(TypeError, winsound.PlaySound, 1, 0)
94101

102+
def test_keyword_args(self):
103+
safe_PlaySound(flags=winsound.SND_ALIAS, sound="SystemExit")
104+
95105
def test_snd_memory(self):
96106
with open(support.findfile('pluck-pcm8.wav',
97107
subdir='audiodata'), 'rb') as f:

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,9 @@ Build
215215
Windows
216216
-------
217217

218+
- Issue #27982: The functions of the winsound module now accept keyword
219+
arguments.
220+
218221
- Issue #20366: Build full text search support into SQLite on Windows.
219222

220223
- Issue #27756: Adds new icons for Python files and processes on Windows.

PC/clinic/winsound.c.h

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ preserve
33
[clinic start generated code]*/
44

55
PyDoc_STRVAR(winsound_PlaySound__doc__,
6-
"PlaySound($module, sound, flags, /)\n"
6+
"PlaySound($module, /, sound, flags)\n"
77
"--\n"
88
"\n"
99
"A wrapper around the Windows PlaySound API.\n"
@@ -14,19 +14,21 @@ PyDoc_STRVAR(winsound_PlaySound__doc__,
1414
" Flag values, ored together. See module documentation.");
1515

1616
#define WINSOUND_PLAYSOUND_METHODDEF \
17-
{"PlaySound", (PyCFunction)winsound_PlaySound, METH_VARARGS, winsound_PlaySound__doc__},
17+
{"PlaySound", (PyCFunction)winsound_PlaySound, METH_VARARGS|METH_KEYWORDS, winsound_PlaySound__doc__},
1818

1919
static PyObject *
2020
winsound_PlaySound_impl(PyObject *module, PyObject *sound, int flags);
2121

2222
static PyObject *
23-
winsound_PlaySound(PyObject *module, PyObject *args)
23+
winsound_PlaySound(PyObject *module, PyObject *args, PyObject *kwargs)
2424
{
2525
PyObject *return_value = NULL;
26+
static const char * const _keywords[] = {"sound", "flags", NULL};
27+
static _PyArg_Parser _parser = {"Oi:PlaySound", _keywords, 0};
2628
PyObject *sound;
2729
int flags;
2830

29-
if (!PyArg_ParseTuple(args, "Oi:PlaySound",
31+
if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser,
3032
&sound, &flags)) {
3133
goto exit;
3234
}
@@ -37,7 +39,7 @@ winsound_PlaySound(PyObject *module, PyObject *args)
3739
}
3840

3941
PyDoc_STRVAR(winsound_Beep__doc__,
40-
"Beep($module, frequency, duration, /)\n"
42+
"Beep($module, /, frequency, duration)\n"
4143
"--\n"
4244
"\n"
4345
"A wrapper around the Windows Beep API.\n"
@@ -49,19 +51,21 @@ PyDoc_STRVAR(winsound_Beep__doc__,
4951
" How long the sound should play, in milliseconds.");
5052

5153
#define WINSOUND_BEEP_METHODDEF \
52-
{"Beep", (PyCFunction)winsound_Beep, METH_VARARGS, winsound_Beep__doc__},
54+
{"Beep", (PyCFunction)winsound_Beep, METH_VARARGS|METH_KEYWORDS, winsound_Beep__doc__},
5355

5456
static PyObject *
5557
winsound_Beep_impl(PyObject *module, int frequency, int duration);
5658

5759
static PyObject *
58-
winsound_Beep(PyObject *module, PyObject *args)
60+
winsound_Beep(PyObject *module, PyObject *args, PyObject *kwargs)
5961
{
6062
PyObject *return_value = NULL;
63+
static const char * const _keywords[] = {"frequency", "duration", NULL};
64+
static _PyArg_Parser _parser = {"ii:Beep", _keywords, 0};
6165
int frequency;
6266
int duration;
6367

64-
if (!PyArg_ParseTuple(args, "ii:Beep",
68+
if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser,
6569
&frequency, &duration)) {
6670
goto exit;
6771
}
@@ -72,32 +76,34 @@ winsound_Beep(PyObject *module, PyObject *args)
7276
}
7377

7478
PyDoc_STRVAR(winsound_MessageBeep__doc__,
75-
"MessageBeep($module, x=MB_OK, /)\n"
79+
"MessageBeep($module, /, type=MB_OK)\n"
7680
"--\n"
7781
"\n"
7882
"Call Windows MessageBeep(x).\n"
7983
"\n"
8084
"x defaults to MB_OK.");
8185

8286
#define WINSOUND_MESSAGEBEEP_METHODDEF \
83-
{"MessageBeep", (PyCFunction)winsound_MessageBeep, METH_VARARGS, winsound_MessageBeep__doc__},
87+
{"MessageBeep", (PyCFunction)winsound_MessageBeep, METH_VARARGS|METH_KEYWORDS, winsound_MessageBeep__doc__},
8488

8589
static PyObject *
86-
winsound_MessageBeep_impl(PyObject *module, int x);
90+
winsound_MessageBeep_impl(PyObject *module, int type);
8791

8892
static PyObject *
89-
winsound_MessageBeep(PyObject *module, PyObject *args)
93+
winsound_MessageBeep(PyObject *module, PyObject *args, PyObject *kwargs)
9094
{
9195
PyObject *return_value = NULL;
92-
int x = MB_OK;
96+
static const char * const _keywords[] = {"type", NULL};
97+
static _PyArg_Parser _parser = {"|i:MessageBeep", _keywords, 0};
98+
int type = MB_OK;
9399

94-
if (!PyArg_ParseTuple(args, "|i:MessageBeep",
95-
&x)) {
100+
if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser,
101+
&type)) {
96102
goto exit;
97103
}
98-
return_value = winsound_MessageBeep_impl(module, x);
104+
return_value = winsound_MessageBeep_impl(module, type);
99105

100106
exit:
101107
return return_value;
102108
}
103-
/*[clinic end generated code: output=b999334e2e444ad2 input=a9049054013a1b77]*/
109+
/*[clinic end generated code: output=40b3d3ef2faefb15 input=a9049054013a1b77]*/

PC/winsound.c

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ PyDoc_STRVAR(sound_module_doc,
5252
"SND_NOWAIT - Return immediately if the sound driver is busy\n" // Without any errors
5353
"\n"
5454
"Beep(frequency, duration) - Make a beep through the PC speaker.\n"
55-
"MessageBeep(x) - Call Windows MessageBeep.");
55+
"MessageBeep(type) - Call Windows MessageBeep.");
5656

5757
/*[clinic input]
5858
module winsound
@@ -68,14 +68,13 @@ winsound.PlaySound
6868
The sound to play; a filename, data, or None.
6969
flags: int
7070
Flag values, ored together. See module documentation.
71-
/
7271
7372
A wrapper around the Windows PlaySound API.
7473
[clinic start generated code]*/
7574

7675
static PyObject *
7776
winsound_PlaySound_impl(PyObject *module, PyObject *sound, int flags)
78-
/*[clinic end generated code: output=49a0fd16a372ebeb input=7bdf637f10201d37]*/
77+
/*[clinic end generated code: output=49a0fd16a372ebeb input=c63e1f2d848da2f2]*/
7978
{
8079
int ok;
8180
wchar_t *wsound;
@@ -132,14 +131,13 @@ winsound.Beep
132131
Must be in the range 37 through 32,767.
133132
duration: int
134133
How long the sound should play, in milliseconds.
135-
/
136134
137135
A wrapper around the Windows Beep API.
138136
[clinic start generated code]*/
139137

140138
static PyObject *
141139
winsound_Beep_impl(PyObject *module, int frequency, int duration)
142-
/*[clinic end generated code: output=f32382e52ee9b2fb input=628a99d2ddf73798]*/
140+
/*[clinic end generated code: output=f32382e52ee9b2fb input=40e360cfa00a5cf0]*/
143141
{
144142
BOOL ok;
145143

@@ -163,22 +161,21 @@ winsound_Beep_impl(PyObject *module, int frequency, int duration)
163161
/*[clinic input]
164162
winsound.MessageBeep
165163
166-
x: int(c_default="MB_OK") = MB_OK
167-
/
164+
type: int(c_default="MB_OK") = MB_OK
168165
169166
Call Windows MessageBeep(x).
170167
171168
x defaults to MB_OK.
172169
[clinic start generated code]*/
173170

174171
static PyObject *
175-
winsound_MessageBeep_impl(PyObject *module, int x)
176-
/*[clinic end generated code: output=1ad89e4d8d30a957 input=a776c8a85c9853f6]*/
172+
winsound_MessageBeep_impl(PyObject *module, int type)
173+
/*[clinic end generated code: output=120875455121121f input=db185f741ae21401]*/
177174
{
178175
BOOL ok;
179176

180177
Py_BEGIN_ALLOW_THREADS
181-
ok = MessageBeep(x);
178+
ok = MessageBeep(type);
182179
Py_END_ALLOW_THREADS
183180

184181
if (!ok) {

0 commit comments

Comments
 (0)