Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit fe39bd1

Browse files
committed
- merge branches
2 parents 5884449 + 3746aee commit fe39bd1

7 files changed

Lines changed: 428 additions & 199 deletions

File tree

Lib/test/test_zipfile.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1134,6 +1134,24 @@ def test_comments(self):
11341134
with zipfile.ZipFile(TESTFN, mode="r") as zipfr:
11351135
self.assertEqual(zipfr.comment, comment2)
11361136

1137+
# check that comments are correctly modified in append mode
1138+
with zipfile.ZipFile(TESTFN,mode="w") as zipf:
1139+
zipf.comment = b"original comment"
1140+
zipf.writestr("foo.txt", "O, for a Muse of Fire!")
1141+
with zipfile.ZipFile(TESTFN,mode="a") as zipf:
1142+
zipf.comment = b"an updated comment"
1143+
with zipfile.ZipFile(TESTFN,mode="r") as zipf:
1144+
self.assertEqual(zipf.comment, b"an updated comment")
1145+
1146+
# check that comments are correctly shortened in append mode
1147+
with zipfile.ZipFile(TESTFN,mode="w") as zipf:
1148+
zipf.comment = b"original comment that's longer"
1149+
zipf.writestr("foo.txt", "O, for a Muse of Fire!")
1150+
with zipfile.ZipFile(TESTFN,mode="a") as zipf:
1151+
zipf.comment = b"shorter comment"
1152+
with zipfile.ZipFile(TESTFN,mode="r") as zipf:
1153+
self.assertEqual(zipf.comment, b"shorter comment")
1154+
11371155
def test_unicode_comment(self):
11381156
with zipfile.ZipFile(TESTFN, "w", zipfile.ZIP_STORED) as zipf:
11391157
zipf.writestr("foo.txt", "O, for a Muse of Fire!")

Modules/_decimal/_decimal.c

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3203,7 +3203,8 @@ static PyObject *
32033203
dec_as_long(PyObject *dec, PyObject *context, int round)
32043204
{
32053205
PyLongObject *pylong;
3206-
size_t maxsize, n;
3206+
digit *ob_digit;
3207+
size_t n;
32073208
Py_ssize_t i;
32083209
mpd_t *x;
32093210
mpd_context_t workctx;
@@ -3234,32 +3235,33 @@ dec_as_long(PyObject *dec, PyObject *context, int round)
32343235
return NULL;
32353236
}
32363237

3237-
maxsize = mpd_sizeinbase(x, PyLong_BASE);
3238-
if (maxsize > PY_SSIZE_T_MAX) {
3239-
mpd_del(x);
3240-
PyErr_NoMemory();
3241-
return NULL;
3242-
}
3243-
pylong = _PyLong_New(maxsize);
3244-
if (pylong == NULL) {
3245-
mpd_del(x);
3246-
return NULL;
3247-
}
3248-
32493238
status = 0;
3239+
ob_digit = NULL;
32503240
#if PYLONG_BITS_IN_DIGIT == 30
3251-
n = mpd_qexport_u32(pylong->ob_digit, maxsize, PyLong_BASE, x, &status);
3241+
n = mpd_qexport_u32(&ob_digit, 0, PyLong_BASE, x, &status);
32523242
#elif PYLONG_BITS_IN_DIGIT == 15
3253-
n = mpd_qexport_u16(pylong->ob_digit, maxsize, PyLong_BASE, x, &status);
3243+
n = mpd_qexport_u16(&ob_digit, 0, PyLong_BASE, x, &status);
32543244
#else
3255-
#error "PYLONG_BITS_IN_DIGIT should be 15 or 30"
3245+
#error "PYLONG_BITS_IN_DIGIT should be 15 or 30"
32563246
#endif
3257-
if (dec_addstatus(context, status)) {
3258-
Py_DECREF((PyObject *) pylong);
3247+
3248+
if (n == SIZE_MAX) {
3249+
PyErr_NoMemory();
3250+
mpd_del(x);
3251+
return NULL;
3252+
}
3253+
3254+
assert(n > 0);
3255+
pylong = _PyLong_New(n);
3256+
if (pylong == NULL) {
3257+
mpd_free(ob_digit);
32593258
mpd_del(x);
32603259
return NULL;
32613260
}
32623261

3262+
memcpy(pylong->ob_digit, ob_digit, n * sizeof(digit));
3263+
mpd_free(ob_digit);
3264+
32633265
i = n;
32643266
while ((i > 0) && (pylong->ob_digit[i-1] == 0)) {
32653267
i--;

Modules/_decimal/libmpdec/basearith.c

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -583,8 +583,30 @@ _mpd_shortadd_b(mpd_uint_t *w, mpd_size_t m, mpd_uint_t v, mpd_uint_t b)
583583
return carry;
584584
}
585585

586+
/* w := product of u (len n) and v (single word). Return carry. */
587+
mpd_uint_t
588+
_mpd_shortmul_c(mpd_uint_t *w, const mpd_uint_t *u, mpd_size_t n, mpd_uint_t v)
589+
{
590+
mpd_uint_t hi, lo;
591+
mpd_uint_t carry = 0;
592+
mpd_size_t i;
593+
594+
assert(n > 0);
595+
596+
for (i=0; i < n; i++) {
597+
598+
_mpd_mul_words(&hi, &lo, u[i], v);
599+
lo = carry + lo;
600+
if (lo < carry) hi++;
601+
602+
_mpd_div_words_r(&carry, &w[i], hi, lo);
603+
}
604+
605+
return carry;
606+
}
607+
586608
/* w := product of u (len n) and v (single word) */
587-
void
609+
mpd_uint_t
588610
_mpd_shortmul_b(mpd_uint_t *w, const mpd_uint_t *u, mpd_size_t n,
589611
mpd_uint_t v, mpd_uint_t b)
590612
{
@@ -602,7 +624,8 @@ _mpd_shortmul_b(mpd_uint_t *w, const mpd_uint_t *u, mpd_size_t n,
602624

603625
_mpd_div_words(&carry, &w[i], hi, lo, b);
604626
}
605-
w[i] = carry;
627+
628+
return carry;
606629
}
607630

608631
/*

Modules/_decimal/libmpdec/basearith.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,10 @@ void _mpd_basemul(mpd_uint_t *w, const mpd_uint_t *u, const mpd_uint_t *v,
4949
mpd_size_t m, mpd_size_t n);
5050
void _mpd_shortmul(mpd_uint_t *w, const mpd_uint_t *u, mpd_size_t n,
5151
mpd_uint_t v);
52-
void _mpd_shortmul_b(mpd_uint_t *w, const mpd_uint_t *u, mpd_size_t n,
53-
mpd_uint_t v, mpd_uint_t b);
52+
mpd_uint_t _mpd_shortmul_c(mpd_uint_t *w, const mpd_uint_t *u, mpd_size_t n,
53+
mpd_uint_t v);
54+
mpd_uint_t _mpd_shortmul_b(mpd_uint_t *w, const mpd_uint_t *u, mpd_size_t n,
55+
mpd_uint_t v, mpd_uint_t b);
5456
mpd_uint_t _mpd_shortdiv(mpd_uint_t *w, const mpd_uint_t *u, mpd_size_t n,
5557
mpd_uint_t v);
5658
mpd_uint_t _mpd_shortdiv_b(mpd_uint_t *w, const mpd_uint_t *u, mpd_size_t n,

0 commit comments

Comments
 (0)