OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Discovering which argument caused an error with Python CFFI

  • Thread starter Thread starter jhaiduce
  • Start date Start date
J

jhaiduce

Guest
I'm using the Python CFFI module to wrap functions that have multiple arguments of the same type. If I pass the wrong type to one of the arguments, CFFI gives an error identifying actual type and the expected type, but not the variable name or position in the argument list.

For example, the following code takes two arguments of the same type, and passes the wrong type to one of those arguments:

Code:
import cffi

ffi=cffi.FFI()

ffi.cdef('double mult(double x, double y);')
lib=ffi.verify('double mult(double x, double y){return x*y;};')
result=lib.mult(2,'3')

(I know ffi.verify is deprecated; I don't use it in my production code but it results in a much simpler MWE)

The above code gives the following output:

Code:
Traceback (most recent call last):
  File "/Users/haiducek/Development/cffi_type_error/cffi_type_error.py", line 7, in <module>
    result=lib.mult(2,'3')
           ^^^^^^^^^^^^^^^
TypeError: must be real number, not str

Is there a way to get CFFI to tell me which argument of the called function was responsible for the TypeError?
<p>I'm using the Python CFFI module to wrap functions that have multiple arguments of the same type. If I pass the wrong type to one of the arguments, CFFI gives an error identifying actual type and the expected type, but not the variable name or position in the argument list.</p>
<p>For example, the following code takes two arguments of the same type, and passes the wrong type to one of those arguments:</p>
<pre><code>import cffi

ffi=cffi.FFI()

ffi.cdef('double mult(double x, double y);')
lib=ffi.verify('double mult(double x, double y){return x*y;};')
result=lib.mult(2,'3')
</code></pre>
<p>(I know ffi.verify is deprecated; I don't use it in my production code but it results in a much simpler MWE)</p>
<p>The above code gives the following output:</p>
<pre><code>Traceback (most recent call last):
File "/Users/haiducek/Development/cffi_type_error/cffi_type_error.py", line 7, in <module>
result=lib.mult(2,'3')
^^^^^^^^^^^^^^^
TypeError: must be real number, not str
</code></pre>
<p>Is there a way to get CFFI to tell me which argument of the called function was responsible for the TypeError?</p>
 

Latest posts

Top