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

Different behavior on cpython and pypy interpreter. #23

Open
Rinoahu opened this issue Nov 20, 2021 · 2 comments
Open

Different behavior on cpython and pypy interpreter. #23

Rinoahu opened this issue Nov 20, 2021 · 2 comments

Comments

@Rinoahu
Copy link

Rinoahu commented Nov 20, 2021

The following code works on cpython (I only test it on 3.7). However, it doesn't work on pypy 3:

from cppyy.gbl.std import vector as Vector
import numpy as np
from array import array

x = Vector['int'](range(10))
x = Vector['int'](np.arange(10))
x = Vector['int'](array('i', range(10)))
@Rinoahu Rinoahu changed the title differents behavior on cpython and pypy interpreter. different behavior on cpython and pypy interpreter. Nov 20, 2021
@Rinoahu Rinoahu changed the title different behavior on cpython and pypy interpreter. Different behavior on cpython and pypy interpreter. Nov 20, 2021
@wlav
Copy link
Owner

wlav commented Nov 21, 2021

Yes, unfortunately, the PyPy port is behind the CPython one and several more recent features are missing in the former. (Combo of development for PyPy being harder and more folks asking for CPython.)

@Rinoahu
Copy link
Author

Rinoahu commented Apr 4, 2023

A solution https://foss.heptapod.net/pypy/pypy/-/issues/3607

import cppyy

def pythonize_vector(klass, name):
    if 'vector<' in name:
        old_init = klass.__init__
        def pyinit(self, *args):
            if len(args) == 1:
                try:
                    iter(args[0])
                    old_init(self)
                    self. Reserve(len(args[0]))
                    self += args[0]
                    return
                except TypeError:
                    pass
            return old_init(self, *args)
        klass.__init__ = pyinit

cppyy.py.add_pythonization(pythonize_vector, 'std')

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