7
7
end
8
8
9
9
struct PyConvertRule
10
- type :: Type
11
- func :: Function
12
- priority :: PyConvertPriority
10
+ type:: Type
11
+ func:: Function
12
+ priority:: PyConvertPriority
13
13
end
14
14
15
- const PYCONVERT_RULES = Dict {String, Vector{PyConvertRule}} ()
15
+ const PYCONVERT_RULES = Dict {String,Vector{PyConvertRule}} ()
16
16
const PYCONVERT_EXTRATYPES = Py[]
17
17
18
18
"""
@@ -201,7 +201,7 @@ function _pyconvert_get_rules(pytype::Py)
201
201
# check the original MRO is preserved
202
202
omro_ = filter (t -> pyisin (t, omro), mro)
203
203
@assert length (omro) == length (omro_)
204
- @assert all (pyis (x,y) for (x,y) in zip (omro, omro_))
204
+ @assert all (pyis (x, y) for (x, y) in zip (omro, omro_))
205
205
206
206
# get the names of the types in the MRO of pytype
207
207
xmro = [String[pyconvert_typename (t)] for t in mro]
@@ -240,22 +240,23 @@ function _pyconvert_get_rules(pytype::Py)
240
240
rules = PyConvertRule[rule for tname in mro for rule in get! (Vector{PyConvertRule}, PYCONVERT_RULES, tname)]
241
241
242
242
# order the rules by priority, then by original order
243
- order = sort (axes (rules, 1 ), by = i -> (rules[i]. priority, - i), rev = true )
243
+ order = sort (axes (rules, 1 ), by= i -> (rules[i]. priority, - i), rev= true )
244
244
rules = rules[order]
245
245
246
- @debug " pyconvert" pytype mro= join (mro, " " )
246
+ @debug " pyconvert" pytype mro = join (mro, " " )
247
247
return rules
248
248
end
249
249
250
250
const PYCONVERT_PREFERRED_TYPE = Dict {Py,Type} ()
251
251
252
- pyconvert_preferred_type (pytype:: Py ) = get! (PYCONVERT_PREFERRED_TYPE, pytype) do
253
- if pyissubclass (pytype, pybuiltins. int)
254
- Union{Int,BigInt}
255
- else
256
- _pyconvert_get_rules (pytype)[1 ]. type
252
+ pyconvert_preferred_type (pytype:: Py ) =
253
+ get! (PYCONVERT_PREFERRED_TYPE, pytype) do
254
+ if pyissubclass (pytype, pybuiltins. int)
255
+ Union{Int,BigInt}
256
+ else
257
+ _pyconvert_get_rules (pytype)[1 ]. type
258
+ end
257
259
end
258
- end
259
260
260
261
function pyconvert_get_rules (type:: Type , pytype:: Py )
261
262
@nospecialize type
@@ -281,15 +282,15 @@ end
281
282
282
283
pyconvert_fix (:: Type{T} , func) where {T} = x -> func (T, x)
283
284
284
- const PYCONVERT_RULES_CACHE = Dict {Type, Dict{C.PyPtr, Vector{Function}}} ()
285
+ const PYCONVERT_RULES_CACHE = Dict {Type,Dict{C.PyPtr,Vector{Function}}} ()
285
286
286
- @generated pyconvert_rules_cache (:: Type{T} ) where {T} = get! (Dict{C. PyPtr, Vector{Function}}, PYCONVERT_RULES_CACHE, T)
287
+ @generated pyconvert_rules_cache (:: Type{T} ) where {T} = get! (Dict{C. PyPtr,Vector{Function}}, PYCONVERT_RULES_CACHE, T)
287
288
288
289
function pyconvert_rule_fast (:: Type{T} , x:: Py ) where {T}
289
290
if T isa Union
290
- a = pyconvert_rule_fast (T. a, x) :: pyconvert_returntype (T. a)
291
+ a = pyconvert_rule_fast (T. a, x):: pyconvert_returntype (T. a)
291
292
pyconvert_isunconverted (a) || return a
292
- b = pyconvert_rule_fast (T. b, x) :: pyconvert_returntype (T. b)
293
+ b = pyconvert_rule_fast (T. b, x):: pyconvert_returntype (T. b)
293
294
pyconvert_isunconverted (b) || return b
294
295
elseif (T == Nothing) | (T == Missing)
295
296
pyisnone (x) && return pyconvert_return (T ())
@@ -318,7 +319,7 @@ function pytryconvert(::Type{T}, x_) where {T}
318
319
319
320
# We can optimize the conversion for some types by overloading pytryconvert_fast.
320
321
# It MUST give the same results as via the slower route using rules.
321
- ans1 = pyconvert_rule_fast (T, x) :: pyconvert_returntype (T)
322
+ ans1 = pyconvert_rule_fast (T, x):: pyconvert_returntype (T)
322
323
pyconvert_isunconverted (ans1) || return ans1
323
324
324
325
# get rules from the cache
@@ -334,7 +335,7 @@ function pytryconvert(::Type{T}, x_) where {T}
334
335
335
336
# apply the rules
336
337
for rule in rules
337
- ans2 = rule (x) :: pyconvert_returntype (T)
338
+ ans2 = rule (x):: pyconvert_returntype (T)
338
339
pyconvert_isunconverted (ans2) || return ans2
339
340
end
340
341
@@ -386,8 +387,8 @@ pyconvertarg(::Type{T}, x, name) where {T} = @autopy x @pyconvert T x_ begin
386
387
end
387
388
388
389
function init_pyconvert ()
389
- push! (PYCONVERT_EXTRATYPES, pyimport (" io" => " IOBase" ))
390
- push! (PYCONVERT_EXTRATYPES, pyimport (" numbers" => (" Number" , " Complex" , " Real" , " Rational" , " Integral" ))... )
390
+ push! (PYCONVERT_EXTRATYPES, pyimport (" io" => " IOBase" ))
391
+ push! (PYCONVERT_EXTRATYPES, pyimport (" numbers" => (" Number" , " Complex" , " Real" , " Rational" , " Integral" ))... )
391
392
push! (PYCONVERT_EXTRATYPES, pyimport (" collections.abc" => (" Iterable" , " Sequence" , " Set" , " Mapping" ))... )
392
393
393
394
priority = PYCONVERT_PRIORITY_CANONICAL
@@ -405,6 +406,7 @@ function init_pyconvert()
405
406
pyconvert_add_rule (" datetime:datetime" , DateTime, pyconvert_rule_datetime, priority)
406
407
pyconvert_add_rule (" datetime:date" , Date, pyconvert_rule_date, priority)
407
408
pyconvert_add_rule (" datetime:time" , Time, pyconvert_rule_time, priority)
409
+ pyconvert_add_rule (" datetime:timedelta" , Microsecond, pyconvert_rule_timedelta, priority)
408
410
pyconvert_add_rule (" builtins:BaseException" , PyException, pyconvert_rule_exception, priority)
409
411
410
412
priority = PYCONVERT_PRIORITY_NORMAL
@@ -428,6 +430,9 @@ function init_pyconvert()
428
430
pyconvert_add_rule (" collections.abc:Sequence" , Tuple, pyconvert_rule_iterable, priority)
429
431
pyconvert_add_rule (" collections.abc:Set" , Set, pyconvert_rule_iterable, priority)
430
432
pyconvert_add_rule (" collections.abc:Mapping" , Dict, pyconvert_rule_mapping, priority)
433
+ pyconvert_add_rule (" datetime:timedelta" , Millisecond, pyconvert_rule_timedelta, priority)
434
+ pyconvert_add_rule (" datetime:timedelta" , Second, pyconvert_rule_timedelta, priority)
435
+ pyconvert_add_rule (" datetime:timedelta" , Nanosecond, pyconvert_rule_timedelta, priority)
431
436
432
437
priority = PYCONVERT_PRIORITY_FALLBACK
433
438
pyconvert_add_rule (" builtins:object" , Py, pyconvert_rule_object, priority)
0 commit comments