Skip to content

(from visualstudio-docs repo) "Call the DLL from Python" example not working #6931

@WilliamAntonRohm

Description

@WilliamAntonRohm

Possible bug in Call the DLL from Python, per this visualstudio-docs issue.

Here is that issue's original content:

When trying to run the Call the DLL from Python section, nothing gets printed on screen and the console simply prompts me to press any key. Upon closing the terminal, I get a Visual Studio error window, with the following description:

Could not convert to integer 3221225477, Path 'exitCode'. Value was either too large or too small for an Int32

For reference, I have only followed the PyBind11 section, and my .py and .cpp files are shown below:

  • main.py
from random import random
from time import perf_counter
from superfastcode import fast_tanh

COUNT = 500000  # Change this value depending on the speed of your computer
DATA = [(random() - 0.5) * 3 for _ in range(COUNT)]

e = 2.7182818284590452353602874713527

def sinh(x):
    return (1 - (e ** (-2 * x))) / (2 * (e ** -x))

def cosh(x):
    return (1 + (e ** (-2 * x))) / (2 * (e ** -x))

def tanh(x):
    tanh_x = sinh(x) / cosh(x)
    return tanh_x

def test(fn, name):
    start = perf_counter()
    result = fn(DATA)
    duration = perf_counter() - start
    print('{} took {:.3f} seconds\n\n'.format(name, duration))

    for d in result:
        assert -1 <= d <= 1, " incorrect values"

if __name__ == "__main__":
    print('Running benchmarks with COUNT = {}'.format(COUNT))

    test(lambda d: [tanh(x) for x in d], '[tanh(x) for x in d] (Python implementation)')

    test(lambda d: [fast_tanh(x) for x in d], '[fast_tanh(x) for x in d] (PyBind11 C++ extension)')
  • module.cpp:
#include <pybind11/pybind11.h>

#include <Windows.h>
#include <cmath>

const double e = 2.7182818284590452353602874713527;

double sinh_impl(double x) {
    return (1 - pow(e, (-2 * x))) / (2 * pow(e, -x));
}

double cosh_impl(double x) {
    return (1 + pow(e, (-2 * x))) / (2 * pow(e, -x));
}

double tanh_impl(double x) {
    return sinh_impl(x) / cosh_impl(x);
}

namespace py = pybind11;

PYBIND11_MODULE(superfastcode, m) {
    m.def("fast_tanh", &tanh_impl, R"pbdoc(
        Compute a hyperbolic tangent of a single argument expressed in radians.
    )pbdoc");

#ifdef VERSION_INFO
    m.attr("__version__") = VERSION_INFO;
#else
    m.attr("__version__") = "dev";
#endif
}

I should not that the above error occurs during the import statement from superfastcode import fast_tanh.

Any clues? Thanks much in advance.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions