@@ -3,13 +3,14 @@ use super::objstr;
3
3
use super :: objtype;
4
4
use crate :: format:: FormatSpec ;
5
5
use crate :: pyobject:: {
6
- FromPyObjectRef , IntoPyObject , PyContext , PyFuncArgs , PyObject , PyObjectPayload , PyObjectRef ,
7
- PyResult , TryFromObject , TypeProtocol ,
6
+ FromPyObjectRef , IdProtocol , IntoPyObject , PyAttributes , PyContext , PyFuncArgs , PyObject ,
7
+ PyObjectPayload , PyObjectRef , PyResult , TryFromObject , TypeProtocol ,
8
8
} ;
9
9
use crate :: vm:: VirtualMachine ;
10
10
use num_bigint:: { BigInt , ToBigInt } ;
11
11
use num_integer:: Integer ;
12
12
use num_traits:: { Pow , Signed , ToPrimitive , Zero } ;
13
+ use std:: cell:: RefCell ;
13
14
use std:: hash:: { Hash , Hasher } ;
14
15
15
16
// This proxy allows for easy switching between types.
@@ -77,7 +78,14 @@ fn int_new(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
77
78
None => Zero :: zero ( ) ,
78
79
} ;
79
80
Ok ( PyObject :: new (
80
- PyObjectPayload :: Integer { value : val } ,
81
+ if cls. is ( & vm. ctx . int_type ( ) ) {
82
+ PyObjectPayload :: Integer { value : val }
83
+ } else {
84
+ PyObjectPayload :: Instance {
85
+ dict : RefCell :: new ( PyAttributes :: new ( ) ) ,
86
+ parent_payload : Some ( Box :: new ( PyObjectPayload :: Integer { value : val } ) ) ,
87
+ }
88
+ } ,
81
89
cls. clone ( ) ,
82
90
) )
83
91
}
@@ -112,10 +120,22 @@ pub fn to_int(vm: &mut VirtualMachine, obj: &PyObjectRef, base: u32) -> PyResult
112
120
113
121
// Retrieve inner int value:
114
122
pub fn get_value ( obj : & PyObjectRef ) -> IntType {
115
- if let PyObjectPayload :: Integer { value } = & obj. payload {
116
- value. clone ( )
117
- } else {
118
- panic ! ( "Inner error getting int {:?}" , obj) ;
123
+ match & obj. payload {
124
+ PyObjectPayload :: Integer { ref value } => value. clone ( ) ,
125
+ PyObjectPayload :: Instance {
126
+ ref parent_payload, ..
127
+ } => {
128
+ if let Some ( parent_payload) = parent_payload {
129
+ if let PyObjectPayload :: Integer { ref value } = * * parent_payload {
130
+ value. clone ( )
131
+ } else {
132
+ panic ! ( "Inner error getting parent int {:?}" , obj) ;
133
+ }
134
+ } else {
135
+ panic ! ( "Inner error getting parent int {:?}" , obj) ;
136
+ }
137
+ }
138
+ _ => panic ! ( "Inner error getting int {:?}" , obj) ,
119
139
}
120
140
}
121
141
0 commit comments