77end
88
99struct PyConvertRule
10- type :: Type
11- func :: Function
12- priority :: PyConvertPriority
10+ type:: Type
11+ func:: Function
12+ priority:: PyConvertPriority
1313end
1414
15- const PYCONVERT_RULES = Dict {String, Vector{PyConvertRule}} ()
15+ const PYCONVERT_RULES = Dict {String,Vector{PyConvertRule}} ()
1616const PYCONVERT_EXTRATYPES = Py[]
1717
1818"""
@@ -201,7 +201,7 @@ function _pyconvert_get_rules(pytype::Py)
201201 # check the original MRO is preserved
202202 omro_ = filter (t -> pyisin (t, omro), mro)
203203 @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_))
205205
206206 # get the names of the types in the MRO of pytype
207207 xmro = [String[pyconvert_typename (t)] for t in mro]
@@ -240,22 +240,23 @@ function _pyconvert_get_rules(pytype::Py)
240240 rules = PyConvertRule[rule for tname in mro for rule in get! (Vector{PyConvertRule}, PYCONVERT_RULES, tname)]
241241
242242 # 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 )
244244 rules = rules[order]
245245
246- @debug " pyconvert" pytype mro= join (mro, " " )
246+ @debug " pyconvert" pytype mro = join (mro, " " )
247247 return rules
248248end
249249
250250const PYCONVERT_PREFERRED_TYPE = Dict {Py,Type} ()
251251
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
257259 end
258- end
259260
260261function pyconvert_get_rules (type:: Type , pytype:: Py )
261262 @nospecialize type
@@ -281,15 +282,15 @@ end
281282
282283pyconvert_fix (:: Type{T} , func) where {T} = x -> func (T, x)
283284
284- const PYCONVERT_RULES_CACHE = Dict {Type, Dict{C.PyPtr, Vector{Function}}} ()
285+ const PYCONVERT_RULES_CACHE = Dict {Type,Dict{C.PyPtr,Vector{Function}}} ()
285286
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)
287288
288289function pyconvert_rule_fast (:: Type{T} , x:: Py ) where {T}
289290 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)
291292 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)
293294 pyconvert_isunconverted (b) || return b
294295 elseif (T == Nothing) | (T == Missing)
295296 pyisnone (x) && return pyconvert_return (T ())
@@ -318,7 +319,7 @@ function pytryconvert(::Type{T}, x_) where {T}
318319
319320 # We can optimize the conversion for some types by overloading pytryconvert_fast.
320321 # 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)
322323 pyconvert_isunconverted (ans1) || return ans1
323324
324325 # get rules from the cache
@@ -334,7 +335,7 @@ function pytryconvert(::Type{T}, x_) where {T}
334335
335336 # apply the rules
336337 for rule in rules
337- ans2 = rule (x) :: pyconvert_returntype (T)
338+ ans2 = rule (x):: pyconvert_returntype (T)
338339 pyconvert_isunconverted (ans2) || return ans2
339340 end
340341
@@ -386,8 +387,8 @@ pyconvertarg(::Type{T}, x, name) where {T} = @autopy x @pyconvert T x_ begin
386387end
387388
388389function 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" ))... )
391392 push! (PYCONVERT_EXTRATYPES, pyimport (" collections.abc" => (" Iterable" , " Sequence" , " Set" , " Mapping" ))... )
392393
393394 priority = PYCONVERT_PRIORITY_CANONICAL
@@ -405,6 +406,7 @@ function init_pyconvert()
405406 pyconvert_add_rule (" datetime:datetime" , DateTime, pyconvert_rule_datetime, priority)
406407 pyconvert_add_rule (" datetime:date" , Date, pyconvert_rule_date, priority)
407408 pyconvert_add_rule (" datetime:time" , Time, pyconvert_rule_time, priority)
409+ pyconvert_add_rule (" datetime:timedelta" , Microsecond, pyconvert_rule_timedelta, priority)
408410 pyconvert_add_rule (" builtins:BaseException" , PyException, pyconvert_rule_exception, priority)
409411
410412 priority = PYCONVERT_PRIORITY_NORMAL
@@ -428,6 +430,9 @@ function init_pyconvert()
428430 pyconvert_add_rule (" collections.abc:Sequence" , Tuple, pyconvert_rule_iterable, priority)
429431 pyconvert_add_rule (" collections.abc:Set" , Set, pyconvert_rule_iterable, priority)
430432 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)
431436
432437 priority = PYCONVERT_PRIORITY_FALLBACK
433438 pyconvert_add_rule (" builtins:object" , Py, pyconvert_rule_object, priority)
0 commit comments