[cython/cython] [ENH] Documentation to pass arguments in setup.py ? (Issue #4797)

0 views
Skip to first unread message

hexxone

unread,
May 18, 2022, 7:16:16 AM5/18/22
to cython/cython, Subscribed

Is your feature request related to a problem? Please describe.

I am trying to build a pyarmor-protected file.

In my setup.py, I would like to pass the arguments -3 --lenient --keep-going to the cythonize() call.

So far my file looks like this:

# setup.py
try:
    from setuptools import setup
    from setuptools.extension import Extension
except ImportError:
    from distutils.core import setup
    from distutils.extension import Extension

import os
from Cython.Build import cythonize
from os.path import abspath

libImport = abspath("../some/libs")
pyImport = abspath("../some/include")

cflags = '-3 --lenient --keep-going'

ext_modules = [
    Extension(
        "pytransform",
        sources=[ "src/dist/obf/pytransform.py" ],
        extra_compile_args=cflags
    ),
    Extension(
        "Calculate",
        sources=[ "src/dist/obf/calculate.py" ],
        extra_compile_args=cflags
    )
]

setup(
    name="Calculate",
    description= 'Calculate',
    author= 'Test',
    author_email= 'admin@test-net,
    install_requires= ['cython'],
    packages= ['calculate'], 
    ext_modules= cythonize(ext_modules),
    include_dirs= [libImport, pyImport],
)

but the "cflags" arguments dont get recognized?

I end up with the error:
dist\calculate.py:4:0: undeclared name not builtin: __pyarmor__

Describe the solution you'd like

Add documentation on how to properly pass in arguments from code.

The existing page does not say anything about it: http://docs.cython.org/en/latest/src/quickstart/build.html

Describe alternatives you've considered

This works:
cythonize -3 -k --lenient .\dist\calculate.py

But manually building from the command-line is annoying if you have a lot of files.


Reply to this email directly, view it on GitHub, or unsubscribe.
You are receiving this because you are subscribed to this thread.Message ID: <cython/cython/issues/4797@github.com>

da-woods

unread,
May 19, 2022, 2:12:31 PM5/19/22
to cython/cython, Subscribed

There's a bit more detail in the user-guide section of the docs, although admittedly I don't think it covers this very well. Improvements are welcome.

--linient enables Options.error_on_unknown_names = False and Options.error_on_uninitialized = False so you'd be best advised to set it that way.

-3 is equivalent to defaulting to the language_level compiler directive to 3. Personally I'd set it with #cython: language_level = 3 in the source file, since the correct setting is defined by the source file.

I'm not sure about -k - my personal view is that it's rarely useful but that shouldn't stop you from using it if you want


Reply to this email directly, view it on GitHub, or unsubscribe.

You are receiving this because you are subscribed to this thread.Message ID: <cython/cython/issues/4797/1132029694@github.com>

scoder

unread,
May 19, 2022, 5:34:38 PM5/19/22
to cython/cython, Subscribed

-k stems from the time when I tried to compile CPython's standard library with Cython, and it would fail to compile several of the modules (sometimes even because of bugs, funny enough). That has become less of an issue these days, so yes, -k probably isn't very useful any more.


Reply to this email directly, view it on GitHub, or unsubscribe.

You are receiving this because you are subscribed to this thread.Message ID: <cython/cython/issues/4797/1132227416@github.com>

scoder

unread,
May 19, 2022, 5:37:53 PM5/19/22
to cython/cython, Subscribed

Note that cflags are really C flags, i.e. options for the C compiler and not for Cython.

You can look at Cython's command line parser to see what each of the options does and how to mimic it programmatically.


Reply to this email directly, view it on GitHub, or unsubscribe.

You are receiving this because you are subscribed to this thread.Message ID: <cython/cython/issues/4797/1132230907@github.com>

scoder

unread,
May 19, 2022, 5:40:18 PM5/19/22
to cython/cython, Subscribed

Actually, linking to the command line parser from the docs is probably a fair way to document the programmatic options in relation to the command line arguments.


Reply to this email directly, view it on GitHub, or unsubscribe.

You are receiving this because you are subscribed to this thread.Message ID: <cython/cython/issues/4797/1132232690@github.com>

hexxone

unread,
May 20, 2022, 4:59:56 AM5/20/22
to cython/cython, Subscribed

--linient enables Options.error_on_unknown_names = False and Options.error_on_uninitialized = False so you'd be best advised to set it that way.

Note that cflags (i.e. extra_compile_args) are really C flags, i.e. options for the C compiler and not for Cython.

You can look at Cython's command line parser to see what each of the Cython options does and how to mimic it programmatically.

Oh well, thank you two for the hints!

I probably scrolled by the "Options" section at some point, but just didn't realise, the error_on_ -options are related to the --lenient argument.

-3 is equivalent to defaulting to the language_level compiler directive to 3. Personally I'd set it with #cython: language_level = 3 in the source file, since the correct setting is defined by the source file.

This usually seems like a good advice aswell, but since I obfuscate the scripts with pyarmor before compiling, I don't have much control over the source in this case :(.

I'm not sure about -k - my personal view is that it's rarely useful but that shouldn't stop you from using it if you want

And you're right, the option was recommended by the "pyarmor" devs but it seems to work without it aswell.


Reply to this email directly, view it on GitHub, or unsubscribe.

You are receiving this because you are subscribed to this thread.Message ID: <cython/cython/issues/4797/1132655989@github.com>

hexxone

unread,
May 20, 2022, 5:00:52 AM5/20/22
to cython/cython, Subscribed

For anyone interested, now my setup.py looks like this:

try:
    from setuptools import setup
    from setuptools.extension import Extension
except ImportError:
    from distutils.core import setup
    from distutils.extension import Extension

from os.path import abspath

from Cython.Build import cythonize
from Cython.Compiler import Options

libImport = abspath("../some/libs")
pyImport = abspath("../some/include")

Options.error_on_unknown_names = False
Options.error_on_uninitialized = False
Options.language_level = 3

ex_mod = [
	Extension(
		"pytransform",
		sources=[ "dist/pytransform.py" ],
	),
	Extension(
		"calculate",
		sources=[ "dist/calculate.py" ],
	)
]

setup(
    name="Calculate",
    description= 'Calculate',
    author= 'Test',
    author_email= 'admin@test-net,
    install_requires= ['cython'],
    packages= ['calculate'], 
    ext_modules= cythonize(ext_mod),
    include_dirs= [libImport, pyImport],
)


Reply to this email directly, view it on GitHub, or unsubscribe.

You are receiving this because you are subscribed to this thread.Message ID: <cython/cython/issues/4797/1132656864@github.com>

hexxone

unread,
May 19, 2024, 2:36:51 AM5/19/24
to cython/cython, Subscribed

Closed #4797 as completed.


Reply to this email directly, view it on GitHub, or unsubscribe.

You are receiving this because you are subscribed to this thread.Message ID: <cython/cython/issue/4797/issue_event/12857687665@github.com>

Reply all
Reply to author
Forward
0 new messages