tostorchconnector 1.0.2__py3-none-any.whl → 1.0.4__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of tostorchconnector might be problematic. Click here for more details.

@@ -1,5 +1,6 @@
1
1
  import io
2
2
  import logging
3
+ import threading
3
4
  from functools import cached_property
4
5
  from os import SEEK_SET, SEEK_CUR, SEEK_END
5
6
  from typing import Optional, Callable, Any
@@ -28,6 +29,8 @@ class TosObjectReader(io.BufferedIOBase):
28
29
  self._total_size: Optional[int] = None
29
30
  self._read_offset = 0
30
31
  self._buffer = io.BytesIO()
32
+ self._closed = False
33
+ self._lock = threading.Lock()
31
34
 
32
35
  @property
33
36
  def bucket(self) -> str:
@@ -37,6 +40,26 @@ class TosObjectReader(io.BufferedIOBase):
37
40
  def key(self) -> str:
38
41
  return self._key
39
42
 
43
+ def __enter__(self):
44
+ return self
45
+
46
+ def __exit__(self, exc_type, exc_val, exc_tb):
47
+ if exc_type is not None:
48
+ try:
49
+ log.info(f'Exception occurred before closing stream: {exc_type.__name__}: {exc_val}')
50
+ except:
51
+ pass
52
+ else:
53
+ self.close()
54
+
55
+ def close(self) -> None:
56
+ with self._lock:
57
+ if self._closed:
58
+ return
59
+ self._closed = True
60
+ if self._object_stream and isinstance(self._object_stream, ReadStream):
61
+ self._object_stream.close()
62
+
40
63
  def read(self, size: Optional[int] = None) -> Optional[bytes]:
41
64
  if self._is_read_to_end():
42
65
  return b''
@@ -52,11 +75,14 @@ class TosObjectReader(io.BufferedIOBase):
52
75
  while 1:
53
76
  chunk = self._object_stream.read(self._object_stream_offset, chunk_size)
54
77
  if not chunk:
78
+ self.close()
55
79
  break
56
80
  self._object_stream_offset += len(chunk)
57
81
  self._buffer.write(chunk)
58
- finally:
59
- self._object_stream.close()
82
+ except:
83
+ self.close()
84
+ raise
85
+
60
86
  elif isinstance(self._object_stream, GetObjectOutput):
61
87
  for chunk in self._object_stream:
62
88
  self._buffer.write(chunk)
@@ -149,13 +175,15 @@ class TosObjectReader(io.BufferedIOBase):
149
175
  while offset > size:
150
176
  chunk = self._object_stream.read(self._object_stream_offset, chunk_size)
151
177
  if not chunk:
178
+ self.close()
152
179
  break
153
180
  size += self._buffer.write(chunk)
154
181
  self._object_stream_offset += len(chunk)
155
182
  self._total_size = self._buffer.tell()
156
- finally:
157
- self._object_stream.close()
158
- else:
183
+ except:
184
+ self.close()
185
+ raise
186
+ elif isinstance(self._object_stream, GetObjectOutput):
159
187
  try:
160
188
  while offset > size:
161
189
  size += self._buffer.write(next(self._object_stream))
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: tostorchconnector
3
- Version: 1.0.2
3
+ Version: 1.0.4
4
4
  Summary: TOS connector integration for PyTorch
5
5
  Author-email: xiangshijian <xiangshijian@bytedance.com>
6
6
  Classifier: Development Status :: 4 - Beta
@@ -5,10 +5,10 @@ tostorchconnector/tos_common.py,sha256=oxxxzpA7Adn3hwNkpB9fHCueGGmZEc2f9tFqtEgPn
5
5
  tostorchconnector/tos_iterable_dataset.py,sha256=a28XPfZJwvWJJeI3qhHXJAAhKyg4U1DHavUI3VsQorY,4312
6
6
  tostorchconnector/tos_map_dataset.py,sha256=o8Fq8O5aG2LS8NA9eFtEfBbkisWwHg-dItO8UAqu08o,3471
7
7
  tostorchconnector/tos_object_meta.py,sha256=YrEQikioD5v0C_VcoudvTn0apUcxqxmSNQ4dDIip1Zc,562
8
- tostorchconnector/tos_object_reader.py,sha256=G6TA3IweJl1BzECmbYibZS7trPUq37Uxlz0EGS9Ul4Y,5647
8
+ tostorchconnector/tos_object_reader.py,sha256=g2Iek4eDQllbr5AeFe81kB_zKSBBw18BGYElZqbnPcE,6472
9
9
  tostorchconnector/tos_object_writer.py,sha256=-I2coKCuUITL_LHf5we3cZBQ6rdn0bQL6NwpF39uDuU,1949
10
- tostorchconnector-1.0.2.dist-info/licenses/LICENSE,sha256=WBJyZyF8q8ZxohuLttIbv7HNLBRA0NnnRWWgfDlDZBE,11361
11
- tostorchconnector-1.0.2.dist-info/METADATA,sha256=G2BM2Q1ECH6rC8s7rkmHbqmSMzb2QZ95hRPGb3PZ5Lk,881
12
- tostorchconnector-1.0.2.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
13
- tostorchconnector-1.0.2.dist-info/top_level.txt,sha256=w9WvBP6KEi5Dourf86IdBbpWe-fad84S4SBftSg7H-k,18
14
- tostorchconnector-1.0.2.dist-info/RECORD,,
10
+ tostorchconnector-1.0.4.dist-info/licenses/LICENSE,sha256=WBJyZyF8q8ZxohuLttIbv7HNLBRA0NnnRWWgfDlDZBE,11361
11
+ tostorchconnector-1.0.4.dist-info/METADATA,sha256=wQTa2P1xE5nSaoyHGABTHHRF1lqx29mQeD6OyoXWnE0,881
12
+ tostorchconnector-1.0.4.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
13
+ tostorchconnector-1.0.4.dist-info/top_level.txt,sha256=w9WvBP6KEi5Dourf86IdBbpWe-fad84S4SBftSg7H-k,18
14
+ tostorchconnector-1.0.4.dist-info/RECORD,,