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

Skip to content

Commit 6ec1efb

Browse files
committed
add imp.get_frozen_object()
1 parent b7b4562 commit 6ec1efb

1 file changed

Lines changed: 54 additions & 13 deletions

File tree

Python/import.c

Lines changed: 54 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -411,13 +411,13 @@ find_module(name, path, buf, buflen, p_fp)
411411
{
412412
int i, npath, len, namelen;
413413
struct filedescr *fdp;
414-
FILE *fp;
414+
FILE *fp = NULL;
415415

416416
if (path == NULL)
417417
path = sysget("path");
418418
if (path == NULL || !is_listobject(path)) {
419419
err_setstr(ImportError,
420-
"module search path must be list of directory names");
420+
"sys.path must be a list of directory names");
421421
return NULL;
422422
}
423423
npath = getlistsize(path);
@@ -528,7 +528,7 @@ init_builtin(name)
528528
if (strcmp(name, inittab[i].name) == 0) {
529529
if (inittab[i].initfunc == NULL) {
530530
err_setstr(ImportError,
531-
"cannot re-init internal module");
531+
"Cannot re-init internal module");
532532
return -1;
533533
}
534534
if (verbose)
@@ -544,39 +544,66 @@ init_builtin(name)
544544
}
545545

546546

547-
/* Initialize a frozen module.
548-
Return 1 for succes, 0 if the module is not found, and -1 with
549-
an exception set if the initialization failed. */
547+
/* Frozen modules */
550548

551549
extern struct frozen {
552550
char *name;
553551
char *code;
554552
int size;
555553
} frozen_modules[];
556554

557-
/* This function is also used from frozenmain.c */
558-
559-
int
560-
init_frozen(name)
555+
static struct frozen *
556+
find_frozen(name)
561557
char *name;
562558
{
563559
struct frozen *p;
564560
object *co;
565-
object *m;
561+
566562
for (p = frozen_modules; ; p++) {
567563
if (p->name == NULL)
568-
return 0;
564+
return NULL;
569565
if (strcmp(p->name, name) == 0)
570566
break;
571567
}
568+
return p;
569+
}
570+
571+
static object *
572+
get_frozen_object(name)
573+
char *name;
574+
{
575+
struct frozen *p = find_frozen(name);
576+
577+
if (p == NULL) {
578+
err_setstr(ImportError, "No such frozen object");
579+
return NULL;
580+
}
581+
return rds_object(p->code, p->size);
582+
}
583+
584+
/* Initialize a frozen module.
585+
Return 1 for succes, 0 if the module is not found, and -1 with
586+
an exception set if the initialization failed.
587+
This function is also used from frozenmain.c */
588+
589+
int
590+
init_frozen(name)
591+
char *name;
592+
{
593+
struct frozen *p = find_frozen(name);
594+
object *co;
595+
object *m;
596+
597+
if (p == NULL)
598+
return 0;
572599
if (verbose)
573600
fprintf(stderr, "import %s # frozen\n", name);
574601
co = rds_object(p->code, p->size);
575602
if (co == NULL)
576603
return -1;
577604
if (!is_codeobject(co)) {
578605
DECREF(co);
579-
err_setstr(SystemError, "frozen object is not a code object");
606+
err_setstr(TypeError, "frozen object is not a code object");
580607
return -1;
581608
}
582609
m = exec_code_module(name, co);
@@ -785,6 +812,19 @@ imp_init_frozen(self, args)
785812
return m;
786813
}
787814

815+
static object *
816+
imp_get_frozen_object(self, args)
817+
object *self;
818+
object *args;
819+
{
820+
char *name;
821+
int ret;
822+
object *m;
823+
if (!newgetargs(args, "s", &name))
824+
return NULL;
825+
return get_frozen_object(name);
826+
}
827+
788828
static object *
789829
imp_is_builtin(self, args)
790830
object *self;
@@ -928,6 +968,7 @@ imp_new_module(self, args)
928968
}
929969

930970
static struct methodlist imp_methods[] = {
971+
{"get_frozen_object", imp_get_frozen_object, 1},
931972
{"get_magic", imp_get_magic, 1},
932973
{"get_suffixes", imp_get_suffixes, 1},
933974
{"find_module", imp_find_module, 1},

0 commit comments

Comments
 (0)