11use crate :: cross:: clrepr:: CLReprObject ;
22use crate :: cross:: { CLRepr , CLReprObjectKind , StringType } ;
3- use pyo3:: exceptions:: { PyNotImplementedError , PyTypeError } ;
3+ use crate :: python:: utils:: PyAnyHelpers ;
4+ use pyo3:: exceptions:: { PyException , PyNotImplementedError , PyTypeError } ;
45use pyo3:: types:: {
5- PyBool , PyComplex , PyDate , PyDict , PyFloat , PyFrame , PyFunction , PyInt , PyList , PySequence ,
6- PySet , PyString , PyTraceback , PyTuple ,
6+ PyBool , PyComplex , PyDate , PyDateTime , PyDelta , PyDict , PyFloat , PyFrame , PyFrozenSet ,
7+ PyFunction , PyInt , PyList , PySequence , PySet , PyString , PyTraceback , PyTuple ,
78} ;
89use pyo3:: { Py , PyAny , PyErr , PyObject , Python , ToPyObject } ;
910
@@ -12,7 +13,7 @@ pub enum PythonRef {
1213 PyObject ( PyObject ) ,
1314 PyFunction ( Py < PyFunction > ) ,
1415 /// Special type to transfer functions through JavaScript
15- /// In JS it's an external object. It's not the same as Function.
16+ /// In JS it's an external object.
1617 PyExternalFunction ( Py < PyFunction > ) ,
1718}
1819
@@ -87,10 +88,28 @@ impl CLRepr {
8788 return Err ( PyErr :: new :: < PyTypeError , _ > (
8889 "Unable to represent PyComplex type as CLR from Python" . to_string ( ) ,
8990 ) ) ;
91+ } else if v. get_type ( ) . is_subclass_of :: < PyDateTime > ( ) ? {
92+ return Err ( PyErr :: new :: < PyTypeError , _ > (
93+ "Unable to represent PyDateTime type as CLR from Python" . to_string ( ) ,
94+ ) ) ;
9095 } else if v. get_type ( ) . is_subclass_of :: < PyDate > ( ) ? {
9196 return Err ( PyErr :: new :: < PyTypeError , _ > (
9297 "Unable to represent PyDate type as CLR from Python" . to_string ( ) ,
9398 ) ) ;
99+ } else if v. get_type ( ) . is_subclass_of :: < PyFrozenSet > ( ) ? {
100+ let set = v. downcast :: < PyFrozenSet > ( ) ?;
101+
102+ return Err ( PyErr :: new :: < PyTypeError , _ > ( format ! (
103+ "Unable to represent PyFrozenSet type as CLR from Python, value: {:?}" ,
104+ set
105+ ) ) ) ;
106+ } else if v. get_type ( ) . is_subclass_of :: < PyException > ( ) ? {
107+ let exception = v. downcast :: < PyException > ( ) ?;
108+
109+ return Err ( PyErr :: new :: < PyTypeError , _ > ( format ! (
110+ "Unable to represent PyException type as CLR from Python, value: {:?}" ,
111+ exception
112+ ) ) ) ;
94113 } else if v. get_type ( ) . is_subclass_of :: < PyFrame > ( ) ? {
95114 let frame = v. downcast :: < PyFrame > ( ) ?;
96115
@@ -105,17 +124,22 @@ impl CLRepr {
105124 "Unable to represent PyTraceback type as CLR from Python, value: {:?}" ,
106125 trb
107126 ) ) ) ;
108- } else {
109- let is_sequence = unsafe { pyo3:: ffi:: PySequence_Check ( v. as_ptr ( ) ) == 1 } ;
110- if is_sequence {
111- let seq = v. downcast :: < PySequence > ( ) ?;
112-
113- return Err ( PyErr :: new :: < PyTypeError , _ > ( format ! (
114- "Unable to represent PySequence type as CLR from Python, value: {:?}" ,
115- seq
116- ) ) ) ;
117- }
127+ } else if v. get_type ( ) . is_subclass_of :: < PyDelta > ( ) ? {
128+ let delta = v. downcast :: < PyDelta > ( ) ?;
129+
130+ return Err ( PyErr :: new :: < PyTypeError , _ > ( format ! (
131+ "Unable to represent PyDelta type as CLR from Python, value: {:?}" ,
132+ delta
133+ ) ) ) ;
134+ } else if v. is_sequence ( ) ? {
135+ let seq = v. downcast :: < PySequence > ( ) ?;
118136
137+ return Err ( PyErr :: new :: < PyTypeError , _ > ( format ! (
138+ "Unable to represent PySequence type as CLR from Python, value: {:?}" ,
139+ seq
140+ ) ) ) ;
141+ } else {
142+ // Fallback to PyObject, it will lead to throw error in the JS side
119143 Self :: PythonRef ( PythonRef :: PyObject ( v. into ( ) ) )
120144 } )
121145 }
0 commit comments