Skip to content

Commit

Permalink
prevent import py2/3 module in py3/2. removed exec wrapper from hclass
Browse files Browse the repository at this point in the history
  • Loading branch information
Robin De Schepper committed Mar 20, 2021
1 parent 0286454 commit e447ce7
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 9 deletions.
8 changes: 7 additions & 1 deletion share/lib/python/neuron/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,10 +233,16 @@ def test_rxd(exitOnError=True):
# using the idiom self.basemethod = self.baseattr('methodname')
# ------------------------------------------------------------------------------

import sys, types

if sys.version_info[0] == 2:
from neuron.hclass2 import hclass
# Add hclass3, the py3 variant of hclass2 to the module cache so that hclass3.py
# is never read by the Python 2 interpreter and can contain Python 3 only syntax.
sys.modules["neuron.hclass3"] = types.ModuleType("neuron.hclass3")
else:
from neuron.hclass3 import hclass
from neuron.hclass3 import hclass, nonlocal_hclass
sys.modules["neuron.hclass2"] = types.ModuleType("neuron.hclass2")

# global list of paths already loaded by load_mechanisms
nrn_dll_loaded = []
Expand Down
7 changes: 3 additions & 4 deletions share/lib/python/neuron/hclass2.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
# h.anyclass methods may be overridden. If so the base method can be called
# using the idiom self.basemethod = self.baseattr('methodname')
# ------------------------------------------------------------------------------

from neuron import h, hoc
import nrn

#avoid syntax error if compiled by python 3
exec('''

class MetaHocObject(type):
"""Provides Exception for Inheritance of multiple HocObject"""
def __new__(cls, name, bases, attrs):
Expand All @@ -22,7 +22,7 @@ def __new__(cls, name, bases, attrs):
raise TypeError('Multiple Inheritance of HocObject in %s' % name
+ ' through %s not allowed' % ','.join(m))
#note that join(b.__name__ for b in m) is not valid for Python 2.3

return type.__new__(cls, name, bases, attrs)

def hclass(c):
Expand All @@ -38,4 +38,3 @@ def __new__(cls, *args, **kwds):
return hoc.HocObject.__new__(cls, *args, **kwds2)
setattr(hc, 'htype', c)
return hc
''')
7 changes: 3 additions & 4 deletions share/lib/python/neuron/hclass3.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
# h.anyclass methods may be overridden. If so the base method can be called
# using the idiom self.basemethod = self.baseattr('methodname')
# ------------------------------------------------------------------------------

from neuron import h, hoc
import nrn

#avoid syntax error if compiled by python 2
exec('''

class MetaHocObject(type):
"""Provides Exception for Inheritance of multiple HocObject"""
def __new__(cls, name, bases, attrs):
Expand All @@ -22,7 +22,7 @@ def __new__(cls, name, bases, attrs):
raise TypeError('Multiple Inheritance of HocObject in %s' % name
+ ' through %s not allowed' % ','.join(m))
#note that join(b.__name__ for b in m) is not valid for Python 2.3

return type.__new__(cls, name, bases, attrs)

def hclass(c):
Expand All @@ -38,4 +38,3 @@ def __new__(cls, *args, **kwds):
return hoc.HocObject.__new__(cls, *args, **kwds2)
setattr(hc, 'htype', c)
return hc
''')

0 comments on commit e447ce7

Please sign in to comment.