Fix get_filepath_or_buffer and _get_handle · pandas-dev/pandas@24ea605 (original) (raw)

`@@ -140,39 +140,6 @@ def _is_s3_url(url):

`

140

140

`return False

`

141

141

``

142

142

``

143

``

`-

def maybe_read_encoded_stream(reader, encoding=None, compression=None):

`

144

``

`-

"""

`

145

``

`-

Read an encoded stream from the reader and transform the bytes to unicode if

`

146

``

`-

required based on the encoding.

`

147

``

-

148

``

`-

Parameters

`

149

``

`-


`

150

``

`-

reader : a streamable file-like object

`

151

``

`-

encoding : optional, the encoding to attempt to read

`

152

``

-

153

``

`-

Returns

`

154

``

`-


`

155

``

`-

a tuple of (a stream of decoded bytes, the encoding which was used)

`

156

``

`-

"""

`

157

``

-

158

``

`-

if compat.PY3 or encoding is not None: # pragma: no cover

`

159

``

`-

if encoding:

`

160

``

`-

errors = 'strict'

`

161

``

`-

else:

`

162

``

`-

errors = 'replace'

`

163

``

`-

encoding = 'utf-8'

`

164

``

-

165

``

`-

if compression == 'gzip':

`

166

``

`-

reader = BytesIO(reader.read())

`

167

``

`-

else:

`

168

``

`-

reader = StringIO(reader.read().decode(encoding, errors))

`

169

``

`-

else:

`

170

``

`-

if compression == 'gzip':

`

171

``

`-

reader = BytesIO(reader.read())

`

172

``

`-

encoding = None

`

173

``

`-

return reader, encoding

`

174

``

-

175

``

-

176

143

`def _expand_user(filepath_or_buffer):

`

177

144

`"""Return the argument with an initial component of ~ or ~user

`

178

145

` replaced by that user's home directory.

`

`@@ -242,7 +209,7 @@ def get_filepath_or_buffer(filepath_or_buffer, encoding=None,

`

242

209

`if content_encoding == 'gzip':

`

243

210

`# Override compression based on Content-Encoding header

`

244

211

`compression = 'gzip'

`

245

``

`-

reader, encoding = maybe_read_encoded_stream(req, encoding, compression)

`

``

212

`+

reader = BytesIO(req.read())

`

246

213

`return reader, encoding, compression

`

247

214

``

248

215

`if _is_s3_url(filepath_or_buffer):

`

`@@ -296,12 +263,7 @@ def _get_handle(path_or_buf, mode, encoding=None, compression=None,

`

296

263

`f = path_or_buf

`

297

264

`is_path = isinstance(path_or_buf, compat.string_types)

`

298

265

``

299

``

`-

in Python 3, convert BytesIO or fileobjects passed with an encoding

`

300

``

`-

if compat.PY3 and isinstance(path_or_buf, compat.BytesIO):

`

301

``

`-

from io import TextIOWrapper

`

302

``

`-

return TextIOWrapper(path_or_buf, encoding=encoding)

`

303

``

-

304

``

`-

elif compression:

`

``

266

`+

if compression:

`

305

267

`compression = compression.lower()

`

306

268

``

307

269

`if compat.PY2 and not is_path and encoding:

`

`@@ -371,6 +333,11 @@ def _get_handle(path_or_buf, mode, encoding=None, compression=None,

`

371

333

`# Python 3 and no explicit encoding

`

372

334

`f = open(path_or_buf, mode, errors='replace')

`

373

335

``

``

336

`+

in Python 3, convert BytesIO or fileobjects passed with an encoding

`

``

337

`+

if compat.PY3 and isinstance(path_or_buf, compat.BytesIO):

`

``

338

`+

from io import TextIOWrapper

`

``

339

`+

f = TextIOWrapper(f, encoding=encoding)

`

``

340

+

374

341

`if memory_map and hasattr(f, 'fileno'):

`

375

342

`try:

`

376

343

`g = MMapWrapper(f)

`