Left: | ||
Right: |
LEFT | RIGHT |
---|---|
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 28 matching lines...) Expand all Loading... | |
39 -U interpeter flag will cause MAGIC+1 being used. They have been | 39 -U interpeter flag will cause MAGIC+1 being used. They have been |
40 odd numbers for some time now. | 40 odd numbers for some time now. |
41 | 41 |
42 There were a variety of old schemes for setting the magic number. | 42 There were a variety of old schemes for setting the magic number. |
43 The current working scheme is to increment the previous value by | 43 The current working scheme is to increment the previous value by |
44 10. | 44 10. |
45 | 45 |
46 Starting with the adoption of PEP 3147 in Python 3.2, every bump in magic | 46 Starting with the adoption of PEP 3147 in Python 3.2, every bump in magic |
47 number also includes a new "magic tag", i.e. a human readable string used | 47 number also includes a new "magic tag", i.e. a human readable string used |
48 to represent the magic number in __pycache__ directories. When you change | 48 to represent the magic number in __pycache__ directories. When you change |
49 the magic number, you must insert the old value in the _PyMagicNumberTags | 49 the magic number, you must also set a new unique magic tag. Generally this |
50 array. The tags are included in the following table, starting with Python | 50 can be named after the Python major version of the magic number bump, but |
51 3.2a0. | 51 it can really be anything, as long as it's different than anything else |
52 that's come before. The tags are included in the following table, starting | |
53 with Python 3.2a0. | |
52 | 54 |
53 Known values: | 55 Known values: |
54 Python 1.5: 20121 | 56 Python 1.5: 20121 |
55 Python 1.5.1: 20121 | 57 Python 1.5.1: 20121 |
56 Python 1.5.2: 20121 | 58 Python 1.5.2: 20121 |
57 Python 1.6: 50428 | 59 Python 1.6: 50428 |
58 Python 2.0: 50823 | 60 Python 2.0: 50823 |
59 Python 2.0.1: 50823 | 61 Python 2.0.1: 50823 |
60 Python 2.1: 60202 | 62 Python 2.1: 60202 |
61 Python 2.1.1: 60202 | 63 Python 2.1.1: 60202 |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
104 /* If you change MAGIC, you must change TAG and you must insert the old value | 106 /* If you change MAGIC, you must change TAG and you must insert the old value |
105 into _PyMagicNumberTags below. | 107 into _PyMagicNumberTags below. |
106 */ | 108 */ |
107 #define MAGIC (3160 | ((long)'\r'<<16) | ((long)'\n'<<24)) | 109 #define MAGIC (3160 | ((long)'\r'<<16) | ((long)'\n'<<24)) |
108 #define TAG "cpython-32" | 110 #define TAG "cpython-32" |
109 #define CACHEDIR "__pycache__" | 111 #define CACHEDIR "__pycache__" |
110 /* Current magic word and string tag as globals. */ | 112 /* Current magic word and string tag as globals. */ |
111 static long pyc_magic = MAGIC; | 113 static long pyc_magic = MAGIC; |
112 static const char *pyc_tag = TAG; | 114 static const char *pyc_tag = TAG; |
113 | 115 |
114 /* Beginning with Python 3.2, PEP 3147 is implemented. All magic numbers are | |
115 associated with a string tag. These tags are used in pyc file names placed | |
116 inside the __pycache__ directories. Whenever you bump the magic number | |
117 for this version of Python, you need to append a new tag to the array | |
118 below. Do not ever remove a mapping. The current one can always use the | |
119 macros. | |
120 | |
121 By convention, the syntax of the tags are <flavor>-<major><minor>, | |
122 e.g. cpython-32. So for a source file foo.py, the pyc would be written to | |
123 __pycache__/foo.cpython-32.pyc | |
124 */ | |
125 | |
126 /* Mapping from magic numbers to magic string tags. */ | |
127 static struct pymagics { | |
128 long number; | |
129 char *tag; | |
130 } _PyMagicNumberTags[] = { | |
131 /* When MAGIC and TAG are bumped, insert the old value here. */ | |
132 {MAGIC, TAG}, | |
133 {0, 0} | |
134 }; | |
135 | |
136 /* See _PyImport_FixupExtension() below */ | 116 /* See _PyImport_FixupExtension() below */ |
137 static PyObject *extensions = NULL; | 117 static PyObject *extensions = NULL; |
138 | 118 |
139 /* This table is defined in config.c: */ | 119 /* This table is defined in config.c: */ |
140 extern struct _inittab _PyImport_Inittab[]; | 120 extern struct _inittab _PyImport_Inittab[]; |
141 | 121 |
142 /* Method from Parser/tokenizer.c */ | 122 /* Method from Parser/tokenizer.c */ |
143 extern char * PyTokenizer_FindEncoding(int); | 123 extern char * PyTokenizer_FindEncoding(int); |
144 | 124 |
145 struct _inittab *PyImport_Inittab = _PyImport_Inittab; | 125 struct _inittab *PyImport_Inittab = _PyImport_Inittab; |
(...skipping 400 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
546 } | 526 } |
547 | 527 |
548 /* Finally, clear and delete the modules directory */ | 528 /* Finally, clear and delete the modules directory */ |
549 PyDict_Clear(modules); | 529 PyDict_Clear(modules); |
550 interp->modules = NULL; | 530 interp->modules = NULL; |
551 Py_DECREF(modules); | 531 Py_DECREF(modules); |
552 Py_CLEAR(interp->modules_reloading); | 532 Py_CLEAR(interp->modules_reloading); |
553 } | 533 } |
554 | 534 |
555 | 535 |
556 /* Helper for pythonrun.c -- return magic number */ | 536 /* Helper for pythonrun.c -- return magic number and tag. */ |
557 | 537 |
558 long | 538 long |
559 PyImport_GetMagicNumber(void) | 539 PyImport_GetMagicNumber(void) |
560 { | 540 { |
561 return pyc_magic; | 541 return pyc_magic; |
562 } | 542 } |
563 | 543 |
544 | |
545 const char * | |
546 PyImport_GetMagicTag(void) | |
547 { | |
548 return pyc_tag; | |
549 } | |
564 | 550 |
565 /* Magic for extension modules (built-in as well as dynamically | 551 /* Magic for extension modules (built-in as well as dynamically |
566 loaded). To prevent initializing an extension module more than | 552 loaded). To prevent initializing an extension module more than |
567 once, we keep a static dictionary 'extensions' keyed by module name | 553 once, we keep a static dictionary 'extensions' keyed by module name |
568 (for built-in modules) or by filename (for dynamically loaded | 554 (for built-in modules) or by filename (for dynamically loaded |
569 modules), containing these modules. A copy of the module's | 555 modules), containing these modules. A copy of the module's |
570 dictionary is stored by calling _PyImport_FixupExtension() | 556 dictionary is stored by calling _PyImport_FixupExtension() |
571 immediately after the module initialization function succeeds. A | 557 immediately after the module initialization function succeeds. A |
572 copy can be retrieved from there by calling | 558 copy can be retrieved from there by calling |
573 _PyImport_FindExtension(). | 559 _PyImport_FindExtension(). |
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
701 { | 687 { |
702 PyObject *modules = PyImport_GetModuleDict(); | 688 PyObject *modules = PyImport_GetModuleDict(); |
703 if (PyDict_GetItemString(modules, name) == NULL) | 689 if (PyDict_GetItemString(modules, name) == NULL) |
704 return; | 690 return; |
705 if (PyDict_DelItemString(modules, name) < 0) | 691 if (PyDict_DelItemString(modules, name) < 0) |
706 Py_FatalError("import: deleting existing key in" | 692 Py_FatalError("import: deleting existing key in" |
707 "sys.modules failed"); | 693 "sys.modules failed"); |
708 } | 694 } |
709 | 695 |
710 static PyObject * get_sourcefile(char *file); | 696 static PyObject * get_sourcefile(char *file); |
697 static char *make_source_pathname(char *pathname, char *buf); | |
698 static char *make_compiled_pathname(char *pathname, char *buf, size_t buflen, | |
699 int debug); | |
711 | 700 |
712 /* Execute a code object in a module and return the module object | 701 /* Execute a code object in a module and return the module object |
713 * WITH INCREMENTED REFERENCE COUNT. If an error occurs, name is | 702 * WITH INCREMENTED REFERENCE COUNT. If an error occurs, name is |
714 * removed from sys.modules, to avoid leaving damaged module objects | 703 * removed from sys.modules, to avoid leaving damaged module objects |
715 * in sys.modules. The caller may wish to restore the original | 704 * in sys.modules. The caller may wish to restore the original |
716 * module object (if any) in this case; PyImport_ReloadModule is an | 705 * module object (if any) in this case; PyImport_ReloadModule is an |
717 * example. | 706 * example. |
707 * | |
708 * Note that PyImport_ExecCodeModuleWithPathnames() is the preferred, richer | |
709 * interface. The other two exist primarily for backward compatibility. | |
718 */ | 710 */ |
719 PyObject * | 711 PyObject * |
720 PyImport_ExecCodeModule(char *name, PyObject *co) | 712 PyImport_ExecCodeModule(char *name, PyObject *co) |
721 { | 713 { |
722 » return PyImport_ExecCodeModuleEx(name, co, (char *)NULL); | 714 » return PyImport_ExecCodeModuleWithPathnames( |
715 » » name, co, (char *)NULL, (char *)NULL); | |
723 } | 716 } |
724 | 717 |
725 PyObject * | 718 PyObject * |
726 PyImport_ExecCodeModuleEx(char *name, PyObject *co, char *pathname) | 719 PyImport_ExecCodeModuleEx(char *name, PyObject *co, char *pathname) |
720 { | |
721 return PyImport_ExecCodeModuleWithPathnames( | |
722 name, co, pathname, (char *)NULL); | |
723 } | |
724 | |
725 PyObject * | |
726 PyImport_ExecCodeModuleWithPathnames(char *name, PyObject *co, char *pathname, | |
727 char *cpathname) | |
727 { | 728 { |
728 PyObject *modules = PyImport_GetModuleDict(); | 729 PyObject *modules = PyImport_GetModuleDict(); |
729 PyObject *m, *d, *v; | 730 PyObject *m, *d, *v; |
730 | 731 |
731 m = PyImport_AddModule(name); | 732 m = PyImport_AddModule(name); |
732 if (m == NULL) | 733 if (m == NULL) |
733 return NULL; | 734 return NULL; |
734 /* If the module is being reloaded, we get the old module back | 735 /* If the module is being reloaded, we get the old module back |
735 and re-use its dict to exec the new code. */ | 736 and re-use its dict to exec the new code. */ |
736 d = PyModule_GetDict(m); | 737 d = PyModule_GetDict(m); |
(...skipping 10 matching lines...) Expand all Loading... | |
747 PyErr_Clear(); | 748 PyErr_Clear(); |
748 } | 749 } |
749 if (v == NULL) { | 750 if (v == NULL) { |
750 v = ((PyCodeObject *)co)->co_filename; | 751 v = ((PyCodeObject *)co)->co_filename; |
751 Py_INCREF(v); | 752 Py_INCREF(v); |
752 } | 753 } |
753 if (PyDict_SetItemString(d, "__file__", v) != 0) | 754 if (PyDict_SetItemString(d, "__file__", v) != 0) |
754 PyErr_Clear(); /* Not important enough to report */ | 755 PyErr_Clear(); /* Not important enough to report */ |
755 Py_DECREF(v); | 756 Py_DECREF(v); |
756 | 757 |
758 /* Remember the pyc path name as the __cached__ attribute. */ | |
759 if (cpathname == NULL) { | |
760 v = Py_None; | |
761 Py_INCREF(v); | |
762 } | |
763 else if ((v = PyUnicode_FromString(cpathname)) == NULL) { | |
764 PyErr_Clear(); /* Not important enough to report */ | |
765 v = Py_None; | |
766 Py_INCREF(v); | |
767 } | |
768 if (PyDict_SetItemString(d, "__cached__", v) != 0) | |
769 PyErr_Clear(); /* Not important enough to report */ | |
770 Py_DECREF(v); | |
771 | |
757 v = PyEval_EvalCode((PyCodeObject *)co, d, d); | 772 v = PyEval_EvalCode((PyCodeObject *)co, d, d); |
758 if (v == NULL) | 773 if (v == NULL) |
759 goto error; | 774 goto error; |
760 Py_DECREF(v); | 775 Py_DECREF(v); |
761 | 776 |
762 if ((m = PyDict_GetItemString(modules, name)) == NULL) { | 777 if ((m = PyDict_GetItemString(modules, name)) == NULL) { |
763 PyErr_Format(PyExc_ImportError, | 778 PyErr_Format(PyExc_ImportError, |
764 "Loaded module %.200s not found in sys.modules", | 779 "Loaded module %.200s not found in sys.modules", |
765 name); | 780 name); |
766 return NULL; | 781 return NULL; |
767 } | 782 } |
768 | 783 |
769 Py_INCREF(m); | 784 Py_INCREF(m); |
770 | 785 |
771 return m; | 786 return m; |
772 | 787 |
773 error: | 788 error: |
774 remove_module(name); | 789 remove_module(name); |
775 return NULL; | 790 return NULL; |
776 } | 791 } |
777 | 792 |
778 | 793 |
779 /* Like strrchr(string, '/') but searches for the rightmost of either SEP | 794 /* Like strrchr(string, '/') but searches for the rightmost of either SEP |
780 or ALTSEP, if the latter is defined. | 795 or ALTSEP, if the latter is defined. |
781 */ | 796 */ |
782 static char *rightmost_sep(const char *s) | 797 static char * |
Benjamin
2010/04/07 02:50:04
The convention seems to have the return type on it
Benjamin
2010/04/07 02:50:04
I don't know if it's worth bothering with const if
barry
2010/04/08 12:53:47
Done.
barry
2010/04/08 12:53:47
Done.
| |
798 rightmost_sep(char *s) | |
783 { | 799 { |
784 char *found, c; | 800 char *found, c; |
785 for (found = NULL; (c = *s); s++) { | 801 for (found = NULL; (c = *s); s++) { |
786 if (c == SEP | 802 if (c == SEP |
787 #ifdef ALTSEP | 803 #ifdef ALTSEP |
788 || c == ALTSEP | 804 || c == ALTSEP |
789 #endif | 805 #endif |
790 » » » ) | 806 » » ) |
791 { | 807 { |
Benjamin
2010/04/07 02:50:04
The brace if it is here at all should be right aft
barry
2010/04/08 12:53:47
I tried it both ways, but because of the interveni
| |
792 » » » found = (char *)s; | 808 » » » found = s; |
793 } | 809 } |
794 } | 810 } |
795 return found; | 811 return found; |
796 } | 812 } |
797 | 813 |
798 | 814 |
799 /* Given a pathname for a Python source file, fill a buffer with the | 815 /* Given a pathname for a Python source file, fill a buffer with the |
800 pathname for the corresponding compiled file. Return the pathname | 816 pathname for the corresponding compiled file. Return the pathname |
801 for the compiled file, or NULL if there's no space in the buffer. | 817 for the compiled file, or NULL if there's no space in the buffer. |
802 Doesn't set an exception. */ | 818 Doesn't set an exception. */ |
803 | 819 |
804 static char * | 820 static char * |
805 make_compiled_pathname(char *pathname, char *buf, size_t buflen, int debug) | 821 make_compiled_pathname(char *pathname, char *buf, size_t buflen, int debug) |
806 { | 822 { |
807 /* foo.py -> __pycache__/foo.<tag>.pyc */ | 823 /* foo.py -> __pycache__/foo.<tag>.pyc */ |
808 size_t len = strlen(pathname); | 824 size_t len = strlen(pathname); |
809 size_t i, save; | 825 size_t i, save; |
810 char *pos; | 826 char *pos; |
811 int sep = SEP; | 827 int sep = SEP; |
812 | 828 |
813 » /* Sanity check that the buffer has enough space to hold what will | 829 » /* Sanity check that the buffer has roughly enough space to hold what |
814 » eventually be the full path to the compiled file. The 4 extra | 830 » will eventually be the full path to the compiled file. The 5 extra |
815 bytes include the slash afer __pycache__, the two extra dots, the | 831 bytes include the slash afer __pycache__, the two extra dots, the |
816 » extra trailing character ('c' or 'o') and null. | 832 » extra trailing character ('c' or 'o') and null. This isn't exact |
833 » because the contents of the buffer can affect how many actual | |
834 » characters of the string get into the buffer. We'll do a final | |
835 » sanity check before writing the extension to ensure we do not | |
836 » overflow the buffer. | |
817 */ | 837 */ |
818 if (len + strlen(CACHEDIR) + strlen(pyc_tag) + 5 > buflen) | 838 if (len + strlen(CACHEDIR) + strlen(pyc_tag) + 5 > buflen) |
819 return NULL; | 839 return NULL; |
Benjamin
2010/04/07 02:50:04
Shouldn't make_compiled_pathname set an error here
barry
2010/04/08 12:53:47
I'm not so sure about this. m_c_p() is used in tw
| |
820 | 840 |
821 /* Find the last path separator and copy everything from the start of | 841 /* Find the last path separator and copy everything from the start of |
Benjamin
2010/04/07 02:50:04
What if the pathname is "/"? Then you'll end up in
barry
2010/04/08 12:53:47
'/__pycache__/cpython-32.pyc'
which is nonsense,
| |
822 the source string up to and including the separator. | 842 the source string up to and including the separator. |
823 */ | 843 */ |
824 if ((pos = rightmost_sep(pathname)) == NULL) { | 844 if ((pos = rightmost_sep(pathname)) == NULL) { |
825 i = 0; | 845 i = 0; |
826 } | 846 } |
827 else { | 847 else { |
828 sep = *pos; | 848 sep = *pos; |
829 i = pos - pathname + 1; | 849 i = pos - pathname + 1; |
830 strncpy(buf, pathname, i); | 850 strncpy(buf, pathname, i); |
831 } | 851 } |
832 | 852 |
833 save = i; | 853 save = i; |
834 buf[i++] = '\0'; | 854 buf[i++] = '\0'; |
835 /* Add __pycache__/ */ | 855 /* Add __pycache__/ */ |
836 strcat(buf, CACHEDIR); | 856 strcat(buf, CACHEDIR); |
837 i += strlen(CACHEDIR) - 1; | 857 i += strlen(CACHEDIR) - 1; |
838 buf[i++] = sep; | 858 buf[i++] = sep; |
839 buf[i++] = '\0'; | 859 buf[i++] = '\0'; |
840 /* Add the base filename, but remove the .py or .pyw extension, since | 860 /* Add the base filename, but remove the .py or .pyw extension, since |
841 the tag name must go before the extension. | 861 the tag name must go before the extension. |
842 */ | 862 */ |
843 strcat(buf, pathname + save); | 863 strcat(buf, pathname + save); |
844 if ((pos = strrchr(buf, '.')) != NULL) | 864 if ((pos = strrchr(buf, '.')) != NULL) |
845 *++pos = '\0'; | 865 *++pos = '\0'; |
846 strcat(buf, pyc_tag); | 866 strcat(buf, pyc_tag); |
867 /* The length test above assumes that we're only adding one character | |
868 to the end of what would normally be the extension. What if there | |
869 is no extension, or the string ends in '.' or '.p', and otherwise | |
870 fills the buffer? By appending 4 more characters onto the string | |
871 here, we could overrun the buffer. | |
872 | |
873 As a simple example, let's say buflen=32 and the input string is | |
874 'xxx.py'. strlen() would be 6 and the test above would yield: | |
875 | |
876 (6 + 11 + 10 + 5 == 32) > 32 | |
877 | |
878 which is false and so the name mangling would continue. This would | |
879 be fine because we'd end up with this string in buf: | |
880 | |
881 __pycache__/xxx.cpython-32.pyc\0 | |
882 | |
883 strlen(of that) == 30 + the nul fits inside a 32 character buffer. | |
884 We can even handle an input string of say 'xxxxx' above because | |
885 that's (5 + 11 + 10 + 5 == 31) > 32 which is also false. Name | |
886 mangling that yields: | |
887 | |
888 __pycache__/xxxxxcpython-32.pyc\0 | |
889 | |
890 which is 32 characters including the nul, and thus fits in the | |
891 buffer. However, an input string of 'xxxxxx' would yield a result | |
892 string of: | |
893 | |
894 __pycache__/xxxxxxcpython-32.pyc\0 | |
895 | |
896 which is 33 characters long (including the nul), thus overflowing | |
897 the buffer, even though the first test would fail, i.e.: the input | |
898 string is also 6 characters long, so 32 > 32 is false. | |
899 | |
900 The reason the first test fails but we still overflow the buffer is | |
901 that the test above only expects to add one extra character to be | |
902 added to the extension, and here we're adding three (pyc). We | |
903 don't add the first dot, so that reclaims one of expected | |
904 positions, leaving us overflowing by 1 byte (3 extra - 1 reclaimed | |
905 dot - 1 expected extra == 1 overflowed). | |
906 | |
907 The best we can do is ensure that we still have enough room in the | |
908 target buffer before we write the extension. Because it's always | |
909 only the extension that can cause the overflow, and never the other | |
910 path bytes we've written, it's sufficient to just do one more test | |
911 here. Still, the assertion that follows can't hurt. | |
912 */ | |
913 #if 0 | |
914 printf("strlen(buf): %d; buflen: %d\n", (int)strlen(buf), (int)buflen); | |
915 #endif | |
916 if (strlen(buf) + 5 > buflen) | |
917 return NULL; | |
847 strcat(buf, debug ? ".pyc" : ".pyo"); | 918 strcat(buf, debug ? ".pyc" : ".pyo"); |
919 assert(strlen(buf) < buflen); | |
848 return buf; | 920 return buf; |
849 } | 921 } |
850 | 922 |
851 | 923 |
852 /* Given a pathname to a Python byte compiled file, return the path to the | 924 /* Given a pathname to a Python byte compiled file, return the path to the |
853 source file, if the path matches the PEP 3147 format. This does not check | 925 source file, if the path matches the PEP 3147 format. This does not check |
854 for any file existence, however, if the pyc file name does not match PEP | 926 for any file existence, however, if the pyc file name does not match PEP |
855 3147 style, NULL is returned. buf must be at least as big as pathname; | 927 3147 style, NULL is returned. buf must be at least as big as pathname; |
856 the resulting path will always be shorter. */ | 928 the resulting path will always be shorter. */ |
857 ··· | 929 ··· |
(...skipping 11 matching lines...) Expand all Loading... | |
869 if ((right = rightmost_sep(pathname)) == NULL) | 941 if ((right = rightmost_sep(pathname)) == NULL) |
870 return NULL; | 942 return NULL; |
871 sep = *right; | 943 sep = *right; |
872 *right = '\0'; | 944 *right = '\0'; |
873 left = rightmost_sep(pathname); | 945 left = rightmost_sep(pathname); |
874 *right = sep; | 946 *right = sep; |
875 if (left == NULL) | 947 if (left == NULL) |
876 left = pathname; | 948 left = pathname; |
877 else | 949 else |
878 left++; | 950 left++; |
879 » if (right-left != 11 || strncmp(left, CACHEDIR, right-left) != 0) | 951 » if (right-left != strlen(CACHEDIR) || |
952 » strncmp(left, CACHEDIR, right-left) != 0) | |
880 return NULL; | 953 return NULL; |
881 | 954 |
882 /* Now verify that the path component to the right of the last slash | 955 /* Now verify that the path component to the right of the last slash |
883 has two dots in it. | 956 has two dots in it. |
884 */ | 957 */ |
885 » if ((dot0 = strchr(right+1, '.')) == NULL) | 958 » if ((dot0 = strchr(right + 1, '.')) == NULL) |
Benjamin
2010/04/07 02:50:04
I think there should be space between "+" and oper
barry
2010/04/08 12:53:47
Done.
| |
886 » » return NULL; | 959 » » return NULL; |
887 » if ((dot1 = strchr(dot0+1, '.')) == NULL) | 960 » if ((dot1 = strchr(dot0 + 1, '.')) == NULL) |
888 return NULL; | 961 return NULL; |
889 /* Too many dots? */ | 962 /* Too many dots? */ |
890 » if (strchr(dot1+1, '.') != NULL) | 963 » if (strchr(dot1 + 1, '.') != NULL) |
891 return NULL; | 964 return NULL; |
892 | 965 |
893 /* This is a PEP 3147 path. Start by copying everything from the | 966 /* This is a PEP 3147 path. Start by copying everything from the |
894 start of pathname up to and including the leftmost slash. Then | 967 start of pathname up to and including the leftmost slash. Then |
895 copy the file's basename, removing the magic tag and adding a .py | 968 copy the file's basename, removing the magic tag and adding a .py |
896 suffix. | 969 suffix. |
897 */ | 970 */ |
898 strncpy(buf, pathname, (i=left-pathname)); | 971 strncpy(buf, pathname, (i=left-pathname)); |
899 strncpy(buf+i, right+1, (j=dot0-right)); | 972 strncpy(buf+i, right+1, (j=dot0-right)); |
900 strcpy(buf+i+j, "py"); | 973 strcpy(buf+i+j, "py"); |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
974 "Bad magic number in %.200s", cpathname); | 1047 "Bad magic number in %.200s", cpathname); |
975 return NULL; | 1048 return NULL; |
976 } | 1049 } |
977 (void) PyMarshal_ReadLongFromFile(fp); | 1050 (void) PyMarshal_ReadLongFromFile(fp); |
978 co = read_compiled_module(cpathname, fp); | 1051 co = read_compiled_module(cpathname, fp); |
979 if (co == NULL) | 1052 if (co == NULL) |
980 return NULL; | 1053 return NULL; |
981 if (Py_VerboseFlag) | 1054 if (Py_VerboseFlag) |
982 PySys_WriteStderr("import %s # precompiled from %s\n", | 1055 PySys_WriteStderr("import %s # precompiled from %s\n", |
983 name, cpathname); | 1056 name, cpathname); |
984 » m = PyImport_ExecCodeModuleEx(name, (PyObject *)co, cpathname); | 1057 » m = PyImport_ExecCodeModuleWithPathnames( |
1058 » » name, (PyObject *)co, cpathname, cpathname); | |
985 Py_DECREF(co); | 1059 Py_DECREF(co); |
986 | 1060 |
987 return m; | 1061 return m; |
988 } | 1062 } |
989 | 1063 |
990 /* Parse a source file and return the corresponding code object */ | 1064 /* Parse a source file and return the corresponding code object */ |
991 | 1065 |
992 static PyCodeObject * | 1066 static PyCodeObject * |
993 parse_source_module(const char *pathname, FILE *fp) | 1067 parse_source_module(const char *pathname, FILE *fp) |
994 { | 1068 { |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1051 remove the file. */ | 1125 remove the file. */ |
1052 | 1126 |
1053 static void | 1127 static void |
1054 write_compiled_module(PyCodeObject *co, char *cpathname, struct stat *srcstat) | 1128 write_compiled_module(PyCodeObject *co, char *cpathname, struct stat *srcstat) |
1055 { | 1129 { |
1056 FILE *fp; | 1130 FILE *fp; |
1057 char *dirpath; | 1131 char *dirpath; |
1058 time_t mtime = srcstat->st_mtime; | 1132 time_t mtime = srcstat->st_mtime; |
1059 #ifdef MS_WINDOWS /* since Windows uses different permissions */ | 1133 #ifdef MS_WINDOWS /* since Windows uses different permissions */ |
1060 mode_t mode = srcstat->st_mode & ~S_IEXEC; | 1134 mode_t mode = srcstat->st_mode & ~S_IEXEC; |
1061 » mode_t dirmode = srcstat->st_mode | S_IEXEC; /* XXX */ | 1135 » mode_t dirmode = srcstat->st_mode | S_IEXEC; /* XXX Is this correct |
Benjamin
2010/04/07 02:50:04
Perhaps you could expand on this XXX.
barry
2010/04/08 12:53:47
I expanded the comment. Basically I'm unsure if t
| |
1136 » » » » » » » for Windows? | |
1137 » » » » » » » 2010-04-07 BAW */ | |
1062 #else | 1138 #else |
1063 mode_t mode = srcstat->st_mode & ~S_IXUSR & ~S_IXGRP & ~S_IXOTH; | 1139 mode_t mode = srcstat->st_mode & ~S_IXUSR & ~S_IXGRP & ~S_IXOTH; |
1064 mode_t dirmode = (srcstat->st_mode | | 1140 mode_t dirmode = (srcstat->st_mode | |
1065 S_IXUSR | S_IXGRP | S_IXOTH | | 1141 S_IXUSR | S_IXGRP | S_IXOTH | |
1066 S_IWUSR | S_IWGRP | S_IWOTH); | 1142 S_IWUSR | S_IWGRP | S_IWOTH); |
1067 #endif | 1143 #endif |
1068 int saved; | 1144 int saved; |
1069 | 1145 |
1070 /* Ensure that the __pycache__ directory exists. */ | 1146 /* Ensure that the __pycache__ directory exists. */ |
1071 dirpath = rightmost_sep(cpathname); | 1147 dirpath = rightmost_sep(cpathname); |
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1215 return NULL; | 1291 return NULL; |
1216 if (Py_VerboseFlag) | 1292 if (Py_VerboseFlag) |
1217 PySys_WriteStderr("import %s # from %s\n", | 1293 PySys_WriteStderr("import %s # from %s\n", |
1218 name, pathname); | 1294 name, pathname); |
1219 if (cpathname) { | 1295 if (cpathname) { |
1220 PyObject *ro = PySys_GetObject("dont_write_bytecode"); | 1296 PyObject *ro = PySys_GetObject("dont_write_bytecode"); |
1221 if (ro == NULL || !PyObject_IsTrue(ro)) | 1297 if (ro == NULL || !PyObject_IsTrue(ro)) |
1222 write_compiled_module(co, cpathname, &st); | 1298 write_compiled_module(co, cpathname, &st); |
1223 } | 1299 } |
1224 } | 1300 } |
1225 » m = PyImport_ExecCodeModuleEx(name, (PyObject *)co, pathname); | 1301 » m = PyImport_ExecCodeModuleWithPathnames( |
1302 » » name, (PyObject *)co, pathname, cpathname); | |
1226 Py_DECREF(co); | 1303 Py_DECREF(co); |
1227 | 1304 |
1228 return m; | 1305 return m; |
1229 } | 1306 } |
1230 | 1307 |
1231 /* Get source file -> unicode or None | 1308 /* Get source file -> unicode or None |
1232 * Returns the path to the py file if available, else the given path | 1309 * Returns the path to the py file if available, else the given path |
1233 */ | 1310 */ |
1234 static PyObject * | 1311 static PyObject * |
1235 get_sourcefile(char *file) | 1312 get_sourcefile(char *file) |
1236 { | 1313 { |
1237 char py[MAXPATHLEN + 1]; | 1314 char py[MAXPATHLEN + 1]; |
1238 Py_ssize_t len; | 1315 Py_ssize_t len; |
1239 PyObject *u; | 1316 PyObject *u; |
1240 struct stat statbuf; | 1317 struct stat statbuf; |
1241 | 1318 |
1242 if (!file || !*file) { | 1319 if (!file || !*file) { |
1243 Py_RETURN_NONE; | 1320 Py_RETURN_NONE; |
1244 } | 1321 } |
1245 | 1322 |
1246 len = strlen(file); | 1323 len = strlen(file); |
1247 /* match '*.py?' */ | 1324 /* match '*.py?' */ |
1248 if (len > MAXPATHLEN || PyOS_strnicmp(&file[len-4], ".py", 3) != 0) { | 1325 if (len > MAXPATHLEN || PyOS_strnicmp(&file[len-4], ".py", 3) != 0) { |
1249 return PyUnicode_DecodeFSDefault(file); | 1326 return PyUnicode_DecodeFSDefault(file); |
1250 } | 1327 } |
1251 | 1328 |
1252 /* Start by trying to turn PEP 3147 path into source path. If that | 1329 » /* Start by trying to turn PEP 3147 path into source path. If that |
1253 * fails, just chop off the trailing character, i.e. legacy pyc path | 1330 » * fails, just chop off the trailing character, i.e. legacy pyc path |
1254 * to py. | 1331 » * to py. |
1255 */ | 1332 » */ |
1256 if (make_source_pathname(file, py) == NULL) { | 1333 if (make_source_pathname(file, py) == NULL) { |
1257 strncpy(py, file, len-1); | 1334 strncpy(py, file, len-1); |
1258 py[len-1] = '\0'; | 1335 py[len-1] = '\0'; |
1259 } | 1336 } |
1260 | 1337 |
1261 if (stat(py, &statbuf) == 0 && | 1338 if (stat(py, &statbuf) == 0 && |
1262 S_ISREG(statbuf.st_mode)) { | 1339 S_ISREG(statbuf.st_mode)) { |
1263 u = PyUnicode_DecodeFSDefault(py); | 1340 u = PyUnicode_DecodeFSDefault(py); |
1264 } | 1341 } |
1265 else { | 1342 else { |
(...skipping 1728 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2994 return PyBytes_FromStringAndSize(buf, 4); | 3071 return PyBytes_FromStringAndSize(buf, 4); |
2995 };······ | 3072 };······ |
2996 | 3073 |
2997 static PyObject * | 3074 static PyObject * |
2998 imp_get_magic(PyObject *self, PyObject *noargs) | 3075 imp_get_magic(PyObject *self, PyObject *noargs) |
2999 { | 3076 { |
3000 return imp_make_magic(pyc_magic); | 3077 return imp_make_magic(pyc_magic); |
3001 } | 3078 } |
3002 | 3079 |
3003 static PyObject * | 3080 static PyObject * |
3081 imp_get_tag(PyObject *self, PyObject *noargs) | |
3082 { | |
3083 return PyUnicode_FromString(pyc_tag); | |
3084 } | |
3085 | |
3086 static PyObject * | |
3004 imp_get_suffixes(PyObject *self, PyObject *noargs) | 3087 imp_get_suffixes(PyObject *self, PyObject *noargs) |
3005 { | 3088 { |
3006 PyObject *list; | 3089 PyObject *list; |
3007 struct filedescr *fdp; | 3090 struct filedescr *fdp; |
3008 | 3091 |
3009 list = PyList_New(0); | 3092 list = PyList_New(0); |
3010 if (list == NULL) | 3093 if (list == NULL) |
3011 return NULL; | 3094 return NULL; |
3012 for (fdp = _PyImport_Filetab; fdp->suffix != NULL; fdp++) { | 3095 for (fdp = _PyImport_Filetab; fdp->suffix != NULL; fdp++) { |
3013 PyObject *item = Py_BuildValue("ssi", | 3096 PyObject *item = Py_BuildValue("ssi", |
(...skipping 345 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
3359 { | 3442 { |
3360 return PyImport_ReloadModule(v); | 3443 return PyImport_ReloadModule(v); |
3361 } | 3444 } |
3362 | 3445 |
3363 PyDoc_STRVAR(doc_reload, | 3446 PyDoc_STRVAR(doc_reload, |
3364 "reload(module) -> module\n\ | 3447 "reload(module) -> module\n\ |
3365 \n\ | 3448 \n\ |
3366 Reload the module. The module must have been successfully imported before."); | 3449 Reload the module. The module must have been successfully imported before."); |
3367 | 3450 |
3368 static PyObject * | 3451 static PyObject * |
3369 imp_cache_from_source(PyObject *self, PyObject *args, PyObject *kws) | 3452 imp_cache_from_source(PyObject *self, PyObject *args, PyObject *kws) |
Benjamin
2010/04/07 02:50:04
These functions needs documentation in Doc/library
barry
2010/04/08 12:53:47
Done.
| |
3370 { | 3453 { |
3371 » static char *kwlist[] = {"path", "force__debug__", NULL}; | 3454 » static char *kwlist[] = {"path", "debug_override", NULL}; |
Benjamin
2010/04/07 02:50:04
I think the argument should either be called force
barry
2010/04/08 12:53:47
How about `debug_override`?
| |
3372 | 3455 |
3373 char buf[MAXPATHLEN+1]; | 3456 char buf[MAXPATHLEN+1]; |
3374 char *pathname, *cpathname; | 3457 char *pathname, *cpathname; |
3375 » PyObject *force__debug__ = Py_None; | 3458 » PyObject *debug_override = Py_None; |
3376 int debug = !Py_OptimizeFlag; | 3459 int debug = !Py_OptimizeFlag; |
3377 | 3460 |
3378 if (!PyArg_ParseTupleAndKeywords( | 3461 if (!PyArg_ParseTupleAndKeywords( |
3379 args, kws, "es|O", kwlist, | 3462 args, kws, "es|O", kwlist, |
3380 » » Py_FileSystemDefaultEncoding, &pathname, &force__debug__)) | 3463 » » Py_FileSystemDefaultEncoding, &pathname, &debug_override)) |
3381 » » return NULL; | 3464 » » return NULL; |
3382 | 3465 |
3383 » if (force__debug__ != Py_None) { | 3466 » if (debug_override != Py_None) |
Benjamin
2010/04/07 02:50:04
Why not just use PyObject_IsTrueBool?
barry
2010/04/08 12:53:47
You mean PyObject_IsTrue()? Because I really just
| |
3384 » » if (force__debug__ == Py_True) | 3467 » » if ((debug = PyObject_IsTrue(debug_override)) < 0) |
3385 » » » debug = 1; | 3468 » » » return NULL; |
3386 » » else if (force__debug__ == Py_False) | |
3387 » » » debug = 0; | |
3388 » » else { | |
3389 » » » PyErr_Format(PyExc_ValueError, | |
3390 » » » » "force__debug__ must be True or False"); | |
3391 » » » PyMem_Free(pathname); | |
3392 » » » return NULL; | |
3393 » » } | |
3394 » } | |
3395 | 3469 |
3396 cpathname = make_compiled_pathname(pathname, buf, MAXPATHLEN+1, debug); | 3470 cpathname = make_compiled_pathname(pathname, buf, MAXPATHLEN+1, debug); |
3397 PyMem_Free(pathname); | 3471 PyMem_Free(pathname); |
3398 | 3472 |
3399 if (cpathname == NULL) { | 3473 if (cpathname == NULL) { |
3400 PyErr_Format(PyExc_SystemError, "path buffer too short"); | 3474 PyErr_Format(PyExc_SystemError, "path buffer too short"); |
3401 return NULL; | 3475 return NULL; |
3402 } | 3476 } |
3403 return PyUnicode_FromString(buf); | 3477 return PyUnicode_FromString(buf); |
3404 } | 3478 } |
3405 | 3479 |
3406 PyDoc_STRVAR(doc_cache_from_source, | 3480 PyDoc_STRVAR(doc_cache_from_source, |
3407 "Given the path to a .py file, return the path to its .pyc/.pyo file.\n\ | 3481 "Given the path to a .py file, return the path to its .pyc/.pyo file.\n\ |
3408 \n\ | 3482 \n\ |
3409 The .py file does not need to exist; this simply returns the path to the\n\ | 3483 The .py file does not need to exist; this simply returns the path to the\n\ |
3410 .pyc/.pyo file calculated as if the .py file were imported. The extension\n\ | 3484 .pyc/.pyo file calculated as if the .py file were imported. The extension\n\ |
3411 will be .pyc unless __debug__ is not defined, then it will be .pyo.\n\ | 3485 will be .pyc unless __debug__ is not defined, then it will be .pyo.\n\ |
3412 \n\ | 3486 \n\ |
3413 If force__debug__ is not None, then it must be a boolean and is taken as\n\ | 3487 If debug_override is not None, then it must be a boolean and is taken as\n\ |
3414 the value of __debug__ instead."); | 3488 the value of __debug__ instead."); |
3415 | 3489 |
3416 static PyObject * | 3490 static PyObject * |
3417 imp_source_from_cache(PyObject *self, PyObject *args, PyObject *kws) | 3491 imp_source_from_cache(PyObject *self, PyObject *args, PyObject *kws) |
3418 { | 3492 { |
3419 static char *kwlist[] = {"path", NULL}; | 3493 static char *kwlist[] = {"path", NULL}; |
3420 | 3494 |
3421 char *pathname; | 3495 char *pathname; |
3422 char buf[MAXPATHLEN+1]; | 3496 char buf[MAXPATHLEN+1]; |
3423 | 3497 |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
3458 | 3532 |
3459 PyDoc_STRVAR(doc_load_module, | 3533 PyDoc_STRVAR(doc_load_module, |
3460 "load_module(name, file, filename, (suffix, mode, type)) -> module\n\ | 3534 "load_module(name, file, filename, (suffix, mode, type)) -> module\n\ |
3461 Load a module, given information returned by find_module().\n\ | 3535 Load a module, given information returned by find_module().\n\ |
3462 The module name must include the full package name, if any."); | 3536 The module name must include the full package name, if any."); |
3463 | 3537 |
3464 PyDoc_STRVAR(doc_get_magic, | 3538 PyDoc_STRVAR(doc_get_magic, |
3465 "get_magic() -> string\n\ | 3539 "get_magic() -> string\n\ |
3466 Return the magic number for .pyc or .pyo files."); | 3540 Return the magic number for .pyc or .pyo files."); |
3467 | 3541 |
3542 PyDoc_STRVAR(doc_get_tag, | |
3543 "get_tag() -> string\n\ | |
3544 Return the magic tag for .pyc or .pyo files."); | |
3545 | |
3468 PyDoc_STRVAR(doc_get_suffixes, | 3546 PyDoc_STRVAR(doc_get_suffixes, |
3469 "get_suffixes() -> [(suffix, mode, type), ...]\n\ | 3547 "get_suffixes() -> [(suffix, mode, type), ...]\n\ |
3470 Return a list of (suffix, mode, type) tuples describing the files\n\ | 3548 Return a list of (suffix, mode, type) tuples describing the files\n\ |
3471 that find_module() looks for."); | 3549 that find_module() looks for."); |
3472 | 3550 |
3473 PyDoc_STRVAR(doc_new_module, | 3551 PyDoc_STRVAR(doc_new_module, |
3474 "new_module(name) -> module\n\ | 3552 "new_module(name) -> module\n\ |
3475 Create a new module. Do not enter it in sys.modules.\n\ | 3553 Create a new module. Do not enter it in sys.modules.\n\ |
3476 The module name must include the full package name, if any."); | 3554 The module name must include the full package name, if any."); |
3477 | 3555 |
(...skipping 10 matching lines...) Expand all Loading... | |
3488 On platforms without threads, this function does nothing."); | 3566 On platforms without threads, this function does nothing."); |
3489 | 3567 |
3490 PyDoc_STRVAR(doc_release_lock, | 3568 PyDoc_STRVAR(doc_release_lock, |
3491 "release_lock() -> None\n\ | 3569 "release_lock() -> None\n\ |
3492 Release the interpreter's import lock.\n\ | 3570 Release the interpreter's import lock.\n\ |
3493 On platforms without threads, this function does nothing."); | 3571 On platforms without threads, this function does nothing."); |
3494 | 3572 |
3495 static PyMethodDef imp_methods[] = { | 3573 static PyMethodDef imp_methods[] = { |
3496 {"find_module", imp_find_module, METH_VARARGS, doc_find_module}, | 3574 {"find_module", imp_find_module, METH_VARARGS, doc_find_module}, |
3497 {"get_magic", imp_get_magic, METH_NOARGS, doc_get_magic}, | 3575 {"get_magic", imp_get_magic, METH_NOARGS, doc_get_magic}, |
3576 {"get_tag", imp_get_tag, METH_NOARGS, doc_get_tag}, | |
3498 {"get_suffixes", imp_get_suffixes, METH_NOARGS, doc_get_suffixes}, | 3577 {"get_suffixes", imp_get_suffixes, METH_NOARGS, doc_get_suffixes}, |
3499 {"load_module", imp_load_module, METH_VARARGS, doc_load_module}, | 3578 {"load_module", imp_load_module, METH_VARARGS, doc_load_module}, |
3500 {"new_module", imp_new_module, METH_VARARGS, doc_new_module}, | 3579 {"new_module", imp_new_module, METH_VARARGS, doc_new_module}, |
3501 {"lock_held", imp_lock_held, METH_NOARGS, doc_lock_held}, | 3580 {"lock_held", imp_lock_held, METH_NOARGS, doc_lock_held}, |
3502 {"acquire_lock", imp_acquire_lock, METH_NOARGS, doc_acquire_lock}, | 3581 {"acquire_lock", imp_acquire_lock, METH_NOARGS, doc_acquire_lock}, |
3503 {"release_lock", imp_release_lock, METH_NOARGS, doc_release_lock}, | 3582 {"release_lock", imp_release_lock, METH_NOARGS, doc_release_lock}, |
3504 {"reload", imp_reload, METH_O, doc_reload}, | 3583 {"reload", imp_reload, METH_O, doc_reload}, |
3505 {"cache_from_source", (PyCFunction)imp_cache_from_source, | 3584 {"cache_from_source", (PyCFunction)imp_cache_from_source, |
3506 METH_VARARGS | METH_KEYWORDS, doc_cache_from_source}, | 3585 METH_VARARGS | METH_KEYWORDS, doc_cache_from_source}, |
3507 {"source_from_cache", (PyCFunction)imp_source_from_cache, | 3586 {"source_from_cache", (PyCFunction)imp_source_from_cache, |
(...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
3657 imp_methods, | 3736 imp_methods, |
3658 NULL, | 3737 NULL, |
3659 NULL, | 3738 NULL, |
3660 NULL, | 3739 NULL, |
3661 NULL | 3740 NULL |
3662 }; | 3741 }; |
3663 | 3742 |
3664 PyMODINIT_FUNC | 3743 PyMODINIT_FUNC |
3665 PyInit_imp(void) | 3744 PyInit_imp(void) |
3666 { | 3745 { |
3667 » PyObject *m, *d, *magics = NULL; | 3746 » PyObject *m, *d; |
3668 » struct pymagics *t; | |
3669 | 3747 |
3670 if (PyType_Ready(&PyNullImporter_Type) < 0) | 3748 if (PyType_Ready(&PyNullImporter_Type) < 0) |
3671 return NULL; | 3749 return NULL; |
3672 | 3750 |
3673 m = PyModule_Create(&impmodule); | 3751 m = PyModule_Create(&impmodule); |
3674 if (m == NULL) | 3752 if (m == NULL) |
3675 goto failure; | 3753 goto failure; |
3676 d = PyModule_GetDict(m); | 3754 d = PyModule_GetDict(m); |
3677 if (d == NULL) | 3755 if (d == NULL) |
3678 goto failure; | 3756 goto failure; |
3679 | 3757 |
3680 if (setint(d, "SEARCH_ERROR", SEARCH_ERROR) < 0) goto failure; | 3758 if (setint(d, "SEARCH_ERROR", SEARCH_ERROR) < 0) goto failure; |
3681 if (setint(d, "PY_SOURCE", PY_SOURCE) < 0) goto failure; | 3759 if (setint(d, "PY_SOURCE", PY_SOURCE) < 0) goto failure; |
3682 if (setint(d, "PY_COMPILED", PY_COMPILED) < 0) goto failure; | 3760 if (setint(d, "PY_COMPILED", PY_COMPILED) < 0) goto failure; |
3683 if (setint(d, "C_EXTENSION", C_EXTENSION) < 0) goto failure; | 3761 if (setint(d, "C_EXTENSION", C_EXTENSION) < 0) goto failure; |
3684 if (setint(d, "PY_RESOURCE", PY_RESOURCE) < 0) goto failure; | 3762 if (setint(d, "PY_RESOURCE", PY_RESOURCE) < 0) goto failure; |
3685 if (setint(d, "PKG_DIRECTORY", PKG_DIRECTORY) < 0) goto failure; | 3763 if (setint(d, "PKG_DIRECTORY", PKG_DIRECTORY) < 0) goto failure; |
3686 if (setint(d, "C_BUILTIN", C_BUILTIN) < 0) goto failure; | 3764 if (setint(d, "C_BUILTIN", C_BUILTIN) < 0) goto failure; |
3687 if (setint(d, "PY_FROZEN", PY_FROZEN) < 0) goto failure; | 3765 if (setint(d, "PY_FROZEN", PY_FROZEN) < 0) goto failure; |
3688 if (setint(d, "PY_CODERESOURCE", PY_CODERESOURCE) < 0) goto failure; | 3766 if (setint(d, "PY_CODERESOURCE", PY_CODERESOURCE) < 0) goto failure; |
3689 if (setint(d, "IMP_HOOK", IMP_HOOK) < 0) goto failure; | 3767 if (setint(d, "IMP_HOOK", IMP_HOOK) < 0) goto failure; |
3690 | 3768 |
3691 Py_INCREF(&PyNullImporter_Type); | 3769 Py_INCREF(&PyNullImporter_Type); |
3692 PyModule_AddObject(m, "NullImporter", (PyObject *)&PyNullImporter_Type); | 3770 PyModule_AddObject(m, "NullImporter", (PyObject *)&PyNullImporter_Type); |
3693 /* Set up magic number tag mappings. */ | |
3694 if ((magics = PyDict_New()) == NULL) goto failure; | |
3695 if (PyModule_AddObject(m, "magic_tags", magics) < 0) goto failure; | |
Benjamin
2010/04/07 02:50:04
This attribute needs to be documented, too.
barry
2010/04/08 12:53:47
Done.
| |
3696 for (t = _PyMagicNumberTags; t->tag != NULL; ++t) { | |
3697 int result; | |
3698 PyObject *number = imp_make_magic(t->number); | |
3699 PyObject *tag = PyUnicode_FromString(t->tag); | |
3700 | |
3701 if (number == NULL || tag == NULL) { | |
3702 Py_XDECREF(number); | |
3703 Py_XDECREF(tag); | |
3704 goto failure; | |
3705 } | |
3706 result = PyDict_SetItem(magics, number, tag); | |
3707 Py_DECREF(number); | |
3708 Py_DECREF(tag); | |
3709 if (result < 0) goto failure; | |
3710 } | |
3711 return m; | 3771 return m; |
3712 failure: | 3772 failure: |
3713 Py_XDECREF(m); | 3773 Py_XDECREF(m); |
3714 Py_XDECREF(magics); | |
3715 return NULL; | 3774 return NULL; |
3716 } | 3775 } |
3717 | 3776 |
3718 | 3777 |
3719 /* API for embedding applications that want to add their own entries | 3778 /* API for embedding applications that want to add their own entries |
3720 to the table of built-in modules. This should normally be called | 3779 to the table of built-in modules. This should normally be called |
3721 *before* Py_Initialize(). When the table resize fails, -1 is | 3780 *before* Py_Initialize(). When the table resize fails, -1 is |
3722 returned and the existing table is unchanged. | 3781 returned and the existing table is unchanged. |
3723 | 3782 |
3724 After a similar function by Just van Rossum. */ | 3783 After a similar function by Just van Rossum. */ |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
3764 | 3823 |
3765 newtab[0].name = (char *)name; | 3824 newtab[0].name = (char *)name; |
3766 newtab[0].initfunc = initfunc; | 3825 newtab[0].initfunc = initfunc; |
3767 | 3826 |
3768 return PyImport_ExtendInittab(newtab); | 3827 return PyImport_ExtendInittab(newtab); |
3769 } | 3828 } |
3770 | 3829 |
3771 #ifdef __cplusplus | 3830 #ifdef __cplusplus |
3772 } | 3831 } |
3773 #endif | 3832 #endif |
LEFT | RIGHT |