PEP 3147 implementation - Code Review (original) (raw)

OLD

NEW

1

1

2 /* Module definition and import implementation */

2 /* Module definition and import implementation */

3

3

4 #include "Python.h"

4 #include "Python.h"

5

5

6 #include "Python-ast.h"

6 #include "Python-ast.h"

7 #undef Yield /* undefine macro conflicting with winbase.h */

7 #undef Yield /* undefine macro conflicting with winbase.h */

8 #include "pyarena.h"

8 #include "pyarena.h"

9 #include "pythonrun.h"

9 #include "pythonrun.h"

10 #include "errcode.h"

10 #include "errcode.h"

(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading...

87 Python 3.0a4: 3111 (WITH_CLEANUP optimization).

87 Python 3.0a4: 3111 (WITH_CLEANUP optimization).

88 Python 3.0a5: 3131 (lexical exception stacking, including POP_EXCEPT)

88 Python 3.0a5: 3131 (lexical exception stacking, including POP_EXCEPT)

89 Python 3.1a0: 3141 (optimize list, set and dict comprehensions:

89 Python 3.1a0: 3141 (optimize list, set and dict comprehensions:

90 change LIST_APPEND and SET_ADD, add MAP_ADD)

90 change LIST_APPEND and SET_ADD, add MAP_ADD)

91 Python 3.1a0: 3151 (optimize conditional branches:

91 Python 3.1a0: 3151 (optimize conditional branches:

92 introduce POP_JUMP_IF_FALSE and POP_JUMP_IF_TRUE)

92 introduce POP_JUMP_IF_FALSE and POP_JUMP_IF_TRUE)

93 Python 3.2a0: 3160 (add SETUP_WITH)

93 Python 3.2a0: 3160 (add SETUP_WITH)

94 */

94 */

95

95

96 #define MAGIC (3160 | ((long)'\r'<<16) | ((long)'\n'<<24))

96 #define MAGIC (3160 | ((long)'\r'<<16) | ((long)'\n'<<24))

97 /* Magic word as global */

97 #define TAG "cpython-32"

98 #define CACHEDIR "__pycache__"

99 /* Current magic word and string tag as globals. */

98 static long pyc_magic = MAGIC;

100 static long pyc_magic = MAGIC;

101 static char *pyc_tag = TAG;

102 static char *pyc_dir = CACHEDIR;

103

104 /* Beginning with Python 3.2, PEP 3147 is implemented. All magic numbers are

105 associated with a string tag. These tags are used in pyc file names placed

106 inside the __pycache__ directories. Whenever you bump the magic number

107 for this version of Python, you need to append a new tag to the array

108 below. Do not ever remove a mapping. The current one can always use the

109 macros.

110

111 By convention, the syntax of the tags are -,

112 e.g. cpython-32. So for a source file foo.py, the pyc would be written to

113 __pycache__/foo.cpython-32.pyc

114 */

115 static struct pymagics _PyMagicNumberTags[] = {

116 {MAGIC, TAG},

117 {0, 0}

118 };

99

119

100 /* See _PyImport_FixupExtension() below */

120 /* See _PyImport_FixupExtension() below */

101 static PyObject *extensions = NULL;

121 static PyObject *extensions = NULL;

102

122

103 /* This table is defined in config.c: */

123 /* This table is defined in config.c: */

104 extern struct _inittab _PyImport_Inittab[];

124 extern struct _inittab _PyImport_Inittab[];

105

125

106 /* Method from Parser/tokenizer.c */

126 /* Method from Parser/tokenizer.c */

107 extern char * PyTokenizer_FindEncoding(int);

127 extern char * PyTokenizer_FindEncoding(int);

108

128

(...skipping 632 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading...

741

761

742

762

743 /* Given a pathname for a Python source file, fill a buffer with the

763 /* Given a pathname for a Python source file, fill a buffer with the

744 pathname for the corresponding compiled file. Return the pathname

764 pathname for the corresponding compiled file. Return the pathname

745 for the compiled file, or NULL if there's no space in the buffer.

765 for the compiled file, or NULL if there's no space in the buffer.

746 Doesn't set an exception. */

766 Doesn't set an exception. */

747

767

748 static char *

768 static char *

749 make_compiled_pathname(char *pathname, char *buf, size_t buflen)

769 make_compiled_pathname(char *pathname, char *buf, size_t buflen)

750 {

770 {

771 /* foo.py -> __pycache__/foo..pyc */

751 size_t len = strlen(pathname);

772 size_t len = strlen(pathname);

752 » if (len+2 > buflen)

773 » size_t i, save;

774 char *pos;

775

776 /* Sanity check that the buffer has enough space to hold what will

777 eventually be the full path to the compiled file. The 4 extra

778 bytes include the slash afer __pycache__, the two extra dots, the

779 extra trailing character ('c' or 'o') and null.

780 */

781 if (len + strlen(CACHEDIR) + strlen(pyc_tag) + 4 >= buflen)

753 return NULL;

782 return NULL;

754

783

755 #ifdef MS_WINDOWS

784 » /* Find the last path separator and copy everything from the start of

756 » /* Treat .pyw as if it were .py. The case of ".pyw" must match

785 » the source string up to and including the separator.

757 » that used in _PyImport_StandardFiletab. */

786 » */

758 » if (len >= 4 && strcmp(&pathname[len-4], ".pyw") == 0)

787 » if ((pos = strrchr(pathname, SEP)) == NULL) {

759 » » --len;» /* pretend 'w' isn't there */

788 » » i = 0;

760 #endif

789 » }

761 » memcpy(buf, pathname, len);

790 » else {

762 » buf[len] = Py_OptimizeFlag ? 'o' : 'c';

791 » » i = pos - pathname + 1;

763 » buf[len+1] = '\0';

792 » » strncpy(buf, pathname, i);

793 » }

794

795 » save = i;

796 » buf[i++] = '\0';

797 » /* Add __pycache__/ */

798 » strcat(buf, pyc_dir);

799 » i += strlen(pyc_dir) - 1;

800 » buf[i++] = SEP;

801 » buf[i++] = '\0';

802 » /* Add the base filename, but remove the .py or .pyw extension, since

803 » the tag name must go before the extension.

804 » */

805 » strcat(buf, pathname + save);

806 » if ((pos = strrchr(buf, '.')) != NULL)

807 » » *++pos = '\0';

808 » strcat(buf, pyc_tag);

809 » strcat(buf, Py_OptimizeFlag ? ".pyo" : ".pyc");

764

810

765 return buf;

811 return buf;

766 }

812 }

767

813

768

814

815 /* Given a pathname to a Python byte compiled file, return the path to the

816 source file, if the path matches the PEP 3147 format. This does not check

817 for any file existence, however, if the pyc file name does not match PEP

818 3147 style, NULL is returned. buf must be at least as big as pathname;

819 the resulting path will always be shorter. */

820 ···

821 static char *

822 make_source_pathname(char *pathname, char *buf)

823 {

824 /* __pycache__/foo..pyc -> foo.py */

825 size_t i, j;

826 char *left, *right, *dot0, *dot1;

827

828 /* Look back two slashes from the end. In between these two slashes

829 must be the string __pycache__ or this is not a PEP 3147 style

830 path. It's possible for there to be only one slash.

831 */

832 if ((right = strrchr(pathname, SEP)) == NULL)

833 return NULL;

834 *right = '\0';

835 left = strrchr(pathname, SEP);

836 *right = SEP;

837 if (left == NULL)

838 left = pathname;

839 else

840 left++;

841 if (right-left != 11 || strncmp(left, "__pycache__", right-left) != 0)

842 return NULL;

843

844 /* Now verify that the path component to the right of the last slash

845 has two dots in it.

846 */

847 if ((dot0 = strchr(right+1, '.')) == NULL)

848 return NULL;

849 if ((dot1 = strchr(dot0+1, '.')) == NULL)

850 return NULL;

851 /* Too many dots? */

852 if (strchr(dot1+1, '.') != NULL)

853 return NULL;

854

855 /* This is a PEP 3147 path. Start by copying everything from the

856 start of pathname up to and including the leftmost slash. Then

857 copy the file's basename, removing the magic tag and adding a .py

858 suffix.

859 */

860 strncpy(buf, pathname, (i=left-pathname));

861 strncpy(buf+i, right+1, (j=dot0-right));

862 strcpy(buf+i+j, "py");

863 return buf;

864 }

865

769 /* Given a pathname for a Python source file, its time of last

866 /* Given a pathname for a Python source file, its time of last

770 modification, and a pathname for a compiled file, check whether the

867 modification, and a pathname for a compiled file, check whether the

771 compiled file represents the same version of the source. If so,

868 compiled file represents the same version of the source. If so,

772 return a FILE pointer for the compiled file, positioned just after

869 return a FILE pointer for the compiled file, positioned just after

773 the header; if not, return NULL.

870 the header; if not, return NULL.

774 Doesn't set an exception. */

871 Doesn't set an exception. */

775

872

776 static FILE *

873 static FILE *

777 check_compiled_module(char *pathname, time_t mtime, char *cpathname)

874 check_compiled_module(char *pathname, time_t mtime, char *cpathname)

778 {

875 {

(...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading...

912

1009

913 /* Write a compiled module to a file, placing the time of last

1010 /* Write a compiled module to a file, placing the time of last

914 modification of its source into the header.

1011 modification of its source into the header.

915 Errors are ignored, if a write error occurs an attempt is made to

1012 Errors are ignored, if a write error occurs an attempt is made to

916 remove the file. */

1013 remove the file. */

917

1014

918 static void

1015 static void

919 write_compiled_module(PyCodeObject *co, char *cpathname, struct stat *srcstat)

1016 write_compiled_module(PyCodeObject *co, char *cpathname, struct stat *srcstat)

920 {

1017 {

921 FILE *fp;

1018 FILE *fp;

1019 char *dirpath;

922 time_t mtime = srcstat->st_mtime;

1020 time_t mtime = srcstat->st_mtime;

923 #ifdef MS_WINDOWS /* since Windows uses different permissions */

1021 #ifdef MS_WINDOWS /* since Windows uses different permissions */

924 mode_t mode = srcstat->st_mode & ~S_IEXEC;

1022 mode_t mode = srcstat->st_mode & ~S_IEXEC;

1023 mode_t dirmode = srcstat->st_mode | S_IEXEC; /* XXX */

925 #else

1024 #else

926 mode_t mode = srcstat->st_mode & ~S_IXUSR & ~S_IXGRP & ~S_IXOTH;

1025 mode_t mode = srcstat->st_mode & ~S_IXUSR & ~S_IXGRP & ~S_IXOTH;

1026 mode_t dirmode = (srcstat->st_mode |

1027 S_IXUSR | S_IXGRP | S_IXOTH |

1028 S_IWUSR | S_IWGRP | S_IWOTH);

927 #endif

1029 #endif

928

1030

1031 /* Ensure that the __pycache__ directory exists. */

1032 dirpath = strrchr(cpathname, '/');

1033 if (dirpath == NULL) {

1034 if (Py_VerboseFlag)

1035 PySys_WriteStderr(

1036 "# no __pycache__ path found %s\n", cpathname);

1037 return;

1038 }

1039 *dirpath = '\0';

1040 if (mkdir(cpathname, dirmode) < 0 && errno != EEXIST) {

1041 *dirpath = '/';

1042 if (Py_VerboseFlag)

1043 PySys_WriteStderr(

1044 "# cannot create cache dir %s\n", cpathname);

1045 return;

1046 }

1047 *dirpath = '/';

1048

929 fp = open_exclusive(cpathname, mode);

1049 fp = open_exclusive(cpathname, mode);

930 if (fp == NULL) {

1050 if (fp == NULL) {

931 if (Py_VerboseFlag)

1051 if (Py_VerboseFlag)

932 PySys_WriteStderr(

1052 PySys_WriteStderr(

933 "# can't create %s\n", cpathname);

1053 "# can't create %s\n", cpathname);

934 return;

1054 return;

935 }

1055 }

936 PyMarshal_WriteLongToFile(pyc_magic, fp, Py_MARSHAL_VERSION);

1056 PyMarshal_WriteLongToFile(pyc_magic, fp, Py_MARSHAL_VERSION);

937 /* First write a 0 for mtime */

1057 /* First write a 0 for mtime */

938 PyMarshal_WriteLongToFile(0L, fp, Py_MARSHAL_VERSION);

1058 PyMarshal_WriteLongToFile(0L, fp, Py_MARSHAL_VERSION);

(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading...

1080 if (!file || !*file) {

1200 if (!file || !*file) {

1081 Py_RETURN_NONE;

1201 Py_RETURN_NONE;

1082 }

1202 }

1083

1203

1084 len = strlen(file);

1204 len = strlen(file);

1085 /* match '*.py?' */

1205 /* match '*.py?' */

1086 if (len > MAXPATHLEN || PyOS_strnicmp(&file[len-4], ".py", 3) != 0) {

1206 if (len > MAXPATHLEN || PyOS_strnicmp(&file[len-4], ".py", 3) != 0) {

1087 return PyUnicode_DecodeFSDefault(file);

1207 return PyUnicode_DecodeFSDefault(file);

1088 }

1208 }

1089

1209

1090 » strncpy(py, file, len-1);

1210 /* Start by trying to turn PEP 3147 path into source path. If that

1091 » py[len-1] = '\0';

1211 * fails, just chop off the trailing character, i.e. legacy pyc path

1212 * to py.

1213 */

1214 » if (make_source_pathname((char *)file, py) == NULL) {

1215 » » strncpy(py, file, len-1);

1216 » » py[len-1] = '\0';

1217 » }

1218

1092 if (stat(py, &statbuf) == 0 &&

1219 if (stat(py, &statbuf) == 0 &&

1093 S_ISREG(statbuf.st_mode)) {

1220 S_ISREG(statbuf.st_mode)) {

1094 u = PyUnicode_DecodeFSDefault(py);

1221 u = PyUnicode_DecodeFSDefault(py);

1095 }

1222 }

1096 else {

1223 else {

1097 u = PyUnicode_DecodeFSDefault(file);

1224 u = PyUnicode_DecodeFSDefault(file);

1098 }

1225 }

1099 return u;

1226 return u;

1100 }

1227 }

1101

1228

(...skipping 1704 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading...

2806

2933

2807 return r;

2934 return r;

2808 }

2935 }

2809

2936

2810

2937

2811 /* Module 'imp' provides Python access to the primitives used for

2938 /* Module 'imp' provides Python access to the primitives used for

2812 importing modules.

2939 importing modules.

2813 */

2940 */

2814

2941

2815 static PyObject *

2942 static PyObject *

2943 imp_make_magic(long magic)

2944 {

2945 char buf[4];

2946

2947 buf[0] = (char) ((magic >> 0) & 0xff);

2948 buf[1] = (char) ((magic >> 8) & 0xff);

2949 buf[2] = (char) ((magic >> 16) & 0xff);

2950 buf[3] = (char) ((magic >> 24) & 0xff);

2951

2952 return PyBytes_FromStringAndSize(buf, 4);

2953 };······

2954

2955 static PyObject *

2816 imp_get_magic(PyObject *self, PyObject *noargs)

2956 imp_get_magic(PyObject *self, PyObject *noargs)

2817 {

2957 {

2818 » char buf[4];

2958 » return imp_make_magic(pyc_magic);

2819

2820 » buf[0] = (char) ((pyc_magic >> 0) & 0xff);

2821 » buf[1] = (char) ((pyc_magic >> 8) & 0xff);

2822 » buf[2] = (char) ((pyc_magic >> 16) & 0xff);

2823 » buf[3] = (char) ((pyc_magic >> 24) & 0xff);

2824

2825 » return PyBytes_FromStringAndSize(buf, 4);

2826 }

2959 }

2827

2960

2828 static PyObject *

2961 static PyObject *

2829 imp_get_suffixes(PyObject *self, PyObject *noargs)

2962 imp_get_suffixes(PyObject *self, PyObject *noargs)

2830 {

2963 {

2831 PyObject *list;

2964 PyObject *list;

2832 struct filedescr *fdp;

2965 struct filedescr *fdp;

2833

2966

2834 list = PyList_New(0);

2967 list = PyList_New(0);

2835 if (list == NULL)

2968 if (list == NULL)

(...skipping 347 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading...

3183 imp_reload(PyObject *self, PyObject *v)

3316 imp_reload(PyObject *self, PyObject *v)

3184 {

3317 {

3185 return PyImport_ReloadModule(v);

3318 return PyImport_ReloadModule(v);

3186 }

3319 }

3187

3320

3188 PyDoc_STRVAR(doc_reload,

3321 PyDoc_STRVAR(doc_reload,

3189 "reload(module) -> module\n\

3322 "reload(module) -> module\n\

3190 \n\

3323 \n\

3191 Reload the module. The module must have been successfully imported before.");

3324 Reload the module. The module must have been successfully imported before.");

3192

3325

3326 static PyObject *

3327 imp_cache_from_source(PyObject *self, PyObject *args, PyObject *kws)

3328 {

3329 static char *kwlist[] = {"path", "force__debug__", NULL};

3330

3331 char buf[MAXPATHLEN+1];

3332 char *pathname, *cpathname;

3333 PyObject *force__debug__ = Py_None;

3334 int oldflag = Py_OptimizeFlag;;

3335

3336 if (!PyArg_ParseTupleAndKeywords(

3337 args, kws, "es|O", kwlist,

3338 Py_FileSystemDefaultEncoding, &pathname, &force__debug__))

3339 return NULL;

3340

3341 /* Hack the Py_OptimizeFlag based on whether __debug__ is forced or

3342 not.

3343 */

3344 if (force__debug__ != Py_None) {

3345 if (force__debug__ == Py_True)

3346 Py_OptimizeFlag = 0;

3347 else if (force__debug__ == Py_False)

3348 Py_OptimizeFlag = 1;

3349 else {

3350 PyErr_Format(PyExc_ValueError,

3351 "force__debug__ must be True or False");

3352 PyMem_Free(pathname);

3353 return NULL;

3354 }

3355 }

3356

3357 cpathname = make_compiled_pathname(pathname, buf, MAXPATHLEN+1);

3358 PyMem_Free(pathname);

3359 Py_OptimizeFlag = oldflag;

3360

3361 if (cpathname == NULL)

3362 return NULL;

3363 return PyUnicode_FromString(buf);

3364 }

3365

3366 PyDoc_STRVAR(doc_cache_from_source,

3367 "Given the path to a .py file, return the path to its .pyc/.pyo file.\n\

3368 \n\

3369 The .py file does not need to exist; this simply returns the path to the\n\

3370 .pyc/.py file calculated as if the .py file were imported. The extension\n\

3371 will be .pyc unless __debug__ is defined, then it will be .pyo.\n\

3372 \n\

3373 If force__debug__ is not None, then it must be a boolean and is taken as\n\

3374 the value of __debug__ instead.");

3375

3376 static PyObject *

3377 imp_source_from_cache(PyObject *self, PyObject *args, PyObject *kws)

3378 {

3379 static char *kwlist[] = {"path", NULL};

3380

3381 char *pathname;

3382 char buf[MAXPATHLEN+1];

3383

3384 if (!PyArg_ParseTupleAndKeywords(

3385 args, kws, "es", kwlist,

3386 Py_FileSystemDefaultEncoding, &pathname))

3387 return NULL;

3388

3389 if (make_source_pathname(pathname, buf) == NULL) {

3390 PyMem_Free(pathname);

3391 Py_INCREF(Py_None);

3392 return Py_None;

3393 }

3394 PyMem_Free(pathname);

3395 return PyUnicode_FromString(buf);

3396 }

3397

3398 PyDoc_STRVAR(doc_source_from_cache,

3399 "Given the path to a .pyc./.pyo file, return the path to its .py file.\n\

3400 \n\

3401 The .pyc/.pyo file does not need to exist; this simply returns the path to\n\

3402 the .py file calculated to correspond to the .pyc/.pyo file. If path\n\

3403 does not conform to PEP 3147 format, None will be returned.");

3404

3193 /* Doc strings */

3405 /* Doc strings */

3194

3406

3195 PyDoc_STRVAR(doc_imp,

3407 PyDoc_STRVAR(doc_imp,

3196 "This module provides the components needed to build your own\n\

3408 "This module provides the components needed to build your own\n\

3197 __import__ function. Undocumented functions are obsolete.");

3409 __import__ function. Undocumented functions are obsolete.");

3198

3410

3199 PyDoc_STRVAR(doc_find_module,

3411 PyDoc_STRVAR(doc_find_module,

3200 "find_module(name, [path]) -> (file, filename, (suffix, mode, type))\n\

3412 "find_module(name, [path]) -> (file, filename, (suffix, mode, type))\n\

3201 Search for a module. If path is omitted or None, search for a\n\

3413 Search for a module. If path is omitted or None, search for a\n\

3202 built-in, frozen or special module and continue search in sys.path.\n\

3414 built-in, frozen or special module and continue search in sys.path.\n\

(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading...

3242 static PyMethodDef imp_methods[] = {

3454 static PyMethodDef imp_methods[] = {

3243 {"find_module", imp_find_module, METH_VARARGS, doc_find_module},

3455 {"find_module", imp_find_module, METH_VARARGS, doc_find_module},

3244 {"get_magic", imp_get_magic, METH_NOARGS, doc_get_magic},

3456 {"get_magic", imp_get_magic, METH_NOARGS, doc_get_magic},

3245 {"get_suffixes", imp_get_suffixes, METH_NOARGS, doc_get_suffixes},

3457 {"get_suffixes", imp_get_suffixes, METH_NOARGS, doc_get_suffixes},

3246 {"load_module", imp_load_module, METH_VARARGS, doc_load_module},

3458 {"load_module", imp_load_module, METH_VARARGS, doc_load_module},

3247 {"new_module", imp_new_module, METH_VARARGS, doc_new_module},

3459 {"new_module", imp_new_module, METH_VARARGS, doc_new_module},

3248 {"lock_held", imp_lock_held, METH_NOARGS, doc_lock_held},

3460 {"lock_held", imp_lock_held, METH_NOARGS, doc_lock_held},

3249 {"acquire_lock", imp_acquire_lock, METH_NOARGS, doc_acquire_lock},

3461 {"acquire_lock", imp_acquire_lock, METH_NOARGS, doc_acquire_lock},

3250 {"release_lock", imp_release_lock, METH_NOARGS, doc_release_lock},

3462 {"release_lock", imp_release_lock, METH_NOARGS, doc_release_lock},

3251 {"reload", imp_reload, METH_O, doc_reload},

3463 {"reload", imp_reload, METH_O, doc_reload},

3464 {"cache_from_source", (PyCFunction)imp_cache_from_source,

3465 METH_VARARGS | METH_KEYWORDS, doc_cache_from_source},

3466 {"source_from_cache", (PyCFunction)imp_source_from_cache,

3467 METH_VARARGS | METH_KEYWORDS, doc_source_from_cache},

3252 /* The rest are obsolete */

3468 /* The rest are obsolete */

3253 {"get_frozen_object", imp_get_frozen_object, METH_VARARGS},

3469 {"get_frozen_object", imp_get_frozen_object, METH_VARARGS},

3254 {"is_frozen_package", imp_is_frozen_package, METH_VARARGS},

3470 {"is_frozen_package", imp_is_frozen_package, METH_VARARGS},

3255 {"init_builtin", imp_init_builtin, METH_VARARGS},

3471 {"init_builtin", imp_init_builtin, METH_VARARGS},

3256 {"init_frozen", imp_init_frozen, METH_VARARGS},

3472 {"init_frozen", imp_init_frozen, METH_VARARGS},

3257 {"is_builtin", imp_is_builtin, METH_VARARGS},

3473 {"is_builtin", imp_is_builtin, METH_VARARGS},

3258 {"is_frozen", imp_is_frozen, METH_VARARGS},

3474 {"is_frozen", imp_is_frozen, METH_VARARGS},

3259 {"load_compiled", imp_load_compiled, METH_VARARGS},

3475 {"load_compiled", imp_load_compiled, METH_VARARGS},

3260 #ifdef HAVE_DYNAMIC_LOADING

3476 #ifdef HAVE_DYNAMIC_LOADING

3261 {"load_dynamic", imp_load_dynamic, METH_VARARGS},

3477 {"load_dynamic", imp_load_dynamic, METH_VARARGS},

(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading...

3400 imp_methods,

3616 imp_methods,

3401 NULL,

3617 NULL,

3402 NULL,

3618 NULL,

3403 NULL,

3619 NULL,

3404 NULL

3620 NULL

3405 };

3621 };

3406

3622

3407 PyMODINIT_FUNC

3623 PyMODINIT_FUNC

3408 PyInit_imp(void)

3624 PyInit_imp(void)

3409 {

3625 {

3410 » PyObject *m, *d;

3626 » PyObject *m, *d, *magics = NULL;

3627 » struct pymagics *t;

3411

3628

3412 if (PyType_Ready(&PyNullImporter_Type) < 0)

3629 if (PyType_Ready(&PyNullImporter_Type) < 0)

3413 return NULL;

3630 return NULL;

3414

3631

3415 m = PyModule_Create(&impmodule);

3632 m = PyModule_Create(&impmodule);

3416 if (m == NULL)

3633 if (m == NULL)

3417 goto failure;

3634 goto failure;

3418 d = PyModule_GetDict(m);

3635 d = PyModule_GetDict(m);

3419 if (d == NULL)

3636 if (d == NULL)

3420 goto failure;

3637 goto failure;

3421

3638

3422 if (setint(d, "SEARCH_ERROR", SEARCH_ERROR) < 0) goto failure;

3639 if (setint(d, "SEARCH_ERROR", SEARCH_ERROR) < 0) goto failure;

3423 if (setint(d, "PY_SOURCE", PY_SOURCE) < 0) goto failure;

3640 if (setint(d, "PY_SOURCE", PY_SOURCE) < 0) goto failure;

3424 if (setint(d, "PY_COMPILED", PY_COMPILED) < 0) goto failure;

3641 if (setint(d, "PY_COMPILED", PY_COMPILED) < 0) goto failure;

3425 if (setint(d, "C_EXTENSION", C_EXTENSION) < 0) goto failure;

3642 if (setint(d, "C_EXTENSION", C_EXTENSION) < 0) goto failure;

3426 if (setint(d, "PY_RESOURCE", PY_RESOURCE) < 0) goto failure;

3643 if (setint(d, "PY_RESOURCE", PY_RESOURCE) < 0) goto failure;

3427 if (setint(d, "PKG_DIRECTORY", PKG_DIRECTORY) < 0) goto failure;

3644 if (setint(d, "PKG_DIRECTORY", PKG_DIRECTORY) < 0) goto failure;

3428 if (setint(d, "C_BUILTIN", C_BUILTIN) < 0) goto failure;

3645 if (setint(d, "C_BUILTIN", C_BUILTIN) < 0) goto failure;

3429 if (setint(d, "PY_FROZEN", PY_FROZEN) < 0) goto failure;

3646 if (setint(d, "PY_FROZEN", PY_FROZEN) < 0) goto failure;

3430 if (setint(d, "PY_CODERESOURCE", PY_CODERESOURCE) < 0) goto failure;

3647 if (setint(d, "PY_CODERESOURCE", PY_CODERESOURCE) < 0) goto failure;

3431 if (setint(d, "IMP_HOOK", IMP_HOOK) < 0) goto failure;

3648 if (setint(d, "IMP_HOOK", IMP_HOOK) < 0) goto failure;

3432

3649

3433 Py_INCREF(&PyNullImporter_Type);

3650 Py_INCREF(&PyNullImporter_Type);

3434 PyModule_AddObject(m, "NullImporter", (PyObject *)&PyNullImporter_Type);

3651 PyModule_AddObject(m, "NullImporter", (PyObject *)&PyNullImporter_Type);

3652 /* Set up magic number tag mappings. */

3653 if ((magics = PyDict_New()) == NULL) goto failure;

3654 if (PyModule_AddObject(m, "magic_tags", magics) < 0) goto failure;

3655 for (t = _PyMagicNumberTags; t->tag != NULL; ++t) {

3656 int result;

3657 PyObject *number = imp_make_magic(t->number);

3658 PyObject *tag = PyUnicode_FromString(t->tag);

3659

3660 if (number == NULL || tag == NULL) {

3661 Py_XDECREF(number);

3662 Py_XDECREF(tag);

3663 goto failure;

3664 }

3665 result = PyDict_SetItem(magics, number, tag);

3666 Py_DECREF(number);

3667 Py_DECREF(tag);

3668 if (result < 0) goto failure;

3669 }

3435 return m;

3670 return m;

3436 failure:

3671 failure:

3437 Py_XDECREF(m);

3672 Py_XDECREF(m);

3673 Py_XDECREF(magics);

3438 return NULL;

3674 return NULL;

3439

3440 }

3675 }

3441

3676

3442

3677

3443 /* API for embedding applications that want to add their own entries

3678 /* API for embedding applications that want to add their own entries

3444 to the table of built-in modules. This should normally be called

3679 to the table of built-in modules. This should normally be called

3445 *before* Py_Initialize(). When the table resize fails, -1 is

3680 *before* Py_Initialize(). When the table resize fails, -1 is

3446 returned and the existing table is unchanged.

3681 returned and the existing table is unchanged.

3447

3682

3448 After a similar function by Just van Rossum. */

3683 After a similar function by Just van Rossum. */

3449

3684

(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading...

3488

3723

3489 newtab[0].name = (char *)name;

3724 newtab[0].name = (char *)name;

3490 newtab[0].initfunc = initfunc;

3725 newtab[0].initfunc = initfunc;

3491

3726

3492 return PyImport_ExtendInittab(newtab);

3727 return PyImport_ExtendInittab(newtab);

3493 }

3728 }

3494

3729

3495 #ifdef __cplusplus

3730 #ifdef __cplusplus

3496 }

3731 }

3497 #endif

3732 #endif

OLD

NEW