cffi.api.CDefError: parse error Python - python-3.x

Using cffi, I'm trying to call some Rust function in Python code.
Here is the python code:
from cffi import FFI
def rust(solution):
ffi = FFI()
lib = ffi.dlopen("./libnorm.so")
ffi.cdef('float norm(float**)')
return lib.norm(solution)
solution = [[1, 1], [1, 2], [1, 3], [1, 4], [1, 5], [1, 6]]
print(rust(solution))
and this is the rust code:
#! [crate_type = "dylib"]
#[no_mangle]
pub extern fn norm(array: Vec< Vec<f64>>) -> f64 {
return array.len() as f64
}
I compiled the lib with:
rustc --crate-type=dylib norm.rs
But when I run my python script, I get this stack:
Traceback (most recent call last):
File "/usr/lib64/python3.5/site-packages/cffi/cparser.py", line 260, in _parse
ast = _get_parser().parse(csource)
File "/usr/lib/python3.5/site-packages/pycparser/c_parser.py", line 151, in parse
debug=debuglevel)
File "/usr/lib/python3.5/site-packages/pycparser/ply/yacc.py", line 331, in parse
return self.parseopt_notrack(input, lexer, debug, tracking, tokenfunc)
File "/usr/lib/python3.5/site-packages/pycparser/ply/yacc.py", line 1181, in parseopt_notrack
tok = call_errorfunc(self.errorfunc, errtoken, self)
File "/usr/lib/python3.5/site-packages/pycparser/ply/yacc.py", line 193, in call_errorfunc
r = errorfunc(token)
File "/usr/lib/python3.5/site-packages/pycparser/c_parser.py", line 1723, in p_error
self._parse_error('At end of input', self.clex.filename)
File "/usr/lib/python3.5/site-packages/pycparser/plyparser.py", line 55, in _parse_error
raise ParseError("%s: %s" % (coord, msg))
pycparser.plyparser.ParseError: : At end of input
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "main.py", line 35, in <module>
print(rust(solution))
File "main.py", line 28, in rust
ffi.cdef('float norm(float**)')
File "/usr/lib64/python3.5/site-packages/cffi/api.py", line 105, in cdef
self._cdef(csource, override=override, packed=packed)
File "/usr/lib64/python3.5/site-packages/cffi/api.py", line 119, in _cdef
self._parser.parse(csource, override=override, **options)
File "/usr/lib64/python3.5/site-packages/cffi/cparser.py", line 299, in parse
self._internal_parse(csource)
File "/usr/lib64/python3.5/site-packages/cffi/cparser.py", line 304, in _internal_parse
ast, macros, csource = self._parse(csource)
File "/usr/lib64/python3.5/site-packages/cffi/cparser.py", line 262, in _parse
self.convert_pycparser_error(e, csource)
File "/usr/lib64/python3.5/site-packages/cffi/cparser.py", line 291, in convert_pycparser_error
raise api.CDefError(msg)
cffi.api.CDefError: parse error
: At end of input
Am I missing something?
I tried several version for: ffi.cdef('float norm(float**)'). Like ffi.cdef('float norm(Vec< Vec<float>>)') or ffi.cdef('float norm(float[][])')
But it doesn't work too.
Thanks for future replying.

Related

Pyinstaller gives an error KeyError: 'CALL_FUNCTION'

when i try to compile my code i get this error
i have wheel module and all necessary modules
with the code itself, everything is also in order, I tried to compile other programs and had the same error, I did not find anything about this
Traceback (most recent call last):
File "<frozen runpy>", line 198, in _run_module_as_main
File "<frozen runpy>", line 88, in _run_code
File "C:\Users\user\AppData\Local\Programs\Python\Python311\Scripts\pyinstaller.exe\__main__.py", line 7, in <module>
File "C:\Users\user\AppData\Local\Programs\Python\Python311\Lib\site-packages\PyInstaller\__main__.py", line 107, in run
parser = generate_parser()
^^^^^^^^^^^^^^^^^
File "C:\Users\user\AppData\Local\Programs\Python\Python311\Lib\site-packages\PyInstaller\__main__.py", line 78, in generate_parser
import PyInstaller.building.build_main
File "C:\Users\user\AppData\Local\Programs\Python\Python311\Lib\site-packages\PyInstaller\building\build_main.py", line 35, in <module>
from PyInstaller.depend import bindepend
File "C:\Users\user\AppData\Local\Programs\Python\Python311\Lib\site-packages\PyInstaller\depend\bindepend.py", line 26, in <module>
from PyInstaller.depend import dylib, utils
File "C:\Users\user\AppData\Local\Programs\Python\Python311\Lib\site-packages\PyInstaller\depend\utils.py", line 33, in <module>
from PyInstaller.depend import bytecode
File "C:\Users\user\AppData\Local\Programs\Python\Python311\Lib\site-packages\PyInstaller\depend\bytecode.py", line 95, in <module>
_call_function_bytecode = bytecode_regex(rb"""
^^^^^^^^^^^^^^^^^^^^
File "C:\Users\user\AppData\Local\Programs\Python\Python311\Lib\site-packages\PyInstaller\depend\bytecode.py", line 60, in bytecode_regex
pattern = re.sub(
^^^^^^^
File "C:\Users\user\AppData\Local\Programs\Python\Python311\Lib\re\__init__.py", line 185, in sub
return _compile(pattern, flags).sub(repl, string, count)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\user\AppData\Local\Programs\Python\Python311\Lib\site-packages\PyInstaller\depend\bytecode.py", line 62, in <lambda>
lambda m: _instruction_to_regex(m[1].decode()),
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\user\AppData\Local\Programs\Python\Python311\Lib\site-packages\PyInstaller\depend\bytecode.py", line 40, in _instruction_to_regex
return re.escape(bytes([dis.opmap[x]]))
~~~~~~~~~^^^
KeyError: 'CALL_FUNCTION'
Seeing the same thing here, and it appears to be related to the use of Python 3.11 (which is pre-release). Using 3.10 avoids this issue.

Error when trying to run via multiprocessing in Python 3

The following code works fine
[process_data(item, data_frame_list[item]) for item in data_frame_list if data_frame_list[item].shape[0] > 5]
I'm trying to convert this code to run in parallel
pool_obj = multiprocessing.Pool()
[pool_obj.map(process_data,item, data_frame_list[item]) for item in data_frame_list if data_frame_list[item].shape[0] > 5]
This results in errors
Traceback (most recent call last):
File "/home/pyuser/PycharmProjects/project_sample/testyard_2.py", line 425, in <module>
[pool_obj.map(process_data,item, data_frame_list[item]) for item in data_frame_list if data_frame_list[item].shape[0] > 5]
File "/home/pyuser/PycharmProjects/project_sample/testyard_2.py", line 425, in <listcomp>
[pool_obj.map(process_data,item, data_frame_list[item]) for item in data_frame_list if data_frame_list[item].shape[0] > 5]
File "/usr/lib/python3.8/multiprocessing/pool.py", line 364, in map
return self._map_async(func, iterable, mapstar, chunksize).get()
File "/usr/lib/python3.8/multiprocessing/pool.py", line 485, in _map_async
result = MapResult(self, chunksize, len(iterable), callback,
File "/usr/lib/python3.8/multiprocessing/pool.py", line 797, in __init__
if chunksize <= 0:
File "/home/pyuser/PycharmProjects/project_sample/venv/lib/python3.8/site-packages/pandas/core/ops/common.py", line 69, in new_method
return method(self, other)
File "/home/pyuser/PycharmProjects/project_sample/venv/lib/python3.8/site-packages/pandas/core/arraylike.py", line 44, in __le__
return self._cmp_method(other, operator.le)
File "/home/pyuser/PycharmProjects/project_sample/venv/lib/python3.8/site-packages/pandas/core/frame.py", line 6849, in _cmp_method
new_data = self._dispatch_frame_op(other, op, axis=axis)
File "/home/pyuser/PycharmProjects/project_sample/venv/lib/python3.8/site-packages/pandas/core/frame.py", line 6888, in _dispatch_frame_op
bm = self._mgr.apply(array_op, right=right)
File "/home/pyuser/PycharmProjects/project_sample/venv/lib/python3.8/site-packages/pandas/core/internals/managers.py", line 325, in apply
applied = b.apply(f, **kwargs)
File "/home/pyuser/PycharmProjects/project_sample/venv/lib/python3.8/site-packages/pandas/core/internals/blocks.py", line 382, in apply
result = func(self.values, **kwargs)
File "/home/pyuser/PycharmProjects/project_sample/venv/lib/python3.8/site-packages/pandas/core/ops/array_ops.py", line 284, in comparison_op
res_values = comp_method_OBJECT_ARRAY(op, lvalues, rvalues)
File "/home/pyuser/PycharmProjects/project_sample/venv/lib/python3.8/site-packages/pandas/core/ops/array_ops.py", line 73, in comp_method_OBJECT_ARRAY
result = libops.scalar_compare(x.ravel(), y, op)
File "pandas/_libs/ops.pyx", line 107, in pandas._libs.ops.scalar_compare
TypeError: '<=' not supported between instances of 'str' and 'int'
I'm not able to work out what is incorrect with what I've done. Could I please request some guidance?
Used a different library that has easier usage. All is working now.
from joblib import Parallel, delayed
import multiprocessing
Parallel(n_jobs=multiprocessing.cpu_count())(delayed(process_data)(item, data_frame_list[item]) for item in data_frame_list if data_frame_list[item].shape[0] > 5)

Python, pyinstaller raised an error as follows

when I ran the application, it raised error as follows.
My python version is 3.8.10 and pyinstaller version is 4.4.
How to deal with it?
Traceback (most recent call last):
File "run.py", line 360, in huaban.run.MainWorkPanel.start_download
File "run.py", line 594, in huaban.run.MainWorkPanel.real_run
File "scrapy\crawler.py", line 280, in __init__
File "scrapy\crawler.py", line 152, in __init__
File "scrapy\crawler.py", line 146, in _get_spider_loader
File "scrapy\spiderloader.py", line 67, in from_settings
File "scrapy\spiderloader.py", line 24, in __init__
File "scrapy\spiderloader.py", line 51, in _load_all_spiders
File "scrapy\utils\misc.py", line 83, in walk_modules
File "C:\soft\VirtualenvFiles\huaban\Lib\site-packages\PyInstaller\hooks\rthooks\pyi_rth_pkgutil.py", line 71, in _pyi_pkgutil_iter_modules
assert pkg_path.startswith(SYS_PREFIX)
TypeError: startswith first arg must be str or a tuple of str, not PureWindowsPath
Fixed in PyInstaller 4.5.
See here.

Text to speech sapi5 python 3.8.6

Whenever I try to run my text to speech program in python 3.8.6
it gives me error
My Code
import pyttsx3
engine = pyttsx3.init('sapi5')
voices = engine.getProperty('voices')
print(voices[0].id)
print(voices)
engine.setProperty('voices', voices[0].id)
engine.say("hello")
engine.runAndWait()
Error
Traceback (most recent call last):
File "C:/Users/Admin/PycharmProjects/automating_Practices/Chat_Bot_PRogram/Voice_chatbot.py", line 20, in <module>
voices = engine.getProperty('voices')
File "C:\Python\Python386\lib\site-packages\pyttsx3\engine.py", line 146, in getProperty
return self.proxy.getProperty(name)
File "C:\Python\Python386\lib\site-packages\pyttsx3\driver.py", line 173, in getProperty
return self._driver.getProperty(name)
File "C:\Python\Python386\lib\site-packages\pyttsx3\drivers\sapi5.py", line 88, in getProperty
return [self._toVoice(attr) for attr in self._tts.GetVoices()]
File "C:\Python\Python386\lib\site-packages\pyttsx3\drivers\sapi5.py", line 88, in <listcomp>
return [self._toVoice(attr) for attr in self._tts.GetVoices()]
File "C:\Python\Python386\lib\site-packages\pyttsx3\drivers\sapi5.py", line 77, in _toVoice
return Voice(attr.Id, attr.GetDescription())
_ctypes.COMError: (-2147200966, None, (None, None, None, 0, None))
Please can anyone help me with this

Error while creating a service using nameko for Pysnmp get_request--Attempted "nameko_entrypoints" operation on ASN.1 schema object

I am new to pysnmp and nameko. I have been assignment a work to create a service in nameko framework to perform snmp get_request using pysnmp library.
Below is the code i have tried
from pysnmp.hlapi import *
from nameko.rpc import rpc
class GreetingService(object):
name = "greeting_service"
#rpc
def getFunc(oid):
errorIndication, errorStatus, errorIndex, varBinds = next(
getCmd(SnmpEngine(),
CommunityData('public', mpModel=0),
UdpTransportTarget(('snmp.live.gambitcommunications.com', 161)),
ContextData(),
ObjectType(ObjectIdentity('SNMPv2-MIB', oid, 0)))
)
if errorIndication:
print(errorIndication)
elif errorStatus:
print('%s at %s' % (errorStatus.prettyPrint(),
errorIndex and varBinds[int(errorIndex) - 1][0] or '?'))
else:
for varBind in varBinds:
print(' = '.join([x.prettyPrint() for x in varBind]))
if __name__ == "__main__":
getFunc('sysName')
when i try to start the service using terminal with the following command
$ nameko run helloworld
I get the following error message.
syed#syed-ThinkPad-E480:~/Pysnmp$ nameko run helloworld
Traceback (most recent call last):
File "/home/syed/.local/bin/nameko", line 11, in <module>
sys.exit(main())
File "/home/syed/.local/lib/python3.7/site-packages/nameko/cli/main.py", line 112, in main
args.main(args)
File "/home/syed/.local/lib/python3.7/site-packages/nameko/cli/commands.py", line 110, in main
main(args)
File "/home/syed/.local/lib/python3.7/site-packages/nameko/cli/run.py", line 181, in main
import_service(path)
File "/home/syed/.local/lib/python3.7/site-packages/nameko/cli/run.py", line 71, in import_service
if inspect.getmembers(potential_service, is_entrypoint):
File "/usr/lib/python3.7/inspect.py", line 354, in getmembers
if not predicate or predicate(value):
File "/home/syed/.local/lib/python3.7/site-packages/nameko/cli/run.py", line 35, in is_entrypoint
return hasattr(method, ENTRYPOINT_EXTENSIONS_ATTR)
File "/home/syed/.local/lib/python3.7/site-packages/pyasn1/type/base.py", line 221, in __getattr__
raise error.PyAsn1Error('Attempted "%s" operation on ASN.1 schema object' % attr)
pyasn1.error.PyAsn1Error: Attempted "nameko_entrypoints" operation on ASN.1 schema object
Error in sys.excepthook:
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/apport_python_hook.py", line 63, in apport_excepthook
from apport.fileutils import likely_packaged, get_recent_crashes
File "/usr/lib/python3/dist-packages/apport/__init__.py", line 5, in <module>
from apport.report import Report
File "/usr/lib/python3/dist-packages/apport/report.py", line 30, in <module>
import apport.fileutils
File "/usr/lib/python3/dist-packages/apport/fileutils.py", line 23, in <module>
from apport.packaging_impl import impl as packaging
File "/usr/lib/python3/dist-packages/apport/packaging_impl.py", line 24, in <module>
import apt
File "/usr/lib/python3/dist-packages/apt/__init__.py", line 23, in <module>
import apt_pkg
ModuleNotFoundError: No module named 'apt_pkg'
Original exception was:
Traceback (most recent call last):
File "/home/syed/.local/bin/nameko", line 11, in <module>
sys.exit(main())
File "/home/syed/.local/lib/python3.7/site-packages/nameko/cli/main.py", line 112, in main
args.main(args)
File "/home/syed/.local/lib/python3.7/site-packages/nameko/cli/commands.py", line 110, in main
main(args)
File "/home/syed/.local/lib/python3.7/site-packages/nameko/cli/run.py", line 181, in main
import_service(path)
File "/home/syed/.local/lib/python3.7/site-packages/nameko/cli/run.py", line 71, in import_service
if inspect.getmembers(potential_service, is_entrypoint):
File "/usr/lib/python3.7/inspect.py", line 354, in getmembers
if not predicate or predicate(value):
File "/home/syed/.local/lib/python3.7/site-packages/nameko/cli/run.py", line 35, in is_entrypoint
return hasattr(method, ENTRYPOINT_EXTENSIONS_ATTR)
File "/home/syed/.local/lib/python3.7/site-packages/pyasn1/type/base.py", line 221, in __getattr__
raise error.PyAsn1Error('Attempted "%s" operation on ASN.1 schema object' % attr)
pyasn1.error.PyAsn1Error: Attempted "nameko_entrypoints" operation on ASN.1 schema object
Please help me out to understand weather what i have tried is correct way or else it is wrong.. If so how to rectify the mistake.
Any help will be appreciable.
Thanks in advance
It has something to do with the way how nameko hooks up your code...
It seems to try looking up nameko_entrypoints attribute at all objects it can find in your module eventually running into ASN.1 schema objects (which are sacred and should not be used for anything other than blueprinting purposes).
My suggestion would be to replace from pysnmp.hlapi import * with specific imports of pysnmp classes/functions you are using in your code. That should hopefully hide fragile pieces out of nameko's sight.

Resources