From 4d966a1cf9acb21c9f71f2a630da11e4ac848502 Mon Sep 17 00:00:00 2001 From: Yury Selivanov Date: Tue, 1 May 2018 11:15:20 -0400 Subject: Reject keyword and positional arguments in __new__. Fixes #2. --- immutables/__init__.py | 2 +- immutables/_map.c | 8 ++++++++ tests/test_map.py | 7 +++++++ 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/immutables/__init__.py b/immutables/__init__.py index f6f93c7..a85dc6e 100644 --- a/immutables/__init__.py +++ b/immutables/__init__.py @@ -8,4 +8,4 @@ else: __all__ = 'Map', -__version__ = '0.4' +__version__ = '0.5' diff --git a/immutables/_map.c b/immutables/_map.c index be82cd9..d0a5642 100644 --- a/immutables/_map.c +++ b/immutables/_map.c @@ -2679,6 +2679,14 @@ map_dump(MapObject *self); static PyObject * map_tp_new(PyTypeObject *type, PyObject *args, PyObject *kwds) { + if (kwds != NULL && PyDict_Size(kwds) != 0) { + PyErr_SetString(PyExc_TypeError, "__new__ takes no keyword arguments"); + return NULL; + } + if (args != NULL && PyTuple_Size(args) != 0) { + PyErr_SetString(PyExc_TypeError, "__new__ takes no positional arguments"); + return NULL; + } return (PyObject*)map_new(); } diff --git a/tests/test_map.py b/tests/test_map.py index 660f742..fe2a131 100644 --- a/tests/test_map.py +++ b/tests/test_map.py @@ -93,6 +93,13 @@ class BaseMapTest: Map = None + def test_init_no_args(self): + with self.assertRaisesRegex(TypeError, 'positional argument'): + self.Map(dict(a=1)) + + with self.assertRaisesRegex(TypeError, 'keyword argument'): + self.Map(a=1) + def test_hashkey_helper_1(self): k1 = HashKey(10, 'aaa') k2 = HashKey(10, 'bbb') -- cgit v1.2.3