File tree 1 file changed +6
-9
lines changed
1 file changed +6
-9
lines changed Original file line number Diff line number Diff line change @@ -65,6 +65,7 @@ class KaitaiStream(object):
65
65
def __init__ (self , io ):
66
66
self ._io = io
67
67
self .align_to_byte ()
68
+ self .precompute_size ()
68
69
69
70
def __enter__ (self ):
70
71
return self
@@ -95,21 +96,17 @@ def pos(self):
95
96
return self ._io .tell ()
96
97
97
98
def size (self ):
98
- if hasattr (self , 'size_cached' ):
99
- return self .size_cached
99
+ # Further optimisation: make `size()` into `size`, requires compiler coop.
100
+ return self .size_cached
101
+
102
+ def precompute_size (self ):
100
103
# Python has no internal File object API function to get
101
- # current file / StringIO size, thus we use the following
102
- # trick.
104
+ # current file / StringIO size, thus we use the following trick.
103
105
io = self ._io
104
- # Remember our current position
105
106
cur_pos = io .tell ()
106
- # Seek to the end of the File object
107
- # Remember position, which is equal to the full length
108
107
full_size = io .seek (0 , SEEK_END )
109
- # Seek back to the current position
110
108
io .seek (cur_pos )
111
109
self .size_cached = full_size
112
- return full_size
113
110
114
111
# ========================================================================
115
112
# Integer numbers
You can’t perform that action at this time.
0 commit comments