Index: Parser/tokenizer.c =================================================================== --- Parser/tokenizer.c (revision 65848) +++ Parser/tokenizer.c (working copy) @@ -448,8 +448,12 @@ if (io == NULL) goto cleanup; - stream = PyObject_CallMethod(io, "open", "ssis", - tok->filename, "r", -1, enc); + if (tok->filename) + stream = PyObject_CallMethod(io, "open", "ssis", + tok->filename, "r", -1, enc); + else + stream = PyObject_CallMethod(io, "open", "isis", + fileno(tok->fp), "r", -1, enc); if (stream == NULL) goto cleanup; Index: Lib/test/test_imp.py =================================================================== --- Lib/test/test_imp.py (revision 65848) +++ Lib/test/test_imp.py (working copy) @@ -1,4 +1,5 @@ import imp +import sys import unittest from test import support @@ -59,6 +60,21 @@ '"""Tokenization help for Python programs.\n') fp.close() + def test_issue3594(self): + temp_mod_name = 'test_imp_helper' + sys.path.insert(0, '.') + try: + with open(temp_mod_name + '.py', 'w') as file: + file.write("# coding: cp1252\nu = 'test.test_imp'\n") + file, filename, info = imp.find_module(temp_mod_name) + file.close() + self.assertEquals(file.encoding, 'cp1252') + finally: + del sys.path[0] + support.unlink(temp_mod_name + '.py') + support.unlink(temp_mod_name + '.pyc') + support.unlink(temp_mod_name + '.pyo') + def test_reload(self): import marshal imp.reload(marshal)