Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

qdiv (possibly) modifies its first argument #150

Closed
videlec opened this issue Jun 22, 2017 · 3 comments
Closed

qdiv (possibly) modifies its first argument #150

videlec opened this issue Jun 22, 2017 · 3 comments

Comments

@videlec
Copy link
Contributor

videlec commented Jun 22, 2017

>>> import gmpy2
>>> a = gmpy2.mpq(1,2)
>>> b = gmpy2.mpq(2,3)
>>> gmpy2.qdiv(a,b)
mpq(3,4)
>>> a   # a gest modified!!!
mpq(3,4)

The reason is that the function GMPy_MPQ_From_Rational does not return a copy of its argument if it is already a rational.

(see also sympy issue #12753)

@casevh
Copy link
Collaborator

casevh commented Jun 22, 2017

The following code in gmpy2_mpq_misc.c:

mpq_div(tempx->q, tempx->q, tempy->q);
Py_DECREF((PyObject*)tempy);

should be changed to something like:

tempresult = (PyObject*)GMPy_MPZ_New(context);
mpq_div(tempresult->q, tempx->q, tempy->q);

< do stuff >

Py_DECREF(tempresult);

Obviously needs error checking and other changes.

I can work on a complete fix in the next couple of days.

@videlec
Copy link
Contributor Author

videlec commented Jun 22, 2017

Thanks. Note that sympy is wrongly using qdiv and I will provide an easier fix for them.

@casevh
Copy link
Collaborator

casevh commented Jul 8, 2017

I've pushed a fix. Thanks for the report.

@casevh casevh closed this as completed Jul 8, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants