@@ -2949,21 +2949,34 @@ PyImport_Import(PyObject *module_name)
2949
2949
importing modules.
2950
2950
*/
2951
2951
2952
- // Pyston change: we don't support get_magic
2953
- #if 0
2952
+ // Pyston change: there is a surprising number of custom pyc-reading and -writing
2953
+ // code out there that wants to use imp.get_magic. If we simply remove imp.get_magic,
2954
+ // that code will tend to fail in unrecoverable ways. While hacky, the best-working option
2955
+ // found so far seems to be to have imp.get_magic exist but to return a changing value so
2956
+ // that the check will never succeed.
2954
2957
static PyObject *
2955
2958
imp_get_magic (PyObject * self , PyObject * noargs )
2956
2959
{
2960
+ static int counter ;
2961
+ if (counter == 0 ) {
2962
+ // something randomish
2963
+ counter = 0x12345678 + (getpid () << 16 );
2964
+ }
2965
+ // and that changes each time you call it
2966
+ counter ++ ;
2967
+
2957
2968
char buf [4 ];
2969
+ memcpy (buf , & counter , 4 );
2958
2970
2971
+ #if 0
2959
2972
buf [0 ] = (char ) ((pyc_magic >> 0 ) & 0xff );
2960
2973
buf [1 ] = (char ) ((pyc_magic >> 8 ) & 0xff );
2961
2974
buf [2 ] = (char ) ((pyc_magic >> 16 ) & 0xff );
2962
2975
buf [3 ] = (char ) ((pyc_magic >> 24 ) & 0xff );
2976
+ #endif
2963
2977
2964
2978
return PyString_FromStringAndSize (buf , 4 );
2965
2979
}
2966
- #endif
2967
2980
2968
2981
static PyObject *
2969
2982
imp_get_suffixes (PyObject * self , PyObject * noargs )
@@ -3329,8 +3342,7 @@ On platforms without threads, this function does nothing.");
3329
3342
static PyMethodDef imp_methods [] = {
3330
3343
{"reload" , imp_reload , METH_O , doc_reload },
3331
3344
{"find_module" , imp_find_module , METH_VARARGS , doc_find_module },
3332
- // Pyston change: we don't support this function
3333
- // {"get_magic", imp_get_magic, METH_NOARGS, doc_get_magic},
3345
+ {"get_magic" , imp_get_magic , METH_NOARGS , doc_get_magic },
3334
3346
{"get_suffixes" , imp_get_suffixes , METH_NOARGS , doc_get_suffixes },
3335
3347
{"load_module" , imp_load_module , METH_VARARGS , doc_load_module },
3336
3348
{"new_module" , imp_new_module , METH_VARARGS , doc_new_module },
0 commit comments