Skip to content

Commit 2cdc8d6

Browse files
jericksodvarrazzo
authored andcommitted
Fix Windows 64bit lobject support for very (>2GB) large objects
The type 'long' with Windows Visual C is 32bits in size for both 32bit and 64bit platforms. Changed type of variables that could be > 2GB from long to Py_ssize_t.
1 parent ab5d8f4 commit 2cdc8d6

File tree

3 files changed

+24
-22
lines changed

3 files changed

+24
-22
lines changed

psycopg/lobject.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ RAISES_NEG HIDDEN int lobject_export(lobjectObject *self, const char *filename);
6060
RAISES_NEG HIDDEN Py_ssize_t lobject_read(lobjectObject *self, char *buf, size_t len);
6161
RAISES_NEG HIDDEN Py_ssize_t lobject_write(lobjectObject *self, const char *buf,
6262
size_t len);
63-
RAISES_NEG HIDDEN long lobject_seek(lobjectObject *self, long pos, int whence);
64-
RAISES_NEG HIDDEN long lobject_tell(lobjectObject *self);
63+
RAISES_NEG HIDDEN Py_ssize_t lobject_seek(lobjectObject *self, Py_ssize_t pos, int whence);
64+
RAISES_NEG HIDDEN Py_ssize_t lobject_tell(lobjectObject *self);
6565
RAISES_NEG HIDDEN int lobject_truncate(lobjectObject *self, size_t len);
6666
RAISES_NEG HIDDEN int lobject_close(lobjectObject *self);
6767

psycopg/lobject_int.c

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -376,12 +376,12 @@ lobject_read(lobjectObject *self, char *buf, size_t len)
376376

377377
/* lobject_seek - move the current position in the lo */
378378

379-
RAISES_NEG long
380-
lobject_seek(lobjectObject *self, long pos, int whence)
379+
RAISES_NEG Py_ssize_t
380+
lobject_seek(lobjectObject *self, Py_ssize_t pos, int whence)
381381
{
382382
PGresult *pgres = NULL;
383383
char *error = NULL;
384-
long where;
384+
Py_ssize_t where;
385385

386386
Dprintf("lobject_seek: fd = %d, pos = %ld, whence = %d",
387387
self->fd, pos, whence);
@@ -391,12 +391,12 @@ lobject_seek(lobjectObject *self, long pos, int whence)
391391

392392
#ifdef HAVE_LO64
393393
if (self->conn->server_version < 90300) {
394-
where = (long)lo_lseek(self->conn->pgconn, self->fd, (int)pos, whence);
394+
where = (Py_ssize_t)lo_lseek(self->conn->pgconn, self->fd, (int)pos, whence);
395395
} else {
396-
where = lo_lseek64(self->conn->pgconn, self->fd, pos, whence);
396+
where = (Py_ssize_t)lo_lseek64(self->conn->pgconn, self->fd, pos, whence);
397397
}
398398
#else
399-
where = (long)lo_lseek(self->conn->pgconn, self->fd, (int)pos, whence);
399+
where = (Py_ssize_t)lo_lseek(self->conn->pgconn, self->fd, (int)pos, whence);
400400
#endif
401401
Dprintf("lobject_seek: where = %ld", where);
402402
if (where < 0)
@@ -412,12 +412,12 @@ lobject_seek(lobjectObject *self, long pos, int whence)
412412

413413
/* lobject_tell - tell the current position in the lo */
414414

415-
RAISES_NEG long
415+
RAISES_NEG Py_ssize_t
416416
lobject_tell(lobjectObject *self)
417417
{
418418
PGresult *pgres = NULL;
419419
char *error = NULL;
420-
long where;
420+
Py_ssize_t where;
421421

422422
Dprintf("lobject_tell: fd = %d", self->fd);
423423

@@ -426,12 +426,12 @@ lobject_tell(lobjectObject *self)
426426

427427
#ifdef HAVE_LO64
428428
if (self->conn->server_version < 90300) {
429-
where = (long)lo_tell(self->conn->pgconn, self->fd);
429+
where = (Py_ssize_t)lo_tell(self->conn->pgconn, self->fd);
430430
} else {
431-
where = lo_tell64(self->conn->pgconn, self->fd);
431+
where = (Py_ssize_t)lo_tell64(self->conn->pgconn, self->fd);
432432
}
433433
#else
434-
where = (long)lo_tell(self->conn->pgconn, self->fd);
434+
where = (Py_ssize_t)lo_tell(self->conn->pgconn, self->fd);
435435
#endif
436436
Dprintf("lobject_tell: where = %ld", where);
437437
if (where < 0)

psycopg/lobject_type.c

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ psyco_lobj_write(lobjectObject *self, PyObject *args)
105105
goto exit;
106106
}
107107

108-
rv = PyInt_FromLong((long)res);
108+
rv = PyInt_FromSsize_t((Py_ssize_t)res);
109109

110110
exit:
111111
Py_XDECREF(data);
@@ -121,7 +121,7 @@ static PyObject *
121121
psyco_lobj_read(lobjectObject *self, PyObject *args)
122122
{
123123
PyObject *res;
124-
long where, end;
124+
Py_ssize_t where, end;
125125
Py_ssize_t size = -1;
126126
char *buffer;
127127

@@ -165,10 +165,10 @@ psyco_lobj_read(lobjectObject *self, PyObject *args)
165165
static PyObject *
166166
psyco_lobj_seek(lobjectObject *self, PyObject *args)
167167
{
168-
long offset, pos=0;
168+
Py_ssize_t offset, pos=0;
169169
int whence=0;
170170

171-
if (!PyArg_ParseTuple(args, "l|i", &offset, &whence))
171+
if (!PyArg_ParseTuple(args, "n|i", &offset, &whence))
172172
return NULL;
173173

174174
EXC_IF_LOBJ_CLOSED(self);
@@ -197,7 +197,7 @@ psyco_lobj_seek(lobjectObject *self, PyObject *args)
197197
if ((pos = lobject_seek(self, offset, whence)) < 0)
198198
return NULL;
199199

200-
return PyLong_FromLong(pos);
200+
return PyLong_FromSsize_t(pos);
201201
}
202202

203203
/* tell method - tell current position in the lobject */
@@ -208,7 +208,7 @@ psyco_lobj_seek(lobjectObject *self, PyObject *args)
208208
static PyObject *
209209
psyco_lobj_tell(lobjectObject *self, PyObject *args)
210210
{
211-
long pos;
211+
Py_ssize_t pos;
212212

213213
EXC_IF_LOBJ_CLOSED(self);
214214
EXC_IF_LOBJ_LEVEL0(self);
@@ -217,7 +217,7 @@ psyco_lobj_tell(lobjectObject *self, PyObject *args)
217217
if ((pos = lobject_tell(self)) < 0)
218218
return NULL;
219219

220-
return PyLong_FromLong(pos);
220+
return PyLong_FromSsize_t(pos);
221221
}
222222

223223
/* unlink method - unlink (destroy) the lobject */
@@ -274,10 +274,12 @@ psyco_lobj_get_closed(lobjectObject *self, void *closure)
274274
static PyObject *
275275
psyco_lobj_truncate(lobjectObject *self, PyObject *args)
276276
{
277-
long len = 0;
277+
Py_ssize_t len = 0;
278278

279-
if (!PyArg_ParseTuple(args, "|l", &len))
279+
Dprintf("psyco_lobj_truncate: Enter lobject object at %p", self);
280+
if (!PyArg_ParseTuple(args, "|n", &len))
280281
return NULL;
282+
Dprintf("psyco_lobj_truncate: Parsed Successfully");
281283

282284
EXC_IF_LOBJ_CLOSED(self);
283285
EXC_IF_LOBJ_LEVEL0(self);

0 commit comments

Comments
 (0)