Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commita04c14f

Browse files
committed
restrict keyword placeholders
1 parentd217592 commita04c14f

File tree

3 files changed

+27
-15
lines changed

3 files changed

+27
-15
lines changed

‎Lib/functools.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,10 @@ class partial:
342342
def__new__(cls,func,/,*args,**keywords):
343343
ifnotcallable(func):
344344
raiseTypeError("the first argument must be callable")
345+
ifkeywords:
346+
forvinkeywords.values():
347+
ifvisPlaceholder:
348+
raiseTypeError("keyword Placeholders are not allowed")
345349
ifisinstance(func,partial):
346350
pto_phcount=func._phcount
347351
tot_args=func.args

‎Lib/test/test_functools.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,12 @@ def test_placeholders_optimization(self):
254254
self.assertEqual(p2.args, (PH,0))
255255
self.assertEqual(p2(1), ((1,0), {}))
256256

257+
deftest_placeholders_kw_restriction(self):
258+
PH=self.module.Placeholder
259+
exc_string='keyword Placeholders are not allowed'
260+
withself.assertRaisesRegex(TypeError,exc_string):
261+
self.partial(capture,a=PH)
262+
257263
deftest_construct_placeholder_singleton(self):
258264
PH=self.module.Placeholder
259265
tp=type(PH)

‎Modules/_functoolsmodule.c

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -290,29 +290,31 @@ partial_new(PyTypeObject *type, PyObject *args, PyObject *kw)
290290

291291
/* process keywords */
292292
if (pto_kw==NULL||PyDict_GET_SIZE(pto_kw)==0) {
293-
if (kw==NULL) {
294-
pto->kw=PyDict_New();
295-
}
296-
elseif (Py_REFCNT(kw)==1) {
297-
pto->kw=Py_NewRef(kw);
298-
}
299-
else {
300-
pto->kw=PyDict_Copy(kw);
301-
}
293+
pto->kw=PyDict_New();
302294
}
303295
else {
304296
pto->kw=PyDict_Copy(pto_kw);
305-
if (kw!=NULL&&pto->kw!=NULL) {
306-
if (PyDict_Merge(pto->kw,kw,1)!=0) {
297+
if (pto->kw==NULL) {
298+
Py_DECREF(pto);
299+
returnNULL;
300+
}
301+
}
302+
if (kw!=NULL&&pto->kw!=NULL) {
303+
PyObject*key,*val;
304+
Py_ssize_tpos=0;
305+
while (PyDict_Next(kw,&pos,&key,&val)) {
306+
if (val==phold) {
307+
Py_DECREF(pto);
308+
PyErr_SetString(PyExc_TypeError,
309+
"keyword Placeholders are not allowed");
310+
returnNULL;
311+
}
312+
if (PyDict_SetItem(pto->kw,key,val)) {
307313
Py_DECREF(pto);
308314
returnNULL;
309315
}
310316
}
311317
}
312-
if (pto->kw==NULL) {
313-
Py_DECREF(pto);
314-
returnNULL;
315-
}
316318

317319
partial_setvectorcall(pto);
318320
return (PyObject*)pto;

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp