Skip to content

Commit

Permalink
Remove support for Cython 2
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Buesch <[email protected]>
  • Loading branch information
mbuesch committed Apr 2, 2020
1 parent 27ecca6 commit e985f4c
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 63 deletions.
2 changes: 0 additions & 2 deletions DEVELOPMENT.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ To run a code statement coverage trace during unit testing the option `./tests/r
In order to support both compiling the Awlsim core with Cython and running the same code in a plain Python interpreter without compilation, a method to patch the files is required. The setup.py scrips patch each .py source file and create a .pyx file before starting Cython compilation. The Cython patching mechanism rewrites all `import`s to import the compiled Cython modules instead of the plain Python modules. In addition to that some special comments are provided as hint to the Cython patcher:

* `#@cy` : Enable (un-comment) this line during Cython patching.
* `#@cy2` : Enable (un-comment) this line during Cython patching, if compiling for Python 2.
* `#@cy3` : Enable (un-comment) this line during Cython patching, if compiling for Python 3.
* `#@cy-posix` : Enable (un-comment) this line during Cython patching, if compiling for a Posix platform.
* `#@cy-win` : Enable (un-comment) this line during Cython patching, if compiling for a Windows platform.
* `#@nocy` : Disable (comment) this line during Cython patching.
Expand Down
6 changes: 1 addition & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -227,15 +227,11 @@ The following environment variables control Awlsim's build (setup.py) behavior:

* `AWLSIM_CYTHON_BUILD`<br />
`=0` Do not build any Cython modules. (default on non-Posix)<br />
`=1` Build Cython modules.<br />
`=2` Build Cython modules only, if setup.py is being executed by Python 2.<br />
`=3` Build Cython modules only, if setup.py is being executed by Python 3. (default on Posix)<br />
`=1` Build Cython modules. (default on Posix)<br />

* `AWLSIM_CYTHON_PARALLEL`<br />
`=0` Do not use parallel compilation for Cython modules.<br />
`=1` Invoke multiple compilers in parallel (faster on multicore). (default)<br />
`=2` Invoke multiple compilers only, if setup.py is being executed by Python 2.<br />
`=3` Invoke multiple compilers only, if setup.py is being executed by Python 3.<br />

* `AWLSIM_PROFILE`<br />
`=0` Do not enable profiling support in compiled Cython modules. (default)<br />
Expand Down
6 changes: 2 additions & 4 deletions awlsim/common/datatypehelpers.pxd.in
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,10 @@ cdef uint32_t getMSB(uint32_t value)


cdef inline _Bool isInteger(object value):
#@cy3 return isinstance(value, int)
#@cy2 return isinstance(value, (int, long))
return isinstance(value, int)

cdef inline _Bool isString(object value):
#@cy3 return isinstance(value, str)
#@cy2 return isinstance(value, (unicode, str))
return isinstance(value, str)

cdef inline uint32_t len_u32(object obj):
return <uint32_t>(min(len(obj), <object>0xFFFFFFFFu))
Expand Down
8 changes: 4 additions & 4 deletions awlsim/core/memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -769,11 +769,11 @@ def store(self, offset, memObj): #@nocy
def __len__(self):
return self.__dataBytesLen

def __bool__(self): #@cy3
return bool(self.__dataBytesLen) #@cy3
def __bool__(self):
return bool(self.__dataBytesLen)

def __nonzero__(self): #@cy2
return bool(self.__dataBytesLen) #@cy2
def __nonzero__(self): #@nocy
return bool(self.__dataBytesLen) #@nocy

def __repr__(self): #@nocov
#@cy cdef list ret
Expand Down
45 changes: 9 additions & 36 deletions misc/setup_cython.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#
# Cython patcher
# v1.20
# v1.21
#
# Copyright (C) 2012-2019 Michael Buesch <[email protected]>
# Copyright (C) 2012-2020 Michael Buesch <[email protected]>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -77,14 +77,10 @@ def makedirs(path, mode=0o755):
raise e

def hashFile(path):
if sys.version_info[0] < 3:
ExpectedException = IOError
else:
ExpectedException = FileNotFoundError
try:
with open(path, "rb") as fd:
return hashlib.sha1(fd.read()).hexdigest()
except ExpectedException as e:
except FileNotFoundError as e:
return None

def __fileopIfChanged(fromFile, toFile, fileops):
Expand Down Expand Up @@ -167,12 +163,6 @@ def uncomment(line, removeStr):
elif "#@cy-win" in stripLine:
if _isWindows:
line = uncomment(line, "#@cy-win")
elif "#@cy2" in stripLine:
if sys.version_info[0] < 3:
line = uncomment(line, "#@cy2")
elif "#@cy3" in stripLine:
if sys.version_info[0] >= 3:
line = uncomment(line, "#@cy3")
elif "#@cy" in stripLine:
line = uncomment(line, "#@cy")

Expand Down Expand Up @@ -229,15 +219,6 @@ def uncomment(line, removeStr):
if "#@nocy" in stripLine:
line = "#" + line

# Comment all lines containing #@cyX
# for the not matching version.
if sys.version_info[0] < 3:
if "#@cy3" in stripLine:
line = "#" + line
else:
if "#@cy2" in stripLine:
line = "#" + line

# Comment all lines containing #@cy-posix/win
# for the not matching platform.
if _isPosix:
Expand Down Expand Up @@ -377,7 +358,7 @@ def registerCythonModule(baseDir, sourceModName):
# Warn about unused variables?
"warn.unused" : False,
# Set language version
"language_level" : 2 if sys.version_info[0] < 3 else 3,
"language_level" : 3,
},
define_macros=[
("CYTHON_TRACE", str(int(profileEnabled))),
Expand Down Expand Up @@ -408,6 +389,11 @@ def cythonBuildPossible():

_cythonPossible = False

if sys.version_info[0] < 3:
print("WARNING: Could not build the CYTHON modules: "
"Cython 2 not supported. Please use Cython 3.")
return False

try:
import Cython.Compiler.Options
# Omit docstrings in cythoned modules.
Expand All @@ -431,19 +417,6 @@ def cythonBuildPossible():
_cythonPossible = True
return True

if sys.version_info[0] < 3:
# Cython2 build libraries need method pickling
# for parallel build.
def unpickle_method(fname, obj, cls):
# Ignore MRO. We don't seem to inherit methods.
return cls.__dict__[fname].__get__(obj, cls)
def pickle_method(m):
return unpickle_method, (m.im_func.__name__,
m.im_self,
m.im_class)
import copy_reg, types
copy_reg.pickle(types.MethodType, pickle_method, unpickle_method)

def cyBuildWrapper(arg):
# This function does the same thing as the for-loop-body
# inside of Cython's build_ext.build_extensions() method.
Expand Down
18 changes: 6 additions & 12 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,11 @@
#
# AWLSIM_CYTHON_BUILD:
# 0 (default on non-Posix): Do not build any Cython modules.
# 1: Build Cython modules.
# 2: Build Cython modules only, if setup.py is being executed by Python 2.
# 3 (default on Posix): Build Cython modules only, if setup.py is being executed by Python 3.
# 1 (default on Posix): Build Cython modules.
#
# AWLSIM_CYTHON_PARALLEL:
# 0: Do not use parallel compilation for Cython modules.
# 1 (default): Invoke multiple compilers in parallel (faster on multicore).
# 2: Invoke multiple compilers only, if setup.py is being executed by Python 2.
# 3: Invoke multiple compilers only, if setup.py is being executed by Python 3.
#
# AWLSIM_PROFILE:
# 0 (default): Do not enable profiling support in compiled Cython modules.
Expand Down Expand Up @@ -65,16 +61,14 @@ def getEnvInt(name, default = 0):
return default

def getEnvBool(name, default = False):
return bool(getEnvInt(name, 1 if default else 0))
return getEnvInt(name, 1 if default else 0) > 0


fullBuild = getEnvBool("AWLSIM_FULL_BUILD")
buildCython = getEnvInt("AWLSIM_CYTHON_BUILD", 3 if isPosix else 0)
buildCython = ((buildCython == 1) or (buildCython == sys.version_info[0]))
setup_cython.parallelBuild = bool(getEnvInt("AWLSIM_CYTHON_PARALLEL", 1) == 1 or\
getEnvInt("AWLSIM_CYTHON_PARALLEL", 1) == sys.version_info[0])
setup_cython.profileEnabled = bool(getEnvInt("AWLSIM_PROFILE") > 0)
setup_cython.debugEnabled = bool(getEnvInt("AWLSIM_DEBUG_BUILD") > 0)
buildCython = getEnvBool("AWLSIM_CYTHON_BUILD", True if isPosix else False)
setup_cython.parallelBuild = getEnvBool("AWLSIM_CYTHON_PARALLEL", True)
setup_cython.profileEnabled = getEnvBool("AWLSIM_PROFILE")
setup_cython.debugEnabled = getEnvBool("AWLSIM_DEBUG_BUILD")


def pyCythonPatchLine(line):
Expand Down

0 comments on commit e985f4c

Please sign in to comment.