• Kirill Smelkov's avatar
    gpython: Run code in carefully prepared __main__ module · 2351dd27
    Kirill Smelkov authored
    Because else sys.modules['__main__'] points to the module of gpython
    itself, not user program. This difference can be important when e.g.
    user defines classes in main module and tries to pickle them. Such
    pickling will fail if gpython is not careful to run user's code in its
    own main.
    
    Without this patch added test fails as
    
          File "check_main.py", line 51, in <module>
            main()
          File "check_main.py", line 39, in main
            assert hasattr(mainmod, 'MyUniqueClassXYZ'),        dir(mainmod)
        AssertionError: ['__builtins__', '__doc__', '__file__', '__name__', '__package__', 'main']
    
    The problem was discovered while trying to run test program from
    https://github.com/zopefoundation/ZEO/issues/166 :
    
        $ gpython zopenrace-zeo.py
        Traceback (most recent call last):
          File "/home/kirr/tmp/trashme/Z/py2.venv/bin/gpython", line 8, in <module>
            sys.exit(main())
          File "/home/kirr/tmp/trashme/Z/py2.venv/lib/python2.7/site-packages/gpython/__init__.py", line 395, in main
            pymain(argv, init)
          File "/home/kirr/tmp/trashme/Z/py2.venv/lib/python2.7/site-packages/gpython/__init__.py", line 266, in pymain
            run()
          File "/home/kirr/tmp/trashme/Z/py2.venv/lib/python2.7/site-packages/gpython/__init__.py", line 172, in run
            _execfile(filepath, g)
          File "/home/kirr/tmp/trashme/Z/py2.venv/lib/python2.7/site-packages/gpython/__init__.py", line 275, in _execfile
            six.exec_(code, globals, locals)
          File "/home/kirr/tmp/trashme/Z/py2.venv/lib/python2.7/site-packages/six.py", line 719, in exec_
            exec("""exec _code_ in _globs_, _locs_""")
          File "<string>", line 1, in <module>
          File "zopenrace-zeo.py", line 205, in <module>
            main()
          File "zopenrace-zeo.py", line 190, in main
            init()
          File "zopenrace-zeo.py", line 126, in init
            transaction.commit()
          File "/home/kirr/tmp/trashme/Z/py2.venv/lib/python2.7/site-packages/transaction/_manager.py", line 257, in commit
            return self.manager.commit()
          File "/home/kirr/tmp/trashme/Z/py2.venv/lib/python2.7/site-packages/transaction/_manager.py", line 134, in commit
            return self.get().commit()
          File "/home/kirr/tmp/trashme/Z/py2.venv/lib/python2.7/site-packages/transaction/_transaction.py", line 282, in commit
            reraise(t, v, tb)
          File "/home/kirr/tmp/trashme/Z/py2.venv/lib/python2.7/site-packages/transaction/_transaction.py", line 273, in commit
            self._commitResources()
          File "/home/kirr/tmp/trashme/Z/py2.venv/lib/python2.7/site-packages/transaction/_transaction.py", line 456, in _commitResources
            reraise(t, v, tb)
          File "/home/kirr/tmp/trashme/Z/py2.venv/lib/python2.7/site-packages/transaction/_transaction.py", line 430, in _commitResources
            rm.commit(self)
          File "/home/kirr/tmp/trashme/Z/py2.venv/lib/python2.7/site-packages/ZODB/Connection.py", line 497, in commit
            self._commit(transaction)
          File "/home/kirr/tmp/trashme/Z/py2.venv/lib/python2.7/site-packages/ZODB/Connection.py", line 546, in _commit
            self._store_objects(ObjectWriter(obj), transaction)
          File "/home/kirr/tmp/trashme/Z/py2.venv/lib/python2.7/site-packages/ZODB/Connection.py", line 578, in _store_objects
            p = writer.serialize(obj)  # This calls __getstate__ of obj
          File "/home/kirr/tmp/trashme/Z/py2.venv/lib/python2.7/site-packages/ZODB/serialize.py", line 430, in serialize
            return self._dump(meta, obj.__getstate__())
          File "/home/kirr/tmp/trashme/Z/py2.venv/lib/python2.7/site-packages/ZODB/serialize.py", line 439, in _dump
            self._p.dump(state)
        PicklingError: Can't pickle <class '__main__.PInt'>: attribute lookup __main__.PInt failed
    
    /reviewed-by @jerome
    /reviewed-on nexedi/pygolang!15
    2351dd27
__init__.py 18.7 KB