PTVS icon indicating copy to clipboard operation
PTVS copied to clipboard

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

Open WilliamAntonRohm opened this issue 3 years ago • 6 comments

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.

WilliamAntonRohm avatar Mar 21 '22 19:03 WilliamAntonRohm

Please send your findings on to the original poster Lyudmil.

WilliamAntonRohm avatar Mar 21 '22 19:03 WilliamAntonRohm

I have this problem as well when running this FinRL 0.3.4 tutorial with VS 2019, python 3.9 on Windows 10. https://github.com/AI4Finance-Foundation/FinRL/blob/master/tutorials/1-Introduction/FinRL_StockTrading_Fundamental.ipynb

jaysacco avatar May 05 '22 15:05 jaysacco

I am unable to reproduce this on VS 2022 regardless of Python version or bitness. Can you please clarify the exact version numbers involved?

int19h avatar Jul 06 '22 17:07 int19h

See my post on May 5 for version number info.

jaysacco avatar Jul 06 '22 17:07 jaysacco

Does it still reproduce for you if you try running the same solution with VS 2022?

int19h avatar Jul 06 '22 18:07 int19h

I haven’t updated VS yet and have no plans to at this point. I’m sure I will in the future though.

From: Pavel Minaev @.*** Sent: Wednesday, July 6, 2022 13:05 To: microsoft/PTVS Cc: jaysacco; Manual Subject: Re: [microsoft/PTVS] (from visualstudio-docs repo) "Call the DLL from Python" example not working (Issue #6931)

Does it still reproduce for you if you try running the same solution with VS 2022?

— Reply to this email directly, view it on GitHub https://github.com/microsoft/PTVS/issues/6931#issuecomment-1176523003 , or unsubscribe https://github.com/notifications/unsubscribe-auth/AGBBAPMGZ2NMPM7FJLVIHGTVSXDGTANCNFSM5RIVSQVA . You are receiving this because you are subscribed to this thread.Image removed by sender.Message ID: @.***>

jaysacco avatar Jul 06 '22 21:07 jaysacco

Closing old issue. If this is still a problem, please reopen with the information requested. thanks

judej avatar Aug 30 '22 22:08 judej

This still occurs with Visual Studio 2022 (17.3.5), Python 3.913, and TensorFlow 2.10.0

ctraina avatar Oct 08 '22 22:10 ctraina

yes on VS 2022

Microsoft Visual Studio Community 2022 Version 17.4.3 VisualStudio.17.Release/17.4.3+33205.214

Python; Python 3.9 (64-bit)

image

whyrv avatar Dec 17 '22 14:12 whyrv