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

Skip to content

Commit 7f9fa97

Browse files
committed
fix import related leaks
1 parent 855d0b3 commit 7f9fa97

2 files changed

Lines changed: 23 additions & 17 deletions

File tree

Python/bltinmodule.c

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,10 @@ builtin___import__(self, args)
4747
object *args;
4848
{
4949
char *name;
50-
object *m;
5150

5251
if (!newgetargs(args, "s:__import__", &name))
5352
return NULL;
54-
m = import_module(name);
55-
XINCREF(m);
56-
57-
return m;
53+
return import_module(name);
5854
}
5955

6056

Python/import.c

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,8 @@ get_modules()
110110
/* Get the module object corresponding to a module name.
111111
First check the modules dictionary if there's one there,
112112
if not, create a new one and insert in in the modules dictionary.
113-
Because the former action is most common, this does not return a
114-
'new' reference! */
113+
Because the former action is most common, THIS DOES NOT RETURN A
114+
'NEW' REFERENCE! */
115115

116116
object *
117117
add_module(name)
@@ -135,7 +135,8 @@ add_module(name)
135135
}
136136

137137

138-
/* Execute a code object in a module and return its module object */
138+
/* Execute a code object in a module and return the module object
139+
WITH INCREMENTED REFERENCE COUNT */
139140

140141
static object *
141142
exec_code_module(name, co)
@@ -247,7 +248,7 @@ read_compiled_module(fp)
247248

248249

249250
/* Load a module from a compiled file, execute it, and return its
250-
module object */
251+
module object WITH INCREMENTED REFERENCE COUNT */
251252

252253
static object *
253254
load_compiled_module(name, cpathname, fp)
@@ -344,8 +345,8 @@ write_compiled_module(co, cpathname, mtime)
344345

345346

346347
/* Load a source module from a given file and return its module
347-
object. If there's a matching byte-compiled file, use that
348-
instead. */
348+
object WITH INCREMENTED REFERENCE COUNT. If there's a matching
349+
byte-compiled file, use that instead. */
349350

350351
static object *
351352
load_source_module(name, pathname, fp)
@@ -452,7 +453,7 @@ find_module(name, path, buf, buflen, p_fp)
452453

453454

454455
/* Load an external module using the default search path and return
455-
its module object */
456+
its module object WITH INCREMENTED REFERENCE COUNT */
456457

457458
static object *
458459
load_module(name)
@@ -461,7 +462,7 @@ load_module(name)
461462
char buf[MAXPATHLEN+1];
462463
struct filedescr *fdp;
463464
FILE *fp = NULL;
464-
object *m = NULL;
465+
object *m;
465466

466467
fdp = find_module(name, (object *)NULL, buf, MAXPATHLEN+1, &fp);
467468
if (fdp == NULL)
@@ -484,6 +485,7 @@ load_module(name)
484485
default:
485486
err_setstr(SystemError,
486487
"find_module returned unexpected result");
488+
m = NULL;
487489

488490
}
489491
fclose(fp);
@@ -556,20 +558,26 @@ init_frozen(name)
556558
}
557559
m = exec_code_module(name, (codeobject *)co);
558560
DECREF(co);
559-
return m == NULL ? -1 : 1;
561+
if (m == NULL)
562+
return -1;
563+
DECREF(m);
564+
return 1;
560565
}
561566

562567

563568
/* Import a module, either built-in, frozen, or external, and return
564-
its module object */
569+
its module object WITH INCREMENTED REFERENCE COUNT */
565570

566571
object *
567572
import_module(name)
568573
char *name;
569574
{
570575
object *m;
571576

572-
if ((m = dictlookup(import_modules, name)) == NULL) {
577+
if ((m = dictlookup(import_modules, name)) != NULL) {
578+
INCREF(m);
579+
}
580+
else {
573581
int i;
574582
if ((i = init_builtin(name)) || (i = init_frozen(name))) {
575583
if (i < 0)
@@ -579,6 +587,8 @@ import_module(name)
579587
err_setstr(SystemError,
580588
"built-in module not initialized properly");
581589
}
590+
else
591+
INCREF(m);
582592
}
583593
else
584594
m = load_module(name);
@@ -613,10 +623,10 @@ reload_module(m)
613623
if ((i = init_builtin(name)) || (i = init_frozen(name))) {
614624
if (i < 0)
615625
return NULL;
626+
INCREF(m);
616627
}
617628
else
618629
m = load_module(name);
619-
XINCREF(m);
620630
return m;
621631
}
622632

0 commit comments

Comments
 (0)