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

Skip to content

Commit c896700

Browse files
committed
Partial fix for bug #1306
Multiple reinitializations of Python 3.0 failed on a system without a hardcoded default fs encoding. The patch makes sure that the default fs encoding is freed and reset to NULL on e.g. Linux. I've also taken the liberty to increase the debugging in Objects/object.c:_Py_ForgetReference(). The method is used to validate the reference chain. Reinitialization still fails in the 3rd round of my test suite: * ob object : <refcnt 0 at 0x821c840> type : str refcount: 0 address : 0x821c840 * op->_ob_prev->_ob_next object : <refcnt 0 at 0x821c840> type : str refcount: 0 address : 0x821c840 * op->_ob_next->_ob_prev object : bytearray(b'') type : bytearray refcount: 1 address : 0x826b838 Fatal Python error: UNREF invalid object
1 parent a22e8bd commit c896700

4 files changed

Lines changed: 18 additions & 1 deletion

File tree

Include/fileobject.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ PyAPI_FUNC(char *) Py_UniversalNewlineFgets(char *, int, FILE*, PyObject *);
2020
If non-NULL, this is different than the default encoding for strings
2121
*/
2222
PyAPI_DATA(const char *) Py_FileSystemDefaultEncoding;
23+
PyAPI_DATA(const int) Py_HasFileSystemDefaultEncoding;
2324

2425
/* Internal API
2526

Objects/object.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1532,8 +1532,15 @@ _Py_ForgetReference(register PyObject *op)
15321532
if (op->ob_refcnt < 0)
15331533
Py_FatalError("UNREF negative refcnt");
15341534
if (op == &refchain ||
1535-
op->_ob_prev->_ob_next != op || op->_ob_next->_ob_prev != op)
1535+
op->_ob_prev->_ob_next != op || op->_ob_next->_ob_prev != op) {
1536+
fprintf(stderr, "* ob\n");
1537+
_PyObject_Dump(op);
1538+
fprintf(stderr, "* op->_ob_prev->_ob_next\n");
1539+
_PyObject_Dump(op->_ob_prev->_ob_next);
1540+
fprintf(stderr, "* op->_ob_next->_ob_prev\n");
1541+
_PyObject_Dump(op->_ob_next->_ob_prev);
15361542
Py_FatalError("UNREF invalid object");
1543+
}
15371544
#ifdef SLOW_UNREF_CHECK
15381545
for (p = refchain._ob_next; p != &refchain; p = p->_ob_next) {
15391546
if (p == op)

Python/bltinmodule.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,13 @@
1616
*/
1717
#if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T)
1818
const char *Py_FileSystemDefaultEncoding = "mbcs";
19+
const int Py_HasFileSystemDefaultEncoding = 1;
1920
#elif defined(__APPLE__)
2021
const char *Py_FileSystemDefaultEncoding = "utf-8";
22+
const int Py_HasFileSystemDefaultEncoding = 1;
2123
#else
2224
const char *Py_FileSystemDefaultEncoding = NULL; /* use default */
25+
const int Py_HasFileSystemDefaultEncoding = 0;
2326
#endif
2427

2528
static PyObject *

Python/pythonrun.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,12 @@ Py_Finalize(void)
502502
/* Cleanup Unicode implementation */
503503
_PyUnicode_Fini();
504504

505+
/* reset file system default encoding */
506+
if (!Py_HasFileSystemDefaultEncoding) {
507+
free((char*)Py_FileSystemDefaultEncoding);
508+
Py_FileSystemDefaultEncoding = NULL;
509+
}
510+
505511
/* XXX Still allocated:
506512
- various static ad-hoc pointers to interned strings
507513
- int and float free list blocks

0 commit comments

Comments
 (0)