| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -116,6 +116,14 @@ def test_bytes(self): | |||
| 116 | 116 | for s in [b"", b"Andr\xe8 Previn", b"abc", b" "*10000]: | |
| 117 | 117 | self.helper(s) | |
| 118 | 118 | ||
| 119 | + @support.cpython_only | ||
| 120 | + def test_bytes_singleton(self): | ||
| 121 | + for version in range(marshal.version + 1): | ||
| 122 | + for sample in [b"", b"x"]: | ||
| 123 | + new = marshal.loads(marshal.dumps(sample, version)) | ||
| 124 | + self.assertIs(new, sample) | ||
| 125 | + | ||
| 126 | + | ||
| 119 | 127 | class ExceptionTestCase(unittest.TestCase): | |
| 120 | 128 | def test_exceptions(self): | |
| 121 | 129 | new = marshal.loads(marshal.dumps(StopIteration)) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,2 @@ | |||
| 1 | + :func:`marshal.load` and :func:`marshal.loads` can now get 1-byte string | ||
| 2 | + singletons. Patch by Victor Stinner. | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1308,16 +1308,12 @@ r_object(RFILE *p) | |||
| 1308 | 1308 | } | |
| 1309 | 1309 | break; | |
| 1310 | 1310 | } | |
| 1311 | - v = PyBytes_FromStringAndSize((char *)NULL, n); | ||
| 1312 | - if (v == NULL) | ||
| 1313 | - break; | ||
| 1314 | 1311 | ptr = r_string(n, p); | |
| 1315 | 1312 | if (ptr == NULL) { | |
| 1316 | - Py_DECREF(v); | ||
| 1317 | 1313 | break; | |
| 1318 | 1314 | } | |
| 1319 | - memcpy(PyBytes_AS_STRING(v), ptr, n); | ||
| 1320 | - retval = v; | ||
| 1315 | + // Get a singleton for 1-byte string | ||
| 1316 | + retval = PyBytes_FromStringAndSize(ptr, n); // can be NULL | ||
| 1321 | 1317 | R_REF(retval); | |
| 1322 | 1318 | break; | |
| 1323 | 1319 | } | |
| Back | FazBrowse Home | New Git URL |
0 commit comments