1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
|
/*
* zran_file_util.c - file utilities used in zran.c to manipulate either
* Python file-like objects or file descriptors.
*
* Functions which interact with Python file-likes will acquire and release
* the GIL as needed.
*/
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#define PY_SSIZE_T_CLEAN
#include <Python.h>
#include "zran_file_util.h"
#ifdef _WIN32
#define FSEEK _fseeki64
#define FTELL _ftelli64
#include "windows.h"
#include "io.h"
#else
#define FSEEK fseeko
#define FTELL ftello
#endif
/*
* The zran functions are typically called with the GIL released. These
* macros are used to temporarily (re-)acquire and release the GIL when
* interacting with Python file-like objects.
*/
#define _ZRAN_FILE_UTIL_ACQUIRE_GIL \
PyGILState_STATE s; \
s = PyGILState_Ensure();
#define _ZRAN_FILE_UTIL_RELEASE_GIL \
PyGILState_Release(s);
/*
* Implements a method analogous to fread that is performed on Python
* file-like objects.
*/
size_t _fread_python(void *ptr, size_t size, size_t nmemb, PyObject *f) {
PyObject *data = NULL;
char *buf;
Py_ssize_t len;
_ZRAN_FILE_UTIL_ACQUIRE_GIL
if ((data = PyObject_CallMethod(f, "read", "(n)", size * nmemb)) == NULL)
goto fail;
if ((buf = PyBytes_AsString(data)) == NULL)
goto fail;
if ((len = PyBytes_Size(data)) == -1)
goto fail;
memmove(ptr, buf, (size_t) len);
Py_DECREF(data);
_ZRAN_FILE_UTIL_RELEASE_GIL
return (size_t) len / size;
fail:
Py_XDECREF(data);
_ZRAN_FILE_UTIL_RELEASE_GIL
return 0;
}
/*
* Implements a method analogous to ftell that is performed on Python
* file-like objects.
*/
int64_t _ftell_python(PyObject *f) {
PyObject *data = NULL;
int64_t result;
_ZRAN_FILE_UTIL_ACQUIRE_GIL
data = PyObject_CallMethod(f, "tell", NULL);
if (data == NULL)
goto fail;
result = PyLong_AsLong(data);
if (result == -1 && PyErr_Occurred())
goto fail;
Py_DECREF(data);
_ZRAN_FILE_UTIL_RELEASE_GIL
return result;
fail:
Py_XDECREF(data);
_ZRAN_FILE_UTIL_RELEASE_GIL
return -1;
}
/*
* Implements a method analogous to fseek that is performed on Python
* file-like objects.
*/
int _fseek_python(PyObject *f, int64_t offset, int whence) {
PyObject *data = NULL;
_ZRAN_FILE_UTIL_ACQUIRE_GIL
/*
* The seek method returns type long, which has
* different sizes on different platforms
*/
if (sizeof(long) == 8)
data = PyObject_CallMethod(f, "seek", "(l,i)", offset, whence);
else if (sizeof(long long) == 8)
data = PyObject_CallMethod(f, "seek", "(L,i)", offset, whence);
else
goto fail;
if (data == NULL)
goto fail;
Py_DECREF(data);
_ZRAN_FILE_UTIL_RELEASE_GIL
return 0;
fail:
Py_XDECREF(data);
_ZRAN_FILE_UTIL_RELEASE_GIL
return -1;
}
/*
* Implements a method analogous to feof that is performed on Python file-like
* objects. If f_ret, the number of bytes returned by the last read, is zero,
* then we're at EOF.
*/
int _feof_python(PyObject *f, size_t f_ret) {
return f_ret == 0;
}
/*
* Implements a method analogous to ferror that is performed on Python
* file-like objects.
*/
int _ferror_python(PyObject *f) {
PyObject *result;
_ZRAN_FILE_UTIL_ACQUIRE_GIL
result = PyErr_Occurred();
_ZRAN_FILE_UTIL_RELEASE_GIL
if (result != NULL) return 1;
else return 0;
}
/*
* Implements a method analogous to fflush that is performed on Python
* file-like objects.
*/
int _fflush_python(PyObject *f) {
PyObject *data = NULL;
_ZRAN_FILE_UTIL_ACQUIRE_GIL
if ((data = PyObject_CallMethod(f, "flush", NULL)) == NULL) goto fail;
Py_DECREF(data);
_ZRAN_FILE_UTIL_RELEASE_GIL
return 0;
fail:
Py_XDECREF(data);
_ZRAN_FILE_UTIL_RELEASE_GIL
return -1;
}
/*
* Implements a method analogous to fwrite that is performed on Python
* file-like objects.
*/
size_t _fwrite_python(const void *ptr,
size_t size,
size_t nmemb,
PyObject *f) {
PyObject *input = NULL;
PyObject *data = NULL;
long len;
_ZRAN_FILE_UTIL_ACQUIRE_GIL
if ((input = PyBytes_FromStringAndSize(ptr, size * nmemb)) == NULL)
goto fail;
if ((data = PyObject_CallMethod(f, "write", "(O)", input)) == NULL)
goto fail;
#if PY_MAJOR_VERSION >= 3
if ((len = PyLong_AsLong(data)) == -1 && PyErr_Occurred())
goto fail;
#else
// In Python 2, a file object's write() method does not return the number
// of bytes written, so let's just assume that everything has been written
// properly.
len = size * nmemb;
#endif
Py_DECREF(input);
Py_DECREF(data);
_ZRAN_FILE_UTIL_RELEASE_GIL
return (size_t) len / size;
fail:
Py_XDECREF(input);
Py_XDECREF(data);
_ZRAN_FILE_UTIL_RELEASE_GIL
return 0;
}
/*
* Implements a method analogous to getc that is performed on Python file-like
* objects.
*/
int _getc_python(PyObject *f) {
char buf;
if (_fread_python(&buf, 1, 1, f) == 0) {
// Reached EOF, or an error (in which case the error indicator is set).
// Either way, we should return -1.
return -1;
}
return buf;
}
/*
* Calls the .seekable() method on Python file-like objects.
*/
int _seekable_python(PyObject *f) {
PyObject *data = NULL;
int64_t result;
_ZRAN_FILE_UTIL_ACQUIRE_GIL
data = PyObject_CallMethod(f, "seekable", NULL);
if (data == NULL)
goto fail;
result = PyLong_AsLong(data);
if (result == -1 && PyErr_Occurred())
goto fail;
Py_DECREF(data);
_ZRAN_FILE_UTIL_RELEASE_GIL
return result;
fail:
Py_XDECREF(data);
_ZRAN_FILE_UTIL_RELEASE_GIL
return -1;
}
/*
* Hacky implementation of seekable() for Python 2.
* If f.tell() returns an error, assume that the
* object is unseekable.
*/
int _seekable_python2(PyObject *f) {
int64_t ret;
ret = _ftell_python(f);
if (ret < 0) {
PyErr_Clear();
}
return ret >= 0;
}
/*
* Calls ferror on fd if specified, otherwise the Python-specific method on f.
*/
int ferror_(FILE *fd, PyObject *f) {
return fd != NULL ? ferror(fd) : _ferror_python(f);
}
/*
* Calls fseek on fd if specified, otherwise the Python-specific method on f.
*/
int fseek_(FILE *fd, PyObject *f, int64_t offset, int whence) {
return fd != NULL
? FSEEK(fd, offset, whence)
: _fseek_python(f, offset, whence);
}
/*
* Calls ftell on fd if specified, otherwise the Python-specific method on f.
*/
int64_t ftell_(FILE *fd, PyObject *f) {
return fd != NULL ? FTELL(fd) : _ftell_python(f);
}
/*
* Calls fread on fd if specified, otherwise the Python-specific method on f.
*/
size_t fread_(void *ptr, size_t size, size_t nmemb, FILE *fd, PyObject *f) {
return fd != NULL
? fread(ptr, size, nmemb, fd)
: _fread_python(ptr, size, nmemb, f);
}
/*
* Calls feof on fd if specified, otherwise the Python-specific method on f.
* If fd is not specified, requires f_ret, the number of bytes read on the last
* read, to determine if the file is at EOF.
*/
int feof_(FILE *fd, PyObject *f, size_t f_ret) {
return fd != NULL ? feof(fd): _feof_python(f, f_ret);
}
/*
* Calls fflush on fd if specified, otherwise the Python-specific method on f.
*/
int fflush_(FILE *fd, PyObject *f) {
return fd != NULL ? fflush(fd): _fflush_python(f);
}
/*
* Calls fwrite on fd if specified, otherwise the Python-specific method on f.
*/
size_t fwrite_(const void *ptr,
size_t size,
size_t nmemb,
FILE *fd,
PyObject *f) {
return fd != NULL
? fwrite(ptr, size, nmemb, fd)
: _fwrite_python(ptr, size, nmemb, f);
}
/*
* Calls getc on fd if specified, otherwise the Python-specific method on f.
*/
int getc_(FILE *fd, PyObject *f) {
return fd != NULL ? getc(fd): _getc_python(f);
}
/*
* Returns whether the given file is seekable. If fd is specified, assumes it's always seekable.
* If f is specified, calls f.seekable() to see if the Python file object is seekable.
*/
int seekable_(FILE *fd, PyObject *f) {
#if PY_MAJOR_VERSION > 2
return fd != NULL ? 1: _seekable_python(f);
#else
return fd != NULL ? 1: _seekable_python2(f);
#endif
}
|