# \[SOLVED\] Porting Python module to CPython: how to translate global scope?

**URL:** <https://discuss.python.org/t/solved-porting-python-module-to-cpython-how-to-translate-global-scope/1866>\
**Category:** Python Help\
**Tags:** solved\
**Created:** [June 15, 2019, 3:05am UTC](https://discuss.python.org/t/solved-porting-python-module-to-cpython-how-to-translate-global-scope/1866 "2019-06-15T03:05:35Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![ExperimentFailed](https://avatars.discourse-cdn.com/v4/letter/e/ad7895/32.png) [@ExperimentFailed](https://discuss.python.org/u/ExperimentFailed)\
**Post date:** [June 15, 2019, 3:05am UTC](https://discuss.python.org/t/solved-porting-python-module-to-cpython-how-to-translate-global-scope/1866/1 "2019-06-15T03:05:35Z")

</div>

Hello,

First post woo!

Let me start by pointing out that beyond very simple examples, I’m jumping off the deep end with C. I have some C# & Swift experience prior (also consider myself ~master but shy of Doctorate in pure Python) which has all helped me pick up on this pretty quickly, and I’ve already ported several functions (surprised myself with how fast I picked it up even). However, I’m stuck on this one issue pretty hard and not quite sure even how to google it (obviously have tried).

My module/extension needs some global dictionaries avaliable both in the module, and in Python.

To be very specific, from [Bitbucket](https://bitbucket.org/experimentfailed/testcraft/src/working/world.py), I’m trying to express lines 36, 40, 44, and 47 via the CPython API. However, just putting e.g.:

`PyObject* worldBlocks = PyDict_New();`

in the global scope of my C does not work. The compiler throws:

```
world_test.c:17:25: error: initializer element is not constant

```

If I put the same code in a function that returns void, I get the same error. If I put it in a function that returns PyObject, then it works. This behavior baffles me, probably due to my awkward language background and overall lack of knowledge in C. Actually, I more or less understand why the above doesn’t work in C global scope, but not quite sure about why it doesn’t in a generic void function (discovered that on accident)… Anyway, still not sure how to get the dicts I need into the this mystical global land that certainly has to exist…

I’m lost on this one, which feels weird with how far I’ve come. I ported my terrain generator ([Bitbucket](https://bitbucket.org/experimentfailed/testcraft/src/working/terrain_gen/)) with relative ease!

Thanks for reading and for any insight!

---

<div class="post-metadata">

**Author:** ![pitrou](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/pitrou/32/28_2.png) [@pitrou](https://discuss.python.org/u/pitrou)\
**Post date:** [June 15, 2019, 9:55am UTC](https://discuss.python.org/t/solved-porting-python-module-to-cpython-how-to-translate-global-scope/1866/2 "2019-06-15T09:55:18Z")

</div>

> [@ExperimentFailed](#):
>
> in the global scope of my C does not work. The compiler throws:

Indeed. C is very restrictive about what you can initialize a global variable with. Basically you can only use a _compile-time_ constant as initializer.

> [@ExperimentFailed](#):
>
> If I put the same code in a function that returns void, I get the same error.

I doubt it. Please post the full code of that function.

---

<div class="post-metadata">

**Author:** ![ExperimentFailed](https://avatars.discourse-cdn.com/v4/letter/e/ad7895/32.png) [@ExperimentFailed](https://discuss.python.org/u/ExperimentFailed)\
**Post date:** [June 15, 2019, 5:54pm UTC](https://discuss.python.org/t/solved-porting-python-module-to-cpython-how-to-translate-global-scope/1866/3 "2019-06-15T17:54:40Z")

</div>

Sorry, I guess you’re right - Popping those lines into a function that returns void works perfectly fine. Got my wires crossed somehow there, or perhaps made another error last night while I was trying things out.

My bad! I’ve tried a few more things with that in mind, but not getting anywhere. Tried adding a get function for each global dictionary, but it segfaults after a couple of calls. I was burned out so haven’t looked into that any further yet.

Still feels like I’m overlooking something. Much more googling lead me [here](https://pythonextensionpatterns.readthedocs.io/en/latest/module_globals.html), which may have the solution but it’s late and I’m tired so will read up on that tomorrow.

Cheers.

---

<div class="post-metadata">

**Author:** ![pitrou](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/pitrou/32/28_2.png) [@pitrou](https://discuss.python.org/u/pitrou)\
**Post date:** [June 16, 2019, 9:01am UTC](https://discuss.python.org/t/solved-porting-python-module-to-cpython-how-to-translate-global-scope/1866/4 "2019-06-16T09:01:30Z")

</div>

Unless you’re a C expert and have a special need for writing your extension module directly in C, I would recommend [Cython](https://cython.org/).

---

<div class="post-metadata">

**Author:** ![mjpieters](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/mjpieters/32/710_2.png) [@mjpieters](https://discuss.python.org/u/mjpieters)\
**Post date:** [June 16, 2019, 1:01pm UTC](https://discuss.python.org/t/solved-porting-python-module-to-cpython-how-to-translate-global-scope/1866/5 "2019-06-16T13:01:33Z")

</div>

If you want to create _module-level globals_, do so during [module initialization](https://docs.python.org/3/c-api/module.html#initializing-c-modules).

In your init function, create the dict, then set it as a module attribute with [`PyModule_AddObject`](https://docs.python.org/3/c-api/module.html#c.PyModule_AddObject). This is how the [`os.environ` dictionary is added to the `os` module](https://github.com/python/cpython/blob/45e0411eee023b21acf1615e995d26058d66c0f1/Modules/posixmodule.c#L14341-L14346) for example (albeit that the `PyDict_New()` call is made in a helper function).

None of this requires a C global.

---

<div class="post-metadata">

**Author:** ![ExperimentFailed](https://avatars.discourse-cdn.com/v4/letter/e/ad7895/32.png) [@ExperimentFailed](https://discuss.python.org/u/ExperimentFailed)\
**Post date:** [June 16, 2019, 2:42pm UTC](https://discuss.python.org/t/solved-porting-python-module-to-cpython-how-to-translate-global-scope/1866/6 "2019-06-16T14:42:35Z")

</div>

Thanks for the replies. I need access to the dictionaries in BOTH Python and C.

I did try Cython initially but I spent a week just to break even on performance. It became clear I’d need to not only learn many C concepts but also Cython syntax… Picking up C has been quite a bit easier and the returns on my time way better.

I think I"m on the right track, defining my dicts as C globals, initializing them in my PyMODINIT\_FUNC function, then writing “get” functions (that also has to increment the reference counter) get them into Python.

The weird thing now, though, is that my test.py segfaults, but if I run the interpreter via gdb, I can’t reproduce the issue and everything seems to work perfectly.

---

<div class="post-metadata">

**Author:** ![mjpieters](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/mjpieters/32/710_2.png) [@mjpieters](https://discuss.python.org/u/mjpieters)\
**Post date:** [June 16, 2019, 9:00pm UTC](https://discuss.python.org/t/solved-porting-python-module-to-cpython-how-to-translate-global-scope/1866/7 "2019-06-16T21:00:25Z")

</div>

> [@ExperimentFailed](#):
>
> Thanks for the replies. I need access to the dictionaries in BOTH Python and C.

Yes, which is why I pointed you to a real-world example. The `os.environ` object is exactly such a dictionary, it is created in a C-level module and accessible from Python too.

---

<div class="post-metadata">

**Author:** ![ExperimentFailed](https://avatars.discourse-cdn.com/v4/letter/e/ad7895/32.png) [@ExperimentFailed](https://discuss.python.org/u/ExperimentFailed)\
**Post date:** [June 16, 2019, 9:19pm UTC](https://discuss.python.org/t/solved-porting-python-module-to-cpython-how-to-translate-global-scope/1866/8 "2019-06-16T21:19:44Z")

</div>

Sorry, I was on my phone when I replied, and overlooked that. I figured it out anyway and just came back to update that I was able to implement it. Edit: Thought it solved my segfault, but a bit more testing brought it back.

Thanks again!!

---

<div class="post-metadata">

**Author:** ![jkloth](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/jkloth/32/253_2.png) [@jkloth](https://discuss.python.org/u/jkloth)\
**Post date:** [June 16, 2019, 9:47pm UTC](https://discuss.python.org/t/solved-porting-python-module-to-cpython-how-to-translate-global-scope/1866/9 "2019-06-16T21:47:45Z")

</div>

A quick look through your repository [[https://bitbucket.org/experimentfailed/testcraft/src/working/world\_c\_test/world\_test.c](https://bitbucket.org/experimentfailed/testcraft/src/working/world_c_test/world_test.c)] you are missing several `Py_INCREF` calls which would more than likely be the cause of your segfaults.

Every call to `PyTuple_SetItem` steals a reference from the item that is set:

`PyTuple_SetItem(return_tuple, 0, Py_None) // must incref Py_None`

Also, the returned values from `PyArg_ParseTuple` are _borrowed_ references. They too will need incref’ing when set.

`PyTuple_GetItem` also returns _borrowed_ references.

---

<div class="post-metadata">

**Author:** ![ExperimentFailed](https://avatars.discourse-cdn.com/v4/letter/e/ad7895/32.png) [@ExperimentFailed](https://discuss.python.org/u/ExperimentFailed)\
**Post date:** [June 16, 2019, 10:52pm UTC](https://discuss.python.org/t/solved-porting-python-module-to-cpython-how-to-translate-global-scope/1866/10 "2019-06-16T22:52:12Z")

</div>

Thank you… I had a sneaking suspicion it might have something to do with the reference counters, but since I couldn’t reproduce the issue with gdb, was feeling a bit helpless to solve it (I had actually already solved one such issue using gdb so thought it would show me where I’m making rookie mistakes like that).

I will go over everything again with that info in mind.

**Update** : Success! Martijn Pieters’ tip helped me get the global dicts setup properly and Jeremy Kloth’s tip solved my segfault. This is probably not the best way to do this, but I basically just printed before and after reference counts throughout the code, to look for unexpected results. Turns out, a PyListObject I was adding to a dictionary was losing reference, and need an Py\_INCREF. For now, seems like the ref counts stay correct (actually, maybe some areas to Py\_DECREF)!

Cheers and many thanks to all who replied…Everyone taught me something valuable!
