# Binding regex from Python

**URL:** <https://users.rust-lang.org/t/binding-regex-from-python/7253>\
**Category:** help\
**Created:** [September 10, 2016, 11:38pm UTC](https://users.rust-lang.org/t/binding-regex-from-python/7253 "2016-09-10T23:38:24Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![davidblewett](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/davidblewett/32/1090_2.png) [@davidblewett](https://users.rust-lang.org/u/davidblewett)\
**Post date:** [September 10, 2016, 11:38pm UTC](https://users.rust-lang.org/t/binding-regex-from-python/7253/1 "2016-09-10T23:38:24Z")

</div>

I have a need for using the regex crate from Python (translation: Python \<-\> C \<-\> Rust). I've tried 3 different approaches, and they all have their downsides.

My first attempt was using cffi and manually writing simple FFI functions in Rust [1].  
This attempt I'm not confident in, as I'm sure there are gotchas that I'm not aware of.

After doing more research, I discovered the existing C binding (rure).  
My second attempt was to write a simple wrapper of rure, to make it easy to build a binary wheel for Python [2].  
This attempt failed, and I'm not familiar enough with Rust (or C) to figure out why.  
I tried several variations of wrapping rure, but they all failed in similar ways (either Python's cffi responding with symbols missing from the library, or null pointers for structs).

My 3rd attempt was to forego the wrapper and instead include the small amount of Python code directly in the regex-capi crate [3].  
I don't mind maintaining a fork in my personal GitHub account for this glue code, but that seems flaky.  
I also don't want to foist maintaining Python bindings on the regex maintainers (as it seems rure-go was split out to a distinct repo [4])

1. [https://github.com/davidblewett/rusty-re](https://github.com/davidblewett/rusty-re)
2. [https://github.com/davidblewett/rure-python](https://github.com/davidblewett/rure-python)
3. [https://github.com/rust-lang-nursery/regex/compare/master...davidblewett:rure-python?expand=1](https://github.com/rust-lang-nursery/regex/compare/master...davidblewett:rure-python?expand=1)
4. [https://github.com/BurntSushi/rure-go](https://github.com/BurntSushi/rure-go)

---

<div class="post-metadata">

**Author:** ![occupy\_paul\_st](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/occupy_paul_st/32/16694_2.png) [@occupy\_paul\_st](https://users.rust-lang.org/u/occupy_paul_st)\
**Post date:** [September 1, 2020, 6:06pm UTC](https://users.rust-lang.org/t/binding-regex-from-python/7253/2 "2020-09-01T18:06:36Z")

</div>

Thanks for mentioning this! I can see that you are continuing to maintain the [rure-python](https://github.com/davidblewett/rure-python) project!

Now that pyO3 is available on stable Rust, I had the thought: would it make sense to use pyO3 to generate these bindings? But I'm thinking that using rure is probably better, because the hand-crafted bindings are probably already well-designed.

---

<div class="post-metadata">

**Author:** ![BurntSushi](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/burntsushi/32/135_2.png) [@BurntSushi](https://users.rust-lang.org/u/BurntSushi)\
**Post date:** [September 1, 2020, 7:05pm UTC](https://users.rust-lang.org/t/binding-regex-from-python/7253/3 "2020-09-01T19:05:21Z")

</div>

What benefit would pyO3 bring given that the bindings for rure have already been written?

---

<div class="post-metadata">

**Author:** ![occupy\_paul\_st](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/occupy_paul_st/32/16694_2.png) [@occupy\_paul\_st](https://users.rust-lang.org/u/occupy_paul_st)\
**Post date:** [September 1, 2020, 9:35pm UTC](https://users.rust-lang.org/t/binding-regex-from-python/7253/4 "2020-09-01T21:35:15Z")

</div>

Maybe it would make it slightly easier to add new bindings or something, but... yes, I suspect that there would be basically no benefit, and probably several drawbacks. 😀

---

<div class="post-metadata">

**Author:** ![BurntSushi](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/burntsushi/32/135_2.png) [@BurntSushi](https://users.rust-lang.org/u/BurntSushi)\
**Post date:** [September 1, 2020, 11:33pm UTC](https://users.rust-lang.org/t/binding-regex-from-python/7253/5 "2020-09-01T23:33:12Z")

</div>

Those are my thoughts as well. And the regex C API experiences very little change. 🙂

---

<div class="post-metadata">

**Author:** ![davidblewett](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/davidblewett/32/1090_2.png) [@davidblewett](https://users.rust-lang.org/u/davidblewett)\
**Post date:** [September 3, 2020, 2:23pm UTC](https://users.rust-lang.org/t/binding-regex-from-python/7253/6 "2020-09-03T14:23:52Z")

</div>

The largest problem with `rure-python` has been building and distributing binary Python wheels. It's not difficult if you maintain your own private PyPI repo (for say distributing your work-specific code), but if you rely solely on the public one it is a pain. It's difficult to build `manylinux` compatible wheels. If PyO3 has mechanisms to automate that, it could be a win.

---

<div class="post-metadata">

**Author:** ![steffahn](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/steffahn/32/47569_2.png) [@steffahn](https://users.rust-lang.org/u/steffahn)\
**Post date:** [January 12, 2023, 9:02am UTC](https://users.rust-lang.org/t/binding-regex-from-python/7253/7 "2023-01-12T09:02:11Z")

</div>


