Skip to content

Commit

Permalink
fix: revert to silent fallback to pure-Python build (#93)
Browse files Browse the repository at this point in the history
Closes #92.

Add a fix for a segfault on MacOS 11 ("Big Sur"):

- In `extend`, parse prior 'crc' value to 'extend' as 'unsigned long'

'PyArg_ParseTuple' w/ 'k' flag seems not to assign cleanly into a 'uint32_t'
variable on at least some MacOS / Python combinations.

Closes #96.
  • Loading branch information
tseaver committed Sep 7, 2021
1 parent 96f8132 commit 789a420
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 12 deletions.
32 changes: 23 additions & 9 deletions setup.py
Expand Up @@ -57,22 +57,30 @@ def run(self):
)


if CRC32C_PURE_PYTHON:
def build_pure_python():
setuptools.setup(
packages=["google_crc32c"],
package_dir={"": "src"},
ext_modules=[],
)


def build_c_extension():
setuptools.setup(
packages=["google_crc32c"],
package_dir={"": "src"},
ext_modules=[module],
cmdclass={"build_ext": BuildExtWithDLL},
)


if CRC32C_PURE_PYTHON:
build_pure_python()
else:
try:
setuptools.setup(
packages=["google_crc32c"],
package_dir={"": "src"},
ext_modules=[module],
cmdclass={"build_ext": BuildExtWithDLL},
)
build_c_extension()
except SystemExit:
if "CRC32C_PURE_PYTHON" not in os.environ:
if CRC32C_PURE_PYTHON_EXPLICIT:
# If build / install fails, it is likely a compilation error with
# the C extension: advise user how to enable the pure-Python
# build.
Expand All @@ -81,4 +89,10 @@ def run(self):
"To enable building / installing a pure-Python-only version, "
"set 'CRC32C_PURE_PYTHON=1' in the environment."
)
raise
raise

logging.info(
"Compiling the C Extension for the crc32c library failed. "
"Falling back to pure Python build."
)
build_pure_python()
7 changes: 4 additions & 3 deletions src/google_crc32c/_crc32c.c
Expand Up @@ -6,14 +6,15 @@
static PyObject *
_crc32c_extend(PyObject *self, PyObject *args)
{
unsigned long crc_input;
uint32_t crc;
const char *chunk;
Py_ssize_t length;

if (!PyArg_ParseTuple(args, "ks#", &crc, &chunk, &length))
if (!PyArg_ParseTuple(args, "ky#", &crc_input, &chunk, &length))
return NULL;

crc = crc32c_extend(crc, (const uint8_t*)chunk, length);
crc = crc32c_extend((uint32_t)crc_input, (const uint8_t*)chunk, length);

return PyLong_FromUnsignedLong(crc);
}
Expand All @@ -26,7 +27,7 @@ _crc32c_value(PyObject *self, PyObject *args)
const char *chunk;
Py_ssize_t length;

if (!PyArg_ParseTuple(args, "s#", &chunk, &length))
if (!PyArg_ParseTuple(args, "y#", &chunk, &length))
return NULL;

crc = crc32c_value((const uint8_t*)chunk, length);
Expand Down

0 comments on commit 789a420

Please sign in to comment.