Skip to content

Commit 4d966a1

Browse files
committed
Reject keyword and positional arguments in __new__. Fixes #2.
1 parent 771954f commit 4d966a1

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed

immutables/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@
88

99

1010
__all__ = 'Map',
11-
__version__ = '0.4'
11+
__version__ = '0.5'

immutables/_map.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2679,6 +2679,14 @@ map_dump(MapObject *self);
26792679
static PyObject *
26802680
map_tp_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
26812681
{
2682+
if (kwds != NULL && PyDict_Size(kwds) != 0) {
2683+
PyErr_SetString(PyExc_TypeError, "__new__ takes no keyword arguments");
2684+
return NULL;
2685+
}
2686+
if (args != NULL && PyTuple_Size(args) != 0) {
2687+
PyErr_SetString(PyExc_TypeError, "__new__ takes no positional arguments");
2688+
return NULL;
2689+
}
26822690
return (PyObject*)map_new();
26832691
}
26842692

tests/test_map.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,13 @@ class BaseMapTest:
9393

9494
Map = None
9595

96+
def test_init_no_args(self):
97+
with self.assertRaisesRegex(TypeError, 'positional argument'):
98+
self.Map(dict(a=1))
99+
100+
with self.assertRaisesRegex(TypeError, 'keyword argument'):
101+
self.Map(a=1)
102+
96103
def test_hashkey_helper_1(self):
97104
k1 = HashKey(10, 'aaa')
98105
k2 = HashKey(10, 'bbb')

0 commit comments

Comments
 (0)