PySCeS / pysces

The official PySCeS project source code repository.
https://pysces.github.io
Other
34 stars 10 forks source link

Deleting random funtions in PyscesModel.py breaks latest IPython console #24

Closed jmrohwer closed 7 years ago

jmrohwer commented 7 years ago

This (PyscesModel.py lines 53-58)

# this is incredibly crude but effectively masks off unsupported random functions
del random.setstate, random.division, random.getstate,
del random.randrange, random.Random, random.choice
del random.sample, random.shuffle, random.jumpahead
del random.SystemRandom, random.WichmannHill, random.triangular
# used by functions random.NV_MAGICCONST, random.SG_MAGICCONST, random.BPF, random.RECIP_BPF

is a very bad idea. For example, it breaks the latest IPython console, which relies on random.shuffle:

(pysces) jr@jr-yoga900:Data $ ipython
Python 2.7.12 (default, Nov 19 2016, 06:48:10) 
Type "copyright", "credits" or "license" for more information.

IPython 5.4.1 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

In [1]: import pysces

In [2]:                                                                                                                             
Traceback (most recent call last):
  File "/home/jr/.virtualenvs/pysces/bin/ipython", line 11, in <module>
    sys.exit(start_ipython())
  File "/home/jr/.virtualenvs/pysces/local/lib/python2.7/site-packages/IPython/__init__.py", line 119, in start_ipython
    return launch_new_instance(argv=argv, **kwargs)
  File "/home/jr/.virtualenvs/pysces/local/lib/python2.7/site-packages/traitlets/config/application.py", line 658, in launch_instance
    app.start()
  File "/home/jr/.virtualenvs/pysces/local/lib/python2.7/site-packages/IPython/terminal/ipapp.py", line 355, in start
    self.shell.mainloop()
  File "/home/jr/.virtualenvs/pysces/local/lib/python2.7/site-packages/IPython/terminal/interactiveshell.py", line 493, in mainloop
    self.interact()
  File "/home/jr/.virtualenvs/pysces/local/lib/python2.7/site-packages/IPython/terminal/interactiveshell.py", line 476, in interact
    code = self.prompt_for_code()
  File "/home/jr/.virtualenvs/pysces/local/lib/python2.7/site-packages/IPython/terminal/interactiveshell.py", line 370, in prompt_for_code
    pre_run=self.pre_prompt, reset_current_buffer=True)
  File "/home/jr/.virtualenvs/pysces/local/lib/python2.7/site-packages/prompt_toolkit/interface.py", line 415, in run
    self.eventloop.run(self.input, self.create_eventloop_callbacks())
  File "/home/jr/.virtualenvs/pysces/local/lib/python2.7/site-packages/prompt_toolkit/eventloop/posix.py", line 157, in run
    random.shuffle(tasks)
AttributeError: 'module' object has no attribute 'shuffle'

If you suspect this is an IPython bug, please report it at:
    https://github.com/ipython/ipython/issues
or send an email to the mailing list at ipython-dev@python.org

You can print a more detailed traceback right now with "%tb", or use "%debug"
to interactively debug it.

Extra-detailed tracebacks for bug-reporting purposes can be enabled via:
    %config Application.verbose_crash=True

Any better ways to fix it? (The jupyter notebook still works fine, but generally it is a bad, bad idea to delete functions from python core modules.

jmrohwer commented 7 years ago

Breaks multiprocessing (and thus parallel scanning) as well:

Exception in thread Thread-1:
Traceback (most recent call last):
  File "/usr/lib/python2.7/threading.py", line 801, in __bootstrap_inner
    self.run()
  File "/usr/lib/python2.7/threading.py", line 754, in run
    self.__target(*self.__args, **self.__kwargs)
  File "/usr/lib/python2.7/multiprocessing/pool.py", line 326, in _handle_workers
    pool._maintain_pool()
  File "/usr/lib/python2.7/multiprocessing/pool.py", line 230, in _maintain_pool
    self._repopulate_pool()
  File "/usr/lib/python2.7/multiprocessing/pool.py", line 223, in _repopulate_pool
    w.start()
  File "/usr/lib/python2.7/multiprocessing/process.py", line 130, in start
    self._popen = Popen(self)
  File "/usr/lib/python2.7/multiprocessing/forking.py", line 125, in __init__
    random.seed()
  File "/usr/lib/python2.7/random.py", line 118, in seed
    super(Random, self).seed(a)
NameError: global name 'Random' is not defined
bgoli commented 7 years ago

Aargh I forgot about this import module irritation. The best would probably be to kill of the masking, which to be honest, I didn't like anyway

jmrohwer commented 7 years ago

OK are you going to fix and commit?

bgoli commented 7 years ago

Done, sometimes Python's referencing drives me nuts. This is simply so people don't use unparseable stuff in the input file so is not so important, you'll just get a parse error which is ok.

bgoli commented 7 years ago

Actually, seeing as this is all just Python I can create a PySCeS version of random that only has the funcitons I want in it and import that, then we should be fine.

bgoli commented 7 years ago

Ok I implemented the above, this shouldn't interfere with the standard modules