Skip to content

Commit f687935

Browse files
authored
Allow pydict accept Pair args (#616)
* Allow pydict accept Pair args * ensure pydict(::Pair...) method has at least one argument * simplify test for pydict(::Pair...) --------- Co-authored-by: Christopher Doris <github.com/cjdoris>
1 parent eb771e1 commit f687935

File tree

2 files changed

+31
-58
lines changed

2 files changed

+31
-58
lines changed

src/Core/builtins.jl

Lines changed: 30 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -75,33 +75,28 @@ end
7575
7676
Equivalent to `setattr(x, k, v)` or `x.k = v` in Python.
7777
"""
78-
pysetattr(x, k, v) = (
79-
errcheck(@autopy x k v C.PyObject_SetAttr(x_, k_, v_)); nothing
80-
)
78+
pysetattr(x, k, v) = (errcheck(@autopy x k v C.PyObject_SetAttr(x_, k_, v_)); nothing)
8179

8280
"""
8381
pydelattr(x, k)
8482
8583
Equivalent to `delattr(x, k)` or `del x.k` in Python.
8684
"""
87-
pydelattr(x, k) =
88-
(errcheck(@autopy x k C.PyObject_SetAttr(x_, k_, C.PyNULL)); nothing)
85+
pydelattr(x, k) = (errcheck(@autopy x k C.PyObject_SetAttr(x_, k_, C.PyNULL)); nothing)
8986

9087
"""
9188
pyissubclass(s, t)
9289
9390
Test if `s` is a subclass of `t`. Equivalent to `issubclass(s, t)` in Python.
9491
"""
95-
pyissubclass(s, t) =
96-
errcheck(@autopy s t C.PyObject_IsSubclass(s_, t_)) == 1
92+
pyissubclass(s, t) = errcheck(@autopy s t C.PyObject_IsSubclass(s_, t_)) == 1
9793

9894
"""
9995
pyisinstance(x, t)
10096
10197
Test if `x` is of type `t`. Equivalent to `isinstance(x, t)` in Python.
10298
"""
103-
pyisinstance(x, t) =
104-
errcheck(@autopy x t C.PyObject_IsInstance(x_, t_)) == 1
99+
pyisinstance(x, t) = errcheck(@autopy x t C.PyObject_IsInstance(x_, t_)) == 1
105100

106101
"""
107102
pyhash(x)
@@ -179,17 +174,14 @@ end
179174
180175
Equivalent to `setitem(x, k, v)` or `x[k] = v` in Python.
181176
"""
182-
pysetitem(x, k, v) = (
183-
errcheck(@autopy x k v C.PyObject_SetItem(x_, k_, v_)); nothing
184-
)
177+
pysetitem(x, k, v) = (errcheck(@autopy x k v C.PyObject_SetItem(x_, k_, v_)); nothing)
185178

186179
"""
187180
pydelitem(x, k)
188181
189182
Equivalent to `delitem(x, k)` or `del x[k]` in Python.
190183
"""
191-
pydelitem(x, k) =
192-
(errcheck(@autopy x k C.PyObject_DelItem(x_, k_)); nothing)
184+
pydelitem(x, k) = (errcheck(@autopy x k C.PyObject_DelItem(x_, k_)); nothing)
193185

194186
"""
195187
pydir(x)
@@ -199,13 +191,9 @@ Equivalent to `dir(x)` in Python.
199191
pydir(x) = pynew(errcheck(@autopy x C.PyObject_Dir(x_)))
200192

201193
pycallargs(f) = pynew(errcheck(@autopy f C.PyObject_CallObject(f_, C.PyNULL)))
202-
pycallargs(f, args) =
203-
pynew(errcheck(@autopy f args C.PyObject_CallObject(f_, args_)))
204-
pycallargs(f, args, kwargs) = pynew(
205-
errcheck(
206-
@autopy f args kwargs C.PyObject_Call(f_, args_, kwargs_)
207-
),
208-
)
194+
pycallargs(f, args) = pynew(errcheck(@autopy f args C.PyObject_CallObject(f_, args_)))
195+
pycallargs(f, args, kwargs) =
196+
pynew(errcheck(@autopy f args kwargs C.PyObject_Call(f_, args_, kwargs_)))
209197

210198
"""
211199
pycall(f, args...; kwargs...)
@@ -235,53 +223,47 @@ pycall(f, args...; kwargs...) =
235223
236224
Equivalent to `x == y` in Python. The second form converts to `Bool`.
237225
"""
238-
pyeq(x, y) =
239-
pynew(errcheck(@autopy x y C.PyObject_RichCompare(x_, y_, C.Py_EQ)))
226+
pyeq(x, y) = pynew(errcheck(@autopy x y C.PyObject_RichCompare(x_, y_, C.Py_EQ)))
240227

241228
"""
242229
pyne(x, y)
243230
pyne(Bool, x, y)
244231
245232
Equivalent to `x != y` in Python. The second form converts to `Bool`.
246233
"""
247-
pyne(x, y) =
248-
pynew(errcheck(@autopy x y C.PyObject_RichCompare(x_, y_, C.Py_NE)))
234+
pyne(x, y) = pynew(errcheck(@autopy x y C.PyObject_RichCompare(x_, y_, C.Py_NE)))
249235

250236
"""
251237
pyle(x, y)
252238
pyle(Bool, x, y)
253239
254240
Equivalent to `x <= y` in Python. The second form converts to `Bool`.
255241
"""
256-
pyle(x, y) =
257-
pynew(errcheck(@autopy x y C.PyObject_RichCompare(x_, y_, C.Py_LE)))
242+
pyle(x, y) = pynew(errcheck(@autopy x y C.PyObject_RichCompare(x_, y_, C.Py_LE)))
258243

259244
"""
260245
pylt(x, y)
261246
pylt(Bool, x, y)
262247
263248
Equivalent to `x < y` in Python. The second form converts to `Bool`.
264249
"""
265-
pylt(x, y) =
266-
pynew(errcheck(@autopy x y C.PyObject_RichCompare(x_, y_, C.Py_LT)))
250+
pylt(x, y) = pynew(errcheck(@autopy x y C.PyObject_RichCompare(x_, y_, C.Py_LT)))
267251

268252
"""
269253
pyge(x, y)
270254
pyge(Bool, x, y)
271255
272256
Equivalent to `x >= y` in Python. The second form converts to `Bool`.
273257
"""
274-
pyge(x, y) =
275-
pynew(errcheck(@autopy x y C.PyObject_RichCompare(x_, y_, C.Py_GE)))
258+
pyge(x, y) = pynew(errcheck(@autopy x y C.PyObject_RichCompare(x_, y_, C.Py_GE)))
276259

277260
"""
278261
pygt(x, y)
279262
pygt(Bool, x, y)
280263
281264
Equivalent to `x > y` in Python. The second form converts to `Bool`.
282265
"""
283-
pygt(x, y) =
284-
pynew(errcheck(@autopy x y C.PyObject_RichCompare(x_, y_, C.Py_GT)))
266+
pygt(x, y) = pynew(errcheck(@autopy x y C.PyObject_RichCompare(x_, y_, C.Py_GT)))
285267
pyeq(::Type{Bool}, x, y) =
286268
errcheck(@autopy x y C.PyObject_RichCompareBool(x_, y_, C.Py_EQ)) == 1
287269
pyne(::Type{Bool}, x, y) =
@@ -369,15 +351,13 @@ pymul(x, y) = pynew(errcheck(@autopy x y C.PyNumber_Multiply(x_, y_)))
369351
370352
Equivalent to `x @ y` in Python.
371353
"""
372-
pymatmul(x, y) =
373-
pynew(errcheck(@autopy x y C.PyNumber_MatrixMultiply(x_, y_)))
354+
pymatmul(x, y) = pynew(errcheck(@autopy x y C.PyNumber_MatrixMultiply(x_, y_)))
374355
"""
375356
pyfloordiv(x, y)
376357
377358
Equivalent to `x // y` in Python.
378359
"""
379-
pyfloordiv(x, y) =
380-
pynew(errcheck(@autopy x y C.PyNumber_FloorDivide(x_, y_)))
360+
pyfloordiv(x, y) = pynew(errcheck(@autopy x y C.PyNumber_FloorDivide(x_, y_)))
381361
"""
382362
pytruediv(x, y)
383363
@@ -439,57 +419,49 @@ pyiadd(x, y) = pynew(errcheck(@autopy x y C.PyNumber_InPlaceAdd(x_, y_)))
439419
440420
In-place subtract. `x = pyisub(x, y)` is equivalent to `x -= y` in Python.
441421
"""
442-
pyisub(x, y) =
443-
pynew(errcheck(@autopy x y C.PyNumber_InPlaceSubtract(x_, y_)))
422+
pyisub(x, y) = pynew(errcheck(@autopy x y C.PyNumber_InPlaceSubtract(x_, y_)))
444423
"""
445424
pyimul(x, y)
446425
447426
In-place multiply. `x = pyimul(x, y)` is equivalent to `x *= y` in Python.
448427
"""
449-
pyimul(x, y) =
450-
pynew(errcheck(@autopy x y C.PyNumber_InPlaceMultiply(x_, y_)))
428+
pyimul(x, y) = pynew(errcheck(@autopy x y C.PyNumber_InPlaceMultiply(x_, y_)))
451429
"""
452430
pyimatmul(x, y)
453431
454432
In-place matrix multiply. `x = pyimatmul(x, y)` is equivalent to `x @= y` in Python.
455433
"""
456-
pyimatmul(x, y) =
457-
pynew(errcheck(@autopy x y C.PyNumber_InPlaceMatrixMultiply(x_, y_)))
434+
pyimatmul(x, y) = pynew(errcheck(@autopy x y C.PyNumber_InPlaceMatrixMultiply(x_, y_)))
458435
"""
459436
pyifloordiv(x, y)
460437
461438
In-place floor divide. `x = pyifloordiv(x, y)` is equivalent to `x //= y` in Python.
462439
"""
463-
pyifloordiv(x, y) =
464-
pynew(errcheck(@autopy x y C.PyNumber_InPlaceFloorDivide(x_, y_)))
440+
pyifloordiv(x, y) = pynew(errcheck(@autopy x y C.PyNumber_InPlaceFloorDivide(x_, y_)))
465441
"""
466442
pyitruediv(x, y)
467443
468444
In-place true division. `x = pyitruediv(x, y)` is equivalent to `x /= y` in Python.
469445
"""
470-
pyitruediv(x, y) =
471-
pynew(errcheck(@autopy x y C.PyNumber_InPlaceTrueDivide(x_, y_)))
446+
pyitruediv(x, y) = pynew(errcheck(@autopy x y C.PyNumber_InPlaceTrueDivide(x_, y_)))
472447
"""
473448
pyimod(x, y)
474449
475450
In-place subtraction. `x = pyimod(x, y)` is equivalent to `x %= y` in Python.
476451
"""
477-
pyimod(x, y) =
478-
pynew(errcheck(@autopy x y C.PyNumber_InPlaceRemainder(x_, y_)))
452+
pyimod(x, y) = pynew(errcheck(@autopy x y C.PyNumber_InPlaceRemainder(x_, y_)))
479453
"""
480454
pyilshift(x, y)
481455
482456
In-place left shift. `x = pyilshift(x, y)` is equivalent to `x <<= y` in Python.
483457
"""
484-
pyilshift(x, y) =
485-
pynew(errcheck(@autopy x y C.PyNumber_InPlaceLshift(x_, y_)))
458+
pyilshift(x, y) = pynew(errcheck(@autopy x y C.PyNumber_InPlaceLshift(x_, y_)))
486459
"""
487460
pyirshift(x, y)
488461
489462
In-place right shift. `x = pyirshift(x, y)` is equivalent to `x >>= y` in Python.
490463
"""
491-
pyirshift(x, y) =
492-
pynew(errcheck(@autopy x y C.PyNumber_InPlaceRshift(x_, y_)))
464+
pyirshift(x, y) = pynew(errcheck(@autopy x y C.PyNumber_InPlaceRshift(x_, y_)))
493465
"""
494466
pyiand(x, y)
495467
@@ -522,9 +494,8 @@ pypow(x, y, z = pybuiltins.None) =
522494
523495
In-place power. `x = pyipow(x, y)` is equivalent to `x **= y` in Python.
524496
"""
525-
pyipow(x, y, z = pybuiltins.None) = pynew(
526-
errcheck(@autopy x y z C.PyNumber_InPlacePower(x_, y_, z_)),
527-
)
497+
pyipow(x, y, z = pybuiltins.None) =
498+
pynew(errcheck(@autopy x y z C.PyNumber_InPlacePower(x_, y_, z_)))
528499

529500
### iter
530501

@@ -1001,8 +972,7 @@ pyfrozenset(x) = ispy(x) ? pybuiltins.frozenset(x) : pyfrozenset_fromiter(x)
1001972

1002973
### dict
1003974

1004-
pydict_setitem(x::Py, k, v) =
1005-
errcheck(@autopy k v C.PyDict_SetItem(x, k_, v_))
975+
pydict_setitem(x::Py, k, v) = errcheck(@autopy k v C.PyDict_SetItem(x, k_, v_))
1006976

1007977
function pydict_fromiter(kvs)
1008978
ans = pydict()
@@ -1023,6 +993,7 @@ end
1023993
"""
1024994
pydict(x)
1025995
pydict(; x...)
996+
pydict(x::Pair...)
1026997
1027998
Convert `x` to a Python `dict`. In the second form, the keys are strings.
1028999
@@ -1033,6 +1004,7 @@ pydict(; kwargs...) =
10331004
isempty(kwargs) ? pynew(errcheck(C.PyDict_New())) : pystrdict_fromiter(kwargs)
10341005
pydict(x) = ispy(x) ? pybuiltins.dict(x) : pydict_fromiter(x)
10351006
pydict(x::NamedTuple) = pydict(; x...)
1007+
pydict(pair::Pair, pairs::Pair...) = pydict((pair, pairs...))
10361008

10371009
### datetime
10381010

test/Core.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,7 @@ end
508508
@test pyeq(Bool, pydict(Dict("foo" => 1, "bar" => 2)), x)
509509
@test pyeq(Bool, pydict((foo = 1, bar = 2)), x)
510510
@test pyeq(Bool, pydict(x), x)
511+
@test pyeq(Bool, pydict("foo" => 1, "bar" => 2), x)
511512
end
512513

513514
@testitem "bool" begin

0 commit comments

Comments
 (0)