Skip to content

Commit bdf32f7

Browse files
committed
memory loading bug fix for python3 (bytes/string change)
1 parent a33a1df commit bdf32f7

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

screepsapi/screepsapi.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
except ImportError:
99
from io import StringIO
1010

11+
from io import BytesIO
1112
from gzip import GzipFile
1213
import json
1314
import logging
@@ -75,14 +76,26 @@ def user_rooms(self, userid, shard='shard0'):
7576
def memory(self, path='', shard='shard0'):
7677
ret = self.get('user/memory', path=path, shard=shard)
7778
if 'data' in ret:
78-
ret['data'] = json.load(GzipFile(fileobj=StringIO(b64decode(ret['data'][3:]))))
79+
try:
80+
gzip_input = StringIO(b64decode(ret['data'][3:]))
81+
except:
82+
gzip_input = BytesIO(b64decode(ret['data'][3:]))
83+
ret['data'] = json.load(GzipFile(fileobj=gzip_input))
7984
return ret
8085

8186
def set_memory(self, path, value, shard='shard0'):
8287
return self.post('user/memory', path=path, value=value, shard=shard)
8388

8489
def get_segment(self, segment, shard='shard0'):
85-
return self.get('user/memory-segment', segment=segment, shard=shard)
90+
ret = self.get('user/memory-segment', segment=segment, shard=shard)
91+
if 'data' in ret and ret['data'][:3] == 'gz:':
92+
try:
93+
gzip_input = StringIO(b64decode(ret['data'][3:]))
94+
except:
95+
gzip_input = BytesIO(b64decode(ret['data'][3:]))
96+
ret['data'] = GzipFile(fileobj=gzip_input)
97+
return ret
98+
8699

87100
def set_segment(self, segment, data, shard='shard0'):
88101
return self.post('user/memory-segment', segment=segment, data=data, shard=shard)

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
long_description = open('README.md').read()
99

1010

11-
version = '0.3.0'
11+
version = '0.4.1'
1212
setup(
1313
name = 'screepsapi',
1414
version = version,

0 commit comments

Comments
 (0)