Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions Include/internal/pycore_bytesobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,15 @@ PyAPI_FUNC(void*) _PyBytesWriter_WriteBytes(_PyBytesWriter *writer,
PyAPI_FUNC(PyBytesWriter*) _PyBytesWriter_CreateByteArray(
Py_ssize_t size);


struct PyBytesWriter {
char small_buffer[256];
PyObject *obj;
Py_ssize_t size;
int use_bytearray;
int overallocate;
};

#ifdef __cplusplus
}
#endif
Expand Down
24 changes: 21 additions & 3 deletions Lib/_pydatetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -1072,7 +1072,11 @@ def fromisocalendar(cls, year, week, day):

@classmethod
def strptime(cls, date_string, format):
"""Parse string according to the given date format (like time.strptime())."""
"""Parse string according to the given date format (like time.strptime()).

For a list of supported format codes, see the documentation:
https://docs.python.org/3/library/datetime.html#format-codes
"""
import _strptime
return _strptime._strptime_datetime_date(cls, date_string, format)

Expand Down Expand Up @@ -1109,6 +1113,8 @@ def strftime(self, format):
Format using strftime().

Example: "%d/%m/%Y, %H:%M:%S"
For a list of supported format codes, see the documentation:
https://docs.python.org/3/library/datetime.html#format-codes
"""
return _wrap_strftime(self, format, self.timetuple())

Expand Down Expand Up @@ -1456,8 +1462,13 @@ def __new__(cls, hour=0, minute=0, second=0, microsecond=0, tzinfo=None, *, fold
return self

@classmethod

def strptime(cls, date_string, format):
"""Parse string according to the given time format (like time.strptime())."""
"""Parse string according to the given time format (like time.strptime()).

For a list of supported format codes, see the documentation:
https://docs.python.org/3/library/datetime.html#format-codes
"""
import _strptime
return _strptime._strptime_datetime_time(cls, date_string, format)

Expand Down Expand Up @@ -1650,6 +1661,9 @@ def fromisoformat(cls, time_string):
def strftime(self, format):
"""Format using strftime(). The date part of the timestamp passed
to underlying strftime should not be used.

For a list of supported format codes, see the documentation:
https://docs.python.org/3/library/datetime.html#format-codes
"""
# The year must be >= 1000 else Python's strftime implementation
# can raise a bogus exception.
Expand Down Expand Up @@ -2198,7 +2212,11 @@ def __str__(self):

@classmethod
def strptime(cls, date_string, format):
"""Parse string according to the given date and time format (like time.strptime())."""
"""Parse string according to the given time format (like time.strptime()).

For a list of supported format codes, see the documentation:
https://docs.python.org/3/library/datetime.html#format-codes
"""
import _strptime
return _strptime._strptime_datetime_datetime(cls, date_string, format)

Expand Down
25 changes: 20 additions & 5 deletions Modules/_datetimemodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -3468,12 +3468,15 @@ datetime.date.strptime
/

Parse string according to the given date format (like time.strptime()).

For a list of supported format codes, see the documentation:
https://docs.python.org/3/library/datetime.html#format-codes
[clinic start generated code]*/

static PyObject *
datetime_date_strptime_impl(PyTypeObject *type, PyObject *string,
PyObject *format)
/*[clinic end generated code: output=454d473bee2d5161 input=001904ab34f594a1]*/
/*[clinic end generated code: output=454d473bee2d5161 input=31d57bb789433e99]*/
{
PyObject *result;

Expand Down Expand Up @@ -3608,11 +3611,14 @@ datetime.date.strftime
Format using strftime().

Example: "%d/%m/%Y, %H:%M:%S".

For a list of supported format codes, see the documentation:
https://docs.python.org/3/library/datetime.html#format-codes
[clinic start generated code]*/

static PyObject *
datetime_date_strftime_impl(PyObject *self, PyObject *format)
/*[clinic end generated code: output=6529b70095e16778 input=72af55077e606ed8]*/
/*[clinic end generated code: output=6529b70095e16778 input=b6fd4a2ded27b557]*/
{
/* This method can be inherited, and needs to call the
* timetuple() method appropriate to self's class.
Expand Down Expand Up @@ -4711,12 +4717,15 @@ datetime.time.strptime
/

Parse string according to the given time format (like time.strptime()).

For a list of supported format codes, see the documentation:
https://docs.python.org/3/library/datetime.html#format-codes
[clinic start generated code]*/

static PyObject *
datetime_time_strptime_impl(PyTypeObject *type, PyObject *string,
PyObject *format)
/*[clinic end generated code: output=ae05a9bc0241d3bf input=6d0f263a5f94d78d]*/
/*[clinic end generated code: output=ae05a9bc0241d3bf input=82ba425ecacc54aa]*/
{
PyObject *result;

Expand Down Expand Up @@ -4891,11 +4900,14 @@ datetime.time.strftime
Format using strftime().

The date part of the timestamp passed to underlying strftime should not be used.

For a list of supported format codes, see the documentation:
https://docs.python.org/3/library/datetime.html#format-codes
[clinic start generated code]*/

static PyObject *
datetime_time_strftime_impl(PyDateTime_Time *self, PyObject *format)
/*[clinic end generated code: output=10f65af20e2a78c7 input=541934a2860f7db5]*/
/*[clinic end generated code: output=10f65af20e2a78c7 input=c4a5bbecd798654b]*/
{
PyObject *result;
PyObject *tuple;
Expand Down Expand Up @@ -5787,12 +5799,15 @@ datetime.datetime.strptime
/

Parse string according to the given date and time format (like time.strptime()).

For a list of supported format codes, see the documentation:
https://docs.python.org/3/library/datetime.html#format-codes
[clinic start generated code]*/

static PyObject *
datetime_datetime_strptime_impl(PyTypeObject *type, PyObject *string,
PyObject *format)
/*[clinic end generated code: output=af2c2d024f3203f5 input=d7597c7f5327117b]*/
/*[clinic end generated code: output=af2c2d024f3203f5 input=ef7807589f1d50e7]*/
{
PyObject *result;

Expand Down
44 changes: 23 additions & 21 deletions Modules/_io/bufferedio.c
Original file line number Diff line number Diff line change
Expand Up @@ -1026,58 +1026,60 @@ static PyObject *
_io__Buffered_read1_impl(buffered *self, Py_ssize_t n)
/*[clinic end generated code: output=bcc4fb4e54d103a3 input=3d0ad241aa52b36c]*/
{
Py_ssize_t have, r;
PyObject *res = NULL;

CHECK_INITIALIZED(self)
if (n < 0) {
n = self->buffer_size;
}

CHECK_CLOSED(self, "read of closed file")

if (n == 0)
return PyBytes_FromStringAndSize(NULL, 0);
if (n == 0) {
return Py_GetConstant(Py_CONSTANT_EMPTY_BYTES);
}

/* Return up to n bytes. If at least one byte is buffered, we
only return buffered bytes. Otherwise, we do one raw read. */

have = Py_SAFE_DOWNCAST(READAHEAD(self), Py_off_t, Py_ssize_t);
Py_ssize_t have = Py_SAFE_DOWNCAST(READAHEAD(self), Py_off_t, Py_ssize_t);
if (have > 0) {
n = Py_MIN(have, n);
res = _bufferedreader_read_fast(self, n);
PyObject *res = _bufferedreader_read_fast(self, n);
assert(res != Py_None);
return res;
}
res = PyBytes_FromStringAndSize(NULL, n);
if (res == NULL)
return NULL;

if (!ENTER_BUFFERED(self)) {
Py_DECREF(res);
return NULL;
}

/* Flush the write buffer if necessary */
if (self->writable) {
PyObject *r = buffered_flush_and_rewind_unlocked(self);
if (r == NULL) {
PyObject *res = buffered_flush_and_rewind_unlocked(self);
if (res == NULL) {
LEAVE_BUFFERED(self)
Py_DECREF(res);
return NULL;
}
Py_DECREF(r);
Py_DECREF(res);
}
_bufferedreader_reset_buf(self);
r = _bufferedreader_raw_read(self, PyBytes_AS_STRING(res), n);

PyBytesWriter *writer = PyBytesWriter_Create(n);
if (writer == NULL) {
return NULL;
}

Py_ssize_t r = _bufferedreader_raw_read(self,
PyBytesWriter_GetData(writer), n);
LEAVE_BUFFERED(self)
if (r == -1) {
Py_DECREF(res);
PyBytesWriter_Discard(writer);
return NULL;
}
if (r == -2)
if (r == -2) {
r = 0;
if (n > r)
_PyBytes_Resize(&res, r);
return res;
}

return PyBytesWriter_FinishWithSize(writer, r);
}

static PyObject *
Expand Down
52 changes: 19 additions & 33 deletions Modules/_io/fileio.c
Original file line number Diff line number Diff line change
Expand Up @@ -739,7 +739,7 @@ _io_FileIO_readall_impl(fileio *self)
/*[clinic end generated code: output=faa0292b213b4022 input=10d8b2ec403302dc]*/
{
Py_off_t pos, end;
PyObject *result;
PyBytesWriter *writer;
Py_ssize_t bytes_read = 0;
Py_ssize_t n;
size_t bufsize;
Expand Down Expand Up @@ -794,10 +794,10 @@ _io_FileIO_readall_impl(fileio *self)
}
}


result = PyBytes_FromStringAndSize(NULL, bufsize);
if (result == NULL)
writer = PyBytesWriter_Create(bufsize);
if (writer == NULL) {
return NULL;
}

while (1) {
if (bytes_read >= (Py_ssize_t)bufsize) {
Expand All @@ -806,18 +806,18 @@ _io_FileIO_readall_impl(fileio *self)
PyErr_SetString(PyExc_OverflowError,
"unbounded read returned more bytes "
"than a Python bytes object can hold");
Py_DECREF(result);
PyBytesWriter_Discard(writer);
return NULL;
}

if (PyBytes_GET_SIZE(result) < (Py_ssize_t)bufsize) {
if (_PyBytes_Resize(&result, bufsize) < 0)
if (PyBytesWriter_GetSize(writer) < (Py_ssize_t)bufsize) {
if (PyBytesWriter_Resize(writer, bufsize) < 0)
return NULL;
}
}

n = _Py_read(self->fd,
PyBytes_AS_STRING(result) + bytes_read,
(char*)PyBytesWriter_GetData(writer) + bytes_read,
bufsize - bytes_read);

if (n == 0)
Expand All @@ -827,20 +827,16 @@ _io_FileIO_readall_impl(fileio *self)
PyErr_Clear();
if (bytes_read > 0)
break;
Py_DECREF(result);
PyBytesWriter_Discard(writer);
Py_RETURN_NONE;
}
Py_DECREF(result);
PyBytesWriter_Discard(writer);
return NULL;
}
bytes_read += n;
}

if (PyBytes_GET_SIZE(result) > bytes_read) {
if (_PyBytes_Resize(&result, bytes_read) < 0)
return NULL;
}
return result;
return PyBytesWriter_FinishWithSize(writer, bytes_read);
}

/*[clinic input]
Expand All @@ -866,10 +862,6 @@ static PyObject *
_io_FileIO_read_impl(fileio *self, PyTypeObject *cls, Py_ssize_t size)
/*[clinic end generated code: output=bbd749c7c224143e input=752d1ad3db8564a5]*/
{
char *ptr;
Py_ssize_t n;
PyObject *bytes;

if (self->fd < 0)
return err_closed();
if (!self->readable) {
Expand All @@ -884,31 +876,25 @@ _io_FileIO_read_impl(fileio *self, PyTypeObject *cls, Py_ssize_t size)
size = _PY_READ_MAX;
}

bytes = PyBytes_FromStringAndSize(NULL, size);
if (bytes == NULL)
PyBytesWriter *writer = PyBytesWriter_Create(size);
if (writer == NULL) {
return NULL;
ptr = PyBytes_AS_STRING(bytes);
}
char *ptr = PyBytesWriter_GetData(writer);

n = _Py_read(self->fd, ptr, size);
Py_ssize_t n = _Py_read(self->fd, ptr, size);
if (n == -1) {
/* copy errno because Py_DECREF() can indirectly modify it */
// copy errno because PyBytesWriter_Discard() can indirectly modify it
int err = errno;
Py_DECREF(bytes);
PyBytesWriter_Discard(writer);
if (err == EAGAIN) {
PyErr_Clear();
Py_RETURN_NONE;
}
return NULL;
}

if (n != size) {
if (_PyBytes_Resize(&bytes, n) < 0) {
Py_CLEAR(bytes);
return NULL;
}
}

return (PyObject *) bytes;
return PyBytesWriter_FinishWithSize(writer, n);
}

/*[clinic input]
Expand Down
Loading
Loading